mirror of https://github.com/KLayout/klayout.git
Mute noisy diagnostics with Python debug builds, avoid post-Finalize Python API access by pre-Finalize cleanup of modules. This avoids an assertion in Python debug builds and is a better style anyway.
This commit is contained in:
parent
53b7c985f0
commit
20d599b1de
|
|
@ -360,6 +360,10 @@ PythonInterpreter::PythonInterpreter (bool embedded)
|
|||
|
||||
PythonInterpreter::~PythonInterpreter ()
|
||||
{
|
||||
for (auto m = m_modules.begin (); m != m_modules.end (); ++m) {
|
||||
(*m)->cleanup ();
|
||||
}
|
||||
|
||||
m_stdout_channel = PythonRef ();
|
||||
m_stderr_channel = PythonRef ();
|
||||
m_stdout = PythonPtr ();
|
||||
|
|
@ -370,6 +374,23 @@ PythonInterpreter::~PythonInterpreter ()
|
|||
if (m_embedded) {
|
||||
Py_Finalize ();
|
||||
}
|
||||
|
||||
for (auto m = m_modules.begin (); m != m_modules.end (); ++m) {
|
||||
delete *m;
|
||||
}
|
||||
m_modules.clear ();
|
||||
}
|
||||
|
||||
void
|
||||
PythonInterpreter::register_module (pya::PythonModule *module)
|
||||
{
|
||||
for (auto m = m_modules.begin (); m != m_modules.end (); ++m) {
|
||||
if (*m == module) {
|
||||
return; // already registered
|
||||
}
|
||||
}
|
||||
|
||||
m_modules.push_back (module);
|
||||
}
|
||||
|
||||
char *
|
||||
|
|
|
|||
|
|
@ -105,6 +105,14 @@ public:
|
|||
*/
|
||||
~PythonInterpreter ();
|
||||
|
||||
/**
|
||||
* @brief Registers a module
|
||||
*
|
||||
* The registered modules are cleaned up before the interpreter shuts down. The interpreter takes
|
||||
* ownership of the module object.
|
||||
*/
|
||||
void register_module (pya::PythonModule *module);
|
||||
|
||||
/**
|
||||
* @brief Add the given path to the search path
|
||||
*/
|
||||
|
|
@ -279,7 +287,7 @@ private:
|
|||
std::map<PyObject *, size_t> m_file_id_map;
|
||||
std::wstring mp_py3_app_name;
|
||||
bool m_embedded;
|
||||
std::unique_ptr<pya::PythonModule> m_pya_module;
|
||||
std::vector<pya::PythonModule *> m_modules;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -51,6 +51,9 @@ pya_object_deallocate (PyObject *self)
|
|||
// we better work around it.
|
||||
++self->ob_refcnt;
|
||||
|
||||
// Mute Python warnings in debug case
|
||||
PyObject_GC_UnTrack (self);
|
||||
|
||||
PYAObjectBase *p = PYAObjectBase::from_pyobject (self);
|
||||
p->~PYAObjectBase ();
|
||||
Py_TYPE (self)->tp_free (self);
|
||||
|
|
|
|||
|
|
@ -71,12 +71,6 @@ PythonModule::PythonModule ()
|
|||
|
||||
PythonModule::~PythonModule ()
|
||||
{
|
||||
PYAObjectBase::clear_callbacks_cache ();
|
||||
|
||||
// the Python objects were probably deleted by Python itself as it exited -
|
||||
// don't try to delete them again.
|
||||
mp_module.release ();
|
||||
|
||||
while (!m_methods_heap.empty ()) {
|
||||
delete m_methods_heap.back ();
|
||||
m_methods_heap.pop_back ();
|
||||
|
|
@ -93,6 +87,16 @@ PythonModule::~PythonModule ()
|
|||
}
|
||||
}
|
||||
|
||||
void
|
||||
PythonModule::cleanup ()
|
||||
{
|
||||
// the Python objects are probably deleted by Python itself as it exits -
|
||||
// don't try to delete them again in the destructor.
|
||||
mp_module.release ();
|
||||
|
||||
PYAObjectBase::clear_callbacks_cache ();
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PythonModule::module ()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -65,6 +65,12 @@ public:
|
|||
*/
|
||||
~PythonModule ();
|
||||
|
||||
/**
|
||||
* @brief Clean up the module
|
||||
* This method is called by the interpreter before Py_Finalize
|
||||
*/
|
||||
void cleanup ();
|
||||
|
||||
/**
|
||||
* @brief Initializes the module
|
||||
* This entry point is for external use where the module has not been created yet
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@
|
|||
|
||||
#include "pyaModule.h"
|
||||
#include "pyaUtils.h"
|
||||
#include "pya.h"
|
||||
|
||||
#include "gsi.h"
|
||||
#include "gsiExpression.h"
|
||||
|
|
@ -41,7 +42,12 @@
|
|||
static PyObject *
|
||||
module_init (const char *pymod_name, const char *mod_name, const char *mod_description)
|
||||
{
|
||||
static pya::PythonModule module;
|
||||
if (! pya::PythonInterpreter::instance ()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
pya::PythonModule *module = new pya::PythonModule ();
|
||||
pya::PythonInterpreter::instance ()->register_module (module);
|
||||
|
||||
PYA_TRY
|
||||
|
||||
|
|
@ -50,10 +56,10 @@ module_init (const char *pymod_name, const char *mod_name, const char *mod_descr
|
|||
// required for the tiling processor for example
|
||||
gsi::initialize_expressions ();
|
||||
|
||||
module.init (pymod_name, mod_description);
|
||||
module.make_classes (mod_name);
|
||||
module->init (pymod_name, mod_description);
|
||||
module->make_classes (mod_name);
|
||||
|
||||
return module.take_module ();
|
||||
return module->take_module ();
|
||||
|
||||
PYA_CATCH_ANYWHERE
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue