Skip to content
Merged
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ mod rust_ext {

### Execute a Python program from Rust and get results

``` toml
```toml
[package]
name = "numpy-test"

Expand All @@ -96,15 +96,15 @@ numpy = "0.29"

```rust
use numpy::{PyArray1, PyArrayMethods};
use pyo3::{types::{IntoPyDict, PyAnyMethods}, PyResult, Python, ffi::c_str};
use pyo3::{types::{IntoPyDict, PyAnyMethods}, PyResult, Python};

fn main() -> PyResult<()> {
Python::attach(|py| {
let np = py.import("numpy")?;
let locals = [("np", np)].into_py_dict(py)?;

let pyarray = py
.eval(c_str!("np.absolute(np.array([-1, -2, -3], dtype='int32'))"), Some(&locals), None)?
.eval(c"np.absolute(np.array([-1, -2, -3], dtype='int32'))", Some(&locals), None)?
.cast_into::<PyArray1<i32>>()?;

let readonly = pyarray.readonly();
Expand Down
11 changes: 3 additions & 8 deletions benches/array_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::hint::black_box;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use numpy::{PyArrayLike1, PyArrayLike2, PyArrayLike3};
use pyo3::{
ffi::c_str,
types::{PyAnyMethods, PyDict},
Python,
};
Expand All @@ -18,11 +17,7 @@ fn extract_array_like_1(c: &mut Criterion) {
locals.set_item("size", size).unwrap();

let list = py
.eval(
c_str!("[float(i) for i in range(size)]"),
Some(&locals),
None,
)
.eval(c"[float(i) for i in range(size)]", Some(&locals), None)
.unwrap();

group.throughput(Throughput::Elements(size as u64));
Expand All @@ -48,7 +43,7 @@ fn extract_array_like_2(c: &mut Criterion) {

let list = py
.eval(
c_str!("[[float(i + j) for i in range(size)] for j in range(size)]"),
c"[[float(i + j) for i in range(size)] for j in range(size)]",
Some(&locals),
None,
)
Expand Down Expand Up @@ -77,7 +72,7 @@ fn extract_array_like_3(c: &mut Criterion) {

let list = py
.eval(
c_str!("[[[float(i + j + k) for i in range(size)] for j in range(size)] for k in range(size)]"),
c"[[[float(i + j + k) for i in range(size)] for j in range(size)] for k in range(size)]",
Some(&locals),
None,
)
Expand Down
4 changes: 2 additions & 2 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -953,12 +953,12 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> + Sized {
///
/// ```
/// use numpy::{PyArray2, PyArrayMethods};
/// use pyo3::{Python, types::PyAnyMethods, ffi::c_str};
/// use pyo3::{Python, types::PyAnyMethods};
///
/// # fn main() -> pyo3::PyResult<()> {
/// Python::attach(|py| {
/// let pyarray= py
/// .eval(c_str!("__import__('numpy').array([[0, 1], [2, 3]], dtype='int64')"), None, None)?
/// .eval(c"__import__('numpy').array([[0, 1], [2, 3]], dtype='int64')", None, None)?
/// .cast_into::<PyArray2<i64>>()?;
///
/// assert_eq!(pyarray.to_vec()?, vec![0, 1, 2, 3]);
Expand Down
23 changes: 11 additions & 12 deletions src/borrow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@
//!
//! ```rust
//! use numpy::{PyArray1, PyArrayMethods};
//! use pyo3::{types::{IntoPyDict, PyAnyMethods}, Python, ffi::c_str};
//! use pyo3::{types::{IntoPyDict, PyAnyMethods}, Python};
//!
//! # fn main() -> pyo3::PyResult<()> {
//! Python::attach(|py| {
//! let array = PyArray1::arange(py, 0.0, 10.0, 1.0);
//! let locals = [("array", array)].into_py_dict(py)?;
//!
//! let view1 = py.eval(c_str!("array[:5]"), None, Some(&locals))?.cast_into::<PyArray1<f64>>()?;
//! let view2 = py.eval(c_str!("array[5:]"), None, Some(&locals))?.cast_into::<PyArray1<f64>>()?;
//! let view3 = py.eval(c_str!("array[::2]"), None, Some(&locals))?.cast_into::<PyArray1<f64>>()?;
//! let view4 = py.eval(c_str!("array[1::2]"), None, Some(&locals))?.cast_into::<PyArray1<f64>>()?;
//! let view1 = py.eval(c"array[:5]", None, Some(&locals))?.cast_into::<PyArray1<f64>>()?;
//! let view2 = py.eval(c"array[5:]", None, Some(&locals))?.cast_into::<PyArray1<f64>>()?;
//! let view3 = py.eval(c"array[::2]", None, Some(&locals))?.cast_into::<PyArray1<f64>>()?;
//! let view4 = py.eval(c"array[1::2]", None, Some(&locals))?.cast_into::<PyArray1<f64>>()?;
//!
//! {
//! let _view1 = view1.readwrite();
Expand All @@ -95,15 +95,15 @@
//! # use std::panic::{catch_unwind, AssertUnwindSafe};
//! #
//! use numpy::{PyArray2, PyArrayMethods};
//! use pyo3::{types::{IntoPyDict, PyAnyMethods}, Python, ffi::c_str};
//! use pyo3::{types::{IntoPyDict, PyAnyMethods}, Python};
//!
//! # fn main() -> pyo3::PyResult<()> {
//! Python::attach(|py| {
//! let array = PyArray2::<f64>::zeros(py, (10, 10), false);
//! let locals = [("array", array)].into_py_dict(py)?;
//!
//! let view1 = py.eval(c_str!("array[:, ::3]"), None, Some(&locals))?.cast_into::<PyArray2<f64>>()?;
//! let view2 = py.eval(c_str!("array[:, 1::3]"), None, Some(&locals))?.cast_into::<PyArray2<f64>>()?;
//! let view1 = py.eval(c"array[:, ::3]", None, Some(&locals))?.cast_into::<PyArray2<f64>>()?;
//! let view2 = py.eval(c"array[:, 1::3]", None, Some(&locals))?.cast_into::<PyArray2<f64>>()?;
//!
//! // A false conflict as the views do not actually share any elements.
//! let res = catch_unwind(AssertUnwindSafe(|| {
Expand Down Expand Up @@ -300,7 +300,7 @@ where
///
/// ```rust
/// # use pyo3::prelude::*;
/// use pyo3::{py_run, ffi::c_str};
/// use pyo3::py_run;
/// use numpy::{get_array_module, PyReadonlyArray2};
/// use nalgebra::{MatrixView, Const, Dyn};
///
Expand All @@ -318,7 +318,7 @@ where
///
/// # fn main() -> pyo3::PyResult<()> {
/// Python::attach(|py| {
/// let np = py.eval(c_str!("__import__('numpy')"), None, None)?;
/// let np = py.eval(c"__import__('numpy')", None, None)?;
/// let sum_standard_layout = wrap_pyfunction!(sum_standard_layout)(py)?;
/// let sum_dynamic_strides = wrap_pyfunction!(sum_dynamic_strides)(py)?;
///
Expand Down Expand Up @@ -682,7 +682,6 @@ mod tests {
};

use crate::array::PyArray1;
use pyo3::ffi::c_str;

#[test]
fn test_debug_formatting() {
Expand Down Expand Up @@ -728,7 +727,7 @@ mod tests {
// The view will make the internal reference check of `PyArray_Resize` fail.
let locals = [("array", &array)].into_py_dict(py).unwrap();
let _view = py
.eval(c_str!("array[:]"), None, Some(&locals))
.eval(c"array[:]", None, Some(&locals))
.unwrap()
.cast_into::<PyArray1<f64>>()
.unwrap();
Expand Down
36 changes: 17 additions & 19 deletions src/borrow/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::slice::from_raw_parts;
use std::sync::Mutex;

use num_integer::gcd;
use pyo3::ffi::c_str;
use pyo3::sync::PyOnceLock;
use pyo3::types::{PyAnyMethods, PyCapsuleMethods};
use pyo3::{exceptions::PyTypeError, types::PyCapsule, PyResult, Python};
Expand Down Expand Up @@ -154,7 +153,7 @@ fn insert_shared<'py>(py: Python<'py>) -> PyResult<NonNull<Shared>> {
// SAFETY: All versions of the shared borrow checking API start with a version field.
let version = unsafe {
*capsule
.pointer_checked(Some(c_str!("_RUST_NUMPY_BORROW_CHECKING_API")))?
.pointer_checked(Some(c"_RUST_NUMPY_BORROW_CHECKING_API"))?
.cast::<u64>()
.as_ptr() // FIXME(icxolu): use read on MSRV 1.80
};
Expand All @@ -164,7 +163,7 @@ fn insert_shared<'py>(py: Python<'py>) -> PyResult<NonNull<Shared>> {
)));
}

let ptr = capsule.pointer_checked(Some(c_str!("_RUST_NUMPY_BORROW_CHECKING_API")))?;
let ptr = capsule.pointer_checked(Some(c"_RUST_NUMPY_BORROW_CHECKING_API"))?;

// Intentionally leak a reference to the capsule
// so we can safely cache a pointer into its interior.
Expand Down Expand Up @@ -455,7 +454,6 @@ mod tests {
use crate::array::{PyArray, PyArray1, PyArray2, PyArray3, PyArrayMethods};
use crate::convert::IntoPyArray;
use crate::untyped_array::PyUntypedArrayMethods;
use pyo3::ffi::c_str;

struct BorrowFlagsState {
#[cfg(not(Py_GIL_DISABLED))]
Expand Down Expand Up @@ -535,7 +533,7 @@ mod tests {

let locals = [("array", &array)].into_py_dict(py).unwrap();
let view = py
.eval(c_str!("array[:,:,0]"), None, Some(&locals))
.eval(c"array[:,:,0]", None, Some(&locals))
.unwrap()
.cast_into::<PyArray2<f64>>()
.unwrap();
Expand Down Expand Up @@ -564,7 +562,7 @@ mod tests {

let locals = [("array", &array)].into_py_dict(py).unwrap();
let view = py
.eval(c_str!("array[:,:,0]"), None, Some(&locals))
.eval(c"array[:,:,0]", None, Some(&locals))
.unwrap()
.cast_into::<PyArray2<f64>>()
.unwrap();
Expand Down Expand Up @@ -599,7 +597,7 @@ mod tests {

let locals = [("array", &array)].into_py_dict(py).unwrap();
let view1 = py
.eval(c_str!("array[:,:,0]"), None, Some(&locals))
.eval(c"array[:,:,0]", None, Some(&locals))
.unwrap()
.cast_into::<PyArray2<f64>>()
.unwrap();
Expand All @@ -610,7 +608,7 @@ mod tests {

let locals = [("view1", &view1)].into_py_dict(py).unwrap();
let view2 = py
.eval(c_str!("view1[:,0]"), None, Some(&locals))
.eval(c"view1[:,0]", None, Some(&locals))
.unwrap()
.cast_into::<PyArray1<f64>>()
.unwrap();
Expand Down Expand Up @@ -647,7 +645,7 @@ mod tests {

let locals = [("array", &array)].into_py_dict(py).unwrap();
let view1 = py
.eval(c_str!("array[:,:,0]"), None, Some(&locals))
.eval(c"array[:,:,0]", None, Some(&locals))
.unwrap()
.cast_into::<PyArray2<f64>>()
.unwrap();
Expand All @@ -658,7 +656,7 @@ mod tests {

let locals = [("view1", &view1)].into_py_dict(py).unwrap();
let view2 = py
.eval(c_str!("view1[:,0]"), None, Some(&locals))
.eval(c"view1[:,0]", None, Some(&locals))
.unwrap()
.cast_into::<PyArray1<f64>>()
.unwrap();
Expand Down Expand Up @@ -701,7 +699,7 @@ mod tests {

let locals = [("array", &array)].into_py_dict(py).unwrap();
let view = py
.eval(c_str!("array[::-1,:,::-1]"), None, Some(&locals))
.eval(c"array[::-1,:,::-1]", None, Some(&locals))
.unwrap()
.cast_into::<PyArray3<f64>>()
.unwrap();
Expand Down Expand Up @@ -750,7 +748,7 @@ mod tests {
let locals = [("array", array)].into_py_dict(py).unwrap();

let view1 = py
.eval(c_str!("array[:,::3]"), None, Some(&locals))
.eval(c"array[:,::3]", None, Some(&locals))
.unwrap()
.cast_into::<PyArray2<f64>>()
.unwrap();
Expand All @@ -761,7 +759,7 @@ mod tests {
assert_eq!(key1.gcd_strides, 8);

let view2 = py
.eval(c_str!("array[:,1::3]"), None, Some(&locals))
.eval(c"array[:,1::3]", None, Some(&locals))
.unwrap()
.cast_into::<PyArray2<f64>>()
.unwrap();
Expand All @@ -772,7 +770,7 @@ mod tests {
assert_eq!(key2.gcd_strides, 8);

let view3 = py
.eval(c_str!("array[:,::2]"), None, Some(&locals))
.eval(c"array[:,::2]", None, Some(&locals))
.unwrap()
.cast_into::<PyArray2<f64>>()
.unwrap();
Expand All @@ -783,7 +781,7 @@ mod tests {
assert_eq!(key3.gcd_strides, 16);

let view4 = py
.eval(c_str!("array[:,1::2]"), None, Some(&locals))
.eval(c"array[:,1::2]", None, Some(&locals))
.unwrap()
.cast_into::<PyArray2<f64>>()
.unwrap();
Expand Down Expand Up @@ -852,7 +850,7 @@ mod tests {
let locals = [("array", array)].into_py_dict(py).unwrap();

let view1 = py
.eval(c_str!("array[:5]"), None, Some(&locals))
.eval(c"array[:5]", None, Some(&locals))
.unwrap()
.cast_into::<PyArray1<f64>>()
.unwrap();
Expand All @@ -871,7 +869,7 @@ mod tests {
}

let view2 = py
.eval(c_str!("array[5:]"), None, Some(&locals))
.eval(c"array[5:]", None, Some(&locals))
.unwrap()
.cast_into::<PyArray1<f64>>()
.unwrap();
Expand All @@ -892,7 +890,7 @@ mod tests {
}

let view3 = py
.eval(c_str!("array[5:]"), None, Some(&locals))
.eval(c"array[5:]", None, Some(&locals))
.unwrap()
.cast_into::<PyArray1<f64>>()
.unwrap();
Expand All @@ -916,7 +914,7 @@ mod tests {
}

let view4 = py
.eval(c_str!("array[7:]"), None, Some(&locals))
.eval(c"array[7:]", None, Some(&locals))
.unwrap()
.cast_into::<PyArray1<f64>>()
.unwrap();
Expand Down
15 changes: 7 additions & 8 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@
//!
//! ```
//! use numpy::{datetime::{units, Datetime, Timedelta}, PyArray1, PyArrayMethods};
//! use pyo3::{Python, types::PyAnyMethods, ffi::c_str};
//! use pyo3::{Python, types::PyAnyMethods};
//! # use pyo3::types::PyDict;
//!
//! # fn main() -> pyo3::PyResult<()> {
//! Python::attach(|py| {
//! # let locals = py
//! # .eval(c_str!("{ 'np': __import__('numpy') }"), None, None)?
//! # .eval(c"{ 'np': __import__('numpy') }", None, None)?
//! # .cast_into::<PyDict>()?;
//! #
//! let array = py
//! .eval(
//! c_str!("np.array([np.datetime64('2017-04-21')])"),
//! c"np.array([np.datetime64('2017-04-21')])",
//! None,
//! Some(&locals),
//! )?
Expand All @@ -35,7 +35,7 @@
//!
//! let array = py
//! .eval(
//! c_str!("np.array([np.datetime64('2022-03-29')]) - np.array([np.datetime64('2017-04-21')])"),
//! c"np.array([np.datetime64('2022-03-29')]) - np.array([np.datetime64('2017-04-21')])",
//! None,
//! Some(&locals),
//! )?
Expand Down Expand Up @@ -256,7 +256,6 @@ mod tests {
use super::*;

use pyo3::{
ffi::c_str,
py_run,
types::{PyDict, PyModule},
};
Expand All @@ -267,14 +266,14 @@ mod tests {
fn from_python_to_rust() {
Python::attach(|py| {
let locals = py
.eval(c_str!("{ 'np': __import__('numpy') }"), None, None)
.eval(c"{ 'np': __import__('numpy') }", None, None)
.unwrap()
.cast_into::<PyDict>()
.unwrap();

let array = py
.eval(
c_str!("np.array([np.datetime64('1970-01-01')])"),
c"np.array([np.datetime64('1970-01-01')])",
None,
Some(&locals),
)
Expand All @@ -295,7 +294,7 @@ mod tests {
*array.readwrite().get_mut(0).unwrap() = Timedelta::<units::Minutes>::from(5);

let np = py
.eval(c_str!("__import__('numpy')"), None, None)
.eval(c"__import__('numpy')", None, None)
.unwrap()
.cast_into::<PyModule>()
.unwrap();
Expand Down
Loading
Loading