[Python-Dev] New PEP: Using ssize_t as the index type

"Martin v. Löwis" martin at v.loewis.de
Fri Dec 30 10:48:52 CET 2005


Fredrik Lundh wrote:
> well, one thing seems to missing from your PEP: in several modules, you've
> changed the cast used in the type table. e.g.
...
> is this change backwards compatible ?

See the section "Conversion guidelines". I prefer the approach taken in
the patch below, i.e. remove the casts in initializing the type object.
Adding more typedefs for the function types would be the other option.

Regards,
Martin

Index: Modules/_elementtree.c
===================================================================
--- Modules/_elementtree.c	(Revision 41837)
+++ Modules/_elementtree.c	(Arbeitskopie)
@@ -92,6 +92,9 @@
 #endif

 /* compatibility macros */
+#if (PY_VERSION_HEX < 0x02050000)
+typedef int Py_ssize_t;
+#endif
 #if (PY_VERSION_HEX < 0x02040000)
 #define PyDict_CheckExact PyDict_Check
 #if (PY_VERSION_HEX < 0x02020000)
@@ -916,8 +919,9 @@
 }

 static PyObject*
-element_getitem(ElementObject* self, Py_ssize_t index)
+element_getitem(PyObject* _self, Py_ssize_t index)
 {
+    ElementObject* self = (ElementObject*)_self;
     if (!self->extra || index < 0 || index >= self->extra->length) {
         PyErr_SetString(
             PyExc_IndexError,
@@ -931,8 +935,9 @@
 }

 static PyObject*
-element_getslice(ElementObject* self, Py_ssize_t start, Py_ssize_t end)
+element_getslice(PyObject* _self, Py_ssize_t start, Py_ssize_t end)
 {
+    ElementObject* self = (ElementObject*)_self;
     Py_ssize_t i;
     PyObject* list;

@@ -1158,8 +1163,9 @@
 }

 static int
-element_setslice(ElementObject* self, Py_ssize_t start, Py_ssize_t end,
PyObject* item)
+element_setslice(PyObject* _self, Py_ssize_t start, Py_ssize_t end,
PyObject* item)
 {
+    ElementObject* self = (ElementObject*)_self;
     int i, new, old;
     PyObject* recycle = NULL;

@@ -1228,8 +1234,9 @@
 }

 static int
-element_setitem(ElementObject* self, Py_ssize_t index, PyObject* item)
+element_setitem(PyObject* _self, Py_ssize_t index, PyObject* item)
 {
+    ElementObject* self = (ElementObject*)_self;
     int i;
     PyObject* old;

@@ -1371,10 +1378,10 @@
     (inquiry) element_length,
     0, /* sq_concat */
     0, /* sq_repeat */
-    (ssizeargfunc) element_getitem,
-    (ssizessizeargfunc) element_getslice,
-    (ssizeobjargproc) element_setitem,
-    (ssizessizeobjargproc) element_setslice,
+    element_getitem,
+    element_getslice,
+    element_setitem,
+    element_setslice,
 };

 statichere PyTypeObject Element_Type = {


More information about the Python-Dev mailing list