@@ -505,6 +505,18 @@ def __enter__(self):
505505 def __exit__ (self , * exc_details ):
506506 raise self .exc
507507
508+ class RaiseExcWithContext :
509+ def __init__ (self , outer , inner ):
510+ self .outer = outer
511+ self .inner = inner
512+ def __enter__ (self ):
513+ return self
514+ def __exit__ (self , * exc_details ):
515+ try :
516+ raise self .inner
517+ except :
518+ raise self .outer
519+
508520 class SuppressExc :
509521 def __enter__ (self ):
510522 return self
@@ -514,11 +526,10 @@ def __exit__(self, *exc_details):
514526
515527 try :
516528 with RaiseExc (IndexError ):
517- with RaiseExc (KeyError ):
518- with RaiseExc (AttributeError ):
519- with SuppressExc ():
520- with RaiseExc (ValueError ):
521- 1 / 0
529+ with RaiseExcWithContext (KeyError , AttributeError ):
530+ with SuppressExc ():
531+ with RaiseExc (ValueError ):
532+ 1 / 0
522533 except IndexError as exc :
523534 self .assertIsInstance (exc .__context__ , KeyError )
524535 self .assertIsInstance (exc .__context__ .__context__ , AttributeError )
@@ -553,12 +564,8 @@ def suppress_exc(*exc_details):
553564 except IndexError as exc :
554565 self .assertIsInstance (exc .__context__ , KeyError )
555566 self .assertIsInstance (exc .__context__ .__context__ , AttributeError )
556- # Inner exceptions were suppressed, but the with statement
557- # cleanup code adds the one from the body back in as the
558- # context of the exception raised by the outer callbacks
559- # See http://bugs.python.org/issue14969
560- suite_exc = exc .__context__ .__context__ .__context__
561- self .assertIsInstance (suite_exc , ZeroDivisionError )
567+ # Inner exceptions were suppressed
568+ self .assertIsNone (exc .__context__ .__context__ .__context__ )
562569 else :
563570 self .fail ("Expected IndexError, but no exception was raised" )
564571 # Check the inner exceptions
0 commit comments