/src/cpython/Objects/enumobject.c
Line | Count | Source (jump to first uncovered line) |
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 | 108M | #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 | 7.01M | { |
51 | 7.01M | enumobject *en; |
52 | | |
53 | 7.01M | en = (enumobject *)type->tp_alloc(type, 0); |
54 | 7.01M | if (en == NULL) |
55 | 0 | return NULL; |
56 | 7.01M | if (start != NULL) { |
57 | 3.57k | start = PyNumber_Index(start); |
58 | 3.57k | if (start == NULL) { |
59 | 0 | Py_DECREF(en); |
60 | 0 | return NULL; |
61 | 0 | } |
62 | 3.57k | assert(PyLong_Check(start)); |
63 | 3.57k | en->en_index = PyLong_AsSsize_t(start); |
64 | 3.57k | 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 | 3.57k | } else { |
69 | 3.57k | en->en_longindex = NULL; |
70 | 3.57k | Py_DECREF(start); |
71 | 3.57k | } |
72 | 7.01M | } else { |
73 | 7.01M | en->en_index = 0; |
74 | 7.01M | en->en_longindex = NULL; |
75 | 7.01M | } |
76 | 7.01M | en->en_sit = PyObject_GetIter(iterable); |
77 | 7.01M | if (en->en_sit == NULL) { |
78 | 0 | Py_DECREF(en); |
79 | 0 | return NULL; |
80 | 0 | } |
81 | 7.01M | en->en_result = PyTuple_Pack(2, Py_None, Py_None); |
82 | 7.01M | if (en->en_result == NULL) { |
83 | 0 | Py_DECREF(en); |
84 | 0 | return NULL; |
85 | 0 | } |
86 | 7.01M | en->one = _PyLong_GetOne(); /* borrowed reference */ |
87 | 7.01M | return (PyObject *)en; |
88 | 7.01M | } |
89 | | |
90 | | static int check_keyword(PyObject *kwnames, int index, |
91 | | const char *name) |
92 | 0 | { |
93 | 0 | PyObject *kw = PyTuple_GET_ITEM(kwnames, index); |
94 | 0 | 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 | 0 | return 1; |
100 | 0 | } |
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 | 7.01M | { |
107 | 7.01M | PyTypeObject *tp = _PyType_CAST(type); |
108 | 7.01M | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
109 | 7.01M | Py_ssize_t nkwargs = 0; |
110 | 7.01M | if (kwnames != NULL) { |
111 | 0 | nkwargs = PyTuple_GET_SIZE(kwnames); |
112 | 0 | } |
113 | | |
114 | | // Manually implement enumerate(iterable, start=...) |
115 | 7.01M | if (nargs + nkwargs == 2) { |
116 | 3.57k | if (nkwargs == 1) { |
117 | 0 | if (!check_keyword(kwnames, 0, "start")) { |
118 | 0 | return NULL; |
119 | 0 | } |
120 | 3.57k | } 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 | 3.57k | return enum_new_impl(tp, args[0], args[1]); |
135 | 3.57k | } |
136 | | |
137 | 7.01M | if (nargs + nkwargs == 1) { |
138 | 7.01M | if (nkwargs == 1 && !check_keyword(kwnames, 0, "iterable")) { |
139 | 0 | return NULL; |
140 | 0 | } |
141 | 7.01M | return enum_new_impl(tp, args[0], NULL); |
142 | 7.01M | } |
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 (%d given)", nargs + nkwargs); |
152 | 0 | return NULL; |
153 | 0 | } |
154 | | |
155 | | static void |
156 | | enum_dealloc(PyObject *op) |
157 | 7.01M | { |
158 | 7.01M | enumobject *en = _enumobject_CAST(op); |
159 | 7.01M | PyObject_GC_UnTrack(en); |
160 | 7.01M | Py_XDECREF(en->en_sit); |
161 | 7.01M | Py_XDECREF(en->en_result); |
162 | 7.01M | Py_XDECREF(en->en_longindex); |
163 | 7.01M | Py_TYPE(en)->tp_free(en); |
164 | 7.01M | } |
165 | | |
166 | | static int |
167 | | enum_traverse(PyObject *op, visitproc visit, void *arg) |
168 | 1.50k | { |
169 | 1.50k | enumobject *en = _enumobject_CAST(op); |
170 | 1.50k | Py_VISIT(en->en_sit); |
171 | 1.50k | Py_VISIT(en->en_result); |
172 | 1.50k | Py_VISIT(en->en_longindex); |
173 | 1.50k | return 0; |
174 | 1.50k | } |
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 | PyObject *next_index = en->en_longindex; |
182 | 0 | if (next_index == NULL) { |
183 | 0 | next_index = PyLong_FromSsize_t(PY_SSIZE_T_MAX); |
184 | 0 | if (next_index == NULL) { |
185 | 0 | return NULL; |
186 | 0 | } |
187 | 0 | } |
188 | 0 | assert(next_index != NULL); |
189 | 0 | PyObject *stepped_up = PyNumber_Add(next_index, en->one); |
190 | 0 | if (stepped_up == NULL) { |
191 | 0 | return NULL; |
192 | 0 | } |
193 | 0 | en->en_longindex = stepped_up; |
194 | 0 | return next_index; |
195 | 0 | } |
196 | | |
197 | | static PyObject * |
198 | | enum_next_long(enumobject *en, PyObject* next_item) |
199 | 0 | { |
200 | 0 | PyObject *result = en->en_result; |
201 | 0 | PyObject *next_index; |
202 | 0 | PyObject *old_index; |
203 | 0 | PyObject *old_item; |
204 | | |
205 | |
|
206 | 0 | Py_BEGIN_CRITICAL_SECTION(en); |
207 | 0 | next_index = increment_longindex_lock_held(en); |
208 | 0 | Py_END_CRITICAL_SECTION(); |
209 | 0 | if (next_index == NULL) { |
210 | 0 | Py_DECREF(next_item); |
211 | 0 | return NULL; |
212 | 0 | } |
213 | | |
214 | 0 | if (_PyObject_IsUniquelyReferenced(result)) { |
215 | 0 | Py_INCREF(result); |
216 | 0 | old_index = PyTuple_GET_ITEM(result, 0); |
217 | 0 | old_item = PyTuple_GET_ITEM(result, 1); |
218 | 0 | PyTuple_SET_ITEM(result, 0, next_index); |
219 | 0 | PyTuple_SET_ITEM(result, 1, next_item); |
220 | 0 | Py_DECREF(old_index); |
221 | 0 | Py_DECREF(old_item); |
222 | | // bpo-42536: The GC may have untracked this result tuple. Since we're |
223 | | // recycling it, make sure it's tracked again: |
224 | 0 | _PyTuple_Recycle(result); |
225 | 0 | return result; |
226 | 0 | } |
227 | 0 | result = PyTuple_New(2); |
228 | 0 | if (result == NULL) { |
229 | 0 | Py_DECREF(next_index); |
230 | 0 | Py_DECREF(next_item); |
231 | 0 | return NULL; |
232 | 0 | } |
233 | 0 | PyTuple_SET_ITEM(result, 0, next_index); |
234 | 0 | PyTuple_SET_ITEM(result, 1, next_item); |
235 | 0 | return result; |
236 | 0 | } |
237 | | |
238 | | static PyObject * |
239 | | enum_next(PyObject *op) |
240 | 101M | { |
241 | 101M | enumobject *en = _enumobject_CAST(op); |
242 | 101M | PyObject *next_index; |
243 | 101M | PyObject *next_item; |
244 | 101M | PyObject *result = en->en_result; |
245 | 101M | PyObject *it = en->en_sit; |
246 | 101M | PyObject *old_index; |
247 | 101M | PyObject *old_item; |
248 | | |
249 | 101M | next_item = (*Py_TYPE(it)->tp_iternext)(it); |
250 | 101M | if (next_item == NULL) |
251 | 7.01M | return NULL; |
252 | | |
253 | 94.1M | Py_ssize_t en_index = FT_ATOMIC_LOAD_SSIZE_RELAXED(en->en_index); |
254 | 94.1M | if (en_index == PY_SSIZE_T_MAX) |
255 | 0 | return enum_next_long(en, next_item); |
256 | | |
257 | 94.1M | next_index = PyLong_FromSsize_t(en_index); |
258 | 94.1M | if (next_index == NULL) { |
259 | 0 | Py_DECREF(next_item); |
260 | 0 | return NULL; |
261 | 0 | } |
262 | 94.1M | FT_ATOMIC_STORE_SSIZE_RELAXED(en->en_index, en_index + 1); |
263 | | |
264 | 94.1M | if (_PyObject_IsUniquelyReferenced(result)) { |
265 | 94.1M | Py_INCREF(result); |
266 | 94.1M | old_index = PyTuple_GET_ITEM(result, 0); |
267 | 94.1M | old_item = PyTuple_GET_ITEM(result, 1); |
268 | 94.1M | PyTuple_SET_ITEM(result, 0, next_index); |
269 | 94.1M | PyTuple_SET_ITEM(result, 1, next_item); |
270 | 94.1M | Py_DECREF(old_index); |
271 | 94.1M | Py_DECREF(old_item); |
272 | | // bpo-42536: The GC may have untracked this result tuple. Since we're |
273 | | // recycling it, make sure it's tracked again: |
274 | 94.1M | _PyTuple_Recycle(result); |
275 | 94.1M | return result; |
276 | 94.1M | } |
277 | 0 | result = PyTuple_New(2); |
278 | 0 | if (result == NULL) { |
279 | 0 | Py_DECREF(next_index); |
280 | 0 | Py_DECREF(next_item); |
281 | 0 | return NULL; |
282 | 0 | } |
283 | 0 | PyTuple_SET_ITEM(result, 0, next_index); |
284 | 0 | PyTuple_SET_ITEM(result, 1, next_item); |
285 | 0 | return result; |
286 | 0 | } |
287 | | |
288 | | static PyObject * |
289 | | enum_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) |
290 | 0 | { |
291 | 0 | enumobject *en = _enumobject_CAST(op); |
292 | 0 | PyObject *result; |
293 | 0 | Py_BEGIN_CRITICAL_SECTION(en); |
294 | 0 | if (en->en_longindex != NULL) |
295 | 0 | result = Py_BuildValue("O(OO)", Py_TYPE(en), en->en_sit, en->en_longindex); |
296 | 0 | else |
297 | 0 | result = Py_BuildValue("O(On)", Py_TYPE(en), en->en_sit, en->en_index); |
298 | 0 | Py_END_CRITICAL_SECTION(); |
299 | 0 | return result; |
300 | 0 | } |
301 | | |
302 | | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
303 | | |
304 | | static PyMethodDef enum_methods[] = { |
305 | | {"__reduce__", enum_reduce, METH_NOARGS, reduce_doc}, |
306 | | {"__class_getitem__", Py_GenericAlias, |
307 | | METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, |
308 | | {NULL, NULL} /* sentinel */ |
309 | | }; |
310 | | |
311 | | PyTypeObject PyEnum_Type = { |
312 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
313 | | "enumerate", /* tp_name */ |
314 | | sizeof(enumobject), /* tp_basicsize */ |
315 | | 0, /* tp_itemsize */ |
316 | | /* methods */ |
317 | | enum_dealloc, /* tp_dealloc */ |
318 | | 0, /* tp_vectorcall_offset */ |
319 | | 0, /* tp_getattr */ |
320 | | 0, /* tp_setattr */ |
321 | | 0, /* tp_as_async */ |
322 | | 0, /* tp_repr */ |
323 | | 0, /* tp_as_number */ |
324 | | 0, /* tp_as_sequence */ |
325 | | 0, /* tp_as_mapping */ |
326 | | 0, /* tp_hash */ |
327 | | 0, /* tp_call */ |
328 | | 0, /* tp_str */ |
329 | | PyObject_GenericGetAttr, /* tp_getattro */ |
330 | | 0, /* tp_setattro */ |
331 | | 0, /* tp_as_buffer */ |
332 | | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
333 | | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
334 | | enum_new__doc__, /* tp_doc */ |
335 | | enum_traverse, /* tp_traverse */ |
336 | | 0, /* tp_clear */ |
337 | | 0, /* tp_richcompare */ |
338 | | 0, /* tp_weaklistoffset */ |
339 | | PyObject_SelfIter, /* tp_iter */ |
340 | | enum_next, /* tp_iternext */ |
341 | | enum_methods, /* tp_methods */ |
342 | | 0, /* tp_members */ |
343 | | 0, /* tp_getset */ |
344 | | 0, /* tp_base */ |
345 | | 0, /* tp_dict */ |
346 | | 0, /* tp_descr_get */ |
347 | | 0, /* tp_descr_set */ |
348 | | 0, /* tp_dictoffset */ |
349 | | 0, /* tp_init */ |
350 | | PyType_GenericAlloc, /* tp_alloc */ |
351 | | enum_new, /* tp_new */ |
352 | | PyObject_GC_Del, /* tp_free */ |
353 | | .tp_vectorcall = enumerate_vectorcall |
354 | | }; |
355 | | |
356 | | /* Reversed Object ***************************************************************/ |
357 | | |
358 | | typedef struct { |
359 | | PyObject_HEAD |
360 | | Py_ssize_t index; |
361 | | PyObject* seq; |
362 | | } reversedobject; |
363 | | |
364 | 0 | #define _reversedobject_CAST(op) ((reversedobject *)(op)) |
365 | | |
366 | | /*[clinic input] |
367 | | @classmethod |
368 | | reversed.__new__ as reversed_new |
369 | | |
370 | | sequence as seq: object |
371 | | / |
372 | | |
373 | | Return a reverse iterator over the values of the given sequence. |
374 | | [clinic start generated code]*/ |
375 | | |
376 | | static PyObject * |
377 | | reversed_new_impl(PyTypeObject *type, PyObject *seq) |
378 | | /*[clinic end generated code: output=f7854cc1df26f570 input=aeb720361e5e3f1d]*/ |
379 | 41.4M | { |
380 | 41.4M | Py_ssize_t n; |
381 | 41.4M | PyObject *reversed_meth; |
382 | 41.4M | reversedobject *ro; |
383 | | |
384 | 41.4M | reversed_meth = _PyObject_LookupSpecial(seq, &_Py_ID(__reversed__)); |
385 | 41.4M | if (reversed_meth == Py_None) { |
386 | 0 | Py_DECREF(reversed_meth); |
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 | 41.4M | if (reversed_meth != NULL) { |
393 | 41.4M | PyObject *res = _PyObject_CallNoArgs(reversed_meth); |
394 | 41.4M | Py_DECREF(reversed_meth); |
395 | 41.4M | return res; |
396 | 41.4M | } |
397 | 0 | else if (PyErr_Occurred()) |
398 | 0 | return NULL; |
399 | | |
400 | 0 | if (!PySequence_Check(seq)) { |
401 | 0 | PyErr_Format(PyExc_TypeError, |
402 | 0 | "'%.200s' object is not reversible", |
403 | 0 | Py_TYPE(seq)->tp_name); |
404 | 0 | return NULL; |
405 | 0 | } |
406 | | |
407 | 0 | n = PySequence_Size(seq); |
408 | 0 | if (n == -1) |
409 | 0 | return NULL; |
410 | | |
411 | 0 | ro = (reversedobject *)type->tp_alloc(type, 0); |
412 | 0 | if (ro == NULL) |
413 | 0 | return NULL; |
414 | | |
415 | 0 | ro->index = n-1; |
416 | 0 | ro->seq = Py_NewRef(seq); |
417 | 0 | return (PyObject *)ro; |
418 | 0 | } |
419 | | |
420 | | static PyObject * |
421 | | reversed_vectorcall(PyObject *type, PyObject * const*args, |
422 | | size_t nargsf, PyObject *kwnames) |
423 | 41.4M | { |
424 | 41.4M | if (!_PyArg_NoKwnames("reversed", kwnames)) { |
425 | 0 | return NULL; |
426 | 0 | } |
427 | | |
428 | 41.4M | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
429 | 41.4M | if (!_PyArg_CheckPositional("reversed", nargs, 1, 1)) { |
430 | 0 | return NULL; |
431 | 0 | } |
432 | | |
433 | 41.4M | return reversed_new_impl(_PyType_CAST(type), args[0]); |
434 | 41.4M | } |
435 | | |
436 | | static void |
437 | | reversed_dealloc(PyObject *op) |
438 | 0 | { |
439 | 0 | reversedobject *ro = _reversedobject_CAST(op); |
440 | 0 | PyObject_GC_UnTrack(ro); |
441 | 0 | Py_XDECREF(ro->seq); |
442 | 0 | Py_TYPE(ro)->tp_free(ro); |
443 | 0 | } |
444 | | |
445 | | static int |
446 | | reversed_traverse(PyObject *op, visitproc visit, void *arg) |
447 | 0 | { |
448 | 0 | reversedobject *ro = _reversedobject_CAST(op); |
449 | 0 | Py_VISIT(ro->seq); |
450 | 0 | return 0; |
451 | 0 | } |
452 | | |
453 | | static PyObject * |
454 | | reversed_next(PyObject *op) |
455 | 0 | { |
456 | 0 | reversedobject *ro = _reversedobject_CAST(op); |
457 | 0 | PyObject *item; |
458 | 0 | Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(ro->index); |
459 | |
|
460 | 0 | if (index >= 0) { |
461 | 0 | item = PySequence_GetItem(ro->seq, index); |
462 | 0 | if (item != NULL) { |
463 | 0 | FT_ATOMIC_STORE_SSIZE_RELAXED(ro->index, index - 1); |
464 | 0 | return item; |
465 | 0 | } |
466 | 0 | if (PyErr_ExceptionMatches(PyExc_IndexError) || |
467 | 0 | PyErr_ExceptionMatches(PyExc_StopIteration)) |
468 | 0 | PyErr_Clear(); |
469 | 0 | } |
470 | 0 | FT_ATOMIC_STORE_SSIZE_RELAXED(ro->index, -1); |
471 | 0 | #ifndef Py_GIL_DISABLED |
472 | 0 | Py_CLEAR(ro->seq); |
473 | 0 | #endif |
474 | 0 | return NULL; |
475 | 0 | } |
476 | | |
477 | | static PyObject * |
478 | | reversed_len(PyObject *op, PyObject *Py_UNUSED(ignored)) |
479 | 0 | { |
480 | 0 | reversedobject *ro = _reversedobject_CAST(op); |
481 | 0 | Py_ssize_t position, seqsize; |
482 | 0 | Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(ro->index); |
483 | |
|
484 | 0 | if (index == -1) |
485 | 0 | return PyLong_FromLong(0); |
486 | 0 | assert(ro->seq != NULL); |
487 | 0 | seqsize = PySequence_Size(ro->seq); |
488 | 0 | if (seqsize == -1) |
489 | 0 | return NULL; |
490 | 0 | position = index + 1; |
491 | 0 | return PyLong_FromSsize_t((seqsize < position) ? 0 : position); |
492 | 0 | } |
493 | | |
494 | | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
495 | | |
496 | | static PyObject * |
497 | | reversed_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) |
498 | 0 | { |
499 | 0 | reversedobject *ro = _reversedobject_CAST(op); |
500 | 0 | Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(ro->index); |
501 | 0 | if (index != -1) { |
502 | 0 | return Py_BuildValue("O(O)n", Py_TYPE(ro), ro->seq, ro->index); |
503 | 0 | } |
504 | 0 | else { |
505 | 0 | return Py_BuildValue("O(())", Py_TYPE(ro)); |
506 | 0 | } |
507 | 0 | } |
508 | | |
509 | | static PyObject * |
510 | | reversed_setstate(PyObject *op, PyObject *state) |
511 | 0 | { |
512 | 0 | reversedobject *ro = _reversedobject_CAST(op); |
513 | 0 | Py_ssize_t index = PyLong_AsSsize_t(state); |
514 | 0 | if (index == -1 && PyErr_Occurred()) |
515 | 0 | return NULL; |
516 | 0 | Py_ssize_t ro_index = FT_ATOMIC_LOAD_SSIZE_RELAXED(ro->index); |
517 | | // if the iterator is exhausted we do not set the state |
518 | | // this is for backwards compatibility reasons. in practice this situation |
519 | | // will not occur, see gh-120971 |
520 | 0 | if (ro_index != -1) { |
521 | 0 | Py_ssize_t n = PySequence_Size(ro->seq); |
522 | 0 | if (n < 0) |
523 | 0 | return NULL; |
524 | 0 | if (index < -1) |
525 | 0 | index = -1; |
526 | 0 | else if (index > n-1) |
527 | 0 | index = n-1; |
528 | 0 | FT_ATOMIC_STORE_SSIZE_RELAXED(ro->index, index); |
529 | 0 | } |
530 | 0 | Py_RETURN_NONE; |
531 | 0 | } |
532 | | |
533 | | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
534 | | |
535 | | static PyMethodDef reversediter_methods[] = { |
536 | | {"__length_hint__", reversed_len, METH_NOARGS, length_hint_doc}, |
537 | | {"__reduce__", reversed_reduce, METH_NOARGS, reduce_doc}, |
538 | | {"__setstate__", reversed_setstate, METH_O, setstate_doc}, |
539 | | {NULL, NULL} /* sentinel */ |
540 | | }; |
541 | | |
542 | | PyTypeObject PyReversed_Type = { |
543 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
544 | | "reversed", /* tp_name */ |
545 | | sizeof(reversedobject), /* tp_basicsize */ |
546 | | 0, /* tp_itemsize */ |
547 | | /* methods */ |
548 | | reversed_dealloc, /* tp_dealloc */ |
549 | | 0, /* tp_vectorcall_offset */ |
550 | | 0, /* tp_getattr */ |
551 | | 0, /* tp_setattr */ |
552 | | 0, /* tp_as_async */ |
553 | | 0, /* tp_repr */ |
554 | | 0, /* tp_as_number */ |
555 | | 0, /* tp_as_sequence */ |
556 | | 0, /* tp_as_mapping */ |
557 | | 0, /* tp_hash */ |
558 | | 0, /* tp_call */ |
559 | | 0, /* tp_str */ |
560 | | PyObject_GenericGetAttr, /* tp_getattro */ |
561 | | 0, /* tp_setattro */ |
562 | | 0, /* tp_as_buffer */ |
563 | | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
564 | | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
565 | | reversed_new__doc__, /* tp_doc */ |
566 | | reversed_traverse, /* tp_traverse */ |
567 | | 0, /* tp_clear */ |
568 | | 0, /* tp_richcompare */ |
569 | | 0, /* tp_weaklistoffset */ |
570 | | PyObject_SelfIter, /* tp_iter */ |
571 | | reversed_next, /* tp_iternext */ |
572 | | reversediter_methods, /* tp_methods */ |
573 | | 0, /* tp_members */ |
574 | | 0, /* tp_getset */ |
575 | | 0, /* tp_base */ |
576 | | 0, /* tp_dict */ |
577 | | 0, /* tp_descr_get */ |
578 | | 0, /* tp_descr_set */ |
579 | | 0, /* tp_dictoffset */ |
580 | | 0, /* tp_init */ |
581 | | PyType_GenericAlloc, /* tp_alloc */ |
582 | | reversed_new, /* tp_new */ |
583 | | PyObject_GC_Del, /* tp_free */ |
584 | | .tp_vectorcall = reversed_vectorcall, |
585 | | }; |