From c08f9b05d46f78d0b61d0521c0de2f525d031425 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Mon, 26 Jan 2026 04:37:40 -0500 Subject: [PATCH] Revert "don't attempt to UTF-8-decode binary values (#98)" This reverts commit 15519e282c22c6f732ae60fae555d653716f83ea. --- CHANGELOG | 6 ++++++ cli_helpers/utils.py | 11 ++++++++++- tests/tabular_output/test_preprocessors.py | 2 +- tests/test_utils.py | 4 ++-- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index f31ee22..b2e27e8 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,11 @@ # Changelog +## Version 2.8.2 + +(released on 2026-01-26) + +- Revert: don't attempt to UTF-8-decode binary values. + ## Version 2.8.1 (released on 2026-01-24) diff --git a/cli_helpers/utils.py b/cli_helpers/utils.py index 12fca1d..053bdea 100644 --- a/cli_helpers/utils.py +++ b/cli_helpers/utils.py @@ -21,7 +21,16 @@ def bytes_to_string(b): """ if isinstance(b, binary_type): - return "0x" + binascii.hexlify(b).decode("ascii") + 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 b diff --git a/tests/tabular_output/test_preprocessors.py b/tests/tabular_output/test_preprocessors.py index 3545d9a..5ebd06d 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, "0x4a696c6c"]], [0, "name"]) + expected = ([[1, "John"], [2, "Jill"]], [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 b10d6f7..ba43937 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") == "0x666f6f626172" + assert utils.bytes_to_string(b"foobar") == "foobar" 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") == "0x666f6f" + assert utils.to_string(b"foo") == "foo" def test_to_string_non_bytes():