From 96849ef941dde2cfaabba90b6ad503592d8017a3 Mon Sep 17 00:00:00 2001 From: Sergey Shepelev Date: Sun, 13 Sep 2015 13:11:32 +0300 Subject: [PATCH] subprocess: support universal_newlines https://github.com/eventlet/eventlet/issues/243 --- eventlet/green/subprocess.py | 15 +++++++++++++-- tests/subprocess_test.py | 9 +++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/eventlet/green/subprocess.py b/eventlet/green/subprocess.py index 7ce38cf..8a2cd50 100644 --- a/eventlet/green/subprocess.py +++ b/eventlet/green/subprocess.py @@ -55,8 +55,19 @@ class Popen(subprocess_orig.Popen): # eventlet.processes.Process.run() method. for attr in "stdin", "stdout", "stderr": pipe = getattr(self, attr) - if pipe is not None and not type(pipe) == greenio.GreenPipe: - wrapped_pipe = greenio.GreenPipe(pipe, pipe.mode, bufsize) + if pipe is not None and type(pipe) != greenio.GreenPipe: + # https://github.com/eventlet/eventlet/issues/243 + # AttributeError: '_io.TextIOWrapper' object has no attribute 'mode' + mode = getattr(pipe, 'mode', '') + if not mode: + if pipe.readable(): + mode += 'r' + if pipe.writable(): + mode += 'w' + # ValueError: can't have unbuffered text I/O + if bufsize == 0: + bufsize = -1 + wrapped_pipe = greenio.GreenPipe(pipe, mode, bufsize) setattr(self, attr, wrapped_pipe) __init__.__doc__ = subprocess_orig.Popen.__init__.__doc__ diff --git a/tests/subprocess_test.py b/tests/subprocess_test.py index 085f656..2220c87 100644 --- a/tests/subprocess_test.py +++ b/tests/subprocess_test.py @@ -64,3 +64,12 @@ def test_close_popen_stdin_with_close_fds(): p.stdin.close() except Exception as e: assert False, "Exception should not be raised, got %r instead" % e + + +def test_universal_lines(): + p = subprocess.Popen( + [sys.executable, '--version'], + shell=False, + stdout=subprocess.PIPE, + universal_newlines=True) + p.communicate(None)