Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

- Unreleased
- Adjust struct definitions to match the selected NumPy C API version(#534)
- Add features to select the NumPy C API version (#534)
- Fix PyArray_DTypeMeta definition when Py_LIMITED_API is disabled (#532)

- v0.28.0
Expand Down
35 changes: 33 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ authors = [
]
description = "PyO3-based Rust bindings of the NumPy C-API"
documentation = "https://docs.rs/numpy"
edition = "2021"
rust-version = "1.83"
edition.workspace = true
rust-version.workspace = true
repository = "https://github.com/PyO3/rust-numpy"
categories = ["api-bindings", "development-tools::ffi", "science"]
keywords = ["python", "numpy", "ffi", "pyo3"]
Expand All @@ -20,6 +20,29 @@ exclude = [
"x.py",
]

[features]
default = ["target-npy119"]

# Default and minimum supported version are chosen to match the content of
# header `numpy/_core/include/numpy/numpyconfig.h`` in the first available
# version of numpy v2.
target-npy115 = ["numpy-build-config/target-npy115"]
target-npy116 = ["numpy-build-config/target-npy116"]
target-npy117 = ["numpy-build-config/target-npy117"]
target-npy118 = ["numpy-build-config/target-npy118"]
target-npy119 = ["numpy-build-config/target-npy119"]
target-npy120 = ["numpy-build-config/target-npy120"]
target-npy121 = ["numpy-build-config/target-npy121"]
target-npy122 = ["numpy-build-config/target-npy122"]
target-npy123 = ["numpy-build-config/target-npy123"]
target-npy124 = ["numpy-build-config/target-npy124"]
target-npy125 = ["numpy-build-config/target-npy125"]
target-npy20 = ["numpy-build-config/target-npy20"]
target-npy21 = ["numpy-build-config/target-npy21"]
target-npy22 = ["numpy-build-config/target-npy22"]
target-npy23 = ["numpy-build-config/target-npy23"]
target-npy24 = ["numpy-build-config/target-npy24"]

[dependencies]
half = { version = "2.0", default-features = false, optional = true }
libc = "0.2"
Expand All @@ -37,6 +60,7 @@ nalgebra = { version = ">=0.30, <0.35", default-features = false, features = ["s

[build-dependencies]
pyo3-build-config = { version = "0.28", features = ["resolve-config"]}
numpy-build-config = { path = "numpy-build-config", version = "0.28.0" }

[package.metadata.docs.rs]
all-features = true
Expand All @@ -47,3 +71,10 @@ elided-lifetimes-in-paths = "deny"

[lints.clippy]
needless-lifetimes = "allow"

[workspace.package]
edition = "2021"
rust-version = "1.83"

[workspace]
members = ["numpy-build-config"]
1 change: 1 addition & 0 deletions build.rs
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();
}
33 changes: 33 additions & 0 deletions numpy-build-config/Cargo.toml
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"]
25 changes: 25 additions & 0 deletions numpy-build-config/LICENSE
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.
101 changes: 101 additions & 0 deletions numpy-build-config/src/impl_.rs
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
];
23 changes: 23 additions & 0 deletions numpy-build-config/src/lib.rs
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);
}
}
49 changes: 46 additions & 3 deletions src/npyffi/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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)]
Expand Down Expand Up @@ -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,

pub reserved3: *mut c_void,
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 =
Expand Down Expand Up @@ -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]);
Expand Down
6 changes: 6 additions & 0 deletions src/npyffi/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ pub enum NPY_SELECTKIND {
NPY_INTROSELECT = 0,
}

#[cfg(Numpy_2_4)]
pub const NPY_SAME_VALUE_CASTING_FLAG: u32 = 64;

#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum NPY_CASTING {
Expand All @@ -162,6 +165,9 @@ pub enum NPY_CASTING {
NPY_SAFE_CASTING = 2,
NPY_SAME_KIND_CASTING = 3,
NPY_UNSAFE_CASTING = 4,

#[cfg(Numpy_2_4)]
NPY_SAME_VALUE_CASTING = Self::NPY_UNSAFE_CASTING as u32 | NPY_SAME_VALUE_CASTING_FLAG,
}

#[repr(u32)]
Expand Down
Loading