/src/Python-3.8.3/Objects/call.c
Line  | Count  | Source  | 
1  |  | #include "Python.h"  | 
2  |  | #include "pycore_object.h"  | 
3  |  | #include "pycore_pystate.h"  | 
4  |  | #include "pycore_tupleobject.h"  | 
5  |  | #include "frameobject.h"  | 
6  |  |  | 
7  |  |  | 
8  |  | static PyObject *  | 
9  |  | cfunction_call_varargs(PyObject *func, PyObject *args, PyObject *kwargs);  | 
10  |  |  | 
11  |  |  | 
12  |  | static PyObject *  | 
13  |  | null_error(void)  | 
14  | 0  | { | 
15  | 0  |     if (!PyErr_Occurred())  | 
16  | 0  |         PyErr_SetString(PyExc_SystemError,  | 
17  | 0  |                         "null argument to internal routine");  | 
18  | 0  |     return NULL;  | 
19  | 0  | }  | 
20  |  |  | 
21  |  |  | 
22  |  | PyObject*  | 
23  |  | _Py_CheckFunctionResult(PyObject *callable, PyObject *result, const char *where)  | 
24  | 184k  | { | 
25  | 184k  |     int err_occurred = (PyErr_Occurred() != NULL);  | 
26  |  |  | 
27  | 184k  |     assert((callable != NULL) ^ (where != NULL));  | 
28  |  |  | 
29  | 184k  |     if (result == NULL) { | 
30  | 4.61k  |         if (!err_occurred) { | 
31  | 0  |             if (callable)  | 
32  | 0  |                 PyErr_Format(PyExc_SystemError,  | 
33  | 0  |                              "%R returned NULL without setting an error",  | 
34  | 0  |                              callable);  | 
35  | 0  |             else  | 
36  | 0  |                 PyErr_Format(PyExc_SystemError,  | 
37  | 0  |                              "%s returned NULL without setting an error",  | 
38  | 0  |                              where);  | 
39  |  | #ifdef Py_DEBUG  | 
40  |  |             /* Ensure that the bug is caught in debug mode */  | 
41  |  |             Py_FatalError("a function returned NULL without setting an error"); | 
42  |  | #endif  | 
43  | 0  |             return NULL;  | 
44  | 0  |         }  | 
45  | 4.61k  |     }  | 
46  | 179k  |     else { | 
47  | 179k  |         if (err_occurred) { | 
48  | 0  |             Py_DECREF(result);  | 
49  |  | 
  | 
50  | 0  |             if (callable) { | 
51  | 0  |                 _PyErr_FormatFromCause(PyExc_SystemError,  | 
52  | 0  |                         "%R returned a result with an error set",  | 
53  | 0  |                         callable);  | 
54  | 0  |             }  | 
55  | 0  |             else { | 
56  | 0  |                 _PyErr_FormatFromCause(PyExc_SystemError,  | 
57  | 0  |                         "%s returned a result with an error set",  | 
58  | 0  |                         where);  | 
59  | 0  |             }  | 
60  |  | #ifdef Py_DEBUG  | 
61  |  |             /* Ensure that the bug is caught in debug mode */  | 
62  |  |             Py_FatalError("a function returned a result with an error set"); | 
63  |  | #endif  | 
64  | 0  |             return NULL;  | 
65  | 0  |         }  | 
66  | 179k  |     }  | 
67  | 184k  |     return result;  | 
68  | 184k  | }  | 
69  |  |  | 
70  |  |  | 
71  |  | /* --- Core PyObject call functions ------------------------------- */  | 
72  |  |  | 
73  |  | PyObject *  | 
74  |  | _PyObject_FastCallDict(PyObject *callable, PyObject *const *args,  | 
75  |  |                        size_t nargsf, PyObject *kwargs)  | 
76  | 6.50k  | { | 
77  |  |     /* _PyObject_FastCallDict() must not be called with an exception set,  | 
78  |  |        because it can clear it (directly or indirectly) and so the  | 
79  |  |        caller loses its exception */  | 
80  | 6.50k  |     assert(!PyErr_Occurred());  | 
81  | 6.50k  |     assert(callable != NULL);  | 
82  |  |  | 
83  | 6.50k  |     Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);  | 
84  | 6.50k  |     assert(nargs >= 0);  | 
85  | 6.50k  |     assert(nargs == 0 || args != NULL);  | 
86  | 6.50k  |     assert(kwargs == NULL || PyDict_Check(kwargs));  | 
87  |  |  | 
88  | 6.50k  |     vectorcallfunc func = _PyVectorcall_Function(callable);  | 
89  | 6.50k  |     if (func == NULL) { | 
90  |  |         /* Use tp_call instead */  | 
91  | 1.34k  |         return _PyObject_MakeTpCall(callable, args, nargs, kwargs);  | 
92  | 1.34k  |     }  | 
93  |  |  | 
94  | 5.16k  |     PyObject *res;  | 
95  | 5.16k  |     if (kwargs == NULL) { | 
96  | 3.88k  |         res = func(callable, args, nargsf, NULL);  | 
97  | 3.88k  |     }  | 
98  | 1.28k  |     else { | 
99  | 1.28k  |         PyObject *kwnames;  | 
100  | 1.28k  |         PyObject *const *newargs;  | 
101  | 1.28k  |         if (_PyStack_UnpackDict(args, nargs, kwargs, &newargs, &kwnames) < 0) { | 
102  | 0  |             return NULL;  | 
103  | 0  |         }  | 
104  | 1.28k  |         res = func(callable, newargs, nargs, kwnames);  | 
105  | 1.28k  |         if (kwnames != NULL) { | 
106  | 566  |             Py_ssize_t i, n = PyTuple_GET_SIZE(kwnames) + nargs;  | 
107  | 3.22k  |             for (i = 0; i < n; i++) { | 
108  | 2.65k  |                 Py_DECREF(newargs[i]);  | 
109  | 2.65k  |             }  | 
110  | 566  |             PyMem_Free((PyObject **)newargs);  | 
111  | 566  |             Py_DECREF(kwnames);  | 
112  | 566  |         }  | 
113  | 1.28k  |     }  | 
114  | 5.16k  |     return _Py_CheckFunctionResult(callable, res, NULL);  | 
115  | 5.16k  | }  | 
116  |  |  | 
117  |  |  | 
118  |  | PyObject *  | 
119  |  | _PyObject_MakeTpCall(PyObject *callable, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords)  | 
120  | 24.7k  | { | 
121  |  |     /* Slow path: build a temporary tuple for positional arguments and a  | 
122  |  |      * temporary dictionary for keyword arguments (if any) */  | 
123  | 24.7k  |     ternaryfunc call = Py_TYPE(callable)->tp_call;  | 
124  | 24.7k  |     if (call == NULL) { | 
125  | 0  |         PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",  | 
126  | 0  |                      Py_TYPE(callable)->tp_name);  | 
127  | 0  |         return NULL;  | 
128  | 0  |     }  | 
129  |  |  | 
130  | 24.7k  |     assert(nargs >= 0);  | 
131  | 24.7k  |     assert(nargs == 0 || args != NULL);  | 
132  | 24.7k  |     assert(keywords == NULL || PyTuple_Check(keywords) || PyDict_Check(keywords));  | 
133  | 24.7k  |     PyObject *argstuple = _PyTuple_FromArray(args, nargs);  | 
134  | 24.7k  |     if (argstuple == NULL) { | 
135  | 0  |         return NULL;  | 
136  | 0  |     }  | 
137  |  |  | 
138  | 24.7k  |     PyObject *kwdict;  | 
139  | 24.7k  |     if (keywords == NULL || PyDict_Check(keywords)) { | 
140  | 23.9k  |         kwdict = keywords;  | 
141  | 23.9k  |     }  | 
142  | 744  |     else { | 
143  | 744  |         if (PyTuple_GET_SIZE(keywords)) { | 
144  | 744  |             assert(args != NULL);  | 
145  | 744  |             kwdict = _PyStack_AsDict(args + nargs, keywords);  | 
146  | 744  |             if (kwdict == NULL) { | 
147  | 0  |                 Py_DECREF(argstuple);  | 
148  | 0  |                 return NULL;  | 
149  | 0  |             }  | 
150  | 744  |         }  | 
151  | 0  |         else { | 
152  | 0  |             keywords = kwdict = NULL;  | 
153  | 0  |         }  | 
154  | 744  |     }  | 
155  |  |  | 
156  | 24.7k  |     PyObject *result = NULL;  | 
157  | 24.7k  |     if (Py_EnterRecursiveCall(" while calling a Python object") == 0) | 
158  | 24.7k  |     { | 
159  | 24.7k  |         result = call(callable, argstuple, kwdict);  | 
160  | 24.7k  |         Py_LeaveRecursiveCall();  | 
161  | 24.7k  |     }  | 
162  |  |  | 
163  | 24.7k  |     Py_DECREF(argstuple);  | 
164  | 24.7k  |     if (kwdict != keywords) { | 
165  | 744  |         Py_DECREF(kwdict);  | 
166  | 744  |     }  | 
167  |  |  | 
168  | 24.7k  |     result = _Py_CheckFunctionResult(callable, result, NULL);  | 
169  | 24.7k  |     return result;  | 
170  | 24.7k  | }  | 
171  |  |  | 
172  |  |  | 
173  |  | PyObject *  | 
174  |  | PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *kwargs)  | 
175  | 997  | { | 
176  |  |     /* get vectorcallfunc as in _PyVectorcall_Function, but without  | 
177  |  |      * the _Py_TPFLAGS_HAVE_VECTORCALL check */  | 
178  | 997  |     Py_ssize_t offset = Py_TYPE(callable)->tp_vectorcall_offset;  | 
179  | 997  |     if (offset <= 0) { | 
180  | 0  |         PyErr_Format(PyExc_TypeError, "'%.200s' object does not support vectorcall",  | 
181  | 0  |                      Py_TYPE(callable)->tp_name);  | 
182  | 0  |         return NULL;  | 
183  | 0  |     }  | 
184  | 997  |     vectorcallfunc func = *(vectorcallfunc *)(((char *)callable) + offset);  | 
185  | 997  |     if (func == NULL) { | 
186  | 0  |         PyErr_Format(PyExc_TypeError, "'%.200s' object does not support vectorcall",  | 
187  | 0  |                      Py_TYPE(callable)->tp_name);  | 
188  | 0  |         return NULL;  | 
189  | 0  |     }  | 
190  |  |  | 
191  |  |     /* Convert arguments & call */  | 
192  | 997  |     PyObject *const *args;  | 
193  | 997  |     Py_ssize_t nargs = PyTuple_GET_SIZE(tuple);  | 
194  | 997  |     PyObject *kwnames;  | 
195  | 997  |     if (_PyStack_UnpackDict(_PyTuple_ITEMS(tuple), nargs,  | 
196  | 997  |         kwargs, &args, &kwnames) < 0) { | 
197  | 0  |         return NULL;  | 
198  | 0  |     }  | 
199  | 997  |     PyObject *result = func(callable, args, nargs, kwnames);  | 
200  | 997  |     if (kwnames != NULL) { | 
201  | 42  |         Py_ssize_t i, n = PyTuple_GET_SIZE(kwnames) + nargs;  | 
202  | 182  |         for (i = 0; i < n; i++) { | 
203  | 140  |             Py_DECREF(args[i]);  | 
204  | 140  |         }  | 
205  | 42  |         PyMem_Free((PyObject **)args);  | 
206  | 42  |         Py_DECREF(kwnames);  | 
207  | 42  |     }  | 
208  |  |  | 
209  | 997  |     return _Py_CheckFunctionResult(callable, result, NULL);  | 
210  | 997  | }  | 
211  |  |  | 
212  |  |  | 
213  |  | PyObject *  | 
214  |  | PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)  | 
215  | 1.20k  | { | 
216  | 1.20k  |     ternaryfunc call;  | 
217  | 1.20k  |     PyObject *result;  | 
218  |  |  | 
219  |  |     /* PyObject_Call() must not be called with an exception set,  | 
220  |  |        because it can clear it (directly or indirectly) and so the  | 
221  |  |        caller loses its exception */  | 
222  | 1.20k  |     assert(!PyErr_Occurred());  | 
223  | 1.20k  |     assert(PyTuple_Check(args));  | 
224  | 1.20k  |     assert(kwargs == NULL || PyDict_Check(kwargs));  | 
225  |  |  | 
226  | 1.20k  |     if (_PyVectorcall_Function(callable) != NULL) { | 
227  | 382  |         return PyVectorcall_Call(callable, args, kwargs);  | 
228  | 382  |     }  | 
229  | 825  |     else if (PyCFunction_Check(callable)) { | 
230  |  |         /* This must be a METH_VARARGS function, otherwise we would be  | 
231  |  |          * in the previous case */  | 
232  | 0  |         return cfunction_call_varargs(callable, args, kwargs);  | 
233  | 0  |     }  | 
234  | 825  |     else { | 
235  | 825  |         call = callable->ob_type->tp_call;  | 
236  | 825  |         if (call == NULL) { | 
237  | 0  |             PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",  | 
238  | 0  |                          callable->ob_type->tp_name);  | 
239  | 0  |             return NULL;  | 
240  | 0  |         }  | 
241  |  |  | 
242  | 825  |         if (Py_EnterRecursiveCall(" while calling a Python object")) | 
243  | 0  |             return NULL;  | 
244  |  |  | 
245  | 825  |         result = (*call)(callable, args, kwargs);  | 
246  |  |  | 
247  | 825  |         Py_LeaveRecursiveCall();  | 
248  |  |  | 
249  | 825  |         return _Py_CheckFunctionResult(callable, result, NULL);  | 
250  | 825  |     }  | 
251  | 1.20k  | }  | 
252  |  |  | 
253  |  |  | 
254  |  | /* --- PyFunction call functions ---------------------------------- */  | 
255  |  |  | 
256  |  | static PyObject* _Py_HOT_FUNCTION  | 
257  |  | function_code_fastcall(PyCodeObject *co, PyObject *const *args, Py_ssize_t nargs,  | 
258  |  |                        PyObject *globals)  | 
259  | 21.7k  | { | 
260  | 21.7k  |     PyFrameObject *f;  | 
261  | 21.7k  |     PyThreadState *tstate = _PyThreadState_GET();  | 
262  | 21.7k  |     PyObject **fastlocals;  | 
263  | 21.7k  |     Py_ssize_t i;  | 
264  | 21.7k  |     PyObject *result;  | 
265  |  |  | 
266  | 21.7k  |     assert(globals != NULL);  | 
267  |  |     /* XXX Perhaps we should create a specialized  | 
268  |  |        _PyFrame_New_NoTrack() that doesn't take locals, but does  | 
269  |  |        take builtins without sanity checking them.  | 
270  |  |        */  | 
271  | 21.7k  |     assert(tstate != NULL);  | 
272  | 21.7k  |     f = _PyFrame_New_NoTrack(tstate, co, globals, NULL);  | 
273  | 21.7k  |     if (f == NULL) { | 
274  | 0  |         return NULL;  | 
275  | 0  |     }  | 
276  |  |  | 
277  | 21.7k  |     fastlocals = f->f_localsplus;  | 
278  |  |  | 
279  | 57.3k  |     for (i = 0; i < nargs; i++) { | 
280  | 35.6k  |         Py_INCREF(*args);  | 
281  | 35.6k  |         fastlocals[i] = *args++;  | 
282  | 35.6k  |     }  | 
283  | 21.7k  |     result = PyEval_EvalFrameEx(f,0);  | 
284  |  |  | 
285  | 21.7k  |     if (Py_REFCNT(f) > 1) { | 
286  | 414  |         Py_DECREF(f);  | 
287  | 414  |         _PyObject_GC_TRACK(f);  | 
288  | 414  |     }  | 
289  | 21.2k  |     else { | 
290  | 21.2k  |         ++tstate->recursion_depth;  | 
291  | 21.2k  |         Py_DECREF(f);  | 
292  | 21.2k  |         --tstate->recursion_depth;  | 
293  | 21.2k  |     }  | 
294  | 21.7k  |     return result;  | 
295  | 21.7k  | }  | 
296  |  |  | 
297  |  |  | 
298  |  | PyObject *  | 
299  |  | _PyFunction_FastCallDict(PyObject *func, PyObject *const *args, Py_ssize_t nargs,  | 
300  |  |                          PyObject *kwargs)  | 
301  | 0  | { | 
302  | 0  |     PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);  | 
303  | 0  |     PyObject *globals = PyFunction_GET_GLOBALS(func);  | 
304  | 0  |     PyObject *argdefs = PyFunction_GET_DEFAULTS(func);  | 
305  | 0  |     PyObject *kwdefs, *closure, *name, *qualname;  | 
306  | 0  |     PyObject *kwtuple, **k;  | 
307  | 0  |     PyObject **d;  | 
308  | 0  |     Py_ssize_t nd, nk;  | 
309  | 0  |     PyObject *result;  | 
310  |  | 
  | 
311  | 0  |     assert(func != NULL);  | 
312  | 0  |     assert(nargs >= 0);  | 
313  | 0  |     assert(nargs == 0 || args != NULL);  | 
314  | 0  |     assert(kwargs == NULL || PyDict_Check(kwargs));  | 
315  |  | 
  | 
316  | 0  |     if (co->co_kwonlyargcount == 0 &&  | 
317  | 0  |         (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) &&  | 
318  | 0  |         (co->co_flags & ~PyCF_MASK) == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))  | 
319  | 0  |     { | 
320  |  |         /* Fast paths */  | 
321  | 0  |         if (argdefs == NULL && co->co_argcount == nargs) { | 
322  | 0  |             return function_code_fastcall(co, args, nargs, globals);  | 
323  | 0  |         }  | 
324  | 0  |         else if (nargs == 0 && argdefs != NULL  | 
325  | 0  |                  && co->co_argcount == PyTuple_GET_SIZE(argdefs)) { | 
326  |  |             /* function called with no arguments, but all parameters have  | 
327  |  |                a default value: use default values as arguments .*/  | 
328  | 0  |             args = _PyTuple_ITEMS(argdefs);  | 
329  | 0  |             return function_code_fastcall(co, args, PyTuple_GET_SIZE(argdefs),  | 
330  | 0  |                                           globals);  | 
331  | 0  |         }  | 
332  | 0  |     }  | 
333  |  |  | 
334  | 0  |     nk = (kwargs != NULL) ? PyDict_GET_SIZE(kwargs) : 0;  | 
335  | 0  |     if (nk != 0) { | 
336  | 0  |         Py_ssize_t pos, i;  | 
337  |  |  | 
338  |  |         /* bpo-29318, bpo-27840: Caller and callee functions must not share  | 
339  |  |            the dictionary: kwargs must be copied. */  | 
340  | 0  |         kwtuple = PyTuple_New(2 * nk);  | 
341  | 0  |         if (kwtuple == NULL) { | 
342  | 0  |             return NULL;  | 
343  | 0  |         }  | 
344  |  |  | 
345  | 0  |         k = _PyTuple_ITEMS(kwtuple);  | 
346  | 0  |         pos = i = 0;  | 
347  | 0  |         while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) { | 
348  |  |             /* We must hold strong references because keyword arguments can be  | 
349  |  |                indirectly modified while the function is called:  | 
350  |  |                see issue #2016 and test_extcall */  | 
351  | 0  |             Py_INCREF(k[i]);  | 
352  | 0  |             Py_INCREF(k[i+1]);  | 
353  | 0  |             i += 2;  | 
354  | 0  |         }  | 
355  | 0  |         assert(i / 2 == nk);  | 
356  | 0  |     }  | 
357  | 0  |     else { | 
358  | 0  |         kwtuple = NULL;  | 
359  | 0  |         k = NULL;  | 
360  | 0  |     }  | 
361  |  |  | 
362  | 0  |     kwdefs = PyFunction_GET_KW_DEFAULTS(func);  | 
363  | 0  |     closure = PyFunction_GET_CLOSURE(func);  | 
364  | 0  |     name = ((PyFunctionObject *)func) -> func_name;  | 
365  | 0  |     qualname = ((PyFunctionObject *)func) -> func_qualname;  | 
366  |  | 
  | 
367  | 0  |     if (argdefs != NULL) { | 
368  | 0  |         d = _PyTuple_ITEMS(argdefs);  | 
369  | 0  |         nd = PyTuple_GET_SIZE(argdefs);  | 
370  | 0  |     }  | 
371  | 0  |     else { | 
372  | 0  |         d = NULL;  | 
373  | 0  |         nd = 0;  | 
374  | 0  |     }  | 
375  |  | 
  | 
376  | 0  |     result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,  | 
377  | 0  |                                       args, nargs,  | 
378  | 0  |                                       k, k != NULL ? k + 1 : NULL, nk, 2,  | 
379  | 0  |                                       d, nd, kwdefs,  | 
380  | 0  |                                       closure, name, qualname);  | 
381  | 0  |     Py_XDECREF(kwtuple);  | 
382  | 0  |     return result;  | 
383  | 0  | }  | 
384  |  |  | 
385  |  |  | 
386  |  | PyObject *  | 
387  |  | _PyFunction_Vectorcall(PyObject *func, PyObject* const* stack,  | 
388  |  |                        size_t nargsf, PyObject *kwnames)  | 
389  | 35.6k  | { | 
390  | 35.6k  |     PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);  | 
391  | 35.6k  |     PyObject *globals = PyFunction_GET_GLOBALS(func);  | 
392  | 35.6k  |     PyObject *argdefs = PyFunction_GET_DEFAULTS(func);  | 
393  | 35.6k  |     PyObject *kwdefs, *closure, *name, *qualname;  | 
394  | 35.6k  |     PyObject **d;  | 
395  | 35.6k  |     Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);  | 
396  | 35.6k  |     Py_ssize_t nd;  | 
397  |  |  | 
398  | 35.6k  |     assert(PyFunction_Check(func));  | 
399  | 35.6k  |     Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);  | 
400  | 35.6k  |     assert(nargs >= 0);  | 
401  | 35.6k  |     assert(kwnames == NULL || PyTuple_CheckExact(kwnames));  | 
402  | 35.6k  |     assert((nargs == 0 && nkwargs == 0) || stack != NULL);  | 
403  |  |     /* kwnames must only contains str strings, no subclass, and all keys must  | 
404  |  |        be unique */  | 
405  |  |  | 
406  | 35.6k  |     if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&  | 
407  | 31.3k  |         (co->co_flags & ~PyCF_MASK) == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))  | 
408  | 24.1k  |     { | 
409  | 24.1k  |         if (argdefs == NULL && co->co_argcount == nargs) { | 
410  | 21.7k  |             return function_code_fastcall(co, stack, nargs, globals);  | 
411  | 21.7k  |         }  | 
412  | 2.43k  |         else if (nargs == 0 && argdefs != NULL  | 
413  | 0  |                  && co->co_argcount == PyTuple_GET_SIZE(argdefs)) { | 
414  |  |             /* function called with no arguments, but all parameters have  | 
415  |  |                a default value: use default values as arguments .*/  | 
416  | 0  |             stack = _PyTuple_ITEMS(argdefs);  | 
417  | 0  |             return function_code_fastcall(co, stack, PyTuple_GET_SIZE(argdefs),  | 
418  | 0  |                                           globals);  | 
419  | 0  |         }  | 
420  | 24.1k  |     }  | 
421  |  |  | 
422  | 13.9k  |     kwdefs = PyFunction_GET_KW_DEFAULTS(func);  | 
423  | 13.9k  |     closure = PyFunction_GET_CLOSURE(func);  | 
424  | 13.9k  |     name = ((PyFunctionObject *)func) -> func_name;  | 
425  | 13.9k  |     qualname = ((PyFunctionObject *)func) -> func_qualname;  | 
426  |  |  | 
427  | 13.9k  |     if (argdefs != NULL) { | 
428  | 4.09k  |         d = _PyTuple_ITEMS(argdefs);  | 
429  | 4.09k  |         nd = PyTuple_GET_SIZE(argdefs);  | 
430  | 4.09k  |     }  | 
431  | 9.89k  |     else { | 
432  | 9.89k  |         d = NULL;  | 
433  | 9.89k  |         nd = 0;  | 
434  | 9.89k  |     }  | 
435  | 13.9k  |     return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,  | 
436  | 13.9k  |                                     stack, nargs,  | 
437  | 13.9k  |                                     nkwargs ? _PyTuple_ITEMS(kwnames) : NULL,  | 
438  | 13.9k  |                                     stack + nargs,  | 
439  | 13.9k  |                                     nkwargs, 1,  | 
440  | 13.9k  |                                     d, (int)nd, kwdefs,  | 
441  | 13.9k  |                                     closure, name, qualname);  | 
442  | 35.6k  | }  | 
443  |  |  | 
444  |  |  | 
445  |  | /* --- PyCFunction call functions --------------------------------- */  | 
446  |  |  | 
447  |  | PyObject *  | 
448  |  | _PyMethodDef_RawFastCallDict(PyMethodDef *method, PyObject *self,  | 
449  |  |                              PyObject *const *args, Py_ssize_t nargs,  | 
450  |  |                              PyObject *kwargs)  | 
451  | 0  | { | 
452  |  |     /* _PyMethodDef_RawFastCallDict() must not be called with an exception set,  | 
453  |  |        because it can clear it (directly or indirectly) and so the  | 
454  |  |        caller loses its exception */  | 
455  | 0  |     assert(!PyErr_Occurred());  | 
456  |  | 
  | 
457  | 0  |     assert(method != NULL);  | 
458  | 0  |     assert(nargs >= 0);  | 
459  | 0  |     assert(nargs == 0 || args != NULL);  | 
460  | 0  |     assert(kwargs == NULL || PyDict_Check(kwargs));  | 
461  |  | 
  | 
462  | 0  |     PyCFunction meth = method->ml_meth;  | 
463  | 0  |     int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);  | 
464  | 0  |     PyObject *result = NULL;  | 
465  |  | 
  | 
466  | 0  |     if (Py_EnterRecursiveCall(" while calling a Python object")) { | 
467  | 0  |         return NULL;  | 
468  | 0  |     }  | 
469  |  |  | 
470  | 0  |     switch (flags)  | 
471  | 0  |     { | 
472  | 0  |     case METH_NOARGS:  | 
473  | 0  |         if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) { | 
474  | 0  |             goto no_keyword_error;  | 
475  | 0  |         }  | 
476  |  |  | 
477  | 0  |         if (nargs != 0) { | 
478  | 0  |             PyErr_Format(PyExc_TypeError,  | 
479  | 0  |                 "%.200s() takes no arguments (%zd given)",  | 
480  | 0  |                 method->ml_name, nargs);  | 
481  | 0  |             goto exit;  | 
482  | 0  |         }  | 
483  |  |  | 
484  | 0  |         result = (*meth) (self, NULL);  | 
485  | 0  |         break;  | 
486  |  |  | 
487  | 0  |     case METH_O:  | 
488  | 0  |         if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) { | 
489  | 0  |             goto no_keyword_error;  | 
490  | 0  |         }  | 
491  |  |  | 
492  | 0  |         if (nargs != 1) { | 
493  | 0  |             PyErr_Format(PyExc_TypeError,  | 
494  | 0  |                 "%.200s() takes exactly one argument (%zd given)",  | 
495  | 0  |                 method->ml_name, nargs);  | 
496  | 0  |             goto exit;  | 
497  | 0  |         }  | 
498  |  |  | 
499  | 0  |         result = (*meth) (self, args[0]);  | 
500  | 0  |         break;  | 
501  |  |  | 
502  | 0  |     case METH_VARARGS:  | 
503  | 0  |         if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) { | 
504  | 0  |             goto no_keyword_error;  | 
505  | 0  |         }  | 
506  |  |         /* fall through */  | 
507  |  |  | 
508  | 0  |     case METH_VARARGS | METH_KEYWORDS:  | 
509  | 0  |     { | 
510  |  |         /* Slow-path: create a temporary tuple for positional arguments */  | 
511  | 0  |         PyObject *argstuple = _PyTuple_FromArray(args, nargs);  | 
512  | 0  |         if (argstuple == NULL) { | 
513  | 0  |             goto exit;  | 
514  | 0  |         }  | 
515  |  |  | 
516  | 0  |         if (flags & METH_KEYWORDS) { | 
517  | 0  |             result = (*(PyCFunctionWithKeywords)(void(*)(void))meth) (self, argstuple, kwargs);  | 
518  | 0  |         }  | 
519  | 0  |         else { | 
520  | 0  |             result = (*meth) (self, argstuple);  | 
521  | 0  |         }  | 
522  | 0  |         Py_DECREF(argstuple);  | 
523  | 0  |         break;  | 
524  | 0  |     }  | 
525  |  |  | 
526  | 0  |     case METH_FASTCALL:  | 
527  | 0  |     { | 
528  | 0  |         if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) { | 
529  | 0  |             goto no_keyword_error;  | 
530  | 0  |         }  | 
531  |  |  | 
532  | 0  |         result = (*(_PyCFunctionFast)(void(*)(void))meth) (self, args, nargs);  | 
533  | 0  |         break;  | 
534  | 0  |     }  | 
535  |  |  | 
536  | 0  |     case METH_FASTCALL | METH_KEYWORDS:  | 
537  | 0  |     { | 
538  | 0  |         PyObject *const *stack;  | 
539  | 0  |         PyObject *kwnames;  | 
540  | 0  |         _PyCFunctionFastWithKeywords fastmeth = (_PyCFunctionFastWithKeywords)(void(*)(void))meth;  | 
541  |  | 
  | 
542  | 0  |         if (_PyStack_UnpackDict(args, nargs, kwargs, &stack, &kwnames) < 0) { | 
543  | 0  |             goto exit;  | 
544  | 0  |         }  | 
545  |  |  | 
546  | 0  |         result = (*fastmeth) (self, stack, nargs, kwnames);  | 
547  | 0  |         if (kwnames != NULL) { | 
548  | 0  |             Py_ssize_t i, n = nargs + PyTuple_GET_SIZE(kwnames);  | 
549  | 0  |             for (i = 0; i < n; i++) { | 
550  | 0  |                 Py_DECREF(stack[i]);  | 
551  | 0  |             }  | 
552  | 0  |             PyMem_Free((PyObject **)stack);  | 
553  | 0  |             Py_DECREF(kwnames);  | 
554  | 0  |         }  | 
555  | 0  |         break;  | 
556  | 0  |     }  | 
557  |  |  | 
558  | 0  |     default:  | 
559  | 0  |         PyErr_SetString(PyExc_SystemError,  | 
560  | 0  |                         "Bad call flags in _PyMethodDef_RawFastCallDict. "  | 
561  | 0  |                         "METH_OLDARGS is no longer supported!");  | 
562  | 0  |         goto exit;  | 
563  | 0  |     }  | 
564  |  |  | 
565  | 0  |     goto exit;  | 
566  |  |  | 
567  | 0  | no_keyword_error:  | 
568  | 0  |     PyErr_Format(PyExc_TypeError,  | 
569  | 0  |                  "%.200s() takes no keyword arguments",  | 
570  | 0  |                  method->ml_name);  | 
571  |  | 
  | 
572  | 0  | exit:  | 
573  | 0  |     Py_LeaveRecursiveCall();  | 
574  | 0  |     return result;  | 
575  | 0  | }  | 
576  |  |  | 
577  |  |  | 
578  |  | PyObject *  | 
579  |  | _PyCFunction_FastCallDict(PyObject *func,  | 
580  |  |                           PyObject *const *args, Py_ssize_t nargs,  | 
581  |  |                           PyObject *kwargs)  | 
582  | 0  | { | 
583  | 0  |     PyObject *result;  | 
584  |  | 
  | 
585  | 0  |     assert(func != NULL);  | 
586  | 0  |     assert(PyCFunction_Check(func));  | 
587  |  | 
  | 
588  | 0  |     result = _PyMethodDef_RawFastCallDict(((PyCFunctionObject*)func)->m_ml,  | 
589  | 0  |                                           PyCFunction_GET_SELF(func),  | 
590  | 0  |                                           args, nargs, kwargs);  | 
591  | 0  |     result = _Py_CheckFunctionResult(func, result, NULL);  | 
592  | 0  |     return result;  | 
593  | 0  | }  | 
594  |  |  | 
595  |  |  | 
596  |  | PyObject *  | 
597  |  | _PyMethodDef_RawFastCallKeywords(PyMethodDef *method, PyObject *self,  | 
598  |  |                                  PyObject *const *args, Py_ssize_t nargs,  | 
599  |  |                                  PyObject *kwnames)  | 
600  | 0  | { | 
601  |  |     /* _PyMethodDef_RawFastCallKeywords() must not be called with an exception set,  | 
602  |  |        because it can clear it (directly or indirectly) and so the  | 
603  |  |        caller loses its exception */  | 
604  | 0  |     assert(!PyErr_Occurred());  | 
605  |  | 
  | 
606  | 0  |     assert(method != NULL);  | 
607  | 0  |     assert(nargs >= 0);  | 
608  | 0  |     assert(kwnames == NULL || PyTuple_CheckExact(kwnames));  | 
609  |  |     /* kwnames must only contains str strings, no subclass, and all keys must  | 
610  |  |        be unique */  | 
611  |  | 
  | 
612  | 0  |     PyCFunction meth = method->ml_meth;  | 
613  | 0  |     int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);  | 
614  | 0  |     Py_ssize_t nkwargs = kwnames == NULL ? 0 : PyTuple_GET_SIZE(kwnames);  | 
615  | 0  |     PyObject *result = NULL;  | 
616  |  | 
  | 
617  | 0  |     if (Py_EnterRecursiveCall(" while calling a Python object")) { | 
618  | 0  |         return NULL;  | 
619  | 0  |     }  | 
620  |  |  | 
621  | 0  |     switch (flags)  | 
622  | 0  |     { | 
623  | 0  |     case METH_NOARGS:  | 
624  | 0  |         if (nkwargs) { | 
625  | 0  |             goto no_keyword_error;  | 
626  | 0  |         }  | 
627  |  |  | 
628  | 0  |         if (nargs != 0) { | 
629  | 0  |             PyErr_Format(PyExc_TypeError,  | 
630  | 0  |                 "%.200s() takes no arguments (%zd given)",  | 
631  | 0  |                 method->ml_name, nargs);  | 
632  | 0  |             goto exit;  | 
633  | 0  |         }  | 
634  |  |  | 
635  | 0  |         result = (*meth) (self, NULL);  | 
636  | 0  |         break;  | 
637  |  |  | 
638  | 0  |     case METH_O:  | 
639  | 0  |         if (nkwargs) { | 
640  | 0  |             goto no_keyword_error;  | 
641  | 0  |         }  | 
642  |  |  | 
643  | 0  |         if (nargs != 1) { | 
644  | 0  |             PyErr_Format(PyExc_TypeError,  | 
645  | 0  |                 "%.200s() takes exactly one argument (%zd given)",  | 
646  | 0  |                 method->ml_name, nargs);  | 
647  | 0  |             goto exit;  | 
648  | 0  |         }  | 
649  |  |  | 
650  | 0  |         result = (*meth) (self, args[0]);  | 
651  | 0  |         break;  | 
652  |  |  | 
653  | 0  |     case METH_FASTCALL:  | 
654  | 0  |         if (nkwargs) { | 
655  | 0  |             goto no_keyword_error;  | 
656  | 0  |         }  | 
657  | 0  |         result = ((_PyCFunctionFast)(void(*)(void))meth) (self, args, nargs);  | 
658  | 0  |         break;  | 
659  |  |  | 
660  | 0  |     case METH_FASTCALL | METH_KEYWORDS:  | 
661  |  |         /* Fast-path: avoid temporary dict to pass keyword arguments */  | 
662  | 0  |         result = ((_PyCFunctionFastWithKeywords)(void(*)(void))meth) (self, args, nargs, kwnames);  | 
663  | 0  |         break;  | 
664  |  |  | 
665  | 0  |     case METH_VARARGS:  | 
666  | 0  |         if (nkwargs) { | 
667  | 0  |             goto no_keyword_error;  | 
668  | 0  |         }  | 
669  |  |         /* fall through */  | 
670  |  |  | 
671  | 0  |     case METH_VARARGS | METH_KEYWORDS:  | 
672  | 0  |     { | 
673  |  |         /* Slow-path: create a temporary tuple for positional arguments  | 
674  |  |            and a temporary dict for keyword arguments */  | 
675  | 0  |         PyObject *argtuple;  | 
676  |  | 
  | 
677  | 0  |         argtuple = _PyTuple_FromArray(args, nargs);  | 
678  | 0  |         if (argtuple == NULL) { | 
679  | 0  |             goto exit;  | 
680  | 0  |         }  | 
681  |  |  | 
682  | 0  |         if (flags & METH_KEYWORDS) { | 
683  | 0  |             PyObject *kwdict;  | 
684  |  | 
  | 
685  | 0  |             if (nkwargs > 0) { | 
686  | 0  |                 kwdict = _PyStack_AsDict(args + nargs, kwnames);  | 
687  | 0  |                 if (kwdict == NULL) { | 
688  | 0  |                     Py_DECREF(argtuple);  | 
689  | 0  |                     goto exit;  | 
690  | 0  |                 }  | 
691  | 0  |             }  | 
692  | 0  |             else { | 
693  | 0  |                 kwdict = NULL;  | 
694  | 0  |             }  | 
695  |  |  | 
696  | 0  |             result = (*(PyCFunctionWithKeywords)(void(*)(void))meth) (self, argtuple, kwdict);  | 
697  | 0  |             Py_XDECREF(kwdict);  | 
698  | 0  |         }  | 
699  | 0  |         else { | 
700  | 0  |             result = (*meth) (self, argtuple);  | 
701  | 0  |         }  | 
702  | 0  |         Py_DECREF(argtuple);  | 
703  | 0  |         break;  | 
704  | 0  |     }  | 
705  |  |  | 
706  | 0  |     default:  | 
707  | 0  |         PyErr_SetString(PyExc_SystemError,  | 
708  | 0  |                         "Bad call flags in _PyMethodDef_RawFastCallKeywords. "  | 
709  | 0  |                         "METH_OLDARGS is no longer supported!");  | 
710  | 0  |         goto exit;  | 
711  | 0  |     }  | 
712  |  |  | 
713  | 0  |     goto exit;  | 
714  |  |  | 
715  | 0  | no_keyword_error:  | 
716  | 0  |     PyErr_Format(PyExc_TypeError,  | 
717  | 0  |                  "%.200s() takes no keyword arguments",  | 
718  | 0  |                  method->ml_name);  | 
719  |  | 
  | 
720  | 0  | exit:  | 
721  | 0  |     Py_LeaveRecursiveCall();  | 
722  | 0  |     return result;  | 
723  | 0  | }  | 
724  |  |  | 
725  |  |  | 
726  |  | static PyObject *  | 
727  |  | cfunction_call_varargs(PyObject *func, PyObject *args, PyObject *kwargs)  | 
728  | 10.1k  | { | 
729  | 10.1k  |     assert(!PyErr_Occurred());  | 
730  | 10.1k  |     assert(kwargs == NULL || PyDict_Check(kwargs));  | 
731  |  |  | 
732  | 10.1k  |     PyCFunction meth = PyCFunction_GET_FUNCTION(func);  | 
733  | 10.1k  |     PyObject *self = PyCFunction_GET_SELF(func);  | 
734  | 10.1k  |     PyObject *result;  | 
735  |  |  | 
736  | 10.1k  |     assert(PyCFunction_GET_FLAGS(func) & METH_VARARGS);  | 
737  | 10.1k  |     if (PyCFunction_GET_FLAGS(func) & METH_KEYWORDS) { | 
738  | 2.35k  |         if (Py_EnterRecursiveCall(" while calling a Python object")) { | 
739  | 0  |             return NULL;  | 
740  | 0  |         }  | 
741  |  |  | 
742  | 2.35k  |         result = (*(PyCFunctionWithKeywords)(void(*)(void))meth)(self, args, kwargs);  | 
743  |  |  | 
744  | 2.35k  |         Py_LeaveRecursiveCall();  | 
745  | 2.35k  |     }  | 
746  | 7.77k  |     else { | 
747  | 7.77k  |         if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) { | 
748  | 0  |             PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",  | 
749  | 0  |                          ((PyCFunctionObject*)func)->m_ml->ml_name);  | 
750  | 0  |             return NULL;  | 
751  | 0  |         }  | 
752  |  |  | 
753  | 7.77k  |         if (Py_EnterRecursiveCall(" while calling a Python object")) { | 
754  | 0  |             return NULL;  | 
755  | 0  |         }  | 
756  |  |  | 
757  | 7.77k  |         result = (*meth)(self, args);  | 
758  |  |  | 
759  | 7.77k  |         Py_LeaveRecursiveCall();  | 
760  | 7.77k  |     }  | 
761  |  |  | 
762  | 10.1k  |     return _Py_CheckFunctionResult(func, result, NULL);  | 
763  | 10.1k  | }  | 
764  |  |  | 
765  |  |  | 
766  |  | PyObject *  | 
767  |  | PyCFunction_Call(PyObject *func, PyObject *args, PyObject *kwargs)  | 
768  | 10.7k  | { | 
769  |  |     /* For METH_VARARGS, we cannot use vectorcall as the vectorcall pointer  | 
770  |  |      * is NULL. This is intentional, since vectorcall would be slower. */  | 
771  | 10.7k  |     if (PyCFunction_GET_FLAGS(func) & METH_VARARGS) { | 
772  | 10.1k  |         return cfunction_call_varargs(func, args, kwargs);  | 
773  | 10.1k  |     }  | 
774  | 615  |     return PyVectorcall_Call(func, args, kwargs);  | 
775  | 10.7k  | }  | 
776  |  |  | 
777  |  |  | 
778  |  | /* --- More complex call functions -------------------------------- */  | 
779  |  |  | 
780  |  | /* External interface to call any callable object.  | 
781  |  |    The args must be a tuple or NULL.  The kwargs must be a dict or NULL. */  | 
782  |  | PyObject *  | 
783  |  | PyEval_CallObjectWithKeywords(PyObject *callable,  | 
784  |  |                               PyObject *args, PyObject *kwargs)  | 
785  | 29  | { | 
786  |  | #ifdef Py_DEBUG  | 
787  |  |     /* PyEval_CallObjectWithKeywords() must not be called with an exception  | 
788  |  |        set. It raises a new exception if parameters are invalid or if  | 
789  |  |        PyTuple_New() fails, and so the original exception is lost. */  | 
790  |  |     assert(!PyErr_Occurred());  | 
791  |  | #endif  | 
792  |  |  | 
793  | 29  |     if (args != NULL && !PyTuple_Check(args)) { | 
794  | 0  |         PyErr_SetString(PyExc_TypeError,  | 
795  | 0  |                         "argument list must be a tuple");  | 
796  | 0  |         return NULL;  | 
797  | 0  |     }  | 
798  |  |  | 
799  | 29  |     if (kwargs != NULL && !PyDict_Check(kwargs)) { | 
800  | 0  |         PyErr_SetString(PyExc_TypeError,  | 
801  | 0  |                         "keyword list must be a dictionary");  | 
802  | 0  |         return NULL;  | 
803  | 0  |     }  | 
804  |  |  | 
805  | 29  |     if (args == NULL) { | 
806  | 0  |         return _PyObject_FastCallDict(callable, NULL, 0, kwargs);  | 
807  | 0  |     }  | 
808  | 29  |     else { | 
809  | 29  |         return PyObject_Call(callable, args, kwargs);  | 
810  | 29  |     }  | 
811  | 29  | }  | 
812  |  |  | 
813  |  |  | 
814  |  | PyObject *  | 
815  |  | PyObject_CallObject(PyObject *callable, PyObject *args)  | 
816  | 0  | { | 
817  | 0  |     return PyEval_CallObjectWithKeywords(callable, args, NULL);  | 
818  | 0  | }  | 
819  |  |  | 
820  |  |  | 
821  |  | /* Positional arguments are obj followed by args:  | 
822  |  |    call callable(obj, *args, **kwargs) */  | 
823  |  | PyObject *  | 
824  |  | _PyObject_FastCall_Prepend(PyObject *callable, PyObject *obj,  | 
825  |  |                            PyObject *const *args, Py_ssize_t nargs)  | 
826  | 833  | { | 
827  | 833  |     PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];  | 
828  | 833  |     PyObject **args2;  | 
829  | 833  |     PyObject *result;  | 
830  |  |  | 
831  | 833  |     nargs++;  | 
832  | 833  |     if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) { | 
833  | 833  |         args2 = small_stack;  | 
834  | 833  |     }  | 
835  | 0  |     else { | 
836  | 0  |         args2 = PyMem_Malloc(nargs * sizeof(PyObject *));  | 
837  | 0  |         if (args2 == NULL) { | 
838  | 0  |             PyErr_NoMemory();  | 
839  | 0  |             return NULL;  | 
840  | 0  |         }  | 
841  | 0  |     }  | 
842  |  |  | 
843  |  |     /* use borrowed references */  | 
844  | 833  |     args2[0] = obj;  | 
845  | 833  |     if (nargs > 1) { | 
846  | 675  |         memcpy(&args2[1], args, (nargs - 1) * sizeof(PyObject *));  | 
847  | 675  |     }  | 
848  |  |  | 
849  | 833  |     result = _PyObject_FastCall(callable, args2, nargs);  | 
850  | 833  |     if (args2 != small_stack) { | 
851  | 0  |         PyMem_Free(args2);  | 
852  | 0  |     }  | 
853  | 833  |     return result;  | 
854  | 833  | }  | 
855  |  |  | 
856  |  |  | 
857  |  | /* Call callable(obj, *args, **kwargs). */  | 
858  |  | PyObject *  | 
859  |  | _PyObject_Call_Prepend(PyObject *callable,  | 
860  |  |                        PyObject *obj, PyObject *args, PyObject *kwargs)  | 
861  | 2.47k  | { | 
862  | 2.47k  |     PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];  | 
863  | 2.47k  |     PyObject **stack;  | 
864  | 2.47k  |     Py_ssize_t argcount;  | 
865  | 2.47k  |     PyObject *result;  | 
866  |  |  | 
867  | 2.47k  |     assert(PyTuple_Check(args));  | 
868  |  |  | 
869  | 2.47k  |     argcount = PyTuple_GET_SIZE(args);  | 
870  | 2.47k  |     if (argcount + 1 <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) { | 
871  | 2.43k  |         stack = small_stack;  | 
872  | 2.43k  |     }  | 
873  | 42  |     else { | 
874  | 42  |         stack = PyMem_Malloc((argcount + 1) * sizeof(PyObject *));  | 
875  | 42  |         if (stack == NULL) { | 
876  | 0  |             PyErr_NoMemory();  | 
877  | 0  |             return NULL;  | 
878  | 0  |         }  | 
879  | 42  |     }  | 
880  |  |  | 
881  |  |     /* use borrowed references */  | 
882  | 2.47k  |     stack[0] = obj;  | 
883  | 2.47k  |     memcpy(&stack[1],  | 
884  | 2.47k  |            _PyTuple_ITEMS(args),  | 
885  | 2.47k  |            argcount * sizeof(PyObject *));  | 
886  |  |  | 
887  | 2.47k  |     result = _PyObject_FastCallDict(callable,  | 
888  | 2.47k  |                                     stack, argcount + 1,  | 
889  | 2.47k  |                                     kwargs);  | 
890  | 2.47k  |     if (stack != small_stack) { | 
891  | 42  |         PyMem_Free(stack);  | 
892  | 42  |     }  | 
893  | 2.47k  |     return result;  | 
894  | 2.47k  | }  | 
895  |  |  | 
896  |  |  | 
897  |  | /* --- Call with a format string ---------------------------------- */  | 
898  |  |  | 
899  |  | static PyObject *  | 
900  |  | _PyObject_CallFunctionVa(PyObject *callable, const char *format,  | 
901  |  |                          va_list va, int is_size_t)  | 
902  | 3.89k  | { | 
903  | 3.89k  |     PyObject* small_stack[_PY_FASTCALL_SMALL_STACK];  | 
904  | 3.89k  |     const Py_ssize_t small_stack_len = Py_ARRAY_LENGTH(small_stack);  | 
905  | 3.89k  |     PyObject **stack;  | 
906  | 3.89k  |     Py_ssize_t nargs, i;  | 
907  | 3.89k  |     PyObject *result;  | 
908  |  |  | 
909  | 3.89k  |     if (callable == NULL) { | 
910  | 0  |         return null_error();  | 
911  | 0  |     }  | 
912  |  |  | 
913  | 3.89k  |     if (!format || !*format) { | 
914  | 1.74k  |         return _PyObject_CallNoArg(callable);  | 
915  | 1.74k  |     }  | 
916  |  |  | 
917  | 2.15k  |     if (is_size_t) { | 
918  | 1.48k  |         stack = _Py_VaBuildStack_SizeT(small_stack, small_stack_len,  | 
919  | 1.48k  |                                        format, va, &nargs);  | 
920  | 1.48k  |     }  | 
921  | 670  |     else { | 
922  | 670  |         stack = _Py_VaBuildStack(small_stack, small_stack_len,  | 
923  | 670  |                                  format, va, &nargs);  | 
924  | 670  |     }  | 
925  | 2.15k  |     if (stack == NULL) { | 
926  | 0  |         return NULL;  | 
927  | 0  |     }  | 
928  |  |  | 
929  | 2.15k  |     if (nargs == 1 && PyTuple_Check(stack[0])) { | 
930  |  |         /* Special cases for backward compatibility:  | 
931  |  |            - PyObject_CallFunction(func, "O", tuple) calls func(*tuple)  | 
932  |  |            - PyObject_CallFunction(func, "(OOO)", arg1, arg2, arg3) calls  | 
933  |  |              func(*(arg1, arg2, arg3)): func(arg1, arg2, arg3) */  | 
934  | 0  |         PyObject *args = stack[0];  | 
935  | 0  |         result = _PyObject_FastCall(callable,  | 
936  | 0  |                                     _PyTuple_ITEMS(args),  | 
937  | 0  |                                     PyTuple_GET_SIZE(args));  | 
938  | 0  |     }  | 
939  | 2.15k  |     else { | 
940  | 2.15k  |         result = _PyObject_FastCall(callable, stack, nargs);  | 
941  | 2.15k  |     }  | 
942  |  |  | 
943  | 10.9k  |     for (i = 0; i < nargs; ++i) { | 
944  | 8.81k  |         Py_DECREF(stack[i]);  | 
945  | 8.81k  |     }  | 
946  | 2.15k  |     if (stack != small_stack) { | 
947  | 84  |         PyMem_Free(stack);  | 
948  | 84  |     }  | 
949  | 2.15k  |     return result;  | 
950  | 2.15k  | }  | 
951  |  |  | 
952  |  |  | 
953  |  | PyObject *  | 
954  |  | PyObject_CallFunction(PyObject *callable, const char *format, ...)  | 
955  | 572  | { | 
956  | 572  |     va_list va;  | 
957  | 572  |     PyObject *result;  | 
958  |  |  | 
959  | 572  |     va_start(va, format);  | 
960  | 572  |     result = _PyObject_CallFunctionVa(callable, format, va, 0);  | 
961  | 572  |     va_end(va);  | 
962  |  |  | 
963  | 572  |     return result;  | 
964  | 572  | }  | 
965  |  |  | 
966  |  |  | 
967  |  | /* PyEval_CallFunction is exact copy of PyObject_CallFunction.  | 
968  |  |  * This function is kept for backward compatibility.  | 
969  |  |  */  | 
970  |  | PyObject *  | 
971  |  | PyEval_CallFunction(PyObject *callable, const char *format, ...)  | 
972  | 0  | { | 
973  | 0  |     va_list va;  | 
974  | 0  |     PyObject *result;  | 
975  |  | 
  | 
976  | 0  |     va_start(va, format);  | 
977  | 0  |     result = _PyObject_CallFunctionVa(callable, format, va, 0);  | 
978  | 0  |     va_end(va);  | 
979  |  | 
  | 
980  | 0  |     return result;  | 
981  | 0  | }  | 
982  |  |  | 
983  |  |  | 
984  |  | PyObject *  | 
985  |  | _PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)  | 
986  | 1.24k  | { | 
987  | 1.24k  |     va_list va;  | 
988  | 1.24k  |     PyObject *result;  | 
989  |  |  | 
990  | 1.24k  |     va_start(va, format);  | 
991  | 1.24k  |     result = _PyObject_CallFunctionVa(callable, format, va, 1);  | 
992  | 1.24k  |     va_end(va);  | 
993  |  |  | 
994  | 1.24k  |     return result;  | 
995  | 1.24k  | }  | 
996  |  |  | 
997  |  |  | 
998  |  | static PyObject*  | 
999  |  | callmethod(PyObject* callable, const char *format, va_list va, int is_size_t)  | 
1000  | 2.07k  | { | 
1001  | 2.07k  |     assert(callable != NULL);  | 
1002  |  |  | 
1003  | 2.07k  |     if (!PyCallable_Check(callable)) { | 
1004  | 0  |         PyErr_Format(PyExc_TypeError,  | 
1005  | 0  |                      "attribute of type '%.200s' is not callable",  | 
1006  | 0  |                      Py_TYPE(callable)->tp_name);  | 
1007  | 0  |         return NULL;  | 
1008  | 0  |     }  | 
1009  |  |  | 
1010  | 2.07k  |     return _PyObject_CallFunctionVa(callable, format, va, is_size_t);  | 
1011  | 2.07k  | }  | 
1012  |  |  | 
1013  |  |  | 
1014  |  | PyObject *  | 
1015  |  | PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)  | 
1016  | 358  | { | 
1017  | 358  |     va_list va;  | 
1018  | 358  |     PyObject *callable, *retval;  | 
1019  |  |  | 
1020  | 358  |     if (obj == NULL || name == NULL) { | 
1021  | 0  |         return null_error();  | 
1022  | 0  |     }  | 
1023  |  |  | 
1024  | 358  |     callable = PyObject_GetAttrString(obj, name);  | 
1025  | 358  |     if (callable == NULL)  | 
1026  | 0  |         return NULL;  | 
1027  |  |  | 
1028  | 358  |     va_start(va, format);  | 
1029  | 358  |     retval = callmethod(callable, format, va, 0);  | 
1030  | 358  |     va_end(va);  | 
1031  |  |  | 
1032  | 358  |     Py_DECREF(callable);  | 
1033  | 358  |     return retval;  | 
1034  | 358  | }  | 
1035  |  |  | 
1036  |  |  | 
1037  |  | /* PyEval_CallMethod is exact copy of PyObject_CallMethod.  | 
1038  |  |  * This function is kept for backward compatibility.  | 
1039  |  |  */  | 
1040  |  | PyObject *  | 
1041  |  | PyEval_CallMethod(PyObject *obj, const char *name, const char *format, ...)  | 
1042  | 0  | { | 
1043  | 0  |     va_list va;  | 
1044  | 0  |     PyObject *callable, *retval;  | 
1045  |  | 
  | 
1046  | 0  |     if (obj == NULL || name == NULL) { | 
1047  | 0  |         return null_error();  | 
1048  | 0  |     }  | 
1049  |  |  | 
1050  | 0  |     callable = PyObject_GetAttrString(obj, name);  | 
1051  | 0  |     if (callable == NULL)  | 
1052  | 0  |         return NULL;  | 
1053  |  |  | 
1054  | 0  |     va_start(va, format);  | 
1055  | 0  |     retval = callmethod(callable, format, va, 0);  | 
1056  | 0  |     va_end(va);  | 
1057  |  | 
  | 
1058  | 0  |     Py_DECREF(callable);  | 
1059  | 0  |     return retval;  | 
1060  | 0  | }  | 
1061  |  |  | 
1062  |  |  | 
1063  |  | PyObject *  | 
1064  |  | _PyObject_CallMethodId(PyObject *obj, _Py_Identifier *name,  | 
1065  |  |                        const char *format, ...)  | 
1066  | 1.06k  | { | 
1067  | 1.06k  |     va_list va;  | 
1068  | 1.06k  |     PyObject *callable, *retval;  | 
1069  |  |  | 
1070  | 1.06k  |     if (obj == NULL || name == NULL) { | 
1071  | 0  |         return null_error();  | 
1072  | 0  |     }  | 
1073  |  |  | 
1074  | 1.06k  |     callable = _PyObject_GetAttrId(obj, name);  | 
1075  | 1.06k  |     if (callable == NULL)  | 
1076  | 0  |         return NULL;  | 
1077  |  |  | 
1078  | 1.06k  |     va_start(va, format);  | 
1079  | 1.06k  |     retval = callmethod(callable, format, va, 0);  | 
1080  | 1.06k  |     va_end(va);  | 
1081  |  |  | 
1082  | 1.06k  |     Py_DECREF(callable);  | 
1083  | 1.06k  |     return retval;  | 
1084  | 1.06k  | }  | 
1085  |  |  | 
1086  |  |  | 
1087  |  | PyObject *  | 
1088  |  | _PyObject_CallMethod_SizeT(PyObject *obj, const char *name,  | 
1089  |  |                            const char *format, ...)  | 
1090  | 0  | { | 
1091  | 0  |     va_list va;  | 
1092  | 0  |     PyObject *callable, *retval;  | 
1093  |  | 
  | 
1094  | 0  |     if (obj == NULL || name == NULL) { | 
1095  | 0  |         return null_error();  | 
1096  | 0  |     }  | 
1097  |  |  | 
1098  | 0  |     callable = PyObject_GetAttrString(obj, name);  | 
1099  | 0  |     if (callable == NULL)  | 
1100  | 0  |         return NULL;  | 
1101  |  |  | 
1102  | 0  |     va_start(va, format);  | 
1103  | 0  |     retval = callmethod(callable, format, va, 1);  | 
1104  | 0  |     va_end(va);  | 
1105  |  | 
  | 
1106  | 0  |     Py_DECREF(callable);  | 
1107  | 0  |     return retval;  | 
1108  | 0  | }  | 
1109  |  |  | 
1110  |  |  | 
1111  |  | PyObject *  | 
1112  |  | _PyObject_CallMethodId_SizeT(PyObject *obj, _Py_Identifier *name,  | 
1113  |  |                              const char *format, ...)  | 
1114  | 659  | { | 
1115  | 659  |     va_list va;  | 
1116  | 659  |     PyObject *callable, *retval;  | 
1117  |  |  | 
1118  | 659  |     if (obj == NULL || name == NULL) { | 
1119  | 0  |         return null_error();  | 
1120  | 0  |     }  | 
1121  |  |  | 
1122  | 659  |     callable = _PyObject_GetAttrId(obj, name);  | 
1123  | 659  |     if (callable == NULL) { | 
1124  | 0  |         return NULL;  | 
1125  | 0  |     }  | 
1126  |  |  | 
1127  | 659  |     va_start(va, format);  | 
1128  | 659  |     retval = callmethod(callable, format, va, 1);  | 
1129  | 659  |     va_end(va);  | 
1130  |  |  | 
1131  | 659  |     Py_DECREF(callable);  | 
1132  | 659  |     return retval;  | 
1133  | 659  | }  | 
1134  |  |  | 
1135  |  |  | 
1136  |  | /* --- Call with "..." arguments ---------------------------------- */  | 
1137  |  |  | 
1138  |  | static PyObject *  | 
1139  |  | object_vacall(PyObject *base, PyObject *callable, va_list vargs)  | 
1140  | 6.71k  | { | 
1141  | 6.71k  |     PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];  | 
1142  | 6.71k  |     PyObject **stack;  | 
1143  | 6.71k  |     Py_ssize_t nargs;  | 
1144  | 6.71k  |     PyObject *result;  | 
1145  | 6.71k  |     Py_ssize_t i;  | 
1146  | 6.71k  |     va_list countva;  | 
1147  |  |  | 
1148  | 6.71k  |     if (callable == NULL) { | 
1149  | 0  |         return null_error();  | 
1150  | 0  |     }  | 
1151  |  |  | 
1152  |  |     /* Count the number of arguments */  | 
1153  | 6.71k  |     va_copy(countva, vargs);  | 
1154  | 6.71k  |     nargs = base ? 1 : 0;  | 
1155  | 12.1k  |     while (1) { | 
1156  | 12.1k  |         PyObject *arg = va_arg(countva, PyObject *);  | 
1157  | 12.1k  |         if (arg == NULL) { | 
1158  | 6.71k  |             break;  | 
1159  | 6.71k  |         }  | 
1160  | 5.47k  |         nargs++;  | 
1161  | 5.47k  |     }  | 
1162  | 6.71k  |     va_end(countva);  | 
1163  |  |  | 
1164  |  |     /* Copy arguments */  | 
1165  | 6.71k  |     if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) { | 
1166  | 6.71k  |         stack = small_stack;  | 
1167  | 6.71k  |     }  | 
1168  | 0  |     else { | 
1169  | 0  |         stack = PyMem_Malloc(nargs * sizeof(stack[0]));  | 
1170  | 0  |         if (stack == NULL) { | 
1171  | 0  |             PyErr_NoMemory();  | 
1172  | 0  |             return NULL;  | 
1173  | 0  |         }  | 
1174  | 0  |     }  | 
1175  |  |  | 
1176  | 6.71k  |     i = 0;  | 
1177  | 6.71k  |     if (base) { | 
1178  | 1.97k  |         stack[i++] = base;  | 
1179  | 1.97k  |     }  | 
1180  |  |  | 
1181  | 12.1k  |     for (; i < nargs; ++i) { | 
1182  | 5.47k  |         stack[i] = va_arg(vargs, PyObject *);  | 
1183  | 5.47k  |     }  | 
1184  |  |  | 
1185  |  |     /* Call the function */  | 
1186  | 6.71k  |     result = _PyObject_FastCall(callable, stack, nargs);  | 
1187  |  |  | 
1188  | 6.71k  |     if (stack != small_stack) { | 
1189  | 0  |         PyMem_Free(stack);  | 
1190  | 0  |     }  | 
1191  | 6.71k  |     return result;  | 
1192  | 6.71k  | }  | 
1193  |  |  | 
1194  |  |  | 
1195  |  | /* Private API for the LOAD_METHOD opcode. */  | 
1196  |  | extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);  | 
1197  |  |  | 
1198  |  | PyObject *  | 
1199  |  | PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ...)  | 
1200  | 1.93k  | { | 
1201  | 1.93k  |     if (obj == NULL || name == NULL) { | 
1202  | 0  |         return null_error();  | 
1203  | 0  |     }  | 
1204  |  |  | 
1205  | 1.93k  |     PyObject *callable = NULL;  | 
1206  | 1.93k  |     int is_method = _PyObject_GetMethod(obj, name, &callable);  | 
1207  | 1.93k  |     if (callable == NULL) { | 
1208  | 0  |         return NULL;  | 
1209  | 0  |     }  | 
1210  | 1.93k  |     obj = is_method ? obj : NULL;  | 
1211  |  |  | 
1212  | 1.93k  |     va_list vargs;  | 
1213  | 1.93k  |     va_start(vargs, name);  | 
1214  | 1.93k  |     PyObject *result = object_vacall(obj, callable, vargs);  | 
1215  | 1.93k  |     va_end(vargs);  | 
1216  |  |  | 
1217  | 1.93k  |     Py_DECREF(callable);  | 
1218  | 1.93k  |     return result;  | 
1219  | 1.93k  | }  | 
1220  |  |  | 
1221  |  |  | 
1222  |  | PyObject *  | 
1223  |  | _PyObject_CallMethodIdObjArgs(PyObject *obj,  | 
1224  |  |                               struct _Py_Identifier *name, ...)  | 
1225  | 1.27k  | { | 
1226  | 1.27k  |     if (obj == NULL || name == NULL) { | 
1227  | 0  |         return null_error();  | 
1228  | 0  |     }  | 
1229  |  |  | 
1230  | 1.27k  |     PyObject *oname = _PyUnicode_FromId(name); /* borrowed */  | 
1231  | 1.27k  |     if (!oname) { | 
1232  | 0  |         return NULL;  | 
1233  | 0  |     }  | 
1234  |  |  | 
1235  | 1.27k  |     PyObject *callable = NULL;  | 
1236  | 1.27k  |     int is_method = _PyObject_GetMethod(obj, oname, &callable);  | 
1237  | 1.27k  |     if (callable == NULL) { | 
1238  | 0  |         return NULL;  | 
1239  | 0  |     }  | 
1240  | 1.27k  |     obj = is_method ? obj : NULL;  | 
1241  |  |  | 
1242  | 1.27k  |     va_list vargs;  | 
1243  | 1.27k  |     va_start(vargs, name);  | 
1244  | 1.27k  |     PyObject *result = object_vacall(obj, callable, vargs);  | 
1245  | 1.27k  |     va_end(vargs);  | 
1246  |  |  | 
1247  | 1.27k  |     Py_DECREF(callable);  | 
1248  | 1.27k  |     return result;  | 
1249  | 1.27k  | }  | 
1250  |  |  | 
1251  |  |  | 
1252  |  | PyObject *  | 
1253  |  | PyObject_CallFunctionObjArgs(PyObject *callable, ...)  | 
1254  | 3.50k  | { | 
1255  | 3.50k  |     va_list vargs;  | 
1256  | 3.50k  |     PyObject *result;  | 
1257  |  |  | 
1258  | 3.50k  |     va_start(vargs, callable);  | 
1259  | 3.50k  |     result = object_vacall(NULL, callable, vargs);  | 
1260  | 3.50k  |     va_end(vargs);  | 
1261  |  |  | 
1262  | 3.50k  |     return result;  | 
1263  | 3.50k  | }  | 
1264  |  |  | 
1265  |  |  | 
1266  |  | /* --- PyStack functions ------------------------------------------ */  | 
1267  |  |  | 
1268  |  | PyObject *  | 
1269  |  | _PyStack_AsDict(PyObject *const *values, PyObject *kwnames)  | 
1270  | 872  | { | 
1271  | 872  |     Py_ssize_t nkwargs;  | 
1272  | 872  |     PyObject *kwdict;  | 
1273  | 872  |     Py_ssize_t i;  | 
1274  |  |  | 
1275  | 872  |     assert(kwnames != NULL);  | 
1276  | 872  |     nkwargs = PyTuple_GET_SIZE(kwnames);  | 
1277  | 872  |     kwdict = _PyDict_NewPresized(nkwargs);  | 
1278  | 872  |     if (kwdict == NULL) { | 
1279  | 0  |         return NULL;  | 
1280  | 0  |     }  | 
1281  |  |  | 
1282  | 2.17k  |     for (i = 0; i < nkwargs; i++) { | 
1283  | 1.30k  |         PyObject *key = PyTuple_GET_ITEM(kwnames, i);  | 
1284  | 1.30k  |         PyObject *value = *values++;  | 
1285  |  |         /* If key already exists, replace it with the new value */  | 
1286  | 1.30k  |         if (PyDict_SetItem(kwdict, key, value)) { | 
1287  | 0  |             Py_DECREF(kwdict);  | 
1288  | 0  |             return NULL;  | 
1289  | 0  |         }  | 
1290  | 1.30k  |     }  | 
1291  | 872  |     return kwdict;  | 
1292  | 872  | }  | 
1293  |  |  | 
1294  |  |  | 
1295  |  | int  | 
1296  |  | _PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs,  | 
1297  |  |                     PyObject *const **p_stack, PyObject **p_kwnames)  | 
1298  | 2.27k  | { | 
1299  | 2.27k  |     PyObject **stack, **kwstack;  | 
1300  | 2.27k  |     Py_ssize_t nkwargs;  | 
1301  | 2.27k  |     Py_ssize_t pos, i;  | 
1302  | 2.27k  |     PyObject *key, *value;  | 
1303  | 2.27k  |     PyObject *kwnames;  | 
1304  |  |  | 
1305  | 2.27k  |     assert(nargs >= 0);  | 
1306  | 2.27k  |     assert(kwargs == NULL || PyDict_CheckExact(kwargs));  | 
1307  |  |  | 
1308  | 2.27k  |     if (kwargs == NULL || (nkwargs = PyDict_GET_SIZE(kwargs)) == 0) { | 
1309  | 1.66k  |         *p_stack = args;  | 
1310  | 1.66k  |         *p_kwnames = NULL;  | 
1311  | 1.66k  |         return 0;  | 
1312  | 1.66k  |     }  | 
1313  |  |  | 
1314  | 608  |     if ((size_t)nargs > PY_SSIZE_T_MAX / sizeof(stack[0]) - (size_t)nkwargs) { | 
1315  | 0  |         PyErr_NoMemory();  | 
1316  | 0  |         return -1;  | 
1317  | 0  |     }  | 
1318  |  |  | 
1319  | 608  |     stack = PyMem_Malloc((nargs + nkwargs) * sizeof(stack[0]));  | 
1320  | 608  |     if (stack == NULL) { | 
1321  | 0  |         PyErr_NoMemory();  | 
1322  | 0  |         return -1;  | 
1323  | 0  |     }  | 
1324  |  |  | 
1325  | 608  |     kwnames = PyTuple_New(nkwargs);  | 
1326  | 608  |     if (kwnames == NULL) { | 
1327  | 0  |         PyMem_Free(stack);  | 
1328  | 0  |         return -1;  | 
1329  | 0  |     }  | 
1330  |  |  | 
1331  |  |     /* Copy positional arguments */  | 
1332  | 2.38k  |     for (i = 0; i < nargs; i++) { | 
1333  | 1.78k  |         Py_INCREF(args[i]);  | 
1334  | 1.78k  |         stack[i] = args[i];  | 
1335  | 1.78k  |     }  | 
1336  |  |  | 
1337  | 608  |     kwstack = stack + nargs;  | 
1338  | 608  |     pos = i = 0;  | 
1339  |  |     /* This loop doesn't support lookup function mutating the dictionary  | 
1340  |  |        to change its size. It's a deliberate choice for speed, this function is  | 
1341  |  |        called in the performance critical hot code. */  | 
1342  | 1.62k  |     while (PyDict_Next(kwargs, &pos, &key, &value)) { | 
1343  | 1.01k  |         Py_INCREF(key);  | 
1344  | 1.01k  |         Py_INCREF(value);  | 
1345  | 1.01k  |         PyTuple_SET_ITEM(kwnames, i, key);  | 
1346  | 1.01k  |         kwstack[i] = value;  | 
1347  | 1.01k  |         i++;  | 
1348  | 1.01k  |     }  | 
1349  |  |  | 
1350  | 608  |     *p_stack = stack;  | 
1351  | 608  |     *p_kwnames = kwnames;  | 
1352  | 608  |     return 0;  | 
1353  | 608  | }  |