Skip to content

Commit

Permalink
smarter cloth finalization
Browse files Browse the repository at this point in the history
  • Loading branch information
plq committed Oct 6, 2016
1 parent 92939e3 commit 197cdce
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions spyne/protocol/cloth/_base.py
Expand Up @@ -34,6 +34,7 @@
from spyne.protocol.cloth.to_parent import ToParentMixin
from spyne.protocol.cloth.to_cloth import ToClothMixin
from spyne.util.six import BytesIO
from spyne.util.color import R, B


class XmlClothProtocolContext(ProtocolContext):
Expand Down Expand Up @@ -90,7 +91,7 @@ def serialize(self, ctx, message):
# FIXME: the eff is this?
ctx.out_object = (inst,)

retval = self.incgen(ctx, cls, inst, name)
retval = self._incgen(ctx, cls, inst, name)
else:
with self.docfile(ctx.out_stream) as xf:
retval = self.to_parent(ctx, cls, inst, xf, name)
Expand Down Expand Up @@ -118,7 +119,7 @@ def serialize(self, ctx, message):
else:
result_inst, = ctx.out_object

retval = self.incgen(ctx, result_class, result_inst, name)
retval = self._incgen(ctx, result_class, result_inst, name)

self.event_manager.fire_event('after_serialize', ctx)

Expand All @@ -135,7 +136,12 @@ def create_out_string(self, ctx, charset=None):
ctx.out_string = [ctx.out_stream.getvalue()]

@coroutine
def incgen(self, ctx, cls, inst, name):
def _incgen(self, ctx, cls, inst, name):
"""Entry point to the (stack of) XmlCloth-based protocols.
Not supposed to be overridden.
"""

if name is None:
name = cls.get_type_name()

Expand All @@ -156,10 +162,6 @@ def incgen(self, ctx, cls, inst, name):
ret.throw(b)
except StopIteration:
pass
finally:
self._close_cloth(ctx, xf)
else:
self._close_cloth(ctx, xf)

except LxmlSyntaxError as e:
if e.msg == 'no content written':
Expand Down Expand Up @@ -203,19 +205,28 @@ def check_class_cloths(self, ctx, cls, inst, parent, name, **kwargs):

@coroutine
def subserialize(self, ctx, cls, inst, parent, name='', **kwargs):
"""Bridge between multiple XmlCloth-based protocols.
Not supposed to be overridden.
"""

pstack = ctx.protocol.prot_stack
pstack.append(self)
logger.debug("push prot %r. newlen: %d", self, len(pstack))
logger.debug("%s push prot %r. newlen: %d", R("%"), self, len(pstack))

have_cloth = False
if self._root_cloth is not None:
logger.debug("to root cloth")
ret = self.to_root_cloth(ctx, cls, inst, self._root_cloth,
parent, name)
have_cloth = True

elif self._cloth is not None:
logger.debug("to parent cloth")
ret = self.to_parent_cloth(ctx, cls, inst, self._cloth, parent,
name)
have_cloth = True

else:
logger.debug("to parent")
ret = self.start_to_parent(ctx, cls, inst, parent, name, **kwargs)
Expand All @@ -231,9 +242,22 @@ def subserialize(self, ctx, cls, inst, parent, name='', **kwargs):
ret.throw(b)
except StopIteration:
pass
finally:
self._finalize_protocol(ctx, parent, have_cloth)
else:
self._finalize_protocol(ctx, parent, have_cloth)

pstack.pop()
logger.debug("pop prot %r. newlen: %d", self, len(pstack))
logger.debug("%s pop prot %r. newlen: %d", B("%"), self, len(pstack))

def _finalize_protocol(self, ctx, parent, have_cloth):
if have_cloth:
self._close_cloth(ctx, parent)
return

if len(ctx.protocol.prot_stack) == 1 and len(ctx.protocol.eltstack) > 0:
self._close_cloth(ctx, parent)
return

def decompose_incoming_envelope(self, ctx, message):
raise NotImplementedError("This is an output-only protocol.")

0 comments on commit 197cdce

Please sign in to comment.