Skip to content

Commit c902c16

Browse files
committed
fixup! support for readonly buffers (#863)
1 parent 4fa4520 commit c902c16

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

include/pybind11/detail/class.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ extern "C" inline int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int fla
486486
view->len *= s;
487487
view->readonly = info->readonly;
488488
if ((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE && info->readonly) {
489-
if(view)
489+
if (view)
490490
view->obj = nullptr;
491491
PyErr_SetString(PyExc_BufferError, "Writable buffer requested for readonly storage");
492492
return -1;

tests/test_buffers.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import io
22
import struct
3+
import sys
4+
35
import pytest
6+
47
from pybind11_tests import buffers as m
58
from pybind11_tests import ConstructorStats
69

10+
PY3 = sys.version_info[0] >= 3
11+
712
pytestmark = pytest.requires_numpy
813

914
with pytest.suppress(ImportError):
@@ -92,22 +97,22 @@ def test_pointer_to_member_fn():
9297
def test_readonly_buffer():
9398
buf = m.BufferReadOnly(0x64)
9499
view = memoryview(buf)
95-
assert view[0] == 0x64
100+
assert view[0] == 0x64 if PY3 else b'd'
96101
assert view.readonly
97102

98103

99104
@pytest.unsupported_on_pypy
100105
def test_selective_readonly_buffer():
101106
buf = m.BufferReadOnlySelect()
102107

103-
memoryview(buf)[0] = 0x64
108+
memoryview(buf)[0] = 0x64 if PY3 else b'd'
104109
assert buf.value == 0x64
105110

106111
io.BytesIO(b'A').readinto(buf)
107112
assert buf.value == ord(b'A')
108113

109114
buf.readonly = True
110115
with pytest.raises(TypeError):
111-
memoryview(buf)[0] = 0
116+
memoryview(buf)[0] = 0 if PY3 else b'\0'
112117
with pytest.raises(TypeError):
113118
io.BytesIO(b'1').readinto(buf)

0 commit comments

Comments
 (0)