Skip to content

Commit 65484fb

Browse files
committed
fix astanin#49: colored numbers in a unicode/str subclass (colorclass compatibility)
1 parent f50230a commit 65484fb

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

tabulate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ def _format(val, valtype, floatfmt, missingval="", has_invisible=True):
525525
except TypeError:
526526
return _text_type(val)
527527
elif valtype is float:
528-
is_a_colored_number = has_invisible and type(val) in [_text_type, _binary_type]
528+
is_a_colored_number = has_invisible and isinstance(val, (_text_type, _binary_type))
529529
if is_a_colored_number:
530530
raw_val = _strip_invisible(val)
531531
formatted_val = format(float(raw_val), floatfmt)

test/test_regression.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from __future__ import print_function
66
from __future__ import unicode_literals
7-
from tabulate import tabulate
7+
from tabulate import tabulate, _text_type
88
from common import assert_equal, assert_in
99

1010

@@ -214,3 +214,20 @@ def test_long_integers():
214214
result = tabulate(table, tablefmt="plain")
215215
expected = "18446744073709551614"
216216
assert_equal(result, expected)
217+
218+
219+
def test_colorclass_colors():
220+
"Regression: ANSI colors in a unicode/str subclass (issue #49)"
221+
try:
222+
import colorclass
223+
s = colorclass.Color("{magenta}3.14{/magenta}")
224+
result = tabulate([[s]], tablefmt="plain")
225+
expected = "\x1b[35m3.14\x1b[39m"
226+
assert_equal(result, expected)
227+
except ImportError:
228+
class textclass(_text_type):
229+
pass
230+
s = textclass("\x1b[35m3.14\x1b[39m")
231+
result = tabulate([[s]], tablefmt="plain")
232+
expected = "\x1b[35m3.14\x1b[39m"
233+
assert_equal(result, expected)

0 commit comments

Comments
 (0)