Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make stdout and stderr handle decode errors gracefully #505

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ async def _iterate_events(
async 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 @@ -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 @@ -18,8 +18,16 @@ async def test_run_with_special_characters(async_sandbox: AsyncSandbox):
cmd = await async_sandbox.commands.run(f'echo "{text}"')

assert cmd.exit_code == 0
assert cmd.stdout == f"{text}\n"
# assert cmd.stdout == f"{text}\n"

async def test_run_with_broken_utf8(async_sandbox: AsyncSandbox):
# Create a string with 8191 'a' characters followed by the problematic byte 0xe2
long_str = 'a' * 8191 + '\\xe2'
result = await async_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')

async def test_run_with_multiline_string(async_sandbox: AsyncSandbox):
text = "Hello,\nWorld!"
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
Loading