Skip to content

Commit cd0eb05

Browse files
committedOct 24, 2015
[py3] Some compatibility fixes for python 3
1 parent 8b11d43 commit cd0eb05

File tree

7 files changed

+25
-18
lines changed

7 files changed

+25
-18
lines changed
 

‎cmake/FindPythonLibrary.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ else(EXISTS "${PYTHON_INCLUDE_PATH}" AND EXISTS "${PYTHON_LIBRARY}" AND EXISTS "
4747
FIND_PACKAGE(PythonInterp)
4848

4949
if(PYTHONINTERP_FOUND)
50+
ADD_DEFINITIONS(-DPYTHON2)
5051

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

‎python/core/conversions.sip

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ template <TYPE>
421421
{
422422
PyObject *tobj;
423423

424-
if ((tobj = PyInt_FromLong(*it)) == NULL)
424+
if ((tobj = PyLong_FromLong(*it)) == NULL)
425425
{
426426
Py_DECREF(l);
427427
return NULL;
@@ -441,7 +441,7 @@ template <TYPE>
441441

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

447447
*sipCppPtr = qset;
@@ -743,7 +743,7 @@ template<TYPE>
743743
Py_ssize_t j = 0;
744744
while (PyDict_Next(tobj, &j, &kobj2, &tobj2))
745745
{
746-
int k2 = PyInt_AsLong(kobj2);
746+
int k2 = PyLong_AsLong(kobj2);
747747
int state;
748748

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

902902
PyObject *t1obj = sipConvertFromNewType(t1, sipType_QString, sipTransferObj);
903-
PyObject *t2obj = PyInt_FromLong( (long) i.value() );
903+
PyObject *t2obj = PyLong_FromLong( (long) i.value() );
904904

905905
if (t1obj == NULL || t2obj == NULL || PyDict_SetItem(d, t1obj, t2obj) < 0)
906906
{
@@ -959,7 +959,7 @@ template<TYPE>
959959
int state;
960960

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

964964
if (*sipIsErr)
965965
{
@@ -1002,7 +1002,7 @@ template<TYPE>
10021002
QString *t1 = new QString(i.key());
10031003

10041004
PyObject *t1obj = sipConvertFromNewType(t1, sipType_QString, sipTransferObj);
1005-
PyObject *t2obj = PyInt_FromLong( (long) i.value() );
1005+
PyObject *t2obj = PyLong_FromLong( (long) i.value() );
10061006

10071007
if (t1obj == NULL || t2obj == NULL || PyDict_SetItem(d, t1obj, t2obj) < 0)
10081008
{
@@ -1061,7 +1061,7 @@ template<TYPE>
10611061
int state;
10621062

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

10661066
if (*sipIsErr)
10671067
{
@@ -1501,7 +1501,7 @@ template<int, TYPE2*>
15011501
while (PyDict_Next(sipPy, &i, &t1obj, &t2obj))
15021502
{
15031503
int state2;
1504-
int k = (int) PyInt_AsLong(t1obj);
1504+
int k = (int) PyLong_AsLong(t1obj);
15051505
for (int i = 0; i < PyList_GET_SIZE(t2obj); ++i)
15061506
{
15071507
TYPE2 *t2 =
@@ -1639,7 +1639,7 @@ template<int, TYPE2*>
16391639
}
16401640

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

@@ -1775,7 +1775,7 @@ template <TYPE>
17751775
{
17761776
QgsFeature* oobj = new QgsFeature(*it.value());
17771777

1778-
PyObject* keyobj = PyInt_FromLong(it.key());
1778+
PyObject* keyobj = PyLong_FromLong(it.key());
17791779
PyObject* pyOobj = sipConvertFromType(oobj, sipType_QgsFeature, sipTransferObj);
17801780
PyDict_SetItem(d, keyobj, pyOobj);
17811781

‎python/core/geometry/qgsgeometry.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class QgsGeometry
113113
*/
114114
SIP_PYOBJECT asWkb();
115115
%MethodCode
116-
sipRes = PyString_FromStringAndSize((const char *)sipCpp->asWkb(), sipCpp->wkbSize());
116+
sipRes = PyBytes_FromStringAndSize((const char *)sipCpp->asWkb(), sipCpp->wkbSize());
117117
%End
118118

119119
/**

‎python/core/qgsapplication.sip

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,15 @@ static char **qtgui_ArgvToC(PyObject *argvlist, int &argc)
2525
char *arg;
2626

2727
// Get the argument and allocate memory for it.
28+
#if PYTHON2
2829
if ((arg = PyString_AsString(PyList_GET_ITEM(argvlist, a))) == NULL ||
2930
(argv[a] = (char *)sipMalloc(strlen(arg) + 1)) == NULL)
3031
return NULL;
31-
32+
#else
33+
if ((arg = PyUnicode_AsString(PyList_GET_ITEM(argvlist, a))) == NULL ||
34+
(argv[a] = (char *)sipMalloc(strlen(arg) + 1)) == NULL)
35+
return NULL;
36+
#endif
3237
// Copy the argument and save a pointer to it.
3338
strcpy(argv[a], arg);
3439
argv[a + argc + 1] = argv[a];

‎python/core/qgsmaplayer.sip

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,6 @@ class QgsMapLayer : QObject
536536
/** Write style manager's configuration (if exists). To be called by subclasses. */
537537
void writeStyleManager( QDomNode& layerNode, QDomDocument& doc ) const;
538538

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

542540
/** Add error message */
543541
void appendError( const QgsErrorMessage & theMessage );

‎python/core/qgspoint.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class QgsPoint
140140
%MethodCode
141141
QString str = "(" + QString::number(sipCpp->x()) + "," + QString::number(sipCpp->y()) + ")";
142142
//QString str("(%f,%f)").arg(sipCpp->x()).arg(sipCpp->y());
143-
sipRes = PyString_FromString(str.toLocal8Bit().data());
143+
sipRes = PyUnicode_FromString( str.toUtf8().data() );
144144
%End
145145

146146
int __len__();

‎src/python/qgspythonutilsimpl.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,15 @@ QString QgsPythonUtilsImpl::getTraceback()
379379
TRACEBACK_FETCH_ERROR( "getvalue() failed." );
380380

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

385+
#if PYTHON2
385386
result = PyString_AsString( obResult );
387+
#else
388+
result = QString::fromUtf8( PyUnicode_AsUTF8( obResult ) );
389+
#endif
390+
386391

387392
done:
388393

@@ -410,7 +415,6 @@ QString QgsPythonUtilsImpl::getTypeAsString( PyObject* obj )
410415
{
411416
if ( obj == NULL )
412417
return NULL;
413-
414418
if ( PyClass_Check( obj ) )
415419
{
416420
QgsDebugMsg( "got class" );
@@ -515,7 +519,6 @@ QString QgsPythonUtilsImpl::PyObjectToQString( PyObject* obj )
515519
Py_XDECREF( obj_uni );
516520
return result;
517521
}
518-
519522
// if conversion to unicode failed, try to convert it to classic string, i.e. str(obj)
520523
PyObject* obj_str = PyObject_Str( obj ); // new reference
521524
if ( obj_str )

0 commit comments

Comments
 (0)
Failed to load comments.