Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-60115: Support frozen modules for linecache.getline()#131638
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Changes from1 commit
7bbf5d631387a0c101cf967505cc381adfb188329cbcf913bFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -63,6 +63,16 @@ def _getlines_from_code(code): | ||
| return [] | ||
| def _source_unavailable(filename): | ||
| """Return True if the source code is unavailable for such file name""" | ||
gaogaotiantian marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| return ( | ||
| not filename | ||
| or (filename.startswith('<') | ||
| and filename.endswith('>') | ||
| and not filename.startswith('<frozen ')) | ||
| ) | ||
| def checkcache(filename=None): | ||
| """Discard cache entries that are out of date. | ||
| (This is not checked upon each call!)""" | ||
| @@ -118,10 +128,17 @@ def updatecache(filename, module_globals=None): | ||
| if filename in cache: | ||
| if len(cache[filename]) != 1: | ||
| cache.pop(filename, None) | ||
| if_source_unavailable(filename): | ||
| return [] | ||
| if filename.startswith('<frozen ') and module_globals is not None: | ||
| # This is a frozen module, so we need to use the filename | ||
| # from the module globals. | ||
| fullname = module_globals.get('__file__', None) | ||
gaogaotiantian marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| if fullname is None: | ||
| return [] | ||
| else: | ||
| fullname = filename | ||
| try: | ||
| stat = os.stat(fullname) | ||
| except OSError: | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -281,6 +281,19 @@ def test_loader(self): | ||
| self.assertEqual(linecache.getlines(filename, module_globals), | ||
| ['source for x.y.z\n']) | ||
| def test_frozen(self): | ||
| filename = '<frozen fakemodule>' | ||
| module_globals = {'__file__': FILENAME} | ||
| empty = linecache.getlines(filename) | ||
| self.assertEqual(empty, []) | ||
| lines = linecache.getlines(filename, module_globals) | ||
| self.assertGreater(len(lines), 0) | ||
| lines_cached = linecache.getlines(filename) | ||
| self.assertEqual(lines, lines_cached) | ||
picnixz marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| linecache.clearcache() | ||
| empty = linecache.getlines(filename) | ||
| self.assertEqual(empty, []) | ||
| def test_invalid_names(self): | ||
| for name, desc in [ | ||
| ('\x00', 'NUL bytes filename'), | ||
Uh oh!
There was an error while loading.Please reload this page.