Skip to content

Commit 37ce726

Browse files
authored
Merge pull request #178 from static-frame/177/np2
NumPy 2.0 support
2 parents f6b3008 + ebf4c06 commit 37ce726

8 files changed

+35
-32
lines changed

performance/__main__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,9 @@ def __init__(self):
507507
self.values = [
508508
np.longlong(-1), np.int_(-1), np.intc(-1), np.short(-1), np.byte(-1),
509509
np.ubyte(1), np.ushort(1), np.uintc(1), np.uint(1), np.ulonglong(1),
510-
np.half(1.0), np.single(1.0), np.float_(1.0), np.longfloat(1.0),
511-
np.csingle(1.0j), np.complex_(1.0j), np.clongfloat(1.0j),
512-
np.bool_(0), np.str_('1'), np.unicode_('1'), np.void(1),
510+
np.half(1.0), np.single(1.0), np.float64(1.0), np.longdouble(1.0),
511+
np.csingle(1.0j), np.complex_(1.0j), np.clongdouble(1.0j),
512+
np.bool_(0), np.str_('1'), np.str_('1'), np.void(1),
513513
np.object(), np.datetime64('NaT'), np.timedelta64('NaT'), np.nan,
514514
12, 12.0, True, None, float('NaN'), object(), (1, 2, 3),
515515
NT(1, 2, 3), datetime.date(2020, 12, 31), datetime.timedelta(14),

requirements-build-3_11.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
numpy==1.23.5
1+
numpy==2.0.0
22

requirements-build-3_12.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
numpy==1.26.2
1+
numpy==2.0.0
22
setuptools==69.*

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ def get_ext_dir(*components: tp.Iterable[str]) -> tp.Sequence[str]:
4141
'src/methods.c',
4242
'src/tri_map.c',
4343
],
44-
include_dirs=get_ext_dir('numpy', 'core', 'include'),
45-
library_dirs=get_ext_dir('numpy', 'core', 'lib'),
44+
include_dirs=get_ext_dir('numpy', '_core', 'include'),
45+
library_dirs=get_ext_dir('numpy', '_core', 'lib'),
4646
define_macros=[("AK_VERSION", AK_VERSION)],
4747
libraries=['npymath'], # not including mlib at this time
4848
)

src/delimited_to_arrays.c

+12-11
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,7 @@ AK_CPL_Free(AK_CodePointLine* cpl)
10691069
PyMem_Free(cpl->buffer);
10701070
PyMem_Free(cpl->offsets);
10711071
if (cpl->type_parser) {
1072-
PyMem_Free(cpl->type_parser);
1072+
AK_TP_Free(cpl->type_parser);
10731073
}
10741074
PyMem_Free(cpl);
10751075
}
@@ -1364,7 +1364,7 @@ AK_CPL_to_array_float(AK_CodePointLine* cpl, PyArray_Descr* dtype, char tsep, ch
13641364
// initialize error code to 0; only update on error.
13651365
int error = 0;
13661366
bool matched_elsize = true;
1367-
int elsize = dtype->elsize;
1367+
int elsize = PyDataType_ELSIZE(dtype);
13681368

13691369
NPY_BEGIN_THREADS_DEF;
13701370
NPY_BEGIN_THREADS;
@@ -1442,7 +1442,7 @@ AK_CPL_to_array_int(AK_CodePointLine* cpl, PyArray_Descr* dtype, char tsep)
14421442
// initialize error code to 0; only update on error.
14431443
int error = 0;
14441444
bool matched_elsize = true;
1445-
int elsize = dtype->elsize;
1445+
int elsize = PyDataType_ELSIZE(dtype);
14461446

14471447
NPY_BEGIN_THREADS_DEF;
14481448
NPY_BEGIN_THREADS;
@@ -1515,7 +1515,7 @@ AK_CPL_to_array_uint(AK_CodePointLine* cpl, PyArray_Descr* dtype, char tsep)
15151515
// initialize error code to 0; only update on error.
15161516
int error = 0;
15171517
bool matched_elsize = true;
1518-
int elsize = dtype->elsize;
1518+
int elsize = PyDataType_ELSIZE(dtype);
15191519

15201520
NPY_BEGIN_THREADS_DEF;
15211521
NPY_BEGIN_THREADS;
@@ -1583,15 +1583,15 @@ AK_CPL_to_array_unicode(AK_CodePointLine* cpl, PyArray_Descr* dtype)
15831583
bool capped_points;
15841584

15851585
// mutate the passed dtype as it is new and will be stolen in array construction
1586-
if (dtype->elsize == 0) {
1586+
if (PyDataType_ELSIZE(dtype) == 0) {
15871587
field_points = cpl->offset_max;
1588-
dtype->elsize = (int)(field_points * UCS4_SIZE);
1588+
// dtype->elsize = (int)(field_points * UCS4_SIZE);
1589+
PyDataType_SET_ELSIZE(dtype, (npy_intp)(field_points * UCS4_SIZE));
15891590
capped_points = false;
15901591
}
15911592
else {
15921593
// assume that elsize is already given in units of 4
1593-
// assert(dtype->elsize % UCS4_SIZE == 0);
1594-
field_points = dtype->elsize / UCS4_SIZE;
1594+
field_points = PyDataType_ELSIZE(dtype) / UCS4_SIZE;
15951595
capped_points = true;
15961596
}
15971597

@@ -1649,13 +1649,14 @@ AK_CPL_to_array_bytes(AK_CodePointLine* cpl, PyArray_Descr* dtype)
16491649
Py_ssize_t field_points;
16501650
bool capped_points;
16511651

1652-
if (dtype->elsize == 0) {
1652+
if (PyDataType_ELSIZE(dtype) == 0) {
16531653
field_points = cpl->offset_max;
1654-
dtype->elsize = (int)field_points;
1654+
// dtype->elsize = (int)field_points;
1655+
PyDataType_SET_ELSIZE(dtype, (npy_intp)field_points);
16551656
capped_points = false;
16561657
}
16571658
else {
1658-
field_points = dtype->elsize;
1659+
field_points = PyDataType_ELSIZE(dtype);
16591660
capped_points = true;
16601661
}
16611662

src/methods.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -560,16 +560,16 @@ isna_element(PyObject *m, PyObject *args, PyObject *kwargs)
560560
}
561561
if (PyArray_IsScalar(element, Complex64)) {
562562
npy_cfloat val = PyArrayScalar_VAL(element, Complex64);
563-
return PyBool_FromLong(isnan(val.real) || isnan(val.imag));
563+
return PyBool_FromLong(isnan(npy_crealf(val)) || isnan(npy_cimagf(val)));
564564
}
565565
if (PyArray_IsScalar(element, Complex128)) {
566566
npy_cdouble val = PyArrayScalar_VAL(element, Complex128);
567-
return PyBool_FromLong(isnan(val.real) || isnan(val.imag));
567+
return PyBool_FromLong(isnan(npy_creal(val)) || isnan(npy_cimag(val)));
568568
}
569569
# ifdef PyComplex256ArrType_Type
570570
if (PyArray_IsScalar(element, Complex256)) {
571571
npy_clongdouble val = PyArrayScalar_VAL(element, Complex256);
572-
return PyBool_FromLong(isnan(val.real) || isnan(val.imag));
572+
return PyBool_FromLong(isnan(npy_creall(val)) || isnan(npy_cimagl(val)));
573573
}
574574
# endif
575575

src/tri_map.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
static inline NPY_DATETIMEUNIT
1515
AK_dt_unit_from_array(PyArrayObject* a) {
1616
// This is based on get_datetime_metadata_from_dtype in the NumPy source, but that function is private. This does not check that the dtype is of the appropriate type.
17-
PyArray_DatetimeMetaData* dma = &(((PyArray_DatetimeDTypeMetaData *)PyArray_DESCR(a)->c_metadata)->meta);
17+
PyArray_Descr* dt = PyArray_DESCR(a); // borrowed ref
18+
PyArray_DatetimeMetaData* dma = &(((PyArray_DatetimeDTypeMetaData *)PyDataType_C_METADATA(dt))->meta);
19+
// PyArray_DatetimeMetaData* dma = &(((PyArray_DatetimeDTypeMetaData *)PyArray_DESCR(a)->c_metadata)->meta);
1820
return dma->base;
1921
}
2022

@@ -856,9 +858,9 @@ AK_TM_fill_object(TriMapObject* tm,
856858
#define AK_TM_TRANSFER_FLEXIBLE(c_type) do { \
857859
Py_ssize_t one_count = from_src ? tm->src_one_count : tm->dst_one_count;\
858860
TriMapOne* one_pairs = from_src ? tm->src_one : tm->dst_one; \
859-
npy_intp t_element_size = PyArray_DESCR(array_to)->elsize; \
861+
npy_intp t_element_size = PyArray_ITEMSIZE(array_to); \
860862
npy_intp t_element_cp = t_element_size / sizeof(c_type); \
861-
npy_intp f_element_size = PyArray_DESCR(array_from)->elsize; \
863+
npy_intp f_element_size = PyArray_ITEMSIZE(array_from); \
862864
c_type* array_to_data = (c_type*)PyArray_DATA(array_to); \
863865
c_type* f; \
864866
c_type* t; \
@@ -906,7 +908,7 @@ AK_TM_fill_unicode(TriMapObject* tm,
906908

907909
Py_UCS4* array_to_data = (Py_UCS4*)PyArray_DATA(array_to);
908910
// code points per element
909-
npy_intp cp = PyArray_DESCR(array_to)->elsize / UCS4_SIZE;
911+
npy_intp cp = PyArray_ITEMSIZE(array_to) / UCS4_SIZE;
910912

911913
bool decref_fill_value = false;
912914
if (PyBytes_Check(fill_value)) {
@@ -948,7 +950,7 @@ AK_TM_fill_string(TriMapObject* tm,
948950
? tm->final_src_fill : tm->final_dst_fill);
949951

950952
char* array_to_data = (char*)PyArray_DATA(array_to);
951-
npy_intp cp = PyArray_DESCR(array_to)->elsize;
953+
npy_intp cp = PyArray_ITEMSIZE(array_to);
952954
if (!PyBytes_Check(fill_value)) {
953955
return -1;
954956
}

test/test_util.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,11 @@ def test_dtype_from_element_core_dtypes(self) -> None:
527527
np.ulonglong,
528528
np.half,
529529
np.single,
530-
np.float_,
531-
np.longfloat,
530+
np.float64,
531+
np.longdouble,
532532
np.csingle,
533-
np.complex_,
534-
np.clongfloat,
533+
np.complex128,
534+
np.clongdouble,
535535
np.bool_,
536536
]
537537
for dtype in dtypes:
@@ -540,12 +540,12 @@ def test_dtype_from_element_core_dtypes(self) -> None:
540540
def test_dtype_from_element_str_and_misc_dtypes(self) -> None:
541541
dtype_obj_pairs = [
542542
(np.dtype('<U1'), np.str_('1')),
543-
(np.dtype('<U1'), np.unicode_('1')),
543+
(np.dtype('<U1'), np.str_('1')),
544544
(np.dtype('V1'), np.void(1)),
545545
(np.dtype('O'), object),
546546
(np.dtype('<M8'), np.datetime64('NaT')),
547547
(np.dtype('<m8'), np.timedelta64('NaT')),
548-
(np.float_, np.nan),
548+
(np.float64, np.nan),
549549
]
550550
for dtype, obj in dtype_obj_pairs:
551551
self.assertEqual(dtype, dtype_from_element(obj))

0 commit comments

Comments
 (0)