Skip to content

Commit 26ce3cc

Browse files
committed
[py3] Some python3 compatibility fixes
1 parent dea8c9f commit 26ce3cc

File tree

3 files changed

+51
-34
lines changed

3 files changed

+51
-34
lines changed

src/core/auth/qgsauthmanager.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class CORE_EXPORT QgsAuthManager : public QObject
165165
void setScheduledAuthDbEraseRequestEmitted( bool emitted ) { mScheduledDbEraseRequestEmitted = emitted; }
166166

167167
/** Simple text tag describing authentication system for message logs */
168-
const QString authManTag() const { return smAuthManTag; }
168+
QString authManTag() const { return smAuthManTag; }
169169

170170
/** Instantiate and register existing C++ core authentication methods from plugins */
171171
bool registerCoreAuthMethods();

src/python/qgspythonutilsimpl.cpp

+48-31
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
#include <QStringList>
3434
#include <QDir>
3535

36+
#ifdef PYTHON2
37+
#define PYOBJ2QSTRING(obj) PyString_AsString( obj )
38+
#else
39+
#define PYOBJ2QSTRING(obj) QString::fromUtf8( PyUnicode_AsUTF8( obj ) )
40+
#endif
41+
3642
PyThreadState* _mainState;
3743

3844
QgsPythonUtilsImpl::QgsPythonUtilsImpl()
@@ -119,13 +125,21 @@ bool QgsPythonUtilsImpl::checkSystemImports()
119125
return false;
120126
}
121127
}
122-
128+
#ifdef PYTHON2
123129
// import Qt bindings
124130
if ( !runString( "from PyQt4 import QtCore, QtGui",
125-
QObject::tr( "Couldn't load PyQt4." ) + '\n' + QObject::tr( "Python support will be disabled." ) ) )
131+
QObject::tr( "Couldn't load PyQt." ) + '\n' + QObject::tr( "Python support will be disabled." ) ) )
132+
{
133+
return false;
134+
}
135+
#else
136+
// import Qt bindings
137+
if ( !runString( "from PyQt5 import QtCore, QtGui",
138+
QObject::tr( "Couldn't load PyQt." ) + '\n' + QObject::tr( "Python support will be disabled." ) ) )
126139
{
127140
return false;
128141
}
142+
#endif
129143

130144
// import QGIS bindings
131145
QString error_msg = QObject::tr( "Couldn't load PyQGIS." ) + '\n' + QObject::tr( "Python support will be disabled." );
@@ -349,9 +363,15 @@ QString QgsPythonUtilsImpl::getTraceback()
349363
PyErr_Fetch( &type, &value, &traceback );
350364
PyErr_NormalizeException( &type, &value, &traceback );
351365

352-
modStringIO = PyImport_ImportModule( "cStringIO" );
366+
#ifdef PYTHON2
367+
const char* iomod = "cStringIO";
368+
#else
369+
const char* iomod = "io";
370+
#endif
371+
372+
modStringIO = PyImport_ImportModule( iomod );
353373
if ( modStringIO == NULL )
354-
TRACEBACK_FETCH_ERROR( "can't import cStringIO" );
374+
TRACEBACK_FETCH_ERROR( QString( "can't import %1" ).arg( iomod ) );
355375

356376
obStringIO = PyObject_CallMethod( modStringIO, ( char* ) "StringIO", NULL );
357377

@@ -382,12 +402,7 @@ QString QgsPythonUtilsImpl::getTraceback()
382402
if ( !PyUnicode_Check( obResult ) )
383403
TRACEBACK_FETCH_ERROR( "getvalue() did not return a string" );
384404

385-
#if PYTHON2
386-
result = PyString_AsString( obResult );
387-
#else
388-
result = QString::fromUtf8( PyUnicode_AsUTF8( obResult ) );
389-
#endif
390-
405+
result = PYOBJ2QSTRING( obResult );
391406

392407
done:
393408

@@ -413,23 +428,27 @@ QString QgsPythonUtilsImpl::getTraceback()
413428

414429
QString QgsPythonUtilsImpl::getTypeAsString( PyObject* obj )
415430
{
416-
if ( obj == NULL )
417-
return NULL;
431+
if ( !obj )
432+
return 0;
433+
434+
#ifdef PYTHON2
418435
if ( PyClass_Check( obj ) )
419436
{
420437
QgsDebugMsg( "got class" );
421438
return QString( PyString_AsString((( PyClassObject* )obj )->cl_name ) );
422439
}
423-
else if ( PyType_Check( obj ) )
424-
{
425-
QgsDebugMsg( "got type" );
426-
return QString((( PyTypeObject* )obj )->tp_name );
427-
}
428440
else
429-
{
430-
QgsDebugMsg( "got object" );
431-
return PyObjectToQString( obj );
432-
}
441+
#endif
442+
if ( PyType_Check( obj ) )
443+
{
444+
QgsDebugMsg( "got type" );
445+
return QString((( PyTypeObject* )obj )->tp_name );
446+
}
447+
else
448+
{
449+
QgsDebugMsg( "got object" );
450+
return PyObjectToQString( obj );
451+
}
433452
}
434453

435454
bool QgsPythonUtilsImpl::getError( QString& errorClassName, QString& errorText )
@@ -487,15 +506,11 @@ QString QgsPythonUtilsImpl::PyObjectToQString( PyObject* obj )
487506
// check whether the object is already a unicode string
488507
if ( PyUnicode_Check( obj ) )
489508
{
490-
PyObject* utf8 = PyUnicode_AsUTF8String( obj );
491-
if ( utf8 )
492-
result = QString::fromUtf8( PyString_AS_STRING( utf8 ) );
493-
else
494-
result = "(qgis error)";
495-
Py_XDECREF( utf8 );
509+
result = PYOBJ2QSTRING( obj );
496510
return result;
497511
}
498512

513+
#if PYTHON2
499514
// check whether the object is a classical (8-bit) string
500515
if ( PyString_Check( obj ) )
501516
{
@@ -504,7 +519,6 @@ QString QgsPythonUtilsImpl::PyObjectToQString( PyObject* obj )
504519

505520
// it's some other type of object:
506521
// convert object to unicode string (equivalent to calling unicode(obj) )
507-
508522
PyObject* obj_uni = PyObject_Unicode( obj ); // obj_uni is new reference
509523
if ( obj_uni )
510524
{
@@ -515,15 +529,18 @@ QString QgsPythonUtilsImpl::PyObjectToQString( PyObject* obj )
515529
result = QString::fromUtf8( PyString_AsString( obj_utf8 ) );
516530
else
517531
result = "(qgis error)";
532+
518533
Py_XDECREF( obj_utf8 );
519534
Py_XDECREF( obj_uni );
520535
return result;
521536
}
537+
#endif
538+
522539
// if conversion to unicode failed, try to convert it to classic string, i.e. str(obj)
523540
PyObject* obj_str = PyObject_Str( obj ); // new reference
524541
if ( obj_str )
525542
{
526-
result = QString::fromUtf8( PyString_AS_STRING( obj_str ) );
543+
result = PYOBJ2QSTRING( obj_str );
527544
Py_XDECREF( obj_str );
528545
return result;
529546
}
@@ -572,11 +589,11 @@ QString QgsPythonUtilsImpl::homePythonPath()
572589
QString settingsDir = QgsApplication::qgisSettingsDirPath();
573590
if ( QDir::cleanPath( settingsDir ) == QDir::homePath() + QString( "/.qgis%1" ).arg( QGis::QGIS_VERSION_INT / 10000 ) )
574591
{
575-
return QString( "\"%1/.qgis%2/python\".decode('utf-8')" ).arg( QDir::homePath() ).arg( QGis::QGIS_VERSION_INT / 10000 );
592+
return QString( "b\"%1/.qgis%2/python\".decode('utf-8')" ).arg( QDir::homePath() ).arg( QGis::QGIS_VERSION_INT / 10000 );
576593
}
577594
else
578595
{
579-
return '"' + settingsDir.replace( '\\', "\\\\" ) + "python\".decode('utf-8')";
596+
return "b\"" + settingsDir.replace( '\\', "\\\\" ) + "python\".decode('utf-8')";
580597
}
581598
}
582599

tests/src/core/testqgspainteffect.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,13 @@ void TestQgsPaintEffect::stackSaveRestore()
254254

255255
//check if stack effect node was written
256256
QDomNodeList evalNodeList = effectParentElem.childNodes();
257-
QCOMPARE( evalNodeList.length(), ( unsigned int )1 );
257+
QCOMPARE( evalNodeList.count(), 1 );
258258
QDomElement effectElem = evalNodeList.at( 0 ).toElement();
259259
QCOMPARE( effectElem.attribute( "type" ), stack->type() );
260260

261261
//should be two effect child nodes
262262
QDomNodeList childNodeList = effectElem.elementsByTagName( "effect" );
263-
QCOMPARE( childNodeList.length(), ( unsigned int )2 );
263+
QCOMPARE( childNodeList.count(), 2 );
264264
QCOMPARE( childNodeList.at( 0 ).toElement().attribute( "type" ), blur->type() );
265265
QCOMPARE( childNodeList.at( 1 ).toElement().attribute( "type" ), shadow->type() );
266266

0 commit comments

Comments
 (0)