-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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-111262: Add PyDict_Pop() function [with default value] #111939
Conversation
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.
Please show how it will be used in existing code instead of the old API? It will help to see what design is better.
For now, both uses of _PyDict_Pop_KnownHash()
ignore the returning value. It means either that the design is not optimal or that the caller does not fully utilize the API.
Are you suggesting to return You asked for this information:
Do you want to suggest a different API?
Well, I wasn't sure if this PR should change the existing code "too much". I just pushed a change to use _PyDict_Pop_KnownHash() return value. As |
I replaced usage of private _PyDict_Pop() with new public PyDict_Pop() to show how its return value can be useful to check for errors. |
Change the API of the internal _PyDict_Pop_KnownHash() function to return an int. Co-Authored-By: Stefan Behnel <[email protected]>
Co-authored-by: Antoine Pitrou <[email protected]>
Oh, this PR is related to issue gh-111262. Before I mentioned the wrong issue number. |
This looks OK to me API-wise, but as always, preferably wait for the C-API workgroup to review it. Please don't add it to the limited API before such a review. Do we need |
My gut feeling tells me that the most important use case for this is a fire-and-forget "delete an item" kind of operation, where I do not care whether the key is found or not. This is at least a little less efficient than it should be, because users still need to pass an explicit default value (probably Why do we need to raise an exception at all? Why not return the default value regardless, i.e. return Options I see:
I actually like the second option best, it seems quite simple. If users really want an exception to be raised, raising a |
That written, returning three different codes is somewhat annoying since it means that users need to put the return code in a variable first, rather than just writing However, at least with my second option, the returned dict value is actually a proper point of distinction as well, so users could safely use this simple "error on -1" pattern and then check whether the value is |
+1 from me as well. Using a default value with |
Thank you Victor for showing how it can be used in the code (at least in our code). We now can see advantages and drawbacks of different designs. I was going to write the same as @scoder. He just reads my mind. Most of uses cases here are trivial "delete without raising KeyError". The default value does not matter in this case. KeyError can be raised in only two cases where the default value can be NULL: in the implementations of the I propose to implement (perhaps in a separate PR) the variant of |
Ok, and now something completely different: PR gh-112028
|
If needed, it can be added later, but in Python code base, it doesn't seem to be needed. So far, nobody asked for such API.
The Steering Council has been asked to take a decision on PEP 731 -- C API Working Group Charter 3 weeks ago, and so far, no decision was taken. I don't think that the lack of such working group should hold limited C API change. Can't we take decisions as a community until such group exists? |
It can be used at least in two places in the CPython codebase. Even if it was not used, it is worth to add it, because all similar functions have a |
Would you mind to point me where it could be used? |
I don't think we should. This PR and others like it are setting precedents for new future C-API, and we do not agree on the details of the direction we want to take. We should be extra careful that the PRs adhere the guidelines we want to set, and we'll want to improve the guidelines based on the individual PRs. |
I'm not sure I understand this argument, since the API proposed in this PR just follows established and uncontroversial conventions. Taking a quick look at the guidelines being discussed, the only one that seems to apply is |
I filed capi-workgroup/api-evolution#40, see you there for generic discussion about one aspect of proposed new API. |
Theoretically yes, but since we know that the group is coming, it's a bit rude to try and race ahead of it just in case it disagrees with your design. As I proposed on the other PR, any new API going in now shouldn't be treated as precedent for guidelines going forward. It may end up as a wart, but that's the risk you take adding new things in this period. Help get the WG established quickly and things will start to be unblocked (and definitely don't add anything to the stable ABI in the meantime). |
Ok. By the way, 26 functions were added to Python 3.13 stable ABI. Should they be removed until the C API Working Group is created and decides what to do with them?
This PR is related to the removal of private functions in Python 3.13. Apparently, users are now eagger to test Python 3.13 as soon as alpha1, reported issues, and so I created #111481 and #112026 to try to mitigate issues. Are you suggesting to freeze the C API for now? |
These were all existing functions though, right? I'm not aware of anything brand new being added - we've all been saying to hold off on brand new APIs being marked long-term stable for months now. You still get to use your judgement. If you judge that there's only one reasonable way to implement an API, then go ahead and take the risk. Just be aware that we are actively trying to plan how to plan the C API into the future, and so unilateral design decisions risk making a mockery of that plan, and by extension, the entire project. You're aware of the problems and proposed solutions that have been raised (see the issue repositories at https://github.com/capi-workgroup/ if not), and so anything falling in those areas is likely a good candidate for bringing to a discussion before committing us to support the design for the next 10+ years. |
I created Set guidelines to add APIs to the limited C API and to the stable ABI. I suggest to continue the discussion there. I already modified this PyDict_Pop() PR to not add it to the limited API. |
I close this PR: #112028 was merged with a different API. |
Change the API of the internal _PyDict_Pop_KnownHash() function to return an int.
📚 Documentation preview 📚: https://cpython-previews--111939.org.readthedocs.build/