Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/local_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ def returncode(self) -> typing.Optional[int]:
assert type(self._local_process) is subprocess.Popen
return self._local_process.poll()

def close(self) -> None:
self.__exit__(None, None, None)
return

def communicate(
self,
input: typing.Optional[T_OS_EXEC_INPUT] = None,
Expand Down
3 changes: 3 additions & 0 deletions src/os_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def stderr(self) -> typing.Optional[T_OS_IO]:
def returncode(self) -> typing.Optional[int]:
RaiseError.PropertyIsNotImplemented(__class__, "get_returncode")

def close(self) -> None:
RaiseError.MethodIsNotImplemented(__class__, "close")

T_COMMUNICATE_RESULT = typing.Union[
typing.Tuple[bytes, bytes],
typing.Tuple[str, str],
Expand Down
4 changes: 4 additions & 0 deletions src/remote_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ def stderr(self) -> typing.Optional[T_OS_IO]:
def returncode(self) -> typing.Optional[int]:
return self._poll()

def close(self) -> None:
self.__exit__(None, None, None)
return

def communicate(
self,
input: typing.Optional[T_OS_EXEC_INPUT] = None,
Expand Down
64 changes: 43 additions & 21 deletions tests/test_os_ops_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4199,17 +4199,22 @@ def LOCAL_f(data):
f.seek(0)
return f

# Yes, it it is not good. I know.
tmp_stdin = LOCAL_f(popen_data.expected_result)
tmp_stdin: typing.Optional[typing.IO[typing.Any]] = None

controller = os_ops.popen(
cmd,
text=popen_data.param_text,
encoding=popen_data.param_encoding,
stdin=tmp_stdin,
)
controller: typing.Optional[OsProcessController] = None

try:
tmp_stdin = LOCAL_f(
popen_data.expected_result,
)

controller = os_ops.popen(
cmd,
text=popen_data.param_text,
encoding=popen_data.param_encoding,
stdin=tmp_stdin,
)

with tmp_stdin, controller:
assert isinstance(controller, OsProcessController)

returncode = controller.wait()
Expand All @@ -4224,7 +4229,12 @@ def LOCAL_f(data):
assert controller.stderr is not None
x = controller.stderr.read()
assert len(x) == 0
pass
finally:
if controller is not None:
controller.close()

if tmp_stdin is not None:
tmp_stdin.close()

return

Expand Down Expand Up @@ -4355,18 +4365,21 @@ def LOCAL_f():
encoding=popen_data2.param_encoding,
)

# Yes, it it is not good. I know.
tmp_stderr = LOCAL_f()
tmp_stdout = LOCAL_f()
controller = os_ops.popen(
cmd,
text=popen_data2.param_text,
encoding=popen_data2.param_encoding,
stdout=tmp_stdout,
stderr=tmp_stderr,
)
tmp_stderr: typing.Optional[typing.IO[typing.Any]] = None
tmp_stdout: typing.Optional[typing.IO[typing.Any]] = None
controller: typing.Optional[OsProcessController] = None

try:
tmp_stderr = LOCAL_f()
tmp_stdout = LOCAL_f()
controller = os_ops.popen(
cmd,
text=popen_data2.param_text,
encoding=popen_data2.param_encoding,
stdout=tmp_stdout,
stderr=tmp_stderr,
)

with tmp_stderr, tmp_stdout, controller:
assert isinstance(controller, OsProcessController)
assert controller.wait() == 0
assert controller.returncode == 0
Expand All @@ -4386,6 +4399,15 @@ def LOCAL_f():
assert len(v2) > 0
logging.info("stderr: {!r}".format(v2))
assert v2 == popen_data2.expected_result2
finally:
if controller is not None:
controller.close()

if tmp_stdout is not None:
tmp_stdout.close()

if tmp_stderr is not None:
tmp_stderr.close()

return

Expand Down