Skip to content

Commit

Permalink
#5452: Interpreter initialisation is now handled in PythonModule inte…
Browse files Browse the repository at this point in the history
…rnally
  • Loading branch information
codereader committed Dec 20, 2020
1 parent 7323145 commit c27e326
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
32 changes: 32 additions & 0 deletions plugins/script/PythonModule.cpp
Expand Up @@ -5,6 +5,14 @@
#define QUOTE(x) Str(x)
#define PYBIND11_VERSION_STR (QUOTE(PYBIND11_VERSION_MAJOR) "." QUOTE(PYBIND11_VERSION_MINOR) "." QUOTE(PYBIND11_VERSION_PATCH))

#if (PYBIND11_VERSION_MAJOR > 2) || (PYBIND11_VERSION_MAJOR == 2 && PYBIND11_VERSION_MINOR >= 2)
#define DR_PYBIND_HAS_EMBED_H
#endif

#ifdef DR_PYBIND_HAS_EMBED_H
#include <pybind11/embed.h>
#endif

#include "itextstream.h"

namespace script
Expand All @@ -19,9 +27,33 @@ PythonModule::PythonModule(const NamedInterfaces& interfaceList) :

PythonModule::~PythonModule()
{
// Release the references to trigger the internal cleanup before Py_Finalize
_module.dec_ref();
_module.release();

_globals.dec_ref();
_globals.release();

// The finalize_interpreter() function is not available in older versions
#ifdef DR_PYBIND_HAS_EMBED_H
py::finalize_interpreter();
#else
Py_Finalize();
#endif

_instance = nullptr;
}

void PythonModule::initialise()
{
// The initialize_interpreter() function is not available in older versions
#ifdef DR_PYBIND_HAS_EMBED_H
py::initialize_interpreter();
#else
Py_Initialize();
#endif
}

void PythonModule::registerModule()
{
rMessage() << "Registering darkradiant module to Python using pybind11 version " <<
Expand Down
3 changes: 3 additions & 0 deletions plugins/script/PythonModule.h
Expand Up @@ -32,6 +32,9 @@ class PythonModule final
PythonModule(const NamedInterfaces& interfaceList);
~PythonModule();

// Starts up the interpreter, imports the darkradiant module
void initialise();

static const char* NAME();

// Get the module object
Expand Down
23 changes: 2 additions & 21 deletions plugins/script/ScriptingSystem.cpp
Expand Up @@ -5,14 +5,6 @@
#include <pybind11/stl_bind.h>
#include <pybind11/eval.h>

#if (PYBIND11_VERSION_MAJOR > 2) || (PYBIND11_VERSION_MAJOR == 2 && PYBIND11_VERSION_MINOR >= 2)
#define DR_PYBIND_HAS_EMBED_H
#endif

#ifdef DR_PYBIND_HAS_EMBED_H
#include <pybind11/embed.h>
#endif

#include "i18n.h"
#include "itextstream.h"
#include "iradiant.h"
Expand Down Expand Up @@ -189,12 +181,8 @@ sigc::signal<void>& ScriptingSystem::signal_onScriptsReloaded()

void ScriptingSystem::initialise()
{
// The finalize_interpreter() function is not available in older versions
#ifdef DR_PYBIND_HAS_EMBED_H
py::initialize_interpreter();
#else
Py_Initialize();
#endif
// Fire up the interpreter
_pythonModule->initialise();

{
try
Expand Down Expand Up @@ -471,13 +459,6 @@ void ScriptingSystem::shutdownModule()
_interfaces.clear();

_pythonModule.reset();

// The finalize_interpreter() function is not available in older versions
#ifdef DR_PYBIND_HAS_EMBED_H
py::finalize_interpreter();
#else
Py_Finalize();
#endif
}

} // namespace script

0 comments on commit c27e326

Please sign in to comment.