-
Notifications
You must be signed in to change notification settings - Fork 132
Select Numpy C API version #534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| fn main() { | ||
| pyo3_build_config::use_pyo3_cfgs(); | ||
| numpy_build_config::use_numpy_cfgs(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| [package] | ||
| name = "numpy-build-config" | ||
| version = "0.28.0" | ||
| authors = [ | ||
| "The rust-numpy Project Developers", | ||
| "PyO3 Project and Contributors <https://github.com/PyO3>", | ||
| ] | ||
| description = "Build configuration for the numpy crate" | ||
| edition = "2021" | ||
| rust-version = "1.83" | ||
| repository = "https://github.com/PyO3/rust-numpy" | ||
| license = "BSD-2-Clause" | ||
|
|
||
| [dependencies] | ||
|
|
||
| [features] | ||
| default = [] | ||
| target-npy115 = [] | ||
| target-npy116 = ["target-npy115"] | ||
| target-npy117 = ["target-npy116"] | ||
| target-npy118 = ["target-npy117"] | ||
| target-npy119 = ["target-npy118"] | ||
| target-npy120 = ["target-npy119"] | ||
| target-npy121 = ["target-npy120"] | ||
| target-npy122 = ["target-npy121"] | ||
| target-npy123 = ["target-npy122"] | ||
| target-npy124 = ["target-npy123"] | ||
| target-npy125 = ["target-npy124"] | ||
| target-npy20 = ["target-npy125"] | ||
| target-npy21 = ["target-npy20"] | ||
| target-npy22 = ["target-npy21"] | ||
| target-npy23 = ["target-npy22"] | ||
| target-npy24 = ["target-npy23"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| BSD 2-Clause License | ||
|
|
||
| Copyright (c) 2017, Toshiki Teramura | ||
| All rights reserved. | ||
|
|
||
| Redistribution and use in source and binary forms, with or without | ||
| modification, are permitted provided that the following conditions are met: | ||
|
|
||
| * Redistributions of source code must retain the above copyright notice, this | ||
| list of conditions and the following disclaimer. | ||
|
|
||
| * Redistributions in binary form must reproduce the above copyright notice, | ||
| this list of conditions and the following disclaimer in the documentation | ||
| and/or other materials provided with the distribution. | ||
|
|
||
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
| FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
| OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| #[derive(Debug, Clone, Copy)] | ||
| pub struct NumpyVersion { | ||
| pub minor: u32, | ||
| pub major: u32, | ||
| } | ||
|
|
||
| #[allow(non_snake_case)] | ||
| impl NumpyVersion { | ||
| const fn V1(minor: u32) -> Self { | ||
| Self { major: 1, minor } | ||
| } | ||
| const fn V2(minor: u32) -> Self { | ||
| Self { major: 2, minor } | ||
| } | ||
| } | ||
|
|
||
| impl NumpyVersion { | ||
| /// An iterator over supported versions of numpy API. | ||
| pub fn supported() -> impl Iterator<Item = Self> { | ||
| SUPPORTED_VERSIONS.iter().copied() | ||
| } | ||
|
|
||
| /// An iterator over enabled versions of numpy API. | ||
| pub fn enabled() -> impl Iterator<Item = Self> { | ||
| ENABLED_VERSIONS.iter().copied() | ||
| } | ||
| } | ||
|
|
||
| const SUPPORTED_VERSIONS: &[NumpyVersion] = &[ | ||
| NumpyVersion::V1(15), | ||
| NumpyVersion::V1(16), | ||
| NumpyVersion::V1(17), | ||
| NumpyVersion::V1(18), | ||
| NumpyVersion::V1(19), | ||
| NumpyVersion::V1(20), | ||
| NumpyVersion::V1(21), | ||
| NumpyVersion::V1(22), | ||
| NumpyVersion::V1(23), | ||
| NumpyVersion::V1(24), | ||
| NumpyVersion::V1(25), | ||
| NumpyVersion::V2(0), | ||
| NumpyVersion::V2(1), | ||
| NumpyVersion::V2(2), | ||
| NumpyVersion::V2(3), | ||
| NumpyVersion::V2(4), | ||
| ]; | ||
|
|
||
| const ENABLED_VERSIONS: &[NumpyVersion] = &[ | ||
| #[cfg(feature = "target-npy115")] | ||
| NumpyVersion::V1(15), // 0x0000000c | ||
| #[cfg(any( | ||
| feature = "target-npy116", | ||
| feature = "target-npy117", | ||
| feature = "target-npy118", | ||
| feature = "target-npy119" | ||
| ))] | ||
| NumpyVersion::V1(16), // 0x0000000d | ||
| #[cfg(any( | ||
| feature = "target-npy116", | ||
| feature = "target-npy117", | ||
| feature = "target-npy118", | ||
| feature = "target-npy119" | ||
| ))] | ||
| NumpyVersion::V1(17), // 0x0000000d | ||
| #[cfg(any( | ||
| feature = "target-npy116", | ||
| feature = "target-npy117", | ||
| feature = "target-npy118", | ||
| feature = "target-npy119" | ||
| ))] | ||
| NumpyVersion::V1(18), // 0x0000000d | ||
| #[cfg(any( | ||
| feature = "target-npy116", | ||
| feature = "target-npy117", | ||
| feature = "target-npy118", | ||
| feature = "target-npy119" | ||
| ))] | ||
| NumpyVersion::V1(19), // 0x0000000d | ||
| #[cfg(any(feature = "target-npy120", feature = "target-npy121"))] | ||
| NumpyVersion::V1(20), // 0x0000000e | ||
| #[cfg(any(feature = "target-npy120", feature = "target-npy121"))] | ||
| NumpyVersion::V1(21), // 0x0000000e | ||
| #[cfg(feature = "target-npy122")] | ||
| NumpyVersion::V1(22), // 0x0000000f | ||
| #[cfg(any(feature = "target-npy123", feature = "target-npy124"))] | ||
| NumpyVersion::V1(23), // 0x00000010 | ||
| #[cfg(any(feature = "target-npy123", feature = "target-npy124"))] | ||
| NumpyVersion::V1(24), // 0x00000010 | ||
| #[cfg(feature = "target-npy125")] | ||
| NumpyVersion::V1(25), // 0x00000011 | ||
| #[cfg(feature = "target-npy20")] | ||
| NumpyVersion::V2(0), // 0x00000012 | ||
| #[cfg(any(feature = "target-npy21", feature = "target-npy22"))] | ||
| NumpyVersion::V2(1), // 0x00000013 | ||
| #[cfg(any(feature = "target-npy21", feature = "target-npy22"))] | ||
| NumpyVersion::V2(2), // 0x00000013 | ||
| #[cfg(feature = "target-npy23")] | ||
| NumpyVersion::V2(3), // 0x00000014 | ||
| #[cfg(feature = "target-npy24")] | ||
| NumpyVersion::V2(4), // 0x00000015 | ||
| ]; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| use self::impl_::NumpyVersion; | ||
|
|
||
| mod impl_; | ||
|
|
||
| pub fn use_numpy_cfgs() { | ||
| print_expected_features(); | ||
| print_enabled_features(); | ||
| } | ||
|
|
||
| fn print_expected_features() { | ||
| for version in NumpyVersion::supported() { | ||
| println!( | ||
| "cargo:rustc-check-cfg=cfg(Numpy_{}_{})", | ||
| version.major, version.minor | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| fn print_enabled_features() { | ||
| for version in NumpyVersion::enabled() { | ||
| println!("cargo:rustc-cfg=Numpy_{}_{}", version.major, version.minor); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,12 @@ pub struct PyArrayObject { | |
| pub descr: *mut PyArray_Descr, | ||
| pub flags: c_int, | ||
| pub weakreflist: *mut PyObject, | ||
|
|
||
| #[cfg(Numpy_1_20)] | ||
| pub _buffer_info: *mut c_void, | ||
|
|
||
| #[cfg(Numpy_1_22)] | ||
| pub mem_handler: *mut PyObject, | ||
| } | ||
|
|
||
| #[repr(C)] | ||
|
|
@@ -32,6 +38,19 @@ pub struct PyArray_Descr { | |
| pub byteorder: c_char, | ||
| pub _former_flags: c_char, | ||
| pub type_num: c_int, | ||
|
|
||
| #[cfg(Numpy_2_0)] | ||
| pub flags: npy_uint64, | ||
| #[cfg(Numpy_2_0)] | ||
| pub elsize: npy_intp, | ||
| #[cfg(Numpy_2_0)] | ||
| pub alignment: npy_intp, | ||
| #[cfg(Numpy_2_0)] | ||
| pub metadata: *mut PyObject, | ||
| #[cfg(Numpy_2_0)] | ||
| pub hash: npy_hash_t, | ||
| #[cfg(Numpy_2_0)] | ||
| pub reserved_null: [*mut c_void; 2], | ||
| } | ||
|
|
||
| #[repr(C)] | ||
|
|
@@ -365,11 +384,31 @@ pub struct PyUFuncObject { | |
| pub core_offsets: *mut c_int, | ||
| pub core_signature: *mut c_char, | ||
| pub type_resolver: PyUFunc_TypeResolutionFunc, | ||
| pub legacy_inner_loop_selector: PyUFunc_LegacyInnerLoopSelectionFunc, | ||
| pub reserved2: *mut c_void, | ||
| pub masked_inner_loop_selector: PyUFunc_MaskedInnerLoopSelectionFunc, | ||
| pub dict: *mut PyObject, | ||
|
|
||
| #[cfg(all(Py_3_8, not(Py_LIMITED_API)))] | ||
| pub vectorcall: Option<vectorcallfunc>, | ||
| #[cfg(not(all(Py_3_8, not(Py_LIMITED_API))))] | ||
| pub vectorcall: *mut c_void, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was changed to |
||
|
|
||
| pub reserved3: *mut c_void, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Separately from the discussion in this PR, it makes sense to fix these incorrect bindings. Can you fix the issues I pointed out above and send in a PR that doesn't depend on details that changed in later NumPy versions? I think that means wrapping the fields that changed as |
||
| pub op_flags: *mut npy_uint32, | ||
| pub iter_flags: npy_uint32, | ||
|
|
||
| #[cfg(Numpy_1_16)] | ||
| pub core_dim_sizes: *mut npy_intp, | ||
| #[cfg(Numpy_1_16)] | ||
| pub core_dim_flags: *mut npy_uint32, | ||
| #[cfg(Numpy_1_16)] | ||
| pub identity_value: *mut PyObject, | ||
|
|
||
| #[cfg(Numpy_1_22)] | ||
| pub _dispatch_cache: *mut c_void, | ||
| #[cfg(Numpy_1_22)] | ||
| pub _loops: *mut PyObject, | ||
|
|
||
| #[cfg(Numpy_2_1)] | ||
| pub process_core_dims_func: PyUFunc_ProcessCoreDimsFunc, | ||
| } | ||
|
|
||
| pub type PyUFuncGenericFunction = | ||
|
|
@@ -415,6 +454,10 @@ pub type PyUFunc_MaskedInnerLoopSelectionFunc = Option< | |
| ) -> c_int, | ||
| >; | ||
|
|
||
| #[cfg(Numpy_2_1)] | ||
| pub type PyUFunc_ProcessCoreDimsFunc = | ||
| Option<unsafe extern "C" fn(*mut PyUFuncObject, *mut npy_intp) -> c_int>; | ||
|
|
||
| #[repr(C)] | ||
| #[derive(Debug, Copy, Clone)] | ||
| pub struct NpyIter([u8; 0]); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was changed to dict only relatively recently in 2024, see numpy/numpy#27735. It was first available in NumPy 2.2.