Skip to content
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

API: Introduce PyDataType_FLAGS accessor for public access #25816

Merged
merged 6 commits into from Feb 17, 2024

Conversation

seberg
Copy link
Member

@seberg seberg commented Feb 13, 2024

The flags are too small at 8 bits, so create a new uint64 accessor function for public use. Internally, we can keep using flags just fine (since we don't need version specific handling).

Cython 2 will end up going through python to get the flags so make a note of that in the release notes.


Another relatively small preparatory step for opening up PyArray_Descr to serious changes. This moves flags specifically to be an inline function and exposes it as such to Cython.

Note: I wanted to use npy_uint64 (up for discussion), which is why I had to move the type definition block to the beginning. But that seemed very reasonable in either case.

The flags are too small at 8 bits, so create a new uint64 accessor
function for public use.  Internally, we can keep using flags just
fine (since we don't need version specific handling).

Cython 2 will end up going through python to get the flags so make
a note of that in the release notes.
@seberg seberg changed the title API: Introduce PyDataType_FLAGS accessor for public access API: Introduce PyDataType_FLAGS accessor for public access Feb 13, 2024
@seberg seberg force-pushed the descr-flags-accessor branch 2 times, most recently from 43c8d78 to c754f2a Compare February 13, 2024 11:34
The need to declare static inline functions which are then defined
in `npy_2_compat.h` means that we must make sure `npy_2_compat.h`
actually gets included always to avoid some compilers to error.
@seberg
Copy link
Member Author

seberg commented Feb 13, 2024

The last commit is one slight wrinkle, but I don't think it is too bad.

Because these functions need runtime dependent behavior, they cannot be reasonably used in ndarraytypes.h since compilers will complain about forward defined static inline functions which end up never defined.
That is just the reality of it I think, but means that I had to move existing functionality which needs explicit runtime dependency into ndarrayobject.h.

@seberg
Copy link
Member Author

seberg commented Feb 13, 2024

Note that this also serves as a blueprint for changing itemsize/elsize and alignment access, but I thought it might be easier to do this first quickly.

@@ -0,0 +1,10 @@
* Due to runtime dependencies, some functionality was moved out of
``numpy/ndarraytypes.h`` and is only available after including ``ndarrayobject.h``
Copy link
Member

Choose a reason for hiding this comment

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

It's not clear which functionality you're talking about. Can you rephrase to make this first sentence less ambiguous?

doc/release/upcoming_changes/25816.c_api.rst Outdated Show resolved Hide resolved
static inline npy_uint64
PyDataType_FLAGS(const PyArray_Descr *dtype)
{
return dtype->flags;
Copy link
Member

Choose a reason for hiding this comment

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

I think this needs an explicit cast to npy_uint64, because this is a signed char on numpy 1.x and might be negative?

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought the return value will do the trick, but maybe better safe then sorry.

This comment was marked as outdated.

return ((PyArray_Descr *)dtype)->flags;
}
else {
return ((PyArray_DescrProto *)dtype)->flags;
Copy link
Member

Choose a reason for hiding this comment

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

I think you need an explicit cast here as well.

numpy/__init__.cython-30.pxd Show resolved Hide resolved
@seberg seberg mentioned this pull request Feb 13, 2024
5 tasks
Copy link
Member

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

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

I just did some searching to judge the downstream impact of this change, and I think we're fine, at least as far as public code goes. I see a few uses of some of these macros in old docs and in a couple of unmaintained packages, but nothing in the latest version of any major reverse dependency (some usage in old versions of pandas though).

So I think this one might be low risk, although I wouldn't be surprised if there is a long tail of users of some of these macros that don't show up on github searches.

It would ameliorate my concern about this if we could get a mention in the migration guide to deal with any immediate fallout after this is merged, since people might miss release note fragments.

@rgommers rgommers added this to the 2.0.0 release milestone Feb 16, 2024
@ngoldbaum
Copy link
Member

I'm going to merge this one now. There's a note to update the migration guide in the followup tracking issue.

@ngoldbaum ngoldbaum merged commit f0d4714 into numpy:main Feb 17, 2024
63 checks passed
@seberg seberg deleted the descr-flags-accessor branch February 17, 2024 17:52
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.

None yet

3 participants