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/
Date: Sat,  6 Mar 2021 09:14:56 +0000 (UTC)	[thread overview]
Message-ID: <1615021613.1e843f853a9afe82d599e6ab09064147ddc1d271.zmedico@gentoo> (raw)

commit:     1e843f853a9afe82d599e6ab09064147ddc1d271
Author:     Matt Turner <mattst88 <AT> gentoo <DOT> org>
AuthorDate: Thu Mar  4 19:24:58 2021 +0000
Commit:     Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Sat Mar  6 09:06:53 2021 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=1e843f85

Use asyncio.subprocess.Process directly

With no need to support Python 2, we can remove our private
implementation.

Signed-off-by: Matt Turner <mattst88 <AT> gentoo.org>
Signed-off-by: Zac Medico <zmedico <AT> gentoo.org>

 lib/portage/util/futures/_asyncio/__init__.py |   8 +-
 lib/portage/util/futures/_asyncio/process.py  | 116 --------------------------
 2 files changed, 4 insertions(+), 120 deletions(-)

diff --git a/lib/portage/util/futures/_asyncio/__init__.py b/lib/portage/util/futures/_asyncio/__init__.py
index 5590963f1..207e7205d 100644
--- a/lib/portage/util/futures/_asyncio/__init__.py
+++ b/lib/portage/util/futures/_asyncio/__init__.py
@@ -25,6 +25,7 @@ import types
 import weakref
 
 import asyncio as _real_asyncio
+from asyncio.subprocess import Process
 
 try:
 	import threading
@@ -45,7 +46,6 @@ from portage.util.futures.futures import (
 	TimeoutError,
 )
 # pylint: enable=redefined-builtin
-from portage.util.futures._asyncio.process import _Process
 from portage.util.futures._asyncio.tasks import (
 	ALL_COMPLETED,
 	FIRST_COMPLETED,
@@ -124,8 +124,8 @@ def create_subprocess_exec(*args, **kwargs):
 	@type loop: event loop
 	@type kwargs: varies
 	@param kwargs: subprocess.Popen parameters
-	@rtype: asyncio.Future (or compatible)
-	@return: subset of asyncio.subprocess.Process interface
+	@rtype: asyncio.subprocess.Process (or compatible)
+	@return: asyncio.subprocess.Process interface
 	"""
 	loop = _wrap_loop(kwargs.pop('loop', None))
 	# Python 3.4 and later implement PEP 446, which makes newly
@@ -138,7 +138,7 @@ def create_subprocess_exec(*args, **kwargs):
 
 	result = loop.create_future()
 
-	result.set_result(_Process(subprocess.Popen(
+	result.set_result(Process(subprocess.Popen(
 		args,
 		stdin=kwargs.pop('stdin', None),
 		stdout=kwargs.pop('stdout', None),

diff --git a/lib/portage/util/futures/_asyncio/process.py b/lib/portage/util/futures/_asyncio/process.py
deleted file mode 100644
index 275c9031a..000000000
--- a/lib/portage/util/futures/_asyncio/process.py
+++ /dev/null
@@ -1,116 +0,0 @@
-# Copyright 2018-2020 Gentoo Authors
-# Distributed under the terms of the GNU General Public License v2
-
-import os
-
-import portage
-portage.proxy.lazyimport.lazyimport(globals(),
-	'portage.util.futures:asyncio',
-	'portage.util.futures.unix_events:_set_nonblocking',
-)
-from portage.util.futures._asyncio.streams import _reader, _writer
-from portage.util.futures.compat_coroutine import coroutine, coroutine_return
-
-
-class _Process:
-	"""
-	Emulate a subset of the asyncio.subprocess.Process interface,
-	for python2.
-	"""
-	def __init__(self, proc, loop):
-		"""
-		@param proc: process instance
-		@type proc: subprocess.Popen
-		@param loop: asyncio.AbstractEventLoop (or compatible)
-		@type loop: event loop
-		"""
-		self._proc = proc
-		self._loop = loop
-		self.terminate = proc.terminate
-		self.kill = proc.kill
-		self.send_signal = proc.send_signal
-		self.pid = proc.pid
-		self._waiters = []
-		loop._asyncio_child_watcher.\
-			add_child_handler(self.pid, self._proc_exit)
-
-	@property
-	def returncode(self):
-		return self._proc.returncode
-
-	@coroutine
-	def communicate(self, input=None, loop=None): # pylint: disable=redefined-builtin
-		"""
-		Read data from stdout and stderr, until end-of-file is reached.
-		Wait for process to terminate.
-
-		@param input: stdin content to write
-		@type input: bytes
-		@return: tuple (stdout_data, stderr_data)
-		@rtype: asyncio.Future (or compatible)
-		"""
-		loop = asyncio._wrap_loop(loop or self._loop)
-		futures = []
-		for input_file in (self._proc.stdout, self._proc.stderr):
-			if input_file is None:
-				future = loop.create_future()
-				future.set_result(None)
-			else:
-				future = _reader(input_file, loop=loop)
-			futures.append(future)
-
-		writer = None
-		if input is not None:
-			if self._proc.stdin is None:
-				raise TypeError('communicate: expected file or int, got {}'.format(type(self._proc.stdin)))
-			stdin = self._proc.stdin
-			stdin = os.fdopen(stdin, 'wb', 0) if isinstance(stdin, int) else stdin
-			_set_nonblocking(stdin.fileno())
-			writer = asyncio.ensure_future(_writer(stdin, input, loop=loop), loop=loop)
-			writer.add_done_callback(lambda writer: stdin.close())
-
-		try:
-			yield asyncio.wait(futures + [self.wait(loop=loop)], loop=loop)
-		finally:
-			if writer is not None:
-				if writer.done():
-					# Consume expected exceptions.
-					try:
-						writer.result()
-					except EnvironmentError:
-						# This is normal if the other end of the pipe was closed.
-						pass
-				else:
-					writer.cancel()
-
-		coroutine_return(tuple(future.result() for future in futures))
-
-	def wait(self, loop=None):
-		"""
-		Wait for child process to terminate. Set and return returncode attribute.
-
-		@return: returncode
-		@rtype: asyncio.Future (or compatible)
-		"""
-		loop = asyncio._wrap_loop(loop or self._loop)
-		waiter = loop.create_future()
-		if self.returncode is None:
-			self._waiters.append(waiter)
-			waiter.add_done_callback(self._waiter_cancel)
-		else:
-			waiter.set_result(self.returncode)
-		return waiter
-
-	def _waiter_cancel(self, waiter):
-		if waiter.cancelled():
-			try:
-				self._waiters.remove(waiter)
-			except ValueError:
-				pass
-
-	def _proc_exit(self, pid, returncode):
-		self._proc.returncode = returncode
-		waiters = self._waiters
-		self._waiters = []
-		for waiter in waiters:
-			waiter.set_result(returncode)


             reply	other threads:[~2021-03-06  9:15 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-06  9:14 Zac Medico [this message]
  -- strict thread matches above, loose matches on Subject: below --
2024-09-01  7:02 [gentoo-commits] proj/portage:master commit in: lib/portage/util/futures/_asyncio/ Zac Medico
2024-09-01  7:02 Zac Medico
2024-09-01  7:02 Zac Medico
2024-08-31 19:20 Zac Medico
2024-08-19 14:49 Zac Medico
2024-08-19 14:49 Zac Medico
2024-02-22 15:36 Zac Medico
2024-02-21 16:00 Zac Medico
2021-09-20  5:36 Zac Medico
2021-03-07 15:17 Zac Medico
2021-03-07  5:28 Zac Medico
2020-12-07  8:41 Zac Medico
2020-08-03 19:30 Zac Medico
2020-06-18 18:06 Zac Medico
2020-02-29  4:33 Zac Medico
2019-10-18  3:43 Zac Medico
2019-05-18 22:25 Zac Medico

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=1615021613.1e843f853a9afe82d599e6ab09064147ddc1d271.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