Skip to content

Commit

Permalink
Support breakpoint on functions with annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
gaogaotiantian committed Oct 23, 2024
1 parent 322f14e commit 05c3065
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def find_first_executable_line(code):
return code.co_firstlineno

def find_function(funcname, filename):
cre = re.compile(r'def\s+%s\s*[(]' % re.escape(funcname))
cre = re.compile(r'def\s+%s(\s*\[.+\])?\s*[(]' % re.escape(funcname))
try:
fp = tokenize.open(filename)
except OSError:
Expand All @@ -138,7 +138,13 @@ def find_function(funcname, filename):

if funcdef:
try:
funccode = compile(funcdef, filename, 'exec').co_consts[0]
code = compile(funcdef, filename, 'exec')
for const in code.co_consts:
if isinstance(const, CodeType) and const.co_name == funcname:
funccode = const
break
else:
continue
except SyntaxError:
continue
lineno_offset = find_first_executable_line(funccode)
Expand Down
36 changes: 36 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,42 @@ def test_pdb_breakpoint_commands():
4
"""

def test_pdb_breakpoint_on_annotated_function_def():
"""Test breakpoints on function definitions with annotation.
>>> def foo[T]():
... return 0
>>> def bar() -> int:
... return 0
>>> def foobar[T]() -> int:
... return 0
>>> reset_Breakpoint()
>>> def test_function():
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
... pass
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
... 'break foo',
... 'break bar',
... 'break foobar',
... 'continue',
... ]):
... test_function()
> <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[4]>(2)test_function()
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
(Pdb) break foo
Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[0]>:2
(Pdb) break bar
Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[1]>:2
(Pdb) break foobar
Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[2]>:2
(Pdb) continue
"""

def test_pdb_commands():
"""Test the commands command of pdb.
Expand Down

0 comments on commit 05c3065

Please sign in to comment.