From 990c392e6998e72a689512ac0c27c67e179daa85 Mon Sep 17 00:00:00 2001 From: Adam Reichold Date: Tue, 1 Mar 2022 09:59:28 +0100 Subject: [PATCH 1/3] Drop the re-exports of PyMem_Raw* calls as those are not available using the limited API. --- examples/simple/Cargo.toml | 2 +- src/npyffi/array.rs | 6 ------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/examples/simple/Cargo.toml b/examples/simple/Cargo.toml index 1adc0243d..9c77bffdd 100644 --- a/examples/simple/Cargo.toml +++ b/examples/simple/Cargo.toml @@ -9,5 +9,5 @@ name = "rust_ext" crate-type = ["cdylib"] [dependencies] -pyo3 = { version = "0.16", features = ["extension-module"] } +pyo3 = { version = "0.16", features = ["extension-module", "abi3-py37"] } numpy = { path = "../.." } diff --git a/src/npyffi/array.rs b/src/npyffi/array.rs index cda4ffd30..188e3426a 100644 --- a/src/npyffi/array.rs +++ b/src/npyffi/array.rs @@ -394,12 +394,6 @@ pub unsafe fn PyArray_CheckExact(py: Python, op: *mut PyObject) -> c_int { (ffi::Py_TYPE(op) == PY_ARRAY_API.get_type_object(py, NpyTypes::PyArray_Type)) as _ } -// these are under `#if NPY_USE_PYMEM == 1` which seems to be always defined as 1 -pub use pyo3::ffi::{ - PyMem_RawFree as PyArray_free, PyMem_RawMalloc as PyArray_malloc, - PyMem_RawRealloc as PyArray_realloc, -}; - #[cfg(test)] mod tests { use super::PY_ARRAY_API; From 712626b6070226df1e2f460d28028cddd9b3bbff Mon Sep 17 00:00:00 2001 From: Adam Reichold Date: Tue, 1 Mar 2022 10:26:42 +0100 Subject: [PATCH 2/3] Bring the PyArray_* allocation functions back conditionally based on the enabled PyO3 features. --- Cargo.toml | 3 +++ build.rs | 5 +++++ src/npyffi/array.rs | 7 +++++++ 3 files changed, 15 insertions(+) create mode 100644 build.rs diff --git a/Cargo.toml b/Cargo.toml index f7f42e1c5..ef172c3c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,9 @@ num-traits = "0.2" ndarray = ">= 0.13, < 0.16" pyo3 = { version = "0.16", default-features = false } +[build-dependencies] +pyo3-build-config = "0.16" + [dev-dependencies] pyo3 = { version = "0.16", features = ["auto-initialize"] } diff --git a/build.rs b/build.rs new file mode 100644 index 000000000..926c3a66c --- /dev/null +++ b/build.rs @@ -0,0 +1,5 @@ +use pyo3_build_config::use_pyo3_cfgs; + +fn main() { + use_pyo3_cfgs(); +} diff --git a/src/npyffi/array.rs b/src/npyffi/array.rs index 188e3426a..8fd4ba406 100644 --- a/src/npyffi/array.rs +++ b/src/npyffi/array.rs @@ -394,6 +394,13 @@ pub unsafe fn PyArray_CheckExact(py: Python, op: *mut PyObject) -> c_int { (ffi::Py_TYPE(op) == PY_ARRAY_API.get_type_object(py, NpyTypes::PyArray_Type)) as _ } +// these are under `#if NPY_USE_PYMEM == 1` which seems to be always defined as 1 +#[cfg(not(Py_LIMITED_API))] +pub use pyo3::ffi::{ + PyMem_RawFree as PyArray_free, PyMem_RawMalloc as PyArray_malloc, + PyMem_RawRealloc as PyArray_realloc, +}; + #[cfg(test)] mod tests { use super::PY_ARRAY_API; From ea410caee5873cf5c5ed56db56baf2f51b208e6e Mon Sep 17 00:00:00 2001 From: Adam Reichold Date: Thu, 3 Mar 2022 21:32:51 +0100 Subject: [PATCH 3/3] Remove the PyArray_* allocation functions again, but give users a hint where to find the underlying PyO3 functions. This reverts commit 712626b6070226df1e2f460d28028cddd9b3bbff. --- Cargo.toml | 3 --- build.rs | 5 ----- src/npyffi/array.rs | 14 ++++++-------- 3 files changed, 6 insertions(+), 16 deletions(-) delete mode 100644 build.rs diff --git a/Cargo.toml b/Cargo.toml index ef172c3c0..f7f42e1c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,9 +21,6 @@ num-traits = "0.2" ndarray = ">= 0.13, < 0.16" pyo3 = { version = "0.16", default-features = false } -[build-dependencies] -pyo3-build-config = "0.16" - [dev-dependencies] pyo3 = { version = "0.16", features = ["auto-initialize"] } diff --git a/build.rs b/build.rs deleted file mode 100644 index 926c3a66c..000000000 --- a/build.rs +++ /dev/null @@ -1,5 +0,0 @@ -use pyo3_build_config::use_pyo3_cfgs; - -fn main() { - use_pyo3_cfgs(); -} diff --git a/src/npyffi/array.rs b/src/npyffi/array.rs index 8fd4ba406..1470f8aac 100644 --- a/src/npyffi/array.rs +++ b/src/npyffi/array.rs @@ -1,9 +1,14 @@ //! Low-Level binding for [Array API](https://numpy.org/doc/stable/reference/c-api/array.html) -use libc::FILE; +//! +//! Note that NumPy's low-level allocation functions `PyArray_{malloc,realloc,free}` are not part of this module. +//! The reason is that they would be re-exports of the `PyMem_Raw{Malloc,Realloc,Free}` functions from PyO3, +//! but those are not unconditionally exported, i.e. they are not available when using the limited Python C-API. + use std::cell::Cell; use std::os::raw::*; use std::ptr::null; +use libc::FILE; use pyo3::ffi::{self, PyObject, PyTypeObject}; use crate::npyffi::*; @@ -394,13 +399,6 @@ pub unsafe fn PyArray_CheckExact(py: Python, op: *mut PyObject) -> c_int { (ffi::Py_TYPE(op) == PY_ARRAY_API.get_type_object(py, NpyTypes::PyArray_Type)) as _ } -// these are under `#if NPY_USE_PYMEM == 1` which seems to be always defined as 1 -#[cfg(not(Py_LIMITED_API))] -pub use pyo3::ffi::{ - PyMem_RawFree as PyArray_free, PyMem_RawMalloc as PyArray_malloc, - PyMem_RawRealloc as PyArray_realloc, -}; - #[cfg(test)] mod tests { use super::PY_ARRAY_API;