Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 1 | /* enumerate object */ |
| 2 | |
| 3 | #include "Python.h" |
| 4 | |
| 5 | typedef struct { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6 | PyObject_HEAD |
| 7 | Py_ssize_t en_index; /* current index of enumeration */ |
| 8 | PyObject* en_sit; /* secondary iterator of enumeration */ |
| 9 | PyObject* en_result; /* result tuple */ |
| 10 | PyObject* en_longindex; /* index for sequences >= PY_SSIZE_T_MAX */ |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 11 | } enumobject; |
| 12 | |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 13 | static PyObject * |
| 14 | enum_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 15 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 16 | enumobject *en; |
| 17 | PyObject *seq = NULL; |
| 18 | PyObject *start = NULL; |
| 19 | static char *kwlist[] = {"sequence", "start", 0}; |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 20 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 21 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:enumerate", kwlist, |
| 22 | &seq, &start)) |
| 23 | return NULL; |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 24 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 25 | en = (enumobject *)type->tp_alloc(type, 0); |
| 26 | if (en == NULL) |
| 27 | return NULL; |
| 28 | if (start != NULL) { |
| 29 | start = PyNumber_Index(start); |
| 30 | if (start == NULL) { |
| 31 | Py_DECREF(en); |
| 32 | return NULL; |
| 33 | } |
Serhiy Storchaka | 48c8bf2 | 2018-07-31 09:09:36 +0300 | [diff] [blame] | 34 | assert(_PyAnyInt_Check(start)); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 35 | en->en_index = PyInt_AsSsize_t(start); |
| 36 | if (en->en_index == -1 && PyErr_Occurred()) { |
| 37 | PyErr_Clear(); |
| 38 | en->en_index = PY_SSIZE_T_MAX; |
| 39 | en->en_longindex = start; |
| 40 | } else { |
| 41 | en->en_longindex = NULL; |
| 42 | Py_DECREF(start); |
| 43 | } |
| 44 | } else { |
| 45 | en->en_index = 0; |
| 46 | en->en_longindex = NULL; |
| 47 | } |
| 48 | en->en_sit = PyObject_GetIter(seq); |
| 49 | if (en->en_sit == NULL) { |
| 50 | Py_DECREF(en); |
| 51 | return NULL; |
| 52 | } |
| 53 | en->en_result = PyTuple_Pack(2, Py_None, Py_None); |
| 54 | if (en->en_result == NULL) { |
| 55 | Py_DECREF(en); |
| 56 | return NULL; |
| 57 | } |
| 58 | return (PyObject *)en; |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static void |
| 62 | enum_dealloc(enumobject *en) |
| 63 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 64 | PyObject_GC_UnTrack(en); |
| 65 | Py_XDECREF(en->en_sit); |
| 66 | Py_XDECREF(en->en_result); |
| 67 | Py_XDECREF(en->en_longindex); |
| 68 | Py_TYPE(en)->tp_free(en); |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static int |
| 72 | enum_traverse(enumobject *en, visitproc visit, void *arg) |
| 73 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 74 | Py_VISIT(en->en_sit); |
| 75 | Py_VISIT(en->en_result); |
| 76 | Py_VISIT(en->en_longindex); |
| 77 | return 0; |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static PyObject * |
Raymond Hettinger | 8f66937 | 2007-10-03 21:18:11 +0000 | [diff] [blame] | 81 | enum_next_long(enumobject *en, PyObject* next_item) |
| 82 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 83 | static PyObject *one = NULL; |
| 84 | PyObject *result = en->en_result; |
| 85 | PyObject *next_index; |
| 86 | PyObject *stepped_up; |
Raymond Hettinger | 8f66937 | 2007-10-03 21:18:11 +0000 | [diff] [blame] | 87 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 88 | if (en->en_longindex == NULL) { |
| 89 | en->en_longindex = PyInt_FromSsize_t(PY_SSIZE_T_MAX); |
Serhiy Storchaka | 19eb87d | 2017-09-26 09:11:27 +0300 | [diff] [blame] | 90 | if (en->en_longindex == NULL) { |
| 91 | Py_DECREF(next_item); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 92 | return NULL; |
Serhiy Storchaka | 19eb87d | 2017-09-26 09:11:27 +0300 | [diff] [blame] | 93 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 94 | } |
| 95 | if (one == NULL) { |
| 96 | one = PyInt_FromLong(1); |
Serhiy Storchaka | 19eb87d | 2017-09-26 09:11:27 +0300 | [diff] [blame] | 97 | if (one == NULL) { |
| 98 | Py_DECREF(next_item); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 99 | return NULL; |
Serhiy Storchaka | 19eb87d | 2017-09-26 09:11:27 +0300 | [diff] [blame] | 100 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 101 | } |
| 102 | next_index = en->en_longindex; |
| 103 | assert(next_index != NULL); |
| 104 | stepped_up = PyNumber_Add(next_index, one); |
Serhiy Storchaka | 19eb87d | 2017-09-26 09:11:27 +0300 | [diff] [blame] | 105 | if (stepped_up == NULL) { |
| 106 | Py_DECREF(next_item); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 107 | return NULL; |
Serhiy Storchaka | 19eb87d | 2017-09-26 09:11:27 +0300 | [diff] [blame] | 108 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 109 | en->en_longindex = stepped_up; |
Raymond Hettinger | 8f66937 | 2007-10-03 21:18:11 +0000 | [diff] [blame] | 110 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 111 | if (result->ob_refcnt == 1) { |
| 112 | Py_INCREF(result); |
| 113 | Py_DECREF(PyTuple_GET_ITEM(result, 0)); |
| 114 | Py_DECREF(PyTuple_GET_ITEM(result, 1)); |
| 115 | } else { |
| 116 | result = PyTuple_New(2); |
| 117 | if (result == NULL) { |
| 118 | Py_DECREF(next_index); |
| 119 | Py_DECREF(next_item); |
| 120 | return NULL; |
| 121 | } |
| 122 | } |
| 123 | PyTuple_SET_ITEM(result, 0, next_index); |
| 124 | PyTuple_SET_ITEM(result, 1, next_item); |
| 125 | return result; |
Raymond Hettinger | 8f66937 | 2007-10-03 21:18:11 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static PyObject * |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 129 | enum_next(enumobject *en) |
| 130 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 131 | PyObject *next_index; |
| 132 | PyObject *next_item; |
| 133 | PyObject *result = en->en_result; |
| 134 | PyObject *it = en->en_sit; |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 135 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 136 | next_item = (*Py_TYPE(it)->tp_iternext)(it); |
| 137 | if (next_item == NULL) |
| 138 | return NULL; |
Guido van Rossum | ca5ed5b | 2002-07-16 21:02:42 +0000 | [diff] [blame] | 139 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 140 | if (en->en_index == PY_SSIZE_T_MAX) |
| 141 | return enum_next_long(en, next_item); |
Raymond Hettinger | 8f66937 | 2007-10-03 21:18:11 +0000 | [diff] [blame] | 142 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 143 | next_index = PyInt_FromSsize_t(en->en_index); |
| 144 | if (next_index == NULL) { |
| 145 | Py_DECREF(next_item); |
| 146 | return NULL; |
| 147 | } |
| 148 | en->en_index++; |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 149 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 150 | if (result->ob_refcnt == 1) { |
| 151 | Py_INCREF(result); |
| 152 | Py_DECREF(PyTuple_GET_ITEM(result, 0)); |
| 153 | Py_DECREF(PyTuple_GET_ITEM(result, 1)); |
| 154 | } else { |
| 155 | result = PyTuple_New(2); |
| 156 | if (result == NULL) { |
| 157 | Py_DECREF(next_index); |
| 158 | Py_DECREF(next_item); |
| 159 | return NULL; |
| 160 | } |
| 161 | } |
| 162 | PyTuple_SET_ITEM(result, 0, next_index); |
| 163 | PyTuple_SET_ITEM(result, 1, next_item); |
| 164 | return result; |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 167 | PyDoc_STRVAR(enum_doc, |
Georg Brandl | 54d2898 | 2010-05-22 11:43:25 +0000 | [diff] [blame] | 168 | "enumerate(iterable[, start]) -> iterator for index, value of iterable\n" |
Jeremy Hylton | fbbe347 | 2003-04-21 20:26:25 +0000 | [diff] [blame] | 169 | "\n" |
Ezio Melotti | a9a7611 | 2009-09-25 16:07:55 +0000 | [diff] [blame] | 170 | "Return an enumerate object. iterable must be another object that supports\n" |
Jeremy Hylton | fbbe347 | 2003-04-21 20:26:25 +0000 | [diff] [blame] | 171 | "iteration. The enumerate object yields pairs containing a count (from\n" |
Georg Brandl | 54d2898 | 2010-05-22 11:43:25 +0000 | [diff] [blame] | 172 | "start, which defaults to zero) and a value yielded by the iterable argument.\n" |
| 173 | "enumerate is useful for obtaining an indexed list:\n" |
| 174 | " (0, seq[0]), (1, seq[1]), (2, seq[2]), ..."); |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 175 | |
| 176 | PyTypeObject PyEnum_Type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 177 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 178 | "enumerate", /* tp_name */ |
| 179 | sizeof(enumobject), /* tp_basicsize */ |
| 180 | 0, /* tp_itemsize */ |
| 181 | /* methods */ |
| 182 | (destructor)enum_dealloc, /* tp_dealloc */ |
| 183 | 0, /* tp_print */ |
| 184 | 0, /* tp_getattr */ |
| 185 | 0, /* tp_setattr */ |
| 186 | 0, /* tp_compare */ |
| 187 | 0, /* tp_repr */ |
| 188 | 0, /* tp_as_number */ |
| 189 | 0, /* tp_as_sequence */ |
| 190 | 0, /* tp_as_mapping */ |
| 191 | 0, /* tp_hash */ |
| 192 | 0, /* tp_call */ |
| 193 | 0, /* tp_str */ |
| 194 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 195 | 0, /* tp_setattro */ |
| 196 | 0, /* tp_as_buffer */ |
| 197 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 198 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 199 | enum_doc, /* tp_doc */ |
| 200 | (traverseproc)enum_traverse, /* tp_traverse */ |
| 201 | 0, /* tp_clear */ |
| 202 | 0, /* tp_richcompare */ |
| 203 | 0, /* tp_weaklistoffset */ |
| 204 | PyObject_SelfIter, /* tp_iter */ |
| 205 | (iternextfunc)enum_next, /* tp_iternext */ |
| 206 | 0, /* tp_methods */ |
| 207 | 0, /* tp_members */ |
| 208 | 0, /* tp_getset */ |
| 209 | 0, /* tp_base */ |
| 210 | 0, /* tp_dict */ |
| 211 | 0, /* tp_descr_get */ |
| 212 | 0, /* tp_descr_set */ |
| 213 | 0, /* tp_dictoffset */ |
| 214 | 0, /* tp_init */ |
| 215 | PyType_GenericAlloc, /* tp_alloc */ |
| 216 | enum_new, /* tp_new */ |
| 217 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 218 | }; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 219 | |
| 220 | /* Reversed Object ***************************************************************/ |
| 221 | |
| 222 | typedef struct { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 223 | PyObject_HEAD |
| 224 | Py_ssize_t index; |
| 225 | PyObject* seq; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 226 | } reversedobject; |
| 227 | |
| 228 | static PyObject * |
| 229 | reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 230 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 231 | Py_ssize_t n; |
| 232 | PyObject *seq, *reversed_meth; |
| 233 | static PyObject *reversed_cache = NULL; |
| 234 | reversedobject *ro; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 235 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 236 | if (type == &PyReversed_Type && !_PyArg_NoKeywords("reversed()", kwds)) |
| 237 | return NULL; |
Georg Brandl | ecf9091 | 2008-05-16 13:24:29 +0000 | [diff] [blame] | 238 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 239 | if (!PyArg_UnpackTuple(args, "reversed", 1, 1, &seq) ) |
| 240 | return NULL; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 241 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 242 | if (PyInstance_Check(seq)) { |
| 243 | reversed_meth = PyObject_GetAttrString(seq, "__reversed__"); |
| 244 | if (reversed_meth == NULL) { |
| 245 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 246 | PyErr_Clear(); |
| 247 | else |
| 248 | return NULL; |
| 249 | } |
| 250 | } |
| 251 | else { |
| 252 | reversed_meth = _PyObject_LookupSpecial(seq, "__reversed__", |
| 253 | &reversed_cache); |
| 254 | if (reversed_meth == NULL && PyErr_Occurred()) |
| 255 | return NULL; |
| 256 | } |
| 257 | if (reversed_meth != NULL) { |
| 258 | PyObject *res = PyObject_CallFunctionObjArgs(reversed_meth, NULL); |
| 259 | Py_DECREF(reversed_meth); |
| 260 | return res; |
| 261 | } |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 262 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 263 | if (!PySequence_Check(seq)) { |
| 264 | PyErr_SetString(PyExc_TypeError, |
| 265 | "argument to reversed() must be a sequence"); |
| 266 | return NULL; |
| 267 | } |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 268 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 269 | n = PySequence_Size(seq); |
| 270 | if (n == -1) |
| 271 | return NULL; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 272 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 273 | ro = (reversedobject *)type->tp_alloc(type, 0); |
| 274 | if (ro == NULL) |
| 275 | return NULL; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 276 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 277 | ro->index = n-1; |
| 278 | Py_INCREF(seq); |
| 279 | ro->seq = seq; |
| 280 | return (PyObject *)ro; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static void |
| 284 | reversed_dealloc(reversedobject *ro) |
| 285 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 286 | PyObject_GC_UnTrack(ro); |
| 287 | Py_XDECREF(ro->seq); |
| 288 | Py_TYPE(ro)->tp_free(ro); |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static int |
| 292 | reversed_traverse(reversedobject *ro, visitproc visit, void *arg) |
| 293 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 294 | Py_VISIT(ro->seq); |
| 295 | return 0; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static PyObject * |
| 299 | reversed_next(reversedobject *ro) |
| 300 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 301 | PyObject *item; |
| 302 | Py_ssize_t index = ro->index; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 303 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 304 | if (index >= 0) { |
| 305 | item = PySequence_GetItem(ro->seq, index); |
| 306 | if (item != NULL) { |
| 307 | ro->index--; |
| 308 | return item; |
| 309 | } |
| 310 | if (PyErr_ExceptionMatches(PyExc_IndexError) || |
| 311 | PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 312 | PyErr_Clear(); |
| 313 | } |
| 314 | ro->index = -1; |
| 315 | Py_CLEAR(ro->seq); |
| 316 | return NULL; |
Raymond Hettinger | 029dba5 | 2004-02-10 09:33:39 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 319 | PyDoc_STRVAR(reversed_doc, |
Raymond Hettinger | d2afee4 | 2004-08-25 19:42:12 +0000 | [diff] [blame] | 320 | "reversed(sequence) -> reverse iterator over values of the sequence\n" |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 321 | "\n" |
| 322 | "Return a reverse iterator"); |
| 323 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 324 | static PyObject * |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 325 | reversed_len(reversedobject *ro) |
| 326 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 327 | Py_ssize_t position, seqsize; |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 328 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 329 | if (ro->seq == NULL) |
| 330 | return PyInt_FromLong(0); |
| 331 | seqsize = PySequence_Size(ro->seq); |
| 332 | if (seqsize == -1) |
| 333 | return NULL; |
| 334 | position = ro->index + 1; |
| 335 | return PyInt_FromSsize_t((seqsize < position) ? 0 : position); |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 338 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 339 | |
| 340 | static PyMethodDef reversediter_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 341 | {"__length_hint__", (PyCFunction)reversed_len, METH_NOARGS, length_hint_doc}, |
| 342 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 06353f7 | 2004-02-08 10:49:42 +0000 | [diff] [blame] | 343 | }; |
| 344 | |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 345 | PyTypeObject PyReversed_Type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 346 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 347 | "reversed", /* tp_name */ |
| 348 | sizeof(reversedobject), /* tp_basicsize */ |
| 349 | 0, /* tp_itemsize */ |
| 350 | /* methods */ |
| 351 | (destructor)reversed_dealloc, /* tp_dealloc */ |
| 352 | 0, /* tp_print */ |
| 353 | 0, /* tp_getattr */ |
| 354 | 0, /* tp_setattr */ |
| 355 | 0, /* tp_compare */ |
| 356 | 0, /* tp_repr */ |
| 357 | 0, /* tp_as_number */ |
| 358 | 0, /* tp_as_sequence */ |
| 359 | 0, /* tp_as_mapping */ |
| 360 | 0, /* tp_hash */ |
| 361 | 0, /* tp_call */ |
| 362 | 0, /* tp_str */ |
| 363 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 364 | 0, /* tp_setattro */ |
| 365 | 0, /* tp_as_buffer */ |
| 366 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 367 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 368 | reversed_doc, /* tp_doc */ |
| 369 | (traverseproc)reversed_traverse,/* tp_traverse */ |
| 370 | 0, /* tp_clear */ |
| 371 | 0, /* tp_richcompare */ |
| 372 | 0, /* tp_weaklistoffset */ |
| 373 | PyObject_SelfIter, /* tp_iter */ |
| 374 | (iternextfunc)reversed_next, /* tp_iternext */ |
| 375 | reversediter_methods, /* tp_methods */ |
| 376 | 0, /* tp_members */ |
| 377 | 0, /* tp_getset */ |
| 378 | 0, /* tp_base */ |
| 379 | 0, /* tp_dict */ |
| 380 | 0, /* tp_descr_get */ |
| 381 | 0, /* tp_descr_set */ |
| 382 | 0, /* tp_dictoffset */ |
| 383 | 0, /* tp_init */ |
| 384 | PyType_GenericAlloc, /* tp_alloc */ |
| 385 | reversed_new, /* tp_new */ |
| 386 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 387 | }; |