|
35 | 35 | #include <QStandardPaths> |
36 | 36 | #include <QDebug> |
37 | 37 |
|
38 | | -#if (PY_VERSION_HEX < 0x03000000) |
39 | | -#define PYOBJ2QSTRING(obj) PyString_AsString( obj ) |
40 | | -#elif (PY_VERSION_HEX < 0x03030000) |
41 | | -#define PYOBJ2QSTRING(obj) QString::fromUtf8( PyBytes_AsString(PyUnicode_AsUTF8String( obj ) ) ) |
42 | | -#else |
43 | | -#define PYOBJ2QSTRING(obj) QString::fromUtf8( PyUnicode_AsUTF8( obj ) ) |
44 | | -#endif |
45 | | - |
46 | 38 | PyThreadState* _mainState; |
47 | 39 |
|
48 | 40 | QgsPythonUtilsImpl::QgsPythonUtilsImpl() |
@@ -98,11 +90,7 @@ bool QgsPythonUtilsImpl::checkSystemImports() |
98 | 90 | // we store here paths in unicode strings |
99 | 91 | // the str constant will contain utf8 code (through runString) |
100 | 92 | // so we call '...'.decode('utf-8') to make a unicode string |
101 | | -#if (PY_VERSION_HEX < 0x03000000) |
102 | | - pluginpaths << '"' + p + "\".decode('utf-8')"; |
103 | | -#else |
104 | 93 | pluginpaths << '"' + p + '"'; |
105 | | -#endif |
106 | 94 | } |
107 | 95 | pluginpaths << homePluginsPath(); |
108 | 96 | pluginpaths << '"' + pluginsPath() + '"'; |
@@ -133,21 +121,12 @@ bool QgsPythonUtilsImpl::checkSystemImports() |
133 | 121 | return false; |
134 | 122 | } |
135 | 123 | } |
136 | | -#if (PY_VERSION_HEX < 0x03000000) |
137 | | - // import Qt bindings |
138 | | - if ( !runString( "from PyQt4 import QtCore, QtGui", |
139 | | - QObject::tr( "Couldn't load PyQt." ) + '\n' + QObject::tr( "Python support will be disabled." ) ) ) |
140 | | - { |
141 | | - return false; |
142 | | - } |
143 | | -#else |
144 | 124 | // import Qt bindings |
145 | 125 | if ( !runString( QStringLiteral( "from PyQt5 import QtCore, QtGui" ), |
146 | 126 | QObject::tr( "Couldn't load PyQt." ) + '\n' + QObject::tr( "Python support will be disabled." ) ) ) |
147 | 127 | { |
148 | 128 | return false; |
149 | 129 | } |
150 | | -#endif |
151 | 130 |
|
152 | 131 | // import QGIS bindings |
153 | 132 | QString error_msg = QObject::tr( "Couldn't load PyQGIS." ) + '\n' + QObject::tr( "Python support will be disabled." ); |
@@ -384,11 +363,7 @@ QString QgsPythonUtilsImpl::getTraceback() |
384 | 363 | PyErr_Fetch( &type, &value, &traceback ); |
385 | 364 | PyErr_NormalizeException( &type, &value, &traceback ); |
386 | 365 |
|
387 | | -#if (PY_VERSION_HEX < 0x03000000) |
388 | | - const char* iomod = "cStringIO"; |
389 | | -#else |
390 | 366 | const char* iomod = "io"; |
391 | | -#endif |
392 | 367 |
|
393 | 368 | modStringIO = PyImport_ImportModule( iomod ); |
394 | 369 | if ( !modStringIO ) |
@@ -421,16 +396,10 @@ QString QgsPythonUtilsImpl::getTraceback() |
421 | 396 | TRACEBACK_FETCH_ERROR( "getvalue() failed." ); |
422 | 397 |
|
423 | 398 | /* And it should be a string all ready to go - duplicate it. */ |
424 | | - if ( ! |
425 | | -#if (PY_VERSION_HEX < 0x03000000) |
426 | | - PyString_Check( obResult ) |
427 | | -#else |
428 | | - PyUnicode_Check( obResult ) |
429 | | -#endif |
430 | | - ) |
| 399 | + if ( !PyUnicode_Check( obResult ) ) |
431 | 400 | TRACEBACK_FETCH_ERROR( "getvalue() did not return a string" ); |
432 | 401 |
|
433 | | - result = PYOBJ2QSTRING( obResult ); |
| 402 | + result = QString::fromUtf8( PyUnicode_AsUTF8( obResult ) ); |
434 | 403 |
|
435 | 404 | done: |
436 | 405 |
|
@@ -459,24 +428,16 @@ QString QgsPythonUtilsImpl::getTypeAsString( PyObject* obj ) |
459 | 428 | if ( !obj ) |
460 | 429 | return QString(); |
461 | 430 |
|
462 | | -#if (PY_VERSION_HEX < 0x03000000) |
463 | | - if ( PyClass_Check( obj ) ) |
| 431 | + if ( PyType_Check( obj ) ) |
464 | 432 | { |
465 | | - QgsDebugMsg( "got class" ); |
466 | | - return QString( PyString_AsString((( PyClassObject* )obj )->cl_name ) ); |
| 433 | + QgsDebugMsg( "got type" ); |
| 434 | + return QString((( PyTypeObject* )obj )->tp_name ); |
467 | 435 | } |
468 | 436 | else |
469 | | -#endif |
470 | | - if ( PyType_Check( obj ) ) |
471 | | - { |
472 | | - QgsDebugMsg( "got type" ); |
473 | | - return QString((( PyTypeObject* )obj )->tp_name ); |
474 | | - } |
475 | | - else |
476 | | - { |
477 | | - QgsDebugMsg( "got object" ); |
478 | | - return PyObjectToQString( obj ); |
479 | | - } |
| 437 | + { |
| 438 | + QgsDebugMsg( "got object" ); |
| 439 | + return PyObjectToQString( obj ); |
| 440 | + } |
480 | 441 | } |
481 | 442 |
|
482 | 443 | bool QgsPythonUtilsImpl::getError( QString& errorClassName, QString& errorText ) |
@@ -534,41 +495,15 @@ QString QgsPythonUtilsImpl::PyObjectToQString( PyObject* obj ) |
534 | 495 | // check whether the object is already a unicode string |
535 | 496 | if ( PyUnicode_Check( obj ) ) |
536 | 497 | { |
537 | | - result = PYOBJ2QSTRING( obj ); |
| 498 | + result = QString::fromUtf8( PyUnicode_AsUTF8( obj ) ); |
538 | 499 | return result; |
539 | 500 | } |
540 | 501 |
|
541 | | -#if (PY_VERSION_HEX < 0x03000000) |
542 | | - // check whether the object is a classical (8-bit) string |
543 | | - if ( PyString_Check( obj ) ) |
544 | | - { |
545 | | - return QString::fromUtf8( PyString_AS_STRING( obj ) ); |
546 | | - } |
547 | | - |
548 | | - // it's some other type of object: |
549 | | - // convert object to Unicode string (equivalent to calling unicode(obj) ) |
550 | | - PyObject* obj_uni = PyObject_Unicode( obj ); // obj_uni is new reference |
551 | | - if ( obj_uni ) |
552 | | - { |
553 | | - // get utf-8 representation of unicode string (new reference) |
554 | | - PyObject* obj_utf8 = PyUnicode_AsUTF8String( obj_uni ); |
555 | | - // convert from utf-8 to QString |
556 | | - if ( obj_utf8 ) |
557 | | - result = QString::fromUtf8( PyString_AsString( obj_utf8 ) ); |
558 | | - else |
559 | | - result = "(qgis error)"; |
560 | | - |
561 | | - Py_XDECREF( obj_utf8 ); |
562 | | - Py_XDECREF( obj_uni ); |
563 | | - return result; |
564 | | - } |
565 | | -#endif |
566 | | - |
567 | 502 | // if conversion to Unicode failed, try to convert it to classic string, i.e. str(obj) |
568 | 503 | PyObject* obj_str = PyObject_Str( obj ); // new reference |
569 | 504 | if ( obj_str ) |
570 | 505 | { |
571 | | - result = PYOBJ2QSTRING( obj_str ); |
| 506 | + result = QString::fromUtf8( PyUnicode_AsUTF8( obj_str ) ); |
572 | 507 | Py_XDECREF( obj_str ); |
573 | 508 | return result; |
574 | 509 | } |
|
0 commit comments