Commits

Lenard Lindstrom  committed ac4f6f6

Correct faulty assertions added to the C code in changeset 67f1c85 and later

Some untested assert statements were added to base.c and surface.c as while
adding new buffer support. Some asserts failed when the unit tests were run
on a Pygame debug build. The PyBUF_PYGAME new buffer request flag, changeset
11e8afc, confused the base.c functions _arraystruct_as_buffer and
PgArrayStruct_AsBuffer. The BufferProxy __array_interface__ and
__array_struct__ getters failed to add the PyBUF_PYGAME flag, causing an
assertion fail in the surface.c _init_buffer function. A couple of other
assertion problems were fixed as well.

  • Participants
  • Parent commits 11e8afc

Comments (0)

Files changed (3)

     internal_p->cobj = 0;
     view_p->internal = internal_p;
     if (PyBUF_HAS_FLAG (flags, PyBUF_FORMAT)) {
-        if (_arraystruct_to_format(internal_p->format, inter_p, 3)) {
+        if (_arraystruct_to_format(internal_p->format,
+                                   inter_p,
+                                   sizeof (internal_p->format))) {
             return -1;
         }
     view_p->format = internal_p->format;
         view_p->format = 0;
     }
     view_p->buf = inter_p->data;
-    if ((flags | PyBUF_WRITABLE) == PyBUF_WRITABLE) {
-        view_p->ndim = 0;
-    }
-    else {
-        view_p->ndim = (Py_ssize_t)inter_p->nd;
-    }
     view_p->itemsize = (Py_ssize_t)inter_p->itemsize;
     view_p->readonly = readonly;
     if (PyBUF_HAS_FLAG (flags, PyBUF_ND)) {
+        view_p->ndim = (Py_ssize_t)inter_p->nd;
         view_p->shape = internal_p->imem;
         for (i = 0; i < view_p->ndim; ++i) {
             view_p->shape[i] = (Py_ssize_t)inter_p->shape[i];
         }
     }
     else if (inter_p->flags & PAI_CONTIGUOUS) {
+        view_p->ndim = 0;
         view_p->shape = 0;
     }
     else {
     if (!PyBUF_HAS_FLAG (flags, PyBUF_FORMAT)) {
         view_p->format = 0;
     }
-    if ((flags | PyBUF_WRITABLE) == PyBUF_WRITABLE) {
+    if (!PyBUF_HAS_FLAG (flags, PyBUF_ND)) {
         view_p->ndim = 0;
     }
     Py_XINCREF (obj);

File src/bufferproxy.c

 #define PROXY_TYPE_NAME "BufferProxy"
 #define PROXY_TYPE_FULLNAME (IMPPREFIX PROXY_TYPE_NAME)
 
+#ifdef NDEBUG
+#define PyBUF_PG_VIEW PyBUF_RECORDS
+#define PyBUF_PG_VIEW_RO PyBUF_RECORDS_RO
+#else
+#define PyBUF_PG_VIEW (PyBUF_RECORDS | PyBUF_PYGAME)
+#define PyBUF_PG_VIEW_RO (PyBUF_RECORDS_RO | PyBUF_PYGAME)
+#endif
+
 typedef struct PgBufproxyObject_s {
     PyObject_HEAD
     PyObject *obj;                             /* Wrapped object (parent)     */
         pg_view_p->consumer = (PyObject *)proxy;
         if (proxy->get_buffer(proxy->obj,
                               (Py_buffer *)pg_view_p,
-                              PyBUF_RECORDS_RO)) {
+                              PyBUF_PG_VIEW_RO)) {
             PyMem_Free(pg_view_p);
             return 0;
         }
         proxy->pg_view_p = pg_view_p;
     }
-    assert(((Py_buffer *)view_p)->len && ((Py_buffer *)view_p)->itemsize);
+    assert(((Py_buffer *)pg_view_p)->len && ((Py_buffer *)pg_view_p)->itemsize);
     return (Py_buffer *)pg_view_p;
 }
 
     }
 #undef ARG_FORMAT
 
-    if (proxy_getbuffer(self, &view, PyBUF_RECORDS)) {
+    if (proxy_getbuffer(self, &view, PyBUF_PG_VIEW)) {
         return 0;
     }
     if (!PyBuffer_IsContiguous(&view, 'A')) {
 {
     Py_buffer *obj_view_p = PyMem_Malloc(sizeof (Pg_buffer));
 
+#ifndef NDEBUG
+    flags |= PyBUF_PYGAME;
+#endif
     view_p->obj = 0;
     if (!obj_view_p) {
         PyErr_NoMemory();

File src/surface.c

     Pg_bufferinternal *internal;
 
     assert (surf);
-    assert (pg_view_p);
+    assert (view_p);
     assert (PyObject_IsInstance (surf, (PyObject *)&PySurface_Type));
     assert (PyBUF_HAS_FLAG (flags, PyBUF_PYGAME));
     consumer = ((Pg_buffer *)view_p)->consumer;