Skip to content
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

[3.11] gh-86291: linecache: get module name from __spec__ if available (GH-22908) #115732

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions Lib/linecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
38 changes: 38 additions & 0 deletions Lib/test/test_linecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os.path
import tempfile
import tokenize
from importlib.machinery import ModuleSpec
from test import support
from test.support import os_helper

Expand Down Expand Up @@ -97,6 +98,16 @@ class BadUnicode_WithDeclaration(GetLineTestsBadData, unittest.TestCase):
file_byte_string = b'# coding=utf-8\n\x80abc'


class FakeLoader:
def get_source(self, fullname):
return f'source for {fullname}'


class NoSourceLoader:
def get_source(self, fullname):
return None


class LineCacheTests(unittest.TestCase):

def test_getline(self):
Expand Down Expand Up @@ -238,6 +249,33 @@ def raise_memoryerror(*args, **kwargs):
self.assertEqual(lines3, [])
self.assertEqual(linecache.getlines(FILENAME), lines)

def test_loader(self):
filename = 'scheme://path'

for loader in (None, object(), NoSourceLoader()):
linecache.clearcache()
module_globals = {'__name__': 'a.b.c', '__loader__': loader}
self.assertEqual(linecache.getlines(filename, module_globals), [])

linecache.clearcache()
module_globals = {'__name__': 'a.b.c', '__loader__': FakeLoader()}
self.assertEqual(linecache.getlines(filename, module_globals),
['source for a.b.c\n'])

for spec in (None, object(), ModuleSpec('', FakeLoader())):
linecache.clearcache()
module_globals = {'__name__': 'a.b.c', '__loader__': FakeLoader(),
'__spec__': spec}
self.assertEqual(linecache.getlines(filename, module_globals),
['source for a.b.c\n'])

linecache.clearcache()
spec = ModuleSpec('x.y.z', FakeLoader())
module_globals = {'__name__': 'a.b.c', '__loader__': spec.loader,
'__spec__': spec}
self.assertEqual(linecache.getlines(filename, module_globals),
['source for x.y.z\n'])


class LineCacheInvalidationTests(unittest.TestCase):
def setUp(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
linecache: get module name from ``__spec__`` if available. This allows getting
source code for the ``__main__`` module when a custom loader is used.
Loading