• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python importing.load_source_module函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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
开发者ID:pombredanne,项目名称:pypy,代码行数:12,代码来源:interp_imp.py


示例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()
开发者ID:yuyichao,项目名称:pypy,代码行数:16,代码来源:test_import.py


示例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()
开发者ID:cimarieta,项目名称:usp,代码行数:16,代码来源:test_import.py


示例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()
开发者ID:cimarieta,项目名称:usp,代码行数:18,代码来源:test_import.py


示例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()
开发者ID:cimarieta,项目名称:usp,代码行数:19,代码来源:test_import.py


示例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()
开发者ID:cimarieta,项目名称:usp,代码行数:20,代码来源:test_import.py


示例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)
开发者ID:yuyichao,项目名称:pypy,代码行数:94,代码来源:test_import.py


示例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)
开发者ID:mozillazg,项目名称:pypy,代码行数:3,代码来源:test_import.py



注:本文中的pypy.module.imp.importing.load_source_module函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python base.convert_to_array函数代码示例发布时间:2022-05-27
下一篇:
Python importing.get_pyc_magic函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap