]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #23838: linecache now clears the cache and returns an empty result on
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 1 Apr 2015 13:53:53 +0000 (16:53 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 1 Apr 2015 13:53:53 +0000 (16:53 +0300)
MemoryError.

Lib/linecache.py
Lib/test/test_linecache.py
Misc/NEWS

index 811f27fe3657c776fc5795d469f24cb21615492f..4b97be3f05f8d8a8b4fab3cffaad9c1cd2e58c38 100644 (file)
@@ -36,8 +36,12 @@ def getlines(filename, module_globals=None):
 
     if filename in cache:
         return cache[filename][2]
-    else:
+
+    try:
         return updatecache(filename, module_globals)
+    except MemoryError:
+        clearcache()
+        return []
 
 
 def checkcache(filename=None):
index b5f80a4f43e22fe8d2680f511855227055cf88f9..6f7ecd15d289310aaa4396bf5af488c69be21093 100644 (file)
@@ -124,6 +124,22 @@ class LineCacheTests(unittest.TestCase):
                 self.assertEqual(line, getline(source_name, index + 1))
                 source_list.append(line)
 
+    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)
 
index 410f0b0e1723295bd0925a485969df880ef5729e..9b57f4ab9cc19cb402530b4057ee5e383ccc2be5 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #23838: linecache now clears the cache and returns an empty result on
+  MemoryError.
+
 - Issue #23742: ntpath.expandvars() no longer loses unbalanced single quotes.
 
 - Issue #21802: The reader in BufferedRWPair now is closed even when closing