Kaydet (Commit) 05ddbf08 authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka

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

MemoryError.
...@@ -40,11 +40,14 @@ def getlines(filename, module_globals=None): ...@@ -40,11 +40,14 @@ def getlines(filename, module_globals=None):
if filename in cache: if filename in cache:
entry = cache[filename] entry = cache[filename]
if len(entry) == 1: if len(entry) != 1:
return updatecache(filename, module_globals)
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):
......
...@@ -169,9 +169,21 @@ class LineCacheTests(unittest.TestCase): ...@@ -169,9 +169,21 @@ class LineCacheTests(unittest.TestCase):
linecache.lazycache(NONEXISTENT_FILENAME, globals())) linecache.lazycache(NONEXISTENT_FILENAME, globals()))
self.assertEqual(4, len(linecache.cache[NONEXISTENT_FILENAME])) self.assertEqual(4, len(linecache.cache[NONEXISTENT_FILENAME]))
def test_memoryerror(self):
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)
def test_main():
support.run_unittest(LineCacheTests)
if __name__ == "__main__": if __name__ == "__main__":
test_main() unittest.main()
...@@ -13,6 +13,9 @@ Core and Builtins ...@@ -13,6 +13,9 @@ Core and Builtins
Library Library
------- -------
- Issue #23838: linecache now clears the cache and returns an empty result on
MemoryError.
- Issue #10395: Added os.path.commonpath(). Implemented in posixpath and ntpath. - Issue #10395: Added os.path.commonpath(). Implemented in posixpath and ntpath.
Based on patch by Rafik Draoui. Based on patch by Rafik Draoui.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment