Skip to content

Commit 8f2c0f7

Browse files
gh-125884: Support breakpoint on functions with annotations (#125892)
1 parent 13c9fa3 commit 8f2c0f7

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

Lib/pdb.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def find_first_executable_line(code):
118118
return code.co_firstlineno
119119

120120
def find_function(funcname, filename):
121-
cre = re.compile(r'def\s+%s\s*[(]' % re.escape(funcname))
121+
cre = re.compile(r'def\s+%s(\s*\[.+\])?\s*[(]' % re.escape(funcname))
122122
try:
123123
fp = tokenize.open(filename)
124124
except OSError:
@@ -138,9 +138,12 @@ def find_function(funcname, filename):
138138

139139
if funcdef:
140140
try:
141-
funccode = compile(funcdef, filename, 'exec').co_consts[0]
141+
code = compile(funcdef, filename, 'exec')
142142
except SyntaxError:
143143
continue
144+
# We should always be able to find the code object here
145+
funccode = next(c for c in code.co_consts if
146+
isinstance(c, CodeType) and c.co_name == funcname)
144147
lineno_offset = find_first_executable_line(funccode)
145148
return funcname, filename, funcstart + lineno_offset - 1
146149
return None

Lib/test/test_pdb.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,42 @@ def test_pdb_breakpoint_commands():
363363
4
364364
"""
365365

366+
def test_pdb_breakpoint_on_annotated_function_def():
367+
"""Test breakpoints on function definitions with annotation.
368+
369+
>>> def foo[T]():
370+
... return 0
371+
372+
>>> def bar() -> int:
373+
... return 0
374+
375+
>>> def foobar[T]() -> int:
376+
... return 0
377+
378+
>>> reset_Breakpoint()
379+
380+
>>> def test_function():
381+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
382+
... pass
383+
384+
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
385+
... 'break foo',
386+
... 'break bar',
387+
... 'break foobar',
388+
... 'continue',
389+
... ]):
390+
... test_function()
391+
> <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[4]>(2)test_function()
392+
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
393+
(Pdb) break foo
394+
Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[0]>:2
395+
(Pdb) break bar
396+
Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[1]>:2
397+
(Pdb) break foobar
398+
Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[2]>:2
399+
(Pdb) continue
400+
"""
401+
366402
def test_pdb_commands():
367403
"""Test the commands command of pdb.
368404
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed the bug for :mod:`pdb` where it can't set breakpoints on functions with certain annotations.

0 commit comments

Comments
 (0)