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

Some PyList index fixes #1802

Merged
merged 1 commit into from
Aug 17, 2021
Merged

Some PyList index fixes #1802

merged 1 commit into from
Aug 17, 2021

Conversation

birkenfeld
Copy link
Member

@birkenfeld birkenfeld commented Aug 17, 2021

NB: the behavior on out-of-range indices hasn't changed;
it was merely wrongly documented before.

If we want to panic instead, to match e.g. Vec::insert, this needs to be implemented specially.

@birkenfeld birkenfeld changed the title Accept usize index for PyList::insert. Some PyList index fixes Aug 17, 2021
@birkenfeld birkenfeld force-pushed the pylist_insert_index branch 2 times, most recently from 5c54299 to ed51694 Compare August 17, 2021 06:46
Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Hmm, staring at this, I now think that we need to be careful when casting the usize function arguments to Py_ssize_t...

src/types/list.rs Show resolved Hide resolved
src/types/list.rs Outdated Show resolved Hide resolved
@birkenfeld
Copy link
Member Author

Yeah that's a good point. I think the simplest way would be clamping to Py_ssize_t::MAX in all cases; that will let Python raise IndexError or clamp as desired. A possible false positive case, i.e. a Python collection with Py_ssize_t::MAX items, is not realistic in any case.

…rt and PyList::set_item.

NB: the behavior on out-of-range indices hasn't changed;
it was merely wrongly documented before.

See #1667
Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

👍 needs a CHANGELOG entry, I think, otherwise LGTM!

It might be worth adding tests with out-of-range usize values, now that we've realised that edge case...

}
}

/// Takes a slice of the tuple from `low` to the end and returns it as a new tuple.
pub fn split_from(&self, low: isize) -> &PyTuple {
pub fn split_from(&self, low: usize) -> &PyTuple {
Copy link
Member

Choose a reason for hiding this comment

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

Looks like this function lacks a test, so it would be great to add one if you're willing!

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually I was wondering if it's needed. The operation exists neither in Python nor in Rust, is not used internally, and it's easy enough to use slice instead.

Copy link
Member

Choose a reason for hiding this comment

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

Fair point. Yes let's deprecate it and plan to remove.

As you say, it's easy to reproduce with either .slice() or .as_slice().split_at()

@birkenfeld
Copy link
Member Author

I was thinking that this should have a common changelog entry with #1733 and the other followups.

BTW, is internal_tricks the right module for the helper function? Happy to move it somewhere else.

@davidhewitt
Copy link
Member

BTW, is internal_tricks the right module for the helper function? Happy to move it somewhere else.

Not really sure where it fits either. I think it's fine there for now. As it's pub(crate) only, quite easy for us to relocate later.

@davidhewitt
Copy link
Member

It looks like this is unlikely to cause much pain when cherry-picking other things from main for 0.14.3, so I'll proceed to merge this now.

@davidhewitt davidhewitt merged commit df41903 into main Aug 17, 2021
birkenfeld added a commit that referenced this pull request Aug 18, 2021
birkenfeld added a commit that referenced this pull request Aug 23, 2021
@birkenfeld birkenfeld deleted the pylist_insert_index branch August 29, 2021 15:01
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