1+ from __future__ import annotations
2+
13import inspect
24import signal
35import sys
46from functools import wraps
5- import attr
7+ from typing import TYPE_CHECKING
68
7- import async_generator
9+ import attr
810
911from .._util import is_main_thread
1012
11- if False :
13+ if TYPE_CHECKING :
1214 from typing import Any , TypeVar , Callable
1315
1416 F = TypeVar ("F" , bound = Callable [..., Any ])
5557#
5658# If this raises a KeyboardInterrupt, it might be because the coroutine got
5759# interrupted and has unwound... or it might be the KeyboardInterrupt
58- # arrived just *after* 'send' returned, so the coroutine is still running
60+ # arrived just *after* 'send' returned, so the coroutine is still running,
5961# but we just lost the message it sent. (And worse, in our actual task
6062# runner, the send is hidden inside a utility function etc.)
6163#
@@ -109,6 +111,14 @@ def currently_ki_protected():
109111 return ki_protection_enabled (sys ._getframe ())
110112
111113
114+ # This is to support the async_generator package necessary for aclosing on <3.10
115+ # functions decorated @async_generator are given this magic property that's a
116+ # reference to the object itself
117+ # see python-trio/async_generator/async_generator/_impl.py
118+ def legacy_isasyncgenfunction (obj ):
119+ return getattr (obj , "_async_gen_function" , None ) == id (obj )
120+
121+
112122def _ki_protection_decorator (enabled ):
113123 def decorator (fn ):
114124 # In some version of Python, isgeneratorfunction returns true for
@@ -141,7 +151,7 @@ def wrapper(*args, **kwargs):
141151 return gen
142152
143153 return wrapper
144- elif async_generator .isasyncgenfunction (fn ):
154+ elif inspect .isasyncgenfunction ( fn ) or legacy_isasyncgenfunction (fn ):
145155
146156 @wraps (fn )
147157 def wrapper (* args , ** kwargs ):
@@ -163,10 +173,10 @@ def wrapper(*args, **kwargs):
163173 return decorator
164174
165175
166- enable_ki_protection = _ki_protection_decorator ( True ) # type : Callable[[F], F]
176+ enable_ki_protection : Callable [[F ], F ] = _ki_protection_decorator ( True )
167177enable_ki_protection .__name__ = "enable_ki_protection"
168178
169- disable_ki_protection = _ki_protection_decorator ( False ) # type : Callable[[F], F]
179+ disable_ki_protection : Callable [[F ], F ] = _ki_protection_decorator ( False )
170180disable_ki_protection .__name__ = "disable_ki_protection"
171181
172182
0 commit comments