← 返回首页
bpo-38823: Add a private _PyModule_StealObject API. by brandtbucher · Pull Request #17298 · python/cpython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

bpo-38823: Add a private _PyModule_StealObject API.#17298

Closed
brandtbucher wants to merge 8 commits into
python:masterfrom
brandtbucher:_PyModule_StealObject
Closed

bpo-38823: Add a private _PyModule_StealObject API.#17298
brandtbucher wants to merge 8 commits into
python:masterfrom
brandtbucher:_PyModule_StealObject

Conversation

brandtbucher commented Nov 20, 2019
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Member

This will be helpful for the refactoring I'm doing in bpo-38823, and keep the diffs small. It's also just nice to have around, since it (not PyModule_AddObject) is actually what we want in most cases.

https://bugs.python.org/issue38823

brandtbucher added the type-feature A feature request or enhancement label Nov 20, 2019

Copy link
Copy Markdown
Member Author

It would be nice to have this backported to 3.8 and 3.7, so that fixes using it may be backported as well. I'm not sure if that's appropriate though, so maybe somebody else can make the call.

brandtbucher commented Nov 21, 2019
edited
Loading

Copy link
Copy Markdown
Member Author

@vstinner Do you mind taking a peek at this when you have a free minute?

ncoghlan commented Dec 5, 2019

Copy link
Copy Markdown
Contributor

Pre-emptively backporting wouldn't be OK, but it could be considered if there was a bug fix backport that depended on it.

ncoghlan commented Dec 5, 2019

Copy link
Copy Markdown
Contributor

Do you still need this, though? It looks like most of the cleanup PRs have already been merged.

vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

I really dislike C functions "stealing" references. I prefer functions doing Py_INCREF() themselves. For example, PyList_Append(list, item) does Py_INCREF(item) as expected.

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.

brandtbucher requested review from a team and rhettinger as code owners January 28, 2020 01:46

Copy link
Copy Markdown
Member Author

@ncoghlan I've edited the PR to include a few of the many modules that still need to be refactored. The linked PRs on the issue were only the ones that would not benefit from this addition. I estimate that there are several dozen more. Hopefully it's clear that this function would make the bugfixes much more straightforward, enough to backport the function itself for their use.

@vstinner I too prefer it when functions don't steal references. If I were designing a brand new API, I would definitely not do it this way. The issue is that there are dozens of modules with complicated (broken) initialization code that relies on the stealing behavior of PyModule_AddObject (but doesn't properly handle errors). See _ctypes for a good example of code that is much easier to fix if we keep the stealing behvior.

If you still disagree, I can change it to never steal. It's just my impression from looking at the mountain of complex, broken code ahead of me that this is probably the easier route to take, especially since this API is just meant to be a "band-aid" for another broken API.

Copy link
Copy Markdown
Member Author

I didn't expect the Spanish Inquisition!

Copy link
Copy Markdown

Nobody expects the Spanish Inquisition!

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

Copy link
Copy Markdown
Member

Looks like this PR can be closed after victor merged this PR: #23122 @brandtbucher

Copy link
Copy Markdown
Member

@brandtbucher:

@vstinner I too prefer it when functions don't steal references. If I were designing a brand new API, I would definitely not do it this way. The issue is that there are dozens of modules with complicated (broken) initialization code that relies on the stealing behavior of PyModule_AddObject (but doesn't properly handle errors). See _ctypes for a good example of code that is much easier to fix if we keep the stealing behvior.

If you still disagree, I can change it to never steal. It's just my impression from looking at the mountain of complex, broken code ahead of me that this is probably the easier route to take, especially since this API is just meant to be a "band-aid" for another broken API.

I added PyModule_AddObjectRef() which uses strong references, rather than only stealing a reference on success.

I also enhanced the documentation to show concrete examples:
https://docs.python.org/dev/c-api/module.html#c.PyModule_AddObjectRef

I modified a few extension to use PyModule_AddObjectRef(). Sometimes, PyModule_AddObject() is more appropriate. Sometimes, PyModule_AddObjectRef() is more appropriate. Both functions are relevant, and I don't see a clear winner.

I agree than fixing existing code is painful, but I hope that new code using mostly PyModule_AddObjectRef() would be simpler to review. I'm not sure that it's simpler to write new code using PyModule_AddObjectRef(), since you might need more Py_DECREF() calls.

My intent is to have more "regular" code about reference counting. See also: https://bugs.python.org/issue42294

Since you wrote that this API is a band aid on a broken API, I consider that you are fine with rejecting it and move on to the new PyModule_AddObjectRef().

Anyway, thanks for you attempt to make the C API less broken :-)

vstinner closed this Nov 12, 2020
brandtbucher deleted the _PyModule_StealObject branch July 21, 2022 20:01
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting change review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

Footer

© 2026 GitHub, Inc.