-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
BUG: Avoid intp conversion regression in Cython 3 #25094
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
Conversation
This is the minimal backport version of numpygh-25094, which simply aligns the Cython 2 and Cython 3 definitions.
In the Cython 3 `__init__.pxd` `intp` was defined to `Py_ssize_t` which is *correct*. However, Cython has special rules for `Py_ssize_t`. It uses the `PyNumber_AsIndex` (`operator.index`) function for conversion which rejects for example floats. Of course I prefer that behavior, but: 1. There is no great reason why `np.intp_t` should be all that special. 2. Changing it silently changes downstream (and `np.random`) behavior. It would be awesome if Cython would allow transitioning to using `operator.index()` style coercion since you can still explicitly convert via `int()`. Now, why the new definition to `signed long`!? `ssize_t` doesn't work because it is *also* special, using `PyLong_AsSize_t`. It seems to me that adding signed might give this type a bit of preference in promotion, so it is is just for good sport. (It is impossible to get Cython promotion fully right.)
d54da80
to
e26e75e
Compare
PRs that make you go um ... Is the long here an actual C long? |
No, it tells Cython to make it behave like a C long, but it is whatever it is on the C level (which is an EDIT: Cython has |
LGTM. Is there a corresponding issue that this closes? |
gh-25061 was it as it is really a more broad issue, but already closed in the narrow version. I also ran into this in sklearn and the mtrand.pyx changes (which is undone here). |
In it goes, thanks Sebastian. |
This is the minimal backport version of numpygh-25094, which simply aligns the Cython 2 and Cython 3 definitions.
In the Cython 3
__init__.pxd
intp
was defined toPy_ssize_t
which is correct. However, Cython has special rules forPy_ssize_t
. It uses thePyNumber_AsIndex
(operator.index
) function for conversion which rejects for example floats.Of course I prefer that behavior, but:
np.intp_t
should be all that special.np.random
) behavior.It would be awesome if Cython would allow transitioning to using
operator.index()
style coercion since you can still explicitly convert viaint()
.Now, why the new definition to
signed long
!?ssize_t
doesn't work because it is also special, usingPyLong_AsSize_t
. It seems to me that adding signed might give this type a bit of preference in promotion, so it is is just for good sport. (It is impossible to get Cython promotion fully right.)