diff options
author | Stefan Krah <skrah@bytereef.org> | 2012-03-05 16:45:17 (GMT) |
---|---|---|
committer | Stefan Krah <skrah@bytereef.org> | 2012-03-05 16:45:17 (GMT) |
commit | 1649c1b33a1d8f46ccf97b8373f62a22bbb3fde6 (patch) | |
tree | 36e4da0291c5bd73fbe2aa5b077ff10b30d07651 | |
parent | bf6c7eca43d7c7d80525c97af08aef52ec81e4a1 (diff) | |
download | cpython-1649c1b33a1d8f46ccf97b8373f62a22bbb3fde6.zip cpython-1649c1b33a1d8f46ccf97b8373f62a22bbb3fde6.tar.gz cpython-1649c1b33a1d8f46ccf97b8373f62a22bbb3fde6.tar.bz2 |
Issue #14181: Preserve backwards compatibility for getbufferprocs that a) do
not adhere to the new documentation and b) manage to clobber view->obj before
returning failure.
-rw-r--r-- | Lib/test/test_buffer.py | 6 | ||||
-rw-r--r-- | Modules/_testbuffer.c | 29 | ||||
-rw-r--r-- | Objects/memoryobject.c | 2 |
3 files changed, 22 insertions, 15 deletions
diff --git a/Lib/test/test_buffer.py b/Lib/test/test_buffer.py index 533338e..e0006f2 100644 --- a/Lib/test/test_buffer.py +++ b/Lib/test/test_buffer.py @@ -3609,6 +3609,12 @@ class TestBufferProtocol(unittest.TestCase): lst=lst) del x, y, z, m + def test_memoryview_getbuffer_undefined(self): + + # getbufferproc does not adhere to the new documentation + nd = ndarray([1,2,3], [3], flags=ND_GETBUF_FAIL|ND_GETBUF_UNDEFINED) + self.assertRaises(BufferError, memoryview, nd) + def test_issue_7385(self): x = ndarray([1,2,3], shape=[3], flags=ND_GETBUF_FAIL) self.assertRaises(BufferError, memoryview, x) diff --git a/Modules/_testbuffer.c b/Modules/_testbuffer.c index 63f6b53..cc4aea8 100644 --- a/Modules/_testbuffer.c +++ b/Modules/_testbuffer.c @@ -44,23 +44,21 @@ static PyTypeObject NDArray_Type; #define ADJUST_PTR(ptr, suboffsets) \ (HAVE_PTR(suboffsets) ? *((char**)ptr) + suboffsets[0] : ptr) +/* Default: NumPy style (strides), read-only, no var-export, C-style layout */ +#define ND_DEFAULT 0x000 /* User configurable flags for the ndarray */ -#define ND_VAREXPORT 0x001 /* change layout while buffers are exported */ - +#define ND_VAREXPORT 0x001 /* change layout while buffers are exported */ /* User configurable flags for each base buffer */ -#define ND_WRITABLE 0x002 /* mark base buffer as writable */ -#define ND_FORTRAN 0x004 /* Fortran contiguous layout */ -#define ND_SCALAR 0x008 /* scalar: ndim = 0 */ -#define ND_PIL 0x010 /* convert to PIL-style array (suboffsets) */ -#define ND_GETBUF_FAIL 0x020 /* test issue 7385 */ -#define ND_REDIRECT 0x040 /* redirect buffer requests */ - -/* Default: NumPy style (strides), read-only, no var-export, C-style layout */ -#define ND_DEFAULT 0x0 - +#define ND_WRITABLE 0x002 /* mark base buffer as writable */ +#define ND_FORTRAN 0x004 /* Fortran contiguous layout */ +#define ND_SCALAR 0x008 /* scalar: ndim = 0 */ +#define ND_PIL 0x010 /* convert to PIL-style array (suboffsets) */ +#define ND_REDIRECT 0x020 /* redirect buffer requests */ +#define ND_GETBUF_FAIL 0x040 /* trigger getbuffer failure */ +#define ND_GETBUF_UNDEFINED 0x080 /* undefined view.obj */ /* Internal flags for the base buffer */ -#define ND_C 0x080 /* C contiguous layout (default) */ -#define ND_OWN_ARRAYS 0x100 /* consumer owns arrays */ +#define ND_C 0x100 /* C contiguous layout (default) */ +#define ND_OWN_ARRAYS 0x200 /* consumer owns arrays */ /* ndarray properties */ #define ND_IS_CONSUMER(nd) \ @@ -1449,6 +1447,8 @@ ndarray_getbuf(NDArrayObject *self, Py_buffer *view, int flags) if (baseflags & ND_GETBUF_FAIL) { PyErr_SetString(PyExc_BufferError, "ND_GETBUF_FAIL: forced test exception"); + if (baseflags & ND_GETBUF_UNDEFINED) + view->obj = (PyObject *)0x1; /* wrong but permitted in <= 3.2 */ return -1; } @@ -2782,6 +2782,7 @@ PyInit__testbuffer(void) PyModule_AddIntConstant(m, "ND_SCALAR", ND_SCALAR); PyModule_AddIntConstant(m, "ND_PIL", ND_PIL); PyModule_AddIntConstant(m, "ND_GETBUF_FAIL", ND_GETBUF_FAIL); + PyModule_AddIntConstant(m, "ND_GETBUF_UNDEFINED", ND_GETBUF_UNDEFINED); PyModule_AddIntConstant(m, "ND_REDIRECT", ND_REDIRECT); PyModule_AddIntConstant(m, "PyBUF_SIMPLE", PyBUF_SIMPLE); diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c index 8ffdd41..67f7e01 100644 --- a/Objects/memoryobject.c +++ b/Objects/memoryobject.c @@ -86,7 +86,7 @@ _PyManagedBuffer_FromObject(PyObject *base) return NULL; if (PyObject_GetBuffer(base, &mbuf->master, PyBUF_FULL_RO) < 0) { - /* mbuf->master.obj must be NULL. */ + mbuf->master.obj = NULL; Py_DECREF(mbuf); return NULL; } |