Skip to content

Commit

Permalink
Fix handling of non-ascii bytes tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
davisagli committed Sep 29, 2018
1 parent d8fb92e commit 387ca13
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/zope/schema/tests/test_vocabulary.py
Expand Up @@ -58,6 +58,13 @@ def test_bytes_value(self):
self.assertEqual(term.token, 'term')
self.assertFalse(ITitledTokenizedTerm.providedBy(term))

def test_bytes_non_ascii_value(self):
from zope.schema.interfaces import ITitledTokenizedTerm
term = self._makeOne(b'Snowman \xe2\x98\x83')
self.assertEqual(term.value, b'Snowman \xe2\x98\x83')
self.assertEqual(term.token, 'Snowman \\xe2\\x98\\x83')
self.assertFalse(ITitledTokenizedTerm.providedBy(term))

def test_unicode_non_ascii_value(self):
from zope.schema.interfaces import ITitledTokenizedTerm
term = self._makeOne(u'Snowman \u2603')
Expand Down
4 changes: 3 additions & 1 deletion src/zope/schema/vocabulary.py
Expand Up @@ -55,7 +55,9 @@ def __init__(self, value, token=None, title=None):
# we want here. On the other hand, we want to try to keep the token as
# readable as possible. On both 2 and 3, self.token should be a native
# string (ASCIILine).
if not isinstance(token, (str, bytes, text_type)):
if isinstance(token, bytes):
token = token.decode('raw_unicode_escape')
elif not isinstance(token, (str, text_type)):
# Nothing we recognize as intended to be textual data.
# Get its str() as promised
token = str(token)
Expand Down

0 comments on commit 387ca13

Please sign in to comment.