1- __all__ = 'coroutine' , ' iscoroutinefunction' , 'iscoroutine'
1+ __all__ = 'iscoroutinefunction' , 'iscoroutine'
22
33import collections .abc
4- import functools
54import inspect
65import os
76import sys
87import traceback
98import types
10- import warnings
119
12- from . import base_futures
1310from . import constants
1411from . import format_helpers
1512from .log import logger
@@ -30,9 +27,6 @@ def _is_debug_mode():
3027 bool (os .environ .get ('PYTHONASYNCIODEBUG' )))
3128
3229
33- _DEBUG = _is_debug_mode ()
34-
35-
3630class CoroWrapper :
3731 # Wrapper for coroutine object in _DEBUG mode.
3832
@@ -102,61 +96,6 @@ def __del__(self):
10296 logger .error (msg )
10397
10498
105- def coroutine (func ):
106- """Decorator to mark coroutines.
107-
108- If the coroutine is not yielded from before it is destroyed,
109- an error message is logged.
110- """
111- warnings .warn ('"@coroutine" decorator is deprecated since Python 3.8, use "async def" instead' ,
112- DeprecationWarning ,
113- stacklevel = 2 )
114- if inspect .iscoroutinefunction (func ):
115- # In Python 3.5 that's all we need to do for coroutines
116- # defined with "async def".
117- return func
118-
119- if inspect .isgeneratorfunction (func ):
120- coro = func
121- else :
122- @functools .wraps (func )
123- def coro (* args , ** kw ):
124- res = func (* args , ** kw )
125- if (base_futures .isfuture (res ) or inspect .isgenerator (res ) or
126- isinstance (res , CoroWrapper )):
127- res = yield from res
128- else :
129- # If 'res' is an awaitable, run it.
130- try :
131- await_meth = res .__await__
132- except AttributeError :
133- pass
134- else :
135- if isinstance (res , collections .abc .Awaitable ):
136- res = yield from await_meth ()
137- return res
138-
139- coro = types .coroutine (coro )
140- if not _DEBUG :
141- wrapper = coro
142- else :
143- @functools .wraps (func )
144- def wrapper (* args , ** kwds ):
145- w = CoroWrapper (coro (* args , ** kwds ), func = func )
146- if w ._source_traceback :
147- del w ._source_traceback [- 1 ]
148- # Python < 3.5 does not implement __qualname__
149- # on generator objects, so we set it manually.
150- # We use getattr as some callables (such as
151- # functools.partial may lack __qualname__).
152- w .__name__ = getattr (func , '__name__' , None )
153- w .__qualname__ = getattr (func , '__qualname__' , None )
154- return w
155-
156- wrapper ._is_coroutine = _is_coroutine # For iscoroutinefunction().
157- return wrapper
158-
159-
16099# A marker for iscoroutinefunction.
161100_is_coroutine = object ()
162101
0 commit comments