/src/cpython/Objects/clinic/enumobject.c.h
Line | Count | Source (jump to first uncovered line) |
1 | | /*[clinic input] |
2 | | preserve |
3 | | [clinic start generated code]*/ |
4 | | |
5 | | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
6 | | # include "pycore_gc.h" // PyGC_Head |
7 | | # include "pycore_runtime.h" // _Py_ID() |
8 | | #endif |
9 | | #include "pycore_modsupport.h" // _PyArg_UnpackKeywords() |
10 | | |
11 | | PyDoc_STRVAR(enum_new__doc__, |
12 | | "enumerate(iterable, start=0)\n" |
13 | | "--\n" |
14 | | "\n" |
15 | | "Return an enumerate object.\n" |
16 | | "\n" |
17 | | " iterable\n" |
18 | | " an object supporting iteration\n" |
19 | | "\n" |
20 | | "The enumerate object yields pairs containing a count (from start, which\n" |
21 | | "defaults to zero) and a value yielded by the iterable argument.\n" |
22 | | "\n" |
23 | | "enumerate is useful for obtaining an indexed list:\n" |
24 | | " (0, seq[0]), (1, seq[1]), (2, seq[2]), ..."); |
25 | | |
26 | | static PyObject * |
27 | | enum_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *start); |
28 | | |
29 | | static PyObject * |
30 | | enum_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
31 | 0 | { |
32 | 0 | PyObject *return_value = NULL; |
33 | 0 | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
34 | |
|
35 | 0 | #define NUM_KEYWORDS 2 |
36 | 0 | static struct { |
37 | 0 | PyGC_Head _this_is_not_used; |
38 | 0 | PyObject_VAR_HEAD |
39 | 0 | Py_hash_t ob_hash; |
40 | 0 | PyObject *ob_item[NUM_KEYWORDS]; |
41 | 0 | } _kwtuple = { |
42 | 0 | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
43 | 0 | .ob_hash = -1, |
44 | 0 | .ob_item = { &_Py_ID(iterable), &_Py_ID(start), }, |
45 | 0 | }; |
46 | 0 | #undef NUM_KEYWORDS |
47 | 0 | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
48 | |
|
49 | | #else // !Py_BUILD_CORE |
50 | | # define KWTUPLE NULL |
51 | | #endif // !Py_BUILD_CORE |
52 | |
|
53 | 0 | static const char * const _keywords[] = {"iterable", "start", NULL}; |
54 | 0 | static _PyArg_Parser _parser = { |
55 | 0 | .keywords = _keywords, |
56 | 0 | .fname = "enumerate", |
57 | 0 | .kwtuple = KWTUPLE, |
58 | 0 | }; |
59 | 0 | #undef KWTUPLE |
60 | 0 | PyObject *argsbuf[2]; |
61 | 0 | PyObject * const *fastargs; |
62 | 0 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
63 | 0 | Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; |
64 | 0 | PyObject *iterable; |
65 | 0 | PyObject *start = 0; |
66 | |
|
67 | 0 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, |
68 | 0 | /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
69 | 0 | if (!fastargs) { |
70 | 0 | goto exit; |
71 | 0 | } |
72 | 0 | iterable = fastargs[0]; |
73 | 0 | if (!noptargs) { |
74 | 0 | goto skip_optional_pos; |
75 | 0 | } |
76 | 0 | start = fastargs[1]; |
77 | 0 | skip_optional_pos: |
78 | 0 | return_value = enum_new_impl(type, iterable, start); |
79 | |
|
80 | 0 | exit: |
81 | 0 | return return_value; |
82 | 0 | } |
83 | | |
84 | | PyDoc_STRVAR(reversed_new__doc__, |
85 | | "reversed(sequence, /)\n" |
86 | | "--\n" |
87 | | "\n" |
88 | | "Return a reverse iterator over the values of the given sequence."); |
89 | | |
90 | | static PyObject * |
91 | | reversed_new_impl(PyTypeObject *type, PyObject *seq); |
92 | | |
93 | | static PyObject * |
94 | | reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
95 | 0 | { |
96 | 0 | PyObject *return_value = NULL; |
97 | 0 | PyTypeObject *base_tp = &PyReversed_Type; |
98 | 0 | PyObject *seq; |
99 | |
|
100 | 0 | if ((type == base_tp || type->tp_init == base_tp->tp_init) && |
101 | 0 | !_PyArg_NoKeywords("reversed", kwargs)) { |
102 | 0 | goto exit; |
103 | 0 | } |
104 | 0 | if (!_PyArg_CheckPositional("reversed", PyTuple_GET_SIZE(args), 1, 1)) { |
105 | 0 | goto exit; |
106 | 0 | } |
107 | 0 | seq = PyTuple_GET_ITEM(args, 0); |
108 | 0 | return_value = reversed_new_impl(type, seq); |
109 | |
|
110 | 0 | exit: |
111 | 0 | return return_value; |
112 | 0 | } |
113 | | /*[clinic end generated code: output=3300305b351674d3 input=a9049054013a1b77]*/ |