Commits

Lenard Lindstrom committed 1f6a9a7

Give the pygame._view.BufferProxy callback hooks more convensional names.

The words "prelude" and "postscript" are replaced with "before" and "after".

Comments (0)

Files changed (4)

 typedef struct PgBufproxyObject_s {
     PyObject_HEAD
     Py_buffer view;
-    Py_ssize_t imem[6];                   /* shape/stride alloc for ndim <= 3 */
-    char cmem[3];                         /* format alloc for simple types    */
-    int flags;                            /* contiguity and array shape order */
-    PgBufproxy_PreludeCallback prelude;       /* Lock callback             */
-    PgBufproxy_PostscriptCallback postscript; /* Release callback          */
-    PyObject *pyprelude;                  /* Python lock callable             */
-    PyObject *pypostscript;               /* Python release callback          */
-    int global_release;                   /* dealloc callback flag            */
-    PyObject *dict;                       /* Allow arbitrary attributes       */
-    PyObject *weakrefs;                   /* There can be reference cycles    */
+    Py_ssize_t imem[6];                /* shape/stride alloc for ndim <= 3 */
+    char cmem[3];                      /* format alloc for simple types    */
+    int flags;                         /* contiguity and array shape order */
+    PgBufproxy_CallbackBefore before;  /* Lock callback                    */
+    PgBufproxy_CallbackAfter after;    /* Release callback                 */
+    PyObject *pybefore;                /* Python lock callable             */
+    PyObject *pyafter;                 /* Python release callback          */
+    int global_release;                /* dealloc callback flag            */
+    PyObject *dict;                    /* Allow arbitrary attributes       */
+    PyObject *weakrefs;                /* There can be reference cycles    */
 } PgBufproxyObject;
 
 typedef struct capsule_interface_s {
  * Helper functions.
  */
 static int
-_bufproxy_null_prelude(PyObject *bufproxy) {
+_bufproxy_null_before(PyObject *bufproxy) {
     return 0;
 }
 
 static void
-_bufproxy_null_postscript(PyObject *bufproxy) {
+_bufproxy_null_after(PyObject *bufproxy) {
     return;
 }
 
 static int
-_bufproxy_python_prelude(PyObject *bufproxy)
+_bufproxy_python_before(PyObject *bufproxy)
 {
     PgBufproxyObject *v = (PgBufproxyObject *)bufproxy;
     PyObject *rvalue;
         parent = Py_None;
     }
     Py_INCREF(parent);
-    rvalue = PyObject_CallFunctionObjArgs(v->pyprelude, parent, 0);
+    rvalue = PyObject_CallFunctionObjArgs(v->pybefore, parent, 0);
     Py_DECREF(parent);
     if (rvalue) {
         Py_DECREF(rvalue);
 }
 
 static void
-_bufproxy_python_postscript(PyObject *bufproxy)
+_bufproxy_python_after(PyObject *bufproxy)
 {
     PgBufproxyObject *v = (PgBufproxyObject *)bufproxy;
     PyObject *rvalue;
         parent = Py_None;
     }
     Py_INCREF(parent);
-    rvalue = PyObject_CallFunctionObjArgs(v->pypostscript, parent, 0);
+    rvalue = PyObject_CallFunctionObjArgs(v->pyafter, parent, 0);
     PyErr_Clear();
     Py_XDECREF(rvalue);
     Py_DECREF(parent);
 _bufproxy_new_from_type(PyTypeObject *type,
                     Py_buffer *view,
                     int flags,
-                    PgBufproxy_PreludeCallback prelude,
-                    PgBufproxy_PostscriptCallback postscript,
-                    PyObject *pyprelude,
-                    PyObject *pypostscript)
+                    PgBufproxy_CallbackBefore before,
+                    PgBufproxy_CallbackAfter after,
+                    PyObject *pybefore,
+                    PyObject *pyafter)
 {
     int ndim = view->ndim;
     PgBufproxyObject *self;
     }
 
     self->flags = flags;
-    self->prelude = _bufproxy_null_prelude;
-    if (pyprelude) {
-        Py_INCREF(pyprelude);
-        self->prelude = _bufproxy_python_prelude;
+    self->before = _bufproxy_null_before;
+    if (pybefore) {
+        Py_INCREF(pybefore);
+        self->before = _bufproxy_python_before;
     }
-    else if (prelude) {
-        self->prelude = prelude;
+    else if (before) {
+        self->before = before;
     }
-    self->pyprelude = pyprelude;
-    self->postscript = _bufproxy_null_postscript;
-    if (pypostscript) {
-        Py_INCREF(pypostscript);
-        self->postscript = _bufproxy_python_postscript;
+    self->pybefore = pybefore;
+    self->after = _bufproxy_null_after;
+    if (pyafter) {
+        Py_INCREF(pyafter);
+        self->after = _bufproxy_python_after;
     }
-    else if (postscript) {
-        self->postscript = postscript;
+    else if (after) {
+        self->after = after;
     }
-    self->pypostscript = pypostscript;
+    self->pyafter = pyafter;
     self->global_release = 0;
     return (PyObject *)self;
 }
 _bufproxy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
     Py_buffer view;
-    PyObject *pyprelude = 0;
-    PyObject *pypostscript = 0;
+    PyObject *pybefore = 0;
+    PyObject *pyafter = 0;
     PyObject *self = 0;
     int i;
     /* The argument evaluation order is important: strides must follow shape. */
     char *keywords[] = {"shape", "typestr", "data", "strides", "parent",
-                        "prelude", "postscript", 0};
+                        "before", "after", 0};
 
     view.obj = 0;
     view.len = 0;
                                      _data_arg_convert, &view,
                                      _strides_arg_convert, &view,
                                      _parent_arg_convert, &view,
-                                     &pyprelude, &pypostscript)) {
+                                     &pybefore, &pyafter)) {
         _free_view(&view);
         return 0;
     }
-    if (pyprelude == Py_None) {
-        pyprelude = 0;
+    if (pybefore == Py_None) {
+        pybefore = 0;
     }
-    if (pypostscript == Py_None) {
-        pypostscript = 0;
+    if (pyafter == Py_None) {
+        pyafter = 0;
     }
     Py_XINCREF((PyObject *)view.obj);
     view.len = view.itemsize;
         view.len *= view.shape[i];
     }
     self = _bufproxy_new_from_type(type, &view, 0,
-                               0, 0, pyprelude, pypostscript);
+                               0, 0, pybefore, pyafter);
     _free_view(&view);
     return self;
 }
 _bufproxy_dealloc(PgBufproxyObject *self)
 {
     /* Guard against recursion */
-    if (!self->prelude) {
+    if (!self->before) {
         return;
     }
-    self->prelude = 0;
+    self->before = 0;
 
     if (self->global_release) {
-        self->postscript((PyObject *)self);
+        self->after((PyObject *)self);
     }
     Py_XDECREF((PyObject *)self->view.obj);
-    Py_XDECREF(self->pyprelude);
-    Py_XDECREF(self->pypostscript);
+    Py_XDECREF(self->pybefore);
+    Py_XDECREF(self->pyafter);
     Py_XDECREF(self->dict);
     if (self->weakrefs) {
         PyObject_ClearWeakRefs((PyObject *)self);
         return 0;
     }
     if (!self->global_release) {
-        if (self->prelude((PyObject *)self)) {
+        if (self->before((PyObject *)self)) {
             Py_DECREF(capsule);
             capsule = 0;
         }
     PyObject *dict = Pg_ViewAsDict(&self->view);
 
     if (dict && !self->global_release) {
-        if (self->prelude((PyObject *)self)) {
+        if (self->before((PyObject *)self)) {
             Py_DECREF(dict);
             dict = 0;
         }
         PyErr_SetString(PyExc_BufferError, "buffer not F contiguous");
         return -1;
     }
-    if (v->prelude(obj)) {
+    if (v->before(obj)) {
         return -1;
     }
     view->obj = obj;
 static void
 _bufproxy_releasebuffer(PyObject *obj, Py_buffer *view)
 {
-    ((PgBufproxyObject *)obj)->postscript(obj);
+    ((PgBufproxyObject *)obj)->after(obj);
 }
 
 static PyBufferProcs _bufproxy_bufferprocs =
 
 static PyTypeObject PgBufproxy_Type =
 {
-    TYPE_HEAD (NULL, 0)
-    "pygame._view.Bufproxy"     /* tp_name */
+    TYPE_HEAD(NULL, 0)
+    "pygame._view.Bufproxy",    /* tp_name */
     sizeof (PgBufproxyObject),  /* tp_basicsize */
     0,                          /* tp_itemsize */
     (destructor)_bufproxy_dealloc,  /* tp_dealloc */
 static PyObject *
 PgBufproxy_New(Py_buffer *view,
            int flags,
-           PgBufproxy_PreludeCallback prelude,
-           PgBufproxy_PostscriptCallback postscript)
+           PgBufproxy_CallbackBefore before,
+           PgBufproxy_CallbackAfter after)
 {
     return _bufproxy_new_from_type(&PgBufproxy_Type,
                                view,
                                flags,
-                               prelude,
-                               postscript,
+                               before,
+                               after,
                                0,
                                0);
 }
 
 #include "pgarrinter.h"
 
-typedef int (*PgBufproxy_PreludeCallback)(PyObject *);
-typedef void (*PgBufproxy_PostscriptCallback)(PyObject *);
+typedef int (*PgBufproxy_CallbackBefore)(PyObject *);
+typedef void (*PgBufproxy_CallbackAfter)(PyObject *);
 
 /* Bufferproxy flags */
 #define BUFPROXY_CONTIGUOUS    1
 
 typedef PyObject *(*_pgbufproxy_new_t)(Py_buffer *,
                                    int,
-                                   PgBufproxy_PreludeCallback,
-                                   PgBufproxy_PostscriptCallback);
+                                   PgBufproxy_CallbackBefore,
+                                   PgBufproxy_CallbackAfter);
 typedef PyObject *(*_pgbufproxy_get_obj_t)(PyObject *);
 typedef int *(*_pg_getarrayinterface_t)(PyObject *,
                                         PyObject **,
 static PyObject *surf_get_pixels_address (PyObject *self,
                                           PyObject *closure);
 static int _view_kind(PyObject *obj, void *view_kind_vptr);
-static int _view_prelude(PyObject *view);
-static void _view_postscript(PyObject *view);
+static int _view_before(PyObject *view);
+static void _view_after(PyObject *view);
 static PyObject *_raise_get_view_ndim_error(int bitsize, SurfViewKind kind);
 
 
     view.obj = self;
     Py_INCREF (self);
 
-    return PgBufproxy_New (&view, flags, _view_prelude, _view_postscript);
+    return PgBufproxy_New (&view, flags, _view_before, _view_after);
 }
 
 static int
 }
 
 static int
-_view_prelude (PyObject *view)
+_view_before (PyObject *view)
 {
     PyObject *surf = PgBufproxy_GetParent (view);
     int rcode;
 }
 
 static void
-_view_postscript (PyObject *view)
+_view_after (PyObject *view)
 {
     PyObject *surf = PgBufproxy_GetParent(view);
 

test/_view_test.py

         v = BufferProxy(parent=p, **self.view_keywords)
         self.assert_(v.parent is p)
 
-    def test_prelude(self):
+    def test_before(self):
         def callback(parent):
             success.append(parent is p)
 
 
         # For array interface
         success = []
-        v = BufferProxy(parent=p, prelude=callback, **self.view_keywords)
+        v = BufferProxy(parent=p, before=callback, **self.view_keywords)
         self.assertEqual(len(success), 0)
         d = v.__array_interface__
         self.assertEqual(len(success), 1)
 
         # For array struct
         success = []
-        v = BufferProxy(parent=p, prelude=callback, **self.view_keywords)
+        v = BufferProxy(parent=p, before=callback, **self.view_keywords)
         self.assertEqual(len(success), 0)
         c = v.__array_struct__
         self.assertEqual(len(success), 1)
         self.assertEqual(len(success), 1)
 
         # Callback raises an exception
-        v = BufferProxy(prelude=raise_exception, **self.view_keywords)
+        v = BufferProxy(before=raise_exception, **self.view_keywords)
         self.assertRaises(MyException, lambda : v.__array_struct__)
 
-    def test_postscript(self):
+    def test_after(self):
         def callback(parent):
             success.append(parent is p)
 
 
         # For array interface
         success = []
-        v = BufferProxy(parent=p, postscript=callback, **self.view_keywords)
+        v = BufferProxy(parent=p, after=callback, **self.view_keywords)
         self.assertEqual(len(success), 0)
         d = v.__array_interface__
         self.assertEqual(len(success), 0)
 
         # For array struct
         success = []
-        v = BufferProxy(parent=p, postscript=callback, **self.view_keywords)
+        v = BufferProxy(parent=p, after=callback, **self.view_keywords)
         self.assertEqual(len(success), 0)
         c = v.__array_struct__
         self.assertEqual(len(success), 0)
 
     def test_gc(self):
         """refcount agnostic check that contained objects are freed"""
-        def prelude_callback(parent):
+        def before_callback(parent):
             return r[0]
-        def postscript_callback(parent):
+        def after_callback(parent):
             return r[1]
         class Obj(object):
             pass
         weak_p = weakref.ref(p)
         weak_r0 = weakref.ref(r[0])
         weak_r1 = weakref.ref(r[1])
-        weak_prelude = weakref.ref(prelude_callback)
-        weak_postscript = weakref.ref(postscript_callback)
+        weak_before = weakref.ref(before_callback)
+        weak_after = weakref.ref(after_callback)
         v = BufferProxy(parent=p,
-                 prelude=prelude_callback,
-                 postscript=postscript_callback,
+                 before=before_callback,
+                 after=after_callback,
                  **self.view_keywords)
         weak_v = weakref.ref(v)
-        p = prelude_callback = postscript_callback = None
+        p = before_callback = after_callback = None
         gc.collect()
         self.assertTrue(weak_p() is not None)
-        self.assertTrue(weak_prelude() is not None)
-        self.assertTrue(weak_postscript() is not None)
+        self.assertTrue(weak_before() is not None)
+        self.assertTrue(weak_after() is not None)
         v = None
         gc.collect()
         self.assertTrue(weak_v() is None)
         self.assertTrue(weak_p() is None)
-        self.assertTrue(weak_prelude() is None)
-        self.assertTrue(weak_postscript() is None)
+        self.assertTrue(weak_before() is None)
+        self.assertTrue(weak_after() is None)
         self.assertTrue(weak_r0() is not None)
         self.assertTrue(weak_r1() is not None)
         r = None