From 27549e006d1f241a9f3f76be448ab1a66e4578a2 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Sat, 6 Jul 2024 00:23:42 +0200 Subject: [PATCH] Fixing issue #1771 - Python 3.11.7 introduced a new behavior for getting the stack trace line number --- src/pya/pya/pyaUtils.cc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/pya/pya/pyaUtils.cc b/src/pya/pya/pyaUtils.cc index d367923ff..459a5efff 100644 --- a/src/pya/pya/pyaUtils.cc +++ b/src/pya/pya/pyaUtils.cc @@ -64,10 +64,15 @@ void check_error () if (exc_traceback) { PyTracebackObject *traceback = (PyTracebackObject*) exc_traceback.get (); for (PyTracebackObject *t = traceback; t; t = t->tb_next) { + int lineno = t->tb_lineno; #if PY_VERSION_HEX >= 0x030B0000 - backtrace.push_back (tl::BacktraceElement (python2c (PyFrame_GetCode(t->tb_frame)->co_filename), t->tb_lineno)); + // since version 3.11.7, lineno may be -1 and indicates that the frame has to be inspected + if (lineno < 0) { + lineno = PyFrame_GetLineNumber(t->tb_frame); + } + backtrace.push_back (tl::BacktraceElement (python2c (PyFrame_GetCode(t->tb_frame)->co_filename), lineno)); #else - backtrace.push_back (tl::BacktraceElement (python2c (t->tb_frame->f_code->co_filename), t->tb_lineno)); + backtrace.push_back (tl::BacktraceElement (python2c (t->tb_frame->f_code->co_filename), lineno)); #endif } std::reverse (backtrace.begin (), backtrace.end ());