pypy / pypy / module / imp / test / test_app.py

Armin Rigo 48995d6 
Amaury Forgeot d… ad5cfb1 

Benjamin Peterso… bdebcd1 


Amaury Forgeot d… 40f54ef 
Amaury Forgeot d… d6e7687 
Benjamin Peterso… bdebcd1 

Alex Gaynor dde0251 
Antonio Cuni 7f5530c 




Benjamin Peterso… bdebcd1 
Alex Gaynor dde0251 





Benjamin Peterso… bdebcd1 
Alex Gaynor dde0251 




Antonio Cuni 138699b 
Alex Gaynor dde0251 


Benjamin Peterso… bdebcd1 


Antonio Cuni 58feb84 
Benjamin Peterso… bdebcd1 






Antonio Cuni 7f5530c 











Michael Foord 44001db 

Amaury Forgeot d… 7293a2a 

Benjamin Peterso… bdebcd1 








David Schneider 3c10bab 


Benjamin Peterso… bdebcd1 























Carl Friedrich B… 8b504e2 

Benjamin Peterso… bdebcd1 


















Carl Friedrich B… 8b504e2 
Benjamin Peterso… bdebcd1 

















Maciej Fijalkows… c2ae102 










Amaury Forgeot d… d99f756 





Alex Gaynor 99a2441 







Alex Gaynor 0fb739c 

Amaury Forgeot d… f0ac076 







Antonio Cuni ec3859b 
Amaury Forgeot d… f0ac076 





Antonio Cuni ec3859b 
Amaury Forgeot d… f0ac076 







Antonio Cuni 58feb84 
Amaury Forgeot d… f0ac076 















Antonio Cuni ec3859b 
Amaury Forgeot d… f0ac076 












Philip Jenvey e23475a 



Philip Jenvey 68ad4df 
from __future__ import with_statement
from pypy.tool.udir import udir

MARKER = 42

class AppTestImpModule:
    spaceconfig = dict(usemodules=('imp',))

    def setup_class(cls):
        cls.w_imp = cls.space.getbuiltinmodule('imp')
        cls.w_file_module = cls.space.wrap(__file__)
        latin1 = udir.join('latin1.py')
        latin1.write("# -*- coding: iso-8859-1 -*\n")
        fake_latin1 = udir.join('fake_latin1.py')
        fake_latin1.write("print('-*- coding: iso-8859-1 -*')")
        cls.w_udir = cls.space.wrap(str(udir))

    def w__py_file(self):
        fn = self.file_module
        if fn.lower().endswith('c') or fn.lower().endswith('o'):
            fn = fn[:-1]
        assert fn.lower().endswith('.py')
        return fn

    def w__pyc_file(self):
        import marshal, imp
        co = compile("marker=42", "x.py", "exec")
        f = open('@TEST.pyc', 'wb')
        f.write(imp.get_magic())
        f.write(b'\x00\x00\x00\x00')
        marshal.dump(co, f)
        f.close()
        return '@TEST.pyc'

    def test_find_module(self):
        import os
        file, pathname, description = self.imp.find_module('cmd')
        assert file is not None
        file.close()
        assert os.path.exists(pathname)
        pathname = pathname.lower()
        assert pathname.endswith('.py') # even if .pyc is up-to-date
        assert description in self.imp.get_suffixes()

    def test_find_module_with_encoding(self):
        import sys
        sys.path.insert(0, self.udir)
        try:
            file, pathname, description = self.imp.find_module('latin1')
            assert file.encoding == 'iso-8859-1'
            #
            file, pathname, description = self.imp.find_module('fake_latin1')
            assert file.encoding == 'utf-8'
        finally:
            del sys.path[0]

    def test_load_dynamic(self):
        raises(ImportError, self.imp.load_dynamic, 'foo', 'bar')
        raises(ImportError, self.imp.load_dynamic, 'foo', 'bar',
               open(self.file_module))

    def test_suffixes(self):
        for suffix, mode, type in self.imp.get_suffixes():
            if mode == self.imp.PY_SOURCE:
                assert suffix == '.py'
                assert type == 'r'
            elif mode == self.imp.PY_COMPILED:
                assert suffix in ('.pyc', '.pyo')
                assert type == 'rb'
            elif mode == self.imp.C_EXTENSION:
                assert suffix.endswith(('.pyd', '.so'))
                assert type == 'rb'


    def test_obscure_functions(self):
        mod = self.imp.new_module('hi')
        assert mod.__name__ == 'hi'
        mod = self.imp.init_builtin('hello.world.this.is.never.a.builtin.module.name')
        assert mod is None
        mod = self.imp.init_frozen('hello.world.this.is.never.a.frozen.module.name')
        assert mod is None
        assert self.imp.is_builtin('sys')
        assert not self.imp.is_builtin('hello.world.this.is.never.a.builtin.module.name')
        assert not self.imp.is_frozen('hello.world.this.is.never.a.frozen.module.name')


    def test_load_module_py(self):
        fn = self._py_file()
        descr = ('.py', 'U', self.imp.PY_SOURCE)
        f = open(fn, 'U')
        mod = self.imp.load_module('test_imp_extra_AUTO1', f, fn, descr)
        f.close()
        assert mod.MARKER == 42
        import test_imp_extra_AUTO1
        assert mod is test_imp_extra_AUTO1

    def test_load_module_pyc_1(self):
        import os
        fn = self._pyc_file()
        try:
            descr = ('.pyc', 'rb', self.imp.PY_COMPILED)
            f = open(fn, 'rb')
            mod = self.imp.load_module('test_imp_extra_AUTO2', f, fn, descr)
            f.close()
            assert mod.marker == 42
            import test_imp_extra_AUTO2
            assert mod is test_imp_extra_AUTO2
        finally:
            os.unlink(fn)

    def test_load_source(self):
        fn = self._py_file()
        mod = self.imp.load_source('test_imp_extra_AUTO3', fn)
        assert mod.MARKER == 42
        import test_imp_extra_AUTO3
        assert mod is test_imp_extra_AUTO3

    def test_load_module_pyc_2(self):
        import os
        fn = self._pyc_file()
        try:
            mod = self.imp.load_compiled('test_imp_extra_AUTO4', fn)
            assert mod.marker == 42
            import test_imp_extra_AUTO4
            assert mod is test_imp_extra_AUTO4
        finally:
            os.unlink(fn)

    def test_load_broken_pyc(self):
        fn = self._py_file()
        try:
            self.imp.load_compiled('test_imp_extra_AUTO5', fn)
        except ImportError:
            pass
        else:
            raise Exception("expected an ImportError")

    def test_load_module_in_sys_modules(self):
        fn = self._py_file()
        f = open(fn, 'rb')
        descr = ('.py', 'U', self.imp.PY_SOURCE)
        mod = self.imp.load_module('test_imp_extra_AUTO6', f, fn, descr)
        f.close()
        f = open(fn, 'rb')
        mod2 = self.imp.load_module('test_imp_extra_AUTO6', f, fn, descr)
        f.close()
        assert mod2 is mod

    def test_nullimporter(self):
        import os
        importer = self.imp.NullImporter("path")
        assert importer.find_module(1, 2, 3, 4) is None
        raises(ImportError, self.imp.NullImporter, os.getcwd())

    def test_path_importer_cache(self):
        import os
        import sys

        lib_pypy = os.path.abspath(
            os.path.join(self.file_module, "..", "..", "..", "..", "..", "lib_pypy")
        )
        # Doesn't end up in there when run with -A
        assert sys.path_importer_cache.get(lib_pypy) is None

    def test_rewrite_pyc_check_code_name(self):
        # This one is adapted from cpython's Lib/test/test_import.py
        from os import chmod
        from os.path import join
        from sys import modules, path
        from shutil import rmtree
        from tempfile import mkdtemp
        code = b"""if 1:
            import sys
            code_filename = sys._getframe().f_code.co_filename
            module_filename = __file__
            constant = 1
            def func():
                pass
            func_filename = func.__code__.co_filename
            """

        module_name = "unlikely_module_name"
        dir_name = mkdtemp(prefix='pypy_test')
        file_name = join(dir_name, module_name + '.py')
        with open(file_name, "wb") as f:
            f.write(code)
        compiled_name = file_name + ("c" if __debug__ else "o")
        chmod(file_name, 0o777)

        # Setup
        sys_path = path[:]
        orig_module = modules.pop(module_name, None)
        assert modules.get(module_name) == None
        path.insert(0, dir_name)

        # Test
        import py_compile
        py_compile.compile(file_name, dfile="another_module.py")
        __import__(module_name, globals(), locals())
        mod = modules.get(module_name)

        try:
            # Ensure proper results
            assert mod != orig_module
            assert mod.module_filename == file_name
            assert mod.code_filename == file_name
            assert mod.func_filename == file_name
        finally:
            # TearDown
            path[:] = sys_path
            if orig_module is not None:
                modules[module_name] = orig_module
            else:
                try:
                    del modules[module_name]
                except KeyError:
                    pass
            rmtree(dir_name, True)

    def test_get_tag(self):
        import imp
        import sys
        assert imp.get_tag() == 'pypy-%d%d' % sys.pypy_version_info[0:2]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.