mirror of
https://github.com/python/cpython.git
synced 2025-09-09 18:32:22 +00:00
Issue #5437: A preallocated MemoryError instance should not hold traceback
data (including local variables caught in the stack trace) alive infinitely.
This commit is contained in:
parent
1842d0c4d8
commit
07e20ef50b
5 changed files with 130 additions and 34 deletions
|
@ -721,6 +721,45 @@ class ExceptionTests(unittest.TestCase):
|
|||
self.assertEqual(error5.a, 1)
|
||||
self.assertEqual(error5.__doc__, "")
|
||||
|
||||
def test_memory_error_cleanup(self):
|
||||
# Issue #5437: preallocated MemoryError instances should not keep
|
||||
# traceback objects alive.
|
||||
from _testcapi import raise_memoryerror
|
||||
class C:
|
||||
pass
|
||||
wr = None
|
||||
def inner():
|
||||
nonlocal wr
|
||||
c = C()
|
||||
wr = weakref.ref(c)
|
||||
raise_memoryerror()
|
||||
# We cannot use assertRaises since it manually deletes the traceback
|
||||
try:
|
||||
inner()
|
||||
except MemoryError as e:
|
||||
self.assertNotEqual(wr(), None)
|
||||
else:
|
||||
self.fail("MemoryError not raised")
|
||||
self.assertEqual(wr(), None)
|
||||
|
||||
def test_recursion_error_cleanup(self):
|
||||
# Same test as above, but with "recursion exceeded" errors
|
||||
class C:
|
||||
pass
|
||||
wr = None
|
||||
def inner():
|
||||
nonlocal wr
|
||||
c = C()
|
||||
wr = weakref.ref(c)
|
||||
inner()
|
||||
# We cannot use assertRaises since it manually deletes the traceback
|
||||
try:
|
||||
inner()
|
||||
except RuntimeError as e:
|
||||
self.assertNotEqual(wr(), None)
|
||||
else:
|
||||
self.fail("RuntimeError not raised")
|
||||
self.assertEqual(wr(), None)
|
||||
|
||||
def test_main():
|
||||
run_unittest(ExceptionTests)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue