From 18b616578a05e49d87e02530692e8d63338522cd Mon Sep 17 00:00:00 2001 From: George Rennie Date: Mon, 4 Nov 2024 13:14:04 +0100 Subject: [PATCH] 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 --- misc/py_wrap_generator.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/misc/py_wrap_generator.py b/misc/py_wrap_generator.py index 0be07453e..c62e624d2 100644 --- a/misc/py_wrap_generator.py +++ b/misc/py_wrap_generator.py @@ -1579,10 +1579,15 @@ class WFunction: 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 + "(" @@ -2335,6 +2340,11 @@ USING_YOSYS_NAMESPACE namespace YOSYS_PYTHON { + [[noreturn]] static void log_python_exception_as_error() { + PyErr_Print(); + log_error("Python interpreter encountered an exception.\\n"); + } + struct YosysStatics{}; """)