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