Example #1
0
  def throw(ctx, *args, **kwds):

    # Already triggered
    if ctx.trigger:
      raise StopIteration

    ctx.trigger = True

    #raise args
    if args:
      #type, value=None, traceback=None = args
      type, value, traceback = (lambda type, value=None, traceback=None: (type, value, traceback))(*args)
      try:
        raise type, value, traceback

      except:
        pass

    import traceback

    ctx.traceback = untwisted.final(untwisted.partial(sys.stderr.write, ''.join(traceback.format_stack(sys._getframe().f_back)) + traceback.format_exc()))

    ctx.args = args
    ctx.kwds = kwds

    ctx.propagate()

    return ctx
Example #2
0
    def throw(ctx, *args, **kwds):

        # Already triggered
        if ctx.trigger:
            raise StopIteration

        ctx.trigger = True

        #raise args
        if args:
            #type, value=None, traceback=None = args
            type, value, traceback = (lambda type, value=None, traceback=None:
                                      (type, value, traceback))(*args)
            try:
                raise type, value, traceback

            except:
                pass

        import traceback

        ctx.traceback = untwisted.final(
            untwisted.partial(
                sys.stderr.write,
                ''.join(traceback.format_stack(sys._getframe().f_back)) +
                traceback.format_exc()))

        ctx.args = args
        ctx.kwds = kwds

        ctx.propagate()

        return ctx
Example #3
0
  def propagate(ctx):
    while True:
      try:
        callback = ctx.callback.pop(0)

      # No callback
      except IndexError:
        return ctx

      if isinstance(callback, promise):

        # Already triggered
        if hasattr(callback, 'args'):
          raise StopIteration

        callback.trigger = True

        try:
          callback.traceback = ctx.traceback
          del ctx.traceback

        # Don't worry about callback.traceback: Can't be without .args
        except AttributeError:
          pass

        callback.args = ctx.args
        ctx.args = callback,

        callback.kwds = ctx.kwds
        ctx.kwds = {}

        if ctx.callback:
          callback.propagate()

        # Tail call optimization
        else:
          ctx = callback

        continue

      # Skip if callback has no .throw()
      if hasattr(ctx, 'traceback'):
        try:
          callback = callback.throw

        except AttributeError:
          continue

        ctx.traceback.cancel()
        del ctx.traceback

      args = ctx.args
      del ctx.args

      kwds = ctx.kwds
      del ctx.kwds

      try:
        result = callback(*args, **kwds)

      except:
        ctx.traceback = untwisted.final(untwisted.partial(sys.stderr.write, ''.join(traceback.format_stack(sys._getframe().f_back)) + traceback.format_exc()))

        ctx.args = sys.exc_info()
        ctx.kwds = {}

        continue

      if isinstance(result, promise):

        # Tail call optimization
        #result.then(ctx)

        try:
          ctx.traceback = result.traceback
          del result.traceback

        # Don't worry about ctx.traceback: del above
        except AttributeError:
          pass

        try:
          ctx.args = result.args
          result.args = ctx,

          ctx.kwds = result.kwds
          result.kwds = {}

        # Not yet triggered
        except AttributeError:
          result.callback.append(ctx)

          return ctx

        continue

      ctx.args = result,
      ctx.kwds = {}
Example #4
0
    def propagate(ctx):
        while True:
            try:
                callback = ctx.callback.pop(0)

            # No callback
            except IndexError:
                return ctx

            if isinstance(callback, promise):

                # Already triggered
                if hasattr(callback, 'args'):
                    raise StopIteration

                callback.trigger = True

                try:
                    callback.traceback = ctx.traceback
                    del ctx.traceback

                # Don't worry about callback.traceback: Can't be without .args
                except AttributeError:
                    pass

                callback.args = ctx.args
                ctx.args = callback,

                callback.kwds = ctx.kwds
                ctx.kwds = {}

                if ctx.callback:
                    callback.propagate()

                # Tail call optimization
                else:
                    ctx = callback

                continue

            # Skip if callback has no .throw()
            if hasattr(ctx, 'traceback'):
                try:
                    callback = callback.throw

                except AttributeError:
                    continue

                ctx.traceback.cancel()
                del ctx.traceback

            args = ctx.args
            del ctx.args

            kwds = ctx.kwds
            del ctx.kwds

            try:
                result = callback(*args, **kwds)

            except:
                ctx.traceback = untwisted.final(
                    untwisted.partial(
                        sys.stderr.write,
                        ''.join(traceback.format_stack(
                            sys._getframe().f_back)) + traceback.format_exc()))

                ctx.args = sys.exc_info()
                ctx.kwds = {}

                continue

            if isinstance(result, promise):

                # Tail call optimization
                #result.then(ctx)

                try:
                    ctx.traceback = result.traceback
                    del result.traceback

                # Don't worry about ctx.traceback: del above
                except AttributeError:
                    pass

                try:
                    ctx.args = result.args
                    result.args = ctx,

                    ctx.kwds = result.kwds
                    result.kwds = {}

                # Not yet triggered
                except AttributeError:
                    result.callback.append(ctx)

                    return ctx

                continue

            ctx.args = result,
            ctx.kwds = {}