8000 [3.13] gh-125884: Support breakpoint on functions with annotations (G… · python/cpython@e334022 · GitHub
[go: up one dir, main page]

Skip to content

Commit e334022

Browse files
[3.13] gh-125884: Support breakpoint on functions with annotations (G… (#125902)
[3.13] gh-125884: Support breakpoint on functions with annotations (GH-125892) (cherry picked from commit 8f2c0f7)
1 parent e596740 commit e334022

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
@@ -120,7 +120,7 @@ def find_first_executable_line(code):
120120
return code.co_firstlineno
121121

122122
def find_function(funcname, filename):
123-
cre = re.compile(r'def\s+%s\s*[(]' % re.escape(funcname))
123+
cre = re.compile(r'def\s+%s(\s*\[.+\])?\s*[(]' % re.escape(funcname))
124124
try:
125125
fp = tokenize.open(filename)
126126
except OSError:
@@ -140,9 +140,12 @@ def find_function(funcname, filename):
140140

141141
if funcdef:
142142
try:
143-
funccode = compile(funcdef, filename, 'exec').co_consts[0]
143+
code = compile(funcdef, filename, 'exec')
144144
except SyntaxError:
145145
continue
146+
# We should always be able to find the code object here
147+
funccode = next(c for c in code.co_consts if
148+
isinstance(c, CodeType) and c.co_name == funcname)
146149
lineno_offset = find_first_executable_line(funccode)
147150
return funcname, filename, funcstart + lineno_offset - 1
148151
return None

Lib/test/test_pdb.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,42 @@ def test_pdb_breakpoint_commands():
358358
4
359359
"""
360360

361+
def test_pdb_breakpoint_on_annotated_function_def():
362+
"""Test breakpoints on function definitions with annotation.
363+
364+
>>> def foo[T]():
365+
... return 0
366+
367+
>>> def bar() -> int:
368+
... return 0
369+
370+
>>> def foobar[T]() -> int:
371+
... return 0
372+
373+
>>> reset_Breakpoint()
374+
375+
>>> def test_function():
376+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
377+
... pass
378+
379+
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
380+
... 'break foo',
381+
... 'break bar',
382+
... 'break foobar',
383+
... 'continue',
384+
... ]):
385+
... test_function()
386+
> <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[4]>(2)test_function()
387+
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
388+
(Pdb) break foo
389+
Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[0]>:2
390+
(Pdb) break bar
391+
Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[1]>:2
392+
(Pdb) break foobar
393+
Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[2]>:2
394+
(Pdb) continue
395+
"""
396+
361397
def test_pdb_breakpoint_with_filename():
362398
"""Breakpoints with filename:lineno
363399
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)
0