8000 update linecache to 3.13.3 · arihant2math/RustPython@70f3aec · GitHub
[go: up one dir, main page]

Skip to content

Commit 70f3aec

Browse files
committed
update linecache to 3.13.3
1 parent 6567d1d commit 70f3aec

File tree

2 files changed

+150
-21
lines changed

2 files changed

+150
-21
lines changed

Lib/linecache.py

Lines changed: 70 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@
55
that name.
66
"""
77

8-
import functools
9-
import sys
10-
import os
11-
import tokenize
12-
138
__all__ = ["getline", "clearcache", "checkcache", "lazycache"]
149

1510

1611
# The cache. Maps filenames to either a thunk which will provide source code,
1712
# or a tuple (size, mtime, lines, fullname) once loaded.
1813
cache = {}
14+
_interactive_cache = {}
1915

2016

2117
def clearcache():
@@ -49,28 +45,54 @@ def getlines(filename, module_globals=None):
4945
return []
5046

5147

48+
def _getline_from_code(filename, lineno):
49+
lines = _getlines_from_code(filename)
50+
if 1 <= lineno <= len(lines):
51+
return lines[lineno - 1]
52+
return ''
53+
54+
def _make_key(code):
55+
return (code.co_filename, code.co_qualname, code.co_firstlineno)
56+
57+
def _getlines_from_code(code):
58+
code_id = _make_key(code)
59+
if code_id in _interactive_cache:
60+
entry = _interactive_cache[code_id]
61+
if len(entry) != 1:
62+
return _interactive_cache[code_id][2]
63+
return []
64+
65+
5266
def checkcache(filename=None):
5367
"""Discard cache entries that are out of date.
5468
(This is not checked upon each call!)"""
5569

5670
if filename is None:
57-
filenames = list(cache.keys())
58-
elif filename in cache:
59-
filenames = [filename]
71+
# get keys atomically
72+
filenames = cache.copy().keys()
6073
else:
61-
return
74+
filenames = [filename]
6275

6376
for filename in filenames:
64-
entry = cache[filename]
77+
try:
78+
entry = cache[filename]
79+
except KeyError:
80+
continue
81+
6582
if len(entry) == 1:
6683
# lazy cache entry, leave it lazy.
6784
continue
6885
size, mtime, lines, fullname = entry
6986
if mtime is None:
7087
continue # no-op for files loaded via a __loader__
88+
try:
89+
# This import can fail if the interpreter is shutting down
90+
import os
91+
except ImportError:
92+
return
7193
try:
7294
stat = os.stat(fullname)
73-
except OSError:
95+
except (OSError, ValueError):
7496
cache.pop(filename, None)
7597
continue
7698
if size != stat.st_size or mtime != stat.st_mtime:
@@ -82,6 +104,17 @@ def updatecache(filename, module_globals=None):
82104
If something's wrong, print a message, discard the cache entry,
83105
and return an empty list."""
84106

107+
# These imports are not at top level because linecache is in the critical
108+
# path of the interpreter startup and importing os and sys take a lot of time
109+
# and slows down the startup sequence.
110+
try:
111+
import os
112+
import sys
113+
import tokenize
114+
except ImportError:
115+
# These import can fail if the interpreter is shutting down
116+
return []
117+
85118
if filename in cache:
86119
if len(cache[filename]) != 1:
87120
cache.pop(filename, None)
@@ -128,16 +161,20 @@ def updatecache(filename, module_globals=None):
128161
try:
129162
stat = os.stat(fullname)
130163
break
131-
except OSError:
164+
except (OSError, ValueError):
132165
pass
133166
else:
134167
return []
168+
except ValueError: # may be raised by os.stat()
169+
return []
135170
try:
136171
with tokenize.open(fullname) as fp:
137172
lines = fp.readlines()
138173
except (OSError, UnicodeDecodeError, SyntaxError):
139174
return []
140-
if lines and not lines[-1].endswith('\n'):
175+
if not lines:
176+
lines = ['\n']
177+
elif not lines[-1].endswith('\n'):
141178
lines[-1] += '\n'
142179
size, mtime = stat.st_size, stat.st_mtime
143180
cache[filename] = size, mtime, lines, fullname
@@ -166,17 +203,29 @@ def lazycache(filename, module_globals):
166203
return False
167204
# Try for a __loader__, if available
168205
if module_globals and '__name__' in module_globals:
169-
name = module_globals['__name__']
170-
if (loader := module_globals.get('__loader__')) is None:
171-
if spec := module_globals.get('__spec__'):
172-
try:
173-
loader = spec.loader
174-
except AttributeError:
175-
pass
206+
spec = module_globals.get('__spec__')
207+
name = getattr(spec, 'name', None) or module_globals['__name__']
208+
loader = getattr(spec, 'loader', None)
209+
if loader is None:
210+
loader = module_globals.get('__loader__')
176211
get_source = getattr(loader, 'get_source', None)
177212

178213
if name and get_source:
179-
get_lines = functools.partial(get_source, name)
214+
def get_lines(name=name, *args, **kwargs):
215+
return get_source(name, *args, **kwargs)
180216
cache[filename] = (get_lines,)
181217
return True
182218
return False
219+
220+
def _register_code(code, string, name):
221+
entry = (len(string),
222+
None,
223+
[line + '\n' for line in string.splitlines()],
224+
name)
225+
stack = [code]
226+
while stack:
227+
code = stack.pop()
228+
for const in code.co_consts:
229+
if isinstance(const, type(code)):
230+
stack.append(const)
231+
_interactive_cache[_make_key(code)] = entry

Lib/test/test_linecache.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import os.path
66
import tempfile
77
import tokenize
8+
from importlib.machinery import ModuleSpec
89
from test import support
910
from test.support import os_helper
11+
from test.support.script_helper import assert_python_ok
1012

1113

1214
FILENAME = linecache.__file__
@@ -82,6 +84,10 @@ def test_getlines(self):
8284
class EmptyFile(GetLineTestsGoodData, unittest.TestCase):
8385
file_list = []
8486

87+
def test_getlines(self):
88+
lines = linecache.getlines(self.file_name)
89+
self.assertEqual(lines, ['\n'])
90+
8591

8692
class SingleEmptyLine(GetLineTestsGoodData, unittest.TestCase):
8793
file_list = ['\n']
@@ -97,6 +103,16 @@ class BadUnicode_WithDeclaration(GetLineTestsBadData, unittest.TestCase):
97103
file_byte_string = b'# coding=utf-8\n\x80abc'
98104

99105

106+
class FakeLoader:
107+
def get_source(self, fullname):
108+
return f'source for {fullname}'
109+
110+
111+
class NoSourceLoader:
112+
def get_source(self, fullname):
113+
return None
114+
115+
100116
class LineCacheTests(unittest.TestCase):
101117

102118
def test_getline(self):
@@ -238,6 +254,70 @@ def raise_memoryerror(*args, **kwargs):
238254
self.assertEqual(lines3, [])
239255
self.assertEqual(linecache.getlines(FILENAME), lines)
240256

257+
def test_loader(self):
258+
filename = 'scheme://path'
259+
260+
for loader in (None, object(), NoSourceLoader()):
261+
linecache.clearcache()
262+
module_globals = {'__name__': 'a.b.c', '__loader__': loader}
263+
self.assertEqual(linecache.getlines(filename, module_globals), [])
264+
265+
linecache.clearcache()
266+
module_globals = {'__name__': 'a.b.c', '__loader__': FakeLoader()}
267+
self.assertEqual(linecache.getlines(filename, module_globals),
268+
['source for a.b.c\n'])
269+
270+
for spec in (None, object(), ModuleSpec('', FakeLoader())):
271+
linecache.clearcache()
272+
module_globals = {'__name__': 'a.b.c', '__loader__': FakeLoader(),
273+
'__spec__': spec}
274+
self.assertEqual(linecache.getlines(filename, module_globals),
275+
['source for a.b.c\n'])
276+
277+
linecache.clearcache()
278+
spec = ModuleSpec('x.y.z', FakeLoader())
279+
module_globals = {'__name__': 'a.b.c', '__loader__': spec.loader,
280+
'__spec__': spec}
281+
self.assertEqual(linecache.getlines(filename, module_globals),
282+
['source for x.y.z\n'])
283+
284+
def test_invalid_names(self):
285+
for name, desc in [
286+
('\x00', 'NUL bytes filename'),
287+
(__file__ + '\x00', 'filename with embedded NUL bytes'),
288+
# A filename with surrogate codes. A UnicodeEncodeError is raised
289+
# by os.stat() upon querying, which is a subclass of ValueError.
290+
("\uD834\uDD1E.py", 'surrogate codes (MUSICAL SYMBOL G CLEF)'),
291+
# For POSIX platforms, an OSError will be raised but for Windows
292+
# platforms, a ValueError is raised due to the path_t converter.
293+
# See: https://github.com/python/cpython/issues/122170
294+
('a' * 1_000_000, 'very long filename'),
295+
]:
296+
with self.subTest(f'updatecache: {desc}'):
297+
linecache.clearcache()
298+
lines = linecache.updatecache(name)
299+
self.assertListEqual(lines, [])
300+
self.assertNotIn(name, linecache.cache)
301+
302+
# hack into the cache (it shouldn't be allowed
303+
# but we never know what people do...)
304+
for key, fullname in [(name, 'ok'), ('key', name), (name, name)]:
305+
with self.subTest(f'checkcache: {desc}',
306+
key=key, fullname=fullname):
307+
linecache.clearcache()
308+
linecache.cache[key] = (0, 1234, [], fullname)
309+
linecache.checkcache(key)
310+
self.assertNotIn(key, linecache.cache)
311+
312+
# just to be sure that we did not mess with cache
313+
linecache.clearcache()
314+
315+
def test_linecache_python_string(self):
316+
cmdline = "import linecache;assert len(linecache.cache) == 0"
317+
retcode, stdout, stderr = assert_python_ok('-c', cmdline)
318+
self.assertEqual(retcode, 0)
319+
self.assertEqual(stdout, b'')
320+
self.assertEqual(stderr, b'')
241321

242322
class LineCacheInvalidationTests(unittest.TestCase):
243323
def setUp(self):

0 commit comments

Comments
 (0)
0