Skip to content

Commit ef3b665

Browse files
nyalldawson3nids
authored andcommittedJan 24, 2017
Remove some leftover Python 2 code
1 parent 5e17403 commit ef3b665

File tree

1 file changed

+11
-76
lines changed

1 file changed

+11
-76
lines changed
 

‎src/python/qgspythonutilsimpl.cpp

Lines changed: 11 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@
3535
#include <QStandardPaths>
3636
#include <QDebug>
3737

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-
4638
PyThreadState* _mainState;
4739

4840
QgsPythonUtilsImpl::QgsPythonUtilsImpl()
@@ -98,11 +90,7 @@ bool QgsPythonUtilsImpl::checkSystemImports()
9890
// we store here paths in unicode strings
9991
// the str constant will contain utf8 code (through runString)
10092
// 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
10493
pluginpaths << '"' + p + '"';
105-
#endif
10694
}
10795
pluginpaths << homePluginsPath();
10896
pluginpaths << '"' + pluginsPath() + '"';
@@ -133,21 +121,12 @@ bool QgsPythonUtilsImpl::checkSystemImports()
133121
return false;
134122
}
135123
}
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
144124
// import Qt bindings
145125
if ( !runString( QStringLiteral( "from PyQt5 import QtCore, QtGui" ),
146126
QObject::tr( "Couldn't load PyQt." ) + '\n' + QObject::tr( "Python support will be disabled." ) ) )
147127
{
148128
return false;
149129
}
150-
#endif
151130

152131
// import QGIS bindings
153132
QString error_msg = QObject::tr( "Couldn't load PyQGIS." ) + '\n' + QObject::tr( "Python support will be disabled." );
@@ -384,11 +363,7 @@ QString QgsPythonUtilsImpl::getTraceback()
384363
PyErr_Fetch( &type, &value, &traceback );
385364
PyErr_NormalizeException( &type, &value, &traceback );
386365

387-
#if (PY_VERSION_HEX < 0x03000000)
388-
const char* iomod = "cStringIO";
389-
#else
390366
const char* iomod = "io";
391-
#endif
392367

393368
modStringIO = PyImport_ImportModule( iomod );
394369
if ( !modStringIO )
@@ -421,16 +396,10 @@ QString QgsPythonUtilsImpl::getTraceback()
421396
TRACEBACK_FETCH_ERROR( "getvalue() failed." );
422397

423398
/* 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 ) )
431400
TRACEBACK_FETCH_ERROR( "getvalue() did not return a string" );
432401

433-
result = PYOBJ2QSTRING( obResult );
402+
result = QString::fromUtf8( PyUnicode_AsUTF8( obResult ) );
434403

435404
done:
436405

@@ -459,24 +428,16 @@ QString QgsPythonUtilsImpl::getTypeAsString( PyObject* obj )
459428
if ( !obj )
460429
return QString();
461430

462-
#if (PY_VERSION_HEX < 0x03000000)
463-
if ( PyClass_Check( obj ) )
431+
if ( PyType_Check( obj ) )
464432
{
465-
QgsDebugMsg( "got class" );
466-
return QString( PyString_AsString((( PyClassObject* )obj )->cl_name ) );
433+
QgsDebugMsg( "got type" );
434+
return QString((( PyTypeObject* )obj )->tp_name );
467435
}
468436
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+
}
480441
}
481442

482443
bool QgsPythonUtilsImpl::getError( QString& errorClassName, QString& errorText )
@@ -534,41 +495,15 @@ QString QgsPythonUtilsImpl::PyObjectToQString( PyObject* obj )
534495
// check whether the object is already a unicode string
535496
if ( PyUnicode_Check( obj ) )
536497
{
537-
result = PYOBJ2QSTRING( obj );
498+
result = QString::fromUtf8( PyUnicode_AsUTF8( obj ) );
538499
return result;
539500
}
540501

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-
567502
// if conversion to Unicode failed, try to convert it to classic string, i.e. str(obj)
568503
PyObject* obj_str = PyObject_Str( obj ); // new reference
569504
if ( obj_str )
570505
{
571-
result = PYOBJ2QSTRING( obj_str );
506+
result = QString::fromUtf8( PyUnicode_AsUTF8( obj_str ) );
572507
Py_XDECREF( obj_str );
573508
return result;
574509
}

0 commit comments

Comments
 (0)
Please sign in to comment.