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

Avoid UnicodeDecodeError from command output #2970

Merged
merged 2 commits into from
Apr 5, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/changelog/2969.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Instead of raising ``UnicodeDecodeError`` when command output includes non-utf-8 bytes,
``tox`` will now use ``surrogateescape`` error handling to convert the unrecognized bytes
to escape sequences according to :pep:`383` - by :user:`masenf`.
2 changes: 1 addition & 1 deletion src/tox/execute/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def colored(self) -> Iterator[None]:
@property
def text(self) -> str:
with self._content_lock:
return self._content.decode("utf-8")
return self._content.decode("utf-8", errors="surrogateescape")

@property
def content(self) -> bytearray:
Expand Down
6 changes: 6 additions & 0 deletions tests/execute/test_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@
def test_sync_write_repr() -> None:
sync_write = SyncWrite(name="a", target=None, color=Fore.RED)
assert repr(sync_write) == f"SyncWrite(name='a', target=None, color={Fore.RED!r})"


def test_sync_write_decode_surrogate() -> None:
sync_write = SyncWrite(name="a", target=None)
sync_write.handler(b"\xed\n")
assert sync_write.text == "\udced\n"