Skip to content

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed
 

‎src/app/qgspythonutils.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
#include "qgspythonutils.h"
2222

2323
#include "qgsapplication.h"
24+
#include "qgslogger.h"
2425

2526
#include <QMessageBox>
2627

28+
2729
QString QgsPythonUtils::mPluginsPath;
2830
PyObject* QgsPythonUtils::mMainModule;
2931
PyObject* QgsPythonUtils::mMainDict;
@@ -167,6 +169,33 @@ bool QgsPythonUtils::runString(const QString& command)
167169
}
168170

169171

172+
QString QgsPythonUtils::getTypeAsString(PyObject* obj)
173+
{
174+
if (obj == NULL)
175+
return NULL;
176+
177+
if (PyClass_Check(obj))
178+
{
179+
QgsDebugMsg("got class");
180+
return QString(PyString_AsString(((PyClassObject*)obj)->cl_name));
181+
}
182+
else if (PyType_Check(obj))
183+
{
184+
QgsDebugMsg("got type");
185+
return QString(((PyTypeObject*)obj)->tp_name);
186+
}
187+
else
188+
{
189+
QgsDebugMsg("got object");
190+
PyObject* s = PyObject_Str(obj);
191+
QString str;
192+
if (s && PyString_Check(s))
193+
str = QString(PyString_AsString(s));
194+
Py_XDECREF(s);
195+
return str;
196+
}
197+
}
198+
170199
bool QgsPythonUtils::getError(QString& errorClassName, QString& errorText)
171200
{
172201
if (!PyErr_Occurred())
@@ -179,9 +208,9 @@ bool QgsPythonUtils::getError(QString& errorClassName, QString& errorText)
179208

180209
// get the exception information
181210
PyErr_Fetch(&err_type, &err_value, &err_tb);
182-
211+
183212
// get exception's class name
184-
errorClassName = PyString_AS_STRING(((PyClassObject*)err_type)->cl_name);
213+
errorClassName = getTypeAsString(err_type);
185214

186215
// get exception's text
187216
if (err_value != NULL && err_value != Py_None)

‎src/app/qgspythonutils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ class QgsPythonUtils
5757

5858
static bool evalString(const QString& command, QString& result);
5959

60+
//! @return object's type name as a string
61+
static QString getTypeAsString(PyObject* obj);
62+
6063
//! get information about error to the supplied arguments
6164
//! @return false if there was no python error
6265
static bool getError(QString& errorClassName, QString& errorText);

0 commit comments

Comments
 (0)
Please sign in to comment.