Skip to content

Commit b37ee7a

Browse files
committed
Set an exception if the required method does not exist
Since Python 3.10, Python checks in Debug mode if a slot fails without an exception, or succeeds with an exception set, and aborts in this case.
1 parent 46a2fe9 commit b37ee7a

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/PythonQtClassWrapper.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,8 @@ static int PythonQtInstanceWrapper_setitem(PyObject* self, PyObject* index, PyOb
122122
{
123123
PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)self;
124124
bool isSetItem = value;
125-
PythonQtMemberInfo opSlot = isSetItem ?
126-
wrapper->classInfo()->member("__setitem__")
127-
: wrapper->classInfo()->member("__delitem__");
125+
const char* methodName = isSetItem ? "__setitem__" : "__delitem__";
126+
PythonQtMemberInfo opSlot = wrapper->classInfo()->member(methodName);
128127

129128
if (opSlot._type == PythonQtMemberInfo::Slot) {
130129
PyObject* args = PyTuple_New(isSetItem?2:1);
@@ -141,6 +140,8 @@ static int PythonQtInstanceWrapper_setitem(PyObject* self, PyObject* index, PyOb
141140
Py_DECREF(args);
142141
return PyErr_Occurred()?-1:0;
143142
} else {
143+
QString e = QString("No method '%1' on class '%2'").arg(methodName).arg(QString(wrapper->classInfo()->className()));
144+
PyErr_SetString(PyExc_AttributeError, QStringToPythonConstCharPointer(e));
144145
return -1;
145146
}
146147
}

0 commit comments

Comments
 (0)