From 34c941c013c44d9455af0d8650e7afd70876d21c Mon Sep 17 00:00:00 2001 From: AraHaan Date: Thu, 11 Jul 2024 14:22:59 -0400 Subject: [PATCH] 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 `` 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. --- Python/pylifecycle.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index ceb30e9f02df2c..635da9e34bc493 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1438,19 +1438,12 @@ Py_InitializeFromConfig(const PyConfig *config) void Py_InitializeEx(int install_sigs) { - PyStatus status; - - status = _PyRuntime_Initialize(); - if (_PyStatus_EXCEPTION(status)) { - Py_ExitStatusException(status); - } - _PyRuntimeState *runtime = &_PyRuntime; - - if (runtime->initialized) { + if (Py_IsInitialized() != 0) { /* bpo-33932: Calling Py_Initialize() twice does nothing. */ return; } + PyStatus status; PyConfig config; _PyConfig_InitCompatConfig(&config);