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