[Python-checkins] r46369 - sandbox/trunk/hotbuffer/Modules/_hotbuf.c

bob.ippolito python-checkins at python.org
Fri May 26 19:47:01 CEST 2006


Author: bob.ippolito
Date: Fri May 26 19:47:00 2006
New Revision: 46369

Modified:
   sandbox/trunk/hotbuffer/Modules/_hotbuf.c
Log:
use tp_getset instead of tp_members

Modified: sandbox/trunk/hotbuffer/Modules/_hotbuf.c
==============================================================================
--- sandbox/trunk/hotbuffer/Modules/_hotbuf.c	(original)
+++ sandbox/trunk/hotbuffer/Modules/_hotbuf.c	Fri May 26 19:47:00 2006
@@ -816,22 +816,97 @@
 
 #define OFF(x) offsetof(PyHotbufObject, x)
 
-static PyMemberDef hotbuf_members[] = {
-    {"capacity", T_INT, OFF(b_capacity), RO,
-     "buffer's capacity, it's total allocated size"},
-    {"position", T_INT, OFF(b_position), RO,
-     "buffer's position"},
-    {"limit", T_INT, OFF(b_limit), RO,
-     "buffer's limit"},
-    {"mark_position", T_INT, OFF(b_mark_position), RO,
-     "buffer's mark position, -1 if not set"},
-    {"mark_limit", T_INT, OFF(b_mark_limit), RO,
-     "buffer's mark limit, -1 if not set"},
-    {NULL} /* Sentinel */
+/* 
+    Hotbuf Getters and setters
+*/
+
+static PyObject *
+hotbuf_get_capacity(PyHotbufObject *self, void *unused)
+{
+    return PyInt_FromSsize_t(self->b_capacity);
+}
+
+static PyObject *
+hotbuf_get_position(PyHotbufObject *self, void *unused)
+{
+    return PyInt_FromSsize_t(self->b_position);
+}
+
+static int
+hotbuf_set_position(PyHotbufObject *self, PyObject *arg, void *unused)
+{
+    Py_ssize_t newposition;
+
+    newposition = PyInt_AsSsize_t(arg);
+    if (newposition == -1 && PyErr_Occurred())
+        return -1;
+
+    if (newposition > self->b_capacity) {
+        PyErr_SetString(PyExc_IndexError,
+                        "position must be smaller than capacity");
+        return -1;
+    }
+
+    /* Set the new position */
+    self->b_position = newposition;
+
+    return 0;
+}
+
+static PyObject *
+hotbuf_get_limit(PyHotbufObject *self, void *unused)
+{
+    return PyInt_FromSsize_t(self->b_limit);
+}
+
+static int
+hotbuf_set_limit(PyHotbufObject *self, PyObject *arg, void *unused)
+{
+    Py_ssize_t newlimit;
+
+    newlimit = PyInt_AsSsize_t(arg);
+    if (newlimit == -1 && PyErr_Occurred())
+        return -1;
+
+    if (newlimit > self->b_capacity) {
+        PyErr_SetString(PyExc_IndexError,
+                        "limit must be smaller than capacity");
+        return -1;
+    }
+
+    /* Set the new limit */
+    self->b_limit = newlimit;
+
+    /* If the position is larger than the new limit, set it to the new
+       limit. */
+    if (self->b_position > self->b_limit)
+        self->b_position = newlimit;
+
+    return 0;
+}
+
+static PyObject *
+hotbuf_get_mark_position(PyHotbufObject *self, void *unused)
+{
+    return PyInt_FromSsize_t(self->b_mark_position);
+}
+
+static PyObject *
+hotbuf_get_mark_limit(PyHotbufObject *self, void *unused)
+{
+    return PyInt_FromSsize_t(self->b_mark_limit);
+}
+
+static PyGetSetDef hotbuf_getsetlist[] = {
+    {"capacity", (getter)hotbuf_get_capacity, (setter)NULL, "buffer's capacity", NULL},
+    {"position", (getter)hotbuf_get_position, (setter)hotbuf_set_position, "buffer's position", NULL},
+    {"limit", (getter)hotbuf_get_limit, (setter)hotbuf_set_limit, "buffer's limit", NULL},
+    {"mark_position", (getter)hotbuf_get_mark_position, (setter)NULL, "buffer's mark_position, -1 if not set", NULL},
+    {"mark_limit", (getter)hotbuf_get_mark_limit, (setter)NULL, "buffer's mark_limit, -1 if not set", NULL},
+    {NULL}
 };
 
-static PyMethodDef
-hotbuf_methods[] = {
+static PyMethodDef hotbuf_methods[] = {
     {"clear", (PyCFunction)hotbuf_clear, METH_NOARGS, clear__doc__},
     {"setposition", (PyCFunction)hotbuf_setposition, METH_O, setposition__doc__},
     {"advance", (PyCFunction)hotbuf_advance, METH_O, advance__doc__},
@@ -898,8 +973,8 @@
     0,                                          /* tp_iter */
     0,                                          /* tp_iternext */
     hotbuf_methods,                             /* tp_methods */
-    hotbuf_members,                             /* tp_members */
-    0,                                          /* tp_getset */
+    0,                                          /* tp_members */
+    hotbuf_getsetlist,                          /* tp_getset */
     0,                                          /* tp_base */
     0,                                          /* tp_dict */
     0,                                          /* tp_descr_get */


More information about the Python-checkins mailing list