Skip to content

Commit 650c1e8

Browse files
author
Stefan Krah
committed
Issue #14203: Remove obsolete support for view==NULL in bytesiobuf_getbuffer()
and array_buffer_getbuf().
1 parent 38c30e6 commit 650c1e8

File tree

6 files changed

+48
-17
lines changed

6 files changed

+48
-17
lines changed

Lib/test/test_array.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,11 @@ def test_initialize_with_unicode(self):
10411041
a = array.array(self.typecode, "foo")
10421042
a = array.array(self.typecode, array.array('u', 'foo'))
10431043

1044+
@support.cpython_only
1045+
def test_obsolete_write_lock(self):
1046+
from _testcapi import getbuffer_with_null_view
1047+
a = array.array('B', b"")
1048+
self.assertRaises(BufferError, getbuffer_with_null_view, a)
10441049

10451050
class StringTest(BaseTest):
10461051

Lib/test/test_bytes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,10 @@ def delslice():
12241224
self.assertRaises(BufferError, delslice)
12251225
self.assertEqual(b, orig)
12261226

1227+
@test.support.cpython_only
1228+
def test_obsolete_write_lock(self):
1229+
from _testcapi import getbuffer_with_null_view
1230+
self.assertRaises(BufferError, getbuffer_with_null_view, bytearray())
12271231

12281232
class AssortedBytesTest(unittest.TestCase):
12291233
#

Misc/NEWS

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,9 +1560,9 @@ Build
15601560
C API
15611561
-----
15621562

1563-
- Issue #14203: Remove obsolete support for view==NULL in PyBuffer_FillInfo()
1564-
and bytearray_getbuffer(). Both functions now raise BufferError in that
1565-
case.
1563+
- Issue #14203: Remove obsolete support for view==NULL in PyBuffer_FillInfo(),
1564+
bytearray_getbuffer(), bytesiobuf_getbuffer() and array_buffer_getbuf().
1565+
All functions now raise BufferError in that case.
15661566

15671567
- Issue #22445: PyBuffer_IsContiguous() now implements precise contiguity
15681568
tests, compatible with NumPy's NPY_RELAXED_STRIDES_CHECKING compilation

Modules/_io/bytesio.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,23 +1028,24 @@ PyTypeObject PyBytesIO_Type = {
10281028
static int
10291029
bytesiobuf_getbuffer(bytesiobuf *obj, Py_buffer *view, int flags)
10301030
{
1031-
int ret;
10321031
bytesio *b = (bytesio *) obj->source;
1032+
1033+
if (view == NULL) {
1034+
PyErr_SetString(PyExc_BufferError,
1035+
"bytesiobuf_getbuffer: view==NULL argument is obsolete");
1036+
return -1;
1037+
}
10331038
if (SHARED_BUF(b)) {
10341039
if (unshare_buffer(b, b->string_size) < 0)
10351040
return -1;
10361041
}
1037-
if (view == NULL) {
1038-
b->exports++;
1039-
return 0;
1040-
}
1041-
ret = PyBuffer_FillInfo(view, (PyObject*)obj,
1042+
1043+
/* cannot fail if view != NULL and readonly == 0 */
1044+
(void)PyBuffer_FillInfo(view, (PyObject*)obj,
10421045
PyBytes_AS_STRING(b->buf), b->string_size,
10431046
0, flags);
1044-
if (ret >= 0) {
1045-
b->exports++;
1046-
}
1047-
return ret;
1047+
b->exports++;
1048+
return 0;
10481049
}
10491050

10501051
static void

Modules/_testcapimodule.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2518,21 +2518,26 @@ test_from_contiguous(PyObject* self, PyObject *noargs)
25182518

25192519
Py_RETURN_NONE;
25202520
}
2521-
2521+
2522+
extern PyTypeObject _PyBytesIOBuffer_Type;
2523+
25222524
static PyObject *
25232525
test_pep3118_obsolete_write_locks(PyObject* self, PyObject *noargs)
25242526
{
2527+
PyTypeObject *type = &_PyBytesIOBuffer_Type;
25252528
PyObject *b;
25262529
char *dummy[1];
25272530
int ret, match;
25282531

2532+
/* PyBuffer_FillInfo() */
25292533
ret = PyBuffer_FillInfo(NULL, NULL, dummy, 1, 0, PyBUF_SIMPLE);
25302534
match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError);
25312535
PyErr_Clear();
25322536
if (ret != -1 || match == 0)
25332537
goto error;
25342538

2535-
b = PyByteArray_FromStringAndSize("", 0);
2539+
/* bytesiobuf_getbuffer() */
2540+
b = type->tp_alloc(type, 0);
25362541
if (b == NULL) {
25372542
return NULL;
25382543
}
@@ -2552,6 +2557,18 @@ test_pep3118_obsolete_write_locks(PyObject* self, PyObject *noargs)
25522557
return NULL;
25532558
}
25542559

2560+
/* This tests functions that historically supported write locks. It is
2561+
wrong to call getbuffer() with view==NULL and a compliant getbufferproc
2562+
is entitled to segfault in that case. */
2563+
static PyObject *
2564+
getbuffer_with_null_view(PyObject* self, PyObject *obj)
2565+
{
2566+
if (PyObject_GetBuffer(obj, NULL, PyBUF_SIMPLE) < 0)
2567+
return NULL;
2568+
2569+
Py_RETURN_NONE;
2570+
}
2571+
25552572
/* Test that the fatal error from not having a current thread doesn't
25562573
cause an infinite loop. Run via Lib/test/test_capi.py */
25572574
static PyObject *
@@ -3213,6 +3230,7 @@ static PyMethodDef TestMethods[] = {
32133230
{"test_capsule", (PyCFunction)test_capsule, METH_NOARGS},
32143231
{"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS},
32153232
{"test_pep3118_obsolete_write_locks", (PyCFunction)test_pep3118_obsolete_write_locks, METH_NOARGS},
3233+
{"getbuffer_with_null_view", getbuffer_with_null_view, METH_O},
32163234
{"getargs_tuple", getargs_tuple, METH_VARARGS},
32173235
{"getargs_keywords", (PyCFunction)getargs_keywords,
32183236
METH_VARARGS|METH_KEYWORDS},

Modules/arraymodule.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2530,7 +2530,11 @@ static const void *emptybuf = "";
25302530
static int
25312531
array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
25322532
{
2533-
if (view==NULL) goto finish;
2533+
if (view == NULL) {
2534+
PyErr_SetString(PyExc_BufferError,
2535+
"array_buffer_getbuf: view==NULL argument is obsolete");
2536+
return -1;
2537+
}
25342538

25352539
view->buf = (void *)self->ob_item;
25362540
view->obj = (PyObject*)self;
@@ -2560,7 +2564,6 @@ array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
25602564
#endif
25612565
}
25622566

2563-
finish:
25642567
self->ob_exports++;
25652568
return 0;
25662569
}

0 commit comments

Comments
 (0)