Example #1
0
 def test_syshook_no_logdir_text_format(self):
     # Issue 12890: we were emitting the <p> tag in text mode.
     with temp_dir() as tracedir:
         rc, out, err = assert_python_failure(
             '-c',
             ('import cgitb; cgitb.enable(format="text", logdir=%s); '
              'raise ValueError("Hello World")') % repr(tracedir),
             PYTHONIOENCODING='utf-8')
     out = out.decode()
     self.assertIn("ValueError", out)
     self.assertIn("Hello World", out)
     self.assertNotIn('<p>', out)
     self.assertNotIn('</p>', out)
Example #2
0
 def test_issue8202_dash_m_file_ignored(self):
     # Make sure a "-m" file in the current directory
     # does not alter the value of sys.path[0]
     with os_helper.temp_dir() as script_dir:
         script_name = _make_test_script(script_dir, 'other')
         with os_helper.change_cwd(path=script_dir):
             with open("-m", "w", encoding="utf-8") as f:
                 f.write("data")
                 rc, out, err = assert_python_ok('-m', 'other', *example_args,
                                                 __isolated=False)
                 self._check_output(script_name, rc, out,
                                   script_name, script_name, script_dir, '',
                                   importlib.machinery.SourceFileLoader)
Example #3
0
 def test_dash_m_bad_pyc(self):
     with os_helper.temp_dir() as script_dir, \
             os_helper.change_cwd(path=script_dir):
         os.mkdir('test_pkg')
         # Create invalid *.pyc as empty file
         with open('test_pkg/__init__.pyc', 'wb'):
             pass
         err = self.check_dash_m_failure('test_pkg')
         self.assertRegex(
             err, br'Error while finding module specification.*'
             br'ImportError.*bad magic number')
         self.assertNotIn(b'is a package', err)
         self.assertNotIn(b'Traceback', err)
Example #4
0
 def test_directory_compiled(self):
     with temp_dir() as script_dir:
         mod_name = '__main__'
         script_name = self._make_test_script(script_dir, mod_name)
         compiled_name = py_compile.compile(script_name, doraise=True)
         os.remove(script_name)
         if not sys.dont_write_bytecode:
             legacy_pyc = make_legacy_pyc(script_name)
             self._check_script(script_dir,
                                "<run_path>",
                                legacy_pyc,
                                script_dir,
                                mod_name=mod_name)
Example #5
0
 def test_zipfile_compiled(self):
     with temp_dir() as script_dir:
         mod_name = '__main__'
         script_name = self._make_test_script(script_dir, mod_name)
         compiled_name = py_compile.compile(script_name, doraise=True)
         zip_name, fname = make_zip_script(script_dir, 'test_zip',
                                           compiled_name)
         self._check_script(zip_name,
                            "<run_path>",
                            fname,
                            zip_name,
                            mod_name=mod_name,
                            check_loader=False)
Example #6
0
 def test_readonly_files(self):
     with os_helper.temp_dir() as dir:
         fname = os.path.join(dir, 'db')
         with dumbdbm.open(fname, 'n') as f:
             self.assertEqual(list(f.keys()), [])
             for key in self._dict:
                 f[key] = self._dict[key]
         os.chmod(fname + ".dir", stat.S_IRUSR)
         os.chmod(fname + ".dat", stat.S_IRUSR)
         os.chmod(dir, stat.S_IRUSR | stat.S_IXUSR)
         with dumbdbm.open(fname, 'r') as f:
             self.assertEqual(sorted(f.keys()), sorted(self._dict))
             f.close()  # don't write
Example #7
0
 def test_syntaxerror_multi_line_fstring(self):
     script = 'foo = f"""{}\nfoo"""\n'
     with os_helper.temp_dir() as script_dir:
         script_name = _make_test_script(script_dir, 'script', script)
         exitcode, stdout, stderr = assert_python_failure(script_name)
         self.assertEqual(
             stderr.splitlines()[-3:],
             [
                 b'    foo"""',
                 b'          ^',
                 b'SyntaxError: f-string: empty expression not allowed',
             ],
         )
Example #8
0
    def test_output_file_when_changing_directory(self):
        with temp_dir() as tmpdir, change_cwd(tmpdir):
            os.mkdir('dest')
            with open('demo.py', 'w') as f:
                f.write('import os; os.chdir("dest")')

            assert_python_ok(
                '-m', self.profilermodule.__name__,
                '-o', 'out.pstats',
                'demo.py',
            )

            self.assertTrue(os.path.exists('out.pstats'))
Example #9
0
    def test_error_code_on_exception(self):
        err_msg = "unable to open database file"
        if sys.platform.startswith("win"):
            err_code = sqlite.SQLITE_CANTOPEN_ISDIR
        else:
            err_code = sqlite.SQLITE_CANTOPEN

        with temp_dir() as db:
            with self.assertRaisesRegex(sqlite.Error, err_msg) as cm:
                sqlite.connect(db)
            e = cm.exception
            self.assertEqual(e.sqlite_errorcode, err_code)
            self.assertTrue(e.sqlite_errorname.startswith("SQLITE_CANTOPEN"))
Example #10
0
 def test_syshook_no_logdir_default_format(self):
     with temp_dir() as tracedir:
         rc, out, err = assert_python_failure(
             '-c',
             ('import cgitb; cgitb.enable(logdir=%s); '
              'raise ValueError("Hello World")') % repr(tracedir),
             PYTHONIOENCODING='utf-8')
     out = out.decode()
     self.assertIn("ValueError", out)
     self.assertIn("Hello World", out)
     self.assertIn("<strong>&lt;module&gt;</strong>", out)
     # By default we emit HTML markup.
     self.assertIn('<p>', out)
     self.assertIn('</p>', out)
Example #11
0
    def test_line_continuation_EOF_from_file_bpo2180(self):
        """Ensure tok_nextc() does not add too many ending newlines."""
        with os_helper.temp_dir() as temp_dir:
            file_name = script_helper.make_script(temp_dir, 'foo', '\\')
            rc, out, err = script_helper.assert_python_failure(file_name)
            self.assertIn(b'unexpected EOF while parsing', err)
            self.assertIn(b'line 2', err)
            self.assertIn(b'\\', err)

            file_name = script_helper.make_script(temp_dir, 'foo', 'y = 6\\')
            rc, out, err = script_helper.assert_python_failure(file_name)
            self.assertIn(b'unexpected EOF while parsing', err)
            self.assertIn(b'line 2', err)
            self.assertIn(b'y = 6\\', err)
 def test_inspect_getsource_issue4223(self):
     test_src = "def foo(): pass\n"
     with os_helper.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"]
Example #13
0
 def test_issue8202_dash_c_file_ignored(self):
     # Make sure a "-c" file in the current directory
     # does not alter the value of sys.path[0]
     with os_helper.temp_dir() as script_dir:
         with os_helper.change_cwd(path=script_dir):
             with open("-c", "w", encoding="utf-8") as f:
                 f.write("data")
                 rc, out, err = assert_python_ok('-c',
                     'import sys; print("sys.path[0]==%r" % sys.path[0])',
                     __isolated=False)
                 if verbose > 1:
                     print(repr(out))
                 expected = "sys.path[0]==%r" % ''
                 self.assertIn(expected.encode('utf-8'), out)
Example #14
0
 def test_syntaxerror_invalid_escape_sequence_multi_line(self):
     script = 'foo = """\\q"""\n'
     with os_helper.temp_dir() as script_dir:
         script_name = _make_test_script(script_dir, 'script', script)
         exitcode, stdout, stderr = assert_python_failure(
             '-Werror', script_name,
         )
         self.assertEqual(
             stderr.splitlines()[-3:],
             [   b'    foo = """\\q"""',
                 b'          ^^^^^^^^',
                 b'SyntaxError: invalid escape sequence \'\\q\''
             ],
         )
Example #15
0
    def test_change_cwd__non_existent_dir(self):
        """Test passing a non-existent directory."""
        original_cwd = os.getcwd()

        def call_change_cwd(path):
            with os_helper.change_cwd(path) as new_cwd:
                raise Exception("should not get here")

        with os_helper.temp_dir() as parent_dir:
            non_existent_dir = os.path.join(parent_dir, 'does_not_exist')
            self.assertRaises(FileNotFoundError, call_change_cwd,
                              non_existent_dir)

        self.assertEqual(os.getcwd(), original_cwd)
Example #16
0
    def test_temp_dir(self):
        """Test that temp_dir() creates and destroys its directory."""
        parent_dir = tempfile.mkdtemp()
        parent_dir = os.path.realpath(parent_dir)

        try:
            path = os.path.join(parent_dir, 'temp')
            self.assertFalse(os.path.isdir(path))
            with os_helper.temp_dir(path) as temp_path:
                self.assertEqual(temp_path, path)
                self.assertTrue(os.path.isdir(path))
            self.assertFalse(os.path.isdir(path))
        finally:
            os_helper.rmtree(parent_dir)
Example #17
0
 def test_ipython_workaround(self):
     # Some versions of the IPython launch script are missing the
     # __name__ = "__main__" guard, and multiprocessing has long had
     # a workaround for that case
     # See https://github.com/ipython/ipython/issues/4698
     source = test_source_main_skipped_in_children
     with os_helper.temp_dir() as script_dir:
         script_name = _make_test_script(script_dir, 'ipython',
                                         source=source)
         self._check_script(script_name)
         script_no_suffix = _make_test_script(script_dir, 'ipython',
                                              source=source,
                                              omit_suffix=True)
         self._check_script(script_no_suffix)
Example #18
0
 def test_package_compiled(self):
     with os_helper.temp_dir() as script_dir:
         pkg_dir = os.path.join(script_dir, 'test_pkg')
         make_pkg(pkg_dir)
         script_name = _make_test_script(pkg_dir, '__main__')
         compiled_name = py_compile.compile(script_name, doraise=True)
         os.remove(script_name)
         pyc_file = import_helper.make_legacy_pyc(script_name)
         self._check_script(["-m", "test_pkg"],
                            pyc_file,
                            pyc_file,
                            script_dir,
                            'test_pkg',
                            importlib.machinery.SourcelessFileLoader,
                            cwd=script_dir)
Example #19
0
 def test_module_in_subpackage_in_zipfile(self):
     with os_helper.temp_dir() as script_dir:
         zip_name, run_name = _make_test_zip_pkg(script_dir,
                                                 'test_zip',
                                                 'test_pkg',
                                                 'script',
                                                 depth=2)
         self._check_script(["-m", "test_pkg.test_pkg.script"],
                            run_name,
                            run_name,
                            script_dir,
                            'test_pkg.test_pkg',
                            zipimport.zipimporter,
                            PYTHONPATH=zip_name,
                            cwd=script_dir)
Example #20
0
    def test_issue20884(self):
        # On Windows, script with encoding cookie and LF line ending
        # will be failed.
        with os_helper.temp_dir() as script_dir:
            script_name = os.path.join(script_dir, "issue20884.py")
            with open(script_name, "w", encoding="latin1", newline='\n') as f:
                f.write("#coding: iso-8859-1\n")
                f.write('"""\n')
                for _ in range(30):
                    f.write('x' * 80 + '\n')
                f.write('"""\n')

            with os_helper.change_cwd(path=script_dir):
                rc, out, err = assert_python_ok(script_name)
            self.assertEqual(b"", out)
            self.assertEqual(b"", err)
Example #21
0
 def test_issue8202(self):
     # Make sure package __init__ modules see "-m" in sys.argv0 while
     # searching for the module to execute
     with os_helper.temp_dir() as script_dir:
         with os_helper.change_cwd(path=script_dir):
             pkg_dir = os.path.join(script_dir, 'test_pkg')
             make_pkg(pkg_dir, "import sys; print('init_argv0==%r' % sys.argv[0])")
             script_name = _make_test_script(pkg_dir, 'script')
             rc, out, err = assert_python_ok('-m', 'test_pkg.script', *example_args, __isolated=False)
             if verbose > 1:
                 print(repr(out))
             expected = "init_argv0==%r" % '-m'
             self.assertIn(expected.encode('utf-8'), out)
             self._check_output(script_name, rc, out,
                                script_name, script_name, script_dir, 'test_pkg',
                                importlib.machinery.SourceFileLoader)
Example #22
0
    def test_issue20500_exit_with_exception_value(self):
        script = textwrap.dedent("""\
            import sys
            error = None
            try:
                raise ValueError('some text')
            except ValueError as err:
                error = err

            if error:
                sys.exit(error)
            """)
        with os_helper.temp_dir() as script_dir:
            script_name = _make_test_script(script_dir, 'script', script)
            exitcode, stdout, stderr = assert_python_failure(script_name)
            text = stderr.decode('ascii')
            self.assertEqual(text.rstrip(), "some text")
Example #23
0
 def test_pep_409_verbiage(self):
     # Make sure PEP 409 syntax properly suppresses
     # the context of an exception
     script = textwrap.dedent("""\
         try:
             raise ValueError
         except:
             raise NameError from None
         """)
     with os_helper.temp_dir() as script_dir:
         script_name = _make_test_script(script_dir, 'script', script)
         exitcode, stdout, stderr = assert_python_failure(script_name)
         text = stderr.decode('ascii').split('\n')
         self.assertEqual(len(text), 5)
         self.assertTrue(text[0].startswith('Traceback'))
         self.assertTrue(text[1].startswith('  File '))
         self.assertTrue(text[3].startswith('NameError'))
Example #24
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')
Example #25
0
    def test_change_cwd__non_existent_dir__quiet_true(self):
        """Test passing a non-existent directory with quiet=True."""
        original_cwd = os.getcwd()

        with os_helper.temp_dir() as parent_dir:
            bad_dir = os.path.join(parent_dir, 'does_not_exist')
            with warnings_helper.check_warnings() as recorder:
                with os_helper.change_cwd(bad_dir, quiet=True) as new_cwd:
                    self.assertEqual(new_cwd, original_cwd)
                    self.assertEqual(os.getcwd(), new_cwd)
                warnings = [str(w.message) for w in recorder.warnings]

        self.assertEqual(len(warnings), 1, warnings)
        warn = warnings[0]
        self.assertTrue(
            warn.startswith(f'tests may fail, unable to change '
                            f'the current working directory '
                            f'to {bad_dir!r}: '), warn)
Example #26
0
    def test_syntaxerror_indented_caret_position(self):
        script = textwrap.dedent("""\
            if True:
                1 + 1 = 2
            """)
        with os_helper.temp_dir() as script_dir:
            script_name = _make_test_script(script_dir, 'script', script)
            exitcode, stdout, stderr = assert_python_failure(script_name)
            text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read()
            # Confirm that the caret starts under the first 1 character
            self.assertIn("\n    1 + 1 = 2\n    ^^^^^\n", text)

            # Try the same with a form feed at the start of the indented line
            script = ("if True:\n" "\f    1 + 1 = 2\n")
            script_name = _make_test_script(script_dir, "script", script)
            exitcode, stdout, stderr = assert_python_failure(script_name)
            text = io.TextIOWrapper(io.BytesIO(stderr), "ascii").read()
            self.assertNotIn("\f", text)
            self.assertIn("\n    1 + 1 = 2\n    ^^^^^\n", text)
Example #27
0
    def test_selftest(self):
        self.maxDiff = None
        with os_helper.temp_dir() as directory:
            data_path = os.path.join(directory, '_test.py')
            with open(self.script, encoding='utf-8') as f:
                closed = f.read()
            with open(data_path, 'w', encoding='utf-8') as f:
                f.write(closed)

            rc, out, err = assert_python_ok(self.script, '-d', data_path)
            self.assertEqual(out, b'')
            self.assertEqual(err, b'')
            backup = data_path + '~'
            self.assertTrue(os.path.exists(backup))
            with open(backup, encoding='utf-8') as f:
                self.assertEqual(f.read(), closed)
            with open(data_path, encoding='utf-8') as f:
                clean = f.read()
            compile(clean, '_test.py', 'exec')
            self.assertEqual(self.pindent(clean, '-c'), closed)
            self.assertEqual(self.pindent(closed, '-d'), clean)

            rc, out, err = assert_python_ok(self.script, '-c', data_path)
            self.assertEqual(out, b'')
            self.assertEqual(err, b'')
            with open(backup, encoding='utf-8') as f:
                self.assertEqual(f.read(), clean)
            with open(data_path, encoding='utf-8') as f:
                self.assertEqual(f.read(), closed)

            broken = self.lstriplines(closed)
            with open(data_path, 'w', encoding='utf-8') as f:
                f.write(broken)
            rc, out, err = assert_python_ok(self.script, '-r', data_path)
            self.assertEqual(out, b'')
            self.assertEqual(err, b'')
            with open(backup, encoding='utf-8') as f:
                self.assertEqual(f.read(), broken)
            with open(data_path, encoding='utf-8') as f:
                indented = f.read()
            compile(indented, '_test.py', 'exec')
            self.assertEqual(self.pindent(broken, '-r'), indented)
Example #28
0
    def test_temp_dir__existing_dir__quiet_true(self):
        """Test passing a directory that already exists with quiet=True."""
        path = tempfile.mkdtemp()
        path = os.path.realpath(path)

        try:
            with warnings_helper.check_warnings() as recorder:
                with os_helper.temp_dir(path, quiet=True) as temp_path:
                    self.assertEqual(path, temp_path)
                warnings = [str(w.message) for w in recorder.warnings]
            # Make sure temp_dir did not delete the original directory.
            self.assertTrue(os.path.isdir(path))
        finally:
            shutil.rmtree(path)

        self.assertEqual(len(warnings), 1, warnings)
        warn = warnings[0]
        self.assertTrue(
            warn.startswith(f'tests may fail, unable to create '
                            f'temporary directory {path!r}: '), warn)
    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)
Example #30
0
 def test_files_list(self):
     """Make sure the directories are inspected for source files
        bpo-31920
     """
     text1 = 'Text to translate1'
     text2 = 'Text to translate2'
     text3 = 'Text to ignore'
     with temp_cwd(None), temp_dir(None) as sdir:
         os.mkdir(os.path.join(sdir, 'pypkg'))
         with open(os.path.join(sdir, 'pypkg', 'pymod.py'), 'w') as sfile:
             sfile.write(f'_({text1!r})')
         os.mkdir(os.path.join(sdir, 'pkg.py'))
         with open(os.path.join(sdir, 'pkg.py', 'pymod2.py'), 'w') as sfile:
             sfile.write(f'_({text2!r})')
         os.mkdir(os.path.join(sdir, 'CVS'))
         with open(os.path.join(sdir, 'CVS', 'pymod3.py'), 'w') as sfile:
             sfile.write(f'_({text3!r})')
         assert_python_ok(self.script, sdir)
         with open('messages.pot') as fp:
             data = fp.read()
         self.assertIn(f'msgid "{text1}"', data)
         self.assertIn(f'msgid "{text2}"', data)
         self.assertNotIn(text3, data)