Skip to content

Commit 975365c

Browse files
committed
Adjust e3.os.process.Run attributes out, err, raw_out, raw_err
Ensure that those attributes are never set to None. In case there is no output or error just use the default empty bytes/str. This simplify typing eng/shared/anod#618
1 parent 764ad6a commit 975365c

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

src/e3/mock/os/process.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ def __init__(
6060
self,
6161
cmd: list[str],
6262
status: int | None = None,
63-
raw_out: bytes | None = None,
64-
raw_err: bytes | None = None,
63+
raw_out: bytes = b"",
64+
raw_err: bytes = b"",
6565
) -> None:
6666
"""Initialize CommandResult.
6767
@@ -72,8 +72,8 @@ def __init__(
7272
"""
7373
self.cmd = cmd
7474
self.status = status if status is not None else 0
75-
self.raw_out = raw_out if raw_out is not None else b""
76-
self.raw_err = raw_err if raw_err is not None else b""
75+
self.raw_out = raw_out
76+
self.raw_err = raw_err
7777

7878
def check(self, cmd: list[str]) -> None:
7979
"""Check that cmd matches the expected arguments.
@@ -140,8 +140,8 @@ def __call__(self, cmds: AnyCmdLine, *args: Any, **kwargs: Any) -> Run:
140140
:param kwargs: unhandled keyword arguments
141141
"""
142142
self.status: int | None = None
143-
self.raw_out: bytes | None = b""
144-
self.raw_err: bytes | None = b""
143+
self.raw_out: bytes = b""
144+
self.raw_err: bytes = b""
145145

146146
cmds = to_cmd_lines(cmds)
147147
results: list[CommandResult] | None = self.config.get("results")

src/e3/os/process.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,8 @@ def add_interpreter_command(cmd_line: CmdLine) -> CmdLine:
366366
self.error_file = File(error, "w")
367367

368368
self.status: int | None = None
369-
self.raw_out: bytes | None = b""
370-
self.raw_err: bytes | None = b""
369+
self.raw_out: bytes = b""
370+
self.raw_err: bytes = b""
371371
self.cmds = []
372372

373373
if env is not None:
@@ -488,25 +488,23 @@ def add_interpreter_command(cmd_line: CmdLine) -> CmdLine:
488488
self.wait()
489489

490490
@property
491-
def out(self) -> str | None:
491+
def out(self) -> str:
492492
"""Process output as string.
493493
494494
Attempt is done to decode as utf-8 the output. If the output is not in
495495
utf-8 a string representation will be returned
496496
(see e3.text.bytes_as_str).
497497
"""
498-
return bytes_as_str(self.raw_out) if self.raw_out is not None else None
498+
return bytes_as_str(self.raw_out)
499499

500500
@property
501-
def err(self) -> str | None:
501+
def err(self) -> str:
502502
"""Process error as string.
503503
504504
Attempt is done to decode as utf-8 the output. If the output is not in
505505
utf-8 a string representation will be returned
506506
(see e3.text.bytes_as_str).
507507
"""
508-
if self.raw_err is None:
509-
return None
510508
return bytes_as_str(self.raw_err)
511509

512510
def command_line_image(self) -> str:
@@ -569,7 +567,10 @@ def wait(self) -> int:
569567
if isinstance(tmp_input, str):
570568
tmp_input = tmp_input.encode("utf-8")
571569

572-
(self.raw_out, self.raw_err) = self.internal.communicate(tmp_input)
570+
(raw_out, raw_err) = self.internal.communicate(tmp_input)
571+
self.raw_out = raw_out if raw_out is not None else b""
572+
self.raw_err = raw_err if raw_err is not None else b""
573+
573574
self.status = self.internal.returncode
574575

575576
self.close_files()

tests/tests_e3/os/process/main_test.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import pytest
1919

20-
from subprocess import STDOUT
20+
from subprocess import STDOUT, PIPE
2121

2222
try:
2323
import psutil
@@ -561,5 +561,26 @@ def test_shell_override():
561561

562562

563563
def test_error_to_stdout():
564-
p = e3.os.process.Run(["echo", "1"], error=STDOUT)
565-
assert p.err is None
564+
"""Check that redirection of stderr to stdout works."""
565+
python_cmd = ";".join(
566+
[
567+
"import sys; sys.stderr.write('hello_from_stderr')",
568+
"sys.stderr.flush()",
569+
"sys.stdout.write('hello_from_stdout')",
570+
]
571+
)
572+
573+
# Test merge of STDERR in STDOUT
574+
p = e3.os.process.Run([sys.executable, "-c", python_cmd], error=STDOUT)
575+
assert p.out == "hello_from_stderrhello_from_stdout"
576+
assert p.err == ""
577+
578+
# This is in fact the default behavior
579+
p = e3.os.process.Run([sys.executable, "-c", python_cmd])
580+
assert p.out == "hello_from_stderrhello_from_stdout"
581+
assert p.err == ""
582+
583+
# Check when the STDERR and STDOUT are sepearate pipes.
584+
p = e3.os.process.Run([sys.executable, "-c", python_cmd], error=PIPE)
585+
assert p.out == "hello_from_stdout"
586+
assert p.err == "hello_from_stderr"

0 commit comments

Comments
 (0)