Skip to content

Commit

Permalink
reintroduce PyList constructors (PyO3#4386)
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu authored Jul 28, 2024
1 parent 7683c4a commit 5fc5df4
Show file tree
Hide file tree
Showing 25 changed files with 103 additions and 85 deletions.
2 changes: 1 addition & 1 deletion guide/src/conversions/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fails, so usually you will use something like
# use pyo3::types::PyList;
# fn main() -> PyResult<()> {
# Python::with_gil(|py| {
# let list = PyList::new_bound(py, b"foo");
# let list = PyList::new(py, b"foo");
let v: Vec<i32> = list.extract()?;
# assert_eq!(&v, &[102, 111, 111]);
# Ok(())
Expand Down
2 changes: 1 addition & 1 deletion guide/src/exception.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ use pyo3::types::{PyBool, PyList};

Python::with_gil(|py| {
assert!(PyBool::new_bound(py, true).is_instance_of::<PyBool>());
let list = PyList::new_bound(py, &[1, 2, 3, 4]);
let list = PyList::new(py, &[1, 2, 3, 4]);
assert!(!list.is_instance_of::<PyBool>());
assert!(list.is_instance_of::<PyList>());
});
Expand Down
4 changes: 2 additions & 2 deletions guide/src/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ This limitation is important to keep in mind when this setting is used, especial
```rust,ignore
# use pyo3::prelude::*;
# use pyo3::types::PyList;
let numbers: Py<PyList> = Python::with_gil(|py| PyList::empty_bound(py).unbind());
let numbers: Py<PyList> = Python::with_gil(|py| PyList::empty(py).unbind());
Python::with_gil(|py| {
numbers.bind(py).append(23).unwrap();
Expand All @@ -124,7 +124,7 @@ will abort if the list not explicitly disposed via
```rust
# use pyo3::prelude::*;
# use pyo3::types::PyList;
let numbers: Py<PyList> = Python::with_gil(|py| PyList::empty_bound(py).unbind());
let numbers: Py<PyList> = Python::with_gil(|py| PyList::empty(py).unbind());

Python::with_gil(|py| {
numbers.bind(py).append(23).unwrap();
Expand Down
10 changes: 5 additions & 5 deletions guide/src/trait-bounds.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl Model for UserModel {
Python::with_gil(|py| {
self.model
.bind(py)
.call_method("set_variables", (PyList::new_bound(py, var),), None)
.call_method("set_variables", (PyList::new(py, var),), None)
.unwrap();
})
}
Expand Down Expand Up @@ -182,7 +182,7 @@ This wrapper will also perform the type conversions between Python and Rust.
# println!("Rust calling Python to set the variables");
# Python::with_gil(|py| {
# self.model.bind(py)
# .call_method("set_variables", (PyList::new_bound(py, var),), None)
# .call_method("set_variables", (PyList::new(py, var),), None)
# .unwrap();
# })
# }
Expand Down Expand Up @@ -360,7 +360,7 @@ impl Model for UserModel {
# println!("Rust calling Python to set the variables");
# Python::with_gil(|py| {
# self.model.bind(py)
# .call_method("set_variables", (PyList::new_bound(py, var),), None)
# .call_method("set_variables", (PyList::new(py, var),), None)
# .unwrap();
# })
# }
Expand Down Expand Up @@ -419,7 +419,7 @@ impl Model for UserModel {
# println!("Rust calling Python to set the variables");
# Python::with_gil(|py| {
# let py_model = self.model.bind(py)
# .call_method("set_variables", (PyList::new_bound(py, var),), None)
# .call_method("set_variables", (PyList::new(py, var),), None)
# .unwrap();
# })
# }
Expand Down Expand Up @@ -517,7 +517,7 @@ impl Model for UserModel {
Python::with_gil(|py| {
self.model
.bind(py)
.call_method("set_variables", (PyList::new_bound(py, var),), None)
.call_method("set_variables", (PyList::new(py, var),), None)
.unwrap();
})
}
Expand Down
6 changes: 3 additions & 3 deletions guide/src/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ use pyo3::prelude::*;
use pyo3::types::PyList;

fn example<'py>(py: Python<'py>) -> PyResult<()> {
let x: Bound<'py, PyList> = PyList::empty_bound(py);
let x: Bound<'py, PyList> = PyList::empty(py);
x.append(1)?;
let y: Bound<'py, PyList> = x.clone(); // y is a new reference to the same list
drop(x); // release the original reference x
Expand All @@ -77,7 +77,7 @@ use pyo3::prelude::*;
use pyo3::types::PyList;

fn example(py: Python<'_>) -> PyResult<()> {
let x = PyList::empty_bound(py);
let x = PyList::empty(py);
x.append(1)?;
let y = x.clone();
drop(x);
Expand Down Expand Up @@ -232,7 +232,7 @@ fn get_first_item<'py>(list: &Bound<'py, PyList>) -> PyResult<Bound<'py, PyAny>>
list.get_item(0)
}
# Python::with_gil(|py| {
# let l = PyList::new_bound(py, ["hello world"]);
# let l = PyList::new(py, ["hello world"]);
# assert!(get_first_item(&l).unwrap().eq("hello world").unwrap());
# })
```
Expand Down
4 changes: 2 additions & 2 deletions pyo3-benches/benches/bench_frompyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ fn enum_from_pyobject(b: &mut Bencher<'_>) {

fn list_via_downcast(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
let any = PyList::empty_bound(py).into_any();
let any = PyList::empty(py).into_any();

b.iter(|| black_box(&any).downcast::<PyList>().unwrap());
})
}

fn list_via_extract(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
let any = PyList::empty_bound(py).into_any();
let any = PyList::empty(py).into_any();

b.iter(|| black_box(&any).extract::<Bound<'_, PyList>>().unwrap());
})
Expand Down
10 changes: 5 additions & 5 deletions pyo3-benches/benches/bench_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use pyo3::types::{PyList, PySequence};
fn iter_list(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 100_000;
let list = PyList::new_bound(py, 0..LEN);
let list = PyList::new(py, 0..LEN);
let mut sum = 0;
b.iter(|| {
for x in &list {
Expand All @@ -22,14 +22,14 @@ fn iter_list(b: &mut Bencher<'_>) {
fn list_new(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 50_000;
b.iter_with_large_drop(|| PyList::new_bound(py, 0..LEN));
b.iter_with_large_drop(|| PyList::new(py, 0..LEN));
});
}

fn list_get_item(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 50_000;
let list = PyList::new_bound(py, 0..LEN);
let list = PyList::new(py, 0..LEN);
let mut sum = 0;
b.iter(|| {
for i in 0..LEN {
Expand All @@ -43,7 +43,7 @@ fn list_get_item(b: &mut Bencher<'_>) {
fn list_get_item_unchecked(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 50_000;
let list = PyList::new_bound(py, 0..LEN);
let list = PyList::new(py, 0..LEN);
let mut sum = 0;
b.iter(|| {
for i in 0..LEN {
Expand All @@ -58,7 +58,7 @@ fn list_get_item_unchecked(b: &mut Bencher<'_>) {
fn sequence_from_list(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 50_000;
let list = &PyList::new_bound(py, 0..LEN);
let list = &PyList::new(py, 0..LEN);
b.iter(|| black_box(list).downcast::<PySequence>().unwrap());
});
}
Expand Down
2 changes: 1 addition & 1 deletion pyo3-benches/benches/bench_tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fn tuple_new_list(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 50_000;
let tuple = PyTuple::new_bound(py, 0..LEN);
b.iter_with_large_drop(|| PyList::new_bound(py, tuple.iter_borrowed()));
b.iter_with_large_drop(|| PyList::new(py, tuple.iter_borrowed()));
});
}

Expand Down
6 changes: 3 additions & 3 deletions src/conversions/smallvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ mod tests {
Python::with_gil(|py| {
let sv: SmallVec<[u64; 8]> = [1, 2, 3, 4, 5].iter().cloned().collect();
let hso: PyObject = sv.clone().into_py(py);
let l = PyList::new_bound(py, [1, 2, 3, 4, 5]);
let l = PyList::new(py, [1, 2, 3, 4, 5]);
assert!(l.eq(hso).unwrap());
});
}

#[test]
fn test_smallvec_from_py_object() {
Python::with_gil(|py| {
let l = PyList::new_bound(py, [1, 2, 3, 4, 5]);
let l = PyList::new(py, [1, 2, 3, 4, 5]);
let sv: SmallVec<[u64; 8]> = l.extract().unwrap();
assert_eq!(sv.as_slice(), [1, 2, 3, 4, 5]);
});
Expand All @@ -135,7 +135,7 @@ mod tests {
Python::with_gil(|py| {
let sv: SmallVec<[u64; 8]> = [1, 2, 3, 4, 5].iter().cloned().collect();
let hso: PyObject = sv.to_object(py);
let l = PyList::new_bound(py, [1, 2, 3, 4, 5]);
let l = PyList::new(py, [1, 2, 3, 4, 5]);
assert!(l.eq(hso).unwrap());
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
//! - Types that also have the `'py` lifetime, such as the [`Bound<'py, T>`](Bound) smart pointer, are
//! bound to the Python GIL and rely on this to offer their functionality. These types often
//! have a [`.py()`](Bound::py) method to get the associated [`Python<'py>`](Python) token.
//! - Functions which depend on the `'py` lifetime, such as [`PyList::new_bound`](types::PyList::new_bound),
//! - Functions which depend on the `'py` lifetime, such as [`PyList::new`](types::PyList::new),
//! require a [`Python<'py>`](Python) token as an input. Sometimes the token is passed implicitly by
//! taking a [`Bound<'py, T>`](Bound) or other type which is bound to the `'py` lifetime.
//! - Traits which depend on the `'py` lifetime, such as [`FromPyObject<'py>`](FromPyObject), usually have
Expand Down
2 changes: 1 addition & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/// use pyo3::{prelude::*, py_run, types::PyList};
///
/// Python::with_gil(|py| {
/// let list = PyList::new_bound(py, &[1, 2, 3]);
/// let list = PyList::new(py, &[1, 2, 3]);
/// py_run!(py, list, "assert list == [1, 2, 3]");
/// });
/// ```
Expand Down
4 changes: 2 additions & 2 deletions src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -882,15 +882,15 @@ mod tests {

// If allow_threads is implemented correctly, this thread still owns the GIL here
// so the following Python calls should not cause crashes.
let list = PyList::new_bound(py, [1, 2, 3, 4]);
let list = PyList::new(py, [1, 2, 3, 4]);
assert_eq!(list.extract::<Vec<i32>>().unwrap(), vec![1, 2, 3, 4]);
});
}

#[cfg(not(pyo3_disable_reference_pool))]
#[test]
fn test_allow_threads_pass_stuff_in() {
let list = Python::with_gil(|py| PyList::new_bound(py, vec!["foo", "bar"]).unbind());
let list = Python::with_gil(|py| PyList::new(py, vec!["foo", "bar"]).unbind());
let mut v = vec![1, 2, 3];
let a = std::sync::Arc::new(String::from("foo"));

Expand Down
2 changes: 1 addition & 1 deletion src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ unsafe impl<T> Sync for GILProtected<T> where T: Send {}
///
/// pub fn get_shared_list(py: Python<'_>) -> &Bound<'_, PyList> {
/// LIST_CELL
/// .get_or_init(py, || PyList::empty_bound(py).unbind())
/// .get_or_init(py, || PyList::empty(py).unbind())
/// .bind(py)
/// }
/// # Python::with_gil(|py| assert_eq!(get_shared_list(py).len(), 0));
Expand Down
2 changes: 1 addition & 1 deletion src/tests/hygiene/pymethods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Dummy {
fn __delattr__(&mut self, name: ::std::string::String) {}

fn __dir__<'py>(&self, py: crate::Python<'py>) -> crate::Bound<'py, crate::types::PyList> {
crate::types::PyList::new_bound(py, ::std::vec![0_u8])
crate::types::PyList::new(py, ::std::vec![0_u8])
}

//////////////////////
Expand Down
4 changes: 2 additions & 2 deletions src/types/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1938,10 +1938,10 @@ class SimpleClass:
#[test]
fn test_is_empty() {
Python::with_gil(|py| {
let empty_list = PyList::empty_bound(py).into_any();
let empty_list = PyList::empty(py).into_any();
assert!(empty_list.is_empty().unwrap());

let list = PyList::new_bound(py, vec![1, 2, 3]).into_any();
let list = PyList::new(py, vec![1, 2, 3]).into_any();
assert!(!list.is_empty().unwrap());

let not_container = 5.to_object(py).into_bound(py);
Expand Down
4 changes: 2 additions & 2 deletions src/types/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ mod tests {
#[cfg(not(any(PyPy, GraalPy)))]
fn test_from_sequence() {
Python::with_gil(|py| {
let items = PyList::new_bound(py, vec![("a", 1), ("b", 2)]);
let items = PyList::new(py, vec![("a", 1), ("b", 2)]);
let dict = PyDict::from_sequence_bound(&items).unwrap();
assert_eq!(
1,
Expand Down Expand Up @@ -636,7 +636,7 @@ mod tests {
#[cfg(not(any(PyPy, GraalPy)))]
fn test_from_sequence_err() {
Python::with_gil(|py| {
let items = PyList::new_bound(py, vec!["a", "b"]);
let items = PyList::new(py, vec!["a", "b"]);
assert!(PyDict::from_sequence_bound(&items).is_err());
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ mod tests {
let count;
let obj = py.eval_bound("object()", None, None).unwrap();
let list = {
let list = PyList::empty_bound(py);
let list = PyList::empty(py);
list.append(10).unwrap();
list.append(&obj).unwrap();
count = obj.get_refcnt();
Expand Down
Loading

0 comments on commit 5fc5df4

Please sign in to comment.