From 65f9c8e6dda177925346b8e846995cd5b1552167 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Tue, 25 Oct 2022 06:49:34 -0700 Subject: gh-93696: Locate frozen module source with __file__ (GH-93697) Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> (cherry picked from commit d91de288e73c67805e4c838b5f770ab7ec3661f9) Co-authored-by: James Gerity --- Lib/pdb.py | 6 +++ Lib/test/test_pdb.py | 46 ++++++++++++++++++++++ .../2022-06-10-16-37-44.gh-issue-93696.65BI2R.rst | 1 + 3 files changed, 53 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-06-10-16-37-44.gh-issue-93696.65BI2R.rst diff --git a/Lib/pdb.py b/Lib/pdb.py index fe8ddd1..411ce53 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -1332,6 +1332,12 @@ class Pdb(bdb.Bdb, cmd.Cmd): 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("", "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 0000000..8eadab0 --- /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. -- cgit v0.12