Пример #1
0
    def test_old_record(self):
        # test pre-PEP 376 --record option (outside dist-info dir)
        install_dir = self.mkdtemp()
        project_dir, dist = self.create_dist(py_modules=['hello'],
                                             scripts=['sayhi'])
        os.chdir(project_dir)
        self.write_file('hello.py', "def main(): print('o hai')")
        self.write_file('sayhi', 'from hello import main; main()')

        cmd = install_dist(dist)
        dist.command_obj['install_dist'] = cmd
        cmd.root = install_dir
        cmd.record = os.path.join(project_dir, 'filelist')
        cmd.ensure_finalized()
        cmd.run()

        with open(cmd.record) as f:
            content = f.read()

        if sys.version_info[:2] == (3, 1):
            pyc = 'hello.pyc'
        else:
            pyc = 'hello.%s.pyc' % imp.get_tag()
        found = [os.path.basename(line) for line in content.splitlines()]
        expected = [
            'hello.py', pyc, 'sayhi', 'METADATA', 'INSTALLER', 'REQUESTED',
            'RECORD'
        ]
        self.assertEqual(sorted(found), sorted(expected))
Пример #2
0
def vengine_gen_find_module(self, module_name, path, so_suffixes):
    global _ma_triplet
    if _ma_triplet is None:
        try:
            import subprocess as sp
            p = sp.Popen(["gcc", "-print-multiarch"], stdout=sp.PIPE)
            _ma_triplet = str(p.communicate()[0].decode().strip())
        except:
            import warnings
            warnings.warn(
                'failed to detect multiarch paths, please install gcc')

    for so_suffix in so_suffixes + [
            '.%s-%s.so' % (imp.get_tag(), _ma_triplet)
    ]:
        basename = module_name + so_suffix
        if path is None:
            path = sys.path
            # import from non root package would try __pycache__ which is
            # cleaned by pypy installation
            path.insert(0, "/usr/lib/pypy/dist-packages/zmq/backend/cffi")
        for dirname in path:
            filename = os.path.join(dirname, basename)
            if os.path.isfile(filename):
                return filename
Пример #3
0
    def test_byte_compile_optimized(self):
        project_dir, dist = self.create_dist(py_modules=['boiledeggs'])
        os.chdir(project_dir)
        self.write_file('boiledeggs.py', 'import antigravity')
        cmd = build_py(dist)
        cmd.compile = True
        cmd.optimize = 1
        cmd.build_lib = 'here'
        cmd.finalize_options()
        cmd.run()

        found = os.listdir(cmd.build_lib)
        self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py'])
        found = os.listdir(os.path.join(cmd.build_lib, '__pycache__'))
        self.assertEqual(sorted(found), ['boiledeggs.%s.pyc' % imp.get_tag(),
                                         'boiledeggs.%s.pyo' % imp.get_tag()])
Пример #4
0
def get_magic_tag():
    try:
        # For Python Version >= 3.2
        from imp import get_tag
        return get_tag()
    except ImportError:
        return ''
Пример #5
0
def get_magic_tag():
    try:
        # For Python Version >= 3.2
        from imp import get_tag
        return get_tag()
    except ImportError:
        return ''
    def test_record(self):
        install_dir = self.mkdtemp()
        project_dir, dist = self.create_dist(py_modules=['hello'],
                                             scripts=['sayhi'])
        os.chdir(project_dir)
        self.write_file('hello.py', "def main(): print('o hai')")
        self.write_file('sayhi', 'from hello import main; main()')

        cmd = install(dist)
        dist.command_obj['install'] = cmd
        cmd.root = install_dir
        cmd.record = os.path.join(project_dir, 'filelist')
        cmd.ensure_finalized()
        cmd.run()

        f = open(cmd.record)
        try:
            content = f.read()
        finally:
            f.close()

        found = [os.path.basename(line) for line in content.splitlines()]
        expected = [
            'hello.py',
            'hello.%s.pyc' % imp.get_tag(), 'sayhi',
            'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]
        ]
        self.assertEqual(found, expected)
Пример #7
0
class PEP3147Tests(unittest.TestCase):
    """Tests of PEP 3147."""

    tag = imp.get_tag()

    @unittest.skipUnless(sys.implementation.cache_tag is not None,
                         'requires sys.implementation.cache_tag not be None')
    def test_cache_from_source(self):
        # Given the path to a .py file, return the path to its PEP 3147
        # defined .pyc file (i.e. under __pycache__).
        path = os.path.join('foo', 'bar', 'baz', 'qux.py')
        expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
                              'qux.{}.pyc'.format(self.tag))
        self.assertEqual(imp.cache_from_source(path, True), expect)

    @unittest.skipUnless(sys.implementation.cache_tag is not None,
                         'requires sys.implementation.cache_tag to not be '
                         'None')
    def test_source_from_cache(self):
        # Given the path to a PEP 3147 defined .pyc file, return the path to
        # its source.  This tests the good path.
        path = os.path.join('foo', 'bar', 'baz', '__pycache__',
                            'qux.{}.pyc'.format(self.tag))
        expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
        self.assertEqual(imp.source_from_cache(path), expect)
Пример #8
0
    def test_record(self):
        install_dir = self.mkdtemp()
        project_dir, dist = self.create_dist(py_modules=['hello'],
                                             scripts=['sayhi'])
        os.chdir(project_dir)
        self.write_file('hello.py', "def main(): print('o hai')")
        self.write_file('sayhi', 'from hello import main; main()')

        cmd = install(dist)
        dist.command_obj['install'] = cmd
        cmd.root = install_dir
        cmd.record = os.path.join(project_dir, 'filelist')
        cmd.ensure_finalized()
        cmd.run()

        f = open(cmd.record)
        try:
            content = f.read()
        finally:
            f.close()

        found = [os.path.basename(line) for line in content.splitlines()]
        expected = ['hello.py', 'hello.%s.pyc' % imp.get_tag(), 'sayhi',
                    'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]]
        self.assertEqual(found, expected)
Пример #9
0
    def test_package_data(self):
        sources = self.mkdtemp()
        pkg_dir = os.path.join(sources, 'pkg')
        os.mkdir(pkg_dir)
        f = open(os.path.join(pkg_dir, "__init__.py"), "w")
        try:
            f.write("# Pretend this is a package.")
        finally:
            f.close()
        # let's have two files to make sure globbing works
        f = open(os.path.join(pkg_dir, "README.txt"), "w")
        try:
            f.write("Info about this package")
        finally:
            f.close()
        f = open(os.path.join(pkg_dir, "HACKING.txt"), "w")
        try:
            f.write("How to contribute")
        finally:
            f.close()

        destination = self.mkdtemp()

        dist = Distribution({"packages": ["pkg"],
                             "package_dir": sources})

        dist.command_obj["build"] = support.DummyCommand(
            force=False,
            build_lib=destination,
            use_2to3_fixers=None,
            convert_2to3_doctests=None,
            use_2to3=False)
        dist.packages = ["pkg"]
        dist.package_data = {"pkg": ["*.txt"]}
        dist.package_dir = sources

        cmd = build_py(dist)
        cmd.compile = True
        cmd.ensure_finalized()
        self.assertEqual(cmd.package_data, dist.package_data)

        cmd.run()

        # This makes sure the list of outputs includes byte-compiled
        # files for Python modules but not for package data files
        # (there shouldn't *be* byte-code files for those!).
        # FIXME the test below is not doing what the comment above says, and
        # if it did it would show a code bug: if we add a demo.py file to
        # package_data, it gets byte-compiled!
        outputs = cmd.get_outputs()
        self.assertEqual(len(outputs), 4, outputs)
        pkgdest = os.path.join(destination, "pkg")
        files = os.listdir(pkgdest)
        pycache_dir = os.path.join(pkgdest, "__pycache__")
        self.assertIn("__init__.py", files)
        self.assertIn("README.txt", files)
        self.assertIn("HACKING.txt", files)
        pyc_files = os.listdir(pycache_dir)
        self.assertEqual(["__init__.%s.pyc" % imp.get_tag()], pyc_files)
Пример #10
0
class test_cache_from_source():
    from humpty import cache_from_source
    if sys.version_info >= (3, 2):
        tag = imp.get_tag()
        assert cache_from_source('foo.py') == os.path.join(
            '__pycache__', 'foo.%s.pyc' % tag)
    else:
        assert cache_from_source('foo.py') == 'foo.pyc'
Пример #11
0
def remove_bytecode(module_path):
    paths = [module_path + 'c']
    if hasattr(imp, 'get_tag'):
        modname, ext = os.path.splitext(module_path.split(os.sep)[-1])
        paths.append(
            os.path.join(os.path.dirname(module_path), '__pycache__',
                         '{}.{}.pyc'.format(modname, imp.get_tag())))
    for path in paths:
        if os.path.exists(path):
            os.unlink(path)
Пример #12
0
def make_pymodule_path(filename):
    path = os.path.dirname(filename)
    file = os.path.basename(filename)
    mod, ext = os.path.splitext(file)

    if sys.hexversion >= 0x3020000:
        modname = mod + "." + imp.get_tag() + ext
        fullpath = os.path.join(path, '__pycache__', modname)
    else:
        fullpath = filename
    return fullpath
Пример #13
0
def make_pymodule_path(filename):
    path = os.path.dirname(filename)
    file = os.path.basename(filename)
    mod, ext = os.path.splitext(file)

    if sys.hexversion >= 0x3020000:
        modname = mod+"."+imp.get_tag()+ext
        fullpath = os.path.join(path,'__pycache__',modname)
    else:
        fullpath = filename
    return fullpath
Пример #14
0
 def f(self, ext=ext, switch=switch):
     script_helper.assert_python_ok(*(switch +
         ['-m', 'compileall', '-q', self.pkgdir]))
     # Verify the __pycache__ directory contents.
     self.assertTrue(os.path.exists(self.pkgdir_cachedir))
     expected = sorted(base.format(imp.get_tag(), ext) for base in
                       ('__init__.{}.{}', 'bar.{}.{}'))
     self.assertEqual(sorted(os.listdir(self.pkgdir_cachedir)), expected)
     # Make sure there are no .pyc files in the source directory.
     self.assertFalse([fn for fn in os.listdir(self.pkgdir)
                       if fn.endswith(ext)])
Пример #15
0
    def test_simple_built(self):

        # let's create a simple package
        tmp_dir = self.mkdtemp()
        pkg_dir = os.path.join(tmp_dir, 'foo')
        os.mkdir(pkg_dir)
        self.write_file((pkg_dir, 'setup.py'), SETUP_PY)
        self.write_file((pkg_dir, 'foo.py'), '#')
        self.write_file((pkg_dir, 'MANIFEST.in'), 'include foo.py')
        self.write_file((pkg_dir, 'README'), '')

        dist = Distribution({
            'name': 'foo',
            'version': '0.1',
            'py_modules': ['foo'],
            'url': 'xxx',
            'author': 'xxx',
            'author_email': 'xxx'
        })
        dist.script_name = 'setup.py'
        os.chdir(pkg_dir)

        sys.argv = ['setup.py']
        cmd = bdist_dumb(dist)

        # so the output is the same no matter
        # what is the platform
        cmd.format = 'zip'

        cmd.ensure_finalized()
        cmd.run()

        # see what we have
        dist_created = os.listdir(os.path.join(pkg_dir, 'dist'))
        base = "%s.%s.zip" % (dist.get_fullname(), cmd.plat_name)
        if os.name == 'os2':
            base = base.replace(':', '-')

        self.assertEqual(dist_created, [base])

        # now let's check what we have in the zip file
        fp = zipfile.ZipFile(os.path.join('dist', base))
        try:
            contents = fp.namelist()
        finally:
            fp.close()

        contents = sorted(os.path.basename(fn) for fn in contents)
        wanted = [
            'foo-0.1-py%s.%s.egg-info' % sys.version_info[:2],
            'foo.%s.pyc' % imp.get_tag(), 'foo.py'
        ]
        self.assertEqual(contents, sorted(wanted))
Пример #16
0
    def test_simple_built(self):

        # let's create a simple package
        tmp_dir = self.mkdtemp()
        pkg_dir = os.path.join(tmp_dir, "foo")
        os.mkdir(pkg_dir)
        self.write_file((pkg_dir, "setup.py"), SETUP_PY)
        self.write_file((pkg_dir, "foo.py"), "#")
        self.write_file((pkg_dir, "MANIFEST.in"), "include foo.py")
        self.write_file((pkg_dir, "README"), "")

        dist = Distribution(
            {
                "name": "foo",
                "version": "0.1",
                "py_modules": ["foo"],
                "url": "xxx",
                "author": "xxx",
                "author_email": "xxx",
            }
        )
        dist.script_name = "setup.py"
        os.chdir(pkg_dir)

        sys.argv = ["setup.py"]
        cmd = bdist_dumb(dist)

        # so the output is the same no matter
        # what is the platform
        cmd.format = "zip"

        cmd.ensure_finalized()
        cmd.run()

        # see what we have
        dist_created = os.listdir(os.path.join(pkg_dir, "dist"))
        base = "%s.%s.zip" % (dist.get_fullname(), cmd.plat_name)
        if os.name == "os2":
            base = base.replace(":", "-")

        self.assertEqual(dist_created, [base])

        # now let's check what we have in the zip file
        fp = zipfile.ZipFile(os.path.join("dist", base))
        try:
            contents = fp.namelist()
        finally:
            fp.close()

        contents = sorted(os.path.basename(fn) for fn in contents)
        wanted = ["foo-0.1-py%s.%s.egg-info" % sys.version_info[:2], "foo.%s.pyc" % imp.get_tag(), "foo.py"]
        self.assertEqual(contents, sorted(wanted))
Пример #17
0
def remove_bytecode(module_path):
    paths = [module_path + "c"]
    if hasattr(imp, "get_tag"):
        modname, ext = os.path.splitext(module_path.split(os.sep)[-1])
        paths.append(
            os.path.join(
                os.path.dirname(module_path),
                "__pycache__",
                "{}.{}.pyc".format(modname, imp.get_tag()),
            ))
    for path in paths:
        if os.path.exists(path):
            os.unlink(path)
Пример #18
0
def compile_file(fullname, ddir=None, force=0, rx=None, quiet=0):
    """Byte-compile one file.

    Arguments (only fullname is required):

    fullname:  the file to byte-compile
    ddir:      if given, the directory name compiled in to the
               byte-code file.
    force:     if 1, force compilation, even if timestamps are up-to-date
    quiet:     if 1, be quiet during compilation
    """
    success = 1
    name = os.path.basename(fullname)
    if ddir is not None:
        dfile = os.path.join(ddir, name)
    else:
        dfile = None
    if rx is not None:
        mo = rx.search(fullname)
        if mo:
            return success
    if os.path.isfile(fullname):
        head, tail = name[:-3], name[-3:]
        if tail == '.py':
            if not force:
                try:
                    mtime = int(os.stat(fullname).st_mtime)
                    expect = struct.pack('<4sl', imp.get_magic(), mtime)
                    cfile = fullname.replace('.py', imp.get_tag() + '.class')
                    with open(cfile, 'rb') as chandle:
                        actual = chandle.read(8)
                    if expect == actual:
                        return success
                except IOError:
                    pass
            if not quiet:
                print('Compiling', fullname, '...')
            try:
                ok = py_compile.compile(fullname, None, dfile, True)
            except py_compile.PyCompileError as err:
                if quiet:
                    print('Compiling', fullname, '...')
                print(err.msg)
                success = 0
            except IOError as e:
                print("Sorry", e)
                success = 0
            else:
                if ok == 0:
                    success = 0
    return success
Пример #19
0
 def f(self, ext=ext, switch=switch):
     retcode = subprocess.call(
         [sys.executable] + switch +
         ['-m', 'compileall', '-q', self.pkgdir])
     self.assertEqual(retcode, 0)
     # Verify the __pycache__ directory contents.
     cachedir = os.path.join(self.pkgdir, '__pycache__')
     self.assertTrue(os.path.exists(cachedir))
     expected = sorted(base.format(imp.get_tag(), ext) for base in
                       ('__init__.{}.{}', 'bar.{}.{}'))
     self.assertEqual(sorted(os.listdir(cachedir)), expected)
     # Make sure there are no .pyc files in the source directory.
     self.assertFalse([pyc_file for pyc_file in os.listdir(self.pkgdir)
                       if pyc_file.endswith(ext)])
Пример #20
0
    def test_byte_compile_optimized(self):
        project_dir, dist = self.create_dist(py_modules=['boiledeggs'])
        os.chdir(project_dir)
        self.write_file('boiledeggs.py', 'import antigravity')
        cmd = build_py(dist)
        cmd.compile = True
        cmd.optimize = 1
        cmd.build_lib = 'here'
        cmd.finalize_options()
        cmd.run()

        found = os.listdir(cmd.build_lib)
        if sys.version_info[:2] == (3, 1):
            self.assertEqual(
                sorted(found),
                ['boiledeggs.py', 'boiledeggs.pyc', 'boiledeggs.pyo'])
        else:
            self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py'])
            found = os.listdir(os.path.join(cmd.build_lib, '__pycache__'))
            self.assertEqual(sorted(found), [
                'boiledeggs.%s.pyc' % imp.get_tag(),
                'boiledeggs.%s.pyo' % imp.get_tag()
            ])
Пример #21
0
def make_pymodule_path(filename):
    path = os.path.dirname(filename)
    file = os.path.basename(filename)
    mod, ext = os.path.splitext(file)

    if sys.hexversion >= 0x3040000:
        import importlib.util
        fullpath = importlib.util.cache_from_source(filename, ext == '.pyc')
    elif sys.hexversion >= 0x3020000:
        import imp
        modname = mod + "." + imp.get_tag() + ext
        fullpath = os.path.join(path, '__pycache__', modname)
    else:
        fullpath = filename
    return fullpath
Пример #22
0
def make_pymodule_path(filename):
    path = os.path.dirname(filename)
    file = os.path.basename(filename)
    mod, ext = os.path.splitext(file)

    if sys.hexversion >= 0x3040000:
        import importlib.util
        fullpath = importlib.util.cache_from_source(filename, ext=='.pyc')
    elif sys.hexversion >= 0x3020000:
        import imp
        modname = mod+"."+imp.get_tag()+ext
        fullpath = os.path.join(path,'__pycache__',modname)
    else:
        fullpath = filename
    return fullpath
Пример #23
0
    def test_simple_built(self):

        # let's create a simple package
        tmp_dir = self.mkdtemp()
        pkg_dir = os.path.join(tmp_dir, 'foo')
        os.mkdir(pkg_dir)
        self.write_file((pkg_dir, 'setup.py'), SETUP_PY)
        self.write_file((pkg_dir, 'foo.py'), '#')
        self.write_file((pkg_dir, 'MANIFEST.in'), 'include foo.py')
        self.write_file((pkg_dir, 'README'), '')

        dist = Distribution({'name': 'foo', 'version': '0.1',
                             'py_modules': ['foo'],
                             'url': 'xxx', 'author': 'xxx',
                             'author_email': 'xxx'})
        dist.script_name = 'setup.py'
        os.chdir(pkg_dir)

        sys.argv = ['setup.py']
        cmd = bdist_dumb(dist)

        # so the output is the same no matter
        # what is the platform
        cmd.format = 'zip'

        cmd.ensure_finalized()
        cmd.run()

        # see what we have
        dist_created = os.listdir(os.path.join(pkg_dir, 'dist'))
        base = "%s.%s.zip" % (dist.get_fullname(), cmd.plat_name)
        if os.name == 'os2':
            base = base.replace(':', '-')

        self.assertEqual(dist_created, [base])

        # now let's check what we have in the zip file
        fp = zipfile.ZipFile(os.path.join('dist', base))
        try:
            contents = fp.namelist()
        finally:
            fp.close()

        contents = sorted(os.path.basename(fn) for fn in contents)
        wanted = ['foo-0.1-py%s.%s.egg-info' % sys.version_info[:2], 'foo.py']
        if not sys.dont_write_bytecode:
            wanted.append('foo.%s.pyc' % imp.get_tag())
        self.assertEqual(contents, sorted(wanted))
Пример #24
0
 def _cache(self, module):
     if hasattr(imp, "get_tag"):
         tag = imp.get_tag() + "-logment"
     else:
         if hasattr(sys, "pypy_version_info"):
             impl = "pypy"
         elif sys.platform == "java":
             impl = "jython"
         else:
             impl = "cpython"
         ver = sys.version_info
         tag = "%s-%s%s-logment" % (impl, ver[0], ver[1])
     ext = ".py" + (__debug__ and "c" or "o")
     tail = "." + tag + ext
     return os.path.join(os.path.dirname(self.filename), '__pycache__',
                         module.__name__ + tail)
Пример #25
0
def get_sys_info():
    import sys
    import distutils.sysconfig
    import imp
    print("exec_prefix:%s" % sys.exec_prefix)
    print("short_version:%s" % sys.version[:3])
    print("long_version:%s" % sys.version.split()[0])
    print("py_inc_dir:%s" % distutils.sysconfig.get_python_inc())
    print("site_packages_dir:%s" %
          distutils.sysconfig.get_python_lib(plat_specific=1))
    try:
        magic_tag = imp.get_tag()
    except AttributeError:
        magic_tag = ''
    print("magic_tag:%s" % magic_tag)
    return 0
Пример #26
0
    def test_package_data(self):
        sources = self.mkdtemp()
        f = open(os.path.join(sources, "__init__.py"), "w")
        try:
            f.write("# Pretend this is a package.")
        finally:
            f.close()
        f = open(os.path.join(sources, "README.txt"), "w")
        try:
            f.write("Info about this package")
        finally:
            f.close()

        destination = self.mkdtemp()

        dist = Distribution({"packages": ["pkg"],
                             "package_dir": {"pkg": sources}})
        # script_name need not exist, it just need to be initialized
        dist.script_name = os.path.join(sources, "setup.py")
        dist.command_obj["build"] = support.DummyCommand(
            force=0,
            build_lib=destination)
        dist.packages = ["pkg"]
        dist.package_data = {"pkg": ["README.txt"]}
        dist.package_dir = {"pkg": sources}

        cmd = build_py(dist)
        cmd.compile = 1
        cmd.ensure_finalized()
        self.assertEqual(cmd.package_data, dist.package_data)

        cmd.run()

        # This makes sure the list of outputs includes byte-compiled
        # files for Python modules but not for package data files
        # (there shouldn't *be* byte-code files for those!).
        self.assertEqual(len(cmd.get_outputs()), 3)
        pkgdest = os.path.join(destination, "pkg")
        files = os.listdir(pkgdest)
        pycache_dir = os.path.join(pkgdest, "__pycache__")
        self.assertIn("__init__.py", files)
        self.assertIn("README.txt", files)
        if sys.dont_write_bytecode:
            self.assertFalse(os.path.exists(pycache_dir))
        else:
            pyc_files = os.listdir(pycache_dir)
            self.assertIn("__init__.%s.pyc" % imp.get_tag(), pyc_files)
Пример #27
0
    def _gen_exclusion_paths():
        """
        Generate file paths to be excluded for namespace packages (bytecode
        cache files).
        """
        # always exclude the package module itself
        yield "__init__.py"

        yield "__init__.pyc"
        yield "__init__.pyo"

        if not hasattr(imp, "get_tag"):
            return

        base = os.path.join("__pycache__", "__init__." + imp.get_tag())
        yield base + ".pyc"
        yield base + ".pyo"
Пример #28
0
    def _gen_exclusion_paths():
        """
        Generate file paths to be excluded for namespace packages (bytecode
        cache files).
        """
        # always exclude the package module itself
        yield '__init__.py'

        yield '__init__.py.pyc'
        yield '__init__.py.pyo'

        if not hasattr(imp, 'get_tag'):
            return

        base = os.path.join('__pycache__', '__init__.py.' + imp.get_tag())
        yield base + '.pyc'
        yield base + '.pyo'
Пример #29
0
    def _gen_exclusion_paths():
        """
        Generate file paths to be excluded for namespace packages (bytecode
        cache files).
        """
        # always exclude the package module itself
        yield '__init__.py'

        yield '__init__.pyc'
        yield '__init__.pyo'

        if not hasattr(imp, 'get_tag'):
            return

        base = os.path.join('__pycache__', '__init__.' + imp.get_tag())
        yield base + '.pyc'
        yield base + '.pyo'
Пример #30
0
    def _gen_exclusion_paths():
        """
        Generate file paths to be excluded for namespace packages (bytecode
        cache files).
        """
        # always exclude the package module itself
        yield "__init__.py"

        yield "__init__.pyc"
        yield "__init__.pyo"

        if not hasattr(imp, "get_tag"):
            return

        base = os.path.join("__pycache__", "__init__." + imp.get_tag())
        yield base + ".pyc"
        yield base + ".pyo"
        yield base + ".opt-1.pyc"
        yield base + ".opt-2.pyc"
Пример #31
0
    def cache_zip_file(self, zip_path):
        """Read a zip file and cache the modules and packages found inside it.
        """
        zip = zipfile.ZipFile(zip_path)
        for archiveName in zip.namelist():
            baseName, ext = os.path.splitext(archiveName)
            if ext not in ('.pyc', '.pyo'):
                continue
            if '__pycache__' in baseName:
                if sys.version_info[:2] < (3, 2) \
                        or not baseName.endswith(imp.get_tag()):
                    continue
                baseName = os.path.splitext(source_from_cache(archiveName))[0]
            nameparts = baseName.split("/")
            
            if len(nameparts) > 1 and nameparts[-1] == '__init__':
                # dir/__init__.pyc  -> dir is a package
                self.record_loadable_module(nameparts[:-1], None, zip, True)

            self.record_loadable_module(nameparts, archiveName, zip, False)
Пример #32
0
    def cache_zip_file(self, zip_path):
        """Read a zip file and cache the modules and packages found inside it.
        """
        zip = zipfile.ZipFile(zip_path)
        for archiveName in zip.namelist():
            baseName, ext = os.path.splitext(archiveName)
            if ext not in ('.pyc', '.pyo'):
                continue
            if '__pycache__' in baseName:
                if sys.version_info[:2] < (3, 2) \
                        or not baseName.endswith(imp.get_tag()):
                    continue
                baseName = os.path.splitext(source_from_cache(archiveName))[0]
            nameparts = baseName.split("/")

            if len(nameparts) > 1 and nameparts[-1] == '__init__':
                # dir/__init__.pyc  -> dir is a package
                self.record_loadable_module(nameparts[:-1], None, zip, True)

            self.record_loadable_module(nameparts, archiveName, zip, False)
Пример #33
0
class PEP3147Tests(unittest.TestCase):
    """Tests of PEP 3147."""
    tag = imp.get_tag()

    @unittest.skipUnless(sys.implementation.cache_tag is not None,
                         'requires sys.implementation.cache_tag not be None')
    def test_cache_from_source(self):
        path = os.path.join('foo', 'bar', 'baz', 'qux.py')
        expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
                              'qux.{}.pyc'.format(self.tag))
        self.assertEqual(imp.cache_from_source(path, True), expect)

    @unittest.skipUnless(sys.implementation.cache_tag is not None,
                         'requires sys.implementation.cache_tag to not be None'
                         )
    def test_source_from_cache(self):
        path = os.path.join('foo', 'bar', 'baz', '__pycache__',
                            'qux.{}.pyc'.format(self.tag))
        expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
        self.assertEqual(imp.source_from_cache(path), expect)
Пример #34
0
    def test_simple_built(self):

        # let's create a simple package
        tmp_dir = self.mkdtemp()
        pkg_dir = os.path.join(tmp_dir, 'foo')
        os.mkdir(pkg_dir)
        self.write_file((pkg_dir, 'foo.py'), '#')
        self.write_file((pkg_dir, 'MANIFEST.in'), 'include foo.py')
        self.write_file((pkg_dir, 'README'), '')

        dist = Distribution({'name': 'foo', 'version': '0.1',
                             'py_modules': ['foo'],
                             'home_page': 'xxx', 'author': 'xxx',
                             'author_email': 'xxx'})
        os.chdir(pkg_dir)
        cmd = bdist_dumb(dist)

        # so the output is the same no matter
        # what is the platform
        cmd.format = 'zip'

        cmd.ensure_finalized()
        cmd.run()

        # see what we have
        dist_created = os.listdir(os.path.join(pkg_dir, 'dist'))
        base = "%s.%s.zip" % (dist.get_fullname(), cmd.plat_name)
        if os.name == 'os2':
            base = base.replace(':', '-')

        self.assertEqual(dist_created, [base])

        # now let's check what we have in the zip file
        with zipfile.ZipFile(os.path.join('dist', base)) as fp:
            contents = fp.namelist()

        contents = sorted(os.path.basename(fn) for fn in contents)
        wanted = ['foo.py',
                  'foo.%s.pyc' % imp.get_tag(),
                  'METADATA', 'INSTALLER', 'REQUESTED', 'RECORD']
        self.assertEqual(contents, sorted(wanted))
Пример #35
0
 def _get_pyc_filename_in_cache(self, full_path):
   
   full_path = os.path.normpath(full_path)
   
   dirname = os.path.dirname(full_path)
   cache_dir = self._get_cache_dir(dirname)
   if cache_dir is None:
     return None
   
   name = os.path.basename(full_path)
   if sys.version_info[:2] < (3, 0):
     name += '.cpython-%s%s' % sys.version_info[:2]
   else:
     name += '.' + imp.get_tag()
     
   if __debug__:
     name += '.pyc'
   else:
     name += '.pyo'
     
   cache_full_path = cache_dir + '/' + name
   return cache_full_path
Пример #36
0
def vengine_gen_find_module(self, module_name, path, so_suffixes):
    global _ma_triplet
    if _ma_triplet is None:
        try:
            import subprocess as sp
            p = sp.Popen(["gcc", "-print-multiarch"], stdout=sp.PIPE)
            _ma_triplet = str(p.communicate()[0].decode().strip())
        except:
            import warnings
            warnings.warn('failed to detect multiarch paths, please install gcc')

    for so_suffix in so_suffixes + ['.%s-%s.so' % (imp.get_tag(), _ma_triplet)]:
        basename = module_name + so_suffix
        if path is None:
            path = sys.path
            # import from non root package would try __pycache__ which is
            # cleaned by pypy installation
            path.insert(0, "/usr/lib/pypy/dist-packages/zmq/backend/cffi")
        for dirname in path:
            filename = os.path.join(dirname, basename)
            if os.path.isfile(filename):
                return filename
Пример #37
0
    def test_old_record(self):
        # test pre-PEP 376 --record option (outside dist-info dir)
        install_dir = self.mkdtemp()
        project_dir, dist = self.create_dist(py_modules=['hello'],
                                             scripts=['sayhi'])
        os.chdir(project_dir)
        self.write_file('hello.py', "def main(): print('o hai')")
        self.write_file('sayhi', 'from hello import main; main()')

        cmd = install_dist(dist)
        dist.command_obj['install_dist'] = cmd
        cmd.root = install_dir
        cmd.record = os.path.join(project_dir, 'filelist')
        cmd.ensure_finalized()
        cmd.run()

        with open(cmd.record) as f:
            content = f.read()

        found = [os.path.basename(line) for line in content.splitlines()]
        expected = ['hello.py', 'hello.%s.pyc' % imp.get_tag(), 'sayhi',
                    'METADATA', 'INSTALLER', 'REQUESTED', 'RECORD']
        self.assertEqual(sorted(found), sorted(expected))
Пример #38
0
def cache_from_source(path, debug_override=None):
    """Given the path to a .rbk file, return the path to its .pyc/.pyo file.

    A slight modification of the importlib.util.cache_from_source function
    from CPython 3.4.
    """
    debug = not sys.flags.optimize if debug_override is None else debug_override
    if debug:
        suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES
    else:
        suffixes = importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES
    head, tail = os.path.split(path)
    base_filename, sep, _ = tail.partition('.')
    try:
        py_tag = sys.implementation.cache_tag
    except AttributeError:
        # Fallback for Python versions before 3.3
        import imp
        py_tag = imp.get_tag()
    # (1) Make sure the cache tag contains both Rulebook version and Python version
    # (2) Separate rulebook `pyc`s so that the normal import mechanism doesn't try to load them
    tag = '%s-%s' % (RBK_TAG, py_tag)
    filename = ''.join([base_filename, sep, tag, suffixes[0]])
    return os.path.join(head, '__pycache__', filename)
Пример #39
0
import re
import six
import struct
import sys
import types

import atomicwrites
import py

from _pytest.assertion import util
from _pytest.compat import PurePath, spec_from_file_location
from _pytest.paths import fnmatch_ex

# pytest caches rewritten pycs in __pycache__.
if hasattr(imp, "get_tag"):
    PYTEST_TAG = imp.get_tag() + "-PYTEST"
else:
    if hasattr(sys, "pypy_version_info"):
        impl = "pypy"
    elif sys.platform == "java":
        impl = "jython"
    else:
        impl = "cpython"
    ver = sys.version_info
    PYTEST_TAG = "%s-%s%s-PYTEST" % (impl, ver[0], ver[1])
    del ver, impl

PYC_EXT = ".py" + (__debug__ and "c" or "o")
PYC_TAIL = "." + PYTEST_TAG + PYC_EXT

ASCII_IS_DEFAULT_ENCODING = sys.version_info[0] < 3
Пример #40
0
import sys
import types

import py
from _pytest.assertion import util


# Windows gives ENOENT in places *nix gives ENOTDIR.
if sys.platform.startswith("win"):
    PATH_COMPONENT_NOT_DIR = errno.ENOENT
else:
    PATH_COMPONENT_NOT_DIR = errno.ENOTDIR

# py.test caches rewritten pycs in __pycache__.
if hasattr(imp, "get_tag"):
    PYTEST_TAG = imp.get_tag() + "-PYTEST"
else:
    if hasattr(sys, "pypy_version_info"):
        impl = "pypy"
    elif sys.platform == "java":
        impl = "jython"
    else:
        impl = "cpython"
    ver = sys.version_info
    PYTEST_TAG = "%s-%s%s-PYTEST" % (impl, ver[0], ver[1])
    del ver, impl

PYC_EXT = ".py" + "c" if __debug__ else "o"
PYC_TAIL = "." + PYTEST_TAG + PYC_EXT

REWRITE_NEWLINES = sys.version_info[:2] != (2, 7) and sys.version_info < (3, 2)
Пример #41
0
 def test_get_tag(self):
     import imp
     import sys
     assert imp.get_tag() == 'pypy3-%d%d' % sys.pypy_version_info[0:2]
Пример #42
0
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the
#   Free Software Foundation, Inc.,
#   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# This file incorporates work covered by the following copyright and
# permission notice:
#
#     Copyright (c) 2007, Simon Edwards <*****@*****.**>
#     Redistribution and use is allowed according to the terms of the BSD
#     license. For details see the accompanying COPYING-CMAKE-SCRIPTS file.

import sys, distutils.sysconfig, imp

print("exec_prefix:%s" % sys.exec_prefix)
print("short_version:%s" % sys.version[:3])
print("long_version:%s" % sys.version.split()[0])
print("py_inc_dir:%s" % distutils.sysconfig.get_python_inc())
print("site_packages_dir:%s" %
      distutils.sysconfig.get_python_lib(plat_specific=1))
try:
    magic_tag = imp.get_tag()
except AttributeError:
    magic_tag = ''
print("magic_tag:%s" % magic_tag)
Пример #43
0
class PycacheTests(unittest.TestCase):
    # Test the various PEP 3147 related behaviors.

    tag = imp.get_tag()

    def _clean(self):
        forget(TESTFN)
        rmtree('__pycache__')
        unlink(self.source)

    def setUp(self):
        self.source = TESTFN + '.py'
        self._clean()
        with open(self.source, 'w') as fp:
            print('# This is a test file written by test_import.py', file=fp)
        sys.path.insert(0, os.curdir)
        importlib.invalidate_caches()

    def tearDown(self):
        assert sys.path[0] == os.curdir, 'Unexpected sys.path[0]'
        del sys.path[0]
        self._clean()

    def test_import_pyc_path(self):
        self.assertFalse(os.path.exists('__pycache__'))
        __import__(TESTFN)
        self.assertTrue(os.path.exists('__pycache__'))
        self.assertTrue(
            os.path.exists(
                os.path.join(
                    '__pycache__',
                    '{}.{}.py{}'.format(TESTFN, self.tag, __debug__ and 'c'
                                        or 'o'))))

    @unittest.skipUnless(os.name == 'posix',
                         "test meaningful only on posix systems")
    @unittest.skipIf(
        hasattr(os, 'geteuid') and os.geteuid() == 0,
        "due to varying filesystem permission semantics (issue #11956)")
    def test_unwritable_directory(self):
        # When the umask causes the new __pycache__ directory to be
        # unwritable, the import still succeeds but no .pyc file is written.
        with temp_umask(0o222):
            __import__(TESTFN)
        self.assertTrue(os.path.exists('__pycache__'))
        self.assertFalse(
            os.path.exists(
                os.path.join('__pycache__',
                             '{}.{}.pyc'.format(TESTFN, self.tag))))

    def test_missing_source(self):
        # With PEP 3147 cache layout, removing the source but leaving the pyc
        # file does not satisfy the import.
        __import__(TESTFN)
        pyc_file = imp.cache_from_source(self.source)
        self.assertTrue(os.path.exists(pyc_file))
        os.remove(self.source)
        forget(TESTFN)
        self.assertRaises(ImportError, __import__, TESTFN)

    def test_missing_source_legacy(self):
        # Like test_missing_source() except that for backward compatibility,
        # when the pyc file lives where the py file would have been (and named
        # without the tag), it is importable.  The __file__ of the imported
        # module is the pyc location.
        __import__(TESTFN)
        # pyc_file gets removed in _clean() via tearDown().
        pyc_file = make_legacy_pyc(self.source)
        os.remove(self.source)
        unload(TESTFN)
        importlib.invalidate_caches()
        m = __import__(TESTFN)
        self.assertEqual(m.__file__,
                         os.path.join(os.curdir, os.path.relpath(pyc_file)))

    def test___cached__(self):
        # Modules now also have an __cached__ that points to the pyc file.
        m = __import__(TESTFN)
        pyc_file = imp.cache_from_source(TESTFN + '.py')
        self.assertEqual(m.__cached__, os.path.join(os.curdir, pyc_file))

    def test___cached___legacy_pyc(self):
        # Like test___cached__() except that for backward compatibility,
        # when the pyc file lives where the py file would have been (and named
        # without the tag), it is importable.  The __cached__ of the imported
        # module is the pyc location.
        __import__(TESTFN)
        # pyc_file gets removed in _clean() via tearDown().
        pyc_file = make_legacy_pyc(self.source)
        os.remove(self.source)
        unload(TESTFN)
        importlib.invalidate_caches()
        m = __import__(TESTFN)
        self.assertEqual(m.__cached__,
                         os.path.join(os.curdir, os.path.relpath(pyc_file)))

    def test_package___cached__(self):
        # Like test___cached__ but for packages.
        def cleanup():
            rmtree('pep3147')
            unload('pep3147.foo')
            unload('pep3147')

        os.mkdir('pep3147')
        self.addCleanup(cleanup)
        # Touch the __init__.py
        with open(os.path.join('pep3147', '__init__.py'), 'w'):
            pass
        with open(os.path.join('pep3147', 'foo.py'), 'w'):
            pass
        importlib.invalidate_caches()
        m = __import__('pep3147.foo')
        init_pyc = imp.cache_from_source(os.path.join('pep3147',
                                                      '__init__.py'))
        self.assertEqual(m.__cached__, os.path.join(os.curdir, init_pyc))
        foo_pyc = imp.cache_from_source(os.path.join('pep3147', 'foo.py'))
        self.assertEqual(sys.modules['pep3147.foo'].__cached__,
                         os.path.join(os.curdir, foo_pyc))

    def test_package___cached___from_pyc(self):
        # Like test___cached__ but ensuring __cached__ when imported from a
        # PEP 3147 pyc file.
        def cleanup():
            rmtree('pep3147')
            unload('pep3147.foo')
            unload('pep3147')

        os.mkdir('pep3147')
        self.addCleanup(cleanup)
        # Touch the __init__.py
        with open(os.path.join('pep3147', '__init__.py'), 'w'):
            pass
        with open(os.path.join('pep3147', 'foo.py'), 'w'):
            pass
        importlib.invalidate_caches()
        m = __import__('pep3147.foo')
        unload('pep3147.foo')
        unload('pep3147')
        importlib.invalidate_caches()
        m = __import__('pep3147.foo')
        init_pyc = imp.cache_from_source(os.path.join('pep3147',
                                                      '__init__.py'))
        self.assertEqual(m.__cached__, os.path.join(os.curdir, init_pyc))
        foo_pyc = imp.cache_from_source(os.path.join('pep3147', 'foo.py'))
        self.assertEqual(sys.modules['pep3147.foo'].__cached__,
                         os.path.join(os.curdir, foo_pyc))

    def test_recompute_pyc_same_second(self):
        # Even when the source file doesn't change timestamp, a change in
        # source size is enough to trigger recomputation of the pyc file.
        __import__(TESTFN)
        unload(TESTFN)
        with open(self.source, 'a') as fp:
            print("x = 5", file=fp)
        m = __import__(TESTFN)
        self.assertEqual(m.x, 5)
Пример #44
0
class PEP3147Tests(unittest.TestCase):
    """Tests of PEP 3147."""

    tag = imp.get_tag()

    def test_cache_from_source(self):
        # Given the path to a .py file, return the path to its PEP 3147
        # defined .pyc file (i.e. under __pycache__).
        self.assertEqual(
            imp.cache_from_source('/foo/bar/baz/qux.py', True),
            '/foo/bar/baz/__pycache__/qux.{}.pyc'.format(self.tag))

    def test_cache_from_source_optimized(self):
        # Given the path to a .py file, return the path to its PEP 3147
        # defined .pyo file (i.e. under __pycache__).
        self.assertEqual(
            imp.cache_from_source('/foo/bar/baz/qux.py', False),
            '/foo/bar/baz/__pycache__/qux.{}.pyo'.format(self.tag))

    def test_cache_from_source_cwd(self):
        self.assertEqual(
            imp.cache_from_source('foo.py', True),
            os.sep.join(('__pycache__', 'foo.{}.pyc'.format(self.tag))))

    def test_cache_from_source_override(self):
        # When debug_override is not None, it can be any true-ish or false-ish
        # value.
        self.assertEqual(imp.cache_from_source('/foo/bar/baz.py', []),
                         '/foo/bar/__pycache__/baz.{}.pyo'.format(self.tag))
        self.assertEqual(imp.cache_from_source('/foo/bar/baz.py', [17]),
                         '/foo/bar/__pycache__/baz.{}.pyc'.format(self.tag))

        # However if the bool-ishness can't be determined, the exception
        # propagates.
        class Bearish:
            def __bool__(self):
                raise RuntimeError

        self.assertRaises(RuntimeError, imp.cache_from_source,
                          '/foo/bar/baz.py', Bearish())

    @unittest.skipIf(os.altsep is None,
                     'test meaningful only where os.altsep is defined')
    def test_altsep_cache_from_source(self):
        # Windows path and PEP 3147.
        self.assertEqual(
            imp.cache_from_source('\\foo\\bar\\baz\\qux.py', True),
            '\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag))

    @unittest.skipIf(os.altsep is None,
                     'test meaningful only where os.altsep is defined')
    def test_altsep_and_sep_cache_from_source(self):
        # Windows path and PEP 3147 where altsep is right of sep.
        self.assertEqual(
            imp.cache_from_source('\\foo\\bar/baz\\qux.py', True),
            '\\foo\\bar/baz\\__pycache__\\qux.{}.pyc'.format(self.tag))

    @unittest.skipIf(os.altsep is None,
                     'test meaningful only where os.altsep is defined')
    def test_sep_altsep_and_sep_cache_from_source(self):
        # Windows path and PEP 3147 where sep is right of altsep.
        self.assertEqual(
            imp.cache_from_source('\\foo\\bar\\baz/qux.py', True),
            '\\foo\\bar\\baz/__pycache__/qux.{}.pyc'.format(self.tag))

    def test_source_from_cache(self):
        # Given the path to a PEP 3147 defined .pyc file, return the path to
        # its source.  This tests the good path.
        self.assertEqual(
            imp.source_from_cache('/foo/bar/baz/__pycache__/qux.{}.pyc'.format(
                self.tag)), '/foo/bar/baz/qux.py')

    def test_source_from_cache_bad_path(self):
        # When the path to a pyc file is not in PEP 3147 format, a ValueError
        # is raised.
        self.assertRaises(ValueError, imp.source_from_cache,
                          '/foo/bar/bazqux.pyc')

    def test_source_from_cache_no_slash(self):
        # No slashes at all in path -> ValueError
        self.assertRaises(ValueError, imp.source_from_cache,
                          'foo.cpython-32.pyc')

    def test_source_from_cache_too_few_dots(self):
        # Too few dots in final path component -> ValueError
        self.assertRaises(ValueError, imp.source_from_cache,
                          '__pycache__/foo.pyc')

    def test_source_from_cache_too_many_dots(self):
        # Too many dots in final path component -> ValueError
        self.assertRaises(ValueError, imp.source_from_cache,
                          '__pycache__/foo.cpython-32.foo.pyc')

    def test_source_from_cache_no__pycache__(self):
        # Another problem with the path -> ValueError
        self.assertRaises(ValueError, imp.source_from_cache,
                          '/foo/bar/foo.cpython-32.foo.pyc')

    def test_package___file__(self):
        # Test that a package's __file__ points to the right source directory.
        os.mkdir('pep3147')
        sys.path.insert(0, os.curdir)

        def cleanup():
            if sys.path[0] == os.curdir:
                del sys.path[0]
            shutil.rmtree('pep3147')

        self.addCleanup(cleanup)
        # Touch the __init__.py file.
        with open('pep3147/__init__.py', 'w'):
            pass
        m = __import__('pep3147')
        # Ensure we load the pyc file.
        support.forget('pep3147')
        m = __import__('pep3147')
        self.assertEqual(m.__file__,
                         os.sep.join(('.', 'pep3147', '__init__.py')))
    # Check if its a shared library and deconstruct it
    if soabi in dep_path:
        if (debug==True):
            log.write('Shared library found in %s' % dep_path)
        dep_path = dep_path.replace(soabi,'*')
        print (dep_path)
        continue

    if (debug==True):
        log.write(dep_path+'\n')
    # Prints out result, which is what will be used by create_manifest
    print (dep_path)


    import imp
    cpython_tag = imp.get_tag() 
    cached=''
    # Theres no naive way to find *.pyc files on python3
    try:
        if (debug==True):
            log.write('Calling: sys.modules[' + '%s' % item + '].__cached__\n')
        cached = sys.modules['%s' % item].__cached__
    except AttributeError as e:
        # Deals with thread (builtin module) not having __cached__ attribute
        if debug==True:
            log.write(item + ' ')
            log.write(str(e))
            log.write('\n')
        pass
    except NameError as e:
        # Deals with NameError: name 'cached' is not defined
Пример #46
0
 def update_event(self, inp=-1):
     self.set_output_val(0, imp.get_tag())
Пример #47
0
    pyc_name = os_path.join(cage_dir, "pyc.pyc")

    write_module(py_name,
                 "__all__ = ['get_name']\n"
                 "def get_name():\n"
                 "    return __name__\n"
                 "# (NO LONGER NEEDED) EOF")

    Popen([ python, "-c", "import pyc" ], cwd = cage_dir).wait()

    try:
        from imp import get_tag
    except ImportError:
        pass
    else:
        pycache_name = os_path.join(cage_dir, "__pycache__", "pyc.{0:s}.pyc".format(get_tag()))
        assert os_path.isfile(pycache_name)
        rename(pycache_name, pyc_name)
        assert not os_path.isfile(pycache_name)

    remove(py_name)
    assert not os_path.isfile(py_name)

    assert os_path.isfile(pyc_name)
    assert pmnc.pyc.get_name() == "pyc"

    print("ok")

    ###################################

    print("module reload timeout: ", end = "")
Пример #48
0
 def test_get_tag(self):
     import imp
     import sys
     if not hasattr(sys, 'pypy_version_info'):
         skip('This test is PyPy-only')
     assert imp.get_tag() == 'pypy3-%d%d' % sys.pypy_version_info[0:2]
Пример #49
0
    def test_package_data(self):
        sources = self.mkdtemp()
        pkg_dir = os.path.join(sources, 'pkg')
        os.mkdir(pkg_dir)
        f = open(os.path.join(pkg_dir, "__init__.py"), "w")
        try:
            f.write("# Pretend this is a package.")
        finally:
            f.close()
        # let's have two files to make sure globbing works
        f = open(os.path.join(pkg_dir, "README.txt"), "w")
        try:
            f.write("Info about this package")
        finally:
            f.close()
        f = open(os.path.join(pkg_dir, "HACKING.txt"), "w")
        try:
            f.write("How to contribute")
        finally:
            f.close()

        destination = self.mkdtemp()

        dist = Distribution({"packages": ["pkg"], "package_dir": sources})

        dist.command_obj["build"] = support.DummyCommand(
            force=False,
            build_lib=destination,
            use_2to3_fixers=None,
            convert_2to3_doctests=None,
            use_2to3=False)
        dist.packages = ["pkg"]
        dist.package_data = {"pkg": ["*.txt"]}
        dist.package_dir = sources

        cmd = build_py(dist)
        cmd.compile = True
        cmd.ensure_finalized()
        self.assertEqual(cmd.package_data, dist.package_data)

        cmd.run()

        # This makes sure the list of outputs includes byte-compiled
        # files for Python modules but not for package data files
        # (there shouldn't *be* byte-code files for those!).
        # FIXME the test below is not doing what the comment above says, and
        # if it did it would show a code bug: if we add a demo.py file to
        # package_data, it gets byte-compiled!
        outputs = cmd.get_outputs()
        self.assertEqual(len(outputs), 4, outputs)
        pkgdest = os.path.join(destination, "pkg")
        files = os.listdir(pkgdest)
        wanted = ["__init__.py", "HACKING.txt", "README.txt"]
        if sys.version_info[:2] == (3, 1):
            wanted.append("__init__.pyc")
        else:
            wanted.append("__pycache__")
        self.assertEqual(sorted(files), sorted(wanted))
        if sys.version_info[:2] >= (3, 2):
            pycache_dir = os.path.join(pkgdest, "__pycache__")
            pyc_files = os.listdir(pycache_dir)
            self.assertEqual(["__init__.%s.pyc" % imp.get_tag()], pyc_files)
Пример #50
0
class PEP3147Tests(unittest.TestCase):
    """Tests of PEP 3147."""

    tag = imp.get_tag()

    @unittest.skipUnless(sys.implementation.cache_tag is not None,
                         'requires sys.implementation.cache_tag not be None')
    def test_cache_from_source(self):
        # Given the path to a .py file, return the path to its PEP 3147
        # defined .pyc file (i.e. under __pycache__).
        path = os.path.join('foo', 'bar', 'baz', 'qux.py')
        expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
                              'qux.{}.pyc'.format(self.tag))
        self.assertEqual(imp.cache_from_source(path, True), expect)

    def test_cache_from_source_no_cache_tag(self):
        # Non cache tag means NotImplementedError.
        with support.swap_attr(sys.implementation, 'cache_tag', None):
            with self.assertRaises(NotImplementedError):
                imp.cache_from_source('whatever.py')

    def test_cache_from_source_no_dot(self):
        # Directory with a dot, filename without dot.
        path = os.path.join('foo.bar', 'file')
        expect = os.path.join('foo.bar', '__pycache__',
                              'file{}.pyc'.format(self.tag))
        self.assertEqual(imp.cache_from_source(path, True), expect)

    def test_cache_from_source_optimized(self):
        # Given the path to a .py file, return the path to its PEP 3147
        # defined .pyo file (i.e. under __pycache__).
        path = os.path.join('foo', 'bar', 'baz', 'qux.py')
        expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
                              'qux.{}.pyo'.format(self.tag))
        self.assertEqual(imp.cache_from_source(path, False), expect)

    def test_cache_from_source_cwd(self):
        path = 'foo.py'
        expect = os.path.join('__pycache__', 'foo.{}.pyc'.format(self.tag))
        self.assertEqual(imp.cache_from_source(path, True), expect)

    def test_cache_from_source_override(self):
        # When debug_override is not None, it can be any true-ish or false-ish
        # value.
        path = os.path.join('foo', 'bar', 'baz.py')
        partial_expect = os.path.join('foo', 'bar', '__pycache__',
                                      'baz.{}.py'.format(self.tag))
        self.assertEqual(imp.cache_from_source(path, []), partial_expect + 'o')
        self.assertEqual(imp.cache_from_source(path, [17]),
                         partial_expect + 'c')

        # However if the bool-ishness can't be determined, the exception
        # propagates.
        class Bearish:
            def __bool__(self):
                raise RuntimeError

        with self.assertRaises(RuntimeError):
            imp.cache_from_source('/foo/bar/baz.py', Bearish())

    @unittest.skipUnless(os.sep == '\\' and os.altsep == '/',
                         'test meaningful only where os.altsep is defined')
    def test_sep_altsep_and_sep_cache_from_source(self):
        # Windows path and PEP 3147 where sep is right of altsep.
        self.assertEqual(
            imp.cache_from_source('\\foo\\bar\\baz/qux.py', True),
            '\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag))

    @unittest.skipUnless(sys.implementation.cache_tag is not None,
                         'requires sys.implementation.cache_tag to not be '
                         'None')
    def test_source_from_cache(self):
        # Given the path to a PEP 3147 defined .pyc file, return the path to
        # its source.  This tests the good path.
        path = os.path.join('foo', 'bar', 'baz', '__pycache__',
                            'qux.{}.pyc'.format(self.tag))
        expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
        self.assertEqual(imp.source_from_cache(path), expect)

    def test_source_from_cache_no_cache_tag(self):
        # If sys.implementation.cache_tag is None, raise NotImplementedError.
        path = os.path.join('blah', '__pycache__', 'whatever.pyc')
        with support.swap_attr(sys.implementation, 'cache_tag', None):
            with self.assertRaises(NotImplementedError):
                imp.source_from_cache(path)

    def test_source_from_cache_bad_path(self):
        # When the path to a pyc file is not in PEP 3147 format, a ValueError
        # is raised.
        self.assertRaises(ValueError, imp.source_from_cache,
                          '/foo/bar/bazqux.pyc')

    def test_source_from_cache_no_slash(self):
        # No slashes at all in path -> ValueError
        self.assertRaises(ValueError, imp.source_from_cache,
                          'foo.cpython-32.pyc')

    def test_source_from_cache_too_few_dots(self):
        # Too few dots in final path component -> ValueError
        self.assertRaises(ValueError, imp.source_from_cache,
                          '__pycache__/foo.pyc')

    def test_source_from_cache_too_many_dots(self):
        # Too many dots in final path component -> ValueError
        self.assertRaises(ValueError, imp.source_from_cache,
                          '__pycache__/foo.cpython-32.foo.pyc')

    def test_source_from_cache_no__pycache__(self):
        # Another problem with the path -> ValueError
        self.assertRaises(ValueError, imp.source_from_cache,
                          '/foo/bar/foo.cpython-32.foo.pyc')

    def test_package___file__(self):
        try:
            m = __import__('pep3147')
        except ImportError:
            pass
        else:
            self.fail("pep3147 module already exists: %r" % (m, ))
        # Test that a package's __file__ points to the right source directory.
        os.mkdir('pep3147')
        sys.path.insert(0, os.curdir)

        def cleanup():
            if sys.path[0] == os.curdir:
                del sys.path[0]
            shutil.rmtree('pep3147')

        self.addCleanup(cleanup)
        # Touch the __init__.py file.
        support.create_empty_file('pep3147/__init__.py')
        importlib.invalidate_caches()
        expected___file__ = os.sep.join(('.', 'pep3147', '__init__.py'))
        m = __import__('pep3147')
        self.assertEqual(
            m.__file__, expected___file__,
            (m.__file__, m.__path__, sys.path, sys.path_importer_cache))
        # Ensure we load the pyc file.
        support.unload('pep3147')
        m = __import__('pep3147')
        support.unload('pep3147')
        self.assertEqual(
            m.__file__, expected___file__,
            (m.__file__, m.__path__, sys.path, sys.path_importer_cache))