summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_subprocess.py
diff options
context:
space:
mode:
authorSteve Dower <steve.dower@microsoft.com>2016-09-07 03:16:17 (GMT)
committerSteve Dower <steve.dower@microsoft.com>2016-09-07 03:16:17 (GMT)
commit050acaed9926aa0a0367ec1baebbfd797c33ad74 (patch)
treec72f32f184d3c8fe62d0100ffab7494f2f84365d /Lib/test/test_subprocess.py
parent173a1f3dc7a8b43b49e3ef483714c1d51b60d868 (diff)
downloadcpython-050acaed9926aa0a0367ec1baebbfd797c33ad74.zip
cpython-050acaed9926aa0a0367ec1baebbfd797c33ad74.tar.gz
cpython-050acaed9926aa0a0367ec1baebbfd797c33ad74.tar.bz2
Issue #6135: Adds encoding and errors parameters to subprocess
Diffstat (limited to 'Lib/test/test_subprocess.py')
-rw-r--r--Lib/test/test_subprocess.py59
1 files changed, 41 insertions, 18 deletions
diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py
index 0906606..2d9239c 100644
--- a/Lib/test/test_subprocess.py
+++ b/Lib/test/test_subprocess.py
@@ -894,31 +894,42 @@ class ProcessTestCase(BaseTestCase):
#
# UTF-16 and UTF-32-BE are sufficient to check both with BOM and
# without, and UTF-16 and UTF-32.
- import _bootlocale
for encoding in ['utf-16', 'utf-32-be']:
- old_getpreferredencoding = _bootlocale.getpreferredencoding
- # Indirectly via io.TextIOWrapper, Popen() defaults to
- # locale.getpreferredencoding(False) and earlier in Python 3.2 to
- # locale.getpreferredencoding().
- def getpreferredencoding(do_setlocale=True):
- return encoding
code = ("import sys; "
r"sys.stdout.buffer.write('1\r\n2\r3\n4'.encode('%s'))" %
encoding)
args = [sys.executable, '-c', code]
- try:
- _bootlocale.getpreferredencoding = getpreferredencoding
- # We set stdin to be non-None because, as of this writing,
- # a different code path is used when the number of pipes is
- # zero or one.
- popen = subprocess.Popen(args, universal_newlines=True,
- stdin=subprocess.PIPE,
- stdout=subprocess.PIPE)
- stdout, stderr = popen.communicate(input='')
- finally:
- _bootlocale.getpreferredencoding = old_getpreferredencoding
+ # We set stdin to be non-None because, as of this writing,
+ # a different code path is used when the number of pipes is
+ # zero or one.
+ popen = subprocess.Popen(args,
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ encoding=encoding)
+ stdout, stderr = popen.communicate(input='')
self.assertEqual(stdout, '1\n2\n3\n4')
+ def test_communicate_errors(self):
+ for errors, expected in [
+ ('ignore', ''),
+ ('replace', '\ufffd\ufffd'),
+ ('surrogateescape', '\udc80\udc80'),
+ ('backslashreplace', '\\x80\\x80'),
+ ]:
+ code = ("import sys; "
+ r"sys.stdout.buffer.write(b'[\x80\x80]')")
+ args = [sys.executable, '-c', code]
+ # We set stdin to be non-None because, as of this writing,
+ # a different code path is used when the number of pipes is
+ # zero or one.
+ popen = subprocess.Popen(args,
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ encoding='utf-8',
+ errors=errors)
+ stdout, stderr = popen.communicate(input='')
+ self.assertEqual(stdout, '[{}]'.format(expected))
+
def test_no_leaking(self):
# Make sure we leak no resources
if not mswindows:
@@ -2539,6 +2550,18 @@ class Win32ProcessTestCase(BaseTestCase):
with p:
self.assertIn(b"physalis", p.stdout.read())
+ def test_shell_encodings(self):
+ # Run command through the shell (string)
+ for enc in ['ansi', 'oem']:
+ newenv = os.environ.copy()
+ newenv["FRUIT"] = "physalis"
+ p = subprocess.Popen("set", shell=1,
+ stdout=subprocess.PIPE,
+ env=newenv,
+ encoding=enc)
+ with p:
+ self.assertIn("physalis", p.stdout.read(), enc)
+
def test_call_string(self):
# call() function with string argument on Windows
rc = subprocess.call(sys.executable +