diff options
author | Georg Brandl <georg@python.org> | 2009-08-13 09:04:31 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2009-08-13 09:04:31 (GMT) |
commit | ae83d6ee371b5e6aeebe303fb853f5c45638414c (patch) | |
tree | 6ce42b81661f4d7432af393ba303fca20884650b /Lib/subprocess.py | |
parent | 3deeed9a3250dfa1eab6b03db357ce188d512f64 (diff) | |
download | cpython-ae83d6ee371b5e6aeebe303fb853f5c45638414c.zip cpython-ae83d6ee371b5e6aeebe303fb853f5c45638414c.tar.gz cpython-ae83d6ee371b5e6aeebe303fb853f5c45638414c.tar.bz2 |
Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
svn+ssh://svn.python.org/python/branches/py3k
................
r73833 | gregory.p.smith | 2009-07-04 04:46:54 +0200 (Sa, 04 Jul 2009) | 20 lines
Merged revisions 73825-73826 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r73825 | gregory.p.smith | 2009-07-03 18:49:29 -0700 (Fri, 03 Jul 2009) | 9 lines
Use select.poll() in subprocess, when available, rather than select() so that
it does not fail when file descriptors are large. Fixes issue3392.
Patch largely contributed by Frank Chu (fpmc) with some improvements by me.
See http://bugs.python.org/issue3392.
........
r73826 | gregory.p.smith | 2009-07-03 18:55:11 -0700 (Fri, 03 Jul 2009) | 2 lines
news entry for r73825
........
Candidate for backporting to release31-maint as it is a bug fix and changes no
public API.
................
r73838 | gregory.p.smith | 2009-07-04 10:32:15 +0200 (Sa, 04 Jul 2009) | 2 lines
change deprecated unittest method calls into their proper names.
................
r73850 | alexandre.vassalotti | 2009-07-05 07:38:18 +0200 (So, 05 Jul 2009) | 3 lines
Issue 4509: Do not modify an array if we know the change would result
in a failure due to exported buffers.
................
r73851 | alexandre.vassalotti | 2009-07-05 07:47:28 +0200 (So, 05 Jul 2009) | 2 lines
Add more test cases to BaseTest.test_memoryview_no_resize.
................
r73852 | alexandre.vassalotti | 2009-07-05 08:25:14 +0200 (So, 05 Jul 2009) | 5 lines
Fix array.extend and array.__iadd__ to handle the case where an array
is extended with itself.
This bug is specific the py3k version of arraymodule.c
................
r73856 | alexandre.vassalotti | 2009-07-05 08:42:44 +0200 (So, 05 Jul 2009) | 6 lines
Issue 4005: Remove .sort() call on dict_keys object.
This caused pydoc to fail when there was a zip file in sys.path.
Patch contributed by Amaury Forgeot d'Arc.
................
r73857 | alexandre.vassalotti | 2009-07-05 08:50:08 +0200 (So, 05 Jul 2009) | 2 lines
Add NEWS entries for the changes I made recently.
................
Diffstat (limited to 'Lib/subprocess.py')
-rw-r--r-- | Lib/subprocess.py | 129 |
1 files changed, 100 insertions, 29 deletions
diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 368ba0e..f7361e1 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -371,6 +371,7 @@ if mswindows: error = IOError else: import select + _has_poll = hasattr(select, 'poll') import errno import fcntl import pickle @@ -383,6 +384,11 @@ try: except: MAXFD = 256 +# 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) + _active = [] def _cleanup(): @@ -1173,19 +1179,103 @@ 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 = b''.join(stdout) + if stderr is not None: + stderr = b''.join(stderr) + + # Translate newlines, if requested. + # This also turns bytes into strings. + if self.universal_newlines: + if stdout is not None: + stdout = self._translate_newlines(stdout, + self.stdout.encoding) + if stderr is not None: + stderr = self._translate_newlines(stderr, + self.stderr.encoding) + + 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 as e: + if e.args[0] == errno.EINTR: + continue + raise + + # XXX Rewrite these to use non-blocking I/O on the + # file objects; they are no longer using C stdio! + + 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 = [] @@ -1206,10 +1296,7 @@ class Popen(object): # file objects; they are no longer using C stdio! 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): @@ -1230,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 = b"".join(stdout) - if stderr is not None: - stderr = b"".join(stderr) - - # Translate newlines, if requested. - # This also turns bytes into strings. - if self.universal_newlines: - if stdout is not None: - stdout = self._translate_newlines(stdout, - self.stdout.encoding) - if stderr is not None: - stderr = self._translate_newlines(stderr, - self.stderr.encoding) - - self.wait() return (stdout, stderr) + def send_signal(self, sig): """Send a signal to the process """ |