@@ -62,16 +62,23 @@ Python::Interpreter::Interpreter(){
62
62
Py_Initialize ();
63
63
argv=Py_DecodeLocale (" " ,&size);
64
64
PySys_SetArgv (0 ,&argv);
65
+ auto sys=get_loaded_module (" sys" );
66
+ auto exc_func=[](pybind11::object type,pybind11::object value,pybind11::object traceback){
67
+ if (!given_exception_matches (type,PyExc_ImportError))
68
+ Terminal::get ().print (Error (type,value,traceback),true );
69
+ else
70
+ Terminal::get ().print (SyntaxError (type,value,traceback),true );
71
+ };
72
+ sys.attr (" excepthook" )=pybind11::cpp_function (exc_func);
65
73
boost::filesystem::directory_iterator end_it;
66
74
for (boost::filesystem::directory_iterator it (plugin_path);it!=end_it;it++){
67
75
auto module_name=it->path ().stem ().string ();
68
76
if (module_name!=" __pycache__" ){
69
77
auto module=import (module_name);
70
78
if (!module){
79
+ auto msg=" Error loading plugin `" +module_name+" `:\n " ;
71
80
auto err=std::string (Error ());
72
- auto msg=" Error loading plugin " +module_name+" :\n " ;
73
- Terminal::get ().print (msg,true );
74
- Terminal::get ().print (err+" \n " );
81
+ Terminal::get ().print (msg+err+" \n " );
75
82
}
76
83
}
77
84
}
@@ -89,6 +96,15 @@ pybind11::module Python::reload(pybind11::module &module){
89
96
return pybind11::module (PyImport_ReloadModule (module.ptr ()),false );
90
97
}
91
98
99
+ Python::SyntaxError::SyntaxError (pybind11::object type,pybind11::object value,pybind11::object traceback)
100
+ : Error(type,value,traceback){}
101
+
102
+ Python::Error::Error (pybind11::object type,pybind11::object value,pybind11::object traceback){
103
+ exp =type;
104
+ val=value;
105
+ trace=traceback;
106
+ }
107
+
92
108
void Python::Interpreter::add_path (const boost::filesystem::path &path){
93
109
if (path.empty ())
94
110
return ;
@@ -115,14 +131,12 @@ pybind11::object Python::error_occured(){
115
131
return pybind11::object (PyErr_Occurred (),true );
116
132
}
117
133
118
- bool Python::thrown_exception_matches (Error::Type type){
119
- pybind11::object compare;
120
- switch (type){
121
- case Error::Type::Syntax : compare=pybind11::object (PyExc_SyntaxError,false );
122
- case Error::Type::Attribute : compare=pybind11::object (PyExc_AttributeError,false );
123
- case Error::Type::Import : compare=pybind11::object (PyExc_ImportError,false );
124
- }
125
- return PyErr_GivenExceptionMatches (Python::error_occured ().ptr (), compare.ptr ());
134
+ bool Python::thrown_exception_matches (pybind11::handle exception_type){
135
+ return PyErr_ExceptionMatches (exception_type.ptr ());
136
+ }
137
+
138
+ bool Python::given_exception_matches (const pybind11::object &exception , pybind11::handle exception_type){
139
+ return PyErr_GivenExceptionMatches (exception .ptr (),exception_type.ptr ());
126
140
}
127
141
128
142
Python::Error::Error (){
@@ -131,7 +145,7 @@ Python::Error::Error(){
131
145
PyErr_Fetch (&exp .ptr (),&val.ptr (),&trace.ptr ());
132
146
PyErr_NormalizeException (&exp .ptr (),&val.ptr (),&trace.ptr ());
133
147
}catch (const std::exception &e) {
134
- Terminal::get ().print (e.what ());
148
+ Terminal::get ().print (e.what (), true );
135
149
}
136
150
}
137
151
}
@@ -160,5 +174,5 @@ Python::SyntaxError::operator std::string(){
160
174
}
161
175
162
176
Python::Error::operator bool (){
163
- return exp ;
177
+ return exp || trace || val ;
164
178
}
0 commit comments