From 82999e2b4ccc675718104419637454998f1d02d7 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Sat, 24 Jan 2026 14:05:39 -0500 Subject: [PATCH] don't attempt to UTF-8-decode binary values Treat all binary values the same: render them as hex literals. The existing logic allows some binary values to be treated differently than others. --- CHANGELOG | 6 ++++++ cli_helpers/utils.py | 11 +---------- tests/tabular_output/test_preprocessors.py | 2 +- tests/test_utils.py | 4 ++-- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 07f43fc..f31ee22 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,11 @@ # Changelog +## Version 2.8.1 + +(released on 2026-01-24) + +- Don't attempt to UTF-8-decode binary values. + ## Version 2.8.0 (released on 2026-01-24) diff --git a/cli_helpers/utils.py b/cli_helpers/utils.py index 053bdea..12fca1d 100644 --- a/cli_helpers/utils.py +++ b/cli_helpers/utils.py @@ -21,16 +21,7 @@ def bytes_to_string(b): """ if isinstance(b, binary_type): - needs_hex = False - try: - result = b.decode("utf8") - needs_hex = not result.isprintable() - except UnicodeDecodeError: - needs_hex = True - if needs_hex: - return "0x" + binascii.hexlify(b).decode("ascii") - else: - return result + return "0x" + binascii.hexlify(b).decode("ascii") return b diff --git a/tests/tabular_output/test_preprocessors.py b/tests/tabular_output/test_preprocessors.py index 5ebd06d..3545d9a 100644 --- a/tests/tabular_output/test_preprocessors.py +++ b/tests/tabular_output/test_preprocessors.py @@ -84,7 +84,7 @@ def test_bytes_to_string(): """Test the bytes_to_string() function.""" data = [[1, "John"], [2, b"Jill"]] headers = [0, "name"] - expected = ([[1, "John"], [2, "Jill"]], [0, "name"]) + expected = ([[1, "John"], [2, "0x4a696c6c"]], [0, "name"]) results = bytes_to_string(data, headers) assert expected == (list(results[0]), results[1]) diff --git a/tests/test_utils.py b/tests/test_utils.py index ba43937..b10d6f7 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -13,7 +13,7 @@ def test_bytes_to_string_hexlify(): def test_bytes_to_string_decode_bytes(): """Test that bytes_to_string() decodes bytes.""" - assert utils.bytes_to_string(b"foobar") == "foobar" + assert utils.bytes_to_string(b"foobar") == "0x666f6f626172" def test_bytes_to_string_unprintable(): @@ -31,7 +31,7 @@ def test_bytes_to_string_non_bytes(): def test_to_string_bytes(): """Test that to_string() converts bytes to a string.""" - assert utils.to_string(b"foo") == "foo" + assert utils.to_string(b"foo") == "0x666f6f" def test_to_string_non_bytes():