Branch data Line data Source code
1 : : /* Execute compiled code */
2 : :
3 : : #define _PY_INTERPRETER
4 : :
5 : : #include "Python.h"
6 : : #include "pycore_abstract.h" // _PyIndex_Check()
7 : : #include "pycore_call.h" // _PyObject_FastCallDictTstate()
8 : : #include "pycore_ceval.h" // _PyEval_SignalAsyncExc()
9 : : #include "pycore_code.h"
10 : : #include "pycore_function.h"
11 : : #include "pycore_intrinsics.h"
12 : : #include "pycore_long.h" // _PyLong_GetZero()
13 : : #include "pycore_object.h" // _PyObject_GC_TRACK()
14 : : #include "pycore_moduleobject.h" // PyModuleObject
15 : : #include "pycore_opcode.h" // EXTRA_CASES
16 : : #include "pycore_pyerrors.h" // _PyErr_GetRaisedException()
17 : : #include "pycore_pymem.h" // _PyMem_IsPtrFreed()
18 : : #include "pycore_pystate.h" // _PyInterpreterState_GET()
19 : : #include "pycore_range.h" // _PyRangeIterObject
20 : : #include "pycore_sliceobject.h" // _PyBuildSlice_ConsumeRefs
21 : : #include "pycore_sysmodule.h" // _PySys_Audit()
22 : : #include "pycore_tuple.h" // _PyTuple_ITEMS()
23 : : #include "pycore_emscripten_signal.h" // _Py_CHECK_EMSCRIPTEN_SIGNALS
24 : :
25 : : #include "pycore_dict.h"
26 : : #include "dictobject.h"
27 : : #include "pycore_frame.h"
28 : : #include "opcode.h"
29 : : #include "pydtrace.h"
30 : : #include "setobject.h"
31 : : #include "structmember.h" // struct PyMemberDef, T_OFFSET_EX
32 : :
33 : : #include <ctype.h>
34 : : #include <stdbool.h>
35 : :
36 : : #ifdef Py_DEBUG
37 : : /* For debugging the interpreter: */
38 : : # define LLTRACE 1 /* Low-level trace feature */
39 : : #endif
40 : :
41 : : #if !defined(Py_BUILD_CORE)
42 : : # error "ceval.c must be build with Py_BUILD_CORE define for best performance"
43 : : #endif
44 : :
45 : : #if !defined(Py_DEBUG) && !defined(Py_TRACE_REFS)
46 : : // GH-89279: The MSVC compiler does not inline these static inline functions
47 : : // in PGO build in _PyEval_EvalFrameDefault(), because this function is over
48 : : // the limit of PGO, and that limit cannot be configured.
49 : : // Define them as macros to make sure that they are always inlined by the
50 : : // preprocessor.
51 : :
52 : : #undef Py_DECREF
53 : : #define Py_DECREF(arg) \
54 : : do { \
55 : : _Py_DECREF_STAT_INC(); \
56 : : PyObject *op = _PyObject_CAST(arg); \
57 : : if (--op->ob_refcnt == 0) { \
58 : : destructor dealloc = Py_TYPE(op)->tp_dealloc; \
59 : : (*dealloc)(op); \
60 : : } \
61 : : } while (0)
62 : :
63 : : #undef Py_XDECREF
64 : : #define Py_XDECREF(arg) \
65 : : do { \
66 : : PyObject *xop = _PyObject_CAST(arg); \
67 : : if (xop != NULL) { \
68 : : Py_DECREF(xop); \
69 : : } \
70 : : } while (0)
71 : :
72 : : #undef Py_IS_TYPE
73 : : #define Py_IS_TYPE(ob, type) \
74 : : (_PyObject_CAST(ob)->ob_type == (type))
75 : :
76 : : #undef _Py_DECREF_SPECIALIZED
77 : : #define _Py_DECREF_SPECIALIZED(arg, dealloc) \
78 : : do { \
79 : : _Py_DECREF_STAT_INC(); \
80 : : PyObject *op = _PyObject_CAST(arg); \
81 : : if (--op->ob_refcnt == 0) { \
82 : : destructor d = (destructor)(dealloc); \
83 : : d(op); \
84 : : } \
85 : : } while (0)
86 : : #endif
87 : :
88 : : // GH-89279: Similar to above, force inlining by using a macro.
89 : : #if defined(_MSC_VER) && SIZEOF_INT == 4
90 : : #define _Py_atomic_load_relaxed_int32(ATOMIC_VAL) (assert(sizeof((ATOMIC_VAL)->_value) == 4), *((volatile int*)&((ATOMIC_VAL)->_value)))
91 : : #else
92 : : #define _Py_atomic_load_relaxed_int32(ATOMIC_VAL) _Py_atomic_load_relaxed(ATOMIC_VAL)
93 : : #endif
94 : :
95 : : /* Forward declarations */
96 : : static PyObject *trace_call_function(
97 : : PyThreadState *tstate, PyObject *callable, PyObject **stack,
98 : : Py_ssize_t oparg, PyObject *kwnames);
99 : : static PyObject * do_call_core(
100 : : PyThreadState *tstate, PyObject *func,
101 : : PyObject *callargs, PyObject *kwdict, int use_tracing);
102 : :
103 : : #ifdef LLTRACE
104 : : static void
105 : : dump_stack(_PyInterpreterFrame *frame, PyObject **stack_pointer)
106 : : {
107 : : PyObject **stack_base = _PyFrame_Stackbase(frame);
108 : : PyObject *exc = PyErr_GetRaisedException();
109 : : printf(" stack=[");
110 : : for (PyObject **ptr = stack_base; ptr < stack_pointer; ptr++) {
111 : : if (ptr != stack_base) {
112 : : printf(", ");
113 : : }
114 : : if (PyObject_Print(*ptr, stdout, 0) != 0) {
115 : : PyErr_Clear();
116 : : printf("<%s object at %p>",
117 : : Py_TYPE(*ptr)->tp_name, (void *)(*ptr));
118 : : }
119 : : }
120 : : printf("]\n");
121 : : fflush(stdout);
122 : : PyErr_SetRaisedException(exc);
123 : : }
124 : :
125 : : static void
126 : : lltrace_instruction(_PyInterpreterFrame *frame,
127 : : PyObject **stack_pointer,
128 : : _Py_CODEUNIT *next_instr)
129 : : {
130 : : /* This dump_stack() operation is risky, since the repr() of some
131 : : objects enters the interpreter recursively. It is also slow.
132 : : So you might want to comment it out. */
133 : : dump_stack(frame, stack_pointer);
134 : : int oparg = next_instr->op.arg;
135 : : int opcode = next_instr->op.code;
136 : : const char *opname = _PyOpcode_OpName[opcode];
137 : : assert(opname != NULL);
138 : : int offset = (int)(next_instr - _PyCode_CODE(frame->f_code));
139 : : if (HAS_ARG((int)_PyOpcode_Deopt[opcode])) {
140 : : printf("%d: %s %d\n", offset * 2, opname, oparg);
141 : : }
142 : : else {
143 : : printf("%d: %s\n", offset * 2, opname);
144 : : }
145 : : fflush(stdout);
146 : : }
147 : : static void
148 : : lltrace_resume_frame(_PyInterpreterFrame *frame)
149 : : {
150 : : PyObject *fobj = frame->f_funcobj;
151 : : if (frame->owner == FRAME_OWNED_BY_CSTACK ||
152 : : fobj == NULL ||
153 : : !PyFunction_Check(fobj)
154 : : ) {
155 : : printf("\nResuming frame.\n");
156 : : return;
157 : : }
158 : : PyFunctionObject *f = (PyFunctionObject *)fobj;
159 : : PyObject *exc = PyErr_GetRaisedException();
160 : : PyObject *name = f->func_qualname;
161 : : if (name == NULL) {
162 : : name = f->func_name;
163 : : }
164 : : printf("\nResuming frame");
165 : : if (name) {
166 : : printf(" for ");
167 : : if (PyObject_Print(name, stdout, 0) < 0) {
168 : : PyErr_Clear();
169 : : }
170 : : }
171 : : if (f->func_module) {
172 : : printf(" in module ");
173 : : if (PyObject_Print(f->func_module, stdout, 0) < 0) {
174 : : PyErr_Clear();
175 : : }
176 : : }
177 : : printf("\n");
178 : : fflush(stdout);
179 : : PyErr_SetRaisedException(exc);
180 : : }
181 : : #endif
182 : : static int call_trace(Py_tracefunc, PyObject *,
183 : : PyThreadState *, _PyInterpreterFrame *,
184 : : int, PyObject *);
185 : : static int call_trace_protected(Py_tracefunc, PyObject *,
186 : : PyThreadState *, _PyInterpreterFrame *,
187 : : int, PyObject *);
188 : : static void call_exc_trace(Py_tracefunc, PyObject *,
189 : : PyThreadState *, _PyInterpreterFrame *);
190 : : static int maybe_call_line_trace(Py_tracefunc, PyObject *,
191 : : PyThreadState *, _PyInterpreterFrame *, int);
192 : : static void maybe_dtrace_line(_PyInterpreterFrame *, PyTraceInfo *, int);
193 : : static void dtrace_function_entry(_PyInterpreterFrame *);
194 : : static void dtrace_function_return(_PyInterpreterFrame *);
195 : :
196 : : static PyObject * import_name(PyThreadState *, _PyInterpreterFrame *,
197 : : PyObject *, PyObject *, PyObject *);
198 : : static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
199 : : static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
200 : : static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
201 : : static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
202 : : static int check_except_type_valid(PyThreadState *tstate, PyObject* right);
203 : : static int check_except_star_type_valid(PyThreadState *tstate, PyObject* right);
204 : : static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
205 : : static void format_awaitable_error(PyThreadState *, PyTypeObject *, int);
206 : : static int get_exception_handler(PyCodeObject *, int, int*, int*, int*);
207 : : static _PyInterpreterFrame *
208 : : _PyEvalFramePushAndInit(PyThreadState *tstate, PyFunctionObject *func,
209 : : PyObject *locals, PyObject* const* args,
210 : : size_t argcount, PyObject *kwnames);
211 : : static void
212 : : _PyEvalFrameClearAndPop(PyThreadState *tstate, _PyInterpreterFrame *frame);
213 : :
214 : : #define UNBOUNDLOCAL_ERROR_MSG \
215 : : "cannot access local variable '%s' where it is not associated with a value"
216 : : #define UNBOUNDFREE_ERROR_MSG \
217 : : "cannot access free variable '%s' where it is not associated with a" \
218 : : " value in enclosing scope"
219 : :
220 : : #ifndef NDEBUG
221 : : /* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and
222 : : PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen
223 : : when a thread continues to run after Python finalization, especially
224 : : daemon threads. */
225 : : static int
226 : : is_tstate_valid(PyThreadState *tstate)
227 : : {
228 : : assert(!_PyMem_IsPtrFreed(tstate));
229 : : assert(!_PyMem_IsPtrFreed(tstate->interp));
230 : : return 1;
231 : : }
232 : : #endif
233 : :
234 : :
235 : : #ifdef HAVE_ERRNO_H
236 : : #include <errno.h>
237 : : #endif
238 : :
239 : : int
240 : 0 : Py_GetRecursionLimit(void)
241 : : {
242 : 0 : PyInterpreterState *interp = _PyInterpreterState_GET();
243 : 0 : return interp->ceval.recursion_limit;
244 : : }
245 : :
246 : : void
247 : 0 : Py_SetRecursionLimit(int new_limit)
248 : : {
249 : 0 : PyInterpreterState *interp = _PyInterpreterState_GET();
250 : 0 : interp->ceval.recursion_limit = new_limit;
251 [ # # ]: 0 : for (PyThreadState *p = interp->threads.head; p != NULL; p = p->next) {
252 : 0 : int depth = p->py_recursion_limit - p->py_recursion_remaining;
253 : 0 : p->py_recursion_limit = new_limit;
254 : 0 : p->py_recursion_remaining = new_limit - depth;
255 : : }
256 : 0 : }
257 : :
258 : : /* The function _Py_EnterRecursiveCallTstate() only calls _Py_CheckRecursiveCall()
259 : : if the recursion_depth reaches recursion_limit. */
260 : : int
261 : 0 : _Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
262 : : {
263 : : #ifdef USE_STACKCHECK
264 : : if (PyOS_CheckStack()) {
265 : : ++tstate->c_recursion_remaining;
266 : : _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
267 : : return -1;
268 : : }
269 : : #endif
270 [ # # ]: 0 : if (tstate->recursion_headroom) {
271 [ # # ]: 0 : if (tstate->c_recursion_remaining < -50) {
272 : : /* Overflowing while handling an overflow. Give up. */
273 : 0 : Py_FatalError("Cannot recover from stack overflow.");
274 : : }
275 : : }
276 : : else {
277 [ # # ]: 0 : if (tstate->c_recursion_remaining <= 0) {
278 : 0 : tstate->recursion_headroom++;
279 : 0 : _PyErr_Format(tstate, PyExc_RecursionError,
280 : : "maximum recursion depth exceeded%s",
281 : : where);
282 : 0 : tstate->recursion_headroom--;
283 : 0 : ++tstate->c_recursion_remaining;
284 : 0 : return -1;
285 : : }
286 : : }
287 : 0 : return 0;
288 : : }
289 : :
290 : :
291 : : static const binaryfunc binary_ops[] = {
292 : : [NB_ADD] = PyNumber_Add,
293 : : [NB_AND] = PyNumber_And,
294 : : [NB_FLOOR_DIVIDE] = PyNumber_FloorDivide,
295 : : [NB_LSHIFT] = PyNumber_Lshift,
296 : : [NB_MATRIX_MULTIPLY] = PyNumber_MatrixMultiply,
297 : : [NB_MULTIPLY] = PyNumber_Multiply,
298 : : [NB_REMAINDER] = PyNumber_Remainder,
299 : : [NB_OR] = PyNumber_Or,
300 : : [NB_POWER] = _PyNumber_PowerNoMod,
301 : : [NB_RSHIFT] = PyNumber_Rshift,
302 : : [NB_SUBTRACT] = PyNumber_Subtract,
303 : : [NB_TRUE_DIVIDE] = PyNumber_TrueDivide,
304 : : [NB_XOR] = PyNumber_Xor,
305 : : [NB_INPLACE_ADD] = PyNumber_InPlaceAdd,
306 : : [NB_INPLACE_AND] = PyNumber_InPlaceAnd,
307 : : [NB_INPLACE_FLOOR_DIVIDE] = PyNumber_InPlaceFloorDivide,
308 : : [NB_INPLACE_LSHIFT] = PyNumber_InPlaceLshift,
309 : : [NB_INPLACE_MATRIX_MULTIPLY] = PyNumber_InPlaceMatrixMultiply,
310 : : [NB_INPLACE_MULTIPLY] = PyNumber_InPlaceMultiply,
311 : : [NB_INPLACE_REMAINDER] = PyNumber_InPlaceRemainder,
312 : : [NB_INPLACE_OR] = PyNumber_InPlaceOr,
313 : : [NB_INPLACE_POWER] = _PyNumber_InPlacePowerNoMod,
314 : : [NB_INPLACE_RSHIFT] = PyNumber_InPlaceRshift,
315 : : [NB_INPLACE_SUBTRACT] = PyNumber_InPlaceSubtract,
316 : : [NB_INPLACE_TRUE_DIVIDE] = PyNumber_InPlaceTrueDivide,
317 : : [NB_INPLACE_XOR] = PyNumber_InPlaceXor,
318 : : };
319 : :
320 : :
321 : : // PEP 634: Structural Pattern Matching
322 : :
323 : :
324 : : // Return a tuple of values corresponding to keys, with error checks for
325 : : // duplicate/missing keys.
326 : : static PyObject*
327 : 0 : match_keys(PyThreadState *tstate, PyObject *map, PyObject *keys)
328 : : {
329 : : assert(PyTuple_CheckExact(keys));
330 : 0 : Py_ssize_t nkeys = PyTuple_GET_SIZE(keys);
331 [ # # ]: 0 : if (!nkeys) {
332 : : // No keys means no items.
333 : 0 : return PyTuple_New(0);
334 : : }
335 : 0 : PyObject *seen = NULL;
336 : 0 : PyObject *dummy = NULL;
337 : 0 : PyObject *values = NULL;
338 : 0 : PyObject *get = NULL;
339 : : // We use the two argument form of map.get(key, default) for two reasons:
340 : : // - Atomically check for a key and get its value without error handling.
341 : : // - Don't cause key creation or resizing in dict subclasses like
342 : : // collections.defaultdict that define __missing__ (or similar).
343 : 0 : int meth_found = _PyObject_GetMethod(map, &_Py_ID(get), &get);
344 [ # # ]: 0 : if (get == NULL) {
345 : 0 : goto fail;
346 : : }
347 : 0 : seen = PySet_New(NULL);
348 [ # # ]: 0 : if (seen == NULL) {
349 : 0 : goto fail;
350 : : }
351 : : // dummy = object()
352 : 0 : dummy = _PyObject_CallNoArgs((PyObject *)&PyBaseObject_Type);
353 [ # # ]: 0 : if (dummy == NULL) {
354 : 0 : goto fail;
355 : : }
356 : 0 : values = PyTuple_New(nkeys);
357 [ # # ]: 0 : if (values == NULL) {
358 : 0 : goto fail;
359 : : }
360 [ # # ]: 0 : for (Py_ssize_t i = 0; i < nkeys; i++) {
361 : 0 : PyObject *key = PyTuple_GET_ITEM(keys, i);
362 [ # # # # ]: 0 : if (PySet_Contains(seen, key) || PySet_Add(seen, key)) {
363 [ # # ]: 0 : if (!_PyErr_Occurred(tstate)) {
364 : : // Seen it before!
365 : 0 : _PyErr_Format(tstate, PyExc_ValueError,
366 : : "mapping pattern checks duplicate key (%R)", key);
367 : : }
368 : 0 : goto fail;
369 : : }
370 : 0 : PyObject *args[] = { map, key, dummy };
371 : 0 : PyObject *value = NULL;
372 [ # # ]: 0 : if (meth_found) {
373 : 0 : value = PyObject_Vectorcall(get, args, 3, NULL);
374 : : }
375 : : else {
376 : 0 : value = PyObject_Vectorcall(get, &args[1], 2, NULL);
377 : : }
378 [ # # ]: 0 : if (value == NULL) {
379 : 0 : goto fail;
380 : : }
381 [ # # ]: 0 : if (value == dummy) {
382 : : // key not in map!
383 [ # # ]: 0 : Py_DECREF(value);
384 [ # # ]: 0 : Py_DECREF(values);
385 : : // Return None:
386 : 0 : values = Py_NewRef(Py_None);
387 : 0 : goto done;
388 : : }
389 : 0 : PyTuple_SET_ITEM(values, i, value);
390 : : }
391 : : // Success:
392 : 0 : done:
393 [ # # ]: 0 : Py_DECREF(get);
394 [ # # ]: 0 : Py_DECREF(seen);
395 [ # # ]: 0 : Py_DECREF(dummy);
396 : 0 : return values;
397 : 0 : fail:
398 [ # # # # ]: 0 : Py_XDECREF(get);
399 [ # # # # ]: 0 : Py_XDECREF(seen);
400 [ # # # # ]: 0 : Py_XDECREF(dummy);
401 [ # # # # ]: 0 : Py_XDECREF(values);
402 : 0 : return NULL;
403 : : }
404 : :
405 : : // Extract a named attribute from the subject, with additional bookkeeping to
406 : : // raise TypeErrors for repeated lookups. On failure, return NULL (with no
407 : : // error set). Use _PyErr_Occurred(tstate) to disambiguate.
408 : : static PyObject*
409 : 9 : match_class_attr(PyThreadState *tstate, PyObject *subject, PyObject *type,
410 : : PyObject *name, PyObject *seen)
411 : : {
412 : : assert(PyUnicode_CheckExact(name));
413 : : assert(PySet_CheckExact(seen));
414 [ + - - + ]: 9 : if (PySet_Contains(seen, name) || PySet_Add(seen, name)) {
415 [ # # ]: 0 : if (!_PyErr_Occurred(tstate)) {
416 : : // Seen it before!
417 : 0 : _PyErr_Format(tstate, PyExc_TypeError,
418 : : "%s() got multiple sub-patterns for attribute %R",
419 : : ((PyTypeObject*)type)->tp_name, name);
420 : : }
421 : 0 : return NULL;
422 : : }
423 : 9 : PyObject *attr = PyObject_GetAttr(subject, name);
424 [ - + - - ]: 9 : if (attr == NULL && _PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
425 : 0 : _PyErr_Clear(tstate);
426 : : }
427 : 9 : return attr;
428 : : }
429 : :
430 : : // On success (match), return a tuple of extracted attributes. On failure (no
431 : : // match), return NULL. Use _PyErr_Occurred(tstate) to disambiguate.
432 : : static PyObject*
433 : 26 : match_class(PyThreadState *tstate, PyObject *subject, PyObject *type,
434 : : Py_ssize_t nargs, PyObject *kwargs)
435 : : {
436 [ - + ]: 26 : if (!PyType_Check(type)) {
437 : 0 : const char *e = "called match pattern must be a type";
438 : 0 : _PyErr_Format(tstate, PyExc_TypeError, e);
439 : 0 : return NULL;
440 : : }
441 : : assert(PyTuple_CheckExact(kwargs));
442 : : // First, an isinstance check:
443 [ + + ]: 26 : if (PyObject_IsInstance(subject, type) <= 0) {
444 : 16 : return NULL;
445 : : }
446 : : // So far so good:
447 : 10 : PyObject *seen = PySet_New(NULL);
448 [ - + ]: 10 : if (seen == NULL) {
449 : 0 : return NULL;
450 : : }
451 : 10 : PyObject *attrs = PyList_New(0);
452 [ - + ]: 10 : if (attrs == NULL) {
453 [ # # ]: 0 : Py_DECREF(seen);
454 : 0 : return NULL;
455 : : }
456 : : // NOTE: From this point on, goto fail on failure:
457 : 10 : PyObject *match_args = NULL;
458 : : // First, the positional subpatterns:
459 [ + + ]: 10 : if (nargs) {
460 : 9 : int match_self = 0;
461 : 9 : match_args = PyObject_GetAttrString(type, "__match_args__");
462 [ + - ]: 9 : if (match_args) {
463 [ - + ]: 9 : if (!PyTuple_CheckExact(match_args)) {
464 : 0 : const char *e = "%s.__match_args__ must be a tuple (got %s)";
465 : 0 : _PyErr_Format(tstate, PyExc_TypeError, e,
466 : : ((PyTypeObject *)type)->tp_name,
467 : 0 : Py_TYPE(match_args)->tp_name);
468 : 0 : goto fail;
469 : : }
470 : : }
471 [ # # ]: 0 : else if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
472 : 0 : _PyErr_Clear(tstate);
473 : : // _Py_TPFLAGS_MATCH_SELF is only acknowledged if the type does not
474 : : // define __match_args__. This is natural behavior for subclasses:
475 : : // it's as if __match_args__ is some "magic" value that is lost as
476 : : // soon as they redefine it.
477 : 0 : match_args = PyTuple_New(0);
478 : 0 : match_self = PyType_HasFeature((PyTypeObject*)type,
479 : : _Py_TPFLAGS_MATCH_SELF);
480 : : }
481 : : else {
482 : 0 : goto fail;
483 : : }
484 : : assert(PyTuple_CheckExact(match_args));
485 [ + - ]: 9 : Py_ssize_t allowed = match_self ? 1 : PyTuple_GET_SIZE(match_args);
486 [ - + ]: 9 : if (allowed < nargs) {
487 [ # # ]: 0 : const char *plural = (allowed == 1) ? "" : "s";
488 : 0 : _PyErr_Format(tstate, PyExc_TypeError,
489 : : "%s() accepts %d positional sub-pattern%s (%d given)",
490 : : ((PyTypeObject*)type)->tp_name,
491 : : allowed, plural, nargs);
492 : 0 : goto fail;
493 : : }
494 [ - + ]: 9 : if (match_self) {
495 : : // Easy. Copy the subject itself, and move on to kwargs.
496 : 0 : PyList_Append(attrs, subject);
497 : : }
498 : : else {
499 [ + + ]: 18 : for (Py_ssize_t i = 0; i < nargs; i++) {
500 : 9 : PyObject *name = PyTuple_GET_ITEM(match_args, i);
501 [ - + ]: 9 : if (!PyUnicode_CheckExact(name)) {
502 : 0 : _PyErr_Format(tstate, PyExc_TypeError,
503 : : "__match_args__ elements must be strings "
504 : 0 : "(got %s)", Py_TYPE(name)->tp_name);
505 : 0 : goto fail;
506 : : }
507 : 9 : PyObject *attr = match_class_attr(tstate, subject, type, name,
508 : : seen);
509 [ - + ]: 9 : if (attr == NULL) {
510 : 0 : goto fail;
511 : : }
512 : 9 : PyList_Append(attrs, attr);
513 [ - + ]: 9 : Py_DECREF(attr);
514 : : }
515 : : }
516 [ + - - + ]: 9 : Py_CLEAR(match_args);
517 : : }
518 : : // Finally, the keyword subpatterns:
519 [ - + ]: 10 : for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwargs); i++) {
520 : 0 : PyObject *name = PyTuple_GET_ITEM(kwargs, i);
521 : 0 : PyObject *attr = match_class_attr(tstate, subject, type, name, seen);
522 [ # # ]: 0 : if (attr == NULL) {
523 : 0 : goto fail;
524 : : }
525 : 0 : PyList_Append(attrs, attr);
526 [ # # ]: 0 : Py_DECREF(attr);
527 : : }
528 [ + - ]: 10 : Py_SETREF(attrs, PyList_AsTuple(attrs));
529 [ + - ]: 10 : Py_DECREF(seen);
530 : 10 : return attrs;
531 : 0 : fail:
532 : : // We really don't care whether an error was raised or not... that's our
533 : : // caller's problem. All we know is that the match failed.
534 [ # # # # ]: 0 : Py_XDECREF(match_args);
535 [ # # ]: 0 : Py_DECREF(seen);
536 [ # # ]: 0 : Py_DECREF(attrs);
537 : 0 : return NULL;
538 : : }
539 : :
540 : :
541 : : static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
542 : : static int exception_group_match(
543 : : PyObject* exc_value, PyObject *match_type,
544 : : PyObject **match, PyObject **rest);
545 : :
546 : : static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
547 : :
548 : : PyObject *
549 : 828 : PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
550 : : {
551 : 828 : PyThreadState *tstate = _PyThreadState_GET();
552 [ - + ]: 828 : if (locals == NULL) {
553 : 0 : locals = globals;
554 : : }
555 : 828 : PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
556 [ - + ]: 828 : if (builtins == NULL) {
557 : 0 : return NULL;
558 : : }
559 : 828 : PyFrameConstructor desc = {
560 : : .fc_globals = globals,
561 : : .fc_builtins = builtins,
562 : 828 : .fc_name = ((PyCodeObject *)co)->co_name,
563 : 828 : .fc_qualname = ((PyCodeObject *)co)->co_name,
564 : : .fc_code = co,
565 : : .fc_defaults = NULL,
566 : : .fc_kwdefaults = NULL,
567 : : .fc_closure = NULL
568 : : };
569 : 828 : PyFunctionObject *func = _PyFunction_FromConstructor(&desc);
570 [ - + ]: 828 : if (func == NULL) {
571 : 0 : return NULL;
572 : : }
573 : : EVAL_CALL_STAT_INC(EVAL_CALL_LEGACY);
574 : 828 : PyObject *res = _PyEval_Vector(tstate, func, locals, NULL, 0, NULL);
575 [ + + ]: 828 : Py_DECREF(func);
576 : 828 : return res;
577 : : }
578 : :
579 : :
580 : : /* Interpreter main loop */
581 : :
582 : : PyObject *
583 : 0 : PyEval_EvalFrame(PyFrameObject *f)
584 : : {
585 : : /* Function kept for backward compatibility */
586 : 0 : PyThreadState *tstate = _PyThreadState_GET();
587 : 0 : return _PyEval_EvalFrame(tstate, f->f_frame, 0);
588 : : }
589 : :
590 : : PyObject *
591 : 0 : PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
592 : : {
593 : 0 : PyThreadState *tstate = _PyThreadState_GET();
594 : 0 : return _PyEval_EvalFrame(tstate, f->f_frame, throwflag);
595 : : }
596 : :
597 : : #include "ceval_macros.h"
598 : :
599 : : static int
600 : 0 : trace_function_entry(PyThreadState *tstate, _PyInterpreterFrame *frame)
601 : : {
602 [ # # ]: 0 : if (tstate->c_tracefunc != NULL) {
603 : : /* tstate->c_tracefunc, if defined, is a
604 : : function that will be called on *every* entry
605 : : to a code block. Its return value, if not
606 : : None, is a function that will be called at
607 : : the start of each executed line of code.
608 : : (Actually, the function must return itself
609 : : in order to continue tracing.) The trace
610 : : functions are called with three arguments:
611 : : a pointer to the current frame, a string
612 : : indicating why the function is called, and
613 : : an argument which depends on the situation.
614 : : The global trace function is also called
615 : : whenever an exception is detected. */
616 [ # # ]: 0 : if (call_trace_protected(tstate->c_tracefunc,
617 : : tstate->c_traceobj,
618 : : tstate, frame,
619 : : PyTrace_CALL, Py_None)) {
620 : : /* Trace function raised an error */
621 : 0 : return -1;
622 : : }
623 : : }
624 [ # # ]: 0 : if (tstate->c_profilefunc != NULL) {
625 : : /* Similar for c_profilefunc, except it needn't
626 : : return itself and isn't called for "line" events */
627 [ # # ]: 0 : if (call_trace_protected(tstate->c_profilefunc,
628 : : tstate->c_profileobj,
629 : : tstate, frame,
630 : : PyTrace_CALL, Py_None)) {
631 : : /* Profile function raised an error */
632 : 0 : return -1;
633 : : }
634 : : }
635 : 0 : return 0;
636 : : }
637 : :
638 : : static int
639 : 0 : trace_function_exit(PyThreadState *tstate, _PyInterpreterFrame *frame, PyObject *retval)
640 : : {
641 [ # # ]: 0 : if (tstate->c_tracefunc) {
642 [ # # ]: 0 : if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
643 : : tstate, frame, PyTrace_RETURN, retval)) {
644 : 0 : return -1;
645 : : }
646 : : }
647 [ # # ]: 0 : if (tstate->c_profilefunc) {
648 [ # # ]: 0 : if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
649 : : tstate, frame, PyTrace_RETURN, retval)) {
650 : 0 : return -1;
651 : : }
652 : : }
653 : 0 : return 0;
654 : : }
655 : :
656 : :
657 : 0 : int _Py_CheckRecursiveCallPy(
658 : : PyThreadState *tstate)
659 : : {
660 [ # # ]: 0 : if (tstate->recursion_headroom) {
661 [ # # ]: 0 : if (tstate->py_recursion_remaining < -50) {
662 : : /* Overflowing while handling an overflow. Give up. */
663 : 0 : Py_FatalError("Cannot recover from Python stack overflow.");
664 : : }
665 : : }
666 : : else {
667 [ # # ]: 0 : if (tstate->py_recursion_remaining <= 0) {
668 : 0 : tstate->recursion_headroom++;
669 : 0 : _PyErr_Format(tstate, PyExc_RecursionError,
670 : : "maximum recursion depth exceeded");
671 : 0 : tstate->recursion_headroom--;
672 : 0 : return -1;
673 : : }
674 : : }
675 : 0 : return 0;
676 : : }
677 : :
678 : 4159852 : static inline int _Py_EnterRecursivePy(PyThreadState *tstate) {
679 [ - + - - ]: 4159852 : return (tstate->py_recursion_remaining-- <= 0) &&
680 : 0 : _Py_CheckRecursiveCallPy(tstate);
681 : : }
682 : :
683 : :
684 : 4159852 : static inline void _Py_LeaveRecursiveCallPy(PyThreadState *tstate) {
685 : 4159852 : tstate->py_recursion_remaining++;
686 : 4159852 : }
687 : :
688 : :
689 : : /* Disable unused label warnings. They are handy for debugging, even
690 : : if computed gotos aren't used. */
691 : :
692 : : /* TBD - what about other compilers? */
693 : : #if defined(__GNUC__)
694 : : # pragma GCC diagnostic push
695 : : # pragma GCC diagnostic ignored "-Wunused-label"
696 : : #elif defined(_MSC_VER) /* MS_WINDOWS */
697 : : # pragma warning(push)
698 : : # pragma warning(disable:4102)
699 : : #endif
700 : :
701 : : PyObject* _Py_HOT_FUNCTION
702 : 1676264 : _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag)
703 : : {
704 : 1676264 : _Py_EnsureTstateNotNULL(tstate);
705 : : CALL_STAT_INC(pyeval_calls);
706 : :
707 : : #if USE_COMPUTED_GOTOS
708 : : /* Import the static jump table */
709 : : #include "opcode_targets.h"
710 : : #endif
711 : :
712 : : #ifdef Py_STATS
713 : : int lastopcode = 0;
714 : : #endif
715 : : // opcode is an 8-bit value to improve the code generated by MSVC
716 : : // for the big switch below (in combination with the EXTRA_CASES macro).
717 : : uint8_t opcode; /* Current opcode */
718 : : int oparg; /* Current opcode argument, if any */
719 : 1676264 : _Py_atomic_int * const eval_breaker = &tstate->interp->ceval.eval_breaker;
720 : : #ifdef LLTRACE
721 : : int lltrace = 0;
722 : : #endif
723 : :
724 : : _PyCFrame cframe;
725 : : _PyInterpreterFrame entry_frame;
726 : 1676264 : PyObject *kwnames = NULL; // Borrowed reference. Reset by CALL instructions.
727 : :
728 : : /* WARNING: Because the _PyCFrame lives on the C stack,
729 : : * but can be accessed from a heap allocated object (tstate)
730 : : * strict stack discipline must be maintained.
731 : : */
732 : 1676264 : _PyCFrame *prev_cframe = tstate->cframe;
733 : 1676264 : cframe.use_tracing = prev_cframe->use_tracing;
734 : 1676264 : cframe.previous = prev_cframe;
735 : 1676264 : tstate->cframe = &cframe;
736 : :
737 : : assert(tstate->interp->interpreter_trampoline != NULL);
738 : : #ifdef Py_DEBUG
739 : : /* Set these to invalid but identifiable values for debugging. */
740 : : entry_frame.f_funcobj = (PyObject*)0xaaa0;
741 : : entry_frame.f_locals = (PyObject*)0xaaa1;
742 : : entry_frame.frame_obj = (PyFrameObject*)0xaaa2;
743 : : entry_frame.f_globals = (PyObject*)0xaaa3;
744 : : entry_frame.f_builtins = (PyObject*)0xaaa4;
745 : : #endif
746 : 1676264 : entry_frame.f_code = tstate->interp->interpreter_trampoline;
747 : 1676264 : entry_frame.prev_instr =
748 : 1676264 : _PyCode_CODE(tstate->interp->interpreter_trampoline);
749 : 1676264 : entry_frame.stacktop = 0;
750 : 1676264 : entry_frame.owner = FRAME_OWNED_BY_CSTACK;
751 : 1676264 : entry_frame.yield_offset = 0;
752 : : /* Push frame */
753 : 1676264 : entry_frame.previous = prev_cframe->current_frame;
754 : 1676264 : frame->previous = &entry_frame;
755 : 1676264 : cframe.current_frame = frame;
756 : :
757 [ - + ]: 1676264 : if (_Py_EnterRecursiveCallTstate(tstate, "")) {
758 : 0 : tstate->c_recursion_remaining--;
759 : 0 : tstate->py_recursion_remaining--;
760 : 0 : goto exit_unwind;
761 : : }
762 : :
763 : : /* support for generator.throw() */
764 [ + + ]: 1676264 : if (throwflag) {
765 [ - + ]: 1937 : if (_Py_EnterRecursivePy(tstate)) {
766 : 0 : goto exit_unwind;
767 : : }
768 [ - + - - ]: 1937 : TRACE_FUNCTION_THROW_ENTRY();
769 [ - + ]: 1937 : DTRACE_FUNCTION_ENTRY();
770 : 1937 : goto resume_with_error;
771 : : }
772 : :
773 : : /* Local "register" variables.
774 : : * These are cached values from the frame and code object. */
775 : :
776 : : _Py_CODEUNIT *next_instr;
777 : : PyObject **stack_pointer;
778 : :
779 : : /* Sets the above local variables from the frame */
780 : : #define SET_LOCALS_FROM_FRAME() \
781 : : assert(_PyInterpreterFrame_LASTI(frame) >= -1); \
782 : : /* Jump back to the last instruction executed... */ \
783 : : next_instr = frame->prev_instr + 1; \
784 : : stack_pointer = _PyFrame_GetStackPointer(frame); \
785 : : /* Set stackdepth to -1. \
786 : : Update when returning or calling trace function. \
787 : : Having stackdepth <= 0 ensures that invalid \
788 : : values are not visible to the cycle GC. \
789 : : We choose -1 rather than 0 to assist debugging. \
790 : : */ \
791 : : frame->stacktop = -1;
792 : :
793 : :
794 : 1674327 : start_frame:
795 [ - + ]: 4157915 : if (_Py_EnterRecursivePy(tstate)) {
796 : 0 : goto exit_unwind;
797 : : }
798 : :
799 : 4157915 : resume_frame:
800 : 8314308 : SET_LOCALS_FROM_FRAME();
801 : :
802 : : #ifdef LLTRACE
803 : : {
804 : : if (frame != &entry_frame) {
805 : : int r = PyDict_Contains(GLOBALS(), &_Py_ID(__lltrace__));
806 : : if (r < 0) {
807 : : goto exit_unwind;
808 : : }
809 : : lltrace = r;
810 : : }
811 : : if (lltrace) {
812 : : lltrace_resume_frame(frame);
813 : : }
814 : : }
815 : : #endif
816 : :
817 : : #ifdef Py_DEBUG
818 : : /* _PyEval_EvalFrameDefault() must not be called with an exception set,
819 : : because it can clear it (directly or indirectly) and so the
820 : : caller loses its exception */
821 : : assert(!_PyErr_Occurred(tstate));
822 : : #endif
823 : :
824 : 8314308 : DISPATCH();
825 : :
826 : 298 : handle_eval_breaker:
827 : :
828 : : /* Do periodic things, like check for signals and async I/0.
829 : : * We need to do reasonably frequently, but not too frequently.
830 : : * All loops should include a check of the eval breaker.
831 : : * We also check on return from any builtin function.
832 : : */
833 [ - + ]: 298 : if (_Py_HandlePending(tstate) != 0) {
834 : 0 : goto error;
835 : : }
836 : 298 : DISPATCH();
837 : :
838 : : {
839 : : /* Start instructions */
840 : : #if !USE_COMPUTED_GOTOS
841 : : dispatch_opcode:
842 : : switch (opcode)
843 : : #endif
844 : : {
845 : :
846 : : #include "generated_cases.c.h"
847 : :
848 : : #if USE_COMPUTED_GOTOS
849 : 0 : TARGET_DO_TRACING:
850 : : #else
851 : : case DO_TRACING:
852 : : #endif
853 : : {
854 : : assert(cframe.use_tracing);
855 : : assert(tstate->tracing == 0);
856 [ # # ]: 0 : if (INSTR_OFFSET() >= frame->f_code->_co_firsttraceable) {
857 : 0 : int instr_prev = _PyInterpreterFrame_LASTI(frame);
858 : 0 : frame->prev_instr = next_instr;
859 : 0 : NEXTOPARG();
860 : : // No _PyOpcode_Deopt here, since RESUME has no optimized forms:
861 [ # # ]: 0 : if (opcode == RESUME) {
862 [ # # ]: 0 : if (oparg < 2) {
863 [ # # ]: 0 : CHECK_EVAL_BREAKER();
864 : : }
865 : : /* Call tracing */
866 [ # # # # ]: 0 : TRACE_FUNCTION_ENTRY();
867 [ # # ]: 0 : DTRACE_FUNCTION_ENTRY();
868 : : }
869 : : else {
870 : : /* line-by-line tracing support */
871 [ # # ]: 0 : if (PyDTrace_LINE_ENABLED()) {
872 : 0 : maybe_dtrace_line(frame, &tstate->trace_info, instr_prev);
873 : : }
874 : :
875 [ # # ]: 0 : if (cframe.use_tracing &&
876 [ # # # # ]: 0 : tstate->c_tracefunc != NULL && !tstate->tracing) {
877 : : int err;
878 : : /* see maybe_call_line_trace()
879 : : for expository comments */
880 : 0 : _PyFrame_SetStackPointer(frame, stack_pointer);
881 : :
882 : 0 : err = maybe_call_line_trace(tstate->c_tracefunc,
883 : : tstate->c_traceobj,
884 : : tstate, frame, instr_prev);
885 : : // Reload possibly changed frame fields:
886 : 0 : stack_pointer = _PyFrame_GetStackPointer(frame);
887 : 0 : frame->stacktop = -1;
888 : : // next_instr is only reloaded if tracing *does not* raise.
889 : : // This is consistent with the behavior of older Python
890 : : // versions. If a trace function sets a new f_lineno and
891 : : // *then* raises, we use the *old* location when searching
892 : : // for an exception handler, displaying the traceback, and
893 : : // so on:
894 [ # # ]: 0 : if (err) {
895 : : // next_instr wasn't incremented at the start of this
896 : : // instruction. Increment it before handling the error,
897 : : // so that it looks the same as a "normal" instruction:
898 : 0 : next_instr++;
899 : 0 : goto error;
900 : : }
901 : : // Reload next_instr. Don't increment it, though, since
902 : : // we're going to re-dispatch to the "true" instruction now:
903 : 0 : next_instr = frame->prev_instr;
904 : : }
905 : : }
906 : : }
907 : 0 : NEXTOPARG();
908 : : PRE_DISPATCH_GOTO();
909 : : // No _PyOpcode_Deopt here, since EXTENDED_ARG has no optimized forms:
910 [ # # ]: 0 : while (opcode == EXTENDED_ARG) {
911 : : // CPython hasn't ever traced the instruction after an EXTENDED_ARG.
912 : : // Inline the EXTENDED_ARG here, so we can avoid branching there:
913 : 0 : INSTRUCTION_START(EXTENDED_ARG);
914 : 0 : opcode = next_instr->op.code;
915 : 0 : oparg = oparg << 8 | next_instr->op.arg;
916 : : // Make sure the next instruction isn't a RESUME, since that needs
917 : : // to trace properly (and shouldn't have an EXTENDED_ARG, anyways):
918 : : assert(opcode != RESUME);
919 : : PRE_DISPATCH_GOTO();
920 : : }
921 : 0 : opcode = _PyOpcode_Deopt[opcode];
922 [ # # ]: 0 : if (_PyOpcode_Caches[opcode]) {
923 : 0 : uint16_t *counter = &next_instr[1].cache;
924 : : // The instruction is going to decrement the counter, so we need to
925 : : // increment it here to make sure it doesn't try to specialize:
926 [ # # ]: 0 : if (!ADAPTIVE_COUNTER_IS_MAX(*counter)) {
927 : 0 : INCREMENT_ADAPTIVE_COUNTER(*counter);
928 : : }
929 : : }
930 : 0 : DISPATCH_GOTO();
931 : : }
932 : :
933 : : #if USE_COMPUTED_GOTOS
934 : 0 : _unknown_opcode:
935 : : #else
936 : : EXTRA_CASES // From opcode.h, a 'case' for each unused opcode
937 : : #endif
938 : : /* Tell C compilers not to hold the opcode variable in the loop.
939 : : next_instr points the current instruction without TARGET(). */
940 : 0 : opcode = next_instr->op.code;
941 : 0 : _PyErr_Format(tstate, PyExc_SystemError,
942 : : "%U:%d: unknown opcode %d",
943 : 0 : frame->f_code->co_filename,
944 : : _PyInterpreterFrame_GetLine(frame),
945 : : opcode);
946 : 0 : goto error;
947 : :
948 : : } /* End instructions */
949 : :
950 : : /* This should never be reached. Every opcode should end with DISPATCH()
951 : : or goto error. */
952 : : Py_UNREACHABLE();
953 : :
954 : 0 : unbound_local_error:
955 : : {
956 : 0 : format_exc_check_arg(tstate, PyExc_UnboundLocalError,
957 : : UNBOUNDLOCAL_ERROR_MSG,
958 : 0 : PyTuple_GetItem(frame->f_code->co_localsplusnames, oparg)
959 : : );
960 : 0 : goto error;
961 : : }
962 : :
963 : 0 : pop_4_error:
964 : 0 : STACK_SHRINK(1);
965 : 204 : pop_3_error:
966 : 204 : STACK_SHRINK(1);
967 : 101703 : pop_2_error:
968 : 101703 : STACK_SHRINK(1);
969 : 102187 : pop_1_error:
970 : 102187 : STACK_SHRINK(1);
971 : 105269 : error:
972 : 105269 : kwnames = NULL;
973 : : /* Double-check exception status. */
974 : : #ifdef NDEBUG
975 [ - + ]: 105269 : if (!_PyErr_Occurred(tstate)) {
976 : 0 : _PyErr_SetString(tstate, PyExc_SystemError,
977 : : "error return without exception set");
978 : : }
979 : : #else
980 : : assert(_PyErr_Occurred(tstate));
981 : : #endif
982 : :
983 : : /* Log traceback info. */
984 : : assert(frame != &entry_frame);
985 [ + - ]: 105269 : if (!_PyFrame_IsIncomplete(frame)) {
986 : 105269 : PyFrameObject *f = _PyFrame_GetFrameObject(frame);
987 [ + - ]: 105269 : if (f != NULL) {
988 : 105269 : PyTraceBack_Here(f);
989 : : }
990 : : }
991 : :
992 [ + - ]: 105269 : if (tstate->c_tracefunc != NULL) {
993 : : /* Make sure state is set to FRAME_UNWINDING for tracing */
994 : 0 : call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
995 : : tstate, frame);
996 : : }
997 : :
998 : 105269 : exception_unwind:
999 : : {
1000 : : /* We can't use frame->f_lasti here, as RERAISE may have set it */
1001 : 107686 : int offset = INSTR_OFFSET()-1;
1002 : : int level, handler, lasti;
1003 [ + + ]: 107686 : if (get_exception_handler(frame->f_code, offset, &level, &handler, &lasti) == 0) {
1004 : : // No handlers, so exit.
1005 : : assert(_PyErr_Occurred(tstate));
1006 : :
1007 : : /* Pop remaining stack entries. */
1008 : 3459 : PyObject **stackbase = _PyFrame_Stackbase(frame);
1009 [ + + ]: 8067 : while (stack_pointer > stackbase) {
1010 : 4608 : PyObject *o = POP();
1011 [ + - + + ]: 4608 : Py_XDECREF(o);
1012 : : }
1013 : : assert(STACK_LEVEL() == 0);
1014 : 3459 : _PyFrame_SetStackPointer(frame, stack_pointer);
1015 [ - + ]: 3459 : TRACE_FUNCTION_UNWIND();
1016 [ - + ]: 3459 : DTRACE_FUNCTION_EXIT();
1017 : 3459 : goto exit_unwind;
1018 : : }
1019 : :
1020 : : assert(STACK_LEVEL() >= level);
1021 : 104227 : PyObject **new_top = _PyFrame_Stackbase(frame) + level;
1022 [ + + ]: 110063 : while (stack_pointer > new_top) {
1023 : 5836 : PyObject *v = POP();
1024 [ + + + + ]: 5836 : Py_XDECREF(v);
1025 : : }
1026 [ + + ]: 104227 : if (lasti) {
1027 : 2548 : int frame_lasti = _PyInterpreterFrame_LASTI(frame);
1028 : 2548 : PyObject *lasti = PyLong_FromLong(frame_lasti);
1029 [ - + ]: 2548 : if (lasti == NULL) {
1030 : 0 : goto exception_unwind;
1031 : : }
1032 : 2548 : PUSH(lasti);
1033 : : }
1034 : :
1035 : : /* Make the raw exception data
1036 : : available to the handler,
1037 : : so a program can emulate the
1038 : : Python main loop. */
1039 : 104227 : PUSH(_PyErr_GetRaisedException(tstate));
1040 : 104227 : JUMPTO(handler);
1041 : : /* Resume normal execution */
1042 : 104227 : DISPATCH();
1043 : : }
1044 : : }
1045 : :
1046 : 3459 : exit_unwind:
1047 : : assert(_PyErr_Occurred(tstate));
1048 : 3459 : _Py_LeaveRecursiveCallPy(tstate);
1049 : : assert(frame != &entry_frame);
1050 : : // GH-99729: We need to unlink the frame *before* clearing it:
1051 : 3459 : _PyInterpreterFrame *dying = frame;
1052 : 3459 : frame = cframe.current_frame = dying->previous;
1053 : 3459 : _PyEvalFrameClearAndPop(tstate, dying);
1054 [ + + ]: 3459 : if (frame == &entry_frame) {
1055 : : /* Restore previous cframe and exit */
1056 : 2754 : tstate->cframe = cframe.previous;
1057 : 2754 : tstate->cframe->use_tracing = cframe.use_tracing;
1058 : : assert(tstate->cframe->current_frame == frame->previous);
1059 : 2754 : _Py_LeaveRecursiveCallTstate(tstate);
1060 : 2754 : return NULL;
1061 : : }
1062 : :
1063 : 705 : resume_with_error:
1064 : 2642 : SET_LOCALS_FROM_FRAME();
1065 : 2642 : goto error;
1066 : :
1067 : : }
1068 : : #if defined(__GNUC__)
1069 : : # pragma GCC diagnostic pop
1070 : : #elif defined(_MSC_VER) /* MS_WINDOWS */
1071 : : # pragma warning(pop)
1072 : : #endif
1073 : :
1074 : : static void
1075 : 0 : format_missing(PyThreadState *tstate, const char *kind,
1076 : : PyCodeObject *co, PyObject *names, PyObject *qualname)
1077 : : {
1078 : : int err;
1079 : 0 : Py_ssize_t len = PyList_GET_SIZE(names);
1080 : : PyObject *name_str, *comma, *tail, *tmp;
1081 : :
1082 : : assert(PyList_CheckExact(names));
1083 : : assert(len >= 1);
1084 : : /* Deal with the joys of natural language. */
1085 [ # # # ]: 0 : switch (len) {
1086 : 0 : case 1:
1087 : 0 : name_str = PyList_GET_ITEM(names, 0);
1088 : 0 : Py_INCREF(name_str);
1089 : 0 : break;
1090 : 0 : case 2:
1091 : 0 : name_str = PyUnicode_FromFormat("%U and %U",
1092 : 0 : PyList_GET_ITEM(names, len - 2),
1093 : 0 : PyList_GET_ITEM(names, len - 1));
1094 : 0 : break;
1095 : 0 : default:
1096 : 0 : tail = PyUnicode_FromFormat(", %U, and %U",
1097 : 0 : PyList_GET_ITEM(names, len - 2),
1098 : 0 : PyList_GET_ITEM(names, len - 1));
1099 [ # # ]: 0 : if (tail == NULL)
1100 : 0 : return;
1101 : : /* Chop off the last two objects in the list. This shouldn't actually
1102 : : fail, but we can't be too careful. */
1103 : 0 : err = PyList_SetSlice(names, len - 2, len, NULL);
1104 [ # # ]: 0 : if (err == -1) {
1105 [ # # ]: 0 : Py_DECREF(tail);
1106 : 0 : return;
1107 : : }
1108 : : /* Stitch everything up into a nice comma-separated list. */
1109 : 0 : comma = PyUnicode_FromString(", ");
1110 [ # # ]: 0 : if (comma == NULL) {
1111 [ # # ]: 0 : Py_DECREF(tail);
1112 : 0 : return;
1113 : : }
1114 : 0 : tmp = PyUnicode_Join(comma, names);
1115 [ # # ]: 0 : Py_DECREF(comma);
1116 [ # # ]: 0 : if (tmp == NULL) {
1117 [ # # ]: 0 : Py_DECREF(tail);
1118 : 0 : return;
1119 : : }
1120 : 0 : name_str = PyUnicode_Concat(tmp, tail);
1121 [ # # ]: 0 : Py_DECREF(tmp);
1122 [ # # ]: 0 : Py_DECREF(tail);
1123 : 0 : break;
1124 : : }
1125 [ # # ]: 0 : if (name_str == NULL)
1126 : 0 : return;
1127 [ # # ]: 0 : _PyErr_Format(tstate, PyExc_TypeError,
1128 : : "%U() missing %i required %s argument%s: %U",
1129 : : qualname,
1130 : : len,
1131 : : kind,
1132 : : len == 1 ? "" : "s",
1133 : : name_str);
1134 [ # # ]: 0 : Py_DECREF(name_str);
1135 : : }
1136 : :
1137 : : static void
1138 : 0 : missing_arguments(PyThreadState *tstate, PyCodeObject *co,
1139 : : Py_ssize_t missing, Py_ssize_t defcount,
1140 : : PyObject **localsplus, PyObject *qualname)
1141 : : {
1142 : 0 : Py_ssize_t i, j = 0;
1143 : : Py_ssize_t start, end;
1144 : 0 : int positional = (defcount != -1);
1145 [ # # ]: 0 : const char *kind = positional ? "positional" : "keyword-only";
1146 : : PyObject *missing_names;
1147 : :
1148 : : /* Compute the names of the arguments that are missing. */
1149 : 0 : missing_names = PyList_New(missing);
1150 [ # # ]: 0 : if (missing_names == NULL)
1151 : 0 : return;
1152 [ # # ]: 0 : if (positional) {
1153 : 0 : start = 0;
1154 : 0 : end = co->co_argcount - defcount;
1155 : : }
1156 : : else {
1157 : 0 : start = co->co_argcount;
1158 : 0 : end = start + co->co_kwonlyargcount;
1159 : : }
1160 [ # # ]: 0 : for (i = start; i < end; i++) {
1161 [ # # ]: 0 : if (localsplus[i] == NULL) {
1162 : 0 : PyObject *raw = PyTuple_GET_ITEM(co->co_localsplusnames, i);
1163 : 0 : PyObject *name = PyObject_Repr(raw);
1164 [ # # ]: 0 : if (name == NULL) {
1165 [ # # ]: 0 : Py_DECREF(missing_names);
1166 : 0 : return;
1167 : : }
1168 : 0 : PyList_SET_ITEM(missing_names, j++, name);
1169 : : }
1170 : : }
1171 : : assert(j == missing);
1172 : 0 : format_missing(tstate, kind, co, missing_names, qualname);
1173 [ # # ]: 0 : Py_DECREF(missing_names);
1174 : : }
1175 : :
1176 : : static void
1177 : 0 : too_many_positional(PyThreadState *tstate, PyCodeObject *co,
1178 : : Py_ssize_t given, PyObject *defaults,
1179 : : PyObject **localsplus, PyObject *qualname)
1180 : : {
1181 : : int plural;
1182 : 0 : Py_ssize_t kwonly_given = 0;
1183 : : Py_ssize_t i;
1184 : : PyObject *sig, *kwonly_sig;
1185 : 0 : Py_ssize_t co_argcount = co->co_argcount;
1186 : :
1187 : : assert((co->co_flags & CO_VARARGS) == 0);
1188 : : /* Count missing keyword-only args. */
1189 [ # # ]: 0 : for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
1190 [ # # ]: 0 : if (localsplus[i] != NULL) {
1191 : 0 : kwonly_given++;
1192 : : }
1193 : : }
1194 [ # # ]: 0 : Py_ssize_t defcount = defaults == NULL ? 0 : PyTuple_GET_SIZE(defaults);
1195 [ # # ]: 0 : if (defcount) {
1196 : 0 : Py_ssize_t atleast = co_argcount - defcount;
1197 : 0 : plural = 1;
1198 : 0 : sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
1199 : : }
1200 : : else {
1201 : 0 : plural = (co_argcount != 1);
1202 : 0 : sig = PyUnicode_FromFormat("%zd", co_argcount);
1203 : : }
1204 [ # # ]: 0 : if (sig == NULL)
1205 : 0 : return;
1206 [ # # ]: 0 : if (kwonly_given) {
1207 : 0 : const char *format = " positional argument%s (and %zd keyword-only argument%s)";
1208 [ # # # # ]: 0 : kwonly_sig = PyUnicode_FromFormat(format,
1209 : : given != 1 ? "s" : "",
1210 : : kwonly_given,
1211 : : kwonly_given != 1 ? "s" : "");
1212 [ # # ]: 0 : if (kwonly_sig == NULL) {
1213 [ # # ]: 0 : Py_DECREF(sig);
1214 : 0 : return;
1215 : : }
1216 : : }
1217 : : else {
1218 : : /* This will not fail. */
1219 : 0 : kwonly_sig = PyUnicode_FromString("");
1220 : : assert(kwonly_sig != NULL);
1221 : : }
1222 [ # # # # ]: 0 : _PyErr_Format(tstate, PyExc_TypeError,
1223 : : "%U() takes %U positional argument%s but %zd%U %s given",
1224 : : qualname,
1225 : : sig,
1226 : : plural ? "s" : "",
1227 : : given,
1228 : : kwonly_sig,
1229 [ # # ]: 0 : given == 1 && !kwonly_given ? "was" : "were");
1230 [ # # ]: 0 : Py_DECREF(sig);
1231 [ # # ]: 0 : Py_DECREF(kwonly_sig);
1232 : : }
1233 : :
1234 : : static int
1235 : 0 : positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
1236 : : Py_ssize_t kwcount, PyObject* kwnames,
1237 : : PyObject *qualname)
1238 : : {
1239 : 0 : int posonly_conflicts = 0;
1240 : 0 : PyObject* posonly_names = PyList_New(0);
1241 [ # # ]: 0 : if (posonly_names == NULL) {
1242 : 0 : goto fail;
1243 : : }
1244 [ # # ]: 0 : for(int k=0; k < co->co_posonlyargcount; k++){
1245 : 0 : PyObject* posonly_name = PyTuple_GET_ITEM(co->co_localsplusnames, k);
1246 : :
1247 [ # # ]: 0 : for (int k2=0; k2<kwcount; k2++){
1248 : : /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
1249 : 0 : PyObject* kwname = PyTuple_GET_ITEM(kwnames, k2);
1250 [ # # ]: 0 : if (kwname == posonly_name){
1251 [ # # ]: 0 : if(PyList_Append(posonly_names, kwname) != 0) {
1252 : 0 : goto fail;
1253 : : }
1254 : 0 : posonly_conflicts++;
1255 : 0 : continue;
1256 : : }
1257 : :
1258 : 0 : int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
1259 : :
1260 [ # # ]: 0 : if ( cmp > 0) {
1261 [ # # ]: 0 : if(PyList_Append(posonly_names, kwname) != 0) {
1262 : 0 : goto fail;
1263 : : }
1264 : 0 : posonly_conflicts++;
1265 [ # # ]: 0 : } else if (cmp < 0) {
1266 : 0 : goto fail;
1267 : : }
1268 : :
1269 : : }
1270 : : }
1271 [ # # ]: 0 : if (posonly_conflicts) {
1272 : 0 : PyObject* comma = PyUnicode_FromString(", ");
1273 [ # # ]: 0 : if (comma == NULL) {
1274 : 0 : goto fail;
1275 : : }
1276 : 0 : PyObject* error_names = PyUnicode_Join(comma, posonly_names);
1277 [ # # ]: 0 : Py_DECREF(comma);
1278 [ # # ]: 0 : if (error_names == NULL) {
1279 : 0 : goto fail;
1280 : : }
1281 : 0 : _PyErr_Format(tstate, PyExc_TypeError,
1282 : : "%U() got some positional-only arguments passed"
1283 : : " as keyword arguments: '%U'",
1284 : : qualname, error_names);
1285 [ # # ]: 0 : Py_DECREF(error_names);
1286 : 0 : goto fail;
1287 : : }
1288 : :
1289 [ # # ]: 0 : Py_DECREF(posonly_names);
1290 : 0 : return 0;
1291 : :
1292 : 0 : fail:
1293 [ # # # # ]: 0 : Py_XDECREF(posonly_names);
1294 : 0 : return 1;
1295 : :
1296 : : }
1297 : :
1298 : :
1299 : : static inline unsigned char *
1300 : 101742 : scan_back_to_entry_start(unsigned char *p) {
1301 [ + + ]: 208985 : for (; (p[0]&128) == 0; p--);
1302 : 101742 : return p;
1303 : : }
1304 : :
1305 : : static inline unsigned char *
1306 : 6242 : skip_to_next_entry(unsigned char *p, unsigned char *end) {
1307 [ + + + + ]: 22682 : while (p < end && ((p[0] & 128) == 0)) {
1308 : 16440 : p++;
1309 : : }
1310 : 6242 : return p;
1311 : : }
1312 : :
1313 : :
1314 : : #define MAX_LINEAR_SEARCH 40
1315 : :
1316 : : static int
1317 : 107686 : get_exception_handler(PyCodeObject *code, int index, int *level, int *handler, int *lasti)
1318 : : {
1319 : 107686 : unsigned char *start = (unsigned char *)PyBytes_AS_STRING(code->co_exceptiontable);
1320 : 107686 : unsigned char *end = start + PyBytes_GET_SIZE(code->co_exceptiontable);
1321 : : /* Invariants:
1322 : : * start_table == end_table OR
1323 : : * start_table points to a legal entry and end_table points
1324 : : * beyond the table or to a legal entry that is after index.
1325 : : */
1326 [ + + ]: 107686 : if (end - start > MAX_LINEAR_SEARCH) {
1327 : : int offset;
1328 : 100533 : parse_varint(start, &offset);
1329 [ - + ]: 100533 : if (offset > index) {
1330 : 0 : return 0;
1331 : : }
1332 : : do {
1333 : 101742 : unsigned char * mid = start + ((end-start)>>1);
1334 : 101742 : mid = scan_back_to_entry_start(mid);
1335 : 101742 : parse_varint(mid, &offset);
1336 [ + + ]: 101742 : if (offset > index) {
1337 : 100854 : end = mid;
1338 : : }
1339 : : else {
1340 : 888 : start = mid;
1341 : : }
1342 : :
1343 [ + + ]: 101742 : } while (end - start > MAX_LINEAR_SEARCH);
1344 : : }
1345 : 107686 : unsigned char *scan = start;
1346 [ + + ]: 113928 : while (scan < end) {
1347 : : int start_offset, size;
1348 : 110809 : scan = parse_varint(scan, &start_offset);
1349 [ + + ]: 110809 : if (start_offset > index) {
1350 : 340 : break;
1351 : : }
1352 : 110469 : scan = parse_varint(scan, &size);
1353 [ + + ]: 110469 : if (start_offset + size > index) {
1354 : 104227 : scan = parse_varint(scan, handler);
1355 : : int depth_and_lasti;
1356 : 104227 : parse_varint(scan, &depth_and_lasti);
1357 : 104227 : *level = depth_and_lasti >> 1;
1358 : 104227 : *lasti = depth_and_lasti & 1;
1359 : 104227 : return 1;
1360 : : }
1361 : 6242 : scan = skip_to_next_entry(scan, end);
1362 : : }
1363 : 3459 : return 0;
1364 : : }
1365 : :
1366 : : static int
1367 : 1424205 : initialize_locals(PyThreadState *tstate, PyFunctionObject *func,
1368 : : PyObject **localsplus, PyObject *const *args,
1369 : : Py_ssize_t argcount, PyObject *kwnames)
1370 : : {
1371 : 1424205 : PyCodeObject *co = (PyCodeObject*)func->func_code;
1372 : 1424205 : const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
1373 : :
1374 : : /* Create a dictionary for keyword parameters (**kwags) */
1375 : : PyObject *kwdict;
1376 : : Py_ssize_t i;
1377 [ + + ]: 1424205 : if (co->co_flags & CO_VARKEYWORDS) {
1378 : 120689 : kwdict = PyDict_New();
1379 [ - + ]: 120689 : if (kwdict == NULL) {
1380 : 0 : goto fail_pre_positional;
1381 : : }
1382 : 120689 : i = total_args;
1383 [ + + ]: 120689 : if (co->co_flags & CO_VARARGS) {
1384 : 104426 : i++;
1385 : : }
1386 : : assert(localsplus[i] == NULL);
1387 : 120689 : localsplus[i] = kwdict;
1388 : : }
1389 : : else {
1390 : 1303516 : kwdict = NULL;
1391 : : }
1392 : :
1393 : : /* Copy all positional arguments into local variables */
1394 : : Py_ssize_t j, n;
1395 [ + + ]: 1424205 : if (argcount > co->co_argcount) {
1396 : 131204 : n = co->co_argcount;
1397 : : }
1398 : : else {
1399 : 1293001 : n = argcount;
1400 : : }
1401 [ + + ]: 3993006 : for (j = 0; j < n; j++) {
1402 : 2568801 : PyObject *x = args[j];
1403 : : assert(localsplus[j] == NULL);
1404 : 2568801 : localsplus[j] = x;
1405 : : }
1406 : :
1407 : : /* Pack other positional arguments into the *args argument */
1408 [ + + ]: 1424205 : if (co->co_flags & CO_VARARGS) {
1409 : 132278 : PyObject *u = NULL;
1410 [ + + ]: 132278 : if (argcount == n) {
1411 : 1074 : u = Py_NewRef(&_Py_SINGLETON(tuple_empty));
1412 : : }
1413 : : else {
1414 : : assert(args != NULL);
1415 : 131204 : u = _PyTuple_FromArraySteal(args + n, argcount - n);
1416 : : }
1417 [ - + ]: 132278 : if (u == NULL) {
1418 : 0 : goto fail_post_positional;
1419 : : }
1420 : : assert(localsplus[total_args] == NULL);
1421 : 132278 : localsplus[total_args] = u;
1422 : : }
1423 [ - + ]: 1291927 : else if (argcount > n) {
1424 : : /* Too many postional args. Error is reported later */
1425 [ # # ]: 0 : for (j = n; j < argcount; j++) {
1426 [ # # ]: 0 : Py_DECREF(args[j]);
1427 : : }
1428 : : }
1429 : :
1430 : : /* Handle keyword arguments */
1431 [ + + ]: 1424205 : if (kwnames != NULL) {
1432 : 202192 : Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames);
1433 [ + + ]: 408526 : for (i = 0; i < kwcount; i++) {
1434 : : PyObject **co_varnames;
1435 : 206334 : PyObject *keyword = PyTuple_GET_ITEM(kwnames, i);
1436 : 206334 : PyObject *value = args[i+argcount];
1437 : : Py_ssize_t j;
1438 : :
1439 [ + - - + ]: 206334 : if (keyword == NULL || !PyUnicode_Check(keyword)) {
1440 : 0 : _PyErr_Format(tstate, PyExc_TypeError,
1441 : : "%U() keywords must be strings",
1442 : : func->func_qualname);
1443 : 0 : goto kw_fail;
1444 : : }
1445 : :
1446 : : /* Speed hack: do raw pointer compares. As names are
1447 : : normally interned this should almost always hit. */
1448 : 206334 : co_varnames = ((PyTupleObject *)(co->co_localsplusnames))->ob_item;
1449 [ + + ]: 701540 : for (j = co->co_posonlyargcount; j < total_args; j++) {
1450 : 657661 : PyObject *varname = co_varnames[j];
1451 [ + + ]: 657661 : if (varname == keyword) {
1452 : 162455 : goto kw_found;
1453 : : }
1454 : : }
1455 : :
1456 : : /* Slow fallback, just in case */
1457 [ + + ]: 73558 : for (j = co->co_posonlyargcount; j < total_args; j++) {
1458 : 29679 : PyObject *varname = co_varnames[j];
1459 : 29679 : int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
1460 [ - + ]: 29679 : if (cmp > 0) {
1461 : 0 : goto kw_found;
1462 : : }
1463 [ - + ]: 29679 : else if (cmp < 0) {
1464 : 0 : goto kw_fail;
1465 : : }
1466 : : }
1467 : :
1468 : : assert(j >= total_args);
1469 [ - + ]: 43879 : if (kwdict == NULL) {
1470 : :
1471 [ # # ]: 0 : if (co->co_posonlyargcount
1472 [ # # ]: 0 : && positional_only_passed_as_keyword(tstate, co,
1473 : : kwcount, kwnames,
1474 : : func->func_qualname))
1475 : : {
1476 : 0 : goto kw_fail;
1477 : : }
1478 : :
1479 : 0 : _PyErr_Format(tstate, PyExc_TypeError,
1480 : : "%U() got an unexpected keyword argument '%S'",
1481 : : func->func_qualname, keyword);
1482 : 0 : goto kw_fail;
1483 : : }
1484 : :
1485 [ - + ]: 43879 : if (PyDict_SetItem(kwdict, keyword, value) == -1) {
1486 : 0 : goto kw_fail;
1487 : : }
1488 [ - + ]: 43879 : Py_DECREF(value);
1489 : 43879 : continue;
1490 : :
1491 : 0 : kw_fail:
1492 [ # # ]: 0 : for (;i < kwcount; i++) {
1493 : 0 : PyObject *value = args[i+argcount];
1494 [ # # ]: 0 : Py_DECREF(value);
1495 : : }
1496 : 0 : goto fail_post_args;
1497 : :
1498 : 162455 : kw_found:
1499 [ - + ]: 162455 : if (localsplus[j] != NULL) {
1500 : 0 : _PyErr_Format(tstate, PyExc_TypeError,
1501 : : "%U() got multiple values for argument '%S'",
1502 : : func->func_qualname, keyword);
1503 : 0 : goto kw_fail;
1504 : : }
1505 : 162455 : localsplus[j] = value;
1506 : : }
1507 : : }
1508 : :
1509 : : /* Check the number of positional arguments */
1510 [ + + - + ]: 1424205 : if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
1511 : 0 : too_many_positional(tstate, co, argcount, func->func_defaults, localsplus,
1512 : : func->func_qualname);
1513 : 0 : goto fail_post_args;
1514 : : }
1515 : :
1516 : : /* Add missing positional arguments (copy default values from defs) */
1517 [ + + ]: 1424205 : if (argcount < co->co_argcount) {
1518 [ + + ]: 252898 : Py_ssize_t defcount = func->func_defaults == NULL ? 0 : PyTuple_GET_SIZE(func->func_defaults);
1519 : 252898 : Py_ssize_t m = co->co_argcount - defcount;
1520 : 252898 : Py_ssize_t missing = 0;
1521 [ + + ]: 253417 : for (i = argcount; i < m; i++) {
1522 [ - + ]: 519 : if (localsplus[i] == NULL) {
1523 : 0 : missing++;
1524 : : }
1525 : : }
1526 [ - + ]: 252898 : if (missing) {
1527 : 0 : missing_arguments(tstate, co, missing, defcount, localsplus,
1528 : : func->func_qualname);
1529 : 0 : goto fail_post_args;
1530 : : }
1531 [ + + ]: 252898 : if (n > m)
1532 : 50671 : i = n - m;
1533 : : else
1534 : 202227 : i = 0;
1535 [ + + ]: 252898 : if (defcount) {
1536 : 252854 : PyObject **defs = &PyTuple_GET_ITEM(func->func_defaults, 0);
1537 [ + + ]: 509718 : for (; i < defcount; i++) {
1538 [ + + ]: 256864 : if (localsplus[m+i] == NULL) {
1539 : 103036 : PyObject *def = defs[i];
1540 : 103036 : localsplus[m+i] = Py_NewRef(def);
1541 : : }
1542 : : }
1543 : : }
1544 : : }
1545 : :
1546 : : /* Add missing keyword arguments (copy default values from kwdefs) */
1547 [ + + ]: 1424205 : if (co->co_kwonlyargcount > 0) {
1548 : 12416 : Py_ssize_t missing = 0;
1549 [ + + ]: 30564 : for (i = co->co_argcount; i < total_args; i++) {
1550 [ + + ]: 18148 : if (localsplus[i] != NULL)
1551 : 8108 : continue;
1552 : 10040 : PyObject *varname = PyTuple_GET_ITEM(co->co_localsplusnames, i);
1553 [ + - ]: 10040 : if (func->func_kwdefaults != NULL) {
1554 : 10040 : PyObject *def = PyDict_GetItemWithError(func->func_kwdefaults, varname);
1555 [ + - ]: 10040 : if (def) {
1556 : 10040 : localsplus[i] = Py_NewRef(def);
1557 : 10040 : continue;
1558 : : }
1559 [ # # ]: 0 : else if (_PyErr_Occurred(tstate)) {
1560 : 0 : goto fail_post_args;
1561 : : }
1562 : : }
1563 : 0 : missing++;
1564 : : }
1565 [ - + ]: 12416 : if (missing) {
1566 : 0 : missing_arguments(tstate, co, missing, -1, localsplus,
1567 : : func->func_qualname);
1568 : 0 : goto fail_post_args;
1569 : : }
1570 : : }
1571 : 1424205 : return 0;
1572 : :
1573 : 0 : fail_pre_positional:
1574 [ # # ]: 0 : for (j = 0; j < argcount; j++) {
1575 [ # # ]: 0 : Py_DECREF(args[j]);
1576 : : }
1577 : : /* fall through */
1578 : 0 : fail_post_positional:
1579 [ # # ]: 0 : if (kwnames) {
1580 : 0 : Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames);
1581 [ # # ]: 0 : for (j = argcount; j < argcount+kwcount; j++) {
1582 [ # # ]: 0 : Py_DECREF(args[j]);
1583 : : }
1584 : : }
1585 : : /* fall through */
1586 : 0 : fail_post_args:
1587 : 0 : return -1;
1588 : : }
1589 : :
1590 : : static void
1591 : 3485728 : clear_thread_frame(PyThreadState *tstate, _PyInterpreterFrame * frame)
1592 : : {
1593 : : assert(frame->owner == FRAME_OWNED_BY_THREAD);
1594 : : // Make sure that this is, indeed, the top frame. We can't check this in
1595 : : // _PyThreadState_PopFrame, since f_code is already cleared at that point:
1596 : : assert((PyObject **)frame + frame->f_code->co_framesize ==
1597 : : tstate->datastack_top);
1598 : 3485728 : tstate->c_recursion_remaining--;
1599 : : assert(frame->frame_obj == NULL || frame->frame_obj->f_frame == frame);
1600 : 3485728 : _PyFrame_ClearExceptCode(frame);
1601 [ - + ]: 3485728 : Py_DECREF(frame->f_code);
1602 : 3485728 : tstate->c_recursion_remaining++;
1603 : 3485728 : _PyThreadState_PopFrame(tstate, frame);
1604 : 3485728 : }
1605 : :
1606 : : static void
1607 : 122330 : clear_gen_frame(PyThreadState *tstate, _PyInterpreterFrame * frame)
1608 : : {
1609 : : assert(frame->owner == FRAME_OWNED_BY_GENERATOR);
1610 : 122330 : PyGenObject *gen = _PyFrame_GetGenerator(frame);
1611 : 122330 : gen->gi_frame_state = FRAME_CLEARED;
1612 : : assert(tstate->exc_info == &gen->gi_exc_state);
1613 : 122330 : tstate->exc_info = gen->gi_exc_state.previous_item;
1614 : 122330 : gen->gi_exc_state.previous_item = NULL;
1615 : 122330 : tstate->c_recursion_remaining--;
1616 : : assert(frame->frame_obj == NULL || frame->frame_obj->f_frame == frame);
1617 : 122330 : _PyFrame_ClearExceptCode(frame);
1618 : 122330 : tstate->c_recursion_remaining++;
1619 : 122330 : frame->previous = NULL;
1620 : 122330 : }
1621 : :
1622 : : static void
1623 : 3608058 : _PyEvalFrameClearAndPop(PyThreadState *tstate, _PyInterpreterFrame * frame)
1624 : : {
1625 [ + + ]: 3608058 : if (frame->owner == FRAME_OWNED_BY_THREAD) {
1626 : 3485728 : clear_thread_frame(tstate, frame);
1627 : : }
1628 : : else {
1629 : 122330 : clear_gen_frame(tstate, frame);
1630 : : }
1631 : 3608058 : }
1632 : :
1633 : : /* Consumes references to func, locals and all the args */
1634 : : static _PyInterpreterFrame *
1635 : 1424205 : _PyEvalFramePushAndInit(PyThreadState *tstate, PyFunctionObject *func,
1636 : : PyObject *locals, PyObject* const* args,
1637 : : size_t argcount, PyObject *kwnames)
1638 : : {
1639 : 1424205 : PyCodeObject * code = (PyCodeObject *)func->func_code;
1640 : : CALL_STAT_INC(frames_pushed);
1641 : 1424205 : _PyInterpreterFrame *frame = _PyThreadState_PushFrame(tstate, code->co_framesize);
1642 [ - + ]: 1424205 : if (frame == NULL) {
1643 : 0 : goto fail;
1644 : : }
1645 : 1424205 : _PyFrame_Initialize(frame, func, locals, code, 0);
1646 [ - + ]: 1424205 : if (initialize_locals(tstate, func, frame->localsplus, args, argcount, kwnames)) {
1647 : : assert(frame->owner == FRAME_OWNED_BY_THREAD);
1648 : 0 : clear_thread_frame(tstate, frame);
1649 : 0 : return NULL;
1650 : : }
1651 : 1424205 : return frame;
1652 : 0 : fail:
1653 : : /* Consume the references */
1654 [ # # ]: 0 : for (size_t i = 0; i < argcount; i++) {
1655 [ # # ]: 0 : Py_DECREF(args[i]);
1656 : : }
1657 [ # # ]: 0 : if (kwnames) {
1658 : 0 : Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames);
1659 [ # # ]: 0 : for (Py_ssize_t i = 0; i < kwcount; i++) {
1660 [ # # ]: 0 : Py_DECREF(args[i+argcount]);
1661 : : }
1662 : : }
1663 : 0 : PyErr_NoMemory();
1664 : 0 : return NULL;
1665 : : }
1666 : :
1667 : : PyObject *
1668 : 1130299 : _PyEval_Vector(PyThreadState *tstate, PyFunctionObject *func,
1669 : : PyObject *locals,
1670 : : PyObject* const* args, size_t argcount,
1671 : : PyObject *kwnames)
1672 : : {
1673 : : /* _PyEvalFramePushAndInit consumes the references
1674 : : * to func, locals and all its arguments */
1675 : 1130299 : Py_INCREF(func);
1676 : 1130299 : Py_XINCREF(locals);
1677 [ + + ]: 3230951 : for (size_t i = 0; i < argcount; i++) {
1678 : 2100652 : Py_INCREF(args[i]);
1679 : : }
1680 [ + + ]: 1130299 : if (kwnames) {
1681 : 30413 : Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames);
1682 [ + + ]: 62341 : for (Py_ssize_t i = 0; i < kwcount; i++) {
1683 : 31928 : Py_INCREF(args[i+argcount]);
1684 : : }
1685 : : }
1686 : 1130299 : _PyInterpreterFrame *frame = _PyEvalFramePushAndInit(
1687 : : tstate, func, locals, args, argcount, kwnames);
1688 [ - + ]: 1130299 : if (frame == NULL) {
1689 : 0 : return NULL;
1690 : : }
1691 : : EVAL_CALL_STAT_INC(EVAL_CALL_VECTOR);
1692 : 1130299 : return _PyEval_EvalFrame(tstate, frame, 0);
1693 : : }
1694 : :
1695 : : /* Legacy API */
1696 : : PyObject *
1697 : 0 : PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
1698 : : PyObject *const *args, int argcount,
1699 : : PyObject *const *kws, int kwcount,
1700 : : PyObject *const *defs, int defcount,
1701 : : PyObject *kwdefs, PyObject *closure)
1702 : : {
1703 : 0 : PyThreadState *tstate = _PyThreadState_GET();
1704 : 0 : PyObject *res = NULL;
1705 : 0 : PyObject *defaults = _PyTuple_FromArray(defs, defcount);
1706 [ # # ]: 0 : if (defaults == NULL) {
1707 : 0 : return NULL;
1708 : : }
1709 : 0 : PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
1710 [ # # ]: 0 : if (builtins == NULL) {
1711 [ # # ]: 0 : Py_DECREF(defaults);
1712 : 0 : return NULL;
1713 : : }
1714 [ # # ]: 0 : if (locals == NULL) {
1715 : 0 : locals = globals;
1716 : : }
1717 : 0 : PyObject *kwnames = NULL;
1718 : : PyObject *const *allargs;
1719 : 0 : PyObject **newargs = NULL;
1720 : 0 : PyFunctionObject *func = NULL;
1721 [ # # ]: 0 : if (kwcount == 0) {
1722 : 0 : allargs = args;
1723 : : }
1724 : : else {
1725 : 0 : kwnames = PyTuple_New(kwcount);
1726 [ # # ]: 0 : if (kwnames == NULL) {
1727 : 0 : goto fail;
1728 : : }
1729 : 0 : newargs = PyMem_Malloc(sizeof(PyObject *)*(kwcount+argcount));
1730 [ # # ]: 0 : if (newargs == NULL) {
1731 : 0 : goto fail;
1732 : : }
1733 [ # # ]: 0 : for (int i = 0; i < argcount; i++) {
1734 : 0 : newargs[i] = args[i];
1735 : : }
1736 [ # # ]: 0 : for (int i = 0; i < kwcount; i++) {
1737 : 0 : PyTuple_SET_ITEM(kwnames, i, Py_NewRef(kws[2*i]));
1738 : 0 : newargs[argcount+i] = kws[2*i+1];
1739 : : }
1740 : 0 : allargs = newargs;
1741 : : }
1742 : 0 : PyFrameConstructor constr = {
1743 : : .fc_globals = globals,
1744 : : .fc_builtins = builtins,
1745 : 0 : .fc_name = ((PyCodeObject *)_co)->co_name,
1746 : 0 : .fc_qualname = ((PyCodeObject *)_co)->co_name,
1747 : : .fc_code = _co,
1748 : : .fc_defaults = defaults,
1749 : : .fc_kwdefaults = kwdefs,
1750 : : .fc_closure = closure
1751 : : };
1752 : 0 : func = _PyFunction_FromConstructor(&constr);
1753 [ # # ]: 0 : if (func == NULL) {
1754 : 0 : goto fail;
1755 : : }
1756 : : EVAL_CALL_STAT_INC(EVAL_CALL_LEGACY);
1757 : 0 : res = _PyEval_Vector(tstate, func, locals,
1758 : : allargs, argcount,
1759 : : kwnames);
1760 : 0 : fail:
1761 [ # # # # ]: 0 : Py_XDECREF(func);
1762 [ # # # # ]: 0 : Py_XDECREF(kwnames);
1763 : 0 : PyMem_Free(newargs);
1764 [ # # ]: 0 : Py_DECREF(defaults);
1765 : 0 : return res;
1766 : : }
1767 : :
1768 : :
1769 : : /* Logic for the raise statement (too complicated for inlining).
1770 : : This *consumes* a reference count to each of its arguments. */
1771 : : static int
1772 : 413 : do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
1773 : : {
1774 : 413 : PyObject *type = NULL, *value = NULL;
1775 : :
1776 [ + + ]: 413 : if (exc == NULL) {
1777 : : /* Reraise */
1778 : 2 : _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
1779 : 2 : exc = exc_info->exc_value;
1780 [ + - - + ]: 2 : if (Py_IsNone(exc) || exc == NULL) {
1781 : 0 : _PyErr_SetString(tstate, PyExc_RuntimeError,
1782 : : "No active exception to reraise");
1783 : 0 : return 0;
1784 : : }
1785 : 2 : Py_INCREF(exc);
1786 : : assert(PyExceptionInstance_Check(exc));
1787 : 2 : _PyErr_SetRaisedException(tstate, exc);
1788 : 2 : return 1;
1789 : : }
1790 : :
1791 : : /* We support the following forms of raise:
1792 : : raise
1793 : : raise <instance>
1794 : : raise <type> */
1795 : :
1796 [ + + + - ]: 411 : if (PyExceptionClass_Check(exc)) {
1797 : 22 : type = exc;
1798 : 22 : value = _PyObject_CallNoArgs(exc);
1799 [ - + ]: 22 : if (value == NULL)
1800 : 0 : goto raise_error;
1801 [ - + ]: 22 : if (!PyExceptionInstance_Check(value)) {
1802 : 0 : _PyErr_Format(tstate, PyExc_TypeError,
1803 : : "calling %R should have returned an instance of "
1804 : : "BaseException, not %R",
1805 : : type, Py_TYPE(value));
1806 : 0 : goto raise_error;
1807 : : }
1808 : : }
1809 [ + - ]: 389 : else if (PyExceptionInstance_Check(exc)) {
1810 : 389 : value = exc;
1811 : 389 : type = PyExceptionInstance_Class(exc);
1812 : 389 : Py_INCREF(type);
1813 : : }
1814 : : else {
1815 : : /* Not something you can raise. You get an exception
1816 : : anyway, just not what you specified :-) */
1817 [ # # ]: 0 : Py_DECREF(exc);
1818 : 0 : _PyErr_SetString(tstate, PyExc_TypeError,
1819 : : "exceptions must derive from BaseException");
1820 : 0 : goto raise_error;
1821 : : }
1822 : :
1823 : : assert(type != NULL);
1824 : : assert(value != NULL);
1825 : :
1826 [ + + ]: 411 : if (cause) {
1827 : : PyObject *fixed_cause;
1828 [ - + - - ]: 93 : if (PyExceptionClass_Check(cause)) {
1829 : 0 : fixed_cause = _PyObject_CallNoArgs(cause);
1830 [ # # ]: 0 : if (fixed_cause == NULL)
1831 : 0 : goto raise_error;
1832 [ # # ]: 0 : Py_DECREF(cause);
1833 : : }
1834 [ - + ]: 93 : else if (PyExceptionInstance_Check(cause)) {
1835 : 0 : fixed_cause = cause;
1836 : : }
1837 [ + - ]: 93 : else if (Py_IsNone(cause)) {
1838 [ - + ]: 93 : Py_DECREF(cause);
1839 : 93 : fixed_cause = NULL;
1840 : : }
1841 : : else {
1842 : 0 : _PyErr_SetString(tstate, PyExc_TypeError,
1843 : : "exception causes must derive from "
1844 : : "BaseException");
1845 : 0 : goto raise_error;
1846 : : }
1847 : 93 : PyException_SetCause(value, fixed_cause);
1848 : : }
1849 : :
1850 : 411 : _PyErr_SetObject(tstate, type, value);
1851 : : /* _PyErr_SetObject incref's its arguments */
1852 [ - + ]: 411 : Py_DECREF(value);
1853 [ - + ]: 411 : Py_DECREF(type);
1854 : 411 : return 0;
1855 : :
1856 : 0 : raise_error:
1857 [ # # # # ]: 0 : Py_XDECREF(value);
1858 [ # # # # ]: 0 : Py_XDECREF(type);
1859 [ # # # # ]: 0 : Py_XDECREF(cause);
1860 : 0 : return 0;
1861 : : }
1862 : :
1863 : : /* Logic for matching an exception in an except* clause (too
1864 : : complicated for inlining).
1865 : : */
1866 : :
1867 : : static int
1868 : 0 : exception_group_match(PyObject* exc_value, PyObject *match_type,
1869 : : PyObject **match, PyObject **rest)
1870 : : {
1871 [ # # ]: 0 : if (Py_IsNone(exc_value)) {
1872 : 0 : *match = Py_NewRef(Py_None);
1873 : 0 : *rest = Py_NewRef(Py_None);
1874 : 0 : return 0;
1875 : : }
1876 : : assert(PyExceptionInstance_Check(exc_value));
1877 : :
1878 [ # # ]: 0 : if (PyErr_GivenExceptionMatches(exc_value, match_type)) {
1879 : : /* Full match of exc itself */
1880 : 0 : bool is_eg = _PyBaseExceptionGroup_Check(exc_value);
1881 [ # # ]: 0 : if (is_eg) {
1882 : 0 : *match = Py_NewRef(exc_value);
1883 : : }
1884 : : else {
1885 : : /* naked exception - wrap it */
1886 : 0 : PyObject *excs = PyTuple_Pack(1, exc_value);
1887 [ # # ]: 0 : if (excs == NULL) {
1888 : 0 : return -1;
1889 : : }
1890 : 0 : PyObject *wrapped = _PyExc_CreateExceptionGroup("", excs);
1891 [ # # ]: 0 : Py_DECREF(excs);
1892 [ # # ]: 0 : if (wrapped == NULL) {
1893 : 0 : return -1;
1894 : : }
1895 : 0 : *match = wrapped;
1896 : : }
1897 : 0 : *rest = Py_NewRef(Py_None);
1898 : 0 : return 0;
1899 : : }
1900 : :
1901 : : /* exc_value does not match match_type.
1902 : : * Check for partial match if it's an exception group.
1903 : : */
1904 [ # # ]: 0 : if (_PyBaseExceptionGroup_Check(exc_value)) {
1905 : 0 : PyObject *pair = PyObject_CallMethod(exc_value, "split", "(O)",
1906 : : match_type);
1907 [ # # ]: 0 : if (pair == NULL) {
1908 : 0 : return -1;
1909 : : }
1910 : : assert(PyTuple_CheckExact(pair));
1911 : : assert(PyTuple_GET_SIZE(pair) == 2);
1912 : 0 : *match = Py_NewRef(PyTuple_GET_ITEM(pair, 0));
1913 : 0 : *rest = Py_NewRef(PyTuple_GET_ITEM(pair, 1));
1914 [ # # ]: 0 : Py_DECREF(pair);
1915 : 0 : return 0;
1916 : : }
1917 : : /* no match */
1918 : 0 : *match = Py_NewRef(Py_None);
1919 : 0 : *rest = Py_NewRef(exc_value);
1920 : 0 : return 0;
1921 : : }
1922 : :
1923 : : /* Iterate v argcnt times and store the results on the stack (via decreasing
1924 : : sp). Return 1 for success, 0 if error.
1925 : :
1926 : : If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
1927 : : with a variable target.
1928 : : */
1929 : :
1930 : : static int
1931 : 2665 : unpack_iterable(PyThreadState *tstate, PyObject *v,
1932 : : int argcnt, int argcntafter, PyObject **sp)
1933 : : {
1934 : 2665 : int i = 0, j = 0;
1935 : 2665 : Py_ssize_t ll = 0;
1936 : : PyObject *it; /* iter(v) */
1937 : : PyObject *w;
1938 : 2665 : PyObject *l = NULL; /* variable list */
1939 : :
1940 : : assert(v != NULL);
1941 : :
1942 : 2665 : it = PyObject_GetIter(v);
1943 [ - + ]: 2665 : if (it == NULL) {
1944 [ # # ]: 0 : if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
1945 [ # # # # ]: 0 : Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
1946 : : {
1947 : 0 : _PyErr_Format(tstate, PyExc_TypeError,
1948 : : "cannot unpack non-iterable %.200s object",
1949 : 0 : Py_TYPE(v)->tp_name);
1950 : : }
1951 : 0 : return 0;
1952 : : }
1953 : :
1954 [ + + ]: 7807 : for (; i < argcnt; i++) {
1955 : 5142 : w = PyIter_Next(it);
1956 [ - + ]: 5142 : if (w == NULL) {
1957 : : /* Iterator done, via error or exhaustion. */
1958 [ # # ]: 0 : if (!_PyErr_Occurred(tstate)) {
1959 [ # # ]: 0 : if (argcntafter == -1) {
1960 : 0 : _PyErr_Format(tstate, PyExc_ValueError,
1961 : : "not enough values to unpack "
1962 : : "(expected %d, got %d)",
1963 : : argcnt, i);
1964 : : }
1965 : : else {
1966 : 0 : _PyErr_Format(tstate, PyExc_ValueError,
1967 : : "not enough values to unpack "
1968 : : "(expected at least %d, got %d)",
1969 : : argcnt + argcntafter, i);
1970 : : }
1971 : : }
1972 : 0 : goto Error;
1973 : : }
1974 : 5142 : *--sp = w;
1975 : : }
1976 : :
1977 [ + + ]: 2665 : if (argcntafter == -1) {
1978 : : /* We better have exhausted the iterator now. */
1979 : 671 : w = PyIter_Next(it);
1980 [ + - ]: 671 : if (w == NULL) {
1981 [ - + ]: 671 : if (_PyErr_Occurred(tstate))
1982 : 0 : goto Error;
1983 [ + - ]: 671 : Py_DECREF(it);
1984 : 671 : return 1;
1985 : : }
1986 [ # # ]: 0 : Py_DECREF(w);
1987 : 0 : _PyErr_Format(tstate, PyExc_ValueError,
1988 : : "too many values to unpack (expected %d)",
1989 : : argcnt);
1990 : 0 : goto Error;
1991 : : }
1992 : :
1993 : 1994 : l = PySequence_List(it);
1994 [ - + ]: 1994 : if (l == NULL)
1995 : 0 : goto Error;
1996 : 1994 : *--sp = l;
1997 : 1994 : i++;
1998 : :
1999 : 1994 : ll = PyList_GET_SIZE(l);
2000 [ - + ]: 1994 : if (ll < argcntafter) {
2001 : 0 : _PyErr_Format(tstate, PyExc_ValueError,
2002 : : "not enough values to unpack (expected at least %d, got %zd)",
2003 : : argcnt + argcntafter, argcnt + ll);
2004 : 0 : goto Error;
2005 : : }
2006 : :
2007 : : /* Pop the "after-variable" args off the list. */
2008 [ - + ]: 1994 : for (j = argcntafter; j > 0; j--, i++) {
2009 : 0 : *--sp = PyList_GET_ITEM(l, ll - j);
2010 : : }
2011 : : /* Resize the list. */
2012 : 1994 : Py_SET_SIZE(l, ll - argcntafter);
2013 [ + - ]: 1994 : Py_DECREF(it);
2014 : 1994 : return 1;
2015 : :
2016 : 0 : Error:
2017 [ # # ]: 0 : for (; i > 0; i--, sp++)
2018 [ # # ]: 0 : Py_DECREF(*sp);
2019 [ # # # # ]: 0 : Py_XDECREF(it);
2020 : 0 : return 0;
2021 : : }
2022 : :
2023 : : static void
2024 : 0 : call_exc_trace(Py_tracefunc func, PyObject *self,
2025 : : PyThreadState *tstate,
2026 : : _PyInterpreterFrame *f)
2027 : : {
2028 : 0 : PyObject *exc = _PyErr_GetRaisedException(tstate);
2029 : : assert(exc && PyExceptionInstance_Check(exc));
2030 : 0 : PyObject *type = PyExceptionInstance_Class(exc);
2031 : 0 : PyObject *traceback = PyException_GetTraceback(exc);
2032 [ # # ]: 0 : if (traceback == NULL) {
2033 : 0 : traceback = Py_NewRef(Py_None);
2034 : : }
2035 : 0 : PyObject *arg = PyTuple_Pack(3, type, exc, traceback);
2036 [ # # # # ]: 0 : Py_XDECREF(traceback);
2037 : :
2038 [ # # ]: 0 : if (arg == NULL) {
2039 : 0 : _PyErr_SetRaisedException(tstate, exc);
2040 : 0 : return;
2041 : : }
2042 : 0 : int err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
2043 [ # # ]: 0 : Py_DECREF(arg);
2044 [ # # ]: 0 : if (err == 0) {
2045 : 0 : _PyErr_SetRaisedException(tstate, exc);
2046 : : }
2047 : : else {
2048 [ # # # # ]: 0 : Py_XDECREF(exc);
2049 : : }
2050 : : }
2051 : :
2052 : : static int
2053 : 0 : call_trace_protected(Py_tracefunc func, PyObject *obj,
2054 : : PyThreadState *tstate, _PyInterpreterFrame *frame,
2055 : : int what, PyObject *arg)
2056 : : {
2057 : 0 : PyObject *exc = _PyErr_GetRaisedException(tstate);
2058 : 0 : int err = call_trace(func, obj, tstate, frame, what, arg);
2059 [ # # ]: 0 : if (err == 0)
2060 : : {
2061 : 0 : _PyErr_SetRaisedException(tstate, exc);
2062 : 0 : return 0;
2063 : : }
2064 : : else {
2065 [ # # # # ]: 0 : Py_XDECREF(exc);
2066 : 0 : return -1;
2067 : : }
2068 : : }
2069 : :
2070 : : static void
2071 : 0 : initialize_trace_info(PyTraceInfo *trace_info, _PyInterpreterFrame *frame)
2072 : : {
2073 : 0 : PyCodeObject *code = frame->f_code;
2074 [ # # ]: 0 : if (trace_info->code != code) {
2075 : 0 : trace_info->code = code;
2076 : 0 : _PyCode_InitAddressRange(code, &trace_info->bounds);
2077 : : }
2078 : 0 : }
2079 : :
2080 : : void
2081 : 637 : PyThreadState_EnterTracing(PyThreadState *tstate)
2082 : : {
2083 : 637 : tstate->tracing++;
2084 : 637 : tstate->cframe->use_tracing = 0;
2085 : 637 : }
2086 : :
2087 : : void
2088 : 637 : PyThreadState_LeaveTracing(PyThreadState *tstate)
2089 : : {
2090 : : assert(tstate->tracing > 0 && tstate->cframe->use_tracing == 0);
2091 : 637 : tstate->tracing--;
2092 : 637 : _PyThreadState_UpdateTracingState(tstate);
2093 : 637 : }
2094 : :
2095 : : static int
2096 : 0 : call_trace(Py_tracefunc func, PyObject *obj,
2097 : : PyThreadState *tstate, _PyInterpreterFrame *frame,
2098 : : int what, PyObject *arg)
2099 : : {
2100 : : int result;
2101 [ # # ]: 0 : if (tstate->tracing) {
2102 : 0 : return 0;
2103 : : }
2104 : 0 : PyFrameObject *f = _PyFrame_GetFrameObject(frame);
2105 [ # # ]: 0 : if (f == NULL) {
2106 : 0 : return -1;
2107 : : }
2108 : 0 : int old_what = tstate->tracing_what;
2109 : 0 : tstate->tracing_what = what;
2110 : 0 : PyThreadState_EnterTracing(tstate);
2111 : : assert(_PyInterpreterFrame_LASTI(frame) >= 0);
2112 [ # # ]: 0 : if (_PyCode_InitLineArray(frame->f_code)) {
2113 : 0 : return -1;
2114 : : }
2115 : 0 : f->f_lineno = _PyCode_LineNumberFromArray(frame->f_code, _PyInterpreterFrame_LASTI(frame));
2116 : 0 : result = func(obj, f, what, arg);
2117 : 0 : f->f_lineno = 0;
2118 : 0 : PyThreadState_LeaveTracing(tstate);
2119 : 0 : tstate->tracing_what = old_what;
2120 : 0 : return result;
2121 : : }
2122 : :
2123 : : PyObject*
2124 : 0 : _PyEval_CallTracing(PyObject *func, PyObject *args)
2125 : : {
2126 : : // Save and disable tracing
2127 : 0 : PyThreadState *tstate = _PyThreadState_GET();
2128 : 0 : int save_tracing = tstate->tracing;
2129 : 0 : int save_use_tracing = tstate->cframe->use_tracing;
2130 : 0 : tstate->tracing = 0;
2131 : :
2132 : : // Call the tracing function
2133 : 0 : PyObject *result = PyObject_Call(func, args, NULL);
2134 : :
2135 : : // Restore tracing
2136 : 0 : tstate->tracing = save_tracing;
2137 : 0 : tstate->cframe->use_tracing = save_use_tracing;
2138 : 0 : return result;
2139 : : }
2140 : :
2141 : : /* See Objects/lnotab_notes.txt for a description of how tracing works. */
2142 : : static int
2143 : 0 : maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
2144 : : PyThreadState *tstate, _PyInterpreterFrame *frame, int instr_prev)
2145 : : {
2146 : 0 : int result = 0;
2147 : :
2148 : : /* If the last instruction falls at the start of a line or if it
2149 : : represents a jump backwards, update the frame's line number and
2150 : : then call the trace function if we're tracing source lines.
2151 : : */
2152 [ # # ]: 0 : if (_PyCode_InitLineArray(frame->f_code)) {
2153 : 0 : return -1;
2154 : : }
2155 : : int lastline;
2156 [ # # ]: 0 : if (instr_prev <= frame->f_code->_co_firsttraceable) {
2157 : 0 : lastline = -1;
2158 : : }
2159 : : else {
2160 : 0 : lastline = _PyCode_LineNumberFromArray(frame->f_code, instr_prev);
2161 : : }
2162 : 0 : int line = _PyCode_LineNumberFromArray(frame->f_code, _PyInterpreterFrame_LASTI(frame));
2163 : 0 : PyFrameObject *f = _PyFrame_GetFrameObject(frame);
2164 [ # # ]: 0 : if (f == NULL) {
2165 : 0 : return -1;
2166 : : }
2167 [ # # # # ]: 0 : if (line != -1 && f->f_trace_lines) {
2168 : : /* Trace backward edges (except in 'yield from') or if line number has changed */
2169 [ # # ]: 0 : int trace = line != lastline ||
2170 [ # # ]: 0 : (_PyInterpreterFrame_LASTI(frame) < instr_prev &&
2171 : : // SEND has no quickened forms, so no need to use _PyOpcode_Deopt
2172 : : // here:
2173 [ # # ]: 0 : frame->prev_instr->op.code != SEND);
2174 [ # # ]: 0 : if (trace) {
2175 : 0 : result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
2176 : : }
2177 : : }
2178 : : /* Always emit an opcode event if we're tracing all opcodes. */
2179 [ # # # # ]: 0 : if (f->f_trace_opcodes && result == 0) {
2180 : 0 : result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
2181 : : }
2182 : 0 : return result;
2183 : : }
2184 : :
2185 : : int
2186 : 0 : _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
2187 : : {
2188 : : assert(is_tstate_valid(tstate));
2189 : : /* The caller must hold the GIL */
2190 : : assert(PyGILState_Check());
2191 : :
2192 : : /* Call _PySys_Audit() in the context of the current thread state,
2193 : : even if tstate is not the current thread state. */
2194 : 0 : PyThreadState *current_tstate = _PyThreadState_GET();
2195 [ # # ]: 0 : if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
2196 : 0 : return -1;
2197 : : }
2198 : :
2199 : 0 : tstate->c_profilefunc = func;
2200 : 0 : PyObject *old_profileobj = tstate->c_profileobj;
2201 : 0 : tstate->c_profileobj = Py_XNewRef(arg);
2202 : : /* Flag that tracing or profiling is turned on */
2203 : 0 : _PyThreadState_UpdateTracingState(tstate);
2204 : :
2205 : : // gh-98257: Only call Py_XDECREF() once the new profile function is fully
2206 : : // set, so it's safe to call sys.setprofile() again (reentrant call).
2207 [ # # # # ]: 0 : Py_XDECREF(old_profileobj);
2208 : :
2209 : 0 : return 0;
2210 : : }
2211 : :
2212 : : void
2213 : 0 : PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
2214 : : {
2215 : 0 : PyThreadState *tstate = _PyThreadState_GET();
2216 [ # # ]: 0 : if (_PyEval_SetProfile(tstate, func, arg) < 0) {
2217 : : /* Log _PySys_Audit() error */
2218 : 0 : _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
2219 : : }
2220 : 0 : }
2221 : :
2222 : : void
2223 : 0 : PyEval_SetProfileAllThreads(Py_tracefunc func, PyObject *arg)
2224 : : {
2225 : 0 : PyThreadState *this_tstate = _PyThreadState_GET();
2226 : 0 : PyInterpreterState* interp = this_tstate->interp;
2227 : :
2228 : 0 : _PyRuntimeState *runtime = &_PyRuntime;
2229 : 0 : HEAD_LOCK(runtime);
2230 : 0 : PyThreadState* ts = PyInterpreterState_ThreadHead(interp);
2231 : 0 : HEAD_UNLOCK(runtime);
2232 : :
2233 [ # # ]: 0 : while (ts) {
2234 [ # # ]: 0 : if (_PyEval_SetProfile(ts, func, arg) < 0) {
2235 : 0 : _PyErr_WriteUnraisableMsg("in PyEval_SetProfileAllThreads", NULL);
2236 : : }
2237 : 0 : HEAD_LOCK(runtime);
2238 : 0 : ts = PyThreadState_Next(ts);
2239 : 0 : HEAD_UNLOCK(runtime);
2240 : : }
2241 : 0 : }
2242 : :
2243 : : int
2244 : 1 : _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
2245 : : {
2246 : : assert(is_tstate_valid(tstate));
2247 : : /* The caller must hold the GIL */
2248 : : assert(PyGILState_Check());
2249 : :
2250 : : /* Call _PySys_Audit() in the context of the current thread state,
2251 : : even if tstate is not the current thread state. */
2252 : 1 : PyThreadState *current_tstate = _PyThreadState_GET();
2253 [ - + ]: 1 : if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
2254 : 0 : return -1;
2255 : : }
2256 : :
2257 : 1 : tstate->c_tracefunc = func;
2258 : 1 : PyObject *old_traceobj = tstate->c_traceobj;
2259 : 1 : tstate->c_traceobj = Py_XNewRef(arg);
2260 : : /* Flag that tracing or profiling is turned on */
2261 : 1 : _PyThreadState_UpdateTracingState(tstate);
2262 : :
2263 : : // gh-98257: Only call Py_XDECREF() once the new trace function is fully
2264 : : // set, so it's safe to call sys.settrace() again (reentrant call).
2265 [ - + - - ]: 1 : Py_XDECREF(old_traceobj);
2266 : :
2267 : 1 : return 0;
2268 : : }
2269 : :
2270 : : void
2271 : 0 : PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
2272 : : {
2273 : 0 : PyThreadState *tstate = _PyThreadState_GET();
2274 [ # # ]: 0 : if (_PyEval_SetTrace(tstate, func, arg) < 0) {
2275 : : /* Log _PySys_Audit() error */
2276 : 0 : _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
2277 : : }
2278 : 0 : }
2279 : :
2280 : : void
2281 : 0 : PyEval_SetTraceAllThreads(Py_tracefunc func, PyObject *arg)
2282 : : {
2283 : 0 : PyThreadState *this_tstate = _PyThreadState_GET();
2284 : 0 : PyInterpreterState* interp = this_tstate->interp;
2285 : :
2286 : 0 : _PyRuntimeState *runtime = &_PyRuntime;
2287 : 0 : HEAD_LOCK(runtime);
2288 : 0 : PyThreadState* ts = PyInterpreterState_ThreadHead(interp);
2289 : 0 : HEAD_UNLOCK(runtime);
2290 : :
2291 [ # # ]: 0 : while (ts) {
2292 [ # # ]: 0 : if (_PyEval_SetTrace(ts, func, arg) < 0) {
2293 : 0 : _PyErr_WriteUnraisableMsg("in PyEval_SetTraceAllThreads", NULL);
2294 : : }
2295 : 0 : HEAD_LOCK(runtime);
2296 : 0 : ts = PyThreadState_Next(ts);
2297 : 0 : HEAD_UNLOCK(runtime);
2298 : : }
2299 : 0 : }
2300 : :
2301 : : int
2302 : 0 : _PyEval_SetCoroutineOriginTrackingDepth(int depth)
2303 : : {
2304 : 0 : PyThreadState *tstate = _PyThreadState_GET();
2305 [ # # ]: 0 : if (depth < 0) {
2306 : 0 : _PyErr_SetString(tstate, PyExc_ValueError, "depth must be >= 0");
2307 : 0 : return -1;
2308 : : }
2309 : 0 : tstate->coroutine_origin_tracking_depth = depth;
2310 : 0 : return 0;
2311 : : }
2312 : :
2313 : :
2314 : : int
2315 : 0 : _PyEval_GetCoroutineOriginTrackingDepth(void)
2316 : : {
2317 : 0 : PyThreadState *tstate = _PyThreadState_GET();
2318 : 0 : return tstate->coroutine_origin_tracking_depth;
2319 : : }
2320 : :
2321 : : int
2322 : 0 : _PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
2323 : : {
2324 : 0 : PyThreadState *tstate = _PyThreadState_GET();
2325 : :
2326 [ # # ]: 0 : if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
2327 : 0 : return -1;
2328 : : }
2329 : :
2330 [ # # # # ]: 0 : Py_XSETREF(tstate->async_gen_firstiter, Py_XNewRef(firstiter));
2331 : 0 : return 0;
2332 : : }
2333 : :
2334 : : PyObject *
2335 : 0 : _PyEval_GetAsyncGenFirstiter(void)
2336 : : {
2337 : 0 : PyThreadState *tstate = _PyThreadState_GET();
2338 : 0 : return tstate->async_gen_firstiter;
2339 : : }
2340 : :
2341 : : int
2342 : 0 : _PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
2343 : : {
2344 : 0 : PyThreadState *tstate = _PyThreadState_GET();
2345 : :
2346 [ # # ]: 0 : if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
2347 : 0 : return -1;
2348 : : }
2349 : :
2350 [ # # # # ]: 0 : Py_XSETREF(tstate->async_gen_finalizer, Py_XNewRef(finalizer));
2351 : 0 : return 0;
2352 : : }
2353 : :
2354 : : PyObject *
2355 : 0 : _PyEval_GetAsyncGenFinalizer(void)
2356 : : {
2357 : 0 : PyThreadState *tstate = _PyThreadState_GET();
2358 : 0 : return tstate->async_gen_finalizer;
2359 : : }
2360 : :
2361 : : _PyInterpreterFrame *
2362 : 0 : _PyEval_GetFrame(void)
2363 : : {
2364 : 0 : PyThreadState *tstate = _PyThreadState_GET();
2365 : 0 : return _PyThreadState_GetFrame(tstate);
2366 : : }
2367 : :
2368 : : PyFrameObject *
2369 : 0 : PyEval_GetFrame(void)
2370 : : {
2371 : 0 : _PyInterpreterFrame *frame = _PyEval_GetFrame();
2372 [ # # ]: 0 : if (frame == NULL) {
2373 : 0 : return NULL;
2374 : : }
2375 : 0 : PyFrameObject *f = _PyFrame_GetFrameObject(frame);
2376 [ # # ]: 0 : if (f == NULL) {
2377 : 0 : PyErr_Clear();
2378 : : }
2379 : 0 : return f;
2380 : : }
2381 : :
2382 : : PyObject *
2383 : 692 : _PyEval_GetBuiltins(PyThreadState *tstate)
2384 : : {
2385 : 692 : _PyInterpreterFrame *frame = _PyThreadState_GetFrame(tstate);
2386 [ + + ]: 692 : if (frame != NULL) {
2387 : 642 : return frame->f_builtins;
2388 : : }
2389 : 50 : return tstate->interp->builtins;
2390 : : }
2391 : :
2392 : : PyObject *
2393 : 692 : PyEval_GetBuiltins(void)
2394 : : {
2395 : 692 : PyThreadState *tstate = _PyThreadState_GET();
2396 : 692 : return _PyEval_GetBuiltins(tstate);
2397 : : }
2398 : :
2399 : : /* Convenience function to get a builtin from its name */
2400 : : PyObject *
2401 : 1 : _PyEval_GetBuiltin(PyObject *name)
2402 : : {
2403 : 1 : PyThreadState *tstate = _PyThreadState_GET();
2404 : 1 : PyObject *attr = PyDict_GetItemWithError(PyEval_GetBuiltins(), name);
2405 [ + - ]: 1 : if (attr) {
2406 : 1 : Py_INCREF(attr);
2407 : : }
2408 [ # # ]: 0 : else if (!_PyErr_Occurred(tstate)) {
2409 : 0 : _PyErr_SetObject(tstate, PyExc_AttributeError, name);
2410 : : }
2411 : 1 : return attr;
2412 : : }
2413 : :
2414 : : PyObject *
2415 : 0 : _PyEval_GetBuiltinId(_Py_Identifier *name)
2416 : : {
2417 : 0 : return _PyEval_GetBuiltin(_PyUnicode_FromId(name));
2418 : : }
2419 : :
2420 : : PyObject *
2421 : 2 : PyEval_GetLocals(void)
2422 : : {
2423 : 2 : PyThreadState *tstate = _PyThreadState_GET();
2424 : 2 : _PyInterpreterFrame *current_frame = _PyThreadState_GetFrame(tstate);
2425 [ - + ]: 2 : if (current_frame == NULL) {
2426 : 0 : _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
2427 : 0 : return NULL;
2428 : : }
2429 : :
2430 [ - + ]: 2 : if (_PyFrame_FastToLocalsWithError(current_frame) < 0) {
2431 : 0 : return NULL;
2432 : : }
2433 : :
2434 : 2 : PyObject *locals = current_frame->f_locals;
2435 : : assert(locals != NULL);
2436 : 2 : return locals;
2437 : : }
2438 : :
2439 : : PyObject *
2440 : 1357 : PyEval_GetGlobals(void)
2441 : : {
2442 : 1357 : PyThreadState *tstate = _PyThreadState_GET();
2443 : 1357 : _PyInterpreterFrame *current_frame = _PyThreadState_GetFrame(tstate);
2444 [ + + ]: 1357 : if (current_frame == NULL) {
2445 : 180 : return NULL;
2446 : : }
2447 : 1177 : return current_frame->f_globals;
2448 : : }
2449 : :
2450 : : int
2451 : 89 : PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
2452 : : {
2453 : 89 : PyThreadState *tstate = _PyThreadState_GET();
2454 : 89 : _PyInterpreterFrame *current_frame = tstate->cframe->current_frame;
2455 : 89 : int result = cf->cf_flags != 0;
2456 : :
2457 [ + - ]: 89 : if (current_frame != NULL) {
2458 : 89 : const int codeflags = current_frame->f_code->co_flags;
2459 : 89 : const int compilerflags = codeflags & PyCF_MASK;
2460 [ - + ]: 89 : if (compilerflags) {
2461 : 0 : result = 1;
2462 : 0 : cf->cf_flags |= compilerflags;
2463 : : }
2464 : : }
2465 : 89 : return result;
2466 : : }
2467 : :
2468 : :
2469 : : const char *
2470 : 0 : PyEval_GetFuncName(PyObject *func)
2471 : : {
2472 [ # # ]: 0 : if (PyMethod_Check(func))
2473 : 0 : return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
2474 [ # # ]: 0 : else if (PyFunction_Check(func))
2475 : 0 : return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
2476 [ # # ]: 0 : else if (PyCFunction_Check(func))
2477 : 0 : return ((PyCFunctionObject*)func)->m_ml->ml_name;
2478 : : else
2479 : 0 : return Py_TYPE(func)->tp_name;
2480 : : }
2481 : :
2482 : : const char *
2483 : 0 : PyEval_GetFuncDesc(PyObject *func)
2484 : : {
2485 [ # # ]: 0 : if (PyMethod_Check(func))
2486 : 0 : return "()";
2487 [ # # ]: 0 : else if (PyFunction_Check(func))
2488 : 0 : return "()";
2489 [ # # ]: 0 : else if (PyCFunction_Check(func))
2490 : 0 : return "()";
2491 : : else
2492 : 0 : return " object";
2493 : : }
2494 : :
2495 : : #define C_TRACE(x, call) \
2496 : : if (use_tracing && tstate->c_profilefunc) { \
2497 : : if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
2498 : : tstate, tstate->cframe->current_frame, \
2499 : : PyTrace_C_CALL, func)) { \
2500 : : x = NULL; \
2501 : : } \
2502 : : else { \
2503 : : x = call; \
2504 : : if (tstate->c_profilefunc != NULL) { \
2505 : : if (x == NULL) { \
2506 : : call_trace_protected(tstate->c_profilefunc, \
2507 : : tstate->c_profileobj, \
2508 : : tstate, tstate->cframe->current_frame, \
2509 : : PyTrace_C_EXCEPTION, func); \
2510 : : /* XXX should pass (type, value, tb) */ \
2511 : : } else { \
2512 : : if (call_trace(tstate->c_profilefunc, \
2513 : : tstate->c_profileobj, \
2514 : : tstate, tstate->cframe->current_frame, \
2515 : : PyTrace_C_RETURN, func)) { \
2516 : : Py_DECREF(x); \
2517 : : x = NULL; \
2518 : : } \
2519 : : } \
2520 : : } \
2521 : : } \
2522 : : } else { \
2523 : : x = call; \
2524 : : }
2525 : :
2526 : :
2527 : : static PyObject *
2528 : 0 : trace_call_function(PyThreadState *tstate,
2529 : : PyObject *func,
2530 : : PyObject **args, Py_ssize_t nargs,
2531 : : PyObject *kwnames)
2532 : : {
2533 : 0 : int use_tracing = 1;
2534 : : PyObject *x;
2535 [ # # # # ]: 0 : if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
2536 [ # # # # : 0 : C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
# # # # #
# # # #
# ]
2537 : 0 : return x;
2538 : : }
2539 [ # # # # ]: 0 : else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
2540 : : /* We need to create a temporary bound method as argument
2541 : : for profiling.
2542 : :
2543 : : If nargs == 0, then this cannot work because we have no
2544 : : "self". In any case, the call itself would raise
2545 : : TypeError (foo needs an argument), so we just skip
2546 : : profiling. */
2547 : 0 : PyObject *self = args[0];
2548 : 0 : func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
2549 [ # # ]: 0 : if (func == NULL) {
2550 : 0 : return NULL;
2551 : : }
2552 [ # # # # : 0 : C_TRACE(x, PyObject_Vectorcall(func,
# # # # #
# # # #
# ]
2553 : : args+1, nargs-1,
2554 : : kwnames));
2555 [ # # ]: 0 : Py_DECREF(func);
2556 : 0 : return x;
2557 : : }
2558 : 0 : return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
2559 : : }
2560 : :
2561 : : static PyObject *
2562 : 103224 : do_call_core(PyThreadState *tstate,
2563 : : PyObject *func,
2564 : : PyObject *callargs,
2565 : : PyObject *kwdict,
2566 : : int use_tracing
2567 : : )
2568 : : {
2569 : : PyObject *result;
2570 [ + + - + ]: 103224 : if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
2571 [ - + - - : 4197 : C_TRACE(result, PyObject_Call(func, callargs, kwdict));
- - - - -
- - - -
- ]
2572 : 4197 : return result;
2573 : : }
2574 [ - + ]: 99027 : else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
2575 : 0 : Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
2576 [ # # # # ]: 0 : if (nargs > 0 && use_tracing) {
2577 : : /* We need to create a temporary bound method as argument
2578 : : for profiling.
2579 : :
2580 : : If nargs == 0, then this cannot work because we have no
2581 : : "self". In any case, the call itself would raise
2582 : : TypeError (foo needs an argument), so we just skip
2583 : : profiling. */
2584 : 0 : PyObject *self = PyTuple_GET_ITEM(callargs, 0);
2585 : 0 : func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
2586 [ # # ]: 0 : if (func == NULL) {
2587 : 0 : return NULL;
2588 : : }
2589 : :
2590 [ # # # # : 0 : C_TRACE(result, _PyObject_FastCallDictTstate(
# # # # #
# # # #
# ]
2591 : : tstate, func,
2592 : : &_PyTuple_ITEMS(callargs)[1],
2593 : : nargs - 1,
2594 : : kwdict));
2595 [ # # ]: 0 : Py_DECREF(func);
2596 : 0 : return result;
2597 : : }
2598 : : }
2599 : : EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_FUNCTION_EX, func);
2600 : 99027 : return PyObject_Call(func, callargs, kwdict);
2601 : : }
2602 : :
2603 : : /* Extract a slice index from a PyLong or an object with the
2604 : : nb_index slot defined, and store in *pi.
2605 : : Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
2606 : : and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
2607 : : Return 0 on error, 1 on success.
2608 : : */
2609 : : int
2610 : 150047 : _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
2611 : : {
2612 : 150047 : PyThreadState *tstate = _PyThreadState_GET();
2613 [ + - ]: 150047 : if (!Py_IsNone(v)) {
2614 : : Py_ssize_t x;
2615 [ + - ]: 150047 : if (_PyIndex_Check(v)) {
2616 : 150047 : x = PyNumber_AsSsize_t(v, NULL);
2617 [ + + - + ]: 150047 : if (x == -1 && _PyErr_Occurred(tstate))
2618 : 0 : return 0;
2619 : : }
2620 : : else {
2621 : 0 : _PyErr_SetString(tstate, PyExc_TypeError,
2622 : : "slice indices must be integers or "
2623 : : "None or have an __index__ method");
2624 : 0 : return 0;
2625 : : }
2626 : 150047 : *pi = x;
2627 : : }
2628 : 150047 : return 1;
2629 : : }
2630 : :
2631 : : int
2632 : 0 : _PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
2633 : : {
2634 : 0 : PyThreadState *tstate = _PyThreadState_GET();
2635 : : Py_ssize_t x;
2636 [ # # ]: 0 : if (_PyIndex_Check(v)) {
2637 : 0 : x = PyNumber_AsSsize_t(v, NULL);
2638 [ # # # # ]: 0 : if (x == -1 && _PyErr_Occurred(tstate))
2639 : 0 : return 0;
2640 : : }
2641 : : else {
2642 : 0 : _PyErr_SetString(tstate, PyExc_TypeError,
2643 : : "slice indices must be integers or "
2644 : : "have an __index__ method");
2645 : 0 : return 0;
2646 : : }
2647 : 0 : *pi = x;
2648 : 0 : return 1;
2649 : : }
2650 : :
2651 : : static PyObject *
2652 : 3157 : import_name(PyThreadState *tstate, _PyInterpreterFrame *frame,
2653 : : PyObject *name, PyObject *fromlist, PyObject *level)
2654 : : {
2655 : : PyObject *import_func, *res;
2656 : : PyObject* stack[5];
2657 : :
2658 : 3157 : import_func = _PyDict_GetItemWithError(frame->f_builtins, &_Py_ID(__import__));
2659 [ - + ]: 3157 : if (import_func == NULL) {
2660 [ # # ]: 0 : if (!_PyErr_Occurred(tstate)) {
2661 : 0 : _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
2662 : : }
2663 : 0 : return NULL;
2664 : : }
2665 : 3157 : PyObject *locals = frame->f_locals;
2666 : : /* Fast path for not overloaded __import__. */
2667 [ + - ]: 3157 : if (_PyImport_IsDefaultImportFunc(tstate->interp, import_func)) {
2668 : 3157 : int ilevel = _PyLong_AsInt(level);
2669 [ - + - - ]: 3157 : if (ilevel == -1 && _PyErr_Occurred(tstate)) {
2670 : 0 : return NULL;
2671 : : }
2672 [ + + ]: 3157 : res = PyImport_ImportModuleLevelObject(
2673 : : name,
2674 : : frame->f_globals,
2675 : : locals == NULL ? Py_None :locals,
2676 : : fromlist,
2677 : : ilevel);
2678 : 3157 : return res;
2679 : : }
2680 : :
2681 : 0 : Py_INCREF(import_func);
2682 : :
2683 : 0 : stack[0] = name;
2684 : 0 : stack[1] = frame->f_globals;
2685 [ # # ]: 0 : stack[2] = locals == NULL ? Py_None : locals;
2686 : 0 : stack[3] = fromlist;
2687 : 0 : stack[4] = level;
2688 : 0 : res = _PyObject_FastCall(import_func, stack, 5);
2689 [ # # ]: 0 : Py_DECREF(import_func);
2690 : 0 : return res;
2691 : : }
2692 : :
2693 : : static PyObject *
2694 : 2022 : import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
2695 : : {
2696 : : PyObject *x;
2697 : : PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
2698 : :
2699 [ + + ]: 2022 : if (_PyObject_LookupAttr(v, name, &x) != 0) {
2700 : 1993 : return x;
2701 : : }
2702 : : /* Issue #17636: in case this failed because of a circular relative
2703 : : import, try to fallback on reading the module directly from
2704 : : sys.modules. */
2705 : 29 : pkgname = PyObject_GetAttr(v, &_Py_ID(__name__));
2706 [ - + ]: 29 : if (pkgname == NULL) {
2707 : 0 : goto error;
2708 : : }
2709 [ - + ]: 29 : if (!PyUnicode_Check(pkgname)) {
2710 [ # # # # ]: 0 : Py_CLEAR(pkgname);
2711 : 0 : goto error;
2712 : : }
2713 : 29 : fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
2714 [ - + ]: 29 : if (fullmodname == NULL) {
2715 [ # # ]: 0 : Py_DECREF(pkgname);
2716 : 0 : return NULL;
2717 : : }
2718 : 29 : x = PyImport_GetModule(fullmodname);
2719 [ + - ]: 29 : Py_DECREF(fullmodname);
2720 [ + + + - ]: 29 : if (x == NULL && !_PyErr_Occurred(tstate)) {
2721 : 28 : goto error;
2722 : : }
2723 [ - + ]: 1 : Py_DECREF(pkgname);
2724 : 1 : return x;
2725 : 28 : error:
2726 : 28 : pkgpath = PyModule_GetFilenameObject(v);
2727 [ - + ]: 28 : if (pkgname == NULL) {
2728 : 0 : pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
2729 [ # # ]: 0 : if (pkgname_or_unknown == NULL) {
2730 [ # # # # ]: 0 : Py_XDECREF(pkgpath);
2731 : 0 : return NULL;
2732 : : }
2733 : : } else {
2734 : 28 : pkgname_or_unknown = pkgname;
2735 : : }
2736 : :
2737 [ + + - + ]: 28 : if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
2738 : 25 : _PyErr_Clear(tstate);
2739 : 25 : errmsg = PyUnicode_FromFormat(
2740 : : "cannot import name %R from %R (unknown location)",
2741 : : name, pkgname_or_unknown
2742 : : );
2743 : : /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
2744 : 25 : _PyErr_SetImportErrorWithNameFrom(errmsg, pkgname, NULL, name);
2745 : : }
2746 : : else {
2747 : 3 : PyObject *spec = PyObject_GetAttr(v, &_Py_ID(__spec__));
2748 : 3 : const char *fmt =
2749 : 3 : _PyModuleSpec_IsInitializing(spec) ?
2750 : : "cannot import name %R from partially initialized module %R "
2751 [ + + ]: 3 : "(most likely due to a circular import) (%S)" :
2752 : : "cannot import name %R from %R (%S)";
2753 [ + - - + ]: 3 : Py_XDECREF(spec);
2754 : :
2755 : 3 : errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
2756 : : /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
2757 : 3 : _PyErr_SetImportErrorWithNameFrom(errmsg, pkgname, pkgpath, name);
2758 : : }
2759 : :
2760 [ + - - + ]: 28 : Py_XDECREF(errmsg);
2761 [ + - - + ]: 28 : Py_XDECREF(pkgname_or_unknown);
2762 [ + + - + ]: 28 : Py_XDECREF(pkgpath);
2763 : 28 : return NULL;
2764 : : }
2765 : :
2766 : : #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
2767 : : "BaseException is not allowed"
2768 : :
2769 : : #define CANNOT_EXCEPT_STAR_EG "catching ExceptionGroup with except* "\
2770 : : "is not allowed. Use except instead."
2771 : :
2772 : : static int
2773 : 101696 : check_except_type_valid(PyThreadState *tstate, PyObject* right)
2774 : : {
2775 [ + + ]: 101696 : if (PyTuple_Check(right)) {
2776 : : Py_ssize_t i, length;
2777 : 289 : length = PyTuple_GET_SIZE(right);
2778 [ + + ]: 867 : for (i = 0; i < length; i++) {
2779 : 578 : PyObject *exc = PyTuple_GET_ITEM(right, i);
2780 [ + - - + ]: 578 : if (!PyExceptionClass_Check(exc)) {
2781 : 0 : _PyErr_SetString(tstate, PyExc_TypeError,
2782 : : CANNOT_CATCH_MSG);
2783 : 0 : return -1;
2784 : : }
2785 : : }
2786 : : }
2787 : : else {
2788 [ + - - + ]: 101407 : if (!PyExceptionClass_Check(right)) {
2789 : 0 : _PyErr_SetString(tstate, PyExc_TypeError,
2790 : : CANNOT_CATCH_MSG);
2791 : 0 : return -1;
2792 : : }
2793 : : }
2794 : 101696 : return 0;
2795 : : }
2796 : :
2797 : : static int
2798 : 0 : check_except_star_type_valid(PyThreadState *tstate, PyObject* right)
2799 : : {
2800 [ # # ]: 0 : if (check_except_type_valid(tstate, right) < 0) {
2801 : 0 : return -1;
2802 : : }
2803 : :
2804 : : /* reject except *ExceptionGroup */
2805 : :
2806 : 0 : int is_subclass = 0;
2807 [ # # ]: 0 : if (PyTuple_Check(right)) {
2808 : 0 : Py_ssize_t length = PyTuple_GET_SIZE(right);
2809 [ # # ]: 0 : for (Py_ssize_t i = 0; i < length; i++) {
2810 : 0 : PyObject *exc = PyTuple_GET_ITEM(right, i);
2811 : 0 : is_subclass = PyObject_IsSubclass(exc, PyExc_BaseExceptionGroup);
2812 [ # # ]: 0 : if (is_subclass < 0) {
2813 : 0 : return -1;
2814 : : }
2815 [ # # ]: 0 : if (is_subclass) {
2816 : 0 : break;
2817 : : }
2818 : : }
2819 : : }
2820 : : else {
2821 : 0 : is_subclass = PyObject_IsSubclass(right, PyExc_BaseExceptionGroup);
2822 [ # # ]: 0 : if (is_subclass < 0) {
2823 : 0 : return -1;
2824 : : }
2825 : : }
2826 [ # # ]: 0 : if (is_subclass) {
2827 : 0 : _PyErr_SetString(tstate, PyExc_TypeError,
2828 : : CANNOT_EXCEPT_STAR_EG);
2829 : 0 : return -1;
2830 : : }
2831 : 0 : return 0;
2832 : : }
2833 : :
2834 : : static int
2835 : 302 : check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
2836 : : {
2837 [ - + - - ]: 302 : if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
2838 : : /* check_args_iterable() may be called with a live exception:
2839 : : * clear it to prevent calling _PyObject_FunctionStr() with an
2840 : : * exception set. */
2841 : 0 : _PyErr_Clear(tstate);
2842 : 0 : PyObject *funcstr = _PyObject_FunctionStr(func);
2843 [ # # ]: 0 : if (funcstr != NULL) {
2844 : 0 : _PyErr_Format(tstate, PyExc_TypeError,
2845 : : "%U argument after * must be an iterable, not %.200s",
2846 : 0 : funcstr, Py_TYPE(args)->tp_name);
2847 [ # # ]: 0 : Py_DECREF(funcstr);
2848 : : }
2849 : 0 : return -1;
2850 : : }
2851 : 302 : return 0;
2852 : : }
2853 : :
2854 : : static void
2855 : 0 : format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
2856 : : {
2857 : : /* _PyDict_MergeEx raises attribute
2858 : : * error (percolated from an attempt
2859 : : * to get 'keys' attribute) instead of
2860 : : * a type error if its second argument
2861 : : * is not a mapping.
2862 : : */
2863 [ # # ]: 0 : if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2864 : 0 : _PyErr_Clear(tstate);
2865 : 0 : PyObject *funcstr = _PyObject_FunctionStr(func);
2866 [ # # ]: 0 : if (funcstr != NULL) {
2867 : 0 : _PyErr_Format(
2868 : : tstate, PyExc_TypeError,
2869 : : "%U argument after ** must be a mapping, not %.200s",
2870 : 0 : funcstr, Py_TYPE(kwargs)->tp_name);
2871 [ # # ]: 0 : Py_DECREF(funcstr);
2872 : : }
2873 : : }
2874 [ # # ]: 0 : else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2875 : 0 : PyObject *exc = _PyErr_GetRaisedException(tstate);
2876 : 0 : PyObject *args = ((PyBaseExceptionObject *)exc)->args;
2877 [ # # # # : 0 : if (exc && PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1) {
# # ]
2878 : 0 : _PyErr_Clear(tstate);
2879 : 0 : PyObject *funcstr = _PyObject_FunctionStr(func);
2880 [ # # ]: 0 : if (funcstr != NULL) {
2881 : 0 : PyObject *key = PyTuple_GET_ITEM(args, 0);
2882 : 0 : _PyErr_Format(
2883 : : tstate, PyExc_TypeError,
2884 : : "%U got multiple values for keyword argument '%S'",
2885 : : funcstr, key);
2886 [ # # ]: 0 : Py_DECREF(funcstr);
2887 : : }
2888 [ # # # # ]: 0 : Py_XDECREF(exc);
2889 : : }
2890 : : else {
2891 : 0 : _PyErr_SetRaisedException(tstate, exc);
2892 : : }
2893 : : }
2894 : 0 : }
2895 : :
2896 : : static void
2897 : 1 : format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
2898 : : const char *format_str, PyObject *obj)
2899 : : {
2900 : : const char *obj_str;
2901 : :
2902 [ - + ]: 1 : if (!obj)
2903 : 0 : return;
2904 : :
2905 : 1 : obj_str = PyUnicode_AsUTF8(obj);
2906 [ - + ]: 1 : if (!obj_str)
2907 : 0 : return;
2908 : :
2909 : 1 : _PyErr_Format(tstate, exc, format_str, obj_str);
2910 : :
2911 [ + - ]: 1 : if (exc == PyExc_NameError) {
2912 : : // Include the name in the NameError exceptions to offer suggestions later.
2913 : 1 : PyObject *exc = PyErr_GetRaisedException();
2914 [ + - ]: 1 : if (PyErr_GivenExceptionMatches(exc, PyExc_NameError)) {
2915 [ + - ]: 1 : if (((PyNameErrorObject*)exc)->name == NULL) {
2916 : : // We do not care if this fails because we are going to restore the
2917 : : // NameError anyway.
2918 : 1 : (void)PyObject_SetAttr(exc, &_Py_ID(name), obj);
2919 : : }
2920 : : }
2921 : 1 : PyErr_SetRaisedException(exc);
2922 : : }
2923 : : }
2924 : :
2925 : : static void
2926 : 0 : format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
2927 : : {
2928 : : PyObject *name;
2929 : : /* Don't stomp existing exception */
2930 [ # # ]: 0 : if (_PyErr_Occurred(tstate))
2931 : 0 : return;
2932 : 0 : name = PyTuple_GET_ITEM(co->co_localsplusnames, oparg);
2933 [ # # ]: 0 : if (oparg < PyCode_GetFirstFree(co)) {
2934 : 0 : format_exc_check_arg(tstate, PyExc_UnboundLocalError,
2935 : : UNBOUNDLOCAL_ERROR_MSG, name);
2936 : : } else {
2937 : 0 : format_exc_check_arg(tstate, PyExc_NameError,
2938 : : UNBOUNDFREE_ERROR_MSG, name);
2939 : : }
2940 : : }
2941 : :
2942 : : static void
2943 : 0 : format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int oparg)
2944 : : {
2945 [ # # # # ]: 0 : if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
2946 [ # # ]: 0 : if (oparg == 1) {
2947 : 0 : _PyErr_Format(tstate, PyExc_TypeError,
2948 : : "'async with' received an object from __aenter__ "
2949 : : "that does not implement __await__: %.100s",
2950 : : type->tp_name);
2951 : : }
2952 [ # # ]: 0 : else if (oparg == 2) {
2953 : 0 : _PyErr_Format(tstate, PyExc_TypeError,
2954 : : "'async with' received an object from __aexit__ "
2955 : : "that does not implement __await__: %.100s",
2956 : : type->tp_name);
2957 : : }
2958 : : }
2959 : 0 : }
2960 : :
2961 : :
2962 : : Py_ssize_t
2963 : 0 : PyUnstable_Eval_RequestCodeExtraIndex(freefunc free)
2964 : : {
2965 : 0 : PyInterpreterState *interp = _PyInterpreterState_GET();
2966 : : Py_ssize_t new_index;
2967 : :
2968 [ # # ]: 0 : if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
2969 : 0 : return -1;
2970 : : }
2971 : 0 : new_index = interp->co_extra_user_count++;
2972 : 0 : interp->co_extra_freefuncs[new_index] = free;
2973 : 0 : return new_index;
2974 : : }
2975 : :
2976 : : static void
2977 : 0 : dtrace_function_entry(_PyInterpreterFrame *frame)
2978 : : {
2979 : : const char *filename;
2980 : : const char *funcname;
2981 : : int lineno;
2982 : :
2983 : 0 : PyCodeObject *code = frame->f_code;
2984 : 0 : filename = PyUnicode_AsUTF8(code->co_filename);
2985 : 0 : funcname = PyUnicode_AsUTF8(code->co_name);
2986 : 0 : lineno = _PyInterpreterFrame_GetLine(frame);
2987 : :
2988 : 0 : PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
2989 : 0 : }
2990 : :
2991 : : static void
2992 : 0 : dtrace_function_return(_PyInterpreterFrame *frame)
2993 : : {
2994 : : const char *filename;
2995 : : const char *funcname;
2996 : : int lineno;
2997 : :
2998 : 0 : PyCodeObject *code = frame->f_code;
2999 : 0 : filename = PyUnicode_AsUTF8(code->co_filename);
3000 : 0 : funcname = PyUnicode_AsUTF8(code->co_name);
3001 : 0 : lineno = _PyInterpreterFrame_GetLine(frame);
3002 : :
3003 : 0 : PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
3004 : 0 : }
3005 : :
3006 : : /* DTrace equivalent of maybe_call_line_trace. */
3007 : : static void
3008 : 0 : maybe_dtrace_line(_PyInterpreterFrame *frame,
3009 : : PyTraceInfo *trace_info,
3010 : : int instr_prev)
3011 : : {
3012 : : const char *co_filename, *co_name;
3013 : :
3014 : : /* If the last instruction executed isn't in the current
3015 : : instruction window, reset the window.
3016 : : */
3017 : 0 : initialize_trace_info(trace_info, frame);
3018 : 0 : int lastline = _PyCode_CheckLineNumber(instr_prev*sizeof(_Py_CODEUNIT), &trace_info->bounds);
3019 : 0 : int addr = _PyInterpreterFrame_LASTI(frame) * sizeof(_Py_CODEUNIT);
3020 : 0 : int line = _PyCode_CheckLineNumber(addr, &trace_info->bounds);
3021 [ # # ]: 0 : if (line != -1) {
3022 : : /* Trace backward edges or first instruction of a new line */
3023 [ # # # # ]: 0 : if (_PyInterpreterFrame_LASTI(frame) < instr_prev ||
3024 [ # # ]: 0 : (line != lastline && addr == trace_info->bounds.ar_start))
3025 : : {
3026 : 0 : co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
3027 [ # # ]: 0 : if (!co_filename) {
3028 : 0 : co_filename = "?";
3029 : : }
3030 : 0 : co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
3031 [ # # ]: 0 : if (!co_name) {
3032 : 0 : co_name = "?";
3033 : : }
3034 : 0 : PyDTrace_LINE(co_filename, co_name, line);
3035 : : }
3036 : : }
3037 : 0 : }
3038 : :
3039 : : /* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
3040 : : for the limited API. */
3041 : :
3042 : 0 : int Py_EnterRecursiveCall(const char *where)
3043 : : {
3044 : 0 : return _Py_EnterRecursiveCall(where);
3045 : : }
3046 : :
3047 : 0 : void Py_LeaveRecursiveCall(void)
3048 : : {
3049 : 0 : _Py_LeaveRecursiveCall();
3050 : 0 : }
|