We have a C-extension module that works with Datetime DType objects through
the C-API. The existing code works fine for NumPy 1x, but not 2x. The NumPy
2.0 migration documentation
<https://numpy.org/doc/stable/numpy_2_0_migration_guide.html#the-pyarray-descr-struct-has-been-changed>
does not address Datetime DType. This line of code works for 1x:

NPY_DATETIMEUNIT dtu = ((PyArray_DatetimeDTypeMetaData
*)descr->c_metadata)->meta.base;

Here, descr is a PyArray_Descr and the code accesses the datetime's unit
value. The documentation notes that in 2x, this struct is now opaque. I
updated this line of code to use the provided helper macro:

NPY_DATETIMEUNIT dtu = ((PyArray_DatetimeDTypeMetaData
*)((_PyArray_LegacyDescr *)
PyDataType_C_METADATA(descr))->c_metadata)->meta.base;

The code compiles, but raises the exception "Windows fatal exception:
access violation".

I changed the line of code to:

NPY_DATETIMEUNIT dtu = ((PyArray_DatetimeDTypeMetaData
*)((_PyArray_LegacyDescr *)descr)->c_metadata)->meta.base;

This code compiles and runs, but _PyArray_LegacyDescr is semi-private:
[image: image.png]

And, the helper macro NPY_DT_is_legacy is not in the public API. What is
the correct usage pattern for accessing Datetime DType descriptor fields?

Lee
_______________________________________________
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com

Reply via email to