This repository has been archived by the owner on Apr 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
python-2.6.6-subprocess-poll.patch
226 lines (213 loc) · 8.14 KB
/
python-2.6.6-subprocess-poll.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
diff -up Python-2.6.6/Lib/subprocess.py.subprocess-poll Python-2.6.6/Lib/subprocess.py
--- Python-2.6.6/Lib/subprocess.py.subprocess-poll 2010-07-19 10:49:38.000000000 -0400
+++ Python-2.6.6/Lib/subprocess.py 2011-01-11 12:50:54.912403279 -0500
@@ -412,10 +412,17 @@ if mswindows:
error = IOError
else:
import select
+ _has_poll = hasattr(select, 'poll')
import errno
import fcntl
import pickle
+ # When select or poll has indicated that the file is writable,
+ # we can write up to _PIPE_BUF bytes without risk of blocking.
+ # POSIX defines PIPE_BUF as >= 512.
+ _PIPE_BUF = getattr(select, 'PIPE_BUF', 512)
+
+
__all__ = ["Popen", "PIPE", "STDOUT", "call", "check_call", "CalledProcessError"]
if mswindows:
@@ -425,13 +432,6 @@ try:
except:
MAXFD = 256
-# True/False does not exist on 2.2.0
-#try:
-# False
-#except NameError:
-# False = 0
-# True = 1
-
_active = []
def _cleanup():
@@ -1185,19 +1185,100 @@ class Popen(object):
def _communicate(self, input):
- read_set = []
- write_set = []
- stdout = None # Return
- stderr = None # Return
-
if self.stdin:
# Flush stdio buffer. This might block, if the user has
# been writing to .stdin in an uncontrolled fashion.
self.stdin.flush()
- if input:
- write_set.append(self.stdin)
- else:
+ if not input:
self.stdin.close()
+
+ if _has_poll:
+ stdout, stderr = self._communicate_with_poll(input)
+ else:
+ stdout, stderr = self._communicate_with_select(input)
+
+ # All data exchanged. Translate lists into strings.
+ if stdout is not None:
+ stdout = ''.join(stdout)
+ if stderr is not None:
+ stderr = ''.join(stderr)
+
+ # Translate newlines, if requested. We cannot let the file
+ # object do the translation: It is based on stdio, which is
+ # impossible to combine with select (unless forcing no
+ # buffering).
+ if self.universal_newlines and hasattr(file, 'newlines'):
+ if stdout:
+ stdout = self._translate_newlines(stdout)
+ if stderr:
+ stderr = self._translate_newlines(stderr)
+
+ self.wait()
+ return (stdout, stderr)
+
+
+ def _communicate_with_poll(self, input):
+ stdout = None # Return
+ stderr = None # Return
+ fd2file = {}
+ fd2output = {}
+
+ poller = select.poll()
+ def register_and_append(file_obj, eventmask):
+ poller.register(file_obj.fileno(), eventmask)
+ fd2file[file_obj.fileno()] = file_obj
+
+ def close_unregister_and_remove(fd):
+ poller.unregister(fd)
+ fd2file[fd].close()
+ fd2file.pop(fd)
+
+ if self.stdin and input:
+ register_and_append(self.stdin, select.POLLOUT)
+
+ select_POLLIN_POLLPRI = select.POLLIN | select.POLLPRI
+ if self.stdout:
+ register_and_append(self.stdout, select_POLLIN_POLLPRI)
+ fd2output[self.stdout.fileno()] = stdout = []
+ if self.stderr:
+ register_and_append(self.stderr, select_POLLIN_POLLPRI)
+ fd2output[self.stderr.fileno()] = stderr = []
+
+ input_offset = 0
+ while fd2file:
+ try:
+ ready = poller.poll()
+ except select.error, e:
+ if e.args[0] == errno.EINTR:
+ continue
+ raise
+
+ for fd, mode in ready:
+ if mode & select.POLLOUT:
+ chunk = input[input_offset : input_offset + _PIPE_BUF]
+ input_offset += os.write(fd, chunk)
+ if input_offset >= len(input):
+ close_unregister_and_remove(fd)
+ elif mode & select_POLLIN_POLLPRI:
+ data = os.read(fd, 4096)
+ if not data:
+ close_unregister_and_remove(fd)
+ fd2output[fd].append(data)
+ else:
+ # Ignore hang up or errors.
+ close_unregister_and_remove(fd)
+
+ return (stdout, stderr)
+
+
+ def _communicate_with_select(self, input):
+ read_set = []
+ write_set = []
+ stdout = None # Return
+ stderr = None # Return
+
+ if self.stdin and input:
+ write_set.append(self.stdin)
if self.stdout:
read_set.append(self.stdout)
stdout = []
@@ -1215,10 +1296,7 @@ class Popen(object):
raise
if self.stdin in wlist:
- # When select has indicated that the file is writable,
- # we can write up to PIPE_BUF bytes without risk
- # blocking. POSIX defines PIPE_BUF >= 512
- chunk = input[input_offset : input_offset + 512]
+ chunk = input[input_offset : input_offset + _PIPE_BUF]
bytes_written = os.write(self.stdin.fileno(), chunk)
input_offset += bytes_written
if input_offset >= len(input):
@@ -1239,25 +1317,9 @@ class Popen(object):
read_set.remove(self.stderr)
stderr.append(data)
- # All data exchanged. Translate lists into strings.
- if stdout is not None:
- stdout = ''.join(stdout)
- if stderr is not None:
- stderr = ''.join(stderr)
-
- # Translate newlines, if requested. We cannot let the file
- # object do the translation: It is based on stdio, which is
- # impossible to combine with select (unless forcing no
- # buffering).
- if self.universal_newlines and hasattr(file, 'newlines'):
- if stdout:
- stdout = self._translate_newlines(stdout)
- if stderr:
- stderr = self._translate_newlines(stderr)
-
- self.wait()
return (stdout, stderr)
+
def send_signal(self, sig):
"""Send a signal to the process
"""
diff -up Python-2.6.6/Lib/test/test_subprocess.py.subprocess-poll Python-2.6.6/Lib/test/test_subprocess.py
--- Python-2.6.6/Lib/test/test_subprocess.py.subprocess-poll 2010-08-10 20:19:53.000000000 -0400
+++ Python-2.6.6/Lib/test/test_subprocess.py 2011-01-11 12:50:05.783279437 -0500
@@ -785,9 +785,23 @@ class HelperFunctionTests(unittest.TestC
test_eintr_retry_call = _test_eintr_retry_call
+unit_tests = [ProcessTestCase,
+ HelperFunctionTests]
+
+if getattr(subprocess, '_has_poll', False):
+ class ProcessTestCaseNoPoll(ProcessTestCase):
+ def setUp(self):
+ subprocess._has_poll = False
+ ProcessTestCase.setUp(self)
+
+ def tearDown(self):
+ subprocess._has_poll = True
+ ProcessTestCase.tearDown(self)
+
+ unit_tests.append(ProcessTestCaseNoPoll)
+
def test_main():
- test_support.run_unittest(ProcessTestCase,
- HelperFunctionTests)
+ test_support.run_unittest(*unit_tests)
if hasattr(test_support, "reap_children"):
test_support.reap_children()
diff -up Python-2.6.6/Modules/selectmodule.c.subprocess-poll Python-2.6.6/Modules/selectmodule.c
--- Python-2.6.6/Modules/selectmodule.c.subprocess-poll 2011-01-11 12:52:19.913294507 -0500
+++ Python-2.6.6/Modules/selectmodule.c 2011-01-11 12:51:54.105528147 -0500
@@ -1737,6 +1737,10 @@ initselect(void)
Py_INCREF(SelectError);
PyModule_AddObject(m, "error", SelectError);
+#ifdef PIPE_BUF
+ PyModule_AddIntConstant(m, "PIPE_BUF", PIPE_BUF);
+#endif
+
#if defined(HAVE_POLL)
#ifdef __APPLE__
if (select_have_broken_poll()) {