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)