Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 1 | /* enumerate object */ |
| 2 | |
| 3 | #include "Python.h" |
| 4 | |
Serhiy Storchaka | 41baebd | 2017-01-19 18:48:17 +0200 | [diff] [blame] | 5 | #include "clinic/enumobject.c.h" |
| 6 | |
| 7 | /*[clinic input] |
| 8 | class enumerate "enumobject *" "&PyEnum_Type" |
| 9 | class reversed "reversedobject *" "&PyReversed_Type" |
| 10 | [clinic start generated code]*/ |
| 11 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=d2dfdf1a88c88975]*/ |
| 12 | |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 13 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 14 | PyObject_HEAD |
| 15 | Py_ssize_t en_index; /* current index of enumeration */ |
| 16 | PyObject* en_sit; /* secondary iterator of enumeration */ |
| 17 | PyObject* en_result; /* result tuple */ |
| 18 | PyObject* en_longindex; /* index for sequences >= PY_SSIZE_T_MAX */ |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 19 | } enumobject; |
| 20 | |
Serhiy Storchaka | 41baebd | 2017-01-19 18:48:17 +0200 | [diff] [blame] | 21 | |
| 22 | /*[clinic input] |
| 23 | @classmethod |
| 24 | enumerate.__new__ as enum_new |
| 25 | |
| 26 | iterable: object |
| 27 | an object supporting iteration |
| 28 | start: object = 0 |
| 29 | |
| 30 | Return an enumerate object. |
| 31 | |
| 32 | The enumerate object yields pairs containing a count (from start, which |
| 33 | defaults to zero) and a value yielded by the iterable argument. |
| 34 | |
| 35 | enumerate is useful for obtaining an indexed list: |
| 36 | (0, seq[0]), (1, seq[1]), (2, seq[2]), ... |
| 37 | [clinic start generated code]*/ |
| 38 | |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 39 | static PyObject * |
Serhiy Storchaka | 41baebd | 2017-01-19 18:48:17 +0200 | [diff] [blame] | 40 | enum_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *start) |
| 41 | /*[clinic end generated code: output=e95e6e439f812c10 input=782e4911efcb8acf]*/ |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 42 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 43 | enumobject *en; |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 44 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 45 | en = (enumobject *)type->tp_alloc(type, 0); |
| 46 | if (en == NULL) |
| 47 | return NULL; |
| 48 | if (start != NULL) { |
| 49 | start = PyNumber_Index(start); |
| 50 | if (start == NULL) { |
| 51 | Py_DECREF(en); |
| 52 | return NULL; |
| 53 | } |
| 54 | assert(PyLong_Check(start)); |
| 55 | en->en_index = PyLong_AsSsize_t(start); |
| 56 | if (en->en_index == -1 && PyErr_Occurred()) { |
| 57 | PyErr_Clear(); |
| 58 | en->en_index = PY_SSIZE_T_MAX; |
| 59 | en->en_longindex = start; |
| 60 | } else { |
| 61 | en->en_longindex = NULL; |
| 62 | Py_DECREF(start); |
| 63 | } |
| 64 | } else { |
| 65 | en->en_index = 0; |
| 66 | en->en_longindex = NULL; |
| 67 | } |
Serhiy Storchaka | 41baebd | 2017-01-19 18:48:17 +0200 | [diff] [blame] | 68 | en->en_sit = PyObject_GetIter(iterable); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | if (en->en_sit == NULL) { |
| 70 | Py_DECREF(en); |
| 71 | return NULL; |
| 72 | } |
| 73 | en->en_result = PyTuple_Pack(2, Py_None, Py_None); |
| 74 | if (en->en_result == NULL) { |
| 75 | Py_DECREF(en); |
| 76 | return NULL; |
| 77 | } |
| 78 | return (PyObject *)en; |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static void |
| 82 | enum_dealloc(enumobject *en) |
| 83 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | PyObject_GC_UnTrack(en); |
| 85 | Py_XDECREF(en->en_sit); |
| 86 | Py_XDECREF(en->en_result); |
| 87 | Py_XDECREF(en->en_longindex); |
| 88 | Py_TYPE(en)->tp_free(en); |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static int |
| 92 | enum_traverse(enumobject *en, visitproc visit, void *arg) |
| 93 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | Py_VISIT(en->en_sit); |
| 95 | Py_VISIT(en->en_result); |
| 96 | Py_VISIT(en->en_longindex); |
| 97 | return 0; |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 101 | enum_next_long(enumobject *en, PyObject* next_item) |
| 102 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 103 | PyObject *result = en->en_result; |
| 104 | PyObject *next_index; |
| 105 | PyObject *stepped_up; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 106 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 107 | if (en->en_longindex == NULL) { |
| 108 | en->en_longindex = PyLong_FromSsize_t(PY_SSIZE_T_MAX); |
| 109 | if (en->en_longindex == NULL) |
| 110 | return NULL; |
| 111 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 112 | next_index = en->en_longindex; |
| 113 | assert(next_index != NULL); |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 114 | stepped_up = PyNumber_Add(next_index, _PyLong_One); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | if (stepped_up == NULL) |
| 116 | return NULL; |
| 117 | en->en_longindex = stepped_up; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 119 | if (result->ob_refcnt == 1) { |
| 120 | Py_INCREF(result); |
| 121 | Py_DECREF(PyTuple_GET_ITEM(result, 0)); |
| 122 | Py_DECREF(PyTuple_GET_ITEM(result, 1)); |
| 123 | } else { |
| 124 | result = PyTuple_New(2); |
| 125 | if (result == NULL) { |
| 126 | Py_DECREF(next_index); |
| 127 | Py_DECREF(next_item); |
| 128 | return NULL; |
| 129 | } |
| 130 | } |
| 131 | PyTuple_SET_ITEM(result, 0, next_index); |
| 132 | PyTuple_SET_ITEM(result, 1, next_item); |
| 133 | return result; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static PyObject * |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 137 | enum_next(enumobject *en) |
| 138 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | PyObject *next_index; |
| 140 | PyObject *next_item; |
| 141 | PyObject *result = en->en_result; |
| 142 | PyObject *it = en->en_sit; |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 143 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 144 | next_item = (*Py_TYPE(it)->tp_iternext)(it); |
| 145 | if (next_item == NULL) |
| 146 | return NULL; |
Guido van Rossum | ca5ed5b | 2002-07-16 21:02:42 +0000 | [diff] [blame] | 147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 148 | if (en->en_index == PY_SSIZE_T_MAX) |
| 149 | return enum_next_long(en, next_item); |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 150 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | next_index = PyLong_FromSsize_t(en->en_index); |
| 152 | if (next_index == NULL) { |
| 153 | Py_DECREF(next_item); |
| 154 | return NULL; |
| 155 | } |
| 156 | en->en_index++; |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 157 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | if (result->ob_refcnt == 1) { |
| 159 | Py_INCREF(result); |
| 160 | Py_DECREF(PyTuple_GET_ITEM(result, 0)); |
| 161 | Py_DECREF(PyTuple_GET_ITEM(result, 1)); |
| 162 | } else { |
| 163 | result = PyTuple_New(2); |
| 164 | if (result == NULL) { |
| 165 | Py_DECREF(next_index); |
| 166 | Py_DECREF(next_item); |
| 167 | return NULL; |
| 168 | } |
| 169 | } |
| 170 | PyTuple_SET_ITEM(result, 0, next_index); |
| 171 | PyTuple_SET_ITEM(result, 1, next_item); |
| 172 | return result; |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 175 | static PyObject * |
| 176 | enum_reduce(enumobject *en) |
| 177 | { |
| 178 | if (en->en_longindex != NULL) |
| 179 | return Py_BuildValue("O(OO)", Py_TYPE(en), en->en_sit, en->en_longindex); |
| 180 | else |
| 181 | return Py_BuildValue("O(On)", Py_TYPE(en), en->en_sit, en->en_index); |
| 182 | } |
| 183 | |
| 184 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 185 | |
| 186 | static PyMethodDef enum_methods[] = { |
| 187 | {"__reduce__", (PyCFunction)enum_reduce, METH_NOARGS, reduce_doc}, |
| 188 | {NULL, NULL} /* sentinel */ |
| 189 | }; |
| 190 | |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 191 | PyTypeObject PyEnum_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 192 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 193 | "enumerate", /* tp_name */ |
| 194 | sizeof(enumobject), /* tp_basicsize */ |
| 195 | 0, /* tp_itemsize */ |
| 196 | /* methods */ |
| 197 | (destructor)enum_dealloc, /* tp_dealloc */ |
| 198 | 0, /* tp_print */ |
| 199 | 0, /* tp_getattr */ |
| 200 | 0, /* tp_setattr */ |
| 201 | 0, /* tp_reserved */ |
| 202 | 0, /* tp_repr */ |
| 203 | 0, /* tp_as_number */ |
| 204 | 0, /* tp_as_sequence */ |
| 205 | 0, /* tp_as_mapping */ |
| 206 | 0, /* tp_hash */ |
| 207 | 0, /* tp_call */ |
| 208 | 0, /* tp_str */ |
| 209 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 210 | 0, /* tp_setattro */ |
| 211 | 0, /* tp_as_buffer */ |
| 212 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
Serhiy Storchaka | 41baebd | 2017-01-19 18:48:17 +0200 | [diff] [blame] | 213 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 214 | enum_new__doc__, /* tp_doc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | (traverseproc)enum_traverse, /* tp_traverse */ |
| 216 | 0, /* tp_clear */ |
| 217 | 0, /* tp_richcompare */ |
| 218 | 0, /* tp_weaklistoffset */ |
Serhiy Storchaka | 41baebd | 2017-01-19 18:48:17 +0200 | [diff] [blame] | 219 | PyObject_SelfIter, /* tp_iter */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 220 | (iternextfunc)enum_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 221 | enum_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | 0, /* tp_members */ |
| 223 | 0, /* tp_getset */ |
| 224 | 0, /* tp_base */ |
| 225 | 0, /* tp_dict */ |
| 226 | 0, /* tp_descr_get */ |
| 227 | 0, /* tp_descr_set */ |
| 228 | 0, /* tp_dictoffset */ |
| 229 | 0, /* tp_init */ |
| 230 | PyType_GenericAlloc, /* tp_alloc */ |
| 231 | enum_new, /* tp_new */ |
| 232 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 233 | }; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 234 | |
| 235 | /* Reversed Object ***************************************************************/ |
| 236 | |
| 237 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 238 | PyObject_HEAD |
| 239 | Py_ssize_t index; |
| 240 | PyObject* seq; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 241 | } reversedobject; |
| 242 | |
Serhiy Storchaka | 41baebd | 2017-01-19 18:48:17 +0200 | [diff] [blame] | 243 | /*[clinic input] |
| 244 | @classmethod |
| 245 | reversed.__new__ as reversed_new |
| 246 | |
| 247 | sequence as seq: object |
| 248 | / |
| 249 | |
| 250 | Return a reverse iterator over the values of the given sequence. |
| 251 | [clinic start generated code]*/ |
| 252 | |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 253 | static PyObject * |
Serhiy Storchaka | 41baebd | 2017-01-19 18:48:17 +0200 | [diff] [blame] | 254 | reversed_new_impl(PyTypeObject *type, PyObject *seq) |
| 255 | /*[clinic end generated code: output=f7854cc1df26f570 input=aeb720361e5e3f1d]*/ |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 256 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 257 | Py_ssize_t n; |
Serhiy Storchaka | 41baebd | 2017-01-19 18:48:17 +0200 | [diff] [blame] | 258 | PyObject *reversed_meth; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | reversedobject *ro; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 260 | _Py_IDENTIFIER(__reversed__); |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 261 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 262 | reversed_meth = _PyObject_LookupSpecial(seq, &PyId___reversed__); |
Guido van Rossum | 97c1adf | 2016-08-18 09:22:23 -0700 | [diff] [blame] | 263 | if (reversed_meth == Py_None) { |
| 264 | Py_DECREF(reversed_meth); |
| 265 | PyErr_Format(PyExc_TypeError, |
| 266 | "'%.200s' object is not reversible", |
| 267 | Py_TYPE(seq)->tp_name); |
| 268 | return NULL; |
| 269 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | if (reversed_meth != NULL) { |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 271 | PyObject *res = _PyObject_CallNoArg(reversed_meth); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 272 | Py_DECREF(reversed_meth); |
| 273 | return res; |
| 274 | } |
| 275 | else if (PyErr_Occurred()) |
| 276 | return NULL; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | if (!PySequence_Check(seq)) { |
Guido van Rossum | 97c1adf | 2016-08-18 09:22:23 -0700 | [diff] [blame] | 279 | PyErr_Format(PyExc_TypeError, |
| 280 | "'%.200s' object is not reversible", |
| 281 | Py_TYPE(seq)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 282 | return NULL; |
| 283 | } |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 285 | n = PySequence_Size(seq); |
| 286 | if (n == -1) |
| 287 | return NULL; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 289 | ro = (reversedobject *)type->tp_alloc(type, 0); |
| 290 | if (ro == NULL) |
| 291 | return NULL; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 292 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 293 | ro->index = n-1; |
| 294 | Py_INCREF(seq); |
| 295 | ro->seq = seq; |
| 296 | return (PyObject *)ro; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | static void |
| 300 | reversed_dealloc(reversedobject *ro) |
| 301 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | PyObject_GC_UnTrack(ro); |
| 303 | Py_XDECREF(ro->seq); |
| 304 | Py_TYPE(ro)->tp_free(ro); |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static int |
| 308 | reversed_traverse(reversedobject *ro, visitproc visit, void *arg) |
| 309 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 | Py_VISIT(ro->seq); |
| 311 | return 0; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static PyObject * |
| 315 | reversed_next(reversedobject *ro) |
| 316 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 317 | PyObject *item; |
| 318 | Py_ssize_t index = ro->index; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 320 | if (index >= 0) { |
| 321 | item = PySequence_GetItem(ro->seq, index); |
| 322 | if (item != NULL) { |
| 323 | ro->index--; |
| 324 | return item; |
| 325 | } |
| 326 | if (PyErr_ExceptionMatches(PyExc_IndexError) || |
| 327 | PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 328 | PyErr_Clear(); |
| 329 | } |
| 330 | ro->index = -1; |
| 331 | Py_CLEAR(ro->seq); |
| 332 | return NULL; |
Raymond Hettinger | 029dba5 | 2004-02-10 09:33:39 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 335 | static PyObject * |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 336 | reversed_len(reversedobject *ro) |
| 337 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 338 | Py_ssize_t position, seqsize; |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 340 | if (ro->seq == NULL) |
| 341 | return PyLong_FromLong(0); |
| 342 | seqsize = PySequence_Size(ro->seq); |
| 343 | if (seqsize == -1) |
| 344 | return NULL; |
| 345 | position = ro->index + 1; |
| 346 | return PyLong_FromSsize_t((seqsize < position) ? 0 : position); |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 349 | 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] | 350 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 351 | static PyObject * |
| 352 | reversed_reduce(reversedobject *ro) |
| 353 | { |
| 354 | if (ro->seq) |
| 355 | return Py_BuildValue("O(O)n", Py_TYPE(ro), ro->seq, ro->index); |
| 356 | else |
| 357 | return Py_BuildValue("O(())", Py_TYPE(ro)); |
| 358 | } |
| 359 | |
| 360 | static PyObject * |
| 361 | reversed_setstate(reversedobject *ro, PyObject *state) |
| 362 | { |
| 363 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 364 | if (index == -1 && PyErr_Occurred()) |
| 365 | return NULL; |
| 366 | if (ro->seq != 0) { |
| 367 | Py_ssize_t n = PySequence_Size(ro->seq); |
| 368 | if (n < 0) |
| 369 | return NULL; |
| 370 | if (index < -1) |
| 371 | index = -1; |
| 372 | else if (index > n-1) |
| 373 | index = n-1; |
| 374 | ro->index = index; |
| 375 | } |
| 376 | Py_RETURN_NONE; |
| 377 | } |
| 378 | |
| 379 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 380 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 381 | static PyMethodDef reversediter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 382 | {"__length_hint__", (PyCFunction)reversed_len, METH_NOARGS, length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 383 | {"__reduce__", (PyCFunction)reversed_reduce, METH_NOARGS, reduce_doc}, |
| 384 | {"__setstate__", (PyCFunction)reversed_setstate, METH_O, setstate_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 385 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 06353f7 | 2004-02-08 10:49:42 +0000 | [diff] [blame] | 386 | }; |
| 387 | |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 388 | PyTypeObject PyReversed_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 390 | "reversed", /* tp_name */ |
| 391 | sizeof(reversedobject), /* tp_basicsize */ |
| 392 | 0, /* tp_itemsize */ |
| 393 | /* methods */ |
| 394 | (destructor)reversed_dealloc, /* tp_dealloc */ |
| 395 | 0, /* tp_print */ |
| 396 | 0, /* tp_getattr */ |
| 397 | 0, /* tp_setattr */ |
| 398 | 0, /* tp_reserved */ |
| 399 | 0, /* tp_repr */ |
| 400 | 0, /* tp_as_number */ |
Serhiy Storchaka | 41baebd | 2017-01-19 18:48:17 +0200 | [diff] [blame] | 401 | 0, /* tp_as_sequence */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | 0, /* tp_as_mapping */ |
| 403 | 0, /* tp_hash */ |
| 404 | 0, /* tp_call */ |
| 405 | 0, /* tp_str */ |
| 406 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 407 | 0, /* tp_setattro */ |
| 408 | 0, /* tp_as_buffer */ |
| 409 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
Serhiy Storchaka | 41baebd | 2017-01-19 18:48:17 +0200 | [diff] [blame] | 410 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 411 | reversed_new__doc__, /* tp_doc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | (traverseproc)reversed_traverse,/* tp_traverse */ |
| 413 | 0, /* tp_clear */ |
| 414 | 0, /* tp_richcompare */ |
| 415 | 0, /* tp_weaklistoffset */ |
Serhiy Storchaka | 41baebd | 2017-01-19 18:48:17 +0200 | [diff] [blame] | 416 | PyObject_SelfIter, /* tp_iter */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 417 | (iternextfunc)reversed_next, /* tp_iternext */ |
Serhiy Storchaka | 41baebd | 2017-01-19 18:48:17 +0200 | [diff] [blame] | 418 | reversediter_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 419 | 0, /* tp_members */ |
| 420 | 0, /* tp_getset */ |
| 421 | 0, /* tp_base */ |
| 422 | 0, /* tp_dict */ |
| 423 | 0, /* tp_descr_get */ |
| 424 | 0, /* tp_descr_set */ |
| 425 | 0, /* tp_dictoffset */ |
| 426 | 0, /* tp_init */ |
| 427 | PyType_GenericAlloc, /* tp_alloc */ |
| 428 | reversed_new, /* tp_new */ |
| 429 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 430 | }; |