@@ -20,6 +20,8 @@ class AbstractContextManager(abc.ABC):
20
20
21
21
__class_getitem__ = classmethod (GenericAlias )
22
22
23
+ __slots__ = ()
24
+
23
25
def __enter__ (self ):
24
26
"""Return `self` upon entering the runtime context."""
25
27
return self
@@ -42,6 +44,8 @@ class AbstractAsyncContextManager(abc.ABC):
42
44
43
45
__class_getitem__ = classmethod (GenericAlias )
44
46
47
+ __slots__ = ()
48
+
45
49
async def __aenter__ (self ):
46
50
"""Return `self` upon entering the runtime context."""
47
51
return self
@@ -565,11 +569,12 @@ def __enter__(self):
565
569
return self
566
570
567
571
def __exit__ (self , * exc_details ):
568
- received_exc = exc_details [0 ] is not None
572
+ exc = exc_details [1 ]
573
+ received_exc = exc is not None
569
574
570
575
# We manipulate the exception state so it behaves as though
571
576
# we were actually nesting multiple with statements
572
- frame_exc = sys .exc_info ()[ 1 ]
577
+ frame_exc = sys .exception ()
573
578
def _fix_exception_context (new_exc , old_exc ):
574
579
# Context may not be correct, so find the end of the chain
575
580
while 1 :
@@ -592,24 +597,28 @@ def _fix_exception_context(new_exc, old_exc):
592
597
is_sync , cb = self ._exit_callbacks .pop ()
593
598
assert is_sync
594
599
try :
600
+ if exc is None :
601
+ exc_details = None , None , None
602
+ else :
603
+ exc_details = type (exc ), exc , exc .__traceback__
595
604
if cb (* exc_details ):
596
605
suppressed_exc = True
597
606
pending_raise = False
598
- exc_details = (None , None , None )
599
- except :
600
- new_exc_details = sys .exc_info ()
607
+ exc = None
608
+ except BaseException as new_exc :
601
609
# simulate the stack of exceptions by setting the context
602
- _fix_exception_context (new_exc_details [ 1 ], exc_details [ 1 ] )
610
+ _fix_exception_context (new_exc , exc )
603
611
pending_raise = True
604
- exc_details = new_exc_details
612
+ exc = new_exc
613
+
605
614
if pending_raise :
606
615
try :
607
- # bare "raise exc_details[1] " replaces our carefully
616
+ # bare "raise exc " replaces our carefully
608
617
# set-up context
609
- fixed_ctx = exc_details [ 1 ] .__context__
610
- raise exc_details [ 1 ]
618
+ fixed_ctx = exc .__context__
619
+ raise exc
611
620
except BaseException :
612
- exc_details [ 1 ] .__context__ = fixed_ctx
621
+ exc .__context__ = fixed_ctx
613
622
raise
614
623
return received_exc and suppressed_exc
615
624
@@ -705,11 +714,12 @@ async def __aenter__(self):
705
714
return self
706
715
707
716
async def __aexit__ (self , * exc_details ):
708
- received_exc = exc_details [0 ] is not None
717
+ exc = exc_details [1 ]
718
+ received_exc = exc is not None
709
719
710
720
# We manipulate the exception state so it behaves as though
711
721
# we were actually nesting multiple with statements
712
- frame_exc = sys .exc_info ()[ 1 ]
722
+ frame_exc = sys .exception ()
713
723
def _fix_exception_context (new_exc , old_exc ):
714
724
# Context may not be correct, so find the end of the chain
715
725
while 1 :
@@ -731,6 +741,10 @@ def _fix_exception_context(new_exc, old_exc):
731
741
while self ._exit_callbacks :
732
742
is_sync , cb = self ._exit_callbacks .pop ()
733
743
try :
744
+ if exc is None :
745
+ exc_details = None , None , None
746
+ else :
747
+ exc_details = type (exc ), exc , exc .__traceback__
734
748
if is_sync :
735
749
cb_suppress = cb (* exc_details )
736
750
else :
@@ -739,21 +753,21 @@ def _fix_exception_context(new_exc, old_exc):
739
753
if cb_suppress :
740
754
suppressed_exc = True
741
755
pending_raise = False
742
- exc_details = (None , None , None )
743
- except :
744
- new_exc_details = sys .exc_info ()
756
+ exc = None
757
+ except BaseException as new_exc :
745
758
# simulate the stack of exceptions by setting the context
746
- _fix_exception_context (new_exc_details [ 1 ], exc_details [ 1 ] )
759
+ _fix_exception_context (new_exc , exc )
747
760
pending_raise = True
748
- exc_details = new_exc_details
761
+ exc = new_exc
762
+
749
763
if pending_raise :
750
764
try :
751
- # bare "raise exc_details[1] " replaces our carefully
765
+ # bare "raise exc " replaces our carefully
752
766
# set-up context
753
- fixed_ctx = exc_details [ 1 ] .__context__
754
- raise exc_details [ 1 ]
767
+ fixed_ctx = exc .__context__
768
+ raise exc
755
769
except BaseException :
756
- exc_details [ 1 ] .__context__ = fixed_ctx
770
+ exc .__context__ = fixed_ctx
757
771
raise
758
772
return received_exc and suppressed_exc
759
773
0 commit comments