Skip to content

Commit fa0054a

Browse files
committed
Make test_subprocess work. Fix universal newlines in io.py.
1 parent c126e8a commit fa0054a

File tree

3 files changed

+31
-39
lines changed

3 files changed

+31
-39
lines changed

Lib/io.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,14 +1114,6 @@ def seek(self, pos, whence=0):
11141114
self._decoder = decoder
11151115
return orig_pos
11161116

1117-
def _simplify(self, u):
1118-
# XXX Hack until str/unicode unification: return str instead
1119-
# of unicode if it's all ASCII
1120-
try:
1121-
return str(u)
1122-
except UnicodeEncodeError:
1123-
return u
1124-
11251117
def read(self, n=None):
11261118
if n is None:
11271119
n = -1
@@ -1131,15 +1123,15 @@ def read(self, n=None):
11311123
res += decoder.decode(self.buffer.read(), True)
11321124
self._pending = ""
11331125
self._snapshot = None
1134-
return self._simplify(res)
1126+
return res.replace("\r\n", "\n")
11351127
else:
11361128
while len(res) < n:
11371129
readahead, pending = self._read_chunk()
11381130
res += pending
11391131
if not readahead:
11401132
break
11411133
self._pending = res[n:]
1142-
return self._simplify(res[:n])
1134+
return res[:n].replace("\r\n", "\n")
11431135

11441136
def __next__(self):
11451137
self._telling = False
@@ -1155,9 +1147,9 @@ def readline(self, limit=None):
11551147
# XXX Hack to support limit argument, for backwards compatibility
11561148
line = self.readline()
11571149
if len(line) <= limit:
1158-
return self._simplify(line)
1150+
return line
11591151
line, self._pending = line[:limit], line[limit:] + self._pending
1160-
return self._simplify(line)
1152+
return line
11611153

11621154
line = self._pending
11631155
start = 0
@@ -1210,9 +1202,9 @@ def readline(self, limit=None):
12101202
# XXX Update self.newlines here if we want to support that
12111203

12121204
if self._fix_newlines and ending not in ("\n", ""):
1213-
return self._simplify(line[:endpos] + "\n")
1205+
return line[:endpos] + "\n"
12141206
else:
1215-
return self._simplify(line[:nextpos])
1207+
return line[:nextpos]
12161208

12171209

12181210
class StringIO(TextIOWrapper):

Lib/subprocess.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ class Popen(args, bufsize=0, executable=None,
286286
import sys
287287
mswindows = (sys.platform == "win32")
288288

289+
import io
289290
import os
290291
import traceback
291292

@@ -542,23 +543,23 @@ def __init__(self, args, bufsize=0, executable=None,
542543
if bufsize == 0:
543544
bufsize = 1 # Nearly unbuffered (XXX for now)
544545
if p2cwrite is not None:
545-
self.stdin = os.fdopen(p2cwrite, 'wb', bufsize)
546+
self.stdin = io.open(p2cwrite, 'wb', bufsize)
547+
if self.universal_newlines:
548+
self.stdin = io.TextIOWrapper(self.stdin)
546549
if c2pread is not None:
550+
self.stdout = io.open(c2pread, 'rb', bufsize)
547551
if universal_newlines:
548-
self.stdout = os.fdopen(c2pread, 'rU', bufsize)
549-
else:
550-
self.stdout = os.fdopen(c2pread, 'rb', bufsize)
552+
self.stdout = io.TextIOWrapper(self.stdout)
551553
if errread is not None:
554+
self.stderr = io.open(errread, 'rb', bufsize)
552555
if universal_newlines:
553-
self.stderr = os.fdopen(errread, 'rU', bufsize)
554-
else:
555-
self.stderr = os.fdopen(errread, 'rb', bufsize)
556+
self.stderr = io.TextIOWrapper(self.stderr)
556557

557558

558559
def _translate_newlines(self, data):
559560
data = data.replace("\r\n", "\n")
560561
data = data.replace("\r", "\n")
561-
return data
562+
return str(data)
562563

563564

564565
def __del__(self, sys=sys):
@@ -833,9 +834,9 @@ def _communicate(self, input):
833834
# impossible to combine with select (unless forcing no
834835
# buffering).
835836
if self.universal_newlines:
836-
if stdout:
837+
if stdout is not None:
837838
stdout = self._translate_newlines(stdout)
838-
if stderr:
839+
if stderr is not None:
839840
stderr = self._translate_newlines(stderr)
840841

841842
self.wait()
@@ -1009,7 +1010,6 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
10091010
if data:
10101011
os.waitpid(self.pid, 0)
10111012
child_exception = pickle.loads(data)
1012-
print("exc:", child_exception)
10131013
raise child_exception
10141014

10151015

@@ -1108,9 +1108,9 @@ def _communicate(self, input):
11081108
# impossible to combine with select (unless forcing no
11091109
# buffering).
11101110
if self.universal_newlines:
1111-
if stdout:
1111+
if stdout is not None:
11121112
stdout = self._translate_newlines(stdout)
1113-
if stderr:
1113+
if stderr is not None:
11141114
stderr = self._translate_newlines(stderr)
11151115

11161116
self.wait()

Lib/test/test_subprocess.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def test_stdout_pipe(self):
151151
p = subprocess.Popen([sys.executable, "-c",
152152
'import sys; sys.stdout.write("orange")'],
153153
stdout=subprocess.PIPE)
154-
self.assertEqual(p.stdout.read(), "orange")
154+
self.assertEqual(p.stdout.read(), b"orange")
155155

156156
def test_stdout_filedes(self):
157157
# stdout is set to open file descriptor
@@ -172,7 +172,7 @@ def test_stdout_fileobj(self):
172172
stdout=tf)
173173
p.wait()
174174
tf.seek(0)
175-
self.assertEqual(tf.read(), "orange")
175+
self.assertEqual(tf.read(), b"orange")
176176

177177
def test_stderr_pipe(self):
178178
# stderr redirection
@@ -264,7 +264,7 @@ def test_env(self):
264264
'sys.stdout.write(os.getenv("FRUIT"))'],
265265
stdout=subprocess.PIPE,
266266
env=newenv)
267-
self.assertEqual(p.stdout.read(), "orange")
267+
self.assertEqual(p.stdout.read(), b"orange")
268268

269269
def test_communicate_stdin(self):
270270
p = subprocess.Popen([sys.executable, "-c",
@@ -278,7 +278,7 @@ def test_communicate_stdout(self):
278278
'import sys; sys.stdout.write("pineapple")'],
279279
stdout=subprocess.PIPE)
280280
(stdout, stderr) = p.communicate()
281-
self.assertEqual(stdout, "pineapple")
281+
self.assertEqual(stdout, b"pineapple")
282282
self.assertEqual(stderr, None)
283283

284284
def test_communicate_stderr(self):
@@ -353,7 +353,7 @@ def test_universal_newlines(self):
353353
'import sys,os;' + SETBINARY +
354354
'sys.stdout.write("line1\\n");'
355355
'sys.stdout.flush();'
356-
'sys.stdout.write("line2\\r");'
356+
'sys.stdout.write("line2\\n");'
357357
'sys.stdout.flush();'
358358
'sys.stdout.write("line3\\r\\n");'
359359
'sys.stdout.flush();'
@@ -373,7 +373,7 @@ def test_universal_newlines_communicate(self):
373373
'import sys,os;' + SETBINARY +
374374
'sys.stdout.write("line1\\n");'
375375
'sys.stdout.flush();'
376-
'sys.stdout.write("line2\\r");'
376+
'sys.stdout.write("line2\\n");'
377377
'sys.stdout.flush();'
378378
'sys.stdout.write("line3\\r\\n");'
379379
'sys.stdout.flush();'
@@ -385,7 +385,7 @@ def test_universal_newlines_communicate(self):
385385
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
386386
universal_newlines=1)
387387
(stdout, stderr) = p.communicate()
388-
self.assertEqual(stdout, b"line1\nline2\nline3\nline4\nline5\nline6")
388+
self.assertEqual(stdout, "line1\nline2\nline3\nline4\nline5\nline6")
389389

390390
def test_no_leaking(self):
391391
# Make sure we leak no resources
@@ -460,10 +460,10 @@ def test_invalid_bufsize(self):
460460
#
461461
if not mswindows:
462462
def test_exceptions(self):
463-
# catched & re-raised exceptions
463+
# caught & re-raised exceptions
464464
try:
465465
p = subprocess.Popen([sys.executable, "-c", ""],
466-
cwd="/this/path/does/not/exist")
466+
cwd="/this/path/does/not/exist")
467467
except OSError as e:
468468
# The attribute child_traceback should contain "os.chdir"
469469
# somewhere.
@@ -511,7 +511,7 @@ def test_preexec(self):
511511
'sys.stdout.write(os.getenv("FRUIT"))'],
512512
stdout=subprocess.PIPE,
513513
preexec_fn=lambda: os.putenv("FRUIT", "apple"))
514-
self.assertEqual(p.stdout.read(), "apple")
514+
self.assertEqual(p.stdout.read(), b"apple")
515515

516516
def test_args_string(self):
517517
# args is a string
@@ -544,7 +544,7 @@ def test_shell_sequence(self):
544544
p = subprocess.Popen(["echo $FRUIT"], shell=1,
545545
stdout=subprocess.PIPE,
546546
env=newenv)
547-
self.assertEqual(p.stdout.read().strip(), "apple")
547+
self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
548548

549549
def test_shell_string(self):
550550
# Run command through the shell (string)
@@ -553,7 +553,7 @@ def test_shell_string(self):
553553
p = subprocess.Popen("echo $FRUIT", shell=1,
554554
stdout=subprocess.PIPE,
555555
env=newenv)
556-
self.assertEqual(p.stdout.read().strip(), "apple")
556+
self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
557557

558558
def test_call_string(self):
559559
# call() function with string argument on UNIX

0 commit comments

Comments
 (0)