Buffer: Always do item size check
authorDag Sverre Seljebotn <dagss@student.matnat.uio.no>
Sun, 19 Apr 2009 21:06:10 +0000 (23:06 +0200)
committerDag Sverre Seljebotn <dagss@student.matnat.uio.no>
Sun, 19 Apr 2009 21:06:10 +0000 (23:06 +0200)
Cython/Compiler/Buffer.py

index 7934ea34a2f77e0fc94ede702aca423e4ca2d962..2d72c56f8d13136abe60721820f0facebd792dec 100644 (file)
@@ -670,6 +670,11 @@ def get_getbuffer_code(dtype, code):
             __Pyx_BufferNdimError(buf, nd);
             goto fail;
           }
+          if (buf->itemsize != sizeof(%(dtype_cname)s)) {
+            PyErr_SetString(PyExc_ValueError,
+              "Item size of buffer does not match size of %(dtype)s.");
+            goto fail;
+          }
           if (!cast) {
             ts = buf->format;
             ts = __Pyx_ConsumeWhitespace(ts);
@@ -684,12 +689,6 @@ def get_getbuffer_code(dtype, code):
                 __Pyx_DescribeTokenInFormatString(ts));
               goto fail;
             }
-          } else {
-            if (buf->itemsize != sizeof(%(dtype_cname)s)) {
-              PyErr_SetString(PyExc_ValueError,
-                "Attempted cast of buffer to datatype of different size.");
-              goto fail;
-            }
           }
           if (buf->suboffsets == NULL) buf->suboffsets = __Pyx_minusones;
           return 0;