Skip to content

Commit

Permalink
Merge pull request #1502 from messense/check-buffer
Browse files Browse the repository at this point in the history
Check buffer protocol support before getting buffer in sequence protocol specialization
  • Loading branch information
messense authored Mar 16, 2021
2 parents dab62a5 + ba76448 commit c0d4905
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/types/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,14 @@ macro_rules! array_impls {
fn extract(obj: &'source PyAny) -> PyResult<Self> {
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)?;
Expand Down

0 comments on commit c0d4905

Please sign in to comment.