| 1 |
# bpo-42260: Test _PyInterpreterState_GetConfigCopy() |
| 2 |
# and _PyInterpreterState_SetConfig(). |
| 3 |
# |
| 4 |
# Test run in a subprocess since set_config(get_config()) |
| 5 |
# does reset sys attributes to their state of the Python startup |
| 6 |
# (before the site module is run). |
| 7 |
|
| 8 |
import _testinternalcapi |
| 9 |
import os |
| 10 |
import sys |
| 11 |
import unittest |
| 12 |
from test import support |
| 13 |
from test.support import MS_WINDOWS |
| 14 |
|
| 15 |
|
| 16 |
MAX_HASH_SEED = 4294967295 |
| 17 |
|
| 18 |
|
| 19 |
BOOL_OPTIONS = [ |
| 20 |
'isolated', |
| 21 |
'use_environment', |
| 22 |
'dev_mode', |
| 23 |
'install_signal_handlers', |
| 24 |
'use_hash_seed', |
| 25 |
'faulthandler', |
| 26 |
'import_time', |
| 27 |
'code_debug_ranges', |
| 28 |
'show_ref_count', |
| 29 |
'dump_refs', |
| 30 |
'malloc_stats', |
| 31 |
'parse_argv', |
| 32 |
'site_import', |
| 33 |
'warn_default_encoding', |
| 34 |
'inspect', |
| 35 |
'interactive', |
| 36 |
'parser_debug', |
| 37 |
'write_bytecode', |
| 38 |
'quiet', |
| 39 |
'user_site_directory', |
| 40 |
'configure_c_stdio', |
| 41 |
'buffered_stdio', |
| 42 |
'use_frozen_modules', |
| 43 |
'safe_path', |
| 44 |
'pathconfig_warnings', |
| 45 |
'module_search_paths_set', |
| 46 |
'skip_source_first_line', |
| 47 |
'_install_importlib', |
| 48 |
'_init_main', |
| 49 |
'_is_python_build', |
| 50 |
] |
| 51 |
if MS_WINDOWS: |
| 52 |
BOOL_OPTIONS.append('legacy_windows_stdio') |
| 53 |
|
| 54 |
|
| 55 |
class SetConfigTests(unittest.TestCase): |
| 56 |
def setUp(self): |
| 57 |
self.old_config = _testinternalcapi.get_config() |
| 58 |
self.sys_copy = dict(sys.__dict__) |
| 59 |
|
| 60 |
def tearDown(self): |
| 61 |
_testinternalcapi.reset_path_config() |
| 62 |
_testinternalcapi.set_config(self.old_config) |
| 63 |
sys.__dict__.clear() |
| 64 |
sys.__dict__.update(self.sys_copy) |
| 65 |
|
| 66 |
def set_config(self, **kwargs): |
| 67 |
_testinternalcapi.set_config(self.old_config | kwargs) |
| 68 |
|
| 69 |
def check(self, **kwargs): |
| 70 |
self.set_config(**kwargs) |
| 71 |
for key, value in kwargs.items(): |
| 72 |
self.assertEqual(getattr(sys, key), value, |
| 73 |
(key, value)) |
| 74 |
|
| 75 |
def test_set_invalid(self): |
| 76 |
invalid_uint = -1 |
| 77 |
NULL = None |
| 78 |
invalid_wstr = NULL |
| 79 |
# PyWideStringList strings must be non-NULL |
| 80 |
invalid_wstrlist = ["abc", NULL, "def"] |
| 81 |
|
| 82 |
type_tests = [] |
| 83 |
value_tests = [ |
| 84 |
# enum |
| 85 |
('_config_init', 0), |
| 86 |
('_config_init', 4), |
| 87 |
# unsigned long |
| 88 |
("hash_seed", -1), |
| 89 |
("hash_seed", MAX_HASH_SEED + 1), |
| 90 |
] |
| 91 |
|
| 92 |
# int (unsigned) |
| 93 |
int_options = [ |
| 94 |
'_config_init', |
| 95 |
'bytes_warning', |
| 96 |
'optimization_level', |
| 97 |
'tracemalloc', |
| 98 |
'verbose', |
| 99 |
] |
| 100 |
int_options.extend(BOOL_OPTIONS) |
| 101 |
for key in int_options: |
| 102 |
value_tests.append((key, invalid_uint)) |
| 103 |
type_tests.append((key, "abc")) |
| 104 |
type_tests.append((key, 2.0)) |
| 105 |
|
| 106 |
# wchar_t* |
| 107 |
for key in ( |
| 108 |
'filesystem_encoding', |
| 109 |
'filesystem_errors', |
| 110 |
'stdio_encoding', |
| 111 |
'stdio_errors', |
| 112 |
'check_hash_pycs_mode', |
| 113 |
'program_name', |
| 114 |
'platlibdir', |
| 115 |
# optional wstr: |
| 116 |
# 'pythonpath_env' |
| 117 |
# 'home' |
| 118 |
# 'pycache_prefix' |
| 119 |
# 'run_command' |
| 120 |
# 'run_module' |
| 121 |
# 'run_filename' |
| 122 |
# 'executable' |
| 123 |
# 'prefix' |
| 124 |
# 'exec_prefix' |
| 125 |
# 'base_executable' |
| 126 |
# 'base_prefix' |
| 127 |
# 'base_exec_prefix' |
| 128 |
): |
| 129 |
value_tests.append((key, invalid_wstr)) |
| 130 |
type_tests.append((key, b'bytes')) |
| 131 |
type_tests.append((key, 123)) |
| 132 |
|
| 133 |
# PyWideStringList |
| 134 |
for key in ( |
| 135 |
'orig_argv', |
| 136 |
'argv', |
| 137 |
'xoptions', |
| 138 |
'warnoptions', |
| 139 |
'module_search_paths', |
| 140 |
): |
| 141 |
value_tests.append((key, invalid_wstrlist)) |
| 142 |
type_tests.append((key, 123)) |
| 143 |
type_tests.append((key, "abc")) |
| 144 |
type_tests.append((key, [123])) |
| 145 |
type_tests.append((key, [b"bytes"])) |
| 146 |
|
| 147 |
|
| 148 |
if MS_WINDOWS: |
| 149 |
value_tests.append(('legacy_windows_stdio', invalid_uint)) |
| 150 |
|
| 151 |
for exc_type, tests in ( |
| 152 |
(ValueError, value_tests), |
| 153 |
(TypeError, type_tests), |
| 154 |
): |
| 155 |
for key, value in tests: |
| 156 |
config = self.old_config | {key: value} |
| 157 |
with self.subTest(key=key, value=value, exc_type=exc_type): |
| 158 |
with self.assertRaises(exc_type): |
| 159 |
_testinternalcapi.set_config(config) |
| 160 |
|
| 161 |
def test_flags(self): |
| 162 |
bool_options = set(BOOL_OPTIONS) |
| 163 |
for sys_attr, key, value in ( |
| 164 |
("debug", "parser_debug", 1), |
| 165 |
("inspect", "inspect", 2), |
| 166 |
("interactive", "interactive", 3), |
| 167 |
("optimize", "optimization_level", 4), |
| 168 |
("verbose", "verbose", 1), |
| 169 |
("bytes_warning", "bytes_warning", 10), |
| 170 |
("quiet", "quiet", 11), |
| 171 |
("isolated", "isolated", 12), |
| 172 |
): |
| 173 |
with self.subTest(sys=sys_attr, key=key, value=value): |
| 174 |
self.set_config(**{key: value, 'parse_argv': 0}) |
| 175 |
if key in bool_options: |
| 176 |
self.assertEqual(getattr(sys.flags, sys_attr), int(bool(value))) |
| 177 |
else: |
| 178 |
self.assertEqual(getattr(sys.flags, sys_attr), value) |
| 179 |
|
| 180 |
self.set_config(write_bytecode=0) |
| 181 |
self.assertEqual(sys.flags.dont_write_bytecode, True) |
| 182 |
self.assertEqual(sys.dont_write_bytecode, True) |
| 183 |
|
| 184 |
self.set_config(write_bytecode=1) |
| 185 |
self.assertEqual(sys.flags.dont_write_bytecode, False) |
| 186 |
self.assertEqual(sys.dont_write_bytecode, False) |
| 187 |
|
| 188 |
self.set_config(user_site_directory=0, isolated=0) |
| 189 |
self.assertEqual(sys.flags.no_user_site, 1) |
| 190 |
self.set_config(user_site_directory=1, isolated=0) |
| 191 |
self.assertEqual(sys.flags.no_user_site, 0) |
| 192 |
|
| 193 |
self.set_config(site_import=0) |
| 194 |
self.assertEqual(sys.flags.no_site, 1) |
| 195 |
self.set_config(site_import=1) |
| 196 |
self.assertEqual(sys.flags.no_site, 0) |
| 197 |
|
| 198 |
self.set_config(dev_mode=0) |
| 199 |
self.assertEqual(sys.flags.dev_mode, False) |
| 200 |
self.set_config(dev_mode=1) |
| 201 |
self.assertEqual(sys.flags.dev_mode, True) |
| 202 |
|
| 203 |
self.set_config(use_environment=0, isolated=0) |
| 204 |
self.assertEqual(sys.flags.ignore_environment, 1) |
| 205 |
self.set_config(use_environment=1, isolated=0) |
| 206 |
self.assertEqual(sys.flags.ignore_environment, 0) |
| 207 |
|
| 208 |
self.set_config(use_hash_seed=1, hash_seed=0) |
| 209 |
self.assertEqual(sys.flags.hash_randomization, 0) |
| 210 |
self.set_config(use_hash_seed=0, hash_seed=0) |
| 211 |
self.assertEqual(sys.flags.hash_randomization, 1) |
| 212 |
self.set_config(use_hash_seed=1, hash_seed=123) |
| 213 |
self.assertEqual(sys.flags.hash_randomization, 1) |
| 214 |
|
| 215 |
if support.Py_GIL_DISABLED: |
| 216 |
self.set_config(enable_gil=-1) |
| 217 |
self.assertEqual(sys.flags.gil, None) |
| 218 |
self.set_config(enable_gil=0) |
| 219 |
self.assertEqual(sys.flags.gil, 0) |
| 220 |
self.set_config(enable_gil=1) |
| 221 |
self.assertEqual(sys.flags.gil, 1) |
| 222 |
else: |
| 223 |
# Builds without Py_GIL_DISABLED don't have |
| 224 |
# PyConfig.enable_gil. sys.flags.gil is always defined to 1, for |
| 225 |
# consistency. |
| 226 |
self.assertEqual(sys.flags.gil, 1) |
| 227 |
|
| 228 |
def test_options(self): |
| 229 |
self.check(warnoptions=[]) |
| 230 |
self.check(warnoptions=["default", "ignore"]) |
| 231 |
|
| 232 |
self.set_config(xoptions=[]) |
| 233 |
self.assertEqual(sys._xoptions, {}) |
| 234 |
self.set_config(xoptions=["dev", "tracemalloc=5"]) |
| 235 |
self.assertEqual(sys._xoptions, {"dev": True, "tracemalloc": "5"}) |
| 236 |
|
| 237 |
def test_pathconfig(self): |
| 238 |
self.check( |
| 239 |
executable='executable', |
| 240 |
prefix="prefix", |
| 241 |
base_prefix="base_prefix", |
| 242 |
exec_prefix="exec_prefix", |
| 243 |
base_exec_prefix="base_exec_prefix", |
| 244 |
platlibdir="platlibdir") |
| 245 |
|
| 246 |
self.set_config(base_executable="base_executable") |
| 247 |
self.assertEqual(sys._base_executable, "base_executable") |
| 248 |
|
| 249 |
# When base_xxx is NULL, value is copied from xxxx |
| 250 |
self.set_config( |
| 251 |
executable='executable', |
| 252 |
prefix="prefix", |
| 253 |
exec_prefix="exec_prefix", |
| 254 |
base_executable=None, |
| 255 |
base_prefix=None, |
| 256 |
base_exec_prefix=None) |
| 257 |
self.assertEqual(sys._base_executable, "executable") |
| 258 |
self.assertEqual(sys.base_prefix, "prefix") |
| 259 |
self.assertEqual(sys.base_exec_prefix, "exec_prefix") |
| 260 |
|
| 261 |
def test_path(self): |
| 262 |
self.set_config(module_search_paths_set=1, |
| 263 |
module_search_paths=['a', 'b', 'c']) |
| 264 |
self.assertEqual(sys.path, ['a', 'b', 'c']) |
| 265 |
|
| 266 |
# sys.path is reset if module_search_paths_set=0 |
| 267 |
self.set_config(module_search_paths_set=0, |
| 268 |
module_search_paths=['new_path']) |
| 269 |
self.assertNotEqual(sys.path, ['a', 'b', 'c']) |
| 270 |
self.assertNotEqual(sys.path, ['new_path']) |
| 271 |
|
| 272 |
def test_argv(self): |
| 273 |
self.set_config(parse_argv=0, |
| 274 |
argv=['python_program', 'args'], |
| 275 |
orig_argv=['orig', 'orig_args']) |
| 276 |
self.assertEqual(sys.argv, ['python_program', 'args']) |
| 277 |
self.assertEqual(sys.orig_argv, ['orig', 'orig_args']) |
| 278 |
|
| 279 |
self.set_config(parse_argv=0, |
| 280 |
argv=[], |
| 281 |
orig_argv=[]) |
| 282 |
self.assertEqual(sys.argv, ['']) |
| 283 |
self.assertEqual(sys.orig_argv, []) |
| 284 |
|
| 285 |
def test_pycache_prefix(self): |
| 286 |
self.check(pycache_prefix=None) |
| 287 |
self.check(pycache_prefix="pycache_prefix") |
| 288 |
|
| 289 |
|
| 290 |
if __name__ == "__main__": |
| 291 |
unittest.main() |