From ba7644849dfdddf771ab169257f75ee4be13a318 Mon Sep 17 00:00:00 2001 From: messense Date: Tue, 16 Mar 2021 13:32:14 +0800 Subject: [PATCH] Check buffer protocol support before getting buffer in sequence protocol specialization This avoids calling a expensive `PyErr_Format` inside of `PyObject_GetBuffer` when buffer protocol is unsupported --- src/types/sequence.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/types/sequence.rs b/src/types/sequence.rs index 67818de1..423021d0 100644 --- a/src/types/sequence.rs +++ b/src/types/sequence.rs @@ -287,12 +287,14 @@ macro_rules! array_impls { fn extract(obj: &'source PyAny) -> PyResult { let mut array = [T::default(); $N]; // first try buffer protocol - if let Ok(buf) = crate::buffer::PyBuffer::get(obj) { - if buf.dimensions() == 1 && buf.copy_to_slice(obj.py(), &mut array).is_ok() { + if unsafe { ffi::PyObject_CheckBuffer(obj.as_ptr()) } == 1 { + if let Ok(buf) = crate::buffer::PyBuffer::get(obj) { + if buf.dimensions() == 1 && buf.copy_to_slice(obj.py(), &mut array).is_ok() { + buf.release(obj.py()); + return Ok(array); + } buf.release(obj.py()); - return Ok(array); } - buf.release(obj.py()); } // fall back to sequence protocol extract_sequence_into_slice(obj, &mut array)?;