]> granicus.if.org Git - python/commitdiff
Close #18754: Run Python child processes in isolated more in the test suite.
authorVictor Stinner <victor.stinner@gmail.com>
Sat, 12 Oct 2013 12:44:01 +0000 (14:44 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Sat, 12 Oct 2013 12:44:01 +0000 (14:44 +0200)
Lib/test/script_helper.py
Lib/test/test_cmd_line.py
Lib/test/test_cmd_line_script.py
Lib/test/test_compileall.py
Lib/test/test_import.py

index f9d2b65dd25f3704926f8c5a30920399029b0370..4d5c1f120adf02189205b28946b5fde9d0f663a3 100644 (file)
@@ -17,8 +17,17 @@ from test.support import make_legacy_pyc, strip_python_stderr, temp_dir
 
 # Executing the interpreter in a subprocess
 def _assert_python(expected_success, *args, **env_vars):
+    if '__isolated' in env_vars:
+        isolated = env_vars.pop('__isolated')
+    else:
+        isolated = not env_vars
     cmd_line = [sys.executable, '-X', 'faulthandler']
-    if not env_vars:
+    if isolated:
+        # isolated mode: ignore Python environment variables, ignore user
+        # site-packages, and don't add the current directory to sys.path
+        cmd_line.append('-I')
+    elif not env_vars:
+        # ignore Python environment variables
         cmd_line.append('-E')
     # Need to preserve the original environment, for in-place testing of
     # shared library builds.
@@ -51,6 +60,11 @@ def assert_python_ok(*args, **env_vars):
     Assert that running the interpreter with `args` and optional environment
     variables `env_vars` succeeds (rc == 0) and return a (return code, stdout,
     stderr) tuple.
+
+    If the __cleanenv keyword is set, env_vars is used a fresh environment.
+
+    Python is started in isolated mode (command line option -I),
+    except if the __isolated keyword is set to False.
     """
     return _assert_python(True, *args, **env_vars)
 
@@ -59,6 +73,8 @@ def assert_python_failure(*args, **env_vars):
     Assert that running the interpreter with `args` and optional environment
     variables `env_vars` fails (rc != 0) and return a (return code, stdout,
     stderr) tuple.
+
+    See assert_python_ok() for more options.
     """
     return _assert_python(False, *args, **env_vars)
 
index efe45469ebcc9327be71e34eacd45cc965174c63..327c1455fcf741438d508947a2add4f89ccf4f3a 100644 (file)
@@ -263,7 +263,7 @@ class CmdLineTest(unittest.TestCase):
             path = path.encode("ascii", "backslashreplace")
             sys.stdout.buffer.write(path)"""
         rc1, out1, err1 = assert_python_ok('-c', code, PYTHONPATH="")
-        rc2, out2, err2 = assert_python_ok('-c', code)
+        rc2, out2, err2 = assert_python_ok('-c', code, __isolated=False)
         # regarding to Posix specification, outputs should be equal
         # for empty and unset PYTHONPATH
         self.assertEqual(out1, out2)
index b4269b994e451f96aeab553aede72ad23dd85a2a..f804d8645f68d14de4746c6a70c9a307da8a9df4 100644 (file)
@@ -123,7 +123,7 @@ class CmdLineTest(unittest.TestCase):
         if not __debug__:
             cmd_line_switches += ('-' + 'O' * sys.flags.optimize,)
         run_args = cmd_line_switches + (script_name,) + tuple(example_args)
-        rc, out, err = assert_python_ok(*run_args)
+        rc, out, err = assert_python_ok(*run_args, __isolated=False)
         self._check_output(script_name, rc, out + err, expected_file,
                            expected_argv0, expected_path0,
                            expected_package, expected_loader)
@@ -294,7 +294,7 @@ class CmdLineTest(unittest.TestCase):
                 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)
+                rc, out, err = assert_python_ok('-m', 'test_pkg.script', *example_args, __isolated=False)
                 if verbose > 1:
                     print(out)
                 expected = "init_argv0==%r" % '-m'
@@ -311,7 +311,8 @@ class CmdLineTest(unittest.TestCase):
                 with open("-c", "w") as f:
                     f.write("data")
                     rc, out, err = assert_python_ok('-c',
-                        'import sys; print("sys.path[0]==%r" % sys.path[0])')
+                        'import sys; print("sys.path[0]==%r" % sys.path[0])',
+                        __isolated=False)
                     if verbose > 1:
                         print(out)
                     expected = "sys.path[0]==%r" % ''
@@ -325,7 +326,8 @@ class CmdLineTest(unittest.TestCase):
             with support.change_cwd(path=script_dir):
                 with open("-m", "w") as f:
                     f.write("data")
-                    rc, out, err = assert_python_ok('-m', 'other', *example_args)
+                    rc, out, err = assert_python_ok('-m', 'other', *example_args,
+                                                    __isolated=False)
                     self._check_output(script_name, rc, out,
                                       script_name, script_name, '', '',
                                       importlib.machinery.SourceFileLoader)
index cf58ca6fa71d72b999c0c38ab818c9eac9d34444..2122ade0aeda7620e30e2fa84ccf40c4e1e25d78 100644 (file)
@@ -295,7 +295,7 @@ class CommandLineTests(unittest.TestCase):
         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)
+        rc, out, err = script_helper.assert_python_failure(fn, __isolated=False)
         self.assertRegex(err, b'File "dinsdale')
 
     def test_include_bad_file(self):
index a61ee2bcc0d209e5a0a30e6de13c1db25533f1f1..e88141c4e8cc3307f8da239bf78e4e58b70ff302 100644 (file)
@@ -1058,7 +1058,8 @@ class ImportTracebackTests(unittest.TestCase):
         # 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)
+        script_helper.assert_python_ok("-c", "mod = __import__(%a)" % name,
+                                       __isolated=False)
 
 
 if __name__ == '__main__':