diff --git a/src/klayout_main/klayout_main/klayout.cc b/src/klayout_main/klayout_main/klayout.cc index c679c5536..e215f7407 100644 --- a/src/klayout_main/klayout_main/klayout.cc +++ b/src/klayout_main/klayout_main/klayout.cc @@ -65,8 +65,6 @@ # include "gsiQtDesignerExternals.h" # include "gsiQtUiToolsExternals.h" -// pulls in the Qt GSI binding modules - need to be force loaded so they are available -// the pya Python module (Python >= 3.8 does not recognize DLL paths on Windows) FORCE_LINK_GSI_QTCORE FORCE_LINK_GSI_QTGUI FORCE_LINK_GSI_QTWIDGETS @@ -283,9 +281,6 @@ klayout_main_cont (int &argc, char **argv) try { - // initialize the Python interpreter - pya::PythonInterpreter::initialize (); - // this registers the gsi definitions gsi::initialize_external (); diff --git a/src/lay/lay/layApplication.cc b/src/lay/lay/layApplication.cc index e23d5c50d..e9d7bc7e0 100644 --- a/src/lay/lay/layApplication.cc +++ b/src/lay/lay/layApplication.cc @@ -616,6 +616,9 @@ ApplicationBase::init_app () mp_ruby_interpreter = new rba::RubyInterpreter (); mp_python_interpreter = new pya::PythonInterpreter (); + // initialize the Python interpreter - load the pya module + pya::PythonInterpreter::initialize (); + // Read some configuration values that we need early bool editable_from_config = false; diff --git a/src/pya/pya/pya.cc b/src/pya/pya/pya.cc index 38fd8e5c1..5da36d62e 100644 --- a/src/pya/pya/pya.cc +++ b/src/pya/pya/pya.cc @@ -326,16 +326,9 @@ PythonInterpreter::PythonInterpreter (bool embedded) tl::warn << tl::to_string (tr ("Unable to find built-in Python module library path")); } - // Import the pya module - PyObject *pya_module = PyImport_ImportModule (pya_module_name); - if (pya_module == NULL) { - check_error (); - return; - } - // Build two objects that provide a way to redirect stdout, stderr // and instantiate them two times for stdout and stderr. - PYAChannelObject::make_class (pya_module); + PYAChannelObject::make_class (); m_stdout_channel = PythonRef (PYAChannelObject::create (gsi::Console::OS_stdout)); m_stdout = PythonPtr (m_stdout_channel.get ()); m_stderr_channel = PythonRef (PYAChannelObject::create (gsi::Console::OS_stderr)); @@ -601,7 +594,11 @@ PythonInterpreter::available () const void PythonInterpreter::initialize () { - // .. no implementation required .. + // Import the pya module + PyObject *pya_module = PyImport_ImportModule (pya_module_name); + if (pya_module == NULL) { + check_error (); + } } size_t diff --git a/src/pya/pya/pya.h b/src/pya/pya/pya.h index 65fa77cb9..c48bad587 100644 --- a/src/pya/pya/pya.h +++ b/src/pya/pya/pya.h @@ -248,6 +248,7 @@ public: /** * @brief Provide a first (basic) initialization + * Calling this method will load all Python functions and plugins and provide the pya module. */ static void initialize (); diff --git a/src/pya/pya/pyaHelpers.cc b/src/pya/pya/pyaHelpers.cc index fc68b7d2d..c8aa44c60 100644 --- a/src/pya/pya/pyaHelpers.cc +++ b/src/pya/pya/pyaHelpers.cc @@ -102,11 +102,11 @@ pya_channel_init (PyObject *self, PyObject *, PyObject *) } void -PYAChannelObject::make_class (PyObject *module) +PYAChannelObject::make_class () { static PyTypeObject channel_type = { PyVarObject_HEAD_INIT (&PyType_Type, 0) - "pya._Channel", // tp_name + "__PYA_Channel", // tp_name sizeof (PYAChannelObject) // tp_size }; @@ -124,7 +124,8 @@ PYAChannelObject::make_class (PyObject *module) PyType_Ready (&channel_type); Py_INCREF (&channel_type); - PyModule_AddObject (module, "_Channel", (PyObject *) &channel_type); + PyObject *module = PyImport_AddModule("__main__"); + PyModule_AddObject (module, "__PYA_Channel", (PyObject *) &channel_type); cls = &channel_type; } diff --git a/src/pya/pya/pyaHelpers.h b/src/pya/pya/pyaHelpers.h index 67ba2ffbf..89ccf9585 100644 --- a/src/pya/pya/pyaHelpers.h +++ b/src/pya/pya/pyaHelpers.h @@ -44,7 +44,7 @@ class SignalHandler; struct PYAChannelObject : public PyObject { - static void make_class (PyObject *module); + static void make_class (); static PYAChannelObject *create (gsi::Console::output_stream chn); gsi::Console::output_stream channel; diff --git a/src/unit_tests/unit_test_main.cc b/src/unit_tests/unit_test_main.cc index c7ac22612..601a37998 100644 --- a/src/unit_tests/unit_test_main.cc +++ b/src/unit_tests/unit_test_main.cc @@ -490,7 +490,6 @@ main_cont (int &argc, char **argv) try { - pya::PythonInterpreter::initialize (); gsi::initialize_external (); // Search and initialize plugin unit tests