8000 API,MAINT: Make ``NpyIter_GetTransferFlags`` public and avoid old uses by seberg · Pull Request #27998 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

API,MAINT: Make NpyIter_GetTransferFlags public and avoid old uses #27998

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 7 commits into from
Dec 18, 2024

Conversation

seberg
Copy link
Member
@seberg seberg commented Dec 14, 2024

This combines two related things:

  1. Make NpyIter_GetTransferFlags a public function. Now that we have these flags in the public API, there seems no reason not to make this public.
  2. The iterator currently found the NEEDS_API flag every time (with REFS_OK) for use in NpyIter_IterationNeedsAPI. But we should generally not use NpyIter_IterationNeedsAPI anymore in NumPy. The reason is that we need to know what buffering needs, but already know anyway if we need the API for whatever we do with the dtypes. So:
    • Change code to not use it or add a comment for why it is used.
    • Remove the flag and move the logic to NpyIter_IterationNeedsAPI so that we avoid the unnecessary flag discovery (at least in throttled CPU mode ~5% of a trivial reduce).

I completely removed one macro, because I didn't want the confusion of which version is used (or two macros).

This is ready for review, but marking as draft, because the reduce code move is probably only safe after gh-27883. (if we are lucky, the sanitizer CI run notices this)

@seberg seberg marked this pull request as ready for review December 17, 2024 09:54
Copy link
Member
@mattip mattip left a comment

Choose a reason for hiding this comment

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

LGTM, once it is rebased and tests pass.

@@ -2795,7 +2786,8 @@ PyArray_CopyAsFlat(PyArrayObject *dst, PyArrayObject *src, NPY_ORDER order)
NpyIter_Deallocate(src_iter);
return -1;
}
needs_api |= (flags & NPY_METH_REQUIRES_PYAPI) != 0;
/* No need to worry about API use in unbuffered iterator */
needs_api = (flags & NPY_METH_REQUIRES_PYAPI) != 0;
Copy link
Member

Choose a reason for hiding this comment

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

Should probably declare and set the value together.

Suggested change
needs_api = (flags & NPY_METH_REQUIRES_PYAPI) != 0;
int needs_api = (flags & NPY_METH_REQUIRES_PYAPI) != 0;

@seberg seberg force-pushed the nditer-no-api-flag branch from efd6688 to 71e33fd Compare December 17, 2024 11:31
@seberg
Copy link
Member Author
seberg commented Dec 17, 2024

Ping @ngoldbaum for awareness though, since it adds the public function.

Also, will push one small change: The return value should be NPY_ARRAYMETHOD_FLAGS rather than int.
(After that will merge, the circleci/benchmark failures are due to a new scipy-doctest and the lint also has a general failure right now.)

@seberg seberg merged commit 965fdf5 into numpy:main Dec 18, 2024
62 of 66 checks passed
@seberg seberg deleted the nditer-no-api-flag branch December 18, 2024 08:17
@seberg
Copy link
Member Author
seberg commented Dec 18, 2024

OK, put this in very happy to follow up if the docs should be improved (or anything else).

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.

2 participants
0