Skip to content

Commit 1edac3a

Browse files
committed
fix to support Numpy 1.x and Numpy 2.x
1 parent db4efb6 commit 1edac3a

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

Qitom/python/pythonDataObject.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@
4646

4747
#define PROTOCOL_STR_LENGTH 128
4848

49-
// Define NPY_2_0_API_VERSION if it's not available (e.g., when using NumPy 1.x)
50-
#ifndef NPY_2_0_API_VERSION
51-
#define NPY_2_0_API_VERSION 0x00000012
52-
#endif
5349

5450
namespace ito {
5551
template<class T>
@@ -1178,7 +1174,7 @@ bool PythonDataObject::PyDataObj_CopyFromDatetimeNpNdArray(
11781174
// in case of datetime or timedelta: The values are int64, based on 1.1.1970
11791175
// the timebase is given by:
11801176
// Assuming NumPy 1.7+ has the new behavior for descr metadata
1181-
#if (NPY_2_0_API_VERSION)
1177+
#ifdef NPY_2_0_API_VERSION
11821178
const auto md = (PyArray_DatetimeDTypeMetaData*)(descr);
11831179
#else
11841180
const auto md = (PyArray_DatetimeDTypeMetaData*)(descr->c_metadata);
@@ -1313,7 +1309,7 @@ bool PythonDataObject::PyDataObj_CopyFromTimedeltaNpNdArray(
13131309

13141310
// in case of datetime or timedelta: The values are int64, based on 1.1.1970
13151311
// the timebase is given by:
1316-
#if (NPY_2_0_API_VERSION)
1312+
#ifdef NPY_2_0_API_VERSION
13171313
const auto md = (PyArray_DatetimeDTypeMetaData*)(descr);
13181314
#else
13191315
const auto md = (PyArray_DatetimeDTypeMetaData*)(descr->c_metadata);
@@ -8564,7 +8560,11 @@ RetVal PythonDataObject::parseTypeNumber(int typeno, char& typekind, int& itemsi
85648560
// todo: maybe kind and size can be hard coded
85658561
PyArray_Descr* descr = PyArray_DescrNewFromType(NPY_DATETIME);
85668562
typekind = descr->kind; // NPY_DATETIMELTR
8563+
#ifdef NPY_2_0_API_VERSION
85678564
itemsize = PyDataType_ELSIZE(descr);
8565+
#else
8566+
itemsize = descr->elsize; // 8
8567+
#endif
85688568
Py_DECREF(descr);
85698569

85708570
// PyDatetimeScalarObject
@@ -8574,7 +8574,11 @@ RetVal PythonDataObject::parseTypeNumber(int typeno, char& typekind, int& itemsi
85748574
// todo: maybe kind and size can be hard coded
85758575
PyArray_Descr* descr = PyArray_DescrNewFromType(NPY_TIMEDELTA);
85768576
typekind = descr->kind; // NPY_TIMEDELTALTR
8577+
#ifdef NPY_2_0_API_VERSION
85778578
itemsize = PyDataType_ELSIZE(descr);
8579+
#else
8580+
itemsize = descr->elsize; // 8
8581+
#endif
85788582
Py_DECREF(descr);
85798583
break;
85808584
}
@@ -8748,7 +8752,7 @@ std::string PythonDataObject::getNpDTypeStringFromNpDTypeEnum(const int type)
87488752
case NPY_HALF:
87498753
typeStr = "half";
87508754
break;
8751-
#if (NPY_2_0_API_VERSION)
8755+
#ifdef NPY_2_0_API_VERSION
87528756
case NPY_NTYPES_LEGACY:
87538757
#else
87548758
case NPY_NTYPES:
@@ -8980,7 +8984,7 @@ ito::RetVal PythonDataObject::copyNpArrayValuesToDataObject(
89808984
ito::DateTime* rowPtr;
89818985
PyArray_Descr* dtype = PyArray_DESCR(npNdArray);
89828986

8983-
#if (NPY_2_0_API_VERSION)
8987+
#ifdef NPY_2_0_API_VERSION
89848988
const auto md = (PyArray_DatetimeDTypeMetaData*)(dtype);
89858989
#else
89868990
const auto md = (PyArray_DatetimeDTypeMetaData*)(dtype->c_metadata);
@@ -9014,7 +9018,7 @@ ito::RetVal PythonDataObject::copyNpArrayValuesToDataObject(
90149018
ito::TimeDelta* rowPtr;
90159019
PyArray_Descr* dtype = PyArray_DESCR(npNdArray);
90169020

9017-
#if (NPY_2_0_API_VERSION)
9021+
#ifdef NPY_2_0_API_VERSION
90189022
const auto md = (PyArray_DatetimeDTypeMetaData*)(dtype);
90199023
#else
90209024
const auto md = (PyArray_DatetimeDTypeMetaData*)(dtype->c_metadata);
@@ -9560,7 +9564,7 @@ PyArrayObject* nparrayFromTimeDeltaDataObject(
95609564
{
95619565
// step 1: create numpy array
95629566
PyArray_Descr* descr = PyArray_DescrNewFromType(NPY_TIMEDELTA);
9563-
#if (NPY_2_0_API_VERSION)
9567+
#ifdef NPY_2_0_API_VERSION
95649568
auto metaData = (PyArray_DatetimeDTypeMetaData*)(descr);
95659569
#else
95669570
auto metaData = (PyArray_DatetimeDTypeMetaData*)(descr->c_metadata);
@@ -9685,7 +9689,7 @@ PyArrayObject* nparrayFromDateTimeDataObject(
96859689
{
96869690
// step 1: create numpy array
96879691
PyArray_Descr* descr = PyArray_DescrNewFromType(NPY_DATETIME);
9688-
#if (NPY_2_0_API_VERSION)
9692+
#ifdef NPY_2_0_API_VERSION
96899693
auto metaData = (PyArray_DatetimeDTypeMetaData*)(descr);
96909694
#else
96919695
auto metaData = (PyArray_DatetimeDTypeMetaData*)(descr->c_metadata);
@@ -9853,7 +9857,7 @@ PyObject* PythonDataObject::PyDataObj_Array_(PyDataObject* self, PyObject* args)
98539857

98549858
if (newtype && PyDataType_ISDATETIME(newtype))
98559859
{
9856-
#if (NPY_2_0_API_VERSION)
9860+
#ifdef NPY_2_0_API_VERSION
98579861
meta = &(((PyArray_DatetimeDTypeMetaData*)newtype)->meta);
98589862
#else
98599863
meta = &(((PyArray_DatetimeDTypeMetaData*)newtype->c_metadata)->meta);

0 commit comments

Comments
 (0)