Beispiel #1
0
def setup_directory_structure(space):
    root = setuppkg("",
                    a = "imamodule = 1\ninpackage = 0",
                    b = "imamodule = 1\ninpackage = 0",
                    ambig = "imamodule = 1",
                    )
    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",
             )
    setuppkg("pkg.pkg1", a='')
    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"
                        "sys.modules[__name__] = pkg_substituted")
    setuppkg("pkg_substituted", mod='')
    p = setuppkg("readonly", x='')
    p = setuppkg("pkg_univnewlines")
    p.join('__init__.py').write('a=5\nb=6\rc="""hello\r\nworld"""\r')
    p.join('mod.py').write('a=15\nb=16\rc="""foo\r\nbar"""\r')

    # create compiled/x.py and a corresponding pyc file
    p = setuppkg("compiled", x = "x = 84")
    if conftest.option.runappdirect:
        pass
    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())
        finally:
            stream.close()
        if space.config.objspace.usepycfiles:
            # also create a lone .pyc file
            p.join('lone.pyc').write(p.join('x.pyc').read())

    return str(root)
Beispiel #2
0
def setup_directory_structure(space):
    root = setuppkg("",
                    a = "imamodule = 1\ninpackage = 0",
                    b = "imamodule = 1\ninpackage = 0",
                    ambig = "imamodule = 1",
                    )
    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",
             )
    setuppkg("pkg.pkg1", a='')
    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"
                        "sys.modules[__name__] = pkg_substituted")
    setuppkg("pkg_substituted", mod='')
    p = setuppkg("readonly", x='')

    # create compiled/x.py and a corresponding pyc file
    p = setuppkg("compiled", x = "x = 84")
    if conftest.option.runappdirect:
        pass
    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)
        finally:
            stream.close()

    return str(root)
 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
Beispiel #4
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
Beispiel #5
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
Beispiel #6
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(),
                                              write_pyc=False)
     finally:
         stream.close()
     cpathname = udir.join('test.pyc')
     assert not cpathname.check()
Beispiel #7
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)
     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
Beispiel #8
0
    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())
        except OperationError:
            # OperationError("Syntax Error")
            pass
        stream.close()

        cpathname = udir.join('test.pyc')
        assert not cpathname.check()
Beispiel #9
0
    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())
        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()
Beispiel #10
0
def setup_directory_structure(space):
    root = setuppkg("",
                    a = "imamodule = 1\ninpackage = 0",
                    b = "imamodule = 1\ninpackage = 0",
                    ambig = "imamodule = 1",
                    )
    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",
             string     = "inpackage = 1",
             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",
             )
    setuppkg("pkg.pkg1", 
             a          = '',
             relative_d = "from __future__ import absolute_import\nfrom ..string import inpackage",
             relative_e = "from __future__ import absolute_import\nfrom .. import string",
             )
    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"
                        "sys.modules[__name__] = pkg_substituted")
    setuppkg("pkg_substituted", mod='')
    p = setuppkg("readonly", x='')
    p = setuppkg("pkg_univnewlines")
    p.join('__init__.py').write('a=5\nb=6\rc="""hello\r\nworld"""\r')
    p.join('mod.py').write('a=15\nb=16\rc="""foo\r\nbar"""\r')

    # 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)
    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())
        finally:
            stream.close()
        if space.config.objspace.usepycfiles:
            # also create a lone .pyc file
            p.join('lone.pyc').write(p.join('x.pyc').read())

    return str(root)