@@ -153,28 +153,32 @@ def handle(self, name, args, kwargs):
153153 If args is not empty, call a callable passing positional and keyword
154154 arguments.
155155 """
156- if not _is_subtype (self .expected , self ._base_type ):
157- raise TypeError ('%s() arg 1 must be %s' %
158- (name , self ._base_type_str ))
159- if args and args [0 ] is None :
160- warnings .warn ("callable is None" ,
161- DeprecationWarning , 3 )
162- args = ()
163- if not args :
164- self .msg = kwargs .pop ('msg' , None )
165- if kwargs :
166- warnings .warn ('%r is an invalid keyword argument for '
167- 'this function' % next (iter (kwargs )),
168- DeprecationWarning , 3 )
169- return self
170-
171- callable_obj , * args = args
172156 try :
173- self .obj_name = callable_obj .__name__
174- except AttributeError :
175- self .obj_name = str (callable_obj )
176- with self :
177- callable_obj (* args , ** kwargs )
157+ if not _is_subtype (self .expected , self ._base_type ):
158+ raise TypeError ('%s() arg 1 must be %s' %
159+ (name , self ._base_type_str ))
160+ if args and args [0 ] is None :
161+ warnings .warn ("callable is None" ,
162+ DeprecationWarning , 3 )
163+ args = ()
164+ if not args :
165+ self .msg = kwargs .pop ('msg' , None )
166+ if kwargs :
167+ warnings .warn ('%r is an invalid keyword argument for '
168+ 'this function' % next (iter (kwargs )),
169+ DeprecationWarning , 3 )
170+ return self
171+
172+ callable_obj , * args = args
173+ try :
174+ self .obj_name = callable_obj .__name__
175+ except AttributeError :
176+ self .obj_name = str (callable_obj )
177+ with self :
178+ callable_obj (* args , ** kwargs )
179+ finally :
180+ # bpo-23890: manually break a reference cycle
181+ self = None
178182
179183
180184class _AssertRaisesContext (_AssertRaisesBaseContext ):
@@ -725,7 +729,11 @@ def assertRaises(self, expected_exception, *args, **kwargs):
725729 self.assertEqual(the_exception.error_code, 3)
726730 """
727731 context = _AssertRaisesContext (expected_exception , self )
728- return context .handle ('assertRaises' , args , kwargs )
732+ try :
733+ return context .handle ('assertRaises' , args , kwargs )
734+ finally :
735+ # bpo-23890: manually break a reference cycle
736+ context = None
729737
730738 def assertWarns (self , expected_warning , * args , ** kwargs ):
731739 """Fail unless a warning of class warnClass is triggered
0 commit comments