Skip to content

Commit 15519e2

Browse files
authored
don't attempt to UTF-8-decode binary values (#98)
Treat all binary values the same: render them as hex literals. The existing logic allows some binary values to be treated differently than others.
1 parent d601dcd commit 15519e2

File tree

4 files changed

+10
-13
lines changed

4 files changed

+10
-13
lines changed

CHANGELOG

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Version 2.8.1
4+
5+
(released on 2026-01-24)
6+
7+
- Don't attempt to UTF-8-decode binary values.
8+
39
## Version 2.8.0
410

511
(released on 2026-01-24)

cli_helpers/utils.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,7 @@ def bytes_to_string(b):
2121
2222
"""
2323
if isinstance(b, binary_type):
24-
needs_hex = False
25-
try:
26-
result = b.decode("utf8")
27-
needs_hex = not result.isprintable()
28-
except UnicodeDecodeError:
29-
needs_hex = True
30-
if needs_hex:
31-
return "0x" + binascii.hexlify(b).decode("ascii")
32-
else:
33-
return result
24+
return "0x" + binascii.hexlify(b).decode("ascii")
3425
return b
3526

3627

tests/tabular_output/test_preprocessors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_bytes_to_string():
8484
"""Test the bytes_to_string() function."""
8585
data = [[1, "John"], [2, b"Jill"]]
8686
headers = [0, "name"]
87-
expected = ([[1, "John"], [2, "Jill"]], [0, "name"])
87+
expected = ([[1, "John"], [2, "0x4a696c6c"]], [0, "name"])
8888
results = bytes_to_string(data, headers)
8989

9090
assert expected == (list(results[0]), results[1])

tests/test_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def test_bytes_to_string_hexlify():
1313

1414
def test_bytes_to_string_decode_bytes():
1515
"""Test that bytes_to_string() decodes bytes."""
16-
assert utils.bytes_to_string(b"foobar") == "foobar"
16+
assert utils.bytes_to_string(b"foobar") == "0x666f6f626172"
1717

1818

1919
def test_bytes_to_string_unprintable():
@@ -31,7 +31,7 @@ def test_bytes_to_string_non_bytes():
3131

3232
def test_to_string_bytes():
3333
"""Test that to_string() converts bytes to a string."""
34-
assert utils.to_string(b"foo") == "foo"
34+
assert utils.to_string(b"foo") == "0x666f6f"
3535

3636

3737
def test_to_string_non_bytes():

0 commit comments

Comments
 (0)