Skip to content

Commit

Permalink
Further relax extract_sequence as neither PyObject::len nor PyObject:…
Browse files Browse the repository at this point in the history
…:iter actually need the sequence protocol.
  • Loading branch information
adamreichold committed Sep 13, 2022
1 parent 2517222 commit 39c990b
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions src/types/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,18 +299,9 @@ fn extract_sequence<'s, T>(obj: &'s PyAny) -> PyResult<Vec<T>>
where
T: FromPyObject<'s>,
{
// Types that pass `PySequence_Check` usually implement enough of the sequence protocol
// to support this function and if not, we will only fail extraction safely.
let seq = unsafe {
if ffi::PySequence_Check(obj.as_ptr()) != 0 {
<PySequence as PyTryFrom>::try_from_unchecked(obj)
} else {
return Err(PyDowncastError::new(obj, "Sequence").into());
}
};

let mut v = Vec::with_capacity(seq.len().unwrap_or(0) as usize);
for item in seq.iter()? {
let iter = obj.iter()?;
let mut v = Vec::with_capacity(obj.len().unwrap_or(0) as usize);
for item in iter {
v.push(item?.extract::<T>()?);
}
Ok(v)
Expand Down

0 comments on commit 39c990b

Please sign in to comment.