本文整理汇总了Python中pypy.module.imp.importing.load_source_module函数的典型用法代码示例。如果您正苦于以下问题:Python load_source_module函数的具体用法?Python load_source_module怎么用?Python load_source_module使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了load_source_module函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_source
def load_source(space, w_modulename, w_filename, w_file=None):
filename = space.str_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
示例2: test_load_source_module_nowrite
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()
示例3: test_load_source_module_dont_write_bytecode
def test_load_source_module_dont_write_bytecode(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:
space.setattr(space.sys, space.wrap("dont_write_bytecode"), space.w_True)
w_ret = importing.load_source_module(
space, w_modulename, w_mod, pathname, stream.readall(), stream.try_to_find_file_descriptor()
)
finally:
space.setattr(space.sys, space.wrap("dont_write_bytecode"), space.w_False)
stream.close()
cpathname = udir.join("test.pyc")
assert not cpathname.check()
示例4: test_load_source_module_syntaxerror
def test_load_source_module_syntaxerror(self):
# No .pyc file on SyntaxError
space = self.space
w_modulename = space.wrap("somemodule")
w_mod = space.wrap(Module(space, w_modulename))
pathname = _testfilesource(source="<Syntax Error>")
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()
)
except OperationError:
# OperationError("Syntax Error")
pass
stream.close()
cpathname = udir.join("test.pyc")
assert not cpathname.check()
示例5: test_load_source_module_importerror
def test_load_source_module_importerror(self):
# the .pyc file is created before executing the module
space = self.space
w_modulename = space.wrap("somemodule")
w_mod = space.wrap(Module(space, w_modulename))
pathname = _testfilesource(source="a = unknown_name")
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()
)
except OperationError:
# OperationError("NameError", "global name 'unknown_name' is not defined")
pass
stream.close()
# And the .pyc has been generated
cpathname = udir.join("test.pyc")
assert cpathname.check()
示例6: test_load_source_module
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(), stream.try_to_find_file_descriptor()
)
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
cpathname = udir.join("test.pyc")
assert cpathname.check()
cpathname.remove()
示例7: setup_directory_structure
def setup_directory_structure(space):
root = setuppkg("",
a = "imamodule = 1\ninpackage = 0",
b = "imamodule = 1\ninpackage = 0",
ambig = "imamodule = 1",
test_reload = "def test():\n raise ValueError\n",
infinite_reload = "import infinite_reload; reload(infinite_reload)",
del_sys_module = "import sys\ndel sys.modules['del_sys_module']\n",
_md5 = "hello_world = 42\n",
gc = "should_never_be_seen = 42\n",
)
root.ensure("notapackage", dir=1) # empty, no __init__.py
setuppkg("pkg",
a = "imamodule = 1\ninpackage = 1",
relative_a = "import a",
abs_b = "import b",
abs_x_y = "import x.y",
abs_sys = "import sys",
string = "inpackage = 1",
errno = "",
absolute = "from __future__ import absolute_import\nimport string",
relative_b = "from __future__ import absolute_import\nfrom . import string",
relative_c = "from __future__ import absolute_import\nfrom .string import inpackage",
relative_f = "from .imp import get_magic",
relative_g = "import imp; from .imp import get_magic",
)
setuppkg("pkg.pkg1",
__init__ = 'from . import a',
a = '',
relative_d = "from __future__ import absolute_import\nfrom ..string import inpackage",
relative_e = "from __future__ import absolute_import\nfrom .. import string",
relative_g = "from .. import pkg1\nfrom ..pkg1 import b",
b = "insubpackage = 1",
)
setuppkg("pkg.pkg2", a='', b='')
setuppkg("pkg_r", inpkg = "import x.y")
setuppkg("pkg_r.x")
setuppkg("x", y='')
setuppkg("ambig", __init__ = "imapackage = 1")
setuppkg("pkg_relative_a",
__init__ = "import a",
a = "imamodule = 1\ninpackage = 1",
)
setuppkg("pkg_substituting",
__init__ = "import sys, pkg_substituted\n"
"print 'TOTO', __name__\n"
"sys.modules[__name__] = pkg_substituted")
setuppkg("pkg_substituted", mod='')
setuppkg("evil_pkg",
evil = "import sys\n"
"from evil_pkg import good\n"
"sys.modules['evil_pkg.evil'] = good",
good = "a = 42")
p = setuppkg("readonly", x='')
p = setuppkg("pkg_univnewlines")
p.join('__init__.py').write(
'a=5\nb=6\rc="""hello\r\nworld"""\r', mode='wb')
p.join('mod.py').write(
'a=15\nb=16\rc="""foo\r\nbar"""\r', mode='wb')
# create compiled/x.py and a corresponding pyc file
p = setuppkg("compiled", x = "x = 84")
if conftest.option.runappdirect:
import marshal, stat, struct, os, imp
code = py.code.Source(p.join("x.py").read()).compile()
s3 = marshal.dumps(code)
s2 = struct.pack("i", os.stat(str(p.join("x.py")))[stat.ST_MTIME])
p.join("x.pyc").write(imp.get_magic() + s2 + s3, mode='wb')
else:
w = space.wrap
w_modname = w("compiled.x")
filename = str(p.join("x.py"))
stream = streamio.open_file_as_stream(filename, "r")
try:
importing.load_source_module(
space, w_modname, w(importing.Module(space, w_modname)),
filename, stream.readall(),
stream.try_to_find_file_descriptor())
finally:
stream.close()
if space.config.objspace.usepycfiles:
# also create a lone .pyc file
p.join('lone.pyc').write(p.join('x.pyc').read(mode='rb'),
mode='wb')
# create a .pyw file
p = setuppkg("windows", x = "x = 78")
try:
p.join('x.pyw').remove()
except py.error.ENOENT:
pass
p.join('x.py').rename(p.join('x.pyw'))
return str(root)
示例8: _load_source_module
def _load_source_module(space, w_modname, w_mod, *args, **kwds):
kwds.setdefault("check_afterwards", False)
return importing.load_source_module(space, w_modname, w_mod, *args, **kwds)