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)
python类load_dynamic()的实例源码
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 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 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 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_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 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 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 _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 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 __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)