pyo3 icon indicating copy to clipboard operation
pyo3 copied to clipboard

Change the type of length from c_long to isize (in PySlice::index)

Open cmpute opened this issue 2 years ago • 5 comments

https://github.com/PyO3/pyo3/blob/3af73fabcfda5ecd1357819bb8f475bff2f4f467/src/types/slice.rs#L69

The underlying function PySlice_GetIndicesEx has an input length of type Py_ssize_t, which should be mapped to isize, instead of c_long. The conversion from c_long to Py_ssize_t seems unnecessary.

cmpute avatar Jan 23 '24 13:01 cmpute

Agreed, a PR to fix this would be greatly appreciated 👍

davidhewitt avatar Jan 23 '24 20:01 davidhewitt

That said, I assume the length should be non-negative? Maybe usize is better, and we can return an overflow error if the usize is greater than isize::MAX?

davidhewitt avatar Jan 23 '24 20:01 davidhewitt

Agree!. And if more changes are allowed, I propose to further change the type of start/stop/length of PySliceIndices all to usize, and impleming using the recommended new APIs PySlice_Unpack and PySlice_AdjustIndices. (according to official C-API docs)

cmpute avatar Jan 24 '24 01:01 cmpute

I'm certainly open to making those changes to fit better with what Python offers, but we should consider the compatibility story. There's a lot changing in 0.21 already and I'm cautious of adding too much more.

davidhewitt avatar Jan 24 '24 22:01 davidhewitt

I'm certainly open to making those changes to fit better with what Python offers, but we should consider the compatibility story. There's a lot changing in 0.21 already and I'm cautious of adding too much more.

This change is already a break change, but I will try to make the changes small, when I create the PR probably this or next week.

cmpute avatar Jan 25 '24 03:01 cmpute