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