diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 226a78e..6a5c802 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -26,7 +26,6 @@ set(JUCI_SHARED_FILES tooltips.cpp usages_clang.cpp utility.cpp - python_interpreter.cc python_module.cc ) if(LIBLLDB_FOUND) diff --git a/src/plugins.cc b/src/plugins.cc index 6539060..ce51b7a 100644 --- a/src/plugins.cc +++ b/src/plugins.cc @@ -1,17 +1,30 @@ #include "plugins.h" +#include "config.h" #include "python_module.h" #include "terminal.h" -#include "config.h" Plugins::Plugins() : jucipp_module("Jucipp", Module::init_jucipp_module) { auto &config = Config::get(); config.load(); +#ifdef PYTHON_HOME_DIR +#ifdef _WIN32 + const std::wstring python_home(PYTHON_HOME_DIR); + const std::wstring python_path(python_home + L";" + python_home + L"\\lib-dynload;" + python_home + L"\\site-packages" ); + Py_SetPythonHome(python_home.c_str()); + Py_SetPath(python_path.c_str()); +#endif +#endif + py::initialize_interpreter(); py::module::import("sys") .attr("path") .cast() .append(config.plugins.path); } +Plugins::~Plugins() { + py::finalize_interpreter(); +} + void Plugins::load() { boost::filesystem::directory_iterator end_it; for(boost::filesystem::directory_iterator it(Config::get().plugins.path); it != end_it; it++) { diff --git a/src/plugins.h b/src/plugins.h index 056be25..84d98ac 100644 --- a/src/plugins.h +++ b/src/plugins.h @@ -1,14 +1,14 @@ #pragma once -#include "python_interpreter.h" +#include "python_bind.h" #include class __attribute__((visibility("default"))) Plugins { public: Plugins(); + ~Plugins(); void load(); private: py::detail::embedded_module jucipp_module; - Python::Interpreter interpreter; }; diff --git a/src/python_interpreter.cc b/src/python_interpreter.cc deleted file mode 100644 index 5d5d2e0..0000000 --- a/src/python_interpreter.cc +++ /dev/null @@ -1,39 +0,0 @@ -#include "python_interpreter.h" - -pybind11::module Python::Interpreter::add_module(const std::string &module_name) { - return pybind11::reinterpret_borrow(PyImport_AddModule(module_name.c_str())); -} - -pybind11::object Python::Interpreter::error() { - return pybind11::reinterpret_borrow(PyErr_Occurred()); -} - -pybind11::module Python::Interpreter::reload_module(pybind11::module &module) { - auto reload = pybind11::reinterpret_steal(PyImport_ReloadModule(module.ptr())); - if(!reload) { - throw pybind11::error_already_set(); - } - return reload; -} - -#include -#include - -Python::Interpreter::~Interpreter() { - if (error()){ - std::cout << py::error_already_set().what() << std::endl; - } - py::finalize_interpreter(); -} - -Python::Interpreter::Interpreter() { -#ifdef PYTHON_HOME_DIR -#ifdef _WIN32 - const std::wstring python_home(PYTHON_HOME_DIR); - const std::wstring python_path(python_home + L";" + python_home + L"\\lib-dynload;" + python_home + L"\\site-packages" ); - Py_SetPythonHome(python_home.c_str()); - Py_SetPath(python_path.c_str()); -#endif -#endif - py::initialize_interpreter(); -} diff --git a/src/python_interpreter.h b/src/python_interpreter.h deleted file mode 100644 index 1986ab0..0000000 --- a/src/python_interpreter.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once -#include "python_bind.h" - -namespace Python { - class Interpreter { - public: - pybind11::module static add_module(const std::string &module_name); - pybind11::module static reload_module(pybind11::module &module); - pybind11::object static error(); - ~Interpreter(); - Interpreter(); - }; -}; // namespace Python diff --git a/tests/python_bindings/test_suite.cc b/tests/python_bindings/test_suite.cc index ced4fcd..670b3f2 100644 --- a/tests/python_bindings/test_suite.cc +++ b/tests/python_bindings/test_suite.cc @@ -3,10 +3,12 @@ #include suite::suite(const boost::filesystem::path &path) { + py::initialize_interpreter(); auto sys = py::module::import("sys"); sys.attr("path").cast().append((test_file_path / path).string()); config.terminal.history_size = 100; } suite::~suite() { + py::finalize_interpreter(); g_assert_true(has_assertion); } diff --git a/tests/stubs/plugins.cc b/tests/stubs/plugins.cc index c375e43..0c7dec5 100644 --- a/tests/stubs/plugins.cc +++ b/tests/stubs/plugins.cc @@ -4,3 +4,5 @@ Plugins::Plugins() : jucipp_module("Jucipp", Module::init_jucipp_module) {} void Plugins::load() {} + +Plugins::~Plugins() {}