2019-03-27 19:44:33 -04:00
|
|
|
PEP: 587
|
|
|
|
Title: Python Initialization Configuration
|
2023-10-11 08:05:51 -04:00
|
|
|
Author: Victor Stinner <vstinner@python.org>, Alyssa Coghlan <ncoghlan@gmail.com>
|
2019-05-15 12:50:11 -04:00
|
|
|
BDFL-Delegate: Thomas Wouters <thomas@python.org>
|
2019-03-27 19:44:33 -04:00
|
|
|
Discussions-To: python-dev@python.org
|
2019-08-23 13:00:52 -04:00
|
|
|
Status: Final
|
2019-03-27 19:44:33 -04:00
|
|
|
Type: Standards Track
|
|
|
|
Content-Type: text/x-rst
|
2019-03-28 04:46:32 -04:00
|
|
|
Created: 27-Mar-2019
|
2019-03-27 19:44:33 -04:00
|
|
|
Python-Version: 3.8
|
|
|
|
|
|
|
|
Abstract
|
|
|
|
========
|
|
|
|
|
2019-03-27 21:17:12 -04:00
|
|
|
Add a new C API to configure the Python Initialization providing finer
|
|
|
|
control on the whole configuration and better error reporting.
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
It becomes possible to read the configuration and then override some
|
|
|
|
computed parameters before it is applied. It also becomes possible to
|
|
|
|
completely override how Python computes the module search paths
|
|
|
|
(``sys.path``).
|
2019-05-15 12:50:11 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
The new `Isolated Configuration`_ provides sane default values to
|
|
|
|
isolate Python from the system. For example, to embed Python into an
|
|
|
|
application. Using the environment are now opt-in options, rather than
|
|
|
|
an opt-out options. For example, environment variables, command line
|
|
|
|
arguments and global configuration variables are ignored by default.
|
2019-05-15 12:50:11 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
Building a customized Python which behaves as the regular Python becomes
|
2019-05-20 08:05:52 -04:00
|
|
|
easier using the new ``Py_RunMain()`` function. Moreover, using the
|
2019-05-20 07:11:42 -04:00
|
|
|
`Python Configuration`_, ``PyConfig.argv`` arguments are now parsed the
|
|
|
|
same way the regular Python parses command line arguments, and
|
|
|
|
``PyConfig.xoptions`` are handled as ``-X opt`` command line options.
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
This extracts a subset of the API design from the :pep:`432` development
|
2019-05-20 07:11:42 -04:00
|
|
|
and refactoring work that is now considered sufficiently stable to make
|
|
|
|
public (allowing 3rd party embedding applications access to the same
|
|
|
|
configuration APIs that the native CPython CLI is now using).
|
2019-04-16 09:58:12 -04:00
|
|
|
|
2019-03-27 19:44:33 -04:00
|
|
|
|
|
|
|
Rationale
|
|
|
|
=========
|
|
|
|
|
2019-05-15 12:50:11 -04:00
|
|
|
Python is highly configurable but its configuration evolved organically.
|
|
|
|
The initialization configuration is scattered all around the code using
|
|
|
|
different ways to set them: global configuration variables (ex:
|
|
|
|
``Py_IsolatedFlag``), environment variables (ex: ``PYTHONPATH``),
|
|
|
|
command line arguments (ex: ``-b``), configuration files (ex:
|
|
|
|
``pyvenv.cfg``), function calls (ex: ``Py_SetProgramName()``). A
|
|
|
|
straightforward and reliable way to configure Python is needed.
|
|
|
|
|
|
|
|
Some configuration parameters are not accessible from the C API, or not
|
|
|
|
easily. For example, there is no API to override the default values of
|
|
|
|
``sys.executable``.
|
|
|
|
|
|
|
|
Some options like ``PYTHONPATH`` can only be set using an environment
|
2019-05-20 07:11:42 -04:00
|
|
|
variable which has a side effect on Python child processes if not unset
|
|
|
|
properly.
|
2019-05-15 12:50:11 -04:00
|
|
|
|
|
|
|
Some options also depends on other options: see `Priority and Rules`_.
|
|
|
|
Python 3.7 API does not provide a consistent view of the overall
|
|
|
|
configuration.
|
2019-03-27 19:44:33 -04:00
|
|
|
|
|
|
|
The C API of Python 3.7 Initialization takes ``wchar_t*`` strings as
|
|
|
|
input whereas the Python filesystem encoding is set during the
|
2019-05-15 12:50:11 -04:00
|
|
|
initialization which can lead to mojibake.
|
|
|
|
|
|
|
|
Python 3.7 APIs like ``Py_Initialize()`` aborts the process on memory
|
|
|
|
allocation failure which is not convenient when Python is embedded.
|
|
|
|
Moreover, ``Py_Main()`` could exit directly the process rather than
|
|
|
|
returning an exit code. Proposed new API reports the error or exit code
|
|
|
|
to the caller which can decide how to handle it.
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
Implementing the :pep:`540` (UTF-8 Mode) and the new ``-X dev`` correctly
|
2019-05-15 12:50:11 -04:00
|
|
|
was almost impossible in Python 3.6. The code base has been deeply
|
|
|
|
reworked in Python 3.7 and then in Python 3.8 to read the configuration
|
|
|
|
into a structure with no side effect. It becomes possible to clear the
|
|
|
|
configuration (release memory) and read again the configuration if the
|
|
|
|
encoding changed . It is required to implement properly the UTF-8 which
|
|
|
|
changes the encoding using ``-X utf8`` command line option. Internally,
|
|
|
|
bytes ``argv`` strings are decoded from the filesystem encoding. The
|
|
|
|
``-X dev`` changes the memory allocator (behaves as
|
|
|
|
``PYTHONMALLOC=debug``), whereas it was not possible to change the
|
|
|
|
memory allocation *while* parsing the command line arguments. The new
|
|
|
|
design of the internal implementation not only allowed to implement
|
|
|
|
properly ``-X utf8`` and ``-X dev``, it also allows to change the Python
|
|
|
|
behavior way more easily, especially for corner cases like that, and
|
|
|
|
ensure that the configuration remains consistent: see `Priority and
|
|
|
|
Rules`_.
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
This PEP is a partial implementation of :pep:`432` which is the overall
|
2019-03-28 17:05:34 -04:00
|
|
|
design. New fields can be added later to ``PyConfig`` structure to
|
2022-01-21 06:03:51 -05:00
|
|
|
finish the implementation of the :pep:`432` (e.g. by adding a new partial
|
2019-04-16 09:58:12 -04:00
|
|
|
initialization API which allows to configure Python using Python objects to
|
|
|
|
finish the full initialization). However, those features are omitted from this
|
|
|
|
PEP as even the native CPython CLI doesn't work that way - the public API
|
|
|
|
proposal in this PEP is limited to features which have already been implemented
|
|
|
|
and adopted as private APIs for us in the native CPython CLI.
|
2019-03-28 17:05:34 -04:00
|
|
|
|
2019-03-27 19:44:33 -04:00
|
|
|
|
|
|
|
Python Initialization C API
|
|
|
|
===========================
|
|
|
|
|
2019-05-25 05:45:42 -04:00
|
|
|
This PEP proposes to add the following new structures and functions.
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
New structures:
|
2019-03-27 19:44:33 -04:00
|
|
|
|
|
|
|
* ``PyConfig``
|
|
|
|
* ``PyPreConfig``
|
2019-05-25 05:45:42 -04:00
|
|
|
* ``PyStatus``
|
2019-05-02 15:14:06 -04:00
|
|
|
* ``PyWideStringList``
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
New functions:
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``PyConfig_Clear(config)``
|
|
|
|
* ``PyConfig_InitIsolatedConfig()``
|
|
|
|
* ``PyConfig_InitPythonConfig()``
|
|
|
|
* ``PyConfig_Read(config)``
|
|
|
|
* ``PyConfig_SetArgv(config, argc, argv)``
|
|
|
|
* ``PyConfig_SetBytesArgv(config, argc, argv)``
|
|
|
|
* ``PyConfig_SetBytesString(config, config_str, str)``
|
|
|
|
* ``PyConfig_SetString(config, config_str, str)``
|
|
|
|
* ``PyPreConfig_InitIsolatedConfig(preconfig)``
|
|
|
|
* ``PyPreConfig_InitPythonConfig(preconfig)``
|
2019-05-24 15:21:05 -04:00
|
|
|
* ``PyStatus_Error(err_msg)``
|
|
|
|
* ``PyStatus_Exception(status)``
|
|
|
|
* ``PyStatus_Exit(exitcode)``
|
|
|
|
* ``PyStatus_IsError(status)``
|
|
|
|
* ``PyStatus_IsExit(status)``
|
|
|
|
* ``PyStatus_NoMemory()``
|
|
|
|
* ``PyStatus_Ok()``
|
2019-05-02 15:14:06 -04:00
|
|
|
* ``PyWideStringList_Append(list, item)``
|
2019-05-15 12:50:11 -04:00
|
|
|
* ``PyWideStringList_Insert(list, index, item)``
|
|
|
|
* ``Py_BytesMain(argc, argv)``
|
2019-05-24 15:21:05 -04:00
|
|
|
* ``Py_ExitStatusException(status)``
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``Py_InitializeFromConfig(config)``
|
|
|
|
* ``Py_PreInitialize(preconfig)``
|
|
|
|
* ``Py_PreInitializeFromArgs(preconfig, argc, argv)``
|
|
|
|
* ``Py_PreInitializeFromBytesArgs(preconfig, argc, argv)``
|
|
|
|
* ``Py_RunMain()``
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-02 15:14:06 -04:00
|
|
|
This PEP also adds ``_PyRuntimeState.preconfig`` (``PyPreConfig`` type)
|
|
|
|
and ``PyInterpreterState.config`` (``PyConfig`` type) fields to these
|
|
|
|
internal structures. ``PyInterpreterState.config`` becomes the new
|
|
|
|
reference configuration, replacing global configuration variables and
|
|
|
|
other private variables.
|
2019-03-28 17:05:34 -04:00
|
|
|
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-02 15:14:06 -04:00
|
|
|
PyWideStringList
|
|
|
|
----------------
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-02 15:14:06 -04:00
|
|
|
``PyWideStringList`` is a list of ``wchar_t*`` strings.
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-02 15:14:06 -04:00
|
|
|
``PyWideStringList`` structure fields:
|
2019-03-27 19:44:33 -04:00
|
|
|
|
|
|
|
* ``length`` (``Py_ssize_t``)
|
|
|
|
* ``items`` (``wchar_t**``)
|
|
|
|
|
2019-05-02 15:14:06 -04:00
|
|
|
Methods:
|
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
* ``PyStatus PyWideStringList_Append(PyWideStringList *list, const wchar_t *item)``:
|
2019-05-02 15:14:06 -04:00
|
|
|
Append *item* to *list*.
|
2019-05-24 15:21:05 -04:00
|
|
|
* ``PyStatus PyWideStringList_Insert(PyWideStringList *list, Py_ssize_t index, const wchar_t *item)``:
|
2019-05-15 12:50:11 -04:00
|
|
|
Insert *item* into *list* at *index*. If *index* is greater than
|
|
|
|
*list* length, just append *item* to *list*.
|
2019-05-02 15:14:06 -04:00
|
|
|
|
2019-03-27 19:44:33 -04:00
|
|
|
If *length* is non-zero, *items* must be non-NULL and all strings must
|
|
|
|
be non-NULL.
|
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
PyStatus
|
|
|
|
--------
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
``PyStatus`` is a structure to store the status of an initialization
|
|
|
|
function: success, error or exit. For an error, it can store the C
|
|
|
|
function name which created the error.
|
2019-03-27 19:44:33 -04:00
|
|
|
|
|
|
|
Example::
|
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
PyStatus alloc(void **ptr, size_t size)
|
2019-03-27 19:44:33 -04:00
|
|
|
{
|
|
|
|
*ptr = PyMem_RawMalloc(size);
|
|
|
|
if (*ptr == NULL) {
|
2019-05-24 15:21:05 -04:00
|
|
|
return PyStatus_NoMemory();
|
2019-03-27 19:44:33 -04:00
|
|
|
}
|
2019-05-24 15:21:05 -04:00
|
|
|
return PyStatus_Ok();
|
2019-03-27 19:44:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
void *ptr;
|
2019-05-24 15:21:05 -04:00
|
|
|
PyStatus status = alloc(&ptr, 16);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-03-27 19:44:33 -04:00
|
|
|
}
|
|
|
|
PyMem_Free(ptr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
``PyStatus`` fields:
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-02 15:14:06 -04:00
|
|
|
* ``exitcode`` (``int``):
|
2019-05-20 07:11:42 -04:00
|
|
|
Argument passed to ``exit()``.
|
|
|
|
* ``err_msg`` (``const char*``):
|
|
|
|
Error message.
|
|
|
|
* ``func`` (``const char *``):
|
|
|
|
Name of the function which created an error, can be ``NULL``.
|
2019-05-02 15:14:06 -04:00
|
|
|
* private ``_type`` field: for internal usage only.
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
Functions to create a status:
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
* ``PyStatus_Ok()``: Success.
|
|
|
|
* ``PyStatus_Error(err_msg)``: Initialization error with a message.
|
|
|
|
* ``PyStatus_NoMemory()``: Memory allocation failure (out of memory).
|
|
|
|
* ``PyStatus_Exit(exitcode)``: Exit Python with the specified exit code.
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
Functions to handle a status:
|
|
|
|
|
|
|
|
* ``PyStatus_Exception(status)``: Is the result an error or an exit?
|
|
|
|
If true, the exception must be handled; by calling
|
|
|
|
``Py_ExitStatusException(status)`` for example.
|
|
|
|
* ``PyStatus_IsError(status)``: Is the result an error?
|
|
|
|
* ``PyStatus_IsExit(status)``: Is the result an exit?
|
|
|
|
* ``Py_ExitStatusException(status)``: Call ``exit(exitcode)`` if *status*
|
|
|
|
is an exit. Print the error messageand exit with a non-zero exit code
|
|
|
|
if *status* is an error. Must only be called if
|
|
|
|
``PyStatus_Exception(status)`` is true.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
Internally, Python uses macros which set ``PyStatus.func``,
|
|
|
|
whereas functions to create a status set ``func`` to ``NULL``.
|
2019-03-27 19:44:33 -04:00
|
|
|
|
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
Preinitialization with PyPreConfig
|
|
|
|
----------------------------------
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
The ``PyPreConfig`` structure is used to preinitialize Python:
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
* Set the Python memory allocator
|
2019-03-27 19:44:33 -04:00
|
|
|
* Configure the LC_CTYPE locale
|
|
|
|
* Set the UTF-8 mode
|
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
Example using the preinitialization to enable the UTF-8 Mode::
|
|
|
|
|
2019-09-27 22:15:30 -04:00
|
|
|
PyStatus status;
|
2019-05-20 07:11:42 -04:00
|
|
|
PyPreConfig preconfig;
|
2019-09-27 22:15:30 -04:00
|
|
|
|
2019-09-30 18:39:34 -04:00
|
|
|
PyPreConfig_InitPythonConfig(&preconfig);
|
2019-03-27 19:44:33 -04:00
|
|
|
|
|
|
|
preconfig.utf8_mode = 1;
|
|
|
|
|
2019-09-27 22:15:30 -04:00
|
|
|
status = Py_PreInitialize(&preconfig);
|
2019-05-24 15:21:05 -04:00
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2019-03-27 19:44:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* at this point, Python will speak UTF-8 */
|
|
|
|
|
|
|
|
Py_Initialize();
|
|
|
|
/* ... use Python API here ... */
|
|
|
|
Py_Finalize();
|
|
|
|
|
2019-05-25 05:45:42 -04:00
|
|
|
Function to initialize a preconfiguration:
|
2019-05-20 07:11:42 -04:00
|
|
|
|
2019-09-27 22:15:30 -04:00
|
|
|
* ``PyStatus PyPreConfig_InitIsolatedConfig(PyPreConfig *preconfig)``
|
|
|
|
* ``PyStatus PyPreConfig_InitPythonConfig(PyPreConfig *preconfig)``
|
2019-05-20 07:11:42 -04:00
|
|
|
|
2019-05-25 05:45:42 -04:00
|
|
|
Functions to preinitialize Python:
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
* ``PyStatus Py_PreInitialize(const PyPreConfig *preconfig)``
|
|
|
|
* ``PyStatus Py_PreInitializeFromBytesArgs(const PyPreConfig *preconfig, int argc, char * const *argv)``
|
|
|
|
* ``PyStatus Py_PreInitializeFromArgs(const PyPreConfig *preconfig, int argc, wchar_t * const * argv)``
|
2019-05-02 15:14:06 -04:00
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
The caller is responsible to handle exceptions (error or exit) using
|
|
|
|
``PyStatus_Exception()`` and ``Py_ExitStatusException()``.
|
2019-05-15 17:54:05 -04:00
|
|
|
|
2019-05-25 05:45:42 -04:00
|
|
|
For `Python Configuration`_ (``PyPreConfig_InitPythonConfig()``),
|
|
|
|
if Python is initialized with command line arguments, the command line
|
2019-05-20 07:11:42 -04:00
|
|
|
arguments must also be passed to preinitialize Python, since they have
|
2019-05-15 17:54:05 -04:00
|
|
|
an effect on the pre-configuration like encodings. For example, the
|
|
|
|
``-X utf8`` command line option enables the UTF-8 Mode.
|
2019-03-27 19:44:33 -04:00
|
|
|
|
|
|
|
``PyPreConfig`` fields:
|
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``allocator`` (``int``):
|
|
|
|
Name of the memory allocator (ex: ``PYMEM_ALLOCATOR_MALLOC``). Valid
|
|
|
|
values:
|
|
|
|
|
|
|
|
* ``PYMEM_ALLOCATOR_NOT_SET`` (``0``): don't change memory allocators
|
|
|
|
(use defaults)
|
|
|
|
* ``PYMEM_ALLOCATOR_DEFAULT`` (``1``): default memory allocators
|
2019-05-24 15:21:05 -04:00
|
|
|
* ``PYMEM_ALLOCATOR_DEBUG`` (``2``): default memory allocators with
|
|
|
|
debug hooks
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``PYMEM_ALLOCATOR_MALLOC`` (``3``): force usage of ``malloc()``
|
2019-05-24 15:21:05 -04:00
|
|
|
* ``PYMEM_ALLOCATOR_MALLOC_DEBUG`` (``4``): force usage of
|
|
|
|
``malloc()`` with debug hooks
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``PYMEM_ALLOCATOR_PYMALLOC`` (``5``): Python "pymalloc" allocator
|
2019-05-24 15:21:05 -04:00
|
|
|
* ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG`` (``6``): Python "pymalloc"
|
|
|
|
allocator with debug hooks
|
2019-05-20 07:11:42 -04:00
|
|
|
* Note: ``PYMEM_ALLOCATOR_PYMALLOC`` and
|
|
|
|
``PYMEM_ALLOCATOR_PYMALLOC_DEBUG`` are not supported if Python is
|
|
|
|
configured using ``--without-pymalloc``
|
|
|
|
|
|
|
|
* ``configure_locale`` (``int``):
|
|
|
|
Set the LC_CTYPE locale to the user preferred locale? If equals to 0,
|
|
|
|
set ``coerce_c_locale`` and ``coerce_c_locale_warn`` to 0.
|
|
|
|
* ``coerce_c_locale`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
If equals to 2, coerce the C locale; if equals to 1, read the LC_CTYPE
|
|
|
|
locale to decide if it should be coerced.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``coerce_c_locale_warn`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
If non-zero, emit a warning if the C locale is coerced.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``dev_mode`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
See ``PyConfig.dev_mode``.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``isolated`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
See ``PyConfig.isolated``.
|
2019-05-24 15:21:05 -04:00
|
|
|
* ``legacy_windows_fs_encoding`` (``int``, Windows only):
|
2019-05-15 12:50:11 -04:00
|
|
|
If non-zero, disable UTF-8 Mode, set the Python filesystem encoding to
|
|
|
|
``mbcs``, set the filesystem error handler to ``replace``.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``parse_argv`` (``int``):
|
|
|
|
If non-zero, ``Py_PreInitializeFromArgs()`` and
|
|
|
|
``Py_PreInitializeFromBytesArgs()`` parse their ``argv`` argument the
|
|
|
|
same way the regular Python parses command line arguments: see
|
|
|
|
`Command Line Arguments`_.
|
|
|
|
* ``use_environment`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
See ``PyConfig.use_environment``.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``utf8_mode`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
If non-zero, enable the UTF-8 mode.
|
2019-05-02 15:14:06 -04:00
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
The ``legacy_windows_fs_encoding`` field is only available on Windows.
|
|
|
|
``#ifdef MS_WINDOWS`` macro can be used for Windows specific code.
|
|
|
|
|
|
|
|
``PyPreConfig`` private fields, for internal use only:
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
* ``_config_init`` (``int``):
|
2019-07-03 14:20:45 -04:00
|
|
|
Function used to initialize ``PyConfig``, used for preinitialization.
|
2019-05-15 17:54:05 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
``PyMem_SetAllocator()`` can be called after ``Py_PreInitialize()`` and
|
|
|
|
before ``Py_InitializeFromConfig()`` to install a custom memory
|
|
|
|
allocator. It can be called before ``Py_PreInitialize()`` if
|
|
|
|
``allocator`` is set to ``PYMEM_ALLOCATOR_NOT_SET`` (default value).
|
2019-05-15 17:54:05 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
Python memory allocation functions like ``PyMem_RawMalloc()`` must not
|
|
|
|
be used before Python preinitialization, whereas calling directly
|
|
|
|
``malloc()`` and ``free()`` is always safe. ``Py_DecodeLocale()`` must
|
|
|
|
not be called before the preinitialization.
|
2019-05-15 17:54:05 -04:00
|
|
|
|
2019-03-27 19:44:33 -04:00
|
|
|
|
|
|
|
Initialization with PyConfig
|
|
|
|
----------------------------
|
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
The ``PyConfig`` structure contains most parameters to configure Python.
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-15 17:54:05 -04:00
|
|
|
Example setting the program name::
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
void init_python(void)
|
|
|
|
{
|
2019-05-24 15:21:05 -04:00
|
|
|
PyStatus status;
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-10-01 06:19:09 -04:00
|
|
|
PyConfig config;
|
|
|
|
PyConfig_InitPythonConfig(&config);
|
2019-05-02 15:14:06 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
/* Set the program name. Implicitly preinitialize Python. */
|
2019-05-24 15:21:05 -04:00
|
|
|
status = PyConfig_SetString(&config, &config.program_name,
|
2019-05-25 05:45:42 -04:00
|
|
|
L"/path/to/my_program");
|
2019-05-24 15:21:05 -04:00
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-20 07:11:42 -04:00
|
|
|
goto fail;
|
|
|
|
}
|
2019-05-02 15:14:06 -04:00
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-20 07:11:42 -04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
PyConfig_Clear(&config);
|
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
PyConfig_Clear(&config);
|
2019-05-24 15:21:05 -04:00
|
|
|
Py_ExitStatusException(status);
|
2019-03-27 19:44:33 -04:00
|
|
|
}
|
|
|
|
|
2019-05-02 15:14:06 -04:00
|
|
|
``PyConfig`` methods:
|
|
|
|
|
2019-10-01 06:19:09 -04:00
|
|
|
* ``void PyConfig_InitPythonConfig(PyConfig *config)``
|
2019-05-20 07:11:42 -04:00
|
|
|
Initialize configuration with `Python Configuration`_.
|
2019-10-01 06:19:09 -04:00
|
|
|
* ``void PyConfig_InitIsolatedConfig(PyConfig *config)``:
|
2019-05-20 07:11:42 -04:00
|
|
|
Initialize configuration with `Isolated Configuration`_.
|
2019-05-24 15:21:05 -04:00
|
|
|
* ``PyStatus PyConfig_SetString(PyConfig *config, wchar_t * const *config_str, const wchar_t *str)``:
|
2019-05-15 17:54:05 -04:00
|
|
|
Copy the wide character string *str* into ``*config_str``.
|
2019-05-20 07:11:42 -04:00
|
|
|
Preinitialize Python if needed.
|
2019-05-24 15:21:05 -04:00
|
|
|
* ``PyStatus PyConfig_SetBytesString(PyConfig *config, wchar_t * const *config_str, const char *str)``:
|
2019-05-02 15:14:06 -04:00
|
|
|
Decode *str* using ``Py_DecodeLocale()`` and set the result into
|
2019-05-20 07:11:42 -04:00
|
|
|
``*config_str``.
|
|
|
|
Preinitialize Python if needed.
|
2019-05-24 15:21:05 -04:00
|
|
|
* ``PyStatus PyConfig_SetArgv(PyConfig *config, int argc, wchar_t * const *argv)``:
|
2019-05-15 17:54:05 -04:00
|
|
|
Set command line arguments from wide character strings.
|
2019-05-20 07:11:42 -04:00
|
|
|
Preinitialize Python if needed.
|
2019-05-24 15:21:05 -04:00
|
|
|
* ``PyStatus PyConfig_SetBytesArgv(PyConfig *config, int argc, char * const *argv)``:
|
2019-05-15 17:54:05 -04:00
|
|
|
Set command line arguments: decode bytes using ``Py_DecodeLocale()``.
|
2019-05-20 07:11:42 -04:00
|
|
|
Preinitialize Python if needed.
|
2019-05-24 15:21:05 -04:00
|
|
|
* ``PyStatus PyConfig_Read(PyConfig *config)``:
|
2019-05-20 07:11:42 -04:00
|
|
|
Read all Python configuration. Fields which are already initialized
|
|
|
|
are left unchanged.
|
|
|
|
Preinitialize Python if needed.
|
2019-05-02 15:14:06 -04:00
|
|
|
* ``void PyConfig_Clear(PyConfig *config)``:
|
2019-05-15 12:50:11 -04:00
|
|
|
Release configuration memory.
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
Most ``PyConfig`` methods preinitialize Python if needed. In that case,
|
|
|
|
the Python preinitialization configuration in based on the ``PyConfig``.
|
|
|
|
If configuration fields which are in common with ``PyPreConfig`` are
|
|
|
|
tuned, they must be set before calling a ``PyConfig`` method:
|
|
|
|
|
|
|
|
* ``dev_mode``
|
|
|
|
* ``isolated``
|
|
|
|
* ``parse_argv``
|
|
|
|
* ``use_environment``
|
|
|
|
|
|
|
|
Moreover, if ``PyConfig_SetArgv()`` or ``PyConfig_SetBytesArgv()`` is
|
|
|
|
used, this method must be called first, before other methods, since the
|
|
|
|
preinitialization configuration depends on command line arguments (if
|
|
|
|
``parse_argv`` is non-zero).
|
|
|
|
|
2019-03-27 19:44:33 -04:00
|
|
|
Functions to initialize Python:
|
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
* ``PyStatus Py_InitializeFromConfig(const PyConfig *config)``:
|
2019-05-20 07:11:42 -04:00
|
|
|
Initialize Python from *config* configuration.
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
The caller of these methods and functions is responsible to handle
|
|
|
|
exceptions (error or exit) using ``PyStatus_Exception()`` and
|
|
|
|
``Py_ExitStatusException()``.
|
|
|
|
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-15 17:54:05 -04:00
|
|
|
``PyConfig`` fields:
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``argv`` (``PyWideStringList``):
|
|
|
|
Command line arguments, ``sys.argv``. See ``parse_argv`` to parse
|
|
|
|
``argv`` the same way the regular Python parses Python command line
|
|
|
|
arguments. If ``argv`` is empty, an empty string is added to ensure
|
|
|
|
that ``sys.argv`` always exists and is never empty.
|
|
|
|
* ``base_exec_prefix`` (``wchar_t*``):
|
2019-05-15 12:50:11 -04:00
|
|
|
``sys.base_exec_prefix``.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``base_prefix`` (``wchar_t*``):
|
2019-05-15 12:50:11 -04:00
|
|
|
``sys.base_prefix``.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``buffered_stdio`` (``int``):
|
2019-05-16 09:12:46 -04:00
|
|
|
If equals to 0, enable unbuffered mode, making the stdout and stderr streams
|
|
|
|
unbuffered.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``bytes_warning`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
If equals to 1, issue a warning when comparing ``bytes`` or
|
|
|
|
``bytearray`` with ``str``, or comparing ``bytes`` with ``int``. If
|
|
|
|
equal or greater to 2, raise a ``BytesWarning`` exception.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``check_hash_pycs_mode`` (``wchar_t*``):
|
2022-01-21 06:03:51 -05:00
|
|
|
``--check-hash-based-pycs`` command line option value (see :pep:`552`).
|
2019-05-25 05:45:42 -04:00
|
|
|
Valid values: ``always``, ``never`` and ``default``. The default value
|
|
|
|
is ``default``.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``configure_c_stdio`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
If non-zero, configure C standard streams (``stdio``, ``stdout``,
|
|
|
|
``stdout``). For example, set their mode to ``O_BINARY`` on Windows.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``dev_mode`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
Development mode
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``dump_refs`` (``int``):
|
2019-05-25 05:45:42 -04:00
|
|
|
If non-zero, dump all objects which are still alive at exit.
|
|
|
|
Require a special Python build with ``Py_REF_DEBUG`` macro defined.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``exec_prefix`` (``wchar_t*``):
|
2019-05-15 12:50:11 -04:00
|
|
|
``sys.exec_prefix``.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``executable`` (``wchar_t*``):
|
2019-05-15 12:50:11 -04:00
|
|
|
``sys.executable``.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``faulthandler`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
If non-zero, call ``faulthandler.enable()``.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``filesystem_encoding`` (``wchar_t*``):
|
2019-05-15 12:50:11 -04:00
|
|
|
Filesystem encoding, ``sys.getfilesystemencoding()``.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``filesystem_errors`` (``wchar_t*``):
|
2019-05-15 12:50:11 -04:00
|
|
|
Filesystem encoding errors, ``sys.getfilesystemencodeerrors()``.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``use_hash_seed`` (``int``),
|
|
|
|
``hash_seed`` (``unsigned long``):
|
2019-05-15 12:50:11 -04:00
|
|
|
Randomized hash function seed.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``home`` (``wchar_t*``):
|
2019-05-15 12:50:11 -04:00
|
|
|
Python home directory.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``import_time`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
If non-zero, profile import time.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``inspect`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
Enter interactive mode after executing a script or a command.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``install_signal_handlers`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
Install signal handlers?
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``interactive`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
Interactive mode.
|
2019-05-24 15:21:05 -04:00
|
|
|
* ``isolated`` (``int``):
|
|
|
|
If greater than 0, enable isolated mode:
|
|
|
|
|
|
|
|
* ``sys.path`` contains neither the script's directory (computed from
|
|
|
|
``argv[0]`` or the current directory) nor the user's site-packages
|
|
|
|
directory.
|
|
|
|
* Python REPL doesn't import ``readline`` nor enable default readline
|
|
|
|
configuration on interactive prompts.
|
2019-06-02 18:23:46 -04:00
|
|
|
* Set ``use_environment`` and ``user_site_directory`` to 0.
|
2019-05-24 15:21:05 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``legacy_windows_stdio`` (``int``, Windows only):
|
2019-05-15 12:50:11 -04:00
|
|
|
If non-zero, use ``io.FileIO`` instead of ``WindowsConsoleIO`` for
|
|
|
|
``sys.stdin``, ``sys.stdout`` and ``sys.stderr``.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``malloc_stats`` (``int``):
|
2019-05-25 05:45:42 -04:00
|
|
|
If non-zero, dump statistics on ``pymalloc`` memory allocator at exit.
|
|
|
|
The option is ignored if Python is built using ``--without-pymalloc``.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``pythonpath_env`` (``wchar_t*``):
|
2019-05-25 05:45:42 -04:00
|
|
|
Module search paths as a string separated by DELIM (usually ``:``
|
|
|
|
character).
|
2019-05-20 07:11:42 -04:00
|
|
|
Initialized from ``PYTHONPATH`` environment variable value by default.
|
|
|
|
* ``module_search_paths_set`` (``int``),
|
|
|
|
``module_search_paths`` (``PyWideStringList``):
|
|
|
|
``sys.path``. If ``module_search_paths_set`` is equal to 0, the
|
2019-05-25 05:45:42 -04:00
|
|
|
``module_search_paths`` is overridden by the function computing the
|
|
|
|
`Path Configuration`_.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``optimization_level`` (``int``):
|
2019-05-25 05:45:42 -04:00
|
|
|
Compilation optimization level:
|
|
|
|
|
|
|
|
* 0: Peephole optimizer (and ``__debug__`` is set to ``True``)
|
|
|
|
* 1: Remove assertions, set ``__debug__`` to ``False``
|
|
|
|
* 2: Strip docstrings
|
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``parse_argv`` (``int``):
|
|
|
|
If non-zero, parse ``argv`` the same way the regular Python command
|
|
|
|
line arguments, and strip Python arguments from ``argv``: see `Command
|
|
|
|
Line Arguments`_.
|
|
|
|
* ``parser_debug`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
If non-zero, turn on parser debugging output (for expert only,
|
|
|
|
depending on compilation options).
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``pathconfig_warnings`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
If equal to 0, suppress warnings when computing the path
|
2019-05-20 07:11:42 -04:00
|
|
|
configuration (Unix only, Windows does not log any warning). Otherwise,
|
|
|
|
warnings are written into stderr.
|
|
|
|
* ``prefix`` (``wchar_t*``):
|
2019-05-15 12:50:11 -04:00
|
|
|
``sys.prefix``.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``program_name`` (``wchar_t*``):
|
2019-05-15 12:50:11 -04:00
|
|
|
Program name.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``pycache_prefix`` (``wchar_t*``):
|
2019-05-15 12:50:11 -04:00
|
|
|
``.pyc`` cache prefix.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``quiet`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
Quiet mode. For example, don't display the copyright and version
|
|
|
|
messages even in interactive mode.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``run_command`` (``wchar_t*``):
|
2019-05-25 05:45:42 -04:00
|
|
|
``python3 -c COMMAND`` argument.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``run_filename`` (``wchar_t*``):
|
2019-05-25 05:45:42 -04:00
|
|
|
``python3 FILENAME`` argument.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``run_module`` (``wchar_t*``):
|
2019-05-15 12:50:11 -04:00
|
|
|
``python3 -m MODULE`` argument.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``show_alloc_count`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
Show allocation counts at exit?
|
2019-05-25 05:45:42 -04:00
|
|
|
Need a special Python build with ``COUNT_ALLOCS`` macro defined.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``show_ref_count`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
Show total reference count at exit?
|
2019-05-25 05:45:42 -04:00
|
|
|
Need a debug build of Python (``Py_REF_DEBUG`` macro should be
|
|
|
|
defined).
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``site_import`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
Import the ``site`` module at startup?
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``skip_source_first_line`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
Skip the first line of the source?
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``stdio_encoding`` (``wchar_t*``),
|
|
|
|
``stdio_errors`` (``wchar_t*``):
|
2019-05-15 12:50:11 -04:00
|
|
|
Encoding and encoding errors of ``sys.stdin``, ``sys.stdout``
|
|
|
|
and ``sys.stderr``.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``tracemalloc`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
If non-zero, call ``tracemalloc.start(value)``.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``user_site_directory`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
If non-zero, add user site directory to ``sys.path``.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``verbose`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
If non-zero, enable verbose mode.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``warnoptions`` (``PyWideStringList``):
|
2019-05-15 12:50:11 -04:00
|
|
|
Options of the ``warnings`` module to build warnings filters.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``write_bytecode`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
If non-zero, write ``.pyc`` files.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``xoptions`` (``PyWideStringList``):
|
2019-05-15 12:50:11 -04:00
|
|
|
``sys._xoptions``.
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
The ``legacy_windows_stdio`` field is only available on Windows.
|
|
|
|
``#ifdef MS_WINDOWS`` macro can be used for Windows specific code.
|
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
If ``parse_argv`` is non-zero, ``argv`` arguments are parsed the same
|
|
|
|
way the regular Python parses command line arguments, and Python
|
|
|
|
arguments are stripped from ``argv``: see `Command Line Arguments`_.
|
|
|
|
|
|
|
|
The ``xoptions`` options are parsed to set other options: see `-X
|
|
|
|
Options`_.
|
|
|
|
|
2019-05-15 17:54:05 -04:00
|
|
|
``PyConfig`` private fields, for internal use only:
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``_config_init`` (``int``):
|
2019-07-03 14:20:45 -04:00
|
|
|
Function used to initialize ``PyConfig``, used for preinitialization.
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``_install_importlib`` (``int``):
|
2019-05-15 12:50:11 -04:00
|
|
|
Install importlib?
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``_init_main`` (``int``):
|
2019-05-15 17:54:05 -04:00
|
|
|
If equal to 0, stop Python initialization before the "main" phase
|
2022-01-21 06:03:51 -05:00
|
|
|
(see :pep:`432`).
|
2019-05-15 12:50:11 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
More complete example modifying the default configuration, read the
|
|
|
|
configuration, and then override some parameters::
|
2019-05-02 15:14:06 -04:00
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
PyStatus init_python(const char *program_name)
|
2019-05-02 15:14:06 -04:00
|
|
|
{
|
2019-05-24 15:21:05 -04:00
|
|
|
PyStatus status;
|
2019-05-20 07:11:42 -04:00
|
|
|
|
2019-10-01 06:19:09 -04:00
|
|
|
PyConfig config;
|
|
|
|
PyConfig_InitPythonConfig(&config);
|
2019-05-02 15:14:06 -04:00
|
|
|
|
2019-06-25 00:58:50 -04:00
|
|
|
/* Set the program name before reading the configuration
|
2019-05-20 07:11:42 -04:00
|
|
|
(decode byte string from the locale encoding).
|
|
|
|
|
|
|
|
Implicitly preinitialize Python. */
|
2019-05-24 15:21:05 -04:00
|
|
|
status = PyConfig_SetBytesString(&config, &config.program_name,
|
2019-05-15 12:50:11 -04:00
|
|
|
program_name);
|
2019-05-24 15:21:05 -04:00
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-20 07:11:42 -04:00
|
|
|
goto done;
|
2019-05-02 15:14:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read all configuration at once */
|
2019-05-24 15:21:05 -04:00
|
|
|
status = PyConfig_Read(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-20 07:11:42 -04:00
|
|
|
goto done;
|
2019-05-02 15:14:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Append our custom search path to sys.path */
|
2019-05-24 15:21:05 -04:00
|
|
|
status = PyWideStringList_Append(&config.module_search_paths,
|
2019-05-02 15:14:06 -04:00
|
|
|
L"/path/to/more/modules");
|
2019-05-24 15:21:05 -04:00
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-20 07:11:42 -04:00
|
|
|
goto done;
|
2019-05-02 15:14:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Override executable computed by PyConfig_Read() */
|
2019-05-24 15:21:05 -04:00
|
|
|
status = PyConfig_SetString(&config, &config.executable,
|
2019-05-25 05:45:42 -04:00
|
|
|
L"/path/to/my_executable");
|
2019-05-24 15:21:05 -04:00
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-20 07:11:42 -04:00
|
|
|
goto done;
|
2019-05-02 15:14:06 -04:00
|
|
|
}
|
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
2019-05-02 15:14:06 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
done:
|
2019-05-02 15:14:06 -04:00
|
|
|
PyConfig_Clear(&config);
|
2019-05-24 15:21:05 -04:00
|
|
|
return status;
|
2019-05-02 15:14:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
.. note::
|
2019-05-20 07:11:42 -04:00
|
|
|
``PyImport_FrozenModules``, ``PyImport_AppendInittab()`` and
|
|
|
|
``PyImport_ExtendInittab()`` functions are still relevant and
|
2019-05-24 15:21:05 -04:00
|
|
|
continue to work as previously. They should be set or called after
|
|
|
|
Python preinitialization and before the Python initialization.
|
2019-05-02 15:14:06 -04:00
|
|
|
|
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
Isolated Configuration
|
|
|
|
----------------------
|
2019-05-02 15:14:06 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
``PyPreConfig_InitIsolatedConfig()`` and
|
|
|
|
``PyConfig_InitIsolatedConfig()`` functions create a configuration to
|
|
|
|
isolate Python from the system. For example, to embed Python into an
|
|
|
|
application.
|
2019-05-02 15:14:06 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
This configuration ignores global configuration variables, environments
|
|
|
|
variables and command line arguments (``argv`` is not parsed). The C
|
|
|
|
standard streams (ex: ``stdout``) and the LC_CTYPE locale are left
|
|
|
|
unchanged by default.
|
2019-05-02 15:14:06 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
Configuration files are still used with this configuration. Set the
|
|
|
|
`Path Configuration`_ ("output fields") to ignore these configuration
|
|
|
|
files and avoid the function computing the default path configuration.
|
2019-05-02 15:14:06 -04:00
|
|
|
|
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
Python Configuration
|
|
|
|
--------------------
|
2019-05-02 15:14:06 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
``PyPreConfig_InitPythonConfig()`` and ``PyConfig_InitPythonConfig()``
|
|
|
|
functions create a configuration to build a customized Python which
|
|
|
|
behaves as the regular Python.
|
2019-05-02 15:14:06 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
Environments variables and command line arguments are used to configure
|
|
|
|
Python, whereas global configuration variables are ignored.
|
2019-05-02 15:14:06 -04:00
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
This function enables C locale coercion (:pep:`538`) and UTF-8 Mode (PEP
|
2019-05-20 07:11:42 -04:00
|
|
|
540) depending on the LC_CTYPE locale, ``PYTHONUTF8`` and
|
|
|
|
``PYTHONCOERCECLOCALE`` environment variables.
|
|
|
|
|
|
|
|
Example of customized Python always running in isolated mode::
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2019-05-24 15:21:05 -04:00
|
|
|
PyStatus status;
|
2019-05-20 07:11:42 -04:00
|
|
|
|
2019-10-01 06:19:09 -04:00
|
|
|
PyConfig config;
|
|
|
|
PyConfig_InitPythonConfig(&config);
|
2019-05-20 07:11:42 -04:00
|
|
|
|
|
|
|
config.isolated = 1;
|
|
|
|
|
|
|
|
/* Decode command line arguments.
|
|
|
|
Implicitly preinitialize Python (in isolated mode). */
|
2019-05-24 15:21:05 -04:00
|
|
|
status = PyConfig_SetBytesArgv(&config, argc, argv);
|
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-20 07:11:42 -04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
2019-05-20 07:11:42 -04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
PyConfig_Clear(&config);
|
|
|
|
|
|
|
|
return Py_RunMain();
|
|
|
|
|
|
|
|
fail:
|
|
|
|
PyConfig_Clear(&config);
|
2019-05-24 15:21:05 -04:00
|
|
|
if (PyStatus_IsExit(status)) {
|
|
|
|
return status.exitcode;
|
2019-05-20 07:11:42 -04:00
|
|
|
}
|
2019-05-24 15:21:05 -04:00
|
|
|
/* Display the error message and exit the process with
|
|
|
|
non-zero exit code */
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-20 07:11:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
This example is a basic implementation of the "System Python Executable"
|
2022-01-21 06:03:51 -05:00
|
|
|
discussed in :pep:`432`.
|
2019-05-02 15:14:06 -04:00
|
|
|
|
|
|
|
|
2019-05-15 12:50:11 -04:00
|
|
|
Path Configuration
|
|
|
|
------------------
|
2019-05-02 15:14:06 -04:00
|
|
|
|
2019-05-15 12:50:11 -04:00
|
|
|
``PyConfig`` contains multiple fields for the path configuration:
|
2019-05-02 15:14:06 -04:00
|
|
|
|
2019-05-15 12:50:11 -04:00
|
|
|
* Path configuration input fields:
|
2019-05-02 15:14:06 -04:00
|
|
|
|
2019-05-15 12:50:11 -04:00
|
|
|
* ``home``
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``pythonpath_env``
|
2019-05-15 17:54:05 -04:00
|
|
|
* ``pathconfig_warnings``
|
2019-05-02 15:14:06 -04:00
|
|
|
|
2019-05-15 12:50:11 -04:00
|
|
|
* Path configuration output fields:
|
|
|
|
|
|
|
|
* ``exec_prefix``
|
|
|
|
* ``executable``
|
|
|
|
* ``prefix``
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``module_search_paths_set``, ``module_search_paths``
|
2019-05-15 17:54:05 -04:00
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
If at least one "output field" is not set, Python computes the path
|
|
|
|
configuration to fill unset fields. If ``module_search_paths_set`` is
|
2019-08-11 11:53:20 -04:00
|
|
|
equal to 0, ``module_search_paths`` is overridden and
|
2019-05-24 15:21:05 -04:00
|
|
|
``module_search_paths_set`` is set to 1.
|
|
|
|
|
2019-05-15 12:50:11 -04:00
|
|
|
It is possible to completely ignore the function computing the default
|
|
|
|
path configuration by setting explicitly all path configuration output
|
2019-05-25 05:45:42 -04:00
|
|
|
fields listed above. A string is considered as set even if it is non-empty.
|
|
|
|
``module_search_paths`` is considered as set if
|
2019-05-20 07:11:42 -04:00
|
|
|
``module_search_paths_set`` is set to 1. In this case, path
|
2019-05-15 12:50:11 -04:00
|
|
|
configuration input fields are ignored as well.
|
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
Set ``pathconfig_warnings`` to 0 to suppress warnings when computing the
|
|
|
|
path configuration (Unix only, Windows does not log any warning).
|
|
|
|
|
2019-05-15 12:50:11 -04:00
|
|
|
If ``base_prefix`` or ``base_exec_prefix`` fields are not set, they
|
|
|
|
inherit their value from ``prefix`` and ``exec_prefix`` respectively.
|
2019-05-02 15:14:06 -04:00
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
``Py_RunMain()`` and ``Py_Main()`` modify ``sys.path``:
|
|
|
|
|
|
|
|
* If ``run_filename`` is set and is a directory which contains a
|
|
|
|
``__main__.py`` script, prepend ``run_filename`` to ``sys.path``.
|
|
|
|
* If ``isolated`` is zero:
|
|
|
|
|
|
|
|
* If ``run_module`` is set, prepend the current directory to
|
|
|
|
``sys.path``. Do nothing if the current directory cannot be read.
|
|
|
|
* If ``run_filename`` is set, prepends the directory of the filename
|
|
|
|
to ``sys.path``.
|
|
|
|
* Otherwise, prepends an empty string to ``sys.path``.
|
|
|
|
|
2019-05-15 12:50:11 -04:00
|
|
|
If ``site_import`` is non-zero, ``sys.path`` can be modified by the
|
2019-06-02 18:23:46 -04:00
|
|
|
``site`` module. If ``user_site_directory`` is non-zero and the user's
|
2019-05-24 15:21:05 -04:00
|
|
|
site-package directory exists, the ``site`` module appends the user's
|
|
|
|
site-package directory to ``sys.path``.
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
See also `Configuration Files`_ used by the path configuration.
|
2019-05-15 12:50:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
Py_BytesMain()
|
|
|
|
--------------
|
2019-03-27 19:44:33 -04:00
|
|
|
|
|
|
|
Python 3.7 provides a high-level ``Py_Main()`` function which requires
|
|
|
|
to pass command line arguments as ``wchar_t*`` strings. It is
|
|
|
|
non-trivial to use the correct encoding to decode bytes. Python has its
|
|
|
|
own set of issues with C locale coercion and UTF-8 Mode.
|
|
|
|
|
2019-05-15 12:50:11 -04:00
|
|
|
This PEP adds a new ``Py_BytesMain()`` function which takes command line
|
2019-03-27 19:44:33 -04:00
|
|
|
arguments as bytes::
|
|
|
|
|
2019-05-15 12:50:11 -04:00
|
|
|
int Py_BytesMain(int argc, char **argv)
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-03-29 18:29:19 -04:00
|
|
|
Py_RunMain()
|
|
|
|
------------
|
|
|
|
|
|
|
|
The new ``Py_RunMain()`` function executes the command
|
|
|
|
(``PyConfig.run_command``), the script (``PyConfig.run_filename``) or
|
|
|
|
the module (``PyConfig.run_module``) specified on the command line or in
|
|
|
|
the configuration, and then finalizes Python. It returns an exit status
|
2019-05-20 07:11:42 -04:00
|
|
|
that can be passed to the ``exit()`` function. ::
|
2019-03-29 18:29:19 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
int Py_RunMain(void);
|
2019-03-29 18:29:19 -04:00
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
See `Python Configuration`_ for an example of customized Python always
|
|
|
|
running in isolated mode using ``Py_RunMain()``.
|
2019-03-27 19:44:33 -04:00
|
|
|
|
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
Multi-Phase Initialization Private Provisional API
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
This section is a private provisional API introducing multi-phase
|
2022-01-21 06:03:51 -05:00
|
|
|
initialization, the core feature of the :pep:`432`:
|
2019-05-24 15:21:05 -04:00
|
|
|
|
|
|
|
* "Core" initialization phase, "bare minimum Python":
|
|
|
|
|
|
|
|
* Builtin types;
|
|
|
|
* Builtin exceptions;
|
|
|
|
* Builtin and frozen modules;
|
|
|
|
* The ``sys`` module is only partially initialized
|
|
|
|
(ex: ``sys.path`` doesn't exist yet);
|
|
|
|
|
|
|
|
* "Main" initialization phase, Python is fully initialized:
|
|
|
|
|
|
|
|
* Install and configure ``importlib``;
|
|
|
|
* Apply the `Path Configuration`_;
|
|
|
|
* Install signal handlers;
|
|
|
|
* Finish ``sys`` module initialization (ex: create ``sys.stdout`` and
|
|
|
|
``sys.path``);
|
|
|
|
* Enable optional features like ``faulthandler`` and ``tracemalloc``;
|
|
|
|
* Import the ``site`` module;
|
|
|
|
* etc.
|
|
|
|
|
|
|
|
Private provisional API:
|
|
|
|
|
|
|
|
* ``PyConfig._init_main``: if set to 0, ``Py_InitializeFromConfig()``
|
|
|
|
stops at the "Core" initialization phase.
|
|
|
|
* ``PyStatus _Py_InitializeMain(void)``: move to the "Main"
|
|
|
|
initialization phase, finish the Python initialization.
|
|
|
|
|
|
|
|
No module is imported during the "Core" phase and the ``importlib``
|
|
|
|
module is not configured: the `Path Configuration`_ is only applied
|
2019-05-25 05:45:42 -04:00
|
|
|
during the "Main" phase. It may allow to customize Python in Python to
|
2019-05-24 15:21:05 -04:00
|
|
|
override or tune the `Path Configuration`_, maybe install a custom
|
|
|
|
sys.meta_path importer or an import hook, etc.
|
|
|
|
|
|
|
|
It may become possible to compute the `Path Configuration`_ in Python,
|
|
|
|
after the Core phase and before the Main phase, which is one of the PEP
|
|
|
|
432 motivation.
|
|
|
|
|
|
|
|
The "Core" phase is not properly defined: what should be and what should
|
|
|
|
not be available at this phase is not specified yet. The API is marked
|
|
|
|
as private and provisional: the API can be modified or even be removed
|
2019-05-25 05:45:42 -04:00
|
|
|
anytime until a proper public API is designed.
|
2019-05-24 15:21:05 -04:00
|
|
|
|
|
|
|
Example running Python code between "Core" and "Main" initialization
|
|
|
|
phases::
|
|
|
|
|
|
|
|
void init_python(void)
|
|
|
|
{
|
|
|
|
PyStatus status;
|
|
|
|
|
2019-10-01 06:19:09 -04:00
|
|
|
PyConfig config;
|
|
|
|
PyConfig_InitPythonConfig(&config);
|
2019-05-24 15:21:05 -04:00
|
|
|
|
2019-05-25 05:45:42 -04:00
|
|
|
config._init_main = 0;
|
|
|
|
|
|
|
|
/* ... customize 'config' configuration ... */
|
2019-05-24 15:21:05 -04:00
|
|
|
|
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
PyConfig_Clear(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use sys.stderr because sys.stdout is only created
|
|
|
|
by _Py_InitializeMain() */
|
|
|
|
int res = PyRun_SimpleString(
|
|
|
|
"import sys; "
|
|
|
|
"print('Run Python code before _Py_InitializeMain', "
|
|
|
|
"file=sys.stderr)");
|
|
|
|
if (res < 0) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ... put more configuration code here ... */
|
|
|
|
|
|
|
|
status = _Py_InitializeMain();
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-27 19:44:33 -04:00
|
|
|
Backwards Compatibility
|
|
|
|
=======================
|
|
|
|
|
|
|
|
This PEP only adds a new API: it leaves the existing API unchanged and
|
|
|
|
has no impact on the backwards compatibility.
|
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
The Python 3.7 ``Py_Initialize()`` function now disable the C locale
|
2022-01-21 06:03:51 -05:00
|
|
|
coercion (:pep:`538`) and the UTF-8 Mode (:pep:`540`) by default to prevent
|
2019-05-20 07:11:42 -04:00
|
|
|
mojibake. The new API using the `Python Configuration`_ is needed to
|
|
|
|
enable them automatically.
|
|
|
|
|
|
|
|
|
|
|
|
Annexes
|
|
|
|
=======
|
|
|
|
|
|
|
|
Comparison of Python and Isolated Configurations
|
|
|
|
------------------------------------------------
|
|
|
|
|
|
|
|
Differences between ``PyPreConfig_InitPythonConfig()``
|
|
|
|
and ``PyPreConfig_InitIsolatedConfig()``:
|
|
|
|
|
|
|
|
=============================== ======= ========
|
|
|
|
PyPreConfig Python Isolated
|
|
|
|
=============================== ======= ========
|
|
|
|
``coerce_c_locale_warn`` -1 0
|
|
|
|
``coerce_c_locale`` -1 0
|
|
|
|
``configure_locale`` **1** 0
|
|
|
|
``dev_mode`` -1 0
|
2019-05-24 15:21:05 -04:00
|
|
|
``isolated`` 0 **1**
|
2019-05-20 07:11:42 -04:00
|
|
|
``legacy_windows_fs_encoding`` -1 0
|
2019-05-24 15:21:05 -04:00
|
|
|
``use_environment`` 0 0
|
2019-05-20 07:11:42 -04:00
|
|
|
``parse_argv`` **1** 0
|
|
|
|
``utf8_mode`` -1 0
|
|
|
|
=============================== ======= ========
|
|
|
|
|
|
|
|
Differences between ``PyConfig_InitPythonConfig()``
|
|
|
|
and ``PyConfig_InitIsolatedConfig()``:
|
|
|
|
|
|
|
|
=============================== ======= ========
|
|
|
|
PyConfig Python Isolated
|
|
|
|
=============================== ======= ========
|
|
|
|
``configure_c_stdio`` **1** 0
|
|
|
|
``install_signal_handlers`` **1** 0
|
|
|
|
``isolated`` 0 **1**
|
|
|
|
``parse_argv`` **1** 0
|
|
|
|
``pathconfig_warnings`` **1** 0
|
|
|
|
``use_environment`` **1** 0
|
|
|
|
``user_site_directory`` **1** 0
|
|
|
|
=============================== ======= ========
|
2019-03-27 19:44:33 -04:00
|
|
|
|
|
|
|
Priority and Rules
|
|
|
|
------------------
|
|
|
|
|
|
|
|
Priority of configuration parameters, highest to lowest:
|
|
|
|
|
|
|
|
* ``PyConfig``
|
|
|
|
* ``PyPreConfig``
|
|
|
|
* Configuration files
|
|
|
|
* Command line options
|
|
|
|
* Environment variables
|
|
|
|
* Global configuration variables
|
|
|
|
|
|
|
|
Priority of warning options, highest to lowest:
|
|
|
|
|
|
|
|
* ``PyConfig.warnoptions``
|
2019-09-29 20:18:59 -04:00
|
|
|
* ``PySys_AddWarnOption()``
|
|
|
|
* ``PyConfig.bytes_warning`` (add ``"error::BytesWarning"`` filter if
|
|
|
|
greater than 1, add ``"default::BytesWarning`` filter if equals to 1)
|
|
|
|
* ``-W opt`` command line argument
|
|
|
|
* ``PYTHONWARNINGS`` environment variable
|
|
|
|
* ``PyConfig.dev_mode`` (add ``"default"`` filter)
|
2019-03-27 19:44:33 -04:00
|
|
|
|
2019-05-15 12:50:11 -04:00
|
|
|
Rules on ``PyConfig`` parameters:
|
2019-03-27 19:44:33 -04:00
|
|
|
|
|
|
|
* If ``isolated`` is non-zero, ``use_environment`` and
|
2019-05-15 12:50:11 -04:00
|
|
|
``user_site_directory`` are set to 0.
|
2019-03-27 19:44:33 -04:00
|
|
|
* If ``dev_mode`` is non-zero, ``allocator`` is set to ``"debug"``,
|
|
|
|
``faulthandler`` is set to 1, and ``"default"`` filter is added to
|
2019-05-15 12:50:11 -04:00
|
|
|
``warnoptions``. But the ``PYTHONMALLOC`` environment variable has the
|
|
|
|
priority over ``dev_mode`` to set the memory allocator.
|
|
|
|
* If ``base_prefix`` is not set, it inherits ``prefix`` value.
|
|
|
|
* If ``base_exec_prefix`` is not set, it inherits ``exec_prefix`` value.
|
|
|
|
* If the ``python._pth`` configuration file is present, ``isolated`` is
|
2019-05-15 17:54:05 -04:00
|
|
|
set to 1 and ``site_import`` is set to 0; but ``site_import`` is set
|
|
|
|
to 1 if ``python._pth`` contains ``import site``.
|
2019-05-15 12:50:11 -04:00
|
|
|
|
|
|
|
Rules on ``PyConfig`` and ``PyPreConfig`` parameters:
|
|
|
|
|
|
|
|
* If ``PyPreConfig.legacy_windows_fs_encoding`` is non-zero,
|
2019-05-20 07:11:42 -04:00
|
|
|
set ``PyPreConfig.utf8_mode`` to 0, set
|
|
|
|
``PyConfig.filesystem_encoding`` to ``mbcs``, and set
|
|
|
|
``PyConfig.filesystem_errors`` to ``replace``.
|
2019-03-27 19:44:33 -04:00
|
|
|
|
|
|
|
Configuration Files
|
|
|
|
-------------------
|
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
Python configuration files used by the `Path Configuration`_:
|
2019-03-27 19:44:33 -04:00
|
|
|
|
|
|
|
* ``pyvenv.cfg``
|
|
|
|
* ``python._pth`` (Windows only)
|
|
|
|
* ``pybuilddir.txt`` (Unix only)
|
|
|
|
|
|
|
|
Global Configuration Variables
|
|
|
|
------------------------------
|
|
|
|
|
|
|
|
Global configuration variables mapped to ``PyPreConfig`` fields:
|
|
|
|
|
|
|
|
======================================== ================================
|
|
|
|
Variable Field
|
|
|
|
======================================== ================================
|
2019-05-15 12:50:11 -04:00
|
|
|
``Py_IgnoreEnvironmentFlag`` ``use_environment`` (NOT)
|
|
|
|
``Py_IsolatedFlag`` ``isolated``
|
2019-03-27 19:44:33 -04:00
|
|
|
``Py_LegacyWindowsFSEncodingFlag`` ``legacy_windows_fs_encoding``
|
|
|
|
``Py_UTF8Mode`` ``utf8_mode``
|
|
|
|
======================================== ================================
|
|
|
|
|
2019-06-25 00:58:50 -04:00
|
|
|
(NOT) means that the ``PyPreConfig`` value is the opposite of the global
|
2019-05-20 07:11:42 -04:00
|
|
|
configuration variable value. ``Py_LegacyWindowsFSEncodingFlag`` is only
|
|
|
|
available on Windows.
|
2019-05-15 17:54:05 -04:00
|
|
|
|
2019-03-27 19:44:33 -04:00
|
|
|
Global configuration variables mapped to ``PyConfig`` fields:
|
|
|
|
|
|
|
|
======================================== ================================
|
|
|
|
Variable Field
|
|
|
|
======================================== ================================
|
|
|
|
``Py_BytesWarningFlag`` ``bytes_warning``
|
|
|
|
``Py_DebugFlag`` ``parser_debug``
|
2019-05-15 12:50:11 -04:00
|
|
|
``Py_DontWriteBytecodeFlag`` ``write_bytecode`` (NOT)
|
2019-03-27 19:44:33 -04:00
|
|
|
``Py_FileSystemDefaultEncodeErrors`` ``filesystem_errors``
|
|
|
|
``Py_FileSystemDefaultEncoding`` ``filesystem_encoding``
|
2019-05-15 12:50:11 -04:00
|
|
|
``Py_FrozenFlag`` ``pathconfig_warnings`` (NOT)
|
2019-03-27 19:44:33 -04:00
|
|
|
``Py_HasFileSystemDefaultEncoding`` ``filesystem_encoding``
|
|
|
|
``Py_HashRandomizationFlag`` ``use_hash_seed``, ``hash_seed``
|
2019-05-15 12:50:11 -04:00
|
|
|
``Py_IgnoreEnvironmentFlag`` ``use_environment`` (NOT)
|
2019-03-27 19:44:33 -04:00
|
|
|
``Py_InspectFlag`` ``inspect``
|
|
|
|
``Py_InteractiveFlag`` ``interactive``
|
|
|
|
``Py_IsolatedFlag`` ``isolated``
|
|
|
|
``Py_LegacyWindowsStdioFlag`` ``legacy_windows_stdio``
|
2019-05-15 12:50:11 -04:00
|
|
|
``Py_NoSiteFlag`` ``site_import`` (NOT)
|
|
|
|
``Py_NoUserSiteDirectory`` ``user_site_directory`` (NOT)
|
2019-03-27 19:44:33 -04:00
|
|
|
``Py_OptimizeFlag`` ``optimization_level``
|
|
|
|
``Py_QuietFlag`` ``quiet``
|
2019-05-15 12:50:11 -04:00
|
|
|
``Py_UnbufferedStdioFlag`` ``buffered_stdio`` (NOT)
|
2019-03-27 19:44:33 -04:00
|
|
|
``Py_VerboseFlag`` ``verbose``
|
|
|
|
``_Py_HasFileSystemDefaultEncodeErrors`` ``filesystem_errors``
|
|
|
|
======================================== ================================
|
|
|
|
|
2019-06-25 00:58:50 -04:00
|
|
|
(NOT) means that the ``PyConfig`` value is the opposite of the global
|
2019-05-20 07:11:42 -04:00
|
|
|
configuration variable value. ``Py_LegacyWindowsStdioFlag`` is only
|
|
|
|
available on Windows.
|
2019-05-15 17:54:05 -04:00
|
|
|
|
2019-03-27 19:44:33 -04:00
|
|
|
|
|
|
|
Command Line Arguments
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
Usage::
|
|
|
|
|
|
|
|
python3 [options]
|
|
|
|
python3 [options] -c COMMAND
|
|
|
|
python3 [options] -m MODULE
|
|
|
|
python3 [options] SCRIPT
|
|
|
|
|
|
|
|
|
2019-05-15 12:50:11 -04:00
|
|
|
Command line options mapped to pseudo-action on ``PyPreConfig`` fields:
|
2019-05-02 15:14:06 -04:00
|
|
|
|
|
|
|
================================ ================================
|
2019-05-15 12:50:11 -04:00
|
|
|
Option ``PyConfig`` field
|
2019-05-02 15:14:06 -04:00
|
|
|
================================ ================================
|
2019-05-15 12:50:11 -04:00
|
|
|
``-E`` ``use_environment = 0``
|
|
|
|
``-I`` ``isolated = 1``
|
2019-05-02 15:14:06 -04:00
|
|
|
``-X dev`` ``dev_mode = 1``
|
2019-05-15 12:50:11 -04:00
|
|
|
``-X utf8`` ``utf8_mode = 1``
|
|
|
|
``-X utf8=VALUE`` ``utf8_mode = VALUE``
|
2019-05-02 15:14:06 -04:00
|
|
|
================================ ================================
|
|
|
|
|
2019-03-27 19:44:33 -04:00
|
|
|
Command line options mapped to pseudo-action on ``PyConfig`` fields:
|
|
|
|
|
|
|
|
================================ ================================
|
|
|
|
Option ``PyConfig`` field
|
|
|
|
================================ ================================
|
|
|
|
``-b`` ``bytes_warning++``
|
|
|
|
``-B`` ``write_bytecode = 0``
|
2019-05-02 16:45:44 -04:00
|
|
|
``-c COMMAND`` ``run_command = COMMAND``
|
2019-05-25 05:45:42 -04:00
|
|
|
``--check-hash-based-pycs=MODE`` ``check_hash_pycs_mode = MODE``
|
2019-03-27 19:44:33 -04:00
|
|
|
``-d`` ``parser_debug++``
|
|
|
|
``-E`` ``use_environment = 0``
|
|
|
|
``-i`` ``inspect++`` and ``interactive++``
|
|
|
|
``-I`` ``isolated = 1``
|
|
|
|
``-m MODULE`` ``run_module = MODULE``
|
|
|
|
``-O`` ``optimization_level++``
|
|
|
|
``-q`` ``quiet++``
|
|
|
|
``-R`` ``use_hash_seed = 0``
|
|
|
|
``-s`` ``user_site_directory = 0``
|
|
|
|
``-S`` ``site_import``
|
|
|
|
``-t`` ignored (kept for backwards compatibility)
|
|
|
|
``-u`` ``buffered_stdio = 0``
|
|
|
|
``-v`` ``verbose++``
|
|
|
|
``-W WARNING`` add ``WARNING`` to ``warnoptions``
|
|
|
|
``-x`` ``skip_source_first_line = 1``
|
2019-05-15 17:54:05 -04:00
|
|
|
``-X OPTION`` add ``OPTION`` to ``xoptions``
|
2019-05-15 12:50:11 -04:00
|
|
|
================================ ================================
|
|
|
|
|
2019-05-15 17:54:05 -04:00
|
|
|
``-h``, ``-?`` and ``-V`` options are handled without ``PyConfig``.
|
2019-05-15 12:50:11 -04:00
|
|
|
|
|
|
|
-X Options
|
|
|
|
----------
|
|
|
|
|
|
|
|
-X options mapped to pseudo-action on ``PyConfig`` fields:
|
|
|
|
|
|
|
|
================================ ================================
|
|
|
|
Option ``PyConfig`` field
|
|
|
|
================================ ================================
|
2019-05-02 15:14:06 -04:00
|
|
|
``-X dev`` ``dev_mode = 1``
|
|
|
|
``-X faulthandler`` ``faulthandler = 1``
|
|
|
|
``-X importtime`` ``import_time = 1``
|
|
|
|
``-X pycache_prefix=PREFIX`` ``pycache_prefix = PREFIX``
|
2019-05-15 12:50:11 -04:00
|
|
|
``-X showalloccount`` ``show_alloc_count = 1``
|
|
|
|
``-X showrefcount`` ``show_ref_count = 1``
|
2019-05-02 15:14:06 -04:00
|
|
|
``-X tracemalloc=N`` ``tracemalloc = N``
|
2019-03-27 19:44:33 -04:00
|
|
|
================================ ================================
|
|
|
|
|
|
|
|
Environment Variables
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
Environment variables mapped to ``PyPreConfig`` fields:
|
|
|
|
|
|
|
|
================================= =============================================
|
|
|
|
Variable ``PyPreConfig`` field
|
|
|
|
================================= =============================================
|
|
|
|
``PYTHONCOERCECLOCALE`` ``coerce_c_locale``, ``coerce_c_locale_warn``
|
|
|
|
``PYTHONDEVMODE`` ``dev_mode``
|
|
|
|
``PYTHONLEGACYWINDOWSFSENCODING`` ``legacy_windows_fs_encoding``
|
|
|
|
``PYTHONMALLOC`` ``allocator``
|
|
|
|
``PYTHONUTF8`` ``utf8_mode``
|
|
|
|
================================= =============================================
|
|
|
|
|
|
|
|
Environment variables mapped to ``PyConfig`` fields:
|
|
|
|
|
|
|
|
================================= ====================================
|
|
|
|
Variable ``PyConfig`` field
|
|
|
|
================================= ====================================
|
|
|
|
``PYTHONDEBUG`` ``parser_debug``
|
|
|
|
``PYTHONDEVMODE`` ``dev_mode``
|
|
|
|
``PYTHONDONTWRITEBYTECODE`` ``write_bytecode``
|
|
|
|
``PYTHONDUMPREFS`` ``dump_refs``
|
|
|
|
``PYTHONEXECUTABLE`` ``program_name``
|
|
|
|
``PYTHONFAULTHANDLER`` ``faulthandler``
|
|
|
|
``PYTHONHASHSEED`` ``use_hash_seed``, ``hash_seed``
|
|
|
|
``PYTHONHOME`` ``home``
|
|
|
|
``PYTHONINSPECT`` ``inspect``
|
|
|
|
``PYTHONIOENCODING`` ``stdio_encoding``, ``stdio_errors``
|
|
|
|
``PYTHONLEGACYWINDOWSSTDIO`` ``legacy_windows_stdio``
|
|
|
|
``PYTHONMALLOCSTATS`` ``malloc_stats``
|
|
|
|
``PYTHONNOUSERSITE`` ``user_site_directory``
|
|
|
|
``PYTHONOPTIMIZE`` ``optimization_level``
|
2019-05-20 07:11:42 -04:00
|
|
|
``PYTHONPATH`` ``pythonpath_env``
|
2019-03-27 19:44:33 -04:00
|
|
|
``PYTHONPROFILEIMPORTTIME`` ``import_time``
|
|
|
|
``PYTHONPYCACHEPREFIX,`` ``pycache_prefix``
|
|
|
|
``PYTHONTRACEMALLOC`` ``tracemalloc``
|
|
|
|
``PYTHONUNBUFFERED`` ``buffered_stdio``
|
|
|
|
``PYTHONVERBOSE`` ``verbose``
|
|
|
|
``PYTHONWARNINGS`` ``warnoptions``
|
|
|
|
================================= ====================================
|
|
|
|
|
|
|
|
``PYTHONLEGACYWINDOWSFSENCODING`` and ``PYTHONLEGACYWINDOWSSTDIO`` are
|
|
|
|
specific to Windows.
|
|
|
|
|
|
|
|
|
2019-06-25 00:58:50 -04:00
|
|
|
Default Python Configuration
|
2019-05-20 07:11:42 -04:00
|
|
|
-----------------------------
|
|
|
|
|
|
|
|
``PyPreConfig_InitPythonConfig()``:
|
|
|
|
|
|
|
|
* ``allocator`` = ``PYMEM_ALLOCATOR_NOT_SET``
|
|
|
|
* ``coerce_c_locale_warn`` = -1
|
|
|
|
* ``coerce_c_locale`` = -1
|
|
|
|
* ``configure_locale`` = 1
|
|
|
|
* ``dev_mode`` = -1
|
2019-05-24 15:21:05 -04:00
|
|
|
* ``isolated`` = 0
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``legacy_windows_fs_encoding`` = -1
|
2019-05-24 15:21:05 -04:00
|
|
|
* ``use_environment`` = 1
|
2019-05-20 07:11:42 -04:00
|
|
|
* ``utf8_mode`` = -1
|
|
|
|
|
|
|
|
``PyConfig_InitPythonConfig()``:
|
|
|
|
|
|
|
|
* ``argv`` = []
|
|
|
|
* ``base_exec_prefix`` = ``NULL``
|
|
|
|
* ``base_prefix`` = ``NULL``
|
|
|
|
* ``buffered_stdio`` = 1
|
|
|
|
* ``bytes_warning`` = 0
|
|
|
|
* ``check_hash_pycs_mode`` = ``NULL``
|
|
|
|
* ``configure_c_stdio`` = 1
|
|
|
|
* ``dev_mode`` = 0
|
|
|
|
* ``dump_refs`` = 0
|
|
|
|
* ``exec_prefix`` = ``NULL``
|
|
|
|
* ``executable`` = ``NULL``
|
|
|
|
* ``faulthandler`` = 0
|
|
|
|
* ``filesystem_encoding`` = ``NULL``
|
|
|
|
* ``filesystem_errors`` = ``NULL``
|
|
|
|
* ``hash_seed`` = 0
|
|
|
|
* ``home`` = ``NULL``
|
|
|
|
* ``import_time`` = 0
|
|
|
|
* ``inspect`` = 0
|
|
|
|
* ``install_signal_handlers`` = 1
|
|
|
|
* ``interactive`` = 0
|
|
|
|
* ``isolated`` = 0
|
|
|
|
* ``malloc_stats`` = 0
|
|
|
|
* ``module_search_path_env`` = ``NULL``
|
|
|
|
* ``module_search_paths`` = []
|
|
|
|
* ``optimization_level`` = 0
|
|
|
|
* ``parse_argv`` = 1
|
|
|
|
* ``parser_debug`` = 0
|
|
|
|
* ``pathconfig_warnings`` = 1
|
|
|
|
* ``prefix`` = ``NULL``
|
|
|
|
* ``program_name`` = ``NULL``
|
|
|
|
* ``pycache_prefix`` = ``NULL``
|
|
|
|
* ``quiet`` = 0
|
|
|
|
* ``run_command`` = ``NULL``
|
|
|
|
* ``run_filename`` = ``NULL``
|
|
|
|
* ``run_module`` = ``NULL``
|
|
|
|
* ``show_alloc_count`` = 0
|
|
|
|
* ``show_ref_count`` = 0
|
|
|
|
* ``site_import`` = 1
|
|
|
|
* ``skip_source_first_line`` = 0
|
|
|
|
* ``stdio_encoding`` = ``NULL``
|
|
|
|
* ``stdio_errors`` = ``NULL``
|
|
|
|
* ``tracemalloc`` = 0
|
|
|
|
* ``use_environment`` = 1
|
|
|
|
* ``use_hash_seed`` = 0
|
|
|
|
* ``user_site_directory`` = 1
|
|
|
|
* ``verbose`` = 0
|
|
|
|
* ``warnoptions`` = []
|
|
|
|
* ``write_bytecode`` = 1
|
|
|
|
* ``xoptions`` = []
|
|
|
|
* ``_init_main`` = 1
|
|
|
|
* ``_install_importlib`` = 1
|
|
|
|
|
|
|
|
|
2019-06-25 00:58:50 -04:00
|
|
|
Default Isolated Configuration
|
2019-05-20 07:11:42 -04:00
|
|
|
-------------------------------
|
|
|
|
|
|
|
|
``PyPreConfig_InitIsolatedConfig()``:
|
|
|
|
|
|
|
|
* ``allocator`` = ``PYMEM_ALLOCATOR_NOT_SET``
|
|
|
|
* ``coerce_c_locale_warn`` = 0
|
|
|
|
* ``coerce_c_locale`` = 0
|
|
|
|
* ``configure_locale`` = 0
|
|
|
|
* ``dev_mode`` = 0
|
|
|
|
* ``isolated`` = 1
|
|
|
|
* ``legacy_windows_fs_encoding`` = 0
|
|
|
|
* ``use_environment`` = 0
|
|
|
|
* ``utf8_mode`` = 0
|
|
|
|
|
|
|
|
``PyConfig_InitIsolatedConfig()``:
|
|
|
|
|
|
|
|
* ``argv`` = []
|
|
|
|
* ``base_exec_prefix`` = ``NULL``
|
|
|
|
* ``base_prefix`` = ``NULL``
|
|
|
|
* ``buffered_stdio`` = 1
|
|
|
|
* ``bytes_warning`` = 0
|
|
|
|
* ``check_hash_pycs_mode`` = ``NULL``
|
|
|
|
* ``configure_c_stdio`` = 0
|
|
|
|
* ``dev_mode`` = 0
|
|
|
|
* ``dump_refs`` = 0
|
|
|
|
* ``exec_prefix`` = ``NULL``
|
|
|
|
* ``executable`` = ``NULL``
|
|
|
|
* ``faulthandler`` = 0
|
|
|
|
* ``filesystem_encoding`` = ``NULL``
|
|
|
|
* ``filesystem_errors`` = ``NULL``
|
|
|
|
* ``hash_seed`` = 0
|
|
|
|
* ``home`` = ``NULL``
|
|
|
|
* ``import_time`` = 0
|
|
|
|
* ``inspect`` = 0
|
|
|
|
* ``install_signal_handlers`` = 0
|
|
|
|
* ``interactive`` = 0
|
|
|
|
* ``isolated`` = 1
|
|
|
|
* ``malloc_stats`` = 0
|
|
|
|
* ``module_search_path_env`` = ``NULL``
|
|
|
|
* ``module_search_paths`` = []
|
|
|
|
* ``optimization_level`` = 0
|
|
|
|
* ``parse_argv`` = 0
|
|
|
|
* ``parser_debug`` = 0
|
|
|
|
* ``pathconfig_warnings`` = 0
|
|
|
|
* ``prefix`` = ``NULL``
|
|
|
|
* ``program_name`` = ``NULL``
|
|
|
|
* ``pycache_prefix`` = ``NULL``
|
|
|
|
* ``quiet`` = 0
|
|
|
|
* ``run_command`` = ``NULL``
|
|
|
|
* ``run_filename`` = ``NULL``
|
|
|
|
* ``run_module`` = ``NULL``
|
|
|
|
* ``show_alloc_count`` = 0
|
|
|
|
* ``show_ref_count`` = 0
|
|
|
|
* ``site_import`` = 1
|
|
|
|
* ``skip_source_first_line`` = 0
|
|
|
|
* ``stdio_encoding`` = ``NULL``
|
|
|
|
* ``stdio_errors`` = ``NULL``
|
|
|
|
* ``tracemalloc`` = 0
|
|
|
|
* ``use_environment`` = 0
|
|
|
|
* ``use_hash_seed`` = 0
|
|
|
|
* ``user_site_directory`` = 0
|
|
|
|
* ``verbose`` = 0
|
|
|
|
* ``warnoptions`` = []
|
|
|
|
* ``write_bytecode`` = 1
|
|
|
|
* ``xoptions`` = []
|
|
|
|
* ``_init_main`` = 1
|
|
|
|
* ``_install_importlib`` = 1
|
|
|
|
|
|
|
|
|
|
|
|
Python 3.7 API
|
|
|
|
--------------
|
2019-03-27 19:44:33 -04:00
|
|
|
|
|
|
|
Python 3.7 has 4 functions in its C API to initialize and finalize
|
|
|
|
Python:
|
|
|
|
|
|
|
|
* ``Py_Initialize()``, ``Py_InitializeEx()``: initialize Python
|
|
|
|
* ``Py_Finalize()``, ``Py_FinalizeEx()``: finalize Python
|
|
|
|
|
2019-05-15 17:54:05 -04:00
|
|
|
Python 3.7 can be configured using `Global Configuration Variables`_,
|
|
|
|
`Environment Variables`_, and the following functions:
|
2019-03-27 19:44:33 -04:00
|
|
|
|
|
|
|
* ``PyImport_AppendInittab()``
|
|
|
|
* ``PyImport_ExtendInittab()``
|
|
|
|
* ``PyMem_SetAllocator()``
|
|
|
|
* ``PyMem_SetupDebugHooks()``
|
|
|
|
* ``PyObject_SetArenaAllocator()``
|
|
|
|
* ``Py_SetPath()``
|
|
|
|
* ``Py_SetProgramName()``
|
|
|
|
* ``Py_SetPythonHome()``
|
|
|
|
* ``Py_SetStandardStreamEncoding()``
|
|
|
|
* ``PySys_AddWarnOption()``
|
|
|
|
* ``PySys_AddXOption()``
|
|
|
|
* ``PySys_ResetWarnOptions()``
|
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
There is also a high-level ``Py_Main()`` function and
|
|
|
|
``PyImport_FrozenModules`` variable which can be overridden.
|
|
|
|
|
|
|
|
See `Initialization, Finalization, and Threads
|
|
|
|
<https://docs.python.org/dev/c-api/init.html>`_ documentation.
|
2019-03-27 19:44:33 -04:00
|
|
|
|
|
|
|
|
2019-05-15 12:50:11 -04:00
|
|
|
Python Issues
|
|
|
|
=============
|
|
|
|
|
|
|
|
Issues that will be fixed by this PEP, directly or indirectly:
|
|
|
|
|
|
|
|
* `bpo-1195571 <https://bugs.python.org/issue1195571>`_: "simple
|
|
|
|
callback system for Py_FatalError"
|
|
|
|
* `bpo-11320 <https://bugs.python.org/issue11320>`_:
|
|
|
|
"Usage of API method Py_SetPath causes errors in Py_Initialize()
|
2021-09-17 14:18:24 -04:00
|
|
|
(Posix only)"
|
2019-05-15 12:50:11 -04:00
|
|
|
* `bpo-13533 <https://bugs.python.org/issue13533>`_: "Would like
|
|
|
|
Py_Initialize to play friendly with host app"
|
|
|
|
* `bpo-14956 <https://bugs.python.org/issue14956>`_: "custom PYTHONPATH
|
|
|
|
may break apps embedding Python"
|
|
|
|
* `bpo-19983 <https://bugs.python.org/issue19983>`_: "When interrupted
|
|
|
|
during startup, Python should not call abort() but exit()"
|
|
|
|
* `bpo-22213 <https://bugs.python.org/issue22213>`_: "Make pyvenv style
|
2019-06-02 18:23:46 -04:00
|
|
|
virtual environments easier to configure when embedding Python".
|
2019-05-15 12:50:11 -04:00
|
|
|
* `bpo-29778 <https://bugs.python.org/issue29778>`_: "_Py_CheckPython3
|
|
|
|
uses uninitialized dllpath when embedder sets module path with
|
|
|
|
Py_SetPath"
|
|
|
|
* `bpo-30560 <https://bugs.python.org/issue30560>`_: "Add
|
|
|
|
Py_SetFatalErrorAbortFunc: Allow embedding program to handle fatal
|
|
|
|
errors".
|
|
|
|
* `bpo-31745 <https://bugs.python.org/issue31745>`_: "Overloading
|
|
|
|
"Py_GetPath" does not work"
|
|
|
|
* `bpo-32573 <https://bugs.python.org/issue32573>`_: "All sys attributes
|
|
|
|
(.argv, ...) should exist in embedded environments".
|
2019-06-02 18:23:46 -04:00
|
|
|
* `bpo-33135 <https://bugs.python.org/issue33135>`_: "Define field
|
|
|
|
prefixes for the various config structs". The PEP now defines well
|
|
|
|
how warnings options are handled.
|
2019-05-15 12:50:11 -04:00
|
|
|
* `bpo-34725 <https://bugs.python.org/issue34725>`_:
|
|
|
|
"Py_GetProgramFullPath() odd behaviour in Windows"
|
|
|
|
* `bpo-36204 <https://bugs.python.org/issue36204>`_: "Deprecate calling
|
|
|
|
Py_Main() after Py_Initialize()? Add Py_InitializeFromArgv()?"
|
|
|
|
|
|
|
|
Issues of the PEP implementation:
|
|
|
|
|
|
|
|
* `bpo-16961 <https://bugs.python.org/issue16961>`_: "No regression
|
|
|
|
tests for -E and individual environment vars"
|
|
|
|
* `bpo-20361 <https://bugs.python.org/issue20361>`_: "-W command line
|
|
|
|
options and PYTHONWARNINGS environmental variable should not override
|
|
|
|
-b / -bb command line options"
|
|
|
|
* `bpo-26122 <https://bugs.python.org/issue26122>`_: "Isolated mode
|
|
|
|
doesn't ignore PYTHONHASHSEED"
|
|
|
|
* `bpo-29818 <https://bugs.python.org/issue29818>`_:
|
|
|
|
"Py_SetStandardStreamEncoding leads to a memory error in debug mode"
|
|
|
|
* `bpo-31845 <https://bugs.python.org/issue31845>`_:
|
|
|
|
"PYTHONDONTWRITEBYTECODE and PYTHONOPTIMIZE have no effect"
|
|
|
|
* `bpo-32030 <https://bugs.python.org/issue32030>`_: "PEP 432: Rewrite
|
|
|
|
Py_Main()"
|
|
|
|
* `bpo-32124 <https://bugs.python.org/issue32124>`_: "Document functions
|
|
|
|
safe to be called before Py_Initialize()"
|
|
|
|
* `bpo-33042 <https://bugs.python.org/issue33042>`_: "New 3.7 startup
|
|
|
|
sequence crashes PyInstaller"
|
|
|
|
* `bpo-33932 <https://bugs.python.org/issue33932>`_: "Calling
|
|
|
|
Py_Initialize() twice now triggers a fatal error (Python 3.7)"
|
|
|
|
* `bpo-34008 <https://bugs.python.org/issue34008>`_: "Do we support
|
|
|
|
calling Py_Main() after Py_Initialize()?"
|
|
|
|
* `bpo-34170 <https://bugs.python.org/issue34170>`_: "Py_Initialize():
|
|
|
|
computing path configuration must not have side effect (PEP 432)"
|
|
|
|
* `bpo-34589 <https://bugs.python.org/issue34589>`_: "Py_Initialize()
|
|
|
|
and Py_Main() should not enable C locale coercion"
|
|
|
|
* `bpo-34639 <https://bugs.python.org/issue34639>`_:
|
|
|
|
"PYTHONCOERCECLOCALE is ignored when using -E or -I option"
|
|
|
|
* `bpo-36142 <https://bugs.python.org/issue36142>`_: "Add a new
|
|
|
|
_PyPreConfig step to Python initialization to setup memory allocator
|
|
|
|
and encodings"
|
|
|
|
* `bpo-36202 <https://bugs.python.org/issue36202>`_: "Calling
|
|
|
|
Py_DecodeLocale() before _PyPreConfig_Write() can produce mojibake"
|
|
|
|
* `bpo-36301 <https://bugs.python.org/issue36301>`_: "Add
|
|
|
|
_Py_PreInitialize() function"
|
|
|
|
* `bpo-36443 <https://bugs.python.org/issue36443>`_: "Disable
|
|
|
|
coerce_c_locale and utf8_mode by default in _PyPreConfig?"
|
|
|
|
* `bpo-36444 <https://bugs.python.org/issue36444>`_: "Python
|
|
|
|
initialization: remove _PyMainInterpreterConfig"
|
|
|
|
* `bpo-36471 <https://bugs.python.org/issue36471>`_: "PEP 432, PEP 587:
|
|
|
|
Add _Py_RunMain()"
|
|
|
|
* `bpo-36763 <https://bugs.python.org/issue36763>`_: "PEP 587: Rework
|
|
|
|
initialization API to prepare second version of the PEP"
|
|
|
|
* `bpo-36775 <https://bugs.python.org/issue36775>`_: "Rework filesystem
|
|
|
|
codec implementation"
|
|
|
|
* `bpo-36900 <https://bugs.python.org/issue36900>`_: "Use _PyCoreConfig
|
|
|
|
rather than global configuration variables"
|
|
|
|
|
|
|
|
Issues related to this PEP:
|
|
|
|
|
|
|
|
* `bpo-12598 <https://bugs.python.org/issue12598>`_: "Move sys variable
|
|
|
|
initialization from import.c to sysmodule.c"
|
|
|
|
* `bpo-15577 <https://bugs.python.org/issue15577>`_: "Real argc and argv
|
|
|
|
in embedded interpreter"
|
|
|
|
* `bpo-16202 <https://bugs.python.org/issue16202>`_: "sys.path[0]
|
|
|
|
security issues"
|
|
|
|
* `bpo-18309 <https://bugs.python.org/issue18309>`_: "Make python
|
|
|
|
slightly more relocatable"
|
2019-06-02 18:23:46 -04:00
|
|
|
* `bpo-22257 <https://bugs.python.org/issue22257>`_: "PEP 432: Redesign
|
|
|
|
the interpreter startup sequence"
|
2019-05-15 12:50:11 -04:00
|
|
|
* `bpo-25631 <https://bugs.python.org/issue25631>`_: "Segmentation fault
|
|
|
|
with invalid Unicode command-line arguments in embedded Python"
|
|
|
|
* `bpo-26007 <https://bugs.python.org/issue26007>`_: "Support embedding
|
|
|
|
the standard library in an executable"
|
|
|
|
* `bpo-31210 <https://bugs.python.org/issue31210>`_: "Can not import
|
|
|
|
modules if sys.prefix contains DELIM".
|
|
|
|
* `bpo-31349 <https://bugs.python.org/issue31349>`_: "Embedded
|
|
|
|
initialization ignores Py_SetProgramName()"
|
|
|
|
* `bpo-33919 <https://bugs.python.org/issue33919>`_: "Expose
|
|
|
|
_PyCoreConfig structure to Python"
|
|
|
|
* `bpo-35173 <https://bugs.python.org/issue35173>`_: "Re-use already
|
|
|
|
existing functionality to allow Python 2.7.x (both embedded and
|
|
|
|
standalone) to locate the module path according to the shared library"
|
|
|
|
|
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
Discussions
|
|
|
|
===========
|
|
|
|
|
|
|
|
* May 2019:
|
|
|
|
|
|
|
|
* `[Python-Dev] PEP 587 "Python Initialization Configuration" version 4
|
|
|
|
<https://mail.python.org/pipermail/python-dev/2019-May/157492.html>`_
|
|
|
|
* `[Python-Dev] RFC: PEP 587 "Python Initialization Configuration": 3rd version
|
|
|
|
<https://mail.python.org/pipermail/python-dev/2019-May/157435.html>`_
|
|
|
|
* `Study on applications embedding Python
|
|
|
|
<https://mail.python.org/pipermail/python-dev/2019-May/157385.html>`_
|
|
|
|
* `[Python-Dev] RFC: PEP 587 "Python Initialization Configuration":
|
|
|
|
2nd version
|
|
|
|
<https://mail.python.org/pipermail/python-dev/2019-May/157290.html>`_
|
|
|
|
|
|
|
|
* March 2019:
|
|
|
|
|
|
|
|
* `[Python-Dev] PEP 587: Python Initialization Configuration
|
|
|
|
<https://mail.python.org/pipermail/python-dev/2019-March/156892.html>`_
|
|
|
|
* `[Python-Dev] New Python Initialization API
|
|
|
|
<https://mail.python.org/pipermail/python-dev/2019-March/156884.html>`_
|
|
|
|
|
|
|
|
* February 2019:
|
|
|
|
|
|
|
|
* `Adding char* based APIs for Unix
|
|
|
|
<https://discuss.python.org/t/adding-char-based-apis-for-unix/916>`_
|
|
|
|
|
|
|
|
* July-August 2018:
|
|
|
|
|
|
|
|
* July: `[Python-Dev] New _Py_InitializeFromConfig() function (PEP 432)
|
|
|
|
<https://mail.python.org/pipermail/python-dev/2018-July/154882.html>`__
|
|
|
|
* August: `[Python-Dev] New _Py_InitializeFromConfig() function (PEP 432)
|
|
|
|
<https://mail.python.org/pipermail/python-dev/2018-August/154896.html>`__
|
|
|
|
|
2019-05-15 12:50:11 -04:00
|
|
|
Version History
|
|
|
|
===============
|
|
|
|
|
2019-05-24 15:21:05 -04:00
|
|
|
* Version 5:
|
|
|
|
|
|
|
|
* Rename ``PyInitError`` to ``PyStatus``
|
|
|
|
* Rename ``PyInitError_Failed()`` to ``PyStatus_Exception()``
|
|
|
|
* Rename ``Py_ExitInitError()`` to ``Py_ExitStatusException()``
|
|
|
|
* Add ``PyPreConfig._config_init`` private field.
|
|
|
|
* Fix Python Configuration default values: isolated=0
|
|
|
|
and use_environment=1, instead of -1.
|
|
|
|
* Add "Multi-Phase Initialization Private Provisional API"
|
|
|
|
and "Discussions" sections
|
|
|
|
|
2019-05-20 07:11:42 -04:00
|
|
|
* Version 4:
|
|
|
|
|
|
|
|
* Introduce "Python Configuration" and "Isolated Configuration" which
|
|
|
|
are well better defined. Replace all macros with functions.
|
|
|
|
* Replace ``PyPreConfig_INIT`` and ``PyConfig_INIT`` macros with
|
|
|
|
functions:
|
|
|
|
|
|
|
|
* ``PyPreConfig_InitIsolatedConfig()``, ``PyConfig_InitIsolatedConfig()``
|
|
|
|
* ``PyPreConfig_InitPythonConfig()``, ``PyConfig_InitPythonConfig()``
|
|
|
|
|
|
|
|
* ``PyPreConfig`` no longer uses dynamic memory, the ``allocator``
|
|
|
|
field type becomes an int, add ``configure_locale`` and
|
|
|
|
``parse_argv`` field.
|
|
|
|
* ``PyConfig``: rename ``module_search_path_env`` to
|
|
|
|
``pythonpath_env``, rename ``use_module_search_paths`` to
|
|
|
|
``module_search_paths_set``, remove ``program`` and ``dll_path``.
|
|
|
|
* Replace ``Py_INIT_xxx()`` macros with ``PyInitError_xxx()``
|
|
|
|
functions.
|
|
|
|
* Remove the "Constant PyConfig" section. Remove
|
|
|
|
``Py_InitializeFromArgs()`` and ``Py_InitializeFromBytesArgs()``
|
|
|
|
functions.
|
|
|
|
|
2019-05-15 12:50:11 -04:00
|
|
|
* Version 3:
|
|
|
|
|
2019-05-15 18:28:16 -04:00
|
|
|
* ``PyConfig``: Add ``configure_c_stdio`` and ``parse_argv``;
|
2019-05-15 12:50:11 -04:00
|
|
|
rename ``_frozen`` to ``pathconfig_warnings``.
|
2019-05-15 17:54:05 -04:00
|
|
|
* Rename functions using bytes strings and wide character strings. For
|
2019-05-15 18:28:16 -04:00
|
|
|
example, ``Py_PreInitializeFromWideArgs()`` becomes
|
|
|
|
``Py_PreInitializeFromArgs()``, and ``PyConfig_SetArgv()`` becomes
|
|
|
|
``PyConfig_SetBytesArgv()``.
|
2019-05-15 12:50:11 -04:00
|
|
|
* Add ``PyWideStringList_Insert()`` function.
|
|
|
|
* New "Path configuration", "Isolate Python", "Python Issues"
|
|
|
|
and "Version History" sections.
|
|
|
|
* ``PyConfig_SetString()`` and ``PyConfig_SetBytesString()`` now
|
|
|
|
requires the configuration as the first argument.
|
|
|
|
* Rename ``Py_UnixMain()`` to ``Py_BytesMain()``
|
|
|
|
|
|
|
|
* Version 2: Add ``PyConfig`` methods (ex: ``PyConfig_Read()``), add
|
|
|
|
``PyWideStringList_Append()``, rename ``PyWideCharList`` to
|
|
|
|
``PyWideStringList``.
|
|
|
|
* Version 1: Initial version.
|
|
|
|
|
2019-06-02 18:17:29 -04:00
|
|
|
Acceptance
|
|
|
|
==========
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
:pep:`587` was `accepted by Thomas Wouters on May 26, 2019
|
2019-06-02 18:17:29 -04:00
|
|
|
<https://mail.python.org/pipermail/python-dev/2019-May/157721.html>`_.
|
|
|
|
|
|
|
|
|
2019-03-27 19:44:33 -04:00
|
|
|
Copyright
|
|
|
|
=========
|
|
|
|
|
|
|
|
This document has been placed in the public domain.
|