Browse Source

remove interpreter wrapping

python
Jørgen Lien Sellæg 7 years ago committed by Jørgen Sverre Lien Sellæg
parent
commit
c519c9dd94
  1. 1
      src/CMakeLists.txt
  2. 15
      src/plugins.cc
  3. 4
      src/plugins.h
  4. 39
      src/python_interpreter.cc
  5. 13
      src/python_interpreter.h
  6. 2
      tests/python_bindings/test_suite.cc
  7. 2
      tests/stubs/plugins.cc

1
src/CMakeLists.txt

@ -26,7 +26,6 @@ set(JUCI_SHARED_FILES
tooltips.cpp tooltips.cpp
usages_clang.cpp usages_clang.cpp
utility.cpp utility.cpp
python_interpreter.cc
python_module.cc python_module.cc
) )
if(LIBLLDB_FOUND) if(LIBLLDB_FOUND)

15
src/plugins.cc

@ -1,17 +1,30 @@
#include "plugins.h" #include "plugins.h"
#include "config.h"
#include "python_module.h" #include "python_module.h"
#include "terminal.h" #include "terminal.h"
#include "config.h"
Plugins::Plugins() : jucipp_module("Jucipp", Module::init_jucipp_module) { Plugins::Plugins() : jucipp_module("Jucipp", Module::init_jucipp_module) {
auto &config = Config::get(); auto &config = Config::get();
config.load(); 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") py::module::import("sys")
.attr("path") .attr("path")
.cast<py::list>() .cast<py::list>()
.append(config.plugins.path); .append(config.plugins.path);
} }
Plugins::~Plugins() {
py::finalize_interpreter();
}
void Plugins::load() { void Plugins::load() {
boost::filesystem::directory_iterator end_it; boost::filesystem::directory_iterator end_it;
for(boost::filesystem::directory_iterator it(Config::get().plugins.path); it != end_it; it++) { for(boost::filesystem::directory_iterator it(Config::get().plugins.path); it != end_it; it++) {

4
src/plugins.h

@ -1,14 +1,14 @@
#pragma once #pragma once
#include "python_interpreter.h" #include "python_bind.h"
#include <pybind11/embed.h> #include <pybind11/embed.h>
class __attribute__((visibility("default"))) class __attribute__((visibility("default")))
Plugins { Plugins {
public: public:
Plugins(); Plugins();
~Plugins();
void load(); void load();
private: private:
py::detail::embedded_module jucipp_module; py::detail::embedded_module jucipp_module;
Python::Interpreter interpreter;
}; };

39
src/python_interpreter.cc

@ -1,39 +0,0 @@
#include "python_interpreter.h"
pybind11::module Python::Interpreter::add_module(const std::string &module_name) {
return pybind11::reinterpret_borrow<pybind11::module>(PyImport_AddModule(module_name.c_str()));
}
pybind11::object Python::Interpreter::error() {
return pybind11::reinterpret_borrow<pybind11::object>(PyErr_Occurred());
}
pybind11::module Python::Interpreter::reload_module(pybind11::module &module) {
auto reload = pybind11::reinterpret_steal<pybind11::module>(PyImport_ReloadModule(module.ptr()));
if(!reload) {
throw pybind11::error_already_set();
}
return reload;
}
#include <pybind11/embed.h>
#include <iostream>
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();
}

13
src/python_interpreter.h

@ -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

2
tests/python_bindings/test_suite.cc

@ -3,10 +3,12 @@
#include <iostream> #include <iostream>
suite::suite(const boost::filesystem::path &path) { suite::suite(const boost::filesystem::path &path) {
py::initialize_interpreter();
auto sys = py::module::import("sys"); auto sys = py::module::import("sys");
sys.attr("path").cast<py::list>().append((test_file_path / path).string()); sys.attr("path").cast<py::list>().append((test_file_path / path).string());
config.terminal.history_size = 100; config.terminal.history_size = 100;
} }
suite::~suite() { suite::~suite() {
py::finalize_interpreter();
g_assert_true(has_assertion); g_assert_true(has_assertion);
} }

2
tests/stubs/plugins.cc

@ -4,3 +4,5 @@
Plugins::Plugins() : jucipp_module("Jucipp", Module::init_jucipp_module) {} Plugins::Plugins() : jucipp_module("Jucipp", Module::init_jucipp_module) {}
void Plugins::load() {} void Plugins::load() {}
Plugins::~Plugins() {}

Loading…
Cancel
Save