Skip to content

Commit 8fbffef

Browse files
authored
Resolve outstanding resource warnings when running tests (#99)
1 parent 66ffe9c commit 8fbffef

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

osrf_pycommon/process_utils/execute_process_nopty.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,14 @@ def yield_to_stream(data, stream):
130130

131131

132132
def _execute_process_nopty(cmd, cwd, env, shell, stderr_to_stdout=True):
133-
if stderr_to_stdout:
134-
p = Popen(cmd,
135-
stdin=PIPE, stdout=PIPE, stderr=STDOUT,
136-
cwd=cwd, env=env, shell=shell, close_fds=False)
137-
else:
138-
p = Popen(cmd,
139-
stdin=PIPE, stdout=PIPE, stderr=PIPE,
140-
cwd=cwd, env=env, shell=shell, close_fds=False)
141-
142-
# Left over data from read which isn't a complete line yet
143-
left_overs = {p.stdout: b'', p.stderr: b''}
133+
stderr = STDOUT if stderr_to_stdout else PIPE
134+
with Popen(
135+
cmd, stdin=PIPE, stdout=PIPE, stderr=stderr,
136+
cwd=cwd, env=env, shell=shell, close_fds=False
137+
) as p:
138+
# Left over data from read which isn't a complete line yet
139+
left_overs = {p.stdout: b'', p.stderr: b''}
144140

145-
fds = list(filter(None, [p.stdout, p.stderr]))
141+
fds = list(filter(None, [p.stdout, p.stderr]))
146142

147-
return _yield_data(p, fds, left_overs, os.linesep)
143+
yield from _yield_data(p, fds, left_overs, os.linesep)

tests/unit/test_process_utils/impl_aep_asyncio.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ async def run(cmd, **kwargs):
1010
transport, protocol = await async_execute_process(
1111
create_protocol(), cmd, **kwargs)
1212
retcode = await protocol.complete
13+
transport.close()
1314
return protocol.stdout_buffer, protocol.stderr_buffer, retcode

0 commit comments

Comments
 (0)