1. Lenard Lindstrom
  2. newbuffer

Commits

Lenard Lindstrom  committed 9dc3efb

Give the BufferMixin callback methods more Pythonic names

The method names __getbuffer__ and __releasebuffer__ are consistent with the
corresponding special functions defined in Cython.

  • Participants
  • Parent commits 648fe1a
  • Branches default

Comments (0)

Files changed (2)

File newbuffer.c

View file
  • Ignore whitespace
     if (!py_view) {
         return -1;
     }
-    py_method = PyObject_GetAttrString(self, "_get_buffer");
+    py_method = PyObject_GetAttrString(self, "__getbuffer__");
     if (!py_method) {
         Py_DECREF(py_view);
         PyErr_Clear();
     }
     else if (py_rval) {
         PyErr_SetString(PyExc_ValueError,
-                        "_get_buffer method return value was not None");
+                        "__getbuffer__ method return value was not None");
         Py_DECREF(py_rval);
         return -1;
     }
     PyObject *py_rval = 0;
 
     if (py_view) {
-        py_rval = PyObject_CallMethod(self, "_release_buffer", "(O)", py_view);
+        py_rval = PyObject_CallMethod(self, "__releasebuffer__",
+                                      "(O)", py_view);
         if (py_rval) {
             Py_DECREF(py_rval);
         }

File newbuffer_test.py

View file
  • Ignore whitespace
         def __init__(self):
             self.state = "initial"
             self.flags = None
-        def _get_buffer(self, view, flags):
+        def __getbuffer__(self, view, flags):
             self.state = "get"
             self.flags = flags
             if (flags & PyBUF_WRITABLE) == PyBUF_WRITABLE:
             else:
                 view.strides = None
             view.suboffsets = None
-        def _release_buffer(self, view):
+        def __releasebuffer__(self, view):
             self.state = "release"
         def __len__(self):
             return len(self.raw)
         self.assertRaises(AttributeError, delattr, b, 'itemsize')
         self.assertRaises(AttributeError, delattr, b, 'internal')
 
-    def test_get_buffer(self):
+    def test__getbuffer__(self):
         """verify Py_buffer type get_buffer method"""
         b = Py_buffer(self.view_addr)
         self.assertTrue(b.obj is None)
         gc.collect()
         self.assertTrue(b.obj is None)
 
-    def test_release_buffer(self):
+    def test__releasebuffer__(self):
         """verify Py_buffer type release_buffer method"""
         e = self.Exporter()
 
         self.assertEqual(e.state, "release")
 
 class BufferMixinGetBufferTest(unittest.TestCase):
-    """BufferMixin._get_buffer verification"""
+    """BufferMixin.__getbuffer__ verification"""
     class B(BufferMixin):
         def __init__(self):
             self.is_called = False
             self.view = None
             self.is_view_alive = False
             self.is_view_obj_null = False
-        def _get_buffer(self, view, flags):
+        def __getbuffer__(self, view, flags):
             self.is_called = True
             self.is_view_alive = bool(view)
             if (self.is_view_alive):
             raise BufferError()
 
     def test_is_called(self):
-        """verify the _get_buffer method is properly called"""
+        """verify the __getbuffer__ method is properly called"""
         b = self.B()
         m = Py_buffer()
         self.assertFalse(b.is_called)
     def test_wrong_return_type(self):
         """verify exception raised for wrong return value"""
         class B(BufferMixin):
-            def _get_buffer(self, view, flags):
+            def __getbuffer__(self, view, flags):
                 return 0
         m = Py_buffer()
         self.assertRaises(ValueError, m.get_buffer, B())
 
     def test_default_method(self):
-        """verify the _get_buffer method is required"""
+        """verify the __getbuffer__ method is required"""
         m = Py_buffer()
         self.assertRaises(NotImplementedError, m.get_buffer, BufferMixin())
 
 class BufferMixinReleaseBufferTest(unittest.TestCase):
-    """BufferMixin._release_buffer verification"""
+    """BufferMixin.__releasebuffer__ verification"""
     class B(BufferMixin):
         def __init__(self):
             self.is_called = False
             self.is_view_obj_self = False
             self.allocation = create_string_buffer(20)
             self.format = create_string_buffer("=I".encode('ascii'))
-        def _get_buffer(self, view, flags):
+        def __getbuffer__(self, view, flags):
             view.buf = addressof(self.allocation)
             view.len = 20
             view.readonly = False
             view.itemsize = 4
             view.internal = None
             view.obj = self
-        def _release_buffer(self, view):
+        def __releasebuffer__(self, view):
             self.is_called = True
             self.view = view
             self.is_view_alive = bool(view)
             self.is_view_obj_self = view.obj is self
 
     def test_is_called(self):
-        """verify the _release_buffer method actually gets called"""
+        """verify the __releasebuffer__ method actually gets called"""
         import gc
 
         b = self.B()
             nitems = self.width * self.height
             self.allocation = self.c_int32_arr_t(*range(1, nitems + 1))
             self.released = False
-        def _get_buffer(self, view, flags):
+        def __getbuffer__(self, view, flags):
             view.buf = addressof(self.allocation)
             view.len = self.length
             view.readonly = False
             view.itemsize = self.itemsize
             view.internal = None
             view.obj = self
-        def _release_buffer(self, view):
+        def __releasebuffer__(self, view):
             view.release_buffer()
             self.released = True
 
     def test_operation(self):
-        """verify the buffer info given by _get_buffer() reaches the consumer"""
+        """verify the info given by __getbuffer__() reaches the consumer"""
         b = self.B()
         m = Py_buffer()
         m.get_buffer(b, PyBUF_RECORDS_RO)
 
     def test_multiple_inheritance(self):
         class A(object):
-            def _get_buffer(self, view, flags):
+            def __getbuffer__(self, view, flags):
                 view.obj = self
         class B(BufferMixin, A):
             pass