commit: 859fe1a14214fb1a188cc0b49f8683a94cd04011
Author: Zac Medico <zmedico <AT> gentoo <DOT> org>
AuthorDate: Sun Jul 19 03:57:12 2020 +0000
Commit: Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Sun Jul 19 04:20:51 2020 +0000
URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=859fe1a1
AsyncFunction: stdin support
Signed-off-by: Zac Medico <zmedico <AT> gentoo.org>
lib/portage/tests/process/test_AsyncFunction.py | 38 +++++++++++++++++++++++++
lib/portage/util/_async/AsyncFunction.py | 4 +--
2 files changed, 40 insertions(+), 2 deletions(-)
diff --git a/lib/portage/tests/process/test_AsyncFunction.py
b/lib/portage/tests/process/test_AsyncFunction.py
new file mode 100644
index 000000000..55857026d
--- /dev/null
+++ b/lib/portage/tests/process/test_AsyncFunction.py
@@ -0,0 +1,38 @@
+# Copyright 2020 Gentoo Authors
+# Distributed under the terms of the GNU General Public License v2
+
+import sys
+
+from portage import os
+from portage.tests import TestCase
+from portage.util._async.AsyncFunction import AsyncFunction
+from portage.util.futures import asyncio
+from portage.util.futures._asyncio.streams import _writer
+from portage.util.futures.compat_coroutine import coroutine
+from portage.util.futures.unix_events import _set_nonblocking
+
+
+class AsyncFunctionTestCase(TestCase):
+
+ @staticmethod
+ def _read_from_stdin(pw):
+ os.close(pw)
+ return ''.join(sys.stdin)
+
+ @coroutine
+ def _testAsyncFunctionStdin(self, loop):
+ test_string = '1\n2\n3\n'
+ pr, pw = os.pipe()
+ fd_pipes = {0:pr}
+ reader = AsyncFunction(scheduler=loop, fd_pipes=fd_pipes,
target=self._read_from_stdin, args=(pw,))
+ reader.start()
+ os.close(pr)
+ _set_nonblocking(pw)
+ with open(pw, mode='wb', buffering=0) as pipe_write:
+ yield _writer(pipe_write, test_string.encode('utf_8'),
loop=loop)
+ self.assertEqual((yield reader.async_wait()), os.EX_OK)
+ self.assertEqual(reader.result, test_string)
+
+ def testAsyncFunctionStdin(self):
+ loop = asyncio._wrap_loop()
+ loop.run_until_complete(self._testAsyncFunctionStdin(loop))
diff --git a/lib/portage/util/_async/AsyncFunction.py
b/lib/portage/util/_async/AsyncFunction.py
index 1dffa36cc..db77a6f43 100644
--- a/lib/portage/util/_async/AsyncFunction.py
+++ b/lib/portage/util/_async/AsyncFunction.py
@@ -1,4 +1,4 @@
-# Copyright 2015 Gentoo Foundation
+# Copyright 2015-2020 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
import pickle
@@ -23,7 +23,7 @@ class AsyncFunction(ForkProcess):
def _start(self):
pr, pw = os.pipe()
- self.fd_pipes = {}
+ self.fd_pipes = {} if self.fd_pipes is None else self.fd_pipes
self.fd_pipes[pw] = pw
self._async_func_reader_pw = pw
self._async_func_reader = PipeReader(