https://github.com/python/cpython/commit/ea26f6da39294b7d3c28873d070a2218bd528b5f
commit: ea26f6da39294b7d3c28873d070a2218bd528b5f
branch: main
author: Kumar Aditya <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2025-09-12T18:04:55+05:30
summary:

gh-138661: fix data race in `PyCode_Addr2Line` (#138664)

files:
M Include/internal/pycore_code.h
M Objects/codeobject.c
M Python/traceback.c

diff --git a/Include/internal/pycore_code.h b/Include/internal/pycore_code.h
index 8e1415f27b63f3..0f0804d5db61f7 100644
--- a/Include/internal/pycore_code.h
+++ b/Include/internal/pycore_code.h
@@ -274,6 +274,8 @@ extern void _PyLineTable_InitAddressRange(
 /** API for traversing the line number table. */
 extern int _PyLineTable_NextAddressRange(PyCodeAddressRange *range);
 extern int _PyLineTable_PreviousAddressRange(PyCodeAddressRange *range);
+// This is used in dump_frame() in traceback.c without an attached tstate.
+extern int _PyCode_Addr2LineNoTstate(PyCodeObject *co, int addr);
 
 /** API for executors */
 extern void _PyCode_Clear_Executors(PyCodeObject *code);
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index 55ba6ae372be41..0d264a6e346f95 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -1006,7 +1006,7 @@ PyCode_NewEmpty(const char *filename, const char 
*funcname, int firstlineno)
  ******************/
 
 int
-PyCode_Addr2Line(PyCodeObject *co, int addrq)
+_PyCode_Addr2LineNoTstate(PyCodeObject *co, int addrq)
 {
     if (addrq < 0) {
         return co->co_firstlineno;
@@ -1020,6 +1020,16 @@ PyCode_Addr2Line(PyCodeObject *co, int addrq)
     return _PyCode_CheckLineNumber(addrq, &bounds);
 }
 
+int
+PyCode_Addr2Line(PyCodeObject *co, int addrq)
+{
+    int lineno;
+    Py_BEGIN_CRITICAL_SECTION(co);
+    lineno = _PyCode_Addr2LineNoTstate(co, addrq);
+    Py_END_CRITICAL_SECTION();
+    return lineno;
+}
+
 void
 _PyLineTable_InitAddressRange(const char *linetable, Py_ssize_t length, int 
firstlineno, PyCodeAddressRange *range)
 {
diff --git a/Python/traceback.c b/Python/traceback.c
index da7956d1ec47b4..46106e52dbaf82 100644
--- a/Python/traceback.c
+++ b/Python/traceback.c
@@ -993,8 +993,8 @@ dump_frame(int fd, _PyInterpreterFrame *frame)
     } else {
         PUTS(fd, "???");
     }
-
-    int lineno = PyUnstable_InterpreterFrame_GetLine(frame);
+    int lasti = PyUnstable_InterpreterFrame_GetLasti(frame);
+    int lineno = _PyCode_Addr2LineNoTstate(code, lasti);
     PUTS(fd, ", line ");
     if (lineno >= 0) {
         _Py_DumpDecimal(fd, (size_t)lineno);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to