Skip to content

Commit

Permalink
Merge pull request #1786 from mejrs/with_gil2
Browse files Browse the repository at this point in the history
tests: switch to python_with_gil
  • Loading branch information
davidhewitt authored Aug 13, 2021
2 parents 874e7e5 + 596c3ff commit 388c255
Show file tree
Hide file tree
Showing 19 changed files with 1,400 additions and 1,428 deletions.
108 changes: 54 additions & 54 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,64 +663,64 @@ mod tests {

#[test]
fn test_bytes_buffer() {
let gil = Python::acquire_gil();
let py = gil.python();
let bytes = py.eval("b'abcde'", None, None).unwrap();
let buffer = PyBuffer::get(bytes).unwrap();
assert_eq!(buffer.dimensions(), 1);
assert_eq!(buffer.item_count(), 5);
assert_eq!(buffer.format().to_str().unwrap(), "B");
assert_eq!(buffer.shape(), [5]);
// single-dimensional buffer is always contiguous
assert!(buffer.is_c_contiguous());
assert!(buffer.is_fortran_contiguous());

let slice = buffer.as_slice(py).unwrap();
assert_eq!(slice.len(), 5);
assert_eq!(slice[0].get(), b'a');
assert_eq!(slice[2].get(), b'c');

assert!(buffer.copy_to_slice(py, &mut [0u8]).is_err());
let mut arr = [0; 5];
buffer.copy_to_slice(py, &mut arr).unwrap();
assert_eq!(arr, b"abcde" as &[u8]);

assert!(buffer.copy_from_slice(py, &[0u8; 5]).is_err());
assert_eq!(buffer.to_vec(py).unwrap(), b"abcde");
Python::with_gil(|py| {
let bytes = py.eval("b'abcde'", None, None).unwrap();
let buffer = PyBuffer::get(bytes).unwrap();
assert_eq!(buffer.dimensions(), 1);
assert_eq!(buffer.item_count(), 5);
assert_eq!(buffer.format().to_str().unwrap(), "B");
assert_eq!(buffer.shape(), [5]);
// single-dimensional buffer is always contiguous
assert!(buffer.is_c_contiguous());
assert!(buffer.is_fortran_contiguous());

let slice = buffer.as_slice(py).unwrap();
assert_eq!(slice.len(), 5);
assert_eq!(slice[0].get(), b'a');
assert_eq!(slice[2].get(), b'c');

assert!(buffer.copy_to_slice(py, &mut [0u8]).is_err());
let mut arr = [0; 5];
buffer.copy_to_slice(py, &mut arr).unwrap();
assert_eq!(arr, b"abcde" as &[u8]);

assert!(buffer.copy_from_slice(py, &[0u8; 5]).is_err());
assert_eq!(buffer.to_vec(py).unwrap(), b"abcde");
});
}

#[allow(clippy::float_cmp)] // The test wants to ensure that no precision was lost on the Python round-trip
#[test]
fn test_array_buffer() {
let gil = Python::acquire_gil();
let py = gil.python();
let array = py
.import("array")
.unwrap()
.call_method("array", ("f", (1.0, 1.5, 2.0, 2.5)), None)
.unwrap();
let buffer = PyBuffer::get(array).unwrap();
assert_eq!(buffer.dimensions(), 1);
assert_eq!(buffer.item_count(), 4);
assert_eq!(buffer.format().to_str().unwrap(), "f");
assert_eq!(buffer.shape(), [4]);

let slice = buffer.as_slice(py).unwrap();
assert_eq!(slice.len(), 4);
assert_eq!(slice[0].get(), 1.0);
assert_eq!(slice[3].get(), 2.5);

let mut_slice = buffer.as_mut_slice(py).unwrap();
assert_eq!(mut_slice.len(), 4);
assert_eq!(mut_slice[0].get(), 1.0);
mut_slice[3].set(2.75);
assert_eq!(slice[3].get(), 2.75);

buffer
.copy_from_slice(py, &[10.0f32, 11.0, 12.0, 13.0])
.unwrap();
assert_eq!(slice[2].get(), 12.0);

assert_eq!(buffer.to_vec(py).unwrap(), [10.0, 11.0, 12.0, 13.0]);
Python::with_gil(|py| {
let array = py
.import("array")
.unwrap()
.call_method("array", ("f", (1.0, 1.5, 2.0, 2.5)), None)
.unwrap();
let buffer = PyBuffer::get(array).unwrap();
assert_eq!(buffer.dimensions(), 1);
assert_eq!(buffer.item_count(), 4);
assert_eq!(buffer.format().to_str().unwrap(), "f");
assert_eq!(buffer.shape(), [4]);

let slice = buffer.as_slice(py).unwrap();
assert_eq!(slice.len(), 4);
assert_eq!(slice[0].get(), 1.0);
assert_eq!(slice[3].get(), 2.5);

let mut_slice = buffer.as_mut_slice(py).unwrap();
assert_eq!(mut_slice.len(), 4);
assert_eq!(mut_slice[0].get(), 1.0);
mut_slice[3].set(2.75);
assert_eq!(slice[3].get(), 2.75);

buffer
.copy_from_slice(py, &[10.0f32, 11.0, 12.0, 13.0])
.unwrap();
assert_eq!(slice[2].get(), 12.0);

assert_eq!(buffer.to_vec(py).unwrap(), [10.0, 11.0, 12.0, 13.0]);
});
}
}
42 changes: 21 additions & 21 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,38 +551,38 @@ mod tests {

#[test]
fn test_try_from() {
let gil = Python::acquire_gil();
let py = gil.python();
let list: &PyAny = vec![3, 6, 5, 4, 7].to_object(py).into_ref(py);
let dict: &PyAny = vec![("reverse", true)].into_py_dict(py).as_ref();
Python::with_gil(|py| {
let list: &PyAny = vec![3, 6, 5, 4, 7].to_object(py).into_ref(py);
let dict: &PyAny = vec![("reverse", true)].into_py_dict(py).as_ref();

assert!(PyList::try_from(list).is_ok());
assert!(PyDict::try_from(dict).is_ok());
assert!(PyList::try_from(list).is_ok());
assert!(PyDict::try_from(dict).is_ok());

assert!(PyAny::try_from(list).is_ok());
assert!(PyAny::try_from(dict).is_ok());
assert!(PyAny::try_from(list).is_ok());
assert!(PyAny::try_from(dict).is_ok());
});
}

#[test]
fn test_try_from_exact() {
let gil = Python::acquire_gil();
let py = gil.python();
let list: &PyAny = vec![3, 6, 5, 4, 7].to_object(py).into_ref(py);
let dict: &PyAny = vec![("reverse", true)].into_py_dict(py).as_ref();
Python::with_gil(|py| {
let list: &PyAny = vec![3, 6, 5, 4, 7].to_object(py).into_ref(py);
let dict: &PyAny = vec![("reverse", true)].into_py_dict(py).as_ref();

assert!(PyList::try_from_exact(list).is_ok());
assert!(PyDict::try_from_exact(dict).is_ok());
assert!(PyList::try_from_exact(list).is_ok());
assert!(PyDict::try_from_exact(dict).is_ok());

assert!(PyAny::try_from_exact(list).is_err());
assert!(PyAny::try_from_exact(dict).is_err());
assert!(PyAny::try_from_exact(list).is_err());
assert!(PyAny::try_from_exact(dict).is_err());
});
}

#[test]
fn test_try_from_unchecked() {
let gil = Python::acquire_gil();
let py = gil.python();
let list = PyList::new(py, &[1, 2, 3]);
let val = unsafe { <PyList as PyTryFrom>::try_from_unchecked(list.as_ref()) };
assert_eq!(list, val);
Python::with_gil(|py| {
let list = PyList::new(py, &[1, 2, 3]);
let val = unsafe { <PyList as PyTryFrom>::try_from_unchecked(list.as_ref()) };
assert_eq!(list, val);
});
}
}
68 changes: 34 additions & 34 deletions src/err/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,14 +638,14 @@ mod tests {
fn fetching_panic_exception_resumes_unwind() {
use crate::panic::PanicException;

let gil = Python::acquire_gil();
let py = gil.python();
let err: PyErr = PanicException::new_err("new panic");
err.restore(py);
assert!(PyErr::occurred(py));
Python::with_gil(|py| {
let err: PyErr = PanicException::new_err("new panic");
err.restore(py);
assert!(PyErr::occurred(py));

// should resume unwind
let _ = PyErr::fetch(py);
// should resume unwind
let _ = PyErr::fetch(py);
});
}

#[test]
Expand All @@ -657,42 +657,42 @@ mod tests {
// traceback: Some(<traceback object at 0x..)"
// }

let gil = Python::acquire_gil();
let py = gil.python();
let err = py
.run("raise Exception('banana')", None, None)
.expect_err("raising should have given us an error");
Python::with_gil(|py| {
let err = py
.run("raise Exception('banana')", None, None)
.expect_err("raising should have given us an error");

let debug_str = format!("{:?}", err);
assert!(debug_str.starts_with("PyErr { "));
assert!(debug_str.ends_with(" }"));
let debug_str = format!("{:?}", err);
assert!(debug_str.starts_with("PyErr { "));
assert!(debug_str.ends_with(" }"));

// strip "PyErr { " and " }"
let mut fields = debug_str["PyErr { ".len()..debug_str.len() - 2].split(", ");
// strip "PyErr { " and " }"
let mut fields = debug_str["PyErr { ".len()..debug_str.len() - 2].split(", ");

assert_eq!(fields.next().unwrap(), "type: <class 'Exception'>");
if py.version_info() >= (3, 7) {
assert_eq!(fields.next().unwrap(), "value: Exception('banana')");
} else {
// Python 3.6 and below formats the repr differently
assert_eq!(fields.next().unwrap(), ("value: Exception('banana',)"));
}
assert_eq!(fields.next().unwrap(), "type: <class 'Exception'>");
if py.version_info() >= (3, 7) {
assert_eq!(fields.next().unwrap(), "value: Exception('banana')");
} else {
// Python 3.6 and below formats the repr differently
assert_eq!(fields.next().unwrap(), ("value: Exception('banana',)"));
}

let traceback = fields.next().unwrap();
assert!(traceback.starts_with("traceback: Some(<traceback object at 0x"));
assert!(traceback.ends_with(">)"));
let traceback = fields.next().unwrap();
assert!(traceback.starts_with("traceback: Some(<traceback object at 0x"));
assert!(traceback.ends_with(">)"));

assert!(fields.next().is_none());
assert!(fields.next().is_none());
});
}

#[test]
fn err_display() {
let gil = Python::acquire_gil();
let py = gil.python();
let err = py
.run("raise Exception('banana')", None, None)
.expect_err("raising should have given us an error");
assert_eq!(err.to_string(), "Exception: banana");
Python::with_gil(|py| {
let err = py
.run("raise Exception('banana')", None, None)
.expect_err("raising should have given us an error");
assert_eq!(err.to_string(), "Exception: banana");
});
}

#[test]
Expand Down
Loading

0 comments on commit 388c255

Please sign in to comment.