aboutsummaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/linecache.py11
-rw-r--r--Lib/test/test_linecache.py18
2 files changed, 22 insertions, 7 deletions
diff --git a/Lib/linecache.py b/Lib/linecache.py
index 6e65c37e135..3afcce1f0a1 100644
--- a/Lib/linecache.py
+++ b/Lib/linecache.py
@@ -40,11 +40,14 @@ def getlines(filename, module_globals=None):
if filename in cache:
entry = cache[filename]
- if len(entry) == 1:
- return updatecache(filename, module_globals)
- return cache[filename][2]
- else:
+ if len(entry) != 1:
+ return cache[filename][2]
+
+ 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 2fb86626f7d..21ef7389325 100644
--- a/Lib/test/test_linecache.py
+++ b/Lib/test/test_linecache.py
@@ -169,9 +169,21 @@ class LineCacheTests(unittest.TestCase):
linecache.lazycache(NONEXISTENT_FILENAME, globals()))
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__":
- test_main()
+ unittest.main()