Esempio n. 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)
Esempio n. 2
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)
Esempio n. 3
0
def rpython2javascript(mod, function_names, jsconfig=None, use_pdb=True):
    if isinstance(function_names, str):
        function_names = [function_names]
        # avoid confusion
    if mod is None:
        # this means actual module, which is quite hairy to get in python,
        # so we cheat
        import sys
        mod = sys.modules[sys._getframe(1).f_globals['__name__']]
    
    if jsconfig is None:
        jsconfig = Config(js_optiondescr)
    if use_pdb:
        jsconfig.use_pdb = True
    module_name = mod.__name__
    if not function_names and 'main' in mod.__dict__:
        function_names.append('main')
    for func_name in function_names:
        if func_name not in mod.__dict__:
            raise FunctionNotFound("function %r was not found in module %r" % (func_name, module_name))
        func_code = mod.__dict__[func_name]
        if func_code.func_defaults:
            lgt = len(func_code.func_defaults)
        else:
            lgt = 0
        if func_code.func_code.co_argcount > 0 and func_code.func_code. \
                co_argcount != lgt:
            raise BadSignature("Function %s does not have default arguments" % func_name)
    source_ssf = get_source_ssf(mod, module_name, function_names)
    exec(source_ssf) in globals()
    # now we gonna just cut off not needed function
    # XXX: Really do that
    #options = optparse.Values(defaults=DEFAULT_OPTIONS)
    from pypy.config.pypyoption import get_pypy_config
    config = get_pypy_config(translating=True)
    driver = TranslationDriver(config=config)
    try:
        driver.setup(some_strange_function_which_will_never_be_called, [], policy = JsPolicy())
        driver.proceed(["compile_js"])
        if jsconfig.view:
            driver.translator.view()
        return driver.gen.tmpfile.open().read()
        # XXX: Add some possibility to write down selected file
    except Exception, e:
        # do something nice with it
        debug(driver, use_pdb)
Esempio n. 4
0
        except OperationError, e:
            reraise(e)

    __init__.allow_someobjects = True

    driver = TranslationDriver(extmod_name=modname)
    driver.setup(__init__, [object], policy=CPyAnnotatorPolicy(space))
    try:
        driver.proceed(['compile_c'])
    except SystemExit:
        raise
    except:
        if not interactive:
            raise
        debug(driver)
        raise SystemExit(1)
    return driver.cbuilder.c_ext_module

def main(argv):
    usage = """usage: %prog [options] MODULENAME

Compiles a PyPy extension module 
into a regular CPython extension module.

The module is a package with rpython interplevel code,
python applevel code,
and corresponding exports correctly declared."""
    parser = OptionParser(usage)
    parser.add_option("-p", "--package",
                  dest="basepath", default="",