Skip to content

Commit 5ccebc5

Browse files
[3.12] gh-125884: Support breakpoint on functions with annotations (GH-125892)
(cherry picked from commit 8f2c0f7) Co-authored-by: Tian Gao <[email protected]>
1 parent 440ed18 commit 5ccebc5

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

Lib/pdb.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class Restart(Exception):
9696
"post_mortem", "help"]
9797

9898
def find_function(funcname, filename):
99-
cre = re.compile(r'def\s+%s\s*[(]' % re.escape(funcname))
99+
cre = re.compile(r'def\s+%s(\s*\[.+\])?\s*[(]' % re.escape(funcname))
100100
try:
101101
fp = tokenize.open(filename)
102102
except OSError:
@@ -105,7 +105,24 @@ def find_function(funcname, filename):
105105
with fp:
106106
for lineno, line in enumerate(fp, start=1):
107107
if cre.match(line):
108+
<<<<<<< HEAD
108109
return funcname, filename, lineno
110+
=======
111+
funcstart, funcdef = lineno, line
112+
elif funcdef:
113+
funcdef += line
114+
115+
if funcdef:
116+
try:
117+
code = compile(funcdef, filename, 'exec')
118+
except SyntaxError:
119+
continue
120+
# We should always be able to find the code object here
121+
funccode = next(c for c in code.co_consts if
122+
isinstance(c, CodeType) and c.co_name == funcname)
123+
lineno_offset = find_first_executable_line(funccode)
124+
return funcname, filename, funcstart + lineno_offset - 1
125+
>>>>>>> 8f2c0f7a03b... gh-125884: Support breakpoint on functions with annotations (#125892)
109126
return None
110127

111128
def lasti2lineno(code, lasti):

Lib/test/test_pdb.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,42 @@ def test_pdb_breakpoint_commands():
352352
4
353353
"""
354354

355+
def test_pdb_breakpoint_on_annotated_function_def():
356+
"""Test breakpoints on function definitions with annotation.
357+
358+
>>> def foo[T]():
359+
... return 0
360+
361+
>>> def bar() -> int:
362+
... return 0
363+
364+
>>> def foobar[T]() -> int:
365+
... return 0
366+
367+
>>> reset_Breakpoint()
368+
369+
>>> def test_function():
370+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
371+
... pass
372+
373+
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
374+
... 'break foo',
375+
... 'break bar',
376+
... 'break foobar',
377+
... 'continue',
378+
... ]):
379+
... test_function()
380+
> <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[4]>(3)test_function()
381+
-> pass
382+
(Pdb) break foo
383+
Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[0]>:2
384+
(Pdb) break bar
385+
Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[1]>:2
386+
(Pdb) break foobar
387+
Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[2]>:2
388+
(Pdb) continue
389+
"""
390+
355391
def test_pdb_breakpoints_preserved_across_interactive_sessions():
356392
"""Breakpoints are remembered between interactive sessions
357393
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)