Skip to content

Commit

Permalink
Rename sequence .list() and .tuple() to .to_list() and `.to_tup…
Browse files Browse the repository at this point in the history
…le()`
  • Loading branch information
davidhewitt committed Apr 18, 2023
1 parent f08098f commit e7336f0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
1 change: 1 addition & 0 deletions newsfragments/3111.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Rename `PySequence::list` and `PySequence::tuple` to `PySequence::to_list` and `PySequence::to_tuple`. (The old names continue to exist as deprecated forms.)
2 changes: 1 addition & 1 deletion src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ impl PyList {

/// Return a new tuple containing the contents of the list; equivalent to the Python expression `tuple(list)`.
///
/// This method is equivalent to `self.as_sequence().tuple()` and faster than `PyTuple::new(py, this_list)`.
/// This method is equivalent to `self.as_sequence().to_tuple()` and faster than `PyTuple::new(py, this_list)`.
pub fn to_tuple(&self) -> &PyTuple {
unsafe { self.py().from_owned_ptr(ffi::PyList_AsTuple(self.as_ptr())) }
}
Expand Down
56 changes: 50 additions & 6 deletions src/types/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,16 @@ impl PySequence {

/// Returns a fresh list based on the Sequence.
#[inline]
pub fn to_list(&self) -> PyResult<&PyList> {
unsafe {
self.py()
.from_owned_ptr_or_err(ffi::PySequence_List(self.as_ptr()))
}
}

/// Returns a fresh list based on the Sequence.
#[inline]
#[deprecated(since = "0.19.0", note = "renamed to .to_list()")]
pub fn list(&self) -> PyResult<&PyList> {
unsafe {
self.py()
Expand All @@ -248,6 +258,16 @@ impl PySequence {

/// Returns a fresh tuple based on the Sequence.
#[inline]
pub fn to_tuple(&self) -> PyResult<&PyTuple> {
unsafe {
self.py()
.from_owned_ptr_or_err(ffi::PySequence_Tuple(self.as_ptr()))
}
}

/// Returns a fresh tuple based on the Sequence.
#[inline]
#[deprecated(since = "0.19.0", note = "renamed to .to_tuple()")]
pub fn tuple(&self) -> PyResult<&PyTuple> {
unsafe {
self.py()
Expand Down Expand Up @@ -387,7 +407,7 @@ impl Py<PySequence> {

#[cfg(test)]
mod tests {
use crate::types::{PyList, PySequence};
use crate::types::{PyList, PySequence, PyTuple};
use crate::{AsPyPointer, Py, PyObject, Python, ToPyObject};

fn get_object() -> PyObject {
Expand Down Expand Up @@ -802,7 +822,11 @@ mod tests {
let v = vec!["foo", "bar"];
let ob = v.to_object(py);
let seq = ob.downcast::<PySequence>(py).unwrap();
assert!(seq.list().is_ok());
assert!(seq.to_list().unwrap().eq(PyList::new(py, &v)).unwrap());
#[allow(deprecated)]
{
assert!(seq.list().is_ok());
}
});
}

Expand All @@ -812,7 +836,15 @@ mod tests {
let v = "foo";
let ob = v.to_object(py);
let seq: &PySequence = ob.downcast(py).unwrap();
assert!(seq.list().is_ok());
assert!(seq
.to_list()
.unwrap()
.eq(PyList::new(py, &["f", "o", "o"]))
.unwrap());
#[allow(deprecated)]
{
assert!(seq.list().is_ok());
}
});
}

Expand All @@ -822,7 +854,15 @@ mod tests {
let v = ("foo", "bar");
let ob = v.to_object(py);
let seq = ob.downcast::<PySequence>(py).unwrap();
assert!(seq.tuple().is_ok());
assert!(seq
.to_tuple()
.unwrap()
.eq(PyTuple::new(py, &["foo", "bar"]))
.unwrap());
#[allow(deprecated)]
{
assert!(seq.tuple().is_ok());
}
});
}

Expand All @@ -832,7 +872,11 @@ mod tests {
let v = vec!["foo", "bar"];
let ob = v.to_object(py);
let seq = ob.downcast::<PySequence>(py).unwrap();
assert!(seq.tuple().is_ok());
assert!(seq.to_tuple().unwrap().eq(PyTuple::new(py, &v)).unwrap());
#[allow(deprecated)]
{
assert!(seq.tuple().is_ok());
}
});
}

Expand Down Expand Up @@ -876,7 +920,7 @@ mod tests {
let seq = ob.downcast::<PySequence>(py).unwrap();
let type_ptr = seq.as_ref();
let seq_from = unsafe { type_ptr.downcast_unchecked::<PySequence>() };
assert!(seq_from.list().is_ok());
assert!(seq_from.to_list().is_ok());
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,10 @@ impl PyTuple {

/// Return a new list containing the contents of this tuple; equivalent to the Python expression `list(tuple)`.
///
/// This method is equivalent to `self.as_sequence().list()` and faster than `PyList::new(py, self)`.
/// This method is equivalent to `self.as_sequence().to_list()` and faster than `PyList::new(py, self)`.
pub fn to_list(&self) -> &PyList {
self.as_sequence()
.list()
.to_list()
.expect("failed to convert tuple to list")
}
}
Expand Down

0 comments on commit e7336f0

Please sign in to comment.