Fixed new Python module handling for standalone module case

This commit is contained in:
Matthias Koefferlein 2024-09-08 00:24:56 +02:00
parent 1e28e30e71
commit 4f72d3353f
7 changed files with 55 additions and 21 deletions

View File

@ -192,7 +192,7 @@ PythonInterpreter::PythonInterpreter (bool embedded)
sp_interpreter = this; sp_interpreter = this;
// this monitor whether Python shuts down and deletes the interpreter's // this monitors whether Python shuts down and deletes the interpreter's
// instance. // instance.
// NOTE: this assumes, the interpreter was created with new(!) // NOTE: this assumes, the interpreter was created with new(!)
Py_AtExit (&reset_interpreter); Py_AtExit (&reset_interpreter);
@ -364,6 +364,8 @@ PythonInterpreter::~PythonInterpreter ()
(*m)->cleanup (); (*m)->cleanup ();
} }
PYAObjectBase::clear_callbacks_cache (m_embedded);
m_stdout_channel = PythonRef (); m_stdout_channel = PythonRef ();
m_stderr_channel = PythonRef (); m_stderr_channel = PythonRef ();
m_stdout = PythonPtr (); m_stdout = PythonPtr ();

View File

@ -93,8 +93,6 @@ PythonModule::cleanup ()
// the Python objects are probably deleted by Python itself as it exits - // the Python objects are probably deleted by Python itself as it exits -
// don't try to delete them again in the destructor. // don't try to delete them again in the destructor.
mp_module.release (); mp_module.release ();
PYAObjectBase::clear_callbacks_cache ();
} }
PyObject * PyObject *

View File

@ -514,8 +514,16 @@ PYAObjectBase::initialize_callbacks ()
} }
void void
PYAObjectBase::clear_callbacks_cache () PYAObjectBase::clear_callbacks_cache (bool embedded)
{ {
// if not embedded, we cannot use the python API at this stage - do not try to
// reference count the objects there.
if (! embedded) {
for (auto c = s_callbacks_cache.begin (); c != s_callbacks_cache.end (); ++c) {
c->first.release_const ();
}
}
s_callbacks_cache.clear (); s_callbacks_cache.clear ();
} }

View File

@ -200,7 +200,7 @@ public:
/** /**
* @brief Clears the callbacks cache * @brief Clears the callbacks cache
*/ */
static void clear_callbacks_cache (); static void clear_callbacks_cache (bool embedded);
private: private:
friend class StatusChangedListener; friend class StatusChangedListener;

View File

@ -32,19 +32,19 @@ namespace pya
// PythonRef implementation // PythonRef implementation
PythonRef::PythonRef () PythonRef::PythonRef ()
: mp_obj (NULL) : mp_obj (NULL), m_owns_pointer (true)
{ {
// .. nothing yet .. // .. nothing yet ..
} }
PythonRef::PythonRef (const PythonPtr &ptr) PythonRef::PythonRef (const PythonPtr &ptr)
: mp_obj (ptr.get ()) : mp_obj (ptr.get ()), m_owns_pointer (true)
{ {
Py_XINCREF (mp_obj); Py_XINCREF (mp_obj);
} }
PythonRef::PythonRef (PyObject *obj, bool new_ref) PythonRef::PythonRef (PyObject *obj, bool new_ref)
: mp_obj (obj) : mp_obj (obj), m_owns_pointer (true)
{ {
if (! new_ref) { if (! new_ref) {
Py_XINCREF (mp_obj); Py_XINCREF (mp_obj);
@ -53,38 +53,49 @@ PythonRef::PythonRef (PyObject *obj, bool new_ref)
PythonRef &PythonRef::operator= (PyObject *obj) PythonRef &PythonRef::operator= (PyObject *obj)
{ {
Py_XDECREF (mp_obj); if (m_owns_pointer) {
Py_XDECREF (mp_obj);
}
mp_obj = obj; mp_obj = obj;
m_owns_pointer = true;
return *this; return *this;
} }
PythonRef &PythonRef::operator= (const PythonPtr &ptr) PythonRef &PythonRef::operator= (const PythonPtr &ptr)
{ {
Py_XDECREF (mp_obj); if (m_owns_pointer) {
Py_XDECREF (mp_obj);
}
mp_obj = ptr.get (); mp_obj = ptr.get ();
Py_XINCREF (mp_obj); Py_XINCREF (mp_obj);
m_owns_pointer = true;
return *this; return *this;
} }
PythonRef &PythonRef::operator= (const PythonRef &other) PythonRef &PythonRef::operator= (const PythonRef &other)
{ {
if (this != &other && mp_obj != other.mp_obj) { if (this != &other && mp_obj != other.mp_obj) {
Py_XDECREF (mp_obj); if (m_owns_pointer) {
Py_XDECREF (mp_obj);
}
mp_obj = other.mp_obj; mp_obj = other.mp_obj;
m_owns_pointer = true;
Py_XINCREF (mp_obj); Py_XINCREF (mp_obj);
} }
return *this; return *this;
} }
PythonRef::PythonRef (const PythonRef &other) PythonRef::PythonRef (const PythonRef &other)
: mp_obj (other.mp_obj) : mp_obj (other.mp_obj), m_owns_pointer (true)
{ {
Py_XINCREF (mp_obj); Py_XINCREF (mp_obj);
} }
PythonRef::~PythonRef () PythonRef::~PythonRef ()
{ {
Py_XDECREF (mp_obj); if (m_owns_pointer) {
Py_XDECREF (mp_obj);
}
} }
PythonRef::operator bool () const PythonRef::operator bool () const
@ -109,6 +120,12 @@ PyObject *PythonRef::release ()
return o; return o;
} }
PyObject *PythonRef::release_const () const
{
m_owns_pointer = false;
return mp_obj;
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// PythonPtr implementation // PythonPtr implementation

View File

@ -116,6 +116,14 @@ public:
*/ */
PyObject *release (); PyObject *release ();
/**
* @brief Takes the pointer, but does not change the value
* This method will stop the reference from managing the object, but
* maintains the pointer. Do not access the pointer after this
* operation.
*/
PyObject *release_const () const;
/** /**
* @brief Comparison operator * @brief Comparison operator
*/ */
@ -134,6 +142,7 @@ public:
private: private:
PyObject *mp_obj; PyObject *mp_obj;
mutable bool m_owns_pointer;
}; };
/** /**

View File

@ -42,12 +42,7 @@
static PyObject * static PyObject *
module_init (const char *pymod_name, const char *mod_name, const char *mod_description) module_init (const char *pymod_name, const char *mod_name, const char *mod_description)
{ {
if (! pya::PythonInterpreter::instance ()) { std::unique_ptr<pya::PythonModule> module (new pya::PythonModule ());
return 0;
}
pya::PythonModule *module = new pya::PythonModule ();
pya::PythonInterpreter::instance ()->register_module (module);
PYA_TRY PYA_TRY
@ -59,7 +54,12 @@ module_init (const char *pymod_name, const char *mod_name, const char *mod_descr
module->init (pymod_name, mod_description); module->init (pymod_name, mod_description);
module->make_classes (mod_name); module->make_classes (mod_name);
return module->take_module (); PyObject *mod_object = module->take_module ();
tl_assert (pya::PythonInterpreter::instance () != 0);
pya::PythonInterpreter::instance ()->register_module (module.release ());
return mod_object;
PYA_CATCH_ANYWHERE PYA_CATCH_ANYWHERE