Skip to content

gh-148790: Defer _PyRuntime_Initialize to Py_InitializeFromConfig.#121628

Merged
ZeroIntensity merged 2 commits intopython:mainfrom
AraHaan:patch-1
Apr 20, 2026
Merged

gh-148790: Defer _PyRuntime_Initialize to Py_InitializeFromConfig.#121628
ZeroIntensity merged 2 commits intopython:mainfrom
AraHaan:patch-1

Conversation

@AraHaan
Copy link
Copy Markdown
Contributor

@AraHaan AraHaan commented Jul 11, 2024

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 store 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.

Because this is a trivial change to an implementation detail and does not affect functionality at all, I feel like this might not need an issue first. Very glad I took the time to look at this code and seen that it could be simplified somewhat.

@AraHaan AraHaan force-pushed the patch-1 branch 2 times, most recently from 47a50c5 to c4883ab Compare July 12, 2024 13:44
@ericsnowcurrently
Copy link
Copy Markdown
Member

Thanks for working on this, @AraHaan! I'll take a look as soon as I can.

FYI, once you've created the PR (and it isn't "draft"), it's usually best if you do not rebase. Rebasing makes it harder for reviewers. For example, it's harder to know what has changed since the last time I looked at the PR. Do note that we always squash the PR commits into a single merge commit, so you don't need to worry about cluttering up the commit log with your own intermediate commits.

@AraHaan
Copy link
Copy Markdown
Contributor Author

AraHaan commented Jul 12, 2024

Thanks for working on this, @AraHaan! I'll take a look as soon as I can.

FYI, once you've created the PR (and it isn't "draft"), it's usually best if you do not rebase. Rebasing makes it harder for reviewers. For example, it's harder to know what has changed since the last time I looked at the PR. Do note that we always squash the PR commits into a single merge commit, so you don't need to worry about cluttering up the commit log with your own intermediate commits.

I was rebasing onto current main using the website. Sorry about that.

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.
@github-actions
Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 17, 2026
Comment thread Python/pylifecycle.c
{
PyStatus status;

status = _PyRuntime_Initialize();
Copy link
Copy Markdown
Contributor Author

@AraHaan AraHaan Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Py_InitializeFromConfig is called in Py_InitializeEx, this can be deferred to that call & avoids needless code duplication & wasted clock cycles on startup.

@AraHaan AraHaan changed the title Defer _PyRuntime_Initialize to Py_InitializeFromConfig. gh-148790: Defer _PyRuntime_Initialize to Py_InitializeFromConfig. Apr 20, 2026
Copy link
Copy Markdown
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes sense. LGTM!

@ZeroIntensity ZeroIntensity added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes and removed stale Stale PR or inactive for long period of time. labels Apr 20, 2026
@ZeroIntensity ZeroIntensity merged commit 789120e into python:main Apr 20, 2026
63 checks passed
@miss-islington-app
Copy link
Copy Markdown

Thanks @AraHaan for the PR, and @ZeroIntensity for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

@miss-islington-app
Copy link
Copy Markdown

Sorry, @AraHaan and @ZeroIntensity, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 789120e82609a50a94c683a62d043f89e383d23f 3.14

@miss-islington-app
Copy link
Copy Markdown

Sorry, @AraHaan and @ZeroIntensity, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 789120e82609a50a94c683a62d043f89e383d23f 3.13

@ZeroIntensity ZeroIntensity removed the needs backport to 3.13 bugs and security fixes label Apr 20, 2026
@ZeroIntensity ZeroIntensity removed the needs backport to 3.14 bugs and security fixes label Apr 20, 2026
@ZeroIntensity
Copy link
Copy Markdown
Member

Okay, let's not backport this. I was going to backport to prevent conflicts, but since there are already conflicts, there's no real benefit.

@AraHaan AraHaan deleted the patch-1 branch April 21, 2026 09:08
@AraHaan
Copy link
Copy Markdown
Contributor Author

AraHaan commented Apr 21, 2026

I think the conflicts was due to the merge commit to resolve conflicts from main. If you want I can manually try to backport it to 3.13 and 3.14.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants