Skip to content

Commit

Permalink
make stdout and stderr decode handle errors by replacing with unicode…
Browse files Browse the repository at this point in the history
… replacement char; add tests
  • Loading branch information
0div committed Dec 12, 2024
1 parent a72b361 commit 3c52522
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ def _handle_events(
for event in self._events:
if event.event.HasField("data"):
if event.event.data.stdout:
out = event.event.data.stdout.decode()
out = event.event.data.stdout.decode('utf-8', 'replace')
self._stdout += out
yield out, None, None
if event.event.data.stderr:
out = event.event.data.stderr.decode()
out = event.event.data.stderr.decode('utf-8', 'replace')
self._stderr += out
yield None, out, None
if event.event.data.pty:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ def test_run_with_special_characters(sandbox: Sandbox):
assert cmd.exit_code == 0
assert cmd.stdout == f"{text}\n"

def test_run_with_broken_utf8(sandbox: Sandbox):
# Create a string with 8191 'a' characters followed by the problematic byte 0xe2
long_str = 'a' * 8191 + '\\xe2'
result = sandbox.commands.run(f'printf "{long_str}"')
assert result.exit_code == 0

# The broken UTF-8 bytes should be replaced with the Unicode replacement character
assert result.stdout == ('a' * 8191 + '\ufffd')

def test_run_with_multiline_string(sandbox):
text = "Hello,\nWorld!"
Expand Down

0 comments on commit 3c52522

Please sign in to comment.