summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Groszer <agroszer@gmail.com>2020-03-06 15:20:48 +0100
committerAdam Groszer <agroszer@gmail.com>2020-03-06 15:38:52 +0100
commit3c61968968d47606335b9683428060c52eff6eed (patch)
treeb15d6f45592cbf7f9c3830be60c30974d2e3952d
parent6a3e0dbb446fbe729e4e5db7c233c3d317d4a633 (diff)
downloadzope-schema-adamg-fix-text.tar.gz
Fix: add ``Text.unicode_normalization = 'NFC'`` as defaultadamg-fix-text
-rw-r--r--CHANGES.rst8
-rw-r--r--src/zope/schema/_bootstrapfields.py4
-rw-r--r--src/zope/schema/tests/test__bootstrapfields.py18
3 files changed, 26 insertions, 4 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index a117f0f..15539e2 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -2,10 +2,12 @@
Changes
=========
-5.1 (unreleased)
-================
+5.0.1 (unreleased)
+==================
-- Nothing changed yet.
+- Fix: add ``Text.unicode_normalization = 'NFC'`` as default, because some are
+ persisting schema fields. Setting that attribute only in ``__init__``
+ breaks loading old objects.
5.0 (2020-03-06)
diff --git a/src/zope/schema/_bootstrapfields.py b/src/zope/schema/_bootstrapfields.py
index 7152b82..9a64a2d 100644
--- a/src/zope/schema/_bootstrapfields.py
+++ b/src/zope/schema/_bootstrapfields.py
@@ -505,9 +505,11 @@ class MinMaxLen(object):
class Text(MinMaxLen, Field):
"""A field containing text used for human discourse."""
_type = text_type
+ unicode_normalization = 'NFC'
def __init__(self, *args, **kw):
- self.unicode_normalization = kw.pop('unicode_normalization', 'NFC')
+ self.unicode_normalization = kw.pop(
+ 'unicode_normalization', self.unicode_normalization)
super(Text, self).__init__(*args, **kw)
def fromUnicode(self, value):
diff --git a/src/zope/schema/tests/test__bootstrapfields.py b/src/zope/schema/tests/test__bootstrapfields.py
index 34bdfc8..027944c 100644
--- a/src/zope/schema/tests/test__bootstrapfields.py
+++ b/src/zope/schema/tests/test__bootstrapfields.py
@@ -967,6 +967,7 @@ class TextTests(EqualityTestsMixin,
def test_normalization(self):
deadbeef = unicodedata.normalize('NFD', b'\xc3\x84\xc3\x96\xc3\x9c'.decode('utf-8'))
txt = self._makeOne()
+ self.assertEqual(txt.unicode_normalization, 'NFC')
self.assertEqual(
[unicodedata.name(c) for c in txt.fromUnicode(deadbeef)],
[
@@ -976,6 +977,7 @@ class TextTests(EqualityTestsMixin,
]
)
txt = self._makeOne(unicode_normalization=None)
+ self.assertEqual(txt.unicode_normalization, None)
self.assertEqual(
[unicodedata.name(c) for c in txt.fromUnicode(deadbeef)],
[
@@ -988,6 +990,22 @@ class TextTests(EqualityTestsMixin,
]
)
+ def test_normalization_after_pickle(self):
+ # test an edge case where `Text` is persisted
+ # see https://github.com/zopefoundation/zope.schema/issues/90
+ import pickle
+ orig_makeOne = self._makeOne
+ def makeOne(**kwargs):
+ result = orig_makeOne(**kwargs)
+ if not kwargs:
+ # We should have no state to preserve
+ result.__dict__.clear()
+
+ result = pickle.loads(pickle.dumps(result))
+ return result
+
+ self._makeOne = makeOne
+ self.test_normalization()
class TextLineTests(EqualityTestsMixin,