Skip to content

Commit

Permalink
Prevent the 'u' prefix on keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jmwri committed Sep 28, 2016
1 parent 987fd98 commit 902e715
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
9 changes: 9 additions & 0 deletions voluptuous/encoding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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
4 changes: 4 additions & 0 deletions voluptuous/schema_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,14 @@ def _compile_mapping(self, schema, invalid_msg=None):
candidates = list(_iterate_mapping_candidates(_compiled_schema))

def validate_mapping(path, iterable, out):
from .encoding import to_utf8
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(key)
key_path = path + [key]
remove_key = False

Expand Down Expand Up @@ -809,6 +811,8 @@ class Marker(object):
"""Mark nodes for special treatment."""

def __init__(self, schema_, msg=None):
from .encoding import to_utf8
schema_ = to_utf8(schema_)
self.schema = schema_
self._schema = Schema(schema_)
self.msg = msg
Expand Down
21 changes: 21 additions & 0 deletions voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,3 +556,24 @@ def fn(arg):
return "hello"

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_key_is_converted_to_utf8_when_plain_text():
schema = Schema({u'q': 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({u'q': 'will fail'})
except Invalid as e:
assert_equal(str(e), "expected int for dictionary value @ data['q']")

0 comments on commit 902e715

Please sign in to comment.