Coverage Report

Created: 2026-11-30 07:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython3/Objects/clinic/funcobject.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_critical_section.h"// Py_BEGIN_CRITICAL_SECTION()
10
#include "pycore_modsupport.h"    // _PyArg_UnpackKeywords()
11
12
PyDoc_STRVAR(function___annotate____doc__,
13
"Get the code object for a function.");
14
15
static PyObject *
16
function___annotate___get_impl(PyFunctionObject *self);
17
18
static PyObject *
19
function___annotate___get(PyObject *self, void *Py_UNUSED(context))
20
31
{
21
31
    PyObject *return_value = NULL;
22
23
31
    Py_BEGIN_CRITICAL_SECTION(self);
24
31
    return_value = function___annotate___get_impl((PyFunctionObject *)self);
25
31
    Py_END_CRITICAL_SECTION();
26
27
31
    return return_value;
28
31
}
29
30
static int
31
function___annotate___set_impl(PyFunctionObject *self, PyObject *value);
32
33
static int
34
function___annotate___set(PyObject *self, PyObject *arg, void *Py_UNUSED(context))
35
21
{
36
21
    int return_value = -1;
37
21
    PyObject *value;
38
39
21
    if (arg == NULL) {
40
0
        PyErr_Format(PyExc_AttributeError,
41
0
                     "attribute '__annotate__' of '%.100s' objects cannot be deleted",
42
0
                     Py_TYPE(self)->tp_name);
43
0
        return -1;
44
0
    }
45
21
    value = arg;
46
21
    Py_BEGIN_CRITICAL_SECTION(self);
47
21
    return_value = function___annotate___set_impl((PyFunctionObject *)self, value);
48
21
    Py_END_CRITICAL_SECTION();
49
50
21
    return return_value;
51
21
}
52
53
PyDoc_STRVAR(function___annotations____doc__,
54
"Dict of annotations in a function object.");
55
56
static PyObject *
57
function___annotations___get_impl(PyFunctionObject *self);
58
59
static PyObject *
60
function___annotations___get(PyObject *self, void *Py_UNUSED(context))
61
0
{
62
0
    PyObject *return_value = NULL;
63
64
0
    Py_BEGIN_CRITICAL_SECTION(self);
65
0
    return_value = function___annotations___get_impl((PyFunctionObject *)self);
66
0
    Py_END_CRITICAL_SECTION();
67
68
0
    return return_value;
69
0
}
70
71
static int
72
function___annotations___set_impl(PyFunctionObject *self, PyObject *value);
73
74
static int
75
function___annotations___set(PyObject *self, PyObject *arg, void *Py_UNUSED(context))
76
0
{
77
0
    int return_value = -1;
78
0
    PyObject *value = NULL;
79
80
0
    if (arg != NULL) {
81
0
        value = arg;
82
0
    }
83
0
    Py_BEGIN_CRITICAL_SECTION(self);
84
0
    return_value = function___annotations___set_impl((PyFunctionObject *)self, value);
85
0
    Py_END_CRITICAL_SECTION();
86
87
0
    return return_value;
88
0
}
89
90
PyDoc_STRVAR(function___type_params____doc__,
91
"Get the declared type parameters for a function.");
92
93
static PyObject *
94
function___type_params___get_impl(PyFunctionObject *self);
95
96
static PyObject *
97
function___type_params___get(PyObject *self, void *Py_UNUSED(context))
98
31
{
99
31
    PyObject *return_value = NULL;
100
101
31
    Py_BEGIN_CRITICAL_SECTION(self);
102
31
    return_value = function___type_params___get_impl((PyFunctionObject *)self);
103
31
    Py_END_CRITICAL_SECTION();
104
105
31
    return return_value;
106
31
}
107
108
static int
109
function___type_params___set_impl(PyFunctionObject *self, PyObject *value);
110
111
static int
112
function___type_params___set(PyObject *self, PyObject *arg, void *Py_UNUSED(context))
113
21
{
114
21
    int return_value = -1;
115
21
    PyObject *value;
116
117
21
    if (arg == NULL) {
118
0
        PyErr_Format(PyExc_AttributeError,
119
0
                     "attribute '__type_params__' of '%.100s' objects cannot be deleted",
120
0
                     Py_TYPE(self)->tp_name);
121
0
        return -1;
122
0
    }
123
21
    if (!PyTuple_Check(arg)) {
124
0
        PyErr_Format(PyExc_TypeError, "attribute '__type_params__' must be tuple, not %T", arg);
125
0
        goto exit;
126
0
    }
127
21
    value = arg;
128
21
    Py_BEGIN_CRITICAL_SECTION(self);
129
21
    return_value = function___type_params___set_impl((PyFunctionObject *)self, value);
130
21
    Py_END_CRITICAL_SECTION();
131
132
21
exit:
133
21
    return return_value;
134
21
}
135
136
PyDoc_STRVAR(func_new__doc__,
137
"function(code, globals, name=None, argdefs=None, closure=None,\n"
138
"         kwdefaults=None)\n"
139
"--\n"
140
"\n"
141
"Create a function object.\n"
142
"\n"
143
"  code\n"
144
"    a code object\n"
145
"  globals\n"
146
"    the globals dictionary\n"
147
"  name\n"
148
"    a string that overrides the name from the code object\n"
149
"  argdefs\n"
150
"    a tuple that specifies the default argument values\n"
151
"  closure\n"
152
"    a tuple that supplies the bindings for free variables\n"
153
"  kwdefaults\n"
154
"    a dictionary that specifies the default keyword argument values");
155
156
static PyObject *
157
func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
158
              PyObject *name, PyObject *defaults, PyObject *closure,
159
              PyObject *kwdefaults);
160
161
static PyObject *
162
func_new_helper(PyTypeObject *type, PyObject *const *args,
163
    Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames)
164
0
{
165
0
    PyObject *return_value = NULL;
166
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
167
168
0
    #define NUM_KEYWORDS 6
169
0
    static struct {
170
0
        PyGC_Head _this_is_not_used;
171
0
        PyObject_VAR_HEAD
172
0
        Py_hash_t ob_hash;
173
0
        PyObject *ob_item[NUM_KEYWORDS];
174
0
    } _kwtuple = {
175
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
176
0
        .ob_hash = -1,
177
0
        .ob_item = { &_Py_ID(code), &_Py_ID(globals), &_Py_ID(name), &_Py_ID(argdefs), &_Py_ID(closure), &_Py_ID(kwdefaults), },
178
0
    };
179
0
    #undef NUM_KEYWORDS
180
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
181
182
    #else  // !Py_BUILD_CORE
183
    #  define KWTUPLE NULL
184
    #endif  // !Py_BUILD_CORE
185
186
0
    static const char * const _keywords[] = {"code", "globals", "name", "argdefs", "closure", "kwdefaults", NULL};
187
0
    static _PyArg_Parser _parser = {
188
0
        .keywords = _keywords,
189
0
        .fname = "function",
190
0
        .kwtuple = KWTUPLE,
191
0
    };
192
0
    #undef KWTUPLE
193
0
    PyObject *argsbuf[6];
194
0
    PyObject * const *fastargs;
195
0
    Py_ssize_t noptargs = nargs + nkw - 2;
196
0
    PyCodeObject *code;
197
0
    PyObject *globals;
198
0
    PyObject *name = Py_None;
199
0
    PyObject *defaults = Py_None;
200
0
    PyObject *closure = Py_None;
201
0
    PyObject *kwdefaults = Py_None;
202
203
0
    fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser,
204
0
            /*minpos*/ 2, /*maxpos*/ 6, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
205
0
    if (!fastargs) {
206
0
        goto exit;
207
0
    }
208
0
    if (!PyObject_TypeCheck(fastargs[0], &PyCode_Type)) {
209
0
        _PyArg_BadArgument("function", "argument 'code'", (&PyCode_Type)->tp_name, fastargs[0]);
210
0
        goto exit;
211
0
    }
212
0
    code = (PyCodeObject *)fastargs[0];
213
0
    if (!PyDict_Check(fastargs[1])) {
214
0
        _PyArg_BadArgument("function", "argument 'globals'", "dict", fastargs[1]);
215
0
        goto exit;
216
0
    }
217
0
    globals = fastargs[1];
218
0
    if (!noptargs) {
219
0
        goto skip_optional_pos;
220
0
    }
221
0
    if (fastargs[2]) {
222
0
        name = fastargs[2];
223
0
        if (!--noptargs) {
224
0
            goto skip_optional_pos;
225
0
        }
226
0
    }
227
0
    if (fastargs[3]) {
228
0
        defaults = fastargs[3];
229
0
        if (!--noptargs) {
230
0
            goto skip_optional_pos;
231
0
        }
232
0
    }
233
0
    if (fastargs[4]) {
234
0
        closure = fastargs[4];
235
0
        if (!--noptargs) {
236
0
            goto skip_optional_pos;
237
0
        }
238
0
    }
239
0
    kwdefaults = fastargs[5];
240
0
skip_optional_pos:
241
0
    return_value = func_new_impl(type, code, globals, name, defaults, closure, kwdefaults);
242
243
0
exit:
244
0
    return return_value;
245
0
}
246
247
static PyObject *
248
func_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
249
0
{
250
0
    return func_new_helper(type, _PyTuple_CAST(args)->ob_item,
251
0
        PyTuple_GET_SIZE(args),
252
0
        kwargs ? PyDict_GET_SIZE(kwargs) : 0,
253
0
        kwargs, NULL);
254
0
}
255
256
static PyObject *
257
func_vectorcall(PyObject *type, PyObject *const *args,
258
    size_t nargsf, PyObject *kwnames)
259
0
{
260
0
    PyObject *return_value = NULL;
261
0
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
262
0
    PyCodeObject *code;
263
0
    PyObject *globals;
264
0
    PyObject *name = Py_None;
265
0
    PyObject *defaults = Py_None;
266
0
    PyObject *closure = Py_None;
267
0
    PyObject *kwdefaults = Py_None;
268
269
0
    assert(_PyType_CAST(type)->tp_new == func_new);
270
    /* Make sure the type object is immutable: the generated
271
     * vectorcall doesn't deal e.g. with users reassigning __init__. */
272
0
    assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
273
0
    if (kwnames != NULL || nargs < 2 || nargs > 6) {
274
0
        return func_new_helper(_PyType_CAST(type), args, nargs,
275
0
            kwnames ? PyTuple_GET_SIZE(kwnames) : 0,
276
0
            NULL, kwnames);
277
0
    }
278
0
    if (!PyObject_TypeCheck(args[0], &PyCode_Type)) {
279
0
        _PyArg_BadArgument("function", "argument 'code'", (&PyCode_Type)->tp_name, args[0]);
280
0
        goto exit;
281
0
    }
282
0
    code = (PyCodeObject *)args[0];
283
0
    if (!PyDict_Check(args[1])) {
284
0
        _PyArg_BadArgument("function", "argument 'globals'", "dict", args[1]);
285
0
        goto exit;
286
0
    }
287
0
    globals = args[1];
288
0
    if (nargs < 3) {
289
0
        goto skip_optional;
290
0
    }
291
0
    name = args[2];
292
0
    if (nargs < 4) {
293
0
        goto skip_optional;
294
0
    }
295
0
    defaults = args[3];
296
0
    if (nargs < 5) {
297
0
        goto skip_optional;
298
0
    }
299
0
    closure = args[4];
300
0
    if (nargs < 6) {
301
0
        goto skip_optional;
302
0
    }
303
0
    kwdefaults = args[5];
304
0
skip_optional:
305
0
    return_value = func_new_impl(_PyType_CAST(type), code, globals, name, defaults, closure, kwdefaults);
306
307
0
exit:
308
0
    return return_value;
309
0
}
310
#define FUNCTION___ANNOTATE___GETSETDEF {"__annotate__", (getter)function___annotate___get, (setter)function___annotate___set, function___annotate____doc__},
311
312
#define FUNCTION___ANNOTATIONS___GETSETDEF {"__annotations__", (getter)function___annotations___get, (setter)function___annotations___set, function___annotations____doc__},
313
314
#define FUNCTION___TYPE_PARAMS___GETSETDEF {"__type_params__", (getter)function___type_params___get, (setter)function___type_params___set, function___type_params____doc__},
315
316
/*[clinic end generated code: output=3cd73f64e8bd846e input=a9049054013a1b77]*/