Skip to content

Commit

Permalink
Make encoding simpler & add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jmwri committed Sep 28, 2016
1 parent 6069824 commit 5a9e2ee
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
13 changes: 1 addition & 12 deletions voluptuous/encoding.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
def to_utf8_py2(data):
import sys
if sys.version_info < (3,) and isinstance(data, unicode):
return to_utf8(data)
return data


def to_utf8(data):
import sys
if sys.version_info >= (3,):
if isinstance(data, str):
return data.encode('utf-8')
else:
if isinstance(data, unicode):
return data.encode('utf-8')
return data.encode('utf-8')
return data
12 changes: 11 additions & 1 deletion voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import copy
from nose.tools import assert_equal, assert_raises
from nose.tools import assert_equal, assert_raises, assert_true

from voluptuous import (
Schema, Required, Extra, Invalid, In, Remove, Literal,
Expand Down Expand Up @@ -587,3 +587,13 @@ def test_unicode_key_is_converted_to_utf8_when_plain_text():
schema({key: 'will fail'})
except Invalid as e:
assert_equal(str(e), "expected int for dictionary value @ data['q']")


def test_to_utf8_py2():
import sys
from voluptuous.encoding import to_utf8_py2
if sys.version_info >= (3,):
s = 'hello'
else:
s = u'hello'
assert_true(isinstance(to_utf8_py2(s), str))

0 comments on commit 5a9e2ee

Please sign in to comment.