]> granicus.if.org Git - python/commitdiff
Issue #15770: Check invalid arguments in test function. Patch by Victor Stinner.
authorStefan Krah <skrah@bytereef.org>
Thu, 23 Aug 2012 13:53:45 +0000 (15:53 +0200)
committerStefan Krah <skrah@bytereef.org>
Thu, 23 Aug 2012 13:53:45 +0000 (15:53 +0200)
Lib/test/test_buffer.py
Modules/_testbuffer.c

index 80f43466fb0acb6bfe40b9239127dad6009aded8..fb85dae65493b93f2b4bf2d3d16a50d697cd0398 100644 (file)
@@ -1212,6 +1212,8 @@ class TestBufferProtocol(unittest.TestCase):
         self.assertRaises(TypeError, get_contiguous, nd, PyBUF_READ, 961)
         self.assertRaises(UnicodeEncodeError, get_contiguous, nd, PyBUF_READ,
                           '\u2007')
+        self.assertRaises(ValueError, get_contiguous, nd, PyBUF_READ, 'Z')
+        self.assertRaises(ValueError, get_contiguous, nd, 255, 'A')
 
         # cmp_contig()
         nd = ndarray([1], shape=[1])
index b291a14e209eb31b99ff8a07311ac0a1a4f848db..87ada0a092845ae8eee2f9b302a27ad04c71c67d 100644 (file)
@@ -2362,6 +2362,13 @@ get_ascii_order(PyObject *order)
 
     ord = PyBytes_AS_STRING(ascii_order)[0];
     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;
 }
 
@@ -2384,15 +2391,20 @@ get_contiguous(PyObject *self, PyObject *args)
             "buffertype must be PyBUF_READ or PyBUF_WRITE");
         return NULL;
     }
+
     type = PyLong_AsLong(buffertype);
     if (type == -1 && PyErr_Occurred()) {
         return NULL;
     }
+    if (type != PyBUF_READ && type != PyBUF_WRITE) {
+        PyErr_SetString(PyExc_ValueError,
+            "invalid buffer type");
+        return NULL;
+    }
 
     ord = get_ascii_order(order);
-    if (ord == CHAR_MAX) {
+    if (ord == CHAR_MAX)
         return NULL;
-    }
 
     return PyMemoryView_GetContiguous(obj, (int)type, ord);
 }