Skip to content
This repository was archived by the owner on Jun 25, 2020. It is now read-only.

Commit 94c68fd

Browse files
committed
cleanup: add python namespace
1 parent 4746eff commit 94c68fd

File tree

4 files changed

+41
-38
lines changed

4 files changed

+41
-38
lines changed

src/juci.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void Application::on_startup() {
113113
set_app_menu(Menu::get().juci_menu);
114114
set_menubar(Menu::get().window_menu);
115115
}
116-
PythonInterpreter::get();
116+
Python::Interpreter::get();
117117
}
118118

119119
Application::Application() : Gtk::Application("no.sout.juci", Gio::APPLICATION_NON_UNIQUE | Gio::APPLICATION_HANDLES_COMMAND_LINE) {

src/python_interpreter.cc

+13-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ inline pybind11::module pyobject_from_gobj(gpointer ptr){
1313
return pybind11::module(Py_None, false);
1414
}
1515

16-
PythonInterpreter::PythonInterpreter(){
16+
Python::Interpreter::Interpreter(){
1717
auto init_juci_api=[](){
1818
pybind11::module(pygobject_init(-1,-1,-1),false);
1919
pybind11::module api("jucpp","Python bindings for juCi++");
@@ -52,26 +52,29 @@ PythonInterpreter::PythonInterpreter(){
5252
add_path(Config::get().python.site_packages);
5353
add_path(plugin_path);
5454
Py_Initialize();
55+
long unsigned size = 0L;
56+
argv=Py_DecodeLocale("",&size);
57+
PySys_SetArgv(0,&argv);
5558
boost::filesystem::directory_iterator end_it;
5659
for(boost::filesystem::directory_iterator it(plugin_path);it!=end_it;it++){
5760
auto module_name=it->path().stem().string();
5861
if(module_name!="__pycache__"){
5962
auto module=import(module_name);
6063
if(!module)
61-
std::cerr << std::string(PythonError()) << std::endl;
64+
std::cerr << std::string(Error()) << std::endl;
6265
}
6366
}
6467
}
6568

66-
pybind11::module PythonInterpreter::get_loaded_module(const std::string &module_name){
69+
pybind11::module Python::Interpreter::get_loaded_module(const std::string &module_name){
6770
return pybind11::module(PyImport_AddModule(module_name.c_str()), true);
6871
}
6972

70-
pybind11::module PythonInterpreter::import(const std::string &module_name){
73+
pybind11::module Python::Interpreter::import(const std::string &module_name){
7174
return pybind11::module(PyImport_ImportModule(module_name.c_str()), false);
7275
}
7376

74-
void PythonInterpreter::add_path(const boost::filesystem::path &path){
77+
void Python::Interpreter::add_path(const boost::filesystem::path &path){
7578
std::wstring sys_path(Py_GetPath());
7679
if(!sys_path.empty())
7780
#ifdef _WIN32
@@ -83,15 +86,15 @@ void PythonInterpreter::add_path(const boost::filesystem::path &path){
8386
Py_SetPath(sys_path.c_str());
8487
}
8588

86-
PythonInterpreter::~PythonInterpreter(){
87-
auto err=PythonError();
89+
Python::Interpreter::~Interpreter(){
90+
auto err=Error();
8891
if(Py_IsInitialized())
8992
Py_Finalize();
9093
if(err)
9194
std::cerr << std::string(err) << std::endl;
9295
}
9396

94-
PythonError::PythonError(){
97+
Python::Error::Error(){
9598
pybind11::object error(PyErr_Occurred(), false);
9699
if(error){
97100
PyObject *exception,*value,*traceback;
@@ -107,10 +110,10 @@ PythonError::PythonError(){
107110
}
108111
}
109112

110-
PythonError::operator std::string(){
113+
Python::Error::operator std::string(){
111114
return exp + "\n" + val + "\n" + trace;
112115
}
113116

114-
PythonError::operator bool(){
117+
Python::Error::operator bool(){
115118
return !exp.empty();
116119
}

src/python_interpreter.h

+23-23
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@
66

77
#include <iostream>
88
using namespace std;
9+
namespace Python {
10+
class Interpreter {
11+
private:
12+
Interpreter();
13+
~Interpreter();
14+
wchar_t *argv;
15+
public:
16+
static Interpreter& get(){
17+
static Interpreter singleton;
18+
return singleton;
19+
}
20+
pybind11::module get_loaded_module(const std::string &module_name);
21+
pybind11::module import(const std::string &module_name);
22+
void add_path(const boost::filesystem::path &path);
23+
};
924

10-
class PythonInterpreter {
11-
private:
12-
PythonInterpreter();
13-
~PythonInterpreter();
14-
wchar_t *argv;
15-
public:
16-
static PythonInterpreter& get(){
17-
static PythonInterpreter singleton;
18-
return singleton;
19-
}
20-
pybind11::module get_loaded_module(const std::string &module_name);
21-
pybind11::module import(const std::string &module_name);
22-
void add_path(const boost::filesystem::path &path);
23-
};
24-
25-
class PythonError {
26-
public:
27-
PythonError();
28-
operator std::string();
29-
operator bool();
30-
std::string exp, val, trace;
31-
};
32-
25+
class Error {
26+
public:
27+
Error();
28+
operator std::string();
29+
operator bool();
30+
std::string exp, val, trace;
31+
};
32+
} // namespace Python
3333
#endif // JUCI_PYTHON_INTERPRETER_H_

src/window.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,15 @@ void Window::set_menu_actions() {
254254
}
255255
if(file_path>Config::get().python.plugin_directory){
256256
auto stem=file_path.stem().string();
257-
auto module=PythonInterpreter::get().get_loaded_module(stem);
257+
auto module=Python::Interpreter::get().get_loaded_module(stem);
258258
if(module){
259259
auto module_new=pybind11::module(PyImport_ReloadModule(module.ptr()),false);
260260
if(module_new)
261261
Terminal::get().print("Python module "+stem + " has been reloaded \n");
262-
else PythonError();
262+
else Python::Error();
263263
}else{
264-
PythonError();
265-
module=PythonInterpreter::get().import(stem);
264+
Python::Error();
265+
module=Python::Interpreter::get().import(stem);
266266
if(module)
267267
Terminal::get().print("Python module "+stem + " has been reloaded \n");
268268
}

0 commit comments

Comments
 (0)