[Python-checkins] gh-93696: Locate frozen module source with __file__ (GH-93697)

miss-islington webhook-mailer at python.org
Tue Oct 25 09:48:48 EDT 2022


https://github.com/python/cpython/commit/a86a7b827bd679b87f619329d3c3f870fa01ee0a
commit: a86a7b827bd679b87f619329d3c3f870fa01ee0a
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-10-25T06:48:41-07:00
summary:

gh-93696: Locate frozen module source with __file__ (GH-93697)


Co-authored-by: Kumar Aditya <59607654+kumaraditya303 at users.noreply.github.com>
(cherry picked from commit d91de288e73c67805e4c838b5f770ab7ec3661f9)

Co-authored-by: James Gerity <snoopjedi at gmail.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2022-06-10-16-37-44.gh-issue-93696.65BI2R.rst
M Lib/pdb.py
M Lib/test/test_pdb.py

diff --git a/Lib/pdb.py b/Lib/pdb.py
index 7ab50b4845d3..7401a675253d 100755
--- a/Lib/pdb.py
+++ b/Lib/pdb.py
@@ -1248,6 +1248,12 @@ 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__
+        # 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)
         try:
             lines = linecache.getlines(filename, self.curframe.f_globals)
diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py
index 6ac1a4a3c302..7f0bc719f0af 100644
--- a/Lib/test/test_pdb.py
+++ b/Lib/test/test_pdb.py
@@ -1915,6 +1915,52 @@ def test_issue42383(self):
             self.assertEqual(stdout.split('\n')[6].rstrip('\r'), expected)
 
 
+    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):
     def setUp(self):
         linecache.clearcache()  # Pdb.checkline() uses linecache.getline()
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-06-10-16-37-44.gh-issue-93696.65BI2R.rst b/Misc/NEWS.d/next/Core and Builtins/2022-06-10-16-37-44.gh-issue-93696.65BI2R.rst
new file mode 100644
index 000000000000..8eadab0ad8fb
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-06-10-16-37-44.gh-issue-93696.65BI2R.rst	
@@ -0,0 +1 @@
+Allow :mod:`pdb` to locate source for frozen modules in the standard library.



More information about the Python-checkins mailing list