/src/cpython/Objects/enumobject.c
Line | Count | Source |
1 | | /* enumerate object */ |
2 | | |
3 | | #include "Python.h" |
4 | | #include "pycore_call.h" // _PyObject_CallNoArgs() |
5 | | #include "pycore_long.h" // _PyLong_GetOne() |
6 | | #include "pycore_modsupport.h" // _PyArg_NoKwnames() |
7 | | #include "pycore_object.h" // _PyObject_GC_TRACK() |
8 | | #include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString |
9 | | #include "pycore_tuple.h" // _PyTuple_Recycle() |
10 | | |
11 | | #include "clinic/enumobject.c.h" |
12 | | |
13 | | /*[clinic input] |
14 | | class enumerate "enumobject *" "&PyEnum_Type" |
15 | | class reversed "reversedobject *" "&PyReversed_Type" |
16 | | [clinic start generated code]*/ |
17 | | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=d2dfdf1a88c88975]*/ |
18 | | |
19 | | typedef struct { |
20 | | PyObject_HEAD |
21 | | Py_ssize_t en_index; /* current index of enumeration */ |
22 | | PyObject* en_sit; /* secondary iterator of enumeration */ |
23 | | PyObject* en_result; /* result tuple */ |
24 | | PyObject* en_longindex; /* index for sequences >= PY_SSIZE_T_MAX */ |
25 | | PyObject* one; /* borrowed reference */ |
26 | | } enumobject; |
27 | | |
28 | 29.2M | #define _enumobject_CAST(op) ((enumobject *)(op)) |
29 | | |
30 | | /*[clinic input] |
31 | | @classmethod |
32 | | enumerate.__new__ as enum_new |
33 | | |
34 | | iterable: object |
35 | | an object supporting iteration |
36 | | start: object = 0 |
37 | | |
38 | | Return an enumerate object. |
39 | | |
40 | | The enumerate object yields pairs containing a count (from start, which |
41 | | defaults to zero) and a value yielded by the iterable argument. |
42 | | |
43 | | enumerate is useful for obtaining an indexed list: |
44 | | (0, seq[0]), (1, seq[1]), (2, seq[2]), ... |
45 | | [clinic start generated code]*/ |
46 | | |
47 | | static PyObject * |
48 | | enum_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *start) |
49 | | /*[clinic end generated code: output=e95e6e439f812c10 input=782e4911efcb8acf]*/ |
50 | 8.03M | { |
51 | 8.03M | enumobject *en; |
52 | | |
53 | 8.03M | en = (enumobject *)type->tp_alloc(type, 0); |
54 | 8.03M | if (en == NULL) |
55 | 0 | return NULL; |
56 | 8.03M | if (start != NULL) { |
57 | 8.68k | start = PyNumber_Index(start); |
58 | 8.68k | if (start == NULL) { |
59 | 0 | Py_DECREF(en); |
60 | 0 | return NULL; |
61 | 0 | } |
62 | 8.68k | assert(PyLong_Check(start)); |
63 | 8.68k | en->en_index = PyLong_AsSsize_t(start); |
64 | 8.68k | if (en->en_index == -1 && PyErr_Occurred()) { |
65 | 0 | PyErr_Clear(); |
66 | 0 | en->en_index = PY_SSIZE_T_MAX; |
67 | 0 | en->en_longindex = start; |
68 | 8.68k | } else { |
69 | 8.68k | en->en_longindex = NULL; |
70 | 8.68k | Py_DECREF(start); |
71 | 8.68k | } |
72 | 8.02M | } else { |
73 | 8.02M | en->en_index = 0; |
74 | 8.02M | en->en_longindex = NULL; |
75 | 8.02M | } |
76 | 8.03M | en->en_sit = PyObject_GetIter(iterable); |
77 | 8.03M | if (en->en_sit == NULL) { |
78 | 0 | Py_DECREF(en); |
79 | 0 | return NULL; |
80 | 0 | } |
81 | 8.03M | en->en_result = _PyTuple_FromPairSteal(Py_None, Py_None); |
82 | 8.03M | if (en->en_result == NULL) { |
83 | 0 | Py_DECREF(en); |
84 | 0 | return NULL; |
85 | 0 | } |
86 | 8.03M | en->one = _PyLong_GetOne(); /* borrowed reference */ |
87 | 8.03M | return (PyObject *)en; |
88 | 8.03M | } |
89 | | |
90 | | static int check_keyword(PyObject *kwnames, int index, |
91 | | const char *name) |
92 | 6.03k | { |
93 | 6.03k | PyObject *kw = PyTuple_GET_ITEM(kwnames, index); |
94 | 6.03k | if (!_PyUnicode_EqualToASCIIString(kw, name)) { |
95 | 0 | PyErr_Format(PyExc_TypeError, |
96 | 0 | "'%S' is an invalid keyword argument for enumerate()", kw); |
97 | 0 | return 0; |
98 | 0 | } |
99 | 6.03k | return 1; |
100 | 6.03k | } |
101 | | |
102 | | // TODO: Use AC when bpo-43447 is supported |
103 | | static PyObject * |
104 | | enumerate_vectorcall(PyObject *type, PyObject *const *args, |
105 | | size_t nargsf, PyObject *kwnames) |
106 | 8.03M | { |
107 | 8.03M | PyTypeObject *tp = _PyType_CAST(type); |
108 | 8.03M | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
109 | 8.03M | Py_ssize_t nkwargs = 0; |
110 | 8.03M | if (kwnames != NULL) { |
111 | 6.03k | nkwargs = PyTuple_GET_SIZE(kwnames); |
112 | 6.03k | } |
113 | | |
114 | | // Manually implement enumerate(iterable, start=...) |
115 | 8.03M | if (nargs + nkwargs == 2) { |
116 | 8.68k | if (nkwargs == 1) { |
117 | 6.03k | if (!check_keyword(kwnames, 0, "start")) { |
118 | 0 | return NULL; |
119 | 0 | } |
120 | 6.03k | } else if (nkwargs == 2) { |
121 | 0 | PyObject *kw0 = PyTuple_GET_ITEM(kwnames, 0); |
122 | 0 | if (_PyUnicode_EqualToASCIIString(kw0, "start")) { |
123 | 0 | if (!check_keyword(kwnames, 1, "iterable")) { |
124 | 0 | return NULL; |
125 | 0 | } |
126 | 0 | return enum_new_impl(tp, args[1], args[0]); |
127 | 0 | } |
128 | 0 | if (!check_keyword(kwnames, 0, "iterable") || |
129 | 0 | !check_keyword(kwnames, 1, "start")) { |
130 | 0 | return NULL; |
131 | 0 | } |
132 | |
|
133 | 0 | } |
134 | 8.68k | return enum_new_impl(tp, args[0], args[1]); |
135 | 8.68k | } |
136 | | |
137 | 8.02M | if (nargs + nkwargs == 1) { |
138 | 8.02M | if (nkwargs == 1 && !check_keyword(kwnames, 0, "iterable")) { |
139 | 0 | return NULL; |
140 | 0 | } |
141 | 8.02M | return enum_new_impl(tp, args[0], NULL); |
142 | 8.02M | } |
143 | | |
144 | 0 | if (nargs == 0) { |
145 | 0 | PyErr_SetString(PyExc_TypeError, |
146 | 0 | "enumerate() missing required argument 'iterable'"); |
147 | 0 | return NULL; |
148 | 0 | } |
149 | | |
150 | 0 | PyErr_Format(PyExc_TypeError, |
151 | 0 | "enumerate() takes at most 2 arguments (%zd given)", nargs + nkwargs); |
152 | 0 | return NULL; |
153 | 0 | } |
154 | | |
155 | | static void |
156 | | enum_dealloc(PyObject *op) |
157 | 8.03M | { |
158 | 8.03M | enumobject *en = _enumobject_CAST(op); |
159 | 8.03M | PyObject_GC_UnTrack(en); |
160 | 8.03M | Py_XDECREF(en->en_sit); |
161 | 8.03M | Py_XDECREF(en->en_result); |
162 | 8.03M | Py_XDECREF(en->en_longindex); |
163 | 8.03M | Py_TYPE(en)->tp_free(en); |
164 | 8.03M | } |
165 | | |
166 | | static int |
167 | | enum_traverse(PyObject *op, visitproc visit, void *arg) |
168 | 1.98k | { |
169 | 1.98k | enumobject *en = _enumobject_CAST(op); |
170 | 1.98k | Py_VISIT(en->en_sit); |
171 | 1.98k | Py_VISIT(en->en_result); |
172 | 1.98k | Py_VISIT(en->en_longindex); |
173 | 1.98k | return 0; |
174 | 1.98k | } |
175 | | |
176 | | // increment en_longindex with lock held, return the next index to be used |
177 | | // or NULL on error |
178 | | static inline PyObject * |
179 | | increment_longindex_lock_held(enumobject *en) |
180 | 0 | { |
181 | 0 | if (en->en_longindex == NULL) { |
182 | 0 | en->en_longindex = PyLong_FromSsize_t(PY_SSIZE_T_MAX); |
183 | 0 | if (en->en_longindex == NULL) { |
184 | 0 | return NULL; |
185 | 0 | } |
186 | 0 | } |
187 | 0 | assert(en->en_longindex != NULL); |
188 | | // We hold one reference to "next_index" (a.k.a. the old value of |
189 | | // en->en_longindex); we'll either return it or keep it in en->en_longindex |
190 | 0 | PyObject *next_index = en->en_longindex; |
191 | 0 | PyObject *stepped_up = PyNumber_Add(next_index, en->one); |
192 | 0 | if (stepped_up == NULL) { |
193 | 0 | return NULL; |
194 | 0 | } |
195 | 0 | en->en_longindex = stepped_up; |
196 | 0 | return next_index; |
197 | 0 | } |
198 | | |
199 | | static PyObject * |
200 | | enum_next_long(enumobject *en, PyObject* next_item) |
201 | 0 | { |
202 | 0 | PyObject *result = en->en_result; |
203 | 0 | PyObject *next_index; |
204 | 0 | PyObject *old_index; |
205 | 0 | PyObject *old_item; |
206 | | |
207 | |
|
208 | 0 | Py_BEGIN_CRITICAL_SECTION(en); |
209 | 0 | next_index = increment_longindex_lock_held(en); |
210 | 0 | Py_END_CRITICAL_SECTION(); |
211 | 0 | if (next_index == NULL) { |
212 | 0 | Py_DECREF(next_item); |
213 | 0 | return NULL; |
214 | 0 | } |
215 | | |
216 | 0 | if (_PyObject_IsUniquelyReferenced(result)) { |
217 | 0 | Py_INCREF(result); |
218 | 0 | old_index = PyTuple_GET_ITEM(result, 0); |
219 | 0 | old_item = PyTuple_GET_ITEM(result, 1); |
220 | 0 | PyTuple_SET_ITEM(result, 0, next_index); |
221 | 0 | PyTuple_SET_ITEM(result, 1, next_item); |
222 | 0 | Py_DECREF(old_index); |
223 | 0 | Py_DECREF(old_item); |
224 | | // bpo-42536: The GC may have untracked this result tuple. Since we're |
225 | | // recycling it, make sure it's tracked again: |
226 | 0 | _PyTuple_Recycle(result); |
227 | 0 | return result; |
228 | 0 | } |
229 | 0 | return _PyTuple_FromPairSteal(next_index, next_item); |
230 | 0 | } |
231 | | |
232 | | static PyObject * |
233 | | enum_next(PyObject *op) |
234 | 21.2M | { |
235 | 21.2M | enumobject *en = _enumobject_CAST(op); |
236 | 21.2M | PyObject *next_index; |
237 | 21.2M | PyObject *next_item; |
238 | 21.2M | PyObject *result = en->en_result; |
239 | 21.2M | PyObject *it = en->en_sit; |
240 | 21.2M | PyObject *old_index; |
241 | 21.2M | PyObject *old_item; |
242 | | |
243 | 21.2M | next_item = (*Py_TYPE(it)->tp_iternext)(it); |
244 | 21.2M | if (next_item == NULL) |
245 | 8.03M | return NULL; |
246 | | |
247 | 13.2M | Py_ssize_t en_index = FT_ATOMIC_LOAD_SSIZE_RELAXED(en->en_index); |
248 | 13.2M | if (en_index == PY_SSIZE_T_MAX) |
249 | 0 | return enum_next_long(en, next_item); |
250 | | |
251 | 13.2M | next_index = PyLong_FromSsize_t(en_index); |
252 | 13.2M | if (next_index == NULL) { |
253 | 0 | Py_DECREF(next_item); |
254 | 0 | return NULL; |
255 | 0 | } |
256 | 13.2M | FT_ATOMIC_STORE_SSIZE_RELAXED(en->en_index, en_index + 1); |
257 | | |
258 | 13.2M | if (_PyObject_IsUniquelyReferenced(result)) { |
259 | 13.2M | Py_INCREF(result); |
260 | 13.2M | old_index = PyTuple_GET_ITEM(result, 0); |
261 | 13.2M | old_item = PyTuple_GET_ITEM(result, 1); |
262 | 13.2M | PyTuple_SET_ITEM(result, 0, next_index); |
263 | 13.2M | PyTuple_SET_ITEM(result, 1, next_item); |
264 | 13.2M | Py_DECREF(old_index); |
265 | 13.2M | Py_DECREF(old_item); |
266 | | // bpo-42536: The GC may have untracked this result tuple. Since we're |
267 | | // recycling it, make sure it's tracked again: |
268 | 13.2M | _PyTuple_Recycle(result); |
269 | 13.2M | return result; |
270 | 13.2M | } |
271 | 0 | return _PyTuple_FromPairSteal(next_index, next_item); |
272 | 13.2M | } |
273 | | |
274 | | static PyObject * |
275 | | enum_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) |
276 | 0 | { |
277 | 0 | enumobject *en = _enumobject_CAST(op); |
278 | 0 | PyObject *result; |
279 | 0 | Py_BEGIN_CRITICAL_SECTION(en); |
280 | 0 | if (en->en_longindex != NULL) |
281 | 0 | result = Py_BuildValue("O(OO)", Py_TYPE(en), en->en_sit, en->en_longindex); |
282 | 0 | else |
283 | 0 | result = Py_BuildValue("O(On)", Py_TYPE(en), en->en_sit, en->en_index); |
284 | 0 | Py_END_CRITICAL_SECTION(); |
285 | 0 | return result; |
286 | 0 | } |
287 | | |
288 | | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
289 | | |
290 | | static PyMethodDef enum_methods[] = { |
291 | | {"__reduce__", enum_reduce, METH_NOARGS, reduce_doc}, |
292 | | {"__class_getitem__", Py_GenericAlias, |
293 | | METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, |
294 | | {NULL, NULL} /* sentinel */ |
295 | | }; |
296 | | |
297 | | PyTypeObject PyEnum_Type = { |
298 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
299 | | "enumerate", /* tp_name */ |
300 | | sizeof(enumobject), /* tp_basicsize */ |
301 | | 0, /* tp_itemsize */ |
302 | | /* methods */ |
303 | | enum_dealloc, /* tp_dealloc */ |
304 | | 0, /* tp_vectorcall_offset */ |
305 | | 0, /* tp_getattr */ |
306 | | 0, /* tp_setattr */ |
307 | | 0, /* tp_as_async */ |
308 | | 0, /* tp_repr */ |
309 | | 0, /* tp_as_number */ |
310 | | 0, /* tp_as_sequence */ |
311 | | 0, /* tp_as_mapping */ |
312 | | 0, /* tp_hash */ |
313 | | 0, /* tp_call */ |
314 | | 0, /* tp_str */ |
315 | | PyObject_GenericGetAttr, /* tp_getattro */ |
316 | | 0, /* tp_setattro */ |
317 | | 0, /* tp_as_buffer */ |
318 | | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
319 | | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
320 | | enum_new__doc__, /* tp_doc */ |
321 | | enum_traverse, /* tp_traverse */ |
322 | | 0, /* tp_clear */ |
323 | | 0, /* tp_richcompare */ |
324 | | 0, /* tp_weaklistoffset */ |
325 | | PyObject_SelfIter, /* tp_iter */ |
326 | | enum_next, /* tp_iternext */ |
327 | | enum_methods, /* tp_methods */ |
328 | | 0, /* tp_members */ |
329 | | 0, /* tp_getset */ |
330 | | 0, /* tp_base */ |
331 | | 0, /* tp_dict */ |
332 | | 0, /* tp_descr_get */ |
333 | | 0, /* tp_descr_set */ |
334 | | 0, /* tp_dictoffset */ |
335 | | 0, /* tp_init */ |
336 | | PyType_GenericAlloc, /* tp_alloc */ |
337 | | enum_new, /* tp_new */ |
338 | | PyObject_GC_Del, /* tp_free */ |
339 | | .tp_vectorcall = enumerate_vectorcall |
340 | | }; |
341 | | |
342 | | /* Reversed Object ***************************************************************/ |
343 | | |
344 | | typedef struct { |
345 | | PyObject_HEAD |
346 | | Py_ssize_t index; |
347 | | PyObject* seq; |
348 | | } reversedobject; |
349 | | |
350 | 1.16k | #define _reversedobject_CAST(op) ((reversedobject *)(op)) |
351 | | |
352 | | /*[clinic input] |
353 | | @classmethod |
354 | | reversed.__new__ as reversed_new |
355 | | |
356 | | object as seq: object |
357 | | / |
358 | | |
359 | | Return a reverse iterator over the values of the given sequence. |
360 | | [clinic start generated code]*/ |
361 | | |
362 | | static PyObject * |
363 | | reversed_new_impl(PyTypeObject *type, PyObject *seq) |
364 | | /*[clinic end generated code: output=f7854cc1df26f570 input=4781869729e3ba50]*/ |
365 | 42.1M | { |
366 | 42.1M | Py_ssize_t n; |
367 | 42.1M | PyObject *reversed_meth; |
368 | 42.1M | reversedobject *ro; |
369 | | |
370 | 42.1M | reversed_meth = _PyObject_LookupSpecial(seq, &_Py_ID(__reversed__)); |
371 | 42.1M | if (reversed_meth == Py_None) { |
372 | 0 | Py_DECREF(reversed_meth); |
373 | 0 | PyErr_Format(PyExc_TypeError, |
374 | 0 | "'%.200s' object is not reversible", |
375 | 0 | Py_TYPE(seq)->tp_name); |
376 | 0 | return NULL; |
377 | 0 | } |
378 | 42.1M | if (reversed_meth != NULL) { |
379 | 42.1M | PyObject *res = _PyObject_CallNoArgs(reversed_meth); |
380 | 42.1M | Py_DECREF(reversed_meth); |
381 | 42.1M | return res; |
382 | 42.1M | } |
383 | 184 | else if (PyErr_Occurred()) |
384 | 0 | return NULL; |
385 | | |
386 | 184 | if (!PySequence_Check(seq)) { |
387 | 0 | PyErr_Format(PyExc_TypeError, |
388 | 0 | "'%.200s' object is not reversible", |
389 | 0 | Py_TYPE(seq)->tp_name); |
390 | 0 | return NULL; |
391 | 0 | } |
392 | | |
393 | 184 | n = PySequence_Size(seq); |
394 | 184 | if (n == -1) |
395 | 0 | return NULL; |
396 | | |
397 | 184 | ro = (reversedobject *)type->tp_alloc(type, 0); |
398 | 184 | if (ro == NULL) |
399 | 0 | return NULL; |
400 | | |
401 | 184 | ro->index = n-1; |
402 | 184 | ro->seq = Py_NewRef(seq); |
403 | 184 | return (PyObject *)ro; |
404 | 184 | } |
405 | | |
406 | | static PyObject * |
407 | | reversed_vectorcall(PyObject *type, PyObject * const*args, |
408 | | size_t nargsf, PyObject *kwnames) |
409 | 42.1M | { |
410 | 42.1M | if (!_PyArg_NoKwnames("reversed", kwnames)) { |
411 | 0 | return NULL; |
412 | 0 | } |
413 | | |
414 | 42.1M | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
415 | 42.1M | if (!_PyArg_CheckPositional("reversed", nargs, 1, 1)) { |
416 | 0 | return NULL; |
417 | 0 | } |
418 | | |
419 | 42.1M | return reversed_new_impl(_PyType_CAST(type), args[0]); |
420 | 42.1M | } |
421 | | |
422 | | static void |
423 | | reversed_dealloc(PyObject *op) |
424 | 184 | { |
425 | 184 | reversedobject *ro = _reversedobject_CAST(op); |
426 | 184 | PyObject_GC_UnTrack(ro); |
427 | 184 | Py_XDECREF(ro->seq); |
428 | 184 | Py_TYPE(ro)->tp_free(ro); |
429 | 184 | } |
430 | | |
431 | | static int |
432 | | reversed_traverse(PyObject *op, visitproc visit, void *arg) |
433 | 0 | { |
434 | 0 | reversedobject *ro = _reversedobject_CAST(op); |
435 | 0 | Py_VISIT(ro->seq); |
436 | 0 | return 0; |
437 | 0 | } |
438 | | |
439 | | static PyObject * |
440 | | reversed_next(PyObject *op) |
441 | 976 | { |
442 | 976 | reversedobject *ro = _reversedobject_CAST(op); |
443 | 976 | PyObject *item; |
444 | 976 | Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(ro->index); |
445 | | |
446 | 976 | if (index >= 0) { |
447 | 842 | item = PySequence_GetItem(ro->seq, index); |
448 | 842 | if (item != NULL) { |
449 | 842 | FT_ATOMIC_STORE_SSIZE_RELAXED(ro->index, index - 1); |
450 | 842 | return item; |
451 | 842 | } |
452 | 0 | if (PyErr_ExceptionMatches(PyExc_IndexError) || |
453 | 0 | PyErr_ExceptionMatches(PyExc_StopIteration)) |
454 | 0 | PyErr_Clear(); |
455 | 0 | } |
456 | 134 | FT_ATOMIC_STORE_SSIZE_RELAXED(ro->index, -1); |
457 | 134 | #ifndef Py_GIL_DISABLED |
458 | 134 | Py_CLEAR(ro->seq); |
459 | 134 | #endif |
460 | 134 | return NULL; |
461 | 976 | } |
462 | | |
463 | | static PyObject * |
464 | | reversed_len(PyObject *op, PyObject *Py_UNUSED(ignored)) |
465 | 0 | { |
466 | 0 | reversedobject *ro = _reversedobject_CAST(op); |
467 | 0 | Py_ssize_t position, seqsize; |
468 | 0 | Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(ro->index); |
469 | |
|
470 | 0 | if (index == -1) |
471 | 0 | return PyLong_FromLong(0); |
472 | 0 | assert(ro->seq != NULL); |
473 | 0 | seqsize = PySequence_Size(ro->seq); |
474 | 0 | if (seqsize == -1) |
475 | 0 | return NULL; |
476 | 0 | position = index + 1; |
477 | 0 | return PyLong_FromSsize_t((seqsize < position) ? 0 : position); |
478 | 0 | } |
479 | | |
480 | | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
481 | | |
482 | | static PyObject * |
483 | | reversed_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) |
484 | 0 | { |
485 | 0 | reversedobject *ro = _reversedobject_CAST(op); |
486 | 0 | Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(ro->index); |
487 | 0 | if (index != -1) { |
488 | 0 | return Py_BuildValue("O(O)n", Py_TYPE(ro), ro->seq, ro->index); |
489 | 0 | } |
490 | 0 | else { |
491 | 0 | return Py_BuildValue("O(())", Py_TYPE(ro)); |
492 | 0 | } |
493 | 0 | } |
494 | | |
495 | | static PyObject * |
496 | | reversed_setstate(PyObject *op, PyObject *state) |
497 | 0 | { |
498 | 0 | reversedobject *ro = _reversedobject_CAST(op); |
499 | 0 | Py_ssize_t index = PyLong_AsSsize_t(state); |
500 | 0 | if (index == -1 && PyErr_Occurred()) |
501 | 0 | return NULL; |
502 | 0 | Py_ssize_t ro_index = FT_ATOMIC_LOAD_SSIZE_RELAXED(ro->index); |
503 | | // if the iterator is exhausted we do not set the state |
504 | | // this is for backwards compatibility reasons. in practice this situation |
505 | | // will not occur, see gh-120971 |
506 | 0 | if (ro_index != -1) { |
507 | 0 | Py_ssize_t n = PySequence_Size(ro->seq); |
508 | 0 | if (n < 0) |
509 | 0 | return NULL; |
510 | 0 | if (index < -1) |
511 | 0 | index = -1; |
512 | 0 | else if (index > n-1) |
513 | 0 | index = n-1; |
514 | 0 | FT_ATOMIC_STORE_SSIZE_RELAXED(ro->index, index); |
515 | 0 | } |
516 | 0 | Py_RETURN_NONE; |
517 | 0 | } |
518 | | |
519 | | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
520 | | |
521 | | static PyMethodDef reversediter_methods[] = { |
522 | | {"__length_hint__", reversed_len, METH_NOARGS, length_hint_doc}, |
523 | | {"__reduce__", reversed_reduce, METH_NOARGS, reduce_doc}, |
524 | | {"__setstate__", reversed_setstate, METH_O, setstate_doc}, |
525 | | {NULL, NULL} /* sentinel */ |
526 | | }; |
527 | | |
528 | | PyTypeObject PyReversed_Type = { |
529 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
530 | | "reversed", /* tp_name */ |
531 | | sizeof(reversedobject), /* tp_basicsize */ |
532 | | 0, /* tp_itemsize */ |
533 | | /* methods */ |
534 | | reversed_dealloc, /* tp_dealloc */ |
535 | | 0, /* tp_vectorcall_offset */ |
536 | | 0, /* tp_getattr */ |
537 | | 0, /* tp_setattr */ |
538 | | 0, /* tp_as_async */ |
539 | | 0, /* tp_repr */ |
540 | | 0, /* tp_as_number */ |
541 | | 0, /* tp_as_sequence */ |
542 | | 0, /* tp_as_mapping */ |
543 | | 0, /* tp_hash */ |
544 | | 0, /* tp_call */ |
545 | | 0, /* tp_str */ |
546 | | PyObject_GenericGetAttr, /* tp_getattro */ |
547 | | 0, /* tp_setattro */ |
548 | | 0, /* tp_as_buffer */ |
549 | | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
550 | | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
551 | | reversed_new__doc__, /* tp_doc */ |
552 | | reversed_traverse, /* tp_traverse */ |
553 | | 0, /* tp_clear */ |
554 | | 0, /* tp_richcompare */ |
555 | | 0, /* tp_weaklistoffset */ |
556 | | PyObject_SelfIter, /* tp_iter */ |
557 | | reversed_next, /* tp_iternext */ |
558 | | reversediter_methods, /* tp_methods */ |
559 | | 0, /* tp_members */ |
560 | | 0, /* tp_getset */ |
561 | | 0, /* tp_base */ |
562 | | 0, /* tp_dict */ |
563 | | 0, /* tp_descr_get */ |
564 | | 0, /* tp_descr_set */ |
565 | | 0, /* tp_dictoffset */ |
566 | | 0, /* tp_init */ |
567 | | PyType_GenericAlloc, /* tp_alloc */ |
568 | | reversed_new, /* tp_new */ |
569 | | PyObject_GC_Del, /* tp_free */ |
570 | | .tp_vectorcall = reversed_vectorcall, |
571 | | }; |