Skip to content

Commit b60a73c

Browse files
committed
Get the correct computed tb lineno
Fix for python/cpython#109181 introduced lazily computed lineno for traceback object in 3.11.7 and 3.12.1. Tested in a number of Python versions, the change seems to be safe.
1 parent e080ab9 commit b60a73c

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/main/c/jni/org_jpy_PyLib.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2667,7 +2667,7 @@ void PyLib_RedirectStdOut(void)
26672667

26682668

26692669
static const int PYLIB_RECURSIVE_CUTOFF = 3;
2670-
#define PyLib_TraceBack_LIMIT 1024
2670+
#define PyLib_TraceBack_LIMIT 1024
26712671

26722672
static PyObject *format_displayline(PyObject *filename, int lineno, PyObject *name)
26732673
{
@@ -2730,6 +2730,10 @@ static int append_to_java_message(PyObject * pyObjUtf8, char **buf, int *bufLen
27302730
return 0;
27312731
}
27322732

2733+
static int get_traceback_lineno(PyTracebackObject *tb) {
2734+
return (int)PyLong_AsLong(PyObject_GetAttrString((PyObject*)tb, "tb_lineno"));
2735+
}
2736+
27332737
static int format_python_traceback(PyTracebackObject *tb, char **buf, int *bufLen)
27342738
{
27352739
int err = 0;
@@ -2754,7 +2758,7 @@ static int format_python_traceback(PyTracebackObject *tb, char **buf, int *bufLe
27542758
PyCodeObject* co = PyFrame_GetCode(tb->tb_frame);
27552759
if (last_file == NULL ||
27562760
co->co_filename != last_file ||
2757-
last_line == -1 || tb->tb_lineno != last_line ||
2761+
last_line == -1 || get_traceback_lineno(tb) != last_line ||
27582762
last_name == NULL || co->co_name != last_name) {
27592763
if (cnt > PYLIB_RECURSIVE_CUTOFF) {
27602764
pyObjUtf8 = format_line_repeated(cnt);
@@ -2765,15 +2769,15 @@ static int format_python_traceback(PyTracebackObject *tb, char **buf, int *bufLe
27652769
}
27662770
}
27672771
last_file = co->co_filename;
2768-
last_line = tb->tb_lineno;
2772+
last_line = get_traceback_lineno(tb);
27692773
last_name = co->co_name;
27702774
cnt = 0;
27712775
}
27722776
cnt++;
27732777
if (err == 0 && cnt <= PYLIB_RECURSIVE_CUTOFF) {
27742778
pyObjUtf8 = format_displayline(
27752779
co->co_filename,
2776-
tb->tb_lineno,
2780+
get_traceback_lineno(tb),
27772781
co->co_name);
27782782
err = append_to_java_message(pyObjUtf8, buf, bufLen);
27792783
if (err != 0) {

0 commit comments

Comments
 (0)