Skip to content

Commit 43fa88e

Browse files
committedOct 7, 2016
Followup 5b2646c to fix tests (refs #15256)
1 parent 5b2646c commit 43fa88e

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed
 

‎src/python/qgspythonutilsimpl.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@
3636
#include <QDebug>
3737

3838
#if (PY_VERSION_HEX < 0x03000000)
39-
QString PYOBJ2QSTRING( PyObject* obj )
39+
QString PY_UNICODE2QSTRING( PyObject* obj )
4040
{
4141
PyObject* utf8 = PyUnicode_AsUTF8String( obj );
4242
QString result = utf8 ? QString::fromUtf8( PyString_AS_STRING( utf8 ) ) : "(qgis error)";
4343
Py_XDECREF( utf8 );
4444
return result;
4545
}
4646
#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 ) ) )
4848
#else
49-
#define PYOBJ2QSTRING(obj) QString::fromUtf8( PyUnicode_AsUTF8( obj ) )
49+
#define PY_UNICODE2QSTRING(obj) QString::fromUtf8( PyUnicode_AsUTF8( obj ) )
5050
#endif
5151

5252
PyThreadState* _mainState;
@@ -415,7 +415,7 @@ QString QgsPythonUtilsImpl::getTraceback()
415415
)
416416
TRACEBACK_FETCH_ERROR( "getvalue() did not return a string" );
417417

418-
result = PYOBJ2QSTRING( obResult );
418+
result = PyObjectToQString( obResult );
419419

420420
done:
421421

@@ -519,7 +519,7 @@ QString QgsPythonUtilsImpl::PyObjectToQString( PyObject* obj )
519519
// check whether the object is already a unicode string
520520
if ( PyUnicode_Check( obj ) )
521521
{
522-
result = PYOBJ2QSTRING( obj );
522+
result = PY_UNICODE2QSTRING( obj );
523523
return result;
524524
}
525525

@@ -535,15 +535,7 @@ QString QgsPythonUtilsImpl::PyObjectToQString( PyObject* obj )
535535
PyObject* obj_uni = PyObject_Unicode( obj ); // obj_uni is new reference
536536
if ( obj_uni )
537537
{
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 );
547539
Py_XDECREF( obj_uni );
548540
return result;
549541
}
@@ -553,7 +545,11 @@ QString QgsPythonUtilsImpl::PyObjectToQString( PyObject* obj )
553545
PyObject* obj_str = PyObject_Str( obj ); // new reference
554546
if ( obj_str )
555547
{
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
557553
Py_XDECREF( obj_str );
558554
return result;
559555
}

‎tests/src/app/testqgisapppython.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ void TestQgisAppPython::evalString()
8989
QVERIFY( mQgisApp->mPythonUtils->evalString( "1+1", result ) );
9090
QCOMPARE( result, QString( "2" ) );
9191

92+
// unicode handling test
93+
QVERIFY( mQgisApp->mPythonUtils->evalString( QString::fromUtf8( "'čerešne'" ), result ) );
94+
QCOMPARE( result, QString::fromUtf8( "čerešne" ) );
95+
QVERIFY( mQgisApp->mPythonUtils->evalString( "unicode('\\xc4\\x8dere\\xc5\\xa1ne', encoding='utf8')", result ) );
96+
QCOMPARE( result, QString::fromUtf8( "čerešne" ) );
97+
9298
//bad string
9399
QVERIFY( !mQgisApp->mPythonUtils->evalString( "1+", result ) );
94100
}

0 commit comments

Comments
 (0)
Please sign in to comment.