8000 gh-59013: Make line number of function breakpoint more precise (#110582) · python/cpython@1c9a0c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1c9a0c4

Browse files
gh-59013: Make line number of function breakpoint more precise (#110582)
1 parent 77bb0d5 commit 1c9a0c4

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

Lib/pdb.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ def do_break(self, arg, temporary = 0):
887887
#use co_name to identify the bkpt (function names
888888
#could be aliased, but co_name is invariant)
889889
funcname = code.co_name
890-
lineno = code.co_firstlineno
890+
lineno = self._find_first_executable_line(code)
891891
filename = code.co_filename
892892
except:
893893
# last thing to try
@@ -990,6 +990,23 @@ def checkline(self, filename, lineno):
990990
return 0
991991
return lineno
992992

993+
def _find_first_executable_line(self, code):
994+
""" Try to find the first executable line of the code object.
995+
996+
Equivalently, find the line number of the instruction that's
997+
after RESUME
998+
999+
Return code.co_firstlineno if no executable line is found.
1000+
"""
1001+
prev = None
1002+
for instr in dis.get_instructions(code):
1003+
if prev is not None and prev.opname == 'RESUME':
1004+
if instr.positions.lineno is not None:
1005+
return instr.positions.lineno
1006+
return code.co_firstlineno
1007+
prev = instr
1008+
return code.co_firstlineno
1009+
9931010
def do_enable(self, arg):
9941011
"""enable bpnumber [bpnumber ...]
9951012

Lib/test/test_pdb.py

Lines changed: 44 additions & 2 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -1516,7 +1516,7 @@ def test_next_until_return_at_return_event():
15161516
> <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(3)test_function()
15171517
-> test_function_2()
15181518
(Pdb) break test_function_2
1519-
Breakpoint 1 at <doctest test.test_pdb.test_next_until_return_at_return_event[0]>:1
1519+
Breakpoint 1 at <doctest test.test_pdb.test_next_until_return_at_return_event[0]>:2
15201520
(Pdb) continue
15211521
> <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
15221522
-> x = 1
@@ -1938,7 +1938,7 @@ def test_pdb_next_command_in_generator_for_loop():
19381938
> <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function()
19391939
-> for i in test_gen():
19401940
(Pdb) break test_gen
1941-
Breakpoint 1 at <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>:1
1941+
Breakpoint 1 at <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>:2
19421942
(Pdb) continue
19431943
> <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(2)test_gen()
19441944
-> yield 0
@@ -2350,6 +2350,48 @@ def test_pdb_ambiguous_statements():
23502350
(Pdb) continue
23512351
"""
23522352

2353+
def test_pdb_function_break():
2354+
"""Testing the line number of break on function
2355+
2356+
>>> def foo(): pass
2357+
2358+
>>> def bar():
2359+
...
2360+
... pass
2361+
2362+
>>> def boo():
2363+
... # comments
2364+
... global x
2365+
... x = 1
2366+
2367+
>>> def gen():
2368+
... yield 42
2369+
2370+
>>> def test_function():
2371+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
2372+
... pass
2373+
2374+
>>> with PdbTestInput([ # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
2375+
... 'break foo',
2376+
... 'break bar',
2377+
... 'break boo',
2378+
... 'break gen',
2379+
... 'continue'
2380+
... ]):
2381+
... test_function()
2382+
> <doctest test.test_pdb.test_pdb_function_break[4]>(3)test_function()
2383+
-> pass
2384+
(Pdb) break foo
2385+
Breakpoint ... at <doctest test.test_pdb.test_pdb_function_break[0]>:1
2386+
(Pdb) break bar
2387+
Breakpoint ... at <doctest test.test_pdb.test_pdb_function_break[1]>:3
2388+
(Pdb) break boo
2389+
Breakpoint ... at <doctest test.test_pdb.test_pdb_function_break[2]>:4
2390+
(Pdb) break gen
2391+
Breakpoint ... at <doctest test.test_pdb.test_pdb_function_break[3]>:2
2392+
(Pdb) continue
2393+
"""
2394+
23532395
def test_pdb_issue_gh_65052():
23542396
"""See GH-65052
23552397
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make line number of function breakpoint more precise in :mod:`pdb`

0 commit comments

Comments
 (0)
0