Skip to content

Commit

Permalink
[py3] Some compatibility fixes for python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Oct 24, 2015
1 parent 8b11d43 commit cd0eb05
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 18 deletions.
1 change: 1 addition & 0 deletions cmake/FindPythonLibrary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ else(EXISTS "${PYTHON_INCLUDE_PATH}" AND EXISTS "${PYTHON_LIBRARY}" AND EXISTS "
FIND_PACKAGE(PythonInterp)

if(PYTHONINTERP_FOUND)
ADD_DEFINITIONS(-DPYTHON2)

FIND_FILE(_find_lib_python_py FindLibPython.py PATHS ${CMAKE_MODULE_PATH})

Expand Down
20 changes: 10 additions & 10 deletions python/core/conversions.sip
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ template <TYPE>
{
PyObject *tobj;

if ((tobj = PyInt_FromLong(*it)) == NULL)
if ((tobj = PyLong_FromLong(*it)) == NULL)
{
Py_DECREF(l);
return NULL;
Expand All @@ -441,7 +441,7 @@ template <TYPE>

for (int i = 0; i < PyList_GET_SIZE(sipPy); ++i)
{
qset->insert(PyInt_AsLong(PyList_GET_ITEM(sipPy, i)));
qset->insert(PyLong_AsLong(PyList_GET_ITEM(sipPy, i)));
}

*sipCppPtr = qset;
Expand Down Expand Up @@ -743,7 +743,7 @@ template<TYPE>
Py_ssize_t j = 0;
while (PyDict_Next(tobj, &j, &kobj2, &tobj2))
{
int k2 = PyInt_AsLong(kobj2);
int k2 = PyLong_AsLong(kobj2);
int state;

TYPE* t2 = reinterpret_cast<TYPE*>(sipConvertToType(tobj2, sipType_TYPE, sipTransferObj,SIP_NOT_NONE,&state,sipIsErr));
Expand Down Expand Up @@ -900,7 +900,7 @@ template<TYPE>
QString *t1 = new QString(i.key());

PyObject *t1obj = sipConvertFromNewType(t1, sipType_QString, sipTransferObj);
PyObject *t2obj = PyInt_FromLong( (long) i.value() );
PyObject *t2obj = PyLong_FromLong( (long) i.value() );

if (t1obj == NULL || t2obj == NULL || PyDict_SetItem(d, t1obj, t2obj) < 0)
{
Expand Down Expand Up @@ -959,7 +959,7 @@ template<TYPE>
int state;

QString *t1 = reinterpret_cast<QString *>(sipConvertToType(t1obj, sipType_QString, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr));
QVariant::Type t2 = (QVariant::Type) PyInt_AsLong(t1obj);
QVariant::Type t2 = (QVariant::Type) PyLong_AsLong(t1obj);

if (*sipIsErr)
{
Expand Down Expand Up @@ -1002,7 +1002,7 @@ template<TYPE>
QString *t1 = new QString(i.key());

PyObject *t1obj = sipConvertFromNewType(t1, sipType_QString, sipTransferObj);
PyObject *t2obj = PyInt_FromLong( (long) i.value() );
PyObject *t2obj = PyLong_FromLong( (long) i.value() );

if (t1obj == NULL || t2obj == NULL || PyDict_SetItem(d, t1obj, t2obj) < 0)
{
Expand Down Expand Up @@ -1061,7 +1061,7 @@ template<TYPE>
int state;

QString *t1 = reinterpret_cast<QString *>(sipConvertToType(t1obj, sipType_QString, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr));
int t2 = PyInt_AsLong(t1obj);
int t2 = PyLong_AsLong(t1obj);

if (*sipIsErr)
{
Expand Down Expand Up @@ -1501,7 +1501,7 @@ template<int, TYPE2*>
while (PyDict_Next(sipPy, &i, &t1obj, &t2obj))
{
int state2;
int k = (int) PyInt_AsLong(t1obj);
int k = (int) PyLong_AsLong(t1obj);
for (int i = 0; i < PyList_GET_SIZE(t2obj); ++i)
{
TYPE2 *t2 =
Expand Down Expand Up @@ -1639,7 +1639,7 @@ template<int, TYPE2*>
}

PyObject *t1obj = sipConvertFromNewType(sipCpp->at(i).first, sipType_QgsVectorLayer, sipTransferObj);
PyObject *t2obj = PyInt_FromLong( (long) sipCpp->at(i).second );
PyObject *t2obj = PyLong_FromLong( (long) sipCpp->at(i).second );
PyList_SetItem( p, 0, t1obj );
PyList_SetItem( p, 1, t2obj );

Expand Down Expand Up @@ -1775,7 +1775,7 @@ template <TYPE>
{
QgsFeature* oobj = new QgsFeature(*it.value());

PyObject* keyobj = PyInt_FromLong(it.key());
PyObject* keyobj = PyLong_FromLong(it.key());
PyObject* pyOobj = sipConvertFromType(oobj, sipType_QgsFeature, sipTransferObj);
PyDict_SetItem(d, keyobj, pyOobj);

Expand Down
2 changes: 1 addition & 1 deletion python/core/geometry/qgsgeometry.sip
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class QgsGeometry
*/
SIP_PYOBJECT asWkb();
%MethodCode
sipRes = PyString_FromStringAndSize((const char *)sipCpp->asWkb(), sipCpp->wkbSize());
sipRes = PyBytes_FromStringAndSize((const char *)sipCpp->asWkb(), sipCpp->wkbSize());
%End

/**
Expand Down
7 changes: 6 additions & 1 deletion python/core/qgsapplication.sip
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ static char **qtgui_ArgvToC(PyObject *argvlist, int &argc)
char *arg;

// Get the argument and allocate memory for it.
#if PYTHON2
if ((arg = PyString_AsString(PyList_GET_ITEM(argvlist, a))) == NULL ||
(argv[a] = (char *)sipMalloc(strlen(arg) + 1)) == NULL)
return NULL;

#else
if ((arg = PyUnicode_AsString(PyList_GET_ITEM(argvlist, a))) == NULL ||
(argv[a] = (char *)sipMalloc(strlen(arg) + 1)) == NULL)
return NULL;
#endif
// Copy the argument and save a pointer to it.
strcpy(argv[a], arg);
argv[a + argc + 1] = argv[a];
Expand Down
2 changes: 0 additions & 2 deletions python/core/qgsmaplayer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,6 @@ class QgsMapLayer : QObject
/** Write style manager's configuration (if exists). To be called by subclasses. */
void writeStyleManager( QDomNode& layerNode, QDomDocument& doc ) const;

/** Debugging member - invoked when a connect() is made to this object */
void connectNotify( const char * signal );

/** Add error message */
void appendError( const QgsErrorMessage & theMessage );
Expand Down
2 changes: 1 addition & 1 deletion python/core/qgspoint.sip
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class QgsPoint
%MethodCode
QString str = "(" + QString::number(sipCpp->x()) + "," + QString::number(sipCpp->y()) + ")";
//QString str("(%f,%f)").arg(sipCpp->x()).arg(sipCpp->y());
sipRes = PyString_FromString(str.toLocal8Bit().data());
sipRes = PyUnicode_FromString( str.toUtf8().data() );
%End

int __len__();
Expand Down
9 changes: 6 additions & 3 deletions src/python/qgspythonutilsimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,15 @@ QString QgsPythonUtilsImpl::getTraceback()
TRACEBACK_FETCH_ERROR( "getvalue() failed." );

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

#if PYTHON2
result = PyString_AsString( obResult );
#else
result = QString::fromUtf8( PyUnicode_AsUTF8( obResult ) );
#endif


done:

Expand Down Expand Up @@ -410,7 +415,6 @@ QString QgsPythonUtilsImpl::getTypeAsString( PyObject* obj )
{
if ( obj == NULL )
return NULL;

if ( PyClass_Check( obj ) )
{
QgsDebugMsg( "got class" );
Expand Down Expand Up @@ -515,7 +519,6 @@ QString QgsPythonUtilsImpl::PyObjectToQString( PyObject* obj )
Py_XDECREF( obj_uni );
return result;
}

// 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 )
Expand Down

1 comment on commit cd0eb05

@m-kuhn
Copy link
Member Author

@m-kuhn m-kuhn commented on cd0eb05 Oct 24, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to demonstrate a possible upgrade path ;)

Please revert if this makes any troubles.

Please sign in to comment.