Skip to content

Commit

Permalink
Add support for IFromBytes. Fixes #92
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Sep 19, 2018
1 parent 3fcc0c8 commit 5b4ad1f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
1.0.0a13 (unreleased)
=====================

- Nothing changed yet.
- Support ``IFromBytes`` fields introduced by zope.schema 4.8.0. See
`issue 92
<https://github.com/NextThought/nti.externalization/issues/92>`_.


1.0.0a12 (2018-09-11)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def _c(m):
'zope.location',
'zope.mimetype >= 2.3.0',
'zope.proxy',
'zope.schema >= 4.7.0',
'zope.schema >= 4.8.0',
'zope.security',
],
extras_require={
Expand Down
1 change: 1 addition & 0 deletions src/nti/externalization/internalization/_fields.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ cdef notify

# optimizations
cdef IField_providedBy
cdef IFromBytes_providedBy
cdef IFromUnicode_providedBy
cdef get_exc_info

Expand Down
4 changes: 4 additions & 0 deletions src/nti/externalization/internalization/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from zope.interface import implementedBy

from zope.schema.interfaces import IField
from zope.schema.interfaces import IFromBytes
from zope.schema.interfaces import IFromUnicode
from zope.schema.interfaces import SchemaNotProvided
from zope.schema.interfaces import SchemaNotCorrectlyImplemented
Expand All @@ -35,6 +36,7 @@
from zope.event import notify

IField_providedBy = IField.providedBy
IFromBytes_providedBy = IFromBytes.providedBy
IFromUnicode_providedBy = IFromUnicode.providedBy

__all__ = [
Expand Down Expand Up @@ -284,6 +286,8 @@ def validate_field_value(self, field_name, field, value):
try:
if isinstance(value, text_type) and IFromUnicode_providedBy(field):
value = field.fromUnicode(value) # implies validation
elif isinstance(value, bytes) and IFromBytes_providedBy(field):
value = field.fromBytes(value) # implies validation
else:
field.validate(value)
except SchemaNotProvided:
Expand Down
21 changes: 21 additions & 0 deletions src/nti/externalization/internalization/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,27 @@ class Foo(object):
self._callFUT(foo, IFoo, u'field', u'text')()
assert_that(foo, has_attr('field', u'text'))

def test_from_bytes(self):
from zope.schema.interfaces import IFromBytes
from zope.schema import Field
@interface.implementer(IFromBytes)
class OnlyBytes(Field):
_type = bytes

def fromBytes(self, value):
return b'from bytes'

class IFoo(interface.Interface):
field = OnlyBytes(title=u'text')

class Foo(object):
pass

foo = Foo()
self._callFUT(foo, IFoo, u'field', b'text')()
assert_that(foo, has_attr('field', b'from bytes'))


def test_raises_SchemaNotCorrectlyImplemented(self):
from zope.schema.interfaces import SchemaNotCorrectlyImplemented
from zope.schema import TextLine
Expand Down
10 changes: 7 additions & 3 deletions src/nti/externalization/tests/test_datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,15 @@ class O(object):
inst.updateFromExternalObject({'ivar': u'2.0'})
assert_that(ext_self, has_property('ivar', 2.0))

# A byte string is NOT fine
# A byte string is fine
inst.updateFromExternalObject({'ivar': b'3.0'})

# An object is NOT fine
with self.assertRaises(WrongType):
inst.updateFromExternalObject({'ivar': b'3.0'})
inst.updateFromExternalObject({'ivar': object()})

assert_that(ext_self, has_property('ivar', 2.0))

assert_that(ext_self, has_property('ivar', 3.0))


# Now a validation error after set
Expand Down

0 comments on commit 5b4ad1f

Please sign in to comment.