]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.9] bpo-45838: Fix incorrect line numbers in Tools/gdb/libpython.py (GH-29628)
authorSam Gross <colesbury@gmail.com>
Fri, 19 Nov 2021 18:53:46 +0000 (13:53 -0500)
committerGitHub <noreply@github.com>
Fri, 19 Nov 2021 18:53:46 +0000 (19:53 +0100)
The line number calculation in libpython.py did not properly handle
negative (signed) line table deltas.

Lib/test/test_gdb.py
Misc/NEWS.d/next/Tools-Demos/2021-11-18-11-20-21.bpo-45838.TH6mwc.rst [new file with mode: 0644]
Tools/gdb/libpython.py

index 5f554897f86a0b33de53e59a53e1263e0a7d0b32..b3aa855a5bd5eff45337331b65ac2509a4a048df 100644 (file)
@@ -955,6 +955,31 @@ id(42)
         self.assertRegex(gdb_output,
                          r"<method-wrapper u?'__init__' of MyList object at ")
 
+    @unittest.skipIf(python_is_optimized(),
+                     "Python was compiled with optimizations")
+    def test_try_finally_lineno(self):
+        cmd = textwrap.dedent('''
+            def foo(x):
+                try:
+                    raise RuntimeError("error")
+                    return x
+                except:
+                    id("break point")
+                finally:
+                    x += 2
+                return x
+            r = foo(3)
+        ''')
+        gdb_output = self.get_stack_trace(cmd,
+                                          cmds_after_breakpoint=["py-bt"])
+        self.assertMultilineMatches(gdb_output,
+                                    r'''^.*
+Traceback \(most recent call first\):
+  <built-in method id of module object .*>
+  File "<string>", line 7, in foo
+  File "<string>", line 11, in <module>
+''')
+
 
 class PyPrintTests(DebuggerTests):
     @unittest.skipIf(python_is_optimized(),
diff --git a/Misc/NEWS.d/next/Tools-Demos/2021-11-18-11-20-21.bpo-45838.TH6mwc.rst b/Misc/NEWS.d/next/Tools-Demos/2021-11-18-11-20-21.bpo-45838.TH6mwc.rst
new file mode 100644 (file)
index 0000000..b6dafc5
--- /dev/null
@@ -0,0 +1 @@
+Fix line number calculation when debugging Python with GDB.
index aeaa63e540d8b54b8296f429d77b68a985d5acbe..45be5ab9bb9b3c8b54e0ab782dab72090a84cc33 100755 (executable)
@@ -659,7 +659,10 @@ class PyCodeObjectPtr(PyObjectPtr):
             addr += ord(addr_incr)
             if addr > addrq:
                 return lineno
-            lineno += ord(line_incr)
+            line_delta = ord(line_incr)
+            if line_delta >= 128:
+                line_delta -= 256
+            lineno += line_delta
         return lineno