1 |
commit: 48c06e489e695321e8059da2dac1c03f6624d2e8 |
2 |
Author: Zac Medico <zmedico <AT> gentoo <DOT> org> |
3 |
AuthorDate: Mon Aug 6 06:43:41 2018 +0000 |
4 |
Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> |
5 |
CommitDate: Mon Sep 24 03:41:17 2018 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=48c06e48 |
7 |
|
8 |
Implement asyncio.iscoroutinefunction for compat_coroutine |
9 |
|
10 |
Sometimes it's useful to test if a function is a coroutine function, |
11 |
so implement a version of asyncio.iscoroutinefunction that works |
12 |
with asyncio.coroutine as well as compat_coroutine.coroutine (since |
13 |
both kinds of coroutine functions behave identically for our |
14 |
purposes). |
15 |
|
16 |
Reviewed-by: Brian Dolbec <dolsen <AT> gentoo.org> |
17 |
Signed-off-by: Zac Medico <zmedico <AT> gentoo.org> |
18 |
|
19 |
lib/portage/util/futures/_asyncio/__init__.py | 14 ++++++++++++++ |
20 |
lib/portage/util/futures/compat_coroutine.py | 12 ++++++++++++ |
21 |
2 files changed, 26 insertions(+) |
22 |
|
23 |
diff --git a/lib/portage/util/futures/_asyncio/__init__.py b/lib/portage/util/futures/_asyncio/__init__.py |
24 |
index faab98e47..2a637624d 100644 |
25 |
--- a/lib/portage/util/futures/_asyncio/__init__.py |
26 |
+++ b/lib/portage/util/futures/_asyncio/__init__.py |
27 |
@@ -36,6 +36,7 @@ except ImportError: |
28 |
import portage |
29 |
portage.proxy.lazyimport.lazyimport(globals(), |
30 |
'portage.util.futures.unix_events:_PortageEventLoopPolicy', |
31 |
+ 'portage.util.futures:compat_coroutine@_compat_coroutine', |
32 |
) |
33 |
from portage.util._eventloop.asyncio_event_loop import AsyncioEventLoop as _AsyncioEventLoop |
34 |
from portage.util._eventloop.global_event_loop import ( |
35 |
@@ -152,6 +153,19 @@ def create_subprocess_exec(*args, **kwargs): |
36 |
return result |
37 |
|
38 |
|
39 |
+def iscoroutinefunction(func): |
40 |
+ """ |
41 |
+ Return True if func is a decorated coroutine function, |
42 |
+ supporting both asyncio.coroutine and compat_coroutine since |
43 |
+ their behavior is identical for all practical purposes. |
44 |
+ """ |
45 |
+ if _compat_coroutine._iscoroutinefunction(func): |
46 |
+ return True |
47 |
+ elif _real_asyncio is not None and _real_asyncio.iscoroutinefunction(func): |
48 |
+ return True |
49 |
+ return False |
50 |
+ |
51 |
+ |
52 |
class Task(Future): |
53 |
""" |
54 |
Schedule the execution of a coroutine: wrap it in a future. A task |
55 |
|
56 |
diff --git a/lib/portage/util/futures/compat_coroutine.py b/lib/portage/util/futures/compat_coroutine.py |
57 |
index 3edfa6bee..b5ff92faf 100644 |
58 |
--- a/lib/portage/util/futures/compat_coroutine.py |
59 |
+++ b/lib/portage/util/futures/compat_coroutine.py |
60 |
@@ -8,6 +8,17 @@ portage.proxy.lazyimport.lazyimport(globals(), |
61 |
'portage.util.futures:asyncio', |
62 |
) |
63 |
|
64 |
+# A marker for iscoroutinefunction. |
65 |
+_is_coroutine = object() |
66 |
+ |
67 |
+ |
68 |
+def _iscoroutinefunction(func): |
69 |
+ """ |
70 |
+ Return True if func is a decorated coroutine function |
71 |
+ created with the coroutine decorator for this module. |
72 |
+ """ |
73 |
+ return getattr(func, '_is_coroutine', None) is _is_coroutine |
74 |
+ |
75 |
|
76 |
def coroutine(generator_func): |
77 |
""" |
78 |
@@ -34,6 +45,7 @@ def coroutine(generator_func): |
79 |
@functools.wraps(generator_func) |
80 |
def wrapped(*args, **kwargs): |
81 |
return _generator_future(generator_func, *args, **kwargs) |
82 |
+ wrapped._is_coroutine = _is_coroutine |
83 |
return wrapped |