-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-106915: Add PyImport_ImportOrAddModule() function #111559
Conversation
Remove PyImport_AddModuleRef() function.
b6f8588
to
2f3cc67
Compare
@encukou proposes |
Oh, if PyImport_AddModuleRef() is removed, I should update pythoncapi-compat as well: https://pythoncapi-compat.readthedocs.io/en/latest/api.html#c.PyImport_AddModuleRef |
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.
API-wise, this looks good to me, thank you! We should of course wait if the C-API WG is approved.
In the docs, I'd mention creating a new (empty) module more prominently, to make extra sure people don't assume the module is imported if missing.
I applied your suggestions manually to also update the doc in Include/import.h at the same time. |
The working group doesn't exist yet. Maybe @gvanrossum @zooba and @iritkatriel want to review this PR? |
@encukou: Would you mind to review the updated PR? |
I'll get to it next week. |
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.
(Got to it sooner!)
+1 from me on the functionality. Thank you!
It looks OK API-wise, too. I recommend checking with the other CAPI-workgroup-to-be members if it fits the direction they want.
One thing to note is the allocation of the “lesser” and “greater” result (0 or 1) for this kind of “setdefault”-style API: should 1 mean “created/assigned” like here, or “found” like in “getitem”-style API?
As far as I'm concerned, it could go either way, but some people might have a strong opinion. And this could become a precedent.
Aha. The function is called import or add: add comes second, so I suppose that it should return success (1) if import is successful. Also, I think that returning 1 if the module exists in sys.modules which be consistent with PyDict_GetItemRef() API. I modified the PR to return 1 if the module is found in sys.modules, or 0 if the module was created (and then added to sys.modules). |
@encukou: Would you mind to review the updated PR? |
I like this better, thanks! |
I think we've still got to thoroughly discuss the return value convention, and personally in this case I'd prefer to see Right now it isn't immediately obvious whether the existing value of the module argument is what is going to be added, or whether a new one will be created ( If there's a need to hurry this API in, then I won't block it, but I would ask that it not be used as precedent when we come to deciding the guideline. If we decide to go the other way, it can just be an exception/wart, but we shouldn't rush an API and then use that to determine the guideline. |
Well, I added @encukou: So, which API do you prefer?
|
Please add your proposal/ideas there. New API is being added without waiting for that discussion...
As always with new API, I'd prefer waiting until the WG can discuss it.
What I prefer now, in the middle of the discussions?
For 3., the
|
This PR implements what @encukou proposed there: #106915 (comment) But apparently, @encukou now dislikes the API. I don't have the bandwidth to dig into the issue. @encukou: if you're motivated, please go ahead. For now, I prefer to close this PR. |
Remove PyImport_AddModuleRef() function.
📚 Documentation preview 📚: https://cpython-previews--111559.org.readthedocs.build/