Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
Merge pull request #153 from Scorpil/fix_unicode_error_python2
Browse files Browse the repository at this point in the history
Fix unicode error python2
  • Loading branch information
kennethreitz committed Oct 26, 2016
2 parents 2bd5aef + a173f76 commit a190f64
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
10 changes: 5 additions & 5 deletions clint/textui/colored.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ class ColoredString(object):
"""Enhanced string for __len__ operations on Colored output."""
def __init__(self, color, s, always_color=False, bold=False):
super(ColoredString, self).__init__()
self.s = s
if not PY3 and isinstance(s, unicode):
self.s = s.encode('utf-8')
else:
self.s = s
self.color = color
self.always_color = always_color
self.bold = bold
Expand Down Expand Up @@ -93,10 +96,7 @@ def __unicode__(self):
__str__ = __unicode__
else:
def __str__(self):
value = self.color_str
if isinstance(value, bytes):
return value
return value.encode('utf8')
return self.color_str

def __iter__(self):
return iter(self.color_str)
Expand Down
17 changes: 11 additions & 6 deletions test_clint.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ def tearDown(self):
pass

class ColoredStringTestCase(unittest.TestCase):

def setUp(self):
from clint.textui.colored import ColoredString

def tearDown(self):
pass

def test_split(self):
from clint.textui.colored import ColoredString
new_str = ColoredString('red', "hello world")
output = new_str.split()
assert output[0].s == "hello"

def test_find(self):
from clint.textui.colored import ColoredString
new_str = ColoredString('blue', "hello world")
output = new_str.find('h')
self.assertEqual(output, 0)

def test_replace(self):
from clint.textui.colored import ColoredString
new_str = ColoredString('green', "hello world")
Expand All @@ -49,14 +49,19 @@ def test_py2_bytes_not_mangled(self):
new_str = ColoredString('RED', '\xe4')
assert '\xe4' in str(new_str)
from clint.textui import puts
puts(new_str)

def test_clint_force_color_env_var(self):
from clint.textui.colored import ColoredString
os.environ['CLINT_FORCE_COLOR'] = "1"
new_str = ColoredString('RED', 'hello world')
assert new_str.always_color == True

def test_clint_unicode_radd(self):
from clint.textui.colored import ColoredString
inp_str = u'hello \u263A'
new_str = u'' + ColoredString('RED', inp_str)
assert inp_str.encode('utf-8') in new_str


class TextuiFormatterTestCase(unittest.TestCase):

Expand Down

0 comments on commit a190f64

Please sign in to comment.