Skip to content

Commit

Permalink
pyosys: catch boost::python::error_already_set
Browse files Browse the repository at this point in the history
* This catches exceptions from internal passes, printing them in a
  readable manner where the user would otherwise see an unspecified
  boost exception
  • Loading branch information
georgerennie committed Nov 20, 2024
1 parent 5b6baa3 commit 18b6165
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions misc/py_wrap_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1579,10 +1579,15 @@ def gen_default_impl(self):
return_stmt = "return " if self.ret_type.name != "void" else ""

text += ")\n\t\t{"
text += "\n\t\t\tif (boost::python::override py_" + self.alias + " = this->get_override(\"py_" + self.alias + "\"))"
text += f"\n\t\t\t\t{return_stmt}" + call_string
text += "\n\t\t\telse"
text += "\n\t\t\tif (boost::python::override py_" + self.alias + " = this->get_override(\"py_" + self.alias + "\")) {"
text += "\n\t\t\t\ttry {"
text += f"\n\t\t\t\t\t{return_stmt}" + call_string
text += "\n\t\t\t\t} catch (boost::python::error_already_set &) {"
text += "\n\t\t\t\t\tlog_python_exception_as_error();"
text += "\n\t\t\t\t}"
text += "\n\t\t\t} else {"
text += f"\n\t\t\t\t{return_stmt}" + self.member_of.name + "::" + call_string
text += "\n\t\t\t}"
text += "\n\t\t}"

text += "\n\n\t\t" + self.ret_type.gen_text() + " default_py_" + self.alias + "("
Expand Down Expand Up @@ -2335,6 +2340,11 @@ def gen_wrappers(filename, debug_level_ = 0):
namespace YOSYS_PYTHON {
[[noreturn]] static void log_python_exception_as_error() {
PyErr_Print();
log_error("Python interpreter encountered an exception.\\n");
}
struct YosysStatics{};
""")

Expand Down

0 comments on commit 18b6165

Please sign in to comment.