Skip to content

Commit

Permalink
Merge pull request #377 from svisser/fix/issue_374
Browse files Browse the repository at this point in the history
Preserve Unicode strings when passed to utility functions
  • Loading branch information
alecthomas authored Dec 31, 2018
2 parents 9644956 + 2f8412a commit 7e18e1c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
37 changes: 36 additions & 1 deletion voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
Contains, Marker, IsDir, IsFile, PathExists, SomeOf, TooManyValid, Self,
raises)
from voluptuous.humanize import humanize_error
from voluptuous.util import u
from voluptuous.util import u, Capitalize, Lower, Strip, Title, Upper


def test_exact_sequence():
Expand Down Expand Up @@ -1287,3 +1287,38 @@ def test_frozenset_of_integers_and_strings():
assert_equal(str(e), "invalid value in frozenset")
else:
assert False, "Did not raise Invalid"


def test_lower_util_handles_various_inputs():
assert Lower(3) == "3"
assert Lower(u"3") == u"3"
assert Lower(b'\xe2\x98\x83'.decode("UTF-8")) == b'\xe2\x98\x83'.decode("UTF-8")
assert Lower(u"A") == u"a"


def test_upper_util_handles_various_inputs():
assert Upper(3) == "3"
assert Upper(u"3") == u"3"
assert Upper(b'\xe2\x98\x83'.decode("UTF-8")) == b'\xe2\x98\x83'.decode("UTF-8")
assert Upper(u"a") == u"A"


def test_capitalize_util_handles_various_inputs():
assert Capitalize(3) == "3"
assert Capitalize(u"3") == u"3"
assert Capitalize(b'\xe2\x98\x83'.decode("UTF-8")) == b'\xe2\x98\x83'.decode("UTF-8")
assert Capitalize(u"aaa aaa") == u"Aaa aaa"


def test_title_util_handles_various_inputs():
assert Title(3) == "3"
assert Title(u"3") == u"3"
assert Title(b'\xe2\x98\x83'.decode("UTF-8")) == b'\xe2\x98\x83'.decode("UTF-8")
assert Title(u"aaa aaa") == u"Aaa Aaa"


def test_strip_util_handles_various_inputs():
assert Strip(3) == "3"
assert Strip(u"3") == u"3"
assert Strip(b'\xe2\x98\x83'.decode("UTF-8")) == b'\xe2\x98\x83'.decode("UTF-8")
assert Strip(u" aaa ") == u"aaa"
18 changes: 13 additions & 5 deletions voluptuous/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@
__author__ = 'tusharmakkar08'


def _fix_str(v):
if sys.version_info[0] == 2 and isinstance(v, unicode):
s = v
else:
s = str(v)
return s


def Lower(v):
"""Transform a string to lower case.
>>> s = Schema(Lower)
>>> s('HI')
'hi'
"""
return str(v).lower()
return _fix_str(v).lower()


def Upper(v):
Expand All @@ -24,7 +32,7 @@ def Upper(v):
>>> s('hi')
'HI'
"""
return str(v).upper()
return _fix_str(v).upper()


def Capitalize(v):
Expand All @@ -34,7 +42,7 @@ def Capitalize(v):
>>> s('hello world')
'Hello world'
"""
return str(v).capitalize()
return _fix_str(v).capitalize()


def Title(v):
Expand All @@ -44,7 +52,7 @@ def Title(v):
>>> s('hello world')
'Hello World'
"""
return str(v).title()
return _fix_str(v).title()


def Strip(v):
Expand All @@ -54,7 +62,7 @@ def Strip(v):
>>> s(' hello world ')
'hello world'
"""
return str(v).strip()
return _fix_str(v).strip()


class DefaultTo(object):
Expand Down

0 comments on commit 7e18e1c

Please sign in to comment.