Skip to content

Commit 1707234

Browse files
committed
fix astanin#192 - Can't print bytes with non-ascii encoding
1 parent 3f0757e commit 1707234

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

tabulate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ def _format(val, valtype, floatfmt, intfmt, missingval="", has_invisible=True):
10671067
elif valtype is bytes:
10681068
try:
10691069
return str(val, "ascii")
1070-
except TypeError:
1070+
except (TypeError, UnicodeDecodeError):
10711071
return str(val)
10721072
elif valtype is float:
10731073
is_a_colored_number = has_invisible and isinstance(val, (str, bytes))

test/test_input.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,3 +518,13 @@ def test_py37orlater_list_of_dataclasses_headers():
518518
assert_equal(expected, result)
519519
except ImportError:
520520
skip("test_py37orlater_list_of_dataclasses_headers is skipped")
521+
522+
523+
def test_list_bytes():
524+
"Input: a list of bytes. (issue #192)"
525+
lb = [["你好".encode("utf-8")], ["你好"]]
526+
expected = "\n".join(
527+
["bytes", "---------------------------", r"b'\xe4\xbd\xa0\xe5\xa5\xbd'", "你好"]
528+
)
529+
result = tabulate(lb, headers=["bytes"])
530+
assert_equal(expected, result)

0 commit comments

Comments
 (0)