Skip to content
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

Change PyIterator::from_object` to return underlying TypeError #1051

Merged
merged 1 commit into from
Jul 19, 2020

Conversation

davidhewitt
Copy link
Member

Reviewing #1050 I realised that PyIterator::from_object() makes an unneccessary PyIter_Check() call as a successful result is already guaranteed to be an iterator.

I simplified it so that the function now returns the error from PyObject_GetIter() instead of a PyDowncastError. This helps the implementation of #1050 and I think also improves the experience here.

Now this function will return the well-known TypeError: 'Foo' object is not iterable which comes from PyObject_GetIter(): (https://github.com/python/cpython/blob/8182cc2e68a3c6ea5d5342fed3f1c76b0521fbc1/Objects/abstract.c#L2624)

@sebpuetz
Copy link
Contributor

Just commenting on this since this was mentioned in my PR: #558 introduced this null check because trying to iterate some non-sense would cause segfaults. Is that behaviour fixed in some other way or would this lead to a regression here?

@davidhewitt
Copy link
Member Author

Thanks for double-checking! Python::from_borrowed_ptr_or_err makes the null check inside it, so that case is still handled.

It actually happens exactly that the test I added to this PR tries to iterate an int and confirms that it results in a TypeError 😄

@davidhewitt davidhewitt merged commit a1dc970 into PyO3:master Jul 19, 2020
@davidhewitt davidhewitt deleted the pyiterator-from-object branch August 10, 2021 07:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants