From: Serhiy Storchaka Date: Wed, 1 Apr 2015 13:53:53 +0000 (+0300) Subject: Issue #23838: linecache now clears the cache and returns an empty result on X-Git-Tag: v2.7.10rc1~85 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=43b49593e700baeb7bf2be9dd1877eee5829e9e3;p=thirdparty%2FPython%2Fcpython.git Issue #23838: linecache now clears the cache and returns an empty result on MemoryError. --- diff --git a/Lib/linecache.py b/Lib/linecache.py index 811f27fe3657..4b97be3f05f8 100644 --- a/Lib/linecache.py +++ b/Lib/linecache.py @@ -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): diff --git a/Lib/test/test_linecache.py b/Lib/test/test_linecache.py index b5f80a4f43e2..6f7ecd15d289 100644 --- a/Lib/test/test_linecache.py +++ b/Lib/test/test_linecache.py @@ -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) diff --git a/Misc/NEWS b/Misc/NEWS index 410f0b0e1723..9b57f4ab9cc1 100644 --- 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