@@ -20,32 +20,25 @@ 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
- #ifdef _WIN32
29
- auto root_path=Config::get ().terminal .msys2_mingw_path ;
30
- append_path (root_path/" include/python3.5m" );
31
- append_path (root_path/" lib/python3.5" );
32
- long long unsigned size = 0L ;
33
- #else
34
- long unsigned size = 0L ;
35
- #endif
28
+
36
29
auto init_juci_api=[](){
37
- pybind11::module (pygobject_init (-1 ,-1 ,-1 ), false );
30
+ auto module = pybind11::reinterpret_steal<pybind11:: module> (pygobject_init (-1 ,-1 ,-1 ));
38
31
pybind11::module api (" jucpp" ," Python bindings for juCi++" );
39
32
api
40
- .def (" get_juci_home" ,[](){return Config::get ().juci_home_path () .string ();})
33
+ .def (" get_juci_home" ,[](){return Config::get ().home_juci_path .string ();})
41
34
.def (" get_plugin_folder" ,[](){return Config::get ().python .plugin_directory ;});
42
35
api
43
36
.def_submodule (" editor" )
44
37
.def (" get_current_gtk_source_view" ,[](){
45
38
auto view=Notebook::get ().get_current_view ();
46
39
if (view)
47
40
return pyobject_from_gobj (view->gobj ());
48
- return pybind11::module (Py_None, false );
41
+ return pybind11::reinterpret_steal<pybind11:: module> (Py_None);
49
42
})
50
43
.def (" get_file_path" ,[](){
51
44
auto view=Notebook::get ().get_current_view ();
@@ -55,13 +48,12 @@ Python::Interpreter::Interpreter(){
55
48
});
56
49
api
57
50
.def (" get_gio_plugin_menu" ,[](){
58
- auto &plugin_menu=Menu::get ().plugin_menu ;
59
- if (!plugin_menu){
60
- plugin_menu=Gio::Menu::create ();
61
- plugin_menu->append (" <empty>" );
62
- Menu::get ().window_menu ->append_submenu (" _Plugins" ,plugin_menu);
51
+ if (!Menu::get ().plugin_menu ){
52
+ Menu::get ().plugin_menu =Gio::Menu::create ();
53
+ Menu::get ().plugin_menu ->append (" <empty>" );
54
+ Menu::get ().window_menu ->append_submenu (" _Plugins" ,Menu::get ().plugin_menu );
63
55
}
64
- return pyobject_from_gobj (plugin_menu->gobj ());
56
+ return pyobject_from_gobj (Menu::get (). plugin_menu ->gobj ());
65
57
})
66
58
.def (" get_gio_window_menu" ,[](){return pyobject_from_gobj (Menu::get ().window_menu ->gobj ());})
67
59
.def (" get_gio_juci_menu" ,[](){return pyobject_from_gobj (Menu::get ().juci_menu ->gobj ());})
@@ -76,129 +68,87 @@ Python::Interpreter::Interpreter(){
76
68
return api.ptr ();
77
69
};
78
70
PyImport_AppendInittab (" jucipp" , init_juci_api);
71
+
79
72
Config::get ().load ();
80
- auto plugin_path=Config::get ().python .plugin_directory ;
81
- add_path (Config::get ().python .site_packages );
82
- add_path (plugin_path);
73
+ configure_path ();
83
74
Py_Initialize ();
75
+ #ifdef _WIN32
76
+ long long unsigned size = 0L ;
77
+ #else
78
+ long unsigned size = 0L ;
79
+ #endif
84
80
argv=DecodeLocale (" " ,&size);
85
81
PySys_SetArgv (0 ,&argv);
86
- auto sys=get_loaded_module (" sys" );
87
- auto exc_func=[](pybind11::object type,pybind11::object value,pybind11::object traceback){
88
- if (!given_exception_matches (type,PyExc_SyntaxError))
89
- Terminal::get ().print (Error (type,value,traceback),true );
90
- else
91
- Terminal::get ().print (SyntaxError (type,value,traceback),true );
92
- };
93
- sys.attr (" excepthook" )=pybind11::cpp_function (exc_func);
94
82
boost::filesystem::directory_iterator end_it;
95
- for (boost::filesystem::directory_iterator it (plugin_path );it!=end_it;it++){
83
+ for (boost::filesystem::directory_iterator it (Config::get (). python . plugin_directory );it!=end_it;it++){
96
84
auto module_name=it->path ().stem ().string ();
97
85
if (module_name.empty ())
98
- break ;
86
+ continue ;
99
87
auto is_directory=boost::filesystem::is_directory (it->path ());
100
88
auto has_py_extension=it->path ().extension ()==" .py" ;
101
89
auto is_pycache=module_name==" __pycache__" ;
102
90
if ((is_directory && !is_pycache)||has_py_extension){
103
- auto module=import (module_name);
104
- if (!module){
105
- auto msg=" Error loading plugin `" +module_name+" `:\n " ;
106
- auto err=std::string (Error ());
107
- 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 " );
108
95
}
109
96
}
110
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
+ }
111
107
}
112
108
113
- pybind11::module Python::get_loaded_module (const std::string &module_name){
114
- return pybind11::module (PyImport_AddModule (module_name.c_str ()), true );
115
- }
116
-
117
- pybind11::module Python::import (const std::string &module_name){
118
- return pybind11::module (PyImport_ImportModule (module_name.c_str ()), false );
119
- }
120
-
121
- pybind11::module Python::reload (pybind11::module &module){
122
- 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 ()));
123
111
}
124
112
125
- Python::SyntaxError::SyntaxError (pybind11::object type,pybind11::object value,pybind11::object traceback)
126
- : Error(type,value,traceback){}
127
-
128
- Python::Error::Error (pybind11::object type,pybind11::object value,pybind11::object traceback){
129
- exp =type;
130
- val=value;
131
- 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;
132
118
}
133
119
134
- void Python::Interpreter::add_path (const boost::filesystem::path &path){
135
- if (path.empty ())
136
- return ;
137
- std::wstring sys_path (Py_GetPath ());
138
- if (!sys_path.empty ())
139
- #ifdef _WIN32
140
- sys_path += ' ;' ;
141
- #else
142
- sys_path += ' :' ;
143
- #endif
144
- sys_path += path.generic_wstring ();
120
+ void Python::Interpreter::configure_path (){
121
+ const std::vector<boost::filesystem::path> python_path = {
122
+ " /usr/lib/python3.6" ,
123
+ " /usr/lib/python3.6/lib-dynload" ,
124
+ " /usr/lib/python3.6/site-packages" ,
125
+ Config::get ().python .site_packages ,
126
+ Config::get ().python .plugin_directory
127
+ };
128
+ std::wstring sys_path;
129
+ for (auto &path:python_path){
130
+ if (path.empty ())
131
+ continue ;
132
+ if (!sys_path.empty ()){
133
+ #ifdef _WIN32
134
+ sys_path += ' ;' ;
135
+ #else
136
+ sys_path += ' :' ;
137
+ #endif
138
+ }
139
+ sys_path += path.generic_wstring ();
140
+ }
145
141
Py_SetPath (sys_path.c_str ());
146
142
}
147
143
148
144
Python::Interpreter::~Interpreter (){
149
- auto err=Error ();
150
145
if (Py_IsInitialized ())
151
146
Py_Finalize ();
152
- if (err )
153
- std::cerr << std::string (err ) << std::endl;
147
+ if (error () )
148
+ std::cerr << pybind11::error_already_set (). what ( ) << std::endl;
154
149
}
155
150
156
- pybind11::object Python::error_occured (){
157
- return pybind11::object (PyErr_Occurred (), true );
151
+ pybind11::object Python::Interpreter::error (){
152
+ return pybind11::reinterpret_borrow<pybind11:: object> (PyErr_Occurred ());
158
153
}
159
154
160
- bool Python::thrown_exception_matches (pybind11::handle exception_type){
161
- return PyErr_ExceptionMatches (exception_type.ptr ());
162
- }
163
-
164
- bool Python::given_exception_matches (const pybind11::object &exception , pybind11::handle exception_type){
165
- return PyErr_GivenExceptionMatches (exception .ptr (),exception_type.ptr ());
166
- }
167
-
168
- Python::Error::Error (){
169
- if (error_occured ()){
170
- try {
171
- PyErr_Fetch (&exp .ptr (),&val.ptr (),&trace.ptr ());
172
- PyErr_NormalizeException (&exp .ptr (),&val.ptr (),&trace.ptr ());
173
- }catch (const std::exception &e) {
174
- Terminal::get ().print (e.what (),true );
175
- }
176
- }
177
- }
178
-
179
- Python::Error::operator std::string (){
180
- return std::string (exp .str ())+" \n " +std::string (val.str ())+" \n " ;
181
- }
182
-
183
- Python::SyntaxError::SyntaxError ():Error(){
184
- if (val){
185
- _Py_IDENTIFIER (msg);
186
- _Py_IDENTIFIER (lineno);
187
- _Py_IDENTIFIER (offset);
188
- _Py_IDENTIFIER (text);
189
- exp =std::string (pybind11::str (_PyObject_GetAttrId (val.ptr (),&PyId_msg),false ));
190
- text=std::string (pybind11::str (_PyObject_GetAttrId (val.ptr (),&PyId_text),false ));
191
- pybind11::object py_line_number (_PyObject_GetAttrId (val.ptr (),&PyId_lineno),false );
192
- pybind11::object py_line_offset (_PyObject_GetAttrId (val.ptr (),&PyId_offset),false );
193
- line_number=pybind11::cast<int >(py_line_number);
194
- line_offset=pybind11::cast<int >(py_line_offset);
195
- }
196
- }
197
-
198
- Python::SyntaxError::operator std::string (){
199
- return exp +" (" +std::to_string (line_number)+" :" +std::to_string (line_offset)+" ):\n " +text;
200
- }
201
-
202
- Python::Error::operator bool (){
203
- return exp || trace || val;
204
- }
0 commit comments