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

Skip to content

Commit c3493aa

Browse files
committed
Make the stdlib test suite helper test.script_helper._assert_python no longer
pass -I or -E to the child process by default when the environment is required for the child process interpreter to function properly.
1 parent 6a1b004 commit c3493aa

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

Lib/test/script_helper.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,17 @@ def _interpreter_requires_environment():
5252

5353
# Executing the interpreter in a subprocess
5454
def _assert_python(expected_success, *args, **env_vars):
55+
env_required = _interpreter_requires_environment()
5556
if '__isolated' in env_vars:
5657
isolated = env_vars.pop('__isolated')
5758
else:
58-
isolated = not env_vars
59+
isolated = not env_vars and not env_required
5960
cmd_line = [sys.executable, '-X', 'faulthandler']
6061
if isolated:
6162
# isolated mode: ignore Python environment variables, ignore user
6263
# site-packages, and don't add the current directory to sys.path
6364
cmd_line.append('-I')
64-
elif not env_vars:
65+
elif not env_vars and not env_required:
6566
# ignore Python environment variables
6667
cmd_line.append('-E')
6768
# Need to preserve the original environment, for in-place testing of

Lib/test/test_script_helper.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,39 @@ def test_assert_python_raises_expect_failure(self):
3333
self.assertIn('import sys; sys.exit(0)', error_msg,
3434
msg='unexpected command line.')
3535

36+
@mock.patch('subprocess.Popen')
37+
def test_assert_python_isolated_when_env_not_required(self, mock_popen):
38+
with mock.patch.object(script_helper,
39+
'_interpreter_requires_environment',
40+
return_value=False) as mock_ire_func:
41+
mock_popen.side_effect = RuntimeError('bail out of unittest')
42+
try:
43+
script_helper._assert_python(True, '-c', 'None')
44+
except RuntimeError as err:
45+
self.assertEqual('bail out of unittest', err.args[0])
46+
self.assertEqual(1, mock_popen.call_count)
47+
self.assertEqual(1, mock_ire_func.call_count)
48+
popen_command = mock_popen.call_args[0][0]
49+
self.assertEqual(sys.executable, popen_command[0])
50+
self.assertIn('None', popen_command)
51+
self.assertIn('-I', popen_command)
52+
self.assertNotIn('-E', popen_command) # -I overrides this
53+
54+
@mock.patch('subprocess.Popen')
55+
def test_assert_python_not_isolated_when_env_is_required(self, mock_popen):
56+
"""Ensure that -I is not passed when the environment is required."""
57+
with mock.patch.object(script_helper,
58+
'_interpreter_requires_environment',
59+
return_value=True) as mock_ire_func:
60+
mock_popen.side_effect = RuntimeError('bail out of unittest')
61+
try:
62+
script_helper._assert_python(True, '-c', 'None')
63+
except RuntimeError as err:
64+
self.assertEqual('bail out of unittest', err.args[0])
65+
popen_command = mock_popen.call_args[0][0]
66+
self.assertNotIn('-I', popen_command)
67+
self.assertNotIn('-E', popen_command)
68+
3669

3770
class TestScriptHelperEnvironment(unittest.TestCase):
3871
"""Code coverage for _interpreter_requires_environment()."""

0 commit comments

Comments
 (0)