Skip to content

Commit 2a32dbf

Browse files
authored
[3.9] bpo-45838: Fix incorrect line numbers in Tools/gdb/libpython.py (GH-29628)
The line number calculation in libpython.py did not properly handle negative (signed) line table deltas.
1 parent 4296396 commit 2a32dbf

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

Lib/test/test_gdb.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,31 @@ def __init__(self):
955955
self.assertRegex(gdb_output,
956956
r"<method-wrapper u?'__init__' of MyList object at ")
957957

958+
@unittest.skipIf(python_is_optimized(),
959+
"Python was compiled with optimizations")
960+
def test_try_finally_lineno(self):
961+
cmd = textwrap.dedent('''
962+
def foo(x):
963+
try:
964+
raise RuntimeError("error")
965+
return x
966+
except:
967+
id("break point")
968+
finally:
969+
x += 2
970+
return x
971+
r = foo(3)
972+
''')
973+
gdb_output = self.get_stack_trace(cmd,
974+
cmds_after_breakpoint=["py-bt"])
975+
self.assertMultilineMatches(gdb_output,
976+
r'''^.*
977+
Traceback \(most recent call first\):
978+
<built-in method id of module object .*>
979+
File "<string>", line 7, in foo
980+
File "<string>", line 11, in <module>
981+
''')
982+
958983

959984
class PyPrintTests(DebuggerTests):
960985
@unittest.skipIf(python_is_optimized(),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix line number calculation when debugging Python with GDB.

Tools/gdb/libpython.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,10 @@ def addr2line(self, addrq):
659659
addr += ord(addr_incr)
660660
if addr > addrq:
661661
return lineno
662-
lineno += ord(line_incr)
662+
line_delta = ord(line_incr)
663+
if line_delta >= 128:
664+
line_delta -= 256
665+
lineno += line_delta
663666
return lineno
664667

665668

0 commit comments

Comments
 (0)