|
33 | 33 |
|
34 | 34 | try: |
35 | 35 | _types_coroutine = types.coroutine |
| 36 | + _types_CoroutineType = types.CoroutineType |
36 | 37 | except AttributeError: |
| 38 | + # Python 3.4 |
37 | 39 | _types_coroutine = None |
| 40 | + _types_CoroutineType = None |
38 | 41 |
|
39 | 42 | try: |
40 | 43 | _inspect_iscoroutinefunction = inspect.iscoroutinefunction |
41 | 44 | except AttributeError: |
| 45 | + # Python 3.4 |
42 | 46 | _inspect_iscoroutinefunction = lambda func: False |
43 | 47 |
|
44 | 48 | try: |
@@ -238,19 +242,27 @@ def wrapper(*args, **kwds): |
238 | 242 | w.__qualname__ = getattr(func, '__qualname__', None) |
239 | 243 | return w |
240 | 244 |
|
241 | | - wrapper._is_coroutine = True # For iscoroutinefunction(). |
| 245 | + wrapper._is_coroutine = _is_coroutine # For iscoroutinefunction(). |
242 | 246 | return wrapper |
243 | 247 |
|
244 | 248 |
|
| 249 | +# A marker for iscoroutinefunction. |
| 250 | +_is_coroutine = object() |
| 251 | + |
| 252 | + |
245 | 253 | def iscoroutinefunction(func): |
246 | 254 | """Return True if func is a decorated coroutine function.""" |
247 | | - return (getattr(func, '_is_coroutine', False) or |
| 255 | + return (getattr(func, '_is_coroutine', None) is _is_coroutine or |
248 | 256 | _inspect_iscoroutinefunction(func)) |
249 | 257 |
|
250 | 258 |
|
251 | 259 | _COROUTINE_TYPES = (types.GeneratorType, CoroWrapper) |
252 | 260 | if _CoroutineABC is not None: |
253 | 261 | _COROUTINE_TYPES += (_CoroutineABC,) |
| 262 | +if _types_CoroutineType is not None: |
| 263 | + # Prioritize native coroutine check to speed-up |
| 264 | + # asyncio.iscoroutine. |
| 265 | + _COROUTINE_TYPES = (_types_CoroutineType,) + _COROUTINE_TYPES |
254 | 266 |
|
255 | 267 |
|
256 | 268 | def iscoroutine(obj): |
|
0 commit comments