Skip to content

gh-57537: Support breakpoints for zipimport modules on pdb #130290

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 4 commits into from
Feb 20, 2025
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
12 changes: 8 additions & 4 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,7 @@ def do_break(self, arg, temporary=False):
filename = None
lineno = None
cond = None
module_globals = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably call this func_globals. Up to you.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to match the name for linecache. It is a module globals acquired from a function, and the part matters is about the module (loader and stuff).

comma = arg.find(',')
if comma > 0:
# parse stuff after comma: "condition"
Expand Down Expand Up @@ -1179,6 +1180,7 @@ def do_break(self, arg, temporary=False):
funcname = code.co_name
lineno = find_first_executable_line(code)
filename = code.co_filename
module_globals = func.__globals__
except:
# last thing to try
(ok, filename, ln) = self.lineinfo(arg)
Expand All @@ -1190,8 +1192,9 @@ def do_break(self, arg, temporary=False):
lineno = int(ln)
if not filename:
filename = self.defaultFile()
filename = self.canonic(filename)
# Check for reasonable breakpoint
line = self.checkline(filename, lineno)
line = self.checkline(filename, lineno, module_globals)
if line:
# now set the break point
err = self.set_break(filename, line, temporary, cond, funcname)
Expand Down Expand Up @@ -1258,7 +1261,7 @@ def lineinfo(self, identifier):
answer = find_function(item, self.canonic(fname))
return answer or failed

def checkline(self, filename, lineno):
def checkline(self, filename, lineno, module_globals=None):
"""Check whether specified line seems to be executable.

Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank
Expand All @@ -1267,8 +1270,9 @@ def checkline(self, filename, lineno):
# this method should be callable before starting debugging, so default
# to "no globals" if there is no current frame
frame = getattr(self, 'curframe', None)
globs = frame.f_globals if frame else None
line = linecache.getline(filename, lineno, globs)
if module_globals is None:
module_globals = frame.f_globals if frame else None
line = linecache.getline(filename, lineno, module_globals)
if not line:
self.message('End of file')
return 0
Expand Down
33 changes: 33 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import textwrap
import linecache
import zipapp
import zipfile

from contextlib import ExitStack, redirect_stdout
from io import StringIO
Expand Down Expand Up @@ -4199,6 +4200,38 @@ def f(x):
self.assertIn('42', stdout)
self.assertIn('return x + 1', stdout)

def test_zipimport(self):
with os_helper.temp_dir() as temp_dir:
os.mkdir(os.path.join(temp_dir, 'source'))
zipmodule = textwrap.dedent(
"""
def bar():
pass
"""
)
script = textwrap.dedent(
f"""
import sys; sys.path.insert(0, {repr(os.path.join(temp_dir, 'zipmodule.zip'))})
import foo
foo.bar()
"""
)

with zipfile.ZipFile(os.path.join(temp_dir, 'zipmodule.zip'), 'w') as zf:
zf.writestr('foo.py', zipmodule)
with open(os.path.join(temp_dir, 'script.py'), 'w') as f:
f.write(script)

stdout, _ = self._run_pdb([os.path.join(temp_dir, 'script.py')], '\n'.join([
'n',
'n',
'b foo.bar',
'c',
'p f"break in {$_frame.f_code.co_name}"',
'q'
]))
self.assertIn('break in bar', stdout)


class ChecklineTests(unittest.TestCase):
def setUp(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support breakpoints for :mod:`zipimport` modules on :mod:`pdb`
Loading