@@ -573,9 +573,9 @@ typedef struct {
573
573
NPY_ITEM_IS_POINTER | NPY_ITEM_REFCOUNT | \
574
574
NPY_NEEDS_INIT | NPY_NEEDS_PYAPI)
575
575
576
- #if !(defined( NPY_INTERNAL_BUILD ) && NPY_INTERNAL_BUILD )
576
+ #if NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION
577
577
/*
578
- * Public version of the Descriptor struct
578
+ * Public version of the Descriptor struct as of 2.x
579
579
*/
580
580
typedef struct _PyArray_Descr {
581
581
PyObject_HEAD
@@ -595,96 +595,78 @@ typedef struct _PyArray_Descr {
595
595
* (not-applicable), or '=' (native).
596
596
*/
597
597
char byteorder ;
598
- /* flags describing data type */
599
- char flags ;
598
+ /* Former flags flags space (unused) to ensure type_num is stable. */
599
+ char _former_flags ;
600
600
/* number representing this type */
601
601
int type_num ;
602
+ /* Space for dtype instance specific flags. */
603
+ npy_uint64 flags ;
602
604
/* element size (itemsize) for this type */
603
- int elsize ;
605
+ npy_intp elsize ;
604
606
/* alignment needed for this type */
605
- int alignment ;
606
- /*
607
- * Non-NULL if this type is
608
- * is an array (C-contiguous)
609
- * of some other type
610
- */
611
- struct _arr_descr * subarray ;
612
- /*
613
- * The fields dictionary for this type
614
- * For statically defined descr this
615
- * is always Py_None
616
- */
617
- PyObject * fields ;
618
- /*
619
- * An ordered tuple of field names or NULL
620
- * if no fields are defined
621
- */
622
- PyObject * names ;
623
- // TODO: Remove: still there to break all downstream nightlies once only
624
- void * _former_f ;
625
- /* Metadata about this dtype */
607
+ npy_intp alignment ;
608
+ /* metadata dict or NULL */
626
609
PyObject * metadata ;
627
- /*
628
- * Metadata specific to the C implementation
629
- * of the particular dtype. This was added
630
- * for NumPy 1.7.0.
631
- */
632
- NpyAuxData * c_metadata ;
633
- /* Cached hash value (-1 if not yet computed).
634
- * This was added for NumPy 2.0.0.
635
- */
610
+ /* Cached hash value (-1 if not yet computed). */
636
611
npy_hash_t hash ;
637
-
612
+ /* Unused slot (must be initialized to NULL) for future use */
613
+ void * reserved_null [2 ];
638
614
} PyArray_Descr ;
639
615
640
- #else /* internal build */
616
+ #else /* 1.x and 2.x compatible version (only shared fields): */
641
617
642
- // TODO: This split definition only exists for piece-meal transitioning
643
- // as it allows change internal use without worrying about public API.
644
618
typedef struct _PyArray_Descr {
645
619
PyObject_HEAD
646
620
PyTypeObject * typeobj ;
647
621
char kind ;
648
622
char type ;
649
623
char byteorder ;
650
- char flags ;
624
+ char _former_flags ;
651
625
int type_num ;
652
- int elsize ;
653
- int alignment ;
654
- /* except hash, the below fields will be legacy descriptor specific */
655
- struct _arr_descr * unreachable_subarray ;
656
- PyObject * unreachable_fields ;
657
- PyObject * unreachable_names ;
658
- PyArray_ArrFuncs * _former_f ;
659
- PyObject * metadata ;
660
- NpyAuxData * unreachable_c_metadata ;
661
- npy_hash_t hash ;
662
626
} PyArray_Descr ;
663
627
664
- #endif /* internal build */
628
+ /* To access modified fields, define the full 2.0 struct: */
629
+ typedef struct {
630
+ PyObject_HEAD
631
+ PyTypeObject * typeobj ;
632
+ char kind ;
633
+ char type ;
634
+ char byteorder ;
635
+ char _former_flags ;
636
+ int type_num ;
637
+ npy_uint64 flags ;
638
+ npy_intp elsize ;
639
+ npy_intp alignment ;
640
+ PyObject * metadata ;
641
+ npy_hash_t hash ;
642
+ void * reserved_null [2 ];
643
+ } _PyArray_DescrNumPy2 ;
665
644
645
+ #endif /* 1.x and 2.x compatible version */
666
646
667
647
/*
668
648
* Semi-private struct with additional field of legacy descriptors (must
669
- * check NPY_DT_is_legacy before casting/accessing).
649
+ * check NPY_DT_is_legacy before casting/accessing). The struct is also not
650
+ * valid when running on 1.x (i.e. in public API use).
670
651
*/
671
652
typedef struct {
672
653
PyObject_HEAD
673
654
PyTypeObject * typeobj ;
674
655
char kind ;
675
656
char type ;
676
657
char byteorder ;
677
- char flags ;
658
+ char _former_flags ;
678
659
int type_num ;
679
- int elsize ;
680
- int alignment ;
660
+ npy_uint64 flags ;
661
+ npy_intp elsize ;
662
+ npy_intp alignment ;
663
+ PyObject * metadata ;
664
+ npy_hash_t hash ;
665
+ void * reserved_null [2 ];
681
666
struct _arr_descr * subarray ;
682
667
PyObject * fields ;
683
668
PyObject * names ;
684
- PyArray_ArrFuncs * _former_f ;
685
- PyObject * metadata ;
686
669
NpyAuxData * c_metadata ;
687
- npy_hash_t hash ;
688
670
} _PyArray_LegacyDescr ;
689
671
690
672
@@ -1569,11 +1551,6 @@ PyArray_FLAGS(const PyArrayObject *arr)
1569
1551
return ((PyArrayObject_fields * )arr )-> flags ;
1570
1552
}
1571
1553
1572
- static inline npy_intp
1573
- PyArray_ITEMSIZE (const PyArrayObject * arr )
1574
- {
1575
- return ((PyArrayObject_fields * )arr )-> descr -> elsize ;
1576
- }
1577
1554
1578
1555
static inline int
1579
1556
PyArray_TYPE (const PyArrayObject * arr )
@@ -1687,42 +1664,13 @@ PyArray_CLEARFLAGS(PyArrayObject *arr, int flags)
1687
1664
#define PyDataType_ISUSERDEF (obj ) PyTypeNum_ISUSERDEF(((PyArray_Descr*)(obj))->type_num)
1688
1665
#define PyDataType_ISEXTENDED (obj ) PyTypeNum_ISEXTENDED(((PyArray_Descr*)(obj))->type_num)
1689
1666
#define PyDataType_ISOBJECT (obj ) PyTypeNum_ISOBJECT(((PyArray_Descr*)(obj))->type_num)
1690
- #define PyDataType_HASFIELDS (obj ) (PyDataType_ISLEGACY((PyArray_Descr*)(obj)) && ((_PyArray_LegacyDescr *)(obj))->names != NULL)
1691
- #define PyDataType_HASSUBARRAY (dtype ) (PyDataType_ISLEGACY(dtype) && ((_PyArray_LegacyDescr *)dtype)->subarray != NULL)
1692
- #define PyDataType_ISUNSIZED (dtype ) ((dtype)->elsize == 0 && \
1693
- !PyDataType_HASFIELDS(dtype))
1694
1667
#define PyDataType_MAKEUNSIZED (dtype ) ((dtype)->elsize = 0)
1695
1668
/*
1696
- * PyDataType_FLAGS, PyDataType_FLACHK, and PyDataType_REFCHK require
1697
- * npy_2_compat.h and are not defined here.
1669
+ * PyDataType_* FLAGS, FLACHK, REFCHK, HASFIELDS, HASSUBARRAY, UNSIZED,
1670
+ * SUBARRAY, NAMES, FIELDS, C_METADATA, and METADATA require version specific
1671
+ * lookup and are defined in npy_2_compat.h.
1698
1672
*/
1699
1673
1700
- /*
1701
- * Access inline functions for legacy fields. Except metadata these fields are
1702
- * specific to structured arrays (names, fields) or datetime (c_metadata).
1703
- * Although technically they may be used (but normally ignored) on non-struct
1704
- * dtypes as well.
1705
- * For structured dtypes, new ways to define and access fields make sense.
1706
- */
1707
- static inline PyArray_ArrayDescr *
1708
- PyDataType_SUBARRAY (PyArray_Descr * dtype ) {
1709
- return !PyDataType_ISLEGACY (dtype ) ? NULL : ((_PyArray_LegacyDescr * )dtype )-> subarray ;
1710
- }
1711
-
1712
- static inline PyObject *
1713
- PyDataType_NAMES (PyArray_Descr * dtype ) {
1714
- return !PyDataType_ISLEGACY (dtype ) ? NULL : ((_PyArray_LegacyDescr * )dtype )-> names ;
1715
- }
1716
-
1717
- static inline PyObject *
1718
- PyDataType_FIELDS (PyArray_Descr * dtype ) {
1719
- return !PyDataType_ISLEGACY (dtype ) ? NULL : ((_PyArray_LegacyDescr * )dtype )-> fields ;
1720
- }
1721
-
1722
- static inline NpyAuxData *
1723
- PyDataType_C_METADATA (PyArray_Descr * dtype ) {
1724
- return !PyDataType_ISLEGACY (dtype ) ? NULL : ((_PyArray_LegacyDescr * )dtype )-> c_metadata ;
1725
- }
1726
1674
1727
1675
#define PyArray_ISBOOL (obj ) PyTypeNum_ISBOOL(PyArray_TYPE(obj))
1728
1676
#define PyArray_ISUNSIGNED (obj ) PyTypeNum_ISUNSIGNED(PyArray_TYPE(obj))
@@ -1967,13 +1915,4 @@ typedef struct {
1967
1915
*/
1968
1916
#undef NPY_DEPRECATED_INCLUDES
1969
1917
1970
- #if defined(NPY_INTERNAL_BUILD ) && NPY_INTERNAL_BUILD
1971
- /*
1972
- * we use ndarraytypes.h alone sometimes, but some functions from
1973
- * npy_2_compat.h are forward declared here, so ensure we have them.
1974
- * (external libraries must eventually include `ndarrayobject.h`)
1975
- */
1976
- #include "npy_2_compat.h"
1977
- #endif
1978
-
1979
1918
#endif /* NUMPY_CORE_INCLUDE_NUMPY_NDARRAYTYPES_H_ */
0 commit comments