Skip to content

Commit

Permalink
More tpying for internalization.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Jun 28, 2018
1 parent 1627cb6 commit 5a57ae8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 11 deletions.
30 changes: 28 additions & 2 deletions src/nti/externalization/_internalization.pxd
Expand Up @@ -18,7 +18,7 @@ cdef SchemaNotProvided
cdef ValidationError
cdef reraise
cdef WrongType
cdef interface_implementedBy

cdef warnings
cdef collections
cdef component
Expand All @@ -28,6 +28,13 @@ cdef inspect
cdef Attributes
cdef ObjectModifiedFromExternalEvent
cdef _zope_event_notify
cdef text_type
cdef IFromUnicode

# optimizations

cdef IPersistent_providedBy
cdef interface_implementedBy

# constants
cdef tuple _primitives
Expand Down Expand Up @@ -56,7 +63,18 @@ cdef class _FieldSet(object):
cdef _notifyModified(containedObject, externalObject, updater=*, external_keys=*,
eventFactory=*, dict kwargs=*)

cdef _recall(k, obj, ext_obj, dict kwargs)
@cython.internal
@cython.final
@cython.freelist(1000)
cdef class _RecallArgs(object):
cdef registry
cdef context
cdef require_updater
cdef notify
cdef pre_hook


cdef _recall(k, obj, ext_obj, _RecallArgs kwargs)
# XXX: This is only public for testing
cpdef _resolve_externals(object_io, updating_object, externalObject,
registry=*, context=*)
Expand All @@ -66,3 +84,11 @@ cpdef find_factory_for(externalized_object, registry=*)

cpdef validate_field_value(self, field_name, field, value)
cpdef validate_named_field_value(self, iface, field_name, value)

cpdef update_from_external_object(containedObject,
externalObject,
registry=*,
context=*,
require_updater=*,
notify=*,
pre_hook=*)
49 changes: 40 additions & 9 deletions src/nti/externalization/internalization.py
Expand Up @@ -64,6 +64,7 @@
# pylint: disable=redefined-outer-name,inherit-non-class

interface_implementedBy = interface.implementedBy
IPersistent_providedBy = IPersistent.providedBy

logger = __import__('logging').getLogger(__name__)

Expand Down Expand Up @@ -338,20 +339,47 @@ def _resolve_externals(object_io, updating_object, externalObject,
# Things we don't bother trying to internalize
_primitives = string_types + (numbers.Number, bool)

class _RecallArgs(object):
__slots__ = (
'registry',
'context',
'require_updater',
'notify',
'pre_hook',
)

def __init__(self, registry,
context,
require_updater,
notify,
pre_hook):
self.registry = registry
self.context = context
self.require_updater = require_updater
self.notify = notify
self.pre_hook = pre_hook


def _recall(k, obj, ext_obj, kwargs):
obj = update_from_external_object(obj, ext_obj, **kwargs)
if IPersistent.providedBy(obj): # pragma: no cover
# We must manually pass all the args to get the optimized
# cython call
obj = update_from_external_object(obj, ext_obj,
registry=kwargs.registry,
context=kwargs.context,
require_updater=kwargs.require_updater,
notify=kwargs.notify,
pre_hook=kwargs.pre_hook)
if IPersistent_providedBy(obj): # pragma: no cover
obj._v_updated_from_external_source = ext_obj
return obj


def _notifyModified(containedObject, externalObject, updater=None, external_keys=(),
def _notifyModified(containedObject, externalObject, updater=None, external_keys=None,
eventFactory=ObjectModifiedFromExternalEvent, kwargs=None):
# try to provide external keys
if kwargs is None:
kwargs = {}
if not external_keys:
if external_keys is None or not external_keys:
external_keys = [k for k in externalObject.keys()]

# TODO: We need to try to find the actual interfaces and fields to allow correct
Expand Down Expand Up @@ -441,11 +469,14 @@ def update_from_external_object(containedObject, externalObject,
for i in range(3):
warnings.warn('pre_hook is deprecated', FutureWarning, stacklevel=i)

kwargs = dict(notify=notify,
context=context,
registry=registry,
pre_hook=pre_hook,
require_updater=require_updater)
kwargs = _RecallArgs(
registry,
context,
require_updater,
notify,
pre_hook
)


# Parse any contained objects
# TODO: We're (deliberately?) not actually updating any contained
Expand Down

0 comments on commit 5a57ae8

Please sign in to comment.