/src/cpython/Objects/call.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "Python.h" |
2 | | #include "pycore_call.h" // _PyObject_CallNoArgsTstate() |
3 | | #include "pycore_ceval.h" // _Py_EnterRecursiveCallTstate() |
4 | | #include "pycore_dict.h" // _PyDict_FromItems() |
5 | | #include "pycore_function.h" // _PyFunction_Vectorcall() definition |
6 | | #include "pycore_modsupport.h" // _Py_VaBuildStack() |
7 | | #include "pycore_object.h" // _PyCFunctionWithKeywords_TrampolineCall() |
8 | | #include "pycore_pyerrors.h" // _PyErr_Occurred() |
9 | | #include "pycore_pystate.h" // _PyThreadState_GET() |
10 | | #include "pycore_tuple.h" // _PyTuple_ITEMS() |
11 | | |
12 | | |
13 | | static PyObject * |
14 | | null_error(PyThreadState *tstate) |
15 | 0 | { |
16 | 0 | if (!_PyErr_Occurred(tstate)) { |
17 | 0 | _PyErr_SetString(tstate, PyExc_SystemError, |
18 | 0 | "null argument to internal routine"); |
19 | 0 | } |
20 | 0 | return NULL; |
21 | 0 | } |
22 | | |
23 | | |
24 | | PyObject* |
25 | | _Py_CheckFunctionResult(PyThreadState *tstate, PyObject *callable, |
26 | | PyObject *result, const char *where) |
27 | 672M | { |
28 | 672M | assert((callable != NULL) ^ (where != NULL)); |
29 | | |
30 | 672M | if (result == NULL) { |
31 | 12.4M | if (!_PyErr_Occurred(tstate)) { |
32 | 0 | if (callable) |
33 | 0 | _PyErr_Format(tstate, PyExc_SystemError, |
34 | 0 | "%R returned NULL without setting an exception", |
35 | 0 | callable); |
36 | 0 | else |
37 | 0 | _PyErr_Format(tstate, PyExc_SystemError, |
38 | 0 | "%s returned NULL without setting an exception", |
39 | 0 | where); |
40 | | #ifdef Py_DEBUG |
41 | | /* Ensure that the bug is caught in debug mode. |
42 | | Py_FatalError() logs the SystemError exception raised above. */ |
43 | | Py_FatalError("a function returned NULL without setting an exception"); |
44 | | #endif |
45 | 0 | return NULL; |
46 | 0 | } |
47 | 12.4M | } |
48 | 660M | else { |
49 | 660M | if (_PyErr_Occurred(tstate)) { |
50 | 0 | Py_DECREF(result); |
51 | |
|
52 | 0 | if (callable) { |
53 | 0 | _PyErr_FormatFromCauseTstate( |
54 | 0 | tstate, PyExc_SystemError, |
55 | 0 | "%R returned a result with an exception set", callable); |
56 | 0 | } |
57 | 0 | else { |
58 | 0 | _PyErr_FormatFromCauseTstate( |
59 | 0 | tstate, PyExc_SystemError, |
60 | 0 | "%s returned a result with an exception set", where); |
61 | 0 | } |
62 | | #ifdef Py_DEBUG |
63 | | /* Ensure that the bug is caught in debug mode. |
64 | | Py_FatalError() logs the SystemError exception raised above. */ |
65 | | Py_FatalError("a function returned a result with an exception set"); |
66 | | #endif |
67 | 0 | return NULL; |
68 | 0 | } |
69 | 660M | } |
70 | 672M | return result; |
71 | 672M | } |
72 | | |
73 | | |
74 | | int |
75 | | _Py_CheckSlotResult(PyObject *obj, const char *slot_name, int success) |
76 | 0 | { |
77 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
78 | 0 | if (!success) { |
79 | 0 | if (!_PyErr_Occurred(tstate)) { |
80 | 0 | _Py_FatalErrorFormat(__func__, |
81 | 0 | "Slot %s of type %s failed " |
82 | 0 | "without setting an exception", |
83 | 0 | slot_name, Py_TYPE(obj)->tp_name); |
84 | 0 | } |
85 | 0 | } |
86 | 0 | else { |
87 | 0 | if (_PyErr_Occurred(tstate)) { |
88 | 0 | _Py_FatalErrorFormat(__func__, |
89 | 0 | "Slot %s of type %s succeeded " |
90 | 0 | "with an exception set", |
91 | 0 | slot_name, Py_TYPE(obj)->tp_name); |
92 | 0 | } |
93 | 0 | } |
94 | 0 | return 1; |
95 | 0 | } |
96 | | |
97 | | |
98 | | /* --- Core PyObject call functions ------------------------------- */ |
99 | | |
100 | | /* Call a callable Python object without any arguments */ |
101 | | PyObject * |
102 | | PyObject_CallNoArgs(PyObject *func) |
103 | 11 | { |
104 | 11 | EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, func); |
105 | 11 | PyThreadState *tstate = _PyThreadState_GET(); |
106 | 11 | return _PyObject_VectorcallTstate(tstate, func, NULL, 0, NULL); |
107 | 11 | } |
108 | | |
109 | | |
110 | | PyObject * |
111 | | _PyObject_VectorcallDictTstate(PyThreadState *tstate, PyObject *callable, |
112 | | PyObject *const *args, size_t nargsf, |
113 | | PyObject *kwargs) |
114 | 38.5M | { |
115 | 38.5M | assert(callable != NULL); |
116 | | |
117 | | /* PyObject_VectorcallDict() must not be called with an exception set, |
118 | | because it can clear it (directly or indirectly) and so the |
119 | | caller loses its exception */ |
120 | 38.5M | assert(!_PyErr_Occurred(tstate)); |
121 | | |
122 | 38.5M | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
123 | 38.5M | assert(nargs >= 0); |
124 | 38.5M | assert(nargs == 0 || args != NULL); |
125 | 38.5M | assert(kwargs == NULL || PyDict_Check(kwargs)); |
126 | | |
127 | 38.5M | vectorcallfunc func = PyVectorcall_Function(callable); |
128 | 38.5M | if (func == NULL) { |
129 | | /* Use tp_call instead */ |
130 | 3.75k | return _PyObject_MakeTpCall(tstate, callable, args, nargs, kwargs); |
131 | 3.75k | } |
132 | | |
133 | 38.5M | PyObject *res; |
134 | 38.5M | if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) { |
135 | 31.8M | res = func(callable, args, nargsf, NULL); |
136 | 31.8M | } |
137 | 6.70M | else { |
138 | 6.70M | PyObject *kwnames; |
139 | 6.70M | PyObject *const *newargs; |
140 | 6.70M | newargs = _PyStack_UnpackDict(tstate, |
141 | 6.70M | args, nargs, |
142 | 6.70M | kwargs, &kwnames); |
143 | 6.70M | if (newargs == NULL) { |
144 | 0 | return NULL; |
145 | 0 | } |
146 | 6.70M | res = func(callable, newargs, |
147 | 6.70M | nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames); |
148 | 6.70M | _PyStack_UnpackDict_Free(newargs, nargs, kwnames); |
149 | 6.70M | } |
150 | 38.5M | return _Py_CheckFunctionResult(tstate, callable, res, NULL); |
151 | 38.5M | } |
152 | | |
153 | | |
154 | | PyObject * |
155 | | PyObject_VectorcallDict(PyObject *callable, PyObject *const *args, |
156 | | size_t nargsf, PyObject *kwargs) |
157 | 258k | { |
158 | 258k | PyThreadState *tstate = _PyThreadState_GET(); |
159 | 258k | return _PyObject_VectorcallDictTstate(tstate, callable, args, nargsf, kwargs); |
160 | 258k | } |
161 | | |
162 | | static void |
163 | | object_is_not_callable(PyThreadState *tstate, PyObject *callable) |
164 | 0 | { |
165 | 0 | if (Py_IS_TYPE(callable, &PyModule_Type)) { |
166 | | // >>> import pprint |
167 | | // >>> pprint(thing) |
168 | | // Traceback (most recent call last): |
169 | | // File "<stdin>", line 1, in <module> |
170 | | // TypeError: 'module' object is not callable. Did you mean: 'pprint.pprint(...)'? |
171 | 0 | PyObject *name = PyModule_GetNameObject(callable); |
172 | 0 | if (name == NULL) { |
173 | 0 | _PyErr_Clear(tstate); |
174 | 0 | goto basic_type_error; |
175 | 0 | } |
176 | 0 | PyObject *attr; |
177 | 0 | int res = PyObject_GetOptionalAttr(callable, name, &attr); |
178 | 0 | if (res < 0) { |
179 | 0 | _PyErr_Clear(tstate); |
180 | 0 | } |
181 | 0 | else if (res > 0 && PyCallable_Check(attr)) { |
182 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
183 | 0 | "'%.200s' object is not callable. " |
184 | 0 | "Did you mean: '%U.%U(...)'?", |
185 | 0 | Py_TYPE(callable)->tp_name, name, name); |
186 | 0 | Py_DECREF(attr); |
187 | 0 | Py_DECREF(name); |
188 | 0 | return; |
189 | 0 | } |
190 | 0 | Py_XDECREF(attr); |
191 | 0 | Py_DECREF(name); |
192 | 0 | } |
193 | 0 | basic_type_error: |
194 | 0 | _PyErr_Format(tstate, PyExc_TypeError, "'%.200s' object is not callable", |
195 | 0 | Py_TYPE(callable)->tp_name); |
196 | 0 | } |
197 | | |
198 | | |
199 | | PyObject * |
200 | | _PyObject_MakeTpCall(PyThreadState *tstate, PyObject *callable, |
201 | | PyObject *const *args, Py_ssize_t nargs, |
202 | | PyObject *keywords) |
203 | 106M | { |
204 | 106M | assert(nargs >= 0); |
205 | 106M | assert(nargs == 0 || args != NULL); |
206 | 106M | assert(keywords == NULL || PyTuple_Check(keywords) || PyDict_Check(keywords)); |
207 | | |
208 | | /* Slow path: build a temporary tuple for positional arguments and a |
209 | | * temporary dictionary for keyword arguments (if any) */ |
210 | 106M | ternaryfunc call = Py_TYPE(callable)->tp_call; |
211 | 106M | if (call == NULL) { |
212 | 0 | object_is_not_callable(tstate, callable); |
213 | 0 | return NULL; |
214 | 0 | } |
215 | | |
216 | 106M | PyObject *argstuple = _PyTuple_FromArray(args, nargs); |
217 | 106M | if (argstuple == NULL) { |
218 | 0 | return NULL; |
219 | 0 | } |
220 | | |
221 | 106M | PyObject *kwdict; |
222 | 106M | if (keywords == NULL || PyDict_Check(keywords)) { |
223 | 100M | kwdict = keywords; |
224 | 100M | } |
225 | 6.73M | else { |
226 | 6.73M | if (PyTuple_GET_SIZE(keywords)) { |
227 | 6.73M | assert(args != NULL); |
228 | 6.73M | kwdict = _PyStack_AsDict(args + nargs, keywords); |
229 | 6.73M | if (kwdict == NULL) { |
230 | 0 | Py_DECREF(argstuple); |
231 | 0 | return NULL; |
232 | 0 | } |
233 | 6.73M | } |
234 | 0 | else { |
235 | 0 | keywords = kwdict = NULL; |
236 | 0 | } |
237 | 6.73M | } |
238 | | |
239 | 106M | PyObject *result = NULL; |
240 | 106M | if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object") == 0) |
241 | 106M | { |
242 | 106M | result = _PyCFunctionWithKeywords_TrampolineCall( |
243 | 106M | (PyCFunctionWithKeywords)call, callable, argstuple, kwdict); |
244 | 106M | _Py_LeaveRecursiveCallTstate(tstate); |
245 | 106M | } |
246 | | |
247 | 106M | Py_DECREF(argstuple); |
248 | 106M | if (kwdict != keywords) { |
249 | 6.73M | Py_DECREF(kwdict); |
250 | 6.73M | } |
251 | | |
252 | 106M | return _Py_CheckFunctionResult(tstate, callable, result, NULL); |
253 | 106M | } |
254 | | |
255 | | |
256 | | vectorcallfunc |
257 | | PyVectorcall_Function(PyObject *callable) |
258 | 72.5M | { |
259 | 72.5M | return _PyVectorcall_FunctionInline(callable); |
260 | 72.5M | } |
261 | | |
262 | | |
263 | | static PyObject * |
264 | | _PyVectorcall_Call(PyThreadState *tstate, vectorcallfunc func, |
265 | | PyObject *callable, PyObject *tuple, PyObject *kwargs) |
266 | 22.7M | { |
267 | 22.7M | assert(func != NULL); |
268 | | |
269 | 22.7M | Py_ssize_t nargs = PyTuple_GET_SIZE(tuple); |
270 | | |
271 | | /* Fast path for no keywords */ |
272 | 22.7M | if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) { |
273 | 22.0M | return func(callable, _PyTuple_ITEMS(tuple), nargs, NULL); |
274 | 22.0M | } |
275 | | |
276 | | /* Convert arguments & call */ |
277 | 718k | PyObject *const *args; |
278 | 718k | PyObject *kwnames; |
279 | 718k | args = _PyStack_UnpackDict(tstate, |
280 | 718k | _PyTuple_ITEMS(tuple), nargs, |
281 | 718k | kwargs, &kwnames); |
282 | 718k | if (args == NULL) { |
283 | 0 | return NULL; |
284 | 0 | } |
285 | 718k | PyObject *result = func(callable, args, |
286 | 718k | nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames); |
287 | 718k | _PyStack_UnpackDict_Free(args, nargs, kwnames); |
288 | | |
289 | 718k | return _Py_CheckFunctionResult(tstate, callable, result, NULL); |
290 | 718k | } |
291 | | |
292 | | |
293 | | PyObject * |
294 | | PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *kwargs) |
295 | 0 | { |
296 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
297 | | |
298 | | /* get vectorcallfunc as in _PyVectorcall_Function, but without |
299 | | * the Py_TPFLAGS_HAVE_VECTORCALL check */ |
300 | 0 | Py_ssize_t offset = Py_TYPE(callable)->tp_vectorcall_offset; |
301 | 0 | if (offset <= 0) { |
302 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
303 | 0 | "'%.200s' object does not support vectorcall", |
304 | 0 | Py_TYPE(callable)->tp_name); |
305 | 0 | return NULL; |
306 | 0 | } |
307 | 0 | assert(PyCallable_Check(callable)); |
308 | |
|
309 | 0 | vectorcallfunc func; |
310 | 0 | memcpy(&func, (char *) callable + offset, sizeof(func)); |
311 | 0 | if (func == NULL) { |
312 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
313 | 0 | "'%.200s' object does not support vectorcall", |
314 | 0 | Py_TYPE(callable)->tp_name); |
315 | 0 | return NULL; |
316 | 0 | } |
317 | | |
318 | 0 | return _PyVectorcall_Call(tstate, func, callable, tuple, kwargs); |
319 | 0 | } |
320 | | |
321 | | |
322 | | PyObject * |
323 | | PyObject_Vectorcall(PyObject *callable, PyObject *const *args, |
324 | | size_t nargsf, PyObject *kwnames) |
325 | 349M | { |
326 | 349M | PyThreadState *tstate = _PyThreadState_GET(); |
327 | 349M | return _PyObject_VectorcallTstate(tstate, callable, |
328 | 349M | args, nargsf, kwnames); |
329 | 349M | } |
330 | | |
331 | | |
332 | | PyObject * |
333 | | _PyObject_Call(PyThreadState *tstate, PyObject *callable, |
334 | | PyObject *args, PyObject *kwargs) |
335 | 33.9M | { |
336 | 33.9M | ternaryfunc call; |
337 | 33.9M | PyObject *result; |
338 | | |
339 | | /* PyObject_Call() must not be called with an exception set, |
340 | | because it can clear it (directly or indirectly) and so the |
341 | | caller loses its exception */ |
342 | 33.9M | assert(!_PyErr_Occurred(tstate)); |
343 | 33.9M | assert(PyTuple_Check(args)); |
344 | 33.9M | assert(kwargs == NULL || PyDict_Check(kwargs)); |
345 | 33.9M | EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, callable); |
346 | 33.9M | vectorcallfunc vector_func = PyVectorcall_Function(callable); |
347 | 33.9M | if (vector_func != NULL) { |
348 | 22.7M | return _PyVectorcall_Call(tstate, vector_func, callable, args, kwargs); |
349 | 22.7M | } |
350 | 11.2M | else { |
351 | 11.2M | call = Py_TYPE(callable)->tp_call; |
352 | 11.2M | if (call == NULL) { |
353 | 0 | object_is_not_callable(tstate, callable); |
354 | 0 | return NULL; |
355 | 0 | } |
356 | | |
357 | 11.2M | if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object")) { |
358 | 0 | return NULL; |
359 | 0 | } |
360 | | |
361 | 11.2M | result = (*call)(callable, args, kwargs); |
362 | | |
363 | 11.2M | _Py_LeaveRecursiveCallTstate(tstate); |
364 | | |
365 | 11.2M | return _Py_CheckFunctionResult(tstate, callable, result, NULL); |
366 | 11.2M | } |
367 | 33.9M | } |
368 | | |
369 | | PyObject * |
370 | | PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs) |
371 | 33.9M | { |
372 | 33.9M | PyThreadState *tstate = _PyThreadState_GET(); |
373 | 33.9M | return _PyObject_Call(tstate, callable, args, kwargs); |
374 | 33.9M | } |
375 | | |
376 | | |
377 | | /* Function removed in the Python 3.13 API but kept in the stable ABI. */ |
378 | | PyAPI_FUNC(PyObject *) |
379 | | PyCFunction_Call(PyObject *callable, PyObject *args, PyObject *kwargs) |
380 | 0 | { |
381 | 0 | return PyObject_Call(callable, args, kwargs); |
382 | 0 | } |
383 | | |
384 | | |
385 | | PyObject * |
386 | | PyObject_CallOneArg(PyObject *func, PyObject *arg) |
387 | 61.0M | { |
388 | 61.0M | EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, func); |
389 | 61.0M | assert(arg != NULL); |
390 | 61.0M | PyObject *_args[2]; |
391 | 61.0M | PyObject **args = _args + 1; // For PY_VECTORCALL_ARGUMENTS_OFFSET |
392 | 61.0M | args[0] = arg; |
393 | 61.0M | PyThreadState *tstate = _PyThreadState_GET(); |
394 | 61.0M | size_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET; |
395 | 61.0M | return _PyObject_VectorcallTstate(tstate, func, args, nargsf, NULL); |
396 | 61.0M | } |
397 | | |
398 | | |
399 | | /* --- PyFunction call functions ---------------------------------- */ |
400 | | |
401 | | PyObject * |
402 | | _PyFunction_Vectorcall(PyObject *func, PyObject* const* stack, |
403 | | size_t nargsf, PyObject *kwnames) |
404 | 138M | { |
405 | 138M | assert(PyFunction_Check(func)); |
406 | 138M | PyFunctionObject *f = (PyFunctionObject *)func; |
407 | 138M | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
408 | 138M | assert(nargs >= 0); |
409 | 138M | PyThreadState *tstate = _PyThreadState_GET(); |
410 | 138M | assert(nargs == 0 || stack != NULL); |
411 | 138M | EVAL_CALL_STAT_INC(EVAL_CALL_FUNCTION_VECTORCALL); |
412 | 138M | if (((PyCodeObject *)f->func_code)->co_flags & CO_OPTIMIZED) { |
413 | 138M | return _PyEval_Vector(tstate, f, NULL, stack, nargs, kwnames); |
414 | 138M | } |
415 | 0 | else { |
416 | 0 | return _PyEval_Vector(tstate, f, f->func_globals, stack, nargs, kwnames); |
417 | 0 | } |
418 | 138M | } |
419 | | |
420 | | /* --- More complex call functions -------------------------------- */ |
421 | | |
422 | | /* External interface to call any callable object. |
423 | | The args must be a tuple or NULL. The kwargs must be a dict or NULL. |
424 | | Function removed in Python 3.13 API but kept in the stable ABI. */ |
425 | | PyAPI_FUNC(PyObject*) |
426 | | PyEval_CallObjectWithKeywords(PyObject *callable, |
427 | | PyObject *args, PyObject *kwargs) |
428 | 0 | { |
429 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
430 | | #ifdef Py_DEBUG |
431 | | /* PyEval_CallObjectWithKeywords() must not be called with an exception |
432 | | set. It raises a new exception if parameters are invalid or if |
433 | | PyTuple_New() fails, and so the original exception is lost. */ |
434 | | assert(!_PyErr_Occurred(tstate)); |
435 | | #endif |
436 | |
|
437 | 0 | if (args != NULL && !PyTuple_Check(args)) { |
438 | 0 | _PyErr_SetString(tstate, PyExc_TypeError, |
439 | 0 | "argument list must be a tuple"); |
440 | 0 | return NULL; |
441 | 0 | } |
442 | | |
443 | 0 | if (kwargs != NULL && !PyDict_Check(kwargs)) { |
444 | 0 | _PyErr_SetString(tstate, PyExc_TypeError, |
445 | 0 | "keyword list must be a dictionary"); |
446 | 0 | return NULL; |
447 | 0 | } |
448 | | |
449 | 0 | if (args == NULL) { |
450 | 0 | return _PyObject_VectorcallDictTstate(tstate, callable, |
451 | 0 | NULL, 0, kwargs); |
452 | 0 | } |
453 | 0 | else { |
454 | 0 | return _PyObject_Call(tstate, callable, args, kwargs); |
455 | 0 | } |
456 | 0 | } |
457 | | |
458 | | |
459 | | PyObject * |
460 | | PyObject_CallObject(PyObject *callable, PyObject *args) |
461 | 73.9k | { |
462 | 73.9k | PyThreadState *tstate = _PyThreadState_GET(); |
463 | 73.9k | assert(!_PyErr_Occurred(tstate)); |
464 | 73.9k | if (args == NULL) { |
465 | 0 | return _PyObject_CallNoArgsTstate(tstate, callable); |
466 | 0 | } |
467 | 73.9k | if (!PyTuple_Check(args)) { |
468 | 0 | _PyErr_SetString(tstate, PyExc_TypeError, |
469 | 0 | "argument list must be a tuple"); |
470 | 0 | return NULL; |
471 | 0 | } |
472 | 73.9k | return _PyObject_Call(tstate, callable, args, NULL); |
473 | 73.9k | } |
474 | | |
475 | | |
476 | | /* Call callable(obj, *args, **kwargs). */ |
477 | | PyObject * |
478 | | _PyObject_Call_Prepend(PyThreadState *tstate, PyObject *callable, |
479 | | PyObject *obj, PyObject *args, PyObject *kwargs) |
480 | 38.2M | { |
481 | 38.2M | assert(PyTuple_Check(args)); |
482 | | |
483 | 38.2M | PyObject *small_stack[_PY_FASTCALL_SMALL_STACK]; |
484 | 38.2M | PyObject **stack; |
485 | | |
486 | 38.2M | Py_ssize_t argcount = PyTuple_GET_SIZE(args); |
487 | 38.2M | if (argcount + 1 <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) { |
488 | 38.2M | stack = small_stack; |
489 | 38.2M | } |
490 | 32 | else { |
491 | 32 | stack = PyMem_Malloc((argcount + 1) * sizeof(PyObject *)); |
492 | 32 | if (stack == NULL) { |
493 | 0 | PyErr_NoMemory(); |
494 | 0 | return NULL; |
495 | 0 | } |
496 | 32 | } |
497 | | |
498 | | /* use borrowed references */ |
499 | 38.2M | stack[0] = obj; |
500 | 38.2M | memcpy(&stack[1], |
501 | 38.2M | _PyTuple_ITEMS(args), |
502 | 38.2M | argcount * sizeof(PyObject *)); |
503 | | |
504 | 38.2M | PyObject *result = _PyObject_VectorcallDictTstate(tstate, callable, |
505 | 38.2M | stack, argcount + 1, |
506 | 38.2M | kwargs); |
507 | 38.2M | if (stack != small_stack) { |
508 | 32 | PyMem_Free(stack); |
509 | 32 | } |
510 | 38.2M | return result; |
511 | 38.2M | } |
512 | | |
513 | | |
514 | | /* --- Call with a format string ---------------------------------- */ |
515 | | |
516 | | static PyObject * |
517 | | _PyObject_CallFunctionVa(PyThreadState *tstate, PyObject *callable, |
518 | | const char *format, va_list va) |
519 | 254k | { |
520 | 254k | PyObject* small_stack[_PY_FASTCALL_SMALL_STACK]; |
521 | 254k | const Py_ssize_t small_stack_len = Py_ARRAY_LENGTH(small_stack); |
522 | 254k | PyObject **stack; |
523 | 254k | Py_ssize_t nargs, i; |
524 | 254k | PyObject *result; |
525 | | |
526 | 254k | if (callable == NULL) { |
527 | 0 | return null_error(tstate); |
528 | 0 | } |
529 | | |
530 | 254k | if (!format || !*format) { |
531 | 2.23k | return _PyObject_CallNoArgsTstate(tstate, callable); |
532 | 2.23k | } |
533 | | |
534 | 251k | stack = _Py_VaBuildStack(small_stack, small_stack_len, |
535 | 251k | format, va, &nargs); |
536 | 251k | if (stack == NULL) { |
537 | 0 | return NULL; |
538 | 0 | } |
539 | 251k | EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, callable); |
540 | 251k | if (nargs == 1 && PyTuple_Check(stack[0])) { |
541 | | /* Special cases for backward compatibility: |
542 | | - PyObject_CallFunction(func, "O", tuple) calls func(*tuple) |
543 | | - PyObject_CallFunction(func, "(OOO)", arg1, arg2, arg3) calls |
544 | | func(*(arg1, arg2, arg3)): func(arg1, arg2, arg3) */ |
545 | 16 | PyObject *args = stack[0]; |
546 | 16 | result = _PyObject_VectorcallTstate(tstate, callable, |
547 | 16 | _PyTuple_ITEMS(args), |
548 | 16 | PyTuple_GET_SIZE(args), |
549 | 16 | NULL); |
550 | 16 | } |
551 | 251k | else { |
552 | 251k | result = _PyObject_VectorcallTstate(tstate, callable, |
553 | 251k | stack, nargs, NULL); |
554 | 251k | } |
555 | | |
556 | 1.49M | for (i = 0; i < nargs; ++i) { |
557 | 1.24M | Py_DECREF(stack[i]); |
558 | 1.24M | } |
559 | 251k | if (stack != small_stack) { |
560 | 96 | PyMem_Free(stack); |
561 | 96 | } |
562 | 251k | return result; |
563 | 251k | } |
564 | | |
565 | | |
566 | | PyObject * |
567 | | PyObject_CallFunction(PyObject *callable, const char *format, ...) |
568 | 251k | { |
569 | 251k | va_list va; |
570 | 251k | PyObject *result; |
571 | 251k | PyThreadState *tstate = _PyThreadState_GET(); |
572 | | |
573 | 251k | va_start(va, format); |
574 | 251k | result = _PyObject_CallFunctionVa(tstate, callable, format, va); |
575 | 251k | va_end(va); |
576 | | |
577 | 251k | return result; |
578 | 251k | } |
579 | | |
580 | | |
581 | | /* PyEval_CallFunction is exact copy of PyObject_CallFunction. |
582 | | Function removed in Python 3.13 API but kept in the stable ABI. */ |
583 | | PyAPI_FUNC(PyObject*) |
584 | | PyEval_CallFunction(PyObject *callable, const char *format, ...) |
585 | 0 | { |
586 | 0 | va_list va; |
587 | 0 | PyObject *result; |
588 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
589 | |
|
590 | 0 | va_start(va, format); |
591 | 0 | result = _PyObject_CallFunctionVa(tstate, callable, format, va); |
592 | 0 | va_end(va); |
593 | |
|
594 | 0 | return result; |
595 | 0 | } |
596 | | |
597 | | |
598 | | /* _PyObject_CallFunction_SizeT is exact copy of PyObject_CallFunction. |
599 | | * This function must be kept because it is part of the stable ABI. |
600 | | */ |
601 | | PyAPI_FUNC(PyObject *) /* abi_only */ |
602 | | _PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...) |
603 | 0 | { |
604 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
605 | |
|
606 | 0 | va_list va; |
607 | 0 | va_start(va, format); |
608 | 0 | PyObject *result = _PyObject_CallFunctionVa(tstate, callable, format, va); |
609 | 0 | va_end(va); |
610 | |
|
611 | 0 | return result; |
612 | 0 | } |
613 | | |
614 | | |
615 | | static PyObject* |
616 | | callmethod(PyThreadState *tstate, PyObject* callable, const char *format, va_list va) |
617 | 2.42k | { |
618 | 2.42k | assert(callable != NULL); |
619 | 2.42k | if (!PyCallable_Check(callable)) { |
620 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
621 | 0 | "attribute of type '%.200s' is not callable", |
622 | 0 | Py_TYPE(callable)->tp_name); |
623 | 0 | return NULL; |
624 | 0 | } |
625 | | |
626 | 2.42k | return _PyObject_CallFunctionVa(tstate, callable, format, va); |
627 | 2.42k | } |
628 | | |
629 | | PyObject * |
630 | | PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...) |
631 | 2.24k | { |
632 | 2.24k | PyThreadState *tstate = _PyThreadState_GET(); |
633 | | |
634 | 2.24k | if (obj == NULL || name == NULL) { |
635 | 0 | return null_error(tstate); |
636 | 0 | } |
637 | | |
638 | 2.24k | PyObject *callable = PyObject_GetAttrString(obj, name); |
639 | 2.24k | if (callable == NULL) { |
640 | 0 | return NULL; |
641 | 0 | } |
642 | | |
643 | 2.24k | va_list va; |
644 | 2.24k | va_start(va, format); |
645 | 2.24k | PyObject *retval = callmethod(tstate, callable, format, va); |
646 | 2.24k | va_end(va); |
647 | | |
648 | 2.24k | Py_DECREF(callable); |
649 | 2.24k | return retval; |
650 | 2.24k | } |
651 | | |
652 | | |
653 | | /* PyEval_CallMethod is exact copy of PyObject_CallMethod. |
654 | | Function removed in Python 3.13 API but kept in the stable ABI. */ |
655 | | PyAPI_FUNC(PyObject*) |
656 | | PyEval_CallMethod(PyObject *obj, const char *name, const char *format, ...) |
657 | 0 | { |
658 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
659 | 0 | if (obj == NULL || name == NULL) { |
660 | 0 | return null_error(tstate); |
661 | 0 | } |
662 | | |
663 | 0 | PyObject *callable = PyObject_GetAttrString(obj, name); |
664 | 0 | if (callable == NULL) { |
665 | 0 | return NULL; |
666 | 0 | } |
667 | | |
668 | 0 | va_list va; |
669 | 0 | va_start(va, format); |
670 | 0 | PyObject *retval = callmethod(tstate, callable, format, va); |
671 | 0 | va_end(va); |
672 | |
|
673 | 0 | Py_DECREF(callable); |
674 | 0 | return retval; |
675 | 0 | } |
676 | | |
677 | | |
678 | | PyObject * |
679 | | _PyObject_CallMethod(PyObject *obj, PyObject *name, |
680 | | const char *format, ...) |
681 | 175 | { |
682 | 175 | PyThreadState *tstate = _PyThreadState_GET(); |
683 | 175 | if (obj == NULL || name == NULL) { |
684 | 0 | return null_error(tstate); |
685 | 0 | } |
686 | | |
687 | 175 | PyObject *callable = PyObject_GetAttr(obj, name); |
688 | 175 | if (callable == NULL) { |
689 | 0 | return NULL; |
690 | 0 | } |
691 | | |
692 | 175 | va_list va; |
693 | 175 | va_start(va, format); |
694 | 175 | PyObject *retval = callmethod(tstate, callable, format, va); |
695 | 175 | va_end(va); |
696 | | |
697 | 175 | Py_DECREF(callable); |
698 | 175 | return retval; |
699 | 175 | } |
700 | | |
701 | | |
702 | | PyObject * |
703 | | _PyObject_CallMethodId(PyObject *obj, _Py_Identifier *name, |
704 | | const char *format, ...) |
705 | 0 | { |
706 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
707 | 0 | if (obj == NULL || name == NULL) { |
708 | 0 | return null_error(tstate); |
709 | 0 | } |
710 | | |
711 | 0 | PyObject *callable = _PyObject_GetAttrId(obj, name); |
712 | 0 | if (callable == NULL) { |
713 | 0 | return NULL; |
714 | 0 | } |
715 | | |
716 | 0 | va_list va; |
717 | 0 | va_start(va, format); |
718 | 0 | PyObject *retval = callmethod(tstate, callable, format, va); |
719 | 0 | va_end(va); |
720 | |
|
721 | 0 | Py_DECREF(callable); |
722 | 0 | return retval; |
723 | 0 | } |
724 | | |
725 | | |
726 | | PyObject * _PyObject_CallMethodFormat(PyThreadState *tstate, PyObject *callable, |
727 | | const char *format, ...) |
728 | 0 | { |
729 | 0 | va_list va; |
730 | 0 | va_start(va, format); |
731 | 0 | PyObject *retval = callmethod(tstate, callable, format, va); |
732 | 0 | va_end(va); |
733 | 0 | return retval; |
734 | 0 | } |
735 | | |
736 | | |
737 | | // _PyObject_CallMethod_SizeT is exact copy of PyObject_CallMethod. |
738 | | // This function must be kept because it is part of the stable ABI. |
739 | | PyAPI_FUNC(PyObject *) /* abi_only */ |
740 | | _PyObject_CallMethod_SizeT(PyObject *obj, const char *name, |
741 | | const char *format, ...) |
742 | 0 | { |
743 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
744 | 0 | if (obj == NULL || name == NULL) { |
745 | 0 | return null_error(tstate); |
746 | 0 | } |
747 | | |
748 | 0 | PyObject *callable = PyObject_GetAttrString(obj, name); |
749 | 0 | if (callable == NULL) { |
750 | 0 | return NULL; |
751 | 0 | } |
752 | | |
753 | 0 | va_list va; |
754 | 0 | va_start(va, format); |
755 | 0 | PyObject *retval = callmethod(tstate, callable, format, va); |
756 | 0 | va_end(va); |
757 | |
|
758 | 0 | Py_DECREF(callable); |
759 | 0 | return retval; |
760 | 0 | } |
761 | | |
762 | | |
763 | | /* --- Call with "..." arguments ---------------------------------- */ |
764 | | |
765 | | static PyObject * |
766 | | object_vacall(PyThreadState *tstate, PyObject *base, |
767 | | PyObject *callable, va_list vargs) |
768 | 61.2k | { |
769 | 61.2k | PyObject *small_stack[_PY_FASTCALL_SMALL_STACK]; |
770 | 61.2k | PyObject **stack; |
771 | 61.2k | Py_ssize_t nargs; |
772 | 61.2k | PyObject *result; |
773 | 61.2k | Py_ssize_t i; |
774 | 61.2k | va_list countva; |
775 | | |
776 | 61.2k | if (callable == NULL) { |
777 | 0 | return null_error(tstate); |
778 | 0 | } |
779 | | |
780 | | /* Count the number of arguments */ |
781 | 61.2k | va_copy(countva, vargs); |
782 | 61.2k | nargs = base ? 1 : 0; |
783 | 144k | while (1) { |
784 | 144k | PyObject *arg = va_arg(countva, PyObject *); |
785 | 144k | if (arg == NULL) { |
786 | 61.2k | break; |
787 | 61.2k | } |
788 | 83.0k | nargs++; |
789 | 83.0k | } |
790 | 61.2k | va_end(countva); |
791 | | |
792 | | /* Copy arguments */ |
793 | 61.2k | if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) { |
794 | 61.2k | stack = small_stack; |
795 | 61.2k | } |
796 | 0 | else { |
797 | 0 | stack = PyMem_Malloc(nargs * sizeof(stack[0])); |
798 | 0 | if (stack == NULL) { |
799 | 0 | PyErr_NoMemory(); |
800 | 0 | return NULL; |
801 | 0 | } |
802 | 0 | } |
803 | | |
804 | 61.2k | i = 0; |
805 | 61.2k | if (base) { |
806 | 39.7k | stack[i++] = base; |
807 | 39.7k | } |
808 | | |
809 | 144k | for (; i < nargs; ++i) { |
810 | 83.0k | stack[i] = va_arg(vargs, PyObject *); |
811 | 83.0k | } |
812 | | |
813 | | #ifdef Py_STATS |
814 | | if (PyFunction_Check(callable)) { |
815 | | EVAL_CALL_STAT_INC(EVAL_CALL_API); |
816 | | } |
817 | | #endif |
818 | | /* Call the function */ |
819 | 61.2k | result = _PyObject_VectorcallTstate(tstate, callable, stack, nargs, NULL); |
820 | | |
821 | 61.2k | if (stack != small_stack) { |
822 | 0 | PyMem_Free(stack); |
823 | 0 | } |
824 | 61.2k | return result; |
825 | 61.2k | } |
826 | | |
827 | | |
828 | | PyObject * |
829 | | PyObject_VectorcallMethod(PyObject *name, PyObject *const *args, |
830 | | size_t nargsf, PyObject *kwnames) |
831 | 34.9k | { |
832 | 34.9k | assert(name != NULL); |
833 | 34.9k | assert(args != NULL); |
834 | 34.9k | assert(PyVectorcall_NARGS(nargsf) >= 1); |
835 | | |
836 | 34.9k | PyThreadState *tstate = _PyThreadState_GET(); |
837 | 34.9k | _PyCStackRef method; |
838 | 34.9k | _PyThreadState_PushCStackRef(tstate, &method); |
839 | | /* Use args[0] as "self" argument */ |
840 | 34.9k | int unbound = _PyObject_GetMethodStackRef(tstate, args[0], name, &method.ref); |
841 | 34.9k | if (PyStackRef_IsNull(method.ref)) { |
842 | 0 | _PyThreadState_PopCStackRef(tstate, &method); |
843 | 0 | return NULL; |
844 | 0 | } |
845 | 34.9k | PyObject *callable = PyStackRef_AsPyObjectBorrow(method.ref); |
846 | | |
847 | 34.9k | if (unbound) { |
848 | | /* We must remove PY_VECTORCALL_ARGUMENTS_OFFSET since |
849 | | * that would be interpreted as allowing to change args[-1] */ |
850 | 33.0k | nargsf &= ~PY_VECTORCALL_ARGUMENTS_OFFSET; |
851 | 33.0k | } |
852 | 1.93k | else { |
853 | | /* Skip "self". We can keep PY_VECTORCALL_ARGUMENTS_OFFSET since |
854 | | * args[-1] in the onward call is args[0] here. */ |
855 | 1.93k | args++; |
856 | 1.93k | nargsf--; |
857 | 1.93k | } |
858 | 34.9k | EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_METHOD, callable); |
859 | 34.9k | PyObject *result = _PyObject_VectorcallTstate(tstate, callable, |
860 | 34.9k | args, nargsf, kwnames); |
861 | 34.9k | _PyThreadState_PopCStackRef(tstate, &method); |
862 | 34.9k | return result; |
863 | 34.9k | } |
864 | | |
865 | | |
866 | | PyObject * |
867 | | PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ...) |
868 | 42.6k | { |
869 | 42.6k | PyThreadState *tstate = _PyThreadState_GET(); |
870 | 42.6k | if (obj == NULL || name == NULL) { |
871 | 0 | return null_error(tstate); |
872 | 0 | } |
873 | | |
874 | 42.6k | _PyCStackRef method; |
875 | 42.6k | _PyThreadState_PushCStackRef(tstate, &method); |
876 | 42.6k | int is_method = _PyObject_GetMethodStackRef(tstate, obj, name, &method.ref); |
877 | 42.6k | if (PyStackRef_IsNull(method.ref)) { |
878 | 0 | _PyThreadState_PopCStackRef(tstate, &method); |
879 | 0 | return NULL; |
880 | 0 | } |
881 | 42.6k | PyObject *callable = PyStackRef_AsPyObjectBorrow(method.ref); |
882 | 42.6k | obj = is_method ? obj : NULL; |
883 | | |
884 | 42.6k | va_list vargs; |
885 | 42.6k | va_start(vargs, name); |
886 | 42.6k | PyObject *result = object_vacall(tstate, obj, callable, vargs); |
887 | 42.6k | va_end(vargs); |
888 | | |
889 | 42.6k | _PyThreadState_PopCStackRef(tstate, &method); |
890 | 42.6k | return result; |
891 | 42.6k | } |
892 | | |
893 | | |
894 | | PyObject * |
895 | | _PyObject_CallMethodIdObjArgs(PyObject *obj, _Py_Identifier *name, ...) |
896 | 0 | { |
897 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
898 | 0 | if (obj == NULL || name == NULL) { |
899 | 0 | return null_error(tstate); |
900 | 0 | } |
901 | | |
902 | 0 | PyObject *oname = _PyUnicode_FromId(name); /* borrowed */ |
903 | 0 | if (!oname) { |
904 | 0 | return NULL; |
905 | 0 | } |
906 | 0 | _PyCStackRef method; |
907 | 0 | _PyThreadState_PushCStackRef(tstate, &method); |
908 | 0 | int is_method = _PyObject_GetMethodStackRef(tstate, obj, oname, &method.ref); |
909 | 0 | if (PyStackRef_IsNull(method.ref)) { |
910 | 0 | _PyThreadState_PopCStackRef(tstate, &method); |
911 | 0 | return NULL; |
912 | 0 | } |
913 | 0 | PyObject *callable = PyStackRef_AsPyObjectBorrow(method.ref); |
914 | |
|
915 | 0 | obj = is_method ? obj : NULL; |
916 | |
|
917 | 0 | va_list vargs; |
918 | 0 | va_start(vargs, name); |
919 | 0 | PyObject *result = object_vacall(tstate, obj, callable, vargs); |
920 | 0 | va_end(vargs); |
921 | |
|
922 | 0 | _PyThreadState_PopCStackRef(tstate, &method); |
923 | 0 | return result; |
924 | 0 | } |
925 | | |
926 | | |
927 | | PyObject * |
928 | | PyObject_CallFunctionObjArgs(PyObject *callable, ...) |
929 | 18.6k | { |
930 | 18.6k | PyThreadState *tstate = _PyThreadState_GET(); |
931 | 18.6k | va_list vargs; |
932 | 18.6k | PyObject *result; |
933 | | |
934 | 18.6k | va_start(vargs, callable); |
935 | 18.6k | result = object_vacall(tstate, NULL, callable, vargs); |
936 | 18.6k | va_end(vargs); |
937 | | |
938 | 18.6k | return result; |
939 | 18.6k | } |
940 | | |
941 | | |
942 | | /* --- PyStack functions ------------------------------------------ */ |
943 | | |
944 | | PyObject * |
945 | | _PyStack_AsDict(PyObject *const *values, PyObject *kwnames) |
946 | 6.73M | { |
947 | 6.73M | Py_ssize_t nkwargs; |
948 | | |
949 | 6.73M | assert(kwnames != NULL); |
950 | 6.73M | nkwargs = PyTuple_GET_SIZE(kwnames); |
951 | 6.73M | return _PyDict_FromItems(&PyTuple_GET_ITEM(kwnames, 0), 1, |
952 | 6.73M | values, 1, nkwargs); |
953 | 6.73M | } |
954 | | |
955 | | |
956 | | /* Convert (args, nargs, kwargs: dict) into a (stack, nargs, kwnames: tuple). |
957 | | |
958 | | Allocate a new argument vector and keyword names tuple. Return the argument |
959 | | vector; return NULL with exception set on error. Return the keyword names |
960 | | tuple in *p_kwnames. |
961 | | |
962 | | This also checks that all keyword names are strings. If not, a TypeError is |
963 | | raised. |
964 | | |
965 | | The newly allocated argument vector supports PY_VECTORCALL_ARGUMENTS_OFFSET. |
966 | | |
967 | | When done, you must call _PyStack_UnpackDict_Free(stack, nargs, kwnames) */ |
968 | | PyObject *const * |
969 | | _PyStack_UnpackDict(PyThreadState *tstate, |
970 | | PyObject *const *args, Py_ssize_t nargs, |
971 | | PyObject *kwargs, PyObject **p_kwnames) |
972 | 7.43M | { |
973 | 7.43M | assert(nargs >= 0); |
974 | 7.43M | assert(kwargs != NULL); |
975 | 7.43M | assert(PyDict_Check(kwargs)); |
976 | | |
977 | 7.43M | Py_ssize_t nkwargs = PyDict_GET_SIZE(kwargs); |
978 | | /* Check for overflow in the PyMem_Malloc() call below. The subtraction |
979 | | * in this check cannot overflow: both maxnargs and nkwargs are |
980 | | * non-negative signed integers, so their difference fits in the type. */ |
981 | 7.43M | Py_ssize_t maxnargs = PY_SSIZE_T_MAX / sizeof(args[0]) - 1; |
982 | 7.43M | if (nargs > maxnargs - nkwargs) { |
983 | 0 | _PyErr_NoMemory(tstate); |
984 | 0 | return NULL; |
985 | 0 | } |
986 | | |
987 | | /* Add 1 to support PY_VECTORCALL_ARGUMENTS_OFFSET */ |
988 | 7.43M | PyObject **stack = PyMem_Malloc((1 + nargs + nkwargs) * sizeof(args[0])); |
989 | 7.43M | if (stack == NULL) { |
990 | 0 | _PyErr_NoMemory(tstate); |
991 | 0 | return NULL; |
992 | 0 | } |
993 | | |
994 | 7.43M | PyObject *kwnames = PyTuple_New(nkwargs); |
995 | 7.43M | if (kwnames == NULL) { |
996 | 0 | PyMem_Free(stack); |
997 | 0 | return NULL; |
998 | 0 | } |
999 | | |
1000 | 7.43M | stack++; /* For PY_VECTORCALL_ARGUMENTS_OFFSET */ |
1001 | | |
1002 | | /* Copy positional arguments */ |
1003 | 14.8M | for (Py_ssize_t i = 0; i < nargs; i++) { |
1004 | 7.45M | stack[i] = Py_NewRef(args[i]); |
1005 | 7.45M | } |
1006 | | |
1007 | 7.43M | PyObject **kwstack = stack + nargs; |
1008 | | /* This loop doesn't support lookup function mutating the dictionary |
1009 | | to change its size. It's a deliberate choice for speed, this function is |
1010 | | called in the performance critical hot code. */ |
1011 | 7.43M | Py_ssize_t pos = 0, i = 0; |
1012 | 7.43M | PyObject *key, *value; |
1013 | 7.43M | unsigned long keys_are_strings = Py_TPFLAGS_UNICODE_SUBCLASS; |
1014 | 16.0M | while (PyDict_Next(kwargs, &pos, &key, &value)) { |
1015 | 8.62M | keys_are_strings &= Py_TYPE(key)->tp_flags; |
1016 | 8.62M | PyTuple_SET_ITEM(kwnames, i, Py_NewRef(key)); |
1017 | 8.62M | kwstack[i] = Py_NewRef(value); |
1018 | 8.62M | i++; |
1019 | 8.62M | } |
1020 | | |
1021 | | /* keys_are_strings has the value Py_TPFLAGS_UNICODE_SUBCLASS if that |
1022 | | * flag is set for all keys. Otherwise, keys_are_strings equals 0. |
1023 | | * We do this check once at the end instead of inside the loop above |
1024 | | * because it simplifies the deallocation in the failing case. |
1025 | | * It happens to also make the loop above slightly more efficient. */ |
1026 | 7.43M | if (!keys_are_strings) { |
1027 | 0 | _PyErr_SetString(tstate, PyExc_TypeError, |
1028 | 0 | "keywords must be strings"); |
1029 | 0 | _PyStack_UnpackDict_Free(stack, nargs, kwnames); |
1030 | 0 | return NULL; |
1031 | 0 | } |
1032 | | |
1033 | 7.43M | *p_kwnames = kwnames; |
1034 | 7.43M | return stack; |
1035 | 7.43M | } |
1036 | | |
1037 | | void |
1038 | | _PyStack_UnpackDict_Free(PyObject *const *stack, Py_ssize_t nargs, |
1039 | | PyObject *kwnames) |
1040 | 7.42M | { |
1041 | 7.42M | Py_ssize_t n = PyTuple_GET_SIZE(kwnames) + nargs; |
1042 | 23.5M | for (Py_ssize_t i = 0; i < n; i++) { |
1043 | 16.0M | Py_DECREF(stack[i]); |
1044 | 16.0M | } |
1045 | 7.42M | _PyStack_UnpackDict_FreeNoDecRef(stack, kwnames); |
1046 | 7.42M | } |
1047 | | |
1048 | | void |
1049 | | _PyStack_UnpackDict_FreeNoDecRef(PyObject *const *stack, PyObject *kwnames) |
1050 | 7.43M | { |
1051 | 7.43M | PyMem_Free((PyObject **)stack - 1); |
1052 | 7.43M | Py_DECREF(kwnames); |
1053 | 7.43M | } |
1054 | | |
1055 | | // Export for the stable ABI |
1056 | | #undef PyVectorcall_NARGS |
1057 | | Py_ssize_t |
1058 | | PyVectorcall_NARGS(size_t n) |
1059 | 0 | { |
1060 | 0 | return _PyVectorcall_NARGS(n); |
1061 | 0 | } |