Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Python binding code to separate class.
Fix Dialog enum's scope.
- Loading branch information
1 parent
ffd6d0b
commit f4588ac
Showing
7 changed files
with
182 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#include "PythonModule.h" | ||
|
||
namespace script | ||
{ | ||
|
||
const char* PythonModule::NAME() | ||
{ | ||
return "darkradiant"; | ||
} | ||
|
||
py::module& PythonModule::GetModule() | ||
{ | ||
if (!_module) | ||
{ | ||
_module.reset(new py::module(NAME())); | ||
} | ||
|
||
return *_module; | ||
} | ||
|
||
void PythonModule::RegisterToPython(const ModuleRegistrationCallback& callback) | ||
{ | ||
_registrationCallback = callback; | ||
|
||
// Register the darkradiant module to Python | ||
int result = PyImport_AppendInittab(NAME(), InitModule); | ||
|
||
if (result == -1) | ||
{ | ||
rError() << "Could not initialise Python module" << std::endl; | ||
return; | ||
} | ||
} | ||
|
||
py::dict& PythonModule::GetGlobals() | ||
{ | ||
if (!_globals) | ||
{ | ||
_globals.reset(new py::dict); | ||
} | ||
|
||
return *_globals; | ||
} | ||
|
||
void PythonModule::Clear() | ||
{ | ||
_module.reset(); | ||
_globals.reset(); | ||
} | ||
|
||
#if PY_MAJOR_VERSION >= 3 | ||
PyObject* PythonModule::InitModule() | ||
{ | ||
return InitModuleImpl(); | ||
} | ||
#else | ||
void PythonModule::InitModule() | ||
{ | ||
InitModuleImpl(); | ||
} | ||
#endif | ||
|
||
PyObject* PythonModule::InitModuleImpl() | ||
{ | ||
try | ||
{ | ||
// Acquire modules here (through callback?) | ||
if (_registrationCallback) | ||
{ | ||
_registrationCallback(GetModule(), GetGlobals()); | ||
} | ||
|
||
py::object main = py::module::import("__main__"); | ||
py::dict globals = main.attr("__dict__").cast<py::dict>(); | ||
|
||
for (auto& i : globals) | ||
{ | ||
GetGlobals()[i.first] = i.second; | ||
} | ||
|
||
return _module->ptr(); | ||
} | ||
catch (py::error_already_set& e) | ||
{ | ||
e.clear(); | ||
PyErr_SetString(PyExc_ImportError, e.what()); | ||
return nullptr; | ||
} | ||
catch (const std::exception& e) | ||
{ | ||
PyErr_SetString(PyExc_ImportError, e.what()); | ||
return nullptr; | ||
} | ||
} | ||
|
||
std::unique_ptr<py::module> PythonModule::_module; | ||
std::unique_ptr<py::dict> PythonModule::_globals; | ||
PythonModule::ModuleRegistrationCallback PythonModule::_registrationCallback; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#pragma once | ||
|
||
#include <functional> | ||
#include <memory> | ||
#include <pybind11/pybind11.h> | ||
|
||
namespace py = pybind11; | ||
|
||
namespace script | ||
{ | ||
|
||
class PythonModule | ||
{ | ||
private: | ||
// Python objects and initialisation stuff | ||
static std::unique_ptr<py::module> _module; | ||
static std::unique_ptr<py::dict> _globals; | ||
|
||
typedef std::function<void(py::module&, py::dict&)> ModuleRegistrationCallback; | ||
static ModuleRegistrationCallback _registrationCallback; | ||
|
||
public: | ||
static const char* NAME(); | ||
|
||
// Get the module object | ||
static py::module& GetModule(); | ||
|
||
// Get the globals | ||
static py::dict& GetGlobals(); | ||
|
||
static void RegisterToPython(const ModuleRegistrationCallback& callback); | ||
|
||
// Destroys the module and the globals object, no more calls to GetModule() and GetGlobals() afterwards! | ||
static void Clear(); | ||
|
||
// Endpoint called by the Python interface to acquire the module | ||
#if PY_MAJOR_VERSION >= 3 | ||
static PyObject* InitModule(); | ||
#else | ||
static void InitModule(); | ||
#endif | ||
|
||
private: | ||
static PyObject* InitModuleImpl(); | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.