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

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

index e505b031240bfdcbbe116380852d3a10ec1167ec..884cbf493a494da752d15c6a7b52d575fa7649a2 100644 (file)
@@ -37,8 +37,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 5fe0554572c102e149c45d3004160870cd9c4612..79157de43f6231240dc22fbc1ddd56a7cb053512 100644 (file)
@@ -126,8 +126,21 @@ class LineCacheTests(unittest.TestCase):
                 self.assertEqual(line, getline(source_name, index + 1))
                 source_list.append(line)
 
-def test_main():
-    support.run_unittest(LineCacheTests)
+    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)
+
 
 if __name__ == "__main__":
-    test_main()
+    unittest.main()
index 33fc0ee3b0267e380b2d5ef908140ef267cda986..58cdebdee26731d5c19ba69475a618037fd255c4 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 #18473: Fixed 2to3 and 3to2 compatible pickle mappings.  Fixed
   ambigious reverse mappings.  Added many new mappings.  Import mapping is no
   longer applied to modules already mapped with full name mapping.