blob: edfeba5db7dfab0b2b823cffc0173e9a747012e4 [file] [log] [blame]
Victor Stinner331a6a52019-05-27 16:39:22 +02001.. highlight:: c
2
3.. _init-config:
4
5***********************************
6Python Initialization Configuration
7***********************************
8
9.. versionadded:: 3.8
10
Victor Stinnerdc42af82020-11-05 18:58:07 +010011Python can be initialized with :c:func:`Py_InitializeFromConfig` and the
12:c:type:`PyConfig` structure. It can be preinitialized with
13:c:func:`Py_PreInitialize` and the :c:type:`PyPreConfig` structure.
Victor Stinner331a6a52019-05-27 16:39:22 +020014
Victor Stinnerdc42af82020-11-05 18:58:07 +010015There are two kinds of configuration:
Victor Stinner331a6a52019-05-27 16:39:22 +020016
Victor Stinnerdc42af82020-11-05 18:58:07 +010017* The :ref:`Python Configuration <init-python-config>` can be used to build a
18 customized Python which behaves as the regular Python. For example,
19 environments variables and command line arguments are used to configure
20 Python.
Victor Stinner331a6a52019-05-27 16:39:22 +020021
Victor Stinnerdc42af82020-11-05 18:58:07 +010022* The :ref:`Isolated Configuration <init-isolated-conf>` can be used to embed
23 Python into an application. It isolates Python from the system. For example,
24 environments variables are ignored, the LC_CTYPE locale is left unchanged and
25 no signal handler is registred.
Victor Stinner331a6a52019-05-27 16:39:22 +020026
Victor Stinner1beb7c32019-08-23 17:59:12 +010027See also :ref:`Initialization, Finalization, and Threads <initialization>`.
28
Victor Stinner331a6a52019-05-27 16:39:22 +020029.. seealso::
30 :pep:`587` "Python Initialization Configuration".
31
Victor Stinnerdc42af82020-11-05 18:58:07 +010032Example
33=======
34
35Example of customized Python always running in isolated mode::
36
37 int main(int argc, char **argv)
38 {
39 PyStatus status;
40
41 PyConfig config;
42 PyConfig_InitPythonConfig(&config);
43 config.isolated = 1;
44
45 /* Decode command line arguments.
46 Implicitly preinitialize Python (in isolated mode). */
47 status = PyConfig_SetBytesArgv(&config, argc, argv);
48 if (PyStatus_Exception(status)) {
49 goto exception;
50 }
51
52 status = Py_InitializeFromConfig(&config);
53 if (PyStatus_Exception(status)) {
54 goto exception;
55 }
56 PyConfig_Clear(&config);
57
58 return Py_RunMain();
59
60 exception:
61 PyConfig_Clear(&config);
62 if (PyStatus_IsExit(status)) {
63 return status.exitcode;
64 }
65 /* Display the error message and exit the process with
66 non-zero exit code */
67 Py_ExitStatusException(status);
68 }
69
Victor Stinner331a6a52019-05-27 16:39:22 +020070
71PyWideStringList
Victor Stinnerdc42af82020-11-05 18:58:07 +010072================
Victor Stinner331a6a52019-05-27 16:39:22 +020073
74.. c:type:: PyWideStringList
75
76 List of ``wchar_t*`` strings.
77
Serhiy Storchakae835b312019-10-30 21:37:16 +020078 If *length* is non-zero, *items* must be non-``NULL`` and all strings must be
79 non-``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +020080
81 Methods:
82
83 .. c:function:: PyStatus PyWideStringList_Append(PyWideStringList *list, const wchar_t *item)
84
85 Append *item* to *list*.
86
87 Python must be preinitialized to call this function.
88
89 .. c:function:: PyStatus PyWideStringList_Insert(PyWideStringList *list, Py_ssize_t index, const wchar_t *item)
90
Victor Stinner3842f292019-08-23 16:57:54 +010091 Insert *item* into *list* at *index*.
92
93 If *index* is greater than or equal to *list* length, append *item* to
94 *list*.
95
96 *index* must be greater than or equal to 0.
Victor Stinner331a6a52019-05-27 16:39:22 +020097
98 Python must be preinitialized to call this function.
99
100 Structure fields:
101
102 .. c:member:: Py_ssize_t length
103
104 List length.
105
106 .. c:member:: wchar_t** items
107
108 List items.
109
110PyStatus
Victor Stinnerdc42af82020-11-05 18:58:07 +0100111========
Victor Stinner331a6a52019-05-27 16:39:22 +0200112
113.. c:type:: PyStatus
114
115 Structure to store an initialization function status: success, error
116 or exit.
117
118 For an error, it can store the C function name which created the error.
119
120 Structure fields:
121
122 .. c:member:: int exitcode
123
124 Exit code. Argument passed to ``exit()``.
125
126 .. c:member:: const char *err_msg
127
128 Error message.
129
130 .. c:member:: const char *func
131
132 Name of the function which created an error, can be ``NULL``.
133
134 Functions to create a status:
135
136 .. c:function:: PyStatus PyStatus_Ok(void)
137
138 Success.
139
140 .. c:function:: PyStatus PyStatus_Error(const char *err_msg)
141
142 Initialization error with a message.
143
Victor Stinner048a3562020-11-05 00:45:56 +0100144 *err_msg* must not be ``NULL``.
145
Victor Stinner331a6a52019-05-27 16:39:22 +0200146 .. c:function:: PyStatus PyStatus_NoMemory(void)
147
148 Memory allocation failure (out of memory).
149
150 .. c:function:: PyStatus PyStatus_Exit(int exitcode)
151
152 Exit Python with the specified exit code.
153
154 Functions to handle a status:
155
156 .. c:function:: int PyStatus_Exception(PyStatus status)
157
158 Is the status an error or an exit? If true, the exception must be
159 handled; by calling :c:func:`Py_ExitStatusException` for example.
160
161 .. c:function:: int PyStatus_IsError(PyStatus status)
162
163 Is the result an error?
164
165 .. c:function:: int PyStatus_IsExit(PyStatus status)
166
167 Is the result an exit?
168
169 .. c:function:: void Py_ExitStatusException(PyStatus status)
170
171 Call ``exit(exitcode)`` if *status* is an exit. Print the error
172 message and exit with a non-zero exit code if *status* is an error. Must
173 only be called if ``PyStatus_Exception(status)`` is non-zero.
174
175.. note::
176 Internally, Python uses macros which set ``PyStatus.func``,
177 whereas functions to create a status set ``func`` to ``NULL``.
178
179Example::
180
181 PyStatus alloc(void **ptr, size_t size)
182 {
183 *ptr = PyMem_RawMalloc(size);
184 if (*ptr == NULL) {
185 return PyStatus_NoMemory();
186 }
187 return PyStatus_Ok();
188 }
189
190 int main(int argc, char **argv)
191 {
192 void *ptr;
193 PyStatus status = alloc(&ptr, 16);
194 if (PyStatus_Exception(status)) {
195 Py_ExitStatusException(status);
196 }
197 PyMem_Free(ptr);
198 return 0;
199 }
200
201
202PyPreConfig
Victor Stinnerdc42af82020-11-05 18:58:07 +0100203===========
Victor Stinner331a6a52019-05-27 16:39:22 +0200204
205.. c:type:: PyPreConfig
206
Victor Stinner4b9aad42020-11-02 16:49:54 +0100207 Structure used to preinitialize Python.
Victor Stinner331a6a52019-05-27 16:39:22 +0200208
209 Function to initialize a preconfiguration:
210
tomerv741008a2020-07-01 12:32:54 +0300211 .. c:function:: void PyPreConfig_InitPythonConfig(PyPreConfig *preconfig)
Victor Stinner331a6a52019-05-27 16:39:22 +0200212
213 Initialize the preconfiguration with :ref:`Python Configuration
214 <init-python-config>`.
215
tomerv741008a2020-07-01 12:32:54 +0300216 .. c:function:: void PyPreConfig_InitIsolatedConfig(PyPreConfig *preconfig)
Victor Stinner331a6a52019-05-27 16:39:22 +0200217
218 Initialize the preconfiguration with :ref:`Isolated Configuration
219 <init-isolated-conf>`.
220
221 Structure fields:
222
223 .. c:member:: int allocator
224
Victor Stinner4b9aad42020-11-02 16:49:54 +0100225 Name of the Python memory allocators:
Victor Stinner331a6a52019-05-27 16:39:22 +0200226
227 * ``PYMEM_ALLOCATOR_NOT_SET`` (``0``): don't change memory allocators
228 (use defaults)
229 * ``PYMEM_ALLOCATOR_DEFAULT`` (``1``): default memory allocators
230 * ``PYMEM_ALLOCATOR_DEBUG`` (``2``): default memory allocators with
231 debug hooks
232 * ``PYMEM_ALLOCATOR_MALLOC`` (``3``): force usage of ``malloc()``
233 * ``PYMEM_ALLOCATOR_MALLOC_DEBUG`` (``4``): force usage of
234 ``malloc()`` with debug hooks
235 * ``PYMEM_ALLOCATOR_PYMALLOC`` (``5``): :ref:`Python pymalloc memory
236 allocator <pymalloc>`
237 * ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG`` (``6``): :ref:`Python pymalloc
238 memory allocator <pymalloc>` with debug hooks
239
240 ``PYMEM_ALLOCATOR_PYMALLOC`` and ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG``
241 are not supported if Python is configured using ``--without-pymalloc``
242
243 See :ref:`Memory Management <memory>`.
244
Victor Stinner4b9aad42020-11-02 16:49:54 +0100245 Default: ``PYMEM_ALLOCATOR_NOT_SET``.
246
Victor Stinner331a6a52019-05-27 16:39:22 +0200247 .. c:member:: int configure_locale
248
Victor Stinner4b9aad42020-11-02 16:49:54 +0100249 Set the LC_CTYPE locale to the user preferred locale?
250
251 If equals to 0, set :c:member:`~PyPreConfig.coerce_c_locale` and
252 :c:member:`~PyPreConfig.coerce_c_locale_warn` members to 0.
253
254 See the :term:`locale encoding`.
255
256 Default: ``1`` in Python config, ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +0200257
258 .. c:member:: int coerce_c_locale
259
Victor Stinner4b9aad42020-11-02 16:49:54 +0100260 If equals to 2, coerce the C locale.
261
262 If equals to 1, read the LC_CTYPE locale to decide if it should be
263 coerced.
264
265 See the :term:`locale encoding`.
266
267 Default: ``-1`` in Python config, ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +0200268
269 .. c:member:: int coerce_c_locale_warn
Victor Stinner88feaec2019-09-26 03:15:07 +0200270
Victor Stinner331a6a52019-05-27 16:39:22 +0200271 If non-zero, emit a warning if the C locale is coerced.
272
Victor Stinner4b9aad42020-11-02 16:49:54 +0100273 Default: ``-1`` in Python config, ``0`` in isolated config.
274
Victor Stinner331a6a52019-05-27 16:39:22 +0200275 .. c:member:: int dev_mode
276
Victor Stinner4b9aad42020-11-02 16:49:54 +0100277 If non-zero, enables the :ref:`Python Development Mode <devmode>`:
278 see :c:member:`PyConfig.dev_mode`.
279
280 Default: ``-1`` in Python mode, ``0`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200281
282 .. c:member:: int isolated
283
Victor Stinner4b9aad42020-11-02 16:49:54 +0100284 Isolated mode: see :c:member:`PyConfig.isolated`.
285
286 Default: ``0`` in Python mode, ``1`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200287
Victor Stinnere662c392020-11-01 23:07:23 +0100288 .. c:member:: int legacy_windows_fs_encoding
Victor Stinner331a6a52019-05-27 16:39:22 +0200289
Victor Stinnere662c392020-11-01 23:07:23 +0100290 If non-zero:
291
292 * Set :c:member:`PyPreConfig.utf8_mode` to ``0``,
293 * Set :c:member:`PyConfig.filesystem_encoding` to ``"mbcs"``,
294 * Set :c:member:`PyConfig.filesystem_errors` to ``"replace"``.
295
296 Initialized the from :envvar:`PYTHONLEGACYWINDOWSFSENCODING` environment
297 variable value.
Victor Stinner331a6a52019-05-27 16:39:22 +0200298
299 Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
300 Windows specific code.
301
Victor Stinner4b9aad42020-11-02 16:49:54 +0100302 Default: ``0``.
303
Victor Stinner331a6a52019-05-27 16:39:22 +0200304 .. c:member:: int parse_argv
305
306 If non-zero, :c:func:`Py_PreInitializeFromArgs` and
307 :c:func:`Py_PreInitializeFromBytesArgs` parse their ``argv`` argument the
308 same way the regular Python parses command line arguments: see
309 :ref:`Command Line Arguments <using-on-cmdline>`.
310
Victor Stinner4b9aad42020-11-02 16:49:54 +0100311 Default: ``1`` in Python config, ``0`` in isolated config.
312
Victor Stinner331a6a52019-05-27 16:39:22 +0200313 .. c:member:: int use_environment
314
Victor Stinner4b9aad42020-11-02 16:49:54 +0100315 Use :ref:`environment variables <using-on-envvars>`? See
316 :c:member:`PyConfig.use_environment`.
317
318 Default: ``1`` in Python config and ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +0200319
320 .. c:member:: int utf8_mode
321
Victor Stinner4b9aad42020-11-02 16:49:54 +0100322 If non-zero, enable the :ref:`Python UTF-8 Mode <utf8-mode>`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200323
Victor Stinner4b9aad42020-11-02 16:49:54 +0100324 Set by the :option:`-X utf8 <-X>` command line option and the
325 :envvar:`PYTHONUTF8` environment variable.
326
327 Default: ``-1`` in Python config and ``0`` in isolated config.
328
329
330.. _c-preinit:
331
332Preinitialize Python with PyPreConfig
Victor Stinnerdc42af82020-11-05 18:58:07 +0100333=====================================
Victor Stinner4b9aad42020-11-02 16:49:54 +0100334
335The preinitialization of Python:
336
337* Set the Python memory allocators (:c:member:`PyPreConfig.allocator`)
338* Configure the LC_CTYPE locale (:term:`locale encoding`)
339* Set the :ref:`Python UTF-8 Mode <utf8-mode>`
340 (:c:member:`PyPreConfig.utf8_mode`)
Victor Stinner331a6a52019-05-27 16:39:22 +0200341
Victor Stinnerdc42af82020-11-05 18:58:07 +0100342The current preconfiguration (``PyPreConfig`` type) is stored in
343``_PyRuntime.preconfig``.
344
Victor Stinner331a6a52019-05-27 16:39:22 +0200345Functions to preinitialize Python:
346
347.. c:function:: PyStatus Py_PreInitialize(const PyPreConfig *preconfig)
348
349 Preinitialize Python from *preconfig* preconfiguration.
350
Victor Stinnerdc42af82020-11-05 18:58:07 +0100351 *preconfig* must not be ``NULL``.
352
Victor Stinner331a6a52019-05-27 16:39:22 +0200353.. c:function:: PyStatus Py_PreInitializeFromBytesArgs(const PyPreConfig *preconfig, int argc, char * const *argv)
354
Victor Stinner4b9aad42020-11-02 16:49:54 +0100355 Preinitialize Python from *preconfig* preconfiguration.
356
357 Parse *argv* command line arguments (bytes strings) if
358 :c:member:`~PyPreConfig.parse_argv` of *preconfig* is non-zero.
Victor Stinner331a6a52019-05-27 16:39:22 +0200359
Victor Stinnerdc42af82020-11-05 18:58:07 +0100360 *preconfig* must not be ``NULL``.
361
Victor Stinner331a6a52019-05-27 16:39:22 +0200362.. c:function:: PyStatus Py_PreInitializeFromArgs(const PyPreConfig *preconfig, int argc, wchar_t * const * argv)
363
Victor Stinner4b9aad42020-11-02 16:49:54 +0100364 Preinitialize Python from *preconfig* preconfiguration.
365
366 Parse *argv* command line arguments (wide strings) if
367 :c:member:`~PyPreConfig.parse_argv` of *preconfig* is non-zero.
Victor Stinner331a6a52019-05-27 16:39:22 +0200368
Victor Stinnerdc42af82020-11-05 18:58:07 +0100369 *preconfig* must not be ``NULL``.
370
Victor Stinner331a6a52019-05-27 16:39:22 +0200371The caller is responsible to handle exceptions (error or exit) using
372:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
373
374For :ref:`Python Configuration <init-python-config>`
375(:c:func:`PyPreConfig_InitPythonConfig`), if Python is initialized with
376command line arguments, the command line arguments must also be passed to
377preinitialize Python, since they have an effect on the pre-configuration
Victor Stinner88feaec2019-09-26 03:15:07 +0200378like encodings. For example, the :option:`-X utf8 <-X>` command line option
Victor Stinner4b9aad42020-11-02 16:49:54 +0100379enables the :ref:`Python UTF-8 Mode <utf8-mode>`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200380
381``PyMem_SetAllocator()`` can be called after :c:func:`Py_PreInitialize` and
382before :c:func:`Py_InitializeFromConfig` to install a custom memory allocator.
383It can be called before :c:func:`Py_PreInitialize` if
384:c:member:`PyPreConfig.allocator` is set to ``PYMEM_ALLOCATOR_NOT_SET``.
385
386Python memory allocation functions like :c:func:`PyMem_RawMalloc` must not be
Victor Stinner4b9aad42020-11-02 16:49:54 +0100387used before the Python preinitialization, whereas calling directly ``malloc()``
388and ``free()`` is always safe. :c:func:`Py_DecodeLocale` must not be called
389before the Python preinitialization.
Victor Stinner331a6a52019-05-27 16:39:22 +0200390
Victor Stinner4b9aad42020-11-02 16:49:54 +0100391Example using the preinitialization to enable
392the :ref:`Python UTF-8 Mode <utf8-mode>`::
Victor Stinner331a6a52019-05-27 16:39:22 +0200393
Victor Stinner441b10c2019-09-28 04:28:35 +0200394 PyStatus status;
Victor Stinner331a6a52019-05-27 16:39:22 +0200395 PyPreConfig preconfig;
Victor Stinner3c30a762019-10-01 10:56:37 +0200396 PyPreConfig_InitPythonConfig(&preconfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200397
398 preconfig.utf8_mode = 1;
399
Victor Stinner441b10c2019-09-28 04:28:35 +0200400 status = Py_PreInitialize(&preconfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200401 if (PyStatus_Exception(status)) {
402 Py_ExitStatusException(status);
403 }
404
Victor Stinner4b9aad42020-11-02 16:49:54 +0100405 /* at this point, Python speaks UTF-8 */
Victor Stinner331a6a52019-05-27 16:39:22 +0200406
407 Py_Initialize();
408 /* ... use Python API here ... */
409 Py_Finalize();
410
411
412PyConfig
Victor Stinnerdc42af82020-11-05 18:58:07 +0100413========
Victor Stinner331a6a52019-05-27 16:39:22 +0200414
415.. c:type:: PyConfig
416
417 Structure containing most parameters to configure Python.
418
Victor Stinner4b9aad42020-11-02 16:49:54 +0100419 When done, the :c:func:`PyConfig_Clear` function must be used to release the
420 configuration memory.
421
Victor Stinner331a6a52019-05-27 16:39:22 +0200422 Structure methods:
423
Victor Stinner8462a492019-10-01 12:06:16 +0200424 .. c:function:: void PyConfig_InitPythonConfig(PyConfig *config)
Victor Stinner331a6a52019-05-27 16:39:22 +0200425
Victor Stinner4b9aad42020-11-02 16:49:54 +0100426 Initialize configuration with the :ref:`Python Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +0200427 <init-python-config>`.
428
Victor Stinner8462a492019-10-01 12:06:16 +0200429 .. c:function:: void PyConfig_InitIsolatedConfig(PyConfig *config)
Victor Stinner331a6a52019-05-27 16:39:22 +0200430
Victor Stinner4b9aad42020-11-02 16:49:54 +0100431 Initialize configuration with the :ref:`Isolated Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +0200432 <init-isolated-conf>`.
433
434 .. c:function:: PyStatus PyConfig_SetString(PyConfig *config, wchar_t * const *config_str, const wchar_t *str)
435
436 Copy the wide character string *str* into ``*config_str``.
437
Victor Stinner4b9aad42020-11-02 16:49:54 +0100438 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200439
440 .. c:function:: PyStatus PyConfig_SetBytesString(PyConfig *config, wchar_t * const *config_str, const char *str)
441
Victor Stinner4b9aad42020-11-02 16:49:54 +0100442 Decode *str* using :c:func:`Py_DecodeLocale` and set the result into
443 ``*config_str``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200444
Victor Stinner4b9aad42020-11-02 16:49:54 +0100445 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200446
447 .. c:function:: PyStatus PyConfig_SetArgv(PyConfig *config, int argc, wchar_t * const *argv)
448
Victor Stinner4b9aad42020-11-02 16:49:54 +0100449 Set command line arguments (:c:member:`~PyConfig.argv` member of
450 *config*) from the *argv* list of wide character strings.
Victor Stinner331a6a52019-05-27 16:39:22 +0200451
Victor Stinner4b9aad42020-11-02 16:49:54 +0100452 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200453
454 .. c:function:: PyStatus PyConfig_SetBytesArgv(PyConfig *config, int argc, char * const *argv)
455
Victor Stinner4b9aad42020-11-02 16:49:54 +0100456 Set command line arguments (:c:member:`~PyConfig.argv` member of
457 *config*) from the *argv* list of bytes strings. Decode bytes using
458 :c:func:`Py_DecodeLocale`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200459
Victor Stinner4b9aad42020-11-02 16:49:54 +0100460 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200461
Victor Stinner36242fd2019-07-01 19:13:50 +0200462 .. c:function:: PyStatus PyConfig_SetWideStringList(PyConfig *config, PyWideStringList *list, Py_ssize_t length, wchar_t **items)
463
464 Set the list of wide strings *list* to *length* and *items*.
465
Victor Stinner4b9aad42020-11-02 16:49:54 +0100466 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner36242fd2019-07-01 19:13:50 +0200467
Victor Stinner331a6a52019-05-27 16:39:22 +0200468 .. c:function:: PyStatus PyConfig_Read(PyConfig *config)
469
470 Read all Python configuration.
471
472 Fields which are already initialized are left unchanged.
473
Victor Stinnerdc42af82020-11-05 18:58:07 +0100474 The :c:func:`PyConfig_Read` function only parses
475 :c:member:`PyConfig.argv` arguments once: :c:member:`PyConfig.parse_argv`
476 is set to ``2`` after arguments are parsed. Since Python arguments are
477 strippped from :c:member:`PyConfig.argv`, parsing arguments twice would
478 parse the application options as Python options.
479
Victor Stinner4b9aad42020-11-02 16:49:54 +0100480 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200481
Victor Stinnerdc42af82020-11-05 18:58:07 +0100482 .. versionchanged:: 3.10
483 The :c:member:`PyConfig.argv` arguments are now only parsed once,
484 :c:member:`PyConfig.parse_argv` is set to ``2`` after arguments are
485 parsed, and arguments are only parsed if
486 :c:member:`PyConfig.parse_argv` equals ``1``.
487
Victor Stinner331a6a52019-05-27 16:39:22 +0200488 .. c:function:: void PyConfig_Clear(PyConfig *config)
489
490 Release configuration memory.
491
Victor Stinner4b9aad42020-11-02 16:49:54 +0100492 Most ``PyConfig`` methods :ref:`preinitialize Python <c-preinit>` if needed.
493 In that case, the Python preinitialization configuration
494 (:c:type:`PyPreConfig`) in based on the :c:type:`PyConfig`. If configuration
495 fields which are in common with :c:type:`PyPreConfig` are tuned, they must
496 be set before calling a :c:type:`PyConfig` method:
Victor Stinner331a6a52019-05-27 16:39:22 +0200497
Victor Stinner4b9aad42020-11-02 16:49:54 +0100498 * :c:member:`PyConfig.dev_mode`
499 * :c:member:`PyConfig.isolated`
500 * :c:member:`PyConfig.parse_argv`
501 * :c:member:`PyConfig.use_environment`
Victor Stinner331a6a52019-05-27 16:39:22 +0200502
503 Moreover, if :c:func:`PyConfig_SetArgv` or :c:func:`PyConfig_SetBytesArgv`
Victor Stinner4b9aad42020-11-02 16:49:54 +0100504 is used, this method must be called before other methods, since the
Victor Stinner331a6a52019-05-27 16:39:22 +0200505 preinitialization configuration depends on command line arguments (if
506 :c:member:`parse_argv` is non-zero).
507
508 The caller of these methods is responsible to handle exceptions (error or
509 exit) using ``PyStatus_Exception()`` and ``Py_ExitStatusException()``.
510
511 Structure fields:
512
513 .. c:member:: PyWideStringList argv
514
Victor Stinner4b9aad42020-11-02 16:49:54 +0100515 Command line arguments: :data:`sys.argv`.
516
517 Set :c:member:`~PyConfig.parse_argv` to ``1`` to parse
518 :c:member:`~PyConfig.argv` the same way the regular Python parses Python
519 command line arguments and then to strip Python arguments from
520 :c:member:`~PyConfig.argv`.
521
522 If :c:member:`~PyConfig.argv` is empty, an empty string is added to
523 ensure that :data:`sys.argv` always exists and is never empty.
524
525 Default: ``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200526
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200527 See also the :c:member:`~PyConfig.orig_argv` member.
528
Victor Stinner331a6a52019-05-27 16:39:22 +0200529 .. c:member:: wchar_t* base_exec_prefix
530
531 :data:`sys.base_exec_prefix`.
532
Victor Stinner4b9aad42020-11-02 16:49:54 +0100533 Default: ``NULL``.
534
535 Part of the :ref:`Path Configuration <init-path-config>` output.
536
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200537 .. c:member:: wchar_t* base_executable
538
Victor Stinner4b9aad42020-11-02 16:49:54 +0100539 Python base executable: :data:`sys._base_executable`.
540
541 Set by the :envvar:`__PYVENV_LAUNCHER__` environment variable.
542
543 Set from :c:member:`PyConfig.executable` if ``NULL``.
544
545 Default: ``NULL``.
546
547 Part of the :ref:`Path Configuration <init-path-config>` output.
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200548
Victor Stinner331a6a52019-05-27 16:39:22 +0200549 .. c:member:: wchar_t* base_prefix
550
551 :data:`sys.base_prefix`.
552
Victor Stinner4b9aad42020-11-02 16:49:54 +0100553 Default: ``NULL``.
Sandro Mani8f023a22020-06-08 17:28:11 +0200554
Victor Stinner4b9aad42020-11-02 16:49:54 +0100555 Part of the :ref:`Path Configuration <init-path-config>` output.
Sandro Mani8f023a22020-06-08 17:28:11 +0200556
Victor Stinner331a6a52019-05-27 16:39:22 +0200557 .. c:member:: int buffered_stdio
558
Victor Stinner4b9aad42020-11-02 16:49:54 +0100559 If equals to 0 and :c:member:`~PyConfig.configure_c_stdio` is non-zero,
560 disable buffering on the C streams stdout and stderr.
561
562 Set to 0 by the :option:`-u` command line option and the
563 :envvar:`PYTHONUNBUFFERED` environment variable.
Victor Stinner331a6a52019-05-27 16:39:22 +0200564
565 stdin is always opened in buffered mode.
566
Victor Stinner4b9aad42020-11-02 16:49:54 +0100567 Default: ``1``.
568
Victor Stinner331a6a52019-05-27 16:39:22 +0200569 .. c:member:: int bytes_warning
570
571 If equals to 1, issue a warning when comparing :class:`bytes` or
572 :class:`bytearray` with :class:`str`, or comparing :class:`bytes` with
Victor Stinner4b9aad42020-11-02 16:49:54 +0100573 :class:`int`.
574
575 If equal or greater to 2, raise a :exc:`BytesWarning` exception in these
576 cases.
577
578 Incremented by the :option:`-b` command line option.
579
580 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200581
582 .. c:member:: wchar_t* check_hash_pycs_mode
583
Victor Stinner4b9aad42020-11-02 16:49:54 +0100584 Control the validation behavior of hash-based ``.pyc`` files:
585 value of the :option:`--check-hash-based-pycs` command line option.
Victor Stinner331a6a52019-05-27 16:39:22 +0200586
Victor Stinner4b9aad42020-11-02 16:49:54 +0100587 Valid values:
Victor Stinner331a6a52019-05-27 16:39:22 +0200588
Victor Stinner4b9aad42020-11-02 16:49:54 +0100589 - ``L"always"``: Hash the source file for invalidation regardless of
590 value of the 'check_source' flag.
591 - ``L"never"``: Assume that hash-based pycs always are valid.
592 - ``L"default"``: The 'check_source' flag in hash-based pycs
593 determines invalidation.
594
595 Default: ``L"default"``.
596
597 See also :pep:`552` "Deterministic pycs".
Victor Stinner331a6a52019-05-27 16:39:22 +0200598
599 .. c:member:: int configure_c_stdio
600
Victor Stinner4b9aad42020-11-02 16:49:54 +0100601 If non-zero, configure C standard streams:
602
603 * On Windows, set the binary mode (``O_BINARY``) on stdin, stdout and
604 stderr.
605 * If :c:member:`~PyConfig.buffered_stdio` equals zero, disable buffering
606 of stdin, stdout and stderr streams.
607 * If :c:member:`~PyConfig.interactive` is non-zero, enable stream
608 buffering on stdin and stdout (only stdout on Windows).
609
610 Default: ``1`` in Python config, ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +0200611
612 .. c:member:: int dev_mode
613
Victor Stinnerb9783d22020-01-24 10:22:18 +0100614 If non-zero, enable the :ref:`Python Development Mode <devmode>`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200615
Victor Stinner4b9aad42020-11-02 16:49:54 +0100616 Default: ``-1`` in Python mode, ``0`` in isolated mode.
617
Victor Stinner331a6a52019-05-27 16:39:22 +0200618 .. c:member:: int dump_refs
619
Victor Stinner4b9aad42020-11-02 16:49:54 +0100620 Dump Python refererences?
621
Victor Stinner331a6a52019-05-27 16:39:22 +0200622 If non-zero, dump all objects which are still alive at exit.
623
Victor Stinner4b9aad42020-11-02 16:49:54 +0100624 Set to ``1`` by the :envvar:`PYTHONDUMPREFS` environment variable.
625
626 Need a special build of Python with the ``Py_TRACE_REFS`` macro defined.
627
628 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200629
630 .. c:member:: wchar_t* exec_prefix
631
Victor Stinner4b9aad42020-11-02 16:49:54 +0100632 The site-specific directory prefix where the platform-dependent Python
633 files are installed: :data:`sys.exec_prefix`.
634
635 Default: ``NULL``.
636
637 Part of the :ref:`Path Configuration <init-path-config>` output.
Victor Stinner331a6a52019-05-27 16:39:22 +0200638
639 .. c:member:: wchar_t* executable
640
Victor Stinner4b9aad42020-11-02 16:49:54 +0100641 The absolute path of the executable binary for the Python interpreter:
Victor Stinner331a6a52019-05-27 16:39:22 +0200642 :data:`sys.executable`.
643
Victor Stinner4b9aad42020-11-02 16:49:54 +0100644 Default: ``NULL``.
645
646 Part of the :ref:`Path Configuration <init-path-config>` output.
647
Victor Stinner331a6a52019-05-27 16:39:22 +0200648 .. c:member:: int faulthandler
649
Victor Stinner4b9aad42020-11-02 16:49:54 +0100650 Enable faulthandler?
651
Victor Stinner88feaec2019-09-26 03:15:07 +0200652 If non-zero, call :func:`faulthandler.enable` at startup.
Victor Stinner331a6a52019-05-27 16:39:22 +0200653
Victor Stinner4b9aad42020-11-02 16:49:54 +0100654 Set to ``1`` by :option:`-X faulthandler <-X>` and the
655 :envvar:`PYTHONFAULTHANDLER` environment variable.
656
657 Default: ``-1`` in Python mode, ``0`` in isolated mode.
658
Victor Stinner331a6a52019-05-27 16:39:22 +0200659 .. c:member:: wchar_t* filesystem_encoding
660
Victor Stinner4b9aad42020-11-02 16:49:54 +0100661 :term:`Filesystem encoding <filesystem encoding and error handler>`:
662 :func:`sys.getfilesystemencoding`.
Victor Stinnere662c392020-11-01 23:07:23 +0100663
664 On macOS, Android and VxWorks: use ``"utf-8"`` by default.
665
666 On Windows: use ``"utf-8"`` by default, or ``"mbcs"`` if
667 :c:member:`~PyPreConfig.legacy_windows_fs_encoding` of
668 :c:type:`PyPreConfig` is non-zero.
669
670 Default encoding on other platforms:
671
672 * ``"utf-8"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero.
673 * ``"ascii"`` if Python detects that ``nl_langinfo(CODESET)`` announces
674 the ASCII encoding (or Roman8 encoding on HP-UX), whereas the
675 ``mbstowcs()`` function decodes from a different encoding (usually
676 Latin1).
677 * ``"utf-8"`` if ``nl_langinfo(CODESET)`` returns an empty string.
Victor Stinner4b9aad42020-11-02 16:49:54 +0100678 * Otherwise, use the :term:`locale encoding`:
Victor Stinnere662c392020-11-01 23:07:23 +0100679 ``nl_langinfo(CODESET)`` result.
680
681 At Python statup, the encoding name is normalized to the Python codec
682 name. For example, ``"ANSI_X3.4-1968"`` is replaced with ``"ascii"``.
683
684 See also the :c:member:`~PyConfig.filesystem_errors` member.
Victor Stinner331a6a52019-05-27 16:39:22 +0200685
686 .. c:member:: wchar_t* filesystem_errors
687
Victor Stinner4b9aad42020-11-02 16:49:54 +0100688 :term:`Filesystem error handler <filesystem encoding and error handler>`:
689 :func:`sys.getfilesystemencodeerrors`.
Victor Stinnere662c392020-11-01 23:07:23 +0100690
691 On Windows: use ``"surrogatepass"`` by default, or ``"replace"`` if
692 :c:member:`~PyPreConfig.legacy_windows_fs_encoding` of
693 :c:type:`PyPreConfig` is non-zero.
694
695 On other platforms: use ``"surrogateescape"`` by default.
696
697 Supported error handlers:
698
699 * ``"strict"``
700 * ``"surrogateescape"``
701 * ``"surrogatepass"`` (only supported with the UTF-8 encoding)
702
703 See also the :c:member:`~PyConfig.filesystem_encoding` member.
Victor Stinner331a6a52019-05-27 16:39:22 +0200704
705 .. c:member:: unsigned long hash_seed
706 .. c:member:: int use_hash_seed
707
708 Randomized hash function seed.
709
710 If :c:member:`~PyConfig.use_hash_seed` is zero, a seed is chosen randomly
Victor Stinner4b9aad42020-11-02 16:49:54 +0100711 at Python startup, and :c:member:`~PyConfig.hash_seed` is ignored.
712
713 Set by the :envvar:`PYTHONHASHSEED` environment variable.
714
715 Default *use_hash_seed* value: ``-1`` in Python mode, ``0`` in isolated
716 mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200717
718 .. c:member:: wchar_t* home
719
720 Python home directory.
721
Victor Stinner4b9aad42020-11-02 16:49:54 +0100722 If :c:func:`Py_SetPythonHome` has been called, use its argument if it is
723 not ``NULL``.
724
725 Set by the :envvar:`PYTHONHOME` environment variable.
726
727 Default: ``NULL``.
728
729 Part of the :ref:`Path Configuration <init-path-config>` input.
Victor Stinner88feaec2019-09-26 03:15:07 +0200730
Victor Stinner331a6a52019-05-27 16:39:22 +0200731 .. c:member:: int import_time
732
733 If non-zero, profile import time.
734
Victor Stinner4b9aad42020-11-02 16:49:54 +0100735 Set the ``1`` by the :option:`-X importtime <-X>` option and the
736 :envvar:`PYTHONPROFILEIMPORTTIME` environment variable.
737
738 Default: ``0``.
739
Victor Stinner331a6a52019-05-27 16:39:22 +0200740 .. c:member:: int inspect
741
742 Enter interactive mode after executing a script or a command.
743
Victor Stinner4b9aad42020-11-02 16:49:54 +0100744 If greater than 0, enable inspect: when a script is passed as first
745 argument or the -c option is used, enter interactive mode after executing
746 the script or the command, even when :data:`sys.stdin` does not appear to
747 be a terminal.
748
749 Incremented by the :option:`-i` command line option. Set to ``1`` if the
750 :envvar:`PYTHONINSPECT` environment variable is non-empty.
751
752 Default: ``0``.
753
Victor Stinner331a6a52019-05-27 16:39:22 +0200754 .. c:member:: int install_signal_handlers
755
Victor Stinner4b9aad42020-11-02 16:49:54 +0100756 Install Python signal handlers?
757
758 Default: ``1`` in Python mode, ``0`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200759
760 .. c:member:: int interactive
761
Victor Stinner4b9aad42020-11-02 16:49:54 +0100762 If greater than 0, enable the interactive mode (REPL).
763
764 Incremented by the :option:`-i` command line option.
765
766 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200767
768 .. c:member:: int isolated
769
770 If greater than 0, enable isolated mode:
771
772 * :data:`sys.path` contains neither the script's directory (computed from
773 ``argv[0]`` or the current directory) nor the user's site-packages
774 directory.
775 * Python REPL doesn't import :mod:`readline` nor enable default readline
776 configuration on interactive prompts.
777 * Set :c:member:`~PyConfig.use_environment` and
778 :c:member:`~PyConfig.user_site_directory` to 0.
779
Victor Stinner4b9aad42020-11-02 16:49:54 +0100780 Default: ``0`` in Python mode, ``1`` in isolated mode.
781
782 See also :c:member:`PyPreConfig.isolated`.
783
Victor Stinner331a6a52019-05-27 16:39:22 +0200784 .. c:member:: int legacy_windows_stdio
785
786 If non-zero, use :class:`io.FileIO` instead of
787 :class:`io.WindowsConsoleIO` for :data:`sys.stdin`, :data:`sys.stdout`
788 and :data:`sys.stderr`.
789
Victor Stinner4b9aad42020-11-02 16:49:54 +0100790 Set to ``1`` if the :envvar:`PYTHONLEGACYWINDOWSSTDIO` environment
791 variable is set to a non-empty string.
792
Victor Stinner331a6a52019-05-27 16:39:22 +0200793 Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
794 Windows specific code.
795
Victor Stinner4b9aad42020-11-02 16:49:54 +0100796 Default: ``0``.
797
798 See also the :pep:`528` (Change Windows console encoding to UTF-8).
799
Victor Stinner331a6a52019-05-27 16:39:22 +0200800 .. c:member:: int malloc_stats
801
802 If non-zero, dump statistics on :ref:`Python pymalloc memory allocator
803 <pymalloc>` at exit.
804
Victor Stinner4b9aad42020-11-02 16:49:54 +0100805 Set to ``1`` by the :envvar:`PYTHONMALLOCSTATS` environment variable.
806
Victor Stinner331a6a52019-05-27 16:39:22 +0200807 The option is ignored if Python is built using ``--without-pymalloc``.
808
Victor Stinner4b9aad42020-11-02 16:49:54 +0100809 Default: ``0``.
810
811 .. c:member:: wchar_t* platlibdir
812
813 Platform library directory name: :data:`sys.platlibdir`.
814
815 Set by the :envvar:`PYTHONPLATLIBDIR` environment variable.
816
817 Default: value of the ``PLATLIBDIR`` macro which is set at configure time
818 by ``--with-platlibdir`` (default: ``"lib"``).
819
820 Part of the :ref:`Path Configuration <init-path-config>` input.
821
822 .. versionadded:: 3.9
823
Victor Stinner331a6a52019-05-27 16:39:22 +0200824 .. c:member:: wchar_t* pythonpath_env
825
Victor Stinner4b9aad42020-11-02 16:49:54 +0100826 Module search paths (:data:`sys.path`) as a string separated by ``DELIM``
Victor Stinner331a6a52019-05-27 16:39:22 +0200827 (:data:`os.path.pathsep`).
828
Victor Stinner4b9aad42020-11-02 16:49:54 +0100829 Set by the :envvar:`PYTHONPATH` environment variable.
830
831 Default: ``NULL``.
832
833 Part of the :ref:`Path Configuration <init-path-config>` input.
Victor Stinner331a6a52019-05-27 16:39:22 +0200834
835 .. c:member:: PyWideStringList module_search_paths
836 .. c:member:: int module_search_paths_set
837
Victor Stinner4b9aad42020-11-02 16:49:54 +0100838 Module search paths: :data:`sys.path`.
839
840 If :c:member:`~PyConfig.module_search_paths_set` is equal to 0, the
841 function calculating the :ref:`Path Configuration <init-path-config>`
842 overrides the :c:member:`~PyConfig.module_search_paths` and sets
843 :c:member:`~PyConfig.module_search_paths_set` to ``1``.
844
845 Default: empty list (``module_search_paths``) and ``0``
846 (``module_search_paths_set``).
847
848 Part of the :ref:`Path Configuration <init-path-config>` output.
Victor Stinner331a6a52019-05-27 16:39:22 +0200849
850 .. c:member:: int optimization_level
851
852 Compilation optimization level:
853
Victor Stinner4b9aad42020-11-02 16:49:54 +0100854 * ``0``: Peephole optimizer, set ``__debug__`` to ``True``.
855 * ``1``: Level 0, remove assertions, set ``__debug__`` to ``False``.
856 * ``2``: Level 1, strip docstrings.
857
858 Incremented by the :option:`-O` command line option. Set to the
859 :envvar:`PYTHONOPTIMIZE` environment variable value.
860
861 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200862
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200863 .. c:member:: PyWideStringList orig_argv
864
865 The list of the original command line arguments passed to the Python
Victor Stinner4b9aad42020-11-02 16:49:54 +0100866 executable: :data:`sys.orig_argv`.
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200867
868 If :c:member:`~PyConfig.orig_argv` list is empty and
869 :c:member:`~PyConfig.argv` is not a list only containing an empty
Victor Stinnerdc42af82020-11-05 18:58:07 +0100870 string, :c:func:`PyConfig_Read` copies :c:member:`~PyConfig.argv` into
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200871 :c:member:`~PyConfig.orig_argv` before modifying
872 :c:member:`~PyConfig.argv` (if :c:member:`~PyConfig.parse_argv` is
873 non-zero).
874
875 See also the :c:member:`~PyConfig.argv` member and the
876 :c:func:`Py_GetArgcArgv` function.
877
Victor Stinner4b9aad42020-11-02 16:49:54 +0100878 Default: empty list.
879
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200880 .. versionadded:: 3.10
881
Victor Stinner331a6a52019-05-27 16:39:22 +0200882 .. c:member:: int parse_argv
883
Victor Stinner4b9aad42020-11-02 16:49:54 +0100884 Parse command line arguments?
885
Victor Stinnerdc42af82020-11-05 18:58:07 +0100886 If equals to ``1``, parse :c:member:`~PyConfig.argv` the same way the regular
Victor Stinner4b9aad42020-11-02 16:49:54 +0100887 Python parses :ref:`command line arguments <using-on-cmdline>`, and strip
888 Python arguments from :c:member:`~PyConfig.argv`.
889
Victor Stinnerdc42af82020-11-05 18:58:07 +0100890 The :c:func:`PyConfig_Read` function only parses
891 :c:member:`PyConfig.argv` arguments once: :c:member:`PyConfig.parse_argv`
892 is set to ``2`` after arguments are parsed. Since Python arguments are
893 strippped from :c:member:`PyConfig.argv`, parsing arguments twice would
894 parse the application options as Python options.
895
Victor Stinner4b9aad42020-11-02 16:49:54 +0100896 Default: ``1`` in Python mode, ``0`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200897
Victor Stinnerdc42af82020-11-05 18:58:07 +0100898 .. versionchanged:: 3.10
899 The :c:member:`PyConfig.argv` arguments are now only parsed if
900 :c:member:`PyConfig.parse_argv` equals to ``1``.
901
Victor Stinner331a6a52019-05-27 16:39:22 +0200902 .. c:member:: int parser_debug
903
Victor Stinner4b9aad42020-11-02 16:49:54 +0100904 Parser debug mode. If greater than 0, turn on parser debugging output (for expert only, depending
Victor Stinner331a6a52019-05-27 16:39:22 +0200905 on compilation options).
906
Victor Stinner4b9aad42020-11-02 16:49:54 +0100907 Incremented by the :option:`-d` command line option. Set to the
908 :envvar:`PYTHONDEBUG` environment variable value.
909
910 Default: ``0``.
911
Victor Stinner331a6a52019-05-27 16:39:22 +0200912 .. c:member:: int pathconfig_warnings
913
Victor Stinner4b9aad42020-11-02 16:49:54 +0100914 On Unix, if non-zero, calculating the :ref:`Path Configuration
915 <init-path-config>` can log warnings into ``stderr``. If equals to 0,
916 suppress these warnings.
917
918 It has no effect on Windows.
919
920 Default: ``1`` in Python mode, ``0`` in isolated mode.
921
922 Part of the :ref:`Path Configuration <init-path-config>` input.
Victor Stinner331a6a52019-05-27 16:39:22 +0200923
924 .. c:member:: wchar_t* prefix
925
Victor Stinner4b9aad42020-11-02 16:49:54 +0100926 The site-specific directory prefix where the platform independent Python
927 files are installed: :data:`sys.prefix`.
928
929 Default: ``NULL``.
930
931 Part of the :ref:`Path Configuration <init-path-config>` output.
Victor Stinner331a6a52019-05-27 16:39:22 +0200932
933 .. c:member:: wchar_t* program_name
934
Victor Stinner4b9aad42020-11-02 16:49:54 +0100935 Program name used to initialize :c:member:`~PyConfig.executable` and in
936 early error messages during Python initialization.
937
938 * If :func:`Py_SetProgramName` has been called, use its argument.
939 * On macOS, use :envvar:`PYTHONEXECUTABLE` environment variable if set.
940 * If the ``WITH_NEXT_FRAMEWORK`` macro is defined, use
941 :envvar:`__PYVENV_LAUNCHER__` environment variable if set.
942 * Use ``argv[0]`` of :c:member:`~PyConfig.argv` if available and
943 non-empty.
944 * Otherwise, use ``L"python"`` on Windows, or ``L"python3"`` on other
945 platforms.
946
947 Default: ``NULL``.
948
949 Part of the :ref:`Path Configuration <init-path-config>` input.
Victor Stinner331a6a52019-05-27 16:39:22 +0200950
951 .. c:member:: wchar_t* pycache_prefix
952
Victor Stinner4b9aad42020-11-02 16:49:54 +0100953 Directory where cached ``.pyc`` files are written:
954 :data:`sys.pycache_prefix`.
955
956 Set by the :option:`-X pycache_prefix=PATH <-X>` command line option and
957 the :envvar:`PYTHONPYCACHEPREFIX` environment variable.
Victor Stinner88feaec2019-09-26 03:15:07 +0200958
Serhiy Storchakae835b312019-10-30 21:37:16 +0200959 If ``NULL``, :data:`sys.pycache_prefix` is set to ``None``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200960
Victor Stinner4b9aad42020-11-02 16:49:54 +0100961 Default: ``NULL``.
962
Victor Stinner331a6a52019-05-27 16:39:22 +0200963 .. c:member:: int quiet
964
Victor Stinner4b9aad42020-11-02 16:49:54 +0100965 Quiet mode. If greater than 0, don't display the copyright and version at
966 Python startup in interactive mode.
967
968 Incremented by the :option:`-q` command line option.
969
970 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200971
972 .. c:member:: wchar_t* run_command
973
Victor Stinner4b9aad42020-11-02 16:49:54 +0100974 Value of the :option:`-c` command line option.
975
976 Used by :c:func:`Py_RunMain`.
977
978 Default: ``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200979
980 .. c:member:: wchar_t* run_filename
981
Victor Stinner4b9aad42020-11-02 16:49:54 +0100982 Filename passed on the command line: trailing command line argument
983 without :option:`-c` or :option:`-m`.
984
985 For example, it is set to ``script.py`` by the ``python3 script.py arg``
986 command.
987
988 Used by :c:func:`Py_RunMain`.
989
990 Default: ``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200991
992 .. c:member:: wchar_t* run_module
993
Victor Stinner4b9aad42020-11-02 16:49:54 +0100994 Value of the :option:`-m` command line option.
995
996 Used by :c:func:`Py_RunMain`.
997
998 Default: ``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200999
Victor Stinner331a6a52019-05-27 16:39:22 +02001000 .. c:member:: int show_ref_count
1001
1002 Show total reference count at exit?
1003
Victor Stinner88feaec2019-09-26 03:15:07 +02001004 Set to 1 by :option:`-X showrefcount <-X>` command line option.
1005
Victor Stinner331a6a52019-05-27 16:39:22 +02001006 Need a debug build of Python (``Py_REF_DEBUG`` macro must be defined).
1007
Victor Stinner4b9aad42020-11-02 16:49:54 +01001008 Default: ``0``.
1009
Victor Stinner331a6a52019-05-27 16:39:22 +02001010 .. c:member:: int site_import
1011
1012 Import the :mod:`site` module at startup?
1013
Victor Stinner4b9aad42020-11-02 16:49:54 +01001014 If equal to zero, disable the import of the module site and the
1015 site-dependent manipulations of :data:`sys.path` that it entails.
1016
1017 Also disable these manipulations if the :mod:`site` module is explicitly
1018 imported later (call :func:`site.main` if you want them to be triggered).
1019
1020 Set to ``0`` by the :option:`-S` command line option.
1021
1022 :data:`sys.flags.no_site` is set to the inverted value of
1023 :c:member:`~PyConfig.site_import`.
1024
1025 Default: ``1``.
1026
Victor Stinner331a6a52019-05-27 16:39:22 +02001027 .. c:member:: int skip_source_first_line
1028
Victor Stinner4b9aad42020-11-02 16:49:54 +01001029 If non-zero, skip the first line of the :c:member:`PyConfig.run_filename`
1030 source.
1031
1032 It allows the usage of non-Unix forms of ``#!cmd``. This is intended for
1033 a DOS specific hack only.
1034
1035 Set to ``1`` by the :option:`-x` command line option.
1036
1037 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +02001038
1039 .. c:member:: wchar_t* stdio_encoding
1040 .. c:member:: wchar_t* stdio_errors
1041
1042 Encoding and encoding errors of :data:`sys.stdin`, :data:`sys.stdout` and
Victor Stinner4b9aad42020-11-02 16:49:54 +01001043 :data:`sys.stderr` (but :data:`sys.stderr` always uses
1044 ``"backslashreplace"`` error handler).
1045
1046 If :c:func:`Py_SetStandardStreamEncoding` has been called, use its
1047 *error* and *errors* arguments if they are not ``NULL``.
1048
1049 Use the :envvar:`PYTHONIOENCODING` environment variable if it is
1050 non-empty.
1051
1052 Default encoding:
1053
1054 * ``"UTF-8"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero.
1055 * Otherwise, use the :term:`locale encoding`.
1056
1057 Default error handler:
1058
1059 * On Windows: use ``"surrogateescape"``.
1060 * ``"surrogateescape"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero,
1061 or if the LC_CTYPE locale is "C" or "POSIX".
1062 * ``"strict"`` otherwise.
Victor Stinner331a6a52019-05-27 16:39:22 +02001063
1064 .. c:member:: int tracemalloc
1065
Victor Stinner4b9aad42020-11-02 16:49:54 +01001066 Enable tracemalloc?
1067
Victor Stinner88feaec2019-09-26 03:15:07 +02001068 If non-zero, call :func:`tracemalloc.start` at startup.
Victor Stinner331a6a52019-05-27 16:39:22 +02001069
Victor Stinner4b9aad42020-11-02 16:49:54 +01001070 Set by :option:`-X tracemalloc=N <-X>` command line option and by the
1071 :envvar:`PYTHONTRACEMALLOC` environment variable.
1072
1073 Default: ``-1`` in Python mode, ``0`` in isolated mode.
1074
Victor Stinner331a6a52019-05-27 16:39:22 +02001075 .. c:member:: int use_environment
1076
Victor Stinner4b9aad42020-11-02 16:49:54 +01001077 Use :ref:`environment variables <using-on-envvars>`?
1078
1079 If equals to zero, ignore the :ref:`environment variables
1080 <using-on-envvars>`.
1081
1082 Default: ``1`` in Python config and ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +02001083
1084 .. c:member:: int user_site_directory
1085
Victor Stinner4b9aad42020-11-02 16:49:54 +01001086 If non-zero, add the user site directory to :data:`sys.path`.
1087
1088 Set to ``0`` by the :option:`-s` and :option:`-I` command line options.
1089
1090 Set to ``0`` by the :envvar:`PYTHONNOUSERSITE` environment variable.
1091
1092 Default: ``1`` in Python mode, ``0`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +02001093
1094 .. c:member:: int verbose
1095
Victor Stinner4b9aad42020-11-02 16:49:54 +01001096 Verbose mode. If greater than 0, print a message each time a module is
1097 imported, showing the place (filename or built-in module) from which
1098 it is loaded.
1099
1100 If greater or equal to 2, print a message for each file that is checked
1101 for when searching for a module. Also provides information on module
1102 cleanup at exit.
1103
1104 Incremented by the :option:`-v` command line option.
1105
1106 Set to the :envvar:`PYTHONVERBOSE` environment variable value.
1107
1108 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +02001109
1110 .. c:member:: PyWideStringList warnoptions
1111
Victor Stinner4b9aad42020-11-02 16:49:54 +01001112 Options of the :mod:`warnings` module to build warnings filters, lowest
1113 to highest priority: :data:`sys.warnoptions`.
Victor Stinnerfb4ae152019-09-30 01:40:17 +02001114
1115 The :mod:`warnings` module adds :data:`sys.warnoptions` in the reverse
1116 order: the last :c:member:`PyConfig.warnoptions` item becomes the first
1117 item of :data:`warnings.filters` which is checked first (highest
1118 priority).
Victor Stinner331a6a52019-05-27 16:39:22 +02001119
Victor Stinner4b9aad42020-11-02 16:49:54 +01001120 Default: empty list.
1121
Victor Stinner331a6a52019-05-27 16:39:22 +02001122 .. c:member:: int write_bytecode
1123
Victor Stinner4b9aad42020-11-02 16:49:54 +01001124 If equal to 0, Python won't try to write ``.pyc`` files on the import of
1125 source modules.
1126
1127 Set to ``0`` by the :option:`-B` command line option and the
1128 :envvar:`PYTHONDONTWRITEBYTECODE` environment variable.
Victor Stinner331a6a52019-05-27 16:39:22 +02001129
Victor Stinner88feaec2019-09-26 03:15:07 +02001130 :data:`sys.dont_write_bytecode` is initialized to the inverted value of
1131 :c:member:`~PyConfig.write_bytecode`.
1132
Victor Stinner4b9aad42020-11-02 16:49:54 +01001133 Default: ``1``.
1134
Victor Stinner331a6a52019-05-27 16:39:22 +02001135 .. c:member:: PyWideStringList xoptions
1136
Victor Stinner4b9aad42020-11-02 16:49:54 +01001137 Values of the :option:`-X` command line options: :data:`sys._xoptions`.
Victor Stinner331a6a52019-05-27 16:39:22 +02001138
Victor Stinner4b9aad42020-11-02 16:49:54 +01001139 Default: empty list.
Victor Stinner331a6a52019-05-27 16:39:22 +02001140
Victor Stinner4b9aad42020-11-02 16:49:54 +01001141If :c:member:`~PyConfig.parse_argv` is non-zero, :c:member:`~PyConfig.argv`
1142arguments are parsed the same way the regular Python parses :ref:`command line
1143arguments <using-on-cmdline>`, and Python arguments are stripped from
1144:c:member:`~PyConfig.argv`.
1145
1146The :c:member:`~PyConfig.xoptions` options are parsed to set other options: see
1147the :option:`-X` command line option.
Victor Stinner331a6a52019-05-27 16:39:22 +02001148
Victor Stinnerc6e5c112020-02-03 15:17:15 +01001149.. versionchanged:: 3.9
1150
1151 The ``show_alloc_count`` field has been removed.
1152
Victor Stinner331a6a52019-05-27 16:39:22 +02001153
1154Initialization with PyConfig
Victor Stinnerdc42af82020-11-05 18:58:07 +01001155============================
Victor Stinner331a6a52019-05-27 16:39:22 +02001156
1157Function to initialize Python:
1158
1159.. c:function:: PyStatus Py_InitializeFromConfig(const PyConfig *config)
1160
1161 Initialize Python from *config* configuration.
1162
1163The caller is responsible to handle exceptions (error or exit) using
1164:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
1165
Victor Stinner4b9aad42020-11-02 16:49:54 +01001166If :c:func:`PyImport_FrozenModules`, :c:func:`PyImport_AppendInittab` or
1167:c:func:`PyImport_ExtendInittab` are used, they must be set or called after
1168Python preinitialization and before the Python initialization.
Victor Stinner331a6a52019-05-27 16:39:22 +02001169
Victor Stinnerdc42af82020-11-05 18:58:07 +01001170The current configuration (``PyConfig`` type) is stored in
1171``PyInterpreterState.config``.
1172
Victor Stinner331a6a52019-05-27 16:39:22 +02001173Example setting the program name::
1174
1175 void init_python(void)
1176 {
1177 PyStatus status;
Victor Stinner331a6a52019-05-27 16:39:22 +02001178
Victor Stinner8462a492019-10-01 12:06:16 +02001179 PyConfig config;
1180 PyConfig_InitPythonConfig(&config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001181
1182 /* Set the program name. Implicitly preinitialize Python. */
1183 status = PyConfig_SetString(&config, &config.program_name,
1184 L"/path/to/my_program");
1185 if (PyStatus_Exception(status)) {
Victor Stinnerdc42af82020-11-05 18:58:07 +01001186 goto exception;
Victor Stinner331a6a52019-05-27 16:39:22 +02001187 }
1188
1189 status = Py_InitializeFromConfig(&config);
1190 if (PyStatus_Exception(status)) {
Victor Stinnerdc42af82020-11-05 18:58:07 +01001191 goto exception;
Victor Stinner331a6a52019-05-27 16:39:22 +02001192 }
1193 PyConfig_Clear(&config);
1194 return;
1195
Victor Stinnerdc42af82020-11-05 18:58:07 +01001196 exception:
Victor Stinner331a6a52019-05-27 16:39:22 +02001197 PyConfig_Clear(&config);
1198 Py_ExitStatusException(status);
1199 }
1200
1201More complete example modifying the default configuration, read the
1202configuration, and then override some parameters::
1203
1204 PyStatus init_python(const char *program_name)
1205 {
1206 PyStatus status;
Victor Stinner331a6a52019-05-27 16:39:22 +02001207
Victor Stinner8462a492019-10-01 12:06:16 +02001208 PyConfig config;
1209 PyConfig_InitPythonConfig(&config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001210
Gurupad Hegde6c7bb382019-12-28 17:16:02 -05001211 /* Set the program name before reading the configuration
Victor Stinner331a6a52019-05-27 16:39:22 +02001212 (decode byte string from the locale encoding).
1213
1214 Implicitly preinitialize Python. */
1215 status = PyConfig_SetBytesString(&config, &config.program_name,
Victor Stinner4b9aad42020-11-02 16:49:54 +01001216 program_name);
Victor Stinner331a6a52019-05-27 16:39:22 +02001217 if (PyStatus_Exception(status)) {
1218 goto done;
1219 }
1220
1221 /* Read all configuration at once */
1222 status = PyConfig_Read(&config);
1223 if (PyStatus_Exception(status)) {
1224 goto done;
1225 }
1226
1227 /* Append our custom search path to sys.path */
1228 status = PyWideStringList_Append(&config.module_search_paths,
Victor Stinner88feaec2019-09-26 03:15:07 +02001229 L"/path/to/more/modules");
Victor Stinner331a6a52019-05-27 16:39:22 +02001230 if (PyStatus_Exception(status)) {
1231 goto done;
1232 }
1233
1234 /* Override executable computed by PyConfig_Read() */
1235 status = PyConfig_SetString(&config, &config.executable,
1236 L"/path/to/my_executable");
1237 if (PyStatus_Exception(status)) {
1238 goto done;
1239 }
1240
1241 status = Py_InitializeFromConfig(&config);
1242
1243 done:
1244 PyConfig_Clear(&config);
1245 return status;
1246 }
1247
1248
1249.. _init-isolated-conf:
1250
1251Isolated Configuration
Victor Stinnerdc42af82020-11-05 18:58:07 +01001252======================
Victor Stinner331a6a52019-05-27 16:39:22 +02001253
1254:c:func:`PyPreConfig_InitIsolatedConfig` and
1255:c:func:`PyConfig_InitIsolatedConfig` functions create a configuration to
1256isolate Python from the system. For example, to embed Python into an
1257application.
1258
1259This configuration ignores global configuration variables, environments
Victor Stinner88feaec2019-09-26 03:15:07 +02001260variables, command line arguments (:c:member:`PyConfig.argv` is not parsed)
1261and user site directory. The C standard streams (ex: ``stdout``) and the
1262LC_CTYPE locale are left unchanged. Signal handlers are not installed.
Victor Stinner331a6a52019-05-27 16:39:22 +02001263
1264Configuration files are still used with this configuration. Set the
1265:ref:`Path Configuration <init-path-config>` ("output fields") to ignore these
1266configuration files and avoid the function computing the default path
1267configuration.
1268
1269
1270.. _init-python-config:
1271
1272Python Configuration
Victor Stinnerdc42af82020-11-05 18:58:07 +01001273====================
Victor Stinner331a6a52019-05-27 16:39:22 +02001274
1275:c:func:`PyPreConfig_InitPythonConfig` and :c:func:`PyConfig_InitPythonConfig`
1276functions create a configuration to build a customized Python which behaves as
1277the regular Python.
1278
1279Environments variables and command line arguments are used to configure
1280Python, whereas global configuration variables are ignored.
1281
Victor Stinner4b9aad42020-11-02 16:49:54 +01001282This function enables C locale coercion (:pep:`538`)
1283and :ref:`Python UTF-8 Mode <utf8-mode>`
Victor Stinner331a6a52019-05-27 16:39:22 +02001284(:pep:`540`) depending on the LC_CTYPE locale, :envvar:`PYTHONUTF8` and
1285:envvar:`PYTHONCOERCECLOCALE` environment variables.
1286
Victor Stinner331a6a52019-05-27 16:39:22 +02001287
1288.. _init-path-config:
1289
1290Path Configuration
Victor Stinnerdc42af82020-11-05 18:58:07 +01001291==================
Victor Stinner331a6a52019-05-27 16:39:22 +02001292
1293:c:type:`PyConfig` contains multiple fields for the path configuration:
1294
Victor Stinner8bf39b62019-09-26 02:22:35 +02001295* Path configuration inputs:
Victor Stinner331a6a52019-05-27 16:39:22 +02001296
1297 * :c:member:`PyConfig.home`
Sandro Mani8f023a22020-06-08 17:28:11 +02001298 * :c:member:`PyConfig.platlibdir`
Victor Stinner331a6a52019-05-27 16:39:22 +02001299 * :c:member:`PyConfig.pathconfig_warnings`
Victor Stinnerfcdb0272019-09-23 14:45:47 +02001300 * :c:member:`PyConfig.program_name`
1301 * :c:member:`PyConfig.pythonpath_env`
Victor Stinner8bf39b62019-09-26 02:22:35 +02001302 * current working directory: to get absolute paths
1303 * ``PATH`` environment variable to get the program full path
1304 (from :c:member:`PyConfig.program_name`)
1305 * ``__PYVENV_LAUNCHER__`` environment variable
1306 * (Windows only) Application paths in the registry under
1307 "Software\Python\PythonCore\X.Y\PythonPath" of HKEY_CURRENT_USER and
1308 HKEY_LOCAL_MACHINE (where X.Y is the Python version).
Victor Stinner331a6a52019-05-27 16:39:22 +02001309
1310* Path configuration output fields:
1311
Victor Stinner8bf39b62019-09-26 02:22:35 +02001312 * :c:member:`PyConfig.base_exec_prefix`
Victor Stinnerfcdb0272019-09-23 14:45:47 +02001313 * :c:member:`PyConfig.base_executable`
Victor Stinner8bf39b62019-09-26 02:22:35 +02001314 * :c:member:`PyConfig.base_prefix`
Victor Stinner331a6a52019-05-27 16:39:22 +02001315 * :c:member:`PyConfig.exec_prefix`
1316 * :c:member:`PyConfig.executable`
Victor Stinner331a6a52019-05-27 16:39:22 +02001317 * :c:member:`PyConfig.module_search_paths_set`,
1318 :c:member:`PyConfig.module_search_paths`
Victor Stinner8bf39b62019-09-26 02:22:35 +02001319 * :c:member:`PyConfig.prefix`
Victor Stinner331a6a52019-05-27 16:39:22 +02001320
Victor Stinner8bf39b62019-09-26 02:22:35 +02001321If at least one "output field" is not set, Python calculates the path
Victor Stinner331a6a52019-05-27 16:39:22 +02001322configuration to fill unset fields. If
1323:c:member:`~PyConfig.module_search_paths_set` is equal to 0,
Min ho Kim39d87b52019-08-31 06:21:19 +10001324:c:member:`~PyConfig.module_search_paths` is overridden and
Victor Stinner331a6a52019-05-27 16:39:22 +02001325:c:member:`~PyConfig.module_search_paths_set` is set to 1.
1326
Victor Stinner8bf39b62019-09-26 02:22:35 +02001327It is possible to completely ignore the function calculating the default
Victor Stinner331a6a52019-05-27 16:39:22 +02001328path configuration by setting explicitly all path configuration output
1329fields listed above. A string is considered as set even if it is non-empty.
1330``module_search_paths`` is considered as set if
1331``module_search_paths_set`` is set to 1. In this case, path
1332configuration input fields are ignored as well.
1333
1334Set :c:member:`~PyConfig.pathconfig_warnings` to 0 to suppress warnings when
Victor Stinner8bf39b62019-09-26 02:22:35 +02001335calculating the path configuration (Unix only, Windows does not log any warning).
Victor Stinner331a6a52019-05-27 16:39:22 +02001336
1337If :c:member:`~PyConfig.base_prefix` or :c:member:`~PyConfig.base_exec_prefix`
1338fields are not set, they inherit their value from :c:member:`~PyConfig.prefix`
1339and :c:member:`~PyConfig.exec_prefix` respectively.
1340
1341:c:func:`Py_RunMain` and :c:func:`Py_Main` modify :data:`sys.path`:
1342
1343* If :c:member:`~PyConfig.run_filename` is set and is a directory which contains a
1344 ``__main__.py`` script, prepend :c:member:`~PyConfig.run_filename` to
1345 :data:`sys.path`.
1346* If :c:member:`~PyConfig.isolated` is zero:
1347
1348 * If :c:member:`~PyConfig.run_module` is set, prepend the current directory
1349 to :data:`sys.path`. Do nothing if the current directory cannot be read.
1350 * If :c:member:`~PyConfig.run_filename` is set, prepend the directory of the
1351 filename to :data:`sys.path`.
1352 * Otherwise, prepend an empty string to :data:`sys.path`.
1353
1354If :c:member:`~PyConfig.site_import` is non-zero, :data:`sys.path` can be
1355modified by the :mod:`site` module. If
1356:c:member:`~PyConfig.user_site_directory` is non-zero and the user's
1357site-package directory exists, the :mod:`site` module appends the user's
1358site-package directory to :data:`sys.path`.
1359
1360The following configuration files are used by the path configuration:
1361
1362* ``pyvenv.cfg``
1363* ``python._pth`` (Windows only)
1364* ``pybuilddir.txt`` (Unix only)
1365
Victor Stinnerfcdb0272019-09-23 14:45:47 +02001366The ``__PYVENV_LAUNCHER__`` environment variable is used to set
1367:c:member:`PyConfig.base_executable`
1368
Victor Stinner331a6a52019-05-27 16:39:22 +02001369
1370Py_RunMain()
Victor Stinnerdc42af82020-11-05 18:58:07 +01001371============
Victor Stinner331a6a52019-05-27 16:39:22 +02001372
1373.. c:function:: int Py_RunMain(void)
1374
1375 Execute the command (:c:member:`PyConfig.run_command`), the script
1376 (:c:member:`PyConfig.run_filename`) or the module
1377 (:c:member:`PyConfig.run_module`) specified on the command line or in the
1378 configuration.
1379
1380 By default and when if :option:`-i` option is used, run the REPL.
1381
1382 Finally, finalizes Python and returns an exit status that can be passed to
1383 the ``exit()`` function.
1384
1385See :ref:`Python Configuration <init-python-config>` for an example of
1386customized Python always running in isolated mode using
1387:c:func:`Py_RunMain`.
1388
1389
Victor Stinnere81f6e62020-06-08 18:12:59 +02001390Py_GetArgcArgv()
Victor Stinnerdc42af82020-11-05 18:58:07 +01001391================
Victor Stinnere81f6e62020-06-08 18:12:59 +02001392
1393.. c:function:: void Py_GetArgcArgv(int *argc, wchar_t ***argv)
1394
1395 Get the original command line arguments, before Python modified them.
1396
Victor Stinnerdd8a93e2020-06-30 00:49:03 +02001397 See also :c:member:`PyConfig.orig_argv` member.
1398
Victor Stinnere81f6e62020-06-08 18:12:59 +02001399
Victor Stinner331a6a52019-05-27 16:39:22 +02001400Multi-Phase Initialization Private Provisional API
Victor Stinnerdc42af82020-11-05 18:58:07 +01001401==================================================
Victor Stinner331a6a52019-05-27 16:39:22 +02001402
1403This section is a private provisional API introducing multi-phase
1404initialization, the core feature of the :pep:`432`:
1405
1406* "Core" initialization phase, "bare minimum Python":
1407
1408 * Builtin types;
1409 * Builtin exceptions;
1410 * Builtin and frozen modules;
1411 * The :mod:`sys` module is only partially initialized
Victor Stinner88feaec2019-09-26 03:15:07 +02001412 (ex: :data:`sys.path` doesn't exist yet).
Victor Stinner331a6a52019-05-27 16:39:22 +02001413
1414* "Main" initialization phase, Python is fully initialized:
1415
1416 * Install and configure :mod:`importlib`;
1417 * Apply the :ref:`Path Configuration <init-path-config>`;
1418 * Install signal handlers;
1419 * Finish :mod:`sys` module initialization (ex: create :data:`sys.stdout`
1420 and :data:`sys.path`);
1421 * Enable optional features like :mod:`faulthandler` and :mod:`tracemalloc`;
1422 * Import the :mod:`site` module;
1423 * etc.
1424
1425Private provisional API:
1426
1427* :c:member:`PyConfig._init_main`: if set to 0,
1428 :c:func:`Py_InitializeFromConfig` stops at the "Core" initialization phase.
Victor Stinner252346a2020-05-01 11:33:44 +02001429* :c:member:`PyConfig._isolated_interpreter`: if non-zero,
1430 disallow threads, subprocesses and fork.
Victor Stinner331a6a52019-05-27 16:39:22 +02001431
1432.. c:function:: PyStatus _Py_InitializeMain(void)
1433
1434 Move to the "Main" initialization phase, finish the Python initialization.
1435
1436No module is imported during the "Core" phase and the ``importlib`` module is
1437not configured: the :ref:`Path Configuration <init-path-config>` is only
1438applied during the "Main" phase. It may allow to customize Python in Python to
1439override or tune the :ref:`Path Configuration <init-path-config>`, maybe
Victor Stinner88feaec2019-09-26 03:15:07 +02001440install a custom :data:`sys.meta_path` importer or an import hook, etc.
Victor Stinner331a6a52019-05-27 16:39:22 +02001441
Victor Stinner88feaec2019-09-26 03:15:07 +02001442It may become possible to calculatin the :ref:`Path Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +02001443<init-path-config>` in Python, after the Core phase and before the Main phase,
1444which is one of the :pep:`432` motivation.
1445
1446The "Core" phase is not properly defined: what should be and what should
1447not be available at this phase is not specified yet. The API is marked
1448as private and provisional: the API can be modified or even be removed
1449anytime until a proper public API is designed.
1450
1451Example running Python code between "Core" and "Main" initialization
1452phases::
1453
1454 void init_python(void)
1455 {
1456 PyStatus status;
Victor Stinner8462a492019-10-01 12:06:16 +02001457
Victor Stinner331a6a52019-05-27 16:39:22 +02001458 PyConfig config;
Victor Stinner8462a492019-10-01 12:06:16 +02001459 PyConfig_InitPythonConfig(&config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001460 config._init_main = 0;
1461
1462 /* ... customize 'config' configuration ... */
1463
1464 status = Py_InitializeFromConfig(&config);
1465 PyConfig_Clear(&config);
1466 if (PyStatus_Exception(status)) {
1467 Py_ExitStatusException(status);
1468 }
1469
1470 /* Use sys.stderr because sys.stdout is only created
1471 by _Py_InitializeMain() */
1472 int res = PyRun_SimpleString(
1473 "import sys; "
1474 "print('Run Python code before _Py_InitializeMain', "
1475 "file=sys.stderr)");
1476 if (res < 0) {
1477 exit(1);
1478 }
1479
1480 /* ... put more configuration code here ... */
1481
1482 status = _Py_InitializeMain();
1483 if (PyStatus_Exception(status)) {
1484 Py_ExitStatusException(status);
1485 }
1486 }