gh-122170: Handle ValueError raised by os.stat() in linecache (GH-122176)

This commit is contained in:
Bénédikt Tran 2024-07-27 12:10:42 +02:00 committed by GitHub
parent 8ac5565be2
commit 7a6d4ccf0e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 37 additions and 2 deletions

View file

@ -280,6 +280,37 @@ class LineCacheTests(unittest.TestCase):
self.assertEqual(linecache.getlines(filename, module_globals),
['source for x.y.z\n'])
def test_invalid_names(self):
for name, desc in [
('\x00', 'NUL bytes filename'),
(__file__ + '\x00', 'filename with embedded NUL bytes'),
# A filename with surrogate codes. A UnicodeEncodeError is raised
# by os.stat() upon querying, which is a subclass of ValueError.
("\uD834\uDD1E.py", 'surrogate codes (MUSICAL SYMBOL G CLEF)'),
# For POSIX platforms, an OSError will be raised but for Windows
# platforms, a ValueError is raised due to the path_t converter.
# See: https://github.com/python/cpython/issues/122170
('a' * 1_000_000, 'very long filename'),
]:
with self.subTest(f'updatecache: {desc}'):
linecache.clearcache()
lines = linecache.updatecache(name)
self.assertListEqual(lines, [])
self.assertNotIn(name, linecache.cache)
# hack into the cache (it shouldn't be allowed
# but we never know what people do...)
for key, fullname in [(name, 'ok'), ('key', name), (name, name)]:
with self.subTest(f'checkcache: {desc}',
key=key, fullname=fullname):
linecache.clearcache()
linecache.cache[key] = (0, 1234, [], fullname)
linecache.checkcache(key)
self.assertNotIn(key, linecache.cache)
# just to be sure that we did not mess with cache
linecache.clearcache()
class LineCacheInvalidationTests(unittest.TestCase):
def setUp(self):