Skip to content

Commit 34c941c

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 38264a0 commit 34c941c

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
@@ -1438,19 +1438,12 @@ Py_InitializeFromConfig(const PyConfig *config)
14381438
void
14391439
Py_InitializeEx(int install_sigs)
14401440
{
1441-
PyStatus status;
1442-
1443-
status = _PyRuntime_Initialize();
1444-
if (_PyStatus_EXCEPTION(status)) {
1445-
Py_ExitStatusException(status);
1446-
}
1447-
_PyRuntimeState *runtime = &_PyRuntime;
1448-
1449-
if (runtime->initialized) {
1441+
if (Py_IsInitialized() != 0) {
14501442
/* bpo-33932: Calling Py_Initialize() twice does nothing. */
14511443
return;
14521444
}
14531445

1446+
PyStatus status;
14541447
PyConfig config;
14551448
_PyConfig_InitCompatConfig(&config);
14561449

0 commit comments

Comments
 (0)