Exemplo n.º 1
0
    def test_recursion_limit(self):
        subpackage = os.path.join(self.pkgdir, "spam")
        subpackage2 = os.path.join(subpackage, "ham")
        subpackage3 = os.path.join(subpackage2, "eggs")
        for pkg in (subpackage, subpackage2, subpackage3):
            script_helper.make_pkg(pkg)

        subinitfn = os.path.join(subpackage, "__init__.py")
        hamfn = script_helper.make_script(subpackage, "ham", "")
        spamfn = script_helper.make_script(subpackage2, "spam", "")
        eggfn = script_helper.make_script(subpackage3, "egg", "")

        self.assertRunOK("-q", "-r 0", self.pkgdir)
        self.assertNotCompiled(subinitfn)
        self.assertFalse(os.path.exists(os.path.join(subpackage, "__pycache__")))

        self.assertRunOK("-q", "-r 1", self.pkgdir)
        self.assertCompiled(subinitfn)
        self.assertCompiled(hamfn)
        self.assertNotCompiled(spamfn)

        self.assertRunOK("-q", "-r 2", self.pkgdir)
        self.assertCompiled(subinitfn)
        self.assertCompiled(hamfn)
        self.assertCompiled(spamfn)
        self.assertNotCompiled(eggfn)

        self.assertRunOK("-q", "-r 5", self.pkgdir)
        self.assertCompiled(subinitfn)
        self.assertCompiled(hamfn)
        self.assertCompiled(spamfn)
        self.assertCompiled(eggfn)
Exemplo n.º 2
0
    def test_recursion_limit(self):
        subpackage = os.path.join(self.pkgdir, 'spam')
        subpackage2 = os.path.join(subpackage, 'ham')
        subpackage3 = os.path.join(subpackage2, 'eggs')
        for pkg in (subpackage, subpackage2, subpackage3):
            script_helper.make_pkg(pkg)

        subinitfn = os.path.join(subpackage, '__init__.py')
        hamfn = script_helper.make_script(subpackage, 'ham', '')
        spamfn = script_helper.make_script(subpackage2, 'spam', '')
        eggfn = script_helper.make_script(subpackage3, 'egg', '')

        self.assertRunOK('-q', '-r 0', self.pkgdir)
        self.assertNotCompiled(subinitfn)
        self.assertFalse(
            os.path.exists(os.path.join(subpackage, '__pycache__')))

        self.assertRunOK('-q', '-r 1', self.pkgdir)
        self.assertCompiled(subinitfn)
        self.assertCompiled(hamfn)
        self.assertNotCompiled(spamfn)

        self.assertRunOK('-q', '-r 2', self.pkgdir)
        self.assertCompiled(subinitfn)
        self.assertCompiled(hamfn)
        self.assertCompiled(spamfn)
        self.assertNotCompiled(eggfn)

        self.assertRunOK('-q', '-r 5', self.pkgdir)
        self.assertCompiled(subinitfn)
        self.assertCompiled(hamfn)
        self.assertCompiled(spamfn)
        self.assertCompiled(eggfn)
Exemplo n.º 3
0
 def test_no_args_respects_quiet_flag(self):
     self._skip_if_sys_path_not_writable()
     script_helper.make_script(self.directory, 'baz', '')
     noisy = self.assertRunOK(PYTHONPATH=self.directory)
     self.assertIn(b'Listing ', noisy)
     quiet = self.assertRunOK('-q', PYTHONPATH=self.directory)
     self.assertNotIn(b'Listing ', quiet)
Exemplo n.º 4
0
 def test_no_args_respects_quiet_flag(self):
     self._skip_if_sys_path_not_writable()
     script_helper.make_script(self.directory, "baz", "")
     noisy = self.assertRunOK(PYTHONPATH=self.directory)
     self.assertIn(b"Listing ", noisy)
     quiet = self.assertRunOK("-q", PYTHONPATH=self.directory)
     self.assertNotIn(b"Listing ", quiet)
def _make_test_script(script_dir, script_basename,
                      source=test_source, omit_suffix=False):
    to_return = make_script(script_dir, script_basename,
                            source, omit_suffix)
    # Hack to check explicit relative imports
    if script_basename == "check_sibling":
        make_script(script_dir, "sibling", "")
    importlib.invalidate_caches()
    return to_return
Exemplo n.º 6
0
 def test_d_runtime_error(self):
     bazfn = script_helper.make_script(self.pkgdir, "baz", "raise Exception")
     self.assertRunOK("-q", "-d", "dinsdale", self.pkgdir)
     fn = script_helper.make_script(self.pkgdir, "bing", "import baz")
     pyc = importlib.util.cache_from_source(bazfn)
     os.rename(pyc, os.path.join(self.pkgdir, "baz.pyc"))
     os.remove(bazfn)
     rc, out, err = script_helper.assert_python_failure(fn, __isolated=False)
     self.assertRegex(err, b'File "dinsdale')
Exemplo n.º 7
0
 def setUp(self):
     self.directory = tempfile.mkdtemp()
     self.addCleanup(support.rmtree, self.directory)
     self.pkgdir = os.path.join(self.directory, 'foo')
     os.mkdir(self.pkgdir)
     self.pkgdir_cachedir = os.path.join(self.pkgdir, '__pycache__')
     # Create the __init__.py and a package module.
     self.initfn = script_helper.make_script(self.pkgdir, '__init__', '')
     self.barfn = script_helper.make_script(self.pkgdir, 'bar', '')
Exemplo n.º 8
0
 def test_d_runtime_error(self):
     bazfn = script_helper.make_script(self.pkgdir, 'baz', 'raise Exception')
     self.assertRunOK('-q', '-d', 'dinsdale', self.pkgdir)
     fn = script_helper.make_script(self.pkgdir, 'bing', 'import baz')
     pyc = importlib.util.cache_from_source(bazfn)
     os.rename(pyc, os.path.join(self.pkgdir, 'baz.pyc'))
     os.remove(bazfn)
     rc, out, err = script_helper.assert_python_failure(fn, __isolated=False)
     self.assertRegex(err, b'File "dinsdale')
Exemplo n.º 9
0
 def test_multiple_dirs(self):
     pkgdir2 = os.path.join(self.directory, "foo2")
     os.mkdir(pkgdir2)
     init2fn = script_helper.make_script(pkgdir2, "__init__", "")
     bar2fn = script_helper.make_script(pkgdir2, "bar2", "")
     self.assertRunOK("-q", self.pkgdir, pkgdir2)
     self.assertCompiled(self.initfn)
     self.assertCompiled(self.barfn)
     self.assertCompiled(init2fn)
     self.assertCompiled(bar2fn)
Exemplo n.º 10
0
 def test_multiple_dirs(self):
     pkgdir2 = os.path.join(self.directory, 'foo2')
     os.mkdir(pkgdir2)
     init2fn = script_helper.make_script(pkgdir2, '__init__', '')
     bar2fn = script_helper.make_script(pkgdir2, 'bar2', '')
     self.assertRunOK('-q', self.pkgdir, pkgdir2)
     self.assertCompiled(self.initfn)
     self.assertCompiled(self.barfn)
     self.assertCompiled(init2fn)
     self.assertCompiled(bar2fn)
Exemplo n.º 11
0
 def test_recursion_control(self):
     subpackage = os.path.join(self.pkgdir, "spam")
     os.mkdir(subpackage)
     subinitfn = script_helper.make_script(subpackage, "__init__", "")
     hamfn = script_helper.make_script(subpackage, "ham", "")
     self.assertRunOK("-q", "-l", self.pkgdir)
     self.assertNotCompiled(subinitfn)
     self.assertFalse(os.path.exists(os.path.join(subpackage, "__pycache__")))
     self.assertRunOK("-q", self.pkgdir)
     self.assertCompiled(subinitfn)
     self.assertCompiled(hamfn)
Exemplo n.º 12
0
 def test_recursion_control(self):
     subpackage = os.path.join(self.pkgdir, 'spam')
     os.mkdir(subpackage)
     subinitfn = script_helper.make_script(subpackage, '__init__', '')
     hamfn = script_helper.make_script(subpackage, 'ham', '')
     self.assertRunOK('-q', '-l', self.pkgdir)
     self.assertNotCompiled(subinitfn)
     self.assertFalse(os.path.exists(os.path.join(subpackage, '__pycache__')))
     self.assertRunOK('-q', self.pkgdir)
     self.assertCompiled(subinitfn)
     self.assertCompiled(hamfn)
Exemplo n.º 13
0
 def test_include_on_stdin(self):
     f1 = script_helper.make_script(self.pkgdir, 'f1', '')
     f2 = script_helper.make_script(self.pkgdir, 'f2', '')
     f3 = script_helper.make_script(self.pkgdir, 'f3', '')
     f4 = script_helper.make_script(self.pkgdir, 'f4', '')
     p = script_helper.spawn_python(*(self._get_run_args(()) + ['-i', '-']))
     p.stdin.write((f3+os.linesep).encode('ascii'))
     script_helper.kill_python(p)
     self.assertNotCompiled(f1)
     self.assertNotCompiled(f2)
     self.assertCompiled(f3)
     self.assertNotCompiled(f4)
Exemplo n.º 14
0
 def test_pyc_invalidation_mode(self):
     script_helper.make_script(self.pkgdir, 'f1', '')
     pyc = importlib.util.cache_from_source(
         os.path.join(self.pkgdir, 'f1.py'))
     self.assertRunOK('--invalidation-mode=checked-hash', self.pkgdir)
     with open(pyc, 'rb') as fp:
         data = fp.read()
     self.assertEqual(int.from_bytes(data[4:8], 'little'), 0b11)
     self.assertRunOK('--invalidation-mode=unchecked-hash', self.pkgdir)
     with open(pyc, 'rb') as fp:
         data = fp.read()
     self.assertEqual(int.from_bytes(data[4:8], 'little'), 0b01)
Exemplo n.º 15
0
 def test_include_on_stdin(self):
     f1 = script_helper.make_script(self.pkgdir, "f1", "")
     f2 = script_helper.make_script(self.pkgdir, "f2", "")
     f3 = script_helper.make_script(self.pkgdir, "f3", "")
     f4 = script_helper.make_script(self.pkgdir, "f4", "")
     p = script_helper.spawn_python(*(self._get_run_args(()) + ["-i", "-"]))
     p.stdin.write((f3 + os.linesep).encode("ascii"))
     script_helper.kill_python(p)
     self.assertNotCompiled(f1)
     self.assertNotCompiled(f2)
     self.assertCompiled(f3)
     self.assertNotCompiled(f4)
Exemplo n.º 16
0
 def test_include_file_no_arg(self):
     f1 = script_helper.make_script(self.pkgdir, "f1", "")
     f2 = script_helper.make_script(self.pkgdir, "f2", "")
     f3 = script_helper.make_script(self.pkgdir, "f3", "")
     f4 = script_helper.make_script(self.pkgdir, "f4", "")
     with open(os.path.join(self.directory, "l1"), "w") as l1:
         l1.write(os.path.join(self.pkgdir, "f2.py") + os.linesep)
     self.assertRunOK("-i", os.path.join(self.directory, "l1"))
     self.assertNotCompiled(f1)
     self.assertCompiled(f2)
     self.assertNotCompiled(f3)
     self.assertNotCompiled(f4)
Exemplo n.º 17
0
 def test_include_file_no_arg(self):
     f1 = script_helper.make_script(self.pkgdir, 'f1', '')
     f2 = script_helper.make_script(self.pkgdir, 'f2', '')
     f3 = script_helper.make_script(self.pkgdir, 'f3', '')
     f4 = script_helper.make_script(self.pkgdir, 'f4', '')
     with open(os.path.join(self.directory, 'l1'), 'w') as l1:
         l1.write(os.path.join(self.pkgdir, 'f2.py')+os.linesep)
     self.assertRunOK('-i', os.path.join(self.directory, 'l1'))
     self.assertNotCompiled(f1)
     self.assertCompiled(f2)
     self.assertNotCompiled(f3)
     self.assertNotCompiled(f4)
Exemplo n.º 18
0
    def test_workers(self):
        bar2fn = script_helper.make_script(self.directory, 'bar2', '')
        files = []
        for suffix in range(5):
            pkgdir = os.path.join(self.directory, 'foo{}'.format(suffix))
            os.mkdir(pkgdir)
            fn = script_helper.make_script(pkgdir, '__init__', '')
            files.append(script_helper.make_script(pkgdir, 'bar2', ''))

        self.assertRunOK(self.directory, '-j', '0')
        self.assertCompiled(bar2fn)
        for file in files:
            self.assertCompiled(file)
Exemplo n.º 19
0
    def test_workers(self):
        bar2fn = script_helper.make_script(self.directory, "bar2", "")
        files = []
        for suffix in range(5):
            pkgdir = os.path.join(self.directory, "foo{}".format(suffix))
            os.mkdir(pkgdir)
            fn = script_helper.make_script(pkgdir, "__init__", "")
            files.append(script_helper.make_script(pkgdir, "bar2", ""))

        self.assertRunOK(self.directory, "-j", "0")
        self.assertCompiled(bar2fn)
        for file in files:
            self.assertCompiled(file)
Exemplo n.º 20
0
 def test_unencodable_filename(self):
     # Issue #11619: The Python parser and the import machinery must not
     # encode filenames, especially on Windows
     pyname = script_helper.make_script("", TESTFN_UNENCODABLE, "pass")
     self.addCleanup(unlink, pyname)
     name = pyname[:-3]
     script_helper.assert_python_ok("-c", "mod = __import__(%a)" % name, __isolated=False)
Exemplo n.º 21
0
 def test_compiles_as_much_as_possible(self):
     bingfn = script_helper.make_script(self.pkgdir, "bing", "syntax(error")
     rc, out, err = self.assertRunNotOK("nosuchfile", self.initfn, bingfn, self.barfn)
     self.assertRegex(out, b"rror")
     self.assertNotCompiled(bingfn)
     self.assertCompiled(self.initfn)
     self.assertCompiled(self.barfn)
Exemplo n.º 22
0
    def test_doctest_main_issue4197(self):
        test_src = textwrap.dedent("""\
                    class Test:
                        ">>> 'line 2'"
                        pass

                    import doctest
                    doctest.testmod()
                    """)
        pattern = 'File "%s", line 2, in %s'
        with test.support.temp_dir() as d:
            script_name = make_script(d, 'script', test_src)
            rc, out, err = assert_python_ok(script_name)
            expected = pattern % (script_name, "__main__.Test")
            if verbose:
                print ("Expected line", expected)
                print ("Got stdout:")
                print (ascii(out))
            self.assertIn(expected.encode('utf-8'), out)
            zip_name, run_name = make_zip_script(d, "test_zip",
                                                script_name, '__main__.py')
            rc, out, err = assert_python_ok(zip_name)
            expected = pattern % (run_name, "__main__.Test")
            if verbose:
                print ("Expected line", expected)
                print ("Got stdout:")
                print (ascii(out))
            self.assertIn(expected.encode('utf-8'), out)
Exemplo n.º 23
0
 def test_no_args_compiles_path(self):
     # Note that -l is implied for the no args case.
     bazfn = script_helper.make_script(self.directory, 'baz', '')
     self.assertRunOK(PYTHONPATH=self.directory)
     self.assertCompiled(bazfn)
     self.assertNotCompiled(self.initfn)
     self.assertNotCompiled(self.barfn)
Exemplo n.º 24
0
 def test_no_args_compiles_path(self):
     # Note that -l is implied for the no args case.
     self._skip_if_sys_path_not_writable()
     bazfn = script_helper.make_script(self.directory, "baz", "")
     self.assertRunOK(PYTHONPATH=self.directory)
     self.assertCompiled(bazfn)
     self.assertNotCompiled(self.initfn)
     self.assertNotCompiled(self.barfn)
Exemplo n.º 25
0
 def test_compiles_as_much_as_possible(self):
     bingfn = script_helper.make_script(self.pkgdir, 'bing', 'syntax(error')
     rc, out, err = self.assertRunNotOK('nosuchfile', self.initfn,
                                        bingfn, self.barfn)
     self.assertRegex(out, b'rror')
     self.assertNotCompiled(bingfn)
     self.assertCompiled(self.initfn)
     self.assertCompiled(self.barfn)
Exemplo n.º 26
0
 def test_unencodable_filename(self):
     # Issue #11619: The Python parser and the import machinery must not
     # encode filenames, especially on Windows
     pyname = script_helper.make_script('', TESTFN_UNENCODABLE, 'pass')
     self.addCleanup(unlink, pyname)
     name = pyname[:-3]
     script_helper.assert_python_ok("-c",
                                    "mod = __import__(%a)" % name,
                                    __isolated=False)
Exemplo n.º 27
0
 def test_gc_ordinary_module_at_shutdown(self):
     # Same as above, but with a non-__main__ module.
     with temp_dir() as script_dir:
         module = """if 1:
             class C:
                 def __del__(self):
                     print('__del__ called')
             l = [C()]
             l.append(l)
             """
         code = """if 1:
             import sys
             sys.path.insert(0, %r)
             import gctest
             """ % (script_dir, )
         make_script(script_dir, 'gctest', module)
         rc, out, err = assert_python_ok('-c', code)
         self.assertEqual(out.strip(), b'__del__ called')
def _make_launch_script(script_dir, script_basename, module_name, path=None):
    if path is None:
        path = "os.path.dirname(__file__)"
    else:
        path = repr(path)
    source = launch_source % (path, module_name)
    to_return = make_script(script_dir, script_basename, source)
    importlib.invalidate_caches()
    return to_return
Exemplo n.º 29
0
def _make_launch_script(script_dir, script_basename, module_name, path=None):
    if path is None:
        path = "os.path.dirname(__file__)"
    else:
        path = repr(path)
    source = launch_source % (path, module_name)
    to_return = make_script(script_dir, script_basename, source)
    importlib.invalidate_caches()
    return to_return
Exemplo n.º 30
0
 def test_gc_ordinary_module_at_shutdown(self):
     # Same as above, but with a non-__main__ module.
     with temp_dir() as script_dir:
         module = """if 1:
             class C:
                 def __del__(self):
                     print('__del__ called')
             l = [C()]
             l.append(l)
             """
         code = """if 1:
             import sys
             sys.path.insert(0, %r)
             import gctest
             """ % (script_dir,)
         make_script(script_dir, 'gctest', module)
         rc, out, err = assert_python_ok('-c', code)
         self.assertEqual(out.strip(), b'__del__ called')
Exemplo n.º 31
0
 def test_import_in_del_does_not_crash(self):
     # Issue 4236
     testfn = script_helper.make_script('', TESTFN, textwrap.dedent("""\
         import sys
         class C:
            def __del__(self):
               import importlib
         sys.argv.insert(0, C())
         """))
     script_helper.assert_python_ok(testfn)
Exemplo n.º 32
0
 def test_import_in_del_does_not_crash(self):
     # Issue 4236
     testfn = script_helper.make_script('', TESTFN, textwrap.dedent("""\
         import sys
         class C:
            def __del__(self):
               import importlib
         sys.argv.insert(0, C())
         """))
     script_helper.assert_python_ok(testfn)
 def test_prepend_only(self):
     fullpath = ["test", "build", "real", "path"]
     path = os.path.join(self.directory, *fullpath)
     os.makedirs(path)
     script = script_helper.make_script(path, "test", "1 / 0")
     bc = importlib.util.cache_from_source(script)
     prependdir = "/foo"
     compileall.compile_dir(path, quiet=True, prependdir=prependdir)
     rc, out, err = script_helper.assert_python_failure(bc)
     expected_in = os.path.join(prependdir, self.directory, *fullpath)
     self.assertIn(expected_in, str(err, encoding=sys.getdefaultencoding()))
Exemplo n.º 34
0
 def test_default_recursion_limit(self):
     many_directories = [str(number) for number in range(1, 100)]
     self.long_path = os.path.join(self.directory, "long",
                                   *many_directories)
     os.makedirs(self.long_path)
     self.source_path_long = script_helper.make_script(
         self.long_path, "deepscript", "")
     self.bc_path_long = importlib.util.cache_from_source(
         self.source_path_long)
     self.assertFalse(os.path.isfile(self.bc_path_long))
     self.assertRunOK('-q', os.path.join(self.directory, "long"))
     self.assertTrue(os.path.isfile(self.bc_path_long))
Exemplo n.º 35
0
 def test_no_args_respects_force_flag(self):
     self._skip_if_sys_path_not_writable()
     bazfn = script_helper.make_script(self.directory, 'baz', '')
     self.assertRunOK(PYTHONPATH=self.directory)
     pycpath = importlib.util.cache_from_source(bazfn)
     os.utime(pycpath, (time.time() - 60, ) * 2)
     mtime = os.stat(pycpath).st_mtime
     self.assertRunOK(PYTHONPATH=self.directory)
     mtime2 = os.stat(pycpath).st_mtime
     self.assertEqual(mtime, mtime2)
     self.assertRunOK('-f', PYTHONPATH=self.directory)
     mtime2 = os.stat(pycpath).st_mtime
     self.assertNotEqual(mtime, mtime2)
 def test_strip_and_prepend(self):
     fullpath = ["test", "build", "real", "path"]
     path = os.path.join(self.directory, *fullpath)
     os.makedirs(path)
     script = script_helper.make_script(path, "test", "1 / 0")
     bc = importlib.util.cache_from_source(script)
     stripdir = os.path.join(self.directory, *fullpath[:2])
     prependdir = "/foo"
     self.assertRunOK("-s", stripdir, "-p", prependdir, path)
     rc, out, err = script_helper.assert_python_failure(bc)
     expected_in = os.path.join(prependdir, *fullpath[2:])
     self.assertIn(expected_in, str(err, encoding=sys.getdefaultencoding()))
     self.assertNotIn(stripdir, str(err, encoding=sys.getdefaultencoding()))
 def test_inspect_getsource_issue4223(self):
     test_src = 'def foo(): pass\n'
     with test.support.temp_dir() as d:
         init_name = make_script(d, '__init__', test_src)
         name_in_zip = os.path.join('zip_pkg', os.path.basename(init_name))
         zip_name, run_name = make_zip_script(d, 'test_zip', init_name,
                                              name_in_zip)
         os.remove(init_name)
         sys.path.insert(0, zip_name)
         import zip_pkg
         try:
             self.assertEqual(inspect.getsource(zip_pkg.foo), test_src)
         finally:
             del sys.modules['zip_pkg']
Exemplo n.º 38
0
def _ready_to_import(name=None, source=''):
    name = name or 'spam'
    with temp_dir() as tempdir:
        path = script_helper.make_script(tempdir, name, source)
        old_module = sys.modules.pop(name, None)
        try:
            sys.path.insert(0, tempdir)
            yield name, path
            sys.path.remove(tempdir)
        finally:
            if old_module is not None:
                sys.modules[name] = old_module
            elif name in sys.modules:
                del sys.modules[name]
Exemplo n.º 39
0
    def test_ignore_symlink_destination(self):
        # Create folders for allowed files, symlinks and prohibited area
        allowed_path = os.path.join(self.directory, "test", "dir", "allowed")
        symlinks_path = os.path.join(self.directory, "test", "dir", "symlinks")
        prohibited_path = os.path.join(self.directory, "test", "dir", "prohibited")
        os.makedirs(allowed_path)
        os.makedirs(symlinks_path)
        os.makedirs(prohibited_path)

        # Create scripts and symlinks and remember their byte-compiled versions
        allowed_script = script_helper.make_script(allowed_path, "test_allowed", "a = 0")
        prohibited_script = script_helper.make_script(prohibited_path, "test_prohibited", "a = 0")
        allowed_symlink = os.path.join(symlinks_path, "test_allowed.py")
        prohibited_symlink = os.path.join(symlinks_path, "test_prohibited.py")
        os.symlink(allowed_script, allowed_symlink)
        os.symlink(prohibited_script, prohibited_symlink)
        allowed_bc = importlib.util.cache_from_source(allowed_symlink)
        prohibited_bc = importlib.util.cache_from_source(prohibited_symlink)

        self.assertRunOK(symlinks_path, "-e", allowed_path)

        self.assertTrue(os.path.isfile(allowed_bc))
        self.assertFalse(os.path.isfile(prohibited_bc))
Exemplo n.º 40
0
 def test_inspect_getsource_issue4223(self):
     test_src = "def foo(): pass\n"
     with test.support.temp_dir() as d:
         init_name = make_script(d, '__init__', test_src)
         name_in_zip = os.path.join('zip_pkg',
                                    os.path.basename(init_name))
         zip_name, run_name = make_zip_script(d, 'test_zip',
                                             init_name, name_in_zip)
         os.remove(init_name)
         sys.path.insert(0, zip_name)
         import zip_pkg
         try:
             self.assertEqual(inspect.getsource(zip_pkg.foo), test_src)
         finally:
             del sys.modules["zip_pkg"]
Exemplo n.º 41
0
 def _test_ddir_only(self, *, ddir, parallel=True):
     """Recursive compile_dir ddir must contain package paths; bpo39769."""
     fullpath = ["test", "foo"]
     path = self.directory
     mods = []
     for subdir in fullpath:
         path = os.path.join(path, subdir)
         os.mkdir(path)
         script_helper.make_script(path, "__init__", "")
         mods.append(script_helper.make_script(path, "mod",
                                               "def fn(): 1/0\nfn()\n"))
     compileall.compile_dir(
             self.directory, quiet=True, ddir=ddir,
             workers=2 if parallel else 1)
     self.assertTrue(mods)
     for mod in mods:
         self.assertTrue(mod.startswith(self.directory), mod)
         modcode = importlib.util.cache_from_source(mod)
         modpath = mod[len(self.directory+os.sep):]
         _, _, err = script_helper.assert_python_failure(modcode)
         expected_in = os.path.join(ddir, modpath)
         mod_code_obj = test.test_importlib.util.get_code_from_pyc(modcode)
         self.assertEqual(mod_code_obj.co_filename, expected_in)
         self.assertIn(f'"{expected_in}"', os.fsdecode(err))
Exemplo n.º 42
0
 def test_no_args_respects_force_flag(self):
     bazfn = script_helper.make_script(self.directory, 'baz', '')
     self.assertRunOK(PYTHONPATH=self.directory)
     pycpath = importlib.util.cache_from_source(bazfn)
     # Set atime/mtime backward to avoid file timestamp resolution issues
     os.utime(pycpath, (time.time()-60,)*2)
     mtime = os.stat(pycpath).st_mtime
     # Without force, no recompilation
     self.assertRunOK(PYTHONPATH=self.directory)
     mtime2 = os.stat(pycpath).st_mtime
     self.assertEqual(mtime, mtime2)
     # Now force it.
     self.assertRunOK('-f', PYTHONPATH=self.directory)
     mtime2 = os.stat(pycpath).st_mtime
     self.assertNotEqual(mtime, mtime2)
Exemplo n.º 43
0
 def test_multiple_optimization_levels(self):
     script = script_helper.make_script(self.directory,
                                        "test_optimization",
                                        "a = 0")
     bc = []
     for opt_level in "", 1, 2, 3:
         bc.append(importlib.util.cache_from_source(script,
                                                    optimization=opt_level))
     test_combinations = [[0, 1], [1, 2], [0, 2], [0, 1, 2]]
     for opt_combination in test_combinations:
         compileall.compile_file(script, quiet=True,
                                 optimize=opt_combination)
         for opt_level in opt_combination:
             self.assertTrue(os.path.isfile(bc[opt_level]))
             try:
                 os.unlink(bc[opt_level])
             except Exception:
                 pass
Exemplo n.º 44
0
def _ready_to_import(name=None, source=""):
    # sets up a temporary directory and removes it
    # creates the module file
    # temporarily clears the module from sys.modules (if any)
    # reverts or removes the module when cleaning up
    name = name or "spam"
    with temp_dir() as tempdir:
        path = script_helper.make_script(tempdir, name, source)
        old_module = sys.modules.pop(name, None)
        try:
            sys.path.insert(0, tempdir)
            yield name, path
            sys.path.remove(tempdir)
        finally:
            if old_module is not None:
                sys.modules[name] = old_module
            elif name in sys.modules:
                del sys.modules[name]
 def test_multiple_optimization_levels(self):
     path = os.path.join(self.directory, "optimizations")
     os.makedirs(path)
     script = script_helper.make_script(path, "test_optimization", "a = 0")
     bc = []
     for opt_level in "", 1, 2, 3:
         bc.append(
             importlib.util.cache_from_source(script,
                                              optimization=opt_level))
     test_combinations = [["0", "1"], ["1", "2"], ["0", "2"],
                          ["0", "1", "2"]]
     for opt_combination in test_combinations:
         self.assertRunOK(path, *("-o" + str(n) for n in opt_combination))
         for opt_level in opt_combination:
             self.assertTrue(os.path.isfile(bc[int(opt_level)]))
             try:
                 os.unlink(bc[opt_level])
             except Exception:
                 pass
    def test_pdb_issue4201(self):
        test_src = textwrap.dedent("""                    def f():
                        pass

                    import pdb
                    pdb.Pdb(nosigint=True).runcall(f)
                    """)
        with test.support.temp_dir() as d:
            script_name = make_script(d, 'script', test_src)
            p = spawn_python(script_name)
            p.stdin.write(b'l\n')
            data = kill_python(p)
            self.assertIn(os.path.normcase(script_name.encode('utf-8')), data)
            zip_name, run_name = make_zip_script(d, 'test_zip', script_name,
                                                 '__main__.py')
            p = spawn_python(zip_name)
            p.stdin.write(b'l\n')
            data = kill_python(p)
            self.assertIn(os.path.normcase(run_name.encode('utf-8')), data)
    def test_hardlink(self):
        # 'a = 0' code produces the same bytecode for the 3 optimization
        # levels. All three .pyc files must have the same inode (hardlinks).
        #
        # If deduplication is disabled, all pyc files must have different
        # inodes.
        for dedup in (True, False):
            with tempfile.TemporaryDirectory() as path:
                with self.subTest(dedup=dedup):
                    script = script_helper.make_script(path, "script", "a = 0")
                    pycs = get_pycs(script)

                    args = ["-q", "-o 0", "-o 1", "-o 2"]
                    if dedup:
                        args.append("--hardlink-dupes")
                    self.assertRunOK(path, *args)

                    self.assertEqual(is_hardlink(pycs[0], pycs[1]), dedup)
                    self.assertEqual(is_hardlink(pycs[1], pycs[2]), dedup)
                    self.assertEqual(is_hardlink(pycs[0], pycs[2]), dedup)
Exemplo n.º 48
0
    def test_still_running_at_exit(self):
        script = dedent(f"""
        from textwrap import dedent
        import threading
        import _xxsubinterpreters as _interpreters
        def f():
            _interpreters.run_string(id, dedent('''
                import time
                # Give plenty of time for the main interpreter to finish.
                time.sleep(1_000_000)
                '''))

        t = threading.Thread(target=f)
        t.start()
        """)
        with support.temp_dir() as dirname:
            filename = script_helper.make_script(dirname, 'interp', script)
            with script_helper.spawn_python(filename) as proc:
                retcode = proc.wait()

        self.assertEqual(retcode, 0)
Exemplo n.º 49
0
    def test_pdb_issue4201(self):
        test_src = textwrap.dedent("""\
                    def f():
                        pass

                    import pdb
                    pdb.Pdb(nosigint=True).runcall(f)
                    """)
        with os_helper.temp_dir() as d:
            script_name = make_script(d, 'script', test_src)
            p = spawn_python(script_name)
            p.stdin.write(b'l\n')
            data = kill_python(p)
            # bdb/pdb applies normcase to its filename before displaying
            self.assertIn(os.path.normcase(script_name.encode('utf-8')), data)
            zip_name, run_name = make_zip_script(d, "test_zip", script_name,
                                                 '__main__.py')
            p = spawn_python(zip_name)
            p.stdin.write(b'l\n')
            data = kill_python(p)
            # bdb/pdb applies normcase to its filename before displaying
            self.assertIn(os.path.normcase(run_name.encode('utf-8')), data)
Exemplo n.º 50
0
 def _make_test_script(self, script_dir, script_basename,
                       source=None, omit_suffix=False):
     if source is None:
         source = example_source
     return make_script(script_dir, script_basename,
                        source, omit_suffix)
Exemplo n.º 51
0
def _make_test_script(script_dir, script_basename, source=test_source):
    to_return = make_script(script_dir, script_basename, source)
    importlib.invalidate_caches()
    return to_return
 def test_silent(self):
     script_helper.make_script(self.pkgdir, 'crunchyfrog', 'bad(syntax')
     _, quiet, _ = self.assertRunNotOK('-q', self.pkgdir)
     _, silent, _ = self.assertRunNotOK('-qq', self.pkgdir)
     self.assertNotEqual(b'', quiet)
     self.assertEqual(b'', silent)
Exemplo n.º 53
0
 def test_no_args_respects_quiet_flag(self):
     script_helper.make_script(self.directory, 'baz', '')
     noisy = self.assertRunOK(PYTHONPATH=self.directory)
     self.assertIn(b'Listing ', noisy)
     quiet = self.assertRunOK('-q', PYTHONPATH=self.directory)
     self.assertNotIn(b'Listing ', quiet)
 def test_d_compile_error(self):
     script_helper.make_script(self.pkgdir, 'crunchyfrog', 'bad(syntax')
     rc, out, err = self.assertRunNotOK('-q', '-d', 'dinsdale', self.pkgdir)
     self.assertRegex(out, b'File "dinsdale')
 def make_script(self, code, name="script"):
     return script_helper.make_script(self.path, name, code)
 def test_doctest_issue4197(self):
     test_src = inspect.getsource(test_doctest)
     test_src = test_src.replace(
         'from test import test_doctest',
         'import test_zipped_doctest as test_doctest')
     test_src = test_src.replace('test.test_doctest', 'test_zipped_doctest')
     test_src = test_src.replace('test.sample_doctest',
                                 'sample_zipped_doctest')
     sample_sources = {}
     for mod in [
             sample_doctest, sample_doctest_no_doctests,
             sample_doctest_no_docstrings
     ]:
         src = inspect.getsource(mod)
         src = src.replace('test.test_doctest', 'test_zipped_doctest')
         mod_name = mod.__name__.split('.')[-1]
         mod_name = mod_name.replace('sample_', 'sample_zipped_')
         sample_sources[mod_name] = src
     with test.support.temp_dir() as d:
         script_name = make_script(d, 'test_zipped_doctest', test_src)
         zip_name, run_name = make_zip_script(d, 'test_zip', script_name)
         z = zipfile.ZipFile(zip_name, 'a')
         for mod_name, src in sample_sources.items():
             z.writestr(mod_name + '.py', src)
         z.close()
         if verbose:
             zip_file = zipfile.ZipFile(zip_name, 'r')
             print('Contents of %r:' % zip_name)
             zip_file.printdir()
             zip_file.close()
         os.remove(script_name)
         sys.path.insert(0, zip_name)
         import test_zipped_doctest
         try:
             known_good_tests = [
                 test_zipped_doctest.SampleClass,
                 test_zipped_doctest.SampleClass.NestedClass,
                 test_zipped_doctest.SampleClass.NestedClass.__init__,
                 test_zipped_doctest.SampleClass.__init__,
                 test_zipped_doctest.SampleClass.a_classmethod,
                 test_zipped_doctest.SampleClass.a_property,
                 test_zipped_doctest.SampleClass.a_staticmethod,
                 test_zipped_doctest.SampleClass.double,
                 test_zipped_doctest.SampleClass.get,
                 test_zipped_doctest.SampleNewStyleClass,
                 test_zipped_doctest.SampleNewStyleClass.__init__,
                 test_zipped_doctest.SampleNewStyleClass.double,
                 test_zipped_doctest.SampleNewStyleClass.get,
                 test_zipped_doctest.sample_func,
                 test_zipped_doctest.test_DocTest,
                 test_zipped_doctest.test_DocTestParser,
                 test_zipped_doctest.test_DocTestRunner.basics,
                 test_zipped_doctest.test_DocTestRunner.exceptions,
                 test_zipped_doctest.test_DocTestRunner.option_directives,
                 test_zipped_doctest.test_DocTestRunner.optionflags,
                 test_zipped_doctest.test_DocTestRunner.verbose_flag,
                 test_zipped_doctest.test_Example,
                 test_zipped_doctest.test_debug,
                 test_zipped_doctest.test_testsource,
                 test_zipped_doctest.test_trailing_space_in_test,
                 test_zipped_doctest.test_DocTestSuite,
                 test_zipped_doctest.test_DocTestFinder
             ]
             fail_due_to_missing_data_files = [
                 test_zipped_doctest.test_DocFileSuite,
                 test_zipped_doctest.test_testfile,
                 test_zipped_doctest.test_unittest_reportflags
             ]
             for obj in known_good_tests:
                 _run_object_doctest(obj, test_zipped_doctest)
         finally:
             del sys.modules['test_zipped_doctest']
    def test_doctest_issue4197(self):
        # To avoid having to keep two copies of the doctest module's
        # unit tests in sync, this test works by taking the source of
        # test_doctest itself, rewriting it a bit to cope with a new
        # location, and then throwing it in a zip file to make sure
        # everything still works correctly
        test_src = inspect.getsource(test_doctest)
        test_src = test_src.replace(
                         "from test import test_doctest",
                         "import test_zipped_doctest as test_doctest")
        test_src = test_src.replace("test.test_doctest",
                                    "test_zipped_doctest")
        test_src = test_src.replace("test.sample_doctest",
                                    "sample_zipped_doctest")
        # The sample doctest files rewritten to include in the zipped version.
        sample_sources = {}
        for mod in [sample_doctest, sample_doctest_no_doctests,
                    sample_doctest_no_docstrings]:
            src = inspect.getsource(mod)
            src = src.replace("test.test_doctest", "test_zipped_doctest")
            # Rewrite the module name so that, for example,
            # "test.sample_doctest" becomes "sample_zipped_doctest".
            mod_name = mod.__name__.split(".")[-1]
            mod_name = mod_name.replace("sample_", "sample_zipped_")
            sample_sources[mod_name] = src

        with test.support.temp_dir() as d:
            script_name = make_script(d, 'test_zipped_doctest',
                                            test_src)
            zip_name, run_name = make_zip_script(d, 'test_zip',
                                                script_name)
            z = zipfile.ZipFile(zip_name, 'a')
            for mod_name, src in sample_sources.items():
                z.writestr(mod_name + ".py", src)
            z.close()
            if verbose:
                zip_file = zipfile.ZipFile(zip_name, 'r')
                print ('Contents of %r:' % zip_name)
                zip_file.printdir()
                zip_file.close()
            os.remove(script_name)
            sys.path.insert(0, zip_name)
            import test_zipped_doctest
            try:
                # Some of the doc tests depend on the colocated text files
                # which aren't available to the zipped version (the doctest
                # module currently requires real filenames for non-embedded
                # tests). So we're forced to be selective about which tests
                # to run.
                # doctest could really use some APIs which take a text
                # string or a file object instead of a filename...
                known_good_tests = [
                    test_zipped_doctest.SampleClass,
                    test_zipped_doctest.SampleClass.NestedClass,
                    test_zipped_doctest.SampleClass.NestedClass.__init__,
                    test_zipped_doctest.SampleClass.__init__,
                    test_zipped_doctest.SampleClass.a_classmethod,
                    test_zipped_doctest.SampleClass.a_property,
                    test_zipped_doctest.SampleClass.a_staticmethod,
                    test_zipped_doctest.SampleClass.double,
                    test_zipped_doctest.SampleClass.get,
                    test_zipped_doctest.SampleNewStyleClass,
                    test_zipped_doctest.SampleNewStyleClass.__init__,
                    test_zipped_doctest.SampleNewStyleClass.double,
                    test_zipped_doctest.SampleNewStyleClass.get,
                    test_zipped_doctest.sample_func,
                    test_zipped_doctest.test_DocTest,
                    test_zipped_doctest.test_DocTestParser,
                    test_zipped_doctest.test_DocTestRunner.basics,
                    test_zipped_doctest.test_DocTestRunner.exceptions,
                    test_zipped_doctest.test_DocTestRunner.option_directives,
                    test_zipped_doctest.test_DocTestRunner.optionflags,
                    test_zipped_doctest.test_DocTestRunner.verbose_flag,
                    test_zipped_doctest.test_Example,
                    test_zipped_doctest.test_debug,
                    test_zipped_doctest.test_testsource,
                    test_zipped_doctest.test_trailing_space_in_test,
                    test_zipped_doctest.test_DocTestSuite,
                    test_zipped_doctest.test_DocTestFinder,
                ]
                # These tests are the ones which need access
                # to the data files, so we don't run them
                fail_due_to_missing_data_files = [
                    test_zipped_doctest.test_DocFileSuite,
                    test_zipped_doctest.test_testfile,
                    test_zipped_doctest.test_unittest_reportflags,
                ]

                for obj in known_good_tests:
                    _run_object_doctest(obj, test_zipped_doctest)
            finally:
                del sys.modules["test_zipped_doctest"]
Exemplo n.º 58
0
 def test_unencodable_filename(self):
     pyname = script_helper.make_script('', TESTFN_UNENCODABLE, 'pass')
     self.addCleanup(unlink, pyname)
     name = pyname[:-3]
     script_helper.assert_python_ok('-c', 'mod = __import__(%a)' % name,
         __isolated=False)