diff --git a/src/juci.cc b/src/juci.cc index a785df67..207f4fe2 100644 --- a/src/juci.cc +++ b/src/juci.cc @@ -113,7 +113,7 @@ void Application::on_startup() { set_app_menu(Menu::get().juci_menu); set_menubar(Menu::get().window_menu); } - PythonInterpreter::get(); + Python::Interpreter::get(); } Application::Application() : Gtk::Application("no.sout.juci", Gio::APPLICATION_NON_UNIQUE | Gio::APPLICATION_HANDLES_COMMAND_LINE) { diff --git a/src/python_interpreter.cc b/src/python_interpreter.cc index 605ca959..f8afc9d8 100644 --- a/src/python_interpreter.cc +++ b/src/python_interpreter.cc @@ -13,7 +13,7 @@ inline pybind11::module pyobject_from_gobj(gpointer ptr){ return pybind11::module(Py_None, false); } -PythonInterpreter::PythonInterpreter(){ +Python::Interpreter::Interpreter(){ auto init_juci_api=[](){ pybind11::module(pygobject_init(-1,-1,-1),false); pybind11::module api("jucpp","Python bindings for juCi++"); @@ -52,26 +52,29 @@ PythonInterpreter::PythonInterpreter(){ add_path(Config::get().python.site_packages); add_path(plugin_path); Py_Initialize(); + long unsigned size = 0L; + argv=Py_DecodeLocale("",&size); + PySys_SetArgv(0,&argv); boost::filesystem::directory_iterator end_it; for(boost::filesystem::directory_iterator it(plugin_path);it!=end_it;it++){ auto module_name=it->path().stem().string(); if(module_name!="__pycache__"){ auto module=import(module_name); if(!module) - std::cerr << std::string(PythonError()) << std::endl; + std::cerr << std::string(Error()) << std::endl; } } } -pybind11::module PythonInterpreter::get_loaded_module(const std::string &module_name){ +pybind11::module Python::Interpreter::get_loaded_module(const std::string &module_name){ return pybind11::module(PyImport_AddModule(module_name.c_str()), true); } -pybind11::module PythonInterpreter::import(const std::string &module_name){ +pybind11::module Python::Interpreter::import(const std::string &module_name){ return pybind11::module(PyImport_ImportModule(module_name.c_str()), false); } -void PythonInterpreter::add_path(const boost::filesystem::path &path){ +void Python::Interpreter::add_path(const boost::filesystem::path &path){ std::wstring sys_path(Py_GetPath()); if(!sys_path.empty()) #ifdef _WIN32 @@ -83,15 +86,15 @@ void PythonInterpreter::add_path(const boost::filesystem::path &path){ Py_SetPath(sys_path.c_str()); } -PythonInterpreter::~PythonInterpreter(){ - auto err=PythonError(); +Python::Interpreter::~Interpreter(){ + auto err=Error(); if(Py_IsInitialized()) Py_Finalize(); if(err) std::cerr << std::string(err) << std::endl; } -PythonError::PythonError(){ +Python::Error::Error(){ pybind11::object error(PyErr_Occurred(), false); if(error){ PyObject *exception,*value,*traceback; @@ -107,10 +110,10 @@ PythonError::PythonError(){ } } -PythonError::operator std::string(){ +Python::Error::operator std::string(){ return exp + "\n" + val + "\n" + trace; } -PythonError::operator bool(){ +Python::Error::operator bool(){ return !exp.empty(); } diff --git a/src/python_interpreter.h b/src/python_interpreter.h index 1b16365b..65b3d105 100644 --- a/src/python_interpreter.h +++ b/src/python_interpreter.h @@ -6,28 +6,28 @@ #include using namespace std; +namespace Python { + class Interpreter { + private: + Interpreter(); + ~Interpreter(); + wchar_t *argv; + public: + static Interpreter& get(){ + static Interpreter singleton; + return singleton; + } + pybind11::module get_loaded_module(const std::string &module_name); + pybind11::module import(const std::string &module_name); + void add_path(const boost::filesystem::path &path); + }; -class PythonInterpreter { -private: - PythonInterpreter(); - ~PythonInterpreter(); - wchar_t *argv; -public: - static PythonInterpreter& get(){ - static PythonInterpreter singleton; - return singleton; - } - pybind11::module get_loaded_module(const std::string &module_name); - pybind11::module import(const std::string &module_name); - void add_path(const boost::filesystem::path &path); -}; - -class PythonError { -public: - PythonError(); - operator std::string(); - operator bool(); - std::string exp, val, trace; -}; - + class Error { + public: + Error(); + operator std::string(); + operator bool(); + std::string exp, val, trace; + }; +} // namespace Python #endif // JUCI_PYTHON_INTERPRETER_H_ \ No newline at end of file diff --git a/src/window.cc b/src/window.cc index 526c7e79..c7839aee 100644 --- a/src/window.cc +++ b/src/window.cc @@ -254,15 +254,15 @@ void Window::set_menu_actions() { } if(file_path>Config::get().python.plugin_directory){ auto stem=file_path.stem().string(); - auto module=PythonInterpreter::get().get_loaded_module(stem); + auto module=Python::Interpreter::get().get_loaded_module(stem); if(module){ auto module_new=pybind11::module(PyImport_ReloadModule(module.ptr()),false); if(module_new) Terminal::get().print("Python module "+stem + " has been reloaded \n"); - else PythonError(); + else Python::Error(); }else{ - PythonError(); - module=PythonInterpreter::get().import(stem); + Python::Error(); + module=Python::Interpreter::get().import(stem); if(module) Terminal::get().print("Python module "+stem + " has been reloaded \n"); }