Issue #15770: Check invalid arguments in test function. Patch by Victor Stinner.

This commit is contained in:
Stefan Krah 2012-08-23 15:53:45 +02:00
parent f21587e3a8
commit 66e63170d9
2 changed files with 17 additions and 3 deletions

View file

@ -1212,6 +1212,8 @@ class TestBufferProtocol(unittest.TestCase):
self.assertRaises(TypeError, get_contiguous, nd, PyBUF_READ, 961) self.assertRaises(TypeError, get_contiguous, nd, PyBUF_READ, 961)
self.assertRaises(UnicodeEncodeError, get_contiguous, nd, PyBUF_READ, self.assertRaises(UnicodeEncodeError, get_contiguous, nd, PyBUF_READ,
'\u2007') '\u2007')
self.assertRaises(ValueError, get_contiguous, nd, PyBUF_READ, 'Z')
self.assertRaises(ValueError, get_contiguous, nd, 255, 'A')
# cmp_contig() # cmp_contig()
nd = ndarray([1], shape=[1]) nd = ndarray([1], shape=[1])

View file

@ -2362,6 +2362,13 @@ get_ascii_order(PyObject *order)
ord = PyBytes_AS_STRING(ascii_order)[0]; ord = PyBytes_AS_STRING(ascii_order)[0];
Py_DECREF(ascii_order); Py_DECREF(ascii_order);
if (ord != 'C' && ord != 'F' && ord != 'A') {
PyErr_SetString(PyExc_ValueError,
"invalid order, must be C, F or A");
return CHAR_MAX;
}
return ord; return ord;
} }
@ -2384,16 +2391,21 @@ get_contiguous(PyObject *self, PyObject *args)
"buffertype must be PyBUF_READ or PyBUF_WRITE"); "buffertype must be PyBUF_READ or PyBUF_WRITE");
return NULL; return NULL;
} }
type = PyLong_AsLong(buffertype); type = PyLong_AsLong(buffertype);
if (type == -1 && PyErr_Occurred()) { if (type == -1 && PyErr_Occurred()) {
return NULL; return NULL;
} }
if (type != PyBUF_READ && type != PyBUF_WRITE) {
ord = get_ascii_order(order); PyErr_SetString(PyExc_ValueError,
if (ord == CHAR_MAX) { "invalid buffer type");
return NULL; return NULL;
} }
ord = get_ascii_order(order);
if (ord == CHAR_MAX)
return NULL;
return PyMemoryView_GetContiguous(obj, (int)type, ord); return PyMemoryView_GetContiguous(obj, (int)type, ord);
} }