public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Zac Medico" <zmedico@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/portage:master commit in: lib/portage/util/futures/_asyncio/, lib/portage/util/futures/
Date: Mon, 24 Sep 2018 06:12:11 +0000 (UTC)	[thread overview]
Message-ID: <1537760477.48c06e489e695321e8059da2dac1c03f6624d2e8.zmedico@gentoo> (raw)

commit:     48c06e489e695321e8059da2dac1c03f6624d2e8
Author:     Zac Medico <zmedico <AT> gentoo <DOT> org>
AuthorDate: Mon Aug  6 06:43:41 2018 +0000
Commit:     Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Mon Sep 24 03:41:17 2018 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=48c06e48

Implement asyncio.iscoroutinefunction for compat_coroutine

Sometimes it's useful to test if a function is a coroutine function,
so implement a version of asyncio.iscoroutinefunction that works
with asyncio.coroutine as well as compat_coroutine.coroutine (since
both kinds of coroutine functions behave identically for our
purposes).

Reviewed-by: Brian Dolbec <dolsen <AT> gentoo.org>
Signed-off-by: Zac Medico <zmedico <AT> gentoo.org>

 lib/portage/util/futures/_asyncio/__init__.py | 14 ++++++++++++++
 lib/portage/util/futures/compat_coroutine.py  | 12 ++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/lib/portage/util/futures/_asyncio/__init__.py b/lib/portage/util/futures/_asyncio/__init__.py
index faab98e47..2a637624d 100644
--- a/lib/portage/util/futures/_asyncio/__init__.py
+++ b/lib/portage/util/futures/_asyncio/__init__.py
@@ -36,6 +36,7 @@ except ImportError:
 import portage
 portage.proxy.lazyimport.lazyimport(globals(),
 	'portage.util.futures.unix_events:_PortageEventLoopPolicy',
+	'portage.util.futures:compat_coroutine@_compat_coroutine',
 )
 from portage.util._eventloop.asyncio_event_loop import AsyncioEventLoop as _AsyncioEventLoop
 from portage.util._eventloop.global_event_loop import (
@@ -152,6 +153,19 @@ def create_subprocess_exec(*args, **kwargs):
 	return result
 
 
+def iscoroutinefunction(func):
+	"""
+	Return True if func is a decorated coroutine function,
+	supporting both asyncio.coroutine and compat_coroutine since
+	their behavior is identical for all practical purposes.
+	"""
+	if _compat_coroutine._iscoroutinefunction(func):
+		return True
+	elif _real_asyncio is not None and _real_asyncio.iscoroutinefunction(func):
+		return True
+	return False
+
+
 class Task(Future):
 	"""
 	Schedule the execution of a coroutine: wrap it in a future. A task

diff --git a/lib/portage/util/futures/compat_coroutine.py b/lib/portage/util/futures/compat_coroutine.py
index 3edfa6bee..b5ff92faf 100644
--- a/lib/portage/util/futures/compat_coroutine.py
+++ b/lib/portage/util/futures/compat_coroutine.py
@@ -8,6 +8,17 @@ portage.proxy.lazyimport.lazyimport(globals(),
 	'portage.util.futures:asyncio',
 )
 
+# A marker for iscoroutinefunction.
+_is_coroutine = object()
+
+
+def _iscoroutinefunction(func):
+	"""
+	Return True if func is a decorated coroutine function
+	created with the coroutine decorator for this module.
+	"""
+	return getattr(func, '_is_coroutine', None) is _is_coroutine
+
 
 def coroutine(generator_func):
 	"""
@@ -34,6 +45,7 @@ def coroutine(generator_func):
 	@functools.wraps(generator_func)
 	def wrapped(*args, **kwargs):
 		return _generator_future(generator_func, *args, **kwargs)
+	wrapped._is_coroutine = _is_coroutine
 	return wrapped
 
 


                 reply	other threads:[~2018-09-24  6:12 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1537760477.48c06e489e695321e8059da2dac1c03f6624d2e8.zmedico@gentoo \
    --to=zmedico@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox