/src/cpython/Python/ceval.c
Line | Count | Source |
1 | | #include "python_coverage.h" |
2 | | |
3 | | #define _PY_INTERPRETER |
4 | | |
5 | | #include "Python.h" |
6 | | #include "pycore_abstract.h" // _PyIndex_Check() |
7 | | #include "pycore_audit.h" // _PySys_Audit() |
8 | | #include "pycore_backoff.h" |
9 | | #include "pycore_call.h" // _PyObject_CallNoArgs() |
10 | | #include "pycore_cell.h" // PyCell_GetRef() |
11 | | #include "pycore_ceval.h" // SPECIAL___ENTER__ |
12 | | #include "pycore_code.h" |
13 | | #include "pycore_dict.h" |
14 | | #include "pycore_emscripten_signal.h" // _Py_CHECK_EMSCRIPTEN_SIGNALS |
15 | | #include "pycore_floatobject.h" // _PyFloat_ExactDealloc() |
16 | | #include "pycore_frame.h" |
17 | | #include "pycore_function.h" |
18 | | #include "pycore_genobject.h" // _PyCoro_GetAwaitableIter() |
19 | | #include "pycore_import.h" // _PyImport_IsDefaultImportFunc() |
20 | | #include "pycore_instruments.h" |
21 | | #include "pycore_interpframe.h" // _PyFrame_SetStackPointer() |
22 | | #include "pycore_interpolation.h" // _PyInterpolation_Build() |
23 | | #include "pycore_intrinsics.h" |
24 | | #include "pycore_jit.h" |
25 | | #include "pycore_list.h" // _PyList_GetItemRef() |
26 | | #include "pycore_long.h" // _PyLong_GetZero() |
27 | | #include "pycore_moduleobject.h" // PyModuleObject |
28 | | #include "pycore_object.h" // _PyObject_GC_TRACK() |
29 | | #include "pycore_opcode_metadata.h" // EXTRA_CASES |
30 | | #include "pycore_opcode_utils.h" // MAKE_FUNCTION_* |
31 | | #include "pycore_optimizer.h" // _PyUOpExecutor_Type |
32 | | #include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_* |
33 | | #include "pycore_pyerrors.h" // _PyErr_GetRaisedException() |
34 | | #include "pycore_pystate.h" // _PyInterpreterState_GET() |
35 | | #include "pycore_range.h" // _PyRangeIterObject |
36 | | #include "pycore_setobject.h" // _PySet_Update() |
37 | | #include "pycore_sliceobject.h" // _PyBuildSlice_ConsumeRefs |
38 | | #include "pycore_sysmodule.h" // _PySys_GetOptionalAttrString() |
39 | | #include "pycore_template.h" // _PyTemplate_Build() |
40 | | #include "pycore_traceback.h" // _PyTraceBack_FromFrame |
41 | | #include "pycore_tuple.h" // _PyTuple_ITEMS() |
42 | | #include "pycore_uop_ids.h" // Uops |
43 | | |
44 | | #include "dictobject.h" |
45 | | #include "frameobject.h" // _PyInterpreterFrame_GetLine |
46 | | #include "opcode.h" |
47 | | #include "pydtrace.h" |
48 | | #include "setobject.h" |
49 | | #include "pycore_stackref.h" |
50 | | |
51 | | #include <stdbool.h> // bool |
52 | | |
53 | | #if !defined(Py_BUILD_CORE) |
54 | | # error "ceval.c must be build with Py_BUILD_CORE define for best performance" |
55 | | #endif |
56 | | |
57 | | #if !defined(Py_DEBUG) && !defined(Py_TRACE_REFS) |
58 | | // GH-89279: The MSVC compiler does not inline these static inline functions |
59 | | // in PGO build in _PyEval_EvalFrameDefault(), because this function is over |
60 | | // the limit of PGO, and that limit cannot be configured. |
61 | | // Define them as macros to make sure that they are always inlined by the |
62 | | // preprocessor. |
63 | | |
64 | | #undef Py_IS_TYPE |
65 | | #define Py_IS_TYPE(ob, type) \ |
66 | 7.39G | (_PyObject_CAST(ob)->ob_type == (type)) |
67 | | |
68 | | #undef Py_XDECREF |
69 | | #define Py_XDECREF(arg) \ |
70 | 152M | do { \ |
71 | 152M | PyObject *xop = _PyObject_CAST(arg); \ |
72 | 152M | if (xop != NULL) { \ |
73 | 70.5M | Py_DECREF(xop); \ |
74 | 70.5M | } \ |
75 | 152M | } while (0) |
76 | | |
77 | | #ifndef Py_GIL_DISABLED |
78 | | |
79 | | #undef Py_DECREF |
80 | | #define Py_DECREF(arg) \ |
81 | 171M | do { \ |
82 | 171M | PyObject *op = _PyObject_CAST(arg); \ |
83 | 171M | if (_Py_IsImmortal(op)) { \ |
84 | 45.5M | _Py_DECREF_IMMORTAL_STAT_INC(); \ |
85 | 45.5M | break; \ |
86 | 45.5M | } \ |
87 | 171M | _Py_DECREF_STAT_INC(); \ |
88 | 125M | if (--op->ob_refcnt == 0) { \ |
89 | 79.3M | _PyReftracerTrack(op, PyRefTracer_DESTROY); \ |
90 | 79.3M | destructor dealloc = Py_TYPE(op)->tp_dealloc; \ |
91 | 79.3M | (*dealloc)(op); \ |
92 | 79.3M | } \ |
93 | 125M | } while (0) |
94 | | |
95 | | #undef _Py_DECREF_SPECIALIZED |
96 | | #define _Py_DECREF_SPECIALIZED(arg, dealloc) \ |
97 | | do { \ |
98 | | PyObject *op = _PyObject_CAST(arg); \ |
99 | | if (_Py_IsImmortal(op)) { \ |
100 | | _Py_DECREF_IMMORTAL_STAT_INC(); \ |
101 | | break; \ |
102 | | } \ |
103 | | _Py_DECREF_STAT_INC(); \ |
104 | | if (--op->ob_refcnt == 0) { \ |
105 | | _PyReftracerTrack(op, PyRefTracer_DESTROY); \ |
106 | | destructor d = (destructor)(dealloc); \ |
107 | | d(op); \ |
108 | | } \ |
109 | | } while (0) |
110 | | |
111 | | #else // Py_GIL_DISABLED |
112 | | |
113 | | #undef Py_DECREF |
114 | | #define Py_DECREF(arg) \ |
115 | | do { \ |
116 | | PyObject *op = _PyObject_CAST(arg); \ |
117 | | uint32_t local = _Py_atomic_load_uint32_relaxed(&op->ob_ref_local); \ |
118 | | if (local == _Py_IMMORTAL_REFCNT_LOCAL) { \ |
119 | | _Py_DECREF_IMMORTAL_STAT_INC(); \ |
120 | | break; \ |
121 | | } \ |
122 | | _Py_DECREF_STAT_INC(); \ |
123 | | if (_Py_IsOwnedByCurrentThread(op)) { \ |
124 | | local--; \ |
125 | | _Py_atomic_store_uint32_relaxed(&op->ob_ref_local, local); \ |
126 | | if (local == 0) { \ |
127 | | _Py_MergeZeroLocalRefcount(op); \ |
128 | | } \ |
129 | | } \ |
130 | | else { \ |
131 | | _Py_DecRefShared(op); \ |
132 | | } \ |
133 | | } while (0) |
134 | | |
135 | | #undef _Py_DECREF_SPECIALIZED |
136 | | #define _Py_DECREF_SPECIALIZED(arg, dealloc) Py_DECREF(arg) |
137 | | |
138 | | #endif |
139 | | #endif |
140 | | |
141 | | |
142 | | static void |
143 | | check_invalid_reentrancy(void) |
144 | 228M | { |
145 | | #if defined(Py_DEBUG) && defined(Py_GIL_DISABLED) |
146 | | // In the free-threaded build, the interpreter must not be re-entered if |
147 | | // the world-is-stopped. If so, that's a bug somewhere (quite likely in |
148 | | // the painfully complex typeobject code). |
149 | | PyInterpreterState *interp = _PyInterpreterState_GET(); |
150 | | assert(!interp->stoptheworld.world_stopped); |
151 | | #endif |
152 | 228M | } |
153 | | |
154 | | |
155 | | #ifdef Py_DEBUG |
156 | | static void |
157 | | dump_item(_PyStackRef item) |
158 | | { |
159 | | if (PyStackRef_IsNull(item)) { |
160 | | printf("<NULL>"); |
161 | | return; |
162 | | } |
163 | | if (PyStackRef_IsMalformed(item)) { |
164 | | printf("<INVALID>"); |
165 | | return; |
166 | | } |
167 | | if (PyStackRef_IsTaggedInt(item)) { |
168 | | printf("%" PRId64, (int64_t)PyStackRef_UntagInt(item)); |
169 | | return; |
170 | | } |
171 | | PyObject *obj = PyStackRef_AsPyObjectBorrow(item); |
172 | | if (obj == NULL) { |
173 | | printf("<nil>"); |
174 | | return; |
175 | | } |
176 | | // Don't call __repr__(), it might recurse into the interpreter. |
177 | | printf("<%s at %p>", Py_TYPE(obj)->tp_name, (void *)obj); |
178 | | } |
179 | | |
180 | | static void |
181 | | dump_stack(_PyInterpreterFrame *frame, _PyStackRef *stack_pointer) |
182 | | { |
183 | | _PyFrame_SetStackPointer(frame, stack_pointer); |
184 | | _PyStackRef *locals_base = _PyFrame_GetLocalsArray(frame); |
185 | | _PyStackRef *stack_base = _PyFrame_Stackbase(frame); |
186 | | PyObject *exc = PyErr_GetRaisedException(); |
187 | | printf(" locals=["); |
188 | | for (_PyStackRef *ptr = locals_base; ptr < stack_base; ptr++) { |
189 | | if (ptr != locals_base) { |
190 | | printf(", "); |
191 | | } |
192 | | dump_item(*ptr); |
193 | | } |
194 | | printf("]\n"); |
195 | | if (stack_pointer < stack_base) { |
196 | | printf(" stack=%d\n", (int)(stack_pointer-stack_base)); |
197 | | } |
198 | | else { |
199 | | printf(" stack=["); |
200 | | for (_PyStackRef *ptr = stack_base; ptr < stack_pointer; ptr++) { |
201 | | if (ptr != stack_base) { |
202 | | printf(", "); |
203 | | } |
204 | | dump_item(*ptr); |
205 | | } |
206 | | printf("]\n"); |
207 | | } |
208 | | fflush(stdout); |
209 | | PyErr_SetRaisedException(exc); |
210 | | _PyFrame_GetStackPointer(frame); |
211 | | } |
212 | | |
213 | | static void |
214 | | lltrace_instruction(_PyInterpreterFrame *frame, |
215 | | _PyStackRef *stack_pointer, |
216 | | _Py_CODEUNIT *next_instr, |
217 | | int opcode, |
218 | | int oparg) |
219 | | { |
220 | | int offset = 0; |
221 | | if (frame->owner < FRAME_OWNED_BY_INTERPRETER) { |
222 | | dump_stack(frame, stack_pointer); |
223 | | offset = (int)(next_instr - _PyFrame_GetBytecode(frame)); |
224 | | } |
225 | | const char *opname = _PyOpcode_OpName[opcode]; |
226 | | assert(opname != NULL); |
227 | | if (OPCODE_HAS_ARG((int)_PyOpcode_Deopt[opcode])) { |
228 | | printf("%d: %s %d\n", offset * 2, opname, oparg); |
229 | | } |
230 | | else { |
231 | | printf("%d: %s\n", offset * 2, opname); |
232 | | } |
233 | | fflush(stdout); |
234 | | } |
235 | | |
236 | | static void |
237 | | lltrace_resume_frame(_PyInterpreterFrame *frame) |
238 | | { |
239 | | PyObject *fobj = PyStackRef_AsPyObjectBorrow(frame->f_funcobj); |
240 | | if (!PyStackRef_CodeCheck(frame->f_executable) || |
241 | | fobj == NULL || |
242 | | !PyFunction_Check(fobj) |
243 | | ) { |
244 | | printf("\nResuming frame.\n"); |
245 | | return; |
246 | | } |
247 | | PyFunctionObject *f = (PyFunctionObject *)fobj; |
248 | | PyObject *exc = PyErr_GetRaisedException(); |
249 | | PyObject *name = f->func_qualname; |
250 | | if (name == NULL) { |
251 | | name = f->func_name; |
252 | | } |
253 | | printf("\nResuming frame"); |
254 | | if (name) { |
255 | | printf(" for "); |
256 | | if (PyObject_Print(name, stdout, 0) < 0) { |
257 | | PyErr_Clear(); |
258 | | } |
259 | | } |
260 | | if (f->func_module) { |
261 | | printf(" in module "); |
262 | | if (PyObject_Print(f->func_module, stdout, 0) < 0) { |
263 | | PyErr_Clear(); |
264 | | } |
265 | | } |
266 | | printf("\n"); |
267 | | fflush(stdout); |
268 | | PyErr_SetRaisedException(exc); |
269 | | } |
270 | | |
271 | | static int |
272 | | maybe_lltrace_resume_frame(_PyInterpreterFrame *frame, PyObject *globals) |
273 | | { |
274 | | if (globals == NULL) { |
275 | | return 0; |
276 | | } |
277 | | if (frame->owner >= FRAME_OWNED_BY_INTERPRETER) { |
278 | | return 0; |
279 | | } |
280 | | int r = PyDict_Contains(globals, &_Py_ID(__lltrace__)); |
281 | | if (r < 0) { |
282 | | PyErr_Clear(); |
283 | | return 0; |
284 | | } |
285 | | int lltrace = r * 5; // Levels 1-4 only trace uops |
286 | | if (!lltrace) { |
287 | | // Can also be controlled by environment variable |
288 | | char *python_lltrace = Py_GETENV("PYTHON_LLTRACE"); |
289 | | if (python_lltrace != NULL && *python_lltrace >= '0') { |
290 | | lltrace = *python_lltrace - '0'; // TODO: Parse an int and all that |
291 | | } |
292 | | } |
293 | | if (lltrace >= 5) { |
294 | | lltrace_resume_frame(frame); |
295 | | } |
296 | | return lltrace; |
297 | | } |
298 | | |
299 | | #endif |
300 | | |
301 | | static void monitor_reraise(PyThreadState *tstate, |
302 | | _PyInterpreterFrame *frame, |
303 | | _Py_CODEUNIT *instr); |
304 | | static int monitor_stop_iteration(PyThreadState *tstate, |
305 | | _PyInterpreterFrame *frame, |
306 | | _Py_CODEUNIT *instr, |
307 | | PyObject *value); |
308 | | static void monitor_unwind(PyThreadState *tstate, |
309 | | _PyInterpreterFrame *frame, |
310 | | _Py_CODEUNIT *instr); |
311 | | static int monitor_handled(PyThreadState *tstate, |
312 | | _PyInterpreterFrame *frame, |
313 | | _Py_CODEUNIT *instr, PyObject *exc); |
314 | | static void monitor_throw(PyThreadState *tstate, |
315 | | _PyInterpreterFrame *frame, |
316 | | _Py_CODEUNIT *instr); |
317 | | |
318 | | static int get_exception_handler(PyCodeObject *, int, int*, int*, int*); |
319 | | static _PyInterpreterFrame * |
320 | | _PyEvalFramePushAndInit_Ex(PyThreadState *tstate, _PyStackRef func, |
321 | | PyObject *locals, Py_ssize_t nargs, PyObject *callargs, PyObject *kwargs, _PyInterpreterFrame *previous); |
322 | | |
323 | | #ifdef HAVE_ERRNO_H |
324 | | #include <errno.h> |
325 | | #endif |
326 | | |
327 | | int |
328 | | Py_GetRecursionLimit(void) |
329 | 40 | { |
330 | 40 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
331 | 40 | return interp->ceval.recursion_limit; |
332 | 40 | } |
333 | | |
334 | | void |
335 | | Py_SetRecursionLimit(int new_limit) |
336 | 0 | { |
337 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
338 | 0 | _PyEval_StopTheWorld(interp); |
339 | 0 | interp->ceval.recursion_limit = new_limit; |
340 | 0 | _Py_FOR_EACH_TSTATE_BEGIN(interp, p) { |
341 | 0 | int depth = p->py_recursion_limit - p->py_recursion_remaining; |
342 | 0 | p->py_recursion_limit = new_limit; |
343 | 0 | p->py_recursion_remaining = new_limit - depth; |
344 | 0 | } |
345 | 0 | _Py_FOR_EACH_TSTATE_END(interp); |
346 | 0 | _PyEval_StartTheWorld(interp); |
347 | 0 | } |
348 | | |
349 | | int |
350 | | _Py_ReachedRecursionLimitWithMargin(PyThreadState *tstate, int margin_count) |
351 | 79.4M | { |
352 | 79.4M | uintptr_t here_addr = _Py_get_machine_stack_pointer(); |
353 | 79.4M | _PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate; |
354 | 79.4M | if (here_addr > _tstate->c_stack_soft_limit + margin_count * _PyOS_STACK_MARGIN_BYTES) { |
355 | 79.4M | return 0; |
356 | 79.4M | } |
357 | 0 | if (_tstate->c_stack_hard_limit == 0) { |
358 | 0 | _Py_InitializeRecursionLimits(tstate); |
359 | 0 | } |
360 | 0 | return here_addr <= _tstate->c_stack_soft_limit + margin_count * _PyOS_STACK_MARGIN_BYTES; |
361 | 79.4M | } |
362 | | |
363 | | void |
364 | | _Py_EnterRecursiveCallUnchecked(PyThreadState *tstate) |
365 | 0 | { |
366 | 0 | uintptr_t here_addr = _Py_get_machine_stack_pointer(); |
367 | 0 | _PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate; |
368 | 0 | if (here_addr < _tstate->c_stack_hard_limit) { |
369 | 0 | Py_FatalError("Unchecked stack overflow."); |
370 | 0 | } |
371 | 0 | } |
372 | | |
373 | | #if defined(__s390x__) |
374 | | # define Py_C_STACK_SIZE 320000 |
375 | | #elif defined(_WIN32) |
376 | | // Don't define Py_C_STACK_SIZE, ask the O/S |
377 | | #elif defined(__ANDROID__) |
378 | | # define Py_C_STACK_SIZE 1200000 |
379 | | #elif defined(__sparc__) |
380 | | # define Py_C_STACK_SIZE 1600000 |
381 | | #elif defined(__hppa__) || defined(__powerpc64__) |
382 | | # define Py_C_STACK_SIZE 2000000 |
383 | | #else |
384 | 0 | # define Py_C_STACK_SIZE 4000000 |
385 | | #endif |
386 | | |
387 | | #if defined(__EMSCRIPTEN__) |
388 | | |
389 | | // Temporary workaround to make `pthread_getattr_np` work on Emscripten. |
390 | | // Emscripten 4.0.6 will contain a fix: |
391 | | // https://github.com/emscripten-core/emscripten/pull/23887 |
392 | | |
393 | | #include "emscripten/stack.h" |
394 | | |
395 | | #define pthread_attr_t workaround_pthread_attr_t |
396 | | #define pthread_getattr_np workaround_pthread_getattr_np |
397 | | #define pthread_attr_getguardsize workaround_pthread_attr_getguardsize |
398 | | #define pthread_attr_getstack workaround_pthread_attr_getstack |
399 | | #define pthread_attr_destroy workaround_pthread_attr_destroy |
400 | | |
401 | | typedef struct { |
402 | | void *_a_stackaddr; |
403 | | size_t _a_stacksize, _a_guardsize; |
404 | | } pthread_attr_t; |
405 | | |
406 | | extern __attribute__((__visibility__("hidden"))) unsigned __default_guardsize; |
407 | | |
408 | | // Modified version of pthread_getattr_np from the upstream PR. |
409 | | |
410 | | int pthread_getattr_np(pthread_t thread, pthread_attr_t *attr) { |
411 | | attr->_a_stackaddr = (void*)emscripten_stack_get_base(); |
412 | | attr->_a_stacksize = emscripten_stack_get_base() - emscripten_stack_get_end(); |
413 | | attr->_a_guardsize = __default_guardsize; |
414 | | return 0; |
415 | | } |
416 | | |
417 | | // These three functions copied without any changes from Emscripten libc. |
418 | | |
419 | | int pthread_attr_getguardsize(const pthread_attr_t *restrict a, size_t *restrict size) |
420 | | { |
421 | | *size = a->_a_guardsize; |
422 | | return 0; |
423 | | } |
424 | | |
425 | | int pthread_attr_getstack(const pthread_attr_t *restrict a, void **restrict addr, size_t *restrict size) |
426 | | { |
427 | | /// XXX musl is not standard-conforming? It should not report EINVAL if _a_stackaddr is zero, and it should |
428 | | /// report EINVAL if a is null: http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_attr_getstack.html |
429 | | if (!a) return EINVAL; |
430 | | // if (!a->_a_stackaddr) |
431 | | // return EINVAL; |
432 | | |
433 | | *size = a->_a_stacksize; |
434 | | *addr = (void *)(a->_a_stackaddr - *size); |
435 | | return 0; |
436 | | } |
437 | | |
438 | | int pthread_attr_destroy(pthread_attr_t *a) |
439 | | { |
440 | | return 0; |
441 | | } |
442 | | |
443 | | #endif |
444 | | |
445 | | static void |
446 | | hardware_stack_limits(uintptr_t *top, uintptr_t *base) |
447 | 16 | { |
448 | | #ifdef WIN32 |
449 | | ULONG_PTR low, high; |
450 | | GetCurrentThreadStackLimits(&low, &high); |
451 | | *top = (uintptr_t)high; |
452 | | ULONG guarantee = 0; |
453 | | SetThreadStackGuarantee(&guarantee); |
454 | | *base = (uintptr_t)low + guarantee; |
455 | | #elif defined(__APPLE__) |
456 | | pthread_t this_thread = pthread_self(); |
457 | | void *stack_addr = pthread_get_stackaddr_np(this_thread); // top of the stack |
458 | | size_t stack_size = pthread_get_stacksize_np(this_thread); |
459 | | *top = (uintptr_t)stack_addr; |
460 | | *base = ((uintptr_t)stack_addr) - stack_size; |
461 | | #else |
462 | | /// XXX musl supports HAVE_PTHRED_GETATTR_NP, but the resulting stack size |
463 | | /// (on alpine at least) is much smaller than expected and imposes undue limits |
464 | | /// compared to the old stack size estimation. (We assume musl is not glibc.) |
465 | 16 | # if defined(HAVE_PTHREAD_GETATTR_NP) && !defined(_AIX) && \ |
466 | 16 | !defined(__NetBSD__) && (defined(__GLIBC__) || !defined(__linux__)) |
467 | 16 | size_t stack_size, guard_size; |
468 | 16 | void *stack_addr; |
469 | 16 | pthread_attr_t attr; |
470 | 16 | int err = pthread_getattr_np(pthread_self(), &attr); |
471 | 16 | if (err == 0) { |
472 | 16 | err = pthread_attr_getguardsize(&attr, &guard_size); |
473 | 16 | err |= pthread_attr_getstack(&attr, &stack_addr, &stack_size); |
474 | 16 | err |= pthread_attr_destroy(&attr); |
475 | 16 | } |
476 | 16 | if (err == 0) { |
477 | 16 | *base = ((uintptr_t)stack_addr) + guard_size; |
478 | 16 | *top = (uintptr_t)stack_addr + stack_size; |
479 | 16 | return; |
480 | 16 | } |
481 | 0 | # endif |
482 | 0 | uintptr_t here_addr = _Py_get_machine_stack_pointer(); |
483 | 0 | uintptr_t top_addr = _Py_SIZE_ROUND_UP(here_addr, 4096); |
484 | 0 | *top = top_addr; |
485 | 0 | *base = top_addr - Py_C_STACK_SIZE; |
486 | 0 | #endif |
487 | 0 | } |
488 | | |
489 | | void |
490 | | _Py_InitializeRecursionLimits(PyThreadState *tstate) |
491 | 16 | { |
492 | 16 | uintptr_t top; |
493 | 16 | uintptr_t base; |
494 | 16 | hardware_stack_limits(&top, &base); |
495 | | #ifdef _Py_THREAD_SANITIZER |
496 | | // Thread sanitizer crashes if we use more than half the stack. |
497 | | uintptr_t stacksize = top - base; |
498 | | base += stacksize/2; |
499 | | #endif |
500 | 16 | _PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate; |
501 | 16 | _tstate->c_stack_top = top; |
502 | 16 | _tstate->c_stack_hard_limit = base + _PyOS_STACK_MARGIN_BYTES; |
503 | 16 | _tstate->c_stack_soft_limit = base + _PyOS_STACK_MARGIN_BYTES * 2; |
504 | 16 | } |
505 | | |
506 | | /* The function _Py_EnterRecursiveCallTstate() only calls _Py_CheckRecursiveCall() |
507 | | if the recursion_depth reaches recursion_limit. */ |
508 | | int |
509 | | _Py_CheckRecursiveCall(PyThreadState *tstate, const char *where) |
510 | 45 | { |
511 | 45 | _PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate; |
512 | 45 | uintptr_t here_addr = _Py_get_machine_stack_pointer(); |
513 | 45 | assert(_tstate->c_stack_soft_limit != 0); |
514 | 45 | assert(_tstate->c_stack_hard_limit != 0); |
515 | 45 | if (here_addr < _tstate->c_stack_hard_limit) { |
516 | | /* Overflowing while handling an overflow. Give up. */ |
517 | 0 | int kbytes_used = (int)(_tstate->c_stack_top - here_addr)/1024; |
518 | 0 | char buffer[80]; |
519 | 0 | snprintf(buffer, 80, "Unrecoverable stack overflow (used %d kB)%s", kbytes_used, where); |
520 | 0 | Py_FatalError(buffer); |
521 | 0 | } |
522 | 45 | if (tstate->recursion_headroom) { |
523 | 0 | return 0; |
524 | 0 | } |
525 | 45 | else { |
526 | 45 | int kbytes_used = (int)(_tstate->c_stack_top - here_addr)/1024; |
527 | 45 | tstate->recursion_headroom++; |
528 | 45 | _PyErr_Format(tstate, PyExc_RecursionError, |
529 | 45 | "Stack overflow (used %d kB)%s", |
530 | 45 | kbytes_used, |
531 | 45 | where); |
532 | 45 | tstate->recursion_headroom--; |
533 | 45 | return -1; |
534 | 45 | } |
535 | 45 | } |
536 | | |
537 | | |
538 | | const binaryfunc _PyEval_BinaryOps[] = { |
539 | | [NB_ADD] = PyNumber_Add, |
540 | | [NB_AND] = PyNumber_And, |
541 | | [NB_FLOOR_DIVIDE] = PyNumber_FloorDivide, |
542 | | [NB_LSHIFT] = PyNumber_Lshift, |
543 | | [NB_MATRIX_MULTIPLY] = PyNumber_MatrixMultiply, |
544 | | [NB_MULTIPLY] = PyNumber_Multiply, |
545 | | [NB_REMAINDER] = PyNumber_Remainder, |
546 | | [NB_OR] = PyNumber_Or, |
547 | | [NB_POWER] = _PyNumber_PowerNoMod, |
548 | | [NB_RSHIFT] = PyNumber_Rshift, |
549 | | [NB_SUBTRACT] = PyNumber_Subtract, |
550 | | [NB_TRUE_DIVIDE] = PyNumber_TrueDivide, |
551 | | [NB_XOR] = PyNumber_Xor, |
552 | | [NB_INPLACE_ADD] = PyNumber_InPlaceAdd, |
553 | | [NB_INPLACE_AND] = PyNumber_InPlaceAnd, |
554 | | [NB_INPLACE_FLOOR_DIVIDE] = PyNumber_InPlaceFloorDivide, |
555 | | [NB_INPLACE_LSHIFT] = PyNumber_InPlaceLshift, |
556 | | [NB_INPLACE_MATRIX_MULTIPLY] = PyNumber_InPlaceMatrixMultiply, |
557 | | [NB_INPLACE_MULTIPLY] = PyNumber_InPlaceMultiply, |
558 | | [NB_INPLACE_REMAINDER] = PyNumber_InPlaceRemainder, |
559 | | [NB_INPLACE_OR] = PyNumber_InPlaceOr, |
560 | | [NB_INPLACE_POWER] = _PyNumber_InPlacePowerNoMod, |
561 | | [NB_INPLACE_RSHIFT] = PyNumber_InPlaceRshift, |
562 | | [NB_INPLACE_SUBTRACT] = PyNumber_InPlaceSubtract, |
563 | | [NB_INPLACE_TRUE_DIVIDE] = PyNumber_InPlaceTrueDivide, |
564 | | [NB_INPLACE_XOR] = PyNumber_InPlaceXor, |
565 | | [NB_SUBSCR] = PyObject_GetItem, |
566 | | }; |
567 | | |
568 | | const conversion_func _PyEval_ConversionFuncs[4] = { |
569 | | [FVC_STR] = PyObject_Str, |
570 | | [FVC_REPR] = PyObject_Repr, |
571 | | [FVC_ASCII] = PyObject_ASCII |
572 | | }; |
573 | | |
574 | | const _Py_SpecialMethod _Py_SpecialMethods[] = { |
575 | | [SPECIAL___ENTER__] = { |
576 | | .name = &_Py_ID(__enter__), |
577 | | .error = ( |
578 | | "'%T' object does not support the context manager protocol " |
579 | | "(missed __enter__ method)" |
580 | | ), |
581 | | .error_suggestion = ( |
582 | | "'%T' object does not support the context manager protocol " |
583 | | "(missed __enter__ method) but it supports the asynchronous " |
584 | | "context manager protocol. Did you mean to use 'async with'?" |
585 | | ) |
586 | | }, |
587 | | [SPECIAL___EXIT__] = { |
588 | | .name = &_Py_ID(__exit__), |
589 | | .error = ( |
590 | | "'%T' object does not support the context manager protocol " |
591 | | "(missed __exit__ method)" |
592 | | ), |
593 | | .error_suggestion = ( |
594 | | "'%T' object does not support the context manager protocol " |
595 | | "(missed __exit__ method) but it supports the asynchronous " |
596 | | "context manager protocol. Did you mean to use 'async with'?" |
597 | | ) |
598 | | }, |
599 | | [SPECIAL___AENTER__] = { |
600 | | .name = &_Py_ID(__aenter__), |
601 | | .error = ( |
602 | | "'%T' object does not support the asynchronous " |
603 | | "context manager protocol (missed __aenter__ method)" |
604 | | ), |
605 | | .error_suggestion = ( |
606 | | "'%T' object does not support the asynchronous context manager " |
607 | | "protocol (missed __aenter__ method) but it supports the context " |
608 | | "manager protocol. Did you mean to use 'with'?" |
609 | | ) |
610 | | }, |
611 | | [SPECIAL___AEXIT__] = { |
612 | | .name = &_Py_ID(__aexit__), |
613 | | .error = ( |
614 | | "'%T' object does not support the asynchronous " |
615 | | "context manager protocol (missed __aexit__ method)" |
616 | | ), |
617 | | .error_suggestion = ( |
618 | | "'%T' object does not support the asynchronous context manager " |
619 | | "protocol (missed __aexit__ method) but it supports the context " |
620 | | "manager protocol. Did you mean to use 'with'?" |
621 | | ) |
622 | | } |
623 | | }; |
624 | | |
625 | | const size_t _Py_FunctionAttributeOffsets[] = { |
626 | | [MAKE_FUNCTION_CLOSURE] = offsetof(PyFunctionObject, func_closure), |
627 | | [MAKE_FUNCTION_ANNOTATIONS] = offsetof(PyFunctionObject, func_annotations), |
628 | | [MAKE_FUNCTION_KWDEFAULTS] = offsetof(PyFunctionObject, func_kwdefaults), |
629 | | [MAKE_FUNCTION_DEFAULTS] = offsetof(PyFunctionObject, func_defaults), |
630 | | [MAKE_FUNCTION_ANNOTATE] = offsetof(PyFunctionObject, func_annotate), |
631 | | }; |
632 | | |
633 | | // PEP 634: Structural Pattern Matching |
634 | | |
635 | | |
636 | | // Return a tuple of values corresponding to keys, with error checks for |
637 | | // duplicate/missing keys. |
638 | | PyObject * |
639 | | _PyEval_MatchKeys(PyThreadState *tstate, PyObject *map, PyObject *keys) |
640 | 0 | { |
641 | 0 | assert(PyTuple_CheckExact(keys)); |
642 | 0 | Py_ssize_t nkeys = PyTuple_GET_SIZE(keys); |
643 | 0 | if (!nkeys) { |
644 | | // No keys means no items. |
645 | 0 | return PyTuple_New(0); |
646 | 0 | } |
647 | 0 | PyObject *seen = NULL; |
648 | 0 | PyObject *dummy = NULL; |
649 | 0 | PyObject *values = NULL; |
650 | | // We use the two argument form of map.get(key, default) for two reasons: |
651 | | // - Atomically check for a key and get its value without error handling. |
652 | | // - Don't cause key creation or resizing in dict subclasses like |
653 | | // collections.defaultdict that define __missing__ (or similar). |
654 | 0 | _PyCStackRef cref; |
655 | 0 | _PyThreadState_PushCStackRef(tstate, &cref); |
656 | 0 | int meth_found = _PyObject_GetMethodStackRef(tstate, map, &_Py_ID(get), &cref.ref); |
657 | 0 | PyObject *get = PyStackRef_AsPyObjectBorrow(cref.ref); |
658 | 0 | if (get == NULL) { |
659 | 0 | goto fail; |
660 | 0 | } |
661 | 0 | seen = PySet_New(NULL); |
662 | 0 | if (seen == NULL) { |
663 | 0 | goto fail; |
664 | 0 | } |
665 | | // dummy = object() |
666 | 0 | dummy = _PyObject_CallNoArgs((PyObject *)&PyBaseObject_Type); |
667 | 0 | if (dummy == NULL) { |
668 | 0 | goto fail; |
669 | 0 | } |
670 | 0 | values = PyTuple_New(nkeys); |
671 | 0 | if (values == NULL) { |
672 | 0 | goto fail; |
673 | 0 | } |
674 | 0 | for (Py_ssize_t i = 0; i < nkeys; i++) { |
675 | 0 | PyObject *key = PyTuple_GET_ITEM(keys, i); |
676 | 0 | if (PySet_Contains(seen, key) || PySet_Add(seen, key)) { |
677 | 0 | if (!_PyErr_Occurred(tstate)) { |
678 | | // Seen it before! |
679 | 0 | _PyErr_Format(tstate, PyExc_ValueError, |
680 | 0 | "mapping pattern checks duplicate key (%R)", key); |
681 | 0 | } |
682 | 0 | goto fail; |
683 | 0 | } |
684 | 0 | PyObject *args[] = { map, key, dummy }; |
685 | 0 | PyObject *value = NULL; |
686 | 0 | if (meth_found) { |
687 | 0 | value = PyObject_Vectorcall(get, args, 3, NULL); |
688 | 0 | } |
689 | 0 | else { |
690 | 0 | value = PyObject_Vectorcall(get, &args[1], 2, NULL); |
691 | 0 | } |
692 | 0 | if (value == NULL) { |
693 | 0 | goto fail; |
694 | 0 | } |
695 | 0 | if (value == dummy) { |
696 | | // key not in map! |
697 | 0 | Py_DECREF(value); |
698 | 0 | Py_DECREF(values); |
699 | | // Return None: |
700 | 0 | values = Py_NewRef(Py_None); |
701 | 0 | goto done; |
702 | 0 | } |
703 | 0 | PyTuple_SET_ITEM(values, i, value); |
704 | 0 | } |
705 | | // Success: |
706 | 0 | done: |
707 | 0 | _PyThreadState_PopCStackRef(tstate, &cref); |
708 | 0 | Py_DECREF(seen); |
709 | 0 | Py_DECREF(dummy); |
710 | 0 | return values; |
711 | 0 | fail: |
712 | 0 | _PyThreadState_PopCStackRef(tstate, &cref); |
713 | 0 | Py_XDECREF(seen); |
714 | 0 | Py_XDECREF(dummy); |
715 | 0 | Py_XDECREF(values); |
716 | 0 | return NULL; |
717 | 0 | } |
718 | | |
719 | | // Extract a named attribute from the subject, with additional bookkeeping to |
720 | | // raise TypeErrors for repeated lookups. On failure, return NULL (with no |
721 | | // error set). Use _PyErr_Occurred(tstate) to disambiguate. |
722 | | static PyObject * |
723 | | match_class_attr(PyThreadState *tstate, PyObject *subject, PyObject *type, |
724 | | PyObject *name, PyObject *seen) |
725 | 0 | { |
726 | 0 | assert(PyUnicode_CheckExact(name)); |
727 | 0 | assert(PySet_CheckExact(seen)); |
728 | 0 | if (PySet_Contains(seen, name) || PySet_Add(seen, name)) { |
729 | 0 | if (!_PyErr_Occurred(tstate)) { |
730 | | // Seen it before! |
731 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
732 | 0 | "%s() got multiple sub-patterns for attribute %R", |
733 | 0 | ((PyTypeObject*)type)->tp_name, name); |
734 | 0 | } |
735 | 0 | return NULL; |
736 | 0 | } |
737 | 0 | PyObject *attr; |
738 | 0 | (void)PyObject_GetOptionalAttr(subject, name, &attr); |
739 | 0 | return attr; |
740 | 0 | } |
741 | | |
742 | | // On success (match), return a tuple of extracted attributes. On failure (no |
743 | | // match), return NULL. Use _PyErr_Occurred(tstate) to disambiguate. |
744 | | PyObject* |
745 | | _PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type, |
746 | | Py_ssize_t nargs, PyObject *kwargs) |
747 | 0 | { |
748 | 0 | if (!PyType_Check(type)) { |
749 | 0 | const char *e = "called match pattern must be a class"; |
750 | 0 | _PyErr_Format(tstate, PyExc_TypeError, e); |
751 | 0 | return NULL; |
752 | 0 | } |
753 | 0 | assert(PyTuple_CheckExact(kwargs)); |
754 | | // First, an isinstance check: |
755 | 0 | if (PyObject_IsInstance(subject, type) <= 0) { |
756 | 0 | return NULL; |
757 | 0 | } |
758 | | // So far so good: |
759 | 0 | PyObject *seen = PySet_New(NULL); |
760 | 0 | if (seen == NULL) { |
761 | 0 | return NULL; |
762 | 0 | } |
763 | 0 | PyObject *attrs = PyList_New(0); |
764 | 0 | if (attrs == NULL) { |
765 | 0 | Py_DECREF(seen); |
766 | 0 | return NULL; |
767 | 0 | } |
768 | | // NOTE: From this point on, goto fail on failure: |
769 | 0 | PyObject *match_args = NULL; |
770 | | // First, the positional subpatterns: |
771 | 0 | if (nargs) { |
772 | 0 | int match_self = 0; |
773 | 0 | if (PyObject_GetOptionalAttr(type, &_Py_ID(__match_args__), &match_args) < 0) { |
774 | 0 | goto fail; |
775 | 0 | } |
776 | 0 | if (match_args) { |
777 | 0 | if (!PyTuple_CheckExact(match_args)) { |
778 | 0 | const char *e = "%s.__match_args__ must be a tuple (got %s)"; |
779 | 0 | _PyErr_Format(tstate, PyExc_TypeError, e, |
780 | 0 | ((PyTypeObject *)type)->tp_name, |
781 | 0 | Py_TYPE(match_args)->tp_name); |
782 | 0 | goto fail; |
783 | 0 | } |
784 | 0 | } |
785 | 0 | else { |
786 | | // _Py_TPFLAGS_MATCH_SELF is only acknowledged if the type does not |
787 | | // define __match_args__. This is natural behavior for subclasses: |
788 | | // it's as if __match_args__ is some "magic" value that is lost as |
789 | | // soon as they redefine it. |
790 | 0 | match_args = PyTuple_New(0); |
791 | 0 | match_self = PyType_HasFeature((PyTypeObject*)type, |
792 | 0 | _Py_TPFLAGS_MATCH_SELF); |
793 | 0 | } |
794 | 0 | assert(PyTuple_CheckExact(match_args)); |
795 | 0 | Py_ssize_t allowed = match_self ? 1 : PyTuple_GET_SIZE(match_args); |
796 | 0 | if (allowed < nargs) { |
797 | 0 | const char *plural = (allowed == 1) ? "" : "s"; |
798 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
799 | 0 | "%s() accepts %d positional sub-pattern%s (%d given)", |
800 | 0 | ((PyTypeObject*)type)->tp_name, |
801 | 0 | allowed, plural, nargs); |
802 | 0 | goto fail; |
803 | 0 | } |
804 | 0 | if (match_self) { |
805 | | // Easy. Copy the subject itself, and move on to kwargs. |
806 | 0 | if (PyList_Append(attrs, subject) < 0) { |
807 | 0 | goto fail; |
808 | 0 | } |
809 | 0 | } |
810 | 0 | else { |
811 | 0 | for (Py_ssize_t i = 0; i < nargs; i++) { |
812 | 0 | PyObject *name = PyTuple_GET_ITEM(match_args, i); |
813 | 0 | if (!PyUnicode_CheckExact(name)) { |
814 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
815 | 0 | "__match_args__ elements must be strings " |
816 | 0 | "(got %s)", Py_TYPE(name)->tp_name); |
817 | 0 | goto fail; |
818 | 0 | } |
819 | 0 | PyObject *attr = match_class_attr(tstate, subject, type, name, |
820 | 0 | seen); |
821 | 0 | if (attr == NULL) { |
822 | 0 | goto fail; |
823 | 0 | } |
824 | 0 | if (PyList_Append(attrs, attr) < 0) { |
825 | 0 | Py_DECREF(attr); |
826 | 0 | goto fail; |
827 | 0 | } |
828 | 0 | Py_DECREF(attr); |
829 | 0 | } |
830 | 0 | } |
831 | 0 | Py_CLEAR(match_args); |
832 | 0 | } |
833 | | // Finally, the keyword subpatterns: |
834 | 0 | for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwargs); i++) { |
835 | 0 | PyObject *name = PyTuple_GET_ITEM(kwargs, i); |
836 | 0 | PyObject *attr = match_class_attr(tstate, subject, type, name, seen); |
837 | 0 | if (attr == NULL) { |
838 | 0 | goto fail; |
839 | 0 | } |
840 | 0 | if (PyList_Append(attrs, attr) < 0) { |
841 | 0 | Py_DECREF(attr); |
842 | 0 | goto fail; |
843 | 0 | } |
844 | 0 | Py_DECREF(attr); |
845 | 0 | } |
846 | 0 | Py_SETREF(attrs, PyList_AsTuple(attrs)); |
847 | 0 | Py_DECREF(seen); |
848 | 0 | return attrs; |
849 | 0 | fail: |
850 | | // We really don't care whether an error was raised or not... that's our |
851 | | // caller's problem. All we know is that the match failed. |
852 | 0 | Py_XDECREF(match_args); |
853 | 0 | Py_DECREF(seen); |
854 | 0 | Py_DECREF(attrs); |
855 | 0 | return NULL; |
856 | 0 | } |
857 | | |
858 | | |
859 | | static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause); |
860 | | |
861 | | PyObject * |
862 | | PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals) |
863 | 1.13k | { |
864 | 1.13k | PyThreadState *tstate = _PyThreadState_GET(); |
865 | 1.13k | if (locals == NULL) { |
866 | 0 | locals = globals; |
867 | 0 | } |
868 | 1.13k | PyObject *builtins = _PyDict_LoadBuiltinsFromGlobals(globals); |
869 | 1.13k | if (builtins == NULL) { |
870 | 0 | return NULL; |
871 | 0 | } |
872 | 1.13k | PyFrameConstructor desc = { |
873 | 1.13k | .fc_globals = globals, |
874 | 1.13k | .fc_builtins = builtins, |
875 | 1.13k | .fc_name = ((PyCodeObject *)co)->co_name, |
876 | 1.13k | .fc_qualname = ((PyCodeObject *)co)->co_name, |
877 | 1.13k | .fc_code = co, |
878 | 1.13k | .fc_defaults = NULL, |
879 | 1.13k | .fc_kwdefaults = NULL, |
880 | 1.13k | .fc_closure = NULL |
881 | 1.13k | }; |
882 | 1.13k | PyFunctionObject *func = _PyFunction_FromConstructor(&desc); |
883 | 1.13k | _Py_DECREF_BUILTINS(builtins); |
884 | 1.13k | if (func == NULL) { |
885 | 0 | return NULL; |
886 | 0 | } |
887 | 1.13k | EVAL_CALL_STAT_INC(EVAL_CALL_LEGACY); |
888 | 1.13k | PyObject *res = _PyEval_Vector(tstate, func, locals, NULL, 0, NULL); |
889 | 1.13k | Py_DECREF(func); |
890 | 1.13k | return res; |
891 | 1.13k | } |
892 | | |
893 | | |
894 | | /* Interpreter main loop */ |
895 | | |
896 | | PyObject * |
897 | | PyEval_EvalFrame(PyFrameObject *f) |
898 | 0 | { |
899 | | /* Function kept for backward compatibility */ |
900 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
901 | 0 | return _PyEval_EvalFrame(tstate, f->f_frame, 0); |
902 | 0 | } |
903 | | |
904 | | PyObject * |
905 | | PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) |
906 | 0 | { |
907 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
908 | 0 | return _PyEval_EvalFrame(tstate, f->f_frame, throwflag); |
909 | 0 | } |
910 | | |
911 | | #include "ceval_macros.h" |
912 | | |
913 | | int _Py_CheckRecursiveCallPy( |
914 | | PyThreadState *tstate) |
915 | 162 | { |
916 | 162 | if (tstate->recursion_headroom) { |
917 | 0 | if (tstate->py_recursion_remaining < -50) { |
918 | | /* Overflowing while handling an overflow. Give up. */ |
919 | 0 | Py_FatalError("Cannot recover from Python stack overflow."); |
920 | 0 | } |
921 | 0 | } |
922 | 162 | else { |
923 | 162 | if (tstate->py_recursion_remaining <= 0) { |
924 | 162 | tstate->recursion_headroom++; |
925 | 162 | _PyErr_Format(tstate, PyExc_RecursionError, |
926 | 162 | "maximum recursion depth exceeded"); |
927 | 162 | tstate->recursion_headroom--; |
928 | 162 | return -1; |
929 | 162 | } |
930 | 162 | } |
931 | 0 | return 0; |
932 | 162 | } |
933 | | |
934 | | static const _Py_CODEUNIT _Py_INTERPRETER_TRAMPOLINE_INSTRUCTIONS[] = { |
935 | | /* Put a NOP at the start, so that the IP points into |
936 | | * the code, rather than before it */ |
937 | | { .op.code = NOP, .op.arg = 0 }, |
938 | | { .op.code = INTERPRETER_EXIT, .op.arg = 0 }, /* reached on return */ |
939 | | { .op.code = NOP, .op.arg = 0 }, |
940 | | { .op.code = INTERPRETER_EXIT, .op.arg = 0 }, /* reached on yield */ |
941 | | { .op.code = RESUME, .op.arg = RESUME_OPARG_DEPTH1_MASK | RESUME_AT_FUNC_START } |
942 | | }; |
943 | | |
944 | | #ifdef Py_DEBUG |
945 | | extern void _PyUOpPrint(const _PyUOpInstruction *uop); |
946 | | #endif |
947 | | |
948 | | |
949 | | /* Disable unused label warnings. They are handy for debugging, even |
950 | | if computed gotos aren't used. */ |
951 | | |
952 | | /* TBD - what about other compilers? */ |
953 | | #if defined(__GNUC__) || defined(__clang__) |
954 | | # pragma GCC diagnostic push |
955 | | # pragma GCC diagnostic ignored "-Wunused-label" |
956 | | #elif defined(_MSC_VER) /* MS_WINDOWS */ |
957 | | # pragma warning(push) |
958 | | # pragma warning(disable:4102) |
959 | | #endif |
960 | | |
961 | | |
962 | | PyObject ** |
963 | | _PyObjectArray_FromStackRefArray(_PyStackRef *input, Py_ssize_t nargs, PyObject **scratch) |
964 | 2.00G | { |
965 | 2.00G | PyObject **result; |
966 | 2.00G | if (nargs > MAX_STACKREF_SCRATCH) { |
967 | | // +1 in case PY_VECTORCALL_ARGUMENTS_OFFSET is set. |
968 | 293 | result = PyMem_Malloc((nargs + 1) * sizeof(PyObject *)); |
969 | 293 | if (result == NULL) { |
970 | 0 | return NULL; |
971 | 0 | } |
972 | 293 | result++; |
973 | 293 | } |
974 | 2.00G | else { |
975 | 2.00G | result = scratch; |
976 | 2.00G | } |
977 | 5.87G | for (int i = 0; i < nargs; i++) { |
978 | 3.87G | result[i] = PyStackRef_AsPyObjectBorrow(input[i]); |
979 | 3.87G | } |
980 | 2.00G | return result; |
981 | 2.00G | } |
982 | | |
983 | | void |
984 | | _PyObjectArray_Free(PyObject **array, PyObject **scratch) |
985 | 2.00G | { |
986 | 2.00G | if (array != scratch) { |
987 | 293 | PyMem_Free(array); |
988 | 293 | } |
989 | 2.00G | } |
990 | | |
991 | | |
992 | | /* _PyEval_EvalFrameDefault is too large to optimize for speed with PGO on MSVC. |
993 | | */ |
994 | | #if (defined(_MSC_VER) && \ |
995 | | (_MSC_VER < 1943) && \ |
996 | | defined(_Py_USING_PGO)) |
997 | | #define DO_NOT_OPTIMIZE_INTERP_LOOP |
998 | | #endif |
999 | | |
1000 | | #ifdef DO_NOT_OPTIMIZE_INTERP_LOOP |
1001 | | # pragma optimize("t", off) |
1002 | | /* This setting is reversed below following _PyEval_EvalFrameDefault */ |
1003 | | #endif |
1004 | | |
1005 | | #if _Py_TAIL_CALL_INTERP |
1006 | | #include "opcode_targets.h" |
1007 | | #include "generated_cases.c.h" |
1008 | | #endif |
1009 | | |
1010 | | #if (defined(__GNUC__) && __GNUC__ >= 10 && !defined(__clang__)) && defined(__x86_64__) |
1011 | | /* |
1012 | | * gh-129987: The SLP autovectorizer can cause poor code generation for |
1013 | | * opcode dispatch in some GCC versions (observed in GCCs 12 through 15, |
1014 | | * probably caused by https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115777), |
1015 | | * negating any benefit we get from vectorization elsewhere in the |
1016 | | * interpreter loop. Disabling it significantly affected older GCC versions |
1017 | | * (prior to GCC 9, 40% performance drop), so we have to selectively disable |
1018 | | * it. |
1019 | | */ |
1020 | | #define DONT_SLP_VECTORIZE __attribute__((optimize ("no-tree-slp-vectorize"))) |
1021 | | #else |
1022 | | #define DONT_SLP_VECTORIZE |
1023 | | #endif |
1024 | | |
1025 | | typedef struct { |
1026 | | _PyInterpreterFrame frame; |
1027 | | _PyStackRef stack[1]; |
1028 | | } _PyEntryFrame; |
1029 | | |
1030 | | PyObject* _Py_HOT_FUNCTION DONT_SLP_VECTORIZE |
1031 | | _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag) |
1032 | 228M | { |
1033 | 228M | _Py_EnsureTstateNotNULL(tstate); |
1034 | 228M | check_invalid_reentrancy(); |
1035 | 228M | CALL_STAT_INC(pyeval_calls); |
1036 | | |
1037 | 228M | #if USE_COMPUTED_GOTOS && !_Py_TAIL_CALL_INTERP |
1038 | | /* Import the static jump table */ |
1039 | 228M | #include "opcode_targets.h" |
1040 | 228M | void **opcode_targets = opcode_targets_table; |
1041 | 228M | #endif |
1042 | | |
1043 | | #ifdef Py_STATS |
1044 | | int lastopcode = 0; |
1045 | | #endif |
1046 | 228M | #if !_Py_TAIL_CALL_INTERP |
1047 | 228M | uint8_t opcode; /* Current opcode */ |
1048 | 228M | int oparg; /* Current opcode argument, if any */ |
1049 | 228M | assert(tstate->current_frame == NULL || tstate->current_frame->stackpointer != NULL); |
1050 | 228M | #endif |
1051 | 228M | _PyEntryFrame entry; |
1052 | | |
1053 | 228M | if (_Py_EnterRecursiveCallTstate(tstate, "")) { |
1054 | 0 | assert(frame->owner != FRAME_OWNED_BY_INTERPRETER); |
1055 | 0 | _PyEval_FrameClearAndPop(tstate, frame); |
1056 | 0 | return NULL; |
1057 | 0 | } |
1058 | | |
1059 | | /* Local "register" variables. |
1060 | | * These are cached values from the frame and code object. */ |
1061 | 228M | _Py_CODEUNIT *next_instr; |
1062 | 228M | _PyStackRef *stack_pointer; |
1063 | 228M | entry.stack[0] = PyStackRef_NULL; |
1064 | | #ifdef Py_STACKREF_DEBUG |
1065 | | entry.frame.f_funcobj = PyStackRef_None; |
1066 | | #elif defined(Py_DEBUG) |
1067 | | /* Set these to invalid but identifiable values for debugging. */ |
1068 | | entry.frame.f_funcobj = (_PyStackRef){.bits = 0xaaa0}; |
1069 | | entry.frame.f_locals = (PyObject*)0xaaa1; |
1070 | | entry.frame.frame_obj = (PyFrameObject*)0xaaa2; |
1071 | | entry.frame.f_globals = (PyObject*)0xaaa3; |
1072 | | entry.frame.f_builtins = (PyObject*)0xaaa4; |
1073 | | #endif |
1074 | 228M | entry.frame.f_executable = PyStackRef_None; |
1075 | 228M | entry.frame.instr_ptr = (_Py_CODEUNIT *)_Py_INTERPRETER_TRAMPOLINE_INSTRUCTIONS + 1; |
1076 | 228M | entry.frame.stackpointer = entry.stack; |
1077 | 228M | entry.frame.owner = FRAME_OWNED_BY_INTERPRETER; |
1078 | 228M | entry.frame.visited = 0; |
1079 | 228M | entry.frame.return_offset = 0; |
1080 | | #ifdef Py_DEBUG |
1081 | | entry.frame.lltrace = 0; |
1082 | | #endif |
1083 | | /* Push frame */ |
1084 | 228M | entry.frame.previous = tstate->current_frame; |
1085 | 228M | frame->previous = &entry.frame; |
1086 | 228M | tstate->current_frame = frame; |
1087 | 228M | entry.frame.localsplus[0] = PyStackRef_NULL; |
1088 | | #ifdef _Py_TIER2 |
1089 | | if (tstate->current_executor != NULL) { |
1090 | | entry.frame.localsplus[0] = PyStackRef_FromPyObjectNew(tstate->current_executor); |
1091 | | tstate->current_executor = NULL; |
1092 | | } |
1093 | | #endif |
1094 | | |
1095 | | /* support for generator.throw() */ |
1096 | 228M | if (throwflag) { |
1097 | 982 | if (_Py_EnterRecursivePy(tstate)) { |
1098 | 0 | goto early_exit; |
1099 | 0 | } |
1100 | | #ifdef Py_GIL_DISABLED |
1101 | | /* Load thread-local bytecode */ |
1102 | | if (frame->tlbc_index != ((_PyThreadStateImpl *)tstate)->tlbc_index) { |
1103 | | _Py_CODEUNIT *bytecode = |
1104 | | _PyEval_GetExecutableCode(tstate, _PyFrame_GetCode(frame)); |
1105 | | if (bytecode == NULL) { |
1106 | | goto early_exit; |
1107 | | } |
1108 | | ptrdiff_t off = frame->instr_ptr - _PyFrame_GetBytecode(frame); |
1109 | | frame->tlbc_index = ((_PyThreadStateImpl *)tstate)->tlbc_index; |
1110 | | frame->instr_ptr = bytecode + off; |
1111 | | } |
1112 | | #endif |
1113 | | /* Because this avoids the RESUME, we need to update instrumentation */ |
1114 | 982 | _Py_Instrument(_PyFrame_GetCode(frame), tstate->interp); |
1115 | 982 | next_instr = frame->instr_ptr; |
1116 | 982 | monitor_throw(tstate, frame, next_instr); |
1117 | 982 | stack_pointer = _PyFrame_GetStackPointer(frame); |
1118 | | #if _Py_TAIL_CALL_INTERP |
1119 | | # if Py_STATS |
1120 | | return _TAIL_CALL_error(frame, stack_pointer, tstate, next_instr, instruction_funcptr_table, 0, lastopcode); |
1121 | | # else |
1122 | | return _TAIL_CALL_error(frame, stack_pointer, tstate, next_instr, instruction_funcptr_table, 0); |
1123 | | # endif |
1124 | | #else |
1125 | 982 | goto error; |
1126 | 982 | #endif |
1127 | 982 | } |
1128 | | |
1129 | | #if _Py_TAIL_CALL_INTERP |
1130 | | # if Py_STATS |
1131 | | return _TAIL_CALL_start_frame(frame, NULL, tstate, NULL, instruction_funcptr_table, 0, lastopcode); |
1132 | | # else |
1133 | | return _TAIL_CALL_start_frame(frame, NULL, tstate, NULL, instruction_funcptr_table, 0); |
1134 | | # endif |
1135 | | #else |
1136 | 228M | goto start_frame; |
1137 | 228M | # include "generated_cases.c.h" |
1138 | 0 | #endif |
1139 | | |
1140 | | |
1141 | 0 | early_exit: |
1142 | 0 | assert(_PyErr_Occurred(tstate)); |
1143 | 0 | _Py_LeaveRecursiveCallPy(tstate); |
1144 | 0 | assert(frame->owner != FRAME_OWNED_BY_INTERPRETER); |
1145 | | // GH-99729: We need to unlink the frame *before* clearing it: |
1146 | 0 | _PyInterpreterFrame *dying = frame; |
1147 | 0 | frame = tstate->current_frame = dying->previous; |
1148 | 0 | _PyEval_FrameClearAndPop(tstate, dying); |
1149 | 0 | frame->return_offset = 0; |
1150 | 0 | assert(frame->owner == FRAME_OWNED_BY_INTERPRETER); |
1151 | | /* Restore previous frame and exit */ |
1152 | 0 | tstate->current_frame = frame->previous; |
1153 | 0 | return NULL; |
1154 | 88.9G | } |
1155 | | #ifdef _Py_TIER2 |
1156 | | #ifdef _Py_JIT |
1157 | | _PyJitEntryFuncPtr _Py_jit_entry = _Py_LazyJitTrampoline; |
1158 | | #else |
1159 | | _PyJitEntryFuncPtr _Py_jit_entry = _PyTier2Interpreter; |
1160 | | #endif |
1161 | | #endif |
1162 | | |
1163 | | #if defined(_Py_TIER2) && !defined(_Py_JIT) |
1164 | | |
1165 | | _Py_CODEUNIT * |
1166 | | _PyTier2Interpreter( |
1167 | | _PyExecutorObject *current_executor, _PyInterpreterFrame *frame, |
1168 | | _PyStackRef *stack_pointer, PyThreadState *tstate |
1169 | | ) { |
1170 | | const _PyUOpInstruction *next_uop; |
1171 | | int oparg; |
1172 | | tier2_start: |
1173 | | |
1174 | | next_uop = current_executor->trace; |
1175 | | assert(next_uop->opcode == _START_EXECUTOR || next_uop->opcode == _COLD_EXIT); |
1176 | | |
1177 | | #undef LOAD_IP |
1178 | | #define LOAD_IP(UNUSED) (void)0 |
1179 | | |
1180 | | #ifdef Py_STATS |
1181 | | // Disable these macros that apply to Tier 1 stats when we are in Tier 2 |
1182 | | #undef STAT_INC |
1183 | | #define STAT_INC(opname, name) ((void)0) |
1184 | | #undef STAT_DEC |
1185 | | #define STAT_DEC(opname, name) ((void)0) |
1186 | | #endif |
1187 | | |
1188 | | #undef ENABLE_SPECIALIZATION |
1189 | | #define ENABLE_SPECIALIZATION 0 |
1190 | | #undef ENABLE_SPECIALIZATION_FT |
1191 | | #define ENABLE_SPECIALIZATION_FT 0 |
1192 | | |
1193 | | uint16_t uopcode; |
1194 | | #ifdef Py_STATS |
1195 | | int lastuop = 0; |
1196 | | uint64_t trace_uop_execution_counter = 0; |
1197 | | #endif |
1198 | | |
1199 | | assert(next_uop->opcode == _START_EXECUTOR || next_uop->opcode == _COLD_EXIT); |
1200 | | tier2_dispatch: |
1201 | | for (;;) { |
1202 | | uopcode = next_uop->opcode; |
1203 | | #ifdef Py_DEBUG |
1204 | | if (frame->lltrace >= 3) { |
1205 | | dump_stack(frame, stack_pointer); |
1206 | | if (next_uop->opcode == _START_EXECUTOR) { |
1207 | | printf("%4d uop: ", 0); |
1208 | | } |
1209 | | else { |
1210 | | printf("%4d uop: ", (int)(next_uop - current_executor->trace)); |
1211 | | } |
1212 | | _PyUOpPrint(next_uop); |
1213 | | printf("\n"); |
1214 | | } |
1215 | | #endif |
1216 | | next_uop++; |
1217 | | OPT_STAT_INC(uops_executed); |
1218 | | UOP_STAT_INC(uopcode, execution_count); |
1219 | | UOP_PAIR_INC(uopcode, lastuop); |
1220 | | #ifdef Py_STATS |
1221 | | trace_uop_execution_counter++; |
1222 | | ((_PyUOpInstruction *)next_uop)[-1].execution_count++; |
1223 | | #endif |
1224 | | |
1225 | | switch (uopcode) { |
1226 | | |
1227 | | #include "executor_cases.c.h" |
1228 | | |
1229 | | default: |
1230 | | #ifdef Py_DEBUG |
1231 | | { |
1232 | | printf("Unknown uop: "); |
1233 | | _PyUOpPrint(&next_uop[-1]); |
1234 | | printf(" @ %d\n", (int)(next_uop - current_executor->trace - 1)); |
1235 | | Py_FatalError("Unknown uop"); |
1236 | | } |
1237 | | #else |
1238 | | Py_UNREACHABLE(); |
1239 | | #endif |
1240 | | |
1241 | | } |
1242 | | } |
1243 | | |
1244 | | jump_to_error_target: |
1245 | | #ifdef Py_DEBUG |
1246 | | if (frame->lltrace >= 2) { |
1247 | | printf("Error: [UOp "); |
1248 | | _PyUOpPrint(&next_uop[-1]); |
1249 | | printf(" @ %d -> %s]\n", |
1250 | | (int)(next_uop - current_executor->trace - 1), |
1251 | | _PyOpcode_OpName[frame->instr_ptr->op.code]); |
1252 | | fflush(stdout); |
1253 | | } |
1254 | | #endif |
1255 | | assert(next_uop[-1].format == UOP_FORMAT_JUMP); |
1256 | | uint16_t target = uop_get_error_target(&next_uop[-1]); |
1257 | | next_uop = current_executor->trace + target; |
1258 | | goto tier2_dispatch; |
1259 | | |
1260 | | jump_to_jump_target: |
1261 | | assert(next_uop[-1].format == UOP_FORMAT_JUMP); |
1262 | | target = uop_get_jump_target(&next_uop[-1]); |
1263 | | next_uop = current_executor->trace + target; |
1264 | | goto tier2_dispatch; |
1265 | | |
1266 | | } |
1267 | | #endif // _Py_TIER2 |
1268 | | |
1269 | | |
1270 | | #ifdef DO_NOT_OPTIMIZE_INTERP_LOOP |
1271 | | # pragma optimize("", on) |
1272 | | #endif |
1273 | | |
1274 | | #if defined(__GNUC__) || defined(__clang__) |
1275 | | # pragma GCC diagnostic pop |
1276 | | #elif defined(_MSC_VER) /* MS_WINDOWS */ |
1277 | | # pragma warning(pop) |
1278 | | #endif |
1279 | | |
1280 | | static void |
1281 | | format_missing(PyThreadState *tstate, const char *kind, |
1282 | | PyCodeObject *co, PyObject *names, PyObject *qualname) |
1283 | 0 | { |
1284 | 0 | int err; |
1285 | 0 | Py_ssize_t len = PyList_GET_SIZE(names); |
1286 | 0 | PyObject *name_str, *comma, *tail, *tmp; |
1287 | |
|
1288 | 0 | assert(PyList_CheckExact(names)); |
1289 | 0 | assert(len >= 1); |
1290 | | /* Deal with the joys of natural language. */ |
1291 | 0 | switch (len) { |
1292 | 0 | case 1: |
1293 | 0 | name_str = PyList_GET_ITEM(names, 0); |
1294 | 0 | Py_INCREF(name_str); |
1295 | 0 | break; |
1296 | 0 | case 2: |
1297 | 0 | name_str = PyUnicode_FromFormat("%U and %U", |
1298 | 0 | PyList_GET_ITEM(names, len - 2), |
1299 | 0 | PyList_GET_ITEM(names, len - 1)); |
1300 | 0 | break; |
1301 | 0 | default: |
1302 | 0 | tail = PyUnicode_FromFormat(", %U, and %U", |
1303 | 0 | PyList_GET_ITEM(names, len - 2), |
1304 | 0 | PyList_GET_ITEM(names, len - 1)); |
1305 | 0 | if (tail == NULL) |
1306 | 0 | return; |
1307 | | /* Chop off the last two objects in the list. This shouldn't actually |
1308 | | fail, but we can't be too careful. */ |
1309 | 0 | err = PyList_SetSlice(names, len - 2, len, NULL); |
1310 | 0 | if (err == -1) { |
1311 | 0 | Py_DECREF(tail); |
1312 | 0 | return; |
1313 | 0 | } |
1314 | | /* Stitch everything up into a nice comma-separated list. */ |
1315 | 0 | comma = PyUnicode_FromString(", "); |
1316 | 0 | if (comma == NULL) { |
1317 | 0 | Py_DECREF(tail); |
1318 | 0 | return; |
1319 | 0 | } |
1320 | 0 | tmp = PyUnicode_Join(comma, names); |
1321 | 0 | Py_DECREF(comma); |
1322 | 0 | if (tmp == NULL) { |
1323 | 0 | Py_DECREF(tail); |
1324 | 0 | return; |
1325 | 0 | } |
1326 | 0 | name_str = PyUnicode_Concat(tmp, tail); |
1327 | 0 | Py_DECREF(tmp); |
1328 | 0 | Py_DECREF(tail); |
1329 | 0 | break; |
1330 | 0 | } |
1331 | 0 | if (name_str == NULL) |
1332 | 0 | return; |
1333 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
1334 | 0 | "%U() missing %i required %s argument%s: %U", |
1335 | 0 | qualname, |
1336 | 0 | len, |
1337 | 0 | kind, |
1338 | 0 | len == 1 ? "" : "s", |
1339 | 0 | name_str); |
1340 | 0 | Py_DECREF(name_str); |
1341 | 0 | } |
1342 | | |
1343 | | static void |
1344 | | missing_arguments(PyThreadState *tstate, PyCodeObject *co, |
1345 | | Py_ssize_t missing, Py_ssize_t defcount, |
1346 | | _PyStackRef *localsplus, PyObject *qualname) |
1347 | 0 | { |
1348 | 0 | Py_ssize_t i, j = 0; |
1349 | 0 | Py_ssize_t start, end; |
1350 | 0 | int positional = (defcount != -1); |
1351 | 0 | const char *kind = positional ? "positional" : "keyword-only"; |
1352 | 0 | PyObject *missing_names; |
1353 | | |
1354 | | /* Compute the names of the arguments that are missing. */ |
1355 | 0 | missing_names = PyList_New(missing); |
1356 | 0 | if (missing_names == NULL) |
1357 | 0 | return; |
1358 | 0 | if (positional) { |
1359 | 0 | start = 0; |
1360 | 0 | end = co->co_argcount - defcount; |
1361 | 0 | } |
1362 | 0 | else { |
1363 | 0 | start = co->co_argcount; |
1364 | 0 | end = start + co->co_kwonlyargcount; |
1365 | 0 | } |
1366 | 0 | for (i = start; i < end; i++) { |
1367 | 0 | if (PyStackRef_IsNull(localsplus[i])) { |
1368 | 0 | PyObject *raw = PyTuple_GET_ITEM(co->co_localsplusnames, i); |
1369 | 0 | PyObject *name = PyObject_Repr(raw); |
1370 | 0 | if (name == NULL) { |
1371 | 0 | Py_DECREF(missing_names); |
1372 | 0 | return; |
1373 | 0 | } |
1374 | 0 | PyList_SET_ITEM(missing_names, j++, name); |
1375 | 0 | } |
1376 | 0 | } |
1377 | 0 | assert(j == missing); |
1378 | 0 | format_missing(tstate, kind, co, missing_names, qualname); |
1379 | 0 | Py_DECREF(missing_names); |
1380 | 0 | } |
1381 | | |
1382 | | static void |
1383 | | too_many_positional(PyThreadState *tstate, PyCodeObject *co, |
1384 | | Py_ssize_t given, PyObject *defaults, |
1385 | | _PyStackRef *localsplus, PyObject *qualname) |
1386 | 0 | { |
1387 | 0 | int plural; |
1388 | 0 | Py_ssize_t kwonly_given = 0; |
1389 | 0 | Py_ssize_t i; |
1390 | 0 | PyObject *sig, *kwonly_sig; |
1391 | 0 | Py_ssize_t co_argcount = co->co_argcount; |
1392 | |
|
1393 | 0 | assert((co->co_flags & CO_VARARGS) == 0); |
1394 | | /* Count missing keyword-only args. */ |
1395 | 0 | for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) { |
1396 | 0 | if (PyStackRef_AsPyObjectBorrow(localsplus[i]) != NULL) { |
1397 | 0 | kwonly_given++; |
1398 | 0 | } |
1399 | 0 | } |
1400 | 0 | Py_ssize_t defcount = defaults == NULL ? 0 : PyTuple_GET_SIZE(defaults); |
1401 | 0 | if (defcount) { |
1402 | 0 | Py_ssize_t atleast = co_argcount - defcount; |
1403 | 0 | plural = 1; |
1404 | 0 | sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount); |
1405 | 0 | } |
1406 | 0 | else { |
1407 | 0 | plural = (co_argcount != 1); |
1408 | 0 | sig = PyUnicode_FromFormat("%zd", co_argcount); |
1409 | 0 | } |
1410 | 0 | if (sig == NULL) |
1411 | 0 | return; |
1412 | 0 | if (kwonly_given) { |
1413 | 0 | const char *format = " positional argument%s (and %zd keyword-only argument%s)"; |
1414 | 0 | kwonly_sig = PyUnicode_FromFormat(format, |
1415 | 0 | given != 1 ? "s" : "", |
1416 | 0 | kwonly_given, |
1417 | 0 | kwonly_given != 1 ? "s" : ""); |
1418 | 0 | if (kwonly_sig == NULL) { |
1419 | 0 | Py_DECREF(sig); |
1420 | 0 | return; |
1421 | 0 | } |
1422 | 0 | } |
1423 | 0 | else { |
1424 | | /* This will not fail. */ |
1425 | 0 | kwonly_sig = Py_GetConstant(Py_CONSTANT_EMPTY_STR); |
1426 | 0 | assert(kwonly_sig != NULL); |
1427 | 0 | } |
1428 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
1429 | 0 | "%U() takes %U positional argument%s but %zd%U %s given", |
1430 | 0 | qualname, |
1431 | 0 | sig, |
1432 | 0 | plural ? "s" : "", |
1433 | 0 | given, |
1434 | 0 | kwonly_sig, |
1435 | 0 | given == 1 && !kwonly_given ? "was" : "were"); |
1436 | 0 | Py_DECREF(sig); |
1437 | 0 | Py_DECREF(kwonly_sig); |
1438 | 0 | } |
1439 | | |
1440 | | static int |
1441 | | positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co, |
1442 | | Py_ssize_t kwcount, PyObject* kwnames, |
1443 | | PyObject *qualname) |
1444 | 0 | { |
1445 | 0 | int posonly_conflicts = 0; |
1446 | 0 | PyObject* posonly_names = PyList_New(0); |
1447 | 0 | if (posonly_names == NULL) { |
1448 | 0 | goto fail; |
1449 | 0 | } |
1450 | 0 | for(int k=0; k < co->co_posonlyargcount; k++){ |
1451 | 0 | PyObject* posonly_name = PyTuple_GET_ITEM(co->co_localsplusnames, k); |
1452 | |
|
1453 | 0 | for (int k2=0; k2<kwcount; k2++){ |
1454 | | /* Compare the pointers first and fallback to PyObject_RichCompareBool*/ |
1455 | 0 | PyObject* kwname = PyTuple_GET_ITEM(kwnames, k2); |
1456 | 0 | if (kwname == posonly_name){ |
1457 | 0 | if(PyList_Append(posonly_names, kwname) != 0) { |
1458 | 0 | goto fail; |
1459 | 0 | } |
1460 | 0 | posonly_conflicts++; |
1461 | 0 | continue; |
1462 | 0 | } |
1463 | | |
1464 | 0 | int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ); |
1465 | |
|
1466 | 0 | if ( cmp > 0) { |
1467 | 0 | if(PyList_Append(posonly_names, kwname) != 0) { |
1468 | 0 | goto fail; |
1469 | 0 | } |
1470 | 0 | posonly_conflicts++; |
1471 | 0 | } else if (cmp < 0) { |
1472 | 0 | goto fail; |
1473 | 0 | } |
1474 | |
|
1475 | 0 | } |
1476 | 0 | } |
1477 | 0 | if (posonly_conflicts) { |
1478 | 0 | PyObject* comma = PyUnicode_FromString(", "); |
1479 | 0 | if (comma == NULL) { |
1480 | 0 | goto fail; |
1481 | 0 | } |
1482 | 0 | PyObject* error_names = PyUnicode_Join(comma, posonly_names); |
1483 | 0 | Py_DECREF(comma); |
1484 | 0 | if (error_names == NULL) { |
1485 | 0 | goto fail; |
1486 | 0 | } |
1487 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
1488 | 0 | "%U() got some positional-only arguments passed" |
1489 | 0 | " as keyword arguments: '%U'", |
1490 | 0 | qualname, error_names); |
1491 | 0 | Py_DECREF(error_names); |
1492 | 0 | goto fail; |
1493 | 0 | } |
1494 | | |
1495 | 0 | Py_DECREF(posonly_names); |
1496 | 0 | return 0; |
1497 | | |
1498 | 0 | fail: |
1499 | 0 | Py_XDECREF(posonly_names); |
1500 | 0 | return 1; |
1501 | |
|
1502 | 0 | } |
1503 | | |
1504 | | |
1505 | | static inline unsigned char * |
1506 | 812k | scan_back_to_entry_start(unsigned char *p) { |
1507 | 3.26M | for (; (p[0]&128) == 0; p--); |
1508 | 812k | return p; |
1509 | 812k | } |
1510 | | |
1511 | | static inline unsigned char * |
1512 | 5.80M | skip_to_next_entry(unsigned char *p, unsigned char *end) { |
1513 | 23.1M | while (p < end && ((p[0] & 128) == 0)) { |
1514 | 17.3M | p++; |
1515 | 17.3M | } |
1516 | 5.80M | return p; |
1517 | 5.80M | } |
1518 | | |
1519 | | |
1520 | 32.4M | #define MAX_LINEAR_SEARCH 40 |
1521 | | |
1522 | | static Py_NO_INLINE int |
1523 | | get_exception_handler(PyCodeObject *code, int index, int *level, int *handler, int *lasti) |
1524 | 31.6M | { |
1525 | 31.6M | unsigned char *start = (unsigned char *)PyBytes_AS_STRING(code->co_exceptiontable); |
1526 | 31.6M | unsigned char *end = start + PyBytes_GET_SIZE(code->co_exceptiontable); |
1527 | | /* Invariants: |
1528 | | * start_table == end_table OR |
1529 | | * start_table points to a legal entry and end_table points |
1530 | | * beyond the table or to a legal entry that is after index. |
1531 | | */ |
1532 | 31.6M | if (end - start > MAX_LINEAR_SEARCH) { |
1533 | 689k | int offset; |
1534 | 689k | parse_varint(start, &offset); |
1535 | 689k | if (offset > index) { |
1536 | 80.0k | return 0; |
1537 | 80.0k | } |
1538 | 812k | do { |
1539 | 812k | unsigned char * mid = start + ((end-start)>>1); |
1540 | 812k | mid = scan_back_to_entry_start(mid); |
1541 | 812k | parse_varint(mid, &offset); |
1542 | 812k | if (offset > index) { |
1543 | 656k | end = mid; |
1544 | 656k | } |
1545 | 155k | else { |
1546 | 155k | start = mid; |
1547 | 155k | } |
1548 | | |
1549 | 812k | } while (end - start > MAX_LINEAR_SEARCH); |
1550 | 609k | } |
1551 | 31.5M | unsigned char *scan = start; |
1552 | 37.3M | while (scan < end) { |
1553 | 24.8M | int start_offset, size; |
1554 | 24.8M | scan = parse_varint(scan, &start_offset); |
1555 | 24.8M | if (start_offset > index) { |
1556 | 299k | break; |
1557 | 299k | } |
1558 | 24.5M | scan = parse_varint(scan, &size); |
1559 | 24.5M | if (start_offset + size > index) { |
1560 | 18.7M | scan = parse_varint(scan, handler); |
1561 | 18.7M | int depth_and_lasti; |
1562 | 18.7M | parse_varint(scan, &depth_and_lasti); |
1563 | 18.7M | *level = depth_and_lasti >> 1; |
1564 | 18.7M | *lasti = depth_and_lasti & 1; |
1565 | 18.7M | return 1; |
1566 | 18.7M | } |
1567 | 5.80M | scan = skip_to_next_entry(scan, end); |
1568 | 5.80M | } |
1569 | 12.8M | return 0; |
1570 | 31.5M | } |
1571 | | |
1572 | | static int |
1573 | | initialize_locals(PyThreadState *tstate, PyFunctionObject *func, |
1574 | | _PyStackRef *localsplus, _PyStackRef const *args, |
1575 | | Py_ssize_t argcount, PyObject *kwnames) |
1576 | 162M | { |
1577 | 162M | PyCodeObject *co = (PyCodeObject*)func->func_code; |
1578 | 162M | const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount; |
1579 | | /* Create a dictionary for keyword parameters (**kwags) */ |
1580 | 162M | PyObject *kwdict; |
1581 | 162M | Py_ssize_t i; |
1582 | 162M | if (co->co_flags & CO_VARKEYWORDS) { |
1583 | 18.0M | kwdict = PyDict_New(); |
1584 | 18.0M | if (kwdict == NULL) { |
1585 | 0 | goto fail_pre_positional; |
1586 | 0 | } |
1587 | 18.0M | i = total_args; |
1588 | 18.0M | if (co->co_flags & CO_VARARGS) { |
1589 | 17.9M | i++; |
1590 | 17.9M | } |
1591 | 18.0M | assert(PyStackRef_IsNull(localsplus[i])); |
1592 | 18.0M | localsplus[i] = PyStackRef_FromPyObjectSteal(kwdict); |
1593 | 18.0M | } |
1594 | 144M | else { |
1595 | 144M | kwdict = NULL; |
1596 | 144M | } |
1597 | | |
1598 | | /* Copy all positional arguments into local variables */ |
1599 | 162M | Py_ssize_t j, n; |
1600 | 162M | if (argcount > co->co_argcount) { |
1601 | 7.55M | n = co->co_argcount; |
1602 | 7.55M | } |
1603 | 154M | else { |
1604 | 154M | n = argcount; |
1605 | 154M | } |
1606 | 445M | for (j = 0; j < n; j++) { |
1607 | 283M | assert(PyStackRef_IsNull(localsplus[j])); |
1608 | 283M | localsplus[j] = args[j]; |
1609 | 283M | } |
1610 | | |
1611 | | /* Pack other positional arguments into the *args argument */ |
1612 | 162M | if (co->co_flags & CO_VARARGS) { |
1613 | 19.3M | PyObject *u = NULL; |
1614 | 19.3M | if (argcount == n) { |
1615 | 11.7M | u = (PyObject *)&_Py_SINGLETON(tuple_empty); |
1616 | 11.7M | } |
1617 | 7.55M | else { |
1618 | 7.55M | u = _PyTuple_FromStackRefStealOnSuccess(args + n, argcount - n); |
1619 | 7.55M | if (u == NULL) { |
1620 | 0 | for (Py_ssize_t i = n; i < argcount; i++) { |
1621 | 0 | PyStackRef_CLOSE(args[i]); |
1622 | 0 | } |
1623 | 0 | } |
1624 | 7.55M | } |
1625 | 19.3M | if (u == NULL) { |
1626 | 0 | goto fail_post_positional; |
1627 | 0 | } |
1628 | 19.3M | assert(PyStackRef_AsPyObjectBorrow(localsplus[total_args]) == NULL); |
1629 | 19.3M | localsplus[total_args] = PyStackRef_FromPyObjectSteal(u); |
1630 | 19.3M | } |
1631 | 142M | else if (argcount > n) { |
1632 | | /* Too many positional args. Error is reported later */ |
1633 | 0 | for (j = n; j < argcount; j++) { |
1634 | 0 | PyStackRef_CLOSE(args[j]); |
1635 | 0 | } |
1636 | 0 | } |
1637 | | |
1638 | | /* Handle keyword arguments */ |
1639 | 162M | if (kwnames != NULL) { |
1640 | 8.73M | Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames); |
1641 | 18.8M | for (i = 0; i < kwcount; i++) { |
1642 | 10.1M | PyObject **co_varnames; |
1643 | 10.1M | PyObject *keyword = PyTuple_GET_ITEM(kwnames, i); |
1644 | 10.1M | _PyStackRef value_stackref = args[i+argcount]; |
1645 | 10.1M | Py_ssize_t j; |
1646 | | |
1647 | 10.1M | if (keyword == NULL || !PyUnicode_Check(keyword)) { |
1648 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
1649 | 0 | "%U() keywords must be strings", |
1650 | 0 | func->func_qualname); |
1651 | 0 | goto kw_fail; |
1652 | 0 | } |
1653 | | |
1654 | | /* Speed hack: do raw pointer compares. As names are |
1655 | | normally interned this should almost always hit. */ |
1656 | 10.1M | co_varnames = ((PyTupleObject *)(co->co_localsplusnames))->ob_item; |
1657 | 21.9M | for (j = co->co_posonlyargcount; j < total_args; j++) { |
1658 | 20.3M | PyObject *varname = co_varnames[j]; |
1659 | 20.3M | if (varname == keyword) { |
1660 | 8.53M | goto kw_found; |
1661 | 8.53M | } |
1662 | 20.3M | } |
1663 | | |
1664 | | /* Slow fallback, just in case */ |
1665 | 3.16M | for (j = co->co_posonlyargcount; j < total_args; j++) { |
1666 | 1.58M | PyObject *varname = co_varnames[j]; |
1667 | 1.58M | int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ); |
1668 | 1.58M | if (cmp > 0) { |
1669 | 204 | goto kw_found; |
1670 | 204 | } |
1671 | 1.58M | else if (cmp < 0) { |
1672 | 0 | goto kw_fail; |
1673 | 0 | } |
1674 | 1.58M | } |
1675 | | |
1676 | 1.58M | assert(j >= total_args); |
1677 | 1.58M | if (kwdict == NULL) { |
1678 | |
|
1679 | 0 | if (co->co_posonlyargcount |
1680 | 0 | && positional_only_passed_as_keyword(tstate, co, |
1681 | 0 | kwcount, kwnames, |
1682 | 0 | func->func_qualname)) |
1683 | 0 | { |
1684 | 0 | goto kw_fail; |
1685 | 0 | } |
1686 | | |
1687 | 0 | PyObject* suggestion_keyword = NULL; |
1688 | 0 | if (total_args > co->co_posonlyargcount) { |
1689 | 0 | PyObject* possible_keywords = PyList_New(total_args - co->co_posonlyargcount); |
1690 | |
|
1691 | 0 | if (!possible_keywords) { |
1692 | 0 | PyErr_Clear(); |
1693 | 0 | } else { |
1694 | 0 | for (Py_ssize_t k = co->co_posonlyargcount; k < total_args; k++) { |
1695 | 0 | PyList_SET_ITEM(possible_keywords, k - co->co_posonlyargcount, co_varnames[k]); |
1696 | 0 | } |
1697 | |
|
1698 | 0 | suggestion_keyword = _Py_CalculateSuggestions(possible_keywords, keyword); |
1699 | 0 | Py_DECREF(possible_keywords); |
1700 | 0 | } |
1701 | 0 | } |
1702 | |
|
1703 | 0 | if (suggestion_keyword) { |
1704 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
1705 | 0 | "%U() got an unexpected keyword argument '%S'. Did you mean '%S'?", |
1706 | 0 | func->func_qualname, keyword, suggestion_keyword); |
1707 | 0 | Py_DECREF(suggestion_keyword); |
1708 | 0 | } else { |
1709 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
1710 | 0 | "%U() got an unexpected keyword argument '%S'", |
1711 | 0 | func->func_qualname, keyword); |
1712 | 0 | } |
1713 | |
|
1714 | 0 | goto kw_fail; |
1715 | 0 | } |
1716 | | |
1717 | 1.58M | if (PyDict_SetItem(kwdict, keyword, PyStackRef_AsPyObjectBorrow(value_stackref)) == -1) { |
1718 | 0 | goto kw_fail; |
1719 | 0 | } |
1720 | 1.58M | PyStackRef_CLOSE(value_stackref); |
1721 | 1.58M | continue; |
1722 | | |
1723 | 0 | kw_fail: |
1724 | 0 | for (;i < kwcount; i++) { |
1725 | 0 | PyStackRef_CLOSE(args[i+argcount]); |
1726 | 0 | } |
1727 | 0 | goto fail_post_args; |
1728 | | |
1729 | 8.53M | kw_found: |
1730 | 8.53M | if (PyStackRef_AsPyObjectBorrow(localsplus[j]) != NULL) { |
1731 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
1732 | 0 | "%U() got multiple values for argument '%S'", |
1733 | 0 | func->func_qualname, keyword); |
1734 | 0 | goto kw_fail; |
1735 | 0 | } |
1736 | 8.53M | localsplus[j] = value_stackref; |
1737 | 8.53M | } |
1738 | 8.73M | } |
1739 | | |
1740 | | /* Check the number of positional arguments */ |
1741 | 162M | if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) { |
1742 | 0 | too_many_positional(tstate, co, argcount, func->func_defaults, localsplus, |
1743 | 0 | func->func_qualname); |
1744 | 0 | goto fail_post_args; |
1745 | 0 | } |
1746 | | |
1747 | | /* Add missing positional arguments (copy default values from defs) */ |
1748 | 162M | if (argcount < co->co_argcount) { |
1749 | 28.6M | Py_ssize_t defcount = func->func_defaults == NULL ? 0 : PyTuple_GET_SIZE(func->func_defaults); |
1750 | 28.6M | Py_ssize_t m = co->co_argcount - defcount; |
1751 | 28.6M | Py_ssize_t missing = 0; |
1752 | 28.6M | for (i = argcount; i < m; i++) { |
1753 | 12.1k | if (PyStackRef_IsNull(localsplus[i])) { |
1754 | 0 | missing++; |
1755 | 0 | } |
1756 | 12.1k | } |
1757 | 28.6M | if (missing) { |
1758 | 0 | missing_arguments(tstate, co, missing, defcount, localsplus, |
1759 | 0 | func->func_qualname); |
1760 | 0 | goto fail_post_args; |
1761 | 0 | } |
1762 | 28.6M | if (n > m) |
1763 | 302k | i = n - m; |
1764 | 28.3M | else |
1765 | 28.3M | i = 0; |
1766 | 28.6M | if (defcount) { |
1767 | 28.6M | PyObject **defs = &PyTuple_GET_ITEM(func->func_defaults, 0); |
1768 | 58.6M | for (; i < defcount; i++) { |
1769 | 30.0M | if (PyStackRef_AsPyObjectBorrow(localsplus[m+i]) == NULL) { |
1770 | 22.6M | PyObject *def = defs[i]; |
1771 | 22.6M | localsplus[m+i] = PyStackRef_FromPyObjectNew(def); |
1772 | 22.6M | } |
1773 | 30.0M | } |
1774 | 28.6M | } |
1775 | 28.6M | } |
1776 | | |
1777 | | /* Add missing keyword arguments (copy default values from kwdefs) */ |
1778 | 162M | if (co->co_kwonlyargcount > 0) { |
1779 | 1.56M | Py_ssize_t missing = 0; |
1780 | 5.85M | for (i = co->co_argcount; i < total_args; i++) { |
1781 | 4.29M | if (PyStackRef_AsPyObjectBorrow(localsplus[i]) != NULL) |
1782 | 1.14M | continue; |
1783 | 3.14M | PyObject *varname = PyTuple_GET_ITEM(co->co_localsplusnames, i); |
1784 | 3.14M | if (func->func_kwdefaults != NULL) { |
1785 | 3.14M | PyObject *def; |
1786 | 3.14M | if (PyDict_GetItemRef(func->func_kwdefaults, varname, &def) < 0) { |
1787 | 0 | goto fail_post_args; |
1788 | 0 | } |
1789 | 3.14M | if (def) { |
1790 | 3.14M | localsplus[i] = PyStackRef_FromPyObjectSteal(def); |
1791 | 3.14M | continue; |
1792 | 3.14M | } |
1793 | 3.14M | } |
1794 | 0 | missing++; |
1795 | 0 | } |
1796 | 1.56M | if (missing) { |
1797 | 0 | missing_arguments(tstate, co, missing, -1, localsplus, |
1798 | 0 | func->func_qualname); |
1799 | 0 | goto fail_post_args; |
1800 | 0 | } |
1801 | 1.56M | } |
1802 | 162M | return 0; |
1803 | | |
1804 | 0 | fail_pre_positional: |
1805 | 0 | for (j = 0; j < argcount; j++) { |
1806 | 0 | PyStackRef_CLOSE(args[j]); |
1807 | 0 | } |
1808 | | /* fall through */ |
1809 | 0 | fail_post_positional: |
1810 | 0 | if (kwnames) { |
1811 | 0 | Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames); |
1812 | 0 | for (j = argcount; j < argcount+kwcount; j++) { |
1813 | 0 | PyStackRef_CLOSE(args[j]); |
1814 | 0 | } |
1815 | 0 | } |
1816 | | /* fall through */ |
1817 | 0 | fail_post_args: |
1818 | 0 | return -1; |
1819 | 0 | } |
1820 | | |
1821 | | static void |
1822 | | clear_thread_frame(PyThreadState *tstate, _PyInterpreterFrame * frame) |
1823 | 506M | { |
1824 | 506M | assert(frame->owner == FRAME_OWNED_BY_THREAD); |
1825 | | // Make sure that this is, indeed, the top frame. We can't check this in |
1826 | | // _PyThreadState_PopFrame, since f_code is already cleared at that point: |
1827 | 506M | assert((PyObject **)frame + _PyFrame_GetCode(frame)->co_framesize == |
1828 | 506M | tstate->datastack_top); |
1829 | 506M | assert(frame->frame_obj == NULL || frame->frame_obj->f_frame == frame); |
1830 | 506M | _PyFrame_ClearExceptCode(frame); |
1831 | 506M | PyStackRef_CLEAR(frame->f_executable); |
1832 | 506M | _PyThreadState_PopFrame(tstate, frame); |
1833 | 506M | } |
1834 | | |
1835 | | static void |
1836 | | clear_gen_frame(PyThreadState *tstate, _PyInterpreterFrame * frame) |
1837 | 21.5M | { |
1838 | 21.5M | assert(frame->owner == FRAME_OWNED_BY_GENERATOR); |
1839 | 21.5M | PyGenObject *gen = _PyGen_GetGeneratorFromFrame(frame); |
1840 | 21.5M | gen->gi_frame_state = FRAME_CLEARED; |
1841 | 21.5M | assert(tstate->exc_info == &gen->gi_exc_state); |
1842 | 21.5M | tstate->exc_info = gen->gi_exc_state.previous_item; |
1843 | 21.5M | gen->gi_exc_state.previous_item = NULL; |
1844 | 21.5M | assert(frame->frame_obj == NULL || frame->frame_obj->f_frame == frame); |
1845 | 21.5M | _PyFrame_ClearExceptCode(frame); |
1846 | 21.5M | _PyErr_ClearExcState(&gen->gi_exc_state); |
1847 | 21.5M | frame->previous = NULL; |
1848 | 21.5M | } |
1849 | | |
1850 | | void |
1851 | | _PyEval_FrameClearAndPop(PyThreadState *tstate, _PyInterpreterFrame * frame) |
1852 | 528M | { |
1853 | 528M | if (frame->owner == FRAME_OWNED_BY_THREAD) { |
1854 | 506M | clear_thread_frame(tstate, frame); |
1855 | 506M | } |
1856 | 21.5M | else { |
1857 | 21.5M | clear_gen_frame(tstate, frame); |
1858 | 21.5M | } |
1859 | 528M | } |
1860 | | |
1861 | | /* Consumes references to func, locals and all the args */ |
1862 | | _PyInterpreterFrame * |
1863 | | _PyEvalFramePushAndInit(PyThreadState *tstate, _PyStackRef func, |
1864 | | PyObject *locals, _PyStackRef const* args, |
1865 | | size_t argcount, PyObject *kwnames, _PyInterpreterFrame *previous) |
1866 | 162M | { |
1867 | 162M | PyFunctionObject *func_obj = (PyFunctionObject *)PyStackRef_AsPyObjectBorrow(func); |
1868 | 162M | PyCodeObject * code = (PyCodeObject *)func_obj->func_code; |
1869 | 162M | CALL_STAT_INC(frames_pushed); |
1870 | 162M | _PyInterpreterFrame *frame = _PyThreadState_PushFrame(tstate, code->co_framesize); |
1871 | 162M | if (frame == NULL) { |
1872 | 0 | goto fail; |
1873 | 0 | } |
1874 | 162M | _PyFrame_Initialize(tstate, frame, func, locals, code, 0, previous); |
1875 | 162M | if (initialize_locals(tstate, func_obj, frame->localsplus, args, argcount, kwnames)) { |
1876 | 0 | assert(frame->owner == FRAME_OWNED_BY_THREAD); |
1877 | 0 | clear_thread_frame(tstate, frame); |
1878 | 0 | return NULL; |
1879 | 0 | } |
1880 | 162M | return frame; |
1881 | 0 | fail: |
1882 | | /* Consume the references */ |
1883 | 0 | PyStackRef_CLOSE(func); |
1884 | 0 | Py_XDECREF(locals); |
1885 | 0 | for (size_t i = 0; i < argcount; i++) { |
1886 | 0 | PyStackRef_CLOSE(args[i]); |
1887 | 0 | } |
1888 | 0 | if (kwnames) { |
1889 | 0 | Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames); |
1890 | 0 | for (Py_ssize_t i = 0; i < kwcount; i++) { |
1891 | 0 | PyStackRef_CLOSE(args[i+argcount]); |
1892 | 0 | } |
1893 | 0 | } |
1894 | 0 | PyErr_NoMemory(); |
1895 | 0 | return NULL; |
1896 | 162M | } |
1897 | | |
1898 | | /* Same as _PyEvalFramePushAndInit but takes an args tuple and kwargs dict. |
1899 | | Steals references to func, callargs and kwargs. |
1900 | | */ |
1901 | | static _PyInterpreterFrame * |
1902 | | _PyEvalFramePushAndInit_Ex(PyThreadState *tstate, _PyStackRef func, |
1903 | | PyObject *locals, Py_ssize_t nargs, PyObject *callargs, PyObject *kwargs, _PyInterpreterFrame *previous) |
1904 | 69.1k | { |
1905 | 69.1k | bool has_dict = (kwargs != NULL && PyDict_GET_SIZE(kwargs) > 0); |
1906 | 69.1k | PyObject *kwnames = NULL; |
1907 | 69.1k | _PyStackRef *newargs; |
1908 | 69.1k | PyObject *const *object_array = NULL; |
1909 | 69.1k | _PyStackRef stack_array[8]; |
1910 | 69.1k | if (has_dict) { |
1911 | 2.40k | object_array = _PyStack_UnpackDict(tstate, _PyTuple_ITEMS(callargs), nargs, kwargs, &kwnames); |
1912 | 2.40k | if (object_array == NULL) { |
1913 | 0 | PyStackRef_CLOSE(func); |
1914 | 0 | goto error; |
1915 | 0 | } |
1916 | 2.40k | size_t total_args = nargs + PyDict_GET_SIZE(kwargs); |
1917 | 2.40k | assert(sizeof(PyObject *) == sizeof(_PyStackRef)); |
1918 | 2.40k | newargs = (_PyStackRef *)object_array; |
1919 | 7.45k | for (size_t i = 0; i < total_args; i++) { |
1920 | 5.05k | newargs[i] = PyStackRef_FromPyObjectSteal(object_array[i]); |
1921 | 5.05k | } |
1922 | 2.40k | } |
1923 | 66.7k | else { |
1924 | 66.7k | if (nargs <= 8) { |
1925 | 66.7k | newargs = stack_array; |
1926 | 66.7k | } |
1927 | 16 | else { |
1928 | 16 | newargs = PyMem_Malloc(sizeof(_PyStackRef) *nargs); |
1929 | 16 | if (newargs == NULL) { |
1930 | 0 | PyErr_NoMemory(); |
1931 | 0 | PyStackRef_CLOSE(func); |
1932 | 0 | goto error; |
1933 | 0 | } |
1934 | 16 | } |
1935 | | /* We need to create a new reference for all our args since the new frame steals them. */ |
1936 | 202k | for (Py_ssize_t i = 0; i < nargs; i++) { |
1937 | 135k | newargs[i] = PyStackRef_FromPyObjectNew(PyTuple_GET_ITEM(callargs, i)); |
1938 | 135k | } |
1939 | 66.7k | } |
1940 | 69.1k | _PyInterpreterFrame *new_frame = _PyEvalFramePushAndInit( |
1941 | 69.1k | tstate, func, locals, |
1942 | 69.1k | newargs, nargs, kwnames, previous |
1943 | 69.1k | ); |
1944 | 69.1k | if (has_dict) { |
1945 | 2.40k | _PyStack_UnpackDict_FreeNoDecRef(object_array, kwnames); |
1946 | 2.40k | } |
1947 | 66.7k | else if (nargs > 8) { |
1948 | 16 | PyMem_Free((void *)newargs); |
1949 | 16 | } |
1950 | | /* No need to decref func here because the reference has been stolen by |
1951 | | _PyEvalFramePushAndInit. |
1952 | | */ |
1953 | 69.1k | Py_DECREF(callargs); |
1954 | 69.1k | Py_XDECREF(kwargs); |
1955 | 69.1k | return new_frame; |
1956 | 0 | error: |
1957 | 0 | Py_DECREF(callargs); |
1958 | 0 | Py_XDECREF(kwargs); |
1959 | 0 | return NULL; |
1960 | 69.1k | } |
1961 | | |
1962 | | PyObject * |
1963 | | _PyEval_Vector(PyThreadState *tstate, PyFunctionObject *func, |
1964 | | PyObject *locals, |
1965 | | PyObject* const* args, size_t argcount, |
1966 | | PyObject *kwnames) |
1967 | 139M | { |
1968 | 139M | size_t total_args = argcount; |
1969 | 139M | if (kwnames) { |
1970 | 7.42M | total_args += PyTuple_GET_SIZE(kwnames); |
1971 | 7.42M | } |
1972 | 139M | _PyStackRef stack_array[8]; |
1973 | 139M | _PyStackRef *arguments; |
1974 | 139M | if (total_args <= 8) { |
1975 | 139M | arguments = stack_array; |
1976 | 139M | } |
1977 | 756 | else { |
1978 | 756 | arguments = PyMem_Malloc(sizeof(_PyStackRef) * total_args); |
1979 | 756 | if (arguments == NULL) { |
1980 | 0 | return PyErr_NoMemory(); |
1981 | 0 | } |
1982 | 756 | } |
1983 | | /* _PyEvalFramePushAndInit consumes the references |
1984 | | * to func, locals and all its arguments */ |
1985 | 139M | Py_XINCREF(locals); |
1986 | 379M | for (size_t i = 0; i < argcount; i++) { |
1987 | 239M | arguments[i] = PyStackRef_FromPyObjectNew(args[i]); |
1988 | 239M | } |
1989 | 139M | if (kwnames) { |
1990 | 7.42M | Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames); |
1991 | 16.2M | for (Py_ssize_t i = 0; i < kwcount; i++) { |
1992 | 8.78M | arguments[i+argcount] = PyStackRef_FromPyObjectNew(args[i+argcount]); |
1993 | 8.78M | } |
1994 | 7.42M | } |
1995 | 139M | _PyInterpreterFrame *frame = _PyEvalFramePushAndInit( |
1996 | 139M | tstate, PyStackRef_FromPyObjectNew(func), locals, |
1997 | 139M | arguments, argcount, kwnames, NULL); |
1998 | 139M | if (total_args > 8) { |
1999 | 756 | PyMem_Free(arguments); |
2000 | 756 | } |
2001 | 139M | if (frame == NULL) { |
2002 | 0 | return NULL; |
2003 | 0 | } |
2004 | 139M | EVAL_CALL_STAT_INC(EVAL_CALL_VECTOR); |
2005 | 139M | return _PyEval_EvalFrame(tstate, frame, 0); |
2006 | 139M | } |
2007 | | |
2008 | | /* Legacy API */ |
2009 | | PyObject * |
2010 | | PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals, |
2011 | | PyObject *const *args, int argcount, |
2012 | | PyObject *const *kws, int kwcount, |
2013 | | PyObject *const *defs, int defcount, |
2014 | | PyObject *kwdefs, PyObject *closure) |
2015 | 0 | { |
2016 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
2017 | 0 | PyObject *res = NULL; |
2018 | 0 | PyObject *defaults = PyTuple_FromArray(defs, defcount); |
2019 | 0 | if (defaults == NULL) { |
2020 | 0 | return NULL; |
2021 | 0 | } |
2022 | 0 | PyObject *builtins = _PyDict_LoadBuiltinsFromGlobals(globals); |
2023 | 0 | if (builtins == NULL) { |
2024 | 0 | Py_DECREF(defaults); |
2025 | 0 | return NULL; |
2026 | 0 | } |
2027 | 0 | if (locals == NULL) { |
2028 | 0 | locals = globals; |
2029 | 0 | } |
2030 | 0 | PyObject *kwnames = NULL; |
2031 | 0 | PyObject *const *allargs; |
2032 | 0 | PyObject **newargs = NULL; |
2033 | 0 | PyFunctionObject *func = NULL; |
2034 | 0 | if (kwcount == 0) { |
2035 | 0 | allargs = args; |
2036 | 0 | } |
2037 | 0 | else { |
2038 | 0 | kwnames = PyTuple_New(kwcount); |
2039 | 0 | if (kwnames == NULL) { |
2040 | 0 | goto fail; |
2041 | 0 | } |
2042 | 0 | newargs = PyMem_Malloc(sizeof(PyObject *)*(kwcount+argcount)); |
2043 | 0 | if (newargs == NULL) { |
2044 | 0 | goto fail; |
2045 | 0 | } |
2046 | 0 | for (int i = 0; i < argcount; i++) { |
2047 | 0 | newargs[i] = args[i]; |
2048 | 0 | } |
2049 | 0 | for (int i = 0; i < kwcount; i++) { |
2050 | 0 | PyTuple_SET_ITEM(kwnames, i, Py_NewRef(kws[2*i])); |
2051 | 0 | newargs[argcount+i] = kws[2*i+1]; |
2052 | 0 | } |
2053 | 0 | allargs = newargs; |
2054 | 0 | } |
2055 | 0 | PyFrameConstructor constr = { |
2056 | 0 | .fc_globals = globals, |
2057 | 0 | .fc_builtins = builtins, |
2058 | 0 | .fc_name = ((PyCodeObject *)_co)->co_name, |
2059 | 0 | .fc_qualname = ((PyCodeObject *)_co)->co_name, |
2060 | 0 | .fc_code = _co, |
2061 | 0 | .fc_defaults = defaults, |
2062 | 0 | .fc_kwdefaults = kwdefs, |
2063 | 0 | .fc_closure = closure |
2064 | 0 | }; |
2065 | 0 | func = _PyFunction_FromConstructor(&constr); |
2066 | 0 | if (func == NULL) { |
2067 | 0 | goto fail; |
2068 | 0 | } |
2069 | 0 | EVAL_CALL_STAT_INC(EVAL_CALL_LEGACY); |
2070 | 0 | res = _PyEval_Vector(tstate, func, locals, |
2071 | 0 | allargs, argcount, |
2072 | 0 | kwnames); |
2073 | 0 | fail: |
2074 | 0 | Py_XDECREF(func); |
2075 | 0 | Py_XDECREF(kwnames); |
2076 | 0 | PyMem_Free(newargs); |
2077 | 0 | _Py_DECREF_BUILTINS(builtins); |
2078 | 0 | Py_DECREF(defaults); |
2079 | 0 | return res; |
2080 | 0 | } |
2081 | | |
2082 | | |
2083 | | /* Logic for the raise statement (too complicated for inlining). |
2084 | | This *consumes* a reference count to each of its arguments. */ |
2085 | | static int |
2086 | | do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause) |
2087 | 12.3M | { |
2088 | 12.3M | PyObject *type = NULL, *value = NULL; |
2089 | | |
2090 | 12.3M | if (exc == NULL) { |
2091 | | /* Reraise */ |
2092 | 34 | _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate); |
2093 | 34 | exc = exc_info->exc_value; |
2094 | 34 | if (Py_IsNone(exc) || exc == NULL) { |
2095 | 0 | _PyErr_SetString(tstate, PyExc_RuntimeError, |
2096 | 0 | "No active exception to reraise"); |
2097 | 0 | return 0; |
2098 | 0 | } |
2099 | 34 | Py_INCREF(exc); |
2100 | 34 | assert(PyExceptionInstance_Check(exc)); |
2101 | 34 | _PyErr_SetRaisedException(tstate, exc); |
2102 | 34 | return 1; |
2103 | 34 | } |
2104 | | |
2105 | | /* We support the following forms of raise: |
2106 | | raise |
2107 | | raise <instance> |
2108 | | raise <type> */ |
2109 | | |
2110 | 12.3M | if (PyExceptionClass_Check(exc)) { |
2111 | 11.9M | type = exc; |
2112 | 11.9M | value = _PyObject_CallNoArgs(exc); |
2113 | 11.9M | if (value == NULL) |
2114 | 0 | goto raise_error; |
2115 | 11.9M | if (!PyExceptionInstance_Check(value)) { |
2116 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
2117 | 0 | "calling %R should have returned an instance of " |
2118 | 0 | "BaseException, not %R", |
2119 | 0 | type, Py_TYPE(value)); |
2120 | 0 | goto raise_error; |
2121 | 0 | } |
2122 | 11.9M | } |
2123 | 482k | else if (PyExceptionInstance_Check(exc)) { |
2124 | 482k | value = exc; |
2125 | 482k | type = PyExceptionInstance_Class(exc); |
2126 | 482k | Py_INCREF(type); |
2127 | 482k | } |
2128 | 0 | else { |
2129 | | /* Not something you can raise. You get an exception |
2130 | | anyway, just not what you specified :-) */ |
2131 | 0 | Py_DECREF(exc); |
2132 | 0 | _PyErr_SetString(tstate, PyExc_TypeError, |
2133 | 0 | "exceptions must derive from BaseException"); |
2134 | 0 | goto raise_error; |
2135 | 0 | } |
2136 | | |
2137 | 12.3M | assert(type != NULL); |
2138 | 12.3M | assert(value != NULL); |
2139 | | |
2140 | 12.3M | if (cause) { |
2141 | 2.47k | PyObject *fixed_cause; |
2142 | 2.47k | if (PyExceptionClass_Check(cause)) { |
2143 | 0 | fixed_cause = _PyObject_CallNoArgs(cause); |
2144 | 0 | if (fixed_cause == NULL) |
2145 | 0 | goto raise_error; |
2146 | 0 | if (!PyExceptionInstance_Check(fixed_cause)) { |
2147 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
2148 | 0 | "calling %R should have returned an instance of " |
2149 | 0 | "BaseException, not %R", |
2150 | 0 | cause, Py_TYPE(fixed_cause)); |
2151 | 0 | goto raise_error; |
2152 | 0 | } |
2153 | 0 | Py_DECREF(cause); |
2154 | 0 | } |
2155 | 2.47k | else if (PyExceptionInstance_Check(cause)) { |
2156 | 0 | fixed_cause = cause; |
2157 | 0 | } |
2158 | 2.47k | else if (Py_IsNone(cause)) { |
2159 | 2.47k | Py_DECREF(cause); |
2160 | 2.47k | fixed_cause = NULL; |
2161 | 2.47k | } |
2162 | 0 | else { |
2163 | 0 | _PyErr_SetString(tstate, PyExc_TypeError, |
2164 | 0 | "exception causes must derive from " |
2165 | 0 | "BaseException"); |
2166 | 0 | goto raise_error; |
2167 | 0 | } |
2168 | 2.47k | PyException_SetCause(value, fixed_cause); |
2169 | 2.47k | } |
2170 | | |
2171 | 12.3M | _PyErr_SetObject(tstate, type, value); |
2172 | | /* _PyErr_SetObject incref's its arguments */ |
2173 | 12.3M | Py_DECREF(value); |
2174 | 12.3M | Py_DECREF(type); |
2175 | 12.3M | return 0; |
2176 | | |
2177 | 0 | raise_error: |
2178 | 0 | Py_XDECREF(value); |
2179 | 0 | Py_XDECREF(type); |
2180 | 0 | Py_XDECREF(cause); |
2181 | 0 | return 0; |
2182 | 12.3M | } |
2183 | | |
2184 | | /* Logic for matching an exception in an except* clause (too |
2185 | | complicated for inlining). |
2186 | | */ |
2187 | | |
2188 | | int |
2189 | | _PyEval_ExceptionGroupMatch(_PyInterpreterFrame *frame, PyObject* exc_value, |
2190 | | PyObject *match_type, PyObject **match, PyObject **rest) |
2191 | 0 | { |
2192 | 0 | if (Py_IsNone(exc_value)) { |
2193 | 0 | *match = Py_NewRef(Py_None); |
2194 | 0 | *rest = Py_NewRef(Py_None); |
2195 | 0 | return 0; |
2196 | 0 | } |
2197 | 0 | assert(PyExceptionInstance_Check(exc_value)); |
2198 | |
|
2199 | 0 | if (PyErr_GivenExceptionMatches(exc_value, match_type)) { |
2200 | | /* Full match of exc itself */ |
2201 | 0 | bool is_eg = _PyBaseExceptionGroup_Check(exc_value); |
2202 | 0 | if (is_eg) { |
2203 | 0 | *match = Py_NewRef(exc_value); |
2204 | 0 | } |
2205 | 0 | else { |
2206 | | /* naked exception - wrap it */ |
2207 | 0 | PyObject *excs = PyTuple_Pack(1, exc_value); |
2208 | 0 | if (excs == NULL) { |
2209 | 0 | return -1; |
2210 | 0 | } |
2211 | 0 | PyObject *wrapped = _PyExc_CreateExceptionGroup("", excs); |
2212 | 0 | Py_DECREF(excs); |
2213 | 0 | if (wrapped == NULL) { |
2214 | 0 | return -1; |
2215 | 0 | } |
2216 | 0 | PyFrameObject *f = _PyFrame_GetFrameObject(frame); |
2217 | 0 | if (f != NULL) { |
2218 | 0 | PyObject *tb = _PyTraceBack_FromFrame(NULL, f); |
2219 | 0 | if (tb == NULL) { |
2220 | 0 | return -1; |
2221 | 0 | } |
2222 | 0 | PyException_SetTraceback(wrapped, tb); |
2223 | 0 | Py_DECREF(tb); |
2224 | 0 | } |
2225 | 0 | *match = wrapped; |
2226 | 0 | } |
2227 | 0 | *rest = Py_NewRef(Py_None); |
2228 | 0 | return 0; |
2229 | 0 | } |
2230 | | |
2231 | | /* exc_value does not match match_type. |
2232 | | * Check for partial match if it's an exception group. |
2233 | | */ |
2234 | 0 | if (_PyBaseExceptionGroup_Check(exc_value)) { |
2235 | 0 | PyObject *pair = PyObject_CallMethod(exc_value, "split", "(O)", |
2236 | 0 | match_type); |
2237 | 0 | if (pair == NULL) { |
2238 | 0 | return -1; |
2239 | 0 | } |
2240 | | |
2241 | 0 | if (!PyTuple_CheckExact(pair)) { |
2242 | 0 | PyErr_Format(PyExc_TypeError, |
2243 | 0 | "%.200s.split must return a tuple, not %.200s", |
2244 | 0 | Py_TYPE(exc_value)->tp_name, Py_TYPE(pair)->tp_name); |
2245 | 0 | Py_DECREF(pair); |
2246 | 0 | return -1; |
2247 | 0 | } |
2248 | | |
2249 | | // allow tuples of length > 2 for backwards compatibility |
2250 | 0 | if (PyTuple_GET_SIZE(pair) < 2) { |
2251 | 0 | PyErr_Format(PyExc_TypeError, |
2252 | 0 | "%.200s.split must return a 2-tuple, " |
2253 | 0 | "got tuple of size %zd", |
2254 | 0 | Py_TYPE(exc_value)->tp_name, PyTuple_GET_SIZE(pair)); |
2255 | 0 | Py_DECREF(pair); |
2256 | 0 | return -1; |
2257 | 0 | } |
2258 | | |
2259 | 0 | *match = Py_NewRef(PyTuple_GET_ITEM(pair, 0)); |
2260 | 0 | *rest = Py_NewRef(PyTuple_GET_ITEM(pair, 1)); |
2261 | 0 | Py_DECREF(pair); |
2262 | 0 | return 0; |
2263 | 0 | } |
2264 | | /* no match */ |
2265 | 0 | *match = Py_NewRef(Py_None); |
2266 | 0 | *rest = Py_NewRef(exc_value); |
2267 | 0 | return 0; |
2268 | 0 | } |
2269 | | |
2270 | | /* Iterate v argcnt times and store the results on the stack (via decreasing |
2271 | | sp). Return 1 for success, 0 if error. |
2272 | | |
2273 | | If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack |
2274 | | with a variable target. |
2275 | | */ |
2276 | | |
2277 | | int |
2278 | | _PyEval_UnpackIterableStackRef(PyThreadState *tstate, PyObject *v, |
2279 | | int argcnt, int argcntafter, _PyStackRef *sp) |
2280 | 12.6M | { |
2281 | 12.6M | int i = 0, j = 0; |
2282 | 12.6M | Py_ssize_t ll = 0; |
2283 | 12.6M | PyObject *it; /* iter(v) */ |
2284 | 12.6M | PyObject *w; |
2285 | 12.6M | PyObject *l = NULL; /* variable list */ |
2286 | 12.6M | assert(v != NULL); |
2287 | | |
2288 | 12.6M | it = PyObject_GetIter(v); |
2289 | 12.6M | if (it == NULL) { |
2290 | 0 | if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) && |
2291 | 0 | Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v)) |
2292 | 0 | { |
2293 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
2294 | 0 | "cannot unpack non-iterable %.200s object", |
2295 | 0 | Py_TYPE(v)->tp_name); |
2296 | 0 | } |
2297 | 0 | return 0; |
2298 | 0 | } |
2299 | | |
2300 | 31.5M | for (; i < argcnt; i++) { |
2301 | 26.8M | w = PyIter_Next(it); |
2302 | 26.8M | if (w == NULL) { |
2303 | | /* Iterator done, via error or exhaustion. */ |
2304 | 7.93M | if (!_PyErr_Occurred(tstate)) { |
2305 | 7.93M | if (argcntafter == -1) { |
2306 | 7.93M | _PyErr_Format(tstate, PyExc_ValueError, |
2307 | 7.93M | "not enough values to unpack " |
2308 | 7.93M | "(expected %d, got %d)", |
2309 | 7.93M | argcnt, i); |
2310 | 7.93M | } |
2311 | 0 | else { |
2312 | 0 | _PyErr_Format(tstate, PyExc_ValueError, |
2313 | 0 | "not enough values to unpack " |
2314 | 0 | "(expected at least %d, got %d)", |
2315 | 0 | argcnt + argcntafter, i); |
2316 | 0 | } |
2317 | 7.93M | } |
2318 | 7.93M | goto Error; |
2319 | 7.93M | } |
2320 | 18.8M | *--sp = PyStackRef_FromPyObjectSteal(w); |
2321 | 18.8M | } |
2322 | | |
2323 | 4.73M | if (argcntafter == -1) { |
2324 | | /* We better have exhausted the iterator now. */ |
2325 | 3.19M | w = PyIter_Next(it); |
2326 | 3.19M | if (w == NULL) { |
2327 | 3.16M | if (_PyErr_Occurred(tstate)) |
2328 | 0 | goto Error; |
2329 | 3.16M | Py_DECREF(it); |
2330 | 3.16M | return 1; |
2331 | 3.16M | } |
2332 | 35.6k | Py_DECREF(w); |
2333 | | |
2334 | 35.6k | if (PyList_CheckExact(v) || PyTuple_CheckExact(v) |
2335 | 35.6k | || PyDict_CheckExact(v)) { |
2336 | 35.6k | ll = PyDict_CheckExact(v) ? PyDict_Size(v) : Py_SIZE(v); |
2337 | 35.6k | if (ll > argcnt) { |
2338 | 35.6k | _PyErr_Format(tstate, PyExc_ValueError, |
2339 | 35.6k | "too many values to unpack (expected %d, got %zd)", |
2340 | 35.6k | argcnt, ll); |
2341 | 35.6k | goto Error; |
2342 | 35.6k | } |
2343 | 35.6k | } |
2344 | 0 | _PyErr_Format(tstate, PyExc_ValueError, |
2345 | 0 | "too many values to unpack (expected %d)", |
2346 | 0 | argcnt); |
2347 | 0 | goto Error; |
2348 | 35.6k | } |
2349 | | |
2350 | 1.53M | l = PySequence_List(it); |
2351 | 1.53M | if (l == NULL) |
2352 | 0 | goto Error; |
2353 | 1.53M | *--sp = PyStackRef_FromPyObjectSteal(l); |
2354 | 1.53M | i++; |
2355 | | |
2356 | 1.53M | ll = PyList_GET_SIZE(l); |
2357 | 1.53M | if (ll < argcntafter) { |
2358 | 0 | _PyErr_Format(tstate, PyExc_ValueError, |
2359 | 0 | "not enough values to unpack (expected at least %d, got %zd)", |
2360 | 0 | argcnt + argcntafter, argcnt + ll); |
2361 | 0 | goto Error; |
2362 | 0 | } |
2363 | | |
2364 | | /* Pop the "after-variable" args off the list. */ |
2365 | 1.53M | for (j = argcntafter; j > 0; j--, i++) { |
2366 | 0 | *--sp = PyStackRef_FromPyObjectSteal(PyList_GET_ITEM(l, ll - j)); |
2367 | 0 | } |
2368 | | /* Resize the list. */ |
2369 | 1.53M | Py_SET_SIZE(l, ll - argcntafter); |
2370 | 1.53M | Py_DECREF(it); |
2371 | 1.53M | return 1; |
2372 | | |
2373 | 7.97M | Error: |
2374 | 16.1M | for (; i > 0; i--, sp++) { |
2375 | 8.20M | PyStackRef_CLOSE(*sp); |
2376 | 8.20M | } |
2377 | 7.97M | Py_XDECREF(it); |
2378 | 7.97M | return 0; |
2379 | 1.53M | } |
2380 | | |
2381 | | static int |
2382 | | do_monitor_exc(PyThreadState *tstate, _PyInterpreterFrame *frame, |
2383 | | _Py_CODEUNIT *instr, int event) |
2384 | 0 | { |
2385 | 0 | assert(event < _PY_MONITORING_UNGROUPED_EVENTS); |
2386 | 0 | if (_PyFrame_GetCode(frame)->co_flags & CO_NO_MONITORING_EVENTS) { |
2387 | 0 | return 0; |
2388 | 0 | } |
2389 | 0 | PyObject *exc = PyErr_GetRaisedException(); |
2390 | 0 | assert(exc != NULL); |
2391 | 0 | int err = _Py_call_instrumentation_arg(tstate, event, frame, instr, exc); |
2392 | 0 | if (err == 0) { |
2393 | 0 | PyErr_SetRaisedException(exc); |
2394 | 0 | } |
2395 | 0 | else { |
2396 | 0 | assert(PyErr_Occurred()); |
2397 | 0 | Py_DECREF(exc); |
2398 | 0 | } |
2399 | 0 | return err; |
2400 | 0 | } |
2401 | | |
2402 | | static inline bool |
2403 | | no_tools_for_global_event(PyThreadState *tstate, int event) |
2404 | 75.2M | { |
2405 | 75.2M | return tstate->interp->monitors.tools[event] == 0; |
2406 | 75.2M | } |
2407 | | |
2408 | | static inline bool |
2409 | | no_tools_for_local_event(PyThreadState *tstate, _PyInterpreterFrame *frame, int event) |
2410 | 0 | { |
2411 | 0 | assert(event < _PY_MONITORING_LOCAL_EVENTS); |
2412 | 0 | _PyCoMonitoringData *data = _PyFrame_GetCode(frame)->_co_monitoring; |
2413 | 0 | if (data) { |
2414 | 0 | return data->active_monitors.tools[event] == 0; |
2415 | 0 | } |
2416 | 0 | else { |
2417 | 0 | return no_tools_for_global_event(tstate, event); |
2418 | 0 | } |
2419 | 0 | } |
2420 | | |
2421 | | void |
2422 | | _PyEval_MonitorRaise(PyThreadState *tstate, _PyInterpreterFrame *frame, |
2423 | | _Py_CODEUNIT *instr) |
2424 | 43.3M | { |
2425 | 43.3M | if (no_tools_for_global_event(tstate, PY_MONITORING_EVENT_RAISE)) { |
2426 | 43.3M | return; |
2427 | 43.3M | } |
2428 | 0 | do_monitor_exc(tstate, frame, instr, PY_MONITORING_EVENT_RAISE); |
2429 | 0 | } |
2430 | | |
2431 | | static void |
2432 | | monitor_reraise(PyThreadState *tstate, _PyInterpreterFrame *frame, |
2433 | | _Py_CODEUNIT *instr) |
2434 | 224k | { |
2435 | 224k | if (no_tools_for_global_event(tstate, PY_MONITORING_EVENT_RERAISE)) { |
2436 | 224k | return; |
2437 | 224k | } |
2438 | 0 | do_monitor_exc(tstate, frame, instr, PY_MONITORING_EVENT_RERAISE); |
2439 | 0 | } |
2440 | | |
2441 | | static int |
2442 | | monitor_stop_iteration(PyThreadState *tstate, _PyInterpreterFrame *frame, |
2443 | | _Py_CODEUNIT *instr, PyObject *value) |
2444 | 0 | { |
2445 | 0 | if (no_tools_for_local_event(tstate, frame, PY_MONITORING_EVENT_STOP_ITERATION)) { |
2446 | 0 | return 0; |
2447 | 0 | } |
2448 | 0 | assert(!PyErr_Occurred()); |
2449 | 0 | PyErr_SetObject(PyExc_StopIteration, value); |
2450 | 0 | int res = do_monitor_exc(tstate, frame, instr, PY_MONITORING_EVENT_STOP_ITERATION); |
2451 | 0 | if (res < 0) { |
2452 | 0 | return res; |
2453 | 0 | } |
2454 | 0 | PyErr_SetRaisedException(NULL); |
2455 | 0 | return 0; |
2456 | 0 | } |
2457 | | |
2458 | | static void |
2459 | | monitor_unwind(PyThreadState *tstate, |
2460 | | _PyInterpreterFrame *frame, |
2461 | | _Py_CODEUNIT *instr) |
2462 | 12.9M | { |
2463 | 12.9M | if (no_tools_for_global_event(tstate, PY_MONITORING_EVENT_PY_UNWIND)) { |
2464 | 12.9M | return; |
2465 | 12.9M | } |
2466 | 0 | do_monitor_exc(tstate, frame, instr, PY_MONITORING_EVENT_PY_UNWIND); |
2467 | 0 | } |
2468 | | |
2469 | | bool |
2470 | 27.7k | _PyEval_NoToolsForUnwind(PyThreadState *tstate) { |
2471 | 27.7k | return no_tools_for_global_event(tstate, PY_MONITORING_EVENT_PY_UNWIND); |
2472 | 27.7k | } |
2473 | | |
2474 | | static int |
2475 | | monitor_handled(PyThreadState *tstate, |
2476 | | _PyInterpreterFrame *frame, |
2477 | | _Py_CODEUNIT *instr, PyObject *exc) |
2478 | 18.7M | { |
2479 | 18.7M | if (no_tools_for_global_event(tstate, PY_MONITORING_EVENT_EXCEPTION_HANDLED)) { |
2480 | 18.7M | return 0; |
2481 | 18.7M | } |
2482 | 0 | return _Py_call_instrumentation_arg(tstate, PY_MONITORING_EVENT_EXCEPTION_HANDLED, frame, instr, exc); |
2483 | 18.7M | } |
2484 | | |
2485 | | static void |
2486 | | monitor_throw(PyThreadState *tstate, |
2487 | | _PyInterpreterFrame *frame, |
2488 | | _Py_CODEUNIT *instr) |
2489 | 982 | { |
2490 | 982 | if (no_tools_for_global_event(tstate, PY_MONITORING_EVENT_PY_THROW)) { |
2491 | 982 | return; |
2492 | 982 | } |
2493 | 0 | do_monitor_exc(tstate, frame, instr, PY_MONITORING_EVENT_PY_THROW); |
2494 | 0 | } |
2495 | | |
2496 | | void |
2497 | | PyThreadState_EnterTracing(PyThreadState *tstate) |
2498 | 0 | { |
2499 | 0 | assert(tstate->tracing >= 0); |
2500 | 0 | tstate->tracing++; |
2501 | 0 | } |
2502 | | |
2503 | | void |
2504 | | PyThreadState_LeaveTracing(PyThreadState *tstate) |
2505 | 0 | { |
2506 | 0 | assert(tstate->tracing > 0); |
2507 | 0 | tstate->tracing--; |
2508 | 0 | } |
2509 | | |
2510 | | |
2511 | | PyObject* |
2512 | | _PyEval_CallTracing(PyObject *func, PyObject *args) |
2513 | 0 | { |
2514 | | // Save and disable tracing |
2515 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
2516 | 0 | int save_tracing = tstate->tracing; |
2517 | 0 | tstate->tracing = 0; |
2518 | | |
2519 | | // Call the tracing function |
2520 | 0 | PyObject *result = PyObject_Call(func, args, NULL); |
2521 | | |
2522 | | // Restore tracing |
2523 | 0 | tstate->tracing = save_tracing; |
2524 | 0 | return result; |
2525 | 0 | } |
2526 | | |
2527 | | void |
2528 | | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
2529 | 0 | { |
2530 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
2531 | 0 | if (_PyEval_SetProfile(tstate, func, arg) < 0) { |
2532 | | /* Log _PySys_Audit() error */ |
2533 | 0 | PyErr_FormatUnraisable("Exception ignored in PyEval_SetProfile"); |
2534 | 0 | } |
2535 | 0 | } |
2536 | | |
2537 | | void |
2538 | | PyEval_SetProfileAllThreads(Py_tracefunc func, PyObject *arg) |
2539 | 0 | { |
2540 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
2541 | 0 | if (_PyEval_SetProfileAllThreads(interp, func, arg) < 0) { |
2542 | | /* Log _PySys_Audit() error */ |
2543 | 0 | PyErr_FormatUnraisable("Exception ignored in PyEval_SetProfileAllThreads"); |
2544 | 0 | } |
2545 | 0 | } |
2546 | | |
2547 | | void |
2548 | | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
2549 | 0 | { |
2550 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
2551 | 0 | if (_PyEval_SetTrace(tstate, func, arg) < 0) { |
2552 | | /* Log _PySys_Audit() error */ |
2553 | 0 | PyErr_FormatUnraisable("Exception ignored in PyEval_SetTrace"); |
2554 | 0 | } |
2555 | 0 | } |
2556 | | |
2557 | | void |
2558 | | PyEval_SetTraceAllThreads(Py_tracefunc func, PyObject *arg) |
2559 | 0 | { |
2560 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
2561 | 0 | if (_PyEval_SetTraceAllThreads(interp, func, arg) < 0) { |
2562 | | /* Log _PySys_Audit() error */ |
2563 | 0 | PyErr_FormatUnraisable("Exception ignored in PyEval_SetTraceAllThreads"); |
2564 | 0 | } |
2565 | 0 | } |
2566 | | |
2567 | | int |
2568 | | _PyEval_SetCoroutineOriginTrackingDepth(int depth) |
2569 | 0 | { |
2570 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
2571 | 0 | if (depth < 0) { |
2572 | 0 | _PyErr_SetString(tstate, PyExc_ValueError, "depth must be >= 0"); |
2573 | 0 | return -1; |
2574 | 0 | } |
2575 | 0 | tstate->coroutine_origin_tracking_depth = depth; |
2576 | 0 | return 0; |
2577 | 0 | } |
2578 | | |
2579 | | |
2580 | | int |
2581 | | _PyEval_GetCoroutineOriginTrackingDepth(void) |
2582 | 0 | { |
2583 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
2584 | 0 | return tstate->coroutine_origin_tracking_depth; |
2585 | 0 | } |
2586 | | |
2587 | | int |
2588 | | _PyEval_SetAsyncGenFirstiter(PyObject *firstiter) |
2589 | 0 | { |
2590 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
2591 | |
|
2592 | 0 | if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) { |
2593 | 0 | return -1; |
2594 | 0 | } |
2595 | | |
2596 | 0 | Py_XSETREF(tstate->async_gen_firstiter, Py_XNewRef(firstiter)); |
2597 | 0 | return 0; |
2598 | 0 | } |
2599 | | |
2600 | | PyObject * |
2601 | | _PyEval_GetAsyncGenFirstiter(void) |
2602 | 0 | { |
2603 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
2604 | 0 | return tstate->async_gen_firstiter; |
2605 | 0 | } |
2606 | | |
2607 | | int |
2608 | | _PyEval_SetAsyncGenFinalizer(PyObject *finalizer) |
2609 | 0 | { |
2610 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
2611 | |
|
2612 | 0 | if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) { |
2613 | 0 | return -1; |
2614 | 0 | } |
2615 | | |
2616 | 0 | Py_XSETREF(tstate->async_gen_finalizer, Py_XNewRef(finalizer)); |
2617 | 0 | return 0; |
2618 | 0 | } |
2619 | | |
2620 | | PyObject * |
2621 | | _PyEval_GetAsyncGenFinalizer(void) |
2622 | 0 | { |
2623 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
2624 | 0 | return tstate->async_gen_finalizer; |
2625 | 0 | } |
2626 | | |
2627 | | _PyInterpreterFrame * |
2628 | | _PyEval_GetFrame(void) |
2629 | 374 | { |
2630 | 374 | PyThreadState *tstate = _PyThreadState_GET(); |
2631 | 374 | return _PyThreadState_GetFrame(tstate); |
2632 | 374 | } |
2633 | | |
2634 | | PyFrameObject * |
2635 | | PyEval_GetFrame(void) |
2636 | 0 | { |
2637 | 0 | _PyInterpreterFrame *frame = _PyEval_GetFrame(); |
2638 | 0 | if (frame == NULL) { |
2639 | 0 | return NULL; |
2640 | 0 | } |
2641 | 0 | PyFrameObject *f = _PyFrame_GetFrameObject(frame); |
2642 | 0 | if (f == NULL) { |
2643 | 0 | PyErr_Clear(); |
2644 | 0 | } |
2645 | 0 | return f; |
2646 | 0 | } |
2647 | | |
2648 | | PyObject * |
2649 | | _PyEval_GetBuiltins(PyThreadState *tstate) |
2650 | 1.00k | { |
2651 | 1.00k | _PyInterpreterFrame *frame = _PyThreadState_GetFrame(tstate); |
2652 | 1.00k | if (frame != NULL) { |
2653 | 976 | return frame->f_builtins; |
2654 | 976 | } |
2655 | 32 | return tstate->interp->builtins; |
2656 | 1.00k | } |
2657 | | |
2658 | | PyObject * |
2659 | | PyEval_GetBuiltins(void) |
2660 | 1.00k | { |
2661 | 1.00k | PyThreadState *tstate = _PyThreadState_GET(); |
2662 | 1.00k | return _PyEval_GetBuiltins(tstate); |
2663 | 1.00k | } |
2664 | | |
2665 | | /* Convenience function to get a builtin from its name */ |
2666 | | PyObject * |
2667 | | _PyEval_GetBuiltin(PyObject *name) |
2668 | 0 | { |
2669 | 0 | PyObject *attr; |
2670 | 0 | if (PyMapping_GetOptionalItem(PyEval_GetBuiltins(), name, &attr) == 0) { |
2671 | 0 | PyErr_SetObject(PyExc_AttributeError, name); |
2672 | 0 | } |
2673 | 0 | return attr; |
2674 | 0 | } |
2675 | | |
2676 | | PyObject * |
2677 | | _PyEval_GetBuiltinId(_Py_Identifier *name) |
2678 | 0 | { |
2679 | 0 | return _PyEval_GetBuiltin(_PyUnicode_FromId(name)); |
2680 | 0 | } |
2681 | | |
2682 | | PyObject * |
2683 | | PyEval_GetLocals(void) |
2684 | 0 | { |
2685 | | // We need to return a borrowed reference here, so some tricks are needed |
2686 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
2687 | 0 | _PyInterpreterFrame *current_frame = _PyThreadState_GetFrame(tstate); |
2688 | 0 | if (current_frame == NULL) { |
2689 | 0 | _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist"); |
2690 | 0 | return NULL; |
2691 | 0 | } |
2692 | | |
2693 | | // Be aware that this returns a new reference |
2694 | 0 | PyObject *locals = _PyFrame_GetLocals(current_frame); |
2695 | |
|
2696 | 0 | if (locals == NULL) { |
2697 | 0 | return NULL; |
2698 | 0 | } |
2699 | | |
2700 | 0 | if (PyFrameLocalsProxy_Check(locals)) { |
2701 | 0 | PyFrameObject *f = _PyFrame_GetFrameObject(current_frame); |
2702 | 0 | PyObject *ret = f->f_locals_cache; |
2703 | 0 | if (ret == NULL) { |
2704 | 0 | ret = PyDict_New(); |
2705 | 0 | if (ret == NULL) { |
2706 | 0 | Py_DECREF(locals); |
2707 | 0 | return NULL; |
2708 | 0 | } |
2709 | 0 | f->f_locals_cache = ret; |
2710 | 0 | } |
2711 | 0 | if (PyDict_Update(ret, locals) < 0) { |
2712 | | // At this point, if the cache dict is broken, it will stay broken, as |
2713 | | // trying to clean it up or replace it will just cause other problems |
2714 | 0 | ret = NULL; |
2715 | 0 | } |
2716 | 0 | Py_DECREF(locals); |
2717 | 0 | return ret; |
2718 | 0 | } |
2719 | | |
2720 | 0 | assert(PyMapping_Check(locals)); |
2721 | 0 | assert(Py_REFCNT(locals) > 1); |
2722 | 0 | Py_DECREF(locals); |
2723 | |
|
2724 | 0 | return locals; |
2725 | 0 | } |
2726 | | |
2727 | | PyObject * |
2728 | | _PyEval_GetFrameLocals(void) |
2729 | 0 | { |
2730 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
2731 | 0 | _PyInterpreterFrame *current_frame = _PyThreadState_GetFrame(tstate); |
2732 | 0 | if (current_frame == NULL) { |
2733 | 0 | _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist"); |
2734 | 0 | return NULL; |
2735 | 0 | } |
2736 | | |
2737 | 0 | PyObject *locals = _PyFrame_GetLocals(current_frame); |
2738 | 0 | if (locals == NULL) { |
2739 | 0 | return NULL; |
2740 | 0 | } |
2741 | | |
2742 | 0 | if (PyFrameLocalsProxy_Check(locals)) { |
2743 | 0 | PyObject* ret = PyDict_New(); |
2744 | 0 | if (ret == NULL) { |
2745 | 0 | Py_DECREF(locals); |
2746 | 0 | return NULL; |
2747 | 0 | } |
2748 | 0 | if (PyDict_Update(ret, locals) < 0) { |
2749 | 0 | Py_DECREF(ret); |
2750 | 0 | Py_DECREF(locals); |
2751 | 0 | return NULL; |
2752 | 0 | } |
2753 | 0 | Py_DECREF(locals); |
2754 | 0 | return ret; |
2755 | 0 | } |
2756 | | |
2757 | 0 | assert(PyMapping_Check(locals)); |
2758 | 0 | return locals; |
2759 | 0 | } |
2760 | | |
2761 | | static PyObject * |
2762 | | _PyEval_GetGlobals(PyThreadState *tstate) |
2763 | 270k | { |
2764 | 270k | _PyInterpreterFrame *current_frame = _PyThreadState_GetFrame(tstate); |
2765 | 270k | if (current_frame == NULL) { |
2766 | 128 | return NULL; |
2767 | 128 | } |
2768 | 270k | return current_frame->f_globals; |
2769 | 270k | } |
2770 | | |
2771 | | PyObject * |
2772 | | PyEval_GetGlobals(void) |
2773 | 270k | { |
2774 | 270k | PyThreadState *tstate = _PyThreadState_GET(); |
2775 | 270k | return _PyEval_GetGlobals(tstate); |
2776 | 270k | } |
2777 | | |
2778 | | PyObject * |
2779 | | _PyEval_GetGlobalsFromRunningMain(PyThreadState *tstate) |
2780 | 0 | { |
2781 | 0 | if (!_PyInterpreterState_IsRunningMain(tstate->interp)) { |
2782 | 0 | return NULL; |
2783 | 0 | } |
2784 | 0 | PyObject *mod = _Py_GetMainModule(tstate); |
2785 | 0 | if (_Py_CheckMainModule(mod) < 0) { |
2786 | 0 | Py_XDECREF(mod); |
2787 | 0 | return NULL; |
2788 | 0 | } |
2789 | 0 | PyObject *globals = PyModule_GetDict(mod); // borrowed |
2790 | 0 | Py_DECREF(mod); |
2791 | 0 | return globals; |
2792 | 0 | } |
2793 | | |
2794 | | static PyObject * |
2795 | | get_globals_builtins(PyObject *globals) |
2796 | 1.18k | { |
2797 | 1.18k | PyObject *builtins = NULL; |
2798 | 1.18k | if (PyDict_Check(globals)) { |
2799 | 1.18k | if (PyDict_GetItemRef(globals, &_Py_ID(__builtins__), &builtins) < 0) { |
2800 | 0 | return NULL; |
2801 | 0 | } |
2802 | 1.18k | } |
2803 | 0 | else { |
2804 | 0 | if (PyMapping_GetOptionalItem( |
2805 | 0 | globals, &_Py_ID(__builtins__), &builtins) < 0) |
2806 | 0 | { |
2807 | 0 | return NULL; |
2808 | 0 | } |
2809 | 0 | } |
2810 | 1.18k | return builtins; |
2811 | 1.18k | } |
2812 | | |
2813 | | static int |
2814 | | set_globals_builtins(PyObject *globals, PyObject *builtins) |
2815 | 1.10k | { |
2816 | 1.10k | if (PyDict_Check(globals)) { |
2817 | 1.10k | if (PyDict_SetItem(globals, &_Py_ID(__builtins__), builtins) < 0) { |
2818 | 0 | return -1; |
2819 | 0 | } |
2820 | 1.10k | } |
2821 | 0 | else { |
2822 | 0 | if (PyObject_SetItem(globals, &_Py_ID(__builtins__), builtins) < 0) { |
2823 | 0 | return -1; |
2824 | 0 | } |
2825 | 0 | } |
2826 | 1.10k | return 0; |
2827 | 1.10k | } |
2828 | | |
2829 | | int |
2830 | | _PyEval_EnsureBuiltins(PyThreadState *tstate, PyObject *globals, |
2831 | | PyObject **p_builtins) |
2832 | 1.05k | { |
2833 | 1.05k | PyObject *builtins = get_globals_builtins(globals); |
2834 | 1.05k | if (builtins == NULL) { |
2835 | 976 | if (_PyErr_Occurred(tstate)) { |
2836 | 0 | return -1; |
2837 | 0 | } |
2838 | 976 | builtins = PyEval_GetBuiltins(); // borrowed |
2839 | 976 | if (builtins == NULL) { |
2840 | 0 | assert(_PyErr_Occurred(tstate)); |
2841 | 0 | return -1; |
2842 | 0 | } |
2843 | 976 | Py_INCREF(builtins); |
2844 | 976 | if (set_globals_builtins(globals, builtins) < 0) { |
2845 | 0 | Py_DECREF(builtins); |
2846 | 0 | return -1; |
2847 | 0 | } |
2848 | 976 | } |
2849 | 1.05k | if (p_builtins != NULL) { |
2850 | 0 | *p_builtins = builtins; |
2851 | 0 | } |
2852 | 1.05k | else { |
2853 | 1.05k | Py_DECREF(builtins); |
2854 | 1.05k | } |
2855 | 1.05k | return 0; |
2856 | 1.05k | } |
2857 | | |
2858 | | int |
2859 | | _PyEval_EnsureBuiltinsWithModule(PyThreadState *tstate, PyObject *globals, |
2860 | | PyObject **p_builtins) |
2861 | 128 | { |
2862 | 128 | PyObject *builtins = get_globals_builtins(globals); |
2863 | 128 | if (builtins == NULL) { |
2864 | 128 | if (_PyErr_Occurred(tstate)) { |
2865 | 0 | return -1; |
2866 | 0 | } |
2867 | 128 | builtins = PyImport_ImportModuleLevel("builtins", NULL, NULL, NULL, 0); |
2868 | 128 | if (builtins == NULL) { |
2869 | 0 | return -1; |
2870 | 0 | } |
2871 | 128 | if (set_globals_builtins(globals, builtins) < 0) { |
2872 | 0 | Py_DECREF(builtins); |
2873 | 0 | return -1; |
2874 | 0 | } |
2875 | 128 | } |
2876 | 128 | if (p_builtins != NULL) { |
2877 | 128 | *p_builtins = builtins; |
2878 | 128 | } |
2879 | 0 | else { |
2880 | 0 | Py_DECREF(builtins); |
2881 | 0 | } |
2882 | 128 | return 0; |
2883 | 128 | } |
2884 | | |
2885 | | PyObject* |
2886 | | PyEval_GetFrameLocals(void) |
2887 | 0 | { |
2888 | 0 | return _PyEval_GetFrameLocals(); |
2889 | 0 | } |
2890 | | |
2891 | | PyObject* PyEval_GetFrameGlobals(void) |
2892 | 0 | { |
2893 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
2894 | 0 | _PyInterpreterFrame *current_frame = _PyThreadState_GetFrame(tstate); |
2895 | 0 | if (current_frame == NULL) { |
2896 | 0 | return NULL; |
2897 | 0 | } |
2898 | 0 | return Py_XNewRef(current_frame->f_globals); |
2899 | 0 | } |
2900 | | |
2901 | | PyObject* PyEval_GetFrameBuiltins(void) |
2902 | 0 | { |
2903 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
2904 | 0 | return Py_XNewRef(_PyEval_GetBuiltins(tstate)); |
2905 | 0 | } |
2906 | | |
2907 | | int |
2908 | | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
2909 | 20.9k | { |
2910 | 20.9k | PyThreadState *tstate = _PyThreadState_GET(); |
2911 | 20.9k | _PyInterpreterFrame *current_frame = tstate->current_frame; |
2912 | 20.9k | int result = cf->cf_flags != 0; |
2913 | | |
2914 | 20.9k | if (current_frame != NULL) { |
2915 | 20.9k | const int codeflags = _PyFrame_GetCode(current_frame)->co_flags; |
2916 | 20.9k | const int compilerflags = codeflags & PyCF_MASK; |
2917 | 20.9k | if (compilerflags) { |
2918 | 0 | result = 1; |
2919 | 0 | cf->cf_flags |= compilerflags; |
2920 | 0 | } |
2921 | 20.9k | } |
2922 | 20.9k | return result; |
2923 | 20.9k | } |
2924 | | |
2925 | | |
2926 | | const char * |
2927 | | PyEval_GetFuncName(PyObject *func) |
2928 | 0 | { |
2929 | 0 | if (PyMethod_Check(func)) |
2930 | 0 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
2931 | 0 | else if (PyFunction_Check(func)) |
2932 | 0 | return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name); |
2933 | 0 | else if (PyCFunction_Check(func)) |
2934 | 0 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
2935 | 0 | else |
2936 | 0 | return Py_TYPE(func)->tp_name; |
2937 | 0 | } |
2938 | | |
2939 | | const char * |
2940 | | PyEval_GetFuncDesc(PyObject *func) |
2941 | 0 | { |
2942 | 0 | if (PyMethod_Check(func)) |
2943 | 0 | return "()"; |
2944 | 0 | else if (PyFunction_Check(func)) |
2945 | 0 | return "()"; |
2946 | 0 | else if (PyCFunction_Check(func)) |
2947 | 0 | return "()"; |
2948 | 0 | else |
2949 | 0 | return " object"; |
2950 | 0 | } |
2951 | | |
2952 | | /* Extract a slice index from a PyLong or an object with the |
2953 | | nb_index slot defined, and store in *pi. |
2954 | | Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX, |
2955 | | and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN. |
2956 | | Return 0 on error, 1 on success. |
2957 | | */ |
2958 | | int |
2959 | | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
2960 | 179M | { |
2961 | 179M | PyThreadState *tstate = _PyThreadState_GET(); |
2962 | 179M | if (!Py_IsNone(v)) { |
2963 | 179M | Py_ssize_t x; |
2964 | 179M | if (_PyIndex_Check(v)) { |
2965 | 179M | x = PyNumber_AsSsize_t(v, NULL); |
2966 | 179M | if (x == -1 && _PyErr_Occurred(tstate)) |
2967 | 0 | return 0; |
2968 | 179M | } |
2969 | 0 | else { |
2970 | 0 | _PyErr_SetString(tstate, PyExc_TypeError, |
2971 | 0 | "slice indices must be integers or " |
2972 | 0 | "None or have an __index__ method"); |
2973 | 0 | return 0; |
2974 | 0 | } |
2975 | 179M | *pi = x; |
2976 | 179M | } |
2977 | 179M | return 1; |
2978 | 179M | } |
2979 | | |
2980 | | int |
2981 | | _PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi) |
2982 | 0 | { |
2983 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
2984 | 0 | Py_ssize_t x; |
2985 | 0 | if (_PyIndex_Check(v)) { |
2986 | 0 | x = PyNumber_AsSsize_t(v, NULL); |
2987 | 0 | if (x == -1 && _PyErr_Occurred(tstate)) |
2988 | 0 | return 0; |
2989 | 0 | } |
2990 | 0 | else { |
2991 | 0 | _PyErr_SetString(tstate, PyExc_TypeError, |
2992 | 0 | "slice indices must be integers or " |
2993 | 0 | "have an __index__ method"); |
2994 | 0 | return 0; |
2995 | 0 | } |
2996 | 0 | *pi = x; |
2997 | 0 | return 1; |
2998 | 0 | } |
2999 | | |
3000 | | PyObject * |
3001 | | _PyEval_ImportName(PyThreadState *tstate, _PyInterpreterFrame *frame, |
3002 | | PyObject *name, PyObject *fromlist, PyObject *level) |
3003 | 8.91k | { |
3004 | 8.91k | PyObject *import_func; |
3005 | 8.91k | if (PyMapping_GetOptionalItem(frame->f_builtins, &_Py_ID(__import__), &import_func) < 0) { |
3006 | 0 | return NULL; |
3007 | 0 | } |
3008 | 8.91k | if (import_func == NULL) { |
3009 | 0 | _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found"); |
3010 | 0 | return NULL; |
3011 | 0 | } |
3012 | | |
3013 | 8.91k | PyObject *locals = frame->f_locals; |
3014 | 8.91k | if (locals == NULL) { |
3015 | 5.72k | locals = Py_None; |
3016 | 5.72k | } |
3017 | | |
3018 | | /* Fast path for not overloaded __import__. */ |
3019 | 8.91k | if (_PyImport_IsDefaultImportFunc(tstate->interp, import_func)) { |
3020 | 8.91k | Py_DECREF(import_func); |
3021 | 8.91k | int ilevel = PyLong_AsInt(level); |
3022 | 8.91k | if (ilevel == -1 && _PyErr_Occurred(tstate)) { |
3023 | 0 | return NULL; |
3024 | 0 | } |
3025 | 8.91k | return PyImport_ImportModuleLevelObject( |
3026 | 8.91k | name, |
3027 | 8.91k | frame->f_globals, |
3028 | 8.91k | locals, |
3029 | 8.91k | fromlist, |
3030 | 8.91k | ilevel); |
3031 | 8.91k | } |
3032 | | |
3033 | 0 | PyObject* args[5] = {name, frame->f_globals, locals, fromlist, level}; |
3034 | 0 | PyObject *res = PyObject_Vectorcall(import_func, args, 5, NULL); |
3035 | 0 | Py_DECREF(import_func); |
3036 | 0 | return res; |
3037 | 8.91k | } |
3038 | | |
3039 | | PyObject * |
3040 | | _PyEval_ImportFrom(PyThreadState *tstate, PyObject *v, PyObject *name) |
3041 | 2.57k | { |
3042 | 2.57k | PyObject *x; |
3043 | 2.57k | PyObject *fullmodname, *mod_name, *origin, *mod_name_or_unknown, *errmsg, *spec; |
3044 | | |
3045 | 2.57k | if (PyObject_GetOptionalAttr(v, name, &x) != 0) { |
3046 | 2.53k | return x; |
3047 | 2.53k | } |
3048 | | /* Issue #17636: in case this failed because of a circular relative |
3049 | | import, try to fallback on reading the module directly from |
3050 | | sys.modules. */ |
3051 | 40 | if (PyObject_GetOptionalAttr(v, &_Py_ID(__name__), &mod_name) < 0) { |
3052 | 0 | return NULL; |
3053 | 0 | } |
3054 | 40 | if (mod_name == NULL || !PyUnicode_Check(mod_name)) { |
3055 | 0 | Py_CLEAR(mod_name); |
3056 | 0 | goto error; |
3057 | 0 | } |
3058 | 40 | fullmodname = PyUnicode_FromFormat("%U.%U", mod_name, name); |
3059 | 40 | if (fullmodname == NULL) { |
3060 | 0 | Py_DECREF(mod_name); |
3061 | 0 | return NULL; |
3062 | 0 | } |
3063 | 40 | x = PyImport_GetModule(fullmodname); |
3064 | 40 | Py_DECREF(fullmodname); |
3065 | 40 | if (x == NULL && !_PyErr_Occurred(tstate)) { |
3066 | 40 | goto error; |
3067 | 40 | } |
3068 | 0 | Py_DECREF(mod_name); |
3069 | 0 | return x; |
3070 | | |
3071 | 40 | error: |
3072 | 40 | if (mod_name == NULL) { |
3073 | 0 | mod_name_or_unknown = PyUnicode_FromString("<unknown module name>"); |
3074 | 0 | if (mod_name_or_unknown == NULL) { |
3075 | 0 | return NULL; |
3076 | 0 | } |
3077 | 40 | } else { |
3078 | 40 | mod_name_or_unknown = mod_name; |
3079 | 40 | } |
3080 | | // mod_name is no longer an owned reference |
3081 | 40 | assert(mod_name_or_unknown); |
3082 | 40 | assert(mod_name == NULL || mod_name == mod_name_or_unknown); |
3083 | | |
3084 | 40 | origin = NULL; |
3085 | 40 | if (PyObject_GetOptionalAttr(v, &_Py_ID(__spec__), &spec) < 0) { |
3086 | 0 | Py_DECREF(mod_name_or_unknown); |
3087 | 0 | return NULL; |
3088 | 0 | } |
3089 | 40 | if (spec == NULL) { |
3090 | 0 | errmsg = PyUnicode_FromFormat( |
3091 | 0 | "cannot import name %R from %R (unknown location)", |
3092 | 0 | name, mod_name_or_unknown |
3093 | 0 | ); |
3094 | 0 | goto done_with_errmsg; |
3095 | 0 | } |
3096 | 40 | if (_PyModuleSpec_GetFileOrigin(spec, &origin) < 0) { |
3097 | 0 | goto done; |
3098 | 0 | } |
3099 | | |
3100 | 40 | int is_possibly_shadowing = _PyModule_IsPossiblyShadowing(origin); |
3101 | 40 | if (is_possibly_shadowing < 0) { |
3102 | 0 | goto done; |
3103 | 0 | } |
3104 | 40 | int is_possibly_shadowing_stdlib = 0; |
3105 | 40 | if (is_possibly_shadowing) { |
3106 | 0 | PyObject *stdlib_modules; |
3107 | 0 | if (PySys_GetOptionalAttrString("stdlib_module_names", &stdlib_modules) < 0) { |
3108 | 0 | goto done; |
3109 | 0 | } |
3110 | 0 | if (stdlib_modules && PyAnySet_Check(stdlib_modules)) { |
3111 | 0 | is_possibly_shadowing_stdlib = PySet_Contains(stdlib_modules, mod_name_or_unknown); |
3112 | 0 | if (is_possibly_shadowing_stdlib < 0) { |
3113 | 0 | Py_DECREF(stdlib_modules); |
3114 | 0 | goto done; |
3115 | 0 | } |
3116 | 0 | } |
3117 | 0 | Py_XDECREF(stdlib_modules); |
3118 | 0 | } |
3119 | | |
3120 | 40 | if (origin == NULL && PyModule_Check(v)) { |
3121 | | // Fall back to __file__ for diagnostics if we don't have |
3122 | | // an origin that is a location |
3123 | 40 | origin = PyModule_GetFilenameObject(v); |
3124 | 40 | if (origin == NULL) { |
3125 | 12 | if (!PyErr_ExceptionMatches(PyExc_SystemError)) { |
3126 | 0 | goto done; |
3127 | 0 | } |
3128 | | // PyModule_GetFilenameObject raised "module filename missing" |
3129 | 12 | _PyErr_Clear(tstate); |
3130 | 12 | } |
3131 | 40 | assert(origin == NULL || PyUnicode_Check(origin)); |
3132 | 40 | } |
3133 | | |
3134 | 40 | if (is_possibly_shadowing_stdlib) { |
3135 | 0 | assert(origin); |
3136 | 0 | errmsg = PyUnicode_FromFormat( |
3137 | 0 | "cannot import name %R from %R " |
3138 | 0 | "(consider renaming %R since it has the same " |
3139 | 0 | "name as the standard library module named %R " |
3140 | 0 | "and prevents importing that standard library module)", |
3141 | 0 | name, mod_name_or_unknown, origin, mod_name_or_unknown |
3142 | 0 | ); |
3143 | 0 | } |
3144 | 40 | else { |
3145 | 40 | int rc = _PyModuleSpec_IsInitializing(spec); |
3146 | 40 | if (rc < 0) { |
3147 | 0 | goto done; |
3148 | 0 | } |
3149 | 40 | else if (rc > 0) { |
3150 | 0 | if (is_possibly_shadowing) { |
3151 | 0 | assert(origin); |
3152 | | // For non-stdlib modules, only mention the possibility of |
3153 | | // shadowing if the module is being initialized. |
3154 | 0 | errmsg = PyUnicode_FromFormat( |
3155 | 0 | "cannot import name %R from %R " |
3156 | 0 | "(consider renaming %R if it has the same name " |
3157 | 0 | "as a library you intended to import)", |
3158 | 0 | name, mod_name_or_unknown, origin |
3159 | 0 | ); |
3160 | 0 | } |
3161 | 0 | else if (origin) { |
3162 | 0 | errmsg = PyUnicode_FromFormat( |
3163 | 0 | "cannot import name %R from partially initialized module %R " |
3164 | 0 | "(most likely due to a circular import) (%S)", |
3165 | 0 | name, mod_name_or_unknown, origin |
3166 | 0 | ); |
3167 | 0 | } |
3168 | 0 | else { |
3169 | 0 | errmsg = PyUnicode_FromFormat( |
3170 | 0 | "cannot import name %R from partially initialized module %R " |
3171 | 0 | "(most likely due to a circular import)", |
3172 | 0 | name, mod_name_or_unknown |
3173 | 0 | ); |
3174 | 0 | } |
3175 | 0 | } |
3176 | 40 | else { |
3177 | 40 | assert(rc == 0); |
3178 | 40 | if (origin) { |
3179 | 28 | errmsg = PyUnicode_FromFormat( |
3180 | 28 | "cannot import name %R from %R (%S)", |
3181 | 28 | name, mod_name_or_unknown, origin |
3182 | 28 | ); |
3183 | 28 | } |
3184 | 12 | else { |
3185 | 12 | errmsg = PyUnicode_FromFormat( |
3186 | 12 | "cannot import name %R from %R (unknown location)", |
3187 | 12 | name, mod_name_or_unknown |
3188 | 12 | ); |
3189 | 12 | } |
3190 | 40 | } |
3191 | 40 | } |
3192 | | |
3193 | 40 | done_with_errmsg: |
3194 | 40 | if (errmsg != NULL) { |
3195 | | /* NULL checks for mod_name and origin done by _PyErr_SetImportErrorWithNameFrom */ |
3196 | 40 | _PyErr_SetImportErrorWithNameFrom(errmsg, mod_name, origin, name); |
3197 | 40 | Py_DECREF(errmsg); |
3198 | 40 | } |
3199 | | |
3200 | 40 | done: |
3201 | 40 | Py_XDECREF(origin); |
3202 | 40 | Py_XDECREF(spec); |
3203 | 40 | Py_DECREF(mod_name_or_unknown); |
3204 | 40 | return NULL; |
3205 | 40 | } |
3206 | | |
3207 | 0 | #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\ |
3208 | 0 | "BaseException is not allowed" |
3209 | | |
3210 | 0 | #define CANNOT_EXCEPT_STAR_EG "catching ExceptionGroup with except* "\ |
3211 | 0 | "is not allowed. Use except instead." |
3212 | | |
3213 | | int |
3214 | | _PyEval_CheckExceptTypeValid(PyThreadState *tstate, PyObject* right) |
3215 | 18.4M | { |
3216 | 18.4M | if (PyTuple_Check(right)) { |
3217 | 156k | Py_ssize_t i, length; |
3218 | 156k | length = PyTuple_GET_SIZE(right); |
3219 | 470k | for (i = 0; i < length; i++) { |
3220 | 313k | PyObject *exc = PyTuple_GET_ITEM(right, i); |
3221 | 313k | if (!PyExceptionClass_Check(exc)) { |
3222 | 0 | _PyErr_SetString(tstate, PyExc_TypeError, |
3223 | 0 | CANNOT_CATCH_MSG); |
3224 | 0 | return -1; |
3225 | 0 | } |
3226 | 313k | } |
3227 | 156k | } |
3228 | 18.3M | else { |
3229 | 18.3M | if (!PyExceptionClass_Check(right)) { |
3230 | 0 | _PyErr_SetString(tstate, PyExc_TypeError, |
3231 | 0 | CANNOT_CATCH_MSG); |
3232 | 0 | return -1; |
3233 | 0 | } |
3234 | 18.3M | } |
3235 | 18.4M | return 0; |
3236 | 18.4M | } |
3237 | | |
3238 | | int |
3239 | | _PyEval_CheckExceptStarTypeValid(PyThreadState *tstate, PyObject* right) |
3240 | 0 | { |
3241 | 0 | if (_PyEval_CheckExceptTypeValid(tstate, right) < 0) { |
3242 | 0 | return -1; |
3243 | 0 | } |
3244 | | |
3245 | | /* reject except *ExceptionGroup */ |
3246 | | |
3247 | 0 | int is_subclass = 0; |
3248 | 0 | if (PyTuple_Check(right)) { |
3249 | 0 | Py_ssize_t length = PyTuple_GET_SIZE(right); |
3250 | 0 | for (Py_ssize_t i = 0; i < length; i++) { |
3251 | 0 | PyObject *exc = PyTuple_GET_ITEM(right, i); |
3252 | 0 | is_subclass = PyObject_IsSubclass(exc, PyExc_BaseExceptionGroup); |
3253 | 0 | if (is_subclass < 0) { |
3254 | 0 | return -1; |
3255 | 0 | } |
3256 | 0 | if (is_subclass) { |
3257 | 0 | break; |
3258 | 0 | } |
3259 | 0 | } |
3260 | 0 | } |
3261 | 0 | else { |
3262 | 0 | is_subclass = PyObject_IsSubclass(right, PyExc_BaseExceptionGroup); |
3263 | 0 | if (is_subclass < 0) { |
3264 | 0 | return -1; |
3265 | 0 | } |
3266 | 0 | } |
3267 | 0 | if (is_subclass) { |
3268 | 0 | _PyErr_SetString(tstate, PyExc_TypeError, |
3269 | 0 | CANNOT_EXCEPT_STAR_EG); |
3270 | 0 | return -1; |
3271 | 0 | } |
3272 | 0 | return 0; |
3273 | 0 | } |
3274 | | |
3275 | | int |
3276 | | _Py_Check_ArgsIterable(PyThreadState *tstate, PyObject *func, PyObject *args) |
3277 | 110 | { |
3278 | 110 | if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) { |
3279 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
3280 | 0 | "Value after * must be an iterable, not %.200s", |
3281 | 0 | Py_TYPE(args)->tp_name); |
3282 | 0 | return -1; |
3283 | 0 | } |
3284 | 110 | return 0; |
3285 | 110 | } |
3286 | | |
3287 | | void |
3288 | | _PyEval_FormatKwargsError(PyThreadState *tstate, PyObject *func, PyObject *kwargs) |
3289 | 0 | { |
3290 | | /* _PyDict_MergeEx raises attribute |
3291 | | * error (percolated from an attempt |
3292 | | * to get 'keys' attribute) instead of |
3293 | | * a type error if its second argument |
3294 | | * is not a mapping. |
3295 | | */ |
3296 | 0 | if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) { |
3297 | 0 | _PyErr_Format( |
3298 | 0 | tstate, PyExc_TypeError, |
3299 | 0 | "Value after ** must be a mapping, not %.200s", |
3300 | 0 | Py_TYPE(kwargs)->tp_name); |
3301 | 0 | } |
3302 | 0 | else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
3303 | 0 | PyObject *exc = _PyErr_GetRaisedException(tstate); |
3304 | 0 | PyObject *args = PyException_GetArgs(exc); |
3305 | 0 | if (PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1) { |
3306 | 0 | _PyErr_Clear(tstate); |
3307 | 0 | PyObject *funcstr = _PyObject_FunctionStr(func); |
3308 | 0 | if (funcstr != NULL) { |
3309 | 0 | PyObject *key = PyTuple_GET_ITEM(args, 0); |
3310 | 0 | _PyErr_Format( |
3311 | 0 | tstate, PyExc_TypeError, |
3312 | 0 | "%U got multiple values for keyword argument '%S'", |
3313 | 0 | funcstr, key); |
3314 | 0 | Py_DECREF(funcstr); |
3315 | 0 | } |
3316 | 0 | Py_XDECREF(exc); |
3317 | 0 | } |
3318 | 0 | else { |
3319 | 0 | _PyErr_SetRaisedException(tstate, exc); |
3320 | 0 | } |
3321 | 0 | Py_DECREF(args); |
3322 | 0 | } |
3323 | 0 | } |
3324 | | |
3325 | | void |
3326 | | _PyEval_FormatExcCheckArg(PyThreadState *tstate, PyObject *exc, |
3327 | | const char *format_str, PyObject *obj) |
3328 | 1 | { |
3329 | 1 | const char *obj_str; |
3330 | | |
3331 | 1 | if (!obj) |
3332 | 0 | return; |
3333 | | |
3334 | 1 | obj_str = PyUnicode_AsUTF8(obj); |
3335 | 1 | if (!obj_str) |
3336 | 0 | return; |
3337 | | |
3338 | 1 | _PyErr_Format(tstate, exc, format_str, obj_str); |
3339 | | |
3340 | 1 | if (exc == PyExc_NameError) { |
3341 | | // Include the name in the NameError exceptions to offer suggestions later. |
3342 | 1 | PyObject *exc = PyErr_GetRaisedException(); |
3343 | 1 | if (PyErr_GivenExceptionMatches(exc, PyExc_NameError)) { |
3344 | 1 | if (((PyNameErrorObject*)exc)->name == NULL) { |
3345 | | // We do not care if this fails because we are going to restore the |
3346 | | // NameError anyway. |
3347 | 1 | (void)PyObject_SetAttr(exc, &_Py_ID(name), obj); |
3348 | 1 | } |
3349 | 1 | } |
3350 | 1 | PyErr_SetRaisedException(exc); |
3351 | 1 | } |
3352 | 1 | } |
3353 | | |
3354 | | void |
3355 | | _PyEval_FormatExcUnbound(PyThreadState *tstate, PyCodeObject *co, int oparg) |
3356 | 0 | { |
3357 | 0 | PyObject *name; |
3358 | | /* Don't stomp existing exception */ |
3359 | 0 | if (_PyErr_Occurred(tstate)) |
3360 | 0 | return; |
3361 | 0 | name = PyTuple_GET_ITEM(co->co_localsplusnames, oparg); |
3362 | 0 | if (oparg < PyUnstable_Code_GetFirstFree(co)) { |
3363 | 0 | _PyEval_FormatExcCheckArg(tstate, PyExc_UnboundLocalError, |
3364 | 0 | UNBOUNDLOCAL_ERROR_MSG, name); |
3365 | 0 | } else { |
3366 | 0 | _PyEval_FormatExcCheckArg(tstate, PyExc_NameError, |
3367 | 0 | UNBOUNDFREE_ERROR_MSG, name); |
3368 | 0 | } |
3369 | 0 | } |
3370 | | |
3371 | | void |
3372 | | _PyEval_FormatAwaitableError(PyThreadState *tstate, PyTypeObject *type, int oparg) |
3373 | 0 | { |
3374 | 0 | if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) { |
3375 | 0 | if (oparg == 1) { |
3376 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
3377 | 0 | "'async with' received an object from __aenter__ " |
3378 | 0 | "that does not implement __await__: %.100s", |
3379 | 0 | type->tp_name); |
3380 | 0 | } |
3381 | 0 | else if (oparg == 2) { |
3382 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
3383 | 0 | "'async with' received an object from __aexit__ " |
3384 | 0 | "that does not implement __await__: %.100s", |
3385 | 0 | type->tp_name); |
3386 | 0 | } |
3387 | 0 | } |
3388 | 0 | } |
3389 | | |
3390 | | |
3391 | | Py_ssize_t |
3392 | | PyUnstable_Eval_RequestCodeExtraIndex(freefunc free) |
3393 | 0 | { |
3394 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
3395 | 0 | Py_ssize_t new_index; |
3396 | |
|
3397 | 0 | if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) { |
3398 | 0 | return -1; |
3399 | 0 | } |
3400 | 0 | new_index = interp->co_extra_user_count++; |
3401 | 0 | interp->co_extra_freefuncs[new_index] = free; |
3402 | 0 | return new_index; |
3403 | 0 | } |
3404 | | |
3405 | | /* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions |
3406 | | for the limited API. */ |
3407 | | |
3408 | | int Py_EnterRecursiveCall(const char *where) |
3409 | 1.38M | { |
3410 | 1.38M | return _Py_EnterRecursiveCall(where); |
3411 | 1.38M | } |
3412 | | |
3413 | | void Py_LeaveRecursiveCall(void) |
3414 | 1.18M | { |
3415 | 1.18M | _Py_LeaveRecursiveCall(); |
3416 | 1.18M | } |
3417 | | |
3418 | | PyObject * |
3419 | | _PyEval_GetANext(PyObject *aiter) |
3420 | 0 | { |
3421 | 0 | unaryfunc getter = NULL; |
3422 | 0 | PyObject *next_iter = NULL; |
3423 | 0 | PyTypeObject *type = Py_TYPE(aiter); |
3424 | 0 | if (PyAsyncGen_CheckExact(aiter)) { |
3425 | 0 | return type->tp_as_async->am_anext(aiter); |
3426 | 0 | } |
3427 | 0 | if (type->tp_as_async != NULL){ |
3428 | 0 | getter = type->tp_as_async->am_anext; |
3429 | 0 | } |
3430 | |
|
3431 | 0 | if (getter != NULL) { |
3432 | 0 | next_iter = (*getter)(aiter); |
3433 | 0 | if (next_iter == NULL) { |
3434 | 0 | return NULL; |
3435 | 0 | } |
3436 | 0 | } |
3437 | 0 | else { |
3438 | 0 | PyErr_Format(PyExc_TypeError, |
3439 | 0 | "'async for' requires an iterator with " |
3440 | 0 | "__anext__ method, got %.100s", |
3441 | 0 | type->tp_name); |
3442 | 0 | return NULL; |
3443 | 0 | } |
3444 | | |
3445 | 0 | PyObject *awaitable = _PyCoro_GetAwaitableIter(next_iter); |
3446 | 0 | if (awaitable == NULL) { |
3447 | 0 | _PyErr_FormatFromCause( |
3448 | 0 | PyExc_TypeError, |
3449 | 0 | "'async for' received an invalid object " |
3450 | 0 | "from __anext__: %.100s", |
3451 | 0 | Py_TYPE(next_iter)->tp_name); |
3452 | 0 | } |
3453 | 0 | Py_DECREF(next_iter); |
3454 | 0 | return awaitable; |
3455 | 0 | } |
3456 | | |
3457 | | void |
3458 | | _PyEval_LoadGlobalStackRef(PyObject *globals, PyObject *builtins, PyObject *name, _PyStackRef *writeto) |
3459 | 100k | { |
3460 | 100k | if (PyDict_CheckExact(globals) && PyDict_CheckExact(builtins)) { |
3461 | 100k | _PyDict_LoadGlobalStackRef((PyDictObject *)globals, |
3462 | 100k | (PyDictObject *)builtins, |
3463 | 100k | name, writeto); |
3464 | 100k | if (PyStackRef_IsNull(*writeto) && !PyErr_Occurred()) { |
3465 | | /* _PyDict_LoadGlobal() returns NULL without raising |
3466 | | * an exception if the key doesn't exist */ |
3467 | 1 | _PyEval_FormatExcCheckArg(PyThreadState_GET(), PyExc_NameError, |
3468 | 1 | NAME_ERROR_MSG, name); |
3469 | 1 | } |
3470 | 100k | } |
3471 | 0 | else { |
3472 | | /* Slow-path if globals or builtins is not a dict */ |
3473 | | /* namespace 1: globals */ |
3474 | 0 | PyObject *res; |
3475 | 0 | if (PyMapping_GetOptionalItem(globals, name, &res) < 0) { |
3476 | 0 | *writeto = PyStackRef_NULL; |
3477 | 0 | return; |
3478 | 0 | } |
3479 | 0 | if (res == NULL) { |
3480 | | /* namespace 2: builtins */ |
3481 | 0 | if (PyMapping_GetOptionalItem(builtins, name, &res) < 0) { |
3482 | 0 | *writeto = PyStackRef_NULL; |
3483 | 0 | return; |
3484 | 0 | } |
3485 | 0 | if (res == NULL) { |
3486 | 0 | _PyEval_FormatExcCheckArg( |
3487 | 0 | PyThreadState_GET(), PyExc_NameError, |
3488 | 0 | NAME_ERROR_MSG, name); |
3489 | 0 | *writeto = PyStackRef_NULL; |
3490 | 0 | return; |
3491 | 0 | } |
3492 | 0 | } |
3493 | 0 | *writeto = PyStackRef_FromPyObjectSteal(res); |
3494 | 0 | } |
3495 | 100k | } |
3496 | | |
3497 | | PyObject * |
3498 | | _PyEval_GetAwaitable(PyObject *iterable, int oparg) |
3499 | 0 | { |
3500 | 0 | PyObject *iter = _PyCoro_GetAwaitableIter(iterable); |
3501 | |
|
3502 | 0 | if (iter == NULL) { |
3503 | 0 | _PyEval_FormatAwaitableError(PyThreadState_GET(), |
3504 | 0 | Py_TYPE(iterable), oparg); |
3505 | 0 | } |
3506 | 0 | else if (PyCoro_CheckExact(iter)) { |
3507 | 0 | PyObject *yf = _PyGen_yf((PyGenObject*)iter); |
3508 | 0 | if (yf != NULL) { |
3509 | | /* `iter` is a coroutine object that is being |
3510 | | awaited, `yf` is a pointer to the current awaitable |
3511 | | being awaited on. */ |
3512 | 0 | Py_DECREF(yf); |
3513 | 0 | Py_CLEAR(iter); |
3514 | 0 | _PyErr_SetString(PyThreadState_GET(), PyExc_RuntimeError, |
3515 | 0 | "coroutine is being awaited already"); |
3516 | 0 | } |
3517 | 0 | } |
3518 | 0 | return iter; |
3519 | 0 | } |
3520 | | |
3521 | | PyObject * |
3522 | | _PyEval_LoadName(PyThreadState *tstate, _PyInterpreterFrame *frame, PyObject *name) |
3523 | 47.1k | { |
3524 | | |
3525 | 47.1k | PyObject *value; |
3526 | 47.1k | if (frame->f_locals == NULL) { |
3527 | 0 | _PyErr_SetString(tstate, PyExc_SystemError, |
3528 | 0 | "no locals found"); |
3529 | 0 | return NULL; |
3530 | 0 | } |
3531 | 47.1k | if (PyMapping_GetOptionalItem(frame->f_locals, name, &value) < 0) { |
3532 | 0 | return NULL; |
3533 | 0 | } |
3534 | 47.1k | if (value != NULL) { |
3535 | 31.9k | return value; |
3536 | 31.9k | } |
3537 | 15.2k | if (PyDict_GetItemRef(frame->f_globals, name, &value) < 0) { |
3538 | 0 | return NULL; |
3539 | 0 | } |
3540 | 15.2k | if (value != NULL) { |
3541 | 7.25k | return value; |
3542 | 7.25k | } |
3543 | 7.96k | if (PyMapping_GetOptionalItem(frame->f_builtins, name, &value) < 0) { |
3544 | 0 | return NULL; |
3545 | 0 | } |
3546 | 7.96k | if (value == NULL) { |
3547 | 0 | _PyEval_FormatExcCheckArg( |
3548 | 0 | tstate, PyExc_NameError, |
3549 | 0 | NAME_ERROR_MSG, name); |
3550 | 0 | } |
3551 | 7.96k | return value; |
3552 | 7.96k | } |
3553 | | |
3554 | | static _PyStackRef |
3555 | | foriter_next(PyObject *seq, _PyStackRef index) |
3556 | 2.71k | { |
3557 | 2.71k | assert(PyStackRef_IsTaggedInt(index)); |
3558 | 2.71k | assert(PyTuple_CheckExact(seq) || PyList_CheckExact(seq)); |
3559 | 2.71k | intptr_t i = PyStackRef_UntagInt(index); |
3560 | 2.71k | if (PyTuple_CheckExact(seq)) { |
3561 | 1.17k | size_t size = PyTuple_GET_SIZE(seq); |
3562 | 1.17k | if ((size_t)i >= size) { |
3563 | 280 | return PyStackRef_NULL; |
3564 | 280 | } |
3565 | 896 | return PyStackRef_FromPyObjectNew(PyTuple_GET_ITEM(seq, i)); |
3566 | 1.17k | } |
3567 | 1.54k | PyObject *item = _PyList_GetItemRef((PyListObject *)seq, i); |
3568 | 1.54k | if (item == NULL) { |
3569 | 284 | return PyStackRef_NULL; |
3570 | 284 | } |
3571 | 1.25k | return PyStackRef_FromPyObjectSteal(item); |
3572 | 1.54k | } |
3573 | | |
3574 | | _PyStackRef _PyForIter_VirtualIteratorNext(PyThreadState* tstate, _PyInterpreterFrame* frame, _PyStackRef iter, _PyStackRef* index_ptr) |
3575 | 355M | { |
3576 | 355M | PyObject *iter_o = PyStackRef_AsPyObjectBorrow(iter); |
3577 | 355M | _PyStackRef index = *index_ptr; |
3578 | 355M | if (PyStackRef_IsTaggedInt(index)) { |
3579 | 2.71k | *index_ptr = PyStackRef_IncrementTaggedIntNoOverflow(index); |
3580 | 2.71k | return foriter_next(iter_o, index); |
3581 | 2.71k | } |
3582 | 355M | PyObject *next_o = (*Py_TYPE(iter_o)->tp_iternext)(iter_o); |
3583 | 355M | if (next_o == NULL) { |
3584 | 61.0M | if (_PyErr_Occurred(tstate)) { |
3585 | 11.9M | if (_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) { |
3586 | 11.9M | _PyEval_MonitorRaise(tstate, frame, frame->instr_ptr); |
3587 | 11.9M | _PyErr_Clear(tstate); |
3588 | 11.9M | } |
3589 | 85 | else { |
3590 | 85 | return PyStackRef_ERROR; |
3591 | 85 | } |
3592 | 11.9M | } |
3593 | 61.0M | return PyStackRef_NULL; |
3594 | 61.0M | } |
3595 | 294M | return PyStackRef_FromPyObjectSteal(next_o); |
3596 | 355M | } |
3597 | | |
3598 | | /* Check if a 'cls' provides the given special method. */ |
3599 | | static inline int |
3600 | | type_has_special_method(PyTypeObject *cls, PyObject *name) |
3601 | 0 | { |
3602 | | // _PyType_Lookup() does not set an exception and returns a borrowed ref |
3603 | 0 | assert(!PyErr_Occurred()); |
3604 | 0 | PyObject *r = _PyType_Lookup(cls, name); |
3605 | 0 | return r != NULL && Py_TYPE(r)->tp_descr_get != NULL; |
3606 | 0 | } |
3607 | | |
3608 | | int |
3609 | | _PyEval_SpecialMethodCanSuggest(PyObject *self, int oparg) |
3610 | 0 | { |
3611 | 0 | PyTypeObject *type = Py_TYPE(self); |
3612 | 0 | switch (oparg) { |
3613 | 0 | case SPECIAL___ENTER__: |
3614 | 0 | case SPECIAL___EXIT__: { |
3615 | 0 | return type_has_special_method(type, &_Py_ID(__aenter__)) |
3616 | 0 | && type_has_special_method(type, &_Py_ID(__aexit__)); |
3617 | 0 | } |
3618 | 0 | case SPECIAL___AENTER__: |
3619 | 0 | case SPECIAL___AEXIT__: { |
3620 | 0 | return type_has_special_method(type, &_Py_ID(__enter__)) |
3621 | 0 | && type_has_special_method(type, &_Py_ID(__exit__)); |
3622 | 0 | } |
3623 | 0 | default: |
3624 | 0 | Py_FatalError("unsupported special method"); |
3625 | 0 | } |
3626 | 0 | } |