From d1c28bc3bd2aabd76c4c32bc0bd97e2b47c219dd Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Tue, 17 Jan 2023 20:43:13 +0100 Subject: [PATCH] Provide a fix for issue-1237 (Python error messages should include more information) In Python 3.10 the format of the syntax error exception was changed and parsing through PyArgs_ParseTuple no longer worked. The code change will re-enable proper syntax error parsing for Python 3.10. --- src/pya/pya/pyaUtils.cc | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/pya/pya/pyaUtils.cc b/src/pya/pya/pyaUtils.cc index 46dcb806c..939718d7b 100644 --- a/src/pya/pya/pyaUtils.cc +++ b/src/pya/pya/pyaUtils.cc @@ -74,25 +74,35 @@ void check_error () if (PyErr_GivenExceptionMatches (exc_type.get (), PyExc_SyntaxError) && PyTuple_Check (exc_value.get ()) && PyTuple_Size (exc_value.get ()) >= 2) { const char *sourcefile = 0; + std::string sourcefile_arg; int line = 0; - std::string msg; + std::string msg = "syntax error (could not parse exception)"; - const char *msg_arg = 0, *sourcefile_arg = 0, *text_arg = 0; - int line_arg = 0, column_arg = 0; - if (exc_value && PyArg_ParseTuple (exc_value.get (), "s(siis)", &msg_arg, &sourcefile_arg, &line_arg, &column_arg, &text_arg)) { + try { - // build a Ruby-like message - msg = sourcefile_arg; - msg += ":"; - msg += tl::to_string (line_arg); - msg += ": "; - msg += msg_arg; + if (exc_value && PyTuple_Check (exc_value.get ()) && PyTuple_Size (exc_value.get ()) >= 2) { - sourcefile = sourcefile_arg; - line = line_arg; + std::string msg_arg = python2c (PyTuple_GetItem (exc_value.get (), 0)); - } else { - msg = "syntax error"; + PyObject *args = PyTuple_GetItem (exc_value.get (), 1); + if (PyTuple_Check (args) && PyTuple_Size (args) >= 3) { + sourcefile_arg = python2c (PyTuple_GetItem (args, 0)); + sourcefile = sourcefile_arg.c_str (); + line = python2c (PyTuple_GetItem (args, 1)); + // Not used: column_arg = python2c (PyTuple_GetItem (args, 2); + } + + // build a Ruby-like message + msg = sourcefile_arg; + msg += ":"; + msg += tl::to_string (line); + msg += ": "; + msg += msg_arg; + + } + + } catch (...) { + // ignore exceptions here } if (! backtrace.empty () && ! sourcefile) {