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