From ebac9f5457c8ac4e644964c395e691db37fe9957 Mon Sep 17 00:00:00 2001 From: Adam Reichold Date: Tue, 13 Sep 2022 22:05:12 +0200 Subject: [PATCH] Further relax extract_sequence as neither PyObject::len nor PyObject::iter actually need the sequence protocol. --- src/types/sequence.rs | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/types/sequence.rs b/src/types/sequence.rs index af7cd0c5d19..9621b32d940 100644 --- a/src/types/sequence.rs +++ b/src/types/sequence.rs @@ -299,18 +299,9 @@ fn extract_sequence<'s, T>(obj: &'s PyAny) -> PyResult> 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 { - ::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::()?); } Ok(v)