@@ -20,13 +20,14 @@ static wchar_t* DecodeLocale(const char* arg, size_t *size)
20
20
inline pybind11::module pyobject_from_gobj (gpointer ptr){
21
21
auto obj=G_OBJECT (ptr);
22
22
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);
25
25
}
26
26
27
27
Python::Interpreter::Interpreter (){
28
+
28
29
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 ));
30
31
pybind11::module api (" jucpp" ," Python bindings for juCi++" );
31
32
api
32
33
.def (" get_juci_home" ,[](){return Config::get ().home_juci_path .string ();})
@@ -37,7 +38,7 @@ Python::Interpreter::Interpreter(){
37
38
auto view=Notebook::get ().get_current_view ();
38
39
if (view)
39
40
return pyobject_from_gobj (view->gobj ());
40
- return pybind11::module (Py_None, false );
41
+ return pybind11::reinterpret_steal<pybind11:: module> (Py_None);
41
42
})
42
43
.def (" get_file_path" ,[](){
43
44
auto view=Notebook::get ().get_current_view ();
@@ -67,6 +68,7 @@ Python::Interpreter::Interpreter(){
67
68
return api.ptr ();
68
69
};
69
70
PyImport_AppendInittab (" jucipp" , init_juci_api);
71
+
70
72
Config::get ().load ();
71
73
configure_path ();
72
74
Py_Initialize ();
@@ -77,14 +79,6 @@ Python::Interpreter::Interpreter(){
77
79
#endif
78
80
argv=DecodeLocale (" " ,&size);
79
81
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);
88
82
boost::filesystem::directory_iterator end_it;
89
83
for (boost::filesystem::directory_iterator it (Config::get ().python .plugin_directory );it!=end_it;it++){
90
84
auto module_name=it->path ().stem ().string ();
@@ -94,35 +88,33 @@ Python::Interpreter::Interpreter(){
94
88
auto has_py_extension=it->path ().extension ()==" .py" ;
95
89
auto is_pycache=module_name==" __pycache__" ;
96
90
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 " );
102
95
}
103
96
}
104
97
}
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
+ }
105
107
}
106
108
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 ()));
117
111
}
118
112
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;
126
118
}
127
119
128
120
void Python::Interpreter::configure_path (){
@@ -150,59 +142,13 @@ void Python::Interpreter::configure_path(){
150
142
}
151
143
152
144
Python::Interpreter::~Interpreter (){
153
- auto err=Error ();
154
145
if (Py_IsInitialized ())
155
146
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;
166
149
}
167
150
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 ());
170
153
}
171
154
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
- }
0 commit comments