/src/cpython/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 | 6.22k | { |
21 | 6.22k | PyObject *return_value = NULL; |
22 | | |
23 | 6.22k | Py_BEGIN_CRITICAL_SECTION(self); |
24 | 6.22k | return_value = function___annotate___get_impl((PyFunctionObject *)self); |
25 | 6.22k | Py_END_CRITICAL_SECTION(); |
26 | | |
27 | 6.22k | return return_value; |
28 | 6.22k | } |
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 | 5.96k | { |
36 | 5.96k | int return_value = -1; |
37 | 5.96k | PyObject *value; |
38 | | |
39 | 5.96k | 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 | 5.96k | value = arg; |
46 | 5.96k | Py_BEGIN_CRITICAL_SECTION(self); |
47 | 5.96k | return_value = function___annotate___set_impl((PyFunctionObject *)self, value); |
48 | 5.96k | Py_END_CRITICAL_SECTION(); |
49 | | |
50 | 5.96k | return return_value; |
51 | 5.96k | } |
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 | 10.9k | { |
62 | 10.9k | PyObject *return_value = NULL; |
63 | | |
64 | 10.9k | Py_BEGIN_CRITICAL_SECTION(self); |
65 | 10.9k | return_value = function___annotations___get_impl((PyFunctionObject *)self); |
66 | 10.9k | Py_END_CRITICAL_SECTION(); |
67 | | |
68 | 10.9k | return return_value; |
69 | 10.9k | } |
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 | 5.44k | { |
77 | 5.44k | int return_value = -1; |
78 | 5.44k | PyObject *value = NULL; |
79 | | |
80 | 5.44k | if (arg != NULL) { |
81 | 5.44k | value = arg; |
82 | 5.44k | } |
83 | 5.44k | Py_BEGIN_CRITICAL_SECTION(self); |
84 | 5.44k | return_value = function___annotations___set_impl((PyFunctionObject *)self, value); |
85 | 5.44k | Py_END_CRITICAL_SECTION(); |
86 | | |
87 | 5.44k | return return_value; |
88 | 5.44k | } |
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 | 6.13k | { |
99 | 6.13k | PyObject *return_value = NULL; |
100 | | |
101 | 6.13k | Py_BEGIN_CRITICAL_SECTION(self); |
102 | 6.13k | return_value = function___type_params___get_impl((PyFunctionObject *)self); |
103 | 6.13k | Py_END_CRITICAL_SECTION(); |
104 | | |
105 | 6.13k | return return_value; |
106 | 6.13k | } |
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 | 5.85k | { |
114 | 5.85k | int return_value = -1; |
115 | 5.85k | PyObject *value; |
116 | | |
117 | 5.85k | 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 | 5.85k | 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 | 5.85k | value = arg; |
128 | 5.85k | Py_BEGIN_CRITICAL_SECTION(self); |
129 | 5.85k | return_value = function___type_params___set_impl((PyFunctionObject *)self, value); |
130 | 5.85k | Py_END_CRITICAL_SECTION(); |
131 | | |
132 | 5.85k | exit: |
133 | 5.85k | return return_value; |
134 | 5.85k | } |
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(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
163 | 36 | { |
164 | 36 | PyObject *return_value = NULL; |
165 | 36 | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
166 | | |
167 | 36 | #define NUM_KEYWORDS 6 |
168 | 36 | static struct { |
169 | 36 | PyGC_Head _this_is_not_used; |
170 | 36 | PyObject_VAR_HEAD |
171 | 36 | Py_hash_t ob_hash; |
172 | 36 | PyObject *ob_item[NUM_KEYWORDS]; |
173 | 36 | } _kwtuple = { |
174 | 36 | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
175 | 36 | .ob_hash = -1, |
176 | 36 | .ob_item = { &_Py_ID(code), &_Py_ID(globals), &_Py_ID(name), &_Py_ID(argdefs), &_Py_ID(closure), &_Py_ID(kwdefaults), }, |
177 | 36 | }; |
178 | 36 | #undef NUM_KEYWORDS |
179 | 36 | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
180 | | |
181 | | #else // !Py_BUILD_CORE |
182 | | # define KWTUPLE NULL |
183 | | #endif // !Py_BUILD_CORE |
184 | | |
185 | 36 | static const char * const _keywords[] = {"code", "globals", "name", "argdefs", "closure", "kwdefaults", NULL}; |
186 | 36 | static _PyArg_Parser _parser = { |
187 | 36 | .keywords = _keywords, |
188 | 36 | .fname = "function", |
189 | 36 | .kwtuple = KWTUPLE, |
190 | 36 | }; |
191 | 36 | #undef KWTUPLE |
192 | 36 | PyObject *argsbuf[6]; |
193 | 36 | PyObject * const *fastargs; |
194 | 36 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
195 | 36 | Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 2; |
196 | 36 | PyCodeObject *code; |
197 | 36 | PyObject *globals; |
198 | 36 | PyObject *name = Py_None; |
199 | 36 | PyObject *defaults = Py_None; |
200 | 36 | PyObject *closure = Py_None; |
201 | 36 | PyObject *kwdefaults = Py_None; |
202 | | |
203 | 36 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, |
204 | 36 | /*minpos*/ 2, /*maxpos*/ 6, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
205 | 36 | if (!fastargs) { |
206 | 0 | goto exit; |
207 | 0 | } |
208 | 36 | 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 | 36 | code = (PyCodeObject *)fastargs[0]; |
213 | 36 | if (!PyDict_Check(fastargs[1])) { |
214 | 0 | _PyArg_BadArgument("function", "argument 'globals'", "dict", fastargs[1]); |
215 | 0 | goto exit; |
216 | 0 | } |
217 | 36 | globals = fastargs[1]; |
218 | 36 | if (!noptargs) { |
219 | 0 | goto skip_optional_pos; |
220 | 0 | } |
221 | 36 | if (fastargs[2]) { |
222 | 0 | name = fastargs[2]; |
223 | 0 | if (!--noptargs) { |
224 | 0 | goto skip_optional_pos; |
225 | 0 | } |
226 | 0 | } |
227 | 36 | if (fastargs[3]) { |
228 | 36 | defaults = fastargs[3]; |
229 | 36 | if (!--noptargs) { |
230 | 0 | goto skip_optional_pos; |
231 | 0 | } |
232 | 36 | } |
233 | 36 | if (fastargs[4]) { |
234 | 36 | closure = fastargs[4]; |
235 | 36 | if (!--noptargs) { |
236 | 0 | goto skip_optional_pos; |
237 | 0 | } |
238 | 36 | } |
239 | 36 | kwdefaults = fastargs[5]; |
240 | 36 | skip_optional_pos: |
241 | 36 | return_value = func_new_impl(type, code, globals, name, defaults, closure, kwdefaults); |
242 | | |
243 | 36 | exit: |
244 | 36 | return return_value; |
245 | 36 | } |
246 | | #define FUNCTION___ANNOTATE___GETSETDEF {"__annotate__", (getter)function___annotate___get, (setter)function___annotate___set, function___annotate____doc__}, |
247 | | |
248 | | #define FUNCTION___ANNOTATIONS___GETSETDEF {"__annotations__", (getter)function___annotations___get, (setter)function___annotations___set, function___annotations____doc__}, |
249 | | |
250 | | #define FUNCTION___TYPE_PARAMS___GETSETDEF {"__type_params__", (getter)function___type_params___get, (setter)function___type_params___set, function___type_params____doc__}, |
251 | | |
252 | | /*[clinic end generated code: output=b722bea4e9d8b8be input=a9049054013a1b77]*/ |