Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e8785ff

Browse files
committed
Close #18754: Run Python child processes in isolated more in the test suite.
1 parent c2228c8 commit e8785ff

5 files changed

Lines changed: 27 additions & 8 deletions

File tree

Lib/test/script_helper.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,17 @@
1717

1818
# Executing the interpreter in a subprocess
1919
def _assert_python(expected_success, *args, **env_vars):
20+
if '__isolated' in env_vars:
21+
isolated = env_vars.pop('__isolated')
22+
else:
23+
isolated = not env_vars
2024
cmd_line = [sys.executable, '-X', 'faulthandler']
21-
if not env_vars:
25+
if isolated:
26+
# isolated mode: ignore Python environment variables, ignore user
27+
# site-packages, and don't add the current directory to sys.path
28+
cmd_line.append('-I')
29+
elif not env_vars:
30+
# ignore Python environment variables
2231
cmd_line.append('-E')
2332
# Need to preserve the original environment, for in-place testing of
2433
# shared library builds.
@@ -51,6 +60,11 @@ def assert_python_ok(*args, **env_vars):
5160
Assert that running the interpreter with `args` and optional environment
5261
variables `env_vars` succeeds (rc == 0) and return a (return code, stdout,
5362
stderr) tuple.
63+
64+
If the __cleanenv keyword is set, env_vars is used a fresh environment.
65+
66+
Python is started in isolated mode (command line option -I),
67+
except if the __isolated keyword is set to False.
5468
"""
5569
return _assert_python(True, *args, **env_vars)
5670

@@ -59,6 +73,8 @@ def assert_python_failure(*args, **env_vars):
5973
Assert that running the interpreter with `args` and optional environment
6074
variables `env_vars` fails (rc != 0) and return a (return code, stdout,
6175
stderr) tuple.
76+
77+
See assert_python_ok() for more options.
6278
"""
6379
return _assert_python(False, *args, **env_vars)
6480

Lib/test/test_cmd_line.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def test_empty_PYTHONPATH_issue16309(self):
263263
path = path.encode("ascii", "backslashreplace")
264264
sys.stdout.buffer.write(path)"""
265265
rc1, out1, err1 = assert_python_ok('-c', code, PYTHONPATH="")
266-
rc2, out2, err2 = assert_python_ok('-c', code)
266+
rc2, out2, err2 = assert_python_ok('-c', code, __isolated=False)
267267
# regarding to Posix specification, outputs should be equal
268268
# for empty and unset PYTHONPATH
269269
self.assertEqual(out1, out2)

Lib/test/test_cmd_line_script.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def _check_script(self, script_name, expected_file,
123123
if not __debug__:
124124
cmd_line_switches += ('-' + 'O' * sys.flags.optimize,)
125125
run_args = cmd_line_switches + (script_name,) + tuple(example_args)
126-
rc, out, err = assert_python_ok(*run_args)
126+
rc, out, err = assert_python_ok(*run_args, __isolated=False)
127127
self._check_output(script_name, rc, out + err, expected_file,
128128
expected_argv0, expected_path0,
129129
expected_package, expected_loader)
@@ -294,7 +294,7 @@ def test_issue8202(self):
294294
pkg_dir = os.path.join(script_dir, 'test_pkg')
295295
make_pkg(pkg_dir, "import sys; print('init_argv0==%r' % sys.argv[0])")
296296
script_name = _make_test_script(pkg_dir, 'script')
297-
rc, out, err = assert_python_ok('-m', 'test_pkg.script', *example_args)
297+
rc, out, err = assert_python_ok('-m', 'test_pkg.script', *example_args, __isolated=False)
298298
if verbose > 1:
299299
print(out)
300300
expected = "init_argv0==%r" % '-m'
@@ -311,7 +311,8 @@ def test_issue8202_dash_c_file_ignored(self):
311311
with open("-c", "w") as f:
312312
f.write("data")
313313
rc, out, err = assert_python_ok('-c',
314-
'import sys; print("sys.path[0]==%r" % sys.path[0])')
314+
'import sys; print("sys.path[0]==%r" % sys.path[0])',
315+
__isolated=False)
315316
if verbose > 1:
316317
print(out)
317318
expected = "sys.path[0]==%r" % ''
@@ -325,7 +326,8 @@ def test_issue8202_dash_m_file_ignored(self):
325326
with support.change_cwd(path=script_dir):
326327
with open("-m", "w") as f:
327328
f.write("data")
328-
rc, out, err = assert_python_ok('-m', 'other', *example_args)
329+
rc, out, err = assert_python_ok('-m', 'other', *example_args,
330+
__isolated=False)
329331
self._check_output(script_name, rc, out,
330332
script_name, script_name, '', '',
331333
importlib.machinery.SourceFileLoader)

Lib/test/test_compileall.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def test_d_runtime_error(self):
295295
pyc = importlib.util.cache_from_source(bazfn)
296296
os.rename(pyc, os.path.join(self.pkgdir, 'baz.pyc'))
297297
os.remove(bazfn)
298-
rc, out, err = script_helper.assert_python_failure(fn)
298+
rc, out, err = script_helper.assert_python_failure(fn, __isolated=False)
299299
self.assertRegex(err, b'File "dinsdale')
300300

301301
def test_include_bad_file(self):

Lib/test/test_import.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,8 @@ def test_unencodable_filename(self):
10581058
# encode filenames, especially on Windows
10591059
pyname = script_helper.make_script('', TESTFN_UNENCODABLE, 'pass')
10601060
name = pyname[:-3]
1061-
script_helper.assert_python_ok("-c", "mod = __import__(%a)" % name)
1061+
script_helper.assert_python_ok("-c", "mod = __import__(%a)" % name,
1062+
__isolated=False)
10621063

10631064

10641065
if __name__ == '__main__':

0 commit comments

Comments
 (0)