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

Commit 240e58a

Browse files
committed
Simplify implementation
1 parent ea6f4db commit 240e58a

File tree

4 files changed

+55
-139
lines changed

4 files changed

+55
-139
lines changed

pybind11

Submodule pybind11 updated 190 files

src/python_interpreter.cc

Lines changed: 30 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ static wchar_t* DecodeLocale(const char* arg, size_t *size)
2020
inline pybind11::module pyobject_from_gobj(gpointer ptr){
2121
auto obj=G_OBJECT(ptr);
2222
if(obj)
23-
return pybind11::module(pygobject_new(obj), false);
24-
return pybind11::module(Py_None, false);
23+
return pybind11::reinterpret_steal<pybind11::module>(pygobject_new(obj));
24+
return pybind11::reinterpret_steal<pybind11::module>(Py_None);
2525
}
2626

2727
Python::Interpreter::Interpreter(){
28+
2829
auto init_juci_api=[](){
29-
pybind11::module(pygobject_init(-1,-1,-1),false);
30+
auto module = pybind11::reinterpret_steal<pybind11::module>(pygobject_init(-1,-1,-1));
3031
pybind11::module api("jucpp","Python bindings for juCi++");
3132
api
3233
.def("get_juci_home",[](){return Config::get().home_juci_path.string();})
@@ -37,7 +38,7 @@ Python::Interpreter::Interpreter(){
3738
auto view=Notebook::get().get_current_view();
3839
if(view)
3940
return pyobject_from_gobj(view->gobj());
40-
return pybind11::module(Py_None,false);
41+
return pybind11::reinterpret_steal<pybind11::module>(Py_None);
4142
})
4243
.def("get_file_path",[](){
4344
auto view=Notebook::get().get_current_view();
@@ -67,6 +68,7 @@ Python::Interpreter::Interpreter(){
6768
return api.ptr();
6869
};
6970
PyImport_AppendInittab("jucipp", init_juci_api);
71+
7072
Config::get().load();
7173
configure_path();
7274
Py_Initialize();
@@ -77,14 +79,6 @@ Python::Interpreter::Interpreter(){
7779
#endif
7880
argv=DecodeLocale("",&size);
7981
PySys_SetArgv(0,&argv);
80-
auto sys=get_loaded_module("sys");
81-
auto exc_func=[](pybind11::object type,pybind11::object value,pybind11::object traceback){
82-
if(!given_exception_matches(type,PyExc_SyntaxError))
83-
Terminal::get().print(Error(type,value,traceback),true);
84-
else
85-
Terminal::get().print(SyntaxError(type,value,traceback),true);
86-
};
87-
sys.attr("excepthook")=pybind11::cpp_function(exc_func);
8882
boost::filesystem::directory_iterator end_it;
8983
for(boost::filesystem::directory_iterator it(Config::get().python.plugin_directory);it!=end_it;it++){
9084
auto module_name=it->path().stem().string();
@@ -94,35 +88,33 @@ Python::Interpreter::Interpreter(){
9488
auto has_py_extension=it->path().extension()==".py";
9589
auto is_pycache=module_name=="__pycache__";
9690
if((is_directory && !is_pycache)||has_py_extension){
97-
auto module=import(module_name);
98-
if(!module){
99-
auto msg="Error loading plugin `"+module_name+"`:\n";
100-
auto err=std::string(Error());
101-
Terminal::get().print(msg+err+"\n");
91+
try {
92+
pybind11::module::import(module_name.c_str());
93+
} catch (pybind11::error_already_set &error) {
94+
Terminal::get().print("Error loading plugin `"+module_name+"`:\n"+error.what()+"\n");
10295
}
10396
}
10497
}
98+
auto sys=find_module("sys");
99+
if(sys){
100+
auto exc_func=[](pybind11::object type,pybind11::object value,pybind11::object traceback){
101+
std::cerr << "ERROR FUNCTION";
102+
};
103+
sys.attr("excepthook")=pybind11::cpp_function(exc_func);
104+
} else {
105+
std::cerr << "Failed to set exception hook\n";
106+
}
105107
}
106108

107-
pybind11::module Python::get_loaded_module(const std::string &module_name){
108-
return pybind11::module(PyImport_AddModule(module_name.c_str()), true);
109-
}
110-
111-
pybind11::module Python::import(const std::string &module_name){
112-
return pybind11::module(PyImport_ImportModule(module_name.c_str()), false);
113-
}
114-
115-
pybind11::module Python::reload(pybind11::module &module){
116-
return pybind11::module(PyImport_ReloadModule(module.ptr()),false);
109+
pybind11::module Python::Interpreter::find_module(const std::string &module_name){
110+
return pybind11::reinterpret_borrow<pybind11::module>(PyImport_AddModule(module_name.c_str()));
117111
}
118112

119-
Python::SyntaxError::SyntaxError(pybind11::object type,pybind11::object value,pybind11::object traceback)
120-
: Error(type,value,traceback){}
121-
122-
Python::Error::Error(pybind11::object type,pybind11::object value,pybind11::object traceback){
123-
exp=type;
124-
val=value;
125-
trace=traceback;
113+
pybind11::module Python::Interpreter::reload(pybind11::module &module){
114+
auto reload=pybind11::reinterpret_steal<pybind11::module>(PyImport_ReloadModule(module.ptr()));
115+
if(!reload)
116+
throw pybind11::error_already_set();
117+
return reload;
126118
}
127119

128120
void Python::Interpreter::configure_path(){
@@ -150,59 +142,13 @@ void Python::Interpreter::configure_path(){
150142
}
151143

152144
Python::Interpreter::~Interpreter(){
153-
auto err=Error();
154145
if(Py_IsInitialized())
155146
Py_Finalize();
156-
if(err)
157-
std::cerr << std::string(err) << std::endl;
158-
}
159-
160-
pybind11::object Python::error_occured(){
161-
return pybind11::object(PyErr_Occurred(),true);
162-
}
163-
164-
bool Python::thrown_exception_matches(pybind11::handle exception_type){
165-
return PyErr_ExceptionMatches(exception_type.ptr());
147+
if(error())
148+
std::cerr << pybind11::error_already_set().what() << std::endl;
166149
}
167150

168-
bool Python::given_exception_matches(const pybind11::object &exception, pybind11::handle exception_type){
169-
return PyErr_GivenExceptionMatches(exception.ptr(),exception_type.ptr());
151+
pybind11::object Python::Interpreter::error(){
152+
return pybind11::reinterpret_borrow<pybind11::object>(PyErr_Occurred());
170153
}
171154

172-
Python::Error::Error(){
173-
if(error_occured()){
174-
try{
175-
PyErr_Fetch(&exp.ptr(),&val.ptr(),&trace.ptr());
176-
PyErr_NormalizeException(&exp.ptr(),&val.ptr(),&trace.ptr());
177-
}catch(const std::exception &e) {
178-
Terminal::get().print(e.what(),true);
179-
}
180-
}
181-
}
182-
183-
Python::Error::operator std::string(){
184-
return std::string(exp.str())+"\n"+std::string(val.str())+"\n";
185-
}
186-
187-
Python::SyntaxError::SyntaxError():Error(){
188-
if(val){
189-
_Py_IDENTIFIER(msg);
190-
_Py_IDENTIFIER(lineno);
191-
_Py_IDENTIFIER(offset);
192-
_Py_IDENTIFIER(text);
193-
exp=std::string(pybind11::str(_PyObject_GetAttrId(val.ptr(),&PyId_msg),false));
194-
text=std::string(pybind11::str(_PyObject_GetAttrId(val.ptr(),&PyId_text),false));
195-
pybind11::object py_line_number(_PyObject_GetAttrId(val.ptr(),&PyId_lineno),false);
196-
pybind11::object py_line_offset(_PyObject_GetAttrId(val.ptr(),&PyId_offset),false);
197-
line_number=pybind11::cast<int>(py_line_number);
198-
line_offset=pybind11::cast<int>(py_line_offset);
199-
}
200-
}
201-
202-
Python::SyntaxError::operator std::string(){
203-
return exp+" ("+std::to_string(line_number)+":"+std::to_string(line_offset)+"):\n"+text;
204-
}
205-
206-
Python::Error::operator bool(){
207-
return exp || trace || val;
208-
}

src/python_interpreter.h

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
#include <iostream>
88
using namespace std;
99

10-
class Python {
11-
public:
10+
namespace Python {
1211
class Interpreter {
12+
public:
13+
pybind11::module static find_module(const std::string &module_name);
14+
pybind11::module static reload(pybind11::module &module);
15+
pybind11::object static error();
1316
private:
1417
Interpreter();
1518
~Interpreter();
@@ -21,34 +24,6 @@ class Python {
2124
return singleton;
2225
}
2326
};
24-
25-
pybind11::module static get_loaded_module(const std::string &module_name);
26-
pybind11::module static import(const std::string &module_name);
27-
pybind11::module static reload(pybind11::module &module);
28-
29-
class Error {
30-
public:
31-
Error();
32-
Error(pybind11::object type,pybind11::object value,pybind11::object traceback);
33-
operator std::string();
34-
operator bool();
35-
pybind11::object exp, val, trace;
36-
};
37-
38-
class SyntaxError : public Error{
39-
public:
40-
SyntaxError();
41-
SyntaxError(pybind11::object type,pybind11::object value,pybind11::object traceback);
42-
operator std::string();
43-
std::string exp, text;
44-
int line_number, line_offset;
45-
};
46-
47-
bool static thrown_exception_matches(pybind11::handle exception_type);
48-
bool static given_exception_matches(const pybind11::object &exception,pybind11::handle exception_type);
49-
50-
private:
51-
pybind11::object static error_occured();
5227
};
5328

5429
#endif // JUCI_PYTHON_INTERPRETER_H_

src/window.cc

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -396,38 +396,33 @@ void Window::set_menu_actions() {
396396
Notebook::get().get_view(c)->configure();
397397
Notebook::get().configure(c);
398398
}
399-
if(view->file_path>Config::get().python.plugin_directory){
400-
auto stem=view->file_path.stem().string();
401-
auto module=Python::get_loaded_module(stem);
402-
if(module){
403-
auto module_new=pybind11::module(PyImport_ReloadModule(module.ptr()),false);
404-
if(module_new)
405-
Terminal::get().print("Python module "+stem + " has been reloaded \n");
406-
else Python::Error();
407-
}else{
408-
Python::Error();
409-
module=Python::import(stem);
410-
if(module)
411-
Terminal::get().print("Python module "+stem + " has been reloaded \n");
399+
}
400+
const auto refresh_module = [](const std::string &stem){
401+
auto module = Python::Interpreter::find_module(stem);
402+
if(module) {
403+
try {
404+
module = pybind11::reinterpret_steal<pybind11::module>(PyImport_ReloadModule(module.ptr()));
405+
} catch (const pybind11::error_already_set &error) {
406+
Terminal::get().print("Plugin `"+stem+"` didn't reload\n"+error.what()+"\n");
407+
}
408+
} else {
409+
{ pybind11::error_already_set(); }
410+
try {
411+
module = pybind11::module::import(stem.c_str());
412+
} catch (const pybind11::error_already_set &error) {
413+
Terminal::get().print("Plugin `"+stem+"` didn't reload\n"+error.what()+"\n");
412414
}
413415
}
414-
}
416+
if(module)
417+
Terminal::get().print("Plugin `"+stem+"` was reloaded\n");
418+
};
415419
if(view->file_path.extension().string()==".py"){
416420
auto file_path=view->file_path;
417421
while(file_path.has_parent_path()){
418422
auto parent=file_path.parent_path();
419423
if(parent==Config::get().python.plugin_directory){
420424
auto stem=file_path.stem().string();
421-
auto module=Python::get_loaded_module(stem);
422-
module=module ? Python::reload(module) : Python::import(stem);
423-
if(module)
424-
Terminal::get().print("Plugin `"+stem+"` was reloaded\n");
425-
else {
426-
if(Python::thrown_exception_matches(PyExc_SyntaxError))
427-
Terminal::get().print(Python::SyntaxError());
428-
else
429-
Terminal::get().print(Python::Error());
430-
}
425+
refresh_module(stem);
431426
break;
432427
}
433428
file_path=parent;

0 commit comments

Comments
 (0)