diff --git a/tabulate/__init__.py b/tabulate/__init__.py index 12a2950..db4067a 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -1651,7 +1651,13 @@ def _wrap_text_to_colwidths( if cell is None else ( str(cell) - if cell == "" or _isnumber(cell) + if cell == "" + or _isnumber(cell) + or (isinstance(cell, str) and _ansi_codes.search(cell)) + or ( + isinstance(cell, bytes) + and _ansi_codes_bytes.search(cell) + ) else str(_type(cell, numparse)(cell)) ) ) diff --git a/test/test_regression.py b/test/test_regression.py index 9555676..e73c40b 100644 --- a/test/test_regression.py +++ b/test/test_regression.py @@ -531,6 +531,15 @@ def test_exception_on_empty_data_with_maxcolwidths(): assert_equal(result, "") +def test_colored_number_with_maxcolwidths(): + "Regression: ANSI-colored numeric strings crash when wrapped by maxcolwidths (github issue #359)" + colored_three = "\033[31m3\033[0m" + result = tabulate([[colored_three]], maxcolwidths=10) + assert "3" in result + assert "\033[31m" in result + assert "\033[0m" in result + + def test_numpy_int64_as_integer(): "Regression: format numpy.int64 as integer (github issue #18)" try: