From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 40C5D138334 for ; Mon, 24 Sep 2018 06:12:19 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 04762E092D; Mon, 24 Sep 2018 06:12:16 +0000 (UTC) Received: from smtp.gentoo.org (dev.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id BCA82E092D for ; Mon, 24 Sep 2018 06:12:15 +0000 (UTC) Received: from oystercatcher.gentoo.org (unknown [IPv6:2a01:4f8:202:4333:225:90ff:fed9:fc84]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 8703D335C7D for ; Mon, 24 Sep 2018 06:12:13 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 59DEC3B2 for ; Mon, 24 Sep 2018 06:12:11 +0000 (UTC) From: "Zac Medico" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Zac Medico" Message-ID: <1537760477.48c06e489e695321e8059da2dac1c03f6624d2e8.zmedico@gentoo> Subject: [gentoo-commits] proj/portage:master commit in: lib/portage/util/futures/_asyncio/, lib/portage/util/futures/ X-VCS-Repository: proj/portage X-VCS-Files: lib/portage/util/futures/_asyncio/__init__.py lib/portage/util/futures/compat_coroutine.py X-VCS-Directories: lib/portage/util/futures/_asyncio/ lib/portage/util/futures/ X-VCS-Committer: zmedico X-VCS-Committer-Name: Zac Medico X-VCS-Revision: 48c06e489e695321e8059da2dac1c03f6624d2e8 X-VCS-Branch: master Date: Mon, 24 Sep 2018 06:12:11 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: edef2f2a-9de3-442d-8ec8-b24f8d7db4a7 X-Archives-Hash: 74ae54e969790d98e71e9a0166a96ab0 commit: 48c06e489e695321e8059da2dac1c03f6624d2e8 Author: Zac Medico gentoo org> AuthorDate: Mon Aug 6 06:43:41 2018 +0000 Commit: Zac Medico gentoo 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 gentoo.org> Signed-off-by: Zac Medico 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