summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDavid Glick <david@glicksoftware.com>2018-09-29 12:32:38 -0400
committerDavid Glick <david@glicksoftware.com>2018-09-29 12:32:38 -0400
commit387ca135b4143859887aeeed17bfa85a42fc90e9 (patch)
treeb5fff90c20953b44e5306d2d81c6d113549957e5 /src
parentd8fb92e284f798c73c5e382d2dc105eae72ef9f5 (diff)
downloadzope-schema-387ca135b4143859887aeeed17bfa85a42fc90e9.tar.gz
Fix handling of non-ascii bytes tokens
Diffstat (limited to 'src')
-rw-r--r--src/zope/schema/tests/test_vocabulary.py7
-rw-r--r--src/zope/schema/vocabulary.py4
2 files changed, 10 insertions, 1 deletions
diff --git a/src/zope/schema/tests/test_vocabulary.py b/src/zope/schema/tests/test_vocabulary.py
index 63904b6..8dc17ea 100644
--- a/src/zope/schema/tests/test_vocabulary.py
+++ b/src/zope/schema/tests/test_vocabulary.py
@@ -58,6 +58,13 @@ class SimpleTermTests(unittest.TestCase):
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')
diff --git a/src/zope/schema/vocabulary.py b/src/zope/schema/vocabulary.py
index a7bbc63..eba0d4b 100644
--- a/src/zope/schema/vocabulary.py
+++ b/src/zope/schema/vocabulary.py
@@ -55,7 +55,9 @@ class SimpleTerm(object):
# 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)