Skip to content

Commit b7f250b

Browse files
committed
added support for attribute lookup error callback
1 parent 7ab1fbe commit b7f250b

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/PythonQt.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,20 @@ void PythonQt::qObjectNoLongerWrappedCB(QObject* o)
444444
};
445445
}
446446

447+
void PythonQt::setQObjectMissingAttributeCallback(PythonQtQObjectMissingAttributeCB* cb)
448+
{
449+
_self->_p->_qObjectMissingAttribCB = cb;
450+
}
451+
452+
QString PythonQt::qObjectMissingAttributeCallback(QObject* o, const QString& attribute)
453+
{
454+
if (_self && _self->_p && _self->_p->_qObjectMissingAttribCB) {
455+
return _self->_p->_qObjectMissingAttribCB(o, attribute);
456+
} else {
457+
return QString();
458+
}
459+
}
460+
447461
void PythonQt::registerClass(const QMetaObject* metaobject, const char* package, PythonQtQObjectCreatorFunctionCB* wrapperCreator, PythonQtShellSetInstanceWrapperCB* shell)
448462
{
449463
_p->registerClass(metaobject, package, wrapperCreator, shell);
@@ -1095,7 +1109,7 @@ QStringList PythonQt::introspection(PyObject* module, const QString& objectname,
10951109
if (object) {
10961110
results = introspectObject(object, type);
10971111
}
1098-
1112+
PyErr_Clear();
10991113
return results;
11001114
}
11011115

@@ -1216,6 +1230,7 @@ QStringList PythonQt::introspectObject(PyObject* object, ObjectType type)
12161230
Py_DECREF(keys);
12171231
}
12181232
}
1233+
PyErr_Clear();
12191234
return results;
12201235
}
12211236

@@ -1243,6 +1258,7 @@ PyObject* PythonQt::getObjectByType(const QString& typeName)
12431258
}
12441259
}
12451260

1261+
PyErr_Clear();
12461262
return object;
12471263
}
12481264

@@ -1270,6 +1286,7 @@ QStringList PythonQt::introspectType(const QString& typeName, ObjectType type)
12701286
results = introspectObject(object, type);
12711287
Py_DECREF(object);
12721288
}
1289+
PyErr_Clear();
12731290
return results;
12741291
}
12751292

@@ -1403,6 +1420,7 @@ PythonQtPrivate::PythonQtPrivate()
14031420
_defaultImporter = new PythonQtQFileImporter;
14041421
_noLongerWrappedCB = NULL;
14051422
_wrappedCB = NULL;
1423+
_qObjectMissingAttribCB = NULL;
14061424
_currentClassInfoForClassWrapperCreation = NULL;
14071425
_profilingCB = NULL;
14081426
_hadError = false;

src/PythonQt.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ typedef void PythonQtVoidPtrCB(void* object);
7373
typedef void PythonQtQObjectWrappedCB(QObject* object);
7474
typedef void PythonQtQObjectNoLongerWrappedCB(QObject* object);
7575
typedef void* PythonQtPolymorphicHandlerCB(const void *ptr, const char **class_name);
76+
typedef QString PythonQtQObjectMissingAttributeCB(QObject* object, const QString& attribute);
7677

7778
typedef void PythonQtShellSetInstanceWrapperCB(void* object, PythonQtInstanceWrapper* wrapper);
7879

@@ -583,6 +584,12 @@ class PYTHONQT_EXPORT PythonQt : public QObject {
583584
//! call the callback if it is set
584585
static void qObjectNoLongerWrappedCB(QObject* o);
585586

587+
//! set a callback that is called when a QObject does not have a specific attribute.
588+
void setQObjectMissingAttributeCallback(PythonQtQObjectMissingAttributeCB* cb);
589+
590+
//! call the callback if it is set
591+
static QString qObjectMissingAttributeCallback(QObject* o, const QString& attribute);
592+
586593
//! called by internal help methods
587594
PyObject* helpCalled(PythonQtClassInfo* info);
588595

@@ -820,6 +827,7 @@ class PYTHONQT_EXPORT PythonQtPrivate : public QObject {
820827

821828
PythonQtQObjectNoLongerWrappedCB* _noLongerWrappedCB;
822829
PythonQtQObjectWrappedCB* _wrappedCB;
830+
PythonQtQObjectMissingAttributeCB* _qObjectMissingAttribCB;
823831

824832
QStringList _importIgnorePaths;
825833
QStringList _sharedLibrarySuffixes;

src/PythonQtInstanceWrapper.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,11 @@ static PyObject *PythonQtInstanceWrapper_getattro(PyObject *obj,PyObject *name)
596596
}
597597

598598
QString error = QString(wrapper->classInfo()->className()) + " has no attribute named '" + QString(attributeName) + "'";
599+
if (wrapper->_obj) {
600+
error += PythonQt::self()->qObjectMissingAttributeCallback(wrapper->_obj, QString(attributeName));
601+
}
599602
PyErr_SetString(PyExc_AttributeError, QStringToPythonConstCharPointer(error));
603+
600604
return NULL;
601605
}
602606

0 commit comments

Comments
 (0)