diff options
| author | Georg Bernhard <gogo@bluedynamics.com> | 2020-02-14 10:15:06 +0100 |
|---|---|---|
| committer | Georg Bernhard <gogo@bluedynamics.com> | 2020-02-14 10:15:06 +0100 |
| commit | 0d95c7df41dea6727df11cac551bb1439d60d8d6 (patch) | |
| tree | 0c6e86f4974b19bf7a1d466a3ed91e6e563ab76e | |
| parent | 144e300ced249185fa0eca42a9b7a598d13a9420 (diff) | |
| download | zope-schema-0d95c7df41dea6727df11cac551bb1439d60d8d6.tar.gz | |
Fixing issue86 by normalizing unicode for IFromUnicode
| -rw-r--r-- | src/zope/schema/_bootstrapfields.py | 8 | ||||
| -rw-r--r-- | src/zope/schema/tests/test__bootstrapfields.py | 27 |
2 files changed, 33 insertions, 2 deletions
diff --git a/src/zope/schema/_bootstrapfields.py b/src/zope/schema/_bootstrapfields.py index e140090..0aec192 100644 --- a/src/zope/schema/_bootstrapfields.py +++ b/src/zope/schema/_bootstrapfields.py @@ -20,6 +20,7 @@ import fractions import numbers import sys import threading +import unicodedata from math import isinf from zope.interface import Attribute @@ -505,7 +506,8 @@ class Text(MinMaxLen, Field): """A field containing text used for human discourse.""" _type = text_type - def __init__(self, *args, **kw): + def __init__(self, suppress_unicode_normalization=_NotGiven, *args, **kw): + self.suppress_unicode_normalization = suppress_unicode_normalization super(Text, self).__init__(*args, **kw) def fromUnicode(self, str): @@ -528,8 +530,12 @@ class Text(MinMaxLen, Field): Traceback (most recent call last): ... zope.schema._bootstrapinterfaces.ConstraintNotSatisfied: (u'foo spam', '') + >>> [ unicodedata.name(c) for c in t.fromUnicode(unicodedata.normalize('NFD', 'ÄÖÜ'))] + ['LATIN CAPITAL LETTER A WITH DIAERESIS', 'LATIN CAPITAL LETTER O WITH DIAERESIS', 'LATIN CAPITAL LETTER U WITH DIAERESIS'] """ self.validate(str) + if self.suppress_unicode_normalization is _NotGiven: + str = unicodedata.normalize('NFC', str) return str diff --git a/src/zope/schema/tests/test__bootstrapfields.py b/src/zope/schema/tests/test__bootstrapfields.py index 8aee806..1293026 100644 --- a/src/zope/schema/tests/test__bootstrapfields.py +++ b/src/zope/schema/tests/test__bootstrapfields.py @@ -13,6 +13,7 @@ ############################################################################## import doctest import unittest +import unicodedata # pylint:disable=protected-access,inherit-non-class,blacklisted-name @@ -958,11 +959,35 @@ class TextTests(EqualityTestsMixin, self.assertRaisesWrongType(txt.fromUnicode, txt._type, deadbeef) def test_fromUnicode_hit(self): - deadbeef = u'DEADBEEF' txt = self._makeOne() self.assertEqual(txt.fromUnicode(deadbeef), deadbeef) + def test_normalization(self): + deadbeef = unicodedata.normalize('NFD', 'ÄÖÜ') + txt = self._makeOne() + self.assertEqual( + [unicodedata.name(c) for c in txt.fromUnicode(deadbeef)], + [ + 'LATIN CAPITAL LETTER A WITH DIAERESIS', + 'LATIN CAPITAL LETTER O WITH DIAERESIS', + 'LATIN CAPITAL LETTER U WITH DIAERESIS', + ] + ) + txt = self._makeOne(suppress_unicode_normalization=True) + self.assertEqual( + [unicodedata.name(c) for c in txt.fromUnicode(deadbeef)], + [ + 'LATIN CAPITAL LETTER A', + 'COMBINING DIAERESIS', + 'LATIN CAPITAL LETTER O', + 'COMBINING DIAERESIS', + 'LATIN CAPITAL LETTER U', + 'COMBINING DIAERESIS', + ] + ) + + class TextLineTests(EqualityTestsMixin, WrongTypeTestsMixin, |
