Skip to content

Commit

Permalink
Issue python#14203: Remove obsolete support for view==NULL in PyBuffe…
Browse files Browse the repository at this point in the history
…r_FillInfo()

and bytearray_getbuffer().  Both functions now raise BufferError in that
case.
  • Loading branch information
skrah committed Feb 3, 2015
1 parent 7277761 commit 5178d91
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,10 @@ Build
C API
-----

- Issue #14203: Remove obsolete support for view==NULL in PyBuffer_FillInfo()
and bytearray_getbuffer(). Both functions now raise BufferError in that
case.

- Issue #22445: PyBuffer_IsContiguous() now implements precise contiguity
tests, compatible with NumPy's NPY_RELAXED_STRIDES_CHECKING compilation
flag. Previously the function reported false negatives for corner cases.
Expand Down
34 changes: 34 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2518,6 +2518,39 @@ test_from_contiguous(PyObject* self, PyObject *noargs)

Py_RETURN_NONE;
}

static PyObject *
test_pep3118_obsolete_write_locks(PyObject* self, PyObject *noargs)
{
PyObject *b;
char *dummy[1];
int ret, match;

ret = PyBuffer_FillInfo(NULL, NULL, dummy, 1, 0, PyBUF_SIMPLE);
match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError);
PyErr_Clear();
if (ret != -1 || match == 0)
goto error;

b = PyByteArray_FromStringAndSize("", 0);
if (b == NULL) {
return NULL;
}

ret = PyObject_GetBuffer(b, NULL, PyBUF_SIMPLE);
Py_DECREF(b);
match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError);
PyErr_Clear();
if (ret != -1 || match == 0)
goto error;

Py_RETURN_NONE;

error:
PyErr_SetString(TestError,
"test_pep3118_obsolete_write_locks: failure");
return NULL;
}

/* Test that the fatal error from not having a current thread doesn't
cause an infinite loop. Run via Lib/test/test_capi.py */
Expand Down Expand Up @@ -3179,6 +3212,7 @@ static PyMethodDef TestMethods[] = {
{"test_unicode_compare_with_ascii", (PyCFunction)test_unicode_compare_with_ascii, METH_NOARGS},
{"test_capsule", (PyCFunction)test_capsule, METH_NOARGS},
{"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS},
{"test_pep3118_obsolete_write_locks", (PyCFunction)test_pep3118_obsolete_write_locks, METH_NOARGS},
{"getargs_tuple", getargs_tuple, METH_VARARGS},
{"getargs_keywords", (PyCFunction)getargs_keywords,
METH_VARARGS|METH_KEYWORDS},
Expand Down
7 changes: 6 additions & 1 deletion Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,12 @@ int
PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
int readonly, int flags)
{
if (view == NULL) return 0; /* XXX why not -1? */
if (view == NULL) {
PyErr_SetString(PyExc_BufferError,
"PyBuffer_FillInfo: view==NULL argument is obsolete");
return -1;
}

if (((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) &&
(readonly == 1)) {
PyErr_SetString(PyExc_BufferError,
Expand Down
15 changes: 7 additions & 8 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,17 @@ _getbytevalue(PyObject* arg, int *value)
static int
bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
{
int ret;
void *ptr;
if (view == NULL) {
obj->ob_exports++;
return 0;
PyErr_SetString(PyExc_BufferError,
"bytearray_getbuffer: view==NULL argument is obsolete");
return -1;
}
ptr = (void *) PyByteArray_AS_STRING(obj);
ret = PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
if (ret >= 0) {
obj->ob_exports++;
}
return ret;
/* cannot fail if view != NULL and readonly == 0 */
(void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
obj->ob_exports++;
return 0;
}

static void
Expand Down

0 comments on commit 5178d91

Please sign in to comment.