33
33
#include < QStringList>
34
34
#include < QDir>
35
35
36
+ #ifdef PYTHON2
37
+ #define PYOBJ2QSTRING (obj ) PyString_AsString( obj )
38
+ #else
39
+ #define PYOBJ2QSTRING (obj ) QString::fromUtf8( PyUnicode_AsUTF8( obj ) )
40
+ #endif
41
+
36
42
PyThreadState* _mainState;
37
43
38
44
QgsPythonUtilsImpl::QgsPythonUtilsImpl ()
@@ -119,13 +125,21 @@ bool QgsPythonUtilsImpl::checkSystemImports()
119
125
return false ;
120
126
}
121
127
}
122
-
128
+ # ifdef PYTHON2
123
129
// import Qt bindings
124
130
if ( !runString ( " from PyQt4 import QtCore, QtGui" ,
125
- QObject::tr ( " Couldn't load PyQt4." ) + ' \n ' + QObject::tr ( " Python support will be disabled." ) ) )
131
+ QObject::tr ( " Couldn't load PyQt." ) + ' \n ' + QObject::tr ( " Python support will be disabled." ) ) )
132
+ {
133
+ return false ;
134
+ }
135
+ #else
136
+ // import Qt bindings
137
+ if ( !runString ( " from PyQt5 import QtCore, QtGui" ,
138
+ QObject::tr ( " Couldn't load PyQt." ) + ' \n ' + QObject::tr ( " Python support will be disabled." ) ) )
126
139
{
127
140
return false ;
128
141
}
142
+ #endif
129
143
130
144
// import QGIS bindings
131
145
QString error_msg = QObject::tr ( " Couldn't load PyQGIS." ) + ' \n ' + QObject::tr ( " Python support will be disabled." );
@@ -349,9 +363,15 @@ QString QgsPythonUtilsImpl::getTraceback()
349
363
PyErr_Fetch ( &type, &value, &traceback );
350
364
PyErr_NormalizeException ( &type, &value, &traceback );
351
365
352
- modStringIO = PyImport_ImportModule ( " cStringIO" );
366
+ #ifdef PYTHON2
367
+ const char * iomod = " cStringIO" ;
368
+ #else
369
+ const char * iomod = " io" ;
370
+ #endif
371
+
372
+ modStringIO = PyImport_ImportModule ( iomod );
353
373
if ( modStringIO == NULL )
354
- TRACEBACK_FETCH_ERROR ( " can't import cStringIO " );
374
+ TRACEBACK_FETCH_ERROR ( QString ( " can't import %1 " ). arg ( iomod ) );
355
375
356
376
obStringIO = PyObject_CallMethod ( modStringIO, ( char * ) " StringIO" , NULL );
357
377
@@ -382,12 +402,7 @@ QString QgsPythonUtilsImpl::getTraceback()
382
402
if ( !PyUnicode_Check ( obResult ) )
383
403
TRACEBACK_FETCH_ERROR ( " getvalue() did not return a string" );
384
404
385
- #if PYTHON2
386
- result = PyString_AsString ( obResult );
387
- #else
388
- result = QString::fromUtf8 ( PyUnicode_AsUTF8 ( obResult ) );
389
- #endif
390
-
405
+ result = PYOBJ2QSTRING ( obResult );
391
406
392
407
done:
393
408
@@ -413,23 +428,27 @@ QString QgsPythonUtilsImpl::getTraceback()
413
428
414
429
QString QgsPythonUtilsImpl::getTypeAsString ( PyObject* obj )
415
430
{
416
- if ( obj == NULL )
417
- return NULL ;
431
+ if ( !obj )
432
+ return 0 ;
433
+
434
+ #ifdef PYTHON2
418
435
if ( PyClass_Check ( obj ) )
419
436
{
420
437
QgsDebugMsg ( " got class" );
421
438
return QString ( PyString_AsString ((( PyClassObject* )obj )->cl_name ) );
422
439
}
423
- else if ( PyType_Check ( obj ) )
424
- {
425
- QgsDebugMsg ( " got type" );
426
- return QString ((( PyTypeObject* )obj )->tp_name );
427
- }
428
440
else
429
- {
430
- QgsDebugMsg ( " got object" );
431
- return PyObjectToQString ( obj );
432
- }
441
+ #endif
442
+ if ( PyType_Check ( obj ) )
443
+ {
444
+ QgsDebugMsg ( " got type" );
445
+ return QString ((( PyTypeObject* )obj )->tp_name );
446
+ }
447
+ else
448
+ {
449
+ QgsDebugMsg ( " got object" );
450
+ return PyObjectToQString ( obj );
451
+ }
433
452
}
434
453
435
454
bool QgsPythonUtilsImpl::getError ( QString& errorClassName, QString& errorText )
@@ -487,15 +506,11 @@ QString QgsPythonUtilsImpl::PyObjectToQString( PyObject* obj )
487
506
// check whether the object is already a unicode string
488
507
if ( PyUnicode_Check ( obj ) )
489
508
{
490
- PyObject* utf8 = PyUnicode_AsUTF8String ( obj );
491
- if ( utf8 )
492
- result = QString::fromUtf8 ( PyString_AS_STRING ( utf8 ) );
493
- else
494
- result = " (qgis error)" ;
495
- Py_XDECREF ( utf8 );
509
+ result = PYOBJ2QSTRING ( obj );
496
510
return result;
497
511
}
498
512
513
+ #if PYTHON2
499
514
// check whether the object is a classical (8-bit) string
500
515
if ( PyString_Check ( obj ) )
501
516
{
@@ -504,7 +519,6 @@ QString QgsPythonUtilsImpl::PyObjectToQString( PyObject* obj )
504
519
505
520
// it's some other type of object:
506
521
// convert object to unicode string (equivalent to calling unicode(obj) )
507
-
508
522
PyObject* obj_uni = PyObject_Unicode ( obj ); // obj_uni is new reference
509
523
if ( obj_uni )
510
524
{
@@ -515,15 +529,18 @@ QString QgsPythonUtilsImpl::PyObjectToQString( PyObject* obj )
515
529
result = QString::fromUtf8 ( PyString_AsString ( obj_utf8 ) );
516
530
else
517
531
result = " (qgis error)" ;
532
+
518
533
Py_XDECREF ( obj_utf8 );
519
534
Py_XDECREF ( obj_uni );
520
535
return result;
521
536
}
537
+ #endif
538
+
522
539
// if conversion to unicode failed, try to convert it to classic string, i.e. str(obj)
523
540
PyObject* obj_str = PyObject_Str ( obj ); // new reference
524
541
if ( obj_str )
525
542
{
526
- result = QString::fromUtf8 ( PyString_AS_STRING ( obj_str ) );
543
+ result = PYOBJ2QSTRING ( obj_str );
527
544
Py_XDECREF ( obj_str );
528
545
return result;
529
546
}
@@ -572,11 +589,11 @@ QString QgsPythonUtilsImpl::homePythonPath()
572
589
QString settingsDir = QgsApplication::qgisSettingsDirPath ();
573
590
if ( QDir::cleanPath ( settingsDir ) == QDir::homePath () + QString ( " /.qgis%1" ).arg ( QGis::QGIS_VERSION_INT / 10000 ) )
574
591
{
575
- return QString ( " \" %1/.qgis%2/python\" .decode('utf-8')" ).arg ( QDir::homePath () ).arg ( QGis::QGIS_VERSION_INT / 10000 );
592
+ return QString ( " b \" %1/.qgis%2/python\" .decode('utf-8')" ).arg ( QDir::homePath () ).arg ( QGis::QGIS_VERSION_INT / 10000 );
576
593
}
577
594
else
578
595
{
579
- return ' " ' + settingsDir.replace ( ' \\ ' , " \\\\ " ) + " python\" .decode('utf-8')" ;
596
+ return " b \" " + settingsDir.replace ( ' \\ ' , " \\\\ " ) + " python\" .decode('utf-8')" ;
580
597
}
581
598
}
582
599
0 commit comments