aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/subprocess.py
diff options
context:
space:
mode:
authorThomas Wouters <thomas@python.org>2006-04-21 10:40:58 +0000
committerThomas Wouters <thomas@python.org>2006-04-21 10:40:58 +0000
commit49fd7fa4431da299196d74087df4a04f99f9c46f (patch)
tree35ace5fe78d3d52c7a9ab356ab9f6dbf8d4b71f4 /Lib/subprocess.py
parent9ada3d6e29d5165dadacbe6be07bcd35cfbef59d (diff)
downloadcpython-49fd7fa4431da299196d74087df4a04f99f9c46f.tar.gz
cpython-49fd7fa4431da299196d74087df4a04f99f9c46f.zip
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
number of tests, all because of the codecs/_multibytecodecs issue described here (it's not a Py3K issue, just something Py3K discovers): http://mail.python.org/pipermail/python-dev/2006-April/064051.html Hye-Shik Chang promised to look for a fix, so no need to fix it here. The tests that are expected to break are: test_codecencodings_cn test_codecencodings_hk test_codecencodings_jp test_codecencodings_kr test_codecencodings_tw test_codecs test_multibytecodec This merge fixes an actual test failure (test_weakref) in this branch, though, so I believe merging is the right thing to do anyway.
Diffstat (limited to 'Lib/subprocess.py')
-rw-r--r--Lib/subprocess.py36
1 files changed, 26 insertions, 10 deletions
diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index 6827244fdd1..3cd0df593b8 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -414,7 +414,13 @@ _active = []
def _cleanup():
for inst in _active[:]:
- inst.poll()
+ if inst.poll(_deadstate=sys.maxint) >= 0:
+ try:
+ _active.remove(inst)
+ except ValueError:
+ # This can happen if two threads create a new Popen instance.
+ # It's harmless that it was already removed, so ignore.
+ pass
PIPE = -1
STDOUT = -2
@@ -527,6 +533,7 @@ class Popen(object):
"""Create new Popen instance."""
_cleanup()
+ self._child_created = False
if not isinstance(bufsize, (int, long)):
raise TypeError("bufsize must be an integer")
@@ -592,14 +599,24 @@ class Popen(object):
else:
self.stderr = os.fdopen(errread, 'rb', bufsize)
- _active.append(self)
-
def _translate_newlines(self, data):
data = data.replace("\r\n", "\n")
data = data.replace("\r", "\n")
return data
+
+ def __del__(self):
+ if not self._child_created:
+ # We didn't get to successfully create a child process.
+ return
+ # In case the child hasn't been waited on, check if it's done.
+ self.poll(_deadstate=sys.maxint)
+ if self.returncode is None:
+ # Child is still running, keep us alive until we can wait on it.
+ _active.append(self)
+
+
def communicate(self, input=None):
"""Interact with process: Send data to stdin. Read data from
stdout and stderr, until end-of-file is reached. Wait for
@@ -777,6 +794,7 @@ class Popen(object):
raise WindowsError(*e.args)
# Retain the process handle, but close the thread handle
+ self._child_created = True
self._handle = hp
self.pid = pid
ht.Close()
@@ -795,13 +813,12 @@ class Popen(object):
errwrite.Close()
- def poll(self):
+ def poll(self, _deadstate=None):
"""Check if child process has terminated. Returns returncode
attribute."""
if self.returncode is None:
if WaitForSingleObject(self._handle, 0) == WAIT_OBJECT_0:
self.returncode = GetExitCodeProcess(self._handle)
- _active.remove(self)
return self.returncode
@@ -811,7 +828,6 @@ class Popen(object):
if self.returncode is None:
obj = WaitForSingleObject(self._handle, INFINITE)
self.returncode = GetExitCodeProcess(self._handle)
- _active.remove(self)
return self.returncode
@@ -958,6 +974,7 @@ class Popen(object):
self._set_cloexec_flag(errpipe_write)
self.pid = os.fork()
+ self._child_created = True
if self.pid == 0:
# Child
try:
@@ -1042,10 +1059,8 @@ class Popen(object):
# Should never happen
raise RuntimeError("Unknown child exit status!")
- _active.remove(self)
-
- def poll(self):
+ def poll(self, _deadstate=None):
"""Check if child process has terminated. Returns returncode
attribute."""
if self.returncode is None:
@@ -1054,7 +1069,8 @@ class Popen(object):
if pid == self.pid:
self._handle_exitstatus(sts)
except os.error:
- pass
+ if _deadstate is not None:
+ self.returncode = _deadstate
return self.returncode