]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Avoid O(N**2) bottleneck in _communicate_(). Fixes #1598181. Backport from rev. 53295.
authorPeter Astrand <astrand@lysator.liu.se>
Sun, 21 Jan 2007 15:45:25 +0000 (15:45 +0000)
committerPeter Astrand <astrand@lysator.liu.se>
Sun, 21 Jan 2007 15:45:25 +0000 (15:45 +0000)
Lib/subprocess.py
Misc/NEWS

index eb1a7356719e9fa2d1347bd2ac691c47fba7b8a0..8b25c2fe85c0c45f1b37e4742d64de79f99ebb55 100644 (file)
@@ -1111,6 +1111,7 @@ class Popen(object):
                 read_set.append(self.stderr)
                 stderr = []
 
+            input_offset = 0
             while read_set or write_set:
                 rlist, wlist, xlist = select.select(read_set, write_set, [])
 
@@ -1118,9 +1119,9 @@ class Popen(object):
                     # 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
-                    bytes_written = os.write(self.stdin.fileno(), input[:512])
-                    input = input[bytes_written:]
-                    if not input:
+                    bytes_written = os.write(self.stdin.fileno(), buffer(input, input_offset, 512))
+                    input_offset += bytes_written 
+                    if input_offset >= len(input):
                         self.stdin.close()
                         write_set.remove(self.stdin)
 
index 3a281c4de91b9fd438b7451c732b0290e9f444a9..eeab829d0f332ae298537d74a48fd3a61fbfd110 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -143,6 +143,8 @@ Extension Modules
 Library
 -------
 
+- Bug #1598181: Avoid O(N**2) bottleneck in subprocess communicate(). 
+
 - Patch #1627441: close sockets properly in urllib2.
 
 - Bug #1610795: ctypes.util.find_library works now on BSD systems.