mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Close #18754: Run Python child processes in isolated more in the test suite.
This commit is contained in:
parent
c2228c8995
commit
e8785ff82a
5 changed files with 27 additions and 8 deletions
|
@ -17,8 +17,17 @@ from test.support import make_legacy_pyc, strip_python_stderr, temp_dir
|
||||||
|
|
||||||
# Executing the interpreter in a subprocess
|
# Executing the interpreter in a subprocess
|
||||||
def _assert_python(expected_success, *args, **env_vars):
|
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']
|
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')
|
cmd_line.append('-E')
|
||||||
# Need to preserve the original environment, for in-place testing of
|
# Need to preserve the original environment, for in-place testing of
|
||||||
# shared library builds.
|
# shared library builds.
|
||||||
|
@ -51,6 +60,11 @@ def assert_python_ok(*args, **env_vars):
|
||||||
Assert that running the interpreter with `args` and optional environment
|
Assert that running the interpreter with `args` and optional environment
|
||||||
variables `env_vars` succeeds (rc == 0) and return a (return code, stdout,
|
variables `env_vars` succeeds (rc == 0) and return a (return code, stdout,
|
||||||
stderr) tuple.
|
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)
|
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
|
Assert that running the interpreter with `args` and optional environment
|
||||||
variables `env_vars` fails (rc != 0) and return a (return code, stdout,
|
variables `env_vars` fails (rc != 0) and return a (return code, stdout,
|
||||||
stderr) tuple.
|
stderr) tuple.
|
||||||
|
|
||||||
|
See assert_python_ok() for more options.
|
||||||
"""
|
"""
|
||||||
return _assert_python(False, *args, **env_vars)
|
return _assert_python(False, *args, **env_vars)
|
||||||
|
|
||||||
|
|
|
@ -263,7 +263,7 @@ class CmdLineTest(unittest.TestCase):
|
||||||
path = path.encode("ascii", "backslashreplace")
|
path = path.encode("ascii", "backslashreplace")
|
||||||
sys.stdout.buffer.write(path)"""
|
sys.stdout.buffer.write(path)"""
|
||||||
rc1, out1, err1 = assert_python_ok('-c', code, PYTHONPATH="")
|
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
|
# regarding to Posix specification, outputs should be equal
|
||||||
# for empty and unset PYTHONPATH
|
# for empty and unset PYTHONPATH
|
||||||
self.assertEqual(out1, out2)
|
self.assertEqual(out1, out2)
|
||||||
|
|
|
@ -123,7 +123,7 @@ class CmdLineTest(unittest.TestCase):
|
||||||
if not __debug__:
|
if not __debug__:
|
||||||
cmd_line_switches += ('-' + 'O' * sys.flags.optimize,)
|
cmd_line_switches += ('-' + 'O' * sys.flags.optimize,)
|
||||||
run_args = cmd_line_switches + (script_name,) + tuple(example_args)
|
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,
|
self._check_output(script_name, rc, out + err, expected_file,
|
||||||
expected_argv0, expected_path0,
|
expected_argv0, expected_path0,
|
||||||
expected_package, expected_loader)
|
expected_package, expected_loader)
|
||||||
|
@ -294,7 +294,7 @@ class CmdLineTest(unittest.TestCase):
|
||||||
pkg_dir = os.path.join(script_dir, 'test_pkg')
|
pkg_dir = os.path.join(script_dir, 'test_pkg')
|
||||||
make_pkg(pkg_dir, "import sys; print('init_argv0==%r' % sys.argv[0])")
|
make_pkg(pkg_dir, "import sys; print('init_argv0==%r' % sys.argv[0])")
|
||||||
script_name = _make_test_script(pkg_dir, 'script')
|
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:
|
if verbose > 1:
|
||||||
print(out)
|
print(out)
|
||||||
expected = "init_argv0==%r" % '-m'
|
expected = "init_argv0==%r" % '-m'
|
||||||
|
@ -311,7 +311,8 @@ class CmdLineTest(unittest.TestCase):
|
||||||
with open("-c", "w") as f:
|
with open("-c", "w") as f:
|
||||||
f.write("data")
|
f.write("data")
|
||||||
rc, out, err = assert_python_ok('-c',
|
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:
|
if verbose > 1:
|
||||||
print(out)
|
print(out)
|
||||||
expected = "sys.path[0]==%r" % ''
|
expected = "sys.path[0]==%r" % ''
|
||||||
|
@ -325,7 +326,8 @@ class CmdLineTest(unittest.TestCase):
|
||||||
with support.change_cwd(path=script_dir):
|
with support.change_cwd(path=script_dir):
|
||||||
with open("-m", "w") as f:
|
with open("-m", "w") as f:
|
||||||
f.write("data")
|
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,
|
self._check_output(script_name, rc, out,
|
||||||
script_name, script_name, '', '',
|
script_name, script_name, '', '',
|
||||||
importlib.machinery.SourceFileLoader)
|
importlib.machinery.SourceFileLoader)
|
||||||
|
|
|
@ -295,7 +295,7 @@ class CommandLineTests(unittest.TestCase):
|
||||||
pyc = importlib.util.cache_from_source(bazfn)
|
pyc = importlib.util.cache_from_source(bazfn)
|
||||||
os.rename(pyc, os.path.join(self.pkgdir, 'baz.pyc'))
|
os.rename(pyc, os.path.join(self.pkgdir, 'baz.pyc'))
|
||||||
os.remove(bazfn)
|
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')
|
self.assertRegex(err, b'File "dinsdale')
|
||||||
|
|
||||||
def test_include_bad_file(self):
|
def test_include_bad_file(self):
|
||||||
|
|
|
@ -1058,7 +1058,8 @@ class ImportTracebackTests(unittest.TestCase):
|
||||||
# encode filenames, especially on Windows
|
# encode filenames, especially on Windows
|
||||||
pyname = script_helper.make_script('', TESTFN_UNENCODABLE, 'pass')
|
pyname = script_helper.make_script('', TESTFN_UNENCODABLE, 'pass')
|
||||||
name = pyname[:-3]
|
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__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue