-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-122071: Fixed traceback leaks global code when file does not exist #122126
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
base: main
Are you sure you want to change the base?
Changes from 17 commits
f1e1189
f510454
dcc5718
b40bc28
a9f8458
0fab629
f3a3f97
9d7594a
c28b698
17e3545
44715f2
1940da7
78d1a14
363758a
fa2d76b
7f7308a
250f026
b2407ff
e3796bb
3005636
787ea13
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| import importlib._bootstrap_external | ||
| """Cache lines from Python source files. | ||
|
|
||
| This is intended to read lines from modules imported -- hence if a filename | ||
| is not found, it will look down the module search path for a file by | ||
| that name. | ||
| """ | ||
|
|
||
lhish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| __all__ = ["getline", "clearcache", "checkcache", "lazycache"] | ||
|
|
||
|
|
||
|
|
@@ -177,11 +177,16 @@ def lazycache(filename, module_globals): | |
| return False | ||
| # Try for a __loader__, if available | ||
| if module_globals and '__name__' in module_globals: | ||
| get_lines = lambda *_, **__: "" | ||
| spec = module_globals.get('__spec__') | ||
| name = getattr(spec, 'name', None) or module_globals['__name__'] | ||
| loader = getattr(spec, 'loader', None) | ||
| if loader is None: | ||
| loader = module_globals.get('__loader__') | ||
| mod_file = module_globals.get('__file__') | ||
| if isinstance(loader, importlib._bootstrap_external.SourceFileLoader) and (not mod_file or (not mod_file.endswith(filename) and not mod_file.endswith('.pyc'))): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You said that returning False makes If the issue is with an existing test, then the test might also need to be updated (since the logic has been changed here). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we directly return The output is: Additionally, regarding the Since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're not meant to access the cache using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have two questions regarding your reply. First, Since my English is not very good, I struggled to understand your final suggestion about updating other paths in Second, If simply returns False, it would cause two existing Linecache-related tests to fail: Test Case 1: In this case, returning False would result in a failure because the test expects the cache to contain data. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Don't worry, I replied a bit too fast. My English is not perfect either so don't hesistate to ask if I was unclear!
Sorry, I meant to see whether we correctly covered the cases (namely, to see if the algorithm needs to be updated because of this new logic). For the tests, the logic could be changed. Leaking the global code is probably worse than not leaking it IMO. I'll have a better look at the lazy-caching interface and the question on non-existent filenames (maybe we could make it work). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright, it does seem that returning Additionally, I've adjusted two tests that were failing in my latest commit. However, I'm not completely certain if these adjustments are logically sound. |
||
| cache[filename] = (get_lines,) | ||
| return True | ||
| get_source = getattr(loader, 'get_source', None) | ||
|
|
||
| if name and get_source: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix global code leaks when reporting tracebacks in the REPL and :func:`exec`. |
Uh oh!
There was an error while loading. Please reload this page.