Question regarding transitioning native extension to NumPy 2.0
I am working to transition mkl_fft and mkl_random to NumPy 2.0. Both of these projects contain native extensions. I have distilled unexpected behavior behind observed test failures in minimal C extension: https://github.com/oleksandr-pavlyk/reproducer-for-question-about-numpy2 The extension defines a single Python function which does expects numpy.ndarray, and queries its itemsize in two ways 1. By calling C function declared in "_aux.h" defined in "_aux.c" to call PyArray_ITEMSIZE and return the result 2. By calling PyArray_ITEMSIZE directly https://github.com/oleksandr-pavlyk/reproducer-for-question-about-numpy2/blo... The result obtained by calling C function is always 0, while direct call gives the correct result. I am hoping for advice about what is wrong and how to fix it. Thank you, Sasha
Hi, The issue is caused by improperly importing the numpy C API. If you apply this diff, it will work: diff --git a/_aux.c b/_aux.c index e3f8f32..435b612 100644 --- a/_aux.c +++ b/_aux.c @@ -1,4 +1,6 @@ #include "Python.h" +#define NO_IMPORT_ARRAY +#define PY_ARRAY_UNIQUE_SYMBOL ExtModule #include "numpy/arrayobject.h" #include "_aux.h" diff --git a/ext.c b/ext.c index 65ad2c2..0e8eb3e 100644 --- a/ext.c +++ b/ext.c @@ -1,5 +1,6 @@ #define PY_SSIZE_T_CLEAN #include "Python.h" +#define PY_ARRAY_UNIQUE_SYMBOL ExtModule #include "numpy/arrayobject.h" #include "_aux.h" See also this new docs page, which hopefully clarifies this sort of arcane point: https://numpy.org/devdocs/reference/c-api/array.html#including-and-importing... We were a bit loose in what we allowed before, effectively leaking details of the numpy C API. We cleaned that up, but that means C extensions now need to do this import dance correctly. Hope that helps, Nathan On Fri, May 24, 2024 at 12:52 PM Pavlyk, Oleksandr < oleksandr.pavlyk@intel.com> wrote:
I am working to transition mkl_fft and mkl_random to NumPy 2.0.
Both of these projects contain native extensions.
I have distilled unexpected behavior behind observed test failures in minimal C extension:
https://github.com/oleksandr-pavlyk/reproducer-for-question-about-numpy2
The extension defines a single Python function which does expects numpy.ndarray, and queries its itemsize in two ways
1. By calling C function declared in “_aux.h” defined in “_aux.c” to call PyArray_ITEMSIZE and return the result 2. By calling PyArray_ITEMSIZE directly
https://github.com/oleksandr-pavlyk/reproducer-for-question-about-numpy2/blo...
The result obtained by calling C function is always 0, while direct call gives the correct result.
I am hoping for advice about what is wrong and how to fix it.
Thank you, Sasha _______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-leave@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: nathan12343@gmail.com
participants (2)
-
Nathan
-
Pavlyk, Oleksandr