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

Skip to content

API: Make intp ssize_t and introduce characters nN #24888

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 9 commits into from
Nov 22, 2023

Conversation

seberg
Copy link
Member

@seberg seberg commented Oct 9, 2023

This also removes NPY_INTPLTR to avoid confusion as it would have to be p on old NumPy but should be n on new NumPy versions. I suspect all users would use p explicitly anyway.

Since p is pretty clearly defined as pointer, retain that meaning.


I proposed a few times to change this, and it is no relatively easy. The reason is that I think we have a lot of code that uses the AsSize_T conversion functions and generally use intp as a size, and not to store pointers (we practically never store pointers after all).

There are three caveats:

  • For the integer typecodes pP mean intptr_t and uintptr_t clearly, so ensure that those characters will keep mapping to that. The correct character for (s)size_t seemed nN (as used by the struct module). (This bloats some tests a bit due to allTypes addition)
  • NPY_INTPLTR should be n but would need to be p to be backwards compatible. So I just deleted it. (In principle we could define it as p unless you compile for NumPy >=2 or unless n and p are not equivalent. But it seemed unlikely enough to be used in practice.)
  • On the very odd platform where size_t and uintptr_t do not have the same size, a module compiled against NumPy 2.0 cannot reliably run on 1.x, so raise an explicit error there.

@eric-wieser
Copy link
Member

eric-wieser commented Oct 9, 2023

How much work is it to have both npy_intp and npy_size as types? I can imagine this breaking people who are using npy_intp to interact with a C abi using intptr_t, and in the unlikely event these people are on weird systems where there is a difference, they are going to be very annoyed that intp no longer means intptr_t!

@seberg
Copy link
Member Author

seberg commented Oct 9, 2023

We can add a type, the question is what the default should be? I mean, I can also just add nN character code and npy_size, but the question really is what np.intp should map to in Python (and all the C creation functions which create this via NPY_INTP).

I think that:

  • For our Python usage, we mean ssize_t in principle. Indices do not need to be pointer sized, they need to be ssize_t sized (same for shapes).
  • I am pretty sure we print errors and parse numbers often enough in a way that assumes Py_ssize_t and intp are equivalent. (Not that I am quite sure these all break on OpenVMS)

So my thought here was that actual use is much much closer to size_t. But yes, if your are worried about this and adding a new type of the pointer use-case doesn't seem sufficient, then that is fine...

(+In that case) We should just introduce npy_size now, so that we could do a blanket replace in some 3.0 in a few years in principle.

@seberg seberg force-pushed the intp-to-ssize_t branch 2 times, most recently from fd5863b to 0d04124 Compare October 17, 2023 11:56
@seberg
Copy link
Member Author

seberg commented Oct 17, 2023

So, the work-around for users of intptr_t or uintptr_t is to use the character code p or P (which is viable even in C).
I don't love the change for the npyiter allocation, but it seems more correct.

One caveat: I am not sure how one would add the p code to the appropriate integer size. Maybe we don't even want to do that (since otherwise, it would seem to make sense to reorganize the typing long bit-sizes only, if that was easy?).

@seberg
Copy link
Member Author

seberg commented Oct 27, 2023

Depending on how much we worry about making pointer sized integers only available through the 'p' character code (only immediately affecting niche platforms) or dislike the misnomer, this is now ready.

Fixed up the new/modified table to make a not of 'p' as a pointer sized character code at least.
I think this should be final, unless there is worries about changing the meaning, it would be nice to decide that one way or another. (I clearly tend to think that there probably quite a few subtle bugs around this on affected platforms, that forcing those who actually do store pointers to change their code is hopefully good for those platforms in the long run. And maybe more importantly, I think it is good for those who aim to be clean, because unless you have pointers Py_ssize_t makes a lot of things saner, I expect.)

@mattip
Copy link
Member

mattip commented Nov 13, 2023

Bleh. If I understand this correctly the question is what do we want npy_intp to represent? size_t is for indexing, and intptr_t is for pointers. From the name, you would think npy_intp is meant to be intptr_t. If we were starting over, we could remove the C typedef, and have a python-level np.size_t and np.intptr_t. But we can't.

I think this is the least bad step forward. The "full" tests failed (due to the flaky tests skipped in #25068) , if they pass I will take another look and merge it.

These don't really make sense, even less as public API.
This also removes `NPY_INTPLTR` to avoid confusion as it would have
to be `p` on old NumPy but should be `n` on new NumPy versions.
I suspect all users would use `p` explicitly anyway.

Since `p` is pretty clearly defined as pointer, retain that meaning.
This is probably unnecessary but for good sport seems to make sense.
OpenVMS can have intptr_t at size 8 while size_t and pointers are
4 sized (because you can change the pointer size and the intptr
supports either size).  In general it probably doesn't care here,
in principle, size_t could also be smaller than a pointer though.
(Might have gotten confused in the merge conflict?)
@seberg
Copy link
Member Author

seberg commented Nov 16, 2023

Hum, after rebasing macos test failed with:

=================================== FAILURES ===================================
______________________ numpy/linalg/tests/test_linalg.py _______________________
[gw2] darwin -- Python 3.10.6 /Users/admin/numpy-dev/bin/python3.10
worker 'gw2' crashed while running 'numpy/linalg/tests/test_linalg.py::TestQR::test_stacked_inputs[complex64-outer_size0-size0]'
=========================== short test summary info ============================
FAILED numpy/linalg/tests/test_linalg.py::TestQR::test_stacked_inputs[complex64-outer_size0-size0]

restarted, but wondering what is up.

@seberg
Copy link
Member Author

seberg commented Nov 22, 2023

Time to give this a shot?

@mattip mattip merged commit 9577213 into numpy:main Nov 22, 2023
@mattip
Copy link
Member

mattip commented Nov 22, 2023

Thanks @seberg

@seberg seberg deleted the intp-to-ssize_t branch November 22, 2023 15:25
rgommers added a commit to rgommers/numpy that referenced this pull request Mar 13, 2024
The need for this define was removed with numpygh-24888. A code search
shows no external usages, and the few times this shows up on
the issue tracker it's always defined to `1`.
rgommers added a commit to rgommers/numpy that referenced this pull request Mar 13, 2024
The need for this define was removed with numpygh-24888. A code search
shows no external usages, and the few times this shows up on
the issue tracker it's always defined to `1`.

[skip cirrus] [skip azp] [skip circle]
charris pushed a commit to charris/numpy that referenced this pull request Mar 15, 2024
The need for this define was removed with numpygh-24888. A code search
shows no external usages, and the few times this shows up on
the issue tracker it's always defined to `1`.

[skip cirrus] [skip azp] [skip circle]
charris pushed a commit to charris/numpy that referenced this pull request Mar 15, 2024
The need for this define was removed with numpygh-24888. A code search
shows no external usages, and the few times this shows up on
the issue tracker it's always defined to `1`.

[skip cirrus] [skip azp] [skip circle]
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