Skip to content

Commit

Permalink
Followup 5b2646c to fix tests (refs #15256)
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Oct 7, 2016
1 parent 5b2646c commit 43fa88e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/python/qgspythonutilsimpl.cpp
Expand Up @@ -36,17 +36,17 @@
#include <QDebug>

#if (PY_VERSION_HEX < 0x03000000)
QString PYOBJ2QSTRING( PyObject* obj )
QString PY_UNICODE2QSTRING( PyObject* obj )
{
PyObject* utf8 = PyUnicode_AsUTF8String( obj );
QString result = utf8 ? QString::fromUtf8( PyString_AS_STRING( utf8 ) ) : "(qgis error)";
Py_XDECREF( utf8 );
return result;
}
#elif (PY_VERSION_HEX < 0x03030000)
#define PYOBJ2QSTRING(obj) QString::fromUtf8( PyBytes_AsString(PyUnicode_AsUTF8String( obj ) ) )
#define PY_UNICODE2QSTRING(obj) QString::fromUtf8( PyBytes_AsString(PyUnicode_AsUTF8String( obj ) ) )
#else
#define PYOBJ2QSTRING(obj) QString::fromUtf8( PyUnicode_AsUTF8( obj ) )
#define PY_UNICODE2QSTRING(obj) QString::fromUtf8( PyUnicode_AsUTF8( obj ) )
#endif

PyThreadState* _mainState;
Expand Down Expand Up @@ -415,7 +415,7 @@ QString QgsPythonUtilsImpl::getTraceback()
)
TRACEBACK_FETCH_ERROR( "getvalue() did not return a string" );

result = PYOBJ2QSTRING( obResult );
result = PyObjectToQString( obResult );

done:

Expand Down Expand Up @@ -519,7 +519,7 @@ QString QgsPythonUtilsImpl::PyObjectToQString( PyObject* obj )
// check whether the object is already a unicode string
if ( PyUnicode_Check( obj ) )
{
result = PYOBJ2QSTRING( obj );
result = PY_UNICODE2QSTRING( obj );
return result;
}

Expand All @@ -535,15 +535,7 @@ QString QgsPythonUtilsImpl::PyObjectToQString( PyObject* obj )
PyObject* obj_uni = PyObject_Unicode( obj ); // obj_uni is new reference
if ( obj_uni )
{
// get utf-8 representation of unicode string (new reference)
PyObject* obj_utf8 = PyUnicode_AsUTF8String( obj_uni );
// convert from utf-8 to QString
if ( obj_utf8 )
result = QString::fromUtf8( PyString_AsString( obj_utf8 ) );
else
result = "(qgis error)";

Py_XDECREF( obj_utf8 );
result = PY_UNICODE2QSTRING( obj_uni );
Py_XDECREF( obj_uni );
return result;
}
Expand All @@ -553,7 +545,11 @@ QString QgsPythonUtilsImpl::PyObjectToQString( PyObject* obj )
PyObject* obj_str = PyObject_Str( obj ); // new reference
if ( obj_str )
{
result = PYOBJ2QSTRING( obj_str );
#if (PY_VERSION_HEX < 0x03000000)
result = QString::fromUtf8( PyString_AS_STRING( obj ) );
#else
result = PY_UNICODE2QSTRING( obj_str );
#endif
Py_XDECREF( obj_str );
return result;
}
Expand Down
6 changes: 6 additions & 0 deletions tests/src/app/testqgisapppython.cpp
Expand Up @@ -89,6 +89,12 @@ void TestQgisAppPython::evalString()
QVERIFY( mQgisApp->mPythonUtils->evalString( "1+1", result ) );
QCOMPARE( result, QString( "2" ) );

// unicode handling test
QVERIFY( mQgisApp->mPythonUtils->evalString( QString::fromUtf8( "'čerešne'" ), result ) );
QCOMPARE( result, QString::fromUtf8( "čerešne" ) );
QVERIFY( mQgisApp->mPythonUtils->evalString( "unicode('\\xc4\\x8dere\\xc5\\xa1ne', encoding='utf8')", result ) );
QCOMPARE( result, QString::fromUtf8( "čerešne" ) );

//bad string
QVERIFY( !mQgisApp->mPythonUtils->evalString( "1+", result ) );
}
Expand Down

0 comments on commit 43fa88e

Please sign in to comment.