[Python-checkins] cpython (2.7): Issue #23838: linecache now clears the cache and returns an empty result on

serhiy.storchaka python-checkins at python.org
Wed Apr 1 15:59:07 CEST 2015


https://hg.python.org/cpython/rev/d444496e714a
changeset:   95344:d444496e714a
branch:      2.7
parent:      95340:f8358f6e50e7
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Wed Apr 01 16:53:53 2015 +0300
summary:
  Issue #23838: linecache now clears the cache and returns an empty result on
MemoryError.

files:
  Lib/linecache.py           |   6 +++++-
  Lib/test/test_linecache.py |  16 ++++++++++++++++
  Misc/NEWS                  |   3 +++
  3 files changed, 24 insertions(+), 1 deletions(-)


diff --git a/Lib/linecache.py b/Lib/linecache.py
--- a/Lib/linecache.py
+++ b/Lib/linecache.py
@@ -36,8 +36,12 @@
 
     if filename in cache:
         return cache[filename][2]
-    else:
+
+    try:
         return updatecache(filename, module_globals)
+    except MemoryError:
+        clearcache()
+        return []
 
 
 def checkcache(filename=None):
diff --git a/Lib/test/test_linecache.py b/Lib/test/test_linecache.py
--- a/Lib/test/test_linecache.py
+++ b/Lib/test/test_linecache.py
@@ -124,6 +124,22 @@
                 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)
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,9 @@
 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

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list