mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
gh-93696: Locate frozen module source with __file__ (#93697)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
This commit is contained in:
parent
dd13b23e49
commit
d91de288e7
3 changed files with 53 additions and 0 deletions
|
@ -1332,6 +1332,12 @@ class Pdb(bdb.Bdb, cmd.Cmd):
|
||||||
if last is None:
|
if last is None:
|
||||||
last = first + 10
|
last = first + 10
|
||||||
filename = self.curframe.f_code.co_filename
|
filename = self.curframe.f_code.co_filename
|
||||||
|
# gh-93696: stdlib frozen modules provide a useful __file__
|
||||||
|
# this workaround can be removed with the closure of gh-89815
|
||||||
|
if filename.startswith("<frozen"):
|
||||||
|
tmp = self.curframe.f_globals.get("__file__")
|
||||||
|
if isinstance(tmp, str):
|
||||||
|
filename = tmp
|
||||||
breaklist = self.get_file_breaks(filename)
|
breaklist = self.get_file_breaks(filename)
|
||||||
try:
|
try:
|
||||||
lines = linecache.getlines(filename, self.curframe.f_globals)
|
lines = linecache.getlines(filename, self.curframe.f_globals)
|
||||||
|
|
|
@ -2104,6 +2104,52 @@ def bœr():
|
||||||
stdout, stderr = self.run_pdb_script(script, commands)
|
stdout, stderr = self.run_pdb_script(script, commands)
|
||||||
self.assertFalse(stderr)
|
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:
|
||||||
|
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):
|
class ChecklineTests(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
linecache.clearcache() # Pdb.checkline() uses linecache.getline()
|
linecache.clearcache() # Pdb.checkline() uses linecache.getline()
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Allow :mod:`pdb` to locate source for frozen modules in the standard library.
|
Loading…
Add table
Add a link
Reference in a new issue