@@ -20,13 +20,14 @@ static wchar_t* DecodeLocale(const char* arg, size_t *size)
2020inline 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
2727Python::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
128120void Python::Interpreter::configure_path (){
@@ -150,59 +142,13 @@ void Python::Interpreter::configure_path(){
150142}
151143
152144Python::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- }
0 commit comments