我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用imp.load_dynamic()。
def _verify(ffi, module_name, preamble, *args, **kwds): # FOR TESTS ONLY from testing.udir import udir import imp assert module_name not in sys.modules, "module name conflict: %r" % ( module_name,) kwds.setdefault('tmpdir', str(udir)) outputfilename = recompile(ffi, module_name, preamble, *args, **kwds) module = imp.load_dynamic(module_name, outputfilename) # # hack hack hack: copy all *bound methods* from module.ffi back to the # ffi instance. Then calls like ffi.new() will invoke module.ffi.new(). for name in dir(module.ffi): if not name.startswith('_'): attr = getattr(module.ffi, name) if attr is not getattr(ffi, name, object()): setattr(ffi, name, attr) def typeof_disabled(*args, **kwds): raise NotImplementedError ffi._typeof = typeof_disabled for name in dir(ffi): if not name.startswith('_') and not hasattr(module.ffi, name): setattr(ffi, name, NotImplemented) return module.lib
def test_import_from_lib(self): ffi2 = cffi.FFI() ffi2.cdef("int myfunc(int); int myvar;\n#define MYFOO ...\n") outputfilename = recompile(ffi2, "_test_import_from_lib", "int myfunc(int x) { return x + 1; }\n" "int myvar = -5;\n" "#define MYFOO 42", tmpdir=str(udir)) imp.load_dynamic("_test_import_from_lib", outputfilename) from _test_import_from_lib.lib import myfunc, myvar, MYFOO assert MYFOO == 42 assert myfunc(43) == 44 assert myvar == -5 # but can't be changed, so not very useful py.test.raises(ImportError, "from _test_import_from_lib.lib import bar") d = {} exec("from _test_import_from_lib.lib import *", d) assert (set(key for key in d if not key.startswith('_')) == set(['myfunc', 'MYFOO'])) # # also test "import *" on the module itself, which should be # equivalent to "import ffi, lib" d = {} exec("from _test_import_from_lib import *", d) assert (sorted([x for x in d.keys() if not x.startswith('__')]) == ['ffi', 'lib'])
def _verify(ffi, module_name, preamble, *args, **kwds): # FOR TESTS ONLY from testing.udir import udir import imp assert module_name not in sys.modules, "module name conflict: %r" % ( module_name,) kwds.setdefault('tmpdir', str(udir)) outputfilename = recompile(ffi, module_name, preamble, *args, **kwds) module = imp.load_dynamic(module_name, outputfilename) # # hack hack hack: copy all *bound methods* from module.ffi back to the # ffi instance. Then calls like ffi.new() will invoke module.ffi.new(). for name in dir(module.ffi): if not name.startswith('_'): attr = getattr(module.ffi, name) if attr is not getattr(ffi, name, object()): setattr(ffi, name, attr) def typeof_disabled(*args, **kwds): raise NotImplementedError ffi._typeof = typeof_disabled return module.lib
def load_module(self, name, stuff): file, filename, info = stuff (suff, mode, type) = info try: if type == BUILTIN_MODULE: return self.hooks.init_builtin(name) if type == FROZEN_MODULE: return self.hooks.init_frozen(name) if type == C_EXTENSION: m = self.hooks.load_dynamic(name, filename, file) elif type == PY_SOURCE: m = self.hooks.load_source(name, filename, file) elif type == PY_COMPILED: m = self.hooks.load_compiled(name, filename, file) elif type == PKG_DIRECTORY: m = self.hooks.load_package(name, filename, file) else: raise ImportError, "Unrecognized module type (%r) for %s" % \ (type, name) finally: if file: file.close() m.__file__ = filename return m
def load_module(self, fullname): if fullname in sys.modules: result = sys.modules[fullname] else: if fullname not in self.libs: raise ImportError('unable to find extension for %s' % fullname) result = imp.load_dynamic(fullname, self.libs[fullname]) result.__loader__ = self parts = fullname.rsplit('.', 1) if len(parts) > 1: result.__package__ = parts[0] return result
def write_stub(resource, pyfile): _stub_template = textwrap.dedent(""" def __bootstrap__(): global __bootstrap__, __loader__, __file__ import sys, pkg_resources, imp __file__ = pkg_resources.resource_filename(__name__, %r) __loader__ = None; del __bootstrap__, __loader__ imp.load_dynamic(__name__,__file__) __bootstrap__() """).lstrip() with open(pyfile, 'w') as f: f.write(_stub_template % resource)
def load_dynamic(self, name, filename, file): return self.rexec.load_dynamic(name, filename, file)
def load_dynamic(self, name, filename, file): if name not in self.ok_dynamic_modules: raise ImportError, "untrusted dynamic module: %s" % name if name in sys.modules: src = sys.modules[name] else: src = imp.load_dynamic(name, filename, file) dst = self.copy_except(src, []) return dst
def load_dynamic(self, name, filename, file=None): return imp.load_dynamic(name, filename, file)
def test_multiple_independent_structs(self): CDEF2 = "struct ab { int x; };" ffi2 = cffi.FFI(); ffi2.cdef(CDEF2) outputfilename = recompile(ffi2, "test_multiple_independent_structs", CDEF2, tmpdir=str(udir)) module = imp.load_dynamic("test_multiple_independent_structs", outputfilename) ffi1 = module.ffi foo1 = ffi1.new("struct ab *", [10]) foo2 = ffi .new("struct ab *", [20, 30]) assert foo1.x == 10 assert foo2.a == 20 assert foo2.b == 30
def test_include_struct_union_enum_typedef(self): ffi1, CCODE = construction_params ffi2 = cffi.FFI() ffi2.include(ffi1) outputfilename = recompile(ffi2, "test_include_struct_union_enum_typedef", CCODE, tmpdir=str(udir)) module = imp.load_dynamic("test_include_struct_union_enum_typedef", outputfilename) ffi2 = module.ffi # p = ffi2.new("struct nonpacked *", [b'A', -43141]) assert p.a == b'A' assert p.b == -43141 # p = ffi.new("union simple_u *", [-52525]) assert p.a == -52525 # p = ffi.cast("enum foq", 2) assert ffi.string(p) == "cffiCC0" assert ffi2.sizeof("char[cffiCC0]") == 2 # p = ffi.new("anon_foo_t *", [-52526]) assert p.a == -52526 p = ffi.new("named_foo_p", [-52527]) assert p.a == -52527
def test_compile_module(self): ffi = FFI() ffi.cdef("double sin(double x);") csrc = '/*hi there %s!*/\n#include <math.h>\n' % self v = Verifier(ffi, csrc, force_generic_engine=self.generic, libraries=[self.lib_m]) v.compile_module() assert v.get_module_name().startswith('_cffi_') if v.generates_python_module(): mod = imp.load_dynamic(v.get_module_name(), v.modulefilename) assert hasattr(mod, '_cffi_setup')
def test_compile_module_explicit_filename(self): ffi = FFI() ffi.cdef("double sin(double x);") csrc = '/*hi there %s!2*/\n#include <math.h>\n' % self v = Verifier(ffi, csrc, force_generic_engine=self.generic, libraries=[self.lib_m]) basename = self.__class__.__name__ + 'test_compile_module' v.modulefilename = filename = str(udir.join(basename + '.so')) v.compile_module() assert filename == v.modulefilename assert v.get_module_name() == basename if v.generates_python_module(): mod = imp.load_dynamic(v.get_module_name(), v.modulefilename) assert hasattr(mod, '_cffi_setup')
def _compile_pyx(self, filename, m, src): """ Use Cython to compile the schema """ import capnpy.ext # the package which we will load the .so in import imp # # the generated file needs a reference to __compiler to be able to # import other schemas. In pure-python mode, we simply inject # __compiler in the __dict__ before compiling the source; but in pyx # mode we cannot, hence we need a way to "pass" an argument from the # outside. I think the only way is to temporarily stick it in some # global state, for example sys.modules. Then, as we don't want to # clutter any global state, we cleanup sys.modules. # # So, when compiling foo.capnp, we create a dummy foo_tmp module which # contains __compiler. Then, in foo.pyx, we import it: # from foo_tmp import __compiler # dll = self._pyx_to_dll(filename, m, src) tmpmod = types.ModuleType(m.tmpname) tmpmod.__dict__['__compiler'] = self tmpmod.__dict__['__schema__'] = str(filename) sys.modules[m.tmpname] = tmpmod modname = 'capnpy.ext.%s' % m.modname mod = imp.load_dynamic(modname, str(dll)) # # clean-up the cluttered sys.modules del sys.modules[mod.__name__] del sys.modules[tmpmod.__name__] return mod
def __init__(self, fortran=None, *args, **kwargs): """ To use the pyOM2 legacy interface point the fortran argument to the Veros fortran library: > simulation = GlobalOneDegree(fortran = "pyOM_code.so") """ if fortran: self.legacy_mode = True try: self.fortran = LowercaseAttributeWrapper(imp.load_dynamic("pyOM_code", fortran)) self.use_mpi = False except ImportError: self.fortran = LowercaseAttributeWrapper(imp.load_dynamic("pyOM_code_MPI", fortran)) self.use_mpi = True from mpi4py import MPI self.mpi_comm = MPI.COMM_WORLD self.main_module = LowercaseAttributeWrapper(self.fortran.main_module) self.isoneutral_module = LowercaseAttributeWrapper(self.fortran.isoneutral_module) self.idemix_module = LowercaseAttributeWrapper(self.fortran.idemix_module) self.tke_module = LowercaseAttributeWrapper(self.fortran.tke_module) self.eke_module = LowercaseAttributeWrapper(self.fortran.eke_module) else: self.legacy_mode = False self.use_mpi = False self.fortran = self self.main_module = self self.isoneutral_module = self self.idemix_module = self self.tke_module = self self.eke_module = self self.modules = (self.main_module, self.isoneutral_module, self.idemix_module, self.tke_module, self.eke_module) if self.use_mpi and self.mpi_comm.Get_rank() != 0: kwargs["loglevel"] = "critical" super(VerosLegacy, self).__init__(*args, **kwargs)