[consider merging] avoid an assertion in the Python exit code for accessing an already destroyed Python object

This commit is contained in:
Matthias Koefferlein 2023-03-10 23:45:37 +01:00
parent 87e2def285
commit bee3d11f62
4 changed files with 24 additions and 0 deletions

View File

@ -859,6 +859,14 @@ PythonClassClientData::PythonClassClientData (const gsi::ClassBase *_cls, PyType
// .. nothing yet ..
}
PythonClassClientData::~PythonClassClientData ()
{
// This destructor is called from the exit code. Python may have shut down already.
// We must not try to release the objects in that case and simply don't care about them any longer.
py_type_object.release ();
py_type_object_static.release ();
}
PyTypeObject *
PythonClassClientData::py_type (const gsi::ClassBase &cls_decl, bool as_static)
{

View File

@ -302,6 +302,7 @@ struct PythonClassClientData
: public gsi::PerClassClientSpecificData
{
PythonClassClientData (const gsi::ClassBase *_cls, PyTypeObject *_py_type, PyTypeObject *_py_type_static, PythonModule *module);
~PythonClassClientData ();
PythonPtr py_type_object;
PythonPtr py_type_object_static;

View File

@ -151,6 +151,13 @@ PythonPtr::~PythonPtr ()
Py_XDECREF (mp_obj);
}
PyObject *PythonPtr::release ()
{
PyObject *obj = mp_obj;
mp_obj = NULL;
return obj;
}
PythonPtr::operator bool () const
{
return mp_obj != NULL;

View File

@ -211,6 +211,14 @@ public:
return mp_obj < other.mp_obj;
}
/**
* @brief Releases the object
*
* This method returns and resets the raw pointer without changing the
* reference count.
*/
PyObject *release ();
private:
PyObject *mp_obj;
};