diff options
author | Sergey Shepelev <temotor@gmail.com> | 2015-09-13 13:11:32 +0300 |
---|---|---|
committer | Sergey Shepelev <temotor@gmail.com> | 2015-09-16 01:55:32 +0300 |
commit | 5a4a7ead2435025e2c727c902d7e985be0204417 (patch) | |
tree | f48dbb99c518cc25cb549e57dcb287d3fc94eb89 | |
parent | 8482499f135d2bbe944884e5831534ba1476f559 (diff) | |
download | eventlet-5a4a7ead2435025e2c727c902d7e985be0204417.tar.gz |
subprocess: support universal_newlines
https://github.com/eventlet/eventlet/issues/243
-rw-r--r-- | eventlet/green/subprocess.py | 15 | ||||
-rw-r--r-- | 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) |