diff --git a/pytests/src/buf_and_str.rs b/pytests/src/buf_and_str.rs index 3beda4bfb7d..7cddc686c03 100644 --- a/pytests/src/buf_and_str.rs +++ b/pytests/src/buf_and_str.rs @@ -43,8 +43,8 @@ impl BytesExtractor { #[pyfunction] fn return_memoryview(py: Python<'_>) -> PyResult<&PyMemoryView> { - let bytes = PyBytes::new(py, b"hello world"); - let memoryview = PyMemoryView::from(bytes)?; + let bytes: &PyAny = PyBytes::new(py, b"hello world").into(); + let memoryview = TryInto::try_into(bytes)?; Ok(memoryview) } diff --git a/src/types/bytearray.rs b/src/types/bytearray.rs index d68a7e7325c..f7306d2da35 100644 --- a/src/types/bytearray.rs +++ b/src/types/bytearray.rs @@ -253,8 +253,8 @@ impl<'py> TryFrom<&'py PyAny> for &'py PyByteArray { #[cfg(test)] mod tests { - use crate::exceptions; use crate::types::PyByteArray; + use crate::{exceptions, PyAny}; use crate::{PyObject, Python}; #[test] @@ -332,6 +332,17 @@ mod tests { }); } + #[test] + fn test_try_from() { + Python::with_gil(|py| { + let src = b"Hello Python"; + let bytearray: &PyAny = PyByteArray::new(py, src).into(); + let bytearray: &PyByteArray = TryInto::try_into(bytearray).unwrap(); + + assert_eq!(src, unsafe { bytearray.as_bytes() }); + }); + } + #[test] fn test_resize() { Python::with_gil(|py| {