Ejemplo n.º 1
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)
Ejemplo 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)
Ejemplo n.º 3
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')
Ejemplo n.º 4
0
 def setUp(self):
     self.addCleanup(self._cleanup)
     self.directory = tempfile.mkdtemp()
     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', '')
Ejemplo n.º 5
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', '')
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
Ejemplo n.º 7
0
 def setUp(self):
     self.addCleanup(self._cleanup)
     self.directory = tempfile.mkdtemp()
     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", "")
Ejemplo 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 = imp.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)
     self.assertRegex(err, b'File "dinsdale')
Ejemplo n.º 9
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')
Ejemplo n.º 10
0
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
Ejemplo n.º 11
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)
Ejemplo n.º 12
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)
 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)
Ejemplo n.º 14
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)
Ejemplo n.º 15
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)
Ejemplo n.º 16
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)
Ejemplo n.º 17
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)
Ejemplo n.º 18
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)
 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)
 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('-m', 'compileall', '-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)
Ejemplo n.º 21
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)
Ejemplo n.º 22
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)
Ejemplo n.º 23
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)
Ejemplo n.º 24
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 temp_dir() as d:
            script_name = make_script(d, 'script', test_src)
            exit_code, data = run_python(script_name)
            expected = pattern % (script_name, "__main__.Test")
            if verbose:
                print "Expected line", expected
                print "Got stdout:"
                print data
            self.assertIn(expected, data)
            zip_name, run_name = make_zip_script(d, "test_zip",
                                                script_name, '__main__.py')
            exit_code, data = run_python(zip_name)
            expected = pattern % (run_name, "__main__.Test")
            if verbose:
                print "Expected line", expected
                print "Got stdout:"
                print data
            self.assertIn(expected, data)
Ejemplo 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)
Ejemplo 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')
     name = pyname[:-3]
     script_helper.assert_python_ok("-c", "mod = __import__(%a)" % name,
                                    __isolated=False)
 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)
    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 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)
Ejemplo n.º 29
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)
Ejemplo n.º 30
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)
    return make_script(script_dir, script_basename, source)
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)
    return make_script(script_dir, script_basename, source)
Ejemplo n.º 32
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 temp_dir() as d:
            script_name = make_script(d, 'script', test_src)
            exit_code, data = run_python(script_name)
            expected = pattern % (script_name, "__main__.Test")
            if verbose:
                print "Expected line", expected
                print "Got stdout:"
                print data
            self.assertIn(expected, data)
            zip_name, run_name = make_zip_script(d, "test_zip",
                                                script_name, '__main__.py')
            exit_code, data = run_python(zip_name)
            expected = pattern % (run_name, "__main__.Test")
            if verbose:
                print "Expected line", expected
                print "Got stdout:"
                print data
            self.assertIn(expected, data)
Ejemplo n.º 33
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)
 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)
Ejemplo n.º 35
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)
Ejemplo n.º 36
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
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
Ejemplo n.º 38
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:
             import weakref
             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')
Ejemplo n.º 39
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)
Ejemplo n.º 40
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 imp
         sys.argv.insert(0, C())
         """))
     script_helper.assert_python_ok(testfn)
Ejemplo n.º 41
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:
             import weakref
             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')
Ejemplo n.º 42
0
 def test_inspect_getsource_issue4223(self):
     test_src = "def foo(): pass\n"
     with 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
         self.assertEqual(inspect.getsource(zip_pkg.foo), test_src)
 def test_inspect_getsource_issue4223(self):
     test_src = "def foo(): pass\n"
     with 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
         self.assertEqual(inspect.getsource(zip_pkg.foo), test_src)
Ejemplo n.º 44
0
    def test_inspect_getsource_issue4223(self):
        test_src = "def foo(): pass\n"
        with 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"]
Ejemplo n.º 45
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)
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)
    name = name or "spam"
    with script_helper.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
Ejemplo n.º 47
0
    def test_cached_mode_issue_2051(self):
        mode = 0o600
        source = TESTFN + ".py"
        with script_helper.temp_dir() as tempdir:
            path = script_helper.make_script(tempdir, TESTFN,
                                             "key='top secret'")
            os.chmod(path, mode)
            compiled = imp.cache_from_source(path)
            sys.path.insert(0, tempdir)
            try:
                __import__(TESTFN)
            finally:
                sys.path.remove(tempdir)

            if not os.path.exists(compiled):
                self.fail("__import__ did not result in creation of "
                          "either a .pyc or .pyo file")
            stat_info = os.stat(compiled)

        self.assertEqual(oct(stat.S_IMODE(stat_info.st_mode)), oct(mode))
    def test_pdb_issue4201(self):
        test_src = textwrap.dedent("""\
                    def f():
                        pass

                    import pdb
                    pdb.Pdb(nosigint=True).runcall(f)
                    """)
        with 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(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(run_name.encode('utf-8'), data)
Ejemplo n.º 49
0
    def test_pdb_issue4201(self):
        test_src = textwrap.dedent("""\
                    def f():
                        pass

                    import pdb
                    pdb.Pdb(nosigint=True).runcall(f)
                    """)
        with 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)
    def test_pdb_issue4201(self):
        test_src = textwrap.dedent("""\
                    def f():
                        pass

                    import pdb
                    pdb.runcall(f)
                    """)

        with temp_dir() as d:
            script_name = make_script(d, 'script', test_src)
            p = spawn_python(script_name)
            p.stdin.write('l\n')
            data = kill_python(p)
            # Back-port from CPython 3 (see CPython Issue 14255).
            self.assertNormalisedIn(script_name, data)

            zip_name, run_name = make_zip_script(d, "test_zip",
                                                script_name, '__main__.py')
            p = spawn_python(zip_name)
            p.stdin.write('l\n')
            data = kill_python(p)
            # Back-port from CPython 3 (see CPython Issue 14255).
            self.assertNormalisedIn(run_name, data)
Ejemplo n.º 51
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)
Ejemplo n.º 52
0
 def _make_test_script(self, script_dir, script_basename, source=None):
     if source is None:
         source = example_source
     return make_script(script_dir, script_basename, source)
 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_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_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 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_pdb_set_trace,
                    test_zipped_doctest.test_pdb_set_trace_nested,
                    test_zipped_doctest.test_testsource,
                    test_zipped_doctest.test_trailing_space_in_test,
                    test_zipped_doctest.test_DocTestSuite,
                    test_zipped_doctest.test_DocTestFinder,
                ]
                # These remaining 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"]