Example #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()

    gateways = find_gateways(modname, basepath, module)
    functions = [gw.__spacebind__(space) for gw in gateways]
    arguments = AbstractArguments.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)
Example #2
0
def checkmodule(*modnames, **kwds):
    translate_startup = kwds.pop('translate_startup', True)
    ignore = set(kwds.pop('ignore', ()))
    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:
            if name in ignore:
                continue
            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})
Example #3
0
def test_gettype_mro():
    space = FakeObjSpace()

    def f(i):
        w_x = space.wrap(i)
        w_type = space.type(w_x)
        return len(w_type.mro_w)

    assert interpret(f, [1]) == 2
Example #4
0
def test_wrap_interp2app():
    see, check = make_checker()
    space = FakeObjSpace()
    assert len(space._seen_extras) == 1
    assert len(check) == 0
    space.wrap(interp2app(lambda space: see()))
    assert len(space._seen_extras) == 2
    assert len(check) == 0
    space.translates()
    assert len(check) == 1
Example #5
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
Example #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})
Example #7
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
Example #8
0
def test_wrap_interp2app_int():
    see, check = make_checker()
    def foobar(space, x, w_y, z):
        is_root(w_y)
        see()
        return space.newint(x - z)
    space = FakeObjSpace()
    space.wrap(interp2app(foobar, unwrap_spec=[ObjSpace, int, W_Root, int]))
    space.translates()
    assert check
Example #9
0
def test_gettypefor_untranslated():
    see, check = make_checker()
    class W_Foo(W_Root):
        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
Example #10
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
Example #11
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())
        module.startup(space)
        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})
Example #12
0
def test_gettype_mro_untranslated():
    space = FakeObjSpace()
    w_type = space.type(space.wrap(1))
    assert len(w_type.mro_w) == 2
Example #13
0
def checkmodule(modname,
                translate_startup=True,
                ignore=(),
                c_compile=False,
                extra_func=None,
                rpython_opts=None,
                pypy_opts=None,
                show_pdbplus=False):
    """
    Check that the module 'modname' translates.

    Options:
      translate_startup: TODO, document me

      ignore:       list of module interpleveldefs/appleveldefs to ignore

      c_compile:    determine whether to inokve the C compiler after rtyping

      extra_func:   extra function which will be annotated and called. It takes
                    a single "space" argment

      rpython_opts: dictionariy containing extra configuration options
      pypy_opts:    dictionariy containing extra configuration options

      show_pdbplus: show Pdb+ prompt on error. Useful for pdb commands such as
                    flowg, callg, etc.
    """
    config = get_pypy_config(translating=True)
    if pypy_opts:
        config.set(**pypy_opts)
    space = FakeObjSpace(config)
    seeobj_w = []
    modules = []
    modnames = [modname]
    for modname in modnames:
        mod = __import__('pypy.module.%s.moduledef' % 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:
            if name in ignore:
                continue
            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

    opts = {'translation.list_comprehension_operations': True}
    if rpython_opts:
        opts.update(rpython_opts)

    try:
        space.translates(func,
                         seeobj_w=seeobj_w,
                         c_compile=c_compile,
                         extra_func=extra_func,
                         **opts)
    except:
        if not show_pdbplus:
            raise
        print
        exc, val, tb = sys.exc_info()
        traceback.print_exc()
        sys.pdbplus = p = PdbPlusShow(space.t)
        p.start(tb)
    else:
        if show_pdbplus:
            sys.pdbplus = p = PdbPlusShow(space.t)
            p.start(None)
Example #14
0
def test_create():
    FakeObjSpace()
Example #15
0
 def setup_method(self, meth):
     self.space = FakeObjSpace()