Skip to content
Permalink
Browse files

[py3] Some compatibility fixes for python 3

  • Loading branch information
m-kuhn committed Oct 24, 2015
1 parent 8b11d43 commit cd0eb05deddc610a1a79c40163504efddfe3da3a
@@ -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})

@@ -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;
@@ -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;
@@ -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));
@@ -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)
{
@@ -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)
{
@@ -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)
{
@@ -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)
{
@@ -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 =
@@ -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 );

@@ -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);

@@ -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

/**
@@ -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];
@@ -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 );
@@ -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__();
@@ -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:

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

if ( PyClass_Check( obj ) )
{
QgsDebugMsg( "got class" );
@@ -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 )

1 comment on commit cd0eb05

@m-kuhn

This comment has been minimized.

Copy link
Member Author

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

Just to demonstrate a possible upgrade path ;)

Please revert if this makes any troubles.

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