GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-146355: Fix `main_module` ref leak in `_PyRun_SimpleStringFlagsWithName` by A0su · Pull Request #146356 · python/cpython

Conversation

@A0su

@A0su A0su commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

gh-146355: Fix main_module ref leak in _PyRun_SimpleStringFlagsWithName

This is a sub-issue for #146102 and original gist details can be found here

When the_name is NULL we'll fail to call Py_DECREF on main_module. I used the same goto pattern as _PyRun_SimpleFileObject to fix this.

I omitted a new test entry as we have indirect coverage of functionality in test_embed.py. Not sure if it makes sense for the scope of fixing the ref leak to include the additional code for a test in test_run.py (which has a TODO for this function).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@A0su
refactor _PyRun_SimpleStringFlagsWithName to follow goto pattern in _…
cdf1ef3
…PyRun_SimpleFileObject
@A0su
A0su requested a review from ericsnowcurrently as a code owner March 24, 2026 03:03
@bedevere-app

bedevere-app Bot commented Mar 24, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@python-cla-bot

python-cla-bot Bot commented Mar 24, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added the awaiting review label Mar 24, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Mar 24, 2026
pythonrun.c: main_module ref leak in _PyRun_SimpleStringFlagsWithName #146355
Closed
vstinner
vstinner requested changes Mar 24, 2026
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I dislike this change. IMO it makes the code harder to follow and more complex. I would prefer just that to fix the leak:

diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index a21f494dc69..1e327848e65 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -567,6 +567,7 @@ _PyRun_SimpleStringFlagsWithName(const char *command, const char* name, PyCompil
         PyObject* the_name = PyUnicode_FromString(name);
         if (!the_name) {
             PyErr_Print();
+            Py_DECREF(main_module);
             return -1;
         }
         res = _PyRun_StringFlagsWithName(command, the_name, Py_file_input, dict, dict, flags, 0);

It's a single line to fix the leak.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

A0su reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@bedevere-app bedevere-app Bot removed the awaiting review label Mar 24, 2026
@bedevere-app

bedevere-app Bot commented Mar 24, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added the awaiting changes label Mar 24, 2026
@A0su
change to single line fix adding main_module decref
4c99feb
@bedevere-app

bedevere-app Bot commented Mar 24, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@A0su

A0su commented Mar 24, 2026

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added awaiting change review and removed awaiting changes labels Mar 24, 2026
@bedevere-app

bedevere-app Bot commented Mar 24, 2026

Copy link
Copy Markdown

Thanks for making the requested changes!

@vstinner: please review the changes made to this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app
bedevere-app Bot requested a review from vstinner March 24, 2026 15:42
@vstinner vstinner added the skip news label Mar 25, 2026
vstinner
vstinner approved these changes Mar 25, 2026
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting change review labels Mar 25, 2026
@vstinner
vstinner merged commit 08f6b33 into python:main Mar 25, 2026
55 of 59 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Mar 25, 2026
@vstinner

vstinner commented Mar 25, 2026

Copy link
Copy Markdown
Member

Merged, thanks for the fix.

A0su reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal pablogsal mentioned this pull request Apr 4, 2026
gh-130273: Fix traceback color output with unicode characters #142529
Merged
clin1234 pushed a commit to clin1234/cpython that referenced this pull request Apr 16, 2026
@A0su @clin1234
pythongh-146355: Fix main_module ref leak in `_PyRun_SimpleStringFl…
1660139
…agsWithName` (python#146356)
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
@A0su @ljfp
pythongh-146355: Fix main_module ref leak in `_PyRun_SimpleStringFl…
5f19328
…agsWithName` (python#146356)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner approved these changes
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

skip news

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@A0su @vstinner
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.