mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
gh-129033: Remove _Py_InitializeMain() function (#129034)
Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>
This commit is contained in:
parent
c463270c73
commit
07c3518ffb
7 changed files with 15 additions and 153 deletions
|
@ -1946,89 +1946,13 @@ Py_GetArgcArgv()
|
|||
|
||||
See also :c:member:`PyConfig.orig_argv` member.
|
||||
|
||||
Delaying main module execution
|
||||
==============================
|
||||
|
||||
Multi-Phase Initialization Private Provisional API
|
||||
==================================================
|
||||
In some embedding use cases, it may be desirable to separate interpreter initialization
|
||||
from the execution of the main module.
|
||||
|
||||
This section is a private provisional API introducing multi-phase
|
||||
initialization, the core feature of :pep:`432`:
|
||||
|
||||
* "Core" initialization phase, "bare minimum Python":
|
||||
|
||||
* Builtin types;
|
||||
* Builtin exceptions;
|
||||
* Builtin and frozen modules;
|
||||
* The :mod:`sys` module is only partially initialized
|
||||
(ex: :data:`sys.path` doesn't exist yet).
|
||||
|
||||
* "Main" initialization phase, Python is fully initialized:
|
||||
|
||||
* Install and configure :mod:`importlib`;
|
||||
* Apply the :ref:`Path Configuration <init-path-config>`;
|
||||
* Install signal handlers;
|
||||
* Finish :mod:`sys` module initialization (ex: create :data:`sys.stdout`
|
||||
and :data:`sys.path`);
|
||||
* Enable optional features like :mod:`faulthandler` and :mod:`tracemalloc`;
|
||||
* Import the :mod:`site` module;
|
||||
* etc.
|
||||
|
||||
Private provisional API:
|
||||
|
||||
* :c:member:`PyConfig._init_main`: if set to ``0``,
|
||||
:c:func:`Py_InitializeFromConfig` stops at the "Core" initialization phase.
|
||||
|
||||
.. c:function:: 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 :ref:`Path Configuration <init-path-config>` is only
|
||||
applied during the "Main" phase. It may allow to customize Python in Python to
|
||||
override or tune the :ref:`Path Configuration <init-path-config>`, maybe
|
||||
install a custom :data:`sys.meta_path` importer or an import hook, etc.
|
||||
|
||||
It may become possible to calculate the :ref:`Path Configuration
|
||||
<init-path-config>` 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
|
||||
anytime until a proper public API is designed.
|
||||
|
||||
Example running Python code between "Core" and "Main" initialization
|
||||
phases::
|
||||
|
||||
void init_python(void)
|
||||
{
|
||||
PyStatus status;
|
||||
|
||||
PyConfig config;
|
||||
PyConfig_InitPythonConfig(&config);
|
||||
config._init_main = 0;
|
||||
|
||||
/* ... customize 'config' configuration ... */
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
This separation can be achieved by setting ``PyConfig.run_command`` to the empty
|
||||
string during initialization (to prevent the interpreter from dropping into the
|
||||
interactive prompt), and then subsequently executing the desired main module
|
||||
code using ``__main__.__dict__`` as the global namespace.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue