Improve error handling calling PyObject_SetAttr

Less obscure error when setting 'this' on the SWIG proxy object
attempting to override __setattr__ in C++ (swig-user mailing list
query 19 Aug 2019).
This commit is contained in:
William S Fulton 2019-09-13 07:37:03 +01:00
commit 719eea090d
2 changed files with 11 additions and 6 deletions

View file

@ -69,8 +69,7 @@ namespace swig {
static bool back_reference(PyObject* child, PyObject* owner) {
SwigPyObject* swigThis = SWIG_Python_GetSwigThis(child);
if (swigThis && (swigThis->own & SWIG_POINTER_OWN) != SWIG_POINTER_OWN) {
PyObject_SetAttr(child, container_owner_attribute(), owner);
return true;
return PyObject_SetAttr(child, container_owner_attribute(), owner) != -1;
}
return false;
}

View file

@ -1183,8 +1183,10 @@ SWIG_Python_NewShadowInstance(SwigPyClientData *data, PyObject *swig_this)
}
}
#else
PyObject *key = SWIG_This();
PyObject_SetAttr(inst, key, swig_this);
if (PyObject_SetAttr(inst, SWIG_This(), swig_this) == -1) {
Py_DECREF(inst);
inst = 0;
}
#endif
}
} else {
@ -1196,8 +1198,12 @@ SWIG_Python_NewShadowInstance(SwigPyClientData *data, PyObject *swig_this)
inst = ((PyTypeObject *)data->newargs)->tp_new((PyTypeObject *)data->newargs, empty_args, empty_kwargs);
Py_DECREF(empty_kwargs);
if (inst) {
PyObject_SetAttr(inst, SWIG_This(), swig_this);
Py_TYPE(inst)->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
if (PyObject_SetAttr(inst, SWIG_This(), swig_this) == -1) {
Py_DECREF(inst);
inst = 0;
} else {
Py_TYPE(inst)->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
}
}
}
Py_DECREF(empty_args);