-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-105201: Add PyIter_NextItem() #122331
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-105201: Add PyIter_NextItem() #122331
Conversation
Return -1 and set an exception on error; return 0 if the iterator is exhausted, and return 1 if the next item was fetched successfully. Prefer this API to PyIter_Next(), which requires the caller to use PyErr_Occurred() to differentiate between iterator exhaution and errors. Co-authered-by: Irit Katriel <[email protected]>
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.
Small nitpicks but those are mostly documentation related. By the way, is there any plan to have something working like next(iter, sentinel)
with a sentinel support? or is there some already existing? (or is it even useful for CPython & the universe? I like using next(..., sentinel)
when I don't know whether I have an item or not, but I'm not sure whether this pattern has a simple equivalent in CPython).
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
the bug: 'item' was not set to NULL when an incorrect type was given
Thanks for your helpful remarks, everyone. I found a bug and updated the PR. I also noticed we should add the tests to |
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
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'm very sorry for the late review Erland but I was a bit busy this w-e. Those are nitpicking comments, so you can ignore them if you want (just me being compiled in -Wextra -Wpedantic
).
The return value should the sole means of determining subsequent control flow, and the output parameter should provide only necessary data, IMO. In other words, Which means the example use needs to rewritten from:
which might be a couple of lines longer, but has only one test in the loop, not two. We have three options, regarding the value of
1 requires work, but adds no additional information. 2 provides options for the user to provide some default and have it unchanged once the iterator exhausted. I don't see much value in this, but it allows a use case that 1 does not. 3 is best for performance and it leaves us free to implement I'd prefer to specify 3 for now, as it doesn't prevent us from choosing 1 or 2 later on. |
The C API WG already decided to go for alternative 1: set it explicitly to
Actually, I think I'll drop the example from this PR. We can add it in a following PR, if it is needed. I added @zooba's example from the C API WG repo. |
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
Thanks for all the reviews! |
This comment was marked as off-topic.
This comment was marked as off-topic.
That failure is unrelated, the iOS buildbot is green now. |
Return -1 and set an exception on error; return 0 if the iterator is exhausted, and return 1 if the next item was fetched successfully. Prefer this API to PyIter_Next(), which requires the caller to use PyErr_Occurred() to differentiate between iterator exhaustion and errors. Co-authered-by: Irit Katriel <[email protected]>
Return -1 and set an exception on error; return 0 if the iterator is
exhausted, and return 1 if the next item was fetched successfully.
Prefer this API to PyIter_Next(), which requires the caller to use
PyErr_Occurred() to differentiate between iterator exhaution and errors.
Co-authered-by: Irit Katriel [email protected]
📚 Documentation preview 📚: https://cpython-previews--122331.org.readthedocs.build/