Skip to content

Commit 6c785c0

Browse files
authored
bpo-34170: Add Python/coreconfig.c for _PyCoreConfig (GH-8607)
* Add Include/coreconfig.h * Move config_*() and _PyCoreConfig_*() functions from Modules/main.c to a new Python/coreconfig.c file. * Inline _Py_ReadHashSeed() into config_init_hash_seed() * Move global configuration variables to coreconfig.c
1 parent cfc8831 commit 6c785c0

14 files changed

+1314
-1288
lines changed

Include/Python.h

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
#include "codecs.h"
112112
#include "pyerrors.h"
113113

114+
#include "coreconfig.h"
114115
#include "pystate.h"
115116
#include "context.h"
116117

Include/coreconfig.h

+318
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
#ifndef Py_PYCORECONFIG_H
2+
#define Py_PYCORECONFIG_H
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
8+
#ifndef Py_LIMITED_API
9+
typedef struct {
10+
const char *prefix;
11+
const char *msg;
12+
int user_err;
13+
} _PyInitError;
14+
15+
/* Almost all errors causing Python initialization to fail */
16+
#ifdef _MSC_VER
17+
/* Visual Studio 2015 doesn't implement C99 __func__ in C */
18+
# define _Py_INIT_GET_FUNC() __FUNCTION__
19+
#else
20+
# define _Py_INIT_GET_FUNC() __func__
21+
#endif
22+
23+
#define _Py_INIT_OK() \
24+
(_PyInitError){.prefix = NULL, .msg = NULL, .user_err = 0}
25+
#define _Py_INIT_ERR(MSG) \
26+
(_PyInitError){.prefix = _Py_INIT_GET_FUNC(), .msg = (MSG), .user_err = 0}
27+
/* Error that can be fixed by the user like invalid input parameter.
28+
Don't abort() the process on such error. */
29+
#define _Py_INIT_USER_ERR(MSG) \
30+
(_PyInitError){.prefix = _Py_INIT_GET_FUNC(), .msg = (MSG), .user_err = 1}
31+
#define _Py_INIT_NO_MEMORY() _Py_INIT_USER_ERR("memory allocation failed")
32+
#define _Py_INIT_FAILED(err) \
33+
(err.msg != NULL)
34+
35+
#endif /* !defined(Py_LIMITED_API) */
36+
37+
38+
typedef struct {
39+
/* Install signal handlers? Yes by default. */
40+
int install_signal_handlers;
41+
42+
/* If greater than 0: use environment variables.
43+
Set to 0 by -E command line option. If set to -1 (default), it is
44+
set to !Py_IgnoreEnvironmentFlag. */
45+
int use_environment;
46+
47+
int use_hash_seed; /* PYTHONHASHSEED=x */
48+
unsigned long hash_seed;
49+
50+
const char *allocator; /* Memory allocator: PYTHONMALLOC */
51+
int dev_mode; /* PYTHONDEVMODE, -X dev */
52+
53+
/* Enable faulthandler?
54+
Set to 1 by -X faulthandler and PYTHONFAULTHANDLER. -1 means unset. */
55+
int faulthandler;
56+
57+
/* Enable tracemalloc?
58+
Set by -X tracemalloc=N and PYTHONTRACEMALLOC. -1 means unset */
59+
int tracemalloc;
60+
61+
int import_time; /* PYTHONPROFILEIMPORTTIME, -X importtime */
62+
int show_ref_count; /* -X showrefcount */
63+
int show_alloc_count; /* -X showalloccount */
64+
int dump_refs; /* PYTHONDUMPREFS */
65+
int malloc_stats; /* PYTHONMALLOCSTATS */
66+
int coerce_c_locale; /* PYTHONCOERCECLOCALE, -1 means unknown */
67+
int coerce_c_locale_warn; /* PYTHONCOERCECLOCALE=warn */
68+
69+
/* Enable UTF-8 mode?
70+
Set by -X utf8 command line option and PYTHONUTF8 environment variable.
71+
If set to -1 (default), inherit Py_UTF8Mode value. */
72+
int utf8_mode;
73+
74+
wchar_t *pycache_prefix; /* PYTHONPYCACHEPREFIX, -X pycache_prefix=PATH */
75+
76+
wchar_t *program_name; /* Program name, see also Py_GetProgramName() */
77+
int argc; /* Number of command line arguments,
78+
-1 means unset */
79+
wchar_t **argv; /* Command line arguments */
80+
wchar_t *program; /* argv[0] or "" */
81+
82+
int nxoption; /* Number of -X options */
83+
wchar_t **xoptions; /* -X options */
84+
85+
int nwarnoption; /* Number of warnings options */
86+
wchar_t **warnoptions; /* Warnings options */
87+
88+
/* Path configuration inputs */
89+
wchar_t *module_search_path_env; /* PYTHONPATH environment variable */
90+
wchar_t *home; /* PYTHONHOME environment variable,
91+
see also Py_SetPythonHome(). */
92+
93+
/* Path configuration outputs */
94+
int nmodule_search_path; /* Number of sys.path paths,
95+
-1 means unset */
96+
wchar_t **module_search_paths; /* sys.path paths */
97+
wchar_t *executable; /* sys.executable */
98+
wchar_t *prefix; /* sys.prefix */
99+
wchar_t *base_prefix; /* sys.base_prefix */
100+
wchar_t *exec_prefix; /* sys.exec_prefix */
101+
wchar_t *base_exec_prefix; /* sys.base_exec_prefix */
102+
#ifdef MS_WINDOWS
103+
wchar_t *dll_path; /* Windows DLL path */
104+
#endif
105+
106+
/* If greater than 0, enable isolated mode: sys.path contains
107+
neither the script's directory nor the user's site-packages directory.
108+
109+
Set to 1 by the -I command line option. If set to -1 (default), inherit
110+
Py_IsolatedFlag value. */
111+
int isolated;
112+
113+
/* If equal to zero, disable the import of the module site and the
114+
site-dependent manipulations of sys.path that it entails. Also disable
115+
these manipulations if site is explicitly imported later (call
116+
site.main() if you want them to be triggered).
117+
118+
Set to 0 by the -S command line option. If set to -1 (default), it is
119+
set to !Py_NoSiteFlag. */
120+
int site_import;
121+
122+
/* Bytes warnings:
123+
124+
* If equal to 1, issue a warning when comparing bytes or bytearray with
125+
str or bytes with int.
126+
* If equal or greater to 2, issue an error.
127+
128+
Incremented by the -b command line option. If set to -1 (default), inherit
129+
Py_BytesWarningFlag value. */
130+
int bytes_warning;
131+
132+
/* If greater than 0, enable inspect: when a script is passed as first
133+
argument or the -c option is used, enter interactive mode after
134+
executing the script or the command, even when sys.stdin does not appear
135+
to be a terminal.
136+
137+
Incremented by the -i command line option. Set to 1 if the PYTHONINSPECT
138+
environment variable is non-empty. If set to -1 (default), inherit
139+
Py_InspectFlag value. */
140+
int inspect;
141+
142+
/* If greater than 0: enable the interactive mode (REPL).
143+
144+
Incremented by the -i command line option. If set to -1 (default),
145+
inherit Py_InteractiveFlag value. */
146+
int interactive;
147+
148+
/* Optimization level.
149+
150+
Incremented by the -O command line option. Set by the PYTHONOPTIMIZE
151+
environment variable. If set to -1 (default), inherit Py_OptimizeFlag
152+
value. */
153+
int optimization_level;
154+
155+
/* If greater than 0, enable the debug mode: turn on parser debugging
156+
output (for expert only, depending on compilation options).
157+
158+
Incremented by the -d command line option. Set by the PYTHONDEBUG
159+
environment variable. If set to -1 (default), inherit Py_DebugFlag
160+
value. */
161+
int parser_debug;
162+
163+
/* If equal to 0, Python won't try to write ``.pyc`` files on the
164+
import of source modules.
165+
166+
Set to 0 by the -B command line option and the PYTHONDONTWRITEBYTECODE
167+
environment variable. If set to -1 (default), it is set to
168+
!Py_DontWriteBytecodeFlag. */
169+
int write_bytecode;
170+
171+
/* If greater than 0, enable the verbose mode: print a message each time a
172+
module is initialized, showing the place (filename or built-in module)
173+
from which it is loaded.
174+
175+
If greater or equal to 2, print a message for each file that is checked
176+
for when searching for a module. Also provides information on module
177+
cleanup at exit.
178+
179+
Incremented by the -v option. Set by the PYTHONVERBOSE environment
180+
variable. If set to -1 (default), inherit Py_VerboseFlag value. */
181+
int verbose;
182+
183+
/* If greater than 0, enable the quiet mode: Don't display the copyright
184+
and version messages even in interactive mode.
185+
186+
Incremented by the -q option. If set to -1 (default), inherit
187+
Py_QuietFlag value. */
188+
int quiet;
189+
190+
/* If greater than 0, don't add the user site-packages directory to
191+
sys.path.
192+
193+
Set to 0 by the -s and -I command line options , and the PYTHONNOUSERSITE
194+
environment variable. If set to -1 (default), it is set to
195+
!Py_NoUserSiteDirectory. */
196+
int user_site_directory;
197+
198+
/* If equal to 0, enable unbuffered mode: force the stdout and stderr
199+
streams to be unbuffered.
200+
201+
Set to 0 by the -u option. Set by the PYTHONUNBUFFERED environment
202+
variable.
203+
If set to -1 (default), it is set to !Py_UnbufferedStdioFlag. */
204+
int buffered_stdio;
205+
206+
#ifdef MS_WINDOWS
207+
/* If greater than 1, use the "mbcs" encoding instead of the UTF-8
208+
encoding for the filesystem encoding.
209+
210+
Set to 1 if the PYTHONLEGACYWINDOWSFSENCODING environment variable is
211+
set to a non-empty string. If set to -1 (default), inherit
212+
Py_LegacyWindowsFSEncodingFlag value.
213+
214+
See PEP 529 for more details. */
215+
int legacy_windows_fs_encoding;
216+
217+
/* If greater than zero, use io.FileIO instead of WindowsConsoleIO for sys
218+
standard streams.
219+
220+
Set to 1 if the PYTHONLEGACYWINDOWSSTDIO environment variable is set to
221+
a non-empty string. If set to -1 (default), inherit
222+
Py_LegacyWindowsStdioFlag value.
223+
224+
See PEP 528 for more details. */
225+
int legacy_windows_stdio;
226+
#endif
227+
228+
/* --- Private fields -------- */
229+
230+
/* Install importlib? If set to 0, importlib is not initialized at all.
231+
Needed by freeze_importlib. */
232+
int _install_importlib;
233+
234+
/* Value of the --check-hash-based-pycs configure option. Valid values:
235+
236+
- "default" means the 'check_source' flag in hash-based pycs
237+
determines invalidation
238+
- "always" causes the interpreter to hash the source file for
239+
invalidation regardless of value of 'check_source' bit
240+
- "never" causes the interpreter to always assume hash-based pycs are
241+
valid
242+
243+
Set by the --check-hash-based-pycs command line option.
244+
If set to NULL (default), inherit _Py_CheckHashBasedPycsMode value.
245+
246+
See PEP 552 "Deterministic pycs" for more details. */
247+
const char *_check_hash_pycs_mode;
248+
249+
/* If greater than 0, suppress _PyPathConfig_Calculate() warnings.
250+
251+
If set to -1 (default), inherit Py_FrozenFlag value. */
252+
int _frozen;
253+
254+
} _PyCoreConfig;
255+
256+
#ifdef MS_WINDOWS
257+
# define _PyCoreConfig_WINDOWS_INIT \
258+
.legacy_windows_fs_encoding = -1, \
259+
.legacy_windows_stdio = -1,
260+
#else
261+
# define _PyCoreConfig_WINDOWS_INIT
262+
#endif
263+
264+
#define _PyCoreConfig_INIT \
265+
(_PyCoreConfig){ \
266+
.install_signal_handlers = 1, \
267+
.use_environment = -1, \
268+
.use_hash_seed = -1, \
269+
.faulthandler = -1, \
270+
.tracemalloc = -1, \
271+
.coerce_c_locale = -1, \
272+
.utf8_mode = -1, \
273+
.argc = -1, \
274+
.nmodule_search_path = -1, \
275+
.isolated = -1, \
276+
.site_import = -1, \
277+
.bytes_warning = -1, \
278+
.inspect = -1, \
279+
.interactive = -1, \
280+
.optimization_level = -1, \
281+
.parser_debug= -1, \
282+
.write_bytecode = -1, \
283+
.verbose = -1, \
284+
.quiet = -1, \
285+
.user_site_directory = -1, \
286+
.buffered_stdio = -1, \
287+
_PyCoreConfig_WINDOWS_INIT \
288+
._install_importlib = 1, \
289+
._frozen = -1}
290+
/* Note: _PyCoreConfig_INIT sets other fields to 0/NULL */
291+
292+
293+
#ifndef Py_LIMITED_API
294+
PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(_PyCoreConfig *config);
295+
PyAPI_FUNC(void) _PyCoreConfig_Clear(_PyCoreConfig *);
296+
PyAPI_FUNC(int) _PyCoreConfig_Copy(
297+
_PyCoreConfig *config,
298+
const _PyCoreConfig *config2);
299+
PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitPathConfig(_PyCoreConfig *config);
300+
PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetPathConfig(
301+
const _PyCoreConfig *config);
302+
PyAPI_FUNC(void) _PyCoreConfig_GetGlobalConfig(_PyCoreConfig *config);
303+
PyAPI_FUNC(void) _PyCoreConfig_SetGlobalConfig(const _PyCoreConfig *config);
304+
PyAPI_FUNC(const char*) _PyCoreConfig_GetEnv(
305+
const _PyCoreConfig *config,
306+
const char *name);
307+
PyAPI_FUNC(int) _PyCoreConfig_GetEnvDup(
308+
const _PyCoreConfig *config,
309+
wchar_t **dest,
310+
wchar_t *wname,
311+
char *name);
312+
#endif
313+
314+
315+
#ifdef __cplusplus
316+
}
317+
#endif
318+
#endif /* !Py_PYCORECONFIG_H */

Include/internal/pystate.h

+6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ PyAPI_FUNC(_PyInitError) _PyPathConfig_Calculate_impl(
7272
const _PyCoreConfig *core_config);
7373
PyAPI_FUNC(void) _PyPathConfig_ClearGlobal(void);
7474

75+
PyAPI_FUNC(void) _Py_wstrlist_clear(
76+
int len,
77+
wchar_t **list);
78+
PyAPI_FUNC(wchar_t**) _Py_wstrlist_copy(
79+
int len,
80+
wchar_t **list);
7581
PyAPI_FUNC(_PyInitError) _Py_wstrlist_append(
7682
int *len,
7783
wchar_t ***list,

0 commit comments

Comments
 (0)