Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 1 | __all__ = ['coroutine', |
| 2 | 'iscoroutinefunction', 'iscoroutine'] |
| 3 | |
| 4 | import functools |
| 5 | import inspect |
Victor Stinner | b75380f | 2014-06-30 14:39:11 +0200 | [diff] [blame] | 6 | import opcode |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 7 | import os |
| 8 | import sys |
| 9 | import traceback |
Victor Stinner | b75380f | 2014-06-30 14:39:11 +0200 | [diff] [blame] | 10 | import types |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 11 | |
Victor Stinner | 71080fc | 2015-07-25 02:23:21 +0200 | [diff] [blame] | 12 | from . import compat |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 13 | from . import events |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 14 | from . import base_futures |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 15 | from .log import logger |
| 16 | |
Victor Stinner | b75380f | 2014-06-30 14:39:11 +0200 | [diff] [blame] | 17 | |
| 18 | # Opcode of "yield from" instruction |
| 19 | _YIELD_FROM = opcode.opmap['YIELD_FROM'] |
| 20 | |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 21 | # If you set _DEBUG to true, @coroutine will wrap the resulting |
| 22 | # generator objects in a CoroWrapper instance (defined below). That |
| 23 | # instance will log a message when the generator is never iterated |
| 24 | # over, which may happen when you forget to use "yield from" with a |
| 25 | # coroutine call. Note that the value of the _DEBUG flag is taken |
| 26 | # when the decorator is used, so to be of any use it must be set |
| 27 | # before you define your coroutines. A downside of using this feature |
| 28 | # is that tracebacks show entries for the CoroWrapper.__next__ method |
| 29 | # when _DEBUG is true. |
Andrew Svetlov | c07b16b | 2016-01-11 08:42:49 +0200 | [diff] [blame] | 30 | _DEBUG = (not sys.flags.ignore_environment and |
| 31 | bool(os.environ.get('PYTHONASYNCIODEBUG'))) |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 32 | |
Victor Stinner | b75380f | 2014-06-30 14:39:11 +0200 | [diff] [blame] | 33 | |
Yury Selivanov | 1af2bf7 | 2015-05-11 22:27:25 -0400 | [diff] [blame] | 34 | try: |
Yury Selivanov | dfbd27f | 2015-06-24 09:41:35 -0400 | [diff] [blame] | 35 | _types_coroutine = types.coroutine |
Yury Selivanov | 0ed20cd | 2016-11-15 15:20:34 -0500 | [diff] [blame] | 36 | _types_CoroutineType = types.CoroutineType |
Yury Selivanov | 1af2bf7 | 2015-05-11 22:27:25 -0400 | [diff] [blame] | 37 | except AttributeError: |
Yury Selivanov | 0ed20cd | 2016-11-15 15:20:34 -0500 | [diff] [blame] | 38 | # Python 3.4 |
Yury Selivanov | dfbd27f | 2015-06-24 09:41:35 -0400 | [diff] [blame] | 39 | _types_coroutine = None |
Yury Selivanov | 0ed20cd | 2016-11-15 15:20:34 -0500 | [diff] [blame] | 40 | _types_CoroutineType = None |
Yury Selivanov | 1af2bf7 | 2015-05-11 22:27:25 -0400 | [diff] [blame] | 41 | |
| 42 | try: |
Yury Selivanov | dfbd27f | 2015-06-24 09:41:35 -0400 | [diff] [blame] | 43 | _inspect_iscoroutinefunction = inspect.iscoroutinefunction |
Yury Selivanov | 1af2bf7 | 2015-05-11 22:27:25 -0400 | [diff] [blame] | 44 | except AttributeError: |
Yury Selivanov | 0ed20cd | 2016-11-15 15:20:34 -0500 | [diff] [blame] | 45 | # Python 3.4 |
Yury Selivanov | dfbd27f | 2015-06-24 09:41:35 -0400 | [diff] [blame] | 46 | _inspect_iscoroutinefunction = lambda func: False |
Yury Selivanov | 1af2bf7 | 2015-05-11 22:27:25 -0400 | [diff] [blame] | 47 | |
| 48 | try: |
Yury Selivanov | dfbd27f | 2015-06-24 09:41:35 -0400 | [diff] [blame] | 49 | from collections.abc import Coroutine as _CoroutineABC, \ |
| 50 | Awaitable as _AwaitableABC |
Yury Selivanov | c58cca5 | 2015-05-13 15:21:41 -0400 | [diff] [blame] | 51 | except ImportError: |
Yury Selivanov | dfbd27f | 2015-06-24 09:41:35 -0400 | [diff] [blame] | 52 | _CoroutineABC = _AwaitableABC = None |
Yury Selivanov | c58cca5 | 2015-05-13 15:21:41 -0400 | [diff] [blame] | 53 | |
Yury Selivanov | 1af2bf7 | 2015-05-11 22:27:25 -0400 | [diff] [blame] | 54 | |
Victor Stinner | b75380f | 2014-06-30 14:39:11 +0200 | [diff] [blame] | 55 | # Check for CPython issue #21209 |
| 56 | def has_yield_from_bug(): |
| 57 | class MyGen: |
| 58 | def __init__(self): |
| 59 | self.send_args = None |
| 60 | def __iter__(self): |
| 61 | return self |
| 62 | def __next__(self): |
| 63 | return 42 |
| 64 | def send(self, *what): |
| 65 | self.send_args = what |
| 66 | return None |
| 67 | def yield_from_gen(gen): |
| 68 | yield from gen |
| 69 | value = (1, 2, 3) |
| 70 | gen = MyGen() |
| 71 | coro = yield_from_gen(gen) |
| 72 | next(coro) |
| 73 | coro.send(value) |
| 74 | return gen.send_args != (value,) |
| 75 | _YIELD_FROM_BUG = has_yield_from_bug() |
| 76 | del has_yield_from_bug |
| 77 | |
| 78 | |
Yury Selivanov | 1af2bf7 | 2015-05-11 22:27:25 -0400 | [diff] [blame] | 79 | def debug_wrapper(gen): |
| 80 | # This function is called from 'sys.set_coroutine_wrapper'. |
| 81 | # We only wrap here coroutines defined via 'async def' syntax. |
| 82 | # Generator-based coroutines are wrapped in @coroutine |
| 83 | # decorator. |
Yury Selivanov | dfbd27f | 2015-06-24 09:41:35 -0400 | [diff] [blame] | 84 | return CoroWrapper(gen, None) |
Yury Selivanov | 1af2bf7 | 2015-05-11 22:27:25 -0400 | [diff] [blame] | 85 | |
| 86 | |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 87 | class CoroWrapper: |
Victor Stinner | c39ba7d | 2014-07-11 00:21:27 +0200 | [diff] [blame] | 88 | # Wrapper for coroutine object in _DEBUG mode. |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 89 | |
Yury Selivanov | 1af2bf7 | 2015-05-11 22:27:25 -0400 | [diff] [blame] | 90 | def __init__(self, gen, func=None): |
| 91 | assert inspect.isgenerator(gen) or inspect.iscoroutine(gen), gen |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 92 | self.gen = gen |
Andrew Svetlov | c07b16b | 2016-01-11 08:42:49 +0200 | [diff] [blame] | 93 | self.func = func # Used to unwrap @coroutine decorator |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 94 | self._source_traceback = traceback.extract_stack(sys._getframe(1)) |
Yury Selivanov | 1af2bf7 | 2015-05-11 22:27:25 -0400 | [diff] [blame] | 95 | self.__name__ = getattr(gen, '__name__', None) |
| 96 | self.__qualname__ = getattr(gen, '__qualname__', None) |
Victor Stinner | a9acbe8 | 2014-07-05 15:29:41 +0200 | [diff] [blame] | 97 | |
| 98 | def __repr__(self): |
Victor Stinner | c39ba7d | 2014-07-11 00:21:27 +0200 | [diff] [blame] | 99 | coro_repr = _format_coroutine(self) |
| 100 | if self._source_traceback: |
| 101 | frame = self._source_traceback[-1] |
| 102 | coro_repr += ', created at %s:%s' % (frame[0], frame[1]) |
| 103 | return '<%s %s>' % (self.__class__.__name__, coro_repr) |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 104 | |
| 105 | def __iter__(self): |
| 106 | return self |
| 107 | |
| 108 | def __next__(self): |
Yury Selivanov | 70adad2 | 2015-05-31 21:37:09 -0400 | [diff] [blame] | 109 | return self.gen.send(None) |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 110 | |
Victor Stinner | b75380f | 2014-06-30 14:39:11 +0200 | [diff] [blame] | 111 | if _YIELD_FROM_BUG: |
| 112 | # For for CPython issue #21209: using "yield from" and a custom |
| 113 | # generator, generator.send(tuple) unpacks the tuple instead of passing |
| 114 | # the tuple unchanged. Check if the caller is a generator using "yield |
| 115 | # from" to decide if the parameter should be unpacked or not. |
| 116 | def send(self, *value): |
| 117 | frame = sys._getframe() |
| 118 | caller = frame.f_back |
| 119 | assert caller.f_lasti >= 0 |
| 120 | if caller.f_code.co_code[caller.f_lasti] != _YIELD_FROM: |
| 121 | value = value[0] |
| 122 | return self.gen.send(value) |
| 123 | else: |
| 124 | def send(self, value): |
| 125 | return self.gen.send(value) |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 126 | |
Guido van Rossum | e3c65a7 | 2016-09-30 08:17:15 -0700 | [diff] [blame] | 127 | def throw(self, type, value=None, traceback=None): |
| 128 | return self.gen.throw(type, value, traceback) |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 129 | |
| 130 | def close(self): |
| 131 | return self.gen.close() |
| 132 | |
| 133 | @property |
| 134 | def gi_frame(self): |
| 135 | return self.gen.gi_frame |
| 136 | |
| 137 | @property |
| 138 | def gi_running(self): |
| 139 | return self.gen.gi_running |
| 140 | |
| 141 | @property |
| 142 | def gi_code(self): |
| 143 | return self.gen.gi_code |
| 144 | |
Victor Stinner | 71080fc | 2015-07-25 02:23:21 +0200 | [diff] [blame] | 145 | if compat.PY35: |
Yury Selivanov | 29a602a | 2015-06-24 10:30:14 -0400 | [diff] [blame] | 146 | |
Yury Selivanov | b3dd6d7 | 2015-11-18 12:39:45 -0500 | [diff] [blame] | 147 | def __await__(self): |
| 148 | cr_await = getattr(self.gen, 'cr_await', None) |
| 149 | if cr_await is not None: |
| 150 | raise RuntimeError( |
| 151 | "Cannot await on coroutine {!r} while it's " |
| 152 | "awaiting for {!r}".format(self.gen, cr_await)) |
| 153 | return self |
Yury Selivanov | 29a602a | 2015-06-24 10:30:14 -0400 | [diff] [blame] | 154 | |
| 155 | @property |
Yury Selivanov | 09e6058 | 2015-07-03 00:41:16 -0400 | [diff] [blame] | 156 | def gi_yieldfrom(self): |
| 157 | return self.gen.gi_yieldfrom |
| 158 | |
| 159 | @property |
| 160 | def cr_await(self): |
| 161 | return self.gen.cr_await |
| 162 | |
| 163 | @property |
Yury Selivanov | 29a602a | 2015-06-24 10:30:14 -0400 | [diff] [blame] | 164 | def cr_running(self): |
| 165 | return self.gen.cr_running |
| 166 | |
| 167 | @property |
| 168 | def cr_code(self): |
| 169 | return self.gen.cr_code |
| 170 | |
| 171 | @property |
| 172 | def cr_frame(self): |
| 173 | return self.gen.cr_frame |
| 174 | |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 175 | def __del__(self): |
| 176 | # Be careful accessing self.gen.frame -- self.gen might not exist. |
| 177 | gen = getattr(self, 'gen', None) |
| 178 | frame = getattr(gen, 'gi_frame', None) |
Yury Selivanov | 29a602a | 2015-06-24 10:30:14 -0400 | [diff] [blame] | 179 | if frame is None: |
| 180 | frame = getattr(gen, 'cr_frame', None) |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 181 | if frame is not None and frame.f_lasti == -1: |
Victor Stinner | 737c34f | 2014-07-11 01:04:16 +0200 | [diff] [blame] | 182 | msg = '%r was never yielded from' % self |
Victor Stinner | 2dba23a | 2014-07-03 00:59:00 +0200 | [diff] [blame] | 183 | tb = getattr(self, '_source_traceback', ()) |
| 184 | if tb: |
| 185 | tb = ''.join(traceback.format_list(tb)) |
| 186 | msg += ('\nCoroutine object created at ' |
| 187 | '(most recent call last):\n') |
| 188 | msg += tb.rstrip() |
| 189 | logger.error(msg) |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 190 | |
| 191 | |
| 192 | def coroutine(func): |
| 193 | """Decorator to mark coroutines. |
| 194 | |
| 195 | If the coroutine is not yielded from before it is destroyed, |
| 196 | an error message is logged. |
| 197 | """ |
Yury Selivanov | dfbd27f | 2015-06-24 09:41:35 -0400 | [diff] [blame] | 198 | if _inspect_iscoroutinefunction(func): |
Yury Selivanov | 1af2bf7 | 2015-05-11 22:27:25 -0400 | [diff] [blame] | 199 | # In Python 3.5 that's all we need to do for coroutines |
| 200 | # defiend with "async def". |
| 201 | # Wrapping in CoroWrapper will happen via |
| 202 | # 'sys.set_coroutine_wrapper' function. |
| 203 | return func |
| 204 | |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 205 | if inspect.isgeneratorfunction(func): |
| 206 | coro = func |
| 207 | else: |
| 208 | @functools.wraps(func) |
| 209 | def coro(*args, **kw): |
| 210 | res = func(*args, **kw) |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 211 | if (base_futures.isfuture(res) or inspect.isgenerator(res) or |
Guido van Rossum | 7b3b3dc | 2016-09-09 14:26:31 -0700 | [diff] [blame] | 212 | isinstance(res, CoroWrapper)): |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 213 | res = yield from res |
Yury Selivanov | dfbd27f | 2015-06-24 09:41:35 -0400 | [diff] [blame] | 214 | elif _AwaitableABC is not None: |
Yury Selivanov | a316085 | 2015-05-30 21:02:12 -0400 | [diff] [blame] | 215 | # If 'func' returns an Awaitable (new in 3.5) we |
| 216 | # want to run it. |
| 217 | try: |
| 218 | await_meth = res.__await__ |
| 219 | except AttributeError: |
| 220 | pass |
| 221 | else: |
Yury Selivanov | dfbd27f | 2015-06-24 09:41:35 -0400 | [diff] [blame] | 222 | if isinstance(res, _AwaitableABC): |
Yury Selivanov | a316085 | 2015-05-30 21:02:12 -0400 | [diff] [blame] | 223 | res = yield from await_meth() |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 224 | return res |
| 225 | |
| 226 | if not _DEBUG: |
Yury Selivanov | dfbd27f | 2015-06-24 09:41:35 -0400 | [diff] [blame] | 227 | if _types_coroutine is None: |
Yury Selivanov | 1af2bf7 | 2015-05-11 22:27:25 -0400 | [diff] [blame] | 228 | wrapper = coro |
Yury Selivanov | dfbd27f | 2015-06-24 09:41:35 -0400 | [diff] [blame] | 229 | else: |
| 230 | wrapper = _types_coroutine(coro) |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 231 | else: |
| 232 | @functools.wraps(func) |
| 233 | def wrapper(*args, **kwds): |
Yury Selivanov | 1af2bf7 | 2015-05-11 22:27:25 -0400 | [diff] [blame] | 234 | w = CoroWrapper(coro(*args, **kwds), func=func) |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 235 | if w._source_traceback: |
| 236 | del w._source_traceback[-1] |
Yury Selivanov | 1af2bf7 | 2015-05-11 22:27:25 -0400 | [diff] [blame] | 237 | # Python < 3.5 does not implement __qualname__ |
| 238 | # on generator objects, so we set it manually. |
| 239 | # We use getattr as some callables (such as |
| 240 | # functools.partial may lack __qualname__). |
| 241 | w.__name__ = getattr(func, '__name__', None) |
| 242 | w.__qualname__ = getattr(func, '__qualname__', None) |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 243 | return w |
| 244 | |
Yury Selivanov | 0ed20cd | 2016-11-15 15:20:34 -0500 | [diff] [blame] | 245 | wrapper._is_coroutine = _is_coroutine # For iscoroutinefunction(). |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 246 | return wrapper |
| 247 | |
| 248 | |
Yury Selivanov | 0ed20cd | 2016-11-15 15:20:34 -0500 | [diff] [blame] | 249 | # A marker for iscoroutinefunction. |
| 250 | _is_coroutine = object() |
| 251 | |
| 252 | |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 253 | def iscoroutinefunction(func): |
| 254 | """Return True if func is a decorated coroutine function.""" |
Yury Selivanov | 0ed20cd | 2016-11-15 15:20:34 -0500 | [diff] [blame] | 255 | return (getattr(func, '_is_coroutine', None) is _is_coroutine or |
Yury Selivanov | dfbd27f | 2015-06-24 09:41:35 -0400 | [diff] [blame] | 256 | _inspect_iscoroutinefunction(func)) |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 257 | |
| 258 | |
Victor Stinner | 1a870c9 | 2014-07-07 17:26:54 +0200 | [diff] [blame] | 259 | _COROUTINE_TYPES = (types.GeneratorType, CoroWrapper) |
Yury Selivanov | dfbd27f | 2015-06-24 09:41:35 -0400 | [diff] [blame] | 260 | if _CoroutineABC is not None: |
| 261 | _COROUTINE_TYPES += (_CoroutineABC,) |
Yury Selivanov | 0ed20cd | 2016-11-15 15:20:34 -0500 | [diff] [blame] | 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 |
Yury Selivanov | c58cca5 | 2015-05-13 15:21:41 -0400 | [diff] [blame] | 266 | |
Victor Stinner | b75380f | 2014-06-30 14:39:11 +0200 | [diff] [blame] | 267 | |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 268 | def iscoroutine(obj): |
| 269 | """Return True if obj is a coroutine object.""" |
Victor Stinner | 1a870c9 | 2014-07-07 17:26:54 +0200 | [diff] [blame] | 270 | return isinstance(obj, _COROUTINE_TYPES) |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 271 | |
| 272 | |
| 273 | def _format_coroutine(coro): |
| 274 | assert iscoroutine(coro) |
Guido van Rossum | 0a9933e | 2015-05-02 18:38:24 -0700 | [diff] [blame] | 275 | |
Yury Selivanov | 8dc3e43 | 2016-10-05 19:32:49 -0400 | [diff] [blame] | 276 | if not hasattr(coro, 'cr_code') and not hasattr(coro, 'gi_code'): |
Yury Selivanov | 6cc495e | 2016-11-08 19:16:01 -0500 | [diff] [blame] | 277 | # Most likely a built-in type or a Cython coroutine. |
| 278 | |
| 279 | # Built-in types might not have __qualname__ or __name__. |
| 280 | coro_name = getattr( |
| 281 | coro, '__qualname__', |
| 282 | getattr(coro, '__name__', type(coro).__name__)) |
Yury Selivanov | 8dc3e43 | 2016-10-05 19:32:49 -0400 | [diff] [blame] | 283 | coro_name = '{}()'.format(coro_name) |
| 284 | |
| 285 | running = False |
| 286 | try: |
| 287 | running = coro.cr_running |
| 288 | except AttributeError: |
| 289 | try: |
| 290 | running = coro.gi_running |
| 291 | except AttributeError: |
| 292 | pass |
| 293 | |
| 294 | if running: |
| 295 | return '{} running'.format(coro_name) |
| 296 | else: |
| 297 | return coro_name |
| 298 | |
Yury Selivanov | 29a602a | 2015-06-24 10:30:14 -0400 | [diff] [blame] | 299 | coro_name = None |
Guido van Rossum | 0a9933e | 2015-05-02 18:38:24 -0700 | [diff] [blame] | 300 | if isinstance(coro, CoroWrapper): |
| 301 | func = coro.func |
Yury Selivanov | 29a602a | 2015-06-24 10:30:14 -0400 | [diff] [blame] | 302 | coro_name = coro.__qualname__ |
Yury Selivanov | 339d5e7 | 2015-06-24 10:45:44 -0400 | [diff] [blame] | 303 | if coro_name is not None: |
| 304 | coro_name = '{}()'.format(coro_name) |
Guido van Rossum | 0a9933e | 2015-05-02 18:38:24 -0700 | [diff] [blame] | 305 | else: |
| 306 | func = coro |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 307 | |
Yury Selivanov | 29a602a | 2015-06-24 10:30:14 -0400 | [diff] [blame] | 308 | if coro_name is None: |
Yury Selivanov | 45dccda | 2016-09-15 15:58:15 -0400 | [diff] [blame] | 309 | coro_name = events._format_callback(func, (), {}) |
Yury Selivanov | 29a602a | 2015-06-24 10:30:14 -0400 | [diff] [blame] | 310 | |
| 311 | try: |
| 312 | coro_code = coro.gi_code |
| 313 | except AttributeError: |
| 314 | coro_code = coro.cr_code |
| 315 | |
| 316 | try: |
| 317 | coro_frame = coro.gi_frame |
| 318 | except AttributeError: |
| 319 | coro_frame = coro.cr_frame |
| 320 | |
| 321 | filename = coro_code.co_filename |
Andrew Svetlov | c07b16b | 2016-01-11 08:42:49 +0200 | [diff] [blame] | 322 | lineno = 0 |
| 323 | if (isinstance(coro, CoroWrapper) and |
| 324 | not inspect.isgeneratorfunction(coro.func) and |
| 325 | coro.func is not None): |
| 326 | source = events._get_function_source(coro.func) |
| 327 | if source is not None: |
| 328 | filename, lineno = source |
Yury Selivanov | 29a602a | 2015-06-24 10:30:14 -0400 | [diff] [blame] | 329 | if coro_frame is None: |
Guido van Rossum | 0a9933e | 2015-05-02 18:38:24 -0700 | [diff] [blame] | 330 | coro_repr = ('%s done, defined at %s:%s' |
Victor Stinner | 15cc678 | 2015-01-09 00:09:10 +0100 | [diff] [blame] | 331 | % (coro_name, filename, lineno)) |
Victor Stinner | c39ba7d | 2014-07-11 00:21:27 +0200 | [diff] [blame] | 332 | else: |
Guido van Rossum | 0a9933e | 2015-05-02 18:38:24 -0700 | [diff] [blame] | 333 | coro_repr = ('%s running, defined at %s:%s' |
Victor Stinner | 15cc678 | 2015-01-09 00:09:10 +0100 | [diff] [blame] | 334 | % (coro_name, filename, lineno)) |
Yury Selivanov | 29a602a | 2015-06-24 10:30:14 -0400 | [diff] [blame] | 335 | elif coro_frame is not None: |
| 336 | lineno = coro_frame.f_lineno |
Guido van Rossum | 0a9933e | 2015-05-02 18:38:24 -0700 | [diff] [blame] | 337 | coro_repr = ('%s running at %s:%s' |
Victor Stinner | 15cc678 | 2015-01-09 00:09:10 +0100 | [diff] [blame] | 338 | % (coro_name, filename, lineno)) |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 339 | else: |
Yury Selivanov | 29a602a | 2015-06-24 10:30:14 -0400 | [diff] [blame] | 340 | lineno = coro_code.co_firstlineno |
Guido van Rossum | 0a9933e | 2015-05-02 18:38:24 -0700 | [diff] [blame] | 341 | coro_repr = ('%s done, defined at %s:%s' |
Victor Stinner | 15cc678 | 2015-01-09 00:09:10 +0100 | [diff] [blame] | 342 | % (coro_name, filename, lineno)) |
Victor Stinner | c39ba7d | 2014-07-11 00:21:27 +0200 | [diff] [blame] | 343 | |
| 344 | return coro_repr |