10000 [3.13] gh-125884: Support breakpoint on functions with annotations (G… by gaogaotiantian · Pull Request #125902 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.13] gh-125884: Support breakpoint on functions with annotations (G… #125902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[3.13] gh-125884: Support breakpoint on functions with annotations (G…
…H-125892)

(cherry picked from commit 8f2c0f7)

Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
  • Loading branch information
gaogaotiantian committed Oct 23, 2024
commit ba298d6aabfbce8dc5e9a2005202d157e9f0f487
7 changes: 5 additions & 2 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,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 @@ -140,9 +140,12 @@ def find_function(funcname, filename):

if funcdef:
try:
funccode = compile(funcdef, filename, 'exec').co_consts[0]
code = compile(funcdef, filename, 'exec')
except SyntaxError:
continue
# We should always be able to find the code object here
funccode = next(c for c in code.co_consts if
isinstance(c, CodeType) and c.co_name == funcname)
lineno_offset = find_first_executable_line(funccode)
return funcname, filename, funcstart + lineno_offset - 1
return None
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 @@ -358,6 +358,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_breakpoint_with_filename():
"""Breakpoints with filename:lineno

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed the bug for :mod:`pdb` where it can't set breakpoints on functions with certain annotations.
Loading
0