Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a series of small optmizations for dumping objects #577

Merged
merged 1 commit into from Feb 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions marshmallow/fields.py
Expand Up @@ -393,15 +393,15 @@ def schema(self):
.. versionchanged:: 1.0.0
Renamed from `serializer` to `schema`
"""
# Ensure that only parameter is a tuple
if isinstance(self.only, basestring):
only = (self.only, )
else:
only = self.only

# Inherit context from parent.
context = getattr(self.parent, 'context', {})
if not self.__schema:
# Ensure that only parameter is a tuple
if isinstance(self.only, basestring):
only = (self.only,)
else:
only = self.only

# Inherit context from parent.
context = getattr(self.parent, 'context', {})
if isinstance(self.nested, SchemaABC):
self.__schema = self.nested
self.__schema.context.update(context)
Expand All @@ -421,7 +421,7 @@ def schema(self):
else:
raise ValueError('Nested fields must be passed a '
'Schema, not {0}.'.format(self.nested.__class__))
self.__schema.ordered = getattr(self.parent, 'ordered', False)
self.__schema.ordered = getattr(self.parent, 'ordered', False)
return self.__schema

def _serialize(self, nested_obj, attr, obj):
Expand Down
32 changes: 21 additions & 11 deletions marshmallow/schema.py
Expand Up @@ -146,6 +146,7 @@ def _resolve_processors(self):
do all the hard work.
"""
mro = inspect.getmro(self)
self._has_processors = False
self.__processors__ = defaultdict(list)
for attr_name in dir(self):
# Need to look up the actual descriptor, not whatever might be
Expand All @@ -169,6 +170,7 @@ def _resolve_processors(self):
except AttributeError:
continue

self._has_processors = bool(processor_tags)
for tag in processor_tags:
# Use name here so we can get the bound method later, in case
# the processor was a descriptor or something.
Expand Down Expand Up @@ -355,6 +357,7 @@ def __init__(self, extra=None, only=(), exclude=(), prefix='', strict=None,
self.extra = extra
self.context = context or {}
self._normalize_nested_options()
self._types_seen = set()
self._update_fields(many=many)

def __repr__(self):
Expand Down Expand Up @@ -475,19 +478,26 @@ def dump(self, obj, many=None, update_fields=True, **kwargs):
if many and utils.is_iterable_but_not_string(obj):
obj = list(obj)

try:
processed_obj = self._invoke_dump_processors(
PRE_DUMP,
obj,
many,
original_data=obj)
except ValidationError as error:
errors = error.normalized_messages()
result = None
if self._has_processors:
try:
processed_obj = self._invoke_dump_processors(
PRE_DUMP,
obj,
many,
original_data=obj)
except ValidationError as error:
errors = error.normalized_messages()
result = None
else:
processed_obj = obj

if not errors:
if update_fields:
self._update_fields(processed_obj, many=many)
obj_type = type(processed_obj)
if obj_type not in self._types_seen:
self._update_fields(processed_obj, many=many)
if not isinstance(processed_obj, Mapping):
self._types_seen.add(obj_type)

try:
preresult = self._marshal(
Expand All @@ -506,7 +516,7 @@ def dump(self, obj, many=None, update_fields=True, **kwargs):

result = self._postprocess(preresult, many, obj=obj)

if not errors:
if not errors and self._has_processors:
try:
result = self._invoke_dump_processors(
POST_DUMP,
Expand Down