Skip to content

Commit 18b6165

Browse files
committed
pyosys: catch boost::python::error_already_set
* This catches exceptions from internal passes, printing them in a readable manner where the user would otherwise see an unspecified boost exception
1 parent 5b6baa3 commit 18b6165

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

misc/py_wrap_generator.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,10 +1579,15 @@ def gen_default_impl(self):
15791579
return_stmt = "return " if self.ret_type.name != "void" else ""
15801580

15811581
text += ")\n\t\t{"
1582-
text += "\n\t\t\tif (boost::python::override py_" + self.alias + " = this->get_override(\"py_" + self.alias + "\"))"
1583-
text += f"\n\t\t\t\t{return_stmt}" + call_string
1584-
text += "\n\t\t\telse"
1582+
text += "\n\t\t\tif (boost::python::override py_" + self.alias + " = this->get_override(\"py_" + self.alias + "\")) {"
1583+
text += "\n\t\t\t\ttry {"
1584+
text += f"\n\t\t\t\t\t{return_stmt}" + call_string
1585+
text += "\n\t\t\t\t} catch (boost::python::error_already_set &) {"
1586+
text += "\n\t\t\t\t\tlog_python_exception_as_error();"
1587+
text += "\n\t\t\t\t}"
1588+
text += "\n\t\t\t} else {"
15851589
text += f"\n\t\t\t\t{return_stmt}" + self.member_of.name + "::" + call_string
1590+
text += "\n\t\t\t}"
15861591
text += "\n\t\t}"
15871592

15881593
text += "\n\n\t\t" + self.ret_type.gen_text() + " default_py_" + self.alias + "("
@@ -2335,6 +2340,11 @@ def gen_wrappers(filename, debug_level_ = 0):
23352340
23362341
namespace YOSYS_PYTHON {
23372342
2343+
[[noreturn]] static void log_python_exception_as_error() {
2344+
PyErr_Print();
2345+
log_error("Python interpreter encountered an exception.\\n");
2346+
}
2347+
23382348
struct YosysStatics{};
23392349
""")
23402350

0 commit comments

Comments
 (0)