FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Issue #23838: linecache now clears the cache and returns an empty res… · python/cpython@c512adc · GitHub

Repository navigation

Commit c512adc

Browse files
Issue #23838: linecache now clears the cache and returns an empty result on
MemoryError.
1 parent 263dcd2 commit c512adc

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

‎Lib/linecache.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ def getlines(filename, module_globals=None):
3737

3838
if filename in cache:
3939
return cache[filename][2]
40-
else:
40+
41+
try:
4142
return updatecache(filename, module_globals)
43+
except MemoryError:
44+
clearcache()
45+
return []
4246

4347

4448
def checkcache(filename=None):

‎Lib/test/test_linecache.py‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,21 @@ def test_checkcache(self):
126126
self.assertEqual(line, getline(source_name, index + 1))
127127
source_list.append(line)
128128

129-
def test_main():
130-
support.run_unittest(LineCacheTests)
129+
def test_memoryerror(self):
130+
lines = linecache.getlines(FILENAME)
131+
self.assertTrue(lines)
132+
def raise_memoryerror(*args, **kwargs):
133+
raise MemoryError
134+
with support.swap_attr(linecache, 'updatecache', raise_memoryerror):
135+
lines2 = linecache.getlines(FILENAME)
136+
self.assertEqual(lines2, lines)
137+
138+
linecache.clearcache()
139+
with support.swap_attr(linecache, 'updatecache', raise_memoryerror):
140+
lines3 = linecache.getlines(FILENAME)
141+
self.assertEqual(lines3, [])
142+
self.assertEqual(linecache.getlines(FILENAME), lines)
143+
131144

132145
if __name__ == "__main__":
133-
test_main()
146+
unittest.main()

‎Misc/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Core and Builtins
2121
Library
2222
-------
2323

24+
- Issue #23838: linecache now clears the cache and returns an empty result on
25+
MemoryError.
26+
2427
- Issue #18473: Fixed 2to3 and 3to2 compatible pickle mappings. Fixed
2528
ambigious reverse mappings. Added many new mappings. Import mapping is no
2629
longer applied to modules already mapped with full name mapping.

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL