示例#1
0
def load_cffi1_module(space, name, path, initptr):
    # This is called from pypy.module.cpyext.api.load_extension_module()
    from pypy.module._cffi_backend.call_python import get_ll_cffi_call_python

    initfunc = rffi.cast(INITFUNCPTR, initptr)
    with lltype.scoped_alloc(rffi.VOIDPP.TO, 16, zero=True) as p:
        p[0] = rffi.cast(rffi.VOIDP, VERSION_EXPORT)
        p[1] = rffi.cast(rffi.VOIDP, get_ll_cffi_call_python())
        initfunc(p)
        version = rffi.cast(lltype.Signed, p[0])
        if not (VERSION_MIN <= version <= VERSION_MAX):
            raise oefmt(space.w_ImportError,
                "cffi extension module '%s' uses an unknown version tag %s. "
                "This module might need a more recent version of PyPy. "
                "The current PyPy provides CFFI %s.",
                name, hex(version), _cffi_backend.VERSION)
        src_ctx = rffi.cast(parse_c_type.PCTX, p[1])

    ffi = W_FFIObject(space, src_ctx)
    lib = W_LibObject(ffi, name)
    if src_ctx.c_includes:
        lib.make_includes_from(src_ctx.c_includes)

    w_name = space.wrap(name)
    module = Module(space, w_name)
    if path is not None:
        module.setdictvalue(space, '__file__', space.wrap(path))
    module.setdictvalue(space, 'ffi', space.wrap(ffi))
    module.setdictvalue(space, 'lib', space.wrap(lib))
    w_modules_dict = space.sys.get('modules')
    space.setitem(w_modules_dict, w_name, space.wrap(module))
    space.setitem(w_modules_dict, space.wrap(name + '.lib'), space.wrap(lib))
示例#2
0
 def initialize(self):
     self.concrete_mode = 1
     self.w_None     = Constant(None)
     self.builtin    = Module(self, Constant('__builtin__'),
                              Constant(__builtin__.__dict__))
     def pick_builtin(w_globals):
         return self.builtin
     self.builtin.pick_builtin = pick_builtin
     self.sys        = Module(self, Constant('sys'), Constant(sys.__dict__))
     self.sys.recursionlimit = 100
     self.w_False    = Constant(False)
     self.w_True     = Constant(True)
     self.w_type     = Constant(type)
     self.w_tuple    = Constant(tuple)
     self.concrete_mode = 0
     for exc in [KeyError, ValueError, IndexError, StopIteration,
                 AssertionError, TypeError, AttributeError, ImportError]:
         clsname = exc.__name__
         setattr(self, 'w_'+clsname, Constant(exc))
     # the following exceptions are the ones that should not show up
     # during flow graph construction; they are triggered by
     # non-R-Pythonic constructs or real bugs like typos.
     for exc in [NameError, UnboundLocalError]:
         clsname = exc.__name__
         setattr(self, 'w_'+clsname, None)
     self.specialcases = {}
     #self.make_builtins()
     #self.make_sys()
     # w_str is needed because cmp_exc_match of frames checks against it,
     # as string exceptions are deprecated
     self.w_str = Constant(str)
     # objects which should keep their SomeObjectness
     self.not_really_const = NOT_REALLY_CONST
示例#3
0
def load_cffi1_module(space, name, path, initptr):
    # This is called from pypy.module.cpyext.api.load_extension_module()
    initfunc = rffi.cast(initfunctype, initptr)
    with lltype.scoped_alloc(rffi.VOIDPP.TO, 2) as p:
        p[0] = rffi.cast(rffi.VOIDP, VERSION_EXPORT)
        initfunc(p)
        version = rffi.cast(lltype.Signed, p[0])
        if not (VERSION_MIN <= version <= VERSION_MAX):
            raise oefmt(space.w_ImportError,
                "cffi extension module '%s' has unknown version %s",
                name, hex(version))
        src_ctx = rffi.cast(parse_c_type.PCTX, p[1])

    ffi = W_FFIObject(space, src_ctx)
    lib = W_LibObject(ffi, name)
    if src_ctx.c_includes:
        lib.make_includes_from(src_ctx.c_includes)

    w_name = space.wrap(name)
    module = Module(space, w_name)
    module.setdictvalue(space, '__file__', space.wrap(path))
    module.setdictvalue(space, 'ffi', space.wrap(ffi))
    module.setdictvalue(space, 'lib', space.wrap(lib))
    w_modules_dict = space.sys.get('modules')
    space.setitem(w_modules_dict, w_name, space.wrap(module))
    space.setitem(w_modules_dict, space.wrap(name + '.lib'), space.wrap(lib))
示例#4
0
 def __init__(self, space, w_name):
     """ NOT_RPYTHON """
     Module.__init__(self, space, w_name)
     self.lazy = True
     self.__class__.buildloaders()
     self.loaders = self.loaders.copy()    # copy from the class to the inst
     self.submodules_w = []
示例#5
0
 def __init__(self, space, w_name):
     """ NOT_RPYTHON """
     Module.__init__(self, space, w_name)
     self.lazy = True
     self.__class__.buildloaders()
     self.loaders = self.loaders.copy()  # copy from the class to the inst
     self.submodules_w = []
 def __init__(self, space, w_name):
     Module.__init__(self, space, w_name)
     init_extra_module_attrs(space, self)
     self.lazy = True
     self.lazy_initial_values_w = {}
     self.__class__.buildloaders()
     self.loaders = self.loaders.copy()  # copy from the class to the inst
     self.submodules_w = []
示例#7
0
 def test___file__(self, space):
     w = space.wrap
     m = Module(space, space.wrap('m'))
     py.test.raises(OperationError, space.getattr, w(m), w('__file__'))
     m._cleanup_()
     py.test.raises(OperationError, space.getattr, w(m), w('__file__'))
     space.setattr(w(m), w('__file__'), w('m.py'))
     space.getattr(w(m), w('__file__'))   # does not raise
     m._cleanup_()
     py.test.raises(OperationError, space.getattr, w(m), w('__file__'))
示例#8
0
 def install(self):
     """NOT_RPYTHON: install this module, and it's submodules into
     space.builtin_modules"""
     Module.install(self)
     if hasattr(self, "submodules"):
         space = self.space
         name = space.unwrap(self.w_name)
         for sub_name, module_cls in self.submodules.iteritems():
             module_name = space.wrap("%s.%s" % (name, sub_name))
             m = module_cls(space, module_name)
             m.install()
             self.submodules_w.append(m)
示例#9
0
 def install(self):
     """NOT_RPYTHON: install this module, and it's submodules into
     space.builtin_modules"""
     Module.install(self)
     if hasattr(self, "submodules"):
         space = self.space
         name = space.unwrap(self.w_name)
         for sub_name, module_cls in self.submodules.iteritems():
             module_name = space.wrap("%s.%s" % (name, sub_name))
             m = module_cls(space, module_name)
             m.install()
             self.submodules_w.append(m)
示例#10
0
 def install(self):
     """install this module, and it's submodules into
     space.builtin_modules"""
     Module.install(self)
     if hasattr(self, "submodules"):
         space = self.space
         name = space.text_w(self.w_name)
         for sub_name, module_cls in self.submodules.iteritems():
             if module_cls.submodule_name is None:
                 module_cls.submodule_name = sub_name
             module_name = space.newtext("%s.%s" % (name, sub_name))
             m = module_cls(space, module_name)
             m.install()
             self.submodules_w.append(m)
示例#11
0
 def test_pyc_magic_changes(self):
     # skipped: for now, PyPy generates only one kind of .pyc file
     # per version.  Different versions should differ in
     # sys.implementation.cache_tag, which means that they'll look up
     # different .pyc files anyway.  See test_get_tag() in test_app.py.
     py.test.skip("For now, PyPy generates only one kind of .pyc files")
     # test that the pyc files produced by a space are not reimportable
     # from another, if they differ in what opcodes they support
     allspaces = [self.space]
     for opcodename in self.space.config.objspace.opcodes.getpaths():
         key = 'objspace.opcodes.' + opcodename
         space2 = maketestobjspace(make_config(None, **{key: True}))
         allspaces.append(space2)
     for space1 in allspaces:
         for space2 in allspaces:
             if space1 is space2:
                 continue
             pathname = "whatever"
             mtime = 12345
             co = compile('x = 42', '?', 'exec')
             cpathname = _testfile(space1, importing.get_pyc_magic(space1),
                                   mtime, co)
             w_modulename = space2.wrap('somemodule')
             stream = streamio.open_file_as_stream(cpathname, "rb")
             try:
                 w_mod = space2.wrap(Module(space2, w_modulename))
                 magic = _r_long(stream)
                 timestamp = _r_long(stream)
                 space2.raises_w(space2.w_ImportError,
                                 importing.load_compiled_module, space2,
                                 w_modulename, w_mod, cpathname, magic,
                                 timestamp, stream.readall())
             finally:
                 stream.close()
示例#12
0
def add_module(space, w_name):
    w_mod = check_sys_modules(space, w_name)
    if w_mod is None:
        w_mod = Module(space, w_name)
        init_extra_module_attrs(space, w_mod)
        space.sys.setmodule(w_mod)
    return w_mod
示例#13
0
def PyModule_Create2(space, module, api_version):
    """Create a new module object, given the definition in module, assuming the
    API version module_api_version.  If that version does not match the version
    of the running interpreter, a RuntimeWarning is emitted.
    
    Most uses of this function should be using PyModule_Create()
    instead; only use this if you are sure you need it."""

    modname = rffi.charp2str(module.c_m_name)
    if module.c_m_doc:
        doc = rffi.charp2str(module.c_m_doc)
    else:
        doc = None
    methods = module.c_m_methods

    state = space.fromcache(State)
    f_name, f_path = state.package_context
    if f_name is not None:
        modname = f_name
    w_mod = space.wrap(Module(space, space.wrap(modname)))
    state.package_context = None, None

    if f_path is not None:
        dict_w = {'__file__': space.wrap(f_path)}
    else:
        dict_w = {}
    convert_method_defs(space, dict_w, methods, None, w_mod, modname)
    for key, w_value in dict_w.items():
        space.setattr(w_mod, space.wrap(key), w_value)
    if doc:
        space.setattr(w_mod, space.wrap("__doc__"), space.wrap(doc))
    return w_mod
示例#14
0
 def test_attr(self, space):
     w = space.wrap
     w_m = w(Module(space, space.wrap('m')))
     self.space.setattr(w_m, w('x'), w(15))
     assert space.eq_w(space.getattr(w_m, w('x')), w(15))
     space.delattr(w_m, w('x'))
     space.raises_w(space.w_AttributeError, space.delattr, w_m, w('x'))
示例#15
0
def HPyModule_Create(space, ctx, hpydef):
    modname = rffi.constcharp2str(hpydef.c_m_name)
    w_mod = Module(space, space.newtext(modname))
    #
    # add the functions defined in hpydef.c_legacy_methods
    if hpydef.c_legacy_methods:
        if space.config.objspace.hpy_cpyext_API:
            pymethods = rffi.cast(rffi.VOIDP, hpydef.c_legacy_methods)
            attach_legacy_methods(space, pymethods, w_mod, modname)
        else:
            raise oefmt(
                space.w_RuntimeError,
                "Module %s contains legacy methods, but _hpy_universal "
                "was compiled without cpyext support", modname)
    #
    # add the native HPy defines
    if hpydef.c_defines:
        p = hpydef.c_defines
        i = 0
        while p[i]:
            # hpy native methods
            hpymeth = p[i].c_meth
            name = rffi.constcharp2str(hpymeth.c_name)
            sig = rffi.cast(lltype.Signed, hpymeth.c_signature)
            doc = get_doc(hpymeth.c_doc)
            w_extfunc = interp_extfunc.W_ExtensionFunction(
                space, name, sig, doc, hpymeth.c_impl, w_mod)
            space.setattr(w_mod, space.newtext(w_extfunc.name), w_extfunc)
            i += 1
    return handles.new(space, w_mod)
示例#16
0
 def test_pyc_magic_changes(self):
     # test that the pyc files produced by a space are not reimportable
     # from another, if they differ in what opcodes they support
     allspaces = [self.space]
     for opcodename in self.space.config.objspace.opcodes.getpaths():
         key = 'objspace.opcodes.' + opcodename
         space2 = gettestobjspace(**{key: True})
         allspaces.append(space2)
     for space1 in allspaces:
         for space2 in allspaces:
             if space1 is space2:
                 continue
             pathname = "whatever"
             mtime = 12345
             co = compile('x = 42', '?', 'exec')
             cpathname = _testfile(importing.get_pyc_magic(space1),
                                   mtime, co)
             w_modulename = space2.wrap('somemodule')
             stream = streamio.open_file_as_stream(cpathname, "rb")
             try:
                 w_mod = space2.wrap(Module(space2, w_modulename))
                 magic = importing._r_long(stream)
                 timestamp = importing._r_long(stream)
                 space2.raises_w(space2.w_ImportError,
                                 importing.load_compiled_module,
                                 space2,
                                 w_modulename,
                                 w_mod,
                                 cpathname,
                                 magic,
                                 timestamp,
                                 stream.readall())
             finally:
                 stream.close()
示例#17
0
 def import_py_file(self, space, modname, filename, buf, pkgpath):
     w_mod = Module(space, space.newtext(modname))
     real_name = self.filename + os.path.sep + self.corr_zname(filename)
     space.setattr(w_mod, space.newtext('__loader__'), self)
     importing._prepare_module(space, w_mod, real_name, pkgpath)
     co_filename = self.make_co_filename(filename)
     code_w = importing.parse_source_module(space, co_filename, buf)
     importing.exec_code_module(space, w_mod, code_w, co_filename, None)
     return w_mod
示例#18
0
    def init(self, space):
        """This is called each time the module is imported or reloaded
        """
        if self.w_initialdict is not None:
            # the module was already imported.  Refresh its content with
            # the saved dict, as done with built-in and extension modules
            # on CPython.
            space.call_method(self.w_dict, 'update', self.w_initialdict)

        for w_submodule in self.submodules_w:
            name = space.str0_w(w_submodule.w_name)
            space.setitem(self.w_dict, space.wrap(name.split(".")[-1]), w_submodule)
            space.getbuiltinmodule(name)

        if self.w_initialdict is None:
            Module.init(self, space)
            if not self.lazy and self.w_initialdict is None:
                self.save_module_content_for_future_reload()
示例#19
0
 def install(self):
     """Install this module, and its submodules into
     space.builtin_modules"""
     Module.install(self)
     if hasattr(self, "submodules"):
         space = self.space
         pkgname = space.text_w(self.w_name)
         for sub_name, module_cls in self.submodules.iteritems():
             if module_cls.submodule_name is None:
                 module_cls.submodule_name = sub_name
             else:
                 assert module_cls.submodule_name == sub_name
             name = "%s.%s" % (pkgname, sub_name)
             module_cls.applevel_name = name
             w_name = space.newtext(name)
             m = module_cls(space, w_name)
             m.install()
             self.submodules_w.append(m)
示例#20
0
 def import_py_file(self, space, modname, filename, buf, pkgpath):
     w = space.wrap
     w_mod = w(Module(space, w(modname)))
     real_name = self.name + os.path.sep + self.corr_zname(filename)
     space.setattr(w_mod, w('__loader__'), space.wrap(self))
     importing._prepare_module(space, w_mod, real_name, pkgpath)
     code_w = importing.parse_source_module(space, filename, buf)
     importing.exec_code_module(space, w_mod, code_w)
     return w_mod
示例#21
0
def load_compiled(space, w_modulename, filename, w_file=None):
    w_mod = Module(space, w_modulename)
    importing._prepare_module(space, w_mod, filename, None)
    return _run_compiled_module(space,
                                w_modulename,
                                filename,
                                w_file,
                                w_mod,
                                check_afterwards=True)
示例#22
0
    def init(self, space):
        """This is called each time the module is imported or reloaded
        """
        if self.w_initialdict is not None:
            # the module was already imported.  Refresh its content with
            # the saved dict, as done with built-in and extension modules
            # on CPython.
            space.call_method(self.w_dict, 'update', self.w_initialdict)

        for w_submodule in self.submodules_w:
            name = space.str0_w(w_submodule.w_name)
            space.setitem(self.w_dict, space.wrap(name.split(".")[-1]), w_submodule)
            space.getbuiltinmodule(name)

        if self.w_initialdict is None:
            Module.init(self, space)
            if not self.lazy and self.w_initialdict is None:
                self.w_initialdict = space.call_method(self.w_dict, 'items')
示例#23
0
def load_module(space, w_modulename, find_info, reuse=False):
    if find_info is None:
        return
    if find_info.w_loader:
        return space.call_method(find_info.w_loader, "load_module",
                                 w_modulename)

    if find_info.modtype == C_BUILTIN:
        return space.getbuiltinmodule(find_info.filename, force_init=True)

    if find_info.modtype in (PY_SOURCE, PY_COMPILED, PKG_DIRECTORY):
        if reuse:
            w_mod = space.getitem(space.sys.get('modules'), w_modulename)
        else:
            w_mod = space.wrap(Module(space, w_modulename))
        if find_info.modtype == PKG_DIRECTORY:
            pkgdir = find_info.filename
        else:
            pkgdir = None
        _prepare_module(space, w_mod, find_info.filename, pkgdir)

        try:
            if find_info.modtype == PY_SOURCE:
                load_source_module(space, w_modulename, w_mod,
                                   find_info.filename,
                                   find_info.stream.readall())
                return w_mod
            elif find_info.modtype == PY_COMPILED:
                magic = _r_long(find_info.stream)
                timestamp = _r_long(find_info.stream)
                load_compiled_module(space, w_modulename, w_mod,
                                     find_info.filename, magic, timestamp,
                                     find_info.stream.readall())
                return w_mod
            elif find_info.modtype == PKG_DIRECTORY:
                w_path = space.newlist([space.wrap(find_info.filename)])
                space.setattr(w_mod, space.wrap('__path__'), w_path)
                find_info = find_module(space,
                                        "__init__",
                                        None,
                                        "__init__",
                                        w_path,
                                        use_loader=False)
                if find_info is None:
                    return w_mod
                try:
                    load_module(space, w_modulename, find_info, reuse=True)
                finally:
                    find_info.stream.close()
                # fetch the module again, in case of "substitution"
                w_mod = check_sys_modules(space, w_modulename)
                return w_mod
        except OperationError:
            w_mods = space.sys.get('modules')
            space.call_method(w_mods, 'pop', w_modulename, space.w_None)
            raise
示例#24
0
    def make_builtins(self):
        "NOT_RPYTHON: only for initializing the space."

        from pypy.module.sys import Module
        w_name = self.wrap('sys')
        self.sys = Module(self, w_name)
        w_modules = self.sys.get('modules')
        self.setitem(w_modules, w_name, self.wrap(self.sys))

        from pypy.module.__builtin__ import Module
        w_name = self.wrap('__builtin__')
        self.builtin = Module(self, w_name)
        w_builtin = self.wrap(self.builtin)
        self.setitem(w_modules, w_name, w_builtin)
        self.setitem(self.builtin.w_dict, self.wrap('__builtins__'), w_builtin)

        bootstrap_modules = ['sys', '__builtin__', 'exceptions']
        installed_builtin_modules = bootstrap_modules[:]

        # initialize with "bootstrap types" from objspace  (e.g. w_None)
        for name, value in self.__dict__.items():
            if name.startswith('w_') and not name.endswith('Type'):
                name = name[2:]
                #print "setitem: space instance %-20s into builtins" % name
                self.setitem(self.builtin.w_dict, self.wrap(name), value)

        # install mixed and faked modules and set builtin_module_names on sys
        for mixedname in self.get_builtinmodule_to_install():
            if (mixedname not in bootstrap_modules
                    and not mixedname.startswith('faked+')):
                self.install_mixedmodule(mixedname, installed_builtin_modules)
        for mixedname in self.get_builtinmodule_to_install():
            if mixedname.startswith('faked+'):
                modname = mixedname[6:]
                self.install_faked_module(modname, installed_builtin_modules)

        installed_builtin_modules.sort()
        w_builtin_module_names = self.newtuple(
            [self.wrap(fn) for fn in installed_builtin_modules])

        # force this value into the dict without unlazyfying everything
        self.setitem(self.sys.w_dict, self.wrap('builtin_module_names'),
                     w_builtin_module_names)
示例#25
0
def create_module_from_def_and_spec(space, moddef, w_spec, name):
    moddef = rffi.cast(PyModuleDef, moddef)
    if moddef.c_m_size < 0:
        raise oefmt(
            space.w_SystemError,
            "module %s: m_size may not be negative for multi-phase "
            "initialization", name)
    createf = lltype.nullptr(rffi.VOIDP.TO)
    has_execution_slots = False
    cur_slot = rffi.cast(rffi.CArrayPtr(PyModuleDef_Slot), moddef.c_m_slots)
    if cur_slot:
        while True:
            slot = rffi.cast(lltype.Signed, cur_slot[0].c_slot)
            if slot == 0:
                break
            elif slot == 1:
                if createf:
                    raise oefmt(space.w_SystemError,
                                "module %s has multiple create slots", name)
                createf = cur_slot[0].c_value
            elif slot < 0 or slot > 2:
                raise oefmt(space.w_SystemError,
                            "module %s uses unknown slot ID %d", name, slot)
            else:
                has_execution_slots = True
            cur_slot = rffi.ptradd(cur_slot, 1)
    if createf:
        createf = rffi.cast(createfunctype, createf)
        w_mod = generic_cpy_call(space, createf, w_spec, moddef)
    else:
        w_mod = Module(space, space.newtext(name))
    if isinstance(w_mod, Module):
        mod = rffi.cast(PyModuleObject, as_pyobj(space, w_mod))
        #mod.c_md_state = None
        mod.c_md_def = moddef
    else:
        if moddef.c_m_size > 0 or moddef.c_m_traverse or moddef.c_m_clear or \
           moddef.c_m_free:
            raise oefmt(
                space.w_SystemError,
                "module %s is not a module object, but requests "
                "module state", name)
        if has_execution_slots:
            raise oefmt(
                space.w_SystemError,
                "module %s specifies execution slots, but did not "
                "create a ModuleType instance", name)
    dict_w = {}
    convert_method_defs(space, dict_w, moddef.c_m_methods, None, w_mod, name)
    for key, w_value in dict_w.items():
        space.setattr(w_mod, space.newtext(key), w_value)
    if moddef.c_m_doc:
        doc = rffi.charp2str(rffi.cast(rffi.CCHARP, moddef.c_m_doc))
        space.setattr(w_mod, space.newtext('__doc__'), space.newtext(doc))
    return w_mod
示例#26
0
def load_cffi1_module(space, name, path, initptr):
    # This is called from pypy.module.cpyext.api.load_extension_module()
    from pypy.module._cffi_backend.call_python import get_ll_cffi_call_python

    initfunc = rffi.cast(INITFUNCPTR, initptr)
    with lltype.scoped_alloc(rffi.VOIDPP.TO, 16, zero=True) as p:
        p[0] = rffi.cast(rffi.VOIDP, VERSION_EXPORT)
        p[1] = rffi.cast(rffi.VOIDP, get_ll_cffi_call_python())
        initfunc(p)
        version = rffi.cast(lltype.Signed, p[0])
        if not (VERSION_MIN <= version <= VERSION_MAX):
            raise oefmt(
                space.w_ImportError,
                "cffi extension module '%s' uses an unknown version tag %s. "
                "This module might need a more recent version of PyPy. "
                "The current PyPy provides CFFI %s.", name, hex(version),
                _cffi_backend.VERSION)
        src_ctx = rffi.cast(parse_c_type.PCTX, p[1])

    ffi = W_FFIObject(space, src_ctx)
    lib = W_LibObject(ffi, name)
    if src_ctx.c_includes:
        lib.make_includes_from(src_ctx.c_includes)

    w_name = space.newtext(name)
    module = Module(space, w_name)
    if path is not None:
        module.setdictvalue(space, '__file__', space.newtext(path))
    module.setdictvalue(space, 'ffi', ffi)
    module.setdictvalue(space, 'lib', lib)
    w_modules_dict = space.sys.get('modules')
    space.setitem(w_modules_dict, w_name, module)
    space.setitem(w_modules_dict, space.newtext(name + '.lib'), lib)
    return module
示例#27
0
def load_cffi1_module(space, name, path, initptr):
    # This is called from pypy.module.cpyext.api.load_extension_module()
    initfunc = rffi.cast(initfunctype, initptr)
    with lltype.scoped_alloc(rffi.VOIDPP.TO, 2) as p:
        p[0] = rffi.cast(rffi.VOIDP, VERSION_EXPORT)
        initfunc(p)
        version = rffi.cast(lltype.Signed, p[0])
        if not (VERSION_MIN <= version <= VERSION_MAX):
            raise oefmt(space.w_ImportError,
                        "cffi extension module '%s' has unknown version %s",
                        name, hex(version))
        src_ctx = rffi.cast(parse_c_type.PCTX, p[1])

    ffi = W_FFIObject(space, src_ctx)
    lib = W_LibObject(ffi, name)
    if src_ctx.c_includes:
        lib.make_includes_from(src_ctx.c_includes)

    w_name = space.wrap(name)
    module = Module(space, w_name)
    module.setdictvalue(space, '__file__', space.wrap(path))
    module.setdictvalue(space, 'ffi', space.wrap(ffi))
    module.setdictvalue(space, 'lib', space.wrap(lib))
    w_modules_dict = space.sys.get('modules')
    space.setitem(w_modules_dict, w_name, space.wrap(module))
    space.setitem(w_modules_dict, space.wrap(name + '.lib'), space.wrap(lib))
示例#28
0
def load_source(space, w_modulename, w_filename, w_file=None):
    filename = space.str0_w(w_filename)

    stream = get_file(space, w_file, filename, 'U')

    w_mod = space.wrap(Module(space, w_modulename))
    importing._prepare_module(space, w_mod, filename, None)

    importing.load_source_module(space, w_modulename, w_mod, filename,
                                 stream.readall())
    if space.is_w(w_file, space.w_None):
        stream.close()
    return w_mod
示例#29
0
def testmodule(name, basepath='pypy.module'):
    """Helper to test mixed modules on top of CPython,
    running with the CPy Object Space.  The module should behave
    more or less as if it had been compiled, either with the
    pypy/bin/compilemodule.py tool, or within pypy-c.

    Try:   testmodule('_demo')
    """
    import sys, new
    from pypy.objspace.cpy.objspace import CPyObjSpace
    space = CPyObjSpace()
    
    fullname = "%s.%s" % (basepath, name) 
    Module = __import__(fullname, 
                        None, None, ["Module"]).Module
    appname = Module.get_applevel_name()
    mod = Module(space, space.wrap(appname))
    res = new.module(appname)
    sys.modules[appname] = res
    moddict = space.unwrap(mod.getdict())
    res.__dict__.update(moddict)
    return res
示例#30
0
 def import_py_file(self, space, modname, filename, buf, pkgpath):
     w = space.wrap
     w_mod = w(Module(space, w(modname)))
     real_name = self.name + os.path.sep + self.corr_zname(filename)
     space.setattr(w_mod, w('__loader__'), space.wrap(self))
     importing._prepare_module(space, w_mod, real_name, pkgpath)
     result = importing.load_source_module(space,
                                           w(modname),
                                           w_mod,
                                           filename,
                                           buf,
                                           write_pyc=False)
     return result
示例#31
0
def try_import_mod(space,
                   w_modulename,
                   filepart,
                   w_parent,
                   w_name,
                   pkgdir=None):

    # decide what type we want (pyc/py)
    modtype = find_modtype(space, filepart)

    if modtype == NOFILE:
        return None

    w = space.wrap
    w_mod = w(Module(space, w_modulename))

    try:
        if modtype == PYFILE:
            filename = filepart + ".py"
            stream = streamio.open_file_as_stream(filename, "rU")
        else:
            assert modtype == PYCFILE
            filename = filepart + ".pyc"
            stream = streamio.open_file_as_stream(filename, "rb")

        try:
            _prepare_module(space, w_mod, filename, pkgdir)
            try:
                if modtype == PYFILE:
                    load_source_module(space, w_modulename, w_mod, filename,
                                       stream.readall())
                else:
                    magic = _r_long(stream)
                    timestamp = _r_long(stream)
                    load_compiled_module(space, w_modulename, w_mod, filename,
                                         magic, timestamp, stream.readall())

            except OperationError, e:
                w_mods = space.sys.get('modules')
                space.call_method(w_mods, 'pop', w_modulename, space.w_None)
                raise
        finally:
            stream.close()

    except StreamErrors:
        return None

    w_mod = check_sys_modules(space, w_modulename)
    if w_mod is not None and w_parent is not None:
        space.setattr(w_parent, w_name, w_mod)
    return w_mod
示例#32
0
def load_source(space, w_modulename, w_filename, w_file=None):
    filename = space.fsencode_w(w_filename)

    stream = get_file(space, w_file, filename, 'U')

    w_mod = Module(space, w_modulename)
    importing._prepare_module(space, w_mod, filename, None)

    w_mod = importing.load_source_module(space, w_modulename, w_mod, filename,
                                         stream.readall(),
                                         stream.try_to_find_file_descriptor())
    if space.is_none(w_file):
        stream.close()
    return w_mod
示例#33
0
 def test_load_source_module(self):
     space = self.space
     w_modulename = space.wrap('somemodule')
     w_mod = space.wrap(Module(space, w_modulename))
     pathname = _testfilesource()
     stream = streamio.open_file_as_stream(pathname, "r")
     try:
         w_ret = importing.load_source_module(space, w_modulename, w_mod,
                                              pathname, stream.readall())
     finally:
         stream.close()
     assert w_mod is w_ret
     w_ret = space.getattr(w_mod, space.wrap('x'))
     ret = space.int_w(w_ret)
     assert ret == 42
示例#34
0
 def import_pyc_file(self, space, modname, filename, buf, pkgpath):
     w = space.wrap
     magic = importing._get_long(buf[:4])
     timestamp = importing._get_long(buf[4:8])
     if not self.can_use_pyc(space, filename, magic, timestamp):
         return None
     buf = buf[8:]  # XXX ugly copy, should use sequential read instead
     w_mod = w(Module(space, w(modname)))
     real_name = self.filename + os.path.sep + self.corr_zname(filename)
     space.setattr(w_mod, w('__loader__'), space.wrap(self))
     importing._prepare_module(space, w_mod, real_name, pkgpath)
     result = importing.load_compiled_module(space, w(modname), w_mod,
                                             real_name, magic, timestamp,
                                             buf)
     return result
示例#35
0
 def test___file__(self, space):
     w = space.wrap
     m = Module(space, space.wrap('m'))
     py.test.raises(OperationError, space.getattr, w(m), w('__file__'))
     m._cleanup_()
     py.test.raises(OperationError, space.getattr, w(m), w('__file__'))
     space.setattr(w(m), w('__file__'), w('m.py'))
     space.getattr(w(m), w('__file__'))  # does not raise
     m._cleanup_()
     py.test.raises(OperationError, space.getattr, w(m), w('__file__'))
示例#36
0
 def test_load_source_module_nowrite(self):
     space = self.space
     w_modulename = space.wrap('somemodule')
     w_mod = space.wrap(Module(space, w_modulename))
     pathname = _testfilesource()
     stream = streamio.open_file_as_stream(pathname, "r")
     try:
         w_ret = importing.load_source_module(
             space, w_modulename, w_mod,
             pathname, stream.readall(),
             stream.try_to_find_file_descriptor(),
             write_pyc=False)
     finally:
         stream.close()
     cpathname = udir.join('test.pyc')
     assert not cpathname.check()
示例#37
0
 def import_pyc_file(self, space, modname, filename, w_buf, pkgpath):
     w = space.wrap
     buf = space.str_w(w_buf)
     magic = importing._get_long(buf[:4])
     timestamp = importing._get_long(buf[4:8])
     if self.check_newer_pyfile(space, filename[:-1], timestamp):
         return self.import_py_file(space, modname, filename[:-1], w_buf,
                                    pkgpath)
     buf = buf[8:]  # XXX ugly copy, should use sequential read instead
     w_mod = w(Module(space, w(modname)))
     real_name = self.name + os.path.sep + filename
     importing._prepare_module(space, w_mod, real_name, pkgpath)
     result = importing.load_compiled_module(space, w(modname), w_mod,
                                             filename, magic, timestamp,
                                             buf)
     space.setattr(w_mod, w('__loader__'), space.wrap(self))
     return result
示例#38
0
    def make_builtins(self):
        "NOT_RPYTHON: only for initializing the space."

        from pypy.module.sys import Module
        w_name = self.wrap('sys')
        self.sys = Module(self, w_name)
        w_modules = self.sys.get('modules')
        self.setitem(w_modules, w_name, self.wrap(self.sys))

        from pypy.module.__builtin__ import Module
        w_name = self.wrap('__builtin__')
        self.builtin = Module(self, w_name)
        w_builtin = self.wrap(self.builtin)
        self.setitem(w_modules, w_name, w_builtin)
        self.setitem(self.builtin.w_dict, self.wrap('__builtins__'), w_builtin)

        bootstrap_modules = ['sys', '__builtin__', 'exceptions']
        installed_builtin_modules = bootstrap_modules[:]

        # initialize with "bootstrap types" from objspace  (e.g. w_None)
        for name, value in self.__dict__.items():
            if name.startswith('w_') and not name.endswith('Type'):
                name = name[2:]
                #print "setitem: space instance %-20s into builtins" % name
                self.setitem(self.builtin.w_dict, self.wrap(name), value)

        # install mixed and faked modules and set builtin_module_names on sys
        for mixedname in self.get_builtinmodule_to_install():
            if (mixedname not in bootstrap_modules
                and not mixedname.startswith('faked+')):
                self.install_mixedmodule(mixedname, installed_builtin_modules)
        for mixedname in self.get_builtinmodule_to_install():
            if mixedname.startswith('faked+'):
                modname = mixedname[6:]
                self.install_faked_module(modname, installed_builtin_modules)

        installed_builtin_modules.sort()
        w_builtin_module_names = self.newtuple(
            [self.wrap(fn) for fn in installed_builtin_modules])

        # force this value into the dict without unlazyfying everything
        self.setitem(self.sys.w_dict, self.wrap('builtin_module_names'),
                     w_builtin_module_names)
示例#39
0
 def init(self, space):
     """This is called each time the module is imported or reloaded
     """
     if self.w_initialdict is not None:
         space.call_method(self.w_dict, 'update', self.w_initialdict)
     Module.init(self, space)
示例#40
0
 def __init__(self, space, w_name):
     """ NOT_RPYTHON """
     Module.__init__(self, space, w_name)
     self.lazy = True
     self.__class__.buildloaders()
示例#41
0
class ObjSpace(object):
    """Base class for the interpreter-level implementations of object spaces.
    http://codespeak.net/pypy/dist/pypy/doc/objspace.html"""

    full_exceptions = True  # full support for exceptions (normalization & more)

    def __init__(self, config=None, **kw):
        "NOT_RPYTHON: Basic initialization of objects."
        self.fromcache = InternalSpaceCache(self).getorbuild
        self.threadlocals = ThreadLocals()
        # set recursion limit
        # sets all the internal descriptors
        if config is None:
            from pypy.config.pypyoption import get_pypy_config
            config = get_pypy_config(translating=False)
        self.config = config

        # import extra modules for side-effects
        import pypy.interpreter.nestedscope     # register *_DEREF bytecodes

        self.interned_strings = {}
        self.pending_actions = []
        self.setoptions(**kw)

#        if self.config.objspace.logbytecodes:
#            self.bytecodecounts = {}

        self.initialize()

    def setoptions(self):
        # override this in subclasses for extra-options
        pass

    def startup(self):
        # To be called before using the space

        # Initialize all builtin modules
        from pypy.interpreter.module import Module
        for w_modname in self.unpackiterable(
                                self.sys.get('builtin_module_names')):
            modname = self.str_w(w_modname)
            mod = self.interpclass_w(self.getbuiltinmodule(modname))
            if isinstance(mod, Module):
                mod.startup(self)

    def finish(self):
        w_exitfunc = self.sys.getdictvalue_w(self, 'exitfunc')
        if w_exitfunc is not None:
            self.call_function(w_exitfunc)
        from pypy.interpreter.module import Module
        for w_modname in self.unpackiterable(
                                self.sys.get('builtin_module_names')):
            modname = self.str_w(w_modname)
            mod = self.interpclass_w(self.getbuiltinmodule(modname))
            if isinstance(mod, Module):
                mod.shutdown(self)
        if self.config.objspace.std.withdictmeasurement:
            from pypy.objspace.std.dictmultiobject import report
            report()
        if self.config.objspace.logbytecodes:
            self.reportbytecodecounts()
        if self.config.objspace.std.logspaceoptypes:
            for s in self.FrameClass._space_op_types:
                print s

    def reportbytecodecounts(self):
        os.write(2, "Starting bytecode report.\n")
        fd = os.open('bytecode.txt', os.O_CREAT|os.O_WRONLY|os.O_TRUNC, 0644)
        for opcode, count in self.bytecodecounts.items():
            os.write(fd, str(opcode) + ", " + str(count) + "\n")
        os.close(fd)
        os.write(2, "Reporting done.\n")

    def __repr__(self):
        try:
            return self._this_space_repr_
        except AttributeError:
            return self.__class__.__name__

    def setbuiltinmodule(self, importname):
        """NOT_RPYTHON. load a lazy pypy/module and put it into sys.modules"""
        import sys

        fullname = "pypy.module.%s" % importname

        Module = __import__(fullname,
                            None, None, ["Module"]).Module
        if Module.applevel_name is not None:
            name = Module.applevel_name
        else:
            name = importname

        w_name = self.wrap(name)
        w_mod = self.wrap(Module(self, w_name))
        w_modules = self.sys.get('modules')
        self.setitem(w_modules, w_name, w_mod)
        return name

    def getbuiltinmodule(self, name):
        w_name = self.wrap(name)
        w_modules = self.sys.get('modules')
        return self.getitem(w_modules, w_name)

    def get_builtinmodule_to_install(self):
        """NOT_RPYTHON"""
        try:
            return self._builtinmodule_list
        except AttributeError:
            pass

        modules = []

        # You can enable more modules by specifying --usemodules=xxx,yyy
        for name, value in self.config.objspace.usemodules:
            if value and name not in modules:
                modules.append(name)

        # a bit of custom logic: time2 or rctime take precedence over time
        # XXX this could probably be done as a "requires" in the config
        if ('time2' in modules or 'rctime' in modules) and 'time' in modules:
            modules.remove('time')

        import pypy
        if not self.config.objspace.nofaking:
            for modname in self.ALL_BUILTIN_MODULES:
                if not (os.path.exists(
                        os.path.join(os.path.dirname(pypy.__file__),
                                     'lib', modname+'.py'))):
                    modules.append('faked+'+modname)

        self._builtinmodule_list = modules
        return self._builtinmodule_list

    ALL_BUILTIN_MODULES = [
        'posix', 'nt', 'os2', 'mac', 'ce', 'riscos',
        'math', 'array', 'select',
        '_random', '_sre', 'time', '_socket', 'errno',
        'unicodedata',
        'parser', 'fcntl', '_codecs', 'binascii'
    ]

    def make_builtins(self):
        "NOT_RPYTHON: only for initializing the space."

        from pypy.module.sys import Module
        w_name = self.wrap('sys')
        self.sys = Module(self, w_name)
        w_modules = self.sys.get('modules')
        self.setitem(w_modules, w_name, self.wrap(self.sys))

        from pypy.module.__builtin__ import Module
        w_name = self.wrap('__builtin__')
        self.builtin = Module(self, w_name)
        w_builtin = self.wrap(self.builtin)
        self.setitem(w_modules, w_name, w_builtin)
        self.setitem(self.builtin.w_dict, self.wrap('__builtins__'), w_builtin)

        bootstrap_modules = ['sys', '__builtin__', 'exceptions']
        installed_builtin_modules = bootstrap_modules[:]

        # initialize with "bootstrap types" from objspace  (e.g. w_None)
        for name, value in self.__dict__.items():
            if name.startswith('w_') and not name.endswith('Type'):
                name = name[2:]
                #print "setitem: space instance %-20s into builtins" % name
                self.setitem(self.builtin.w_dict, self.wrap(name), value)

        # install mixed and faked modules and set builtin_module_names on sys
        for mixedname in self.get_builtinmodule_to_install():
            if (mixedname not in bootstrap_modules
                and not mixedname.startswith('faked+')):
                self.install_mixedmodule(mixedname, installed_builtin_modules)
        for mixedname in self.get_builtinmodule_to_install():
            if mixedname.startswith('faked+'):
                modname = mixedname[6:]
                self.install_faked_module(modname, installed_builtin_modules)

        installed_builtin_modules.sort()
        w_builtin_module_names = self.newtuple(
            [self.wrap(fn) for fn in installed_builtin_modules])

        # force this value into the dict without unlazyfying everything
        self.setitem(self.sys.w_dict, self.wrap('builtin_module_names'),
                     w_builtin_module_names)

    def install_mixedmodule(self, mixedname, installed_builtin_modules):
        """NOT_RPYTHON"""
        modname = self.setbuiltinmodule(mixedname)
        if modname:
            assert modname not in installed_builtin_modules, (
                "duplicate interp-level module enabled for the "
                "app-level module %r" % (modname,))
            installed_builtin_modules.append(modname)

    def load_cpython_module(self, modname):
        "NOT_RPYTHON. Steal a module from CPython."
        cpy_module = __import__(modname, {}, {}, ['*'])
        return cpy_module

    def install_faked_module(self, modname, installed_builtin_modules):
        """NOT_RPYTHON"""
        if modname in installed_builtin_modules:
            return
        try:
            module = self.load_cpython_module(modname)
        except ImportError:
            return
        else:
            w_modules = self.sys.get('modules')
            self.setitem(w_modules, self.wrap(modname), self.wrap(module))
            installed_builtin_modules.append(modname)

    def setup_builtin_modules(self):
        "NOT_RPYTHON: only for initializing the space."
        from pypy.interpreter.module import Module
        for w_modname in self.unpackiterable(self.sys.get('builtin_module_names')):
            modname = self.unwrap(w_modname)
            mod = self.getbuiltinmodule(modname)
            if isinstance(mod, Module):
                mod.setup_after_space_initialization()

    def initialize(self):
        """NOT_RPYTHON: Abstract method that should put some minimal
        content into the w_builtins."""

    def enter_cache_building_mode(self):
        "hook for the flow object space"
    def leave_cache_building_mode(self, val):
        "hook for the flow object space"

    def getexecutioncontext(self):
        "Return what we consider to be the active execution context."
        # Important: the annotator must not see a prebuilt ExecutionContext
        # for reasons related to the specialization of the framestack attribute
        # so we make sure that the threadlocals never *have* an
        # ExecutionContext during translation.
        if self.config.translating and not we_are_translated():
            assert self.threadlocals.getvalue() is None, (
                "threadlocals got an ExecutionContext during translation!")
            try:
                return self._ec_during_translation
            except AttributeError:
                ec = self.createexecutioncontext()
                self._ec_during_translation = ec
                return ec
        # normal case follows.  The 'thread' module installs a real
        # thread-local object in self.threadlocals, so this builds
        # and caches a new ec in each thread.
        ec = self.threadlocals.getvalue()
        if ec is None:
            ec = self.createexecutioncontext()
            self.threadlocals.setvalue(ec)
        return ec

    def _freeze_(self):
        return True

    def createexecutioncontext(self):
        "Factory function for execution contexts."
        return ExecutionContext(self)

    def createcompiler(self):
        "Factory function creating a compiler object."
        # XXX simple selection logic for now
        try:
            return self.default_compiler
        except AttributeError:
            if self.config.objspace.compiler == 'cpython':
                compiler = CPythonCompiler(self)
            elif self.config.objspace.compiler == 'ast':
                compiler = PythonAstCompiler(self)
            else:
                raise ValueError('unknown --compiler option value: %r' % (
                    self.config.objspace.compiler,))
            self.default_compiler = compiler
            return compiler

    def createframe(self, code, w_globals, closure=None):
        "Create an empty PyFrame suitable for this code object."
        from pypy.interpreter import pyframe
        return pyframe.PyFrame(self, code, w_globals, closure)

    def allocate_lock(self):
        """Return an interp-level Lock object if threads are enabled,
        and a dummy object if they are not."""
        if self.config.objspace.usemodules.thread:
            # we use a sub-function to avoid putting the 'import' statement
            # here, where the flow space would see it even if thread=False
            return self.__allocate_lock()
        else:
            return dummy_lock

    def __allocate_lock(self):
        from pypy.module.thread.ll_thread import allocate_lock, error
        try:
            return allocate_lock()
        except error:
            raise OperationError(self.w_RuntimeError,
                                 self.wrap("out of resources"))

    # Following is a friendly interface to common object space operations
    # that can be defined in term of more primitive ones.  Subclasses
    # may also override specific functions for performance.

    #def is_(self, w_x, w_y):   -- not really useful.  Must be subclassed
    #    "'x is y'."
    #    w_id_x = self.id(w_x)
    #    w_id_y = self.id(w_y)
    #    return self.eq(w_id_x, w_id_y)

    def not_(self, w_obj):
        return self.wrap(not self.is_true(w_obj))

    def eq_w(self, w_obj1, w_obj2):
        """shortcut for space.is_true(space.eq(w_obj1, w_obj2))"""
        return self.is_w(w_obj1, w_obj2) or self.is_true(self.eq(w_obj1, w_obj2))

    def is_w(self, w_obj1, w_obj2):
        """shortcut for space.is_true(space.is_(w_obj1, w_obj2))"""
        return self.is_true(self.is_(w_obj1, w_obj2))

    def hash_w(self, w_obj):
        """shortcut for space.int_w(space.hash(w_obj))"""
        return self.int_w(self.hash(w_obj))

    def set_str_keyed_item(self, w_obj, w_key, w_value, shadows_type=True):
        return self.setitem(w_obj, w_key, w_value)

    def finditem(self, w_obj, w_key):
        try:
            return self.getitem(w_obj, w_key)
        except OperationError, e:
            if e.match(self, self.w_KeyError):
                return None
            raise