Skip to content

Commit dee628d

Browse files
committed
fix: handle PyBUF_WRITABLE
1 parent 439b3f6 commit dee628d

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

src/buffer.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -792,9 +792,6 @@ pub struct PyUntypedBufferView {
792792
impl PyUntypedBufferView {
793793
/// Acquire a buffer view on the stack with [`ffi::PyBUF_SIMPLE`] flags,
794794
/// pass it to `f`, then release the buffer.
795-
///
796-
/// Format is patched to `"B"` and itemsize to `1`, as required by the
797-
/// buffer protocol for `PyBUF_SIMPLE` requests.
798795
pub fn with<R>(
799796
obj: &Bound<'_, PyAny>,
800797
f: impl FnOnce(&PyUntypedBufferView) -> R,
@@ -835,10 +832,16 @@ impl PyUntypedBufferView {
835832

836833
// SAFETY: Construct view only after successful GetBuffer, so Drop always
837834
// runs on an initialized Py_buffer.
838-
let view = PyUntypedBufferView {
835+
let mut view = PyUntypedBufferView {
839836
raw: unsafe { raw.assume_init() },
840837
};
841838

839+
// For PyBUF_WRITABLE, the consumer must assume itemsize == 1 and format "B".
840+
if flags == ffi::PyBUF_WRITABLE {
841+
view.raw.itemsize = 1;
842+
view.raw.format = ffi::c_str!("B").as_ptr() as *mut _;
843+
}
844+
842845
Ok(f(&view))
843846
}
844847

0 commit comments

Comments
 (0)