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

list,tuple,sequence: add slice indexing #1829

Merged
merged 1 commit into from
Aug 26, 2021

Conversation

davidhewitt
Copy link
Member

Follow up to #1825 to add range indexing.

Given that the decision in #1733 was that we are trying to make these APIs feel as much like idiomatic Rust as possible, I opted to make them panic on out of bounds.

This probably closes #1667 ?

@davidhewitt davidhewitt requested a review from birkenfeld August 24, 2021 22:10
src/types/list.rs Outdated Show resolved Hide resolved
src/types/list.rs Outdated Show resolved Hide resolved
src/types/list.rs Outdated Show resolved Hide resolved
src/types/sequence.rs Outdated Show resolved Hide resolved
src/types/sequence.rs Outdated Show resolved Hide resolved
@davidhewitt davidhewitt force-pushed the slice-index branch 2 times, most recently from cd83a91 to b16cf66 Compare August 25, 2021 21:01
@davidhewitt
Copy link
Member Author

Thanks for the great review!

I reworked this to be a macro as suggested. (I threw it in internal_tricks.rs for now, like everything else ends up...)

I also made conditional use of #[track_caller] (on sufficiently new rust versions) so that the panic message points at the user's indexing code rather than internal_tricks.rs

e.g.

thread 'types::sequence::tests::test_seq_index_trait_range_from_panic' panicked at 'range start index 8 out of range for sequence of length 3', src/types/sequence.rs:512:13

src/internal_tricks.rs Outdated Show resolved Hide resolved
}

index_impls!(PySequence, PyAny, "sequence", sequence_len, sequence_slice);
Copy link
Member

Choose a reason for hiding this comment

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

I see that the output of get_slice is currently not another PySequence, should we change this? IMO it's similar to expecting a sequence out of repeat etc.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I think that's reasonable.

I guess as before, implementations may actually not return a sequence. Then using further sequence APIs will fail, which is ok and sound.

@birkenfeld
Copy link
Member

I also made conditional use of #[track_caller] (on sufficiently new rust versions) so that the panic message points at the user's indexing code rather than internal_tricks.rs

Very nice (also that we can do it conditionally)!

@davidhewitt davidhewitt merged commit f20079b into PyO3:main Aug 26, 2021
@davidhewitt davidhewitt deleted the slice-index branch August 26, 2021 07:30
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.

PyList indexing: negative indices?
2 participants