summaryrefslogtreecommitdiffstats
path: root/Lib/linecache.py
diff options
context:
space:
mode:
authorEugene Toder <eltoder@users.noreply.github.com>2024-02-20 16:47:41 (GMT)
committerGitHub <noreply@github.com>2024-02-20 16:47:41 (GMT)
commite976baba99a5c243ff3a3b5ef2fd14608a398338 (patch)
tree7e9288462d2a1472092f247e7ec4944bd1f952c4 /Lib/linecache.py
parent937d2821501de7adaa5ed8491eef4b7f3dc0940a (diff)
downloadcpython-e976baba99a5c243ff3a3b5ef2fd14608a398338.zip
cpython-e976baba99a5c243ff3a3b5ef2fd14608a398338.tar.gz
cpython-e976baba99a5c243ff3a3b5ef2fd14608a398338.tar.bz2
gh-86291: linecache: get module name from __spec__ if available (GH-22908)
This allows getting source code for the __main__ module when a custom loader is used.
Diffstat (limited to 'Lib/linecache.py')
-rw-r--r--Lib/linecache.py12
1 files changed, 5 insertions, 7 deletions
diff --git a/Lib/linecache.py b/Lib/linecache.py
index 329a140..04c8f45 100644
--- a/Lib/linecache.py
+++ b/Lib/linecache.py
@@ -166,13 +166,11 @@ def lazycache(filename, module_globals):
return False
# Try for a __loader__, if available
if module_globals and '__name__' in module_globals:
- name = module_globals['__name__']
- if (loader := module_globals.get('__loader__')) is None:
- if spec := module_globals.get('__spec__'):
- try:
- loader = spec.loader
- except AttributeError:
- pass
+ 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__')
get_source = getattr(loader, 'get_source', None)
if name and get_source: