From 4cfe2212abf2f60f88d6491e92e582004480cbac Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Wed, 12 Jun 2019 23:27:07 +0200 Subject: [PATCH 1/2] Support converting Array1 --- src/types.rs | 11 +++++++++++ tests/array.rs | 22 +++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/types.rs b/src/types.rs index d46945678..9ab3ffedc 100644 --- a/src/types.rs +++ b/src/types.rs @@ -105,6 +105,17 @@ impl_type_num!(c32, Complex32, NPY_CFLOAT); impl_type_num!(c64, Complex64, NPY_CDOUBLE); impl_type_num!(*mut PyObject, PyObject, NPY_OBJECT); +cfg_if! { + if #[cfg(all(target_pointer_width = "64", windows))] { + impl_type_num!(usize, Uint64, NPY_ULONGLONG); + } else if #[cfg(all(target_pointer_width = "64", not (windows)))] { + impl_type_num!(usize, Uint64, NPY_ULONG, NPY_ULONGLONG); + } else if #[cfg(all(target_pointer_width = "32", windows))] { + impl_type_num!(usize, Uint32, NPY_UINT, NPY_ULONG); + } else if #[cfg(all(target_pointer_width = "32", not (windows)))] { + impl_type_num!(usize, Uint32, NPY_UINT); + } +} cfg_if! { if #[cfg(any(target_pointer_width = "32", windows))] { impl_type_num!(i32, Int32, NPY_INT, NPY_LONG); diff --git a/tests/array.rs b/tests/array.rs index 5c273c9e2..ac98eedcd 100644 --- a/tests/array.rs +++ b/tests/array.rs @@ -249,7 +249,27 @@ macro_rules! small_array_test { }; } -small_array_test!(i8 u8 i16 u16 i32 u32 i64 u64); +small_array_test!(i8 u8 i16 u16 i32 u32 i64 u64 usize); + + +#[test] +fn array_usize_dtype() { + let gil = pyo3::Python::acquire_gil(); + let py = gil.python(); + let locals = PyDict::new(py); + + let a: Vec = vec![1, 2, 3]; + let x = a.into_pyarray(py); + let x_repr = format!("{:?}", x); + + let x_repr_expected = if cfg!(target_pointer_width = "64") { + "array([1, 2, 3], dtype=uint64)" + } else { + "array([1, 2, 3], dtype=uint32)" + }; + + assert_eq!(x_repr, x_repr_expected); +} #[test] fn array_cast() { From 65643f9172df60ab9d387887281b61e58c2f9226 Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Thu, 13 Jun 2019 11:57:38 +0200 Subject: [PATCH 2/2] Review comments --- src/types.rs | 4 ++-- tests/array.rs | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/types.rs b/src/types.rs index 9ab3ffedc..7941bfb8a 100644 --- a/src/types.rs +++ b/src/types.rs @@ -108,11 +108,11 @@ impl_type_num!(*mut PyObject, PyObject, NPY_OBJECT); cfg_if! { if #[cfg(all(target_pointer_width = "64", windows))] { impl_type_num!(usize, Uint64, NPY_ULONGLONG); - } else if #[cfg(all(target_pointer_width = "64", not (windows)))] { + } else if #[cfg(all(target_pointer_width = "64", not(windows)))] { impl_type_num!(usize, Uint64, NPY_ULONG, NPY_ULONGLONG); } else if #[cfg(all(target_pointer_width = "32", windows))] { impl_type_num!(usize, Uint32, NPY_UINT, NPY_ULONG); - } else if #[cfg(all(target_pointer_width = "32", not (windows)))] { + } else if #[cfg(all(target_pointer_width = "32", not(windows)))] { impl_type_num!(usize, Uint32, NPY_UINT); } } diff --git a/tests/array.rs b/tests/array.rs index ac98eedcd..0951a30dc 100644 --- a/tests/array.rs +++ b/tests/array.rs @@ -251,7 +251,6 @@ macro_rules! small_array_test { small_array_test!(i8 u8 i16 u16 i32 u32 i64 u64 usize); - #[test] fn array_usize_dtype() { let gil = pyo3::Python::acquire_gil(); @@ -261,13 +260,12 @@ fn array_usize_dtype() { let a: Vec = vec![1, 2, 3]; let x = a.into_pyarray(py); let x_repr = format!("{:?}", x); - + let x_repr_expected = if cfg!(target_pointer_width = "64") { "array([1, 2, 3], dtype=uint64)" } else { "array([1, 2, 3], dtype=uint32)" }; - assert_eq!(x_repr, x_repr_expected); }