Skip to content

Commit

Permalink
PyList tests: simplify construction
Browse files Browse the repository at this point in the history
  • Loading branch information
birkenfeld committed Jun 6, 2021
1 parent cb6dedb commit 98461a2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fix unneccessary rebuilds when cycling between `cargo check` and `cargo clippy` in a Python virtualenv. [#1557](https://github.com/PyO3/pyo3/pull/1557)
- Fix segfault when dereferencing `ffi::PyDateTimeAPI` without the GIL. [#1563](https://github.com/PyO3/pyo3/pull/1563)
- Fix memory leak when converting to u128 and i128. [#1638](https://github.com/PyO3/pyo3/pull/1638)
- Fix segfault when calling `PyList::get_item` with negative indices. [#1668](https://github.com/PyO3/pyo3/pull/1668)

## [0.13.2] - 2021-02-12
### Packaging
Expand Down
35 changes: 10 additions & 25 deletions src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ mod test {
fn test_new() {
let gil = Python::acquire_gil();
let py = gil.python();
let v = vec![2, 3, 5, 7];
let list = PyList::new(py, &v);
let list = PyList::new(py, &[2, 3, 5, 7]);
assert_eq!(2, list.get_item(0).extract::<i32>().unwrap());
assert_eq!(3, list.get_item(1).extract::<i32>().unwrap());
assert_eq!(5, list.get_item(2).extract::<i32>().unwrap());
Expand All @@ -218,19 +217,15 @@ mod test {
fn test_len() {
let gil = Python::acquire_gil();
let py = gil.python();
let v = vec![1, 2, 3, 4];
let ob = v.to_object(py);
let list = <PyList as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
let list = PyList::new(py, &[1, 2, 3, 4]);
assert_eq!(4, list.len());
}

#[test]
fn test_get_item() {
let gil = Python::acquire_gil();
let py = gil.python();
let v = vec![2, 3, 5, 7];
let ob = v.to_object(py);
let list = <PyList as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
let list = PyList::new(py, &[2, 3, 5, 7]);
assert_eq!(2, list.get_item(0).extract::<i32>().unwrap());
assert_eq!(3, list.get_item(1).extract::<i32>().unwrap());
assert_eq!(5, list.get_item(2).extract::<i32>().unwrap());
Expand All @@ -242,17 +237,15 @@ mod test {
fn test_get_item_invalid() {
let gil = Python::acquire_gil();
let py = gil.python();
let list = PyList::new(py, [2, 3, 5, 7]);
let list = PyList::new(py, &[2, 3, 5, 7]);
list.get_item(-1);
}

#[test]
fn test_set_item() {
let gil = Python::acquire_gil();
let py = gil.python();
let v = vec![2, 3, 5, 7];
let ob = v.to_object(py);
let list = <PyList as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
let list = PyList::new(py, &[2, 3, 5, 7]);
let val = 42i32.to_object(py);
assert_eq!(2, list.get_item(0).extract::<i32>().unwrap());
list.set_item(0, val).unwrap();
Expand Down Expand Up @@ -282,9 +275,7 @@ mod test {
fn test_insert() {
let gil = Python::acquire_gil();
let py = gil.python();
let v = vec![2, 3, 5, 7];
let ob = v.to_object(py);
let list = <PyList as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
let list = PyList::new(py, &[2, 3, 5, 7]);
let val = 42i32.to_object(py);
assert_eq!(4, list.len());
assert_eq!(2, list.get_item(0).extract::<i32>().unwrap());
Expand Down Expand Up @@ -315,9 +306,7 @@ mod test {
fn test_append() {
let gil = Python::acquire_gil();
let py = gil.python();
let v = vec![2];
let ob = v.to_object(py);
let list = <PyList as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
let list = PyList::new(py, &[2]);
list.append(3).unwrap();
assert_eq!(2, list.get_item(0).extract::<i32>().unwrap());
assert_eq!(3, list.get_item(1).extract::<i32>().unwrap());
Expand All @@ -344,8 +333,7 @@ mod test {
let gil = Python::acquire_gil();
let py = gil.python();
let v = vec![2, 3, 5, 7];
let ob = v.to_object(py);
let list = <PyList as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
let list = PyList::new(py, &v);
let mut idx = 0;
for el in list.iter() {
assert_eq!(v[idx], el.extract::<i32>().unwrap());
Expand All @@ -358,9 +346,7 @@ mod test {
fn test_into_iter() {
let gil = Python::acquire_gil();
let py = gil.python();
let v = vec![1, 2, 3, 4];
let ob = v.to_object(py);
let list = <PyList as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
let list = PyList::new(py, &[1, 2, 3, 4]);
for (i, item) in list.iter().enumerate() {
assert_eq!((i + 1) as i32, item.extract::<i32>().unwrap());
}
Expand All @@ -371,8 +357,7 @@ mod test {
let gil = Python::acquire_gil();
let py = gil.python();
let v = vec![2, 3, 5, 7];
let ob = v.to_object(py);
let list = <PyList as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
let list = PyList::new(py, &v);
let v2 = list.as_ref().extract::<Vec<i32>>().unwrap();
assert_eq!(v, v2);
}
Expand Down

1 comment on commit 98461a2

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'pytest-bench'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 98461a2 Previous: 6b68114 Ratio
tests/test_benchmarks.py::test_no_args 3713823.6710968013 iter/sec (stddev: 8.597762264678538e-8) 10127647.933584487 iter/sec (stddev: 5.063365364307096e-9) 2.73

This comment was automatically generated by workflow using github-action-benchmark.

CC: @PyO3/pyo3

Please sign in to comment.