| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  | __all__ = ['coroutine', | 
					
						
							|  |  |  |            'iscoroutinefunction', 'iscoroutine'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import functools | 
					
						
							|  |  |  | import inspect | 
					
						
							| 
									
										
										
										
											2014-06-30 14:39:11 +02:00
										 |  |  | import opcode | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  | import os | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | import traceback | 
					
						
							| 
									
										
										
										
											2014-06-30 14:39:11 +02:00
										 |  |  | import types | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-25 02:23:21 +02:00
										 |  |  | from . import compat | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  | from . import events | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  | from . import base_futures | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  | from .log import logger | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-30 14:39:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Opcode of "yield from" instruction | 
					
						
							|  |  |  | _YIELD_FROM = opcode.opmap['YIELD_FROM'] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  | # If you set _DEBUG to true, @coroutine will wrap the resulting | 
					
						
							|  |  |  | # generator objects in a CoroWrapper instance (defined below).  That | 
					
						
							|  |  |  | # instance will log a message when the generator is never iterated | 
					
						
							|  |  |  | # over, which may happen when you forget to use "yield from" with a | 
					
						
							|  |  |  | # coroutine call.  Note that the value of the _DEBUG flag is taken | 
					
						
							|  |  |  | # when the decorator is used, so to be of any use it must be set | 
					
						
							|  |  |  | # before you define your coroutines.  A downside of using this feature | 
					
						
							|  |  |  | # is that tracebacks show entries for the CoroWrapper.__next__ method | 
					
						
							|  |  |  | # when _DEBUG is true. | 
					
						
							| 
									
										
										
										
											2016-01-11 08:42:49 +02:00
										 |  |  | _DEBUG = (not sys.flags.ignore_environment and | 
					
						
							|  |  |  |           bool(os.environ.get('PYTHONASYNCIODEBUG'))) | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-30 14:39:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-11 22:27:25 -04:00
										 |  |  | try: | 
					
						
							| 
									
										
										
										
											2015-06-24 09:41:35 -04:00
										 |  |  |     _types_coroutine = types.coroutine | 
					
						
							| 
									
										
										
										
											2016-11-15 15:20:34 -05:00
										 |  |  |     _types_CoroutineType = types.CoroutineType | 
					
						
							| 
									
										
										
										
											2015-05-11 22:27:25 -04:00
										 |  |  | except AttributeError: | 
					
						
							| 
									
										
										
										
											2016-11-15 15:20:34 -05:00
										 |  |  |     # Python 3.4 | 
					
						
							| 
									
										
										
										
											2015-06-24 09:41:35 -04:00
										 |  |  |     _types_coroutine = None | 
					
						
							| 
									
										
										
										
											2016-11-15 15:20:34 -05:00
										 |  |  |     _types_CoroutineType = None | 
					
						
							| 
									
										
										
										
											2015-05-11 22:27:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | try: | 
					
						
							| 
									
										
										
										
											2015-06-24 09:41:35 -04:00
										 |  |  |     _inspect_iscoroutinefunction = inspect.iscoroutinefunction | 
					
						
							| 
									
										
										
										
											2015-05-11 22:27:25 -04:00
										 |  |  | except AttributeError: | 
					
						
							| 
									
										
										
										
											2016-11-15 15:20:34 -05:00
										 |  |  |     # Python 3.4 | 
					
						
							| 
									
										
										
										
											2015-06-24 09:41:35 -04:00
										 |  |  |     _inspect_iscoroutinefunction = lambda func: False | 
					
						
							| 
									
										
										
										
											2015-05-11 22:27:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | try: | 
					
						
							| 
									
										
										
										
											2015-06-24 09:41:35 -04:00
										 |  |  |     from collections.abc import Coroutine as _CoroutineABC, \ | 
					
						
							|  |  |  |                                 Awaitable as _AwaitableABC | 
					
						
							| 
									
										
										
										
											2015-05-13 15:21:41 -04:00
										 |  |  | except ImportError: | 
					
						
							| 
									
										
										
										
											2015-06-24 09:41:35 -04:00
										 |  |  |     _CoroutineABC = _AwaitableABC = None | 
					
						
							| 
									
										
										
										
											2015-05-13 15:21:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-11 22:27:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-30 14:39:11 +02:00
										 |  |  | # Check for CPython issue #21209 | 
					
						
							|  |  |  | def has_yield_from_bug(): | 
					
						
							|  |  |  |     class MyGen: | 
					
						
							|  |  |  |         def __init__(self): | 
					
						
							|  |  |  |             self.send_args = None | 
					
						
							|  |  |  |         def __iter__(self): | 
					
						
							|  |  |  |             return self | 
					
						
							|  |  |  |         def __next__(self): | 
					
						
							|  |  |  |             return 42 | 
					
						
							|  |  |  |         def send(self, *what): | 
					
						
							|  |  |  |             self.send_args = what | 
					
						
							|  |  |  |             return None | 
					
						
							|  |  |  |     def yield_from_gen(gen): | 
					
						
							|  |  |  |         yield from gen | 
					
						
							|  |  |  |     value = (1, 2, 3) | 
					
						
							|  |  |  |     gen = MyGen() | 
					
						
							|  |  |  |     coro = yield_from_gen(gen) | 
					
						
							|  |  |  |     next(coro) | 
					
						
							|  |  |  |     coro.send(value) | 
					
						
							|  |  |  |     return gen.send_args != (value,) | 
					
						
							|  |  |  | _YIELD_FROM_BUG = has_yield_from_bug() | 
					
						
							|  |  |  | del has_yield_from_bug | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-11 22:27:25 -04:00
										 |  |  | def debug_wrapper(gen): | 
					
						
							|  |  |  |     # This function is called from 'sys.set_coroutine_wrapper'. | 
					
						
							|  |  |  |     # We only wrap here coroutines defined via 'async def' syntax. | 
					
						
							|  |  |  |     # Generator-based coroutines are wrapped in @coroutine | 
					
						
							|  |  |  |     # decorator. | 
					
						
							| 
									
										
										
										
											2015-06-24 09:41:35 -04:00
										 |  |  |     return CoroWrapper(gen, None) | 
					
						
							| 
									
										
										
										
											2015-05-11 22:27:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  | class CoroWrapper: | 
					
						
							| 
									
										
										
										
											2014-07-11 00:21:27 +02:00
										 |  |  |     # Wrapper for coroutine object in _DEBUG mode. | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-11 22:27:25 -04:00
										 |  |  |     def __init__(self, gen, func=None): | 
					
						
							|  |  |  |         assert inspect.isgenerator(gen) or inspect.iscoroutine(gen), gen | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  |         self.gen = gen | 
					
						
							| 
									
										
										
										
											2016-01-11 08:42:49 +02:00
										 |  |  |         self.func = func  # Used to unwrap @coroutine decorator | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  |         self._source_traceback = traceback.extract_stack(sys._getframe(1)) | 
					
						
							| 
									
										
										
										
											2015-05-11 22:27:25 -04:00
										 |  |  |         self.__name__ = getattr(gen, '__name__', None) | 
					
						
							|  |  |  |         self.__qualname__ = getattr(gen, '__qualname__', None) | 
					
						
							| 
									
										
										
										
											2014-07-05 15:29:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __repr__(self): | 
					
						
							| 
									
										
										
										
											2014-07-11 00:21:27 +02:00
										 |  |  |         coro_repr = _format_coroutine(self) | 
					
						
							|  |  |  |         if self._source_traceback: | 
					
						
							|  |  |  |             frame = self._source_traceback[-1] | 
					
						
							|  |  |  |             coro_repr += ', created at %s:%s' % (frame[0], frame[1]) | 
					
						
							|  |  |  |         return '<%s %s>' % (self.__class__.__name__, coro_repr) | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __iter__(self): | 
					
						
							|  |  |  |         return self | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __next__(self): | 
					
						
							| 
									
										
										
										
											2015-05-31 21:37:09 -04:00
										 |  |  |         return self.gen.send(None) | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-30 14:39:11 +02:00
										 |  |  |     if _YIELD_FROM_BUG: | 
					
						
							|  |  |  |         # For for CPython issue #21209: using "yield from" and a custom | 
					
						
							|  |  |  |         # generator, generator.send(tuple) unpacks the tuple instead of passing | 
					
						
							|  |  |  |         # the tuple unchanged. Check if the caller is a generator using "yield | 
					
						
							|  |  |  |         # from" to decide if the parameter should be unpacked or not. | 
					
						
							|  |  |  |         def send(self, *value): | 
					
						
							|  |  |  |             frame = sys._getframe() | 
					
						
							|  |  |  |             caller = frame.f_back | 
					
						
							|  |  |  |             assert caller.f_lasti >= 0 | 
					
						
							|  |  |  |             if caller.f_code.co_code[caller.f_lasti] != _YIELD_FROM: | 
					
						
							|  |  |  |                 value = value[0] | 
					
						
							|  |  |  |             return self.gen.send(value) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         def send(self, value): | 
					
						
							|  |  |  |             return self.gen.send(value) | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 08:17:15 -07:00
										 |  |  |     def throw(self, type, value=None, traceback=None): | 
					
						
							|  |  |  |         return self.gen.throw(type, value, traceback) | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def close(self): | 
					
						
							|  |  |  |         return self.gen.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def gi_frame(self): | 
					
						
							|  |  |  |         return self.gen.gi_frame | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def gi_running(self): | 
					
						
							|  |  |  |         return self.gen.gi_running | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def gi_code(self): | 
					
						
							|  |  |  |         return self.gen.gi_code | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-25 02:23:21 +02:00
										 |  |  |     if compat.PY35: | 
					
						
							| 
									
										
										
										
											2015-06-24 10:30:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-18 12:39:45 -05:00
										 |  |  |         def __await__(self): | 
					
						
							|  |  |  |             cr_await = getattr(self.gen, 'cr_await', None) | 
					
						
							|  |  |  |             if cr_await is not None: | 
					
						
							|  |  |  |                 raise RuntimeError( | 
					
						
							|  |  |  |                     "Cannot await on coroutine {!r} while it's " | 
					
						
							|  |  |  |                     "awaiting for {!r}".format(self.gen, cr_await)) | 
					
						
							|  |  |  |             return self | 
					
						
							| 
									
										
										
										
											2015-06-24 10:30:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-03 00:41:16 -04:00
										 |  |  |         @property | 
					
						
							|  |  |  |         def gi_yieldfrom(self): | 
					
						
							|  |  |  |             return self.gen.gi_yieldfrom | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @property | 
					
						
							|  |  |  |         def cr_await(self): | 
					
						
							|  |  |  |             return self.gen.cr_await | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-24 10:30:14 -04:00
										 |  |  |         @property | 
					
						
							|  |  |  |         def cr_running(self): | 
					
						
							|  |  |  |             return self.gen.cr_running | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @property | 
					
						
							|  |  |  |         def cr_code(self): | 
					
						
							|  |  |  |             return self.gen.cr_code | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @property | 
					
						
							|  |  |  |         def cr_frame(self): | 
					
						
							|  |  |  |             return self.gen.cr_frame | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  |     def __del__(self): | 
					
						
							|  |  |  |         # Be careful accessing self.gen.frame -- self.gen might not exist. | 
					
						
							|  |  |  |         gen = getattr(self, 'gen', None) | 
					
						
							|  |  |  |         frame = getattr(gen, 'gi_frame', None) | 
					
						
							| 
									
										
										
										
											2015-06-24 10:30:14 -04:00
										 |  |  |         if frame is None: | 
					
						
							|  |  |  |             frame = getattr(gen, 'cr_frame', None) | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  |         if frame is not None and frame.f_lasti == -1: | 
					
						
							| 
									
										
										
										
											2014-07-11 01:04:16 +02:00
										 |  |  |             msg = '%r was never yielded from' % self | 
					
						
							| 
									
										
										
										
											2014-07-03 00:59:00 +02:00
										 |  |  |             tb = getattr(self, '_source_traceback', ()) | 
					
						
							|  |  |  |             if tb: | 
					
						
							|  |  |  |                 tb = ''.join(traceback.format_list(tb)) | 
					
						
							|  |  |  |                 msg += ('\nCoroutine object created at ' | 
					
						
							|  |  |  |                         '(most recent call last):\n') | 
					
						
							|  |  |  |                 msg += tb.rstrip() | 
					
						
							|  |  |  |             logger.error(msg) | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def coroutine(func): | 
					
						
							|  |  |  |     """Decorator to mark coroutines.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     If the coroutine is not yielded from before it is destroyed, | 
					
						
							|  |  |  |     an error message is logged. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2015-06-24 09:41:35 -04:00
										 |  |  |     if _inspect_iscoroutinefunction(func): | 
					
						
							| 
									
										
										
										
											2015-05-11 22:27:25 -04:00
										 |  |  |         # In Python 3.5 that's all we need to do for coroutines | 
					
						
							|  |  |  |         # defiend with "async def". | 
					
						
							|  |  |  |         # Wrapping in CoroWrapper will happen via | 
					
						
							|  |  |  |         # 'sys.set_coroutine_wrapper' function. | 
					
						
							|  |  |  |         return func | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  |     if inspect.isgeneratorfunction(func): | 
					
						
							|  |  |  |         coro = func | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         @functools.wraps(func) | 
					
						
							|  |  |  |         def coro(*args, **kw): | 
					
						
							|  |  |  |             res = func(*args, **kw) | 
					
						
							| 
									
										
										
										
											2016-10-28 12:52:37 -04:00
										 |  |  |             if (base_futures.isfuture(res) or inspect.isgenerator(res) or | 
					
						
							| 
									
										
										
										
											2016-09-09 14:26:31 -07:00
										 |  |  |                 isinstance(res, CoroWrapper)): | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  |                 res = yield from res | 
					
						
							| 
									
										
										
										
											2015-06-24 09:41:35 -04:00
										 |  |  |             elif _AwaitableABC is not None: | 
					
						
							| 
									
										
										
										
											2015-05-30 21:02:12 -04:00
										 |  |  |                 # If 'func' returns an Awaitable (new in 3.5) we | 
					
						
							|  |  |  |                 # want to run it. | 
					
						
							|  |  |  |                 try: | 
					
						
							|  |  |  |                     await_meth = res.__await__ | 
					
						
							|  |  |  |                 except AttributeError: | 
					
						
							|  |  |  |                     pass | 
					
						
							|  |  |  |                 else: | 
					
						
							| 
									
										
										
										
											2015-06-24 09:41:35 -04:00
										 |  |  |                     if isinstance(res, _AwaitableABC): | 
					
						
							| 
									
										
										
										
											2015-05-30 21:02:12 -04:00
										 |  |  |                         res = yield from await_meth() | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  |             return res | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if not _DEBUG: | 
					
						
							| 
									
										
										
										
											2015-06-24 09:41:35 -04:00
										 |  |  |         if _types_coroutine is None: | 
					
						
							| 
									
										
										
										
											2015-05-11 22:27:25 -04:00
										 |  |  |             wrapper = coro | 
					
						
							| 
									
										
										
										
											2015-06-24 09:41:35 -04:00
										 |  |  |         else: | 
					
						
							|  |  |  |             wrapper = _types_coroutine(coro) | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  |     else: | 
					
						
							|  |  |  |         @functools.wraps(func) | 
					
						
							|  |  |  |         def wrapper(*args, **kwds): | 
					
						
							| 
									
										
										
										
											2015-05-11 22:27:25 -04:00
										 |  |  |             w = CoroWrapper(coro(*args, **kwds), func=func) | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  |             if w._source_traceback: | 
					
						
							|  |  |  |                 del w._source_traceback[-1] | 
					
						
							| 
									
										
										
										
											2015-05-11 22:27:25 -04:00
										 |  |  |             # Python < 3.5 does not implement __qualname__ | 
					
						
							|  |  |  |             # on generator objects, so we set it manually. | 
					
						
							|  |  |  |             # We use getattr as some callables (such as | 
					
						
							|  |  |  |             # functools.partial may lack __qualname__). | 
					
						
							|  |  |  |             w.__name__ = getattr(func, '__name__', None) | 
					
						
							|  |  |  |             w.__qualname__ = getattr(func, '__qualname__', None) | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  |             return w | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-15 15:20:34 -05:00
										 |  |  |     wrapper._is_coroutine = _is_coroutine  # For iscoroutinefunction(). | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  |     return wrapper | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-15 15:20:34 -05:00
										 |  |  | # A marker for iscoroutinefunction. | 
					
						
							|  |  |  | _is_coroutine = object() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  | def iscoroutinefunction(func): | 
					
						
							|  |  |  |     """Return True if func is a decorated coroutine function.""" | 
					
						
							| 
									
										
										
										
											2016-11-15 15:20:34 -05:00
										 |  |  |     return (getattr(func, '_is_coroutine', None) is _is_coroutine or | 
					
						
							| 
									
										
										
										
											2015-06-24 09:41:35 -04:00
										 |  |  |             _inspect_iscoroutinefunction(func)) | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-07 17:26:54 +02:00
										 |  |  | _COROUTINE_TYPES = (types.GeneratorType, CoroWrapper) | 
					
						
							| 
									
										
										
										
											2015-06-24 09:41:35 -04:00
										 |  |  | if _CoroutineABC is not None: | 
					
						
							|  |  |  |     _COROUTINE_TYPES += (_CoroutineABC,) | 
					
						
							| 
									
										
										
										
											2016-11-15 15:20:34 -05:00
										 |  |  | if _types_CoroutineType is not None: | 
					
						
							|  |  |  |     # Prioritize native coroutine check to speed-up | 
					
						
							|  |  |  |     # asyncio.iscoroutine. | 
					
						
							|  |  |  |     _COROUTINE_TYPES = (_types_CoroutineType,) + _COROUTINE_TYPES | 
					
						
							| 
									
										
										
										
											2015-05-13 15:21:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-30 14:39:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  | def iscoroutine(obj): | 
					
						
							|  |  |  |     """Return True if obj is a coroutine object.""" | 
					
						
							| 
									
										
										
										
											2014-07-07 17:26:54 +02:00
										 |  |  |     return isinstance(obj, _COROUTINE_TYPES) | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _format_coroutine(coro): | 
					
						
							|  |  |  |     assert iscoroutine(coro) | 
					
						
							| 
									
										
										
										
											2015-05-02 18:38:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-05 19:32:49 -04:00
										 |  |  |     if not hasattr(coro, 'cr_code') and not hasattr(coro, 'gi_code'): | 
					
						
							| 
									
										
										
										
											2016-11-08 19:16:01 -05:00
										 |  |  |         # Most likely a built-in type or a Cython coroutine. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Built-in types might not have __qualname__ or __name__. | 
					
						
							|  |  |  |         coro_name = getattr( | 
					
						
							|  |  |  |             coro, '__qualname__', | 
					
						
							|  |  |  |             getattr(coro, '__name__', type(coro).__name__)) | 
					
						
							| 
									
										
										
										
											2016-10-05 19:32:49 -04:00
										 |  |  |         coro_name = '{}()'.format(coro_name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         running = False | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             running = coro.cr_running | 
					
						
							|  |  |  |         except AttributeError: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 running = coro.gi_running | 
					
						
							|  |  |  |             except AttributeError: | 
					
						
							|  |  |  |                 pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if running: | 
					
						
							|  |  |  |             return '{} running'.format(coro_name) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             return coro_name | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-24 10:30:14 -04:00
										 |  |  |     coro_name = None | 
					
						
							| 
									
										
										
										
											2015-05-02 18:38:24 -07:00
										 |  |  |     if isinstance(coro, CoroWrapper): | 
					
						
							|  |  |  |         func = coro.func | 
					
						
							| 
									
										
										
										
											2015-06-24 10:30:14 -04:00
										 |  |  |         coro_name = coro.__qualname__ | 
					
						
							| 
									
										
										
										
											2015-06-24 10:45:44 -04:00
										 |  |  |         if coro_name is not None: | 
					
						
							|  |  |  |             coro_name = '{}()'.format(coro_name) | 
					
						
							| 
									
										
										
										
											2015-05-02 18:38:24 -07:00
										 |  |  |     else: | 
					
						
							|  |  |  |         func = coro | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-24 10:30:14 -04:00
										 |  |  |     if coro_name is None: | 
					
						
							| 
									
										
										
										
											2016-09-15 15:58:15 -04:00
										 |  |  |         coro_name = events._format_callback(func, (), {}) | 
					
						
							| 
									
										
										
										
											2015-06-24 10:30:14 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         coro_code = coro.gi_code | 
					
						
							|  |  |  |     except AttributeError: | 
					
						
							|  |  |  |         coro_code = coro.cr_code | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         coro_frame = coro.gi_frame | 
					
						
							|  |  |  |     except AttributeError: | 
					
						
							|  |  |  |         coro_frame = coro.cr_frame | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     filename = coro_code.co_filename | 
					
						
							| 
									
										
										
										
											2016-01-11 08:42:49 +02:00
										 |  |  |     lineno = 0 | 
					
						
							|  |  |  |     if (isinstance(coro, CoroWrapper) and | 
					
						
							|  |  |  |             not inspect.isgeneratorfunction(coro.func) and | 
					
						
							|  |  |  |             coro.func is not None): | 
					
						
							|  |  |  |         source = events._get_function_source(coro.func) | 
					
						
							|  |  |  |         if source is not None: | 
					
						
							|  |  |  |             filename, lineno = source | 
					
						
							| 
									
										
										
										
											2015-06-24 10:30:14 -04:00
										 |  |  |         if coro_frame is None: | 
					
						
							| 
									
										
										
										
											2015-05-02 18:38:24 -07:00
										 |  |  |             coro_repr = ('%s done, defined at %s:%s' | 
					
						
							| 
									
										
										
										
											2015-01-09 00:09:10 +01:00
										 |  |  |                          % (coro_name, filename, lineno)) | 
					
						
							| 
									
										
										
										
											2014-07-11 00:21:27 +02:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2015-05-02 18:38:24 -07:00
										 |  |  |             coro_repr = ('%s running, defined at %s:%s' | 
					
						
							| 
									
										
										
										
											2015-01-09 00:09:10 +01:00
										 |  |  |                          % (coro_name, filename, lineno)) | 
					
						
							| 
									
										
										
										
											2015-06-24 10:30:14 -04:00
										 |  |  |     elif coro_frame is not None: | 
					
						
							|  |  |  |         lineno = coro_frame.f_lineno | 
					
						
							| 
									
										
										
										
											2015-05-02 18:38:24 -07:00
										 |  |  |         coro_repr = ('%s running at %s:%s' | 
					
						
							| 
									
										
										
										
											2015-01-09 00:09:10 +01:00
										 |  |  |                      % (coro_name, filename, lineno)) | 
					
						
							| 
									
										
										
										
											2014-06-29 00:46:45 +02:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2015-06-24 10:30:14 -04:00
										 |  |  |         lineno = coro_code.co_firstlineno | 
					
						
							| 
									
										
										
										
											2015-05-02 18:38:24 -07:00
										 |  |  |         coro_repr = ('%s done, defined at %s:%s' | 
					
						
							| 
									
										
										
										
											2015-01-09 00:09:10 +01:00
										 |  |  |                      % (coro_name, filename, lineno)) | 
					
						
							| 
									
										
										
										
											2014-07-11 00:21:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return coro_repr |