def exit(self, result=None): self._cleanup() if not self._is_root: # If not a root section just return the result as-is return result # We are a root sections if IFiber.providedBy(result): # If the result is a fiber, we initialize and start it fiber = IFiber(result) self.descriptor.attach(fiber) return fiber.start() if isinstance(result, defer.Deferred): return result elif isinstance(result, failure.Failure): return defer.fail(result) else: return defer.succeed(result)
def maybe_fiber(_function, *args, **kwargs): try: result = _function(*args, **kwargs) except Exception: return defer.fail(failure.Failure()) else: if IFiber.providedBy(result): return result.start() if isinstance(result, defer.Deferred): return result elif isinstance(result, failure.Failure): return defer.fail(result) else: return defer.succeed(result)
def maybeDeferred(f, *args, **kw): """ Copied from twsited.internet.defer and add a check to detect fibers. """ try: result = f(*args, **kw) except Exception: return fail(failure.Failure()) if IFiber.providedBy(result): import traceback frames = traceback.extract_stack() msg = "%s returned a fiber instead of a deferred" % (f, ) if len(frames) > 1: msg += "; called from %s" % (frames[-2], ) raise RuntimeError(msg) if isinstance(result, Deferred): return result elif isinstance(result, failure.Failure): return fail(result) else: return succeed(result)
def attach(self, fiber): fiber = IFiber(fiber) fiber._bind(self, self.fiber_id, 1)
def _check_side_effet_result(result, info): if isinstance(result, defer.Deferred): raise SideEffectResultError("Side-effect functions %s " "cannot return Deferred" % info) if IFiber.providedBy(result): raise SideEffectResultError("Side-effect functions %s " "cannot return IFiber" % info) return result