changeset: 95345:88a0e6cd93c3 branch: 3.4 parent: 95341:8987218adc99 user: Serhiy Storchaka date: Wed Apr 01 16:54:05 2015 +0300 files: Lib/linecache.py Lib/test/test_linecache.py Misc/NEWS description: Issue #23838: linecache now clears the cache and returns an empty result on MemoryError. diff -r 8987218adc99 -r 88a0e6cd93c3 Lib/linecache.py --- a/Lib/linecache.py Wed Apr 01 13:01:14 2015 +0300 +++ b/Lib/linecache.py Wed Apr 01 16:54:05 2015 +0300 @@ -37,8 +37,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 -r 8987218adc99 -r 88a0e6cd93c3 Lib/test/test_linecache.py --- a/Lib/test/test_linecache.py Wed Apr 01 13:01:14 2015 +0300 +++ b/Lib/test/test_linecache.py Wed Apr 01 16:54:05 2015 +0300 @@ -126,8 +126,21 @@ 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() diff -r 8987218adc99 -r 88a0e6cd93c3 Misc/NEWS --- a/Misc/NEWS Wed Apr 01 13:01:14 2015 +0300 +++ b/Misc/NEWS Wed Apr 01 16:54:05 2015 +0300 @@ -21,6 +21,9 @@ 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.