예제 #1
0
파일: loop.py 프로젝트: zielmicha/pypy
def call_many_to_one(space, shape, func, res_dtype, in_args, out):
    # out must hav been built. func needs no calc_type, is usually an
    # external ufunc
    nin = len(in_args)
    in_iters = [None] * nin
    in_states = [None] * nin
    for i in range(nin):
        in_i = in_args[i]
        assert isinstance(in_i, W_NDimArray)
        in_iter, in_state = in_i.create_iter(shape)
        in_iters[i] = in_iter
        in_states[i] = in_state
    shapelen = len(shape)
    assert isinstance(out, W_NDimArray)
    out_iter, out_state = out.create_iter(shape)
    vals = [None] * nin
    while not out_iter.done(out_state):
        call_many_to_one_driver.jit_merge_point(shapelen=shapelen,
                                                func=func,
                                                res_dtype=res_dtype,
                                                nin=nin)
        for i in range(nin):
            vals[i] = in_iters[i].getitem(in_states[i])
        w_arglist = space.newlist(vals)
        w_out_val = space.call_args(func,
                                    Arguments.frompacked(space, w_arglist))
        out_iter.setitem(out_state, res_dtype.coerce(space, w_out_val))
        for i in range(nin):
            in_states[i] = in_iters[i].next(in_states[i])
        out_state = out_iter.next(out_state)
    return out
예제 #2
0
파일: loop.py 프로젝트: Qointum/pypy
def call_many_to_one(space, shape, func, res_dtype, in_args, out):
    # out must hav been built. func needs no calc_type, is usually an
    # external ufunc
    nin = len(in_args)
    in_iters = [None] * nin
    in_states = [None] * nin
    for i in range(nin):
        in_i = in_args[i]
        assert isinstance(in_i, W_NDimArray)
        in_iter, in_state = in_i.create_iter(shape)
        in_iters[i] = in_iter
        in_states[i] = in_state
    shapelen = len(shape)
    assert isinstance(out, W_NDimArray)
    out_iter, out_state = out.create_iter(shape)
    vals = [None] * nin
    while not out_iter.done(out_state):
        call_many_to_one_driver.jit_merge_point(shapelen=shapelen, func=func,
                                     res_dtype=res_dtype, nin=nin)
        for i in range(nin):
            vals[i] = in_iters[i].getitem(in_states[i])
        w_arglist = space.newlist(vals)
        w_out_val = space.call_args(func, Arguments.frompacked(space, w_arglist))
        out_iter.setitem(out_state, res_dtype.coerce(space, w_out_val))
        for i in range(nin):
            in_states[i] = in_iters[i].next(in_states[i])
        out_state = out_iter.next(out_state)
    return out
예제 #3
0
def checkmodule(modname, backend, interactive=False, basepath='pypy.module'):
    "Compile a fake PyPy module."
    from pypy.objspace.fake.objspace import FakeObjSpace, W_Object
    from pypy.translator.driver import TranslationDriver

    space = FakeObjSpace()
    space.config.translating = True
    ModuleClass = __import__(basepath + '.%s' % modname,
                             None, None, ['Module']).Module
    module = ModuleClass(space, space.wrap(modname))
    w_moduledict = module.getdict(space)

    gateways = find_gateways(modname, basepath, module)
    functions = [gw.__spacebind__(space) for gw in gateways]
    arguments = Arguments.frompacked(space, W_Object(), W_Object())
    dummy_function = copy(functions[0])

    def main(argv): # use the standalone mode not to allow SomeObject
        dummy_rpython(dummy_function)        
        for func in functions:
            func.call_args(arguments)
        return 0

    patch_pypy()
    driver = TranslationDriver()
    driver.setup(main, None)
    try:
        driver.proceed(['compile_' + backend])
    except SystemExit:
        raise
    except:
        if not interactive:
            raise
        debug(driver)
        raise SystemExit(1)
예제 #4
0
def checkmodule(modname, backend, interactive=False, basepath='pypy.module'):
    "Compile a fake PyPy module."
    from pypy.objspace.fake.objspace import FakeObjSpace, W_Object
    from pypy.translator.driver import TranslationDriver

    space = FakeObjSpace()
    space.config.translating = True
    ModuleClass = __import__(basepath + '.%s' % modname,
                             None, None, ['Module']).Module
    module = ModuleClass(space, space.wrap(modname))
    w_moduledict = module.getdict()

    gateways = find_gateways(modname, basepath, module)
    functions = [gw.__spacebind__(space) for gw in gateways]
    arguments = Arguments.frompacked(space, W_Object(), W_Object())
    dummy_function = copy(functions[0])

    def main(argv): # use the standalone mode not to allow SomeObject
        dummy_rpython(dummy_function)        
        for func in functions:
            func.call_args(arguments)
        return 0

    patch_pypy()
    driver = TranslationDriver()
    driver.setup(main, None)
    try:
        driver.proceed(['compile_' + backend])
    except SystemExit:
        raise
    except:
        if not interactive:
            raise
        debug(driver)
        raise SystemExit(1)
예제 #5
0
 def test_topacked_frompacked(self):
     space = DummySpace()
     args = Arguments(space, [1], ['a', 'b'], [2, 3])
     w_args, w_kwds = args.topacked()
     assert w_args == (1,)
     assert w_kwds == {'a': 2, 'b': 3}
     args1 = Arguments.frompacked(space, w_args, w_kwds)
     assert args.arguments_w == [1]
     assert set(args.keywords) == set(['a', 'b'])
     assert args.keywords_w[args.keywords.index('a')] == 2
     assert args.keywords_w[args.keywords.index('b')] == 3        
예제 #6
0
 def test_topacked_frompacked(self):
     space = DummySpace()
     args = Arguments(space, [1], ['a', 'b'], [2, 3])
     w_args, w_kwds = args.topacked()
     assert w_args == (1,)
     assert w_kwds == {'a': 2, 'b': 3}
     args1 = Arguments.frompacked(space, w_args, w_kwds)
     assert args.arguments_w == [1]
     assert set(args.keywords) == set(['a', 'b'])
     assert args.keywords_w[args.keywords.index('a')] == 2
     assert args.keywords_w[args.keywords.index('b')] == 3        
예제 #7
0
파일: loop.py 프로젝트: zielmicha/pypy
def call_many_to_many(space, shape, func, res_dtype, in_args, out_args):
    # out must hav been built. func needs no calc_type, is usually an
    # external ufunc
    nin = len(in_args)
    in_iters = [None] * nin
    in_states = [None] * nin
    nout = len(out_args)
    out_iters = [None] * nout
    out_states = [None] * nout
    for i in range(nin):
        in_i = in_args[i]
        assert isinstance(in_i, W_NDimArray)
        in_iter, in_state = in_i.create_iter(shape)
        in_iters[i] = in_iter
        in_states[i] = in_state
    for i in range(nout):
        out_i = out_args[i]
        assert isinstance(out_i, W_NDimArray)
        out_iter, out_state = out_i.create_iter(shape)
        out_iters[i] = out_iter
        out_states[i] = out_state
    shapelen = len(shape)
    vals = [None] * nin
    while not out_iters[0].done(out_states[0]):
        call_many_to_many_driver.jit_merge_point(shapelen=shapelen,
                                                 func=func,
                                                 res_dtype=res_dtype,
                                                 nin=nin,
                                                 nout=nout)
        for i in range(nin):
            vals[i] = in_iters[i].getitem(in_states[i])
        w_arglist = space.newlist(vals)
        w_outvals = space.call_args(func,
                                    Arguments.frompacked(space, w_arglist))
        # w_outvals should be a tuple, but func can return a single value as well
        if space.isinstance_w(w_outvals, space.w_tuple):
            batch = space.listview(w_outvals)
            for i in range(len(batch)):
                out_iters[i].setitem(out_states[i],
                                     res_dtype.coerce(space, batch[i]))
                out_states[i] = out_iters[i].next(out_states[i])
        else:
            out_iters[0].setitem(out_states[0],
                                 res_dtype.coerce(space, w_outvals))
            out_states[0] = out_iters[0].next(out_states[0])
        for i in range(nin):
            in_states[i] = in_iters[i].next(in_states[i])
    return space.newtuple([convert_to_array(space, o) for o in out_args])
예제 #8
0
    def test_topacked_frompacked(self):
        space = DummySpace()
        args = Arguments(space, [1], ["a", "b"], [2, 3])
        w_args, w_kwds = args.topacked()
        assert w_args == (1,)
        assert w_kwds == {"a": 2, "b": 3}
        args1 = Arguments.frompacked(space, w_args, w_kwds)
        assert args.arguments_w == [1]
        assert set(args.keywords) == set(["a", "b"])
        assert args.keywords_w[args.keywords.index("a")] == 2
        assert args.keywords_w[args.keywords.index("b")] == 3

        args = Arguments(space, [1])
        w_args, w_kwds = args.topacked()
        assert w_args == (1,)
        assert not w_kwds
예제 #9
0
파일: loop.py 프로젝트: abhinavthomas/pypy
def call_many_to_many(space, shape, func, in_dtypes, out_dtypes, in_args, out_args):
    # out must hav been built. func needs no calc_type, is usually an
    # external ufunc
    nin = len(in_args)
    in_iters = [None] * nin
    in_states = [None] * nin
    nout = len(out_args)
    out_iters = [None] * nout
    out_states = [None] * nout
    for i in range(nin):
        in_i = in_args[i]
        assert isinstance(in_i, W_NDimArray)
        in_iter, in_state = in_i.create_iter(shape)
        in_iters[i] = in_iter
        in_states[i] = in_state
    for i in range(nout):
        out_i = out_args[i]
        assert isinstance(out_i, W_NDimArray)
        out_iter, out_state = out_i.create_iter(shape)
        out_iters[i] = out_iter
        out_states[i] = out_state
    shapelen = len(shape)
    vals = [None] * nin
    test_iter, test_state = in_iters[-1], in_states[-1]
    if nout > 0:
        test_iter, test_state = out_iters[0], out_states[0]
    while not test_iter.done(test_state):
        call_many_to_many_driver.jit_merge_point(shapelen=shapelen, func=func,
                             in_dtypes=in_dtypes, out_dtypes=out_dtypes,
                             nin=nin, nout=nout)
        for i in range(nin):
            vals[i] = in_dtypes[i].coerce(space, in_iters[i].getitem(in_states[i]))
        w_arglist = space.newlist(vals)
        w_outvals = space.call_args(func, Arguments.frompacked(space, w_arglist))
        # w_outvals should be a tuple, but func can return a single value as well 
        if space.isinstance_w(w_outvals, space.w_tuple):
            batch = space.listview(w_outvals)
            for i in range(len(batch)):
                out_iters[i].setitem(out_states[i], out_dtypes[i].coerce(space, batch[i]))
                out_states[i] = out_iters[i].next(out_states[i])
        elif nout > 0:
            out_iters[0].setitem(out_states[0], out_dtypes[0].coerce(space, w_outvals))
            out_states[0] = out_iters[0].next(out_states[0])
        for i in range(nin):
            in_states[i] = in_iters[i].next(in_states[i])
        test_state = test_iter.next(test_state)
    return space.newtuple([convert_to_array(space, o) for o in out_args])
예제 #10
0
 def descr__setstate__(self, space, w_args):
     w_flags, w_state, w_thunk, w_parent = space.unpackiterable(w_args,
                                                     expected_length=4)
     self.flags = space.int_w(w_flags)
     if space.is_w(w_parent, space.w_None):
         w_parent = self.w_getmain(space)
     self.parent = space.interp_w(AppCoroutine, w_parent)
     ec = self.space.getexecutioncontext()
     self.subctx.setstate(space, w_state)
     if space.is_w(w_thunk, space.w_None):
         if space.is_w(w_state, space.w_None):
             self.thunk = None
         else:
             self.bind(_ResumeThunk(space, self.costate, self.subctx.topframe))
     else:
         w_func, w_args, w_kwds = space.unpackiterable(w_thunk,
                                                       expected_length=3)
         args = Arguments.frompacked(space, w_args, w_kwds)
         self.bind(_AppThunk(space, self.costate, w_func, args))
예제 #11
0
 def call(self, w_callable, w_args, w_kwds=None):
     args = Arguments.frompacked(self, w_args, w_kwds)
     return self.call_args(w_callable, args)
예제 #12
0
def slot_tp_init(space, w_self, w_args, w_kwds):
    w_descr = space.lookup(w_self, '__init__')
    args = Arguments.frompacked(space, w_args, w_kwds)
    space.get_and_call_args(w_descr, w_self, args)
    return 0
예제 #13
0
파일: slotdefs.py 프로젝트: charred/pypy
def slot_tp_init(space, w_self, w_args, w_kwds):
    w_descr = space.lookup(w_self, '__init__')
    args = Arguments.frompacked(space, w_args, w_kwds)
    space.get_and_call_args(w_descr, w_self, args)
    return 0
예제 #14
0
class AppCoroutine(Coroutine):  # XXX, StacklessFlags):
    def __init__(self, space, state=None):
        self.space = space
        if state is None:
            state = AppCoroutine._get_state(space)
        Coroutine.__init__(self, state)
        self.flags = 0
        self.newsubctx()

    def newsubctx(self):
        ec = self.space.getexecutioncontext()
        self.subctx = ec.Subcontext()

    def descr_method__new__(space, w_subtype):
        co = space.allocate_instance(AppCoroutine, w_subtype)
        AppCoroutine.__init__(co, space)
        return space.wrap(co)

    def _get_state(space):
        return space.fromcache(AppCoState)

    _get_state = staticmethod(_get_state)

    def w_bind(self, w_func, __args__):
        space = self.space
        if self.frame is not None:
            raise OperationError(space.w_ValueError,
                                 space.wrap("cannot bind a bound Coroutine"))
        state = self.costate
        thunk = _AppThunk(space, state, w_func, __args__)
        self.bind(thunk)

    def w_switch(self):
        space = self.space
        if self.frame is None:
            raise OperationError(
                space.w_ValueError,
                space.wrap("cannot switch to an unbound Coroutine"))
        state = self.costate
        self.switch()
        rstack.resume_point("w_switch", state, space)
        w_ret, state.w_tempval = state.w_tempval, space.w_None
        return w_ret

    def w_finished(self, w_excinfo):
        pass

    def finish(self, operror=None):
        space = self.space
        if isinstance(operror, OperationError):
            w_exctype = operror.w_type
            w_excvalue = operror.get_w_value(space)
            w_exctraceback = operror.application_traceback
            w_excinfo = space.newtuple([w_exctype, w_excvalue, w_exctraceback])
        else:
            w_N = space.w_None
            w_excinfo = space.newtuple([w_N, w_N, w_N])

        return space.call_method(space.wrap(self), 'finished', w_excinfo)

    def hello(self):
        ec = self.space.getexecutioncontext()
        self.subctx.enter(ec)

    def goodbye(self):
        ec = self.space.getexecutioncontext()
        self.subctx.leave(ec)

    def w_kill(self):
        self.kill()

    def _userdel(self):
        if self.get_is_zombie():
            return
        self.set_is_zombie(True)
        self.space.userdel(self.space.wrap(self))

    def w_getcurrent(space):
        return space.wrap(AppCoroutine._get_state(space).current)

    w_getcurrent = staticmethod(w_getcurrent)

    def w_getmain(space):
        return space.wrap(AppCoroutine._get_state(space).main)

    w_getmain = staticmethod(w_getmain)

    # pickling interface
    def descr__reduce__(self, space):
        # this is trying to be simplistic at the moment.
        # we neither allow to pickle main (which can become a mess
        # since it has some deep anchestor frames)
        # nor we allow to pickle the current coroutine.
        # rule: switch before pickling.
        # you cannot construct the tree that you are climbing.
        from pypy.interpreter.mixedmodule import MixedModule
        w_mod = space.getbuiltinmodule('_stackless')
        mod = space.interp_w(MixedModule, w_mod)
        w_mod2 = space.getbuiltinmodule('_pickle_support')
        mod2 = space.interp_w(MixedModule, w_mod2)
        w_new_inst = mod.get('coroutine')
        w = space.wrap
        nt = space.newtuple
        ec = self.space.getexecutioncontext()

        if self is self.costate.main:
            return nt([mod.get('_return_main'), nt([])])

        thunk = self.thunk
        if isinstance(thunk, _AppThunk):
            w_args, w_kwds = thunk.args.topacked()
            w_thunk = nt([thunk.w_func, w_args, w_kwds])
        else:
            w_thunk = space.w_None

        tup_base = []
        tup_state = [
            w(self.flags),
            self.subctx.getstate(space),
            w_thunk,
            w(self.parent),
        ]

        return nt([w_new_inst, nt(tup_base), nt(tup_state)])

    def descr__setstate__(self, space, w_args):
        try:
            w_flags, w_state, w_thunk, w_parent = space.unpackiterable(
                w_args, expected_length=4)
        except UnpackValueError, e:
            raise OperationError(space.w_ValueError, space.wrap(e.msg))
        self.flags = space.int_w(w_flags)
        if space.is_w(w_parent, space.w_None):
            w_parent = self.w_getmain(space)
        self.parent = space.interp_w(AppCoroutine, w_parent)
        ec = self.space.getexecutioncontext()
        self.subctx.setstate(space, w_state)
        self.reconstruct_framechain()
        if space.is_w(w_thunk, space.w_None):
            self.thunk = None
        else:
            try:
                w_func, w_args, w_kwds = space.unpackiterable(
                    w_thunk, expected_length=3)
            except UnpackValueError, e:
                raise OperationError(space.w_ValueError, space.wrap(e.msg))
            args = Arguments.frompacked(space, w_args, w_kwds)
            self.bind(_AppThunk(space, self.costate, w_func, args))
예제 #15
0
 def call(self, w_callable, w_args, w_kwds=None):
     args = Arguments.frompacked(self, w_args, w_kwds)
     return self.call_args(w_callable, args)