Skip to content

Commit 405a921

Browse files
committed
fix linecache
1 parent 645a609 commit 405a921

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,12 @@ def test_loader(self):
280280
self.assertEqual(linecache.getlines(filename, module_globals),
281281
['source for x.y.z\n'])
282282

283+
@unittest.skipIf(os.name != 'nt', 'Windows only')
284+
def test_windows_long_filename(self):
285+
linecache.clearcache()
286+
line = linecache.getline('a' * 9999, 1)
287+
self.assertEqual(line, '')
288+
283289

284290
class LineCacheInvalidationTests(unittest.TestCase):
285291
def setUp(self):

0 commit comments

Comments
 (0)