示例#1
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)
示例#2
0
def checkmodule(*modnames, **kwds):
    translate_startup = kwds.pop('translate_startup', True)
    assert not kwds
    config = get_pypy_config(translating=True)
    space = FakeObjSpace(config)
    seeobj_w = []
    modules = []
    for modname in modnames:
        mod = __import__('pypy.module.%s' % modname, None, None, ['__doc__'])
        # force computation and record what we wrap
        module = mod.Module(space, W_Root())
        module.setup_after_space_initialization()
        module.init(space)
        modules.append(module)
        for name in module.loaders:
            seeobj_w.append(module._load_lazily(space, name))
        if hasattr(module, 'submodules'):
            for cls in module.submodules.itervalues():
                submod = cls(space, W_Root())
                for name in submod.loaders:
                    seeobj_w.append(submod._load_lazily(space, name))
    #
    def func():
        for mod in modules:
            mod.startup(space)
    if not translate_startup:
        func()   # call it now
        func = None
    space.translates(func, seeobj_w=seeobj_w,
                     **{'translation.list_comprehension_operations': True})
示例#3
0
def test_wrap_interp2app_int():
    see, check = make_checker()
    def foobar(space, x, w_y, z):
        is_root(w_y)
        see()
        return space.wrap(x - z)
    space = FakeObjSpace()
    space.wrap(interp2app(foobar, unwrap_spec=[ObjSpace, int, W_Root, int]))
    space.translates()
    assert check
示例#4
0
def test_wrap_GetSetProperty():
    see, check = make_checker()
    def foobar(w_obj, space):
        is_root(w_obj)
        see()
        return space.w_None
    space = FakeObjSpace()
    space.wrap(GetSetProperty(foobar))
    space.translates()
    assert check
示例#5
0
def test_wrap_interp2app():
    see, check = make_checker()
    space = FakeObjSpace()
    assert len(space._seen_extras) == 0
    assert len(check) == 0
    space.wrap(interp2app(lambda space: see()))
    assert len(space._seen_extras) == 1
    assert len(check) == 0
    space.translates()
    assert len(check) == 1
示例#6
0
def checkmodule(modname):
    config = get_pypy_config(translating=True)
    space = FakeObjSpace(config)
    mod = __import__('pypy.module.%s' % modname, None, None, ['__doc__'])
    # force computation and record what we wrap
    module = mod.Module(space, W_Root())
    for name in module.loaders:
        module._load_lazily(space, name)
    #
    space.translates(**{'translation.list_comprehension_operations':True})
示例#7
0
def test_gettypefor_untranslated():
    see, check = make_checker()

    class W_Foo(Wrappable):
        def do_it(self, space, w_x):
            is_root(w_x)
            see()
            return W_Root()

    W_Foo.typedef = TypeDef("foo", __module__="barmod", do_it=interp2app(W_Foo.do_it))
    space = FakeObjSpace()
    space.gettypefor(W_Foo)
    assert not check
    space.translates()
    assert check
示例#8
0
def test_wrap_interp2app_later():
    see, check = make_checker()
    #
    @specialize.memo()
    def hithere(space):
        space.wrap(interp2app(foobar2))
    #
    def foobar(space):
        hithere(space)
    def foobar2(space):
        see()
    space = FakeObjSpace()
    space.wrap(interp2app(foobar))
    space.translates()
    assert check
示例#9
0
def checkmodule(*modnames):
    config = get_pypy_config(translating=True)
    space = FakeObjSpace(config)
    seeobj_w = []
    for modname in modnames:
        mod = __import__('pypy.module.%s' % modname, None, None, ['__doc__'])
        # force computation and record what we wrap
        module = mod.Module(space, W_Root())
        for name in module.loaders:
            seeobj_w.append(module._load_lazily(space, name))
        if hasattr(module, 'submodules'):
            for cls in module.submodules.itervalues():
                submod = cls(space, W_Root())
                for name in submod.loaders:
                    seeobj_w.append(submod._load_lazily(space, name))
    #
    space.translates(seeobj_w=seeobj_w,
                     **{'translation.list_comprehension_operations': True})
示例#10
0
def test_see_objects():
    see, check = make_checker()
    class W_Foo(W_Root):
        def __init__(self, x):
            self.x = x
        def do_it(self):
            if self.x == 42:
                return
            see()
    def f():
        W_Foo(42).do_it()
    #
    space = FakeObjSpace()
    space.translates(f)
    assert not check
    #
    space = FakeObjSpace()
    space.translates(f, seeobj_w=[W_Foo(15)])
    assert check
示例#11
0
class TestTranslate:
    def setup_method(self, meth):
        self.space = FakeObjSpace()

    def test_simple(self):
        space = self.space
        space.translates(lambda w_x, w_y: space.add(w_x, w_y))

    def test_methodtable(self):
        space = self.space
        for fixed_arity in [1, 2, 3, 4]:
            #
            methodtable = [
                name for (name, _, arity, _) in space.MethodTable
                if arity == fixed_arity
            ]
            methodtable = unrolling_iterable(methodtable)
            args_w = (W_Root(), ) * fixed_arity

            #
            def f():
                for name in methodtable:
                    getattr(space, name)(*args_w)

            #
            space.translates(f)

    def test_newdict(self):
        space = self.space
        space.translates(lambda:
                         (space.newdict(), space.newdict(strdict=True)))

    def test_constants(self):
        space = self.space
        space.translates(
            lambda: (space.w_None, space.w_True, space.w_False, space.w_int,
                     space.w_bytes, space.w_object, space.w_TypeError))

    def test_wrap(self):
        space = self.space
        space.translates(lambda: (space.newint(42), space.newfloat(42.5),
                                  space.newtext("foo")))

    def test_call_args(self):
        space = self.space
        args = Arguments(space, [W_Root()])
        space.translates(lambda: space.call_args(W_Root(), args))

    def test_gettypefor(self):
        space = self.space

        class W_Foo(W_Root):
            typedef = TypeDef("foo")

        space.translates(lambda: space.gettypefor(W_Foo))

    def test_is_true(self):
        space = self.space
        space.translates(lambda: space.is_true(W_Root()))
        with pytest.raises(AssertionError):
            space.translates(lambda: space.is_true(42))

    def test_unpackiterable(self):
        space = self.space
        space.translates(lambda: (space.unpackiterable(W_Root()),
                                  space.unpackiterable(W_Root(), 42)))

    def test_newlist(self):
        self.space.newlist([W_Root(), W_Root()])

    def test_default_values(self):
        # the __get__ method takes either 2 or 3 arguments
        space = self.space
        space.translates(lambda: (space.get(W_Root(), W_Root()),
                                  space.get(W_Root(), W_Root(), W_Root())))

    def test_bug_utf8_len_w(self):
        space = self.space

        class A(object):
            pass

        def f():
            s = NonConstant('a')
            w_s = space.newutf8(s, 1)
            t, l = space.utf8_len_w(w_s)
            a = A()
            if l == 1:
                a.x = 1
            else:
                raise Exception
            return a.x

        space.translates(f)
示例#12
0
def test_gettype_mro_untranslated():
    space = FakeObjSpace()
    w_type = space.type(space.wrap(1))
    assert len(w_type.mro_w) == 2
示例#13
0
class TestTranslate:
    def setup_method(self, meth):
        self.space = FakeObjSpace()

    def test_simple(self):
        space = self.space
        space.translates(lambda w_x, w_y: space.add(w_x, w_y))

    def test_methodtable(self):
        space = self.space
        for fixed_arity in [1, 2, 3, 4]:
            #
            methodtable = [
                name for (name, _, arity, _) in space.MethodTable
                if arity == fixed_arity
            ]
            methodtable = unrolling_iterable(methodtable)
            args_w = (W_Root(), ) * fixed_arity

            #
            def f():
                for name in methodtable:
                    getattr(space, name)(*args_w)

            #
            space.translates(f)

    def test_newdict(self):
        space = self.space
        space.translates(lambda:
                         (space.newdict(), space.newdict(strdict=True)))

    def test_constants(self):
        space = self.space
        space.translates(
            lambda: (space.w_None, space.w_True, space.w_False, space.w_int,
                     space.w_str, space.w_object, space.w_TypeError))

    def test_wrap(self):
        space = self.space
        space.translates(lambda:
                         (space.wrap(42), space.wrap(42.5), space.wrap("foo")))

    def test_call_args(self):
        space = self.space
        args = Arguments(space, [W_Root()])
        space.translates(lambda: space.call_args(W_Root(), args))

    def test_gettypefor(self):
        space = self.space

        class W_Foo(W_Root):
            typedef = TypeDef("foo")

        space.translates(lambda: space.gettypefor(W_Foo))

    def test_is_true(self):
        space = self.space
        space.translates(lambda: space.is_true(W_Root()))
        py.test.raises(AssertionError, space.translates,
                       lambda: space.is_true(42))

    def test_unpackiterable(self):
        space = self.space
        space.translates(lambda: (space.unpackiterable(W_Root()),
                                  space.unpackiterable(W_Root(), 42)))

    def test_newlist(self):
        self.space.newlist([W_Root(), W_Root()])

    def test_default_values(self):
        # the __get__ method takes either 2 or 3 arguments
        space = self.space
        space.translates(lambda: (space.get(W_Root(), W_Root()),
                                  space.get(W_Root(), W_Root(), W_Root())))
示例#14
0
 def setup_method(self, meth):
     self.space = FakeObjSpace()
示例#15
0
def test_create():
    FakeObjSpace()
示例#16
0
def test_gettype_mro_untranslated():
    space = FakeObjSpace()
    w_type = space.type(space.wrap(1))
    assert len(w_type.mro_w) == 2
示例#17
0
 def setup_method(self, meth):
     self.space = FakeObjSpace()
示例#18
0
class TestTranslate:
    def setup_method(self, meth):
        self.space = FakeObjSpace()

    def test_simple(self):
        space = self.space
        space.translates(lambda w_x, w_y: space.add(w_x, w_y))

    def test_methodtable(self):
        space = self.space
        for fixed_arity in [1, 2, 3, 4]:
            #
            methodtable = [name for (name, _, arity, _) in space.MethodTable
                                if arity == fixed_arity]
            methodtable = unrolling_iterable(methodtable)
            args_w = (W_Root(),) * fixed_arity
            #
            def f():
                for name in methodtable:
                    getattr(space, name)(*args_w)
            #
            space.translates(f)

    def test_newdict(self):
        space = self.space
        space.translates(lambda: (space.newdict(),
                                  space.newdict(strdict=True)))

    def test_constants(self):
        space = self.space
        space.translates(lambda: (space.w_None, space.w_True, space.w_False,
                                  space.w_int, space.w_str, space.w_object,
                                  space.w_TypeError))

    def test_wrap(self):
        space = self.space
        space.translates(lambda: (space.wrap(42), space.wrap(42.5),
                                  space.wrap("foo")))

    def test_call_args(self):
        space = self.space
        args = Arguments(space, [W_Root()])
        space.translates(lambda: space.call_args(W_Root(), args))

    def test_gettypefor(self):
        space = self.space

        class W_Foo(W_Root):
            typedef = TypeDef("foo")
        space.translates(lambda: space.gettypefor(W_Foo))

    def test_is_true(self):
        space = self.space
        space.translates(lambda: space.is_true(W_Root()))
        py.test.raises(AssertionError,
                       space.translates, lambda: space.is_true(42))

    def test_unpackiterable(self):
        space = self.space
        space.translates(lambda: (space.unpackiterable(W_Root()),
                                  space.unpackiterable(W_Root(), 42)))

    def test_newlist(self):
        self.space.newlist([W_Root(), W_Root()])

    def test_default_values(self):
        # the __get__ method takes either 2 or 3 arguments
        space = self.space
        space.translates(lambda: (space.get(W_Root(), W_Root()),
                                  space.get(W_Root(), W_Root(), W_Root())))