Skip to content

gh-93696: Locate frozen module source with __file__ #93697

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 14 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from 11 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
4 changes: 4 additions & 0 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,10 @@ def do_list(self, arg):
if last is None:
last = first + 10
filename = self.curframe.f_code.co_filename
# gh-93696: stdlib frozen modules provide a useful __file__ (see also gh-89815)
if filename.startswith("<frozen"):
if "__file__" in self.curframe.f_globals:
filename = self.curframe.f_globals["__file__"]
Copy link
Member

@JelleZijlstra JelleZijlstra Oct 25, 2022

Choose a reason for hiding this comment

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

What if __file__ is not a string? It looks like get_file_breaks will crash in that case. We should probably just ignore __file__ in that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, that's a good point. I would think it's a small edge, but it's easy to be more cautious about it.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I think it's important for a debugger to be resilient in the face of unusual program state.

breaklist = self.get_file_breaks(filename)
try:
lines = linecache.getlines(filename, self.curframe.f_globals)
Expand Down
46 changes: 46 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2104,6 +2104,52 @@ def inner(v): pass
stdout, stderr = self.run_pdb_script(script, commands)
self.assertFalse(stderr)

def test_gh_93696_frozen_list(self):
frozen_src = """
def func():
x = "Sentinel string for gh-93696"
print(x)
"""
host_program = """
import os
import sys

def _create_fake_frozen_module():
with open('gh93696.py') as f:
src = f.read()

# this function has a co_filename as if it were in a frozen module
dummy_mod = compile(src, "<frozen gh93696>", "exec")
func_code = dummy_mod.co_consts[0]

mod = type(sys)("gh93696")
mod.func = type(lambda: None)(func_code, mod.__dict__)
mod.__file__ = 'gh93696.py'

return mod

mod = _create_fake_frozen_module()
mod.func()
"""
commands = """
break 20
continue
step
list
quit
"""
with open('gh93696.py', 'w') as f:
Copy link
Member

Choose a reason for hiding this comment

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

A little weird that we're writing into the CWD, but that appears to be what the other tests in this file are doing too.

f.write(textwrap.dedent(frozen_src))

with open('gh93696_host.py', 'w') as f:
f.write(textwrap.dedent(host_program))

self.addCleanup(os_helper.unlink, 'gh93696.py')
self.addCleanup(os_helper.unlink, 'gh93696_host.py')
stdout, stderr = self._run_pdb(["gh93696_host.py"], commands)
# verify that pdb found the source of the "frozen" function
self.assertIn('x = "Sentinel string for gh-93696"', stdout, "Sentinel statement not found")

class ChecklineTests(unittest.TestCase):
def setUp(self):
linecache.clearcache() # Pdb.checkline() uses linecache.getline()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow :mod:`pdb` to locate source for frozen modules in the standard library.