@@ -557,11 +557,12 @@ def __enter__(self):
557
557
return self
558
558
559
559
def __exit__ (self , * exc_details ):
560
- received_exc = exc_details [0 ] is not None
560
+ exc = exc_details [1 ]
561
+ received_exc = exc is not None
561
562
562
563
# We manipulate the exception state so it behaves as though
563
564
# we were actually nesting multiple with statements
564
- frame_exc = sys .exc_info ()[ 1 ]
565
+ frame_exc = sys .exception ()
565
566
def _fix_exception_context (new_exc , old_exc ):
566
567
# Context may not be correct, so find the end of the chain
567
568
while 1 :
@@ -584,24 +585,28 @@ def _fix_exception_context(new_exc, old_exc):
584
585
is_sync , cb = self ._exit_callbacks .pop ()
585
586
assert is_sync
586
587
try :
588
+ if exc is None :
589
+ exc_details = None , None , None
590
+ else :
591
+ exc_details = type (exc ), exc , exc .__traceback__
587
592
if cb (* exc_details ):
588
593
suppressed_exc = True
589
594
pending_raise = False
590
- exc_details = (None , None , None )
591
- except :
592
- new_exc_details = sys .exc_info ()
595
+ exc = None
596
+ except BaseException as new_exc :
593
597
# simulate the stack of exceptions by setting the context
594
- _fix_exception_context (new_exc_details [ 1 ], exc_details [ 1 ] )
598
+ _fix_exception_context (new_exc , exc )
595
599
pending_raise = True
596
- exc_details = new_exc_details
600
+ exc = new_exc
601
+
597
602
if pending_raise :
598
603
try :
599
- # bare "raise exc_details[1] " replaces our carefully
604
+ # bare "raise exc " replaces our carefully
600
605
# set-up context
601
- fixed_ctx = exc_details [ 1 ] .__context__
602
- raise exc_details [ 1 ]
606
+ fixed_ctx = exc .__context__
607
+ raise exc
603
608
except BaseException :
604
- exc_details [ 1 ] .__context__ = fixed_ctx
609
+ exc .__context__ = fixed_ctx
605
610
raise
606
611
return received_exc and suppressed_exc
607
612
@@ -697,11 +702,12 @@ async def __aenter__(self):
697
702
return self
698
703
699
704
async def __aexit__ (self , * exc_details ):
700
- received_exc = exc_details [0 ] is not None
705
+ exc = exc_details [1 ]
706
+ received_exc = exc is not None
701
707
702
708
# We manipulate the exception state so it behaves as though
703
709
# we were actually nesting multiple with statements
704
- frame_exc = sys .exc_info ()[ 1 ]
710
+ frame_exc = sys .exception ()
705
711
def _fix_exception_context (new_exc , old_exc ):
706
712
# Context may not be correct, so find the end of the chain
707
713
while 1 :
@@ -723,6 +729,10 @@ def _fix_exception_context(new_exc, old_exc):
723
729
while self ._exit_callbacks :
724
730
is_sync , cb = self ._exit_callbacks .pop ()
725
731
try :
732
+ if exc is None :
733
+ exc_details = None , None , None
734
+ else :
735
+ exc_details = type (exc ), exc , exc .__traceback__
726
736
if is_sync :
727
737
cb_suppress = cb (* exc_details )
728
738
else :
@@ -731,21 +741,21 @@ def _fix_exception_context(new_exc, old_exc):
731
741
if cb_suppress :
732
742
suppressed_exc = True
733
743
pending_raise = False
734
- exc_details = (None , None , None )
735
- except :
736
- new_exc_details = sys .exc_info ()
744
+ exc = None
745
+ except BaseException as new_exc :
737
746
# simulate the stack of exceptions by setting the context
738
- _fix_exception_context (new_exc_details [ 1 ], exc_details [ 1 ] )
747
+ _fix_exception_context (new_exc , exc )
739
748
pending_raise = True
740
- exc_details = new_exc_details
749
+ exc = new_exc
750
+
741
751
if pending_raise :
742
752
try :
743
- # bare "raise exc_details[1] " replaces our carefully
753
+ # bare "raise exc " replaces our carefully
744
754
# set-up context
745
- fixed_ctx = exc_details [ 1 ] .__context__
746
- raise exc_details [ 1 ]
755
+ fixed_ctx = exc .__context__
756
+ raise exc
747
757
except BaseException :
748
- exc_details [ 1 ] .__context__ = fixed_ctx
758
+ exc .__context__ = fixed_ctx
749
759
raise
750
760
return received_exc and suppressed_exc
751
761
0 commit comments