Skip to content
Closed
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
bpo-32797: linecache should search for sources if loader doesn't find…
… them
  • Loading branch information
jdemeyer committed Apr 30, 2018
commit 6f87f7205baf7d9aa41f43ff51a6a342f1f0f5ba
16 changes: 7 additions & 9 deletions Lib/linecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,13 @@ def updatecache(filename, module_globals=None):
except (ImportError, OSError):
pass
else:
if data is None:
# No luck, the PEP302 loader cannot find the source
# for this module.
return []
cache[filename] = (
len(data), None,
[line+'\n' for line in data.splitlines()], fullname
)
return cache[filename][2]
# If data is None, then the PEP302 loader didn't find
# sources. In that case, we still continue searching for
# the sources by filename (bpo-32797).
if data is not None:
lines = [line + '\n' for line in data.splitlines()]
cache[filename] = (len(data), None, lines, fullname)
return lines

# Try looking through the module search path, which is only useful
# when handling a relative filename.
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_linecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,17 @@ def raise_memoryerror(*args, **kwargs):
self.assertEqual(lines3, [])
self.assertEqual(linecache.getlines(FILENAME), lines)

def test_loader_get_source(self):
from types import ModuleType
from importlib.machinery import ExtensionFileLoader
mod = ModuleType("fake_io")
mod_globals = mod.__dict__
mod_globals["__loader__"] = ExtensionFileLoader("fake_io", "")

# bpo-32797: this should return the source code of "io.py" even
# though the loader's get_source() returns None.
self.assertTrue(linecache.getlines("io.py", mod_globals))


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
linecache continues searching for the source file by filename if the ``get_source``
method of the ``__loader__`` returns ``None``.