Improve the "underlying C/C++ object has been deleted" message

python-tqt is reporting "underlying C/C++ object has been deleted". This
is rather unhelpful. A little code has been backported from the equivalent
source files in sip 4.19.23 to make it a little clearer about what is
going wrong (original author Riverbank Computing Limited
<info@riverbankcomputing.com>, licensed under GPL version 2 or 3).

Signed-off-by: aneejit1 <aneejit1@gmail.com>
(cherry picked from commit c9762bd162)
r14.0.x
aneejit1 2 years ago committed by Slávek Banko
parent 5e00e331b7
commit ac6e37dd9e
No known key found for this signature in database
GPG Key ID: 608F5293A04BE668

@ -1432,6 +1432,7 @@ typedef struct _sipTQtAPI {
#define SIP_SHARE_MAP 0x0040 /* If the map slot might be occupied. */
#define SIP_CPP_HAS_REF 0x0080 /* If C/C++ has a reference. */
#define SIP_POSSIBLE_PROXY 0x0100 /* If there might be a proxy slot. */
#define SIP_CREATED 0x1000 /* If the C/C++ object has been created. */
#define sipIsPyOwned(w) ((w)->flags & SIP_PY_OWNED)
#define sipSetPyOwned(w) ((w)->flags |= SIP_PY_OWNED)
@ -1446,6 +1447,7 @@ typedef struct _sipTQtAPI {
#define sipResetCppHasRef(w) ((w)->flags &= ~SIP_CPP_HAS_REF)
#define sipPossibleProxy(w) ((w)->flags & SIP_POSSIBLE_PROXY)
#define sipSetPossibleProxy(w) ((w)->flags |= SIP_POSSIBLE_PROXY)
#define sipWasCreated(sw) ((sw)->flags & SIP_CREATED)
#define SIP_TYPE_TYPE_MASK 0x0007 /* The type type mask. */

@ -551,7 +551,7 @@ static int getSelfFromArgs(sipTypeDef *td, PyObject *args, int argnr,
sipSimpleWrapper **selfp);
static PyObject *createEnumMember(sipTypeDef *td, sipEnumMemberDef *enm);
static int compareTypedefName(const void *key, const void *el);
static int checkPointer(void *ptr);
static int checkPointer(void *ptr, sipSimpleWrapper *sw);
static void *cast_cpp_ptr(void *ptr, PyTypeObject *src_type,
const sipTypeDef *dst_type);
static void finalise(void);
@ -1040,7 +1040,7 @@ static PyObject *callDtor(PyObject *self, PyObject *args)
addr = getPtrTypeDef(sw, &ctd);
if (checkPointer(addr) < 0)
if (checkPointer(addr, sw) < 0)
return NULL;
if (PyObject_TypeCheck((PyObject *)sw, (PyTypeObject *)&sipWrapper_Type))
@ -7418,7 +7418,7 @@ void *sip_api_get_cpp_ptr(sipSimpleWrapper *sw, const sipTypeDef *td)
{
void *ptr = sipGetAddress(sw);
if (checkPointer(ptr) < 0)
if (checkPointer(ptr, sw) < 0)
return NULL;
if (td != NULL)
@ -7454,12 +7454,14 @@ static void *cast_cpp_ptr(void *ptr, PyTypeObject *src_type,
/*
* Check that a pointer is non-NULL.
*/
static int checkPointer(void *ptr)
static int checkPointer(void *ptr, sipSimpleWrapper *sw)
{
if (ptr == NULL)
{
PyErr_SetString(PyExc_RuntimeError,
"underlying C/C++ object has been deleted");
PyErr_Format(PyExc_RuntimeError, (sipWasCreated(sw) ?
"wrapped C/C++ object of type %s has been deleted" :
"super-class __init__() of type %s was never called"),
Py_TYPE(sw)->tp_name);
return -1;
}
@ -8701,7 +8703,7 @@ static int sipSimpleWrapper_init(sipSimpleWrapper *self, PyObject *args,
}
self->u.cppPtr = sipNew;
self->flags = sipFlags;
self->flags = sipFlags | SIP_CREATED;
if (!sipNotInMap(self))
sipOMAddObject(&cppPyMap, self);

Loading…
Cancel
Save