Skip to content

Commit 2f92994

Browse files
committed
Add test for all integers
+ Fix bug in array -> PyArray
1 parent 977b256 commit 2f92994

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

src/convert.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ macro_rules! array_impls {
6363
$(
6464
impl<T: TypeNum> IntoPyArray for [T; $N] {
6565
type Item = T;
66-
fn into_pyarray(mut self, py: Python, np: &PyArrayModule) -> PyArray<T> {
66+
fn into_pyarray(self, py: Python, np: &PyArrayModule) -> PyArray<T> {
6767
let dims = [$N];
68-
let ptr = &mut self as *mut [T; $N];
68+
let ptr = Box::into_raw(Box::new(self));
6969
unsafe {
7070
PyArray::new_(py, np, &dims, null_mut(), ptr as *mut c_void)
7171
}

tests/array.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ fn iter_to_pyarray() {
9292
let gil = pyo3::Python::acquire_gil();
9393
let np = PyArrayModule::import(gil.python()).unwrap();
9494
let arr = PyArray::from_iter(gil.python(), &np, (0..10).map(|x| x * x));
95-
assert_eq!(arr.as_slice().unwrap(), &[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]);
95+
assert_eq!(
96+
arr.as_slice().unwrap(),
97+
&[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
98+
);
9699
}
97100

98101
#[test]
@@ -130,15 +133,6 @@ fn from_vec3() {
130133
);
131134
}
132135

133-
#[test]
134-
fn from_small_array() {
135-
let gil = pyo3::Python::acquire_gil();
136-
let np = PyArrayModule::import(gil.python()).unwrap();
137-
let array: [i32; 5] = [1, 2, 3, 4, 5];
138-
let pyarray = array.into_pyarray(gil.python(), &np);
139-
assert_eq!(pyarray.as_slice().unwrap(), &[1, 2, 3, 4, 5]);
140-
}
141-
142136
#[test]
143137
fn from_eval() {
144138
let gil = pyo3::Python::acquire_gil();
@@ -168,3 +162,22 @@ fn from_eval_fail() {
168162
assert!(converted.is_err());
169163
}
170164

165+
macro_rules! small_array_test {
166+
($($t: ident)+) => {
167+
#[test]
168+
fn from_small_array() {
169+
let gil = pyo3::Python::acquire_gil();
170+
let np = PyArrayModule::import(gil.python()).unwrap();
171+
$({
172+
let array: [$t; 2] = [$t::min_value(), $t::max_value()];
173+
let pyarray = array.into_pyarray(gil.python(), &np);
174+
assert_eq!(
175+
pyarray.as_slice().unwrap(),
176+
&[$t::min_value(), $t::max_value()]
177+
);
178+
})+
179+
}
180+
};
181+
}
182+
183+
small_array_test!(i8 u8 i16 u16 i32 u32 i64 u64);

0 commit comments

Comments
 (0)