Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1 | .. highlight:: c |
| 2 | |
| 3 | .. _init-config: |
| 4 | |
| 5 | *********************************** |
| 6 | Python Initialization Configuration |
| 7 | *********************************** |
| 8 | |
| 9 | .. versionadded:: 3.8 |
| 10 | |
| 11 | Structures: |
| 12 | |
| 13 | * :c:type:`PyConfig` |
| 14 | * :c:type:`PyPreConfig` |
| 15 | * :c:type:`PyStatus` |
| 16 | * :c:type:`PyWideStringList` |
| 17 | |
| 18 | Functions: |
| 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 Stinner | 36242fd | 2019-07-01 19:13:50 +0200 | [diff] [blame] | 28 | * :c:func:`PyConfig_SetWideStringList` |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 29 | * :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` |
Victor Stinner | e81f6e6 | 2020-06-08 18:12:59 +0200 | [diff] [blame] | 46 | * :c:func:`Py_GetArgcArgv` |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 47 | |
| 48 | The preconfiguration (``PyPreConfig`` type) is stored in |
| 49 | ``_PyRuntime.preconfig`` and the configuration (``PyConfig`` type) is stored in |
| 50 | ``PyInterpreterState.config``. |
| 51 | |
Victor Stinner | 1beb7c3 | 2019-08-23 17:59:12 +0100 | [diff] [blame] | 52 | See also :ref:`Initialization, Finalization, and Threads <initialization>`. |
| 53 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 54 | .. seealso:: |
| 55 | :pep:`587` "Python Initialization Configuration". |
| 56 | |
| 57 | |
| 58 | PyWideStringList |
| 59 | ---------------- |
| 60 | |
| 61 | .. c:type:: PyWideStringList |
| 62 | |
| 63 | List of ``wchar_t*`` strings. |
| 64 | |
Serhiy Storchaka | e835b31 | 2019-10-30 21:37:16 +0200 | [diff] [blame] | 65 | If *length* is non-zero, *items* must be non-``NULL`` and all strings must be |
| 66 | non-``NULL``. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 67 | |
| 68 | Methods: |
| 69 | |
| 70 | .. c:function:: PyStatus PyWideStringList_Append(PyWideStringList *list, const wchar_t *item) |
| 71 | |
| 72 | Append *item* to *list*. |
| 73 | |
| 74 | Python must be preinitialized to call this function. |
| 75 | |
| 76 | .. c:function:: PyStatus PyWideStringList_Insert(PyWideStringList *list, Py_ssize_t index, const wchar_t *item) |
| 77 | |
Victor Stinner | 3842f29 | 2019-08-23 16:57:54 +0100 | [diff] [blame] | 78 | Insert *item* into *list* at *index*. |
| 79 | |
| 80 | If *index* is greater than or equal to *list* length, append *item* to |
| 81 | *list*. |
| 82 | |
| 83 | *index* must be greater than or equal to 0. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 84 | |
| 85 | Python must be preinitialized to call this function. |
| 86 | |
| 87 | Structure fields: |
| 88 | |
| 89 | .. c:member:: Py_ssize_t length |
| 90 | |
| 91 | List length. |
| 92 | |
| 93 | .. c:member:: wchar_t** items |
| 94 | |
| 95 | List items. |
| 96 | |
| 97 | PyStatus |
| 98 | -------- |
| 99 | |
| 100 | .. c:type:: PyStatus |
| 101 | |
| 102 | Structure to store an initialization function status: success, error |
| 103 | or exit. |
| 104 | |
| 105 | For an error, it can store the C function name which created the error. |
| 106 | |
| 107 | Structure fields: |
| 108 | |
| 109 | .. c:member:: int exitcode |
| 110 | |
| 111 | Exit code. Argument passed to ``exit()``. |
| 112 | |
| 113 | .. c:member:: const char *err_msg |
| 114 | |
| 115 | Error message. |
| 116 | |
| 117 | .. c:member:: const char *func |
| 118 | |
| 119 | Name of the function which created an error, can be ``NULL``. |
| 120 | |
| 121 | Functions to create a status: |
| 122 | |
| 123 | .. c:function:: PyStatus PyStatus_Ok(void) |
| 124 | |
| 125 | Success. |
| 126 | |
| 127 | .. c:function:: PyStatus PyStatus_Error(const char *err_msg) |
| 128 | |
| 129 | Initialization error with a message. |
| 130 | |
| 131 | .. c:function:: PyStatus PyStatus_NoMemory(void) |
| 132 | |
| 133 | Memory allocation failure (out of memory). |
| 134 | |
| 135 | .. c:function:: PyStatus PyStatus_Exit(int exitcode) |
| 136 | |
| 137 | Exit Python with the specified exit code. |
| 138 | |
| 139 | Functions to handle a status: |
| 140 | |
| 141 | .. c:function:: int PyStatus_Exception(PyStatus status) |
| 142 | |
| 143 | Is the status an error or an exit? If true, the exception must be |
| 144 | handled; by calling :c:func:`Py_ExitStatusException` for example. |
| 145 | |
| 146 | .. c:function:: int PyStatus_IsError(PyStatus status) |
| 147 | |
| 148 | Is the result an error? |
| 149 | |
| 150 | .. c:function:: int PyStatus_IsExit(PyStatus status) |
| 151 | |
| 152 | Is the result an exit? |
| 153 | |
| 154 | .. c:function:: void Py_ExitStatusException(PyStatus status) |
| 155 | |
| 156 | Call ``exit(exitcode)`` if *status* is an exit. Print the error |
| 157 | message and exit with a non-zero exit code if *status* is an error. Must |
| 158 | only be called if ``PyStatus_Exception(status)`` is non-zero. |
| 159 | |
| 160 | .. note:: |
| 161 | Internally, Python uses macros which set ``PyStatus.func``, |
| 162 | whereas functions to create a status set ``func`` to ``NULL``. |
| 163 | |
| 164 | Example:: |
| 165 | |
| 166 | PyStatus alloc(void **ptr, size_t size) |
| 167 | { |
| 168 | *ptr = PyMem_RawMalloc(size); |
| 169 | if (*ptr == NULL) { |
| 170 | return PyStatus_NoMemory(); |
| 171 | } |
| 172 | return PyStatus_Ok(); |
| 173 | } |
| 174 | |
| 175 | int main(int argc, char **argv) |
| 176 | { |
| 177 | void *ptr; |
| 178 | PyStatus status = alloc(&ptr, 16); |
| 179 | if (PyStatus_Exception(status)) { |
| 180 | Py_ExitStatusException(status); |
| 181 | } |
| 182 | PyMem_Free(ptr); |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | PyPreConfig |
| 188 | ----------- |
| 189 | |
| 190 | .. c:type:: PyPreConfig |
| 191 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 192 | Structure used to preinitialize Python. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 193 | |
| 194 | Function to initialize a preconfiguration: |
| 195 | |
tomerv | 741008a | 2020-07-01 12:32:54 +0300 | [diff] [blame] | 196 | .. c:function:: void PyPreConfig_InitPythonConfig(PyPreConfig *preconfig) |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 197 | |
| 198 | Initialize the preconfiguration with :ref:`Python Configuration |
| 199 | <init-python-config>`. |
| 200 | |
tomerv | 741008a | 2020-07-01 12:32:54 +0300 | [diff] [blame] | 201 | .. c:function:: void PyPreConfig_InitIsolatedConfig(PyPreConfig *preconfig) |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 202 | |
| 203 | Initialize the preconfiguration with :ref:`Isolated Configuration |
| 204 | <init-isolated-conf>`. |
| 205 | |
| 206 | Structure fields: |
| 207 | |
| 208 | .. c:member:: int allocator |
| 209 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 210 | Name of the Python memory allocators: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 211 | |
| 212 | * ``PYMEM_ALLOCATOR_NOT_SET`` (``0``): don't change memory allocators |
| 213 | (use defaults) |
| 214 | * ``PYMEM_ALLOCATOR_DEFAULT`` (``1``): default memory allocators |
| 215 | * ``PYMEM_ALLOCATOR_DEBUG`` (``2``): default memory allocators with |
| 216 | debug hooks |
| 217 | * ``PYMEM_ALLOCATOR_MALLOC`` (``3``): force usage of ``malloc()`` |
| 218 | * ``PYMEM_ALLOCATOR_MALLOC_DEBUG`` (``4``): force usage of |
| 219 | ``malloc()`` with debug hooks |
| 220 | * ``PYMEM_ALLOCATOR_PYMALLOC`` (``5``): :ref:`Python pymalloc memory |
| 221 | allocator <pymalloc>` |
| 222 | * ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG`` (``6``): :ref:`Python pymalloc |
| 223 | memory allocator <pymalloc>` with debug hooks |
| 224 | |
| 225 | ``PYMEM_ALLOCATOR_PYMALLOC`` and ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG`` |
| 226 | are not supported if Python is configured using ``--without-pymalloc`` |
| 227 | |
| 228 | See :ref:`Memory Management <memory>`. |
| 229 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 230 | Default: ``PYMEM_ALLOCATOR_NOT_SET``. |
| 231 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 232 | .. c:member:: int configure_locale |
| 233 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 234 | Set the LC_CTYPE locale to the user preferred locale? |
| 235 | |
| 236 | If equals to 0, set :c:member:`~PyPreConfig.coerce_c_locale` and |
| 237 | :c:member:`~PyPreConfig.coerce_c_locale_warn` members to 0. |
| 238 | |
| 239 | See the :term:`locale encoding`. |
| 240 | |
| 241 | Default: ``1`` in Python config, ``0`` in isolated config. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 242 | |
| 243 | .. c:member:: int coerce_c_locale |
| 244 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 245 | If equals to 2, coerce the C locale. |
| 246 | |
| 247 | If equals to 1, read the LC_CTYPE locale to decide if it should be |
| 248 | coerced. |
| 249 | |
| 250 | See the :term:`locale encoding`. |
| 251 | |
| 252 | Default: ``-1`` in Python config, ``0`` in isolated config. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 253 | |
| 254 | .. c:member:: int coerce_c_locale_warn |
Victor Stinner | 88feaec | 2019-09-26 03:15:07 +0200 | [diff] [blame] | 255 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 256 | If non-zero, emit a warning if the C locale is coerced. |
| 257 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 258 | Default: ``-1`` in Python config, ``0`` in isolated config. |
| 259 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 260 | .. c:member:: int dev_mode |
| 261 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 262 | If non-zero, enables the :ref:`Python Development Mode <devmode>`: |
| 263 | see :c:member:`PyConfig.dev_mode`. |
| 264 | |
| 265 | Default: ``-1`` in Python mode, ``0`` in isolated mode. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 266 | |
| 267 | .. c:member:: int isolated |
| 268 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 269 | Isolated mode: see :c:member:`PyConfig.isolated`. |
| 270 | |
| 271 | Default: ``0`` in Python mode, ``1`` in isolated mode. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 272 | |
Victor Stinner | e662c39 | 2020-11-01 23:07:23 +0100 | [diff] [blame] | 273 | .. c:member:: int legacy_windows_fs_encoding |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 274 | |
Victor Stinner | e662c39 | 2020-11-01 23:07:23 +0100 | [diff] [blame] | 275 | If non-zero: |
| 276 | |
| 277 | * Set :c:member:`PyPreConfig.utf8_mode` to ``0``, |
| 278 | * Set :c:member:`PyConfig.filesystem_encoding` to ``"mbcs"``, |
| 279 | * Set :c:member:`PyConfig.filesystem_errors` to ``"replace"``. |
| 280 | |
| 281 | Initialized the from :envvar:`PYTHONLEGACYWINDOWSFSENCODING` environment |
| 282 | variable value. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 283 | |
| 284 | Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for |
| 285 | Windows specific code. |
| 286 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 287 | Default: ``0``. |
| 288 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 289 | .. c:member:: int parse_argv |
| 290 | |
| 291 | If non-zero, :c:func:`Py_PreInitializeFromArgs` and |
| 292 | :c:func:`Py_PreInitializeFromBytesArgs` parse their ``argv`` argument the |
| 293 | same way the regular Python parses command line arguments: see |
| 294 | :ref:`Command Line Arguments <using-on-cmdline>`. |
| 295 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 296 | Default: ``1`` in Python config, ``0`` in isolated config. |
| 297 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 298 | .. c:member:: int use_environment |
| 299 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 300 | Use :ref:`environment variables <using-on-envvars>`? See |
| 301 | :c:member:`PyConfig.use_environment`. |
| 302 | |
| 303 | Default: ``1`` in Python config and ``0`` in isolated config. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 304 | |
| 305 | .. c:member:: int utf8_mode |
| 306 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 307 | If non-zero, enable the :ref:`Python UTF-8 Mode <utf8-mode>`. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 308 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 309 | Set by the :option:`-X utf8 <-X>` command line option and the |
| 310 | :envvar:`PYTHONUTF8` environment variable. |
| 311 | |
| 312 | Default: ``-1`` in Python config and ``0`` in isolated config. |
| 313 | |
| 314 | |
| 315 | .. _c-preinit: |
| 316 | |
| 317 | Preinitialize Python with PyPreConfig |
| 318 | ------------------------------------- |
| 319 | |
| 320 | The preinitialization of Python: |
| 321 | |
| 322 | * Set the Python memory allocators (:c:member:`PyPreConfig.allocator`) |
| 323 | * Configure the LC_CTYPE locale (:term:`locale encoding`) |
| 324 | * Set the :ref:`Python UTF-8 Mode <utf8-mode>` |
| 325 | (:c:member:`PyPreConfig.utf8_mode`) |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 326 | |
| 327 | Functions to preinitialize Python: |
| 328 | |
| 329 | .. c:function:: PyStatus Py_PreInitialize(const PyPreConfig *preconfig) |
| 330 | |
| 331 | Preinitialize Python from *preconfig* preconfiguration. |
| 332 | |
| 333 | .. c:function:: PyStatus Py_PreInitializeFromBytesArgs(const PyPreConfig *preconfig, int argc, char * const *argv) |
| 334 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 335 | Preinitialize Python from *preconfig* preconfiguration. |
| 336 | |
| 337 | Parse *argv* command line arguments (bytes strings) if |
| 338 | :c:member:`~PyPreConfig.parse_argv` of *preconfig* is non-zero. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 339 | |
| 340 | .. c:function:: PyStatus Py_PreInitializeFromArgs(const PyPreConfig *preconfig, int argc, wchar_t * const * argv) |
| 341 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 342 | Preinitialize Python from *preconfig* preconfiguration. |
| 343 | |
| 344 | Parse *argv* command line arguments (wide strings) if |
| 345 | :c:member:`~PyPreConfig.parse_argv` of *preconfig* is non-zero. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 346 | |
| 347 | The caller is responsible to handle exceptions (error or exit) using |
| 348 | :c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`. |
| 349 | |
| 350 | For :ref:`Python Configuration <init-python-config>` |
| 351 | (:c:func:`PyPreConfig_InitPythonConfig`), if Python is initialized with |
| 352 | command line arguments, the command line arguments must also be passed to |
| 353 | preinitialize Python, since they have an effect on the pre-configuration |
Victor Stinner | 88feaec | 2019-09-26 03:15:07 +0200 | [diff] [blame] | 354 | like encodings. For example, the :option:`-X utf8 <-X>` command line option |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 355 | enables the :ref:`Python UTF-8 Mode <utf8-mode>`. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 356 | |
| 357 | ``PyMem_SetAllocator()`` can be called after :c:func:`Py_PreInitialize` and |
| 358 | before :c:func:`Py_InitializeFromConfig` to install a custom memory allocator. |
| 359 | It can be called before :c:func:`Py_PreInitialize` if |
| 360 | :c:member:`PyPreConfig.allocator` is set to ``PYMEM_ALLOCATOR_NOT_SET``. |
| 361 | |
| 362 | Python memory allocation functions like :c:func:`PyMem_RawMalloc` must not be |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 363 | used before the Python preinitialization, whereas calling directly ``malloc()`` |
| 364 | and ``free()`` is always safe. :c:func:`Py_DecodeLocale` must not be called |
| 365 | before the Python preinitialization. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 366 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 367 | Example using the preinitialization to enable |
| 368 | the :ref:`Python UTF-8 Mode <utf8-mode>`:: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 369 | |
Victor Stinner | 441b10c | 2019-09-28 04:28:35 +0200 | [diff] [blame] | 370 | PyStatus status; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 371 | PyPreConfig preconfig; |
Victor Stinner | 3c30a76 | 2019-10-01 10:56:37 +0200 | [diff] [blame] | 372 | PyPreConfig_InitPythonConfig(&preconfig); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 373 | |
| 374 | preconfig.utf8_mode = 1; |
| 375 | |
Victor Stinner | 441b10c | 2019-09-28 04:28:35 +0200 | [diff] [blame] | 376 | status = Py_PreInitialize(&preconfig); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 377 | if (PyStatus_Exception(status)) { |
| 378 | Py_ExitStatusException(status); |
| 379 | } |
| 380 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 381 | /* at this point, Python speaks UTF-8 */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 382 | |
| 383 | Py_Initialize(); |
| 384 | /* ... use Python API here ... */ |
| 385 | Py_Finalize(); |
| 386 | |
| 387 | |
| 388 | PyConfig |
| 389 | -------- |
| 390 | |
| 391 | .. c:type:: PyConfig |
| 392 | |
| 393 | Structure containing most parameters to configure Python. |
| 394 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 395 | When done, the :c:func:`PyConfig_Clear` function must be used to release the |
| 396 | configuration memory. |
| 397 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 398 | Structure methods: |
| 399 | |
Victor Stinner | 8462a49 | 2019-10-01 12:06:16 +0200 | [diff] [blame] | 400 | .. c:function:: void PyConfig_InitPythonConfig(PyConfig *config) |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 401 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 402 | Initialize configuration with the :ref:`Python Configuration |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 403 | <init-python-config>`. |
| 404 | |
Victor Stinner | 8462a49 | 2019-10-01 12:06:16 +0200 | [diff] [blame] | 405 | .. c:function:: void PyConfig_InitIsolatedConfig(PyConfig *config) |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 406 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 407 | Initialize configuration with the :ref:`Isolated Configuration |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 408 | <init-isolated-conf>`. |
| 409 | |
| 410 | .. c:function:: PyStatus PyConfig_SetString(PyConfig *config, wchar_t * const *config_str, const wchar_t *str) |
| 411 | |
| 412 | Copy the wide character string *str* into ``*config_str``. |
| 413 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 414 | :ref:`Preinitialize Python <c-preinit>` if needed. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 415 | |
| 416 | .. c:function:: PyStatus PyConfig_SetBytesString(PyConfig *config, wchar_t * const *config_str, const char *str) |
| 417 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 418 | Decode *str* using :c:func:`Py_DecodeLocale` and set the result into |
| 419 | ``*config_str``. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 420 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 421 | :ref:`Preinitialize Python <c-preinit>` if needed. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 422 | |
| 423 | .. c:function:: PyStatus PyConfig_SetArgv(PyConfig *config, int argc, wchar_t * const *argv) |
| 424 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 425 | Set command line arguments (:c:member:`~PyConfig.argv` member of |
| 426 | *config*) from the *argv* list of wide character strings. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 427 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 428 | :ref:`Preinitialize Python <c-preinit>` if needed. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 429 | |
| 430 | .. c:function:: PyStatus PyConfig_SetBytesArgv(PyConfig *config, int argc, char * const *argv) |
| 431 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 432 | Set command line arguments (:c:member:`~PyConfig.argv` member of |
| 433 | *config*) from the *argv* list of bytes strings. Decode bytes using |
| 434 | :c:func:`Py_DecodeLocale`. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 435 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 436 | :ref:`Preinitialize Python <c-preinit>` if needed. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 437 | |
Victor Stinner | 36242fd | 2019-07-01 19:13:50 +0200 | [diff] [blame] | 438 | .. c:function:: PyStatus PyConfig_SetWideStringList(PyConfig *config, PyWideStringList *list, Py_ssize_t length, wchar_t **items) |
| 439 | |
| 440 | Set the list of wide strings *list* to *length* and *items*. |
| 441 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 442 | :ref:`Preinitialize Python <c-preinit>` if needed. |
Victor Stinner | 36242fd | 2019-07-01 19:13:50 +0200 | [diff] [blame] | 443 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 444 | .. c:function:: PyStatus PyConfig_Read(PyConfig *config) |
| 445 | |
| 446 | Read all Python configuration. |
| 447 | |
| 448 | Fields which are already initialized are left unchanged. |
| 449 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 450 | :ref:`Preinitialize Python <c-preinit>` if needed. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 451 | |
| 452 | .. c:function:: void PyConfig_Clear(PyConfig *config) |
| 453 | |
| 454 | Release configuration memory. |
| 455 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 456 | Most ``PyConfig`` methods :ref:`preinitialize Python <c-preinit>` if needed. |
| 457 | In that case, the Python preinitialization configuration |
| 458 | (:c:type:`PyPreConfig`) in based on the :c:type:`PyConfig`. If configuration |
| 459 | fields which are in common with :c:type:`PyPreConfig` are tuned, they must |
| 460 | be set before calling a :c:type:`PyConfig` method: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 461 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 462 | * :c:member:`PyConfig.dev_mode` |
| 463 | * :c:member:`PyConfig.isolated` |
| 464 | * :c:member:`PyConfig.parse_argv` |
| 465 | * :c:member:`PyConfig.use_environment` |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 466 | |
| 467 | Moreover, if :c:func:`PyConfig_SetArgv` or :c:func:`PyConfig_SetBytesArgv` |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 468 | is used, this method must be called before other methods, since the |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 469 | preinitialization configuration depends on command line arguments (if |
| 470 | :c:member:`parse_argv` is non-zero). |
| 471 | |
| 472 | The caller of these methods is responsible to handle exceptions (error or |
| 473 | exit) using ``PyStatus_Exception()`` and ``Py_ExitStatusException()``. |
| 474 | |
| 475 | Structure fields: |
| 476 | |
| 477 | .. c:member:: PyWideStringList argv |
| 478 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 479 | Command line arguments: :data:`sys.argv`. |
| 480 | |
| 481 | Set :c:member:`~PyConfig.parse_argv` to ``1`` to parse |
| 482 | :c:member:`~PyConfig.argv` the same way the regular Python parses Python |
| 483 | command line arguments and then to strip Python arguments from |
| 484 | :c:member:`~PyConfig.argv`. |
| 485 | |
| 486 | If :c:member:`~PyConfig.argv` is empty, an empty string is added to |
| 487 | ensure that :data:`sys.argv` always exists and is never empty. |
| 488 | |
| 489 | Default: ``NULL``. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 490 | |
Victor Stinner | dd8a93e | 2020-06-30 00:49:03 +0200 | [diff] [blame] | 491 | See also the :c:member:`~PyConfig.orig_argv` member. |
| 492 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 493 | .. c:member:: wchar_t* base_exec_prefix |
| 494 | |
| 495 | :data:`sys.base_exec_prefix`. |
| 496 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 497 | Default: ``NULL``. |
| 498 | |
| 499 | Part of the :ref:`Path Configuration <init-path-config>` output. |
| 500 | |
Victor Stinner | fcdb027 | 2019-09-23 14:45:47 +0200 | [diff] [blame] | 501 | .. c:member:: wchar_t* base_executable |
| 502 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 503 | Python base executable: :data:`sys._base_executable`. |
| 504 | |
| 505 | Set by the :envvar:`__PYVENV_LAUNCHER__` environment variable. |
| 506 | |
| 507 | Set from :c:member:`PyConfig.executable` if ``NULL``. |
| 508 | |
| 509 | Default: ``NULL``. |
| 510 | |
| 511 | Part of the :ref:`Path Configuration <init-path-config>` output. |
Victor Stinner | fcdb027 | 2019-09-23 14:45:47 +0200 | [diff] [blame] | 512 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 513 | .. c:member:: wchar_t* base_prefix |
| 514 | |
| 515 | :data:`sys.base_prefix`. |
| 516 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 517 | Default: ``NULL``. |
Sandro Mani | 8f023a2 | 2020-06-08 17:28:11 +0200 | [diff] [blame] | 518 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 519 | Part of the :ref:`Path Configuration <init-path-config>` output. |
Sandro Mani | 8f023a2 | 2020-06-08 17:28:11 +0200 | [diff] [blame] | 520 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 521 | .. c:member:: int buffered_stdio |
| 522 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 523 | If equals to 0 and :c:member:`~PyConfig.configure_c_stdio` is non-zero, |
| 524 | disable buffering on the C streams stdout and stderr. |
| 525 | |
| 526 | Set to 0 by the :option:`-u` command line option and the |
| 527 | :envvar:`PYTHONUNBUFFERED` environment variable. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 528 | |
| 529 | stdin is always opened in buffered mode. |
| 530 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 531 | Default: ``1``. |
| 532 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 533 | .. c:member:: int bytes_warning |
| 534 | |
| 535 | If equals to 1, issue a warning when comparing :class:`bytes` or |
| 536 | :class:`bytearray` with :class:`str`, or comparing :class:`bytes` with |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 537 | :class:`int`. |
| 538 | |
| 539 | If equal or greater to 2, raise a :exc:`BytesWarning` exception in these |
| 540 | cases. |
| 541 | |
| 542 | Incremented by the :option:`-b` command line option. |
| 543 | |
| 544 | Default: ``0``. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 545 | |
| 546 | .. c:member:: wchar_t* check_hash_pycs_mode |
| 547 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 548 | Control the validation behavior of hash-based ``.pyc`` files: |
| 549 | value of the :option:`--check-hash-based-pycs` command line option. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 550 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 551 | Valid values: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 552 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 553 | - ``L"always"``: Hash the source file for invalidation regardless of |
| 554 | value of the 'check_source' flag. |
| 555 | - ``L"never"``: Assume that hash-based pycs always are valid. |
| 556 | - ``L"default"``: The 'check_source' flag in hash-based pycs |
| 557 | determines invalidation. |
| 558 | |
| 559 | Default: ``L"default"``. |
| 560 | |
| 561 | See also :pep:`552` "Deterministic pycs". |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 562 | |
| 563 | .. c:member:: int configure_c_stdio |
| 564 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 565 | If non-zero, configure C standard streams: |
| 566 | |
| 567 | * On Windows, set the binary mode (``O_BINARY``) on stdin, stdout and |
| 568 | stderr. |
| 569 | * If :c:member:`~PyConfig.buffered_stdio` equals zero, disable buffering |
| 570 | of stdin, stdout and stderr streams. |
| 571 | * If :c:member:`~PyConfig.interactive` is non-zero, enable stream |
| 572 | buffering on stdin and stdout (only stdout on Windows). |
| 573 | |
| 574 | Default: ``1`` in Python config, ``0`` in isolated config. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 575 | |
| 576 | .. c:member:: int dev_mode |
| 577 | |
Victor Stinner | b9783d2 | 2020-01-24 10:22:18 +0100 | [diff] [blame] | 578 | If non-zero, enable the :ref:`Python Development Mode <devmode>`. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 579 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 580 | Default: ``-1`` in Python mode, ``0`` in isolated mode. |
| 581 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 582 | .. c:member:: int dump_refs |
| 583 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 584 | Dump Python refererences? |
| 585 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 586 | If non-zero, dump all objects which are still alive at exit. |
| 587 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 588 | Set to ``1`` by the :envvar:`PYTHONDUMPREFS` environment variable. |
| 589 | |
| 590 | Need a special build of Python with the ``Py_TRACE_REFS`` macro defined. |
| 591 | |
| 592 | Default: ``0``. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 593 | |
| 594 | .. c:member:: wchar_t* exec_prefix |
| 595 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 596 | The site-specific directory prefix where the platform-dependent Python |
| 597 | files are installed: :data:`sys.exec_prefix`. |
| 598 | |
| 599 | Default: ``NULL``. |
| 600 | |
| 601 | Part of the :ref:`Path Configuration <init-path-config>` output. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 602 | |
| 603 | .. c:member:: wchar_t* executable |
| 604 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 605 | The absolute path of the executable binary for the Python interpreter: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 606 | :data:`sys.executable`. |
| 607 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 608 | Default: ``NULL``. |
| 609 | |
| 610 | Part of the :ref:`Path Configuration <init-path-config>` output. |
| 611 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 612 | .. c:member:: int faulthandler |
| 613 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 614 | Enable faulthandler? |
| 615 | |
Victor Stinner | 88feaec | 2019-09-26 03:15:07 +0200 | [diff] [blame] | 616 | If non-zero, call :func:`faulthandler.enable` at startup. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 617 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 618 | Set to ``1`` by :option:`-X faulthandler <-X>` and the |
| 619 | :envvar:`PYTHONFAULTHANDLER` environment variable. |
| 620 | |
| 621 | Default: ``-1`` in Python mode, ``0`` in isolated mode. |
| 622 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 623 | .. c:member:: wchar_t* filesystem_encoding |
| 624 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 625 | :term:`Filesystem encoding <filesystem encoding and error handler>`: |
| 626 | :func:`sys.getfilesystemencoding`. |
Victor Stinner | e662c39 | 2020-11-01 23:07:23 +0100 | [diff] [blame] | 627 | |
| 628 | On macOS, Android and VxWorks: use ``"utf-8"`` by default. |
| 629 | |
| 630 | On Windows: use ``"utf-8"`` by default, or ``"mbcs"`` if |
| 631 | :c:member:`~PyPreConfig.legacy_windows_fs_encoding` of |
| 632 | :c:type:`PyPreConfig` is non-zero. |
| 633 | |
| 634 | Default encoding on other platforms: |
| 635 | |
| 636 | * ``"utf-8"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero. |
| 637 | * ``"ascii"`` if Python detects that ``nl_langinfo(CODESET)`` announces |
| 638 | the ASCII encoding (or Roman8 encoding on HP-UX), whereas the |
| 639 | ``mbstowcs()`` function decodes from a different encoding (usually |
| 640 | Latin1). |
| 641 | * ``"utf-8"`` if ``nl_langinfo(CODESET)`` returns an empty string. |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 642 | * Otherwise, use the :term:`locale encoding`: |
Victor Stinner | e662c39 | 2020-11-01 23:07:23 +0100 | [diff] [blame] | 643 | ``nl_langinfo(CODESET)`` result. |
| 644 | |
| 645 | At Python statup, the encoding name is normalized to the Python codec |
| 646 | name. For example, ``"ANSI_X3.4-1968"`` is replaced with ``"ascii"``. |
| 647 | |
| 648 | See also the :c:member:`~PyConfig.filesystem_errors` member. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 649 | |
| 650 | .. c:member:: wchar_t* filesystem_errors |
| 651 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 652 | :term:`Filesystem error handler <filesystem encoding and error handler>`: |
| 653 | :func:`sys.getfilesystemencodeerrors`. |
Victor Stinner | e662c39 | 2020-11-01 23:07:23 +0100 | [diff] [blame] | 654 | |
| 655 | On Windows: use ``"surrogatepass"`` by default, or ``"replace"`` if |
| 656 | :c:member:`~PyPreConfig.legacy_windows_fs_encoding` of |
| 657 | :c:type:`PyPreConfig` is non-zero. |
| 658 | |
| 659 | On other platforms: use ``"surrogateescape"`` by default. |
| 660 | |
| 661 | Supported error handlers: |
| 662 | |
| 663 | * ``"strict"`` |
| 664 | * ``"surrogateescape"`` |
| 665 | * ``"surrogatepass"`` (only supported with the UTF-8 encoding) |
| 666 | |
| 667 | See also the :c:member:`~PyConfig.filesystem_encoding` member. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 668 | |
| 669 | .. c:member:: unsigned long hash_seed |
| 670 | .. c:member:: int use_hash_seed |
| 671 | |
| 672 | Randomized hash function seed. |
| 673 | |
| 674 | If :c:member:`~PyConfig.use_hash_seed` is zero, a seed is chosen randomly |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 675 | at Python startup, and :c:member:`~PyConfig.hash_seed` is ignored. |
| 676 | |
| 677 | Set by the :envvar:`PYTHONHASHSEED` environment variable. |
| 678 | |
| 679 | Default *use_hash_seed* value: ``-1`` in Python mode, ``0`` in isolated |
| 680 | mode. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 681 | |
| 682 | .. c:member:: wchar_t* home |
| 683 | |
| 684 | Python home directory. |
| 685 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 686 | If :c:func:`Py_SetPythonHome` has been called, use its argument if it is |
| 687 | not ``NULL``. |
| 688 | |
| 689 | Set by the :envvar:`PYTHONHOME` environment variable. |
| 690 | |
| 691 | Default: ``NULL``. |
| 692 | |
| 693 | Part of the :ref:`Path Configuration <init-path-config>` input. |
Victor Stinner | 88feaec | 2019-09-26 03:15:07 +0200 | [diff] [blame] | 694 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 695 | .. c:member:: int import_time |
| 696 | |
| 697 | If non-zero, profile import time. |
| 698 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 699 | Set the ``1`` by the :option:`-X importtime <-X>` option and the |
| 700 | :envvar:`PYTHONPROFILEIMPORTTIME` environment variable. |
| 701 | |
| 702 | Default: ``0``. |
| 703 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 704 | .. c:member:: int inspect |
| 705 | |
| 706 | Enter interactive mode after executing a script or a command. |
| 707 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 708 | If greater than 0, enable inspect: when a script is passed as first |
| 709 | argument or the -c option is used, enter interactive mode after executing |
| 710 | the script or the command, even when :data:`sys.stdin` does not appear to |
| 711 | be a terminal. |
| 712 | |
| 713 | Incremented by the :option:`-i` command line option. Set to ``1`` if the |
| 714 | :envvar:`PYTHONINSPECT` environment variable is non-empty. |
| 715 | |
| 716 | Default: ``0``. |
| 717 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 718 | .. c:member:: int install_signal_handlers |
| 719 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 720 | Install Python signal handlers? |
| 721 | |
| 722 | Default: ``1`` in Python mode, ``0`` in isolated mode. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 723 | |
| 724 | .. c:member:: int interactive |
| 725 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 726 | If greater than 0, enable the interactive mode (REPL). |
| 727 | |
| 728 | Incremented by the :option:`-i` command line option. |
| 729 | |
| 730 | Default: ``0``. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 731 | |
| 732 | .. c:member:: int isolated |
| 733 | |
| 734 | If greater than 0, enable isolated mode: |
| 735 | |
| 736 | * :data:`sys.path` contains neither the script's directory (computed from |
| 737 | ``argv[0]`` or the current directory) nor the user's site-packages |
| 738 | directory. |
| 739 | * Python REPL doesn't import :mod:`readline` nor enable default readline |
| 740 | configuration on interactive prompts. |
| 741 | * Set :c:member:`~PyConfig.use_environment` and |
| 742 | :c:member:`~PyConfig.user_site_directory` to 0. |
| 743 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 744 | Default: ``0`` in Python mode, ``1`` in isolated mode. |
| 745 | |
| 746 | See also :c:member:`PyPreConfig.isolated`. |
| 747 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 748 | .. c:member:: int legacy_windows_stdio |
| 749 | |
| 750 | If non-zero, use :class:`io.FileIO` instead of |
| 751 | :class:`io.WindowsConsoleIO` for :data:`sys.stdin`, :data:`sys.stdout` |
| 752 | and :data:`sys.stderr`. |
| 753 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 754 | Set to ``1`` if the :envvar:`PYTHONLEGACYWINDOWSSTDIO` environment |
| 755 | variable is set to a non-empty string. |
| 756 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 757 | Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for |
| 758 | Windows specific code. |
| 759 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 760 | Default: ``0``. |
| 761 | |
| 762 | See also the :pep:`528` (Change Windows console encoding to UTF-8). |
| 763 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 764 | .. c:member:: int malloc_stats |
| 765 | |
| 766 | If non-zero, dump statistics on :ref:`Python pymalloc memory allocator |
| 767 | <pymalloc>` at exit. |
| 768 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 769 | Set to ``1`` by the :envvar:`PYTHONMALLOCSTATS` environment variable. |
| 770 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 771 | The option is ignored if Python is built using ``--without-pymalloc``. |
| 772 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 773 | Default: ``0``. |
| 774 | |
| 775 | .. c:member:: wchar_t* platlibdir |
| 776 | |
| 777 | Platform library directory name: :data:`sys.platlibdir`. |
| 778 | |
| 779 | Set by the :envvar:`PYTHONPLATLIBDIR` environment variable. |
| 780 | |
| 781 | Default: value of the ``PLATLIBDIR`` macro which is set at configure time |
| 782 | by ``--with-platlibdir`` (default: ``"lib"``). |
| 783 | |
| 784 | Part of the :ref:`Path Configuration <init-path-config>` input. |
| 785 | |
| 786 | .. versionadded:: 3.9 |
| 787 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 788 | .. c:member:: wchar_t* pythonpath_env |
| 789 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 790 | Module search paths (:data:`sys.path`) as a string separated by ``DELIM`` |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 791 | (:data:`os.path.pathsep`). |
| 792 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 793 | Set by the :envvar:`PYTHONPATH` environment variable. |
| 794 | |
| 795 | Default: ``NULL``. |
| 796 | |
| 797 | Part of the :ref:`Path Configuration <init-path-config>` input. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 798 | |
| 799 | .. c:member:: PyWideStringList module_search_paths |
| 800 | .. c:member:: int module_search_paths_set |
| 801 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 802 | Module search paths: :data:`sys.path`. |
| 803 | |
| 804 | If :c:member:`~PyConfig.module_search_paths_set` is equal to 0, the |
| 805 | function calculating the :ref:`Path Configuration <init-path-config>` |
| 806 | overrides the :c:member:`~PyConfig.module_search_paths` and sets |
| 807 | :c:member:`~PyConfig.module_search_paths_set` to ``1``. |
| 808 | |
| 809 | Default: empty list (``module_search_paths``) and ``0`` |
| 810 | (``module_search_paths_set``). |
| 811 | |
| 812 | Part of the :ref:`Path Configuration <init-path-config>` output. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 813 | |
| 814 | .. c:member:: int optimization_level |
| 815 | |
| 816 | Compilation optimization level: |
| 817 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 818 | * ``0``: Peephole optimizer, set ``__debug__`` to ``True``. |
| 819 | * ``1``: Level 0, remove assertions, set ``__debug__`` to ``False``. |
| 820 | * ``2``: Level 1, strip docstrings. |
| 821 | |
| 822 | Incremented by the :option:`-O` command line option. Set to the |
| 823 | :envvar:`PYTHONOPTIMIZE` environment variable value. |
| 824 | |
| 825 | Default: ``0``. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 826 | |
Victor Stinner | dd8a93e | 2020-06-30 00:49:03 +0200 | [diff] [blame] | 827 | .. c:member:: PyWideStringList orig_argv |
| 828 | |
| 829 | The list of the original command line arguments passed to the Python |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 830 | executable: :data:`sys.orig_argv`. |
Victor Stinner | dd8a93e | 2020-06-30 00:49:03 +0200 | [diff] [blame] | 831 | |
| 832 | If :c:member:`~PyConfig.orig_argv` list is empty and |
| 833 | :c:member:`~PyConfig.argv` is not a list only containing an empty |
| 834 | string, :c:func:`PyConfig_Read()` copies :c:member:`~PyConfig.argv` into |
| 835 | :c:member:`~PyConfig.orig_argv` before modifying |
| 836 | :c:member:`~PyConfig.argv` (if :c:member:`~PyConfig.parse_argv` is |
| 837 | non-zero). |
| 838 | |
| 839 | See also the :c:member:`~PyConfig.argv` member and the |
| 840 | :c:func:`Py_GetArgcArgv` function. |
| 841 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 842 | Default: empty list. |
| 843 | |
Victor Stinner | dd8a93e | 2020-06-30 00:49:03 +0200 | [diff] [blame] | 844 | .. versionadded:: 3.10 |
| 845 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 846 | .. c:member:: int parse_argv |
| 847 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 848 | Parse command line arguments? |
| 849 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 850 | If non-zero, parse :c:member:`~PyConfig.argv` the same way the regular |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 851 | Python parses :ref:`command line arguments <using-on-cmdline>`, and strip |
| 852 | Python arguments from :c:member:`~PyConfig.argv`. |
| 853 | |
| 854 | Default: ``1`` in Python mode, ``0`` in isolated mode. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 855 | |
| 856 | .. c:member:: int parser_debug |
| 857 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 858 | Parser debug mode. If greater than 0, turn on parser debugging output (for expert only, depending |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 859 | on compilation options). |
| 860 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 861 | Incremented by the :option:`-d` command line option. Set to the |
| 862 | :envvar:`PYTHONDEBUG` environment variable value. |
| 863 | |
| 864 | Default: ``0``. |
| 865 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 866 | .. c:member:: int pathconfig_warnings |
| 867 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 868 | On Unix, if non-zero, calculating the :ref:`Path Configuration |
| 869 | <init-path-config>` can log warnings into ``stderr``. If equals to 0, |
| 870 | suppress these warnings. |
| 871 | |
| 872 | It has no effect on Windows. |
| 873 | |
| 874 | Default: ``1`` in Python mode, ``0`` in isolated mode. |
| 875 | |
| 876 | Part of the :ref:`Path Configuration <init-path-config>` input. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 877 | |
| 878 | .. c:member:: wchar_t* prefix |
| 879 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 880 | The site-specific directory prefix where the platform independent Python |
| 881 | files are installed: :data:`sys.prefix`. |
| 882 | |
| 883 | Default: ``NULL``. |
| 884 | |
| 885 | Part of the :ref:`Path Configuration <init-path-config>` output. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 886 | |
| 887 | .. c:member:: wchar_t* program_name |
| 888 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 889 | Program name used to initialize :c:member:`~PyConfig.executable` and in |
| 890 | early error messages during Python initialization. |
| 891 | |
| 892 | * If :func:`Py_SetProgramName` has been called, use its argument. |
| 893 | * On macOS, use :envvar:`PYTHONEXECUTABLE` environment variable if set. |
| 894 | * If the ``WITH_NEXT_FRAMEWORK`` macro is defined, use |
| 895 | :envvar:`__PYVENV_LAUNCHER__` environment variable if set. |
| 896 | * Use ``argv[0]`` of :c:member:`~PyConfig.argv` if available and |
| 897 | non-empty. |
| 898 | * Otherwise, use ``L"python"`` on Windows, or ``L"python3"`` on other |
| 899 | platforms. |
| 900 | |
| 901 | Default: ``NULL``. |
| 902 | |
| 903 | Part of the :ref:`Path Configuration <init-path-config>` input. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 904 | |
| 905 | .. c:member:: wchar_t* pycache_prefix |
| 906 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 907 | Directory where cached ``.pyc`` files are written: |
| 908 | :data:`sys.pycache_prefix`. |
| 909 | |
| 910 | Set by the :option:`-X pycache_prefix=PATH <-X>` command line option and |
| 911 | the :envvar:`PYTHONPYCACHEPREFIX` environment variable. |
Victor Stinner | 88feaec | 2019-09-26 03:15:07 +0200 | [diff] [blame] | 912 | |
Serhiy Storchaka | e835b31 | 2019-10-30 21:37:16 +0200 | [diff] [blame] | 913 | If ``NULL``, :data:`sys.pycache_prefix` is set to ``None``. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 914 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 915 | Default: ``NULL``. |
| 916 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 917 | .. c:member:: int quiet |
| 918 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 919 | Quiet mode. If greater than 0, don't display the copyright and version at |
| 920 | Python startup in interactive mode. |
| 921 | |
| 922 | Incremented by the :option:`-q` command line option. |
| 923 | |
| 924 | Default: ``0``. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 925 | |
| 926 | .. c:member:: wchar_t* run_command |
| 927 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 928 | Value of the :option:`-c` command line option. |
| 929 | |
| 930 | Used by :c:func:`Py_RunMain`. |
| 931 | |
| 932 | Default: ``NULL``. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 933 | |
| 934 | .. c:member:: wchar_t* run_filename |
| 935 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 936 | Filename passed on the command line: trailing command line argument |
| 937 | without :option:`-c` or :option:`-m`. |
| 938 | |
| 939 | For example, it is set to ``script.py`` by the ``python3 script.py arg`` |
| 940 | command. |
| 941 | |
| 942 | Used by :c:func:`Py_RunMain`. |
| 943 | |
| 944 | Default: ``NULL``. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 945 | |
| 946 | .. c:member:: wchar_t* run_module |
| 947 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 948 | Value of the :option:`-m` command line option. |
| 949 | |
| 950 | Used by :c:func:`Py_RunMain`. |
| 951 | |
| 952 | Default: ``NULL``. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 953 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 954 | .. c:member:: int show_ref_count |
| 955 | |
| 956 | Show total reference count at exit? |
| 957 | |
Victor Stinner | 88feaec | 2019-09-26 03:15:07 +0200 | [diff] [blame] | 958 | Set to 1 by :option:`-X showrefcount <-X>` command line option. |
| 959 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 960 | Need a debug build of Python (``Py_REF_DEBUG`` macro must be defined). |
| 961 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 962 | Default: ``0``. |
| 963 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 964 | .. c:member:: int site_import |
| 965 | |
| 966 | Import the :mod:`site` module at startup? |
| 967 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 968 | If equal to zero, disable the import of the module site and the |
| 969 | site-dependent manipulations of :data:`sys.path` that it entails. |
| 970 | |
| 971 | Also disable these manipulations if the :mod:`site` module is explicitly |
| 972 | imported later (call :func:`site.main` if you want them to be triggered). |
| 973 | |
| 974 | Set to ``0`` by the :option:`-S` command line option. |
| 975 | |
| 976 | :data:`sys.flags.no_site` is set to the inverted value of |
| 977 | :c:member:`~PyConfig.site_import`. |
| 978 | |
| 979 | Default: ``1``. |
| 980 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 981 | .. c:member:: int skip_source_first_line |
| 982 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 983 | If non-zero, skip the first line of the :c:member:`PyConfig.run_filename` |
| 984 | source. |
| 985 | |
| 986 | It allows the usage of non-Unix forms of ``#!cmd``. This is intended for |
| 987 | a DOS specific hack only. |
| 988 | |
| 989 | Set to ``1`` by the :option:`-x` command line option. |
| 990 | |
| 991 | Default: ``0``. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 992 | |
| 993 | .. c:member:: wchar_t* stdio_encoding |
| 994 | .. c:member:: wchar_t* stdio_errors |
| 995 | |
| 996 | Encoding and encoding errors of :data:`sys.stdin`, :data:`sys.stdout` and |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 997 | :data:`sys.stderr` (but :data:`sys.stderr` always uses |
| 998 | ``"backslashreplace"`` error handler). |
| 999 | |
| 1000 | If :c:func:`Py_SetStandardStreamEncoding` has been called, use its |
| 1001 | *error* and *errors* arguments if they are not ``NULL``. |
| 1002 | |
| 1003 | Use the :envvar:`PYTHONIOENCODING` environment variable if it is |
| 1004 | non-empty. |
| 1005 | |
| 1006 | Default encoding: |
| 1007 | |
| 1008 | * ``"UTF-8"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero. |
| 1009 | * Otherwise, use the :term:`locale encoding`. |
| 1010 | |
| 1011 | Default error handler: |
| 1012 | |
| 1013 | * On Windows: use ``"surrogateescape"``. |
| 1014 | * ``"surrogateescape"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero, |
| 1015 | or if the LC_CTYPE locale is "C" or "POSIX". |
| 1016 | * ``"strict"`` otherwise. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1017 | |
| 1018 | .. c:member:: int tracemalloc |
| 1019 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 1020 | Enable tracemalloc? |
| 1021 | |
Victor Stinner | 88feaec | 2019-09-26 03:15:07 +0200 | [diff] [blame] | 1022 | If non-zero, call :func:`tracemalloc.start` at startup. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1023 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 1024 | Set by :option:`-X tracemalloc=N <-X>` command line option and by the |
| 1025 | :envvar:`PYTHONTRACEMALLOC` environment variable. |
| 1026 | |
| 1027 | Default: ``-1`` in Python mode, ``0`` in isolated mode. |
| 1028 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1029 | .. c:member:: int use_environment |
| 1030 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 1031 | Use :ref:`environment variables <using-on-envvars>`? |
| 1032 | |
| 1033 | If equals to zero, ignore the :ref:`environment variables |
| 1034 | <using-on-envvars>`. |
| 1035 | |
| 1036 | Default: ``1`` in Python config and ``0`` in isolated config. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1037 | |
| 1038 | .. c:member:: int user_site_directory |
| 1039 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 1040 | If non-zero, add the user site directory to :data:`sys.path`. |
| 1041 | |
| 1042 | Set to ``0`` by the :option:`-s` and :option:`-I` command line options. |
| 1043 | |
| 1044 | Set to ``0`` by the :envvar:`PYTHONNOUSERSITE` environment variable. |
| 1045 | |
| 1046 | Default: ``1`` in Python mode, ``0`` in isolated mode. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1047 | |
| 1048 | .. c:member:: int verbose |
| 1049 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 1050 | Verbose mode. If greater than 0, print a message each time a module is |
| 1051 | imported, showing the place (filename or built-in module) from which |
| 1052 | it is loaded. |
| 1053 | |
| 1054 | If greater or equal to 2, print a message for each file that is checked |
| 1055 | for when searching for a module. Also provides information on module |
| 1056 | cleanup at exit. |
| 1057 | |
| 1058 | Incremented by the :option:`-v` command line option. |
| 1059 | |
| 1060 | Set to the :envvar:`PYTHONVERBOSE` environment variable value. |
| 1061 | |
| 1062 | Default: ``0``. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1063 | |
| 1064 | .. c:member:: PyWideStringList warnoptions |
| 1065 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 1066 | Options of the :mod:`warnings` module to build warnings filters, lowest |
| 1067 | to highest priority: :data:`sys.warnoptions`. |
Victor Stinner | fb4ae15 | 2019-09-30 01:40:17 +0200 | [diff] [blame] | 1068 | |
| 1069 | The :mod:`warnings` module adds :data:`sys.warnoptions` in the reverse |
| 1070 | order: the last :c:member:`PyConfig.warnoptions` item becomes the first |
| 1071 | item of :data:`warnings.filters` which is checked first (highest |
| 1072 | priority). |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1073 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 1074 | Default: empty list. |
| 1075 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1076 | .. c:member:: int write_bytecode |
| 1077 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 1078 | If equal to 0, Python won't try to write ``.pyc`` files on the import of |
| 1079 | source modules. |
| 1080 | |
| 1081 | Set to ``0`` by the :option:`-B` command line option and the |
| 1082 | :envvar:`PYTHONDONTWRITEBYTECODE` environment variable. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1083 | |
Victor Stinner | 88feaec | 2019-09-26 03:15:07 +0200 | [diff] [blame] | 1084 | :data:`sys.dont_write_bytecode` is initialized to the inverted value of |
| 1085 | :c:member:`~PyConfig.write_bytecode`. |
| 1086 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 1087 | Default: ``1``. |
| 1088 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1089 | .. c:member:: PyWideStringList xoptions |
| 1090 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 1091 | Values of the :option:`-X` command line options: :data:`sys._xoptions`. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1092 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 1093 | Default: empty list. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1094 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 1095 | If :c:member:`~PyConfig.parse_argv` is non-zero, :c:member:`~PyConfig.argv` |
| 1096 | arguments are parsed the same way the regular Python parses :ref:`command line |
| 1097 | arguments <using-on-cmdline>`, and Python arguments are stripped from |
| 1098 | :c:member:`~PyConfig.argv`. |
| 1099 | |
| 1100 | The :c:member:`~PyConfig.xoptions` options are parsed to set other options: see |
| 1101 | the :option:`-X` command line option. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1102 | |
Victor Stinner | c6e5c11 | 2020-02-03 15:17:15 +0100 | [diff] [blame] | 1103 | .. versionchanged:: 3.9 |
| 1104 | |
| 1105 | The ``show_alloc_count`` field has been removed. |
| 1106 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1107 | |
| 1108 | Initialization with PyConfig |
| 1109 | ---------------------------- |
| 1110 | |
| 1111 | Function to initialize Python: |
| 1112 | |
| 1113 | .. c:function:: PyStatus Py_InitializeFromConfig(const PyConfig *config) |
| 1114 | |
| 1115 | Initialize Python from *config* configuration. |
| 1116 | |
| 1117 | The caller is responsible to handle exceptions (error or exit) using |
| 1118 | :c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`. |
| 1119 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 1120 | If :c:func:`PyImport_FrozenModules`, :c:func:`PyImport_AppendInittab` or |
| 1121 | :c:func:`PyImport_ExtendInittab` are used, they must be set or called after |
| 1122 | Python preinitialization and before the Python initialization. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1123 | |
| 1124 | Example setting the program name:: |
| 1125 | |
| 1126 | void init_python(void) |
| 1127 | { |
| 1128 | PyStatus status; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1129 | |
Victor Stinner | 8462a49 | 2019-10-01 12:06:16 +0200 | [diff] [blame] | 1130 | PyConfig config; |
| 1131 | PyConfig_InitPythonConfig(&config); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1132 | |
| 1133 | /* Set the program name. Implicitly preinitialize Python. */ |
| 1134 | status = PyConfig_SetString(&config, &config.program_name, |
| 1135 | L"/path/to/my_program"); |
| 1136 | if (PyStatus_Exception(status)) { |
| 1137 | goto fail; |
| 1138 | } |
| 1139 | |
| 1140 | status = Py_InitializeFromConfig(&config); |
| 1141 | if (PyStatus_Exception(status)) { |
| 1142 | goto fail; |
| 1143 | } |
| 1144 | PyConfig_Clear(&config); |
| 1145 | return; |
| 1146 | |
| 1147 | fail: |
| 1148 | PyConfig_Clear(&config); |
| 1149 | Py_ExitStatusException(status); |
| 1150 | } |
| 1151 | |
| 1152 | More complete example modifying the default configuration, read the |
| 1153 | configuration, and then override some parameters:: |
| 1154 | |
| 1155 | PyStatus init_python(const char *program_name) |
| 1156 | { |
| 1157 | PyStatus status; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1158 | |
Victor Stinner | 8462a49 | 2019-10-01 12:06:16 +0200 | [diff] [blame] | 1159 | PyConfig config; |
| 1160 | PyConfig_InitPythonConfig(&config); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1161 | |
Gurupad Hegde | 6c7bb38 | 2019-12-28 17:16:02 -0500 | [diff] [blame] | 1162 | /* Set the program name before reading the configuration |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1163 | (decode byte string from the locale encoding). |
| 1164 | |
| 1165 | Implicitly preinitialize Python. */ |
| 1166 | status = PyConfig_SetBytesString(&config, &config.program_name, |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 1167 | program_name); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1168 | if (PyStatus_Exception(status)) { |
| 1169 | goto done; |
| 1170 | } |
| 1171 | |
| 1172 | /* Read all configuration at once */ |
| 1173 | status = PyConfig_Read(&config); |
| 1174 | if (PyStatus_Exception(status)) { |
| 1175 | goto done; |
| 1176 | } |
| 1177 | |
| 1178 | /* Append our custom search path to sys.path */ |
| 1179 | status = PyWideStringList_Append(&config.module_search_paths, |
Victor Stinner | 88feaec | 2019-09-26 03:15:07 +0200 | [diff] [blame] | 1180 | L"/path/to/more/modules"); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1181 | if (PyStatus_Exception(status)) { |
| 1182 | goto done; |
| 1183 | } |
| 1184 | |
| 1185 | /* Override executable computed by PyConfig_Read() */ |
| 1186 | status = PyConfig_SetString(&config, &config.executable, |
| 1187 | L"/path/to/my_executable"); |
| 1188 | if (PyStatus_Exception(status)) { |
| 1189 | goto done; |
| 1190 | } |
| 1191 | |
| 1192 | status = Py_InitializeFromConfig(&config); |
| 1193 | |
| 1194 | done: |
| 1195 | PyConfig_Clear(&config); |
| 1196 | return status; |
| 1197 | } |
| 1198 | |
| 1199 | |
| 1200 | .. _init-isolated-conf: |
| 1201 | |
| 1202 | Isolated Configuration |
| 1203 | ---------------------- |
| 1204 | |
| 1205 | :c:func:`PyPreConfig_InitIsolatedConfig` and |
| 1206 | :c:func:`PyConfig_InitIsolatedConfig` functions create a configuration to |
| 1207 | isolate Python from the system. For example, to embed Python into an |
| 1208 | application. |
| 1209 | |
| 1210 | This configuration ignores global configuration variables, environments |
Victor Stinner | 88feaec | 2019-09-26 03:15:07 +0200 | [diff] [blame] | 1211 | variables, command line arguments (:c:member:`PyConfig.argv` is not parsed) |
| 1212 | and user site directory. The C standard streams (ex: ``stdout``) and the |
| 1213 | LC_CTYPE locale are left unchanged. Signal handlers are not installed. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1214 | |
| 1215 | Configuration files are still used with this configuration. Set the |
| 1216 | :ref:`Path Configuration <init-path-config>` ("output fields") to ignore these |
| 1217 | configuration files and avoid the function computing the default path |
| 1218 | configuration. |
| 1219 | |
| 1220 | |
| 1221 | .. _init-python-config: |
| 1222 | |
| 1223 | Python Configuration |
| 1224 | -------------------- |
| 1225 | |
| 1226 | :c:func:`PyPreConfig_InitPythonConfig` and :c:func:`PyConfig_InitPythonConfig` |
| 1227 | functions create a configuration to build a customized Python which behaves as |
| 1228 | the regular Python. |
| 1229 | |
| 1230 | Environments variables and command line arguments are used to configure |
| 1231 | Python, whereas global configuration variables are ignored. |
| 1232 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame^] | 1233 | This function enables C locale coercion (:pep:`538`) |
| 1234 | and :ref:`Python UTF-8 Mode <utf8-mode>` |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1235 | (:pep:`540`) depending on the LC_CTYPE locale, :envvar:`PYTHONUTF8` and |
| 1236 | :envvar:`PYTHONCOERCECLOCALE` environment variables. |
| 1237 | |
| 1238 | Example of customized Python always running in isolated mode:: |
| 1239 | |
| 1240 | int main(int argc, char **argv) |
| 1241 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1242 | PyStatus status; |
Victor Stinner | 8462a49 | 2019-10-01 12:06:16 +0200 | [diff] [blame] | 1243 | |
Victor Stinner | 441b10c | 2019-09-28 04:28:35 +0200 | [diff] [blame] | 1244 | PyConfig config; |
Victor Stinner | 8462a49 | 2019-10-01 12:06:16 +0200 | [diff] [blame] | 1245 | PyConfig_InitPythonConfig(&config); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1246 | config.isolated = 1; |
| 1247 | |
| 1248 | /* Decode command line arguments. |
| 1249 | Implicitly preinitialize Python (in isolated mode). */ |
| 1250 | status = PyConfig_SetBytesArgv(&config, argc, argv); |
| 1251 | if (PyStatus_Exception(status)) { |
| 1252 | goto fail; |
| 1253 | } |
| 1254 | |
| 1255 | status = Py_InitializeFromConfig(&config); |
| 1256 | if (PyStatus_Exception(status)) { |
| 1257 | goto fail; |
| 1258 | } |
| 1259 | PyConfig_Clear(&config); |
| 1260 | |
| 1261 | return Py_RunMain(); |
| 1262 | |
| 1263 | fail: |
| 1264 | PyConfig_Clear(&config); |
| 1265 | if (PyStatus_IsExit(status)) { |
| 1266 | return status.exitcode; |
| 1267 | } |
| 1268 | /* Display the error message and exit the process with |
| 1269 | non-zero exit code */ |
| 1270 | Py_ExitStatusException(status); |
| 1271 | } |
| 1272 | |
| 1273 | |
| 1274 | .. _init-path-config: |
| 1275 | |
| 1276 | Path Configuration |
| 1277 | ------------------ |
| 1278 | |
| 1279 | :c:type:`PyConfig` contains multiple fields for the path configuration: |
| 1280 | |
Victor Stinner | 8bf39b6 | 2019-09-26 02:22:35 +0200 | [diff] [blame] | 1281 | * Path configuration inputs: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1282 | |
| 1283 | * :c:member:`PyConfig.home` |
Sandro Mani | 8f023a2 | 2020-06-08 17:28:11 +0200 | [diff] [blame] | 1284 | * :c:member:`PyConfig.platlibdir` |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1285 | * :c:member:`PyConfig.pathconfig_warnings` |
Victor Stinner | fcdb027 | 2019-09-23 14:45:47 +0200 | [diff] [blame] | 1286 | * :c:member:`PyConfig.program_name` |
| 1287 | * :c:member:`PyConfig.pythonpath_env` |
Victor Stinner | 8bf39b6 | 2019-09-26 02:22:35 +0200 | [diff] [blame] | 1288 | * current working directory: to get absolute paths |
| 1289 | * ``PATH`` environment variable to get the program full path |
| 1290 | (from :c:member:`PyConfig.program_name`) |
| 1291 | * ``__PYVENV_LAUNCHER__`` environment variable |
| 1292 | * (Windows only) Application paths in the registry under |
| 1293 | "Software\Python\PythonCore\X.Y\PythonPath" of HKEY_CURRENT_USER and |
| 1294 | HKEY_LOCAL_MACHINE (where X.Y is the Python version). |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1295 | |
| 1296 | * Path configuration output fields: |
| 1297 | |
Victor Stinner | 8bf39b6 | 2019-09-26 02:22:35 +0200 | [diff] [blame] | 1298 | * :c:member:`PyConfig.base_exec_prefix` |
Victor Stinner | fcdb027 | 2019-09-23 14:45:47 +0200 | [diff] [blame] | 1299 | * :c:member:`PyConfig.base_executable` |
Victor Stinner | 8bf39b6 | 2019-09-26 02:22:35 +0200 | [diff] [blame] | 1300 | * :c:member:`PyConfig.base_prefix` |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1301 | * :c:member:`PyConfig.exec_prefix` |
| 1302 | * :c:member:`PyConfig.executable` |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1303 | * :c:member:`PyConfig.module_search_paths_set`, |
| 1304 | :c:member:`PyConfig.module_search_paths` |
Victor Stinner | 8bf39b6 | 2019-09-26 02:22:35 +0200 | [diff] [blame] | 1305 | * :c:member:`PyConfig.prefix` |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1306 | |
Victor Stinner | 8bf39b6 | 2019-09-26 02:22:35 +0200 | [diff] [blame] | 1307 | If at least one "output field" is not set, Python calculates the path |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1308 | configuration to fill unset fields. If |
| 1309 | :c:member:`~PyConfig.module_search_paths_set` is equal to 0, |
Min ho Kim | 39d87b5 | 2019-08-31 06:21:19 +1000 | [diff] [blame] | 1310 | :c:member:`~PyConfig.module_search_paths` is overridden and |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1311 | :c:member:`~PyConfig.module_search_paths_set` is set to 1. |
| 1312 | |
Victor Stinner | 8bf39b6 | 2019-09-26 02:22:35 +0200 | [diff] [blame] | 1313 | It is possible to completely ignore the function calculating the default |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1314 | path configuration by setting explicitly all path configuration output |
| 1315 | fields listed above. A string is considered as set even if it is non-empty. |
| 1316 | ``module_search_paths`` is considered as set if |
| 1317 | ``module_search_paths_set`` is set to 1. In this case, path |
| 1318 | configuration input fields are ignored as well. |
| 1319 | |
| 1320 | Set :c:member:`~PyConfig.pathconfig_warnings` to 0 to suppress warnings when |
Victor Stinner | 8bf39b6 | 2019-09-26 02:22:35 +0200 | [diff] [blame] | 1321 | calculating the path configuration (Unix only, Windows does not log any warning). |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1322 | |
| 1323 | If :c:member:`~PyConfig.base_prefix` or :c:member:`~PyConfig.base_exec_prefix` |
| 1324 | fields are not set, they inherit their value from :c:member:`~PyConfig.prefix` |
| 1325 | and :c:member:`~PyConfig.exec_prefix` respectively. |
| 1326 | |
| 1327 | :c:func:`Py_RunMain` and :c:func:`Py_Main` modify :data:`sys.path`: |
| 1328 | |
| 1329 | * If :c:member:`~PyConfig.run_filename` is set and is a directory which contains a |
| 1330 | ``__main__.py`` script, prepend :c:member:`~PyConfig.run_filename` to |
| 1331 | :data:`sys.path`. |
| 1332 | * If :c:member:`~PyConfig.isolated` is zero: |
| 1333 | |
| 1334 | * If :c:member:`~PyConfig.run_module` is set, prepend the current directory |
| 1335 | to :data:`sys.path`. Do nothing if the current directory cannot be read. |
| 1336 | * If :c:member:`~PyConfig.run_filename` is set, prepend the directory of the |
| 1337 | filename to :data:`sys.path`. |
| 1338 | * Otherwise, prepend an empty string to :data:`sys.path`. |
| 1339 | |
| 1340 | If :c:member:`~PyConfig.site_import` is non-zero, :data:`sys.path` can be |
| 1341 | modified by the :mod:`site` module. If |
| 1342 | :c:member:`~PyConfig.user_site_directory` is non-zero and the user's |
| 1343 | site-package directory exists, the :mod:`site` module appends the user's |
| 1344 | site-package directory to :data:`sys.path`. |
| 1345 | |
| 1346 | The following configuration files are used by the path configuration: |
| 1347 | |
| 1348 | * ``pyvenv.cfg`` |
| 1349 | * ``python._pth`` (Windows only) |
| 1350 | * ``pybuilddir.txt`` (Unix only) |
| 1351 | |
Victor Stinner | fcdb027 | 2019-09-23 14:45:47 +0200 | [diff] [blame] | 1352 | The ``__PYVENV_LAUNCHER__`` environment variable is used to set |
| 1353 | :c:member:`PyConfig.base_executable` |
| 1354 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1355 | |
| 1356 | Py_RunMain() |
| 1357 | ------------ |
| 1358 | |
| 1359 | .. c:function:: int Py_RunMain(void) |
| 1360 | |
| 1361 | Execute the command (:c:member:`PyConfig.run_command`), the script |
| 1362 | (:c:member:`PyConfig.run_filename`) or the module |
| 1363 | (:c:member:`PyConfig.run_module`) specified on the command line or in the |
| 1364 | configuration. |
| 1365 | |
| 1366 | By default and when if :option:`-i` option is used, run the REPL. |
| 1367 | |
| 1368 | Finally, finalizes Python and returns an exit status that can be passed to |
| 1369 | the ``exit()`` function. |
| 1370 | |
| 1371 | See :ref:`Python Configuration <init-python-config>` for an example of |
| 1372 | customized Python always running in isolated mode using |
| 1373 | :c:func:`Py_RunMain`. |
| 1374 | |
| 1375 | |
Victor Stinner | e81f6e6 | 2020-06-08 18:12:59 +0200 | [diff] [blame] | 1376 | Py_GetArgcArgv() |
| 1377 | ---------------- |
| 1378 | |
| 1379 | .. c:function:: void Py_GetArgcArgv(int *argc, wchar_t ***argv) |
| 1380 | |
| 1381 | Get the original command line arguments, before Python modified them. |
| 1382 | |
Victor Stinner | dd8a93e | 2020-06-30 00:49:03 +0200 | [diff] [blame] | 1383 | See also :c:member:`PyConfig.orig_argv` member. |
| 1384 | |
Victor Stinner | e81f6e6 | 2020-06-08 18:12:59 +0200 | [diff] [blame] | 1385 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1386 | Multi-Phase Initialization Private Provisional API |
| 1387 | -------------------------------------------------- |
| 1388 | |
| 1389 | This section is a private provisional API introducing multi-phase |
| 1390 | initialization, the core feature of the :pep:`432`: |
| 1391 | |
| 1392 | * "Core" initialization phase, "bare minimum Python": |
| 1393 | |
| 1394 | * Builtin types; |
| 1395 | * Builtin exceptions; |
| 1396 | * Builtin and frozen modules; |
| 1397 | * The :mod:`sys` module is only partially initialized |
Victor Stinner | 88feaec | 2019-09-26 03:15:07 +0200 | [diff] [blame] | 1398 | (ex: :data:`sys.path` doesn't exist yet). |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1399 | |
| 1400 | * "Main" initialization phase, Python is fully initialized: |
| 1401 | |
| 1402 | * Install and configure :mod:`importlib`; |
| 1403 | * Apply the :ref:`Path Configuration <init-path-config>`; |
| 1404 | * Install signal handlers; |
| 1405 | * Finish :mod:`sys` module initialization (ex: create :data:`sys.stdout` |
| 1406 | and :data:`sys.path`); |
| 1407 | * Enable optional features like :mod:`faulthandler` and :mod:`tracemalloc`; |
| 1408 | * Import the :mod:`site` module; |
| 1409 | * etc. |
| 1410 | |
| 1411 | Private provisional API: |
| 1412 | |
| 1413 | * :c:member:`PyConfig._init_main`: if set to 0, |
| 1414 | :c:func:`Py_InitializeFromConfig` stops at the "Core" initialization phase. |
Victor Stinner | 252346a | 2020-05-01 11:33:44 +0200 | [diff] [blame] | 1415 | * :c:member:`PyConfig._isolated_interpreter`: if non-zero, |
| 1416 | disallow threads, subprocesses and fork. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1417 | |
| 1418 | .. c:function:: PyStatus _Py_InitializeMain(void) |
| 1419 | |
| 1420 | Move to the "Main" initialization phase, finish the Python initialization. |
| 1421 | |
| 1422 | No module is imported during the "Core" phase and the ``importlib`` module is |
| 1423 | not configured: the :ref:`Path Configuration <init-path-config>` is only |
| 1424 | applied during the "Main" phase. It may allow to customize Python in Python to |
| 1425 | override or tune the :ref:`Path Configuration <init-path-config>`, maybe |
Victor Stinner | 88feaec | 2019-09-26 03:15:07 +0200 | [diff] [blame] | 1426 | install a custom :data:`sys.meta_path` importer or an import hook, etc. |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1427 | |
Victor Stinner | 88feaec | 2019-09-26 03:15:07 +0200 | [diff] [blame] | 1428 | It may become possible to calculatin the :ref:`Path Configuration |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1429 | <init-path-config>` in Python, after the Core phase and before the Main phase, |
| 1430 | which is one of the :pep:`432` motivation. |
| 1431 | |
| 1432 | The "Core" phase is not properly defined: what should be and what should |
| 1433 | not be available at this phase is not specified yet. The API is marked |
| 1434 | as private and provisional: the API can be modified or even be removed |
| 1435 | anytime until a proper public API is designed. |
| 1436 | |
| 1437 | Example running Python code between "Core" and "Main" initialization |
| 1438 | phases:: |
| 1439 | |
| 1440 | void init_python(void) |
| 1441 | { |
| 1442 | PyStatus status; |
Victor Stinner | 8462a49 | 2019-10-01 12:06:16 +0200 | [diff] [blame] | 1443 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1444 | PyConfig config; |
Victor Stinner | 8462a49 | 2019-10-01 12:06:16 +0200 | [diff] [blame] | 1445 | PyConfig_InitPythonConfig(&config); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1446 | config._init_main = 0; |
| 1447 | |
| 1448 | /* ... customize 'config' configuration ... */ |
| 1449 | |
| 1450 | status = Py_InitializeFromConfig(&config); |
| 1451 | PyConfig_Clear(&config); |
| 1452 | if (PyStatus_Exception(status)) { |
| 1453 | Py_ExitStatusException(status); |
| 1454 | } |
| 1455 | |
| 1456 | /* Use sys.stderr because sys.stdout is only created |
| 1457 | by _Py_InitializeMain() */ |
| 1458 | int res = PyRun_SimpleString( |
| 1459 | "import sys; " |
| 1460 | "print('Run Python code before _Py_InitializeMain', " |
| 1461 | "file=sys.stderr)"); |
| 1462 | if (res < 0) { |
| 1463 | exit(1); |
| 1464 | } |
| 1465 | |
| 1466 | /* ... put more configuration code here ... */ |
| 1467 | |
| 1468 | status = _Py_InitializeMain(); |
| 1469 | if (PyStatus_Exception(status)) { |
| 1470 | Py_ExitStatusException(status); |
| 1471 | } |
| 1472 | } |