Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Objects/clinic/enumobject.c.h
12 views
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
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
{
32
PyObject *return_value = NULL;
33
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
34
35
#define NUM_KEYWORDS 2
36
static struct {
37
PyGC_Head _this_is_not_used;
38
PyObject_VAR_HEAD
39
PyObject *ob_item[NUM_KEYWORDS];
40
} _kwtuple = {
41
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
42
.ob_item = { &_Py_ID(iterable), &_Py_ID(start), },
43
};
44
#undef NUM_KEYWORDS
45
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
46
47
#else // !Py_BUILD_CORE
48
# define KWTUPLE NULL
49
#endif // !Py_BUILD_CORE
50
51
static const char * const _keywords[] = {"iterable", "start", NULL};
52
static _PyArg_Parser _parser = {
53
.keywords = _keywords,
54
.fname = "enumerate",
55
.kwtuple = KWTUPLE,
56
};
57
#undef KWTUPLE
58
PyObject *argsbuf[2];
59
PyObject * const *fastargs;
60
Py_ssize_t nargs = PyTuple_GET_SIZE(args);
61
Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
62
PyObject *iterable;
63
PyObject *start = 0;
64
65
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf);
66
if (!fastargs) {
67
goto exit;
68
}
69
iterable = fastargs[0];
70
if (!noptargs) {
71
goto skip_optional_pos;
72
}
73
start = fastargs[1];
74
skip_optional_pos:
75
return_value = enum_new_impl(type, iterable, start);
76
77
exit:
78
return return_value;
79
}
80
81
PyDoc_STRVAR(reversed_new__doc__,
82
"reversed(sequence, /)\n"
83
"--\n"
84
"\n"
85
"Return a reverse iterator over the values of the given sequence.");
86
87
static PyObject *
88
reversed_new_impl(PyTypeObject *type, PyObject *seq);
89
90
static PyObject *
91
reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
92
{
93
PyObject *return_value = NULL;
94
PyTypeObject *base_tp = &PyReversed_Type;
95
PyObject *seq;
96
97
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
98
!_PyArg_NoKeywords("reversed", kwargs)) {
99
goto exit;
100
}
101
if (!_PyArg_CheckPositional("reversed", PyTuple_GET_SIZE(args), 1, 1)) {
102
goto exit;
103
}
104
seq = PyTuple_GET_ITEM(args, 0);
105
return_value = reversed_new_impl(type, seq);
106
107
exit:
108
return return_value;
109
}
110
/*[clinic end generated code: output=aba0ddbeab1601e3 input=a9049054013a1b77]*/
111
112