Skip to content

Commit cb309a4

Browse files
committed
Unimplement TypeNum for *mut ffi::PyObject
1 parent 51d6020 commit cb309a4

File tree

4 files changed

+112
-142
lines changed

4 files changed

+112
-142
lines changed

src/types.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ pub use num_complex::Complex32 as c32;
55
pub use num_complex::Complex64 as c64;
66

77
use super::npyffi::NPY_TYPES;
8-
use pyo3::ffi::PyObject;
98

109
/// An enum type represents numpy data type.
1110
///
@@ -25,7 +24,6 @@ pub enum NpyDataType {
2524
Float64,
2625
Complex32,
2726
Complex64,
28-
PyObject,
2927
Unsupported,
3028
}
3129

@@ -47,7 +45,6 @@ impl NpyDataType {
4745
x if x == NPY_TYPES::NPY_DOUBLE as i32 => NpyDataType::Float64,
4846
x if x == NPY_TYPES::NPY_CFLOAT as i32 => NpyDataType::Complex32,
4947
x if x == NPY_TYPES::NPY_CDOUBLE as i32 => NpyDataType::Complex64,
50-
x if x == NPY_TYPES::NPY_OBJECT as i32 => NpyDataType::PyObject,
5148
_ => NpyDataType::Unsupported,
5249
}
5350
}
@@ -103,7 +100,6 @@ impl_type_num!(f32, Float32, NPY_FLOAT);
103100
impl_type_num!(f64, Float64, NPY_DOUBLE);
104101
impl_type_num!(c32, Complex32, NPY_CFLOAT);
105102
impl_type_num!(c64, Complex64, NPY_CDOUBLE);
106-
impl_type_num!(*mut PyObject, PyObject, NPY_OBJECT);
107103

108104
cfg_if! {
109105
if #[cfg(all(target_pointer_width = "64", windows))] {

tests/array.rs

Lines changed: 0 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use pyo3::{
44
prelude::*,
55
types::PyList,
66
types::{IntoPyDict, PyDict},
7-
AsPyPointer,
87
};
98

109
fn get_np_locals(py: Python<'_>) -> &'_ PyDict {
@@ -102,53 +101,6 @@ fn as_slice() {
102101
assert!(not_contiguous.as_slice().is_err())
103102
}
104103

105-
#[test]
106-
fn to_pyarray_vec() {
107-
let gil = pyo3::Python::acquire_gil();
108-
109-
let a = vec![1, 2, 3];
110-
let arr = a.to_pyarray(gil.python());
111-
println!("arr.shape = {:?}", arr.shape());
112-
assert_eq!(arr.shape(), [3]);
113-
assert_eq!(arr.as_slice().unwrap(), &[1, 2, 3])
114-
}
115-
116-
#[test]
117-
fn to_pyarray_array() {
118-
let gil = pyo3::Python::acquire_gil();
119-
120-
let a = Array3::<f64>::zeros((3, 4, 2));
121-
let shape = a.shape().iter().cloned().collect::<Vec<_>>();
122-
let strides = a.strides().iter().map(|d| d * 8).collect::<Vec<_>>();
123-
println!("a.shape = {:?}", a.shape());
124-
println!("a.strides = {:?}", a.strides());
125-
let pa = a.to_pyarray(gil.python());
126-
println!("pa.shape = {:?}", pa.shape());
127-
println!("pa.strides = {:?}", pa.strides());
128-
assert_eq!(pa.shape(), shape.as_slice());
129-
assert_eq!(pa.strides(), strides.as_slice());
130-
}
131-
132-
#[test]
133-
fn iter_to_pyarray() {
134-
let gil = pyo3::Python::acquire_gil();
135-
let arr = PyArray::from_iter(gil.python(), (0..10).map(|x| x * x));
136-
assert_eq!(
137-
arr.as_slice().unwrap(),
138-
&[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
139-
);
140-
}
141-
142-
#[test]
143-
fn long_iter_to_pyarray() {
144-
let gil = pyo3::Python::acquire_gil();
145-
let arr = PyArray::from_iter(gil.python(), (0u32..512).map(|x| x));
146-
let slice = arr.as_slice().unwrap();
147-
for (i, &elem) in slice.iter().enumerate() {
148-
assert_eq!(i as u32, elem);
149-
}
150-
}
151-
152104
#[test]
153105
fn is_instance() {
154106
let gil = pyo3::Python::acquire_gil();
@@ -253,42 +205,6 @@ fn from_eval_fail_by_dim() {
253205
.print_and_set_sys_last_vars(gil.python());
254206
}
255207

256-
macro_rules! small_array_test {
257-
($($t: ident)+) => {
258-
#[test]
259-
fn from_small_array() {
260-
let gil = pyo3::Python::acquire_gil();
261-
$({
262-
let array: [$t; 2] = [$t::min_value(), $t::max_value()];
263-
let pyarray = array.to_pyarray(gil.python());
264-
assert_eq!(
265-
pyarray.as_slice().unwrap(),
266-
&[$t::min_value(), $t::max_value()]
267-
);
268-
})+
269-
}
270-
};
271-
}
272-
273-
small_array_test!(i8 u8 i16 u16 i32 u32 i64 u64 usize);
274-
275-
#[test]
276-
fn array_usize_dtype() {
277-
let gil = pyo3::Python::acquire_gil();
278-
let py = gil.python();
279-
280-
let a: Vec<usize> = vec![1, 2, 3];
281-
let x = a.into_pyarray(py);
282-
let x_repr = format!("{:?}", x);
283-
284-
let x_repr_expected = if cfg!(target_pointer_width = "64") {
285-
"array([1, 2, 3], dtype=uint64)"
286-
} else {
287-
"array([1, 2, 3], dtype=uint32)"
288-
};
289-
assert_eq!(x_repr, x_repr_expected);
290-
}
291-
292208
#[test]
293209
fn array_cast() {
294210
let gil = pyo3::Python::acquire_gil();
@@ -297,57 +213,3 @@ fn array_cast() {
297213
let arr_i32: &PyArray2<i32> = arr_f64.cast(false).unwrap();
298214
assert_eq!(arr_i32.as_array(), array![[1, 2, 3], [1, 2, 3]]);
299215
}
300-
301-
#[test]
302-
fn into_pyarray_vec() {
303-
let gil = pyo3::Python::acquire_gil();
304-
let a = vec![1, 2, 3];
305-
let arr = a.into_pyarray(gil.python());
306-
assert_eq!(arr.as_slice().unwrap(), &[1, 2, 3])
307-
}
308-
309-
#[test]
310-
fn into_pyarray_array() {
311-
let gil = pyo3::Python::acquire_gil();
312-
let arr = Array3::<f64>::zeros((3, 4, 2));
313-
let shape = arr.shape().iter().cloned().collect::<Vec<_>>();
314-
let strides = arr.strides().iter().map(|d| d * 8).collect::<Vec<_>>();
315-
let py_arr = arr.into_pyarray(gil.python());
316-
assert_eq!(py_arr.shape(), shape.as_slice());
317-
assert_eq!(py_arr.strides(), strides.as_slice());
318-
}
319-
320-
#[test]
321-
fn into_pyarray_cant_resize() {
322-
let gil = pyo3::Python::acquire_gil();
323-
let a = vec![1, 2, 3];
324-
let arr = a.into_pyarray(gil.python());
325-
assert!(arr.resize(100).is_err())
326-
}
327-
328-
// TODO: Replace it by pyo3::py_run when https://github.com/PyO3/pyo3/pull/512 is released.
329-
macro_rules! py_run {
330-
($py:expr, $val:expr, $code:expr) => {{
331-
let d = pyo3::types::PyDict::new($py);
332-
d.set_item(stringify!($val), &$val).unwrap();
333-
$py.run($code, None, Some(d))
334-
.map_err(|e| {
335-
e.print($py);
336-
$py.run("import sys; sys.stderr.flush()", None, None)
337-
.unwrap();
338-
})
339-
.expect($code)
340-
}};
341-
}
342-
343-
#[test]
344-
fn into_obj_vec_to_pyarray() {
345-
let gil = pyo3::Python::acquire_gil();
346-
let py = gil.python();
347-
let dict = PyDict::new(py);
348-
let string = pyo3::types::PyString::new(py, "Hello python :)");
349-
let a = vec![dict.as_ptr(), string.as_ptr()];
350-
let arr = a.into_pyarray(py);
351-
py_run!(py, arr, "assert arr[0] == {}");
352-
py_run!(py, arr, "assert arr[1] == 'Hello python :)'");
353-
}

tests/py_to_rs.rs

Whitespace-only changes.

tests/to_py.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
use ndarray::*;
2+
use numpy::*;
3+
4+
#[test]
5+
fn to_pyarray_vec() {
6+
let gil = pyo3::Python::acquire_gil();
7+
8+
let a = vec![1, 2, 3];
9+
let arr = a.to_pyarray(gil.python());
10+
println!("arr.shape = {:?}", arr.shape());
11+
assert_eq!(arr.shape(), [3]);
12+
assert_eq!(arr.as_slice().unwrap(), &[1, 2, 3])
13+
}
14+
15+
#[test]
16+
fn to_pyarray_array() {
17+
let gil = pyo3::Python::acquire_gil();
18+
19+
let a = Array3::<f64>::zeros((3, 4, 2));
20+
let shape = a.shape().iter().cloned().collect::<Vec<_>>();
21+
let strides = a.strides().iter().map(|d| d * 8).collect::<Vec<_>>();
22+
println!("a.shape = {:?}", a.shape());
23+
println!("a.strides = {:?}", a.strides());
24+
let pa = a.to_pyarray(gil.python());
25+
println!("pa.shape = {:?}", pa.shape());
26+
println!("pa.strides = {:?}", pa.strides());
27+
assert_eq!(pa.shape(), shape.as_slice());
28+
assert_eq!(pa.strides(), strides.as_slice());
29+
}
30+
31+
#[test]
32+
fn iter_to_pyarray() {
33+
let gil = pyo3::Python::acquire_gil();
34+
let arr = PyArray::from_iter(gil.python(), (0..10).map(|x| x * x));
35+
assert_eq!(
36+
arr.as_slice().unwrap(),
37+
&[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
38+
);
39+
}
40+
41+
#[test]
42+
fn long_iter_to_pyarray() {
43+
let gil = pyo3::Python::acquire_gil();
44+
let arr = PyArray::from_iter(gil.python(), (0u32..512).map(|x| x));
45+
let slice = arr.as_slice().unwrap();
46+
for (i, &elem) in slice.iter().enumerate() {
47+
assert_eq!(i as u32, elem);
48+
}
49+
}
50+
51+
macro_rules! small_array_test {
52+
($($t: ident)+) => {
53+
#[test]
54+
fn from_small_array() {
55+
let gil = pyo3::Python::acquire_gil();
56+
$({
57+
let array: [$t; 2] = [$t::min_value(), $t::max_value()];
58+
let pyarray = array.to_pyarray(gil.python());
59+
assert_eq!(
60+
pyarray.as_slice().unwrap(),
61+
&[$t::min_value(), $t::max_value()]
62+
);
63+
})+
64+
}
65+
};
66+
}
67+
68+
small_array_test!(i8 u8 i16 u16 i32 u32 i64 u64 usize);
69+
70+
#[test]
71+
fn usize_dtype() {
72+
let gil = pyo3::Python::acquire_gil();
73+
let py = gil.python();
74+
75+
let a: Vec<usize> = vec![1, 2, 3];
76+
let x = a.into_pyarray(py);
77+
let x_repr = format!("{:?}", x);
78+
79+
let x_repr_expected = if cfg!(target_pointer_width = "64") {
80+
"array([1, 2, 3], dtype=uint64)"
81+
} else {
82+
"array([1, 2, 3], dtype=uint32)"
83+
};
84+
assert_eq!(x_repr, x_repr_expected);
85+
}
86+
87+
#[test]
88+
fn into_pyarray_vec() {
89+
let gil = pyo3::Python::acquire_gil();
90+
let a = vec![1, 2, 3];
91+
let arr = a.into_pyarray(gil.python());
92+
assert_eq!(arr.as_slice().unwrap(), &[1, 2, 3])
93+
}
94+
95+
#[test]
96+
fn into_pyarray_array() {
97+
let gil = pyo3::Python::acquire_gil();
98+
let arr = Array3::<f64>::zeros((3, 4, 2));
99+
let shape = arr.shape().iter().cloned().collect::<Vec<_>>();
100+
let strides = arr.strides().iter().map(|d| d * 8).collect::<Vec<_>>();
101+
let py_arr = arr.into_pyarray(gil.python());
102+
assert_eq!(py_arr.shape(), shape.as_slice());
103+
assert_eq!(py_arr.strides(), strides.as_slice());
104+
}
105+
106+
#[test]
107+
fn into_pyarray_cant_resize() {
108+
let gil = pyo3::Python::acquire_gil();
109+
let a = vec![1, 2, 3];
110+
let arr = a.into_pyarray(gil.python());
111+
assert!(arr.resize(100).is_err())
112+
}

0 commit comments

Comments
 (0)