1 |
commit: b530e4e67b843837093e545cf81fd4ab8336d2c2 |
2 |
Author: Zac Medico <zmedico <AT> gentoo <DOT> org> |
3 |
AuthorDate: Sun Apr 29 21:09:59 2018 +0000 |
4 |
Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> |
5 |
CommitDate: Sun Apr 29 21:26:37 2018 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=b530e4e6 |
7 |
|
8 |
SubProcess: add_child_handler asyncio compat (bug 654276) |
9 |
|
10 |
Migrate to asyncio's AbstractChildWatcher.add_child_handler |
11 |
interface. Re-use PopenProcess code introduced in commit |
12 |
be800bf0153a28ce034277d103a2021f93ac8b2e. Also remove the |
13 |
child_watch_add method from SchedulerInterface, since there |
14 |
are no more consumers. |
15 |
|
16 |
Bug: https://bugs.gentoo.org/654276 |
17 |
|
18 |
pym/_emerge/SubProcess.py | 58 ++++----------------------- |
19 |
pym/portage/util/_async/PopenProcess.py | 20 ++------- |
20 |
pym/portage/util/_async/SchedulerInterface.py | 1 - |
21 |
3 files changed, 11 insertions(+), 68 deletions(-) |
22 |
|
23 |
diff --git a/pym/_emerge/SubProcess.py b/pym/_emerge/SubProcess.py |
24 |
index 3939de3ed..aa4778737 100644 |
25 |
--- a/pym/_emerge/SubProcess.py |
26 |
+++ b/pym/_emerge/SubProcess.py |
27 |
@@ -19,28 +19,7 @@ class SubProcess(AbstractPollTask): |
28 |
_cancel_timeout = 1000 # 1 second |
29 |
|
30 |
def _poll(self): |
31 |
- if self.returncode is not None: |
32 |
- return self.returncode |
33 |
- if self.pid is None: |
34 |
- return self.returncode |
35 |
- if self._registered: |
36 |
- return self.returncode |
37 |
- |
38 |
- try: |
39 |
- # With waitpid and WNOHANG, only check the |
40 |
- # first element of the tuple since the second |
41 |
- # element may vary (bug #337465). |
42 |
- retval = os.waitpid(self.pid, os.WNOHANG) |
43 |
- except OSError as e: |
44 |
- if e.errno != errno.ECHILD: |
45 |
- raise |
46 |
- del e |
47 |
- retval = (self.pid, 1) |
48 |
- |
49 |
- if retval[0] == 0: |
50 |
- return None |
51 |
- self._set_returncode(retval) |
52 |
- self._async_wait() |
53 |
+ # Simply rely on _async_waitpid_cb to set the returncode. |
54 |
return self.returncode |
55 |
|
56 |
def _cancel(self): |
57 |
@@ -71,20 +50,16 @@ class SubProcess(AbstractPollTask): |
58 |
if self.returncode is not None: |
59 |
self._async_wait() |
60 |
elif self._waitpid_id is None: |
61 |
- self._waitpid_id = self.scheduler.child_watch_add( |
62 |
- self.pid, self._async_waitpid_cb) |
63 |
+ self._waitpid_id = self.pid |
64 |
+ self.scheduler._asyncio_child_watcher.\ |
65 |
+ add_child_handler(self.pid, self._async_waitpid_cb) |
66 |
|
67 |
- def _async_waitpid_cb(self, pid, condition, user_data=None): |
68 |
+ def _async_waitpid_cb(self, pid, returncode): |
69 |
if pid != self.pid: |
70 |
raise AssertionError("expected pid %s, got %s" % (self.pid, pid)) |
71 |
- self._set_returncode((pid, condition)) |
72 |
+ self.returncode = returncode |
73 |
self._async_wait() |
74 |
|
75 |
- def _waitpid_cb(self, pid, condition, user_data=None): |
76 |
- if pid != self.pid: |
77 |
- raise AssertionError("expected pid %s, got %s" % (self.pid, pid)) |
78 |
- self._set_returncode((pid, condition)) |
79 |
- |
80 |
def _orphan_process_warn(self): |
81 |
pass |
82 |
|
83 |
@@ -100,7 +75,8 @@ class SubProcess(AbstractPollTask): |
84 |
self._reg_id = None |
85 |
|
86 |
if self._waitpid_id is not None: |
87 |
- self.scheduler.source_remove(self._waitpid_id) |
88 |
+ self.scheduler._asyncio_child_watcher.\ |
89 |
+ remove_child_handler(self._waitpid_id) |
90 |
self._waitpid_id = None |
91 |
|
92 |
if self._files is not None: |
93 |
@@ -126,21 +102,3 @@ class SubProcess(AbstractPollTask): |
94 |
elif event & self.scheduler.IO_HUP: |
95 |
self._unregister() |
96 |
self._async_waitpid() |
97 |
- |
98 |
- def _set_returncode(self, wait_retval): |
99 |
- """ |
100 |
- Set the returncode in a manner compatible with |
101 |
- subprocess.Popen.returncode: A negative value -N indicates |
102 |
- that the child was terminated by signal N (Unix only). |
103 |
- """ |
104 |
- self._unregister() |
105 |
- |
106 |
- pid, status = wait_retval |
107 |
- |
108 |
- if os.WIFSIGNALED(status): |
109 |
- retval = - os.WTERMSIG(status) |
110 |
- else: |
111 |
- retval = os.WEXITSTATUS(status) |
112 |
- |
113 |
- self.returncode = retval |
114 |
- |
115 |
|
116 |
diff --git a/pym/portage/util/_async/PopenProcess.py b/pym/portage/util/_async/PopenProcess.py |
117 |
index 3fb60d527..c1931327a 100644 |
118 |
--- a/pym/portage/util/_async/PopenProcess.py |
119 |
+++ b/pym/portage/util/_async/PopenProcess.py |
120 |
@@ -25,23 +25,9 @@ class PopenProcess(SubProcess): |
121 |
def _pipe_reader_exit(self, pipe_reader): |
122 |
self._async_waitpid() |
123 |
|
124 |
- def _async_waitpid(self): |
125 |
- if self.returncode is None: |
126 |
- self.scheduler._asyncio_child_watcher.\ |
127 |
- add_child_handler(self.pid, self._async_waitpid_cb) |
128 |
- else: |
129 |
- self._unregister() |
130 |
- self._async_wait() |
131 |
- |
132 |
- def _async_waitpid_cb(self, pid, returncode): |
133 |
+ def _async_waitpid_cb(self, *args, **kwargs): |
134 |
+ SubProcess._async_waitpid_cb(self, *args, **kwargs) |
135 |
if self.proc.returncode is None: |
136 |
# Suppress warning messages like this: |
137 |
# ResourceWarning: subprocess 1234 is still running |
138 |
- self.proc.returncode = returncode |
139 |
- self._unregister() |
140 |
- self.returncode = returncode |
141 |
- self._async_wait() |
142 |
- |
143 |
- def _poll(self): |
144 |
- # Simply rely on _async_waitpid_cb to set the returncode. |
145 |
- return self.returncode |
146 |
+ self.proc.returncode = self.returncode |
147 |
|
148 |
diff --git a/pym/portage/util/_async/SchedulerInterface.py b/pym/portage/util/_async/SchedulerInterface.py |
149 |
index be118ae0e..ff39bc587 100644 |
150 |
--- a/pym/portage/util/_async/SchedulerInterface.py |
151 |
+++ b/pym/portage/util/_async/SchedulerInterface.py |
152 |
@@ -13,7 +13,6 @@ class SchedulerInterface(SlotObject): |
153 |
|
154 |
_event_loop_attrs = ("IO_ERR", "IO_HUP", "IO_IN", |
155 |
"IO_NVAL", "IO_OUT", "IO_PRI", |
156 |
- "child_watch_add", |
157 |
"io_add_watch", |
158 |
"source_remove", |
159 |
"timeout_add", |