mirror of
https://github.com/python/cpython.git
synced 2025-07-08 03:45:36 +00:00
bpo-32357: Optimize asyncio.iscoroutine() for non-native coroutines (#4915)
This commit is contained in:
parent
a7bd64c0c0
commit
a9d7e552c7
4 changed files with 149 additions and 33 deletions
|
@ -1,5 +1,6 @@
|
|||
__all__ = 'coroutine', 'iscoroutinefunction', 'iscoroutine'
|
||||
|
||||
import collections.abc
|
||||
import functools
|
||||
import inspect
|
||||
import os
|
||||
|
@ -7,8 +8,6 @@ import sys
|
|||
import traceback
|
||||
import types
|
||||
|
||||
from collections.abc import Awaitable, Coroutine
|
||||
|
||||
from . import base_futures
|
||||
from . import constants
|
||||
from . import format_helpers
|
||||
|
@ -162,7 +161,7 @@ def coroutine(func):
|
|||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
if isinstance(res, Awaitable):
|
||||
if isinstance(res, collections.abc.Awaitable):
|
||||
res = yield from await_meth()
|
||||
return res
|
||||
|
||||
|
@ -199,12 +198,24 @@ def iscoroutinefunction(func):
|
|||
# Prioritize native coroutine check to speed-up
|
||||
# asyncio.iscoroutine.
|
||||
_COROUTINE_TYPES = (types.CoroutineType, types.GeneratorType,
|
||||
Coroutine, CoroWrapper)
|
||||
collections.abc.Coroutine, CoroWrapper)
|
||||
_iscoroutine_typecache = set()
|
||||
|
||||
|
||||
def iscoroutine(obj):
|
||||
"""Return True if obj is a coroutine object."""
|
||||
return isinstance(obj, _COROUTINE_TYPES)
|
||||
if type(obj) in _iscoroutine_typecache:
|
||||
return True
|
||||
|
||||
if isinstance(obj, _COROUTINE_TYPES):
|
||||
# Just in case we don't want to cache more than 100
|
||||
# positive types. That shouldn't ever happen, unless
|
||||
# someone stressing the system on purpose.
|
||||
if len(_iscoroutine_typecache) < 100:
|
||||
_iscoroutine_typecache.add(type(obj))
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def _format_coroutine(coro):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue