@@ -1640,25 +1640,7 @@ def _attempt_delivery_of_any_pending_cancel(self) -> None:
16401640 if not self ._cancel_status .effectively_cancelled :
16411641 return
16421642
1643- # FIXME: I haven't quite figured out how to get the cancel reason to stay
1644- # alive for passing into other threads, but also not cause any cyclic garbage.
1645- if (
1646- self ._cancel_status is None
1647- or self ._cancel_status ._scope ._cancel_reason is None
1648- ):
1649- # _cancel_status._cancel_reason is None when misnesting
1650- cancelled = Cancelled ._create (source = "unknown" , reason = "misnesting" )
1651- else :
1652- cancelled = Cancelled ._create (
1653- source = self ._cancel_status ._scope ._cancel_reason .source ,
1654- reason = self ._cancel_status ._scope ._cancel_reason .reason ,
1655- source_task = self ._cancel_status ._scope ._cancel_reason .source_task ,
1656- )
1657-
1658- def raise_cancel () -> NoReturn :
1659- raise cancelled
1660-
1661- self ._attempt_abort (raise_cancel )
1643+ self ._attempt_abort (RaiseCancel (self ._cancel_status ._scope ._cancel_reason ))
16621644
16631645 def _attempt_delivery_of_pending_ki (self ) -> None :
16641646 assert self ._runner .ki_pending
@@ -1672,6 +1654,24 @@ def raise_cancel() -> NoReturn:
16721654 self ._attempt_abort (raise_cancel )
16731655
16741656
1657+ class RaiseCancel :
1658+ def __init__ (self , reason : CancelReason | None ) -> None :
1659+ if reason is None :
1660+ self .cancelled = Cancelled ._create (source = "unknown" , reason = "misnesting" )
1661+ else :
1662+ self .cancelled = Cancelled ._create (
1663+ source = reason .source ,
1664+ reason = reason .reason ,
1665+ source_task = reason .source_task ,
1666+ )
1667+
1668+ def __call__ (self ) -> NoReturn :
1669+ try :
1670+ raise self .cancelled
1671+ finally :
1672+ del self .cancelled
1673+
1674+
16751675################################################################
16761676# The central Runner object
16771677################################################################
0 commit comments