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