summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2011-01-03 18:36:36 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2011-01-03 18:36:36 (GMT)
commitf50a6b6b6d5c7b36e7fbe49266edd3083a23cbb9 (patch)
tree722051f4f6c38f4df014b81126ddb9c7f881433a /Lib/test
parent904fe042f3750df886e6a86aa660c1f047c9d0c5 (diff)
downloadcpython-f50a6b6b6d5c7b36e7fbe49266edd3083a23cbb9.zip
cpython-f50a6b6b6d5c7b36e7fbe49266edd3083a23cbb9.tar.gz
cpython-f50a6b6b6d5c7b36e7fbe49266edd3083a23cbb9.tar.bz2
Merged revisions 87695 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r87695 | antoine.pitrou | 2011-01-03 19:23:55 +0100 (lun., 03 janv. 2011) | 5 lines Issue #10806, issue #9905: Fix subprocess pipes when some of the standard file descriptors (0, 1, 2) are closed in the parent process. Initial patch by Ross Lagerwall. ........
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/support.py11
-rw-r--r--Lib/test/test_subprocess.py56
2 files changed, 67 insertions, 0 deletions
diff --git a/Lib/test/support.py b/Lib/test/support.py
index 842cda6..3353053 100644
--- a/Lib/test/support.py
+++ b/Lib/test/support.py
@@ -10,6 +10,7 @@ import gc
import socket
import sys
import os
+import re
import platform
import shutil
import warnings
@@ -1056,3 +1057,13 @@ def reap_children():
break
except:
break
+
+def strip_python_stderr(stderr):
+ """Strip the stderr of a Python process from potential debug output
+ emitted by the interpreter.
+
+ This will typically be run on the result of the communicate() method
+ of a subprocess.Popen object.
+ """
+ stderr = re.sub(br"\[\d+ refs\]\r?\n?$", b"", stderr).strip()
+ return stderr
diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py
index a633af5..ce3e0c2 100644
--- a/Lib/test/test_subprocess.py
+++ b/Lib/test/test_subprocess.py
@@ -804,6 +804,62 @@ class ProcessTestCase(BaseTestCase):
" non-zero with this error:\n%s" %
stderr.decode('utf8'))
+ def check_close_std_fds(self, fds):
+ # Issue #9905: test that subprocess pipes still work properly with
+ # some standard fds closed
+ stdin = 0
+ newfds = []
+ for a in fds:
+ b = os.dup(a)
+ newfds.append(b)
+ if a == 0:
+ stdin = b
+ try:
+ for fd in fds:
+ os.close(fd)
+ out, err = subprocess.Popen([sys.executable, "-c",
+ 'import sys;'
+ 'sys.stdout.write("apple");'
+ 'sys.stdout.flush();'
+ 'sys.stderr.write("orange")'],
+ stdin=stdin,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE).communicate()
+ err = support.strip_python_stderr(err)
+ self.assertEqual((out, err), (b'apple', b'orange'))
+ finally:
+ for b, a in zip(newfds, fds):
+ os.dup2(b, a)
+ for b in newfds:
+ os.close(b)
+
+ def test_close_fd_0(self):
+ self.check_close_std_fds([0])
+
+ def test_close_fd_1(self):
+ self.check_close_std_fds([1])
+
+ def test_close_fd_2(self):
+ self.check_close_std_fds([2])
+
+ def test_close_fds_0_1(self):
+ self.check_close_std_fds([0, 1])
+
+ def test_close_fds_0_2(self):
+ self.check_close_std_fds([0, 2])
+
+ def test_close_fds_1_2(self):
+ self.check_close_std_fds([1, 2])
+
+ def test_close_fds_0_1_2(self):
+ # Issue #10806: test that subprocess pipes still work properly with
+ # all standard fds closed.
+ self.check_close_std_fds([0, 1, 2])
+
+ def test_surrogates_error_message(self):
+ def prepare():
+ raise ValueError("surrogate:\uDCff")
+
#
# Windows tests