@@ -280,6 +280,37 @@ def test_loader(self):
280280 self .assertEqual (linecache .getlines (filename , module_globals ),
281281 ['source for x.y.z\n ' ])
282282
283+ def test_invalid_names (self ):
284+ for name , desc in [
285+ ('\x00 ' , 'NUL bytes filename' ),
286+ (__file__ + '\x00 ' , 'filename with embedded NUL bytes' ),
287+ # A filename with surrogate codes. A UnicodeEncodeError is raised
288+ # by os.stat() upon querying, which is a subclass of ValueError.
289+ ("\uD834 \uDD1E .py" , 'surrogate codes (MUSICAL SYMBOL G CLEF)' ),
290+ # For POSIX platforms, an OSError will be raised but for Windows
291+ # platforms, a ValueError is raised due to the path_t converter.
292+ # See: https://github.com/python/cpython/issues/122170
293+ ('a' * 1_000_000 , 'very long filename' ),
294+ ]:
295+ with self .subTest (f'updatecache: { desc } ' ):
296+ linecache .clearcache ()
297+ lines = linecache .updatecache (name )
298+ self .assertListEqual (lines , [])
299+ self .assertNotIn (name , linecache .cache )
300+
301+ # hack into the cache (it shouldn't be allowed
302+ # but we never know what people do...)
303+ for key , fullname in [(name , 'ok' ), ('key' , name ), (name , name )]:
304+ with self .subTest (f'checkcache: { desc } ' ,
305+ key = key , fullname = fullname ):
306+ linecache .clearcache ()
307+ linecache .cache [key ] = (0 , 1234 , [], fullname )
308+ linecache .checkcache (key )
309+ self .assertNotIn (key , linecache .cache )
310+
311+ # just to be sure that we did not mess with cache
312+ linecache .clearcache ()
313+
283314
284315class LineCacheInvalidationTests (unittest .TestCase ):
285316 def setUp (self ):
0 commit comments