From 7ac51337cad9be8002e3673491657bcf8d698893 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Fri, 28 Jul 2023 21:28:09 +0200 Subject: [PATCH] Script errors now include the class for better readability in Python, normalizing errors in Python --- src/lay/lay/layApplication.cc | 4 ++-- src/pya/pya/pyaUtils.cc | 20 +++++++++++--------- src/tl/tl/tlScriptError.cc | 24 ++++++++++++++++++++---- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/lay/lay/layApplication.cc b/src/lay/lay/layApplication.cc index aef4d5042..6a825293d 100644 --- a/src/lay/lay/layApplication.cc +++ b/src/lay/lay/layApplication.cc @@ -124,9 +124,9 @@ static void ui_exception_handler_tl (const tl::Exception &ex, QWidget *parent) if (gsi_excpt->line () > 0) { tl::error << gsi_excpt->sourcefile () << ":" << gsi_excpt->line () << ": " - << gsi_excpt->msg () << tl::to_string (QObject::tr (" (class ")) << gsi_excpt->cls () << ")"; + << gsi_excpt->msg (); } else { - tl::error << gsi_excpt->msg () << tl::to_string (QObject::tr (" (class ")) << gsi_excpt->cls () << ")"; + tl::error << gsi_excpt->msg (); } lay::RuntimeErrorForm error_dialog (parent, "ruby_error_form", gsi_excpt); diff --git a/src/pya/pya/pyaUtils.cc b/src/pya/pya/pyaUtils.cc index 939718d7b..a279bac18 100644 --- a/src/pya/pya/pyaUtils.cc +++ b/src/pya/pya/pyaUtils.cc @@ -44,17 +44,19 @@ void check_error () { PyObject *py_exc_type = NULL, *py_exc_value = NULL, *py_exc_traceback = NULL; PyErr_Fetch (&py_exc_type, &py_exc_value, &py_exc_traceback); - PythonRef exc_type (py_exc_type); - PythonRef exc_value (py_exc_value); - PythonRef exc_traceback (py_exc_traceback); + if (py_exc_type != NULL) { - std::string exc_cls ("unknown"); - const char *c = ((PyTypeObject *) exc_type.get ())->tp_name; - if (c) { - exc_cls = c; - } + PyErr_NormalizeException (&py_exc_type, &py_exc_value, &py_exc_traceback); - if (exc_type) { + PythonRef exc_type (py_exc_type); + PythonRef exc_value (py_exc_value); + PythonRef exc_traceback (py_exc_traceback); + + std::string exc_cls ("unknown"); + const char *c = ((PyTypeObject *) exc_type.get ())->tp_name; + if (c) { + exc_cls = c; + } // fetch traceback // TODO: really decref the stack trace? how about the other objects in the stack trace? diff --git a/src/tl/tl/tlScriptError.cc b/src/tl/tl/tlScriptError.cc index 67065fc74..aae4e56f9 100644 --- a/src/tl/tl/tlScriptError.cc +++ b/src/tl/tl/tlScriptError.cc @@ -79,14 +79,30 @@ BacktraceElement::to_string() const // ------------------------------------------------------------------- // ScriptError implementation +static std::string make_basic_msg (const char *text, const char *cls) +{ + std::string msg; + if (*cls) { + msg = cls; + } + if (*cls && *text) { + msg += ": "; + } + if (*text) { + msg += text; + } + return msg; +} + + ScriptError::ScriptError (const char *msg, const char *cls, const std::vector &backtrace) - : tl::Exception (msg), m_line (-1), m_cls (cls), m_backtrace (backtrace) + : tl::Exception (make_basic_msg (msg, cls)), m_line (-1), m_cls (cls), m_backtrace (backtrace) { // .. nothing yet .. } ScriptError::ScriptError (const char *msg, const char *sourcefile, int line, const char *cls, const std::vector &backtrace) - : tl::Exception (msg), m_sourcefile (sourcefile), m_line (line), m_cls (cls), m_backtrace (backtrace) + : tl::Exception (make_basic_msg (msg, cls)), m_sourcefile (sourcefile), m_line (line), m_cls (cls), m_backtrace (backtrace) { translate_includes (); } @@ -103,8 +119,8 @@ ScriptError::msg () const std::string m = basic_msg (); if (! m_context.empty ()) { - m += tl::to_string (tr (" in ")) + m_context; - } + m += tl::to_string (tr (" in ")) + m_context; + } for (std::vector::const_iterator bt = backtrace ().begin (); bt != backtrace ().end (); ++bt) { m += "\n ";