Issue #23838: linecache now clears the cache and returns an empty result on

MemoryError.
This commit is contained in:
Serhiy Storchaka 2015-04-01 16:54:05 +03:00
parent 263dcd20a3
commit c512adc90d
3 changed files with 24 additions and 4 deletions

View file

@ -37,8 +37,12 @@ def getlines(filename, module_globals=None):
if filename in cache: if filename in cache:
return cache[filename][2] return cache[filename][2]
else:
try:
return updatecache(filename, module_globals) return updatecache(filename, module_globals)
except MemoryError:
clearcache()
return []
def checkcache(filename=None): def checkcache(filename=None):

View file

@ -126,8 +126,21 @@ class LineCacheTests(unittest.TestCase):
self.assertEqual(line, getline(source_name, index + 1)) self.assertEqual(line, getline(source_name, index + 1))
source_list.append(line) source_list.append(line)
def test_main(): def test_memoryerror(self):
support.run_unittest(LineCacheTests) lines = linecache.getlines(FILENAME)
self.assertTrue(lines)
def raise_memoryerror(*args, **kwargs):
raise MemoryError
with support.swap_attr(linecache, 'updatecache', raise_memoryerror):
lines2 = linecache.getlines(FILENAME)
self.assertEqual(lines2, lines)
linecache.clearcache()
with support.swap_attr(linecache, 'updatecache', raise_memoryerror):
lines3 = linecache.getlines(FILENAME)
self.assertEqual(lines3, [])
self.assertEqual(linecache.getlines(FILENAME), lines)
if __name__ == "__main__": if __name__ == "__main__":
test_main() unittest.main()

View file

@ -21,6 +21,9 @@ Core and Builtins
Library Library
------- -------
- Issue #23838: linecache now clears the cache and returns an empty result on
MemoryError.
- Issue #18473: Fixed 2to3 and 3to2 compatible pickle mappings. Fixed - Issue #18473: Fixed 2to3 and 3to2 compatible pickle mappings. Fixed
ambigious reverse mappings. Added many new mappings. Import mapping is no ambigious reverse mappings. Added many new mappings. Import mapping is no
longer applied to modules already mapped with full name mapping. longer applied to modules already mapped with full name mapping.