-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
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
gh-93696: Locate frozen module source with __file__ #93697
Conversation
Edit: I have now added a test to this PR, see follow-up comment
click to see patchdiff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py
index 0141b739c2..894f3e9099 100644
--- a/Lib/test/test_pdb.py
+++ b/Lib/test/test_pdb.py
@@ -532,6 +532,40 @@ def test_list_commands():
(Pdb) continue
"""
+def test_frozen_list():
+ '''Test the list command on frozen stdlib modules
+
+ >>> def test_function():
+ ... import importlib._bootstrap
+ ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+ ... importlib._bootstrap._resolve_name("os", ".", 1)
+
+ >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
+ ... 'step',
+ ... 'list',
+ ... 'continue',
+ ... ]):
+ ... test_function()
+ > <doctest test.test_pdb.test_frozen_list[0]>(4)test_function()
+ -> importlib._bootstrap._resolve_name("os", ".", 1)
+ (Pdb) step
+ --Call--
+ > <frozen importlib._bootstrap>(1037)_resolve_name()
+ (Pdb) list
+ 1032 def __exit__(self, exc_type, exc_value, exc_traceback):
+ 1033 """Release the import lock regardless of any raised exceptions."""
+ 1034 _imp.release_lock()
+ 1035
+ 1036
+ 1037 -> def _resolve_name(name, package, level):
+ 1038 """Resolve a relative module name to an absolute one."""
+ 1039 bits = package.rsplit('.', level - 1)
+ 1040 if len(bits) < level:
+ 1041 raise ImportError('attempted relative import beyond top-level package')
+ 1042 base = bits[0]
+ (Pdb) continue
+ '''
+
def test_pdb_whatis_command():
"""Test the whatis command |
Updated with a test that creates a module that suitably fakes the behavior of functions inside of frozen modules, in the sense that |
Misc/NEWS.d/next/Core and Builtins/2022-06-10-16-37-44.gh-issue-93696.65BI2R.rst
Outdated
Show resolved
Hide resolved
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@ericsnowcurrently does this patch need any changes? |
@JelleZijlstra generously offered to have a look-see at this during the 3.11 release stream, dropping this here to ping him :) |
list | ||
quit | ||
""" | ||
with open('gh93696.py', 'w') as f: |
There was a problem hiding this comment.
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.
Lib/pdb.py
Outdated
# this workaround can be removed with the closure of gh-89815 | ||
if filename.startswith("<frozen"): | ||
if "__file__" in self.curframe.f_globals: | ||
filename = self.curframe.f_globals["__file__"] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Thanks @SnoopJ for the PR, and @JelleZijlstra for merging it 🌮🎉.. I'm working now to backport this PR to: 3.10, 3.11. |
GH-98653 is a backport of this pull request to the 3.11 branch. |
GH-98654 is a backport of this pull request to the 3.10 branch. |
See #93696, this patch allows
pdb
to locate the source for frozen stdlib modules. With the patch applied, the output ofpdb
is as expected:I am not particularly familiar with the frozen module system so I'm not sure if there are edge cases where this will produce undesirable behavior, but thought that I would send it upstream just in case 😅