blob: 58e417441d1393871c3382ba40b68ad45c0109f4 [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
11Structures:
12
13* :c:type:`PyConfig`
14* :c:type:`PyPreConfig`
15* :c:type:`PyStatus`
16* :c:type:`PyWideStringList`
17
18Functions:
19
20* :c:func:`PyConfig_Clear`
21* :c:func:`PyConfig_InitIsolatedConfig`
22* :c:func:`PyConfig_InitPythonConfig`
23* :c:func:`PyConfig_Read`
24* :c:func:`PyConfig_SetArgv`
25* :c:func:`PyConfig_SetBytesArgv`
26* :c:func:`PyConfig_SetBytesString`
27* :c:func:`PyConfig_SetString`
Victor Stinner36242fd2019-07-01 19:13:50 +020028* :c:func:`PyConfig_SetWideStringList`
Victor Stinner331a6a52019-05-27 16:39:22 +020029* :c:func:`PyPreConfig_InitIsolatedConfig`
30* :c:func:`PyPreConfig_InitPythonConfig`
31* :c:func:`PyStatus_Error`
32* :c:func:`PyStatus_Exception`
33* :c:func:`PyStatus_Exit`
34* :c:func:`PyStatus_IsError`
35* :c:func:`PyStatus_IsExit`
36* :c:func:`PyStatus_NoMemory`
37* :c:func:`PyStatus_Ok`
38* :c:func:`PyWideStringList_Append`
39* :c:func:`PyWideStringList_Insert`
40* :c:func:`Py_ExitStatusException`
41* :c:func:`Py_InitializeFromConfig`
42* :c:func:`Py_PreInitialize`
43* :c:func:`Py_PreInitializeFromArgs`
44* :c:func:`Py_PreInitializeFromBytesArgs`
45* :c:func:`Py_RunMain`
46
47The preconfiguration (``PyPreConfig`` type) is stored in
48``_PyRuntime.preconfig`` and the configuration (``PyConfig`` type) is stored in
49``PyInterpreterState.config``.
50
Victor Stinner1beb7c32019-08-23 17:59:12 +010051See also :ref:`Initialization, Finalization, and Threads <initialization>`.
52
Victor Stinner331a6a52019-05-27 16:39:22 +020053.. seealso::
54 :pep:`587` "Python Initialization Configuration".
55
56
57PyWideStringList
58----------------
59
60.. c:type:: PyWideStringList
61
62 List of ``wchar_t*`` strings.
63
64 If *length* is non-zero, *items* must be non-NULL and all strings must be
65 non-NULL.
66
67 Methods:
68
69 .. c:function:: PyStatus PyWideStringList_Append(PyWideStringList *list, const wchar_t *item)
70
71 Append *item* to *list*.
72
73 Python must be preinitialized to call this function.
74
75 .. c:function:: PyStatus PyWideStringList_Insert(PyWideStringList *list, Py_ssize_t index, const wchar_t *item)
76
Victor Stinner3842f292019-08-23 16:57:54 +010077 Insert *item* into *list* at *index*.
78
79 If *index* is greater than or equal to *list* length, append *item* to
80 *list*.
81
82 *index* must be greater than or equal to 0.
Victor Stinner331a6a52019-05-27 16:39:22 +020083
84 Python must be preinitialized to call this function.
85
86 Structure fields:
87
88 .. c:member:: Py_ssize_t length
89
90 List length.
91
92 .. c:member:: wchar_t** items
93
94 List items.
95
96PyStatus
97--------
98
99.. c:type:: PyStatus
100
101 Structure to store an initialization function status: success, error
102 or exit.
103
104 For an error, it can store the C function name which created the error.
105
106 Structure fields:
107
108 .. c:member:: int exitcode
109
110 Exit code. Argument passed to ``exit()``.
111
112 .. c:member:: const char *err_msg
113
114 Error message.
115
116 .. c:member:: const char *func
117
118 Name of the function which created an error, can be ``NULL``.
119
120 Functions to create a status:
121
122 .. c:function:: PyStatus PyStatus_Ok(void)
123
124 Success.
125
126 .. c:function:: PyStatus PyStatus_Error(const char *err_msg)
127
128 Initialization error with a message.
129
130 .. c:function:: PyStatus PyStatus_NoMemory(void)
131
132 Memory allocation failure (out of memory).
133
134 .. c:function:: PyStatus PyStatus_Exit(int exitcode)
135
136 Exit Python with the specified exit code.
137
138 Functions to handle a status:
139
140 .. c:function:: int PyStatus_Exception(PyStatus status)
141
142 Is the status an error or an exit? If true, the exception must be
143 handled; by calling :c:func:`Py_ExitStatusException` for example.
144
145 .. c:function:: int PyStatus_IsError(PyStatus status)
146
147 Is the result an error?
148
149 .. c:function:: int PyStatus_IsExit(PyStatus status)
150
151 Is the result an exit?
152
153 .. c:function:: void Py_ExitStatusException(PyStatus status)
154
155 Call ``exit(exitcode)`` if *status* is an exit. Print the error
156 message and exit with a non-zero exit code if *status* is an error. Must
157 only be called if ``PyStatus_Exception(status)`` is non-zero.
158
159.. note::
160 Internally, Python uses macros which set ``PyStatus.func``,
161 whereas functions to create a status set ``func`` to ``NULL``.
162
163Example::
164
165 PyStatus alloc(void **ptr, size_t size)
166 {
167 *ptr = PyMem_RawMalloc(size);
168 if (*ptr == NULL) {
169 return PyStatus_NoMemory();
170 }
171 return PyStatus_Ok();
172 }
173
174 int main(int argc, char **argv)
175 {
176 void *ptr;
177 PyStatus status = alloc(&ptr, 16);
178 if (PyStatus_Exception(status)) {
179 Py_ExitStatusException(status);
180 }
181 PyMem_Free(ptr);
182 return 0;
183 }
184
185
186PyPreConfig
187-----------
188
189.. c:type:: PyPreConfig
190
191 Structure used to preinitialize Python:
192
193 * Set the Python memory allocator
194 * Configure the LC_CTYPE locale
195 * Set the UTF-8 mode
196
Victor Stinner441b10c2019-09-28 04:28:35 +0200197 The :c:member:`struct_size` field must be explicitly initialized to
198 ``sizeof(PyPreConfig)``.
199
Victor Stinner331a6a52019-05-27 16:39:22 +0200200 Function to initialize a preconfiguration:
201
Victor Stinner441b10c2019-09-28 04:28:35 +0200202 .. c:function:: PyStatus PyPreConfig_InitIsolatedConfig(PyPreConfig *preconfig)
Victor Stinner331a6a52019-05-27 16:39:22 +0200203
204 Initialize the preconfiguration with :ref:`Python Configuration
205 <init-python-config>`.
206
Victor Stinner441b10c2019-09-28 04:28:35 +0200207 .. c:function:: PyStatus PyPreConfig_InitPythonConfig(PyPreConfig *preconfig)
Victor Stinner331a6a52019-05-27 16:39:22 +0200208
209 Initialize the preconfiguration with :ref:`Isolated Configuration
210 <init-isolated-conf>`.
211
Victor Stinner441b10c2019-09-28 04:28:35 +0200212 The caller of these functions is responsible to handle exceptions (error or
213 exit) using :c:func:`PyStatus_Exception` and
214 :c:func:`Py_ExitStatusException`.
215
Victor Stinner331a6a52019-05-27 16:39:22 +0200216 Structure fields:
217
218 .. c:member:: int allocator
219
220 Name of the memory allocator:
221
222 * ``PYMEM_ALLOCATOR_NOT_SET`` (``0``): don't change memory allocators
223 (use defaults)
224 * ``PYMEM_ALLOCATOR_DEFAULT`` (``1``): default memory allocators
225 * ``PYMEM_ALLOCATOR_DEBUG`` (``2``): default memory allocators with
226 debug hooks
227 * ``PYMEM_ALLOCATOR_MALLOC`` (``3``): force usage of ``malloc()``
228 * ``PYMEM_ALLOCATOR_MALLOC_DEBUG`` (``4``): force usage of
229 ``malloc()`` with debug hooks
230 * ``PYMEM_ALLOCATOR_PYMALLOC`` (``5``): :ref:`Python pymalloc memory
231 allocator <pymalloc>`
232 * ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG`` (``6``): :ref:`Python pymalloc
233 memory allocator <pymalloc>` with debug hooks
234
235 ``PYMEM_ALLOCATOR_PYMALLOC`` and ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG``
236 are not supported if Python is configured using ``--without-pymalloc``
237
238 See :ref:`Memory Management <memory>`.
239
240 .. c:member:: int configure_locale
241
242 Set the LC_CTYPE locale to the user preferred locale? If equals to 0, set
243 :c:member:`coerce_c_locale` and :c:member:`coerce_c_locale_warn` to 0.
244
245 .. c:member:: int coerce_c_locale
246
247 If equals to 2, coerce the C locale; if equals to 1, read the LC_CTYPE
248 locale to decide if it should be coerced.
249
250 .. c:member:: int coerce_c_locale_warn
Victor Stinner88feaec2019-09-26 03:15:07 +0200251
Victor Stinner331a6a52019-05-27 16:39:22 +0200252 If non-zero, emit a warning if the C locale is coerced.
253
254 .. c:member:: int dev_mode
255
256 See :c:member:`PyConfig.dev_mode`.
257
258 .. c:member:: int isolated
259
260 See :c:member:`PyConfig.isolated`.
261
262 .. c:member:: int legacy_windows_fs_encoding (Windows only)
263
264 If non-zero, disable UTF-8 Mode, set the Python filesystem encoding to
265 ``mbcs``, set the filesystem error handler to ``replace``.
266
267 Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
268 Windows specific code.
269
270 .. c:member:: int parse_argv
271
272 If non-zero, :c:func:`Py_PreInitializeFromArgs` and
273 :c:func:`Py_PreInitializeFromBytesArgs` parse their ``argv`` argument the
274 same way the regular Python parses command line arguments: see
275 :ref:`Command Line Arguments <using-on-cmdline>`.
276
Victor Stinner441b10c2019-09-28 04:28:35 +0200277 .. c:member:: size_t struct_size
278
279 Size of the structure in bytes: must be initialized to
280 ``sizeof(PyPreConfig)``.
281
282 Field used for API and ABI compatibility.
283
Victor Stinner331a6a52019-05-27 16:39:22 +0200284 .. c:member:: int use_environment
285
286 See :c:member:`PyConfig.use_environment`.
287
288 .. c:member:: int utf8_mode
289
290 If non-zero, enable the UTF-8 mode.
291
292Preinitialization with PyPreConfig
293----------------------------------
294
295Functions to preinitialize Python:
296
297.. c:function:: PyStatus Py_PreInitialize(const PyPreConfig *preconfig)
298
299 Preinitialize Python from *preconfig* preconfiguration.
300
301.. c:function:: PyStatus Py_PreInitializeFromBytesArgs(const PyPreConfig *preconfig, int argc, char * const *argv)
302
303 Preinitialize Python from *preconfig* preconfiguration and command line
304 arguments (bytes strings).
305
306.. c:function:: PyStatus Py_PreInitializeFromArgs(const PyPreConfig *preconfig, int argc, wchar_t * const * argv)
307
308 Preinitialize Python from *preconfig* preconfiguration and command line
309 arguments (wide strings).
310
311The caller is responsible to handle exceptions (error or exit) using
312:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
313
314For :ref:`Python Configuration <init-python-config>`
315(:c:func:`PyPreConfig_InitPythonConfig`), if Python is initialized with
316command line arguments, the command line arguments must also be passed to
317preinitialize Python, since they have an effect on the pre-configuration
Victor Stinner88feaec2019-09-26 03:15:07 +0200318like encodings. For example, the :option:`-X utf8 <-X>` command line option
Victor Stinner331a6a52019-05-27 16:39:22 +0200319enables the UTF-8 Mode.
320
321``PyMem_SetAllocator()`` can be called after :c:func:`Py_PreInitialize` and
322before :c:func:`Py_InitializeFromConfig` to install a custom memory allocator.
323It can be called before :c:func:`Py_PreInitialize` if
324:c:member:`PyPreConfig.allocator` is set to ``PYMEM_ALLOCATOR_NOT_SET``.
325
326Python memory allocation functions like :c:func:`PyMem_RawMalloc` must not be
327used before Python preinitialization, whereas calling directly ``malloc()`` and
328``free()`` is always safe. :c:func:`Py_DecodeLocale` must not be called before
329the preinitialization.
330
331Example using the preinitialization to enable the UTF-8 Mode::
332
Victor Stinner441b10c2019-09-28 04:28:35 +0200333 PyStatus status;
Victor Stinner331a6a52019-05-27 16:39:22 +0200334 PyPreConfig preconfig;
Victor Stinner441b10c2019-09-28 04:28:35 +0200335 preconfig.struct_size = sizeof(PyPreConfig);
336
337 status = PyPreConfig_InitPythonConfig(&preconfig);
338 if (PyStatus_Exception(status)) {
339 Py_ExitStatusException(status);
340 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200341
342 preconfig.utf8_mode = 1;
343
Victor Stinner441b10c2019-09-28 04:28:35 +0200344 status = Py_PreInitialize(&preconfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200345 if (PyStatus_Exception(status)) {
346 Py_ExitStatusException(status);
347 }
348
349 /* at this point, Python will speak UTF-8 */
350
351 Py_Initialize();
352 /* ... use Python API here ... */
353 Py_Finalize();
354
355
356PyConfig
357--------
358
359.. c:type:: PyConfig
360
361 Structure containing most parameters to configure Python.
362
Victor Stinner441b10c2019-09-28 04:28:35 +0200363 The :c:member:`struct_size` field must be explicitly initialized to
364 ``sizeof(PyConfig)``.
365
Victor Stinner331a6a52019-05-27 16:39:22 +0200366 Structure methods:
367
368 .. c:function:: PyStatus PyConfig_InitPythonConfig(PyConfig *config)
369
370 Initialize configuration with :ref:`Python Configuration
371 <init-python-config>`.
372
373 .. c:function:: PyStatus PyConfig_InitIsolatedConfig(PyConfig *config)
374
375 Initialize configuration with :ref:`Isolated Configuration
376 <init-isolated-conf>`.
377
378 .. c:function:: PyStatus PyConfig_SetString(PyConfig *config, wchar_t * const *config_str, const wchar_t *str)
379
380 Copy the wide character string *str* into ``*config_str``.
381
382 Preinitialize Python if needed.
383
384 .. c:function:: PyStatus PyConfig_SetBytesString(PyConfig *config, wchar_t * const *config_str, const char *str)
385
386 Decode *str* using ``Py_DecodeLocale()`` and set the result into ``*config_str``.
387
388 Preinitialize Python if needed.
389
390 .. c:function:: PyStatus PyConfig_SetArgv(PyConfig *config, int argc, wchar_t * const *argv)
391
392 Set command line arguments from wide character strings.
393
394 Preinitialize Python if needed.
395
396 .. c:function:: PyStatus PyConfig_SetBytesArgv(PyConfig *config, int argc, char * const *argv)
397
398 Set command line arguments: decode bytes using :c:func:`Py_DecodeLocale`.
399
400 Preinitialize Python if needed.
401
Victor Stinner36242fd2019-07-01 19:13:50 +0200402 .. c:function:: PyStatus PyConfig_SetWideStringList(PyConfig *config, PyWideStringList *list, Py_ssize_t length, wchar_t **items)
403
404 Set the list of wide strings *list* to *length* and *items*.
405
406 Preinitialize Python if needed.
407
Victor Stinner331a6a52019-05-27 16:39:22 +0200408 .. c:function:: PyStatus PyConfig_Read(PyConfig *config)
409
410 Read all Python configuration.
411
412 Fields which are already initialized are left unchanged.
413
414 Preinitialize Python if needed.
415
416 .. c:function:: void PyConfig_Clear(PyConfig *config)
417
418 Release configuration memory.
419
420 Most ``PyConfig`` methods preinitialize Python if needed. In that case, the
421 Python preinitialization configuration in based on the :c:type:`PyConfig`.
422 If configuration fields which are in common with :c:type:`PyPreConfig` are
423 tuned, they must be set before calling a :c:type:`PyConfig` method:
424
425 * :c:member:`~PyConfig.dev_mode`
426 * :c:member:`~PyConfig.isolated`
427 * :c:member:`~PyConfig.parse_argv`
428 * :c:member:`~PyConfig.use_environment`
429
430 Moreover, if :c:func:`PyConfig_SetArgv` or :c:func:`PyConfig_SetBytesArgv`
431 is used, this method must be called first, before other methods, since the
432 preinitialization configuration depends on command line arguments (if
433 :c:member:`parse_argv` is non-zero).
434
435 The caller of these methods is responsible to handle exceptions (error or
436 exit) using ``PyStatus_Exception()`` and ``Py_ExitStatusException()``.
437
438 Structure fields:
439
440 .. c:member:: PyWideStringList argv
441
442 Command line arguments, :data:`sys.argv`. See
443 :c:member:`~PyConfig.parse_argv` to parse :c:member:`~PyConfig.argv` the
444 same way the regular Python parses Python command line arguments. If
445 :c:member:`~PyConfig.argv` is empty, an empty string is added to ensure
446 that :data:`sys.argv` always exists and is never empty.
447
448 .. c:member:: wchar_t* base_exec_prefix
449
450 :data:`sys.base_exec_prefix`.
451
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200452 .. c:member:: wchar_t* base_executable
453
454 :data:`sys._base_executable`: ``__PYVENV_LAUNCHER__`` environment
455 variable value, or copy of :c:member:`PyConfig.executable`.
456
Victor Stinner331a6a52019-05-27 16:39:22 +0200457 .. c:member:: wchar_t* base_prefix
458
459 :data:`sys.base_prefix`.
460
461 .. c:member:: int buffered_stdio
462
463 If equals to 0, enable unbuffered mode, making the stdout and stderr
464 streams unbuffered.
465
466 stdin is always opened in buffered mode.
467
468 .. c:member:: int bytes_warning
469
470 If equals to 1, issue a warning when comparing :class:`bytes` or
471 :class:`bytearray` with :class:`str`, or comparing :class:`bytes` with
472 :class:`int`. If equal or greater to 2, raise a :exc:`BytesWarning`
473 exception.
474
475 .. c:member:: wchar_t* check_hash_pycs_mode
476
477 Control the validation behavior of hash-based ``.pyc`` files (see
478 :pep:`552`): :option:`--check-hash-based-pycs` command line option value.
479
480 Valid values: ``always``, ``never`` and ``default``.
481
482 The default value is: ``default``.
483
484 .. c:member:: int configure_c_stdio
485
486 If non-zero, configure C standard streams (``stdio``, ``stdout``,
487 ``stdout``). For example, set their mode to ``O_BINARY`` on Windows.
488
489 .. c:member:: int dev_mode
490
Victor Stinner88feaec2019-09-26 03:15:07 +0200491 Development mode: see :option:`-X dev <-X>`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200492
493 .. c:member:: int dump_refs
494
495 If non-zero, dump all objects which are still alive at exit.
496
497 Require a debug build of Python (``Py_REF_DEBUG`` macro must be defined).
498
499 .. c:member:: wchar_t* exec_prefix
500
501 :data:`sys.exec_prefix`.
502
503 .. c:member:: wchar_t* executable
504
505 :data:`sys.executable`.
506
507 .. c:member:: int faulthandler
508
Victor Stinner88feaec2019-09-26 03:15:07 +0200509 If non-zero, call :func:`faulthandler.enable` at startup.
Victor Stinner331a6a52019-05-27 16:39:22 +0200510
511 .. c:member:: wchar_t* filesystem_encoding
512
513 Filesystem encoding, :func:`sys.getfilesystemencoding`.
514
515 .. c:member:: wchar_t* filesystem_errors
516
517 Filesystem encoding errors, :func:`sys.getfilesystemencodeerrors`.
518
519 .. c:member:: unsigned long hash_seed
520 .. c:member:: int use_hash_seed
521
522 Randomized hash function seed.
523
524 If :c:member:`~PyConfig.use_hash_seed` is zero, a seed is chosen randomly
525 at Pythonstartup, and :c:member:`~PyConfig.hash_seed` is ignored.
526
527 .. c:member:: wchar_t* home
528
529 Python home directory.
530
Victor Stinner88feaec2019-09-26 03:15:07 +0200531 Initialized from :envvar:`PYTHONHOME` environment variable value by
532 default.
533
Victor Stinner331a6a52019-05-27 16:39:22 +0200534 .. c:member:: int import_time
535
536 If non-zero, profile import time.
537
538 .. c:member:: int inspect
539
540 Enter interactive mode after executing a script or a command.
541
542 .. c:member:: int install_signal_handlers
543
544 Install signal handlers?
545
546 .. c:member:: int interactive
547
548 Interactive mode.
549
550 .. c:member:: int isolated
551
552 If greater than 0, enable isolated mode:
553
554 * :data:`sys.path` contains neither the script's directory (computed from
555 ``argv[0]`` or the current directory) nor the user's site-packages
556 directory.
557 * Python REPL doesn't import :mod:`readline` nor enable default readline
558 configuration on interactive prompts.
559 * Set :c:member:`~PyConfig.use_environment` and
560 :c:member:`~PyConfig.user_site_directory` to 0.
561
562 .. c:member:: int legacy_windows_stdio
563
564 If non-zero, use :class:`io.FileIO` instead of
565 :class:`io.WindowsConsoleIO` for :data:`sys.stdin`, :data:`sys.stdout`
566 and :data:`sys.stderr`.
567
568 Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
569 Windows specific code.
570
571 .. c:member:: int malloc_stats
572
573 If non-zero, dump statistics on :ref:`Python pymalloc memory allocator
574 <pymalloc>` at exit.
575
576 The option is ignored if Python is built using ``--without-pymalloc``.
577
578 .. c:member:: wchar_t* pythonpath_env
579
580 Module search paths as a string separated by ``DELIM``
581 (:data:`os.path.pathsep`).
582
583 Initialized from :envvar:`PYTHONPATH` environment variable value by
584 default.
585
586 .. c:member:: PyWideStringList module_search_paths
587 .. c:member:: int module_search_paths_set
588
589 :data:`sys.path`. If :c:member:`~PyConfig.module_search_paths_set` is
590 equal to 0, the :c:member:`~PyConfig.module_search_paths` is overridden
Victor Stinner88feaec2019-09-26 03:15:07 +0200591 by the function calculating the :ref:`Path Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +0200592 <init-path-config>`.
593
594 .. c:member:: int optimization_level
595
596 Compilation optimization level:
597
598 * 0: Peephole optimizer (and ``__debug__`` is set to ``True``)
599 * 1: Remove assertions, set ``__debug__`` to ``False``
600 * 2: Strip docstrings
601
602 .. c:member:: int parse_argv
603
604 If non-zero, parse :c:member:`~PyConfig.argv` the same way the regular
605 Python command line arguments, and strip Python arguments from
606 :c:member:`~PyConfig.argv`: see :ref:`Command Line Arguments
607 <using-on-cmdline>`.
608
609 .. c:member:: int parser_debug
610
611 If non-zero, turn on parser debugging output (for expert only, depending
612 on compilation options).
613
614 .. c:member:: int pathconfig_warnings
615
Victor Stinner88feaec2019-09-26 03:15:07 +0200616 If equal to 0, suppress warnings when calculating the :ref:`Path
617 Configuration <init-path-config>` (Unix only, Windows does not log any
618 warning). Otherwise, warnings are written into ``stderr``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200619
620 .. c:member:: wchar_t* prefix
621
622 :data:`sys.prefix`.
623
624 .. c:member:: wchar_t* program_name
625
Victor Stinner88feaec2019-09-26 03:15:07 +0200626 Program name. Used to initialize :c:member:`~PyConfig.executable`, and in
627 early error messages.
Victor Stinner331a6a52019-05-27 16:39:22 +0200628
629 .. c:member:: wchar_t* pycache_prefix
630
Victor Stinner88feaec2019-09-26 03:15:07 +0200631 :data:`sys.pycache_prefix`: ``.pyc`` cache prefix.
632
633 If NULL, :data:`sys.pycache_prefix` is set to ``None``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200634
635 .. c:member:: int quiet
636
637 Quiet mode. For example, don't display the copyright and version messages
Victor Stinner88feaec2019-09-26 03:15:07 +0200638 in interactive mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200639
640 .. c:member:: wchar_t* run_command
641
Victor Stinner88feaec2019-09-26 03:15:07 +0200642 ``python3 -c COMMAND`` argument. Used by :c:func:`Py_RunMain`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200643
644 .. c:member:: wchar_t* run_filename
645
Victor Stinner88feaec2019-09-26 03:15:07 +0200646 ``python3 FILENAME`` argument. Used by :c:func:`Py_RunMain`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200647
648 .. c:member:: wchar_t* run_module
649
Victor Stinner88feaec2019-09-26 03:15:07 +0200650 ``python3 -m MODULE`` argument. Used by :c:func:`Py_RunMain`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200651
652 .. c:member:: int show_alloc_count
653
654 Show allocation counts at exit?
655
Victor Stinner88feaec2019-09-26 03:15:07 +0200656 Set to 1 by :option:`-X showalloccount <-X>` command line option.
657
Victor Stinner331a6a52019-05-27 16:39:22 +0200658 Need a special Python build with ``COUNT_ALLOCS`` macro defined.
659
660 .. c:member:: int show_ref_count
661
662 Show total reference count at exit?
663
Victor Stinner88feaec2019-09-26 03:15:07 +0200664 Set to 1 by :option:`-X showrefcount <-X>` command line option.
665
Victor Stinner331a6a52019-05-27 16:39:22 +0200666 Need a debug build of Python (``Py_REF_DEBUG`` macro must be defined).
667
668 .. c:member:: int site_import
669
670 Import the :mod:`site` module at startup?
671
672 .. c:member:: int skip_source_first_line
673
674 Skip the first line of the source?
675
676 .. c:member:: wchar_t* stdio_encoding
677 .. c:member:: wchar_t* stdio_errors
678
679 Encoding and encoding errors of :data:`sys.stdin`, :data:`sys.stdout` and
680 :data:`sys.stderr`.
681
Victor Stinner441b10c2019-09-28 04:28:35 +0200682 .. c:member:: size_t struct_size
683
684 Size of the structure in bytes: must be initialized to
685 ``sizeof(PyConfig)``.
686
687 Field used for API and ABI compatibility.
688
Victor Stinner331a6a52019-05-27 16:39:22 +0200689 .. c:member:: int tracemalloc
690
Victor Stinner88feaec2019-09-26 03:15:07 +0200691 If non-zero, call :func:`tracemalloc.start` at startup.
Victor Stinner331a6a52019-05-27 16:39:22 +0200692
693 .. c:member:: int use_environment
694
695 If greater than 0, use :ref:`environment variables <using-on-envvars>`.
696
697 .. c:member:: int user_site_directory
698
699 If non-zero, add user site directory to :data:`sys.path`.
700
701 .. c:member:: int verbose
702
703 If non-zero, enable verbose mode.
704
705 .. c:member:: PyWideStringList warnoptions
706
707 Options of the :mod:`warnings` module to build warnings filters.
708
709 .. c:member:: int write_bytecode
710
711 If non-zero, write ``.pyc`` files.
712
Victor Stinner88feaec2019-09-26 03:15:07 +0200713 :data:`sys.dont_write_bytecode` is initialized to the inverted value of
714 :c:member:`~PyConfig.write_bytecode`.
715
Victor Stinner331a6a52019-05-27 16:39:22 +0200716 .. c:member:: PyWideStringList xoptions
717
718 :data:`sys._xoptions`.
719
720If ``parse_argv`` is non-zero, ``argv`` arguments are parsed the same
721way the regular Python parses command line arguments, and Python
722arguments are stripped from ``argv``: see :ref:`Command Line Arguments
723<using-on-cmdline>`.
724
725The ``xoptions`` options are parsed to set other options: see :option:`-X`
726option.
727
728
729Initialization with PyConfig
730----------------------------
731
732Function to initialize Python:
733
734.. c:function:: PyStatus Py_InitializeFromConfig(const PyConfig *config)
735
736 Initialize Python from *config* configuration.
737
738The caller is responsible to handle exceptions (error or exit) using
739:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
740
Victor Stinner88feaec2019-09-26 03:15:07 +0200741If ``PyImport_FrozenModules``, ``PyImport_AppendInittab()`` or
742``PyImport_ExtendInittab()`` are used, they must be set or called after Python
Victor Stinner331a6a52019-05-27 16:39:22 +0200743preinitialization and before the Python initialization.
744
745Example setting the program name::
746
747 void init_python(void)
748 {
749 PyStatus status;
750 PyConfig config;
Victor Stinner441b10c2019-09-28 04:28:35 +0200751 config.struct_size = sizeof(PyConfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200752
753 status = PyConfig_InitPythonConfig(&config);
754 if (PyStatus_Exception(status)) {
755 goto fail;
756 }
757
758 /* Set the program name. Implicitly preinitialize Python. */
759 status = PyConfig_SetString(&config, &config.program_name,
760 L"/path/to/my_program");
761 if (PyStatus_Exception(status)) {
762 goto fail;
763 }
764
765 status = Py_InitializeFromConfig(&config);
766 if (PyStatus_Exception(status)) {
767 goto fail;
768 }
769 PyConfig_Clear(&config);
770 return;
771
772 fail:
773 PyConfig_Clear(&config);
774 Py_ExitStatusException(status);
775 }
776
777More complete example modifying the default configuration, read the
778configuration, and then override some parameters::
779
780 PyStatus init_python(const char *program_name)
781 {
782 PyStatus status;
783 PyConfig config;
Victor Stinner441b10c2019-09-28 04:28:35 +0200784 config.struct_size = sizeof(PyConfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200785
786 status = PyConfig_InitPythonConfig(&config);
787 if (PyStatus_Exception(status)) {
788 goto done;
789 }
790
791 /* Set the program name before reading the configuraton
792 (decode byte string from the locale encoding).
793
794 Implicitly preinitialize Python. */
795 status = PyConfig_SetBytesString(&config, &config.program_name,
796 program_name);
797 if (PyStatus_Exception(status)) {
798 goto done;
799 }
800
801 /* Read all configuration at once */
802 status = PyConfig_Read(&config);
803 if (PyStatus_Exception(status)) {
804 goto done;
805 }
806
807 /* Append our custom search path to sys.path */
808 status = PyWideStringList_Append(&config.module_search_paths,
Victor Stinner88feaec2019-09-26 03:15:07 +0200809 L"/path/to/more/modules");
Victor Stinner331a6a52019-05-27 16:39:22 +0200810 if (PyStatus_Exception(status)) {
811 goto done;
812 }
813
814 /* Override executable computed by PyConfig_Read() */
815 status = PyConfig_SetString(&config, &config.executable,
816 L"/path/to/my_executable");
817 if (PyStatus_Exception(status)) {
818 goto done;
819 }
820
821 status = Py_InitializeFromConfig(&config);
822
823 done:
824 PyConfig_Clear(&config);
825 return status;
826 }
827
828
829.. _init-isolated-conf:
830
831Isolated Configuration
832----------------------
833
834:c:func:`PyPreConfig_InitIsolatedConfig` and
835:c:func:`PyConfig_InitIsolatedConfig` functions create a configuration to
836isolate Python from the system. For example, to embed Python into an
837application.
838
839This configuration ignores global configuration variables, environments
Victor Stinner88feaec2019-09-26 03:15:07 +0200840variables, command line arguments (:c:member:`PyConfig.argv` is not parsed)
841and user site directory. The C standard streams (ex: ``stdout``) and the
842LC_CTYPE locale are left unchanged. Signal handlers are not installed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200843
844Configuration files are still used with this configuration. Set the
845:ref:`Path Configuration <init-path-config>` ("output fields") to ignore these
846configuration files and avoid the function computing the default path
847configuration.
848
849
850.. _init-python-config:
851
852Python Configuration
853--------------------
854
855:c:func:`PyPreConfig_InitPythonConfig` and :c:func:`PyConfig_InitPythonConfig`
856functions create a configuration to build a customized Python which behaves as
857the regular Python.
858
859Environments variables and command line arguments are used to configure
860Python, whereas global configuration variables are ignored.
861
862This function enables C locale coercion (:pep:`538`) and UTF-8 Mode
863(:pep:`540`) depending on the LC_CTYPE locale, :envvar:`PYTHONUTF8` and
864:envvar:`PYTHONCOERCECLOCALE` environment variables.
865
866Example of customized Python always running in isolated mode::
867
868 int main(int argc, char **argv)
869 {
Victor Stinner331a6a52019-05-27 16:39:22 +0200870 PyStatus status;
Victor Stinner441b10c2019-09-28 04:28:35 +0200871 PyConfig config;
872 config.struct_size = sizeof(PyConfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200873
874 status = PyConfig_InitPythonConfig(&config);
875 if (PyStatus_Exception(status)) {
876 goto fail;
877 }
878
879 config.isolated = 1;
880
881 /* Decode command line arguments.
882 Implicitly preinitialize Python (in isolated mode). */
883 status = PyConfig_SetBytesArgv(&config, argc, argv);
884 if (PyStatus_Exception(status)) {
885 goto fail;
886 }
887
888 status = Py_InitializeFromConfig(&config);
889 if (PyStatus_Exception(status)) {
890 goto fail;
891 }
892 PyConfig_Clear(&config);
893
894 return Py_RunMain();
895
896 fail:
897 PyConfig_Clear(&config);
898 if (PyStatus_IsExit(status)) {
899 return status.exitcode;
900 }
901 /* Display the error message and exit the process with
902 non-zero exit code */
903 Py_ExitStatusException(status);
904 }
905
906
907.. _init-path-config:
908
909Path Configuration
910------------------
911
912:c:type:`PyConfig` contains multiple fields for the path configuration:
913
Victor Stinner8bf39b62019-09-26 02:22:35 +0200914* Path configuration inputs:
Victor Stinner331a6a52019-05-27 16:39:22 +0200915
916 * :c:member:`PyConfig.home`
Victor Stinner331a6a52019-05-27 16:39:22 +0200917 * :c:member:`PyConfig.pathconfig_warnings`
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200918 * :c:member:`PyConfig.program_name`
919 * :c:member:`PyConfig.pythonpath_env`
Victor Stinner8bf39b62019-09-26 02:22:35 +0200920 * current working directory: to get absolute paths
921 * ``PATH`` environment variable to get the program full path
922 (from :c:member:`PyConfig.program_name`)
923 * ``__PYVENV_LAUNCHER__`` environment variable
924 * (Windows only) Application paths in the registry under
925 "Software\Python\PythonCore\X.Y\PythonPath" of HKEY_CURRENT_USER and
926 HKEY_LOCAL_MACHINE (where X.Y is the Python version).
Victor Stinner331a6a52019-05-27 16:39:22 +0200927
928* Path configuration output fields:
929
Victor Stinner8bf39b62019-09-26 02:22:35 +0200930 * :c:member:`PyConfig.base_exec_prefix`
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200931 * :c:member:`PyConfig.base_executable`
Victor Stinner8bf39b62019-09-26 02:22:35 +0200932 * :c:member:`PyConfig.base_prefix`
Victor Stinner331a6a52019-05-27 16:39:22 +0200933 * :c:member:`PyConfig.exec_prefix`
934 * :c:member:`PyConfig.executable`
Victor Stinner331a6a52019-05-27 16:39:22 +0200935 * :c:member:`PyConfig.module_search_paths_set`,
936 :c:member:`PyConfig.module_search_paths`
Victor Stinner8bf39b62019-09-26 02:22:35 +0200937 * :c:member:`PyConfig.prefix`
Victor Stinner331a6a52019-05-27 16:39:22 +0200938
Victor Stinner8bf39b62019-09-26 02:22:35 +0200939If at least one "output field" is not set, Python calculates the path
Victor Stinner331a6a52019-05-27 16:39:22 +0200940configuration to fill unset fields. If
941:c:member:`~PyConfig.module_search_paths_set` is equal to 0,
Min ho Kim39d87b52019-08-31 06:21:19 +1000942:c:member:`~PyConfig.module_search_paths` is overridden and
Victor Stinner331a6a52019-05-27 16:39:22 +0200943:c:member:`~PyConfig.module_search_paths_set` is set to 1.
944
Victor Stinner8bf39b62019-09-26 02:22:35 +0200945It is possible to completely ignore the function calculating the default
Victor Stinner331a6a52019-05-27 16:39:22 +0200946path configuration by setting explicitly all path configuration output
947fields listed above. A string is considered as set even if it is non-empty.
948``module_search_paths`` is considered as set if
949``module_search_paths_set`` is set to 1. In this case, path
950configuration input fields are ignored as well.
951
952Set :c:member:`~PyConfig.pathconfig_warnings` to 0 to suppress warnings when
Victor Stinner8bf39b62019-09-26 02:22:35 +0200953calculating the path configuration (Unix only, Windows does not log any warning).
Victor Stinner331a6a52019-05-27 16:39:22 +0200954
955If :c:member:`~PyConfig.base_prefix` or :c:member:`~PyConfig.base_exec_prefix`
956fields are not set, they inherit their value from :c:member:`~PyConfig.prefix`
957and :c:member:`~PyConfig.exec_prefix` respectively.
958
959:c:func:`Py_RunMain` and :c:func:`Py_Main` modify :data:`sys.path`:
960
961* If :c:member:`~PyConfig.run_filename` is set and is a directory which contains a
962 ``__main__.py`` script, prepend :c:member:`~PyConfig.run_filename` to
963 :data:`sys.path`.
964* If :c:member:`~PyConfig.isolated` is zero:
965
966 * If :c:member:`~PyConfig.run_module` is set, prepend the current directory
967 to :data:`sys.path`. Do nothing if the current directory cannot be read.
968 * If :c:member:`~PyConfig.run_filename` is set, prepend the directory of the
969 filename to :data:`sys.path`.
970 * Otherwise, prepend an empty string to :data:`sys.path`.
971
972If :c:member:`~PyConfig.site_import` is non-zero, :data:`sys.path` can be
973modified by the :mod:`site` module. If
974:c:member:`~PyConfig.user_site_directory` is non-zero and the user's
975site-package directory exists, the :mod:`site` module appends the user's
976site-package directory to :data:`sys.path`.
977
978The following configuration files are used by the path configuration:
979
980* ``pyvenv.cfg``
981* ``python._pth`` (Windows only)
982* ``pybuilddir.txt`` (Unix only)
983
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200984The ``__PYVENV_LAUNCHER__`` environment variable is used to set
985:c:member:`PyConfig.base_executable`
986
Victor Stinner331a6a52019-05-27 16:39:22 +0200987
988Py_RunMain()
989------------
990
991.. c:function:: int Py_RunMain(void)
992
993 Execute the command (:c:member:`PyConfig.run_command`), the script
994 (:c:member:`PyConfig.run_filename`) or the module
995 (:c:member:`PyConfig.run_module`) specified on the command line or in the
996 configuration.
997
998 By default and when if :option:`-i` option is used, run the REPL.
999
1000 Finally, finalizes Python and returns an exit status that can be passed to
1001 the ``exit()`` function.
1002
1003See :ref:`Python Configuration <init-python-config>` for an example of
1004customized Python always running in isolated mode using
1005:c:func:`Py_RunMain`.
1006
1007
1008Multi-Phase Initialization Private Provisional API
1009--------------------------------------------------
1010
1011This section is a private provisional API introducing multi-phase
1012initialization, the core feature of the :pep:`432`:
1013
1014* "Core" initialization phase, "bare minimum Python":
1015
1016 * Builtin types;
1017 * Builtin exceptions;
1018 * Builtin and frozen modules;
1019 * The :mod:`sys` module is only partially initialized
Victor Stinner88feaec2019-09-26 03:15:07 +02001020 (ex: :data:`sys.path` doesn't exist yet).
Victor Stinner331a6a52019-05-27 16:39:22 +02001021
1022* "Main" initialization phase, Python is fully initialized:
1023
1024 * Install and configure :mod:`importlib`;
1025 * Apply the :ref:`Path Configuration <init-path-config>`;
1026 * Install signal handlers;
1027 * Finish :mod:`sys` module initialization (ex: create :data:`sys.stdout`
1028 and :data:`sys.path`);
1029 * Enable optional features like :mod:`faulthandler` and :mod:`tracemalloc`;
1030 * Import the :mod:`site` module;
1031 * etc.
1032
1033Private provisional API:
1034
1035* :c:member:`PyConfig._init_main`: if set to 0,
1036 :c:func:`Py_InitializeFromConfig` stops at the "Core" initialization phase.
1037
1038.. c:function:: PyStatus _Py_InitializeMain(void)
1039
1040 Move to the "Main" initialization phase, finish the Python initialization.
1041
1042No module is imported during the "Core" phase and the ``importlib`` module is
1043not configured: the :ref:`Path Configuration <init-path-config>` is only
1044applied during the "Main" phase. It may allow to customize Python in Python to
1045override or tune the :ref:`Path Configuration <init-path-config>`, maybe
Victor Stinner88feaec2019-09-26 03:15:07 +02001046install a custom :data:`sys.meta_path` importer or an import hook, etc.
Victor Stinner331a6a52019-05-27 16:39:22 +02001047
Victor Stinner88feaec2019-09-26 03:15:07 +02001048It may become possible to calculatin the :ref:`Path Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +02001049<init-path-config>` in Python, after the Core phase and before the Main phase,
1050which is one of the :pep:`432` motivation.
1051
1052The "Core" phase is not properly defined: what should be and what should
1053not be available at this phase is not specified yet. The API is marked
1054as private and provisional: the API can be modified or even be removed
1055anytime until a proper public API is designed.
1056
1057Example running Python code between "Core" and "Main" initialization
1058phases::
1059
1060 void init_python(void)
1061 {
1062 PyStatus status;
1063 PyConfig config;
Victor Stinner441b10c2019-09-28 04:28:35 +02001064 config.struct_size = sizeof(PyConfig);
Victor Stinner331a6a52019-05-27 16:39:22 +02001065
1066 status = PyConfig_InitPythonConfig(&config);
1067 if (PyStatus_Exception(status)) {
1068 PyConfig_Clear(&config);
1069 Py_ExitStatusException(status);
1070 }
1071
1072 config._init_main = 0;
1073
1074 /* ... customize 'config' configuration ... */
1075
1076 status = Py_InitializeFromConfig(&config);
1077 PyConfig_Clear(&config);
1078 if (PyStatus_Exception(status)) {
1079 Py_ExitStatusException(status);
1080 }
1081
1082 /* Use sys.stderr because sys.stdout is only created
1083 by _Py_InitializeMain() */
1084 int res = PyRun_SimpleString(
1085 "import sys; "
1086 "print('Run Python code before _Py_InitializeMain', "
1087 "file=sys.stderr)");
1088 if (res < 0) {
1089 exit(1);
1090 }
1091
1092 /* ... put more configuration code here ... */
1093
1094 status = _Py_InitializeMain();
1095 if (PyStatus_Exception(status)) {
1096 Py_ExitStatusException(status);
1097 }
1098 }