MCPcopy Index your code
hub / github.com/python/cpython / testExceptionCleanupState

Method testExceptionCleanupState

Lib/test/test_exceptions.py:790–909  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

788 e
789
790 def testExceptionCleanupState(self):
791 # Make sure exception state is cleaned up as soon as the except
792 # block is left. See #2507
793
794 class MyException(Exception):
795 def __init__(self, obj):
796 self.obj = obj
797 class MyObj:
798 pass
799
800 def inner_raising_func():
801 # Create some references in exception value and traceback
802 local_ref = obj
803 raise MyException(obj)
804
805 # Qualified "except" with "as"
806 obj = MyObj()
807 wr = weakref.ref(obj)
808 try:
809 inner_raising_func()
810 except MyException as e:
811 pass
812 obj = None
813 gc_collect() # For PyPy or other GCs.
814 obj = wr()
815 self.assertIsNone(obj)
816
817 # Qualified "except" without "as"
818 obj = MyObj()
819 wr = weakref.ref(obj)
820 try:
821 inner_raising_func()
822 except MyException:
823 pass
824 obj = None
825 gc_collect() # For PyPy or other GCs.
826 obj = wr()
827 self.assertIsNone(obj)
828
829 # Bare "except"
830 obj = MyObj()
831 wr = weakref.ref(obj)
832 try:
833 inner_raising_func()
834 except:
835 pass
836 obj = None
837 gc_collect() # For PyPy or other GCs.
838 obj = wr()
839 self.assertIsNone(obj)
840
841 # "except" with premature block leave
842 obj = MyObj()
843 wr = weakref.ref(obj)
844 for i in [0]:
845 try:
846 inner_raising_func()
847 except:

Callers

nothing calls this directly

Calls 5

gc_collectFunction · 0.90
check_impl_detailFunction · 0.90
assertIsNoneMethod · 0.80
MyObjClass · 0.70
ContextClass · 0.70

Tested by

no test coverage detected