Skip to content

Commit 8d2b58c

Browse files
authored
Revert "don't attempt to UTF-8-decode binary values (#98)" (#99)
This reverts commit 15519e2.
1 parent 88ee529 commit 8d2b58c

4 files changed

Lines changed: 19 additions & 4 deletions

File tree

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.2
4+
5+
(released on 2026-01-26)
6+
7+
- Revert: don't attempt to UTF-8-decode binary values.
8+
39
## Version 2.8.1
410

511
(released on 2026-01-24)

cli_helpers/utils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,16 @@ def bytes_to_string(b):
2121
2222
"""
2323
if isinstance(b, binary_type):
24-
return "0x" + binascii.hexlify(b).decode("ascii")
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
2534
return b
2635

2736

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, "0x4a696c6c"]], [0, "name"])
87+
expected = ([[1, "John"], [2, "Jill"]], [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") == "0x666f6f626172"
16+
assert utils.bytes_to_string(b"foobar") == "foobar"
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") == "0x666f6f"
34+
assert utils.to_string(b"foo") == "foo"
3535

3636

3737
def test_to_string_non_bytes():

0 commit comments

Comments
 (0)