Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 2, 2025
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
8 changes: 8 additions & 0 deletions Doc/library/linecache.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ The :mod:`linecache` module defines the following functions:

.. index:: triple: module; search; path

If *filename* indicates a frozen module (starting with ``'<frozen '``), the function
will attepmt to get the real file name from ``module_globals['__file__']`` if
*module_globals* is not ``None``.

If a file named *filename* is not found, the function first checks
for a :pep:`302` ``__loader__`` in *module_globals*.
If there is such a loader and it defines a ``get_source`` method,
Expand All @@ -38,6 +42,10 @@ The :mod:`linecache` module defines the following functions:
Finally, if *filename* is a relative filename,
it is looked up relative to the entries in the module search path, ``sys.path``.

.. versionchanged:: 3.14

Support *filename* of frozen modules.


.. function:: clearcache()

Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,13 @@ json
(Contributed by Trey Hunner in :gh:`122873`.)


linecache
---------

* :func:`linecache.getline` can retrieve source code for frozen modules.
(Contributed by Tian Gao in :gh:`131638`.)


mimetypes
---------

Expand Down
21 changes: 19 additions & 2 deletions Lib/linecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
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!)"""
Expand Down Expand Up @@ -118,10 +128,17 @@ def updatecache(filename, module_globals=None):
if filename in cache:
if len(cache[filename]) != 1:
cache.pop(filename, None)
if not filename or (filename.startswith('<') and filename.endswith('>')):
if _source_unavailable(filename):
return []

fullname = filename
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__')
if fullname is None:
return []
else:
fullname = filename
try:
stat = os.stat(fullname)
except OSError:
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_linecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
linecache.clearcache()
empty = linecache.getlines(filename)
self.assertEqual(empty, [])

def test_invalid_names(self):
for name, desc in [
('\x00', 'NUL bytes filename'),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support frozen modules for :func:`linecache.getline`.
Loading