1 /* Frame object implementation */
2
3 #include "Python.h"
4 #include "pycore_ceval.h" // _PyEval_BuiltinsFromGlobals()
5 #include "pycore_code.h" // CO_FAST_LOCAL, etc.
6 #include "pycore_function.h" // _PyFunction_FromConstructor()
7 #include "pycore_moduleobject.h" // _PyModule_GetDict()
8 #include "pycore_object.h" // _PyObject_GC_UNTRACK()
9 #include "pycore_opcode.h" // _PyOpcode_Caches
10
11 #include "frameobject.h" // PyFrameObject
12 #include "pycore_frame.h"
13 #include "opcode.h" // EXTENDED_ARG
14 #include "structmember.h" // PyMemberDef
15
16 #define OFF(x) offsetof(PyFrameObject, x)
17
18 static PyMemberDef frame_memberlist[] = {
19 {"f_trace_lines", T_BOOL, OFF(f_trace_lines), 0},
20 {NULL} /* Sentinel */
21 };
22
23
24 static PyObject *
25 frame_getlocals(PyFrameObject *f, void *closure)
26 {
27 if (f == NULL) {
28 PyErr_BadInternalCall();
29 return NULL;
30 }
31 assert(!_PyFrame_IsIncomplete(f->f_frame));
32 PyObject *locals = _PyFrame_GetLocals(f->f_frame, 1);
33 if (locals) {
34 f->f_fast_as_locals = 1;
35 }
36 return locals;
37 }
38
39 int
40 PyFrame_GetLineNumber(PyFrameObject *f)
41 {
42 assert(f != NULL);
43 if (f->f_lineno != 0) {
44 return f->f_lineno;
45 }
46 else {
47 return PyUnstable_InterpreterFrame_GetLine(f->f_frame);
48 }
49 }
50
51 static PyObject *
52 frame_getlineno(PyFrameObject *f, void *closure)
53 {
54 int lineno = PyFrame_GetLineNumber(f);
55 if (lineno < 0) {
56 Py_RETURN_NONE;
57 }
58 else {
59 return PyLong_FromLong(lineno);
60 }
61 }
62
63 static PyObject *
64 frame_getlasti(PyFrameObject *f, void *closure)
65 {
66 int lasti = _PyInterpreterFrame_LASTI(f->f_frame);
67 if (lasti < 0) {
68 return PyLong_FromLong(-1);
69 }
70 return PyLong_FromLong(lasti * sizeof(_Py_CODEUNIT));
71 }
72
73 static PyObject *
74 frame_getglobals(PyFrameObject *f, void *closure)
75 {
76 PyObject *globals = f->f_frame->f_globals;
77 if (globals == NULL) {
78 globals = Py_None;
79 }
80 return Py_NewRef(globals);
81 }
82
83 static PyObject *
84 frame_getbuiltins(PyFrameObject *f, void *closure)
85 {
86 PyObject *builtins = f->f_frame->f_builtins;
87 if (builtins == NULL) {
88 builtins = Py_None;
89 }
90 return Py_NewRef(builtins);
91 }
92
93 static PyObject *
94 frame_getcode(PyFrameObject *f, void *closure)
95 {
96 if (PySys_Audit("object.__getattr__", "Os", f, "f_code") < 0) {
97 return NULL;
98 }
99 return (PyObject *)PyFrame_GetCode(f);
100 }
101
102 static PyObject *
103 frame_getback(PyFrameObject *f, void *closure)
104 {
105 PyObject *res = (PyObject *)PyFrame_GetBack(f);
106 if (res == NULL) {
107 Py_RETURN_NONE;
108 }
109 return res;
110 }
111
112 static PyObject *
113 frame_gettrace_opcodes(PyFrameObject *f, void *closure)
114 {
115 PyObject *result = f->f_trace_opcodes ? Py_True : Py_False;
116 return Py_NewRef(result);
117 }
118
119 static int
120 frame_settrace_opcodes(PyFrameObject *f, PyObject* value, void *Py_UNUSED(ignored))
121 {
122 if (!PyBool_Check(value)) {
123 PyErr_SetString(PyExc_TypeError,
124 "attribute value type must be bool");
125 return -1;
126 }
127 if (value == Py_True) {
128 f->f_trace_opcodes = 1;
129 _PyInterpreterState_GET()->f_opcode_trace_set = true;
130 }
131 else {
132 f->f_trace_opcodes = 0;
133 }
134 return 0;
135 }
136
137 /* Model the evaluation stack, to determine which jumps
138 * are safe and how many values needs to be popped.
139 * The stack is modelled by a 64 integer, treating any
140 * stack that can't fit into 64 bits as "overflowed".
141 */
142
143 typedef enum kind {
144 Iterator = 1,
145 Except = 2,
146 Object = 3,
147 Null = 4,
148 Lasti = 5,
149 } Kind;
150
151 static int
152 compatible_kind(Kind from, Kind to) {
153 if (to == 0) {
154 return 0;
155 }
156 if (to == Object) {
157 return from != Null;
158 }
159 if (to == Null) {
160 return 1;
161 }
162 return from == to;
163 }
164
165 #define BITS_PER_BLOCK 3
166
167 #define UNINITIALIZED -2
168 #define OVERFLOWED -1
169
170 #define MAX_STACK_ENTRIES (63/BITS_PER_BLOCK)
171 #define WILL_OVERFLOW (1ULL<<((MAX_STACK_ENTRIES-1)*BITS_PER_BLOCK))
172
173 #define EMPTY_STACK 0
174
175 static inline int64_t
176 push_value(int64_t stack, Kind kind)
177 {
178 if (((uint64_t)stack) >= WILL_OVERFLOW) {
179 return OVERFLOWED;
180 }
181 else {
182 return (stack << BITS_PER_BLOCK) | kind;
183 }
184 }
185
186 static inline int64_t
187 pop_value(int64_t stack)
188 {
189 return Py_ARITHMETIC_RIGHT_SHIFT(int64_t, stack, BITS_PER_BLOCK);
190 }
191
192 #define MASK ((1<<BITS_PER_BLOCK)-1)
193
194 static inline Kind
195 top_of_stack(int64_t stack)
196 {
197 return stack & MASK;
198 }
199
200 static inline Kind
201 peek(int64_t stack, int n)
202 {
203 assert(n >= 1);
204 return (stack>>(BITS_PER_BLOCK*(n-1))) & MASK;
205 }
206
207 static Kind
208 stack_swap(int64_t stack, int n)
209 {
210 assert(n >= 1);
211 Kind to_swap = peek(stack, n);
212 Kind top = top_of_stack(stack);
213 int shift = BITS_PER_BLOCK*(n-1);
214 int64_t replaced_low = (stack & ~(MASK << shift)) | (top << shift);
215 int64_t replaced_top = (replaced_low & ~MASK) | to_swap;
216 return replaced_top;
217 }
218
219 static int64_t
220 pop_to_level(int64_t stack, int level) {
221 if (level == 0) {
222 return EMPTY_STACK;
223 }
224 int64_t max_item = (1<<BITS_PER_BLOCK) - 1;
225 int64_t level_max_stack = max_item << ((level-1) * BITS_PER_BLOCK);
226 while (stack > level_max_stack) {
227 stack = pop_value(stack);
228 }
229 return stack;
230 }
231
232 #if 0
233 /* These functions are useful for debugging the stack marking code */
234
235 static char
236 tos_char(int64_t stack) {
237 switch(top_of_stack(stack)) {
238 case Iterator:
239 return 'I';
240 case Except:
241 return 'E';
242 case Object:
243 return 'O';
244 case Lasti:
245 return 'L';
246 case Null:
247 return 'N';
248 }
249 return '?';
250 }
251
252 static void
253 print_stack(int64_t stack) {
254 if (stack < 0) {
255 if (stack == UNINITIALIZED) {
256 printf("---");
257 }
258 else if (stack == OVERFLOWED) {
259 printf("OVERFLOWED");
260 }
261 else {
262 printf("??");
263 }
264 return;
265 }
266 while (stack) {
267 printf("%c", tos_char(stack));
268 stack = pop_value(stack);
269 }
270 }
271
272 static void
273 print_stacks(int64_t *stacks, int n) {
274 for (int i = 0; i < n; i++) {
275 printf("%d: ", i);
276 print_stack(stacks[i]);
277 printf("\n");
278 }
279 }
280
281 #endif
282
283 static int64_t *
284 mark_stacks(PyCodeObject *code_obj, int len)
285 {
286 PyObject *co_code = _PyCode_GetCode(code_obj);
287 if (co_code == NULL) {
288 return NULL;
289 }
290 _Py_CODEUNIT *code = (_Py_CODEUNIT *)PyBytes_AS_STRING(co_code);
291 int64_t *stacks = PyMem_New(int64_t, len+1);
292 int i, j, opcode;
293
294 if (stacks == NULL) {
295 PyErr_NoMemory();
296 Py_DECREF(co_code);
297 return NULL;
298 }
299 for (int i = 1; i <= len; i++) {
300 stacks[i] = UNINITIALIZED;
301 }
302 stacks[0] = EMPTY_STACK;
303 if (code_obj->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR))
304 {
305 // Generators get sent None while starting:
306 stacks[0] = push_value(stacks[0], Object);
307 }
308 int todo = 1;
309 while (todo) {
310 todo = 0;
311 /* Scan instructions */
312 for (i = 0; i < len;) {
313 int64_t next_stack = stacks[i];
314 opcode = _Py_GetBaseOpcode(code_obj, i);
315 int oparg = 0;
316 while (opcode == EXTENDED_ARG) {
317 oparg = (oparg << 8) | code[i].op.arg;
318 i++;
319 opcode = _Py_GetBaseOpcode(code_obj, i);
320 stacks[i] = next_stack;
321 }
322 int next_i = i + _PyOpcode_Caches[opcode] + 1;
323 if (next_stack == UNINITIALIZED) {
324 i = next_i;
325 continue;
326 }
327 oparg = (oparg << 8) | code[i].op.arg;
328 switch (opcode) {
329 case POP_JUMP_IF_FALSE:
330 case POP_JUMP_IF_TRUE:
331 {
332 int64_t target_stack;
333 int j = next_i + oparg;
334 assert(j < len);
335 next_stack = pop_value(next_stack);
336 target_stack = next_stack;
337 assert(stacks[j] == UNINITIALIZED || stacks[j] == target_stack);
338 stacks[j] = target_stack;
339 stacks[next_i] = next_stack;
340 break;
341 }
342 case SEND:
343 j = oparg + i + INLINE_CACHE_ENTRIES_SEND + 1;
344 assert(j < len);
345 assert(stacks[j] == UNINITIALIZED || stacks[j] == next_stack);
346 stacks[j] = next_stack;
347 stacks[next_i] = next_stack;
348 break;
349 case JUMP_FORWARD:
350 j = oparg + i + 1;
351 assert(j < len);
352 assert(stacks[j] == UNINITIALIZED || stacks[j] == next_stack);
353 stacks[j] = next_stack;
354 break;
355 case JUMP_BACKWARD:
356 case JUMP_BACKWARD_NO_INTERRUPT:
357 j = i + 1 - oparg;
358 assert(j >= 0);
359 assert(j < len);
360 if (stacks[j] == UNINITIALIZED && j < i) {
361 todo = 1;
362 }
363 assert(stacks[j] == UNINITIALIZED || stacks[j] == next_stack);
364 stacks[j] = next_stack;
365 break;
366 case GET_ITER:
367 case GET_AITER:
368 next_stack = push_value(pop_value(next_stack), Iterator);
369 stacks[next_i] = next_stack;
370 break;
371 case FOR_ITER:
372 {
373 int64_t target_stack = push_value(next_stack, Object);
374 stacks[next_i] = target_stack;
375 j = oparg + 1 + INLINE_CACHE_ENTRIES_FOR_ITER + i;
376 assert(j < len);
377 assert(stacks[j] == UNINITIALIZED || stacks[j] == target_stack);
378 stacks[j] = target_stack;
379 break;
380 }
381 case END_ASYNC_FOR:
382 next_stack = pop_value(pop_value(next_stack));
383 stacks[next_i] = next_stack;
384 break;
385 case PUSH_EXC_INFO:
386 next_stack = push_value(next_stack, Except);
387 stacks[next_i] = next_stack;
388 break;
389 case POP_EXCEPT:
390 assert(top_of_stack(next_stack) == Except);
391 next_stack = pop_value(next_stack);
392 stacks[next_i] = next_stack;
393 break;
394 case RETURN_VALUE:
395 assert(pop_value(next_stack) == EMPTY_STACK);
396 assert(top_of_stack(next_stack) == Object);
397 break;
398 case RETURN_CONST:
399 break;
400 case RAISE_VARARGS:
401 break;
402 case RERAISE:
403 assert(top_of_stack(next_stack) == Except);
404 /* End of block */
405 break;
406 case PUSH_NULL:
407 next_stack = push_value(next_stack, Null);
408 stacks[next_i] = next_stack;
409 break;
410 case LOAD_GLOBAL:
411 {
412 int j = oparg;
413 if (j & 1) {
414 next_stack = push_value(next_stack, Null);
415 }
416 next_stack = push_value(next_stack, Object);
417 stacks[next_i] = next_stack;
418 break;
419 }
420 case LOAD_ATTR:
421 {
422 assert(top_of_stack(next_stack) == Object);
423 int j = oparg;
424 if (j & 1) {
425 next_stack = pop_value(next_stack);
426 next_stack = push_value(next_stack, Null);
427 next_stack = push_value(next_stack, Object);
428 }
429 stacks[next_i] = next_stack;
430 break;
431 }
432 case CALL:
433 {
434 int args = oparg;
435 for (int j = 0; j < args+2; j++) {
436 next_stack = pop_value(next_stack);
437 }
438 next_stack = push_value(next_stack, Object);
439 stacks[next_i] = next_stack;
440 break;
441 }
442 case SWAP:
443 {
444 int n = oparg;
445 next_stack = stack_swap(next_stack, n);
446 stacks[next_i] = next_stack;
447 break;
448 }
449 case COPY:
450 {
451 int n = oparg;
452 next_stack = push_value(next_stack, peek(next_stack, n));
453 stacks[next_i] = next_stack;
454 break;
455 }
456 case CACHE:
457 case RESERVED:
458 {
459 assert(0);
460 }
461 default:
462 {
463 int delta = PyCompile_OpcodeStackEffect(opcode, oparg);
464 assert(delta != PY_INVALID_STACK_EFFECT);
465 while (delta < 0) {
466 next_stack = pop_value(next_stack);
467 delta++;
468 }
469 while (delta > 0) {
470 next_stack = push_value(next_stack, Object);
471 delta--;
472 }
473 stacks[next_i] = next_stack;
474 }
475 }
476 i = next_i;
477 }
478 /* Scan exception table */
479 unsigned char *start = (unsigned char *)PyBytes_AS_STRING(code_obj->co_exceptiontable);
480 unsigned char *end = start + PyBytes_GET_SIZE(code_obj->co_exceptiontable);
481 unsigned char *scan = start;
482 while (scan < end) {
483 int start_offset, size, handler;
484 scan = parse_varint(scan, &start_offset);
485 assert(start_offset >= 0 && start_offset < len);
486 scan = parse_varint(scan, &size);
487 assert(size >= 0 && start_offset+size <= len);
488 scan = parse_varint(scan, &handler);
489 assert(handler >= 0 && handler < len);
490 int depth_and_lasti;
491 scan = parse_varint(scan, &depth_and_lasti);
492 int level = depth_and_lasti >> 1;
493 int lasti = depth_and_lasti & 1;
494 if (stacks[start_offset] != UNINITIALIZED) {
495 if (stacks[handler] == UNINITIALIZED) {
496 todo = 1;
497 uint64_t target_stack = pop_to_level(stacks[start_offset], level);
498 if (lasti) {
499 target_stack = push_value(target_stack, Lasti);
500 }
501 target_stack = push_value(target_stack, Except);
502 stacks[handler] = target_stack;
503 }
504 }
505 }
506 }
507 Py_DECREF(co_code);
508 return stacks;
509 }
510
511 static int
512 compatible_stack(int64_t from_stack, int64_t to_stack)
513 {
514 if (from_stack < 0 || to_stack < 0) {
515 return 0;
516 }
517 while(from_stack > to_stack) {
518 from_stack = pop_value(from_stack);
519 }
520 while(from_stack) {
521 Kind from_top = top_of_stack(from_stack);
522 Kind to_top = top_of_stack(to_stack);
523 if (!compatible_kind(from_top, to_top)) {
524 return 0;
525 }
526 from_stack = pop_value(from_stack);
527 to_stack = pop_value(to_stack);
528 }
529 return to_stack == 0;
530 }
531
532 static const char *
533 explain_incompatible_stack(int64_t to_stack)
534 {
535 assert(to_stack != 0);
536 if (to_stack == OVERFLOWED) {
537 return "stack is too deep to analyze";
538 }
539 if (to_stack == UNINITIALIZED) {
540 return "can't jump into an exception handler, or code may be unreachable";
541 }
542 Kind target_kind = top_of_stack(to_stack);
543 switch(target_kind) {
544 case Except:
545 return "can't jump into an 'except' block as there's no exception";
546 case Lasti:
547 return "can't jump into a re-raising block as there's no location";
548 case Object:
549 case Null:
550 return "incompatible stacks";
551 case Iterator:
552 return "can't jump into the body of a for loop";
553 default:
554 Py_UNREACHABLE();
555 }
556 }
557
558 static int *
559 marklines(PyCodeObject *code, int len)
560 {
561 PyCodeAddressRange bounds;
562 _PyCode_InitAddressRange(code, &bounds);
563 assert (bounds.ar_end == 0);
564 int last_line = -1;
565
566 int *linestarts = PyMem_New(int, len);
567 if (linestarts == NULL) {
568 return NULL;
569 }
570 for (int i = 0; i < len; i++) {
571 linestarts[i] = -1;
572 }
573
574 while (_PyLineTable_NextAddressRange(&bounds)) {
575 assert(bounds.ar_start / (int)sizeof(_Py_CODEUNIT) < len);
576 if (bounds.ar_line != last_line && bounds.ar_line != -1) {
577 linestarts[bounds.ar_start / sizeof(_Py_CODEUNIT)] = bounds.ar_line;
578 last_line = bounds.ar_line;
579 }
580 }
581 return linestarts;
582 }
583
584 static int
585 first_line_not_before(int *lines, int len, int line)
586 {
587 int result = INT_MAX;
588 for (int i = 0; i < len; i++) {
589 if (lines[i] < result && lines[i] >= line) {
590 result = lines[i];
591 }
592 }
593 if (result == INT_MAX) {
594 return -1;
595 }
596 return result;
597 }
598
599 static PyFrameState
600 _PyFrame_GetState(PyFrameObject *frame)
601 {
602 assert(!_PyFrame_IsIncomplete(frame->f_frame));
603 if (frame->f_frame->stacktop == 0) {
604 return FRAME_CLEARED;
605 }
606 switch(frame->f_frame->owner) {
607 case FRAME_OWNED_BY_GENERATOR:
608 {
609 PyGenObject *gen = _PyFrame_GetGenerator(frame->f_frame);
610 return gen->gi_frame_state;
611 }
612 case FRAME_OWNED_BY_THREAD:
613 {
614 if (_PyInterpreterFrame_LASTI(frame->f_frame) < 0) {
615 return FRAME_CREATED;
616 }
617 switch (frame->f_frame->prev_instr->op.code)
618 {
619 case COPY_FREE_VARS:
620 case MAKE_CELL:
621 case RETURN_GENERATOR:
622 /* Frame not fully initialized */
623 return FRAME_CREATED;
624 default:
625 return FRAME_EXECUTING;
626 }
627 }
628 case FRAME_OWNED_BY_FRAME_OBJECT:
629 return FRAME_COMPLETED;
630 }
631 Py_UNREACHABLE();
632 }
633
634 /* Setter for f_lineno - you can set f_lineno from within a trace function in
635 * order to jump to a given line of code, subject to some restrictions. Most
636 * lines are OK to jump to because they don't make any assumptions about the
637 * state of the stack (obvious because you could remove the line and the code
638 * would still work without any stack errors), but there are some constructs
639 * that limit jumping:
640 *
641 * o Any exception handlers.
642 * o 'for' and 'async for' loops can't be jumped into because the
643 * iterator needs to be on the stack.
644 * o Jumps cannot be made from within a trace function invoked with a
645 * 'return' or 'exception' event since the eval loop has been exited at
646 * that time.
647 */
648 static int
649 frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignored))
650 {
651 if (p_new_lineno == NULL) {
652 PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
653 return -1;
654 }
655 /* f_lineno must be an integer. */
656 if (!PyLong_CheckExact(p_new_lineno)) {
657 PyErr_SetString(PyExc_ValueError,
658 "lineno must be an integer");
659 return -1;
660 }
661
662 PyFrameState state = _PyFrame_GetState(f);
663 /*
664 * This code preserves the historical restrictions on
665 * setting the line number of a frame.
666 * Jumps are forbidden on a 'return' trace event (except after a yield).
667 * Jumps from 'call' trace events are also forbidden.
668 * In addition, jumps are forbidden when not tracing,
669 * as this is a debugging feature.
670 */
671 int what_event = PyThreadState_GET()->what_event;
672 if (what_event < 0) {
673 PyErr_Format(PyExc_ValueError,
674 "f_lineno can only be set in a trace function");
675 return -1;
676 }
677 switch (what_event) {
678 case PY_MONITORING_EVENT_PY_RESUME:
679 case PY_MONITORING_EVENT_JUMP:
680 case PY_MONITORING_EVENT_BRANCH:
681 case PY_MONITORING_EVENT_LINE:
682 case PY_MONITORING_EVENT_PY_YIELD:
683 /* Setting f_lineno is allowed for the above events */
684 break;
685 case PY_MONITORING_EVENT_PY_START:
686 PyErr_Format(PyExc_ValueError,
687 "can't jump from the 'call' trace event of a new frame");
688 return -1;
689 case PY_MONITORING_EVENT_CALL:
690 case PY_MONITORING_EVENT_C_RETURN:
691 PyErr_SetString(PyExc_ValueError,
692 "can't jump during a call");
693 return -1;
694 case PY_MONITORING_EVENT_PY_RETURN:
695 case PY_MONITORING_EVENT_PY_UNWIND:
696 case PY_MONITORING_EVENT_PY_THROW:
697 case PY_MONITORING_EVENT_RAISE:
698 case PY_MONITORING_EVENT_C_RAISE:
699 case PY_MONITORING_EVENT_INSTRUCTION:
700 case PY_MONITORING_EVENT_EXCEPTION_HANDLED:
701 PyErr_Format(PyExc_ValueError,
702 "can only jump from a 'line' trace event");
703 return -1;
704 default:
705 PyErr_SetString(PyExc_SystemError,
706 "unexpected event type");
707 return -1;
708 }
709
710 int new_lineno;
711
712 /* Fail if the line falls outside the code block and
713 select first line with actual code. */
714 int overflow;
715 long l_new_lineno = PyLong_AsLongAndOverflow(p_new_lineno, &overflow);
716 if (overflow
717 #if SIZEOF_LONG > SIZEOF_INT
718 || l_new_lineno > INT_MAX
719 || l_new_lineno < INT_MIN
720 #endif
721 ) {
722 PyErr_SetString(PyExc_ValueError,
723 "lineno out of range");
724 return -1;
725 }
726 new_lineno = (int)l_new_lineno;
727
728 if (new_lineno < f->f_frame->f_code->co_firstlineno) {
729 PyErr_Format(PyExc_ValueError,
730 "line %d comes before the current code block",
731 new_lineno);
732 return -1;
733 }
734
735 /* PyCode_NewWithPosOnlyArgs limits co_code to be under INT_MAX so this
736 * should never overflow. */
737 int len = (int)Py_SIZE(f->f_frame->f_code);
738 int *lines = marklines(f->f_frame->f_code, len);
739 if (lines == NULL) {
740 return -1;
741 }
742
743 new_lineno = first_line_not_before(lines, len, new_lineno);
744 if (new_lineno < 0) {
745 PyErr_Format(PyExc_ValueError,
746 "line %d comes after the current code block",
747 (int)l_new_lineno);
748 PyMem_Free(lines);
749 return -1;
750 }
751
752 int64_t *stacks = mark_stacks(f->f_frame->f_code, len);
753 if (stacks == NULL) {
754 PyMem_Free(lines);
755 return -1;
756 }
757
758 int64_t best_stack = OVERFLOWED;
759 int best_addr = -1;
760 int64_t start_stack = stacks[_PyInterpreterFrame_LASTI(f->f_frame)];
761 int err = -1;
762 const char *msg = "cannot find bytecode for specified line";
763 for (int i = 0; i < len; i++) {
764 if (lines[i] == new_lineno) {
765 int64_t target_stack = stacks[i];
766 if (compatible_stack(start_stack, target_stack)) {
767 err = 0;
768 if (target_stack > best_stack) {
769 best_stack = target_stack;
770 best_addr = i;
771 }
772 }
773 else if (err < 0) {
774 if (start_stack == OVERFLOWED) {
775 msg = "stack to deep to analyze";
776 }
777 else if (start_stack == UNINITIALIZED) {
778 msg = "can't jump from unreachable code";
779 }
780 else {
781 msg = explain_incompatible_stack(target_stack);
782 err = 1;
783 }
784 }
785 }
786 }
787 PyMem_Free(stacks);
788 PyMem_Free(lines);
789 if (err) {
790 PyErr_SetString(PyExc_ValueError, msg);
791 return -1;
792 }
793 // Populate any NULL locals that the compiler might have "proven" to exist
794 // in the new location. Rather than crashing or changing co_code, just bind
795 // None instead:
796 int unbound = 0;
797 for (int i = 0; i < f->f_frame->f_code->co_nlocalsplus; i++) {
798 // Counting every unbound local is overly-cautious, but a full flow
799 // analysis (like we do in the compiler) is probably too expensive:
800 unbound += f->f_frame->localsplus[i] == NULL;
801 }
802 if (unbound) {
803 const char *e = "assigning None to %d unbound local%s";
804 const char *s = (unbound == 1) ? "" : "s";
805 if (PyErr_WarnFormat(PyExc_RuntimeWarning, 0, e, unbound, s)) {
806 return -1;
807 }
808 // Do this in a second pass to avoid writing a bunch of Nones when
809 // warnings are being treated as errors and the previous bit raises:
810 for (int i = 0; i < f->f_frame->f_code->co_nlocalsplus; i++) {
811 if (f->f_frame->localsplus[i] == NULL) {
812 f->f_frame->localsplus[i] = Py_NewRef(Py_None);
813 unbound--;
814 }
815 }
816 assert(unbound == 0);
817 }
818 if (state == FRAME_SUSPENDED) {
819 /* Account for value popped by yield */
820 start_stack = pop_value(start_stack);
821 }
822 while (start_stack > best_stack) {
823 if (top_of_stack(start_stack) == Except) {
824 /* Pop exception stack as well as the evaluation stack */
825 PyThreadState *tstate = _PyThreadState_GET();
826 _PyErr_StackItem *exc_info = tstate->exc_info;
827 PyObject *value = exc_info->exc_value;
828 PyObject *exc = _PyFrame_StackPop(f->f_frame);
829 assert(PyExceptionInstance_Check(exc) || exc == Py_None);
830 exc_info->exc_value = exc;
831 Py_XDECREF(value);
832 }
833 else {
834 PyObject *v = _PyFrame_StackPop(f->f_frame);
835 Py_XDECREF(v);
836 }
837 start_stack = pop_value(start_stack);
838 }
839 /* Finally set the new lasti and return OK. */
840 f->f_lineno = 0;
841 f->f_frame->prev_instr = _PyCode_CODE(f->f_frame->f_code) + best_addr;
842 return 0;
843 }
844
845 static PyObject *
846 frame_gettrace(PyFrameObject *f, void *closure)
847 {
848 PyObject* trace = f->f_trace;
849 if (trace == NULL)
850 trace = Py_None;
851 return Py_NewRef(trace);
852 }
853
854 static int
855 frame_settrace(PyFrameObject *f, PyObject* v, void *closure)
856 {
857 if (v == Py_None) {
858 v = NULL;
859 }
860 if (v != f->f_trace) {
861 Py_XSETREF(f->f_trace, Py_XNewRef(v));
862 }
863 return 0;
864 }
865
866
867 static PyGetSetDef frame_getsetlist[] = {
868 {"f_back", (getter)frame_getback, NULL, NULL},
869 {"f_locals", (getter)frame_getlocals, NULL, NULL},
870 {"f_lineno", (getter)frame_getlineno,
871 (setter)frame_setlineno, NULL},
872 {"f_trace", (getter)frame_gettrace, (setter)frame_settrace, NULL},
873 {"f_lasti", (getter)frame_getlasti, NULL, NULL},
874 {"f_globals", (getter)frame_getglobals, NULL, NULL},
875 {"f_builtins", (getter)frame_getbuiltins, NULL, NULL},
876 {"f_code", (getter)frame_getcode, NULL, NULL},
877 {"f_trace_opcodes", (getter)frame_gettrace_opcodes, (setter)frame_settrace_opcodes, NULL},
878 {0}
879 };
880
881 static void
882 frame_dealloc(PyFrameObject *f)
883 {
884 /* It is the responsibility of the owning generator/coroutine
885 * to have cleared the generator pointer */
886
887 if (_PyObject_GC_IS_TRACKED(f)) {
888 _PyObject_GC_UNTRACK(f);
889 }
890
891 Py_TRASHCAN_BEGIN(f, frame_dealloc);
892 PyCodeObject *co = NULL;
893
894 /* GH-106092: If f->f_frame was on the stack and we reached the maximum
895 * nesting depth for deallocations, the trashcan may have delayed this
896 * deallocation until after f->f_frame is freed. Avoid dereferencing
897 * f->f_frame unless we know it still points to valid memory. */
898 _PyInterpreterFrame *frame = (_PyInterpreterFrame *)f->_f_frame_data;
899
900 /* Kill all local variables including specials, if we own them */
901 if (f->f_frame == frame && frame->owner == FRAME_OWNED_BY_FRAME_OBJECT) {
902 /* Don't clear code object until the end */
903 co = frame->f_code;
904 frame->f_code = NULL;
905 Py_CLEAR(frame->f_funcobj);
906 Py_CLEAR(frame->f_locals);
907 PyObject **locals = _PyFrame_GetLocalsArray(frame);
908 for (int i = 0; i < frame->stacktop; i++) {
909 Py_CLEAR(locals[i]);
910 }
911 }
912 Py_CLEAR(f->f_back);
913 Py_CLEAR(f->f_trace);
914 PyObject_GC_Del(f);
915 Py_XDECREF(co);
916 Py_TRASHCAN_END;
917 }
918
919 static int
920 frame_traverse(PyFrameObject *f, visitproc visit, void *arg)
921 {
922 Py_VISIT(f->f_back);
923 Py_VISIT(f->f_trace);
924 if (f->f_frame->owner != FRAME_OWNED_BY_FRAME_OBJECT) {
925 return 0;
926 }
927 assert(f->f_frame->frame_obj == NULL);
928 return _PyFrame_Traverse(f->f_frame, visit, arg);
929 }
930
931 static int
932 frame_tp_clear(PyFrameObject *f)
933 {
934 Py_CLEAR(f->f_trace);
935
936 /* locals and stack */
937 PyObject **locals = _PyFrame_GetLocalsArray(f->f_frame);
938 assert(f->f_frame->stacktop >= 0);
939 for (int i = 0; i < f->f_frame->stacktop; i++) {
940 Py_CLEAR(locals[i]);
941 }
942 f->f_frame->stacktop = 0;
943 return 0;
944 }
945
946 static PyObject *
947 frame_clear(PyFrameObject *f, PyObject *Py_UNUSED(ignored))
948 {
949 if (f->f_frame->owner == FRAME_OWNED_BY_GENERATOR) {
950 PyGenObject *gen = _PyFrame_GetGenerator(f->f_frame);
951 if (gen->gi_frame_state == FRAME_EXECUTING) {
952 goto running;
953 }
954 _PyGen_Finalize((PyObject *)gen);
955 }
956 else if (f->f_frame->owner == FRAME_OWNED_BY_THREAD) {
957 goto running;
958 }
959 else {
960 assert(f->f_frame->owner == FRAME_OWNED_BY_FRAME_OBJECT);
961 (void)frame_tp_clear(f);
962 }
963 Py_RETURN_NONE;
964 running:
965 PyErr_SetString(PyExc_RuntimeError,
966 "cannot clear an executing frame");
967 return NULL;
968 }
969
970 PyDoc_STRVAR(clear__doc__,
971 "F.clear(): clear most references held by the frame");
972
973 static PyObject *
974 frame_sizeof(PyFrameObject *f, PyObject *Py_UNUSED(ignored))
975 {
976 Py_ssize_t res;
977 res = offsetof(PyFrameObject, _f_frame_data) + offsetof(_PyInterpreterFrame, localsplus);
978 PyCodeObject *code = f->f_frame->f_code;
979 res += _PyFrame_NumSlotsForCodeObject(code) * sizeof(PyObject *);
980 return PyLong_FromSsize_t(res);
981 }
982
983 PyDoc_STRVAR(sizeof__doc__,
984 "F.__sizeof__() -> size of F in memory, in bytes");
985
986 static PyObject *
987 frame_repr(PyFrameObject *f)
988 {
989 int lineno = PyFrame_GetLineNumber(f);
990 PyCodeObject *code = f->f_frame->f_code;
991 return PyUnicode_FromFormat(
992 "<frame at %p, file %R, line %d, code %S>",
993 f, code->co_filename, lineno, code->co_name);
994 }
995
996 static PyMethodDef frame_methods[] = {
997 {"clear", (PyCFunction)frame_clear, METH_NOARGS,
998 clear__doc__},
999 {"__sizeof__", (PyCFunction)frame_sizeof, METH_NOARGS,
1000 sizeof__doc__},
1001 {NULL, NULL} /* sentinel */
1002 };
1003
1004 PyTypeObject PyFrame_Type = {
1005 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1006 "frame",
1007 offsetof(PyFrameObject, _f_frame_data) +
1008 offsetof(_PyInterpreterFrame, localsplus),
1009 sizeof(PyObject *),
1010 (destructor)frame_dealloc, /* tp_dealloc */
1011 0, /* tp_vectorcall_offset */
1012 0, /* tp_getattr */
1013 0, /* tp_setattr */
1014 0, /* tp_as_async */
1015 (reprfunc)frame_repr, /* tp_repr */
1016 0, /* tp_as_number */
1017 0, /* tp_as_sequence */
1018 0, /* tp_as_mapping */
1019 0, /* tp_hash */
1020 0, /* tp_call */
1021 0, /* tp_str */
1022 PyObject_GenericGetAttr, /* tp_getattro */
1023 PyObject_GenericSetAttr, /* tp_setattro */
1024 0, /* tp_as_buffer */
1025 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1026 0, /* tp_doc */
1027 (traverseproc)frame_traverse, /* tp_traverse */
1028 (inquiry)frame_tp_clear, /* tp_clear */
1029 0, /* tp_richcompare */
1030 0, /* tp_weaklistoffset */
1031 0, /* tp_iter */
1032 0, /* tp_iternext */
1033 frame_methods, /* tp_methods */
1034 frame_memberlist, /* tp_members */
1035 frame_getsetlist, /* tp_getset */
1036 0, /* tp_base */
1037 0, /* tp_dict */
1038 };
1039
1040 static void
1041 init_frame(_PyInterpreterFrame *frame, PyFunctionObject *func, PyObject *locals)
1042 {
1043 PyCodeObject *code = (PyCodeObject *)func->func_code;
1044 _PyFrame_Initialize(frame, (PyFunctionObject*)Py_NewRef(func),
1045 Py_XNewRef(locals), code, 0);
1046 frame->previous = NULL;
1047 }
1048
1049 PyFrameObject*
1050 _PyFrame_New_NoTrack(PyCodeObject *code)
1051 {
1052 CALL_STAT_INC(frame_objects_created);
1053 int slots = code->co_nlocalsplus + code->co_stacksize;
1054 PyFrameObject *f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type, slots);
1055 if (f == NULL) {
1056 return NULL;
1057 }
1058 f->f_back = NULL;
1059 f->f_trace = NULL;
1060 f->f_trace_lines = 1;
1061 f->f_trace_opcodes = 0;
1062 f->f_fast_as_locals = 0;
1063 f->f_lineno = 0;
1064 return f;
1065 }
1066
1067 /* Legacy API */
1068 PyFrameObject*
1069 PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
1070 PyObject *globals, PyObject *locals)
1071 {
1072 PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
1073 if (builtins == NULL) {
1074 return NULL;
1075 }
1076 PyFrameConstructor desc = {
1077 .fc_globals = globals,
1078 .fc_builtins = builtins,
1079 .fc_name = code->co_name,
1080 .fc_qualname = code->co_name,
1081 .fc_code = (PyObject *)code,
1082 .fc_defaults = NULL,
1083 .fc_kwdefaults = NULL,
1084 .fc_closure = NULL
1085 };
1086 PyFunctionObject *func = _PyFunction_FromConstructor(&desc);
1087 if (func == NULL) {
1088 return NULL;
1089 }
1090 PyFrameObject *f = _PyFrame_New_NoTrack(code);
1091 if (f == NULL) {
1092 Py_DECREF(func);
1093 return NULL;
1094 }
1095 init_frame((_PyInterpreterFrame *)f->_f_frame_data, func, locals);
1096 f->f_frame = (_PyInterpreterFrame *)f->_f_frame_data;
1097 f->f_frame->owner = FRAME_OWNED_BY_FRAME_OBJECT;
1098 // This frame needs to be "complete", so pretend that the first RESUME ran:
1099 f->f_frame->prev_instr = _PyCode_CODE(code) + code->_co_firsttraceable;
1100 assert(!_PyFrame_IsIncomplete(f->f_frame));
1101 Py_DECREF(func);
1102 _PyObject_GC_TRACK(f);
1103 return f;
1104 }
1105
1106 static int
1107 _PyFrame_OpAlreadyRan(_PyInterpreterFrame *frame, int opcode, int oparg)
1108 {
1109 // This only works when opcode is a non-quickened form:
1110 assert(_PyOpcode_Deopt[opcode] == opcode);
1111 int check_oparg = 0;
1112 for (_Py_CODEUNIT *instruction = _PyCode_CODE(frame->f_code);
1113 instruction < frame->prev_instr; instruction++)
1114 {
1115 int check_opcode = _PyOpcode_Deopt[instruction->op.code];
1116 check_oparg |= instruction->op.arg;
1117 if (check_opcode == opcode && check_oparg == oparg) {
1118 return 1;
1119 }
1120 if (check_opcode == EXTENDED_ARG) {
1121 check_oparg <<= 8;
1122 }
1123 else {
1124 check_oparg = 0;
1125 }
1126 instruction += _PyOpcode_Caches[check_opcode];
1127 }
1128 return 0;
1129 }
1130
1131
1132 // Initialize frame free variables if needed
1133 static void
1134 frame_init_get_vars(_PyInterpreterFrame *frame)
1135 {
1136 // COPY_FREE_VARS has no quickened forms, so no need to use _PyOpcode_Deopt
1137 // here:
1138 PyCodeObject *co = frame->f_code;
1139 int lasti = _PyInterpreterFrame_LASTI(frame);
1140 if (!(lasti < 0 && _PyCode_CODE(co)->op.code == COPY_FREE_VARS
1141 && PyFunction_Check(frame->f_funcobj)))
1142 {
1143 /* Free vars are initialized */
1144 return;
1145 }
1146
1147 /* Free vars have not been initialized -- Do that */
1148 PyObject *closure = ((PyFunctionObject *)frame->f_funcobj)->func_closure;
1149 int offset = PyCode_GetFirstFree(co);
1150 for (int i = 0; i < co->co_nfreevars; ++i) {
1151 PyObject *o = PyTuple_GET_ITEM(closure, i);
1152 frame->localsplus[offset + i] = Py_NewRef(o);
1153 }
1154 // COPY_FREE_VARS doesn't have inline CACHEs, either:
1155 frame->prev_instr = _PyCode_CODE(frame->f_code);
1156 }
1157
1158
1159 static int
1160 frame_get_var(_PyInterpreterFrame *frame, PyCodeObject *co, int i,
1161 PyObject **pvalue)
1162 {
1163 _PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i);
1164
1165 /* If the namespace is unoptimized, then one of the
1166 following cases applies:
1167 1. It does not contain free variables, because it
1168 uses import * or is a top-level namespace.
1169 2. It is a class namespace.
1170 We don't want to accidentally copy free variables
1171 into the locals dict used by the class.
1172 */
1173 if (kind & CO_FAST_FREE && !(co->co_flags & CO_OPTIMIZED)) {
1174 return 0;
1175 }
1176
1177 PyObject *value = frame->localsplus[i];
1178 if (frame->stacktop) {
1179 if (kind & CO_FAST_FREE) {
1180 // The cell was set by COPY_FREE_VARS.
1181 assert(value != NULL && PyCell_Check(value));
1182 value = PyCell_GET(value);
1183 }
1184 else if (kind & CO_FAST_CELL) {
1185 // Note that no *_DEREF ops can happen before MAKE_CELL
1186 // executes. So there's no need to duplicate the work
1187 // that MAKE_CELL would otherwise do later, if it hasn't
1188 // run yet.
1189 if (value != NULL) {
1190 if (PyCell_Check(value) &&
1191 _PyFrame_OpAlreadyRan(frame, MAKE_CELL, i)) {
1192 // (likely) MAKE_CELL must have executed already.
1193 value = PyCell_GET(value);
1194 }
1195 // (likely) Otherwise it it is an arg (kind & CO_FAST_LOCAL),
1196 // with the initial value set when the frame was created...
1197 // (unlikely) ...or it was set to some initial value by
1198 // an earlier call to PyFrame_LocalsToFast().
1199 }
1200 }
1201 }
1202 else {
1203 assert(value == NULL);
1204 }
1205 *pvalue = value;
1206 return 1;
1207 }
1208
1209
1210 PyObject *
1211 _PyFrame_GetLocals(_PyInterpreterFrame *frame, int include_hidden)
1212 {
1213 /* Merge fast locals into f->f_locals */
1214 PyObject *locals = frame->f_locals;
1215 if (locals == NULL) {
1216 locals = frame->f_locals = PyDict_New();
1217 if (locals == NULL) {
1218 return NULL;
1219 }
1220 }
1221 PyObject *hidden = NULL;
1222
1223 /* If include_hidden, "hidden" fast locals (from inlined comprehensions in
1224 module/class scopes) will be included in the returned dict, but not in
1225 frame->f_locals; the returned dict will be a modified copy. Non-hidden
1226 locals will still be updated in frame->f_locals. */
1227 if (include_hidden) {
1228 hidden = PyDict_New();
1229 if (hidden == NULL) {
1230 return NULL;
1231 }
1232 }
1233
1234 frame_init_get_vars(frame);
1235
1236 PyCodeObject *co = frame->f_code;
1237 for (int i = 0; i < co->co_nlocalsplus; i++) {
1238 PyObject *value; // borrowed reference
1239 if (!frame_get_var(frame, co, i, &value)) {
1240 continue;
1241 }
1242
1243 PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
1244 _PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i);
1245 if (kind & CO_FAST_HIDDEN) {
1246 if (include_hidden && value != NULL) {
1247 if (PyObject_SetItem(hidden, name, value) != 0) {
1248 goto error;
1249 }
1250 }
1251 continue;
1252 }
1253 if (value == NULL) {
1254 if (PyObject_DelItem(locals, name) != 0) {
1255 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1256 PyErr_Clear();
1257 }
1258 else {
1259 goto error;
1260 }
1261 }
1262 }
1263 else {
1264 if (PyObject_SetItem(locals, name, value) != 0) {
1265 goto error;
1266 }
1267 }
1268 }
1269
1270 if (include_hidden && PyDict_Size(hidden)) {
1271 PyObject *innerlocals = PyDict_New();
1272 if (innerlocals == NULL) {
1273 goto error;
1274 }
1275 if (PyDict_Merge(innerlocals, locals, 1) != 0) {
1276 Py_DECREF(innerlocals);
1277 goto error;
1278 }
1279 if (PyDict_Merge(innerlocals, hidden, 1) != 0) {
1280 Py_DECREF(innerlocals);
1281 goto error;
1282 }
1283 locals = innerlocals;
1284 }
1285 else {
1286 Py_INCREF(locals);
1287 }
1288 Py_CLEAR(hidden);
1289
1290 return locals;
1291
1292 error:
1293 Py_XDECREF(hidden);
1294 return NULL;
1295 }
1296
1297
1298 int
1299 _PyFrame_FastToLocalsWithError(_PyInterpreterFrame *frame)
1300 {
1301 PyObject *locals = _PyFrame_GetLocals(frame, 0);
1302 if (locals == NULL) {
1303 return -1;
1304 }
1305 Py_DECREF(locals);
1306 return 0;
1307 }
1308
1309
1310 PyObject *
1311 PyFrame_GetVar(PyFrameObject *frame_obj, PyObject *name)
1312 {
1313 if (!PyUnicode_Check(name)) {
1314 PyErr_Format(PyExc_TypeError, "name must be str, not %s",
1315 Py_TYPE(name)->tp_name);
1316 return NULL;
1317 }
1318
1319 _PyInterpreterFrame *frame = frame_obj->f_frame;
1320 frame_init_get_vars(frame);
1321
1322 PyCodeObject *co = frame->f_code;
1323 for (int i = 0; i < co->co_nlocalsplus; i++) {
1324 PyObject *var_name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
1325 if (!_PyUnicode_Equal(var_name, name)) {
1326 continue;
1327 }
1328
1329 PyObject *value; // borrowed reference
1330 if (!frame_get_var(frame, co, i, &value)) {
1331 break;
1332 }
1333 if (value == NULL) {
1334 break;
1335 }
1336 return Py_NewRef(value);
1337 }
1338
1339 PyErr_Format(PyExc_NameError, "variable %R does not exist", name);
1340 return NULL;
1341 }
1342
1343
1344 PyObject *
1345 PyFrame_GetVarString(PyFrameObject *frame, const char *name)
1346 {
1347 PyObject *name_obj = PyUnicode_FromString(name);
1348 if (name_obj == NULL) {
1349 return NULL;
1350 }
1351 PyObject *value = PyFrame_GetVar(frame, name_obj);
1352 Py_DECREF(name_obj);
1353 return value;
1354 }
1355
1356
1357 int
1358 PyFrame_FastToLocalsWithError(PyFrameObject *f)
1359 {
1360 if (f == NULL) {
1361 PyErr_BadInternalCall();
1362 return -1;
1363 }
1364 assert(!_PyFrame_IsIncomplete(f->f_frame));
1365 int err = _PyFrame_FastToLocalsWithError(f->f_frame);
1366 if (err == 0) {
1367 f->f_fast_as_locals = 1;
1368 }
1369 return err;
1370 }
1371
1372 void
1373 PyFrame_FastToLocals(PyFrameObject *f)
1374 {
1375 int res;
1376 assert(!_PyFrame_IsIncomplete(f->f_frame));
1377 assert(!PyErr_Occurred());
1378
1379 res = PyFrame_FastToLocalsWithError(f);
1380 if (res < 0)
1381 PyErr_Clear();
1382 }
1383
1384 void
1385 _PyFrame_LocalsToFast(_PyInterpreterFrame *frame, int clear)
1386 {
1387 /* Merge locals into fast locals */
1388 PyObject *locals;
1389 PyObject **fast;
1390 PyCodeObject *co;
1391 locals = frame->f_locals;
1392 if (locals == NULL) {
1393 return;
1394 }
1395 fast = _PyFrame_GetLocalsArray(frame);
1396 co = frame->f_code;
1397
1398 PyObject *exc = PyErr_GetRaisedException();
1399 for (int i = 0; i < co->co_nlocalsplus; i++) {
1400 _PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i);
1401
1402 /* Same test as in PyFrame_FastToLocals() above. */
1403 if (kind & CO_FAST_FREE && !(co->co_flags & CO_OPTIMIZED)) {
1404 continue;
1405 }
1406 PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
1407 PyObject *value = PyObject_GetItem(locals, name);
1408 /* We only care about NULLs if clear is true. */
1409 if (value == NULL) {
1410 PyErr_Clear();
1411 if (!clear) {
1412 continue;
1413 }
1414 }
1415 PyObject *oldvalue = fast[i];
1416 PyObject *cell = NULL;
1417 if (kind == CO_FAST_FREE) {
1418 // The cell was set when the frame was created from
1419 // the function's closure.
1420 assert(oldvalue != NULL && PyCell_Check(oldvalue));
1421 cell = oldvalue;
1422 }
1423 else if (kind & CO_FAST_CELL && oldvalue != NULL) {
1424 /* Same test as in PyFrame_FastToLocals() above. */
1425 if (PyCell_Check(oldvalue) &&
1426 _PyFrame_OpAlreadyRan(frame, MAKE_CELL, i)) {
1427 // (likely) MAKE_CELL must have executed already.
1428 cell = oldvalue;
1429 }
1430 // (unlikely) Otherwise, it must have been set to some
1431 // initial value by an earlier call to PyFrame_LocalsToFast().
1432 }
1433 if (cell != NULL) {
1434 oldvalue = PyCell_GET(cell);
1435 if (value != oldvalue) {
1436 PyCell_SET(cell, Py_XNewRef(value));
1437 Py_XDECREF(oldvalue);
1438 }
1439 }
1440 else if (value != oldvalue) {
1441 if (value == NULL) {
1442 // Probably can't delete this, since the compiler's flow
1443 // analysis may have already "proven" that it exists here:
1444 const char *e = "assigning None to unbound local %R";
1445 if (PyErr_WarnFormat(PyExc_RuntimeWarning, 0, e, name)) {
1446 // It's okay if frame_obj is NULL, just try anyways:
1447 PyErr_WriteUnraisable((PyObject *)frame->frame_obj);
1448 }
1449 value = Py_NewRef(Py_None);
1450 }
1451 Py_XSETREF(fast[i], Py_NewRef(value));
1452 }
1453 Py_XDECREF(value);
1454 }
1455 PyErr_SetRaisedException(exc);
1456 }
1457
1458 void
1459 PyFrame_LocalsToFast(PyFrameObject *f, int clear)
1460 {
1461 assert(!_PyFrame_IsIncomplete(f->f_frame));
1462 if (f && f->f_fast_as_locals && _PyFrame_GetState(f) != FRAME_CLEARED) {
1463 _PyFrame_LocalsToFast(f->f_frame, clear);
1464 f->f_fast_as_locals = 0;
1465 }
1466 }
1467
1468 int
1469 _PyFrame_IsEntryFrame(PyFrameObject *frame)
1470 {
1471 assert(frame != NULL);
1472 _PyInterpreterFrame *f = frame->f_frame;
1473 assert(!_PyFrame_IsIncomplete(f));
1474 return f->previous && f->previous->owner == FRAME_OWNED_BY_CSTACK;
1475 }
1476
1477 PyCodeObject *
1478 PyFrame_GetCode(PyFrameObject *frame)
1479 {
1480 assert(frame != NULL);
1481 assert(!_PyFrame_IsIncomplete(frame->f_frame));
1482 PyCodeObject *code = frame->f_frame->f_code;
1483 assert(code != NULL);
1484 return (PyCodeObject*)Py_NewRef(code);
1485 }
1486
1487
1488 PyFrameObject*
1489 PyFrame_GetBack(PyFrameObject *frame)
1490 {
1491 assert(frame != NULL);
1492 assert(!_PyFrame_IsIncomplete(frame->f_frame));
1493 PyFrameObject *back = frame->f_back;
1494 if (back == NULL) {
1495 _PyInterpreterFrame *prev = frame->f_frame->previous;
1496 prev = _PyFrame_GetFirstComplete(prev);
1497 if (prev) {
1498 back = _PyFrame_GetFrameObject(prev);
1499 }
1500 }
1501 return (PyFrameObject*)Py_XNewRef(back);
1502 }
1503
1504 PyObject*
1505 PyFrame_GetLocals(PyFrameObject *frame)
1506 {
1507 assert(!_PyFrame_IsIncomplete(frame->f_frame));
1508 return frame_getlocals(frame, NULL);
1509 }
1510
1511 PyObject*
1512 PyFrame_GetGlobals(PyFrameObject *frame)
1513 {
1514 assert(!_PyFrame_IsIncomplete(frame->f_frame));
1515 return frame_getglobals(frame, NULL);
1516 }
1517
1518 PyObject*
1519 PyFrame_GetBuiltins(PyFrameObject *frame)
1520 {
1521 assert(!_PyFrame_IsIncomplete(frame->f_frame));
1522 return frame_getbuiltins(frame, NULL);
1523 }
1524
1525 int
1526 PyFrame_GetLasti(PyFrameObject *frame)
1527 {
1528 assert(!_PyFrame_IsIncomplete(frame->f_frame));
1529 int lasti = _PyInterpreterFrame_LASTI(frame->f_frame);
1530 if (lasti < 0) {
1531 return -1;
1532 }
1533 return lasti * sizeof(_Py_CODEUNIT);
1534 }
1535
1536 PyObject *
1537 PyFrame_GetGenerator(PyFrameObject *frame)
1538 {
1539 assert(!_PyFrame_IsIncomplete(frame->f_frame));
1540 if (frame->f_frame->owner != FRAME_OWNED_BY_GENERATOR) {
1541 return NULL;
1542 }
1543 PyGenObject *gen = _PyFrame_GetGenerator(frame->f_frame);
1544 return Py_NewRef(gen);
1545 }
1546
1547 PyObject*
1548 _PyEval_BuiltinsFromGlobals(PyThreadState *tstate, PyObject *globals)
1549 {
1550 PyObject *builtins = PyDict_GetItemWithError(globals, &_Py_ID(__builtins__));
1551 if (builtins) {
1552 if (PyModule_Check(builtins)) {
1553 builtins = _PyModule_GetDict(builtins);
1554 assert(builtins != NULL);
1555 }
1556 return builtins;
1557 }
1558 if (PyErr_Occurred()) {
1559 return NULL;
1560 }
1561
1562 return _PyEval_GetBuiltins(tstate);
1563 }