1 |
Sometimes it's useful to test if a function is a coroutine function, |
2 |
so implement a version of asyncio.iscoroutinefunction that works |
3 |
with asyncio.coroutine as well as compat_coroutine.coroutine (since |
4 |
both kinds of coroutine functions behave identically for our |
5 |
purposes). |
6 |
--- |
7 |
lib/portage/util/futures/_asyncio/__init__.py | 14 ++++++++++++++ |
8 |
lib/portage/util/futures/compat_coroutine.py | 12 ++++++++++++ |
9 |
2 files changed, 26 insertions(+) |
10 |
|
11 |
diff --git a/lib/portage/util/futures/_asyncio/__init__.py b/lib/portage/util/futures/_asyncio/__init__.py |
12 |
index faab98e47..2a637624d 100644 |
13 |
--- a/lib/portage/util/futures/_asyncio/__init__.py |
14 |
+++ b/lib/portage/util/futures/_asyncio/__init__.py |
15 |
@@ -36,6 +36,7 @@ except ImportError: |
16 |
import portage |
17 |
portage.proxy.lazyimport.lazyimport(globals(), |
18 |
'portage.util.futures.unix_events:_PortageEventLoopPolicy', |
19 |
+ 'portage.util.futures:compat_coroutine@_compat_coroutine', |
20 |
) |
21 |
from portage.util._eventloop.asyncio_event_loop import AsyncioEventLoop as _AsyncioEventLoop |
22 |
from portage.util._eventloop.global_event_loop import ( |
23 |
@@ -152,6 +153,19 @@ def create_subprocess_exec(*args, **kwargs): |
24 |
return result |
25 |
|
26 |
|
27 |
+def iscoroutinefunction(func): |
28 |
+ """ |
29 |
+ Return True if func is a decorated coroutine function, |
30 |
+ supporting both asyncio.coroutine and compat_coroutine since |
31 |
+ their behavior is identical for all practical purposes. |
32 |
+ """ |
33 |
+ if _compat_coroutine._iscoroutinefunction(func): |
34 |
+ return True |
35 |
+ elif _real_asyncio is not None and _real_asyncio.iscoroutinefunction(func): |
36 |
+ return True |
37 |
+ return False |
38 |
+ |
39 |
+ |
40 |
class Task(Future): |
41 |
""" |
42 |
Schedule the execution of a coroutine: wrap it in a future. A task |
43 |
diff --git a/lib/portage/util/futures/compat_coroutine.py b/lib/portage/util/futures/compat_coroutine.py |
44 |
index 59fdc31b6..be305c1b5 100644 |
45 |
--- a/lib/portage/util/futures/compat_coroutine.py |
46 |
+++ b/lib/portage/util/futures/compat_coroutine.py |
47 |
@@ -8,6 +8,17 @@ portage.proxy.lazyimport.lazyimport(globals(), |
48 |
'portage.util.futures:asyncio', |
49 |
) |
50 |
|
51 |
+# A marker for iscoroutinefunction. |
52 |
+_is_coroutine = object() |
53 |
+ |
54 |
+ |
55 |
+def _iscoroutinefunction(func): |
56 |
+ """ |
57 |
+ Return True if func is a decorated coroutine function |
58 |
+ created with the coroutine decorator for this module. |
59 |
+ """ |
60 |
+ return getattr(func, '_is_coroutine', None) is _is_coroutine |
61 |
+ |
62 |
|
63 |
def coroutine(generator_func): |
64 |
""" |
65 |
@@ -34,6 +45,7 @@ def coroutine(generator_func): |
66 |
@functools.wraps(generator_func) |
67 |
def wrapped(*args, **kwargs): |
68 |
return _generator_future(generator_func, *args, **kwargs) |
69 |
+ wrapped._is_coroutine = _is_coroutine |
70 |
return wrapped |
71 |
|
72 |
|
73 |
-- |
74 |
2.16.4 |