Skip to content
Permalink
Browse files

Remove some leftover Python 2 code

  • Loading branch information
nyalldawson authored and 3nids committed Jan 24, 2017
1 parent 5e17403 commit ef3b665dd1a82d50c2902df84f129f2f8d7cfa13
Showing with 11 additions and 76 deletions.
  1. +11 −76 src/python/qgspythonutilsimpl.cpp
@@ -35,14 +35,6 @@
#include <QStandardPaths>
#include <QDebug>

#if (PY_VERSION_HEX < 0x03000000)
#define PYOBJ2QSTRING(obj) PyString_AsString( obj )
#elif (PY_VERSION_HEX < 0x03030000)
#define PYOBJ2QSTRING(obj) QString::fromUtf8( PyBytes_AsString(PyUnicode_AsUTF8String( obj ) ) )
#else
#define PYOBJ2QSTRING(obj) QString::fromUtf8( PyUnicode_AsUTF8( obj ) )
#endif

PyThreadState* _mainState;

QgsPythonUtilsImpl::QgsPythonUtilsImpl()
@@ -98,11 +90,7 @@ bool QgsPythonUtilsImpl::checkSystemImports()
// we store here paths in unicode strings
// the str constant will contain utf8 code (through runString)
// so we call '...'.decode('utf-8') to make a unicode string
#if (PY_VERSION_HEX < 0x03000000)
pluginpaths << '"' + p + "\".decode('utf-8')";
#else
pluginpaths << '"' + p + '"';
#endif
}
pluginpaths << homePluginsPath();
pluginpaths << '"' + pluginsPath() + '"';
@@ -133,21 +121,12 @@ bool QgsPythonUtilsImpl::checkSystemImports()
return false;
}
}
#if (PY_VERSION_HEX < 0x03000000)
// import Qt bindings
if ( !runString( "from PyQt4 import QtCore, QtGui",
QObject::tr( "Couldn't load PyQt." ) + '\n' + QObject::tr( "Python support will be disabled." ) ) )
{
return false;
}
#else
// import Qt bindings
if ( !runString( QStringLiteral( "from PyQt5 import QtCore, QtGui" ),
QObject::tr( "Couldn't load PyQt." ) + '\n' + QObject::tr( "Python support will be disabled." ) ) )
{
return false;
}
#endif

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

#if (PY_VERSION_HEX < 0x03000000)
const char* iomod = "cStringIO";
#else
const char* iomod = "io";
#endif

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

/* And it should be a string all ready to go - duplicate it. */
if ( !
#if (PY_VERSION_HEX < 0x03000000)
PyString_Check( obResult )
#else
PyUnicode_Check( obResult )
#endif
)
if ( !PyUnicode_Check( obResult ) )
TRACEBACK_FETCH_ERROR( "getvalue() did not return a string" );

result = PYOBJ2QSTRING( obResult );
result = QString::fromUtf8( PyUnicode_AsUTF8( obResult ) );

done:

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

#if (PY_VERSION_HEX < 0x03000000)
if ( PyClass_Check( obj ) )
if ( PyType_Check( obj ) )
{
QgsDebugMsg( "got class" );
return QString( PyString_AsString((( PyClassObject* )obj )->cl_name ) );
QgsDebugMsg( "got type" );
return QString((( PyTypeObject* )obj )->tp_name );
}
else
#endif
if ( PyType_Check( obj ) )
{
QgsDebugMsg( "got type" );
return QString((( PyTypeObject* )obj )->tp_name );
}
else
{
QgsDebugMsg( "got object" );
return PyObjectToQString( obj );
}
{
QgsDebugMsg( "got object" );
return PyObjectToQString( obj );
}
}

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

#if (PY_VERSION_HEX < 0x03000000)
// check whether the object is a classical (8-bit) string
if ( PyString_Check( obj ) )
{
return QString::fromUtf8( PyString_AS_STRING( obj ) );
}

// it's some other type of object:
// convert object to Unicode string (equivalent to calling unicode(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 );
Py_XDECREF( obj_uni );
return result;
}
#endif

// if conversion to Unicode failed, try to convert it to classic string, i.e. str(obj)
PyObject* obj_str = PyObject_Str( obj ); // new reference
if ( obj_str )
{
result = PYOBJ2QSTRING( obj_str );
result = QString::fromUtf8( PyUnicode_AsUTF8( obj_str ) );
Py_XDECREF( obj_str );
return result;
}

0 comments on commit ef3b665

Please sign in to comment.
You can’t perform that action at this time.