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