/src/cpython/Objects/clinic/enumobject.c.h
Line | Count | Source |
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_helper(PyTypeObject *type, PyObject *const *args, |
31 | | Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) |
32 | 5.82k | { |
33 | 5.82k | PyObject *return_value = NULL; |
34 | 5.82k | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
35 | | |
36 | 5.82k | #define NUM_KEYWORDS 2 |
37 | 5.82k | static struct { |
38 | 5.82k | PyGC_Head _this_is_not_used; |
39 | 5.82k | PyObject_VAR_HEAD |
40 | 5.82k | Py_hash_t ob_hash; |
41 | 5.82k | PyObject *ob_item[NUM_KEYWORDS]; |
42 | 5.82k | } _kwtuple = { |
43 | 5.82k | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
44 | 5.82k | .ob_hash = -1, |
45 | 5.82k | .ob_item = { &_Py_ID(iterable), &_Py_ID(start), }, |
46 | 5.82k | }; |
47 | 5.82k | #undef NUM_KEYWORDS |
48 | 5.82k | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
49 | | |
50 | | #else // !Py_BUILD_CORE |
51 | | # define KWTUPLE NULL |
52 | | #endif // !Py_BUILD_CORE |
53 | | |
54 | 5.82k | static const char * const _keywords[] = {"iterable", "start", NULL}; |
55 | 5.82k | static _PyArg_Parser _parser = { |
56 | 5.82k | .keywords = _keywords, |
57 | 5.82k | .fname = "enumerate", |
58 | 5.82k | .kwtuple = KWTUPLE, |
59 | 5.82k | }; |
60 | 5.82k | #undef KWTUPLE |
61 | 5.82k | PyObject *argsbuf[2]; |
62 | 5.82k | PyObject * const *fastargs; |
63 | 5.82k | Py_ssize_t noptargs = nargs + nkw - 1; |
64 | 5.82k | PyObject *iterable; |
65 | 5.82k | PyObject *start = 0; |
66 | | |
67 | 5.82k | fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, |
68 | 5.82k | /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
69 | 5.82k | if (!fastargs) { |
70 | 0 | goto exit; |
71 | 0 | } |
72 | 5.82k | iterable = fastargs[0]; |
73 | 5.82k | if (!noptargs) { |
74 | 0 | goto skip_optional_pos; |
75 | 0 | } |
76 | 5.82k | start = fastargs[1]; |
77 | 5.82k | skip_optional_pos: |
78 | 5.82k | return_value = enum_new_impl(type, iterable, start); |
79 | | |
80 | 5.82k | exit: |
81 | 5.82k | return return_value; |
82 | 5.82k | } |
83 | | |
84 | | static PyObject * |
85 | | enum_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
86 | 0 | { |
87 | 0 | return enum_new_helper(type, _PyTuple_CAST(args)->ob_item, |
88 | 0 | PyTuple_GET_SIZE(args), |
89 | 0 | kwargs ? PyDict_GET_SIZE(kwargs) : 0, |
90 | 0 | kwargs, NULL); |
91 | 0 | } |
92 | | |
93 | | static PyObject * |
94 | | enum_vectorcall(PyObject *type, PyObject *const *args, |
95 | | size_t nargsf, PyObject *kwnames) |
96 | 12.0M | { |
97 | 12.0M | PyObject *return_value = NULL; |
98 | 12.0M | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
99 | 12.0M | PyObject *iterable; |
100 | 12.0M | PyObject *start = 0; |
101 | | |
102 | 12.0M | assert(Py_Is(_PyType_CAST(type), &PyEnum_Type)); |
103 | | /* Make sure the type object is immutable: the generated |
104 | | * vectorcall doesn't deal e.g. with users reassigning __init__. */ |
105 | 12.0M | assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); |
106 | 12.0M | if (kwnames != NULL || nargs < 1 || nargs > 2) { |
107 | 5.82k | return enum_new_helper(_PyType_CAST(type), args, nargs, |
108 | 5.82k | kwnames ? PyTuple_GET_SIZE(kwnames) : 0, |
109 | 5.82k | NULL, kwnames); |
110 | 5.82k | } |
111 | 12.0M | iterable = args[0]; |
112 | 12.0M | if (nargs < 2) { |
113 | 12.0M | goto skip_optional; |
114 | 12.0M | } |
115 | 3.75k | start = args[1]; |
116 | 12.0M | skip_optional: |
117 | 12.0M | return_value = enum_new_impl(_PyType_CAST(type), iterable, start); |
118 | | |
119 | 12.0M | return return_value; |
120 | 3.75k | } |
121 | | |
122 | | PyDoc_STRVAR(reversed_new__doc__, |
123 | | "reversed(object, /)\n" |
124 | | "--\n" |
125 | | "\n" |
126 | | "Return a reverse iterator over the values of the given sequence."); |
127 | | |
128 | | static PyObject * |
129 | | reversed_new_impl(PyTypeObject *type, PyObject *seq); |
130 | | |
131 | | static PyObject * |
132 | | reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
133 | 0 | { |
134 | 0 | PyObject *return_value = NULL; |
135 | 0 | PyTypeObject *base_tp = &PyReversed_Type; |
136 | 0 | PyObject *seq; |
137 | |
|
138 | 0 | if ((type == base_tp || type->tp_init == base_tp->tp_init) && |
139 | 0 | !_PyArg_NoKeywords("reversed", kwargs)) { |
140 | 0 | goto exit; |
141 | 0 | } |
142 | 0 | if (!_PyArg_CheckPositional("reversed", PyTuple_GET_SIZE(args), 1, 1)) { |
143 | 0 | goto exit; |
144 | 0 | } |
145 | 0 | seq = PyTuple_GET_ITEM(args, 0); |
146 | 0 | return_value = reversed_new_impl(type, seq); |
147 | |
|
148 | 0 | exit: |
149 | 0 | return return_value; |
150 | 0 | } |
151 | | |
152 | | static PyObject * |
153 | | reversed_vectorcall(PyObject *type, PyObject *const *args, |
154 | | size_t nargsf, PyObject *kwnames) |
155 | 41.7M | { |
156 | 41.7M | PyObject *return_value = NULL; |
157 | 41.7M | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
158 | 41.7M | PyObject *seq; |
159 | | |
160 | 41.7M | assert(Py_Is(_PyType_CAST(type), &PyReversed_Type)); |
161 | | /* Make sure the type object is immutable: the generated |
162 | | * vectorcall doesn't deal e.g. with users reassigning __init__. */ |
163 | 41.7M | assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); |
164 | 41.7M | if (!_PyArg_NoKwnames("reversed", kwnames)) { |
165 | 0 | goto exit; |
166 | 0 | } |
167 | 41.7M | if (!_PyArg_CheckPositional("reversed", nargs, 1, 1)) { |
168 | 0 | goto exit; |
169 | 0 | } |
170 | 41.7M | seq = args[0]; |
171 | 41.7M | return_value = reversed_new_impl(_PyType_CAST(type), seq); |
172 | | |
173 | 41.7M | exit: |
174 | 41.7M | return return_value; |
175 | 41.7M | } |
176 | | /*[clinic end generated code: output=d0c066334eeb3b17 input=a9049054013a1b77]*/ |