Skip to content

Commit

Permalink
Merge pull request #278 from jd/jd/fix-unicode
Browse files Browse the repository at this point in the history
Remove unicode translation
  • Loading branch information
tusharmakkar08 committed Apr 13, 2017
2 parents cd0be17 + 44fa104 commit 1b4ea20
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 42 deletions.
10 changes: 0 additions & 10 deletions voluptuous/schema_builder.py
Expand Up @@ -283,17 +283,12 @@ def _compile_mapping(self, schema, invalid_msg=None):
additional_candidates.append((skey, (ckey, cvalue)))

def validate_mapping(path, iterable, out):
try:
from util import to_utf8_py2
except ImportError:
from .util import to_utf8_py2
required_keys = all_required_keys.copy()
# keeps track of all default keys that haven't been filled
default_keys = all_default_keys.copy()
error = None
errors = []
for key, value in iterable:
key = to_utf8_py2(key)
key_path = path + [key]
remove_key = False

Expand Down Expand Up @@ -885,11 +880,6 @@ class Marker(object):
"""Mark nodes for special treatment."""

def __init__(self, schema_, msg=None):
try:
from util import to_utf8_py2
except ImportError:
from .util import to_utf8_py2
schema_ = to_utf8_py2(schema_)
self.schema = schema_
self._schema = Schema(schema_)
self.msg = msg
Expand Down
35 changes: 9 additions & 26 deletions voluptuous/tests/tests.py
@@ -1,5 +1,6 @@
import copy
import collections
import sys
from nose.tools import assert_equal, assert_raises, assert_true

from voluptuous import (
Expand All @@ -9,7 +10,7 @@
validate, ExactSequence, Equal, Unordered, Number, Maybe, Datetime, Date,
Contains, Marker)
from voluptuous.humanize import humanize_error
from voluptuous.util import to_utf8_py2, u
from voluptuous.util import u


def test_exact_sequence():
Expand Down Expand Up @@ -642,15 +643,13 @@ def fn(arg):
assert_raises(Invalid, fn, 1)


def test_unicode_key_is_converted_to_utf8_when_in_marker():
"""Verify that when using unicode key the 'u' prefix is not thrown in the exception"""
schema = Schema({Required(u('q')): 1})
# Can't use nose's raises (because we need to access the raised
# exception, nor assert_raises which fails with Python 2.6.9.
try:
schema({})
except Invalid as e:
assert_equal(str(e), "required key not provided @ data['q']")
def test_unicode_as_key():
if sys.version_info >= (3,):
text_type = str
else:
text_type = unicode
schema = Schema({text_type: int})
schema({u("foobar"): 1})


def test_number_validation_with_string():
Expand All @@ -665,17 +664,6 @@ def test_number_validation_with_string():
assert False, "Did not raise Invalid for String"


def test_unicode_key_is_converted_to_utf8_when_plain_text():
key = u('q')
schema = Schema({key: int})
# Can't use nose's raises (because we need to access the raised
# exception, nor assert_raises which fails with Python 2.6.9.
try:
schema({key: 'will fail'})
except Invalid as e:
assert_equal(str(e), "expected int for dictionary value @ data['q']")


def test_number_validation_with_invalid_precision_invalid_scale():
""" test with Number with invalid precision and scale"""
schema = Schema({"number": Number(precision=6, scale=2)})
Expand Down Expand Up @@ -716,11 +704,6 @@ def test_number_when_precision_none_n_valid_scale_case2_yield_decimal_true():
assert_equal(float(out_.get("number")), 123456789012.00)


def test_to_utf8():
s = u('hello')
assert_true(isinstance(to_utf8_py2(s), str))


def test_number_when_precision_none_n_invalid_scale_yield_decimal_true():
""" test with Number with no precision and invalid scale"""
schema = Schema({"number": Number(scale=2, yield_decimal=True)})
Expand Down
6 changes: 0 additions & 6 deletions voluptuous/util.py
Expand Up @@ -152,12 +152,6 @@ def __repr__(self):
return repr(self.lit)


def to_utf8_py2(data):
if sys.version_info < (3,) and isinstance(data, unicode):
return data.encode('utf-8')
return data


def u(x):
if sys.version_info < (3,):
return unicode(x)
Expand Down

0 comments on commit 1b4ea20

Please sign in to comment.