Skip to content

Commit 47a50c5

Browse files
committed
Defer _PyRuntime_Initialize to Py_InitializeFromConfig.
When I was working on my own copy of ``Py_Initialize()`` for my own embedded interpreter that: - manually sets ``sys.path`` (set to the base name of the exe since I story the python stlib in a zip file in it's win32 resource section) - Disables the default explicit ``import site`` (set to 0) - prevents the user site-packages folder from being added (set to 0), The way I did this was with a direct copy and paste of the code in ``Py_InitializeEx`` and changed a only what I needed to implement my own ``Py_Initialize`` that suited my own needs. I hated how I needed to define ``Py_BUILD_CORE_MODULE`` and include ``#include <internal/pycore_runtime.h>`` And then checked and saw that everything but the if check in ``Py_InitializeEx`` is inside of ``Py_InitializeFromConfig`` and that the if check could be replaced easily with ``Py_IsInitialized``. Because of that I submitted this change to remove the needless code duplication here since ``Py_InitializeFromConfig`` is used anyways. It also might increase performance very slightly as a result as well for free.
1 parent e8c91d9 commit 47a50c5

File tree

1 file changed

+2
-9
lines changed

1 file changed

+2
-9
lines changed

Python/pylifecycle.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,19 +1414,12 @@ Py_InitializeFromConfig(const PyConfig *config)
14141414
void
14151415
Py_InitializeEx(int install_sigs)
14161416
{
1417-
PyStatus status;
1418-
1419-
status = _PyRuntime_Initialize();
1420-
if (_PyStatus_EXCEPTION(status)) {
1421-
Py_ExitStatusException(status);
1422-
}
1423-
_PyRuntimeState *runtime = &_PyRuntime;
1424-
1425-
if (runtime->initialized) {
1417+
if (Py_IsInitialized() != 0) {
14261418
/* bpo-33932: Calling Py_Initialize() twice does nothing. */
14271419
return;
14281420
}
14291421

1422+
PyStatus status;
14301423
PyConfig config;
14311424
_PyConfig_InitCompatConfig(&config);
14321425

0 commit comments

Comments
 (0)