@@ -62,16 +62,23 @@ Python::Interpreter::Interpreter(){
6262 Py_Initialize ();
6363 argv=Py_DecodeLocale (" " ,&size);
6464 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);
6573 boost::filesystem::directory_iterator end_it;
6674 for (boost::filesystem::directory_iterator it (plugin_path);it!=end_it;it++){
6775 auto module_name=it->path ().stem ().string ();
6876 if (module_name!=" __pycache__" ){
6977 auto module =import (module_name);
7078 if (!module ){
79+ auto msg=" Error loading plugin `" +module_name+" `:\n " ;
7180 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 " );
7582 }
7683 }
7784 }
@@ -89,6 +96,15 @@ pybind11::module Python::reload(pybind11::module &module){
8996 return pybind11::module (PyImport_ReloadModule (module .ptr ()),false );
9097}
9198
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+
92108void Python::Interpreter::add_path (const boost::filesystem::path &path){
93109 if (path.empty ())
94110 return ;
@@ -115,14 +131,12 @@ pybind11::object Python::error_occured(){
115131 return pybind11::object (PyErr_Occurred (),true );
116132}
117133
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 ());
126140}
127141
128142Python::Error::Error (){
@@ -131,7 +145,7 @@ Python::Error::Error(){
131145 PyErr_Fetch (&exp.ptr (),&val.ptr (),&trace.ptr ());
132146 PyErr_NormalizeException (&exp.ptr (),&val.ptr (),&trace.ptr ());
133147 }catch (const std::exception &e) {
134- Terminal::get ().print (e.what ());
148+ Terminal::get ().print (e.what (), true );
135149 }
136150 }
137151}
@@ -160,5 +174,5 @@ Python::SyntaxError::operator std::string(){
160174}
161175
162176Python::Error::operator bool (){
163- return exp;
177+ return exp || trace || val ;
164178}
0 commit comments