Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions include/pybind11/numpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,18 @@ struct handle_type_name<array> {
template <typename type, typename SFINAE = void>
struct npy_format_descriptor;

// NumPy 2.5.2+ aligns the first field after PyObject_HEAD to 8 bytes on Python 3.15+.
// This changes the layout on 32-bit free-threaded builds, where sizeof(PyObject) == 20.
#if PY_VERSION_HEX >= 0x030F0000
# define PYBIND11_NPY_FIRST_FIELD alignas(8)
#else
# define PYBIND11_NPY_FIRST_FIELD
#endif

/* NumPy 1 proxy (always includes legacy fields) */
struct PyArrayDescr1_Proxy {
PyObject_HEAD
PyObject *typeobj;
PYBIND11_NPY_FIRST_FIELD PyObject *typeobj;
char kind;
char type;
char byteorder;
Expand All @@ -89,7 +97,7 @@ struct PyArrayDescr1_Proxy {

struct PyArrayDescr_Proxy {
PyObject_HEAD
PyObject *typeobj;
PYBIND11_NPY_FIRST_FIELD PyObject *typeobj;
char kind;
char type;
char byteorder;
Expand All @@ -101,7 +109,7 @@ struct PyArrayDescr_Proxy {
/* NumPy 2 proxy, including legacy fields */
struct PyArrayDescr2_Proxy {
PyObject_HEAD
PyObject *typeobj;
PYBIND11_NPY_FIRST_FIELD PyObject *typeobj;
char kind;
char type;
char byteorder;
Expand All @@ -121,7 +129,7 @@ struct PyArrayDescr2_Proxy {

struct PyArray_Proxy {
PyObject_HEAD
char *data;
PYBIND11_NPY_FIRST_FIELD char *data;
int nd;
ssize_t *dimensions;
ssize_t *strides;
Expand All @@ -130,6 +138,8 @@ struct PyArray_Proxy {
int flags;
};

#undef PYBIND11_NPY_FIRST_FIELD

struct PyVoidScalarObject_Proxy {
PyObject_VAR_HEAD char *obval;
PyArrayDescr_Proxy *descr;
Expand Down
Loading