36
36
#include < QDebug>
37
37
38
38
#if (PY_VERSION_HEX < 0x03000000)
39
- QString PYOBJ2QSTRING ( PyObject* obj )
39
+ QString PY_UNICODE2QSTRING ( PyObject* obj )
40
40
{
41
41
PyObject* utf8 = PyUnicode_AsUTF8String ( obj );
42
42
QString result = utf8 ? QString::fromUtf8 ( PyString_AS_STRING ( utf8 ) ) : " (qgis error)" ;
43
43
Py_XDECREF ( utf8 );
44
44
return result;
45
45
}
46
46
#elif (PY_VERSION_HEX < 0x03030000)
47
- #define PYOBJ2QSTRING (obj ) QString::fromUtf8( PyBytes_AsString(PyUnicode_AsUTF8String( obj ) ) )
47
+ #define PY_UNICODE2QSTRING (obj ) QString::fromUtf8( PyBytes_AsString(PyUnicode_AsUTF8String( obj ) ) )
48
48
#else
49
- #define PYOBJ2QSTRING (obj ) QString::fromUtf8( PyUnicode_AsUTF8( obj ) )
49
+ #define PY_UNICODE2QSTRING (obj ) QString::fromUtf8( PyUnicode_AsUTF8( obj ) )
50
50
#endif
51
51
52
52
PyThreadState* _mainState;
@@ -415,7 +415,7 @@ QString QgsPythonUtilsImpl::getTraceback()
415
415
)
416
416
TRACEBACK_FETCH_ERROR ( " getvalue() did not return a string" );
417
417
418
- result = PYOBJ2QSTRING ( obResult );
418
+ result = PyObjectToQString ( obResult );
419
419
420
420
done:
421
421
@@ -519,7 +519,7 @@ QString QgsPythonUtilsImpl::PyObjectToQString( PyObject* obj )
519
519
// check whether the object is already a unicode string
520
520
if ( PyUnicode_Check ( obj ) )
521
521
{
522
- result = PYOBJ2QSTRING ( obj );
522
+ result = PY_UNICODE2QSTRING ( obj );
523
523
return result;
524
524
}
525
525
@@ -535,15 +535,7 @@ QString QgsPythonUtilsImpl::PyObjectToQString( PyObject* obj )
535
535
PyObject* obj_uni = PyObject_Unicode ( obj ); // obj_uni is new reference
536
536
if ( obj_uni )
537
537
{
538
- // get utf-8 representation of unicode string (new reference)
539
- PyObject* obj_utf8 = PyUnicode_AsUTF8String ( obj_uni );
540
- // convert from utf-8 to QString
541
- if ( obj_utf8 )
542
- result = QString::fromUtf8 ( PyString_AsString ( obj_utf8 ) );
543
- else
544
- result = " (qgis error)" ;
545
-
546
- Py_XDECREF ( obj_utf8 );
538
+ result = PY_UNICODE2QSTRING ( obj_uni );
547
539
Py_XDECREF ( obj_uni );
548
540
return result;
549
541
}
@@ -553,7 +545,11 @@ QString QgsPythonUtilsImpl::PyObjectToQString( PyObject* obj )
553
545
PyObject* obj_str = PyObject_Str ( obj ); // new reference
554
546
if ( obj_str )
555
547
{
556
- result = PYOBJ2QSTRING ( obj_str );
548
+ #if (PY_VERSION_HEX < 0x03000000)
549
+ result = QString::fromUtf8 ( PyString_AS_STRING ( obj ) );
550
+ #else
551
+ result = PY_UNICODE2QSTRING ( obj_str );
552
+ #endif
557
553
Py_XDECREF ( obj_str );
558
554
return result;
559
555
}
0 commit comments