diff --git a/osrf_pycommon/process_utils/execute_process_nopty.py b/osrf_pycommon/process_utils/execute_process_nopty.py index fd22aee..65a7ec1 100644 --- a/osrf_pycommon/process_utils/execute_process_nopty.py +++ b/osrf_pycommon/process_utils/execute_process_nopty.py @@ -130,18 +130,14 @@ def yield_to_stream(data, stream): def _execute_process_nopty(cmd, cwd, env, shell, stderr_to_stdout=True): - if stderr_to_stdout: - p = Popen(cmd, - stdin=PIPE, stdout=PIPE, stderr=STDOUT, - cwd=cwd, env=env, shell=shell, close_fds=False) - else: - p = Popen(cmd, - stdin=PIPE, stdout=PIPE, stderr=PIPE, - cwd=cwd, env=env, shell=shell, close_fds=False) - - # Left over data from read which isn't a complete line yet - left_overs = {p.stdout: b'', p.stderr: b''} + stderr = STDOUT if stderr_to_stdout else PIPE + with Popen( + cmd, stdin=PIPE, stdout=PIPE, stderr=stderr, + cwd=cwd, env=env, shell=shell, close_fds=False + ) as p: + # Left over data from read which isn't a complete line yet + left_overs = {p.stdout: b'', p.stderr: b''} - fds = list(filter(None, [p.stdout, p.stderr])) + fds = list(filter(None, [p.stdout, p.stderr])) - return _yield_data(p, fds, left_overs, os.linesep) + yield from _yield_data(p, fds, left_overs, os.linesep) diff --git a/tests/unit/test_process_utils/impl_aep_asyncio.py b/tests/unit/test_process_utils/impl_aep_asyncio.py index a478304..b3ab108 100644 --- a/tests/unit/test_process_utils/impl_aep_asyncio.py +++ b/tests/unit/test_process_utils/impl_aep_asyncio.py @@ -10,4 +10,5 @@ async def run(cmd, **kwargs): transport, protocol = await async_execute_process( create_protocol(), cmd, **kwargs) retcode = await protocol.complete + transport.close() return protocol.stdout_buffer, protocol.stderr_buffer, retcode