/src/cpython3/Python/initconfig.c
Line | Count | Source |
1 | | #include "Python.h" |
2 | | #include "pycore_fileutils.h" // _Py_HasFileSystemDefaultEncodeErrors |
3 | | #include "pycore_getopt.h" // _PyOS_GetOpt() |
4 | | #include "pycore_initconfig.h" // _PyStatus_OK() |
5 | | #include "pycore_interp.h" // _PyInterpreterState.runtime |
6 | | #include "pycore_long.h" // _PY_LONG_MAX_STR_DIGITS_THRESHOLD |
7 | | #include "pycore_pathconfig.h" // _Py_path_config |
8 | | #include "pycore_pyerrors.h" // _PyErr_GetRaisedException() |
9 | | #include "pycore_pylifecycle.h" // _Py_PreInitializeFromConfig() |
10 | | #include "pycore_pymem.h" // _PyMem_DefaultRawMalloc() |
11 | | #include "pycore_pyhash.h" // _Py_HashSecret |
12 | | #include "pycore_pystate.h" // _PyThreadState_GET() |
13 | | #include "pycore_pystats.h" // _Py_StatsOn() |
14 | | #include "pycore_sysmodule.h" // _PySys_SetIntMaxStrDigits() |
15 | | |
16 | | #include "osdefs.h" // DELIM |
17 | | |
18 | | #include <locale.h> // setlocale() |
19 | | #include <stdlib.h> // getenv() |
20 | | #if defined(MS_WINDOWS) || defined(__CYGWIN__) |
21 | | # ifdef HAVE_IO_H |
22 | | # include <io.h> |
23 | | # endif |
24 | | # ifdef HAVE_FCNTL_H |
25 | | # include <fcntl.h> // O_BINARY |
26 | | # endif |
27 | | #endif |
28 | | |
29 | | #ifdef __APPLE__ |
30 | | /* Enable system log by default on non-macOS Apple platforms */ |
31 | | # if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE |
32 | | #define USE_SYSTEM_LOGGER_DEFAULT 1; |
33 | | # else |
34 | | #define USE_SYSTEM_LOGGER_DEFAULT 0; |
35 | | # endif |
36 | | #endif |
37 | | |
38 | | #include "config_common.h" |
39 | | |
40 | | /* --- PyConfig setters ------------------------------------------- */ |
41 | | |
42 | | typedef PyObject* (*config_sys_flag_setter) (int value); |
43 | | |
44 | | static PyObject* |
45 | | config_sys_flag_long(int value) |
46 | 0 | { |
47 | 0 | return PyLong_FromLong(value); |
48 | 0 | } |
49 | | |
50 | | static PyObject* |
51 | | config_sys_flag_not(int value) |
52 | 0 | { |
53 | 0 | value = (!value); |
54 | 0 | return config_sys_flag_long(value); |
55 | 0 | } |
56 | | |
57 | | /* --- PyConfig spec ---------------------------------------------- */ |
58 | | |
59 | | typedef enum { |
60 | | PyConfig_MEMBER_INT = 0, |
61 | | PyConfig_MEMBER_UINT = 1, |
62 | | PyConfig_MEMBER_ULONG = 2, |
63 | | PyConfig_MEMBER_BOOL = 3, |
64 | | |
65 | | PyConfig_MEMBER_WSTR = 10, |
66 | | PyConfig_MEMBER_WSTR_OPT = 11, |
67 | | PyConfig_MEMBER_WSTR_LIST = 12, |
68 | | } PyConfigMemberType; |
69 | | |
70 | | typedef enum { |
71 | | // Option which cannot be get or set by PyConfig_Get() and PyConfig_Set() |
72 | | PyConfig_MEMBER_INIT_ONLY = 0, |
73 | | |
74 | | // Option which cannot be set by PyConfig_Set() |
75 | | PyConfig_MEMBER_READ_ONLY = 1, |
76 | | |
77 | | // Public option: can be get and set by PyConfig_Get() and PyConfig_Set() |
78 | | PyConfig_MEMBER_PUBLIC = 2, |
79 | | } PyConfigMemberVisibility; |
80 | | |
81 | | typedef struct { |
82 | | const char *attr; |
83 | | int flag_index; |
84 | | config_sys_flag_setter flag_setter; |
85 | | } PyConfigSysSpec; |
86 | | |
87 | | typedef struct { |
88 | | const char *name; |
89 | | size_t offset; |
90 | | PyConfigMemberType type; |
91 | | PyConfigMemberVisibility visibility; |
92 | | PyConfigSysSpec sys; |
93 | | } PyConfigSpec; |
94 | | |
95 | | #define SPEC(MEMBER, TYPE, VISIBILITY, sys) \ |
96 | | {#MEMBER, offsetof(PyConfig, MEMBER), \ |
97 | | PyConfig_MEMBER_##TYPE, PyConfig_MEMBER_##VISIBILITY, sys} |
98 | | |
99 | | #define SYS_ATTR(name) {name, -1, NULL} |
100 | | #define SYS_FLAG_SETTER(index, setter) {NULL, index, setter} |
101 | | #define SYS_FLAG(index) SYS_FLAG_SETTER(index, NULL) |
102 | | #define NO_SYS SYS_ATTR(NULL) |
103 | | |
104 | | // Update _test_embed_set_config when adding new members |
105 | | static const PyConfigSpec PYCONFIG_SPEC[] = { |
106 | | // --- Public options ----------- |
107 | | |
108 | | SPEC(argv, WSTR_LIST, PUBLIC, SYS_ATTR("argv")), |
109 | | SPEC(base_exec_prefix, WSTR_OPT, PUBLIC, SYS_ATTR("base_exec_prefix")), |
110 | | SPEC(base_executable, WSTR_OPT, PUBLIC, SYS_ATTR("_base_executable")), |
111 | | SPEC(base_prefix, WSTR_OPT, PUBLIC, SYS_ATTR("base_prefix")), |
112 | | SPEC(bytes_warning, UINT, PUBLIC, SYS_FLAG(9)), |
113 | | SPEC(cpu_count, INT, PUBLIC, NO_SYS), |
114 | | SPEC(lazy_imports, INT, PUBLIC, NO_SYS), |
115 | | SPEC(exec_prefix, WSTR_OPT, PUBLIC, SYS_ATTR("exec_prefix")), |
116 | | SPEC(executable, WSTR_OPT, PUBLIC, SYS_ATTR("executable")), |
117 | | SPEC(inspect, BOOL, PUBLIC, SYS_FLAG(1)), |
118 | | SPEC(int_max_str_digits, UINT, PUBLIC, NO_SYS), |
119 | | SPEC(interactive, BOOL, PUBLIC, SYS_FLAG(2)), |
120 | | SPEC(module_search_paths, WSTR_LIST, PUBLIC, SYS_ATTR("path")), |
121 | | SPEC(optimization_level, UINT, PUBLIC, SYS_FLAG(3)), |
122 | | SPEC(parser_debug, BOOL, PUBLIC, SYS_FLAG(0)), |
123 | | SPEC(platlibdir, WSTR, PUBLIC, SYS_ATTR("platlibdir")), |
124 | | SPEC(prefix, WSTR_OPT, PUBLIC, SYS_ATTR("prefix")), |
125 | | SPEC(pycache_prefix, WSTR_OPT, PUBLIC, SYS_ATTR("pycache_prefix")), |
126 | | SPEC(quiet, BOOL, PUBLIC, SYS_FLAG(10)), |
127 | | SPEC(stdlib_dir, WSTR_OPT, PUBLIC, SYS_ATTR("_stdlib_dir")), |
128 | | SPEC(use_environment, BOOL, PUBLIC, SYS_FLAG_SETTER(7, config_sys_flag_not)), |
129 | | SPEC(verbose, UINT, PUBLIC, SYS_FLAG(8)), |
130 | | SPEC(warnoptions, WSTR_LIST, PUBLIC, SYS_ATTR("warnoptions")), |
131 | | SPEC(write_bytecode, BOOL, PUBLIC, SYS_FLAG_SETTER(4, config_sys_flag_not)), |
132 | | SPEC(xoptions, WSTR_LIST, PUBLIC, SYS_ATTR("_xoptions")), |
133 | | |
134 | | // --- Read-only options ----------- |
135 | | |
136 | | #ifdef Py_STATS |
137 | | SPEC(_pystats, BOOL, READ_ONLY, NO_SYS), |
138 | | #endif |
139 | | SPEC(buffered_stdio, BOOL, READ_ONLY, NO_SYS), |
140 | | SPEC(check_hash_pycs_mode, WSTR, READ_ONLY, NO_SYS), |
141 | | SPEC(code_debug_ranges, BOOL, READ_ONLY, NO_SYS), |
142 | | SPEC(configure_c_stdio, BOOL, READ_ONLY, NO_SYS), |
143 | | SPEC(dev_mode, BOOL, READ_ONLY, NO_SYS), // sys.flags.dev_mode |
144 | | SPEC(dump_refs, BOOL, READ_ONLY, NO_SYS), |
145 | | SPEC(dump_refs_file, WSTR_OPT, READ_ONLY, NO_SYS), |
146 | | #ifdef Py_GIL_DISABLED |
147 | | SPEC(enable_gil, INT, READ_ONLY, NO_SYS), |
148 | | SPEC(tlbc_enabled, INT, READ_ONLY, NO_SYS), |
149 | | #endif |
150 | | SPEC(faulthandler, BOOL, READ_ONLY, NO_SYS), |
151 | | SPEC(filesystem_encoding, WSTR, READ_ONLY, NO_SYS), |
152 | | SPEC(filesystem_errors, WSTR, READ_ONLY, NO_SYS), |
153 | | SPEC(hash_seed, ULONG, READ_ONLY, NO_SYS), |
154 | | SPEC(home, WSTR_OPT, READ_ONLY, NO_SYS), |
155 | | SPEC(thread_inherit_context, INT, READ_ONLY, NO_SYS), |
156 | | SPEC(context_aware_warnings, INT, READ_ONLY, NO_SYS), |
157 | | SPEC(import_time, UINT, READ_ONLY, NO_SYS), |
158 | | SPEC(install_signal_handlers, BOOL, READ_ONLY, NO_SYS), |
159 | | SPEC(isolated, BOOL, READ_ONLY, NO_SYS), // sys.flags.isolated |
160 | | #ifdef MS_WINDOWS |
161 | | SPEC(legacy_windows_stdio, BOOL, READ_ONLY, NO_SYS), |
162 | | #endif |
163 | | SPEC(malloc_stats, BOOL, READ_ONLY, NO_SYS), |
164 | | SPEC(pymalloc_hugepages, BOOL, READ_ONLY, NO_SYS), |
165 | | SPEC(orig_argv, WSTR_LIST, READ_ONLY, SYS_ATTR("orig_argv")), |
166 | | SPEC(parse_argv, BOOL, READ_ONLY, NO_SYS), |
167 | | SPEC(pathconfig_warnings, BOOL, READ_ONLY, NO_SYS), |
168 | | SPEC(perf_profiling, UINT, READ_ONLY, NO_SYS), |
169 | | SPEC(remote_debug, BOOL, READ_ONLY, NO_SYS), |
170 | | SPEC(program_name, WSTR, READ_ONLY, NO_SYS), |
171 | | SPEC(run_command, WSTR_OPT, READ_ONLY, NO_SYS), |
172 | | SPEC(run_filename, WSTR_OPT, READ_ONLY, NO_SYS), |
173 | | SPEC(run_module, WSTR_OPT, READ_ONLY, NO_SYS), |
174 | | #ifdef Py_DEBUG |
175 | | SPEC(run_presite, WSTR_OPT, READ_ONLY, NO_SYS), |
176 | | #endif |
177 | | SPEC(safe_path, BOOL, READ_ONLY, NO_SYS), |
178 | | SPEC(show_ref_count, BOOL, READ_ONLY, NO_SYS), |
179 | | SPEC(site_import, BOOL, READ_ONLY, NO_SYS), // sys.flags.no_site |
180 | | SPEC(skip_source_first_line, BOOL, READ_ONLY, NO_SYS), |
181 | | SPEC(stdio_encoding, WSTR, READ_ONLY, NO_SYS), |
182 | | SPEC(stdio_errors, WSTR, READ_ONLY, NO_SYS), |
183 | | SPEC(tracemalloc, UINT, READ_ONLY, NO_SYS), |
184 | | SPEC(use_frozen_modules, BOOL, READ_ONLY, NO_SYS), |
185 | | SPEC(use_hash_seed, BOOL, READ_ONLY, NO_SYS), |
186 | | #ifdef __APPLE__ |
187 | | SPEC(use_system_logger, BOOL, READ_ONLY, NO_SYS), |
188 | | #endif |
189 | | SPEC(user_site_directory, BOOL, READ_ONLY, NO_SYS), // sys.flags.no_user_site |
190 | | SPEC(warn_default_encoding, BOOL, READ_ONLY, NO_SYS), |
191 | | |
192 | | // --- Init-only options ----------- |
193 | | |
194 | | SPEC(_config_init, UINT, INIT_ONLY, NO_SYS), |
195 | | SPEC(_init_main, BOOL, INIT_ONLY, NO_SYS), |
196 | | SPEC(_install_importlib, BOOL, INIT_ONLY, NO_SYS), |
197 | | SPEC(_is_python_build, BOOL, INIT_ONLY, NO_SYS), |
198 | | SPEC(module_search_paths_set, BOOL, INIT_ONLY, NO_SYS), |
199 | | SPEC(pythonpath_env, WSTR_OPT, INIT_ONLY, NO_SYS), |
200 | | SPEC(sys_path_0, WSTR_OPT, INIT_ONLY, NO_SYS), |
201 | | |
202 | | // Array terminator |
203 | | {NULL, 0, 0, 0, NO_SYS}, |
204 | | }; |
205 | | |
206 | | #undef SPEC |
207 | | #define SPEC(MEMBER, TYPE, VISIBILITY) \ |
208 | | {#MEMBER, offsetof(PyPreConfig, MEMBER), PyConfig_MEMBER_##TYPE, \ |
209 | | PyConfig_MEMBER_##VISIBILITY, NO_SYS} |
210 | | |
211 | | static const PyConfigSpec PYPRECONFIG_SPEC[] = { |
212 | | // --- Read-only options ----------- |
213 | | |
214 | | SPEC(allocator, INT, READ_ONLY), |
215 | | SPEC(coerce_c_locale, BOOL, READ_ONLY), |
216 | | SPEC(coerce_c_locale_warn, BOOL, READ_ONLY), |
217 | | SPEC(configure_locale, BOOL, READ_ONLY), |
218 | | #ifdef MS_WINDOWS |
219 | | SPEC(legacy_windows_fs_encoding, BOOL, READ_ONLY), |
220 | | #endif |
221 | | SPEC(utf8_mode, BOOL, READ_ONLY), |
222 | | |
223 | | // --- Init-only options ----------- |
224 | | // Members already present in PYCONFIG_SPEC |
225 | | |
226 | | SPEC(_config_init, INT, INIT_ONLY), |
227 | | SPEC(dev_mode, BOOL, INIT_ONLY), |
228 | | SPEC(isolated, BOOL, INIT_ONLY), |
229 | | SPEC(parse_argv, BOOL, INIT_ONLY), |
230 | | SPEC(use_environment, BOOL, INIT_ONLY), |
231 | | |
232 | | // Array terminator |
233 | | {NULL, 0, 0, 0, NO_SYS}, |
234 | | }; |
235 | | |
236 | | #undef SPEC |
237 | | #undef SYS_ATTR |
238 | | #undef SYS_FLAG_SETTER |
239 | | #undef SYS_FLAG |
240 | | #undef NO_SYS |
241 | | |
242 | | |
243 | | // Forward declarations |
244 | | static PyObject* config_get(const PyConfig *config, const PyConfigSpec *spec, |
245 | | int use_sys); |
246 | | static void initconfig_free_wstr(wchar_t *member); |
247 | | static void initconfig_free_wstr_list(PyWideStringList *list); |
248 | | static void initconfig_free_config(const PyConfig *config); |
249 | | |
250 | | |
251 | | /* --- Command line options --------------------------------------- */ |
252 | | |
253 | | /* Short usage message (with %s for argv0) */ |
254 | | static const char usage_line[] = |
255 | | "usage: %ls [option] ... [-c cmd | -m mod | file | -] [arg] ...\n"; |
256 | | |
257 | | /* Long help message */ |
258 | | /* Lines sorted by option name; keep in sync with usage_envvars* below */ |
259 | | static const char usage_help[] = "\ |
260 | | Options (and corresponding environment variables):\n\ |
261 | | -b : issue warnings about converting bytes/bytearray to str and comparing\n\ |
262 | | bytes/bytearray with str or bytes with int. (-bb: issue errors)\n\ |
263 | | deprecated since 3.15 and will become no-op in 3.17.\n\ |
264 | | -B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x\n\ |
265 | | -c cmd : program passed in as string (terminates option list)\n\ |
266 | | -d : turn on parser debugging output (for experts only, only works on\n\ |
267 | | debug builds); also PYTHONDEBUG=x\n\ |
268 | | -E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\ |
269 | | -h : print this help message and exit (also -? or --help)\n\ |
270 | | -i : inspect interactively after running script; forces a prompt even\n\ |
271 | | if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\ |
272 | | -I : isolate Python from the user's environment (implies -E, -P and -s)\n\ |
273 | | -m mod : run library module as a script (terminates option list)\n\ |
274 | | -O : remove assert and __debug__-dependent statements; add .opt-1 before\n\ |
275 | | .pyc extension; also PYTHONOPTIMIZE=x\n\ |
276 | | -OO : do -O changes and also discard docstrings; add .opt-2 before\n\ |
277 | | .pyc extension\n\ |
278 | | -P : don't prepend a potentially unsafe path to sys.path; also\n\ |
279 | | PYTHONSAFEPATH\n\ |
280 | | -q : don't print version and copyright messages on interactive startup\n\ |
281 | | -s : don't add user site directory to sys.path; also PYTHONNOUSERSITE=x\n\ |
282 | | -S : don't imply 'import site' on initialization\n\ |
283 | | -u : force the stdout and stderr streams to be unbuffered;\n\ |
284 | | this option has no effect on stdin; also PYTHONUNBUFFERED=x\n\ |
285 | | -v : verbose (trace import statements); also PYTHONVERBOSE=x\n\ |
286 | | can be supplied multiple times to increase verbosity\n\ |
287 | | -V : print the Python version number and exit (also --version)\n\ |
288 | | when given twice, print more information about the build\n\ |
289 | | -W arg : warning control; arg is action:message:category:module:lineno\n\ |
290 | | also PYTHONWARNINGS=arg\n\ |
291 | | -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\ |
292 | | -X opt : set implementation-specific option\n\ |
293 | | --check-hash-based-pycs always|default|never:\n\ |
294 | | control how Python invalidates hash-based .pyc files\n\ |
295 | | --help-env: print help about Python environment variables and exit\n\ |
296 | | --help-xoptions: print help about implementation-specific -X options and exit\n\ |
297 | | --help-all: print complete help information and exit\n\ |
298 | | \n\ |
299 | | Arguments:\n\ |
300 | | file : program read from script file\n\ |
301 | | - : program read from stdin (default; interactive mode if a tty)\n\ |
302 | | arg ...: arguments passed to program in sys.argv[1:]\n\ |
303 | | "; |
304 | | |
305 | | static const char usage_xoptions[] = "\ |
306 | | The following implementation-specific options are available:\n\ |
307 | | -X cpu_count=N: override the return value of os.cpu_count();\n\ |
308 | | -X cpu_count=default cancels overriding; also PYTHON_CPU_COUNT\n\ |
309 | | -X dev : enable Python Development Mode; also PYTHONDEVMODE\n\ |
310 | | -X faulthandler: dump the Python traceback on fatal errors;\n\ |
311 | | also PYTHONFAULTHANDLER\n\ |
312 | | -X frozen_modules=[on|off]: whether to use frozen modules; the default is \"on\"\n\ |
313 | | for installed Python and \"off\" for a local build;\n\ |
314 | | also PYTHON_FROZEN_MODULES\n\ |
315 | | " |
316 | | #ifdef Py_GIL_DISABLED |
317 | | "-X gil=[0|1]: enable (1) or disable (0) the GIL; also PYTHON_GIL\n" |
318 | | #endif |
319 | | "\ |
320 | | -X importtime[=2]: show how long each import takes; use -X importtime=2 to\n\ |
321 | | log imports of already-loaded modules; also PYTHONPROFILEIMPORTTIME\n\ |
322 | | -X lazy_imports=[all|none|normal]: control global lazy imports;\n\ |
323 | | default is normal; also PYTHON_LAZY_IMPORTS\n\ |
324 | | -X int_max_str_digits=N: limit the size of int<->str conversions;\n\ |
325 | | 0 disables the limit; also PYTHONINTMAXSTRDIGITS\n\ |
326 | | -X no_debug_ranges: don't include extra location information in code objects;\n\ |
327 | | also PYTHONNODEBUGRANGES\n\ |
328 | | -X perf: support the Linux \"perf\" profiler; also PYTHONPERFSUPPORT=1\n\ |
329 | | -X perf_jit: support the Linux \"perf\" profiler with DWARF support;\n\ |
330 | | also PYTHON_PERF_JIT_SUPPORT=1\n\ |
331 | | -X disable-remote-debug: disable remote debugging; also PYTHON_DISABLE_REMOTE_DEBUG\n\ |
332 | | " |
333 | | #ifdef Py_DEBUG |
334 | | "-X presite=MOD: import this module before site; also PYTHON_PRESITE\n" |
335 | | #endif |
336 | | "\ |
337 | | -X pycache_prefix=PATH: write .pyc files to a parallel tree instead of to the\n\ |
338 | | code tree; also PYTHONPYCACHEPREFIX\n\ |
339 | | " |
340 | | #ifdef Py_STATS |
341 | | "-X pystats: enable pystats collection at startup; also PYTHONSTATS\n" |
342 | | #endif |
343 | | "\ |
344 | | -X showrefcount: output the total reference count and number of used\n\ |
345 | | memory blocks when the program finishes or after each statement in\n\ |
346 | | the interactive interpreter; only works on debug builds\n" |
347 | | #ifdef Py_GIL_DISABLED |
348 | | "-X tlbc=[0|1]: enable (1) or disable (0) thread-local bytecode. Also\n\ |
349 | | PYTHON_TLBC\n" |
350 | | #endif |
351 | | "\ |
352 | | -X thread_inherit_context=[0|1]: enable (1) or disable (0) threads inheriting\n\ |
353 | | context vars by default; enabled by default in the free-threaded\n\ |
354 | | build and disabled otherwise; also PYTHON_THREAD_INHERIT_CONTEXT\n\ |
355 | | -X context_aware_warnings=[0|1]: if true (1) then the warnings module will\n\ |
356 | | use a context variables; if false (0) then the warnings module will\n\ |
357 | | use module globals, which is not concurrent-safe; set to true for\n\ |
358 | | free-threaded builds and false otherwise; also\n\ |
359 | | PYTHON_CONTEXT_AWARE_WARNINGS\n\ |
360 | | -X pathconfig_warnings=[0|1]: if true (1) then path configuration is allowed\n\ |
361 | | to log warnings into stderr; if false (0) suppress these warnings;\n\ |
362 | | set to true by default; also PYTHON_PATHCONFIG_WARNINGS\n\ |
363 | | -X tracemalloc[=N]: trace Python memory allocations; N sets a traceback limit\n \ |
364 | | of N frames (default: 1); also PYTHONTRACEMALLOC=N\n\ |
365 | | -X utf8[=0|1]: enable (1) or disable (0) UTF-8 mode; also PYTHONUTF8\n\ |
366 | | -X warn_default_encoding: enable opt-in EncodingWarning for 'encoding=None';\n\ |
367 | | also PYTHONWARNDEFAULTENCODING\ |
368 | | "; |
369 | | |
370 | | /* Envvars that don't have equivalent command-line options are listed first */ |
371 | | static const char usage_envvars[] = |
372 | | "Environment variables that change behavior:\n" |
373 | | "PYTHONSTARTUP : file executed on interactive startup (no default)\n" |
374 | | "PYTHONPATH : '%lc'-separated list of directories prefixed to the\n" |
375 | | " default module search path. The result is sys.path.\n" |
376 | | "PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n" |
377 | | " The default module search path uses %s.\n" |
378 | | "PYTHONPLATLIBDIR: override sys.platlibdir\n" |
379 | | "PYTHONCASEOK : ignore case in 'import' statements (Windows)\n" |
380 | | "PYTHONIOENCODING: encoding[:errors] used for stdin/stdout/stderr\n" |
381 | | "PYTHONHASHSEED : if this variable is set to 'random', a random value is used\n" |
382 | | " to seed the hashes of str and bytes objects. It can also be\n" |
383 | | " set to an integer in the range [0,4294967295] to get hash\n" |
384 | | " values with a predictable seed.\n" |
385 | | "PYTHONMALLOC : set the Python memory allocators and/or install debug hooks\n" |
386 | | " on Python memory allocators. Use PYTHONMALLOC=debug to\n" |
387 | | " install debug hooks.\n" |
388 | | "PYTHONMALLOCSTATS: print memory allocator statistics\n" |
389 | | "PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale\n" |
390 | | " coercion behavior. Use PYTHONCOERCECLOCALE=warn to request\n" |
391 | | " display of locale coercion and locale compatibility warnings\n" |
392 | | " on stderr.\n" |
393 | | "PYTHONBREAKPOINT: if this variable is set to 0, it disables the default\n" |
394 | | " debugger. It can be set to the callable of your debugger of\n" |
395 | | " choice.\n" |
396 | | "PYTHON_COLORS : if this variable is set to 1, the interpreter will colorize\n" |
397 | | " various kinds of output. Setting it to 0 deactivates\n" |
398 | | " this behavior.\n" |
399 | | "PYTHON_HISTORY : the location of a .python_history file.\n" |
400 | | "PYTHONASYNCIODEBUG: enable asyncio debug mode\n" |
401 | | #ifdef Py_TRACE_REFS |
402 | | "PYTHONDUMPREFS : dump objects and reference counts still alive after shutdown\n" |
403 | | "PYTHONDUMPREFSFILE: dump objects and reference counts to the specified file\n" |
404 | | #endif |
405 | | #ifdef __APPLE__ |
406 | | "PYTHONEXECUTABLE: set sys.argv[0] to this value (macOS only)\n" |
407 | | #endif |
408 | | #ifdef MS_WINDOWS |
409 | | "PYTHONLEGACYWINDOWSFSENCODING: use legacy \"mbcs\" encoding for file system\n" |
410 | | "PYTHONLEGACYWINDOWSSTDIO: use legacy Windows stdio\n" |
411 | | #endif |
412 | | "PYTHONUSERBASE : defines the user base directory (site.USER_BASE)\n" |
413 | | "PYTHON_BASIC_REPL: use the traditional parser-based REPL\n" |
414 | | "\n" |
415 | | "These variables have equivalent command-line options (see --help for details):\n" |
416 | | "PYTHON_CPU_COUNT: override the return value of os.cpu_count() (-X cpu_count)\n" |
417 | | "PYTHONDEBUG : enable parser debug mode (-d)\n" |
418 | | "PYTHONDEVMODE : enable Python Development Mode (-X dev)\n" |
419 | | "PYTHONDONTWRITEBYTECODE: don't write .pyc files (-B)\n" |
420 | | "PYTHONFAULTHANDLER: dump the Python traceback on fatal errors (-X faulthandler)\n" |
421 | | "PYTHON_FROZEN_MODULES: whether to use frozen modules; the default is \"on\"\n" |
422 | | " for installed Python and \"off\" for a local build\n" |
423 | | " (-X frozen_modules)\n" |
424 | | #ifdef Py_GIL_DISABLED |
425 | | "PYTHON_GIL : when set to 0, disables the GIL (-X gil)\n" |
426 | | #endif |
427 | | "PYTHONINSPECT : inspect interactively after running script (-i)\n" |
428 | | "PYTHONINTMAXSTRDIGITS: limit the size of int<->str conversions;\n" |
429 | | " 0 disables the limit (-X int_max_str_digits=N)\n" |
430 | | "PYTHONNODEBUGRANGES: don't include extra location information in code objects\n" |
431 | | " (-X no_debug_ranges)\n" |
432 | | "PYTHONNOUSERSITE: disable user site directory (-s)\n" |
433 | | "PYTHONOPTIMIZE : enable level 1 optimizations (-O)\n" |
434 | | "PYTHONPERFSUPPORT: support the Linux \"perf\" profiler (-X perf)\n" |
435 | | "PYTHON_PERF_JIT_SUPPORT: enable Linux \"perf\" profiler support with JIT\n" |
436 | | " (-X perf_jit)\n" |
437 | | #ifdef Py_DEBUG |
438 | | "PYTHON_PRESITE: import this module before site (-X presite)\n" |
439 | | #endif |
440 | | "PYTHONPROFILEIMPORTTIME: show how long each import takes (-X importtime)\n" |
441 | | "PYTHON_LAZY_IMPORTS: control global lazy imports (-X lazy_imports)\n" |
442 | | "PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files\n" |
443 | | " (-X pycache_prefix)\n" |
444 | | "PYTHONSAFEPATH : don't prepend a potentially unsafe path to sys.path.\n" |
445 | | #ifdef Py_STATS |
446 | | "PYTHONSTATS : turns on statistics gathering (-X pystats)\n" |
447 | | #endif |
448 | | #ifdef Py_GIL_DISABLED |
449 | | "PYTHON_TLBC : when set to 0, disables thread-local bytecode (-X tlbc)\n" |
450 | | #endif |
451 | | "PYTHON_THREAD_INHERIT_CONTEXT: if true (1), threads inherit context vars\n" |
452 | | " (-X thread_inherit_context)\n" |
453 | | "PYTHON_CONTEXT_AWARE_WARNINGS: if true (1), enable thread-safe warnings module\n" |
454 | | " behaviour (-X context_aware_warnings)\n" |
455 | | "PYTHONTRACEMALLOC: trace Python memory allocations (-X tracemalloc)\n" |
456 | | "PYTHONUNBUFFERED: disable stdout/stderr buffering (-u)\n" |
457 | | "PYTHONUTF8 : control the UTF-8 mode (-X utf8)\n" |
458 | | "PYTHONVERBOSE : trace import statements (-v)\n" |
459 | | "PYTHONWARNDEFAULTENCODING: enable opt-in EncodingWarning for 'encoding=None'\n" |
460 | | " (-X warn_default_encoding)\n" |
461 | | "PYTHONWARNINGS : warning control (-W)\n" |
462 | | ; |
463 | | |
464 | | #if defined(MS_WINDOWS) |
465 | | # define PYTHONHOMEHELP "<prefix>\\python{major}{minor}" |
466 | | #else |
467 | 0 | # define PYTHONHOMEHELP "<prefix>/lib/pythonX.X" |
468 | | #endif |
469 | | |
470 | | |
471 | | /* --- Global configuration variables ----------------------------- */ |
472 | | |
473 | | /* UTF-8 mode (PEP 540): if equal to 1, use the UTF-8 encoding, and change |
474 | | stdin and stdout error handler to "surrogateescape". */ |
475 | | int Py_UTF8Mode = 0; |
476 | | int Py_DebugFlag = 0; /* Needed by parser.c */ |
477 | | int Py_VerboseFlag = 0; /* Needed by import.c */ |
478 | | int Py_QuietFlag = 0; /* Needed by sysmodule.c */ |
479 | | int Py_InteractiveFlag = 0; /* Previously, was used by Py_FdIsInteractive() */ |
480 | | int Py_InspectFlag = 0; /* Needed to determine whether to exit at SystemExit */ |
481 | | int Py_OptimizeFlag = 0; /* Needed by compile.c */ |
482 | | int Py_NoSiteFlag = 0; /* Suppress 'import site' */ |
483 | | int Py_BytesWarningFlag = 0; /* Warn on str(bytes) and str(buffer) */ |
484 | | int Py_FrozenFlag = 0; /* Needed by getpath.c */ |
485 | | int Py_IgnoreEnvironmentFlag = 0; /* e.g. PYTHONPATH, PYTHONHOME */ |
486 | | int Py_DontWriteBytecodeFlag = 0; /* Suppress writing bytecode files (*.pyc) */ |
487 | | int Py_NoUserSiteDirectory = 0; /* for -s and site.py */ |
488 | | int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */ |
489 | | int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */ |
490 | | int Py_IsolatedFlag = 0; /* for -I, isolate from user's env */ |
491 | | #ifdef MS_WINDOWS |
492 | | int Py_LegacyWindowsFSEncodingFlag = 0; /* Uses mbcs instead of utf-8 */ |
493 | | int Py_LegacyWindowsStdioFlag = 0; /* Uses FileIO instead of WindowsConsoleIO */ |
494 | | #endif |
495 | | |
496 | | |
497 | | static PyObject * |
498 | | _Py_GetGlobalVariablesAsDict(void) |
499 | 0 | { |
500 | 0 | _Py_COMP_DIAG_PUSH |
501 | 0 | _Py_COMP_DIAG_IGNORE_DEPR_DECLS |
502 | 0 | PyObject *dict, *obj; |
503 | |
|
504 | 0 | dict = PyDict_New(); |
505 | 0 | if (dict == NULL) { |
506 | 0 | return NULL; |
507 | 0 | } |
508 | | |
509 | 0 | #define SET_ITEM(KEY, EXPR) \ |
510 | 0 | do { \ |
511 | 0 | obj = (EXPR); \ |
512 | 0 | if (obj == NULL) { \ |
513 | 0 | return NULL; \ |
514 | 0 | } \ |
515 | 0 | int res = PyDict_SetItemString(dict, (KEY), obj); \ |
516 | 0 | Py_DECREF(obj); \ |
517 | 0 | if (res < 0) { \ |
518 | 0 | goto fail; \ |
519 | 0 | } \ |
520 | 0 | } while (0) |
521 | 0 | #define SET_ITEM_INT(VAR) \ |
522 | 0 | SET_ITEM(#VAR, PyLong_FromLong(VAR)) |
523 | 0 | #define FROM_STRING(STR) \ |
524 | 0 | ((STR != NULL) ? \ |
525 | 0 | PyUnicode_FromString(STR) \ |
526 | 0 | : Py_NewRef(Py_None)) |
527 | 0 | #define SET_ITEM_STR(VAR) \ |
528 | 0 | SET_ITEM(#VAR, FROM_STRING(VAR)) |
529 | | |
530 | 0 | SET_ITEM_STR(Py_FileSystemDefaultEncoding); |
531 | 0 | SET_ITEM_INT(Py_HasFileSystemDefaultEncoding); |
532 | 0 | SET_ITEM_STR(Py_FileSystemDefaultEncodeErrors); |
533 | 0 | SET_ITEM_INT(_Py_HasFileSystemDefaultEncodeErrors); |
534 | | |
535 | 0 | SET_ITEM_INT(Py_UTF8Mode); |
536 | 0 | SET_ITEM_INT(Py_DebugFlag); |
537 | 0 | SET_ITEM_INT(Py_VerboseFlag); |
538 | 0 | SET_ITEM_INT(Py_QuietFlag); |
539 | 0 | SET_ITEM_INT(Py_InteractiveFlag); |
540 | 0 | SET_ITEM_INT(Py_InspectFlag); |
541 | | |
542 | 0 | SET_ITEM_INT(Py_OptimizeFlag); |
543 | 0 | SET_ITEM_INT(Py_NoSiteFlag); |
544 | 0 | SET_ITEM_INT(Py_BytesWarningFlag); |
545 | 0 | SET_ITEM_INT(Py_FrozenFlag); |
546 | 0 | SET_ITEM_INT(Py_IgnoreEnvironmentFlag); |
547 | 0 | SET_ITEM_INT(Py_DontWriteBytecodeFlag); |
548 | 0 | SET_ITEM_INT(Py_NoUserSiteDirectory); |
549 | 0 | SET_ITEM_INT(Py_UnbufferedStdioFlag); |
550 | 0 | SET_ITEM_INT(Py_HashRandomizationFlag); |
551 | 0 | SET_ITEM_INT(Py_IsolatedFlag); |
552 | | |
553 | | #ifdef MS_WINDOWS |
554 | | SET_ITEM_INT(Py_LegacyWindowsFSEncodingFlag); |
555 | | SET_ITEM_INT(Py_LegacyWindowsStdioFlag); |
556 | | #endif |
557 | | |
558 | 0 | return dict; |
559 | | |
560 | 0 | fail: |
561 | 0 | Py_DECREF(dict); |
562 | 0 | return NULL; |
563 | |
|
564 | 0 | #undef FROM_STRING |
565 | 0 | #undef SET_ITEM |
566 | 0 | #undef SET_ITEM_INT |
567 | 0 | #undef SET_ITEM_STR |
568 | 0 | _Py_COMP_DIAG_POP |
569 | 0 | } |
570 | | |
571 | | char* |
572 | | Py_GETENV(const char *name) |
573 | 198 | { |
574 | 198 | _Py_COMP_DIAG_PUSH |
575 | 198 | _Py_COMP_DIAG_IGNORE_DEPR_DECLS |
576 | 198 | if (Py_IgnoreEnvironmentFlag) { |
577 | 0 | return NULL; |
578 | 0 | } |
579 | 198 | return getenv(name); |
580 | 198 | _Py_COMP_DIAG_POP |
581 | 198 | } |
582 | | |
583 | | /* --- PyStatus ----------------------------------------------- */ |
584 | | |
585 | | PyStatus PyStatus_Ok(void) |
586 | 66 | { return _PyStatus_OK(); } |
587 | | |
588 | | PyStatus PyStatus_Error(const char *err_msg) |
589 | 0 | { |
590 | 0 | assert(err_msg != NULL); |
591 | 0 | return (PyStatus){._type = _PyStatus_TYPE_ERROR, |
592 | 0 | .err_msg = err_msg}; |
593 | 0 | } |
594 | | |
595 | | PyStatus PyStatus_NoMemory(void) |
596 | 0 | { return PyStatus_Error("memory allocation failed"); } |
597 | | |
598 | | PyStatus PyStatus_Exit(int exitcode) |
599 | 0 | { return _PyStatus_EXIT(exitcode); } |
600 | | |
601 | | |
602 | | int PyStatus_IsError(PyStatus status) |
603 | 0 | { return _PyStatus_IS_ERROR(status); } |
604 | | |
605 | | int PyStatus_IsExit(PyStatus status) |
606 | 0 | { return _PyStatus_IS_EXIT(status); } |
607 | | |
608 | | int PyStatus_Exception(PyStatus status) |
609 | 44 | { return _PyStatus_EXCEPTION(status); } |
610 | | |
611 | | void |
612 | | _PyErr_SetFromPyStatus(PyStatus status) |
613 | 0 | { |
614 | 0 | if (!_PyStatus_IS_ERROR(status)) { |
615 | 0 | PyErr_Format(PyExc_SystemError, |
616 | 0 | "_PyErr_SetFromPyStatus() status is not an error"); |
617 | 0 | return; |
618 | 0 | } |
619 | | |
620 | 0 | const char *err_msg = status.err_msg; |
621 | 0 | if (err_msg == NULL || strlen(err_msg) == 0) { |
622 | 0 | PyErr_Format(PyExc_SystemError, |
623 | 0 | "_PyErr_SetFromPyStatus() status has no error message"); |
624 | 0 | return; |
625 | 0 | } |
626 | | |
627 | 0 | if (strcmp(err_msg, _PyStatus_NO_MEMORY_ERRMSG) == 0) { |
628 | 0 | PyErr_NoMemory(); |
629 | 0 | return; |
630 | 0 | } |
631 | | |
632 | 0 | const char *func = status.func; |
633 | 0 | if (func) { |
634 | 0 | PyErr_Format(PyExc_RuntimeError, "%s: %s", func, err_msg); |
635 | 0 | } |
636 | 0 | else { |
637 | 0 | PyErr_Format(PyExc_RuntimeError, "%s", err_msg); |
638 | 0 | } |
639 | 0 | } |
640 | | |
641 | | |
642 | | /* --- PyWideStringList ------------------------------------------------ */ |
643 | | |
644 | | #ifndef NDEBUG |
645 | | int |
646 | | _PyWideStringList_CheckConsistency(const PyWideStringList *list) |
647 | 2.35k | { |
648 | 2.35k | assert(list->length >= 0); |
649 | 2.35k | if (list->length != 0) { |
650 | 264 | assert(list->items != NULL); |
651 | 264 | } |
652 | 2.75k | for (Py_ssize_t i = 0; i < list->length; i++) { |
653 | 396 | assert(list->items[i] != NULL); |
654 | 396 | } |
655 | 2.35k | return 1; |
656 | 2.35k | } |
657 | | #endif /* Py_DEBUG */ |
658 | | |
659 | | |
660 | | static void |
661 | | _PyWideStringList_ClearEx(PyWideStringList *list, |
662 | | bool use_default_allocator) |
663 | 1.18k | { |
664 | 1.18k | assert(_PyWideStringList_CheckConsistency(list)); |
665 | 1.32k | for (Py_ssize_t i=0; i < list->length; i++) { |
666 | 132 | if (use_default_allocator) { |
667 | 0 | _PyMem_DefaultRawFree(list->items[i]); |
668 | 0 | } |
669 | 132 | else { |
670 | 132 | PyMem_RawFree(list->items[i]); |
671 | 132 | } |
672 | 132 | } |
673 | 1.18k | if (use_default_allocator) { |
674 | 22 | _PyMem_DefaultRawFree(list->items); |
675 | 22 | } |
676 | 1.16k | else { |
677 | 1.16k | PyMem_RawFree(list->items); |
678 | 1.16k | } |
679 | 1.18k | list->length = 0; |
680 | 1.18k | list->items = NULL; |
681 | 1.18k | } |
682 | | |
683 | | void |
684 | | _PyWideStringList_Clear(PyWideStringList *list) |
685 | 748 | { |
686 | 748 | _PyWideStringList_ClearEx(list, false); |
687 | 748 | } |
688 | | |
689 | | static int |
690 | | _PyWideStringList_CopyEx(PyWideStringList *list, |
691 | | const PyWideStringList *list2, |
692 | | bool use_default_allocator) |
693 | 440 | { |
694 | 440 | assert(_PyWideStringList_CheckConsistency(list)); |
695 | 440 | assert(_PyWideStringList_CheckConsistency(list2)); |
696 | | |
697 | 440 | if (list2->length == 0) { |
698 | 374 | _PyWideStringList_ClearEx(list, use_default_allocator); |
699 | 374 | return 0; |
700 | 374 | } |
701 | | |
702 | 66 | PyWideStringList copy = _PyWideStringList_INIT; |
703 | | |
704 | 66 | size_t size = list2->length * sizeof(list2->items[0]); |
705 | 66 | if (use_default_allocator) { |
706 | 0 | copy.items = _PyMem_DefaultRawMalloc(size); |
707 | 0 | } |
708 | 66 | else { |
709 | 66 | copy.items = PyMem_RawMalloc(size); |
710 | 66 | } |
711 | 66 | if (copy.items == NULL) { |
712 | 0 | return -1; |
713 | 0 | } |
714 | | |
715 | 176 | for (Py_ssize_t i=0; i < list2->length; i++) { |
716 | 110 | wchar_t *item; |
717 | 110 | if (use_default_allocator) { |
718 | 0 | item = _PyMem_DefaultRawWcsdup(list2->items[i]); |
719 | 0 | } |
720 | 110 | else { |
721 | 110 | item = _PyMem_RawWcsdup(list2->items[i]); |
722 | 110 | } |
723 | 110 | if (item == NULL) { |
724 | 0 | _PyWideStringList_ClearEx(©, use_default_allocator); |
725 | 0 | return -1; |
726 | 0 | } |
727 | 110 | copy.items[i] = item; |
728 | 110 | copy.length = i + 1; |
729 | 110 | } |
730 | | |
731 | 66 | _PyWideStringList_ClearEx(list, use_default_allocator); |
732 | 66 | *list = copy; |
733 | 66 | return 0; |
734 | 66 | } |
735 | | |
736 | | int |
737 | | _PyWideStringList_Copy(PyWideStringList *list, const PyWideStringList *list2) |
738 | 418 | { |
739 | 418 | return _PyWideStringList_CopyEx(list, list2, false); |
740 | 418 | } |
741 | | |
742 | | PyStatus |
743 | | PyWideStringList_Insert(PyWideStringList *list, |
744 | | Py_ssize_t index, const wchar_t *item) |
745 | 110 | { |
746 | 110 | Py_ssize_t len = list->length; |
747 | 110 | if (len == PY_SSIZE_T_MAX) { |
748 | | /* length+1 would overflow */ |
749 | 0 | return _PyStatus_NO_MEMORY(); |
750 | 0 | } |
751 | 110 | if (index < 0) { |
752 | 0 | return _PyStatus_ERR("PyWideStringList_Insert index must be >= 0"); |
753 | 0 | } |
754 | 110 | if (index > len) { |
755 | 0 | index = len; |
756 | 0 | } |
757 | | |
758 | 110 | wchar_t *item2 = _PyMem_RawWcsdup(item); |
759 | 110 | if (item2 == NULL) { |
760 | 0 | return _PyStatus_NO_MEMORY(); |
761 | 0 | } |
762 | | |
763 | 110 | size_t size = (len + 1) * sizeof(list->items[0]); |
764 | 110 | wchar_t **items2 = (wchar_t **)PyMem_RawRealloc(list->items, size); |
765 | 110 | if (items2 == NULL) { |
766 | 0 | PyMem_RawFree(item2); |
767 | 0 | return _PyStatus_NO_MEMORY(); |
768 | 0 | } |
769 | | |
770 | 110 | if (index < len) { |
771 | 0 | memmove(&items2[index + 1], |
772 | 0 | &items2[index], |
773 | 0 | (len - index) * sizeof(items2[0])); |
774 | 0 | } |
775 | | |
776 | 110 | items2[index] = item2; |
777 | 110 | list->items = items2; |
778 | 110 | list->length++; |
779 | 110 | return _PyStatus_OK(); |
780 | 110 | } |
781 | | |
782 | | |
783 | | PyStatus |
784 | | PyWideStringList_Append(PyWideStringList *list, const wchar_t *item) |
785 | 110 | { |
786 | 110 | return PyWideStringList_Insert(list, list->length, item); |
787 | 110 | } |
788 | | |
789 | | |
790 | | PyStatus |
791 | | _PyWideStringList_Extend(PyWideStringList *list, const PyWideStringList *list2) |
792 | 44 | { |
793 | 44 | for (Py_ssize_t i = 0; i < list2->length; i++) { |
794 | 0 | PyStatus status = PyWideStringList_Append(list, list2->items[i]); |
795 | 0 | if (_PyStatus_EXCEPTION(status)) { |
796 | 0 | return status; |
797 | 0 | } |
798 | 0 | } |
799 | 44 | return _PyStatus_OK(); |
800 | 44 | } |
801 | | |
802 | | |
803 | | static int |
804 | | _PyWideStringList_Find(PyWideStringList *list, const wchar_t *item) |
805 | 0 | { |
806 | 0 | for (Py_ssize_t i = 0; i < list->length; i++) { |
807 | 0 | if (wcscmp(list->items[i], item) == 0) { |
808 | 0 | return 1; |
809 | 0 | } |
810 | 0 | } |
811 | 0 | return 0; |
812 | 0 | } |
813 | | |
814 | | |
815 | | PyObject* |
816 | | _PyWideStringList_AsList(const PyWideStringList *list) |
817 | 88 | { |
818 | 88 | assert(_PyWideStringList_CheckConsistency(list)); |
819 | | |
820 | 88 | PyObject *pylist = PyList_New(list->length); |
821 | 88 | if (pylist == NULL) { |
822 | 0 | return NULL; |
823 | 0 | } |
824 | | |
825 | 176 | for (Py_ssize_t i = 0; i < list->length; i++) { |
826 | 88 | PyObject *item = PyUnicode_FromWideChar(list->items[i], -1); |
827 | 88 | if (item == NULL) { |
828 | 0 | Py_DECREF(pylist); |
829 | 0 | return NULL; |
830 | 0 | } |
831 | 88 | PyList_SET_ITEM(pylist, i, item); |
832 | 88 | } |
833 | 88 | return pylist; |
834 | 88 | } |
835 | | |
836 | | |
837 | | static PyObject* |
838 | | _PyWideStringList_AsTuple(const PyWideStringList *list) |
839 | 88 | { |
840 | 88 | assert(_PyWideStringList_CheckConsistency(list)); |
841 | | |
842 | 88 | PyObject *tuple = PyTuple_New(list->length); |
843 | 88 | if (tuple == NULL) { |
844 | 0 | return NULL; |
845 | 0 | } |
846 | | |
847 | 110 | for (Py_ssize_t i = 0; i < list->length; i++) { |
848 | 22 | PyObject *item = PyUnicode_FromWideChar(list->items[i], -1); |
849 | 22 | if (item == NULL) { |
850 | 0 | Py_DECREF(tuple); |
851 | 0 | return NULL; |
852 | 0 | } |
853 | 22 | PyTuple_SET_ITEM(tuple, i, item); |
854 | 22 | } |
855 | 88 | return tuple; |
856 | 88 | } |
857 | | |
858 | | |
859 | | /* --- Py_GetArgcArgv() ------------------------------------------- */ |
860 | | |
861 | | void |
862 | | _Py_ClearArgcArgv(void) |
863 | 0 | { |
864 | 0 | _PyWideStringList_ClearEx(&_PyRuntime.orig_argv, true); |
865 | 0 | } |
866 | | |
867 | | |
868 | | static int |
869 | | _Py_SetArgcArgv(Py_ssize_t argc, wchar_t * const *argv) |
870 | 22 | { |
871 | 22 | const PyWideStringList argv_list = {.length = argc, .items = (wchar_t **)argv}; |
872 | | |
873 | | // XXX _PyRuntime.orig_argv only gets cleared by Py_Main(), |
874 | | // so it currently leaks for embedders. |
875 | 22 | return _PyWideStringList_CopyEx(&_PyRuntime.orig_argv, &argv_list, true); |
876 | 22 | } |
877 | | |
878 | | |
879 | | // _PyConfig_Write() calls _Py_SetArgcArgv() with PyConfig.orig_argv. |
880 | | void |
881 | | Py_GetArgcArgv(int *argc, wchar_t ***argv) |
882 | 0 | { |
883 | 0 | *argc = (int)_PyRuntime.orig_argv.length; |
884 | 0 | *argv = _PyRuntime.orig_argv.items; |
885 | 0 | } |
886 | | |
887 | | |
888 | | /* --- PyConfig ---------------------------------------------- */ |
889 | | |
890 | 22 | #define MAX_HASH_SEED 4294967295UL |
891 | | |
892 | | |
893 | | #ifndef NDEBUG |
894 | | static int |
895 | | config_check_consistency(const PyConfig *config) |
896 | 22 | { |
897 | | /* Check config consistency */ |
898 | 22 | assert(config->isolated >= 0); |
899 | 22 | assert(config->use_environment >= 0); |
900 | 22 | assert(config->dev_mode >= 0); |
901 | 22 | assert(config->install_signal_handlers >= 0); |
902 | 22 | assert(config->use_hash_seed >= 0); |
903 | 22 | assert(config->hash_seed <= MAX_HASH_SEED); |
904 | 22 | assert(config->faulthandler >= 0); |
905 | 22 | assert(config->tracemalloc >= 0); |
906 | 22 | assert(config->import_time >= 0); |
907 | 22 | assert(config->code_debug_ranges >= 0); |
908 | 22 | assert(config->show_ref_count >= 0); |
909 | 22 | assert(config->dump_refs >= 0); |
910 | 22 | assert(config->malloc_stats >= 0); |
911 | 22 | assert(config->pymalloc_hugepages >= 0); |
912 | 22 | assert(config->site_import >= 0); |
913 | 22 | assert(config->bytes_warning >= 0); |
914 | 22 | assert(config->warn_default_encoding >= 0); |
915 | 22 | assert(config->inspect >= 0); |
916 | 22 | assert(config->interactive >= 0); |
917 | 22 | assert(config->optimization_level >= 0); |
918 | 22 | assert(config->parser_debug >= 0); |
919 | 22 | assert(config->write_bytecode >= 0); |
920 | 22 | assert(config->verbose >= 0); |
921 | 22 | assert(config->quiet >= 0); |
922 | 22 | assert(config->user_site_directory >= 0); |
923 | 22 | assert(config->parse_argv >= 0); |
924 | 22 | assert(config->configure_c_stdio >= 0); |
925 | 22 | assert(config->buffered_stdio >= 0); |
926 | 22 | assert(_PyWideStringList_CheckConsistency(&config->orig_argv)); |
927 | 22 | assert(_PyWideStringList_CheckConsistency(&config->argv)); |
928 | | /* sys.argv must be non-empty: empty argv is replaced with [''] */ |
929 | 22 | assert(config->argv.length >= 1); |
930 | 22 | assert(_PyWideStringList_CheckConsistency(&config->xoptions)); |
931 | 22 | assert(_PyWideStringList_CheckConsistency(&config->warnoptions)); |
932 | 22 | assert(_PyWideStringList_CheckConsistency(&config->module_search_paths)); |
933 | 22 | assert(config->module_search_paths_set >= 0); |
934 | 22 | assert(config->filesystem_encoding != NULL); |
935 | 22 | assert(config->filesystem_errors != NULL); |
936 | 22 | assert(config->stdio_encoding != NULL); |
937 | 22 | assert(config->stdio_errors != NULL); |
938 | | #ifdef MS_WINDOWS |
939 | | assert(config->legacy_windows_stdio >= 0); |
940 | | #endif |
941 | | /* -c and -m options are exclusive */ |
942 | 22 | assert(!(config->run_command != NULL && config->run_module != NULL)); |
943 | 22 | assert(config->check_hash_pycs_mode != NULL); |
944 | 22 | assert(config->_install_importlib >= 0); |
945 | 22 | assert(config->pathconfig_warnings >= 0); |
946 | 22 | assert(config->_is_python_build >= 0); |
947 | 22 | assert(config->safe_path >= 0); |
948 | 22 | assert(config->int_max_str_digits >= 0); |
949 | | // cpu_count can be -1 if the user doesn't override it. |
950 | 22 | assert(config->cpu_count != 0); |
951 | | // lazy_imports can be -1 (default), 0 (off), or 1 (on). |
952 | 22 | assert(config->lazy_imports >= -1 && config->lazy_imports <= 1); |
953 | | // config->use_frozen_modules is initialized later |
954 | | // by _PyConfig_InitImportConfig(). |
955 | 22 | assert(config->thread_inherit_context >= 0); |
956 | 22 | assert(config->context_aware_warnings >= 0); |
957 | | #ifdef __APPLE__ |
958 | | assert(config->use_system_logger >= 0); |
959 | | #endif |
960 | | #ifdef Py_STATS |
961 | | assert(config->_pystats >= 0); |
962 | | #endif |
963 | 22 | return 1; |
964 | 22 | } |
965 | | #endif |
966 | | |
967 | | |
968 | | /* Free memory allocated in config, but don't clear all attributes */ |
969 | | void |
970 | | PyConfig_Clear(PyConfig *config) |
971 | 88 | { |
972 | 88 | #define CLEAR(ATTR) \ |
973 | 1.84k | do { \ |
974 | 1.84k | PyMem_RawFree(ATTR); \ |
975 | 1.84k | ATTR = NULL; \ |
976 | 1.84k | } while (0) |
977 | | |
978 | 88 | CLEAR(config->pycache_prefix); |
979 | 88 | CLEAR(config->pythonpath_env); |
980 | 88 | CLEAR(config->home); |
981 | 88 | CLEAR(config->program_name); |
982 | | |
983 | 88 | _PyWideStringList_Clear(&config->argv); |
984 | 88 | _PyWideStringList_Clear(&config->warnoptions); |
985 | 88 | _PyWideStringList_Clear(&config->xoptions); |
986 | 88 | _PyWideStringList_Clear(&config->module_search_paths); |
987 | 88 | config->module_search_paths_set = 0; |
988 | 88 | CLEAR(config->stdlib_dir); |
989 | | |
990 | 88 | CLEAR(config->executable); |
991 | 88 | CLEAR(config->base_executable); |
992 | 88 | CLEAR(config->prefix); |
993 | 88 | CLEAR(config->base_prefix); |
994 | 88 | CLEAR(config->exec_prefix); |
995 | 88 | CLEAR(config->base_exec_prefix); |
996 | 88 | CLEAR(config->platlibdir); |
997 | 88 | CLEAR(config->sys_path_0); |
998 | | |
999 | 88 | CLEAR(config->filesystem_encoding); |
1000 | 88 | CLEAR(config->filesystem_errors); |
1001 | 88 | CLEAR(config->stdio_encoding); |
1002 | 88 | CLEAR(config->stdio_errors); |
1003 | 88 | CLEAR(config->run_command); |
1004 | 88 | CLEAR(config->run_module); |
1005 | 88 | CLEAR(config->run_filename); |
1006 | 88 | CLEAR(config->check_hash_pycs_mode); |
1007 | | #ifdef Py_DEBUG |
1008 | | CLEAR(config->run_presite); |
1009 | | #endif |
1010 | | |
1011 | 88 | _PyWideStringList_Clear(&config->orig_argv); |
1012 | 88 | #undef CLEAR |
1013 | 88 | } |
1014 | | |
1015 | | |
1016 | | void |
1017 | | _PyConfig_InitCompatConfig(PyConfig *config) |
1018 | 66 | { |
1019 | 66 | memset(config, 0, sizeof(*config)); |
1020 | | |
1021 | 66 | config->_config_init = (int)_PyConfig_INIT_COMPAT; |
1022 | 66 | config->import_time = -1; |
1023 | 66 | config->isolated = -1; |
1024 | 66 | config->use_environment = -1; |
1025 | 66 | config->dev_mode = -1; |
1026 | 66 | config->install_signal_handlers = 1; |
1027 | 66 | config->use_hash_seed = -1; |
1028 | 66 | config->faulthandler = -1; |
1029 | 66 | config->tracemalloc = -1; |
1030 | 66 | config->perf_profiling = -1; |
1031 | 66 | config->remote_debug = -1; |
1032 | 66 | config->module_search_paths_set = 0; |
1033 | 66 | config->parse_argv = 0; |
1034 | 66 | config->site_import = -1; |
1035 | 66 | config->bytes_warning = -1; |
1036 | 66 | config->warn_default_encoding = 0; |
1037 | 66 | config->inspect = -1; |
1038 | 66 | config->interactive = -1; |
1039 | 66 | config->optimization_level = -1; |
1040 | 66 | config->parser_debug= -1; |
1041 | 66 | config->write_bytecode = -1; |
1042 | 66 | config->verbose = -1; |
1043 | 66 | config->quiet = -1; |
1044 | 66 | config->user_site_directory = -1; |
1045 | 66 | config->configure_c_stdio = 0; |
1046 | 66 | config->buffered_stdio = -1; |
1047 | 66 | config->_install_importlib = 1; |
1048 | 66 | config->check_hash_pycs_mode = NULL; |
1049 | 66 | config->pathconfig_warnings = -1; |
1050 | 66 | config->_init_main = 1; |
1051 | | #ifdef MS_WINDOWS |
1052 | | config->legacy_windows_stdio = -1; |
1053 | | #endif |
1054 | | #ifdef Py_DEBUG |
1055 | | config->use_frozen_modules = 0; |
1056 | | #else |
1057 | 66 | config->use_frozen_modules = 1; |
1058 | 66 | #endif |
1059 | 66 | config->safe_path = 0; |
1060 | 66 | config->int_max_str_digits = -1; |
1061 | 66 | config->_is_python_build = 0; |
1062 | 66 | config->code_debug_ranges = 1; |
1063 | 66 | config->cpu_count = -1; |
1064 | 66 | config->lazy_imports = -1; |
1065 | | #ifdef Py_GIL_DISABLED |
1066 | | config->thread_inherit_context = 1; |
1067 | | config->context_aware_warnings = 1; |
1068 | | #else |
1069 | 66 | config->thread_inherit_context = 0; |
1070 | 66 | config->context_aware_warnings = 0; |
1071 | 66 | #endif |
1072 | | #ifdef __APPLE__ |
1073 | | config->use_system_logger = USE_SYSTEM_LOGGER_DEFAULT; |
1074 | | #endif |
1075 | | #ifdef Py_GIL_DISABLED |
1076 | | config->enable_gil = _PyConfig_GIL_DEFAULT; |
1077 | | config->tlbc_enabled = 1; |
1078 | | #endif |
1079 | 66 | } |
1080 | | |
1081 | | |
1082 | | static void |
1083 | | config_init_defaults(PyConfig *config) |
1084 | 66 | { |
1085 | 66 | _PyConfig_InitCompatConfig(config); |
1086 | | |
1087 | 66 | config->isolated = 0; |
1088 | 66 | config->use_environment = 1; |
1089 | 66 | config->site_import = 1; |
1090 | 66 | config->bytes_warning = 0; |
1091 | 66 | config->inspect = 0; |
1092 | 66 | config->interactive = 0; |
1093 | 66 | config->optimization_level = 0; |
1094 | 66 | config->parser_debug= 0; |
1095 | 66 | config->write_bytecode = 1; |
1096 | 66 | config->verbose = 0; |
1097 | 66 | config->quiet = 0; |
1098 | 66 | config->user_site_directory = 1; |
1099 | 66 | config->buffered_stdio = 1; |
1100 | 66 | config->pathconfig_warnings = 1; |
1101 | | #ifdef MS_WINDOWS |
1102 | | config->legacy_windows_stdio = 0; |
1103 | | #endif |
1104 | | #ifdef Py_GIL_DISABLED |
1105 | | config->thread_inherit_context = 1; |
1106 | | config->context_aware_warnings = 1; |
1107 | | #else |
1108 | 66 | config->thread_inherit_context = 0; |
1109 | 66 | config->context_aware_warnings = 0; |
1110 | 66 | #endif |
1111 | | #ifdef __APPLE__ |
1112 | | config->use_system_logger = USE_SYSTEM_LOGGER_DEFAULT; |
1113 | | #endif |
1114 | 66 | } |
1115 | | |
1116 | | |
1117 | | void |
1118 | | PyConfig_InitPythonConfig(PyConfig *config) |
1119 | 66 | { |
1120 | 66 | config_init_defaults(config); |
1121 | | |
1122 | 66 | config->_config_init = (int)_PyConfig_INIT_PYTHON; |
1123 | 66 | config->configure_c_stdio = 1; |
1124 | 66 | config->parse_argv = 1; |
1125 | 66 | } |
1126 | | |
1127 | | |
1128 | | void |
1129 | | PyConfig_InitIsolatedConfig(PyConfig *config) |
1130 | 0 | { |
1131 | 0 | config_init_defaults(config); |
1132 | |
|
1133 | 0 | config->_config_init = (int)_PyConfig_INIT_ISOLATED; |
1134 | 0 | config->isolated = 1; |
1135 | 0 | config->use_environment = 0; |
1136 | 0 | config->user_site_directory = 0; |
1137 | 0 | config->dev_mode = 0; |
1138 | 0 | config->install_signal_handlers = 0; |
1139 | 0 | config->use_hash_seed = 0; |
1140 | 0 | config->tracemalloc = 0; |
1141 | 0 | config->perf_profiling = 0; |
1142 | 0 | config->int_max_str_digits = _PY_LONG_DEFAULT_MAX_STR_DIGITS; |
1143 | 0 | config->safe_path = 1; |
1144 | 0 | config->pathconfig_warnings = 0; |
1145 | | #ifdef Py_GIL_DISABLED |
1146 | | config->thread_inherit_context = 1; |
1147 | | #else |
1148 | 0 | config->thread_inherit_context = 0; |
1149 | 0 | #endif |
1150 | | #ifdef MS_WINDOWS |
1151 | | config->legacy_windows_stdio = 0; |
1152 | | #endif |
1153 | | #ifdef __APPLE__ |
1154 | | config->use_system_logger = USE_SYSTEM_LOGGER_DEFAULT; |
1155 | | #endif |
1156 | 0 | } |
1157 | | |
1158 | | |
1159 | | /* Copy str into *config_str (duplicate the string) */ |
1160 | | PyStatus |
1161 | | PyConfig_SetString(PyConfig *config, wchar_t **config_str, const wchar_t *str) |
1162 | 1.56k | { |
1163 | 1.56k | PyStatus status = _Py_PreInitializeFromConfig(config, NULL); |
1164 | 1.56k | if (_PyStatus_EXCEPTION(status)) { |
1165 | 0 | return status; |
1166 | 0 | } |
1167 | | |
1168 | 1.56k | wchar_t *str2; |
1169 | 1.56k | if (str != NULL) { |
1170 | 572 | str2 = _PyMem_RawWcsdup(str); |
1171 | 572 | if (str2 == NULL) { |
1172 | 0 | return _PyStatus_NO_MEMORY(); |
1173 | 0 | } |
1174 | 572 | } |
1175 | 990 | else { |
1176 | 990 | str2 = NULL; |
1177 | 990 | } |
1178 | 1.56k | PyMem_RawFree(*config_str); |
1179 | 1.56k | *config_str = str2; |
1180 | 1.56k | return _PyStatus_OK(); |
1181 | 1.56k | } |
1182 | | |
1183 | | |
1184 | | static PyStatus |
1185 | | config_set_bytes_string(PyConfig *config, wchar_t **config_str, |
1186 | | const char *str, const char *decode_err_msg) |
1187 | 22 | { |
1188 | 22 | PyStatus status = _Py_PreInitializeFromConfig(config, NULL); |
1189 | 22 | if (_PyStatus_EXCEPTION(status)) { |
1190 | 0 | return status; |
1191 | 0 | } |
1192 | | |
1193 | 22 | wchar_t *str2; |
1194 | 22 | if (str != NULL) { |
1195 | 22 | size_t len; |
1196 | 22 | str2 = Py_DecodeLocale(str, &len); |
1197 | 22 | if (str2 == NULL) { |
1198 | 0 | if (len == (size_t)-2) { |
1199 | 0 | return _PyStatus_ERR(decode_err_msg); |
1200 | 0 | } |
1201 | 0 | else { |
1202 | 0 | return _PyStatus_NO_MEMORY(); |
1203 | 0 | } |
1204 | 0 | } |
1205 | 22 | } |
1206 | 0 | else { |
1207 | 0 | str2 = NULL; |
1208 | 0 | } |
1209 | 22 | PyMem_RawFree(*config_str); |
1210 | 22 | *config_str = str2; |
1211 | 22 | return _PyStatus_OK(); |
1212 | 22 | } |
1213 | | |
1214 | | |
1215 | | #define CONFIG_SET_BYTES_STR(config, config_str, str, NAME) \ |
1216 | 22 | config_set_bytes_string(config, config_str, str, "cannot decode " NAME) |
1217 | | |
1218 | | |
1219 | | /* Decode str using Py_DecodeLocale() and set the result into *config_str. |
1220 | | Pre-initialize Python if needed to ensure that encodings are properly |
1221 | | configured. */ |
1222 | | PyStatus |
1223 | | PyConfig_SetBytesString(PyConfig *config, wchar_t **config_str, |
1224 | | const char *str) |
1225 | 22 | { |
1226 | 22 | return CONFIG_SET_BYTES_STR(config, config_str, str, "string"); |
1227 | 22 | } |
1228 | | |
1229 | | |
1230 | | static inline void* |
1231 | | config_get_spec_member(const PyConfig *config, const PyConfigSpec *spec) |
1232 | 7.81k | { |
1233 | 7.81k | return (char *)config + spec->offset; |
1234 | 7.81k | } |
1235 | | |
1236 | | |
1237 | | static inline void* |
1238 | | preconfig_get_spec_member(const PyPreConfig *preconfig, const PyConfigSpec *spec) |
1239 | 0 | { |
1240 | 0 | return (char *)preconfig + spec->offset; |
1241 | 0 | } |
1242 | | |
1243 | | |
1244 | | PyStatus |
1245 | | _PyConfig_Copy(PyConfig *config, const PyConfig *config2) |
1246 | 44 | { |
1247 | 44 | PyConfig_Clear(config); |
1248 | | |
1249 | 44 | PyStatus status; |
1250 | 44 | const PyConfigSpec *spec = PYCONFIG_SPEC; |
1251 | 3.16k | for (; spec->name != NULL; spec++) { |
1252 | 3.12k | void *member = config_get_spec_member(config, spec); |
1253 | 3.12k | const void *member2 = config_get_spec_member((PyConfig*)config2, spec); |
1254 | 3.12k | switch (spec->type) { |
1255 | 176 | case PyConfig_MEMBER_INT: |
1256 | 528 | case PyConfig_MEMBER_UINT: |
1257 | 1.89k | case PyConfig_MEMBER_BOOL: |
1258 | 1.89k | { |
1259 | 1.89k | *(int*)member = *(int*)member2; |
1260 | 1.89k | break; |
1261 | 528 | } |
1262 | 44 | case PyConfig_MEMBER_ULONG: |
1263 | 44 | { |
1264 | 44 | *(unsigned long*)member = *(unsigned long*)member2; |
1265 | 44 | break; |
1266 | 528 | } |
1267 | 308 | case PyConfig_MEMBER_WSTR: |
1268 | 968 | case PyConfig_MEMBER_WSTR_OPT: |
1269 | 968 | { |
1270 | 968 | const wchar_t *str = *(const wchar_t**)member2; |
1271 | 968 | status = PyConfig_SetString(config, (wchar_t**)member, str); |
1272 | 968 | if (_PyStatus_EXCEPTION(status)) { |
1273 | 0 | return status; |
1274 | 0 | } |
1275 | 968 | break; |
1276 | 968 | } |
1277 | 968 | case PyConfig_MEMBER_WSTR_LIST: |
1278 | 220 | { |
1279 | 220 | if (_PyWideStringList_Copy((PyWideStringList*)member, |
1280 | 220 | (const PyWideStringList*)member2) < 0) { |
1281 | 0 | return _PyStatus_NO_MEMORY(); |
1282 | 0 | } |
1283 | 220 | break; |
1284 | 220 | } |
1285 | 220 | default: |
1286 | 0 | Py_UNREACHABLE(); |
1287 | 3.12k | } |
1288 | 3.12k | } |
1289 | 44 | return _PyStatus_OK(); |
1290 | 44 | } |
1291 | | |
1292 | | |
1293 | | PyObject * |
1294 | | _PyConfig_AsDict(const PyConfig *config) |
1295 | 22 | { |
1296 | 22 | PyObject *dict = PyDict_New(); |
1297 | 22 | if (dict == NULL) { |
1298 | 0 | return NULL; |
1299 | 0 | } |
1300 | | |
1301 | 22 | const PyConfigSpec *spec = PYCONFIG_SPEC; |
1302 | 1.58k | for (; spec->name != NULL; spec++) { |
1303 | 1.56k | PyObject *obj = config_get(config, spec, 0); |
1304 | 1.56k | if (obj == NULL) { |
1305 | 0 | Py_DECREF(dict); |
1306 | 0 | return NULL; |
1307 | 0 | } |
1308 | | |
1309 | 1.56k | int res = PyDict_SetItemString(dict, spec->name, obj); |
1310 | 1.56k | Py_DECREF(obj); |
1311 | 1.56k | if (res < 0) { |
1312 | 0 | Py_DECREF(dict); |
1313 | 0 | return NULL; |
1314 | 0 | } |
1315 | 1.56k | } |
1316 | 22 | return dict; |
1317 | 22 | } |
1318 | | |
1319 | | |
1320 | | static void |
1321 | | config_dict_invalid_value(const char *name) |
1322 | 0 | { |
1323 | 0 | PyErr_Format(PyExc_ValueError, "invalid config value: %s", name); |
1324 | 0 | } |
1325 | | |
1326 | | |
1327 | | static int |
1328 | | config_dict_get_int(PyObject *dict, const char *name, int *result) |
1329 | 946 | { |
1330 | 946 | PyObject *item = config_dict_get(dict, name); |
1331 | 946 | if (item == NULL) { |
1332 | 0 | return -1; |
1333 | 0 | } |
1334 | 946 | int value = PyLong_AsInt(item); |
1335 | 946 | Py_DECREF(item); |
1336 | 946 | if (value == -1 && PyErr_Occurred()) { |
1337 | 0 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
1338 | 0 | config_dict_invalid_type(name); |
1339 | 0 | } |
1340 | 0 | else if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
1341 | 0 | config_dict_invalid_value(name); |
1342 | 0 | } |
1343 | 0 | return -1; |
1344 | 0 | } |
1345 | 946 | *result = value; |
1346 | 946 | return 0; |
1347 | 946 | } |
1348 | | |
1349 | | |
1350 | | static int |
1351 | | config_dict_get_ulong(PyObject *dict, const char *name, unsigned long *result) |
1352 | 22 | { |
1353 | 22 | PyObject *item = config_dict_get(dict, name); |
1354 | 22 | if (item == NULL) { |
1355 | 0 | return -1; |
1356 | 0 | } |
1357 | 22 | unsigned long value = PyLong_AsUnsignedLong(item); |
1358 | 22 | Py_DECREF(item); |
1359 | 22 | if (value == (unsigned long)-1 && PyErr_Occurred()) { |
1360 | 0 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
1361 | 0 | config_dict_invalid_type(name); |
1362 | 0 | } |
1363 | 0 | else if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
1364 | 0 | config_dict_invalid_value(name); |
1365 | 0 | } |
1366 | 0 | return -1; |
1367 | 0 | } |
1368 | 22 | *result = value; |
1369 | 22 | return 0; |
1370 | 22 | } |
1371 | | |
1372 | | |
1373 | | static int |
1374 | | config_dict_get_wstr(PyObject *dict, const char *name, PyConfig *config, |
1375 | | wchar_t **result) |
1376 | 484 | { |
1377 | 484 | PyObject *item = config_dict_get(dict, name); |
1378 | 484 | if (item == NULL) { |
1379 | 0 | return -1; |
1380 | 0 | } |
1381 | | |
1382 | 484 | PyStatus status; |
1383 | 484 | if (item == Py_None) { |
1384 | 176 | status = PyConfig_SetString(config, result, NULL); |
1385 | 176 | } |
1386 | 308 | else if (!PyUnicode_Check(item)) { |
1387 | 0 | config_dict_invalid_type(name); |
1388 | 0 | goto error; |
1389 | 0 | } |
1390 | 308 | else { |
1391 | 308 | wchar_t *wstr = PyUnicode_AsWideCharString(item, NULL); |
1392 | 308 | if (wstr == NULL) { |
1393 | 0 | goto error; |
1394 | 0 | } |
1395 | 308 | status = PyConfig_SetString(config, result, wstr); |
1396 | 308 | PyMem_Free(wstr); |
1397 | 308 | } |
1398 | 484 | if (_PyStatus_EXCEPTION(status)) { |
1399 | 0 | PyErr_NoMemory(); |
1400 | 0 | goto error; |
1401 | 0 | } |
1402 | 484 | Py_DECREF(item); |
1403 | 484 | return 0; |
1404 | | |
1405 | 0 | error: |
1406 | 0 | Py_DECREF(item); |
1407 | 0 | return -1; |
1408 | 484 | } |
1409 | | |
1410 | | |
1411 | | static int |
1412 | | config_dict_get_wstrlist(PyObject *dict, const char *name, PyConfig *config, |
1413 | | PyWideStringList *result) |
1414 | 88 | { |
1415 | 88 | PyObject *list = config_dict_get(dict, name); |
1416 | 88 | if (list == NULL) { |
1417 | 0 | return -1; |
1418 | 0 | } |
1419 | | |
1420 | 88 | int is_list = PyList_CheckExact(list); |
1421 | 88 | if (!is_list && !PyTuple_CheckExact(list)) { |
1422 | 0 | Py_DECREF(list); |
1423 | 0 | config_dict_invalid_type(name); |
1424 | 0 | return -1; |
1425 | 0 | } |
1426 | | |
1427 | 88 | PyWideStringList wstrlist = _PyWideStringList_INIT; |
1428 | 88 | Py_ssize_t len = is_list ? PyList_GET_SIZE(list) : PyTuple_GET_SIZE(list); |
1429 | 176 | for (Py_ssize_t i=0; i < len; i++) { |
1430 | 88 | PyObject *item = is_list ? PyList_GET_ITEM(list, i) : PyTuple_GET_ITEM(list, i); |
1431 | | |
1432 | 88 | if (item == Py_None) { |
1433 | 0 | config_dict_invalid_value(name); |
1434 | 0 | goto error; |
1435 | 0 | } |
1436 | 88 | else if (!PyUnicode_Check(item)) { |
1437 | 0 | config_dict_invalid_type(name); |
1438 | 0 | goto error; |
1439 | 0 | } |
1440 | 88 | wchar_t *wstr = PyUnicode_AsWideCharString(item, NULL); |
1441 | 88 | if (wstr == NULL) { |
1442 | 0 | goto error; |
1443 | 0 | } |
1444 | 88 | PyStatus status = PyWideStringList_Append(&wstrlist, wstr); |
1445 | 88 | PyMem_Free(wstr); |
1446 | 88 | if (_PyStatus_EXCEPTION(status)) { |
1447 | 0 | PyErr_NoMemory(); |
1448 | 0 | goto error; |
1449 | 0 | } |
1450 | 88 | } |
1451 | | |
1452 | 88 | if (_PyWideStringList_Copy(result, &wstrlist) < 0) { |
1453 | 0 | PyErr_NoMemory(); |
1454 | 0 | goto error; |
1455 | 0 | } |
1456 | 88 | _PyWideStringList_Clear(&wstrlist); |
1457 | 88 | Py_DECREF(list); |
1458 | 88 | return 0; |
1459 | | |
1460 | 0 | error: |
1461 | 0 | _PyWideStringList_Clear(&wstrlist); |
1462 | 0 | Py_DECREF(list); |
1463 | 0 | return -1; |
1464 | 88 | } |
1465 | | |
1466 | | |
1467 | | static int |
1468 | | config_dict_get_xoptions(PyObject *dict, const char *name, PyConfig *config, |
1469 | | PyWideStringList *result) |
1470 | 22 | { |
1471 | 22 | PyObject *xoptions = config_dict_get(dict, name); |
1472 | 22 | if (xoptions == NULL) { |
1473 | 0 | return -1; |
1474 | 0 | } |
1475 | | |
1476 | 22 | if (!PyDict_CheckExact(xoptions)) { |
1477 | 0 | Py_DECREF(xoptions); |
1478 | 0 | config_dict_invalid_type(name); |
1479 | 0 | return -1; |
1480 | 0 | } |
1481 | | |
1482 | 22 | Py_ssize_t pos = 0; |
1483 | 22 | PyObject *key, *value; |
1484 | 22 | PyWideStringList wstrlist = _PyWideStringList_INIT; |
1485 | 22 | while (PyDict_Next(xoptions, &pos, &key, &value)) { |
1486 | 0 | PyObject *item; |
1487 | |
|
1488 | 0 | if (value != Py_True) { |
1489 | 0 | item = PyUnicode_FromFormat("%S=%S", key, value); |
1490 | 0 | if (item == NULL) { |
1491 | 0 | goto error; |
1492 | 0 | } |
1493 | 0 | } |
1494 | 0 | else { |
1495 | 0 | item = Py_NewRef(key); |
1496 | 0 | } |
1497 | | |
1498 | 0 | wchar_t *wstr = PyUnicode_AsWideCharString(item, NULL); |
1499 | 0 | Py_DECREF(item); |
1500 | 0 | if (wstr == NULL) { |
1501 | 0 | goto error; |
1502 | 0 | } |
1503 | | |
1504 | 0 | PyStatus status = PyWideStringList_Append(&wstrlist, wstr); |
1505 | 0 | PyMem_Free(wstr); |
1506 | 0 | if (_PyStatus_EXCEPTION(status)) { |
1507 | 0 | PyErr_NoMemory(); |
1508 | 0 | goto error; |
1509 | 0 | } |
1510 | 0 | } |
1511 | | |
1512 | 22 | if (_PyWideStringList_Copy(result, &wstrlist) < 0) { |
1513 | 0 | PyErr_NoMemory(); |
1514 | 0 | goto error; |
1515 | 0 | } |
1516 | 22 | _PyWideStringList_Clear(&wstrlist); |
1517 | 22 | Py_DECREF(xoptions); |
1518 | 22 | return 0; |
1519 | | |
1520 | 0 | error: |
1521 | 0 | _PyWideStringList_Clear(&wstrlist); |
1522 | 0 | Py_DECREF(xoptions); |
1523 | 0 | return -1; |
1524 | 22 | } |
1525 | | |
1526 | | |
1527 | | int |
1528 | | _PyConfig_FromDict(PyConfig *config, PyObject *dict) |
1529 | 22 | { |
1530 | 22 | if (!PyDict_Check(dict)) { |
1531 | 0 | PyErr_SetString(PyExc_TypeError, "dict expected"); |
1532 | 0 | return -1; |
1533 | 0 | } |
1534 | | |
1535 | 22 | const PyConfigSpec *spec = PYCONFIG_SPEC; |
1536 | 1.58k | for (; spec->name != NULL; spec++) { |
1537 | 1.56k | char *member = (char *)config + spec->offset; |
1538 | 1.56k | switch (spec->type) { |
1539 | 88 | case PyConfig_MEMBER_INT: |
1540 | 264 | case PyConfig_MEMBER_UINT: |
1541 | 946 | case PyConfig_MEMBER_BOOL: |
1542 | 946 | { |
1543 | 946 | int value; |
1544 | 946 | if (config_dict_get_int(dict, spec->name, &value) < 0) { |
1545 | 0 | return -1; |
1546 | 0 | } |
1547 | 946 | if (spec->type == PyConfig_MEMBER_BOOL |
1548 | 264 | || spec->type == PyConfig_MEMBER_UINT) |
1549 | 858 | { |
1550 | 858 | if (value < 0) { |
1551 | 0 | config_dict_invalid_value(spec->name); |
1552 | 0 | return -1; |
1553 | 0 | } |
1554 | 858 | } |
1555 | 946 | *(int*)member = value; |
1556 | 946 | break; |
1557 | 946 | } |
1558 | 22 | case PyConfig_MEMBER_ULONG: |
1559 | 22 | { |
1560 | 22 | if (config_dict_get_ulong(dict, spec->name, |
1561 | 22 | (unsigned long*)member) < 0) { |
1562 | 0 | return -1; |
1563 | 0 | } |
1564 | 22 | break; |
1565 | 22 | } |
1566 | 154 | case PyConfig_MEMBER_WSTR: |
1567 | 154 | { |
1568 | 154 | wchar_t **wstr = (wchar_t**)member; |
1569 | 154 | if (config_dict_get_wstr(dict, spec->name, config, wstr) < 0) { |
1570 | 0 | return -1; |
1571 | 0 | } |
1572 | 154 | if (*wstr == NULL) { |
1573 | 0 | config_dict_invalid_value(spec->name); |
1574 | 0 | return -1; |
1575 | 0 | } |
1576 | 154 | break; |
1577 | 154 | } |
1578 | 330 | case PyConfig_MEMBER_WSTR_OPT: |
1579 | 330 | { |
1580 | 330 | wchar_t **wstr = (wchar_t**)member; |
1581 | 330 | if (config_dict_get_wstr(dict, spec->name, config, wstr) < 0) { |
1582 | 0 | return -1; |
1583 | 0 | } |
1584 | 330 | break; |
1585 | 330 | } |
1586 | 330 | case PyConfig_MEMBER_WSTR_LIST: |
1587 | 110 | { |
1588 | 110 | if (strcmp(spec->name, "xoptions") == 0) { |
1589 | 22 | if (config_dict_get_xoptions(dict, spec->name, config, |
1590 | 22 | (PyWideStringList*)member) < 0) { |
1591 | 0 | return -1; |
1592 | 0 | } |
1593 | 22 | } |
1594 | 88 | else { |
1595 | 88 | if (config_dict_get_wstrlist(dict, spec->name, config, |
1596 | 88 | (PyWideStringList*)member) < 0) { |
1597 | 0 | return -1; |
1598 | 0 | } |
1599 | 88 | } |
1600 | 110 | break; |
1601 | 110 | } |
1602 | 110 | default: |
1603 | 0 | Py_UNREACHABLE(); |
1604 | 1.56k | } |
1605 | 1.56k | } |
1606 | | |
1607 | 22 | if (!(config->_config_init == _PyConfig_INIT_COMPAT |
1608 | 22 | || config->_config_init == _PyConfig_INIT_PYTHON |
1609 | 0 | || config->_config_init == _PyConfig_INIT_ISOLATED)) |
1610 | 0 | { |
1611 | 0 | config_dict_invalid_value("_config_init"); |
1612 | 0 | return -1; |
1613 | 0 | } |
1614 | | |
1615 | 22 | if (config->hash_seed > MAX_HASH_SEED) { |
1616 | 0 | config_dict_invalid_value("hash_seed"); |
1617 | 0 | return -1; |
1618 | 0 | } |
1619 | 22 | return 0; |
1620 | 22 | } |
1621 | | |
1622 | | |
1623 | | static const char* |
1624 | | config_get_env(const PyConfig *config, const char *name) |
1625 | 418 | { |
1626 | 418 | return _Py_GetEnv(config->use_environment, name); |
1627 | 418 | } |
1628 | | |
1629 | | |
1630 | | /* Get a copy of the environment variable as wchar_t*. |
1631 | | Return 0 on success, but *dest can be NULL. |
1632 | | Return -1 on memory allocation failure. Return -2 on decoding error. */ |
1633 | | static PyStatus |
1634 | | config_get_env_dup(PyConfig *config, |
1635 | | wchar_t **dest, |
1636 | | wchar_t *wname, char *name, |
1637 | | const char *decode_err_msg) |
1638 | 110 | { |
1639 | 110 | assert(*dest == NULL); |
1640 | 110 | assert(config->use_environment >= 0); |
1641 | | |
1642 | 110 | if (!config->use_environment) { |
1643 | 0 | *dest = NULL; |
1644 | 0 | return _PyStatus_OK(); |
1645 | 0 | } |
1646 | | |
1647 | | #ifdef MS_WINDOWS |
1648 | | const wchar_t *var = _wgetenv(wname); |
1649 | | if (!var || var[0] == '\0') { |
1650 | | *dest = NULL; |
1651 | | return _PyStatus_OK(); |
1652 | | } |
1653 | | |
1654 | | return PyConfig_SetString(config, dest, var); |
1655 | | #else |
1656 | 110 | const char *var = getenv(name); |
1657 | 110 | if (!var || var[0] == '\0') { |
1658 | 110 | *dest = NULL; |
1659 | 110 | return _PyStatus_OK(); |
1660 | 110 | } |
1661 | | |
1662 | 0 | return config_set_bytes_string(config, dest, var, decode_err_msg); |
1663 | 110 | #endif |
1664 | 110 | } |
1665 | | |
1666 | | |
1667 | | #define CONFIG_GET_ENV_DUP(CONFIG, DEST, WNAME, NAME) \ |
1668 | 110 | config_get_env_dup(CONFIG, DEST, WNAME, NAME, "cannot decode " NAME) |
1669 | | |
1670 | | |
1671 | | static void |
1672 | | config_get_global_vars(PyConfig *config) |
1673 | 22 | { |
1674 | 22 | _Py_COMP_DIAG_PUSH |
1675 | 22 | _Py_COMP_DIAG_IGNORE_DEPR_DECLS |
1676 | 22 | if (config->_config_init != _PyConfig_INIT_COMPAT) { |
1677 | | /* Python and Isolated configuration ignore global variables */ |
1678 | 22 | return; |
1679 | 22 | } |
1680 | | |
1681 | 0 | #define COPY_FLAG(ATTR, VALUE) \ |
1682 | 0 | if (config->ATTR == -1) { \ |
1683 | 0 | config->ATTR = VALUE; \ |
1684 | 0 | } |
1685 | 0 | #define COPY_NOT_FLAG(ATTR, VALUE) \ |
1686 | 0 | if (config->ATTR == -1) { \ |
1687 | 0 | config->ATTR = !(VALUE); \ |
1688 | 0 | } |
1689 | | |
1690 | 0 | COPY_FLAG(isolated, Py_IsolatedFlag); |
1691 | 0 | COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag); |
1692 | 0 | COPY_FLAG(bytes_warning, Py_BytesWarningFlag); |
1693 | 0 | COPY_FLAG(inspect, Py_InspectFlag); |
1694 | 0 | COPY_FLAG(interactive, Py_InteractiveFlag); |
1695 | 0 | COPY_FLAG(optimization_level, Py_OptimizeFlag); |
1696 | 0 | COPY_FLAG(parser_debug, Py_DebugFlag); |
1697 | 0 | COPY_FLAG(verbose, Py_VerboseFlag); |
1698 | 0 | COPY_FLAG(quiet, Py_QuietFlag); |
1699 | | #ifdef MS_WINDOWS |
1700 | | COPY_FLAG(legacy_windows_stdio, Py_LegacyWindowsStdioFlag); |
1701 | | #endif |
1702 | 0 | COPY_NOT_FLAG(pathconfig_warnings, Py_FrozenFlag); |
1703 | |
|
1704 | 0 | COPY_NOT_FLAG(buffered_stdio, Py_UnbufferedStdioFlag); |
1705 | 0 | COPY_NOT_FLAG(site_import, Py_NoSiteFlag); |
1706 | 0 | COPY_NOT_FLAG(write_bytecode, Py_DontWriteBytecodeFlag); |
1707 | 0 | COPY_NOT_FLAG(user_site_directory, Py_NoUserSiteDirectory); |
1708 | |
|
1709 | 0 | #undef COPY_FLAG |
1710 | 0 | #undef COPY_NOT_FLAG |
1711 | 0 | _Py_COMP_DIAG_POP |
1712 | 0 | } |
1713 | | |
1714 | | |
1715 | | /* Set Py_xxx global configuration variables from 'config' configuration. */ |
1716 | | static void |
1717 | | config_set_global_vars(const PyConfig *config) |
1718 | 22 | { |
1719 | 22 | _Py_COMP_DIAG_PUSH |
1720 | 22 | _Py_COMP_DIAG_IGNORE_DEPR_DECLS |
1721 | 22 | #define COPY_FLAG(ATTR, VAR) \ |
1722 | 176 | if (config->ATTR != -1) { \ |
1723 | 176 | VAR = config->ATTR; \ |
1724 | 176 | } |
1725 | 22 | #define COPY_NOT_FLAG(ATTR, VAR) \ |
1726 | 132 | if (config->ATTR != -1) { \ |
1727 | 132 | VAR = !config->ATTR; \ |
1728 | 132 | } |
1729 | | |
1730 | 22 | COPY_FLAG(isolated, Py_IsolatedFlag); |
1731 | 22 | COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag); |
1732 | 22 | COPY_FLAG(bytes_warning, Py_BytesWarningFlag); |
1733 | 22 | COPY_FLAG(inspect, Py_InspectFlag); |
1734 | 22 | COPY_FLAG(interactive, Py_InteractiveFlag); |
1735 | 22 | COPY_FLAG(optimization_level, Py_OptimizeFlag); |
1736 | 22 | COPY_FLAG(parser_debug, Py_DebugFlag); |
1737 | 22 | COPY_FLAG(verbose, Py_VerboseFlag); |
1738 | 22 | COPY_FLAG(quiet, Py_QuietFlag); |
1739 | | #ifdef MS_WINDOWS |
1740 | | COPY_FLAG(legacy_windows_stdio, Py_LegacyWindowsStdioFlag); |
1741 | | #endif |
1742 | 22 | COPY_NOT_FLAG(pathconfig_warnings, Py_FrozenFlag); |
1743 | | |
1744 | 22 | COPY_NOT_FLAG(buffered_stdio, Py_UnbufferedStdioFlag); |
1745 | 22 | COPY_NOT_FLAG(site_import, Py_NoSiteFlag); |
1746 | 22 | COPY_NOT_FLAG(write_bytecode, Py_DontWriteBytecodeFlag); |
1747 | 22 | COPY_NOT_FLAG(user_site_directory, Py_NoUserSiteDirectory); |
1748 | | |
1749 | | /* Random or non-zero hash seed */ |
1750 | 22 | Py_HashRandomizationFlag = (config->use_hash_seed == 0 || |
1751 | 0 | config->hash_seed != 0); |
1752 | | |
1753 | 22 | #undef COPY_FLAG |
1754 | 22 | #undef COPY_NOT_FLAG |
1755 | 22 | _Py_COMP_DIAG_POP |
1756 | 22 | } |
1757 | | |
1758 | | |
1759 | | static const wchar_t* |
1760 | | config_get_xoption(const PyConfig *config, wchar_t *name) |
1761 | 374 | { |
1762 | 374 | return _Py_get_xoption(&config->xoptions, name); |
1763 | 374 | } |
1764 | | |
1765 | | static const wchar_t* |
1766 | | config_get_xoption_value(const PyConfig *config, wchar_t *name) |
1767 | 110 | { |
1768 | 110 | const wchar_t *xoption = config_get_xoption(config, name); |
1769 | 110 | if (xoption == NULL) { |
1770 | 110 | return NULL; |
1771 | 110 | } |
1772 | 0 | const wchar_t *sep = wcschr(xoption, L'='); |
1773 | 0 | return sep ? sep + 1 : L""; |
1774 | 110 | } |
1775 | | |
1776 | | |
1777 | | static PyStatus |
1778 | | config_init_hash_seed(PyConfig *config) |
1779 | 22 | { |
1780 | 22 | static_assert(sizeof(_Py_HashSecret_t) == sizeof(_Py_HashSecret.uc), |
1781 | 22 | "_Py_HashSecret_t has wrong size"); |
1782 | | |
1783 | 22 | const char *seed_text = config_get_env(config, "PYTHONHASHSEED"); |
1784 | | |
1785 | | /* Convert a text seed to a numeric one */ |
1786 | 22 | if (seed_text && strcmp(seed_text, "random") != 0) { |
1787 | 0 | const char *endptr = seed_text; |
1788 | 0 | unsigned long seed; |
1789 | 0 | errno = 0; |
1790 | 0 | seed = strtoul(seed_text, (char **)&endptr, 10); |
1791 | 0 | if (*endptr != '\0' |
1792 | 0 | || seed > MAX_HASH_SEED |
1793 | 0 | || (errno == ERANGE && seed == ULONG_MAX)) |
1794 | 0 | { |
1795 | 0 | return _PyStatus_ERR("PYTHONHASHSEED must be \"random\" " |
1796 | 0 | "or an integer in range [0; 4294967295]"); |
1797 | 0 | } |
1798 | | /* Use a specific hash */ |
1799 | 0 | config->use_hash_seed = 1; |
1800 | 0 | config->hash_seed = seed; |
1801 | 0 | } |
1802 | 22 | else { |
1803 | | /* Use a random hash */ |
1804 | 22 | config->use_hash_seed = 0; |
1805 | 22 | config->hash_seed = 0; |
1806 | 22 | } |
1807 | 22 | return _PyStatus_OK(); |
1808 | 22 | } |
1809 | | |
1810 | | |
1811 | | static int |
1812 | | config_wstr_to_int(const wchar_t *wstr, int *result) |
1813 | 0 | { |
1814 | 0 | const wchar_t *endptr = wstr; |
1815 | 0 | errno = 0; |
1816 | 0 | long value = wcstol(wstr, (wchar_t **)&endptr, 10); |
1817 | 0 | if (*endptr != '\0' || errno == ERANGE) { |
1818 | 0 | return -1; |
1819 | 0 | } |
1820 | 0 | if (value < INT_MIN || value > INT_MAX) { |
1821 | 0 | return -1; |
1822 | 0 | } |
1823 | | |
1824 | 0 | *result = (int)value; |
1825 | 0 | return 0; |
1826 | 0 | } |
1827 | | |
1828 | | static PyStatus |
1829 | | config_read_gil(PyConfig *config, size_t len, wchar_t first_char) |
1830 | 0 | { |
1831 | 0 | if (len == 1 && first_char == L'0') { |
1832 | | #ifdef Py_GIL_DISABLED |
1833 | | config->enable_gil = _PyConfig_GIL_DISABLE; |
1834 | | #else |
1835 | 0 | return _PyStatus_ERR("Disabling the GIL is not supported by this build"); |
1836 | 0 | #endif |
1837 | 0 | } |
1838 | 0 | else if (len == 1 && first_char == L'1') { |
1839 | | #ifdef Py_GIL_DISABLED |
1840 | | config->enable_gil = _PyConfig_GIL_ENABLE; |
1841 | | #else |
1842 | 0 | return _PyStatus_OK(); |
1843 | 0 | #endif |
1844 | 0 | } |
1845 | 0 | else { |
1846 | 0 | return _PyStatus_ERR("PYTHON_GIL / -X gil must be \"0\" or \"1\""); |
1847 | 0 | } |
1848 | 0 | return _PyStatus_OK(); |
1849 | 0 | } |
1850 | | |
1851 | | static PyStatus |
1852 | | config_read_env_vars(PyConfig *config) |
1853 | 22 | { |
1854 | 22 | PyStatus status; |
1855 | 22 | int use_env = config->use_environment; |
1856 | | |
1857 | | /* Get environment variables */ |
1858 | 22 | _Py_get_env_flag(use_env, &config->parser_debug, "PYTHONDEBUG"); |
1859 | 22 | _Py_get_env_flag(use_env, &config->verbose, "PYTHONVERBOSE"); |
1860 | 22 | _Py_get_env_flag(use_env, &config->optimization_level, "PYTHONOPTIMIZE"); |
1861 | 22 | if (!config->inspect && _Py_GetEnv(use_env, "PYTHONINSPECT")) { |
1862 | 0 | config->inspect = 1; |
1863 | 0 | } |
1864 | | |
1865 | 22 | int dont_write_bytecode = 0; |
1866 | 22 | _Py_get_env_flag(use_env, &dont_write_bytecode, "PYTHONDONTWRITEBYTECODE"); |
1867 | 22 | if (dont_write_bytecode) { |
1868 | 0 | config->write_bytecode = 0; |
1869 | 0 | } |
1870 | | |
1871 | 22 | int no_user_site_directory = 0; |
1872 | 22 | _Py_get_env_flag(use_env, &no_user_site_directory, "PYTHONNOUSERSITE"); |
1873 | 22 | if (no_user_site_directory) { |
1874 | 0 | config->user_site_directory = 0; |
1875 | 0 | } |
1876 | | |
1877 | 22 | int unbuffered_stdio = 0; |
1878 | 22 | _Py_get_env_flag(use_env, &unbuffered_stdio, "PYTHONUNBUFFERED"); |
1879 | 22 | if (unbuffered_stdio) { |
1880 | 0 | config->buffered_stdio = 0; |
1881 | 0 | } |
1882 | | |
1883 | | #ifdef MS_WINDOWS |
1884 | | _Py_get_env_flag(use_env, &config->legacy_windows_stdio, |
1885 | | "PYTHONLEGACYWINDOWSSTDIO"); |
1886 | | #endif |
1887 | | |
1888 | 22 | if (config_get_env(config, "PYTHONDUMPREFS")) { |
1889 | 0 | config->dump_refs = 1; |
1890 | 0 | } |
1891 | 22 | if (config_get_env(config, "PYTHONMALLOCSTATS")) { |
1892 | 0 | config->malloc_stats = 1; |
1893 | 0 | } |
1894 | 22 | { |
1895 | 22 | const char *env = _Py_GetEnv(use_env, "PYTHON_PYMALLOC_HUGEPAGES"); |
1896 | 22 | if (env) { |
1897 | 0 | int value; |
1898 | 0 | if (_Py_str_to_int(env, &value) < 0 || value < 0) { |
1899 | | /* PYTHON_PYMALLOC_HUGEPAGES=text or negative |
1900 | | behaves as PYTHON_PYMALLOC_HUGEPAGES=1 */ |
1901 | 0 | value = 1; |
1902 | 0 | } |
1903 | 0 | config->pymalloc_hugepages = (value > 0); |
1904 | 0 | } |
1905 | 22 | } |
1906 | | |
1907 | 22 | if (config->dump_refs_file == NULL) { |
1908 | 22 | status = CONFIG_GET_ENV_DUP(config, &config->dump_refs_file, |
1909 | 22 | L"PYTHONDUMPREFSFILE", "PYTHONDUMPREFSFILE"); |
1910 | 22 | if (_PyStatus_EXCEPTION(status)) { |
1911 | 0 | return status; |
1912 | 0 | } |
1913 | 22 | } |
1914 | | |
1915 | 22 | if (config->pythonpath_env == NULL) { |
1916 | 22 | status = CONFIG_GET_ENV_DUP(config, &config->pythonpath_env, |
1917 | 22 | L"PYTHONPATH", "PYTHONPATH"); |
1918 | 22 | if (_PyStatus_EXCEPTION(status)) { |
1919 | 0 | return status; |
1920 | 0 | } |
1921 | 22 | } |
1922 | | |
1923 | 22 | if(config->platlibdir == NULL) { |
1924 | 22 | status = CONFIG_GET_ENV_DUP(config, &config->platlibdir, |
1925 | 22 | L"PYTHONPLATLIBDIR", "PYTHONPLATLIBDIR"); |
1926 | 22 | if (_PyStatus_EXCEPTION(status)) { |
1927 | 0 | return status; |
1928 | 0 | } |
1929 | 22 | } |
1930 | | |
1931 | 22 | if (config->use_hash_seed < 0) { |
1932 | 22 | status = config_init_hash_seed(config); |
1933 | 22 | if (_PyStatus_EXCEPTION(status)) { |
1934 | 0 | return status; |
1935 | 0 | } |
1936 | 22 | } |
1937 | | |
1938 | 22 | if (config_get_env(config, "PYTHONSAFEPATH")) { |
1939 | 0 | config->safe_path = 1; |
1940 | 0 | } |
1941 | | |
1942 | 22 | const char *gil = config_get_env(config, "PYTHON_GIL"); |
1943 | 22 | if (gil != NULL) { |
1944 | 0 | size_t len = strlen(gil); |
1945 | 0 | status = config_read_gil(config, len, gil[0]); |
1946 | 0 | if (_PyStatus_EXCEPTION(status)) { |
1947 | 0 | return status; |
1948 | 0 | } |
1949 | 0 | } |
1950 | | |
1951 | 22 | return _PyStatus_OK(); |
1952 | 22 | } |
1953 | | |
1954 | | static PyStatus |
1955 | | config_init_cpu_count(PyConfig *config) |
1956 | 22 | { |
1957 | 22 | const char *env = config_get_env(config, "PYTHON_CPU_COUNT"); |
1958 | 22 | if (env) { |
1959 | 0 | int cpu_count = -1; |
1960 | 0 | if (strcmp(env, "default") == 0) { |
1961 | 0 | cpu_count = -1; |
1962 | 0 | } |
1963 | 0 | else if (_Py_str_to_int(env, &cpu_count) < 0 || cpu_count < 1) { |
1964 | 0 | goto error; |
1965 | 0 | } |
1966 | 0 | config->cpu_count = cpu_count; |
1967 | 0 | } |
1968 | | |
1969 | 22 | const wchar_t *xoption = config_get_xoption(config, L"cpu_count"); |
1970 | 22 | if (xoption) { |
1971 | 0 | int cpu_count = -1; |
1972 | 0 | const wchar_t *sep = wcschr(xoption, L'='); |
1973 | 0 | if (sep) { |
1974 | 0 | if (wcscmp(sep + 1, L"default") == 0) { |
1975 | 0 | cpu_count = -1; |
1976 | 0 | } |
1977 | 0 | else if (config_wstr_to_int(sep + 1, &cpu_count) < 0 || cpu_count < 1) { |
1978 | 0 | goto error; |
1979 | 0 | } |
1980 | 0 | } |
1981 | 0 | else { |
1982 | 0 | goto error; |
1983 | 0 | } |
1984 | 0 | config->cpu_count = cpu_count; |
1985 | 0 | } |
1986 | 22 | return _PyStatus_OK(); |
1987 | | |
1988 | 0 | error: |
1989 | 0 | return _PyStatus_ERR("-X cpu_count=n option: n is missing or an invalid number, " |
1990 | 22 | "n must be greater than 0"); |
1991 | 22 | } |
1992 | | |
1993 | | static PyStatus |
1994 | | config_init_thread_inherit_context(PyConfig *config) |
1995 | 22 | { |
1996 | 22 | const char *env = config_get_env(config, "PYTHON_THREAD_INHERIT_CONTEXT"); |
1997 | 22 | if (env) { |
1998 | 0 | int enabled; |
1999 | 0 | if (_Py_str_to_int(env, &enabled) < 0 || (enabled < 0) || (enabled > 1)) { |
2000 | 0 | return _PyStatus_ERR( |
2001 | 0 | "PYTHON_THREAD_INHERIT_CONTEXT=N: N is missing or invalid"); |
2002 | 0 | } |
2003 | 0 | config->thread_inherit_context = enabled; |
2004 | 0 | } |
2005 | | |
2006 | 22 | const wchar_t *xoption = config_get_xoption(config, L"thread_inherit_context"); |
2007 | 22 | if (xoption) { |
2008 | 0 | int enabled; |
2009 | 0 | const wchar_t *sep = wcschr(xoption, L'='); |
2010 | 0 | if (!sep || (config_wstr_to_int(sep + 1, &enabled) < 0) || (enabled < 0) || (enabled > 1)) { |
2011 | 0 | return _PyStatus_ERR( |
2012 | 0 | "-X thread_inherit_context=n: n is missing or invalid"); |
2013 | 0 | } |
2014 | 0 | config->thread_inherit_context = enabled; |
2015 | 0 | } |
2016 | 22 | return _PyStatus_OK(); |
2017 | 22 | } |
2018 | | |
2019 | | static PyStatus |
2020 | | config_init_context_aware_warnings(PyConfig *config) |
2021 | 22 | { |
2022 | 22 | const char *env = config_get_env(config, "PYTHON_CONTEXT_AWARE_WARNINGS"); |
2023 | 22 | if (env) { |
2024 | 0 | int enabled; |
2025 | 0 | if (_Py_str_to_int(env, &enabled) < 0 || (enabled < 0) || (enabled > 1)) { |
2026 | 0 | return _PyStatus_ERR( |
2027 | 0 | "PYTHON_CONTEXT_AWARE_WARNINGS=N: N is missing or invalid"); |
2028 | 0 | } |
2029 | 0 | config->context_aware_warnings = enabled; |
2030 | 0 | } |
2031 | | |
2032 | 22 | const wchar_t *xoption = config_get_xoption(config, L"context_aware_warnings"); |
2033 | 22 | if (xoption) { |
2034 | 0 | int enabled; |
2035 | 0 | const wchar_t *sep = wcschr(xoption, L'='); |
2036 | 0 | if (!sep || (config_wstr_to_int(sep + 1, &enabled) < 0) || (enabled < 0) || (enabled > 1)) { |
2037 | 0 | return _PyStatus_ERR( |
2038 | 0 | "-X context_aware_warnings=n: n is missing or invalid"); |
2039 | 0 | } |
2040 | 0 | config->context_aware_warnings = enabled; |
2041 | 0 | } |
2042 | 22 | return _PyStatus_OK(); |
2043 | 22 | } |
2044 | | |
2045 | | static PyStatus |
2046 | | config_init_tlbc(PyConfig *config) |
2047 | 22 | { |
2048 | | #ifdef Py_GIL_DISABLED |
2049 | | const char *env = config_get_env(config, "PYTHON_TLBC"); |
2050 | | if (env) { |
2051 | | int enabled; |
2052 | | if (_Py_str_to_int(env, &enabled) < 0 || (enabled < 0) || (enabled > 1)) { |
2053 | | return _PyStatus_ERR( |
2054 | | "PYTHON_TLBC=N: N is missing or invalid"); |
2055 | | } |
2056 | | config->tlbc_enabled = enabled; |
2057 | | } |
2058 | | |
2059 | | const wchar_t *xoption = config_get_xoption(config, L"tlbc"); |
2060 | | if (xoption) { |
2061 | | int enabled; |
2062 | | const wchar_t *sep = wcschr(xoption, L'='); |
2063 | | if (!sep || (config_wstr_to_int(sep + 1, &enabled) < 0) || (enabled < 0) || (enabled > 1)) { |
2064 | | return _PyStatus_ERR( |
2065 | | "-X tlbc=n: n is missing or invalid"); |
2066 | | } |
2067 | | config->tlbc_enabled = enabled; |
2068 | | } |
2069 | | return _PyStatus_OK(); |
2070 | | #else |
2071 | 22 | return _PyStatus_OK(); |
2072 | 22 | #endif |
2073 | 22 | } |
2074 | | |
2075 | | static PyStatus |
2076 | | config_init_perf_profiling(PyConfig *config) |
2077 | 22 | { |
2078 | 22 | int active = 0; |
2079 | 22 | const char *env = config_get_env(config, "PYTHONPERFSUPPORT"); |
2080 | 22 | if (env) { |
2081 | 0 | if (_Py_str_to_int(env, &active) != 0) { |
2082 | 0 | active = 0; |
2083 | 0 | } |
2084 | 0 | if (active) { |
2085 | 0 | config->perf_profiling = 1; |
2086 | 0 | } |
2087 | 0 | } |
2088 | 22 | const wchar_t *xoption = config_get_xoption(config, L"perf"); |
2089 | 22 | if (xoption) { |
2090 | 0 | config->perf_profiling = 1; |
2091 | 0 | } |
2092 | 22 | env = config_get_env(config, "PYTHON_PERF_JIT_SUPPORT"); |
2093 | 22 | if (env) { |
2094 | 0 | if (_Py_str_to_int(env, &active) != 0) { |
2095 | 0 | active = 0; |
2096 | 0 | } |
2097 | 0 | if (active) { |
2098 | 0 | config->perf_profiling = 2; |
2099 | 0 | } |
2100 | 0 | } |
2101 | 22 | xoption = config_get_xoption(config, L"perf_jit"); |
2102 | 22 | if (xoption) { |
2103 | 0 | config->perf_profiling = 2; |
2104 | 0 | } |
2105 | | |
2106 | 22 | return _PyStatus_OK(); |
2107 | | |
2108 | 22 | } |
2109 | | |
2110 | | static PyStatus |
2111 | | config_init_remote_debug(PyConfig *config) |
2112 | 22 | { |
2113 | | #ifndef Py_REMOTE_DEBUG |
2114 | | config->remote_debug = 0; |
2115 | | #else |
2116 | 22 | int active = 1; |
2117 | 22 | const char *env = Py_GETENV("PYTHON_DISABLE_REMOTE_DEBUG"); |
2118 | 22 | if (env) { |
2119 | 0 | active = 0; |
2120 | 0 | } |
2121 | 22 | const wchar_t *xoption = config_get_xoption(config, L"disable-remote-debug"); |
2122 | 22 | if (xoption) { |
2123 | 0 | active = 0; |
2124 | 0 | } |
2125 | | |
2126 | 22 | config->remote_debug = active; |
2127 | 22 | #endif |
2128 | 22 | return _PyStatus_OK(); |
2129 | | |
2130 | 22 | } |
2131 | | |
2132 | | static PyStatus |
2133 | | config_init_tracemalloc(PyConfig *config) |
2134 | 22 | { |
2135 | 22 | int nframe; |
2136 | 22 | int valid; |
2137 | | |
2138 | 22 | const char *env = config_get_env(config, "PYTHONTRACEMALLOC"); |
2139 | 22 | if (env) { |
2140 | 0 | if (!_Py_str_to_int(env, &nframe)) { |
2141 | 0 | valid = (nframe >= 0); |
2142 | 0 | } |
2143 | 0 | else { |
2144 | 0 | valid = 0; |
2145 | 0 | } |
2146 | 0 | if (!valid) { |
2147 | 0 | return _PyStatus_ERR("PYTHONTRACEMALLOC: invalid number of frames"); |
2148 | 0 | } |
2149 | 0 | config->tracemalloc = nframe; |
2150 | 0 | } |
2151 | | |
2152 | 22 | const wchar_t *xoption = config_get_xoption(config, L"tracemalloc"); |
2153 | 22 | if (xoption) { |
2154 | 0 | const wchar_t *sep = wcschr(xoption, L'='); |
2155 | 0 | if (sep) { |
2156 | 0 | if (!config_wstr_to_int(sep + 1, &nframe)) { |
2157 | 0 | valid = (nframe >= 0); |
2158 | 0 | } |
2159 | 0 | else { |
2160 | 0 | valid = 0; |
2161 | 0 | } |
2162 | 0 | if (!valid) { |
2163 | 0 | return _PyStatus_ERR("-X tracemalloc=NFRAME: " |
2164 | 0 | "invalid number of frames"); |
2165 | 0 | } |
2166 | 0 | } |
2167 | 0 | else { |
2168 | | /* -X tracemalloc behaves as -X tracemalloc=1 */ |
2169 | 0 | nframe = 1; |
2170 | 0 | } |
2171 | 0 | config->tracemalloc = nframe; |
2172 | 0 | } |
2173 | 22 | return _PyStatus_OK(); |
2174 | 22 | } |
2175 | | |
2176 | | static PyStatus |
2177 | | config_init_int_max_str_digits(PyConfig *config) |
2178 | 0 | { |
2179 | 0 | int maxdigits; |
2180 | |
|
2181 | 0 | const char *env = config_get_env(config, "PYTHONINTMAXSTRDIGITS"); |
2182 | 0 | if (env) { |
2183 | 0 | bool valid = 0; |
2184 | 0 | if (!_Py_str_to_int(env, &maxdigits)) { |
2185 | 0 | valid = ((maxdigits == 0) || (maxdigits >= _PY_LONG_MAX_STR_DIGITS_THRESHOLD)); |
2186 | 0 | } |
2187 | 0 | if (!valid) { |
2188 | 0 | #define STRINGIFY(VAL) _STRINGIFY(VAL) |
2189 | 0 | #define _STRINGIFY(VAL) #VAL |
2190 | 0 | return _PyStatus_ERR( |
2191 | 0 | "PYTHONINTMAXSTRDIGITS: invalid limit; must be >= " |
2192 | 0 | STRINGIFY(_PY_LONG_MAX_STR_DIGITS_THRESHOLD) |
2193 | 0 | " or 0 for unlimited."); |
2194 | 0 | } |
2195 | 0 | config->int_max_str_digits = maxdigits; |
2196 | 0 | } |
2197 | | |
2198 | 0 | const wchar_t *xoption = config_get_xoption(config, L"int_max_str_digits"); |
2199 | 0 | if (xoption) { |
2200 | 0 | const wchar_t *sep = wcschr(xoption, L'='); |
2201 | 0 | bool valid = 0; |
2202 | 0 | if (sep) { |
2203 | 0 | if (!config_wstr_to_int(sep + 1, &maxdigits)) { |
2204 | 0 | valid = ((maxdigits == 0) || (maxdigits >= _PY_LONG_MAX_STR_DIGITS_THRESHOLD)); |
2205 | 0 | } |
2206 | 0 | } |
2207 | 0 | if (!valid) { |
2208 | 0 | return _PyStatus_ERR( |
2209 | 0 | "-X int_max_str_digits: invalid limit; must be >= " |
2210 | 0 | STRINGIFY(_PY_LONG_MAX_STR_DIGITS_THRESHOLD) |
2211 | 0 | " or 0 for unlimited."); |
2212 | 0 | #undef _STRINGIFY |
2213 | 0 | #undef STRINGIFY |
2214 | 0 | } |
2215 | 0 | config->int_max_str_digits = maxdigits; |
2216 | 0 | } |
2217 | 0 | if (config->int_max_str_digits < 0) { |
2218 | 0 | config->int_max_str_digits = _PY_LONG_DEFAULT_MAX_STR_DIGITS; |
2219 | 0 | } |
2220 | 0 | return _PyStatus_OK(); |
2221 | 0 | } |
2222 | | |
2223 | | static PyStatus |
2224 | | config_init_pycache_prefix(PyConfig *config) |
2225 | 22 | { |
2226 | 22 | assert(config->pycache_prefix == NULL); |
2227 | | |
2228 | 22 | const wchar_t *xoption = config_get_xoption(config, L"pycache_prefix"); |
2229 | 22 | if (xoption) { |
2230 | 0 | const wchar_t *sep = wcschr(xoption, L'='); |
2231 | 0 | if (sep && wcslen(sep) > 1) { |
2232 | 0 | config->pycache_prefix = _PyMem_RawWcsdup(sep + 1); |
2233 | 0 | if (config->pycache_prefix == NULL) { |
2234 | 0 | return _PyStatus_NO_MEMORY(); |
2235 | 0 | } |
2236 | 0 | } |
2237 | 0 | else { |
2238 | | // PYTHONPYCACHEPREFIX env var ignored |
2239 | | // if "-X pycache_prefix=" option is used |
2240 | 0 | config->pycache_prefix = NULL; |
2241 | 0 | } |
2242 | 0 | return _PyStatus_OK(); |
2243 | 0 | } |
2244 | | |
2245 | 22 | return CONFIG_GET_ENV_DUP(config, &config->pycache_prefix, |
2246 | 22 | L"PYTHONPYCACHEPREFIX", |
2247 | 22 | "PYTHONPYCACHEPREFIX"); |
2248 | 22 | } |
2249 | | |
2250 | | |
2251 | | #ifdef Py_DEBUG |
2252 | | static PyStatus |
2253 | | config_init_run_presite(PyConfig *config) |
2254 | | { |
2255 | | assert(config->run_presite == NULL); |
2256 | | |
2257 | | const wchar_t *xoption = config_get_xoption(config, L"presite"); |
2258 | | if (xoption) { |
2259 | | const wchar_t *sep = wcschr(xoption, L'='); |
2260 | | if (sep && wcslen(sep) > 1) { |
2261 | | config->run_presite = _PyMem_RawWcsdup(sep + 1); |
2262 | | if (config->run_presite == NULL) { |
2263 | | return _PyStatus_NO_MEMORY(); |
2264 | | } |
2265 | | } |
2266 | | else { |
2267 | | // PYTHON_PRESITE env var ignored |
2268 | | // if "-X presite=" option is used |
2269 | | config->run_presite = NULL; |
2270 | | } |
2271 | | return _PyStatus_OK(); |
2272 | | } |
2273 | | |
2274 | | return CONFIG_GET_ENV_DUP(config, &config->run_presite, |
2275 | | L"PYTHON_PRESITE", |
2276 | | "PYTHON_PRESITE"); |
2277 | | } |
2278 | | #endif |
2279 | | |
2280 | | static PyStatus |
2281 | | config_init_import_time(PyConfig *config) |
2282 | 22 | { |
2283 | 22 | int importtime = 0; |
2284 | | |
2285 | 22 | const char *env = config_get_env(config, "PYTHONPROFILEIMPORTTIME"); |
2286 | 22 | if (env) { |
2287 | 0 | if (_Py_str_to_int(env, &importtime) != 0) { |
2288 | 0 | importtime = 1; |
2289 | 0 | } |
2290 | 0 | if (importtime < 0 || importtime > 2) { |
2291 | 0 | return _PyStatus_ERR( |
2292 | 0 | "PYTHONPROFILEIMPORTTIME: numeric values other than 1 and 2 " |
2293 | 0 | "are reserved for future use."); |
2294 | 0 | } |
2295 | 0 | } |
2296 | | |
2297 | 22 | const wchar_t *x_value = config_get_xoption_value(config, L"importtime"); |
2298 | 22 | if (x_value) { |
2299 | 0 | if (*x_value == 0 || config_wstr_to_int(x_value, &importtime) != 0) { |
2300 | 0 | importtime = 1; |
2301 | 0 | } |
2302 | 0 | if (importtime < 0 || importtime > 2) { |
2303 | 0 | return _PyStatus_ERR( |
2304 | 0 | "-X importtime: values other than 1 and 2 " |
2305 | 0 | "are reserved for future use."); |
2306 | 0 | } |
2307 | 0 | } |
2308 | | |
2309 | 22 | config->import_time = importtime; |
2310 | 22 | return _PyStatus_OK(); |
2311 | 22 | } |
2312 | | |
2313 | | static PyStatus |
2314 | | config_init_lazy_imports(PyConfig *config) |
2315 | 22 | { |
2316 | 22 | int lazy_imports = -1; |
2317 | | |
2318 | 22 | const char *env = config_get_env(config, "PYTHON_LAZY_IMPORTS"); |
2319 | 22 | if (env) { |
2320 | 0 | if (strcmp(env, "all") == 0) { |
2321 | 0 | lazy_imports = 1; |
2322 | 0 | } |
2323 | 0 | else if (strcmp(env, "none") == 0) { |
2324 | 0 | lazy_imports = 0; |
2325 | 0 | } |
2326 | 0 | else if (strcmp(env, "normal") == 0) { |
2327 | 0 | lazy_imports = -1; |
2328 | 0 | } |
2329 | 0 | else { |
2330 | 0 | return _PyStatus_ERR("PYTHON_LAZY_IMPORTS: invalid value; " |
2331 | 0 | "expected 'all', 'none', or 'normal'"); |
2332 | 0 | } |
2333 | 0 | config->lazy_imports = lazy_imports; |
2334 | 0 | } |
2335 | | |
2336 | 22 | const wchar_t *x_value = config_get_xoption_value(config, L"lazy_imports"); |
2337 | 22 | if (x_value) { |
2338 | 0 | if (wcscmp(x_value, L"all") == 0) { |
2339 | 0 | lazy_imports = 1; |
2340 | 0 | } |
2341 | 0 | else if (wcscmp(x_value, L"none") == 0) { |
2342 | 0 | lazy_imports = 0; |
2343 | 0 | } |
2344 | 0 | else if (wcscmp(x_value, L"normal") == 0) { |
2345 | 0 | lazy_imports = -1; |
2346 | 0 | } |
2347 | 0 | else { |
2348 | 0 | return _PyStatus_ERR("-X lazy_imports: invalid value; " |
2349 | 0 | "expected 'all', 'none', or 'normal'"); |
2350 | 0 | } |
2351 | 0 | config->lazy_imports = lazy_imports; |
2352 | 0 | } |
2353 | 22 | return _PyStatus_OK(); |
2354 | 22 | } |
2355 | | |
2356 | | static PyStatus |
2357 | | config_init_pathconfig_warnings(PyConfig *config) |
2358 | 22 | { |
2359 | 22 | const char *env = config_get_env(config, "PYTHON_PATHCONFIG_WARNINGS"); |
2360 | 22 | if (env) { |
2361 | 0 | int enabled; |
2362 | 0 | if (_Py_str_to_int(env, &enabled) < 0 || (enabled < 0) || (enabled > 1)) { |
2363 | 0 | return _PyStatus_ERR( |
2364 | 0 | "PYTHON_PATHCONFIG_WARNINGS=N: N is missing or invalid"); |
2365 | 0 | } |
2366 | 0 | config->pathconfig_warnings = enabled; |
2367 | 0 | } |
2368 | | |
2369 | 22 | const wchar_t *xoption = config_get_xoption(config, L"pathconfig_warnings"); |
2370 | 22 | if (xoption) { |
2371 | 0 | int enabled; |
2372 | 0 | const wchar_t *sep = wcschr(xoption, L'='); |
2373 | 0 | if (!sep || (config_wstr_to_int(sep + 1, &enabled) < 0) || (enabled < 0) || (enabled > 1)) { |
2374 | 0 | return _PyStatus_ERR( |
2375 | 0 | "-X pathconfig_warnings=n: n is missing or invalid"); |
2376 | 0 | } |
2377 | 0 | config->pathconfig_warnings = enabled; |
2378 | 0 | } |
2379 | 22 | return _PyStatus_OK(); |
2380 | 22 | } |
2381 | | |
2382 | | static PyStatus |
2383 | | config_read_complex_options(PyConfig *config) |
2384 | 22 | { |
2385 | | /* More complex options configured by env var and -X option */ |
2386 | 22 | if (config->faulthandler < 0) { |
2387 | 22 | if (config_get_env(config, "PYTHONFAULTHANDLER") |
2388 | 22 | || config_get_xoption(config, L"faulthandler")) { |
2389 | 0 | config->faulthandler = 1; |
2390 | 0 | } |
2391 | 22 | } |
2392 | 22 | if (config_get_env(config, "PYTHONNODEBUGRANGES") |
2393 | 22 | || config_get_xoption(config, L"no_debug_ranges")) { |
2394 | 0 | config->code_debug_ranges = 0; |
2395 | 0 | } |
2396 | | |
2397 | 22 | PyStatus status; |
2398 | 22 | if (config->import_time < 0) { |
2399 | 22 | status = config_init_import_time(config); |
2400 | 22 | if (_PyStatus_EXCEPTION(status)) { |
2401 | 0 | return status; |
2402 | 0 | } |
2403 | 22 | } |
2404 | | |
2405 | 22 | if (config->lazy_imports < 0) { |
2406 | 22 | status = config_init_lazy_imports(config); |
2407 | 22 | if (_PyStatus_EXCEPTION(status)) { |
2408 | 0 | return status; |
2409 | 0 | } |
2410 | 22 | } |
2411 | | |
2412 | 22 | if (config->tracemalloc < 0) { |
2413 | 22 | status = config_init_tracemalloc(config); |
2414 | 22 | if (_PyStatus_EXCEPTION(status)) { |
2415 | 0 | return status; |
2416 | 0 | } |
2417 | 22 | } |
2418 | | |
2419 | 22 | if (config->perf_profiling < 0) { |
2420 | 22 | status = config_init_perf_profiling(config); |
2421 | 22 | if (_PyStatus_EXCEPTION(status)) { |
2422 | 0 | return status; |
2423 | 0 | } |
2424 | 22 | } |
2425 | | |
2426 | 22 | if (config->remote_debug < 0) { |
2427 | 22 | status = config_init_remote_debug(config); |
2428 | 22 | if (_PyStatus_EXCEPTION(status)) { |
2429 | 0 | return status; |
2430 | 0 | } |
2431 | 22 | } |
2432 | | |
2433 | 22 | if (config->int_max_str_digits < 0) { |
2434 | 0 | status = config_init_int_max_str_digits(config); |
2435 | 0 | if (_PyStatus_EXCEPTION(status)) { |
2436 | 0 | return status; |
2437 | 0 | } |
2438 | 0 | } |
2439 | | |
2440 | 22 | if (config->cpu_count < 0) { |
2441 | 22 | status = config_init_cpu_count(config); |
2442 | 22 | if (_PyStatus_EXCEPTION(status)) { |
2443 | 0 | return status; |
2444 | 0 | } |
2445 | 22 | } |
2446 | | |
2447 | 22 | if (config->pycache_prefix == NULL) { |
2448 | 22 | status = config_init_pycache_prefix(config); |
2449 | 22 | if (_PyStatus_EXCEPTION(status)) { |
2450 | 0 | return status; |
2451 | 0 | } |
2452 | 22 | } |
2453 | | |
2454 | | #ifdef Py_DEBUG |
2455 | | if (config->run_presite == NULL) { |
2456 | | status = config_init_run_presite(config); |
2457 | | if (_PyStatus_EXCEPTION(status)) { |
2458 | | return status; |
2459 | | } |
2460 | | } |
2461 | | #endif |
2462 | | |
2463 | 22 | status = config_init_thread_inherit_context(config); |
2464 | 22 | if (_PyStatus_EXCEPTION(status)) { |
2465 | 0 | return status; |
2466 | 0 | } |
2467 | | |
2468 | 22 | status = config_init_context_aware_warnings(config); |
2469 | 22 | if (_PyStatus_EXCEPTION(status)) { |
2470 | 0 | return status; |
2471 | 0 | } |
2472 | | |
2473 | 22 | status = config_init_tlbc(config); |
2474 | 22 | if (_PyStatus_EXCEPTION(status)) { |
2475 | 0 | return status; |
2476 | 0 | } |
2477 | | |
2478 | 22 | status = config_init_pathconfig_warnings(config); |
2479 | 22 | if (_PyStatus_EXCEPTION(status)) { |
2480 | 0 | return status; |
2481 | 0 | } |
2482 | | |
2483 | 22 | return _PyStatus_OK(); |
2484 | 22 | } |
2485 | | |
2486 | | |
2487 | | static const wchar_t * |
2488 | | config_get_stdio_errors(const PyPreConfig *preconfig) |
2489 | 22 | { |
2490 | 22 | if (preconfig->utf8_mode) { |
2491 | | /* UTF-8 Mode uses UTF-8/surrogateescape */ |
2492 | 22 | return L"surrogateescape"; |
2493 | 22 | } |
2494 | | |
2495 | 0 | #ifndef MS_WINDOWS |
2496 | 0 | const char *loc = setlocale(LC_CTYPE, NULL); |
2497 | 0 | if (loc != NULL) { |
2498 | | /* surrogateescape is the default in the legacy C and POSIX locales */ |
2499 | 0 | if (strcmp(loc, "C") == 0 || strcmp(loc, "POSIX") == 0) { |
2500 | 0 | return L"surrogateescape"; |
2501 | 0 | } |
2502 | | |
2503 | 0 | #ifdef PY_COERCE_C_LOCALE |
2504 | | /* surrogateescape is the default in locale coercion target locales */ |
2505 | 0 | if (_Py_IsLocaleCoercionTarget(loc)) { |
2506 | 0 | return L"surrogateescape"; |
2507 | 0 | } |
2508 | 0 | #endif |
2509 | 0 | } |
2510 | | |
2511 | 0 | return L"strict"; |
2512 | | #else |
2513 | | /* On Windows, always use surrogateescape by default */ |
2514 | | return L"surrogateescape"; |
2515 | | #endif |
2516 | 0 | } |
2517 | | |
2518 | | |
2519 | | // See also config_get_fs_encoding() |
2520 | | static PyStatus |
2521 | | config_get_locale_encoding(PyConfig *config, const PyPreConfig *preconfig, |
2522 | | wchar_t **locale_encoding) |
2523 | 22 | { |
2524 | 22 | wchar_t *encoding; |
2525 | 22 | if (preconfig->utf8_mode) { |
2526 | 22 | encoding = _PyMem_RawWcsdup(L"utf-8"); |
2527 | 22 | } |
2528 | 0 | else { |
2529 | 0 | encoding = _Py_GetLocaleEncoding(); |
2530 | 0 | } |
2531 | 22 | if (encoding == NULL) { |
2532 | 0 | return _PyStatus_NO_MEMORY(); |
2533 | 0 | } |
2534 | 22 | PyStatus status = PyConfig_SetString(config, locale_encoding, encoding); |
2535 | 22 | PyMem_RawFree(encoding); |
2536 | 22 | return status; |
2537 | 22 | } |
2538 | | |
2539 | | |
2540 | | static PyStatus |
2541 | | config_init_stdio_encoding(PyConfig *config, |
2542 | | const PyPreConfig *preconfig) |
2543 | 22 | { |
2544 | 22 | PyStatus status; |
2545 | | |
2546 | | // Exit if encoding and errors are defined |
2547 | 22 | if (config->stdio_encoding != NULL && config->stdio_errors != NULL) { |
2548 | 0 | return _PyStatus_OK(); |
2549 | 0 | } |
2550 | | |
2551 | | /* PYTHONIOENCODING environment variable */ |
2552 | 22 | const char *opt = config_get_env(config, "PYTHONIOENCODING"); |
2553 | 22 | if (opt) { |
2554 | 0 | char *pythonioencoding = _PyMem_RawStrdup(opt); |
2555 | 0 | if (pythonioencoding == NULL) { |
2556 | 0 | return _PyStatus_NO_MEMORY(); |
2557 | 0 | } |
2558 | | |
2559 | 0 | char *errors = strchr(pythonioencoding, ':'); |
2560 | 0 | if (errors) { |
2561 | 0 | *errors = '\0'; |
2562 | 0 | errors++; |
2563 | 0 | if (!errors[0]) { |
2564 | 0 | errors = NULL; |
2565 | 0 | } |
2566 | 0 | } |
2567 | | |
2568 | | /* Does PYTHONIOENCODING contain an encoding? */ |
2569 | 0 | if (pythonioencoding[0]) { |
2570 | 0 | if (config->stdio_encoding == NULL) { |
2571 | 0 | status = CONFIG_SET_BYTES_STR(config, &config->stdio_encoding, |
2572 | 0 | pythonioencoding, |
2573 | 0 | "PYTHONIOENCODING environment variable"); |
2574 | 0 | if (_PyStatus_EXCEPTION(status)) { |
2575 | 0 | PyMem_RawFree(pythonioencoding); |
2576 | 0 | return status; |
2577 | 0 | } |
2578 | 0 | } |
2579 | | |
2580 | | /* If the encoding is set but not the error handler, |
2581 | | use "strict" error handler by default. |
2582 | | PYTHONIOENCODING=latin1 behaves as |
2583 | | PYTHONIOENCODING=latin1:strict. */ |
2584 | 0 | if (!errors) { |
2585 | 0 | errors = "strict"; |
2586 | 0 | } |
2587 | 0 | } |
2588 | | |
2589 | 0 | if (config->stdio_errors == NULL && errors != NULL) { |
2590 | 0 | status = CONFIG_SET_BYTES_STR(config, &config->stdio_errors, |
2591 | 0 | errors, |
2592 | 0 | "PYTHONIOENCODING environment variable"); |
2593 | 0 | if (_PyStatus_EXCEPTION(status)) { |
2594 | 0 | PyMem_RawFree(pythonioencoding); |
2595 | 0 | return status; |
2596 | 0 | } |
2597 | 0 | } |
2598 | | |
2599 | 0 | PyMem_RawFree(pythonioencoding); |
2600 | 0 | } |
2601 | | |
2602 | | /* Choose the default error handler based on the current locale. */ |
2603 | 22 | if (config->stdio_encoding == NULL) { |
2604 | 22 | status = config_get_locale_encoding(config, preconfig, |
2605 | 22 | &config->stdio_encoding); |
2606 | 22 | if (_PyStatus_EXCEPTION(status)) { |
2607 | 0 | return status; |
2608 | 0 | } |
2609 | 22 | } |
2610 | 22 | if (config->stdio_errors == NULL) { |
2611 | 22 | const wchar_t *errors = config_get_stdio_errors(preconfig); |
2612 | 22 | assert(errors != NULL); |
2613 | | |
2614 | 22 | status = PyConfig_SetString(config, &config->stdio_errors, errors); |
2615 | 22 | if (_PyStatus_EXCEPTION(status)) { |
2616 | 0 | return status; |
2617 | 0 | } |
2618 | 22 | } |
2619 | | |
2620 | 22 | return _PyStatus_OK(); |
2621 | 22 | } |
2622 | | |
2623 | | |
2624 | | // See also config_get_locale_encoding() |
2625 | | static PyStatus |
2626 | | config_get_fs_encoding(PyConfig *config, const PyPreConfig *preconfig, |
2627 | | wchar_t **fs_encoding) |
2628 | 22 | { |
2629 | | #ifdef _Py_FORCE_UTF8_FS_ENCODING |
2630 | | return PyConfig_SetString(config, fs_encoding, L"utf-8"); |
2631 | | #elif defined(MS_WINDOWS) |
2632 | | const wchar_t *encoding; |
2633 | | if (preconfig->legacy_windows_fs_encoding) { |
2634 | | // Legacy Windows filesystem encoding: mbcs/replace |
2635 | | encoding = L"mbcs"; |
2636 | | } |
2637 | | else { |
2638 | | // Windows defaults to utf-8/surrogatepass (PEP 529) |
2639 | | encoding = L"utf-8"; |
2640 | | } |
2641 | | return PyConfig_SetString(config, fs_encoding, encoding); |
2642 | | #else // !MS_WINDOWS |
2643 | 22 | if (preconfig->utf8_mode) { |
2644 | 22 | return PyConfig_SetString(config, fs_encoding, L"utf-8"); |
2645 | 22 | } |
2646 | | |
2647 | 0 | if (_Py_GetForceASCII()) { |
2648 | 0 | return PyConfig_SetString(config, fs_encoding, L"ascii"); |
2649 | 0 | } |
2650 | | |
2651 | 0 | return config_get_locale_encoding(config, preconfig, fs_encoding); |
2652 | 0 | #endif // !MS_WINDOWS |
2653 | 0 | } |
2654 | | |
2655 | | |
2656 | | static PyStatus |
2657 | | config_init_fs_encoding(PyConfig *config, const PyPreConfig *preconfig) |
2658 | 22 | { |
2659 | 22 | PyStatus status; |
2660 | | |
2661 | 22 | if (config->filesystem_encoding == NULL) { |
2662 | 22 | status = config_get_fs_encoding(config, preconfig, |
2663 | 22 | &config->filesystem_encoding); |
2664 | 22 | if (_PyStatus_EXCEPTION(status)) { |
2665 | 0 | return status; |
2666 | 0 | } |
2667 | 22 | } |
2668 | | |
2669 | 22 | if (config->filesystem_errors == NULL) { |
2670 | 22 | const wchar_t *errors; |
2671 | | #ifdef MS_WINDOWS |
2672 | | if (preconfig->legacy_windows_fs_encoding) { |
2673 | | errors = L"replace"; |
2674 | | } |
2675 | | else { |
2676 | | errors = L"surrogatepass"; |
2677 | | } |
2678 | | #else |
2679 | 22 | errors = L"surrogateescape"; |
2680 | 22 | #endif |
2681 | 22 | status = PyConfig_SetString(config, &config->filesystem_errors, errors); |
2682 | 22 | if (_PyStatus_EXCEPTION(status)) { |
2683 | 0 | return status; |
2684 | 0 | } |
2685 | 22 | } |
2686 | 22 | return _PyStatus_OK(); |
2687 | 22 | } |
2688 | | |
2689 | | |
2690 | | static PyStatus |
2691 | | config_init_import(PyConfig *config, int compute_path_config) |
2692 | 44 | { |
2693 | 44 | PyStatus status; |
2694 | | |
2695 | 44 | status = _PyConfig_InitPathConfig(config, compute_path_config); |
2696 | 44 | if (_PyStatus_EXCEPTION(status)) { |
2697 | 0 | return status; |
2698 | 0 | } |
2699 | | |
2700 | 44 | const char *env = config_get_env(config, "PYTHON_FROZEN_MODULES"); |
2701 | 44 | if (env == NULL) { |
2702 | 44 | } |
2703 | 0 | else if (strcmp(env, "on") == 0) { |
2704 | 0 | config->use_frozen_modules = 1; |
2705 | 0 | } |
2706 | 0 | else if (strcmp(env, "off") == 0) { |
2707 | 0 | config->use_frozen_modules = 0; |
2708 | 0 | } else { |
2709 | 0 | return PyStatus_Error("bad value for PYTHON_FROZEN_MODULES " |
2710 | 0 | "(expected \"on\" or \"off\")"); |
2711 | 0 | } |
2712 | | |
2713 | | /* -X frozen_modules=[on|off] */ |
2714 | 44 | const wchar_t *value = config_get_xoption_value(config, L"frozen_modules"); |
2715 | 44 | if (value == NULL) { |
2716 | 44 | } |
2717 | 0 | else if (wcscmp(value, L"on") == 0) { |
2718 | 0 | config->use_frozen_modules = 1; |
2719 | 0 | } |
2720 | 0 | else if (wcscmp(value, L"off") == 0) { |
2721 | 0 | config->use_frozen_modules = 0; |
2722 | 0 | } |
2723 | 0 | else if (wcslen(value) == 0) { |
2724 | | // "-X frozen_modules" and "-X frozen_modules=" both imply "on". |
2725 | 0 | config->use_frozen_modules = 1; |
2726 | 0 | } |
2727 | 0 | else { |
2728 | 0 | return PyStatus_Error("bad value for option -X frozen_modules " |
2729 | 0 | "(expected \"on\" or \"off\")"); |
2730 | 0 | } |
2731 | | |
2732 | 44 | assert(config->use_frozen_modules >= 0); |
2733 | 44 | return _PyStatus_OK(); |
2734 | 44 | } |
2735 | | |
2736 | | PyStatus |
2737 | | _PyConfig_InitImportConfig(PyConfig *config) |
2738 | 22 | { |
2739 | 22 | return config_init_import(config, 1); |
2740 | 22 | } |
2741 | | |
2742 | | |
2743 | | static PyStatus |
2744 | | config_read(PyConfig *config, int compute_path_config) |
2745 | 22 | { |
2746 | 22 | PyStatus status; |
2747 | 22 | const PyPreConfig *preconfig = &_PyRuntime.preconfig; |
2748 | | |
2749 | 22 | if (config->use_environment) { |
2750 | 22 | status = config_read_env_vars(config); |
2751 | 22 | if (_PyStatus_EXCEPTION(status)) { |
2752 | 0 | return status; |
2753 | 0 | } |
2754 | 22 | } |
2755 | | |
2756 | | /* -X options */ |
2757 | 22 | if (config_get_xoption(config, L"showrefcount")) { |
2758 | 0 | config->show_ref_count = 1; |
2759 | 0 | } |
2760 | | |
2761 | 22 | const wchar_t *x_gil = config_get_xoption_value(config, L"gil"); |
2762 | 22 | if (x_gil != NULL) { |
2763 | 0 | size_t len = wcslen(x_gil); |
2764 | 0 | status = config_read_gil(config, len, x_gil[0]); |
2765 | 0 | if (_PyStatus_EXCEPTION(status)) { |
2766 | 0 | return status; |
2767 | 0 | } |
2768 | 0 | } |
2769 | | |
2770 | | #ifdef Py_STATS |
2771 | | if (config_get_xoption(config, L"pystats")) { |
2772 | | config->_pystats = 1; |
2773 | | } |
2774 | | else if (config_get_env(config, "PYTHONSTATS")) { |
2775 | | config->_pystats = 1; |
2776 | | } |
2777 | | if (config->_pystats < 0) { |
2778 | | config->_pystats = 0; |
2779 | | } |
2780 | | #endif |
2781 | | |
2782 | 22 | status = config_read_complex_options(config); |
2783 | 22 | if (_PyStatus_EXCEPTION(status)) { |
2784 | 0 | return status; |
2785 | 0 | } |
2786 | | |
2787 | 22 | if (config->_install_importlib) { |
2788 | 22 | status = config_init_import(config, compute_path_config); |
2789 | 22 | if (_PyStatus_EXCEPTION(status)) { |
2790 | 0 | return status; |
2791 | 0 | } |
2792 | 22 | } |
2793 | | |
2794 | | /* default values */ |
2795 | 22 | if (config->dev_mode) { |
2796 | 0 | if (config->faulthandler < 0) { |
2797 | 0 | config->faulthandler = 1; |
2798 | 0 | } |
2799 | 0 | } |
2800 | 22 | if (config->faulthandler < 0) { |
2801 | 22 | config->faulthandler = 0; |
2802 | 22 | } |
2803 | 22 | if (config->tracemalloc < 0) { |
2804 | 22 | config->tracemalloc = 0; |
2805 | 22 | } |
2806 | 22 | if (config->lazy_imports < 0) { |
2807 | 22 | config->lazy_imports = -1; // Default is auto/unset |
2808 | 22 | } |
2809 | 22 | if (config->perf_profiling < 0) { |
2810 | 22 | config->perf_profiling = 0; |
2811 | 22 | } |
2812 | 22 | if (config->remote_debug < 0) { |
2813 | 0 | config->remote_debug = -1; |
2814 | 0 | } |
2815 | 22 | if (config->use_hash_seed < 0) { |
2816 | 0 | config->use_hash_seed = 0; |
2817 | 0 | config->hash_seed = 0; |
2818 | 0 | } |
2819 | | |
2820 | 22 | if (config->filesystem_encoding == NULL || config->filesystem_errors == NULL) { |
2821 | 22 | status = config_init_fs_encoding(config, preconfig); |
2822 | 22 | if (_PyStatus_EXCEPTION(status)) { |
2823 | 0 | return status; |
2824 | 0 | } |
2825 | 22 | } |
2826 | | |
2827 | 22 | status = config_init_stdio_encoding(config, preconfig); |
2828 | 22 | if (_PyStatus_EXCEPTION(status)) { |
2829 | 0 | return status; |
2830 | 0 | } |
2831 | | |
2832 | 22 | if (config->argv.length < 1) { |
2833 | | /* Ensure at least one (empty) argument is seen */ |
2834 | 0 | status = PyWideStringList_Append(&config->argv, L""); |
2835 | 0 | if (_PyStatus_EXCEPTION(status)) { |
2836 | 0 | return status; |
2837 | 0 | } |
2838 | 0 | } |
2839 | | |
2840 | 22 | if (config->check_hash_pycs_mode == NULL) { |
2841 | 22 | status = PyConfig_SetString(config, &config->check_hash_pycs_mode, |
2842 | 22 | L"default"); |
2843 | 22 | if (_PyStatus_EXCEPTION(status)) { |
2844 | 0 | return status; |
2845 | 0 | } |
2846 | 22 | } |
2847 | | |
2848 | 22 | if (config->configure_c_stdio < 0) { |
2849 | 0 | config->configure_c_stdio = 1; |
2850 | 0 | } |
2851 | | |
2852 | | // Only parse arguments once. |
2853 | 22 | if (config->parse_argv == 1) { |
2854 | 22 | config->parse_argv = 2; |
2855 | 22 | } |
2856 | | |
2857 | 22 | return _PyStatus_OK(); |
2858 | 22 | } |
2859 | | |
2860 | | |
2861 | | static void |
2862 | | config_init_stdio(const PyConfig *config) |
2863 | 22 | { |
2864 | | #if defined(MS_WINDOWS) || defined(__CYGWIN__) |
2865 | | /* don't translate newlines (\r\n <=> \n) */ |
2866 | | _setmode(fileno(stdin), O_BINARY); |
2867 | | _setmode(fileno(stdout), O_BINARY); |
2868 | | _setmode(fileno(stderr), O_BINARY); |
2869 | | #endif |
2870 | | |
2871 | 22 | if (!config->buffered_stdio) { |
2872 | 0 | #ifdef HAVE_SETVBUF |
2873 | 0 | setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ); |
2874 | 0 | setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); |
2875 | 0 | setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ); |
2876 | | #else /* !HAVE_SETVBUF */ |
2877 | | setbuf(stdin, (char *)NULL); |
2878 | | setbuf(stdout, (char *)NULL); |
2879 | | setbuf(stderr, (char *)NULL); |
2880 | | #endif /* !HAVE_SETVBUF */ |
2881 | 0 | } |
2882 | 22 | else if (config->interactive) { |
2883 | | #ifdef MS_WINDOWS |
2884 | | /* Doesn't have to have line-buffered -- use unbuffered */ |
2885 | | /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */ |
2886 | | setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); |
2887 | | #else /* !MS_WINDOWS */ |
2888 | 0 | #ifdef HAVE_SETVBUF |
2889 | 0 | setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ); |
2890 | 0 | setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); |
2891 | 0 | #endif /* HAVE_SETVBUF */ |
2892 | 0 | #endif /* !MS_WINDOWS */ |
2893 | | /* Leave stderr alone - it should be unbuffered anyway. */ |
2894 | 0 | } |
2895 | 22 | } |
2896 | | |
2897 | | |
2898 | | /* Write the configuration: |
2899 | | |
2900 | | - set Py_xxx global configuration variables |
2901 | | - initialize C standard streams (stdin, stdout, stderr) */ |
2902 | | PyStatus |
2903 | | _PyConfig_Write(const PyConfig *config, _PyRuntimeState *runtime) |
2904 | 22 | { |
2905 | 22 | config_set_global_vars(config); |
2906 | | |
2907 | 22 | if (config->configure_c_stdio) { |
2908 | 22 | config_init_stdio(config); |
2909 | 22 | } |
2910 | | |
2911 | | /* Write the new pre-configuration into _PyRuntime */ |
2912 | 22 | PyPreConfig *preconfig = &runtime->preconfig; |
2913 | 22 | preconfig->isolated = config->isolated; |
2914 | 22 | preconfig->use_environment = config->use_environment; |
2915 | 22 | preconfig->dev_mode = config->dev_mode; |
2916 | | |
2917 | 22 | if (_Py_SetArgcArgv(config->orig_argv.length, |
2918 | 22 | config->orig_argv.items) < 0) |
2919 | 0 | { |
2920 | 0 | return _PyStatus_NO_MEMORY(); |
2921 | 0 | } |
2922 | | |
2923 | | #ifdef PYMALLOC_USE_HUGEPAGES |
2924 | | runtime->allocators.use_hugepages = config->pymalloc_hugepages; |
2925 | | #endif |
2926 | | |
2927 | 22 | return _PyStatus_OK(); |
2928 | 22 | } |
2929 | | |
2930 | | |
2931 | | /* --- PyConfig command line parser -------------------------- */ |
2932 | | |
2933 | | static void |
2934 | | config_usage(int error, const wchar_t* program) |
2935 | 0 | { |
2936 | 0 | FILE *f = error ? stderr : stdout; |
2937 | |
|
2938 | 0 | fprintf(f, usage_line, program); |
2939 | 0 | if (error) |
2940 | 0 | fprintf(f, "Try `python -h' for more information.\n"); |
2941 | 0 | else { |
2942 | 0 | fputs(usage_help, f); |
2943 | 0 | } |
2944 | 0 | } |
2945 | | |
2946 | | static void |
2947 | | config_envvars_usage(void) |
2948 | 0 | { |
2949 | 0 | printf(usage_envvars, (wint_t)DELIM, (wint_t)DELIM, PYTHONHOMEHELP); |
2950 | 0 | } |
2951 | | |
2952 | | static void |
2953 | | config_xoptions_usage(void) |
2954 | 0 | { |
2955 | 0 | puts(usage_xoptions); |
2956 | 0 | } |
2957 | | |
2958 | | static void |
2959 | | config_complete_usage(const wchar_t* program) |
2960 | 0 | { |
2961 | 0 | config_usage(0, program); |
2962 | 0 | putchar('\n'); |
2963 | 0 | config_envvars_usage(); |
2964 | 0 | putchar('\n'); |
2965 | 0 | config_xoptions_usage(); |
2966 | 0 | } |
2967 | | |
2968 | | |
2969 | | /* Parse the command line arguments */ |
2970 | | static PyStatus |
2971 | | config_parse_cmdline(PyConfig *config, PyWideStringList *warnoptions, |
2972 | | Py_ssize_t *opt_index) |
2973 | 22 | { |
2974 | 22 | PyStatus status; |
2975 | 22 | const PyWideStringList *argv = &config->argv; |
2976 | 22 | int print_version = 0; |
2977 | 22 | const wchar_t* program = config->program_name; |
2978 | 22 | if (!program && argv->length >= 1) { |
2979 | 0 | program = argv->items[0]; |
2980 | 0 | } |
2981 | | |
2982 | 22 | _PyOS_ResetGetOpt(); |
2983 | 22 | do { |
2984 | 22 | int longindex = -1; |
2985 | 22 | int c = _PyOS_GetOpt(argv->length, argv->items, &longindex); |
2986 | 22 | if (c == EOF) { |
2987 | 22 | break; |
2988 | 22 | } |
2989 | | |
2990 | 0 | if (c == 'c') { |
2991 | 0 | if (config->run_command == NULL) { |
2992 | | /* -c is the last option; following arguments |
2993 | | that look like options are left for the |
2994 | | command to interpret. */ |
2995 | 0 | size_t len = wcslen(_PyOS_optarg) + 1 + 1; |
2996 | 0 | wchar_t *command = PyMem_RawMalloc(sizeof(wchar_t) * len); |
2997 | 0 | if (command == NULL) { |
2998 | 0 | return _PyStatus_NO_MEMORY(); |
2999 | 0 | } |
3000 | 0 | memcpy(command, _PyOS_optarg, (len - 2) * sizeof(wchar_t)); |
3001 | 0 | command[len - 2] = '\n'; |
3002 | 0 | command[len - 1] = 0; |
3003 | 0 | config->run_command = command; |
3004 | 0 | } |
3005 | 0 | break; |
3006 | 0 | } |
3007 | | |
3008 | 0 | if (c == 'm') { |
3009 | | /* -m is the last option; following arguments |
3010 | | that look like options are left for the |
3011 | | module to interpret. */ |
3012 | 0 | if (config->run_module == NULL) { |
3013 | 0 | config->run_module = _PyMem_RawWcsdup(_PyOS_optarg); |
3014 | 0 | if (config->run_module == NULL) { |
3015 | 0 | return _PyStatus_NO_MEMORY(); |
3016 | 0 | } |
3017 | 0 | } |
3018 | 0 | break; |
3019 | 0 | } |
3020 | | |
3021 | 0 | switch (c) { |
3022 | | // Integers represent long options, see Python/getopt.c |
3023 | 0 | case 0: |
3024 | | // check-hash-based-pycs |
3025 | 0 | if (wcscmp(_PyOS_optarg, L"always") == 0 |
3026 | 0 | || wcscmp(_PyOS_optarg, L"never") == 0 |
3027 | 0 | || wcscmp(_PyOS_optarg, L"default") == 0) |
3028 | 0 | { |
3029 | 0 | status = PyConfig_SetString(config, &config->check_hash_pycs_mode, |
3030 | 0 | _PyOS_optarg); |
3031 | 0 | if (_PyStatus_EXCEPTION(status)) { |
3032 | 0 | return status; |
3033 | 0 | } |
3034 | 0 | } else { |
3035 | 0 | fprintf(stderr, "--check-hash-based-pycs must be one of " |
3036 | 0 | "'default', 'always', or 'never'\n"); |
3037 | 0 | config_usage(1, program); |
3038 | 0 | return _PyStatus_EXIT(2); |
3039 | 0 | } |
3040 | 0 | break; |
3041 | | |
3042 | 0 | case 1: |
3043 | | // help-all |
3044 | 0 | config_complete_usage(program); |
3045 | 0 | return _PyStatus_EXIT(0); |
3046 | | |
3047 | 0 | case 2: |
3048 | | // help-env |
3049 | 0 | config_envvars_usage(); |
3050 | 0 | return _PyStatus_EXIT(0); |
3051 | | |
3052 | 0 | case 3: |
3053 | | // help-xoptions |
3054 | 0 | config_xoptions_usage(); |
3055 | 0 | return _PyStatus_EXIT(0); |
3056 | | |
3057 | 0 | case 'b': |
3058 | 0 | config->bytes_warning++; |
3059 | 0 | break; |
3060 | | |
3061 | 0 | case 'd': |
3062 | 0 | config->parser_debug++; |
3063 | 0 | break; |
3064 | | |
3065 | 0 | case 'i': |
3066 | 0 | config->inspect++; |
3067 | 0 | config->interactive++; |
3068 | 0 | break; |
3069 | | |
3070 | 0 | case 'E': |
3071 | 0 | case 'I': |
3072 | 0 | case 'X': |
3073 | | /* option handled by _PyPreCmdline_Read() */ |
3074 | 0 | break; |
3075 | | |
3076 | 0 | case 'O': |
3077 | 0 | config->optimization_level++; |
3078 | 0 | break; |
3079 | | |
3080 | 0 | case 'P': |
3081 | 0 | config->safe_path = 1; |
3082 | 0 | break; |
3083 | | |
3084 | 0 | case 'B': |
3085 | 0 | config->write_bytecode = 0; |
3086 | 0 | break; |
3087 | | |
3088 | 0 | case 's': |
3089 | 0 | config->user_site_directory = 0; |
3090 | 0 | break; |
3091 | | |
3092 | 0 | case 'S': |
3093 | 0 | config->site_import = 0; |
3094 | 0 | break; |
3095 | | |
3096 | 0 | case 't': |
3097 | | /* ignored for backwards compatibility */ |
3098 | 0 | break; |
3099 | | |
3100 | 0 | case 'u': |
3101 | 0 | config->buffered_stdio = 0; |
3102 | 0 | break; |
3103 | | |
3104 | 0 | case 'v': |
3105 | 0 | config->verbose++; |
3106 | 0 | break; |
3107 | | |
3108 | 0 | case 'x': |
3109 | 0 | config->skip_source_first_line = 1; |
3110 | 0 | break; |
3111 | | |
3112 | 0 | case 'h': |
3113 | 0 | case '?': |
3114 | 0 | config_usage(0, program); |
3115 | 0 | return _PyStatus_EXIT(0); |
3116 | | |
3117 | 0 | case 'V': |
3118 | 0 | print_version++; |
3119 | 0 | break; |
3120 | | |
3121 | 0 | case 'W': |
3122 | 0 | status = PyWideStringList_Append(warnoptions, _PyOS_optarg); |
3123 | 0 | if (_PyStatus_EXCEPTION(status)) { |
3124 | 0 | return status; |
3125 | 0 | } |
3126 | 0 | break; |
3127 | | |
3128 | 0 | case 'q': |
3129 | 0 | config->quiet++; |
3130 | 0 | break; |
3131 | | |
3132 | 0 | case 'R': |
3133 | 0 | config->use_hash_seed = 0; |
3134 | 0 | break; |
3135 | | |
3136 | | /* This space reserved for other options */ |
3137 | | |
3138 | 0 | default: |
3139 | | /* unknown argument: parsing failed */ |
3140 | 0 | config_usage(1, program); |
3141 | 0 | return _PyStatus_EXIT(2); |
3142 | 0 | } |
3143 | 0 | } while (1); |
3144 | | |
3145 | 22 | if (print_version) { |
3146 | 0 | printf("Python %s\n", |
3147 | 0 | (print_version >= 2) ? Py_GetVersion() : PY_VERSION); |
3148 | 0 | return _PyStatus_EXIT(0); |
3149 | 0 | } |
3150 | | |
3151 | 22 | if (config->run_command == NULL && config->run_module == NULL |
3152 | 22 | && _PyOS_optind < argv->length |
3153 | 0 | && wcscmp(argv->items[_PyOS_optind], L"-") != 0 |
3154 | 0 | && config->run_filename == NULL) |
3155 | 0 | { |
3156 | 0 | config->run_filename = _PyMem_RawWcsdup(argv->items[_PyOS_optind]); |
3157 | 0 | if (config->run_filename == NULL) { |
3158 | 0 | return _PyStatus_NO_MEMORY(); |
3159 | 0 | } |
3160 | 0 | } |
3161 | | |
3162 | 22 | if (config->run_command != NULL || config->run_module != NULL) { |
3163 | | /* Backup _PyOS_optind */ |
3164 | 0 | _PyOS_optind--; |
3165 | 0 | } |
3166 | | |
3167 | 22 | *opt_index = _PyOS_optind; |
3168 | | |
3169 | 22 | return _PyStatus_OK(); |
3170 | 22 | } |
3171 | | |
3172 | | |
3173 | | #ifdef MS_WINDOWS |
3174 | | # define WCSTOK wcstok_s |
3175 | | #else |
3176 | 0 | # define WCSTOK wcstok |
3177 | | #endif |
3178 | | |
3179 | | /* Get warning options from PYTHONWARNINGS environment variable. */ |
3180 | | static PyStatus |
3181 | | config_init_env_warnoptions(PyConfig *config, PyWideStringList *warnoptions) |
3182 | 22 | { |
3183 | 22 | PyStatus status; |
3184 | | /* CONFIG_GET_ENV_DUP requires dest to be initialized to NULL */ |
3185 | 22 | wchar_t *env = NULL; |
3186 | 22 | status = CONFIG_GET_ENV_DUP(config, &env, |
3187 | 22 | L"PYTHONWARNINGS", "PYTHONWARNINGS"); |
3188 | 22 | if (_PyStatus_EXCEPTION(status)) { |
3189 | 0 | return status; |
3190 | 0 | } |
3191 | | |
3192 | | /* env var is not set or is empty */ |
3193 | 22 | if (env == NULL) { |
3194 | 22 | return _PyStatus_OK(); |
3195 | 22 | } |
3196 | | |
3197 | | |
3198 | 0 | wchar_t *warning, *context = NULL; |
3199 | 0 | for (warning = WCSTOK(env, L",", &context); |
3200 | 0 | warning != NULL; |
3201 | 0 | warning = WCSTOK(NULL, L",", &context)) |
3202 | 0 | { |
3203 | 0 | status = PyWideStringList_Append(warnoptions, warning); |
3204 | 0 | if (_PyStatus_EXCEPTION(status)) { |
3205 | 0 | PyMem_RawFree(env); |
3206 | 0 | return status; |
3207 | 0 | } |
3208 | 0 | } |
3209 | 0 | PyMem_RawFree(env); |
3210 | 0 | return _PyStatus_OK(); |
3211 | 0 | } |
3212 | | |
3213 | | |
3214 | | static PyStatus |
3215 | | warnoptions_append(PyConfig *config, PyWideStringList *options, |
3216 | | const wchar_t *option) |
3217 | 0 | { |
3218 | | /* config_init_warnoptions() add existing config warnoptions at the end: |
3219 | | ensure that the new option is not already present in this list to |
3220 | | prevent change the options order when config_init_warnoptions() is |
3221 | | called twice. */ |
3222 | 0 | if (_PyWideStringList_Find(&config->warnoptions, option)) { |
3223 | | /* Already present: do nothing */ |
3224 | 0 | return _PyStatus_OK(); |
3225 | 0 | } |
3226 | 0 | if (_PyWideStringList_Find(options, option)) { |
3227 | | /* Already present: do nothing */ |
3228 | 0 | return _PyStatus_OK(); |
3229 | 0 | } |
3230 | 0 | return PyWideStringList_Append(options, option); |
3231 | 0 | } |
3232 | | |
3233 | | |
3234 | | static PyStatus |
3235 | | warnoptions_extend(PyConfig *config, PyWideStringList *options, |
3236 | | const PyWideStringList *options2) |
3237 | 66 | { |
3238 | 66 | const Py_ssize_t len = options2->length; |
3239 | 66 | wchar_t *const *items = options2->items; |
3240 | | |
3241 | 66 | for (Py_ssize_t i = 0; i < len; i++) { |
3242 | 0 | PyStatus status = warnoptions_append(config, options, items[i]); |
3243 | 0 | if (_PyStatus_EXCEPTION(status)) { |
3244 | 0 | return status; |
3245 | 0 | } |
3246 | 0 | } |
3247 | 66 | return _PyStatus_OK(); |
3248 | 66 | } |
3249 | | |
3250 | | |
3251 | | static PyStatus |
3252 | | config_init_warnoptions(PyConfig *config, |
3253 | | const PyWideStringList *cmdline_warnoptions, |
3254 | | const PyWideStringList *env_warnoptions, |
3255 | | const PyWideStringList *sys_warnoptions) |
3256 | 22 | { |
3257 | 22 | PyStatus status; |
3258 | 22 | PyWideStringList options = _PyWideStringList_INIT; |
3259 | | |
3260 | | /* Priority of warnings options, lowest to highest: |
3261 | | * |
3262 | | * - any implicit filters added by _warnings.c/warnings.py |
3263 | | * - PyConfig.dev_mode: "default" filter |
3264 | | * - PYTHONWARNINGS environment variable |
3265 | | * - '-W' command line options |
3266 | | * - PyConfig.bytes_warning ('-b' and '-bb' command line options): |
3267 | | * "default::BytesWarning" or "error::BytesWarning" filter |
3268 | | * - early PySys_AddWarnOption() calls |
3269 | | * - PyConfig.warnoptions |
3270 | | * |
3271 | | * PyConfig.warnoptions is copied to sys.warnoptions. Since the warnings |
3272 | | * module works on the basis of "the most recently added filter will be |
3273 | | * checked first", we add the lowest precedence entries first so that later |
3274 | | * entries override them. |
3275 | | */ |
3276 | | |
3277 | 22 | if (config->dev_mode) { |
3278 | 0 | status = warnoptions_append(config, &options, L"default"); |
3279 | 0 | if (_PyStatus_EXCEPTION(status)) { |
3280 | 0 | goto error; |
3281 | 0 | } |
3282 | 0 | } |
3283 | | |
3284 | 22 | status = warnoptions_extend(config, &options, env_warnoptions); |
3285 | 22 | if (_PyStatus_EXCEPTION(status)) { |
3286 | 0 | goto error; |
3287 | 0 | } |
3288 | | |
3289 | 22 | status = warnoptions_extend(config, &options, cmdline_warnoptions); |
3290 | 22 | if (_PyStatus_EXCEPTION(status)) { |
3291 | 0 | goto error; |
3292 | 0 | } |
3293 | | |
3294 | | /* If the bytes_warning_flag isn't set, bytesobject.c and bytearrayobject.c |
3295 | | * don't even try to emit a warning, so we skip setting the filter in that |
3296 | | * case. |
3297 | | */ |
3298 | 22 | if (config->bytes_warning) { |
3299 | 0 | const wchar_t *filter; |
3300 | 0 | if (config->bytes_warning> 1) { |
3301 | 0 | filter = L"error::BytesWarning"; |
3302 | 0 | } |
3303 | 0 | else { |
3304 | 0 | filter = L"default::BytesWarning"; |
3305 | 0 | } |
3306 | 0 | status = warnoptions_append(config, &options, filter); |
3307 | 0 | if (_PyStatus_EXCEPTION(status)) { |
3308 | 0 | goto error; |
3309 | 0 | } |
3310 | 0 | } |
3311 | | |
3312 | 22 | status = warnoptions_extend(config, &options, sys_warnoptions); |
3313 | 22 | if (_PyStatus_EXCEPTION(status)) { |
3314 | 0 | goto error; |
3315 | 0 | } |
3316 | | |
3317 | | /* Always add all PyConfig.warnoptions options */ |
3318 | 22 | status = _PyWideStringList_Extend(&options, &config->warnoptions); |
3319 | 22 | if (_PyStatus_EXCEPTION(status)) { |
3320 | 0 | goto error; |
3321 | 0 | } |
3322 | | |
3323 | 22 | _PyWideStringList_Clear(&config->warnoptions); |
3324 | 22 | config->warnoptions = options; |
3325 | 22 | return _PyStatus_OK(); |
3326 | | |
3327 | 0 | error: |
3328 | 0 | _PyWideStringList_Clear(&options); |
3329 | 0 | return status; |
3330 | 22 | } |
3331 | | |
3332 | | |
3333 | | static PyStatus |
3334 | | config_update_argv(PyConfig *config, Py_ssize_t opt_index) |
3335 | 22 | { |
3336 | 22 | const PyWideStringList *cmdline_argv = &config->argv; |
3337 | 22 | PyWideStringList config_argv = _PyWideStringList_INIT; |
3338 | | |
3339 | | /* Copy argv to be able to modify it (to force -c/-m) */ |
3340 | 22 | if (cmdline_argv->length <= opt_index) { |
3341 | | /* Ensure at least one (empty) argument is seen */ |
3342 | 22 | PyStatus status = PyWideStringList_Append(&config_argv, L""); |
3343 | 22 | if (_PyStatus_EXCEPTION(status)) { |
3344 | 0 | return status; |
3345 | 0 | } |
3346 | 22 | } |
3347 | 0 | else { |
3348 | 0 | PyWideStringList slice; |
3349 | 0 | slice.length = cmdline_argv->length - opt_index; |
3350 | 0 | slice.items = &cmdline_argv->items[opt_index]; |
3351 | 0 | if (_PyWideStringList_Copy(&config_argv, &slice) < 0) { |
3352 | 0 | return _PyStatus_NO_MEMORY(); |
3353 | 0 | } |
3354 | 0 | } |
3355 | 22 | assert(config_argv.length >= 1); |
3356 | | |
3357 | 22 | wchar_t *arg0 = NULL; |
3358 | 22 | if (config->run_command != NULL) { |
3359 | | /* Force sys.argv[0] = '-c' */ |
3360 | 0 | arg0 = L"-c"; |
3361 | 0 | } |
3362 | 22 | else if (config->run_module != NULL) { |
3363 | | /* Force sys.argv[0] = '-m'*/ |
3364 | 0 | arg0 = L"-m"; |
3365 | 0 | } |
3366 | | |
3367 | 22 | if (arg0 != NULL) { |
3368 | 0 | arg0 = _PyMem_RawWcsdup(arg0); |
3369 | 0 | if (arg0 == NULL) { |
3370 | 0 | _PyWideStringList_Clear(&config_argv); |
3371 | 0 | return _PyStatus_NO_MEMORY(); |
3372 | 0 | } |
3373 | | |
3374 | 0 | PyMem_RawFree(config_argv.items[0]); |
3375 | 0 | config_argv.items[0] = arg0; |
3376 | 0 | } |
3377 | | |
3378 | 22 | _PyWideStringList_Clear(&config->argv); |
3379 | 22 | config->argv = config_argv; |
3380 | 22 | return _PyStatus_OK(); |
3381 | 22 | } |
3382 | | |
3383 | | |
3384 | | static PyStatus |
3385 | | core_read_precmdline(PyConfig *config, _PyPreCmdline *precmdline) |
3386 | 22 | { |
3387 | 22 | PyStatus status; |
3388 | | |
3389 | 22 | if (config->parse_argv == 1) { |
3390 | 22 | if (_PyWideStringList_Copy(&precmdline->argv, &config->argv) < 0) { |
3391 | 0 | return _PyStatus_NO_MEMORY(); |
3392 | 0 | } |
3393 | 22 | } |
3394 | | |
3395 | 22 | PyPreConfig preconfig; |
3396 | | |
3397 | 22 | status = _PyPreConfig_InitFromPreConfig(&preconfig, &_PyRuntime.preconfig); |
3398 | 22 | if (_PyStatus_EXCEPTION(status)) { |
3399 | 0 | return status; |
3400 | 0 | } |
3401 | | |
3402 | 22 | _PyPreConfig_GetConfig(&preconfig, config); |
3403 | | |
3404 | 22 | status = _PyPreCmdline_Read(precmdline, &preconfig); |
3405 | 22 | if (_PyStatus_EXCEPTION(status)) { |
3406 | 0 | return status; |
3407 | 0 | } |
3408 | | |
3409 | 22 | status = _PyPreCmdline_SetConfig(precmdline, config); |
3410 | 22 | if (_PyStatus_EXCEPTION(status)) { |
3411 | 0 | return status; |
3412 | 0 | } |
3413 | 22 | return _PyStatus_OK(); |
3414 | 22 | } |
3415 | | |
3416 | | |
3417 | | /* Get run_filename absolute path */ |
3418 | | static PyStatus |
3419 | | config_run_filename_abspath(PyConfig *config) |
3420 | 22 | { |
3421 | 22 | if (!config->run_filename) { |
3422 | 22 | return _PyStatus_OK(); |
3423 | 22 | } |
3424 | | |
3425 | 0 | #ifndef MS_WINDOWS |
3426 | 0 | if (_Py_isabs(config->run_filename)) { |
3427 | | /* path is already absolute */ |
3428 | 0 | return _PyStatus_OK(); |
3429 | 0 | } |
3430 | 0 | #endif |
3431 | | |
3432 | 0 | wchar_t *abs_filename; |
3433 | 0 | if (_Py_abspath(config->run_filename, &abs_filename) < 0) { |
3434 | | /* failed to get the absolute path of the command line filename: |
3435 | | ignore the error, keep the relative path */ |
3436 | 0 | return _PyStatus_OK(); |
3437 | 0 | } |
3438 | 0 | if (abs_filename == NULL) { |
3439 | 0 | return _PyStatus_NO_MEMORY(); |
3440 | 0 | } |
3441 | | |
3442 | 0 | PyMem_RawFree(config->run_filename); |
3443 | 0 | config->run_filename = abs_filename; |
3444 | 0 | return _PyStatus_OK(); |
3445 | 0 | } |
3446 | | |
3447 | | |
3448 | | static PyStatus |
3449 | | config_read_cmdline(PyConfig *config) |
3450 | 22 | { |
3451 | 22 | PyStatus status; |
3452 | 22 | PyWideStringList cmdline_warnoptions = _PyWideStringList_INIT; |
3453 | 22 | PyWideStringList env_warnoptions = _PyWideStringList_INIT; |
3454 | 22 | PyWideStringList sys_warnoptions = _PyWideStringList_INIT; |
3455 | | |
3456 | 22 | if (config->parse_argv < 0) { |
3457 | 0 | config->parse_argv = 1; |
3458 | 0 | } |
3459 | | |
3460 | 22 | if (config->parse_argv == 1) { |
3461 | 22 | Py_ssize_t opt_index; |
3462 | 22 | status = config_parse_cmdline(config, &cmdline_warnoptions, &opt_index); |
3463 | 22 | if (_PyStatus_EXCEPTION(status)) { |
3464 | 0 | goto done; |
3465 | 0 | } |
3466 | | |
3467 | 22 | status = config_run_filename_abspath(config); |
3468 | 22 | if (_PyStatus_EXCEPTION(status)) { |
3469 | 0 | goto done; |
3470 | 0 | } |
3471 | | |
3472 | 22 | status = config_update_argv(config, opt_index); |
3473 | 22 | if (_PyStatus_EXCEPTION(status)) { |
3474 | 0 | goto done; |
3475 | 0 | } |
3476 | 22 | } |
3477 | 0 | else { |
3478 | 0 | status = config_run_filename_abspath(config); |
3479 | 0 | if (_PyStatus_EXCEPTION(status)) { |
3480 | 0 | goto done; |
3481 | 0 | } |
3482 | 0 | } |
3483 | | |
3484 | 22 | if (config->use_environment) { |
3485 | 22 | status = config_init_env_warnoptions(config, &env_warnoptions); |
3486 | 22 | if (_PyStatus_EXCEPTION(status)) { |
3487 | 0 | goto done; |
3488 | 0 | } |
3489 | 22 | } |
3490 | | |
3491 | | /* Handle early PySys_AddWarnOption() calls */ |
3492 | 22 | status = _PySys_ReadPreinitWarnOptions(&sys_warnoptions); |
3493 | 22 | if (_PyStatus_EXCEPTION(status)) { |
3494 | 0 | goto done; |
3495 | 0 | } |
3496 | | |
3497 | 22 | status = config_init_warnoptions(config, |
3498 | 22 | &cmdline_warnoptions, |
3499 | 22 | &env_warnoptions, |
3500 | 22 | &sys_warnoptions); |
3501 | 22 | if (_PyStatus_EXCEPTION(status)) { |
3502 | 0 | goto done; |
3503 | 0 | } |
3504 | | |
3505 | 22 | status = _PyStatus_OK(); |
3506 | | |
3507 | 22 | done: |
3508 | 22 | _PyWideStringList_Clear(&cmdline_warnoptions); |
3509 | 22 | _PyWideStringList_Clear(&env_warnoptions); |
3510 | 22 | _PyWideStringList_Clear(&sys_warnoptions); |
3511 | 22 | return status; |
3512 | 22 | } |
3513 | | |
3514 | | |
3515 | | PyStatus |
3516 | | _PyConfig_SetPyArgv(PyConfig *config, const _PyArgv *args) |
3517 | 0 | { |
3518 | 0 | PyStatus status = _Py_PreInitializeFromConfig(config, args); |
3519 | 0 | if (_PyStatus_EXCEPTION(status)) { |
3520 | 0 | return status; |
3521 | 0 | } |
3522 | | |
3523 | 0 | return _PyArgv_AsWstrList(args, &config->argv); |
3524 | 0 | } |
3525 | | |
3526 | | |
3527 | | /* Set config.argv: decode argv using Py_DecodeLocale(). Pre-initialize Python |
3528 | | if needed to ensure that encodings are properly configured. */ |
3529 | | PyStatus |
3530 | | PyConfig_SetBytesArgv(PyConfig *config, Py_ssize_t argc, char * const *argv) |
3531 | 0 | { |
3532 | 0 | _PyArgv args = { |
3533 | 0 | .argc = argc, |
3534 | 0 | .use_bytes_argv = 1, |
3535 | 0 | .bytes_argv = argv, |
3536 | 0 | .wchar_argv = NULL}; |
3537 | 0 | return _PyConfig_SetPyArgv(config, &args); |
3538 | 0 | } |
3539 | | |
3540 | | |
3541 | | PyStatus |
3542 | | PyConfig_SetArgv(PyConfig *config, Py_ssize_t argc, wchar_t * const *argv) |
3543 | 0 | { |
3544 | 0 | _PyArgv args = { |
3545 | 0 | .argc = argc, |
3546 | 0 | .use_bytes_argv = 0, |
3547 | 0 | .bytes_argv = NULL, |
3548 | 0 | .wchar_argv = argv}; |
3549 | 0 | return _PyConfig_SetPyArgv(config, &args); |
3550 | 0 | } |
3551 | | |
3552 | | |
3553 | | PyStatus |
3554 | | PyConfig_SetWideStringList(PyConfig *config, PyWideStringList *list, |
3555 | | Py_ssize_t length, wchar_t **items) |
3556 | 0 | { |
3557 | 0 | PyStatus status = _Py_PreInitializeFromConfig(config, NULL); |
3558 | 0 | if (_PyStatus_EXCEPTION(status)) { |
3559 | 0 | return status; |
3560 | 0 | } |
3561 | | |
3562 | 0 | PyWideStringList list2 = {.length = length, .items = items}; |
3563 | 0 | if (_PyWideStringList_Copy(list, &list2) < 0) { |
3564 | 0 | return _PyStatus_NO_MEMORY(); |
3565 | 0 | } |
3566 | 0 | return _PyStatus_OK(); |
3567 | 0 | } |
3568 | | |
3569 | | |
3570 | | /* Read the configuration into PyConfig from: |
3571 | | |
3572 | | * Command line arguments |
3573 | | * Environment variables |
3574 | | * Py_xxx global configuration variables |
3575 | | |
3576 | | The only side effects are to modify config and to call _Py_SetArgcArgv(). */ |
3577 | | PyStatus |
3578 | | _PyConfig_Read(PyConfig *config, int compute_path_config) |
3579 | 22 | { |
3580 | 22 | PyStatus status; |
3581 | | |
3582 | 22 | status = _Py_PreInitializeFromConfig(config, NULL); |
3583 | 22 | if (_PyStatus_EXCEPTION(status)) { |
3584 | 0 | return status; |
3585 | 0 | } |
3586 | | |
3587 | 22 | config_get_global_vars(config); |
3588 | | |
3589 | 22 | if (config->orig_argv.length == 0 |
3590 | 22 | && !(config->argv.length == 1 |
3591 | 0 | && wcscmp(config->argv.items[0], L"") == 0)) |
3592 | 22 | { |
3593 | 22 | if (_PyWideStringList_Copy(&config->orig_argv, &config->argv) < 0) { |
3594 | 0 | return _PyStatus_NO_MEMORY(); |
3595 | 0 | } |
3596 | 22 | } |
3597 | | |
3598 | 22 | _PyPreCmdline precmdline = _PyPreCmdline_INIT; |
3599 | 22 | status = core_read_precmdline(config, &precmdline); |
3600 | 22 | if (_PyStatus_EXCEPTION(status)) { |
3601 | 0 | goto done; |
3602 | 0 | } |
3603 | | |
3604 | 22 | assert(config->isolated >= 0); |
3605 | 22 | if (config->isolated) { |
3606 | 0 | config->safe_path = 1; |
3607 | 0 | config->use_environment = 0; |
3608 | 0 | config->user_site_directory = 0; |
3609 | 0 | } |
3610 | | |
3611 | 22 | status = config_read_cmdline(config); |
3612 | 22 | if (_PyStatus_EXCEPTION(status)) { |
3613 | 0 | goto done; |
3614 | 0 | } |
3615 | | |
3616 | | /* Handle early PySys_AddXOption() calls */ |
3617 | 22 | status = _PySys_ReadPreinitXOptions(config); |
3618 | 22 | if (_PyStatus_EXCEPTION(status)) { |
3619 | 0 | goto done; |
3620 | 0 | } |
3621 | | |
3622 | 22 | status = config_read(config, compute_path_config); |
3623 | 22 | if (_PyStatus_EXCEPTION(status)) { |
3624 | 0 | goto done; |
3625 | 0 | } |
3626 | | |
3627 | 22 | assert(config_check_consistency(config)); |
3628 | | |
3629 | 22 | status = _PyStatus_OK(); |
3630 | | |
3631 | 22 | done: |
3632 | 22 | _PyPreCmdline_Clear(&precmdline); |
3633 | 22 | return status; |
3634 | 22 | } |
3635 | | |
3636 | | |
3637 | | PyStatus |
3638 | | PyConfig_Read(PyConfig *config) |
3639 | 0 | { |
3640 | 0 | return _PyConfig_Read(config, 0); |
3641 | 0 | } |
3642 | | |
3643 | | |
3644 | | PyObject* |
3645 | | _Py_GetConfigsAsDict(void) |
3646 | 0 | { |
3647 | 0 | PyObject *result = NULL; |
3648 | 0 | PyObject *dict = NULL; |
3649 | |
|
3650 | 0 | result = PyDict_New(); |
3651 | 0 | if (result == NULL) { |
3652 | 0 | goto error; |
3653 | 0 | } |
3654 | | |
3655 | | /* global result */ |
3656 | 0 | dict = _Py_GetGlobalVariablesAsDict(); |
3657 | 0 | if (dict == NULL) { |
3658 | 0 | goto error; |
3659 | 0 | } |
3660 | 0 | if (PyDict_SetItemString(result, "global_config", dict) < 0) { |
3661 | 0 | goto error; |
3662 | 0 | } |
3663 | 0 | Py_CLEAR(dict); |
3664 | | |
3665 | | /* pre config */ |
3666 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
3667 | 0 | const PyPreConfig *pre_config = &interp->runtime->preconfig; |
3668 | 0 | dict = _PyPreConfig_AsDict(pre_config); |
3669 | 0 | if (dict == NULL) { |
3670 | 0 | goto error; |
3671 | 0 | } |
3672 | 0 | if (PyDict_SetItemString(result, "pre_config", dict) < 0) { |
3673 | 0 | goto error; |
3674 | 0 | } |
3675 | 0 | Py_CLEAR(dict); |
3676 | | |
3677 | | /* core config */ |
3678 | 0 | const PyConfig *config = _PyInterpreterState_GetConfig(interp); |
3679 | 0 | dict = _PyConfig_AsDict(config); |
3680 | 0 | if (dict == NULL) { |
3681 | 0 | goto error; |
3682 | 0 | } |
3683 | 0 | if (PyDict_SetItemString(result, "config", dict) < 0) { |
3684 | 0 | goto error; |
3685 | 0 | } |
3686 | 0 | Py_CLEAR(dict); |
3687 | |
|
3688 | 0 | return result; |
3689 | | |
3690 | 0 | error: |
3691 | 0 | Py_XDECREF(result); |
3692 | 0 | Py_XDECREF(dict); |
3693 | 0 | return NULL; |
3694 | 0 | } |
3695 | | |
3696 | | |
3697 | | static void |
3698 | | init_dump_ascii_wstr(const wchar_t *str) |
3699 | 0 | { |
3700 | 0 | if (str == NULL) { |
3701 | 0 | PySys_WriteStderr("(not set)"); |
3702 | 0 | return; |
3703 | 0 | } |
3704 | | |
3705 | 0 | PySys_WriteStderr("'"); |
3706 | 0 | for (; *str != L'\0'; str++) { |
3707 | 0 | unsigned int ch = (unsigned int)*str; |
3708 | 0 | if (ch == L'\'') { |
3709 | 0 | PySys_WriteStderr("\\'"); |
3710 | 0 | } else if (0x20 <= ch && ch < 0x7f) { |
3711 | 0 | PySys_WriteStderr("%c", ch); |
3712 | 0 | } |
3713 | 0 | else if (ch <= 0xff) { |
3714 | 0 | PySys_WriteStderr("\\x%02x", ch); |
3715 | 0 | } |
3716 | 0 | #if SIZEOF_WCHAR_T > 2 |
3717 | 0 | else if (ch > 0xffff) { |
3718 | 0 | PySys_WriteStderr("\\U%08x", ch); |
3719 | 0 | } |
3720 | 0 | #endif |
3721 | 0 | else { |
3722 | 0 | PySys_WriteStderr("\\u%04x", ch); |
3723 | 0 | } |
3724 | 0 | } |
3725 | 0 | PySys_WriteStderr("'"); |
3726 | 0 | } |
3727 | | |
3728 | | |
3729 | | /* Dump the Python path configuration into sys.stderr */ |
3730 | | void |
3731 | | _Py_DumpPathConfig(PyThreadState *tstate) |
3732 | 0 | { |
3733 | 0 | PyObject *exc = _PyErr_GetRaisedException(tstate); |
3734 | |
|
3735 | 0 | PySys_WriteStderr("Python path configuration:\n"); |
3736 | |
|
3737 | 0 | #define DUMP_CONFIG(NAME, FIELD) \ |
3738 | 0 | do { \ |
3739 | 0 | PySys_WriteStderr(" " NAME " = "); \ |
3740 | 0 | init_dump_ascii_wstr(config->FIELD); \ |
3741 | 0 | PySys_WriteStderr("\n"); \ |
3742 | 0 | } while (0) |
3743 | |
|
3744 | 0 | const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp); |
3745 | 0 | DUMP_CONFIG("PYTHONHOME", home); |
3746 | 0 | DUMP_CONFIG("PYTHONPATH", pythonpath_env); |
3747 | 0 | DUMP_CONFIG("program name", program_name); |
3748 | 0 | PySys_WriteStderr(" isolated = %i\n", config->isolated); |
3749 | 0 | PySys_WriteStderr(" environment = %i\n", config->use_environment); |
3750 | 0 | PySys_WriteStderr(" user site = %i\n", config->user_site_directory); |
3751 | 0 | PySys_WriteStderr(" safe_path = %i\n", config->safe_path); |
3752 | 0 | PySys_WriteStderr(" import site = %i\n", config->site_import); |
3753 | 0 | PySys_WriteStderr(" is in build tree = %i\n", config->_is_python_build); |
3754 | 0 | DUMP_CONFIG("stdlib dir", stdlib_dir); |
3755 | 0 | DUMP_CONFIG("sys.path[0]", sys_path_0); |
3756 | 0 | #undef DUMP_CONFIG |
3757 | |
|
3758 | 0 | #define DUMP_SYS(NAME) \ |
3759 | 0 | do { \ |
3760 | 0 | PySys_FormatStderr(" sys.%s = ", #NAME); \ |
3761 | 0 | if (PySys_GetOptionalAttrString(#NAME, &obj) < 0) { \ |
3762 | 0 | PyErr_Clear(); \ |
3763 | 0 | } \ |
3764 | 0 | if (obj != NULL) { \ |
3765 | 0 | PySys_FormatStderr("%A", obj); \ |
3766 | 0 | Py_DECREF(obj); \ |
3767 | 0 | } \ |
3768 | 0 | else { \ |
3769 | 0 | PySys_WriteStderr("(not set)"); \ |
3770 | 0 | } \ |
3771 | 0 | PySys_FormatStderr("\n"); \ |
3772 | 0 | } while (0) |
3773 | |
|
3774 | 0 | PyObject *obj; |
3775 | 0 | DUMP_SYS(_base_executable); |
3776 | 0 | DUMP_SYS(base_prefix); |
3777 | 0 | DUMP_SYS(base_exec_prefix); |
3778 | 0 | DUMP_SYS(platlibdir); |
3779 | 0 | DUMP_SYS(executable); |
3780 | 0 | DUMP_SYS(prefix); |
3781 | 0 | DUMP_SYS(exec_prefix); |
3782 | 0 | #undef DUMP_SYS |
3783 | |
|
3784 | 0 | PyObject *sys_path; |
3785 | 0 | (void) PySys_GetOptionalAttrString("path", &sys_path); |
3786 | 0 | if (sys_path != NULL && PyList_Check(sys_path)) { |
3787 | 0 | PySys_WriteStderr(" sys.path = [\n"); |
3788 | 0 | Py_ssize_t len = PyList_GET_SIZE(sys_path); |
3789 | 0 | for (Py_ssize_t i=0; i < len; i++) { |
3790 | 0 | PyObject *path = PyList_GET_ITEM(sys_path, i); |
3791 | 0 | PySys_FormatStderr(" %A,\n", path); |
3792 | 0 | } |
3793 | 0 | PySys_WriteStderr(" ]\n"); |
3794 | 0 | } |
3795 | 0 | Py_XDECREF(sys_path); |
3796 | |
|
3797 | 0 | _PyErr_SetRaisedException(tstate, exc); |
3798 | 0 | } |
3799 | | |
3800 | | |
3801 | | // --- PyInitConfig API --------------------------------------------------- |
3802 | | |
3803 | | struct PyInitConfig { |
3804 | | PyPreConfig preconfig; |
3805 | | PyConfig config; |
3806 | | struct _inittab *inittab; |
3807 | | Py_ssize_t inittab_size; |
3808 | | PyStatus status; |
3809 | | char *err_msg; |
3810 | | }; |
3811 | | |
3812 | | static PyInitConfig* |
3813 | | initconfig_alloc(void) |
3814 | 0 | { |
3815 | 0 | return calloc(1, sizeof(PyInitConfig)); |
3816 | 0 | } |
3817 | | |
3818 | | |
3819 | | PyInitConfig* |
3820 | | PyInitConfig_Create(void) |
3821 | 0 | { |
3822 | 0 | PyInitConfig *config = initconfig_alloc(); |
3823 | 0 | if (config == NULL) { |
3824 | 0 | return NULL; |
3825 | 0 | } |
3826 | 0 | PyPreConfig_InitIsolatedConfig(&config->preconfig); |
3827 | 0 | PyConfig_InitIsolatedConfig(&config->config); |
3828 | 0 | config->status = _PyStatus_OK(); |
3829 | 0 | return config; |
3830 | 0 | } |
3831 | | |
3832 | | |
3833 | | void |
3834 | | PyInitConfig_Free(PyInitConfig *config) |
3835 | 0 | { |
3836 | 0 | if (config == NULL) { |
3837 | 0 | return; |
3838 | 0 | } |
3839 | | |
3840 | 0 | initconfig_free_config(&config->config); |
3841 | 0 | PyMem_RawFree(config->inittab); |
3842 | 0 | free(config->err_msg); |
3843 | 0 | free(config); |
3844 | 0 | } |
3845 | | |
3846 | | |
3847 | | int |
3848 | | PyInitConfig_GetError(PyInitConfig* config, const char **perr_msg) |
3849 | 0 | { |
3850 | 0 | if (_PyStatus_IS_EXIT(config->status)) { |
3851 | 0 | char buffer[22]; // len("exit code -2147483648\0") |
3852 | 0 | PyOS_snprintf(buffer, sizeof(buffer), |
3853 | 0 | "exit code %i", |
3854 | 0 | config->status.exitcode); |
3855 | |
|
3856 | 0 | if (config->err_msg != NULL) { |
3857 | 0 | free(config->err_msg); |
3858 | 0 | } |
3859 | 0 | config->err_msg = strdup(buffer); |
3860 | 0 | if (config->err_msg != NULL) { |
3861 | 0 | *perr_msg = config->err_msg; |
3862 | 0 | return 1; |
3863 | 0 | } |
3864 | 0 | config->status = _PyStatus_NO_MEMORY(); |
3865 | 0 | } |
3866 | | |
3867 | 0 | if (_PyStatus_IS_ERROR(config->status) && config->status.err_msg != NULL) { |
3868 | 0 | *perr_msg = config->status.err_msg; |
3869 | 0 | return 1; |
3870 | 0 | } |
3871 | 0 | else { |
3872 | 0 | *perr_msg = NULL; |
3873 | 0 | return 0; |
3874 | 0 | } |
3875 | 0 | } |
3876 | | |
3877 | | |
3878 | | int |
3879 | | PyInitConfig_GetExitCode(PyInitConfig* config, int *exitcode) |
3880 | 0 | { |
3881 | 0 | if (_PyStatus_IS_EXIT(config->status)) { |
3882 | 0 | *exitcode = config->status.exitcode; |
3883 | 0 | return 1; |
3884 | 0 | } |
3885 | 0 | else { |
3886 | 0 | return 0; |
3887 | 0 | } |
3888 | 0 | } |
3889 | | |
3890 | | |
3891 | | static void |
3892 | | initconfig_set_error(PyInitConfig *config, const char *err_msg) |
3893 | 0 | { |
3894 | 0 | config->status = _PyStatus_ERR(err_msg); |
3895 | 0 | } |
3896 | | |
3897 | | |
3898 | | static const PyConfigSpec* |
3899 | | initconfig_find_spec(const PyConfigSpec *spec, const char *name) |
3900 | 0 | { |
3901 | 0 | for (; spec->name != NULL; spec++) { |
3902 | 0 | if (strcmp(name, spec->name) == 0) { |
3903 | 0 | return spec; |
3904 | 0 | } |
3905 | 0 | } |
3906 | 0 | return NULL; |
3907 | 0 | } |
3908 | | |
3909 | | |
3910 | | int |
3911 | | PyInitConfig_HasOption(PyInitConfig *config, const char *name) |
3912 | 0 | { |
3913 | 0 | const PyConfigSpec *spec = initconfig_find_spec(PYCONFIG_SPEC, name); |
3914 | 0 | if (spec == NULL) { |
3915 | 0 | spec = initconfig_find_spec(PYPRECONFIG_SPEC, name); |
3916 | 0 | } |
3917 | 0 | return (spec != NULL); |
3918 | 0 | } |
3919 | | |
3920 | | |
3921 | | static const PyConfigSpec* |
3922 | | initconfig_prepare(PyInitConfig *config, const char *name, void **raw_member) |
3923 | 0 | { |
3924 | 0 | const PyConfigSpec *spec = initconfig_find_spec(PYCONFIG_SPEC, name); |
3925 | 0 | if (spec != NULL) { |
3926 | 0 | *raw_member = config_get_spec_member(&config->config, spec); |
3927 | 0 | return spec; |
3928 | 0 | } |
3929 | | |
3930 | 0 | spec = initconfig_find_spec(PYPRECONFIG_SPEC, name); |
3931 | 0 | if (spec != NULL) { |
3932 | 0 | *raw_member = preconfig_get_spec_member(&config->preconfig, spec); |
3933 | 0 | return spec; |
3934 | 0 | } |
3935 | | |
3936 | 0 | initconfig_set_error(config, "unknown config option name"); |
3937 | 0 | return NULL; |
3938 | 0 | } |
3939 | | |
3940 | | |
3941 | | int |
3942 | | PyInitConfig_GetInt(PyInitConfig *config, const char *name, int64_t *value) |
3943 | 0 | { |
3944 | 0 | void *raw_member; |
3945 | 0 | const PyConfigSpec *spec = initconfig_prepare(config, name, &raw_member); |
3946 | 0 | if (spec == NULL) { |
3947 | 0 | return -1; |
3948 | 0 | } |
3949 | | |
3950 | 0 | switch (spec->type) { |
3951 | 0 | case PyConfig_MEMBER_INT: |
3952 | 0 | case PyConfig_MEMBER_UINT: |
3953 | 0 | case PyConfig_MEMBER_BOOL: |
3954 | 0 | { |
3955 | 0 | int *member = raw_member; |
3956 | 0 | *value = *member; |
3957 | 0 | break; |
3958 | 0 | } |
3959 | | |
3960 | 0 | case PyConfig_MEMBER_ULONG: |
3961 | 0 | { |
3962 | 0 | unsigned long *member = raw_member; |
3963 | 0 | #if SIZEOF_LONG >= 8 |
3964 | 0 | if ((unsigned long)INT64_MAX < *member) { |
3965 | 0 | initconfig_set_error(config, |
3966 | 0 | "config option value doesn't fit into int64_t"); |
3967 | 0 | return -1; |
3968 | 0 | } |
3969 | 0 | #endif |
3970 | 0 | *value = *member; |
3971 | 0 | break; |
3972 | 0 | } |
3973 | | |
3974 | 0 | default: |
3975 | 0 | initconfig_set_error(config, "config option type is not int"); |
3976 | 0 | return -1; |
3977 | 0 | } |
3978 | 0 | return 0; |
3979 | 0 | } |
3980 | | |
3981 | | |
3982 | | static char* |
3983 | | wstr_to_utf8(PyInitConfig *config, wchar_t *wstr) |
3984 | 0 | { |
3985 | 0 | char *utf8; |
3986 | 0 | int res = _Py_EncodeUTF8Ex(wstr, &utf8, NULL, NULL, 1, _Py_ERROR_STRICT); |
3987 | 0 | if (res == -2) { |
3988 | 0 | initconfig_set_error(config, "encoding error"); |
3989 | 0 | return NULL; |
3990 | 0 | } |
3991 | 0 | if (res < 0) { |
3992 | 0 | config->status = _PyStatus_NO_MEMORY(); |
3993 | 0 | return NULL; |
3994 | 0 | } |
3995 | | |
3996 | | // Copy to use the malloc() memory allocator |
3997 | 0 | size_t size = strlen(utf8) + 1; |
3998 | 0 | char *str = malloc(size); |
3999 | 0 | if (str == NULL) { |
4000 | 0 | PyMem_RawFree(utf8); |
4001 | 0 | config->status = _PyStatus_NO_MEMORY(); |
4002 | 0 | return NULL; |
4003 | 0 | } |
4004 | | |
4005 | 0 | memcpy(str, utf8, size); |
4006 | 0 | PyMem_RawFree(utf8); |
4007 | 0 | return str; |
4008 | 0 | } |
4009 | | |
4010 | | |
4011 | | int |
4012 | | PyInitConfig_GetStr(PyInitConfig *config, const char *name, char **value) |
4013 | 0 | { |
4014 | 0 | void *raw_member; |
4015 | 0 | const PyConfigSpec *spec = initconfig_prepare(config, name, &raw_member); |
4016 | 0 | if (spec == NULL) { |
4017 | 0 | return -1; |
4018 | 0 | } |
4019 | | |
4020 | 0 | if (spec->type != PyConfig_MEMBER_WSTR |
4021 | 0 | && spec->type != PyConfig_MEMBER_WSTR_OPT) |
4022 | 0 | { |
4023 | 0 | initconfig_set_error(config, "config option type is not string"); |
4024 | 0 | return -1; |
4025 | 0 | } |
4026 | | |
4027 | 0 | wchar_t **member = raw_member; |
4028 | 0 | if (*member == NULL) { |
4029 | 0 | *value = NULL; |
4030 | 0 | return 0; |
4031 | 0 | } |
4032 | | |
4033 | 0 | *value = wstr_to_utf8(config, *member); |
4034 | 0 | if (*value == NULL) { |
4035 | 0 | return -1; |
4036 | 0 | } |
4037 | 0 | return 0; |
4038 | 0 | } |
4039 | | |
4040 | | |
4041 | | int |
4042 | | PyInitConfig_GetStrList(PyInitConfig *config, const char *name, size_t *length, char ***items) |
4043 | 0 | { |
4044 | 0 | void *raw_member; |
4045 | 0 | const PyConfigSpec *spec = initconfig_prepare(config, name, &raw_member); |
4046 | 0 | if (spec == NULL) { |
4047 | 0 | return -1; |
4048 | 0 | } |
4049 | | |
4050 | 0 | if (spec->type != PyConfig_MEMBER_WSTR_LIST) { |
4051 | 0 | initconfig_set_error(config, "config option type is not string list"); |
4052 | 0 | return -1; |
4053 | 0 | } |
4054 | | |
4055 | 0 | PyWideStringList *list = raw_member; |
4056 | 0 | *length = list->length; |
4057 | |
|
4058 | 0 | *items = malloc(list->length * sizeof(char*)); |
4059 | 0 | if (*items == NULL) { |
4060 | 0 | config->status = _PyStatus_NO_MEMORY(); |
4061 | 0 | return -1; |
4062 | 0 | } |
4063 | | |
4064 | 0 | for (Py_ssize_t i=0; i < list->length; i++) { |
4065 | 0 | (*items)[i] = wstr_to_utf8(config, list->items[i]); |
4066 | 0 | if ((*items)[i] == NULL) { |
4067 | 0 | PyInitConfig_FreeStrList(i, *items); |
4068 | 0 | return -1; |
4069 | 0 | } |
4070 | 0 | } |
4071 | 0 | return 0; |
4072 | 0 | } |
4073 | | |
4074 | | |
4075 | | void |
4076 | | PyInitConfig_FreeStrList(size_t length, char **items) |
4077 | 0 | { |
4078 | 0 | for (size_t i=0; i < length; i++) { |
4079 | 0 | free(items[i]); |
4080 | 0 | } |
4081 | 0 | free(items); |
4082 | 0 | } |
4083 | | |
4084 | | |
4085 | | int |
4086 | | PyInitConfig_SetInt(PyInitConfig *config, const char *name, int64_t value) |
4087 | 0 | { |
4088 | 0 | void *raw_member; |
4089 | 0 | const PyConfigSpec *spec = initconfig_prepare(config, name, &raw_member); |
4090 | 0 | if (spec == NULL) { |
4091 | 0 | return -1; |
4092 | 0 | } |
4093 | | |
4094 | 0 | switch (spec->type) { |
4095 | 0 | case PyConfig_MEMBER_INT: |
4096 | 0 | { |
4097 | 0 | if (value < (int64_t)INT_MIN || (int64_t)INT_MAX < value) { |
4098 | 0 | initconfig_set_error(config, |
4099 | 0 | "config option value is out of int range"); |
4100 | 0 | return -1; |
4101 | 0 | } |
4102 | 0 | int int_value = (int)value; |
4103 | |
|
4104 | 0 | int *member = raw_member; |
4105 | 0 | *member = int_value; |
4106 | 0 | break; |
4107 | 0 | } |
4108 | | |
4109 | 0 | case PyConfig_MEMBER_UINT: |
4110 | 0 | case PyConfig_MEMBER_BOOL: |
4111 | 0 | { |
4112 | 0 | if (value < 0 || (uint64_t)UINT_MAX < (uint64_t)value) { |
4113 | 0 | initconfig_set_error(config, |
4114 | 0 | "config option value is out of unsigned int range"); |
4115 | 0 | return -1; |
4116 | 0 | } |
4117 | 0 | int int_value = (int)value; |
4118 | |
|
4119 | 0 | int *member = raw_member; |
4120 | 0 | *member = int_value; |
4121 | 0 | break; |
4122 | 0 | } |
4123 | | |
4124 | 0 | case PyConfig_MEMBER_ULONG: |
4125 | 0 | { |
4126 | 0 | if (value < 0 || (uint64_t)ULONG_MAX < (uint64_t)value) { |
4127 | 0 | initconfig_set_error(config, |
4128 | 0 | "config option value is out of unsigned long range"); |
4129 | 0 | return -1; |
4130 | 0 | } |
4131 | 0 | unsigned long ulong_value = (unsigned long)value; |
4132 | |
|
4133 | 0 | unsigned long *member = raw_member; |
4134 | 0 | *member = ulong_value; |
4135 | 0 | break; |
4136 | 0 | } |
4137 | | |
4138 | 0 | default: |
4139 | 0 | initconfig_set_error(config, "config option type is not int"); |
4140 | 0 | return -1; |
4141 | 0 | } |
4142 | | |
4143 | 0 | if (strcmp(name, "hash_seed") == 0) { |
4144 | 0 | config->config.use_hash_seed = 1; |
4145 | 0 | } |
4146 | |
|
4147 | 0 | return 0; |
4148 | 0 | } |
4149 | | |
4150 | | |
4151 | | static wchar_t* |
4152 | | utf8_to_wstr(PyInitConfig *config, const char *str) |
4153 | 0 | { |
4154 | 0 | wchar_t *wstr; |
4155 | 0 | size_t wlen; |
4156 | 0 | int res = _Py_DecodeUTF8Ex(str, strlen(str), &wstr, &wlen, NULL, _Py_ERROR_STRICT); |
4157 | 0 | if (res == -2) { |
4158 | 0 | initconfig_set_error(config, "decoding error"); |
4159 | 0 | return NULL; |
4160 | 0 | } |
4161 | 0 | if (res < 0) { |
4162 | 0 | config->status = _PyStatus_NO_MEMORY(); |
4163 | 0 | return NULL; |
4164 | 0 | } |
4165 | | |
4166 | | // Copy to use the malloc() memory allocator |
4167 | 0 | size_t size = (wlen + 1) * sizeof(wchar_t); |
4168 | 0 | wchar_t *wstr2 = malloc(size); |
4169 | 0 | if (wstr2 == NULL) { |
4170 | 0 | PyMem_RawFree(wstr); |
4171 | 0 | config->status = _PyStatus_NO_MEMORY(); |
4172 | 0 | return NULL; |
4173 | 0 | } |
4174 | | |
4175 | 0 | memcpy(wstr2, wstr, size); |
4176 | 0 | PyMem_RawFree(wstr); |
4177 | 0 | return wstr2; |
4178 | 0 | } |
4179 | | |
4180 | | |
4181 | | int |
4182 | | PyInitConfig_SetStr(PyInitConfig *config, const char *name, const char* value) |
4183 | 0 | { |
4184 | 0 | void *raw_member; |
4185 | 0 | const PyConfigSpec *spec = initconfig_prepare(config, name, &raw_member); |
4186 | 0 | if (spec == NULL) { |
4187 | 0 | return -1; |
4188 | 0 | } |
4189 | | |
4190 | 0 | if (spec->type != PyConfig_MEMBER_WSTR |
4191 | 0 | && spec->type != PyConfig_MEMBER_WSTR_OPT) { |
4192 | 0 | initconfig_set_error(config, "config option type is not string"); |
4193 | 0 | return -1; |
4194 | 0 | } |
4195 | | |
4196 | 0 | if (value == NULL && spec->type != PyConfig_MEMBER_WSTR_OPT) { |
4197 | 0 | initconfig_set_error(config, "config option string cannot be NULL"); |
4198 | 0 | } |
4199 | |
|
4200 | 0 | wchar_t **member = raw_member; |
4201 | |
|
4202 | 0 | *member = utf8_to_wstr(config, value); |
4203 | 0 | if (*member == NULL) { |
4204 | 0 | return -1; |
4205 | 0 | } |
4206 | 0 | return 0; |
4207 | 0 | } |
4208 | | |
4209 | | |
4210 | | static void |
4211 | | initconfig_free_wstr(wchar_t *member) |
4212 | 0 | { |
4213 | 0 | if (member) { |
4214 | 0 | free(member); |
4215 | 0 | } |
4216 | 0 | } |
4217 | | |
4218 | | |
4219 | | static void |
4220 | | initconfig_free_wstr_list(PyWideStringList *list) |
4221 | 0 | { |
4222 | 0 | for (Py_ssize_t i = 0; i < list->length; i++) { |
4223 | 0 | free(list->items[i]); |
4224 | 0 | } |
4225 | 0 | free(list->items); |
4226 | 0 | } |
4227 | | |
4228 | | |
4229 | | static void |
4230 | | initconfig_free_config(const PyConfig *config) |
4231 | 0 | { |
4232 | 0 | const PyConfigSpec *spec = PYCONFIG_SPEC; |
4233 | 0 | for (; spec->name != NULL; spec++) { |
4234 | 0 | void *member = config_get_spec_member(config, spec); |
4235 | 0 | if (spec->type == PyConfig_MEMBER_WSTR |
4236 | 0 | || spec->type == PyConfig_MEMBER_WSTR_OPT) |
4237 | 0 | { |
4238 | 0 | wchar_t *wstr = *(wchar_t **)member; |
4239 | 0 | initconfig_free_wstr(wstr); |
4240 | 0 | } |
4241 | 0 | else if (spec->type == PyConfig_MEMBER_WSTR_LIST) { |
4242 | 0 | initconfig_free_wstr_list(member); |
4243 | 0 | } |
4244 | 0 | } |
4245 | 0 | } |
4246 | | |
4247 | | |
4248 | | static int |
4249 | | initconfig_set_str_list(PyInitConfig *config, PyWideStringList *list, |
4250 | | Py_ssize_t length, char * const *items) |
4251 | 0 | { |
4252 | 0 | PyWideStringList wlist = _PyWideStringList_INIT; |
4253 | 0 | size_t size = sizeof(wchar_t*) * length; |
4254 | 0 | wlist.items = (wchar_t **)malloc(size); |
4255 | 0 | if (wlist.items == NULL) { |
4256 | 0 | config->status = _PyStatus_NO_MEMORY(); |
4257 | 0 | return -1; |
4258 | 0 | } |
4259 | | |
4260 | 0 | for (Py_ssize_t i = 0; i < length; i++) { |
4261 | 0 | wchar_t *arg = utf8_to_wstr(config, items[i]); |
4262 | 0 | if (arg == NULL) { |
4263 | 0 | initconfig_free_wstr_list(&wlist); |
4264 | 0 | return -1; |
4265 | 0 | } |
4266 | 0 | wlist.items[i] = arg; |
4267 | 0 | wlist.length++; |
4268 | 0 | } |
4269 | | |
4270 | 0 | initconfig_free_wstr_list(list); |
4271 | 0 | *list = wlist; |
4272 | 0 | return 0; |
4273 | 0 | } |
4274 | | |
4275 | | |
4276 | | int |
4277 | | PyInitConfig_SetStrList(PyInitConfig *config, const char *name, |
4278 | | size_t length, char * const *items) |
4279 | 0 | { |
4280 | 0 | void *raw_member; |
4281 | 0 | const PyConfigSpec *spec = initconfig_prepare(config, name, &raw_member); |
4282 | 0 | if (spec == NULL) { |
4283 | 0 | return -1; |
4284 | 0 | } |
4285 | | |
4286 | 0 | if (spec->type != PyConfig_MEMBER_WSTR_LIST) { |
4287 | 0 | initconfig_set_error(config, "config option type is not strings list"); |
4288 | 0 | return -1; |
4289 | 0 | } |
4290 | 0 | PyWideStringList *list = raw_member; |
4291 | 0 | if (initconfig_set_str_list(config, list, length, items) < 0) { |
4292 | 0 | return -1; |
4293 | 0 | } |
4294 | | |
4295 | 0 | if (strcmp(name, "module_search_paths") == 0) { |
4296 | 0 | config->config.module_search_paths_set = 1; |
4297 | 0 | } |
4298 | 0 | return 0; |
4299 | 0 | } |
4300 | | |
4301 | | |
4302 | | int |
4303 | | PyInitConfig_AddModule(PyInitConfig *config, const char *name, |
4304 | | PyObject* (*initfunc)(void)) |
4305 | 0 | { |
4306 | 0 | size_t size = sizeof(struct _inittab) * (config->inittab_size + 2); |
4307 | 0 | struct _inittab *new_inittab = PyMem_RawRealloc(config->inittab, size); |
4308 | 0 | if (new_inittab == NULL) { |
4309 | 0 | config->status = _PyStatus_NO_MEMORY(); |
4310 | 0 | return -1; |
4311 | 0 | } |
4312 | 0 | config->inittab = new_inittab; |
4313 | |
|
4314 | 0 | struct _inittab *entry = &config->inittab[config->inittab_size]; |
4315 | 0 | entry->name = name; |
4316 | 0 | entry->initfunc = initfunc; |
4317 | | |
4318 | | // Terminator entry |
4319 | 0 | entry = &config->inittab[config->inittab_size + 1]; |
4320 | 0 | entry->name = NULL; |
4321 | 0 | entry->initfunc = NULL; |
4322 | |
|
4323 | 0 | config->inittab_size++; |
4324 | 0 | return 0; |
4325 | 0 | } |
4326 | | |
4327 | | |
4328 | | int |
4329 | | Py_InitializeFromInitConfig(PyInitConfig *config) |
4330 | 0 | { |
4331 | 0 | if (config->inittab_size >= 1) { |
4332 | 0 | if (PyImport_ExtendInittab(config->inittab) < 0) { |
4333 | 0 | config->status = _PyStatus_NO_MEMORY(); |
4334 | 0 | return -1; |
4335 | 0 | } |
4336 | 0 | } |
4337 | | |
4338 | 0 | _PyPreConfig_GetConfig(&config->preconfig, &config->config); |
4339 | |
|
4340 | 0 | config->status = Py_PreInitializeFromArgs( |
4341 | 0 | &config->preconfig, |
4342 | 0 | config->config.argv.length, |
4343 | 0 | config->config.argv.items); |
4344 | 0 | if (_PyStatus_EXCEPTION(config->status)) { |
4345 | 0 | return -1; |
4346 | 0 | } |
4347 | | |
4348 | 0 | config->status = Py_InitializeFromConfig(&config->config); |
4349 | 0 | if (_PyStatus_EXCEPTION(config->status)) { |
4350 | 0 | return -1; |
4351 | 0 | } |
4352 | | |
4353 | 0 | return 0; |
4354 | 0 | } |
4355 | | |
4356 | | |
4357 | | // --- PyConfig_Get() ------------------------------------------------------- |
4358 | | |
4359 | | static const PyConfigSpec* |
4360 | | config_generic_find_spec(const PyConfigSpec *spec, const char *name) |
4361 | 0 | { |
4362 | 0 | for (; spec->name != NULL; spec++) { |
4363 | 0 | if (spec->visibility == PyConfig_MEMBER_INIT_ONLY) { |
4364 | 0 | continue; |
4365 | 0 | } |
4366 | 0 | if (strcmp(name, spec->name) == 0) { |
4367 | 0 | return spec; |
4368 | 0 | } |
4369 | 0 | } |
4370 | 0 | return NULL; |
4371 | 0 | } |
4372 | | |
4373 | | |
4374 | | static const PyConfigSpec* |
4375 | | config_find_spec(const char *name) |
4376 | 0 | { |
4377 | 0 | return config_generic_find_spec(PYCONFIG_SPEC, name); |
4378 | 0 | } |
4379 | | |
4380 | | |
4381 | | static const PyConfigSpec* |
4382 | | preconfig_find_spec(const char *name) |
4383 | 0 | { |
4384 | 0 | return config_generic_find_spec(PYPRECONFIG_SPEC, name); |
4385 | 0 | } |
4386 | | |
4387 | | |
4388 | | static int |
4389 | | config_add_xoption(PyObject *dict, const wchar_t *str) |
4390 | 0 | { |
4391 | 0 | PyObject *name = NULL, *value = NULL; |
4392 | |
|
4393 | 0 | const wchar_t *name_end = wcschr(str, L'='); |
4394 | 0 | if (!name_end) { |
4395 | 0 | name = PyUnicode_FromWideChar(str, -1); |
4396 | 0 | if (name == NULL) { |
4397 | 0 | goto error; |
4398 | 0 | } |
4399 | 0 | value = Py_NewRef(Py_True); |
4400 | 0 | } |
4401 | 0 | else { |
4402 | 0 | name = PyUnicode_FromWideChar(str, name_end - str); |
4403 | 0 | if (name == NULL) { |
4404 | 0 | goto error; |
4405 | 0 | } |
4406 | 0 | value = PyUnicode_FromWideChar(name_end + 1, -1); |
4407 | 0 | if (value == NULL) { |
4408 | 0 | goto error; |
4409 | 0 | } |
4410 | 0 | } |
4411 | 0 | if (PyDict_SetItem(dict, name, value) < 0) { |
4412 | 0 | goto error; |
4413 | 0 | } |
4414 | 0 | Py_DECREF(name); |
4415 | 0 | Py_DECREF(value); |
4416 | 0 | return 0; |
4417 | | |
4418 | 0 | error: |
4419 | 0 | Py_XDECREF(name); |
4420 | 0 | Py_XDECREF(value); |
4421 | 0 | return -1; |
4422 | 0 | } |
4423 | | |
4424 | | |
4425 | | PyObject* |
4426 | | _PyConfig_CreateXOptionsDict(const PyConfig *config) |
4427 | 44 | { |
4428 | 44 | PyObject *dict = PyDict_New(); |
4429 | 44 | if (dict == NULL) { |
4430 | 0 | return NULL; |
4431 | 0 | } |
4432 | | |
4433 | 44 | Py_ssize_t nxoption = config->xoptions.length; |
4434 | 44 | wchar_t **xoptions = config->xoptions.items; |
4435 | 44 | for (Py_ssize_t i=0; i < nxoption; i++) { |
4436 | 0 | const wchar_t *option = xoptions[i]; |
4437 | 0 | if (config_add_xoption(dict, option) < 0) { |
4438 | 0 | Py_DECREF(dict); |
4439 | 0 | return NULL; |
4440 | 0 | } |
4441 | 0 | } |
4442 | 44 | return dict; |
4443 | 44 | } |
4444 | | |
4445 | | |
4446 | | static int |
4447 | | config_get_sys_write_bytecode(const PyConfig *config, int *value) |
4448 | 0 | { |
4449 | 0 | PyObject *attr = PySys_GetAttrString("dont_write_bytecode"); |
4450 | 0 | if (attr == NULL) { |
4451 | 0 | return -1; |
4452 | 0 | } |
4453 | | |
4454 | 0 | int is_true = PyObject_IsTrue(attr); |
4455 | 0 | Py_DECREF(attr); |
4456 | 0 | if (is_true < 0) { |
4457 | 0 | return -1; |
4458 | 0 | } |
4459 | 0 | *value = (!is_true); |
4460 | 0 | return 0; |
4461 | 0 | } |
4462 | | |
4463 | | |
4464 | | static PyObject* |
4465 | | config_get(const PyConfig *config, const PyConfigSpec *spec, |
4466 | | int use_sys) |
4467 | 1.56k | { |
4468 | 1.56k | if (use_sys) { |
4469 | 0 | if (spec->sys.attr != NULL) { |
4470 | 0 | return PySys_GetAttrString(spec->sys.attr); |
4471 | 0 | } |
4472 | | |
4473 | 0 | if (strcmp(spec->name, "write_bytecode") == 0) { |
4474 | 0 | int value; |
4475 | 0 | if (config_get_sys_write_bytecode(config, &value) < 0) { |
4476 | 0 | return NULL; |
4477 | 0 | } |
4478 | 0 | return PyBool_FromLong(value); |
4479 | 0 | } |
4480 | | |
4481 | 0 | if (strcmp(spec->name, "int_max_str_digits") == 0) { |
4482 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
4483 | 0 | return PyLong_FromLong(interp->long_state.max_str_digits); |
4484 | 0 | } |
4485 | 0 | } |
4486 | | |
4487 | 1.56k | void *member = config_get_spec_member(config, spec); |
4488 | 1.56k | switch (spec->type) { |
4489 | 88 | case PyConfig_MEMBER_INT: |
4490 | 264 | case PyConfig_MEMBER_UINT: |
4491 | 264 | { |
4492 | 264 | int value = *(int *)member; |
4493 | 264 | return PyLong_FromLong(value); |
4494 | 88 | } |
4495 | | |
4496 | 682 | case PyConfig_MEMBER_BOOL: |
4497 | 682 | { |
4498 | 682 | int value = *(int *)member; |
4499 | 682 | return PyBool_FromLong(value != 0); |
4500 | 88 | } |
4501 | | |
4502 | 22 | case PyConfig_MEMBER_ULONG: |
4503 | 22 | { |
4504 | 22 | unsigned long value = *(unsigned long *)member; |
4505 | 22 | return PyLong_FromUnsignedLong(value); |
4506 | 88 | } |
4507 | | |
4508 | 154 | case PyConfig_MEMBER_WSTR: |
4509 | 484 | case PyConfig_MEMBER_WSTR_OPT: |
4510 | 484 | { |
4511 | 484 | wchar_t *wstr = *(wchar_t **)member; |
4512 | 484 | if (wstr != NULL) { |
4513 | 132 | return PyUnicode_FromWideChar(wstr, -1); |
4514 | 132 | } |
4515 | 352 | else { |
4516 | 352 | return Py_NewRef(Py_None); |
4517 | 352 | } |
4518 | 484 | } |
4519 | | |
4520 | 110 | case PyConfig_MEMBER_WSTR_LIST: |
4521 | 110 | { |
4522 | 110 | if (strcmp(spec->name, "xoptions") == 0) { |
4523 | 22 | return _PyConfig_CreateXOptionsDict(config); |
4524 | 22 | } |
4525 | 88 | else { |
4526 | 88 | const PyWideStringList *list = (const PyWideStringList *)member; |
4527 | 88 | return _PyWideStringList_AsTuple(list); |
4528 | 88 | } |
4529 | 110 | } |
4530 | | |
4531 | 0 | default: |
4532 | 0 | Py_UNREACHABLE(); |
4533 | 1.56k | } |
4534 | 1.56k | } |
4535 | | |
4536 | | |
4537 | | static PyObject* |
4538 | | preconfig_get(const PyPreConfig *preconfig, const PyConfigSpec *spec) |
4539 | 0 | { |
4540 | | // The type of all PYPRECONFIG_SPEC members is INT or BOOL. |
4541 | 0 | assert(spec->type == PyConfig_MEMBER_INT |
4542 | 0 | || spec->type == PyConfig_MEMBER_BOOL); |
4543 | | |
4544 | 0 | char *member = (char *)preconfig + spec->offset; |
4545 | 0 | int value = *(int *)member; |
4546 | |
|
4547 | 0 | if (spec->type == PyConfig_MEMBER_BOOL) { |
4548 | 0 | return PyBool_FromLong(value != 0); |
4549 | 0 | } |
4550 | 0 | else { |
4551 | 0 | return PyLong_FromLong(value); |
4552 | 0 | } |
4553 | 0 | } |
4554 | | |
4555 | | |
4556 | | static void |
4557 | | config_unknown_name_error(const char *name) |
4558 | 0 | { |
4559 | 0 | PyErr_Format(PyExc_ValueError, "unknown config option name: %s", name); |
4560 | 0 | } |
4561 | | |
4562 | | |
4563 | | PyObject* |
4564 | | PyConfig_Get(const char *name) |
4565 | 0 | { |
4566 | 0 | const PyConfigSpec *spec = config_find_spec(name); |
4567 | 0 | if (spec != NULL) { |
4568 | 0 | const PyConfig *config = _Py_GetConfig(); |
4569 | 0 | return config_get(config, spec, 1); |
4570 | 0 | } |
4571 | | |
4572 | 0 | spec = preconfig_find_spec(name); |
4573 | 0 | if (spec != NULL) { |
4574 | 0 | const PyPreConfig *preconfig = &_PyRuntime.preconfig; |
4575 | 0 | return preconfig_get(preconfig, spec); |
4576 | 0 | } |
4577 | | |
4578 | 0 | config_unknown_name_error(name); |
4579 | 0 | return NULL; |
4580 | 0 | } |
4581 | | |
4582 | | |
4583 | | int |
4584 | | PyConfig_GetInt(const char *name, int *value) |
4585 | 0 | { |
4586 | 0 | assert(!PyErr_Occurred()); |
4587 | | |
4588 | 0 | PyObject *obj = PyConfig_Get(name); |
4589 | 0 | if (obj == NULL) { |
4590 | 0 | return -1; |
4591 | 0 | } |
4592 | | |
4593 | 0 | if (!PyLong_Check(obj)) { |
4594 | 0 | Py_DECREF(obj); |
4595 | 0 | PyErr_Format(PyExc_TypeError, "config option %s is not an int", name); |
4596 | 0 | return -1; |
4597 | 0 | } |
4598 | | |
4599 | 0 | int as_int = PyLong_AsInt(obj); |
4600 | 0 | Py_DECREF(obj); |
4601 | 0 | if (as_int == -1 && PyErr_Occurred()) { |
4602 | 0 | PyErr_Format(PyExc_OverflowError, |
4603 | 0 | "config option %s value does not fit into a C int", name); |
4604 | 0 | return -1; |
4605 | 0 | } |
4606 | | |
4607 | 0 | *value = as_int; |
4608 | 0 | return 0; |
4609 | 0 | } |
4610 | | |
4611 | | |
4612 | | static int |
4613 | | config_names_add(PyObject *names, const PyConfigSpec *spec) |
4614 | 0 | { |
4615 | 0 | for (; spec->name != NULL; spec++) { |
4616 | 0 | if (spec->visibility == PyConfig_MEMBER_INIT_ONLY) { |
4617 | 0 | continue; |
4618 | 0 | } |
4619 | 0 | PyObject *name = PyUnicode_FromString(spec->name); |
4620 | 0 | if (name == NULL) { |
4621 | 0 | return -1; |
4622 | 0 | } |
4623 | 0 | int res = PyList_Append(names, name); |
4624 | 0 | Py_DECREF(name); |
4625 | 0 | if (res < 0) { |
4626 | 0 | return -1; |
4627 | 0 | } |
4628 | 0 | } |
4629 | 0 | return 0; |
4630 | 0 | } |
4631 | | |
4632 | | |
4633 | | PyObject* |
4634 | | PyConfig_Names(void) |
4635 | 0 | { |
4636 | 0 | PyObject *names = PyList_New(0); |
4637 | 0 | if (names == NULL) { |
4638 | 0 | goto error; |
4639 | 0 | } |
4640 | | |
4641 | 0 | if (config_names_add(names, PYCONFIG_SPEC) < 0) { |
4642 | 0 | goto error; |
4643 | 0 | } |
4644 | 0 | if (config_names_add(names, PYPRECONFIG_SPEC) < 0) { |
4645 | 0 | goto error; |
4646 | 0 | } |
4647 | | |
4648 | 0 | PyObject *frozen = PyFrozenSet_New(names); |
4649 | 0 | Py_DECREF(names); |
4650 | 0 | return frozen; |
4651 | | |
4652 | 0 | error: |
4653 | 0 | Py_XDECREF(names); |
4654 | 0 | return NULL; |
4655 | 0 | } |
4656 | | |
4657 | | |
4658 | | // --- PyConfig_Set() ------------------------------------------------------- |
4659 | | |
4660 | | static int |
4661 | | config_set_sys_flag(const PyConfigSpec *spec, int int_value) |
4662 | 0 | { |
4663 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
4664 | 0 | PyConfig *config = &interp->config; |
4665 | |
|
4666 | 0 | if (spec->type == PyConfig_MEMBER_BOOL) { |
4667 | 0 | if (int_value != 0) { |
4668 | | // convert values < 0 and values > 1 to 1 |
4669 | 0 | int_value = 1; |
4670 | 0 | } |
4671 | 0 | } |
4672 | |
|
4673 | 0 | PyObject *value; |
4674 | 0 | if (spec->sys.flag_setter) { |
4675 | 0 | value = spec->sys.flag_setter(int_value); |
4676 | 0 | } |
4677 | 0 | else { |
4678 | 0 | value = config_sys_flag_long(int_value); |
4679 | 0 | } |
4680 | 0 | if (value == NULL) { |
4681 | 0 | return -1; |
4682 | 0 | } |
4683 | | |
4684 | | // Set sys.flags.FLAG |
4685 | 0 | Py_ssize_t pos = spec->sys.flag_index; |
4686 | 0 | if (_PySys_SetFlagObj(pos, value) < 0) { |
4687 | 0 | goto error; |
4688 | 0 | } |
4689 | | |
4690 | | // Set PyConfig.ATTR |
4691 | 0 | assert(spec->type == PyConfig_MEMBER_INT |
4692 | 0 | || spec->type == PyConfig_MEMBER_UINT |
4693 | 0 | || spec->type == PyConfig_MEMBER_BOOL); |
4694 | 0 | int *member = config_get_spec_member(config, spec); |
4695 | 0 | *member = int_value; |
4696 | | |
4697 | | // Set sys.dont_write_bytecode attribute |
4698 | 0 | if (strcmp(spec->name, "write_bytecode") == 0) { |
4699 | 0 | if (PySys_SetObject("dont_write_bytecode", value) < 0) { |
4700 | 0 | goto error; |
4701 | 0 | } |
4702 | 0 | } |
4703 | | |
4704 | 0 | Py_DECREF(value); |
4705 | 0 | return 0; |
4706 | | |
4707 | 0 | error: |
4708 | 0 | Py_DECREF(value); |
4709 | 0 | return -1; |
4710 | 0 | } |
4711 | | |
4712 | | |
4713 | | // Set PyConfig.ATTR integer member |
4714 | | static int |
4715 | | config_set_int_attr(const PyConfigSpec *spec, int value) |
4716 | 0 | { |
4717 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
4718 | 0 | PyConfig *config = &interp->config; |
4719 | 0 | int *member = config_get_spec_member(config, spec); |
4720 | 0 | *member = value; |
4721 | 0 | return 0; |
4722 | 0 | } |
4723 | | |
4724 | | |
4725 | | int |
4726 | | PyConfig_Set(const char *name, PyObject *value) |
4727 | 0 | { |
4728 | 0 | if (PySys_Audit("cpython.PyConfig_Set", "sO", name, value) < 0) { |
4729 | 0 | return -1; |
4730 | 0 | } |
4731 | | |
4732 | 0 | const PyConfigSpec *spec = config_find_spec(name); |
4733 | 0 | if (spec == NULL) { |
4734 | 0 | spec = preconfig_find_spec(name); |
4735 | 0 | if (spec == NULL) { |
4736 | 0 | config_unknown_name_error(name); |
4737 | 0 | return -1; |
4738 | 0 | } |
4739 | 0 | assert(spec->visibility != PyConfig_MEMBER_PUBLIC); |
4740 | 0 | } |
4741 | | |
4742 | 0 | if (spec->visibility != PyConfig_MEMBER_PUBLIC) { |
4743 | 0 | PyErr_Format(PyExc_ValueError, "cannot set read-only option %s", |
4744 | 0 | name); |
4745 | 0 | return -1; |
4746 | 0 | } |
4747 | | |
4748 | 0 | int int_value = 0; |
4749 | 0 | int has_int_value = 0; |
4750 | |
|
4751 | 0 | switch (spec->type) { |
4752 | 0 | case PyConfig_MEMBER_INT: |
4753 | 0 | case PyConfig_MEMBER_UINT: |
4754 | 0 | case PyConfig_MEMBER_BOOL: |
4755 | 0 | if (!PyLong_Check(value)) { |
4756 | 0 | PyErr_Format(PyExc_TypeError, "expected int or bool, got %T", value); |
4757 | 0 | return -1; |
4758 | 0 | } |
4759 | 0 | int_value = PyLong_AsInt(value); |
4760 | 0 | if (int_value == -1 && PyErr_Occurred()) { |
4761 | 0 | return -1; |
4762 | 0 | } |
4763 | 0 | if (int_value < 0 && spec->type != PyConfig_MEMBER_INT) { |
4764 | 0 | PyErr_Format(PyExc_ValueError, "value must be >= 0"); |
4765 | 0 | return -1; |
4766 | 0 | } |
4767 | 0 | has_int_value = 1; |
4768 | 0 | break; |
4769 | | |
4770 | 0 | case PyConfig_MEMBER_ULONG: |
4771 | | // not implemented: only hash_seed uses this type, and it's read-only |
4772 | 0 | goto cannot_set; |
4773 | | |
4774 | 0 | case PyConfig_MEMBER_WSTR: |
4775 | 0 | if (!PyUnicode_CheckExact(value)) { |
4776 | 0 | PyErr_Format(PyExc_TypeError, "expected str, got %T", value); |
4777 | 0 | return -1; |
4778 | 0 | } |
4779 | 0 | break; |
4780 | | |
4781 | 0 | case PyConfig_MEMBER_WSTR_OPT: |
4782 | 0 | if (value != Py_None && !PyUnicode_CheckExact(value)) { |
4783 | 0 | PyErr_Format(PyExc_TypeError, "expected str or None, got %T", value); |
4784 | 0 | return -1; |
4785 | 0 | } |
4786 | 0 | break; |
4787 | | |
4788 | 0 | case PyConfig_MEMBER_WSTR_LIST: |
4789 | 0 | if (strcmp(spec->name, "xoptions") != 0) { |
4790 | 0 | if (!PyList_Check(value)) { |
4791 | 0 | PyErr_Format(PyExc_TypeError, "expected list[str], got %T", |
4792 | 0 | value); |
4793 | 0 | return -1; |
4794 | 0 | } |
4795 | 0 | for (Py_ssize_t i=0; i < PyList_GET_SIZE(value); i++) { |
4796 | 0 | PyObject *item = PyList_GET_ITEM(value, i); |
4797 | 0 | if (!PyUnicode_Check(item)) { |
4798 | 0 | PyErr_Format(PyExc_TypeError, |
4799 | 0 | "expected str, list item %zd has type %T", |
4800 | 0 | i, item); |
4801 | 0 | return -1; |
4802 | 0 | } |
4803 | 0 | } |
4804 | 0 | } |
4805 | 0 | else { |
4806 | | // xoptions type is dict[str, str] |
4807 | 0 | if (!PyDict_Check(value)) { |
4808 | 0 | PyErr_Format(PyExc_TypeError, |
4809 | 0 | "expected dict[str, str | bool], got %T", |
4810 | 0 | value); |
4811 | 0 | return -1; |
4812 | 0 | } |
4813 | | |
4814 | 0 | Py_ssize_t pos = 0; |
4815 | 0 | PyObject *key, *item; |
4816 | 0 | while (PyDict_Next(value, &pos, &key, &item)) { |
4817 | 0 | if (!PyUnicode_Check(key)) { |
4818 | 0 | PyErr_Format(PyExc_TypeError, |
4819 | 0 | "expected str, " |
4820 | 0 | "got dict key type %T", key); |
4821 | 0 | return -1; |
4822 | 0 | } |
4823 | 0 | if (!PyUnicode_Check(item) && !PyBool_Check(item)) { |
4824 | 0 | PyErr_Format(PyExc_TypeError, |
4825 | 0 | "expected str or bool, " |
4826 | 0 | "got dict value type %T", key); |
4827 | 0 | return -1; |
4828 | 0 | } |
4829 | 0 | } |
4830 | 0 | } |
4831 | 0 | break; |
4832 | | |
4833 | 0 | default: |
4834 | 0 | Py_UNREACHABLE(); |
4835 | 0 | } |
4836 | | |
4837 | 0 | if (spec->sys.attr != NULL) { |
4838 | | // Set the sys attribute, but don't set PyInterpreterState.config |
4839 | | // to keep the code simple. |
4840 | 0 | return PySys_SetObject(spec->sys.attr, value); |
4841 | 0 | } |
4842 | 0 | else if (has_int_value) { |
4843 | 0 | if (spec->sys.flag_index >= 0) { |
4844 | 0 | return config_set_sys_flag(spec, int_value); |
4845 | 0 | } |
4846 | 0 | else if (strcmp(spec->name, "int_max_str_digits") == 0) { |
4847 | 0 | return _PySys_SetIntMaxStrDigits(int_value); |
4848 | 0 | } |
4849 | 0 | else { |
4850 | 0 | return config_set_int_attr(spec, int_value); |
4851 | 0 | } |
4852 | 0 | } |
4853 | | |
4854 | 0 | cannot_set: |
4855 | 0 | PyErr_Format(PyExc_ValueError, "cannot set option %s", name); |
4856 | 0 | return -1; |
4857 | 0 | } |