Skip to content

Commit 0ee6eac

Browse files
authored
improve debug representation of PyBuffer (#5442)
* improve debug representation of `PyBuffer` * newsfragment * fixup tests
1 parent b31a258 commit 0ee6eac

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

newsfragments/5442.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve `Debug` representation of `PyBuffer<T>`.

src/buffer.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ impl<T> Debug for PyBuffer<T> {
4848
.field("itemsize", &self.0.itemsize)
4949
.field("readonly", &self.0.readonly)
5050
.field("ndim", &self.0.ndim)
51-
.field("format", &self.0.format)
52-
.field("shape", &self.0.shape)
53-
.field("strides", &self.0.strides)
54-
.field("suboffsets", &self.0.suboffsets)
51+
.field("format", &self.format())
52+
.field("shape", &self.shape())
53+
.field("strides", &self.strides())
54+
.field("suboffsets", &self.suboffsets())
5555
.field("internal", &self.0.internal)
5656
.finish()
5757
}
@@ -228,7 +228,9 @@ impl<T: Element> PyBuffer<T> {
228228
Ok(buf)
229229
}
230230
}
231+
}
231232

233+
impl<T> PyBuffer<T> {
232234
/// Gets the pointer to the start of the buffer memory.
233235
///
234236
/// Warning: the buffer memory can be mutated by other code (including
@@ -367,7 +369,9 @@ impl<T: Element> PyBuffer<T> {
367369
pub fn is_fortran_contiguous(&self) -> bool {
368370
unsafe { ffi::PyBuffer_IsContiguous(&*self.0, b'F' as std::ffi::c_char) != 0 }
369371
}
372+
}
370373

374+
impl<T: Element> PyBuffer<T> {
371375
/// Gets the buffer memory as a slice.
372376
///
373377
/// This function succeeds if:
@@ -703,29 +707,24 @@ mod tests {
703707

704708
use crate::ffi;
705709
use crate::types::any::PyAnyMethods;
710+
use crate::types::PyBytes;
706711
use crate::Python;
707712

708713
#[test]
709714
fn test_debug() {
710715
Python::attach(|py| {
711-
let bytes = py.eval(ffi::c_str!("b'abcde'"), None, None).unwrap();
716+
let bytes = PyBytes::new(py, b"abcde");
712717
let buffer: PyBuffer<u8> = PyBuffer::get(&bytes).unwrap();
713718
let expected = format!(
714719
concat!(
715720
"PyBuffer {{ buf: {:?}, obj: {:?}, ",
716721
"len: 5, itemsize: 1, readonly: 1, ",
717-
"ndim: 1, format: {:?}, shape: {:?}, ",
718-
"strides: {:?}, suboffsets: {:?}, internal: {:?} }}",
722+
"ndim: 1, format: \"B\", shape: [5], ",
723+
"strides: [1], suboffsets: None, internal: {:?} }}",
719724
),
720-
buffer.0.buf,
721-
buffer.0.obj,
722-
buffer.0.format,
723-
buffer.0.shape,
724-
buffer.0.strides,
725-
buffer.0.suboffsets,
726-
buffer.0.internal
725+
buffer.0.buf, buffer.0.obj, buffer.0.internal
727726
);
728-
let debug_repr = format!("{buffer:?}");
727+
let debug_repr = format!("{:?}", buffer);
729728
assert_eq!(debug_repr, expected);
730729
});
731730
}
@@ -867,7 +866,7 @@ mod tests {
867866
#[test]
868867
fn test_bytes_buffer() {
869868
Python::attach(|py| {
870-
let bytes = py.eval(ffi::c_str!("b'abcde'"), None, None).unwrap();
869+
let bytes = PyBytes::new(py, b"abcde");
871870
let buffer = PyBuffer::get(&bytes).unwrap();
872871
assert_eq!(buffer.dimensions(), 1);
873872
assert_eq!(buffer.item_count(), 5);

0 commit comments

Comments
 (0)