Exemple #1
0
class DllDef:
    def __init__(self,
                 name,
                 namespace,
                 functions=[],
                 dontmangle=True,
                 isnetmodule=False):
        self.name = name
        self.namespace = namespace
        self.functions = functions  # [(function, annotation), ...]
        self.isnetmodule = isnetmodule
        self.driver = TranslationDriver()
        if dontmangle:
            self.driver.config.translation.ootype.mangle = False
        self.driver.setup_library(self)

    def add_function(self, func, inputtypes):
        self.functions.append((func, inputtypes))

    def get_entrypoint(self, bk):
        graphs = [bk.getdesc(f).cachedgraph(None) for f, _ in self.functions]
        return DllEntryPoint(self.name, graphs, self.isnetmodule)

    def compile(self):
        # add all functions to the appropriate namespace
        if self.namespace:
            for func, _ in self.functions:
                if not hasattr(func, '_namespace_'):
                    func._namespace_ = self.namespace
        self.driver.proceed(['compile_cli'])
Exemple #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(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)
Exemple #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()

    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)
Exemple #4
0
class DllDef:
    def __init__(self, name, namespace, functions=[], dontmangle=True, isnetmodule=False):
        self.name = name
        self.namespace = namespace
        self.functions = functions # [(function, annotation), ...]
        self.isnetmodule = isnetmodule
        self.driver = TranslationDriver()
        if dontmangle:
            self.driver.config.translation.ootype.mangle = False
        self.driver.setup_library(self)

    def add_function(self, func, inputtypes):
        self.functions.append((func, inputtypes))

    def get_entrypoint(self, bk):
        graphs = [bk.getdesc(f).cachedgraph(None) for f, _ in self.functions]
        return DllEntryPoint(self.name, graphs, self.isnetmodule)

    def compile(self):
        # add all functions to the appropriate namespace
        if self.namespace:
            for func, _ in self.functions:
                if not hasattr(func, '_namespace_'):
                    func._namespace_ = self.namespace
        self.driver.proceed(['compile_cli'])
Exemple #5
0
class DLLDef(object):
    def __init__(self, name, functions=[], policy=None, config=None):
        self.name = name
        self.functions = functions # [(function, annotation), ...]
        self.driver = TranslationDriver(config=config)
        self.driver.setup_library(self, policy=policy)

    def compile(self):
        self.driver.proceed(['compile_c'])
        return self.driver.c_entryp

    def getcbuilder(self, translator, config):
        return CLibraryBuilder(translator, None, config,
                               functions=self.functions, name=self.name)
Exemple #6
0
class DLLDef(object):
    def __init__(self, name, functions=[], policy=None, config=None):
        self.name = name
        self.functions = functions  # [(function, annotation), ...]
        self.driver = TranslationDriver(config=config)
        self.driver.setup_library(self, policy=policy)

    def compile(self):
        self.driver.proceed(['compile_c'])
        return self.driver.c_entryp

    def getcbuilder(self, translator, config):
        return CLibraryBuilder(translator,
                               None,
                               config,
                               functions=self.functions,
                               name=self.name)
Exemple #7
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)
Exemple #8
0
                    w1 = space.getitem(w_old, w_key)
                except OperationError:
                    pass
                else:
                    space.setitem(w_moddict, w_key, w1)
            space.call_method(w_moddict, 'update', w_moduledict)

        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.