Skip to content

Commit a2142a7

Browse files
committed
fix linecache
1 parent a9bb3c7 commit a2142a7

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Lib/linecache.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ def checkcache(filename=None):
7373
except OSError:
7474
cache.pop(filename, None)
7575
continue
76+
except ValueError:
77+
# ValueError may happen on Windows platforms for long paths.
78+
# In this case, we assume that we could not just read the file.
79+
#
80+
# See: https://github.com/python/cpython/issues/122170.
81+
if os.name == 'nt':
82+
cache.pop(filename, None)
83+
continue
84+
raise # this should not happen on other platforms
7685
if size != stat.st_size or mtime != stat.st_mtime:
7786
cache.pop(filename, None)
7887

@@ -137,8 +146,15 @@ def updatecache(filename, module_globals=None):
137146
break
138147
except OSError:
139148
pass
149+
except ValueError:
150+
if os.name != 'nt':
151+
raise # this should not happen on other platforms
140152
else:
141153
return []
154+
except ValueError:
155+
if os.name != 'nt':
156+
raise # this should not happen on other platforms
157+
return []
142158
try:
143159
with tokenize.open(fullname) as fp:
144160
lines = fp.readlines()

Lib/test/test_linecache.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,28 @@ def test_loader(self):
280280
self.assertEqual(linecache.getlines(filename, module_globals),
281281
['source for x.y.z\n'])
282282

283+
def test_long_filename(self):
284+
# For POSIX platforms, an OSError will be raised and will take
285+
# the usual path handling. For Windows platforms, a ValueError
286+
# is raised instead but linecache will handle it as if it were
287+
# an OSError in this case.
288+
#
289+
# See: https://github.com/python/cpython/issues/122170
290+
291+
linecache.clearcache()
292+
lines = linecache.updatecache('a' * 9999)
293+
self.assertListEqual(lines, [])
294+
self.assertNotIn('a' * 9999, linecache.cache)
295+
296+
# hack into the cache (it shouldn't be allowed
297+
# but we never know what people do...)
298+
linecache.cache['smallname'] = (0, 1234, [], 'a' * 9999)
299+
linecache.checkcache('smallname')
300+
self.assertNotIn('smallname', linecache.cache)
301+
302+
# just to be sure that we did not mess with cache
303+
linecache.clearcache()
304+
283305

284306
class LineCacheInvalidationTests(unittest.TestCase):
285307
def setUp(self):

0 commit comments

Comments
 (0)