Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 2 commits into from
Nov 10, 2023

Conversation

seberg
Copy link
Member

@seberg seberg commented Nov 9, 2023

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.)

seberg added a commit to seberg/numpy that referenced this pull request Nov 9, 2023
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.)
@charris
Copy link
Member

charris commented Nov 10, 2023

PRs that make you go um ... Is the long here an actual C long?

@seberg
Copy link
Member Author

seberg commented Nov 10, 2023

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 npy_intp). The type is important only for how assignment works and also for how Cython does promotion. (it was an int before! if it was the actual type used by Cython it would go boom.)

EDIT: Cython has sizeof() guards everywhere also in conversion code. But it does have some conversion paths that work differently.

@mattip
Copy link
Member

mattip commented Nov 10, 2023

LGTM. Is there a corresponding issue that this closes?

@seberg
Copy link
Member Author

seberg commented Nov 10, 2023

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).

@charris
Copy link
Member

charris commented Nov 10, 2023

In it goes, thanks Sebastian.

@charris charris merged commit eafca03 into numpy:main Nov 10, 2023
@seberg seberg deleted the avoid-Py_ssize_t branch November 10, 2023 15:03
charris pushed a commit to charris/numpy that referenced this pull request Nov 11, 2023
This is the minimal backport version of numpygh-25094, which simply aligns
the Cython 2 and Cython 3 definitions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants