[go: nahoru, domu]

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

gh-94436: cache failures in linecache #121224

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix tests
  • Loading branch information
picnixz committed Jul 1, 2024
commit 68b1d7dff784894153016850f86e55bb51b85597
24 changes: 23 additions & 1 deletion Lib/linecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ def getline(filename, lineno, module_globals=None):
"""

if filename in failures:
# We explicitly validate the input even for failures
# because for success, they would raise a TypeError.
if not isinstance(lineno, int):
raise TypeError(f"'lineno' must be an int, "
f"got {type(module_globals)}")
_validate_module_globals_type(module_globals)
return ''

lines = getlines(filename, module_globals)
Expand All @@ -51,6 +57,8 @@ def getlines(filename, module_globals=None):
Previous failures are cached and must be invalidated via checkcache().
"""

_validate_module_globals_type(module_globals)

if filename in cache:
assert filename not in failures
entry = cache[filename]
Expand Down Expand Up @@ -136,7 +144,8 @@ def updatecache(filename, module_globals=None):
"""Update a cache entry and return its list of lines.

If something's wrong, possibly print a message, discard the cache entry,
add the file name to the known failures, and return an empty list."""
add the file name to the known failures, and return an empty list.
"""

# These imports are not at top level because linecache is in the critical
# path of the interpreter startup and importing os and sys take a lot of time
Expand All @@ -145,6 +154,8 @@ def updatecache(filename, module_globals=None):
import sys
import tokenize

_validate_module_globals_type(module_globals)

if filename in cache:
if len(cache[filename]) != 1:
cache.pop(filename, None)
Expand Down Expand Up @@ -287,3 +298,14 @@ def _register_code(code, string, name):
None,
[line + '\n' for line in string.splitlines()],
name)


def _validate_module_globals_type(module_globals):
if module_globals is not None:
# Validation is done here and not in linecache
# since the latter may not necessarily use it.
from collections.abc import Mapping

if not isinstance(module_globals, Mapping):
raise TypeError(f"'module_globals' must be a Mapping, "
f"got {type(module_globals)}")
Loading