On Mon, 2025-02-24 at 11:30 -0600, Lee Johnston via NumPy-Discussion
wrote:
> 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-pyarra
> y-descr-struct-has-been-changed>
> does not address Datetime DType. This line of code works for 1x:

As that document mentions:

Where not possible, new accessor functions are required:
...
* `PyDataType_C_METADATA`

which is the mechanism/slot for datetime metdata (as you can se the
field is called `c_metadata`).

You have to use that and compile with NumPy 2.x.  If you don't want to
compile with NumPy 2.x, you'll have to copy paste the `npy_2_compat.h`
file and include it always.

- Sebastian


> 
> 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: sebast...@sipsolutions.net

_______________________________________________
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