From b6c6f7f2b62b06c2ecd7b95dbe6955330a1c7221 Mon Sep 17 00:00:00 2001 From: Adam Reichold Date: Tue, 13 Sep 2022 20:49:20 +0200 Subject: [PATCH] Fix #2615 by relaxing the type check in extract_sequence. This allows us to handle types like one-dimensional NumPy arrays which implement just enough of the sequence protocol to support the extract_sequence function but are not an instance of the sequence ABC. --- src/types/sequence.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/types/sequence.rs b/src/types/sequence.rs index 54de7158..af7cd0c5 100644 --- a/src/types/sequence.rs +++ b/src/types/sequence.rs @@ -299,7 +299,16 @@ fn extract_sequence<'s, T>(obj: &'s PyAny) -> PyResult> where T: FromPyObject<'s>, { - let seq = ::try_from(obj)?; + // 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()? { v.push(item?.extract::()?);