Lenard Lindstrom avatar 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.

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);

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();
     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;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.