• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_modsupport.h"    // _PyArg_CheckPositional()
9 #include "pycore_object.h"        // _PyObject_GC_UNTRACK()
10 #include "pycore_opcode_metadata.h" // _PyOpcode_Deopt, _PyOpcode_Caches
11 
12 
13 #include "frameobject.h"          // PyFrameObject
14 #include "pycore_frame.h"
15 #include "opcode.h"               // EXTENDED_ARG
16 
17 
18 #define OFF(x) offsetof(PyFrameObject, x)
19 
20 
21 // Returns borrowed reference or NULL
22 static PyObject *
framelocalsproxy_getval(_PyInterpreterFrame * frame,PyCodeObject * co,int i)23 framelocalsproxy_getval(_PyInterpreterFrame *frame, PyCodeObject *co, int i)
24 {
25     PyObject **fast = _PyFrame_GetLocalsArray(frame);
26     _PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i);
27 
28     PyObject *value = fast[i];
29     PyObject *cell = NULL;
30 
31     if (value == NULL) {
32         return NULL;
33     }
34 
35     if (kind == CO_FAST_FREE || kind & CO_FAST_CELL) {
36         // The cell was set when the frame was created from
37         // the function's closure.
38         assert(PyCell_Check(value));
39         cell = value;
40     }
41 
42     if (cell != NULL) {
43         value = PyCell_GET(cell);
44     }
45 
46     if (value == NULL) {
47         return NULL;
48     }
49 
50     return value;
51 }
52 
53 static int
framelocalsproxy_getkeyindex(PyFrameObject * frame,PyObject * key,bool read)54 framelocalsproxy_getkeyindex(PyFrameObject *frame, PyObject* key, bool read)
55 {
56     /*
57      * Returns -2 (!) if an error occurred; exception will be set.
58      * Returns the fast locals index of the key on success:
59      *   - if read == true, returns the index if the value is not NULL
60      *   - if read == false, returns the index if the value is not hidden
61      * Otherwise returns -1.
62      */
63 
64     PyCodeObject *co = _PyFrame_GetCode(frame->f_frame);
65 
66     // Ensure that the key is hashable.
67     Py_hash_t key_hash = PyObject_Hash(key);
68     if (key_hash == -1) {
69         return -2;
70     }
71     bool found = false;
72 
73     // We do 2 loops here because it's highly possible the key is interned
74     // and we can do a pointer comparison.
75     for (int i = 0; i < co->co_nlocalsplus; i++) {
76         PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
77         if (name == key) {
78             if (read) {
79                 if (framelocalsproxy_getval(frame->f_frame, co, i) != NULL) {
80                     return i;
81                 }
82             } else {
83                 if (!(_PyLocals_GetKind(co->co_localspluskinds, i) & CO_FAST_HIDDEN)) {
84                     return i;
85                 }
86             }
87             found = true;
88         }
89     }
90     if (found) {
91         // This is an attempt to read an unset local variable or
92         // write to a variable that is hidden from regular write operations
93         return -1;
94     }
95     // This is unlikely, but we need to make sure. This means the key
96     // is not interned.
97     for (int i = 0; i < co->co_nlocalsplus; i++) {
98         PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
99         Py_hash_t name_hash = PyObject_Hash(name);
100         assert(name_hash != -1);  // keys are exact unicode
101         if (name_hash != key_hash) {
102             continue;
103         }
104         int same = PyObject_RichCompareBool(name, key, Py_EQ);
105         if (same < 0) {
106             return -2;
107         }
108         if (same) {
109             if (read) {
110                 if (framelocalsproxy_getval(frame->f_frame, co, i) != NULL) {
111                     return i;
112                 }
113             } else {
114                 if (!(_PyLocals_GetKind(co->co_localspluskinds, i) & CO_FAST_HIDDEN)) {
115                     return i;
116                 }
117             }
118         }
119     }
120 
121     return -1;
122 }
123 
124 static PyObject *
framelocalsproxy_getitem(PyObject * self,PyObject * key)125 framelocalsproxy_getitem(PyObject *self, PyObject *key)
126 {
127     PyFrameObject* frame = ((PyFrameLocalsProxyObject*)self)->frame;
128     PyCodeObject* co = _PyFrame_GetCode(frame->f_frame);
129 
130     int i = framelocalsproxy_getkeyindex(frame, key, true);
131     if (i == -2) {
132         return NULL;
133     }
134     if (i >= 0) {
135         PyObject *value = framelocalsproxy_getval(frame->f_frame, co, i);
136         assert(value != NULL);
137         return Py_NewRef(value);
138     }
139 
140     // Okay not in the fast locals, try extra locals
141 
142     PyObject *extra = frame->f_extra_locals;
143     if (extra != NULL) {
144         PyObject *value = PyDict_GetItem(extra, key);
145         if (value != NULL) {
146             return Py_NewRef(value);
147         }
148     }
149 
150     PyErr_Format(PyExc_KeyError, "local variable '%R' is not defined", key);
151     return NULL;
152 }
153 
154 static int
framelocalsproxy_setitem(PyObject * self,PyObject * key,PyObject * value)155 framelocalsproxy_setitem(PyObject *self, PyObject *key, PyObject *value)
156 {
157     /* Merge locals into fast locals */
158     PyFrameObject* frame = ((PyFrameLocalsProxyObject*)self)->frame;
159     PyObject** fast = _PyFrame_GetLocalsArray(frame->f_frame);
160     PyCodeObject* co = _PyFrame_GetCode(frame->f_frame);
161 
162     int i = framelocalsproxy_getkeyindex(frame, key, false);
163     if (i == -2) {
164         return -1;
165     }
166     if (i >= 0) {
167         if (value == NULL) {
168             PyErr_SetString(PyExc_ValueError, "cannot remove local variables from FrameLocalsProxy");
169             return -1;
170         }
171 
172         _Py_Executors_InvalidateDependency(PyInterpreterState_Get(), co, 1);
173 
174         _PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i);
175         PyObject *oldvalue = fast[i];
176         PyObject *cell = NULL;
177         if (kind == CO_FAST_FREE) {
178             // The cell was set when the frame was created from
179             // the function's closure.
180             assert(oldvalue != NULL && PyCell_Check(oldvalue));
181             cell = oldvalue;
182         } else if (kind & CO_FAST_CELL && oldvalue != NULL) {
183             if (PyCell_Check(oldvalue)) {
184                 cell = oldvalue;
185             }
186         }
187         if (cell != NULL) {
188             oldvalue = PyCell_GET(cell);
189             if (value != oldvalue) {
190                 PyCell_SET(cell, Py_XNewRef(value));
191                 Py_XDECREF(oldvalue);
192             }
193         } else if (value != oldvalue) {
194             Py_XSETREF(fast[i], Py_NewRef(value));
195         }
196         return 0;
197     }
198 
199     // Okay not in the fast locals, try extra locals
200 
201     PyObject *extra = frame->f_extra_locals;
202 
203     if (extra == NULL) {
204         if (value == NULL) {
205             _PyErr_SetKeyError(key);
206             return -1;
207         }
208         extra = PyDict_New();
209         if (extra == NULL) {
210             return -1;
211         }
212         frame->f_extra_locals = extra;
213     }
214 
215     assert(PyDict_Check(extra));
216 
217     if (value == NULL) {
218         return PyDict_DelItem(extra, key);
219     } else {
220         return PyDict_SetItem(extra, key, value);
221     }
222 }
223 
224 static int
framelocalsproxy_merge(PyObject * self,PyObject * other)225 framelocalsproxy_merge(PyObject* self, PyObject* other)
226 {
227     if (!PyDict_Check(other) && !PyFrameLocalsProxy_Check(other)) {
228         return -1;
229     }
230 
231     PyObject *keys = PyMapping_Keys(other);
232     if (keys == NULL) {
233         return -1;
234     }
235 
236     PyObject *iter = PyObject_GetIter(keys);
237     Py_DECREF(keys);
238     if (iter == NULL) {
239         return -1;
240     }
241 
242     PyObject *key = NULL;
243     PyObject *value = NULL;
244 
245     while ((key = PyIter_Next(iter)) != NULL) {
246         value = PyObject_GetItem(other, key);
247         if (value == NULL) {
248             Py_DECREF(key);
249             Py_DECREF(iter);
250             return -1;
251         }
252 
253         if (framelocalsproxy_setitem(self, key, value) < 0) {
254             Py_DECREF(key);
255             Py_DECREF(value);
256             Py_DECREF(iter);
257             return -1;
258         }
259 
260         Py_DECREF(key);
261         Py_DECREF(value);
262     }
263 
264     Py_DECREF(iter);
265 
266     return 0;
267 }
268 
269 static PyObject *
framelocalsproxy_keys(PyObject * self,void * Py_UNUSED (ignored))270 framelocalsproxy_keys(PyObject *self, void *Py_UNUSED(ignored))
271 {
272     PyFrameObject *frame = ((PyFrameLocalsProxyObject*)self)->frame;
273     PyCodeObject *co = _PyFrame_GetCode(frame->f_frame);
274     PyObject *names = PyList_New(0);
275     if (names == NULL) {
276         return NULL;
277     }
278 
279     for (int i = 0; i < co->co_nlocalsplus; i++) {
280         PyObject *val = framelocalsproxy_getval(frame->f_frame, co, i);
281         if (val) {
282             PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
283             if (PyList_Append(names, name) < 0) {
284                 Py_DECREF(names);
285                 return NULL;
286             }
287         }
288     }
289 
290     // Iterate through the extra locals
291     if (frame->f_extra_locals) {
292         assert(PyDict_Check(frame->f_extra_locals));
293 
294         Py_ssize_t i = 0;
295         PyObject *key = NULL;
296         PyObject *value = NULL;
297 
298         while (PyDict_Next(frame->f_extra_locals, &i, &key, &value)) {
299             if (PyList_Append(names, key) < 0) {
300                 Py_DECREF(names);
301                 return NULL;
302             }
303         }
304     }
305 
306     return names;
307 }
308 
309 static void
framelocalsproxy_dealloc(PyObject * self)310 framelocalsproxy_dealloc(PyObject *self)
311 {
312     PyObject_GC_UnTrack(self);
313     Py_CLEAR(((PyFrameLocalsProxyObject*)self)->frame);
314     Py_TYPE(self)->tp_free(self);
315 }
316 
317 static PyObject *
framelocalsproxy_new(PyTypeObject * type,PyObject * args,PyObject * kwds)318 framelocalsproxy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
319 {
320     if (PyTuple_GET_SIZE(args) != 1) {
321         PyErr_Format(PyExc_TypeError,
322                      "FrameLocalsProxy expected 1 argument, got %zd",
323                      PyTuple_GET_SIZE(args));
324         return NULL;
325     }
326     PyObject *item = PyTuple_GET_ITEM(args, 0);
327 
328     if (!PyFrame_Check(item)) {
329         PyErr_Format(PyExc_TypeError, "expect frame, not %T", item);
330         return NULL;
331     }
332     PyFrameObject *frame = (PyFrameObject*)item;
333 
334     if (kwds != NULL && PyDict_Size(kwds) != 0) {
335         PyErr_SetString(PyExc_TypeError,
336                         "FrameLocalsProxy takes no keyword arguments");
337         return 0;
338     }
339 
340     PyFrameLocalsProxyObject *self = (PyFrameLocalsProxyObject *)type->tp_alloc(type, 0);
341     if (self == NULL) {
342         return NULL;
343     }
344 
345     ((PyFrameLocalsProxyObject*)self)->frame = (PyFrameObject*)Py_NewRef(frame);
346 
347     return (PyObject *)self;
348 }
349 
350 static int
framelocalsproxy_tp_clear(PyObject * self)351 framelocalsproxy_tp_clear(PyObject *self)
352 {
353     Py_CLEAR(((PyFrameLocalsProxyObject*)self)->frame);
354     return 0;
355 }
356 
357 static int
framelocalsproxy_visit(PyObject * self,visitproc visit,void * arg)358 framelocalsproxy_visit(PyObject *self, visitproc visit, void *arg)
359 {
360     Py_VISIT(((PyFrameLocalsProxyObject*)self)->frame);
361     return 0;
362 }
363 
364 static PyObject *
framelocalsproxy_iter(PyObject * self)365 framelocalsproxy_iter(PyObject *self)
366 {
367     PyObject* keys = framelocalsproxy_keys(self, NULL);
368     if (keys == NULL) {
369         return NULL;
370     }
371 
372     PyObject* iter = PyObject_GetIter(keys);
373     Py_XDECREF(keys);
374 
375     return iter;
376 }
377 
378 static PyObject *
framelocalsproxy_richcompare(PyObject * self,PyObject * other,int op)379 framelocalsproxy_richcompare(PyObject *self, PyObject *other, int op)
380 {
381     if (PyFrameLocalsProxy_Check(other)) {
382         bool result = ((PyFrameLocalsProxyObject*)self)->frame == ((PyFrameLocalsProxyObject*)other)->frame;
383         if (op == Py_EQ) {
384             return PyBool_FromLong(result);
385         } else if (op == Py_NE) {
386             return PyBool_FromLong(!result);
387         }
388     } else if (PyDict_Check(other)) {
389         PyObject *dct = PyDict_New();
390         if (dct == NULL) {
391             return NULL;
392         }
393 
394         if (PyDict_Update(dct, self) < 0) {
395             Py_DECREF(dct);
396             return NULL;
397         }
398 
399         PyObject *result = PyObject_RichCompare(dct, other, op);
400         Py_DECREF(dct);
401         return result;
402     }
403 
404     Py_RETURN_NOTIMPLEMENTED;
405 }
406 
407 static PyObject *
framelocalsproxy_repr(PyObject * self)408 framelocalsproxy_repr(PyObject *self)
409 {
410     int i = Py_ReprEnter(self);
411     if (i != 0) {
412         return i > 0 ? PyUnicode_FromString("{...}") : NULL;
413     }
414 
415     PyObject *dct = PyDict_New();
416     if (dct == NULL) {
417         Py_ReprLeave(self);
418         return NULL;
419     }
420 
421     if (PyDict_Update(dct, self) < 0) {
422         Py_DECREF(dct);
423         Py_ReprLeave(self);
424         return NULL;
425     }
426 
427     PyObject *repr = PyObject_Repr(dct);
428     Py_DECREF(dct);
429 
430     Py_ReprLeave(self);
431 
432     return repr;
433 }
434 
435 static PyObject*
framelocalsproxy_or(PyObject * self,PyObject * other)436 framelocalsproxy_or(PyObject *self, PyObject *other)
437 {
438     if (!PyDict_Check(other) && !PyFrameLocalsProxy_Check(other)) {
439         Py_RETURN_NOTIMPLEMENTED;
440     }
441 
442     PyObject *result = PyDict_New();
443     if (result == NULL) {
444         return NULL;
445     }
446 
447     if (PyDict_Update(result, self) < 0) {
448         Py_DECREF(result);
449         return NULL;
450     }
451 
452     if (PyDict_Update(result, other) < 0) {
453         Py_DECREF(result);
454         return NULL;
455     }
456 
457     return result;
458 }
459 
460 static PyObject*
framelocalsproxy_inplace_or(PyObject * self,PyObject * other)461 framelocalsproxy_inplace_or(PyObject *self, PyObject *other)
462 {
463     if (!PyDict_Check(other) && !PyFrameLocalsProxy_Check(other)) {
464         Py_RETURN_NOTIMPLEMENTED;
465     }
466 
467     if (framelocalsproxy_merge(self, other) < 0) {
468         Py_RETURN_NOTIMPLEMENTED;
469     }
470 
471     return Py_NewRef(self);
472 }
473 
474 static PyObject*
framelocalsproxy_values(PyObject * self,void * Py_UNUSED (ignored))475 framelocalsproxy_values(PyObject *self, void *Py_UNUSED(ignored))
476 {
477     PyFrameObject *frame = ((PyFrameLocalsProxyObject*)self)->frame;
478     PyCodeObject *co = _PyFrame_GetCode(frame->f_frame);
479     PyObject *values = PyList_New(0);
480     if (values == NULL) {
481         return NULL;
482     }
483 
484     for (int i = 0; i < co->co_nlocalsplus; i++) {
485         PyObject *value = framelocalsproxy_getval(frame->f_frame, co, i);
486         if (value) {
487             if (PyList_Append(values, value) < 0) {
488                 Py_DECREF(values);
489                 return NULL;
490             }
491         }
492     }
493 
494     // Iterate through the extra locals
495     if (frame->f_extra_locals) {
496         Py_ssize_t j = 0;
497         PyObject *key = NULL;
498         PyObject *value = NULL;
499         while (PyDict_Next(frame->f_extra_locals, &j, &key, &value)) {
500             if (PyList_Append(values, value) < 0) {
501                 Py_DECREF(values);
502                 return NULL;
503             }
504         }
505     }
506 
507     return values;
508 }
509 
510 static PyObject *
framelocalsproxy_items(PyObject * self,void * Py_UNUSED (ignored))511 framelocalsproxy_items(PyObject *self, void *Py_UNUSED(ignored))
512 {
513     PyFrameObject *frame = ((PyFrameLocalsProxyObject*)self)->frame;
514     PyCodeObject *co = _PyFrame_GetCode(frame->f_frame);
515     PyObject *items = PyList_New(0);
516     if (items == NULL) {
517         return NULL;
518     }
519 
520     for (int i = 0; i < co->co_nlocalsplus; i++) {
521         PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
522         PyObject *value = framelocalsproxy_getval(frame->f_frame, co, i);
523 
524         if (value) {
525             PyObject *pair = PyTuple_Pack(2, name, value);
526             if (pair == NULL) {
527                 Py_DECREF(items);
528                 return NULL;
529             }
530 
531             if (PyList_Append(items, pair) < 0) {
532                 Py_DECREF(items);
533                 Py_DECREF(pair);
534                 return NULL;
535             }
536 
537             Py_DECREF(pair);
538         }
539     }
540 
541     // Iterate through the extra locals
542     if (frame->f_extra_locals) {
543         Py_ssize_t j = 0;
544         PyObject *key = NULL;
545         PyObject *value = NULL;
546         while (PyDict_Next(frame->f_extra_locals, &j, &key, &value)) {
547             PyObject *pair = PyTuple_Pack(2, key, value);
548             if (pair == NULL) {
549                 Py_DECREF(items);
550                 return NULL;
551             }
552 
553             if (PyList_Append(items, pair) < 0) {
554                 Py_DECREF(items);
555                 Py_DECREF(pair);
556                 return NULL;
557             }
558 
559             Py_DECREF(pair);
560         }
561     }
562 
563     return items;
564 }
565 
566 static Py_ssize_t
framelocalsproxy_length(PyObject * self)567 framelocalsproxy_length(PyObject *self)
568 {
569     PyFrameObject *frame = ((PyFrameLocalsProxyObject*)self)->frame;
570     PyCodeObject *co = _PyFrame_GetCode(frame->f_frame);
571     Py_ssize_t size = 0;
572 
573     if (frame->f_extra_locals != NULL) {
574         assert(PyDict_Check(frame->f_extra_locals));
575         size += PyDict_Size(frame->f_extra_locals);
576     }
577 
578     for (int i = 0; i < co->co_nlocalsplus; i++) {
579         if (framelocalsproxy_getval(frame->f_frame, co, i) != NULL) {
580             size++;
581         }
582     }
583     return size;
584 }
585 
586 static int
framelocalsproxy_contains(PyObject * self,PyObject * key)587 framelocalsproxy_contains(PyObject *self, PyObject *key)
588 {
589     PyFrameObject *frame = ((PyFrameLocalsProxyObject*)self)->frame;
590 
591     int i = framelocalsproxy_getkeyindex(frame, key, true);
592     if (i == -2) {
593         return -1;
594     }
595     if (i >= 0) {
596         return 1;
597     }
598 
599     PyObject *extra = ((PyFrameObject*)frame)->f_extra_locals;
600     if (extra != NULL) {
601         return PyDict_Contains(extra, key);
602     }
603 
604     return 0;
605 }
606 
framelocalsproxy___contains__(PyObject * self,PyObject * key)607 static PyObject* framelocalsproxy___contains__(PyObject *self, PyObject *key)
608 {
609     int result = framelocalsproxy_contains(self, key);
610     if (result < 0) {
611         return NULL;
612     }
613     return PyBool_FromLong(result);
614 }
615 
616 static PyObject*
framelocalsproxy_update(PyObject * self,PyObject * other)617 framelocalsproxy_update(PyObject *self, PyObject *other)
618 {
619     if (framelocalsproxy_merge(self, other) < 0) {
620         PyErr_SetString(PyExc_TypeError, "update() argument must be dict or another FrameLocalsProxy");
621         return NULL;
622     }
623 
624     Py_RETURN_NONE;
625 }
626 
627 static PyObject*
framelocalsproxy_get(PyObject * self,PyObject * const * args,Py_ssize_t nargs)628 framelocalsproxy_get(PyObject* self, PyObject *const *args, Py_ssize_t nargs)
629 {
630     if (nargs < 1 || nargs > 2) {
631         PyErr_SetString(PyExc_TypeError, "get expected 1 or 2 arguments");
632         return NULL;
633     }
634 
635     PyObject *key = args[0];
636     PyObject *default_value = Py_None;
637 
638     if (nargs == 2) {
639         default_value = args[1];
640     }
641 
642     PyObject *result = framelocalsproxy_getitem(self, key);
643 
644     if (result == NULL) {
645         if (PyErr_ExceptionMatches(PyExc_KeyError)) {
646             PyErr_Clear();
647             return Py_XNewRef(default_value);
648         }
649         return NULL;
650     }
651 
652     return result;
653 }
654 
655 static PyObject*
framelocalsproxy_setdefault(PyObject * self,PyObject * const * args,Py_ssize_t nargs)656 framelocalsproxy_setdefault(PyObject* self, PyObject *const *args, Py_ssize_t nargs)
657 {
658     if (nargs < 1 || nargs > 2) {
659         PyErr_SetString(PyExc_TypeError, "setdefault expected 1 or 2 arguments");
660         return NULL;
661     }
662 
663     PyObject *key = args[0];
664     PyObject *default_value = Py_None;
665 
666     if (nargs == 2) {
667         default_value = args[1];
668     }
669 
670     PyObject *result = framelocalsproxy_getitem(self, key);
671 
672     if (result == NULL) {
673         if (PyErr_ExceptionMatches(PyExc_KeyError)) {
674             PyErr_Clear();
675             if (framelocalsproxy_setitem(self, key, default_value) < 0) {
676                 return NULL;
677             }
678             return Py_XNewRef(default_value);
679         }
680         return NULL;
681     }
682 
683     return result;
684 }
685 
686 static PyObject*
framelocalsproxy_pop(PyObject * self,PyObject * const * args,Py_ssize_t nargs)687 framelocalsproxy_pop(PyObject* self, PyObject *const *args, Py_ssize_t nargs)
688 {
689     if (!_PyArg_CheckPositional("pop", nargs, 1, 2)) {
690         return NULL;
691     }
692 
693     PyObject *key = args[0];
694     PyObject *default_value = NULL;
695 
696     if (nargs == 2) {
697         default_value = args[1];
698     }
699 
700     PyFrameObject *frame = ((PyFrameLocalsProxyObject*)self)->frame;
701 
702     int i = framelocalsproxy_getkeyindex(frame, key, false);
703     if (i == -2) {
704         return NULL;
705     }
706 
707     if (i >= 0) {
708         PyErr_SetString(PyExc_ValueError, "cannot remove local variables from FrameLocalsProxy");
709         return NULL;
710     }
711 
712     PyObject *result = NULL;
713 
714     if (frame->f_extra_locals == NULL) {
715         if (default_value != NULL) {
716             return Py_XNewRef(default_value);
717         } else {
718             _PyErr_SetKeyError(key);
719             return NULL;
720         }
721     }
722 
723     if (PyDict_Pop(frame->f_extra_locals, key, &result) < 0) {
724         return NULL;
725     }
726 
727     if (result == NULL) {
728         if (default_value != NULL) {
729             return Py_XNewRef(default_value);
730         } else {
731             _PyErr_SetKeyError(key);
732             return NULL;
733         }
734     }
735 
736     return result;
737 }
738 
739 static PyObject*
framelocalsproxy_copy(PyObject * self,PyObject * Py_UNUSED (ignored))740 framelocalsproxy_copy(PyObject *self, PyObject *Py_UNUSED(ignored))
741 {
742     PyObject* result = PyDict_New();
743 
744     if (result == NULL) {
745         return NULL;
746     }
747 
748     if (PyDict_Update(result, self) < 0) {
749         Py_DECREF(result);
750         return NULL;
751     }
752 
753     return result;
754 }
755 
756 static PyObject*
framelocalsproxy_reversed(PyObject * self,void * Py_UNUSED (ignored))757 framelocalsproxy_reversed(PyObject *self, void *Py_UNUSED(ignored))
758 {
759     PyObject *result = framelocalsproxy_keys(self, NULL);
760 
761     if (result == NULL) {
762         return NULL;
763     }
764 
765     if (PyList_Reverse(result) < 0) {
766         Py_DECREF(result);
767         return NULL;
768     }
769     return result;
770 }
771 
772 static PyNumberMethods framelocalsproxy_as_number = {
773     .nb_or = framelocalsproxy_or,
774     .nb_inplace_or = framelocalsproxy_inplace_or,
775 };
776 
777 static PySequenceMethods framelocalsproxy_as_sequence = {
778     .sq_contains = framelocalsproxy_contains,
779 };
780 
781 static PyMappingMethods framelocalsproxy_as_mapping = {
782     framelocalsproxy_length, // mp_length
783     framelocalsproxy_getitem, // mp_subscript
784     framelocalsproxy_setitem, // mp_ass_subscript
785 };
786 
787 static PyMethodDef framelocalsproxy_methods[] = {
788     {"__contains__",  framelocalsproxy___contains__,      METH_O | METH_COEXIST,
789      NULL},
790     {"__getitem__",   framelocalsproxy_getitem,           METH_O | METH_COEXIST,
791      NULL},
792     {"update",        framelocalsproxy_update,            METH_O,
793      NULL},
794     {"__reversed__", _PyCFunction_CAST(framelocalsproxy_reversed),       METH_NOARGS,
795      NULL},
796     {"copy",         _PyCFunction_CAST(framelocalsproxy_copy),           METH_NOARGS,
797      NULL},
798     {"keys",         _PyCFunction_CAST(framelocalsproxy_keys),           METH_NOARGS,
799      NULL},
800     {"values",       _PyCFunction_CAST(framelocalsproxy_values),         METH_NOARGS,
801      NULL},
802     {"items",        _PyCFunction_CAST(framelocalsproxy_items),          METH_NOARGS,
803      NULL},
804     {"get",          _PyCFunction_CAST(framelocalsproxy_get),            METH_FASTCALL,
805      NULL},
806     {"pop",          _PyCFunction_CAST(framelocalsproxy_pop),            METH_FASTCALL,
807      NULL},
808     {"setdefault",   _PyCFunction_CAST(framelocalsproxy_setdefault),     METH_FASTCALL,
809      NULL},
810     {NULL,            NULL}   /* sentinel */
811 };
812 
813 PyTypeObject PyFrameLocalsProxy_Type = {
814     PyVarObject_HEAD_INIT(&PyType_Type, 0)
815     .tp_name = "FrameLocalsProxy",
816     .tp_basicsize = sizeof(PyFrameLocalsProxyObject),
817     .tp_dealloc = (destructor)framelocalsproxy_dealloc,
818     .tp_repr = &framelocalsproxy_repr,
819     .tp_as_number = &framelocalsproxy_as_number,
820     .tp_as_sequence = &framelocalsproxy_as_sequence,
821     .tp_as_mapping = &framelocalsproxy_as_mapping,
822     .tp_getattro = PyObject_GenericGetAttr,
823     .tp_setattro = PyObject_GenericSetAttr,
824     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_MAPPING,
825     .tp_traverse = framelocalsproxy_visit,
826     .tp_clear = framelocalsproxy_tp_clear,
827     .tp_richcompare = framelocalsproxy_richcompare,
828     .tp_iter = framelocalsproxy_iter,
829     .tp_methods = framelocalsproxy_methods,
830     .tp_alloc = PyType_GenericAlloc,
831     .tp_new = framelocalsproxy_new,
832     .tp_free = PyObject_GC_Del,
833 };
834 
835 PyObject *
_PyFrameLocalsProxy_New(PyFrameObject * frame)836 _PyFrameLocalsProxy_New(PyFrameObject *frame)
837 {
838     PyObject* args = PyTuple_Pack(1, frame);
839     if (args == NULL) {
840         return NULL;
841     }
842 
843     PyObject* proxy = (PyObject*)framelocalsproxy_new(&PyFrameLocalsProxy_Type, args, NULL);
844     Py_DECREF(args);
845     return proxy;
846 }
847 
848 static PyMemberDef frame_memberlist[] = {
849     {"f_trace_lines",   Py_T_BOOL,         OFF(f_trace_lines), 0},
850     {NULL}      /* Sentinel */
851 };
852 
853 static PyObject *
frame_getlocals(PyFrameObject * f,void * closure)854 frame_getlocals(PyFrameObject *f, void *closure)
855 {
856     if (f == NULL) {
857         PyErr_BadInternalCall();
858         return NULL;
859     }
860     assert(!_PyFrame_IsIncomplete(f->f_frame));
861 
862     PyCodeObject *co = _PyFrame_GetCode(f->f_frame);
863 
864     if (!(co->co_flags & CO_OPTIMIZED) && !_PyFrame_HasHiddenLocals(f->f_frame)) {
865         if (f->f_frame->f_locals == NULL) {
866             // We found cases when f_locals is NULL for non-optimized code.
867             // We fill the f_locals with an empty dict to avoid crash until
868             // we find the root cause.
869             f->f_frame->f_locals = PyDict_New();
870             if (f->f_frame->f_locals == NULL) {
871                 return NULL;
872             }
873         }
874         return Py_NewRef(f->f_frame->f_locals);
875     }
876 
877     return _PyFrameLocalsProxy_New(f);
878 }
879 
880 int
PyFrame_GetLineNumber(PyFrameObject * f)881 PyFrame_GetLineNumber(PyFrameObject *f)
882 {
883     assert(f != NULL);
884     if (f->f_lineno == -1) {
885         // We should calculate it once. If we can't get the line number,
886         // set f->f_lineno to 0.
887         f->f_lineno = PyUnstable_InterpreterFrame_GetLine(f->f_frame);
888         if (f->f_lineno < 0) {
889             f->f_lineno = 0;
890             return -1;
891         }
892     }
893 
894     if (f->f_lineno > 0) {
895         return f->f_lineno;
896     }
897     return PyUnstable_InterpreterFrame_GetLine(f->f_frame);
898 }
899 
900 static PyObject *
frame_getlineno(PyFrameObject * f,void * closure)901 frame_getlineno(PyFrameObject *f, void *closure)
902 {
903     int lineno = PyFrame_GetLineNumber(f);
904     if (lineno < 0) {
905         Py_RETURN_NONE;
906     }
907     else {
908         return PyLong_FromLong(lineno);
909     }
910 }
911 
912 static PyObject *
frame_getlasti(PyFrameObject * f,void * closure)913 frame_getlasti(PyFrameObject *f, void *closure)
914 {
915     int lasti = _PyInterpreterFrame_LASTI(f->f_frame);
916     if (lasti < 0) {
917         return PyLong_FromLong(-1);
918     }
919     return PyLong_FromLong(lasti * sizeof(_Py_CODEUNIT));
920 }
921 
922 static PyObject *
frame_getglobals(PyFrameObject * f,void * closure)923 frame_getglobals(PyFrameObject *f, void *closure)
924 {
925     PyObject *globals = f->f_frame->f_globals;
926     if (globals == NULL) {
927         globals = Py_None;
928     }
929     return Py_NewRef(globals);
930 }
931 
932 static PyObject *
frame_getbuiltins(PyFrameObject * f,void * closure)933 frame_getbuiltins(PyFrameObject *f, void *closure)
934 {
935     PyObject *builtins = f->f_frame->f_builtins;
936     if (builtins == NULL) {
937         builtins = Py_None;
938     }
939     return Py_NewRef(builtins);
940 }
941 
942 static PyObject *
frame_getcode(PyFrameObject * f,void * closure)943 frame_getcode(PyFrameObject *f, void *closure)
944 {
945     if (PySys_Audit("object.__getattr__", "Os", f, "f_code") < 0) {
946         return NULL;
947     }
948     return (PyObject *)PyFrame_GetCode(f);
949 }
950 
951 static PyObject *
frame_getback(PyFrameObject * f,void * closure)952 frame_getback(PyFrameObject *f, void *closure)
953 {
954     PyObject *res = (PyObject *)PyFrame_GetBack(f);
955     if (res == NULL) {
956         Py_RETURN_NONE;
957     }
958     return res;
959 }
960 
961 static PyObject *
frame_gettrace_opcodes(PyFrameObject * f,void * closure)962 frame_gettrace_opcodes(PyFrameObject *f, void *closure)
963 {
964     PyObject *result = f->f_trace_opcodes ? Py_True : Py_False;
965     return Py_NewRef(result);
966 }
967 
968 static int
frame_settrace_opcodes(PyFrameObject * f,PyObject * value,void * Py_UNUSED (ignored))969 frame_settrace_opcodes(PyFrameObject *f, PyObject* value, void *Py_UNUSED(ignored))
970 {
971     if (!PyBool_Check(value)) {
972         PyErr_SetString(PyExc_TypeError,
973                         "attribute value type must be bool");
974         return -1;
975     }
976     if (value == Py_True) {
977         f->f_trace_opcodes = 1;
978         if (f->f_trace) {
979             return _PyEval_SetOpcodeTrace(f, true);
980         }
981     }
982     else {
983         f->f_trace_opcodes = 0;
984         return _PyEval_SetOpcodeTrace(f, false);
985     }
986     return 0;
987 }
988 
989 /* Model the evaluation stack, to determine which jumps
990  * are safe and how many values needs to be popped.
991  * The stack is modelled by a 64 integer, treating any
992  * stack that can't fit into 64 bits as "overflowed".
993  */
994 
995 typedef enum kind {
996     Iterator = 1,
997     Except = 2,
998     Object = 3,
999     Null = 4,
1000     Lasti = 5,
1001 } Kind;
1002 
1003 static int
compatible_kind(Kind from,Kind to)1004 compatible_kind(Kind from, Kind to) {
1005     if (to == 0) {
1006         return 0;
1007     }
1008     if (to == Object) {
1009         return from != Null;
1010     }
1011     if (to == Null) {
1012         return 1;
1013     }
1014     return from == to;
1015 }
1016 
1017 #define BITS_PER_BLOCK 3
1018 
1019 #define UNINITIALIZED -2
1020 #define OVERFLOWED -1
1021 
1022 #define MAX_STACK_ENTRIES (63/BITS_PER_BLOCK)
1023 #define WILL_OVERFLOW (1ULL<<((MAX_STACK_ENTRIES-1)*BITS_PER_BLOCK))
1024 
1025 #define EMPTY_STACK 0
1026 
1027 static inline int64_t
push_value(int64_t stack,Kind kind)1028 push_value(int64_t stack, Kind kind)
1029 {
1030     if (((uint64_t)stack) >= WILL_OVERFLOW) {
1031         return OVERFLOWED;
1032     }
1033     else {
1034         return (stack << BITS_PER_BLOCK) | kind;
1035     }
1036 }
1037 
1038 static inline int64_t
pop_value(int64_t stack)1039 pop_value(int64_t stack)
1040 {
1041     return Py_ARITHMETIC_RIGHT_SHIFT(int64_t, stack, BITS_PER_BLOCK);
1042 }
1043 
1044 #define MASK ((1<<BITS_PER_BLOCK)-1)
1045 
1046 static inline Kind
top_of_stack(int64_t stack)1047 top_of_stack(int64_t stack)
1048 {
1049     return stack & MASK;
1050 }
1051 
1052 static inline Kind
peek(int64_t stack,int n)1053 peek(int64_t stack, int n)
1054 {
1055     assert(n >= 1);
1056     return (stack>>(BITS_PER_BLOCK*(n-1))) & MASK;
1057 }
1058 
1059 static Kind
stack_swap(int64_t stack,int n)1060 stack_swap(int64_t stack, int n)
1061 {
1062     assert(n >= 1);
1063     Kind to_swap = peek(stack, n);
1064     Kind top = top_of_stack(stack);
1065     int shift = BITS_PER_BLOCK*(n-1);
1066     int64_t replaced_low = (stack & ~(MASK << shift)) | (top << shift);
1067     int64_t replaced_top = (replaced_low & ~MASK) | to_swap;
1068     return replaced_top;
1069 }
1070 
1071 static int64_t
pop_to_level(int64_t stack,int level)1072 pop_to_level(int64_t stack, int level) {
1073     if (level == 0) {
1074         return EMPTY_STACK;
1075     }
1076     int64_t max_item = (1<<BITS_PER_BLOCK) - 1;
1077     int64_t level_max_stack = max_item << ((level-1) * BITS_PER_BLOCK);
1078     while (stack > level_max_stack) {
1079         stack = pop_value(stack);
1080     }
1081     return stack;
1082 }
1083 
1084 #if 0
1085 /* These functions are useful for debugging the stack marking code */
1086 
1087 static char
1088 tos_char(int64_t stack) {
1089     switch(top_of_stack(stack)) {
1090         case Iterator:
1091             return 'I';
1092         case Except:
1093             return 'E';
1094         case Object:
1095             return 'O';
1096         case Lasti:
1097             return 'L';
1098         case Null:
1099             return 'N';
1100     }
1101     return '?';
1102 }
1103 
1104 static void
1105 print_stack(int64_t stack) {
1106     if (stack < 0) {
1107         if (stack == UNINITIALIZED) {
1108             printf("---");
1109         }
1110         else if (stack == OVERFLOWED) {
1111             printf("OVERFLOWED");
1112         }
1113         else {
1114             printf("??");
1115         }
1116         return;
1117     }
1118     while (stack) {
1119         printf("%c", tos_char(stack));
1120         stack = pop_value(stack);
1121     }
1122 }
1123 
1124 static void
1125 print_stacks(int64_t *stacks, int n) {
1126     for (int i = 0; i < n; i++) {
1127         printf("%d: ", i);
1128         print_stack(stacks[i]);
1129         printf("\n");
1130     }
1131 }
1132 
1133 #endif
1134 
1135 static int64_t *
mark_stacks(PyCodeObject * code_obj,int len)1136 mark_stacks(PyCodeObject *code_obj, int len)
1137 {
1138     PyObject *co_code = _PyCode_GetCode(code_obj);
1139     if (co_code == NULL) {
1140         return NULL;
1141     }
1142     _Py_CODEUNIT *code = (_Py_CODEUNIT *)PyBytes_AS_STRING(co_code);
1143     int64_t *stacks = PyMem_New(int64_t, len+1);
1144     int i, j, opcode;
1145 
1146     if (stacks == NULL) {
1147         PyErr_NoMemory();
1148         Py_DECREF(co_code);
1149         return NULL;
1150     }
1151     for (int i = 1; i <= len; i++) {
1152         stacks[i] = UNINITIALIZED;
1153     }
1154     stacks[0] = EMPTY_STACK;
1155     int todo = 1;
1156     while (todo) {
1157         todo = 0;
1158         /* Scan instructions */
1159         for (i = 0; i < len;) {
1160             int64_t next_stack = stacks[i];
1161             opcode = _Py_GetBaseOpcode(code_obj, i);
1162             int oparg = 0;
1163             while (opcode == EXTENDED_ARG) {
1164                 oparg = (oparg << 8) | code[i].op.arg;
1165                 i++;
1166                 opcode = _Py_GetBaseOpcode(code_obj, i);
1167                 stacks[i] = next_stack;
1168             }
1169             int next_i = i + _PyOpcode_Caches[opcode] + 1;
1170             if (next_stack == UNINITIALIZED) {
1171                 i = next_i;
1172                 continue;
1173             }
1174             oparg = (oparg << 8) | code[i].op.arg;
1175             switch (opcode) {
1176                 case POP_JUMP_IF_FALSE:
1177                 case POP_JUMP_IF_TRUE:
1178                 case POP_JUMP_IF_NONE:
1179                 case POP_JUMP_IF_NOT_NONE:
1180                 {
1181                     int64_t target_stack;
1182                     int j = next_i + oparg;
1183                     assert(j < len);
1184                     next_stack = pop_value(next_stack);
1185                     target_stack = next_stack;
1186                     assert(stacks[j] == UNINITIALIZED || stacks[j] == target_stack);
1187                     stacks[j] = target_stack;
1188                     stacks[next_i] = next_stack;
1189                     break;
1190                 }
1191                 case SEND:
1192                     j = oparg + i + INLINE_CACHE_ENTRIES_SEND + 1;
1193                     assert(j < len);
1194                     assert(stacks[j] == UNINITIALIZED || stacks[j] == next_stack);
1195                     stacks[j] = next_stack;
1196                     stacks[next_i] = next_stack;
1197                     break;
1198                 case JUMP_FORWARD:
1199                     j = oparg + i + 1;
1200                     assert(j < len);
1201                     assert(stacks[j] == UNINITIALIZED || stacks[j] == next_stack);
1202                     stacks[j] = next_stack;
1203                     break;
1204                 case JUMP_BACKWARD:
1205                 case JUMP_BACKWARD_NO_INTERRUPT:
1206                     j = next_i - oparg;
1207                     assert(j >= 0);
1208                     assert(j < len);
1209                     if (stacks[j] == UNINITIALIZED && j < i) {
1210                         todo = 1;
1211                     }
1212                     assert(stacks[j] == UNINITIALIZED || stacks[j] == next_stack);
1213                     stacks[j] = next_stack;
1214                     break;
1215                 case GET_ITER:
1216                 case GET_AITER:
1217                     next_stack = push_value(pop_value(next_stack), Iterator);
1218                     stacks[next_i] = next_stack;
1219                     break;
1220                 case FOR_ITER:
1221                 {
1222                     int64_t target_stack = push_value(next_stack, Object);
1223                     stacks[next_i] = target_stack;
1224                     j = oparg + 1 + INLINE_CACHE_ENTRIES_FOR_ITER + i;
1225                     assert(j < len);
1226                     assert(stacks[j] == UNINITIALIZED || stacks[j] == target_stack);
1227                     stacks[j] = target_stack;
1228                     break;
1229                 }
1230                 case END_ASYNC_FOR:
1231                     next_stack = pop_value(pop_value(next_stack));
1232                     stacks[next_i] = next_stack;
1233                     break;
1234                 case PUSH_EXC_INFO:
1235                     next_stack = push_value(next_stack, Except);
1236                     stacks[next_i] = next_stack;
1237                     break;
1238                 case POP_EXCEPT:
1239                     assert(top_of_stack(next_stack) == Except);
1240                     next_stack = pop_value(next_stack);
1241                     stacks[next_i] = next_stack;
1242                     break;
1243                 case RETURN_VALUE:
1244                     assert(pop_value(next_stack) == EMPTY_STACK);
1245                     assert(top_of_stack(next_stack) == Object);
1246                     break;
1247                 case RETURN_CONST:
1248                     break;
1249                 case RAISE_VARARGS:
1250                     break;
1251                 case RERAISE:
1252                     assert(top_of_stack(next_stack) == Except);
1253                     /* End of block */
1254                     break;
1255                 case PUSH_NULL:
1256                     next_stack = push_value(next_stack, Null);
1257                     stacks[next_i] = next_stack;
1258                     break;
1259                 case LOAD_GLOBAL:
1260                 {
1261                     int j = oparg;
1262                     next_stack = push_value(next_stack, Object);
1263                     if (j & 1) {
1264                         next_stack = push_value(next_stack, Null);
1265                     }
1266                     stacks[next_i] = next_stack;
1267                     break;
1268                 }
1269                 case LOAD_ATTR:
1270                 {
1271                     assert(top_of_stack(next_stack) == Object);
1272                     int j = oparg;
1273                     if (j & 1) {
1274                         next_stack = pop_value(next_stack);
1275                         next_stack = push_value(next_stack, Object);
1276                         next_stack = push_value(next_stack, Null);
1277                     }
1278                     stacks[next_i] = next_stack;
1279                     break;
1280                 }
1281                 case SWAP:
1282                 {
1283                     int n = oparg;
1284                     next_stack = stack_swap(next_stack, n);
1285                     stacks[next_i] = next_stack;
1286                     break;
1287                 }
1288                 case COPY:
1289                 {
1290                     int n = oparg;
1291                     next_stack = push_value(next_stack, peek(next_stack, n));
1292                     stacks[next_i] = next_stack;
1293                     break;
1294                 }
1295                 case CACHE:
1296                 case RESERVED:
1297                 {
1298                     assert(0);
1299                 }
1300                 default:
1301                 {
1302                     int delta = PyCompile_OpcodeStackEffect(opcode, oparg);
1303                     assert(delta != PY_INVALID_STACK_EFFECT);
1304                     while (delta < 0) {
1305                         next_stack = pop_value(next_stack);
1306                         delta++;
1307                     }
1308                     while (delta > 0) {
1309                         next_stack = push_value(next_stack, Object);
1310                         delta--;
1311                     }
1312                     stacks[next_i] = next_stack;
1313                 }
1314             }
1315             i = next_i;
1316         }
1317         /* Scan exception table */
1318         unsigned char *start = (unsigned char *)PyBytes_AS_STRING(code_obj->co_exceptiontable);
1319         unsigned char *end = start + PyBytes_GET_SIZE(code_obj->co_exceptiontable);
1320         unsigned char *scan = start;
1321         while (scan < end) {
1322             int start_offset, size, handler;
1323             scan = parse_varint(scan, &start_offset);
1324             assert(start_offset >= 0 && start_offset < len);
1325             scan = parse_varint(scan, &size);
1326             assert(size >= 0 && start_offset+size <= len);
1327             scan = parse_varint(scan, &handler);
1328             assert(handler >= 0 && handler < len);
1329             int depth_and_lasti;
1330             scan = parse_varint(scan, &depth_and_lasti);
1331             int level = depth_and_lasti >> 1;
1332             int lasti = depth_and_lasti & 1;
1333             if (stacks[start_offset] != UNINITIALIZED) {
1334                 if (stacks[handler] == UNINITIALIZED) {
1335                     todo = 1;
1336                     uint64_t target_stack = pop_to_level(stacks[start_offset], level);
1337                     if (lasti) {
1338                         target_stack = push_value(target_stack, Lasti);
1339                     }
1340                     target_stack = push_value(target_stack, Except);
1341                     stacks[handler] = target_stack;
1342                 }
1343             }
1344         }
1345     }
1346     Py_DECREF(co_code);
1347     return stacks;
1348 }
1349 
1350 static int
compatible_stack(int64_t from_stack,int64_t to_stack)1351 compatible_stack(int64_t from_stack, int64_t to_stack)
1352 {
1353     if (from_stack < 0 || to_stack < 0) {
1354         return 0;
1355     }
1356     while(from_stack > to_stack) {
1357         from_stack = pop_value(from_stack);
1358     }
1359     while(from_stack) {
1360         Kind from_top = top_of_stack(from_stack);
1361         Kind to_top = top_of_stack(to_stack);
1362         if (!compatible_kind(from_top, to_top)) {
1363             return 0;
1364         }
1365         from_stack = pop_value(from_stack);
1366         to_stack = pop_value(to_stack);
1367     }
1368     return to_stack == 0;
1369 }
1370 
1371 static const char *
explain_incompatible_stack(int64_t to_stack)1372 explain_incompatible_stack(int64_t to_stack)
1373 {
1374     assert(to_stack != 0);
1375     if (to_stack == OVERFLOWED) {
1376         return "stack is too deep to analyze";
1377     }
1378     if (to_stack == UNINITIALIZED) {
1379         return "can't jump into an exception handler, or code may be unreachable";
1380     }
1381     Kind target_kind = top_of_stack(to_stack);
1382     switch(target_kind) {
1383         case Except:
1384             return "can't jump into an 'except' block as there's no exception";
1385         case Lasti:
1386             return "can't jump into a re-raising block as there's no location";
1387         case Object:
1388         case Null:
1389             return "incompatible stacks";
1390         case Iterator:
1391             return "can't jump into the body of a for loop";
1392         default:
1393             Py_UNREACHABLE();
1394     }
1395 }
1396 
1397 static int *
marklines(PyCodeObject * code,int len)1398 marklines(PyCodeObject *code, int len)
1399 {
1400     PyCodeAddressRange bounds;
1401     _PyCode_InitAddressRange(code, &bounds);
1402     assert (bounds.ar_end == 0);
1403     int last_line = -1;
1404 
1405     int *linestarts = PyMem_New(int, len);
1406     if (linestarts == NULL) {
1407         return NULL;
1408     }
1409     for (int i = 0; i < len; i++) {
1410         linestarts[i] = -1;
1411     }
1412 
1413     while (_PyLineTable_NextAddressRange(&bounds)) {
1414         assert(bounds.ar_start / (int)sizeof(_Py_CODEUNIT) < len);
1415         if (bounds.ar_line != last_line && bounds.ar_line != -1) {
1416             linestarts[bounds.ar_start / sizeof(_Py_CODEUNIT)] = bounds.ar_line;
1417             last_line = bounds.ar_line;
1418         }
1419     }
1420     return linestarts;
1421 }
1422 
1423 static int
first_line_not_before(int * lines,int len,int line)1424 first_line_not_before(int *lines, int len, int line)
1425 {
1426     int result = INT_MAX;
1427     for (int i = 0; i < len; i++) {
1428         if (lines[i] < result && lines[i] >= line) {
1429             result = lines[i];
1430         }
1431     }
1432     if (result == INT_MAX) {
1433         return -1;
1434     }
1435     return result;
1436 }
1437 
frame_is_suspended(PyFrameObject * frame)1438 static bool frame_is_suspended(PyFrameObject *frame)
1439 {
1440     assert(!_PyFrame_IsIncomplete(frame->f_frame));
1441     if (frame->f_frame->owner == FRAME_OWNED_BY_GENERATOR) {
1442         PyGenObject *gen = _PyFrame_GetGenerator(frame->f_frame);
1443         return FRAME_STATE_SUSPENDED(gen->gi_frame_state);
1444     }
1445     return false;
1446 }
1447 
1448 /* Setter for f_lineno - you can set f_lineno from within a trace function in
1449  * order to jump to a given line of code, subject to some restrictions.  Most
1450  * lines are OK to jump to because they don't make any assumptions about the
1451  * state of the stack (obvious because you could remove the line and the code
1452  * would still work without any stack errors), but there are some constructs
1453  * that limit jumping:
1454  *
1455  *  o Any exception handlers.
1456  *  o 'for' and 'async for' loops can't be jumped into because the
1457  *    iterator needs to be on the stack.
1458  *  o Jumps cannot be made from within a trace function invoked with a
1459  *    'return' or 'exception' event since the eval loop has been exited at
1460  *    that time.
1461  */
1462 static int
frame_setlineno(PyFrameObject * f,PyObject * p_new_lineno,void * Py_UNUSED (ignored))1463 frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignored))
1464 {
1465     PyCodeObject *code = _PyFrame_GetCode(f->f_frame);
1466     if (p_new_lineno == NULL) {
1467         PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
1468         return -1;
1469     }
1470     /* f_lineno must be an integer. */
1471     if (!PyLong_CheckExact(p_new_lineno)) {
1472         PyErr_SetString(PyExc_ValueError,
1473                         "lineno must be an integer");
1474         return -1;
1475     }
1476 
1477     bool is_suspended = frame_is_suspended(f);
1478     /*
1479      * This code preserves the historical restrictions on
1480      * setting the line number of a frame.
1481      * Jumps are forbidden on a 'return' trace event (except after a yield).
1482      * Jumps from 'call' trace events are also forbidden.
1483      * In addition, jumps are forbidden when not tracing,
1484      * as this is a debugging feature.
1485      */
1486     int what_event = PyThreadState_GET()->what_event;
1487     if (what_event < 0) {
1488         PyErr_Format(PyExc_ValueError,
1489                     "f_lineno can only be set in a trace function");
1490         return -1;
1491     }
1492     switch (what_event) {
1493         case PY_MONITORING_EVENT_PY_RESUME:
1494         case PY_MONITORING_EVENT_JUMP:
1495         case PY_MONITORING_EVENT_BRANCH:
1496         case PY_MONITORING_EVENT_LINE:
1497         case PY_MONITORING_EVENT_PY_YIELD:
1498             /* Setting f_lineno is allowed for the above events */
1499             break;
1500         case PY_MONITORING_EVENT_PY_START:
1501             PyErr_Format(PyExc_ValueError,
1502                      "can't jump from the 'call' trace event of a new frame");
1503             return -1;
1504         case PY_MONITORING_EVENT_CALL:
1505         case PY_MONITORING_EVENT_C_RETURN:
1506             PyErr_SetString(PyExc_ValueError,
1507                 "can't jump during a call");
1508             return -1;
1509         case PY_MONITORING_EVENT_PY_RETURN:
1510         case PY_MONITORING_EVENT_PY_UNWIND:
1511         case PY_MONITORING_EVENT_PY_THROW:
1512         case PY_MONITORING_EVENT_RAISE:
1513         case PY_MONITORING_EVENT_C_RAISE:
1514         case PY_MONITORING_EVENT_INSTRUCTION:
1515         case PY_MONITORING_EVENT_EXCEPTION_HANDLED:
1516             PyErr_Format(PyExc_ValueError,
1517                 "can only jump from a 'line' trace event");
1518             return -1;
1519         default:
1520             PyErr_SetString(PyExc_SystemError,
1521                 "unexpected event type");
1522             return -1;
1523     }
1524 
1525     int new_lineno;
1526 
1527     /* Fail if the line falls outside the code block and
1528         select first line with actual code. */
1529     int overflow;
1530     long l_new_lineno = PyLong_AsLongAndOverflow(p_new_lineno, &overflow);
1531     if (overflow
1532 #if SIZEOF_LONG > SIZEOF_INT
1533         || l_new_lineno > INT_MAX
1534         || l_new_lineno < INT_MIN
1535 #endif
1536     ) {
1537         PyErr_SetString(PyExc_ValueError,
1538                         "lineno out of range");
1539         return -1;
1540     }
1541     new_lineno = (int)l_new_lineno;
1542 
1543     if (new_lineno < code->co_firstlineno) {
1544         PyErr_Format(PyExc_ValueError,
1545                     "line %d comes before the current code block",
1546                     new_lineno);
1547         return -1;
1548     }
1549 
1550     /* PyCode_NewWithPosOnlyArgs limits co_code to be under INT_MAX so this
1551      * should never overflow. */
1552     int len = (int)Py_SIZE(code);
1553     int *lines = marklines(code, len);
1554     if (lines == NULL) {
1555         return -1;
1556     }
1557 
1558     new_lineno = first_line_not_before(lines, len, new_lineno);
1559     if (new_lineno < 0) {
1560         PyErr_Format(PyExc_ValueError,
1561                     "line %d comes after the current code block",
1562                     (int)l_new_lineno);
1563         PyMem_Free(lines);
1564         return -1;
1565     }
1566 
1567     int64_t *stacks = mark_stacks(code, len);
1568     if (stacks == NULL) {
1569         PyMem_Free(lines);
1570         return -1;
1571     }
1572 
1573     int64_t best_stack = OVERFLOWED;
1574     int best_addr = -1;
1575     int64_t start_stack = stacks[_PyInterpreterFrame_LASTI(f->f_frame)];
1576     int err = -1;
1577     const char *msg = "cannot find bytecode for specified line";
1578     for (int i = 0; i < len; i++) {
1579         if (lines[i] == new_lineno) {
1580             int64_t target_stack = stacks[i];
1581             if (compatible_stack(start_stack, target_stack)) {
1582                 err = 0;
1583                 if (target_stack > best_stack) {
1584                     best_stack = target_stack;
1585                     best_addr = i;
1586                 }
1587             }
1588             else if (err < 0) {
1589                 if (start_stack == OVERFLOWED) {
1590                     msg = "stack to deep to analyze";
1591                 }
1592                 else if (start_stack == UNINITIALIZED) {
1593                     msg = "can't jump from unreachable code";
1594                 }
1595                 else {
1596                     msg = explain_incompatible_stack(target_stack);
1597                     err = 1;
1598                 }
1599             }
1600         }
1601     }
1602     PyMem_Free(stacks);
1603     PyMem_Free(lines);
1604     if (err) {
1605         PyErr_SetString(PyExc_ValueError, msg);
1606         return -1;
1607     }
1608     // Populate any NULL locals that the compiler might have "proven" to exist
1609     // in the new location. Rather than crashing or changing co_code, just bind
1610     // None instead:
1611     int unbound = 0;
1612     for (int i = 0; i < code->co_nlocalsplus; i++) {
1613         // Counting every unbound local is overly-cautious, but a full flow
1614         // analysis (like we do in the compiler) is probably too expensive:
1615         unbound += f->f_frame->localsplus[i] == NULL;
1616     }
1617     if (unbound) {
1618         const char *e = "assigning None to %d unbound local%s";
1619         const char *s = (unbound == 1) ? "" : "s";
1620         if (PyErr_WarnFormat(PyExc_RuntimeWarning, 0, e, unbound, s)) {
1621             return -1;
1622         }
1623         // Do this in a second pass to avoid writing a bunch of Nones when
1624         // warnings are being treated as errors and the previous bit raises:
1625         for (int i = 0; i < code->co_nlocalsplus; i++) {
1626             if (f->f_frame->localsplus[i] == NULL) {
1627                 f->f_frame->localsplus[i] = Py_NewRef(Py_None);
1628                 unbound--;
1629             }
1630         }
1631         assert(unbound == 0);
1632     }
1633     if (is_suspended) {
1634         /* Account for value popped by yield */
1635         start_stack = pop_value(start_stack);
1636     }
1637     while (start_stack > best_stack) {
1638         if (top_of_stack(start_stack) == Except) {
1639             /* Pop exception stack as well as the evaluation stack */
1640             PyObject *exc = _PyFrame_StackPop(f->f_frame);
1641             assert(PyExceptionInstance_Check(exc) || exc == Py_None);
1642             PyThreadState *tstate = _PyThreadState_GET();
1643             Py_XSETREF(tstate->exc_info->exc_value, exc == Py_None ? NULL : exc);
1644         }
1645         else {
1646             PyObject *v = _PyFrame_StackPop(f->f_frame);
1647             Py_XDECREF(v);
1648         }
1649         start_stack = pop_value(start_stack);
1650     }
1651     /* Finally set the new lasti and return OK. */
1652     f->f_lineno = 0;
1653     f->f_frame->instr_ptr = _PyCode_CODE(code) + best_addr;
1654     return 0;
1655 }
1656 
1657 static PyObject *
frame_gettrace(PyFrameObject * f,void * closure)1658 frame_gettrace(PyFrameObject *f, void *closure)
1659 {
1660     PyObject* trace = f->f_trace;
1661     if (trace == NULL)
1662         trace = Py_None;
1663     return Py_NewRef(trace);
1664 }
1665 
1666 static int
frame_settrace(PyFrameObject * f,PyObject * v,void * closure)1667 frame_settrace(PyFrameObject *f, PyObject* v, void *closure)
1668 {
1669     if (v == Py_None) {
1670         v = NULL;
1671     }
1672     if (v != f->f_trace) {
1673         Py_XSETREF(f->f_trace, Py_XNewRef(v));
1674         if (v != NULL && f->f_trace_opcodes) {
1675             return _PyEval_SetOpcodeTrace(f, true);
1676         }
1677     }
1678     return 0;
1679 }
1680 
1681 
1682 static PyGetSetDef frame_getsetlist[] = {
1683     {"f_back",          (getter)frame_getback, NULL, NULL},
1684     {"f_locals",        (getter)frame_getlocals, NULL, NULL},
1685     {"f_lineno",        (getter)frame_getlineno,
1686                     (setter)frame_setlineno, NULL},
1687     {"f_trace",         (getter)frame_gettrace, (setter)frame_settrace, NULL},
1688     {"f_lasti",         (getter)frame_getlasti, NULL, NULL},
1689     {"f_globals",       (getter)frame_getglobals, NULL, NULL},
1690     {"f_builtins",      (getter)frame_getbuiltins, NULL, NULL},
1691     {"f_code",          (getter)frame_getcode, NULL, NULL},
1692     {"f_trace_opcodes", (getter)frame_gettrace_opcodes, (setter)frame_settrace_opcodes, NULL},
1693     {0}
1694 };
1695 
1696 static void
frame_dealloc(PyFrameObject * f)1697 frame_dealloc(PyFrameObject *f)
1698 {
1699     /* It is the responsibility of the owning generator/coroutine
1700      * to have cleared the generator pointer */
1701 
1702     if (_PyObject_GC_IS_TRACKED(f)) {
1703         _PyObject_GC_UNTRACK(f);
1704     }
1705 
1706     Py_TRASHCAN_BEGIN(f, frame_dealloc);
1707     PyObject *co = NULL;
1708 
1709     /* GH-106092: If f->f_frame was on the stack and we reached the maximum
1710      * nesting depth for deallocations, the trashcan may have delayed this
1711      * deallocation until after f->f_frame is freed. Avoid dereferencing
1712      * f->f_frame unless we know it still points to valid memory. */
1713     _PyInterpreterFrame *frame = (_PyInterpreterFrame *)f->_f_frame_data;
1714 
1715     /* Kill all local variables including specials, if we own them */
1716     if (f->f_frame == frame && frame->owner == FRAME_OWNED_BY_FRAME_OBJECT) {
1717         /* Don't clear code object until the end */
1718         co = frame->f_executable;
1719         frame->f_executable = NULL;
1720         Py_CLEAR(frame->f_funcobj);
1721         Py_CLEAR(frame->f_locals);
1722         PyObject **locals = _PyFrame_GetLocalsArray(frame);
1723         for (int i = 0; i < frame->stacktop; i++) {
1724             Py_CLEAR(locals[i]);
1725         }
1726     }
1727     Py_CLEAR(f->f_back);
1728     Py_CLEAR(f->f_trace);
1729     Py_CLEAR(f->f_extra_locals);
1730     Py_CLEAR(f->f_locals_cache);
1731     PyObject_GC_Del(f);
1732     Py_XDECREF(co);
1733     Py_TRASHCAN_END;
1734 }
1735 
1736 static int
frame_traverse(PyFrameObject * f,visitproc visit,void * arg)1737 frame_traverse(PyFrameObject *f, visitproc visit, void *arg)
1738 {
1739     Py_VISIT(f->f_back);
1740     Py_VISIT(f->f_trace);
1741     Py_VISIT(f->f_extra_locals);
1742     Py_VISIT(f->f_locals_cache);
1743     if (f->f_frame->owner != FRAME_OWNED_BY_FRAME_OBJECT) {
1744         return 0;
1745     }
1746     assert(f->f_frame->frame_obj == NULL);
1747     return _PyFrame_Traverse(f->f_frame, visit, arg);
1748 }
1749 
1750 static int
frame_tp_clear(PyFrameObject * f)1751 frame_tp_clear(PyFrameObject *f)
1752 {
1753     Py_CLEAR(f->f_trace);
1754     Py_CLEAR(f->f_extra_locals);
1755     Py_CLEAR(f->f_locals_cache);
1756 
1757     /* locals and stack */
1758     PyObject **locals = _PyFrame_GetLocalsArray(f->f_frame);
1759     assert(f->f_frame->stacktop >= 0);
1760     for (int i = 0; i < f->f_frame->stacktop; i++) {
1761         Py_CLEAR(locals[i]);
1762     }
1763     f->f_frame->stacktop = 0;
1764     Py_CLEAR(f->f_frame->f_locals);
1765     return 0;
1766 }
1767 
1768 static PyObject *
frame_clear(PyFrameObject * f,PyObject * Py_UNUSED (ignored))1769 frame_clear(PyFrameObject *f, PyObject *Py_UNUSED(ignored))
1770 {
1771     if (f->f_frame->owner == FRAME_OWNED_BY_GENERATOR) {
1772         PyGenObject *gen = _PyFrame_GetGenerator(f->f_frame);
1773         if (gen->gi_frame_state == FRAME_EXECUTING) {
1774             goto running;
1775         }
1776         if (FRAME_STATE_SUSPENDED(gen->gi_frame_state)) {
1777             goto suspended;
1778         }
1779         _PyGen_Finalize((PyObject *)gen);
1780     }
1781     else if (f->f_frame->owner == FRAME_OWNED_BY_THREAD) {
1782         goto running;
1783     }
1784     else {
1785         assert(f->f_frame->owner == FRAME_OWNED_BY_FRAME_OBJECT);
1786         (void)frame_tp_clear(f);
1787     }
1788     Py_RETURN_NONE;
1789 running:
1790     PyErr_SetString(PyExc_RuntimeError,
1791                     "cannot clear an executing frame");
1792     return NULL;
1793 suspended:
1794     PyErr_SetString(PyExc_RuntimeError,
1795                     "cannot clear a suspended frame");
1796     return NULL;
1797 }
1798 
1799 PyDoc_STRVAR(clear__doc__,
1800 "F.clear(): clear most references held by the frame");
1801 
1802 static PyObject *
frame_sizeof(PyFrameObject * f,PyObject * Py_UNUSED (ignored))1803 frame_sizeof(PyFrameObject *f, PyObject *Py_UNUSED(ignored))
1804 {
1805     Py_ssize_t res;
1806     res = offsetof(PyFrameObject, _f_frame_data) + offsetof(_PyInterpreterFrame, localsplus);
1807     PyCodeObject *code = _PyFrame_GetCode(f->f_frame);
1808     res += _PyFrame_NumSlotsForCodeObject(code) * sizeof(PyObject *);
1809     return PyLong_FromSsize_t(res);
1810 }
1811 
1812 PyDoc_STRVAR(sizeof__doc__,
1813 "F.__sizeof__() -> size of F in memory, in bytes");
1814 
1815 static PyObject *
frame_repr(PyFrameObject * f)1816 frame_repr(PyFrameObject *f)
1817 {
1818     int lineno = PyFrame_GetLineNumber(f);
1819     PyCodeObject *code = _PyFrame_GetCode(f->f_frame);
1820     return PyUnicode_FromFormat(
1821         "<frame at %p, file %R, line %d, code %S>",
1822         f, code->co_filename, lineno, code->co_name);
1823 }
1824 
1825 static PyMethodDef frame_methods[] = {
1826     {"clear",           (PyCFunction)frame_clear,       METH_NOARGS,
1827      clear__doc__},
1828     {"__sizeof__",      (PyCFunction)frame_sizeof,      METH_NOARGS,
1829      sizeof__doc__},
1830     {NULL,              NULL}   /* sentinel */
1831 };
1832 
1833 PyTypeObject PyFrame_Type = {
1834     PyVarObject_HEAD_INIT(&PyType_Type, 0)
1835     "frame",
1836     offsetof(PyFrameObject, _f_frame_data) +
1837     offsetof(_PyInterpreterFrame, localsplus),
1838     sizeof(PyObject *),
1839     (destructor)frame_dealloc,                  /* tp_dealloc */
1840     0,                                          /* tp_vectorcall_offset */
1841     0,                                          /* tp_getattr */
1842     0,                                          /* tp_setattr */
1843     0,                                          /* tp_as_async */
1844     (reprfunc)frame_repr,                       /* tp_repr */
1845     0,                                          /* tp_as_number */
1846     0,                                          /* tp_as_sequence */
1847     0,                                          /* tp_as_mapping */
1848     0,                                          /* tp_hash */
1849     0,                                          /* tp_call */
1850     0,                                          /* tp_str */
1851     PyObject_GenericGetAttr,                    /* tp_getattro */
1852     PyObject_GenericSetAttr,                    /* tp_setattro */
1853     0,                                          /* tp_as_buffer */
1854     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1855     0,                                          /* tp_doc */
1856     (traverseproc)frame_traverse,               /* tp_traverse */
1857     (inquiry)frame_tp_clear,                    /* tp_clear */
1858     0,                                          /* tp_richcompare */
1859     0,                                          /* tp_weaklistoffset */
1860     0,                                          /* tp_iter */
1861     0,                                          /* tp_iternext */
1862     frame_methods,                              /* tp_methods */
1863     frame_memberlist,                           /* tp_members */
1864     frame_getsetlist,                           /* tp_getset */
1865     0,                                          /* tp_base */
1866     0,                                          /* tp_dict */
1867 };
1868 
1869 static void
init_frame(_PyInterpreterFrame * frame,PyFunctionObject * func,PyObject * locals)1870 init_frame(_PyInterpreterFrame *frame, PyFunctionObject *func, PyObject *locals)
1871 {
1872     PyCodeObject *code = (PyCodeObject *)func->func_code;
1873     _PyFrame_Initialize(frame, (PyFunctionObject*)Py_NewRef(func),
1874                         Py_XNewRef(locals), code, 0);
1875     frame->previous = NULL;
1876 }
1877 
1878 PyFrameObject*
_PyFrame_New_NoTrack(PyCodeObject * code)1879 _PyFrame_New_NoTrack(PyCodeObject *code)
1880 {
1881     CALL_STAT_INC(frame_objects_created);
1882     int slots = code->co_nlocalsplus + code->co_stacksize;
1883     PyFrameObject *f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type, slots);
1884     if (f == NULL) {
1885         return NULL;
1886     }
1887     f->f_back = NULL;
1888     f->f_trace = NULL;
1889     f->f_trace_lines = 1;
1890     f->f_trace_opcodes = 0;
1891     f->f_lineno = 0;
1892     f->f_extra_locals = NULL;
1893     f->f_locals_cache = NULL;
1894     return f;
1895 }
1896 
1897 /* Legacy API */
1898 PyFrameObject*
PyFrame_New(PyThreadState * tstate,PyCodeObject * code,PyObject * globals,PyObject * locals)1899 PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
1900             PyObject *globals, PyObject *locals)
1901 {
1902     PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
1903     if (builtins == NULL) {
1904         return NULL;
1905     }
1906     PyFrameConstructor desc = {
1907         .fc_globals = globals,
1908         .fc_builtins = builtins,
1909         .fc_name = code->co_name,
1910         .fc_qualname = code->co_name,
1911         .fc_code = (PyObject *)code,
1912         .fc_defaults = NULL,
1913         .fc_kwdefaults = NULL,
1914         .fc_closure = NULL
1915     };
1916     PyFunctionObject *func = _PyFunction_FromConstructor(&desc);
1917     if (func == NULL) {
1918         return NULL;
1919     }
1920     PyFrameObject *f = _PyFrame_New_NoTrack(code);
1921     if (f == NULL) {
1922         Py_DECREF(func);
1923         return NULL;
1924     }
1925     init_frame((_PyInterpreterFrame *)f->_f_frame_data, func, locals);
1926     f->f_frame = (_PyInterpreterFrame *)f->_f_frame_data;
1927     f->f_frame->owner = FRAME_OWNED_BY_FRAME_OBJECT;
1928     // This frame needs to be "complete", so pretend that the first RESUME ran:
1929     f->f_frame->instr_ptr = _PyCode_CODE(code) + code->_co_firsttraceable + 1;
1930     assert(!_PyFrame_IsIncomplete(f->f_frame));
1931     Py_DECREF(func);
1932     _PyObject_GC_TRACK(f);
1933     return f;
1934 }
1935 
1936 static int
_PyFrame_OpAlreadyRan(_PyInterpreterFrame * frame,int opcode,int oparg)1937 _PyFrame_OpAlreadyRan(_PyInterpreterFrame *frame, int opcode, int oparg)
1938 {
1939     // This only works when opcode is a non-quickened form:
1940     assert(_PyOpcode_Deopt[opcode] == opcode);
1941     int check_oparg = 0;
1942     for (_Py_CODEUNIT *instruction = _PyCode_CODE(_PyFrame_GetCode(frame));
1943          instruction < frame->instr_ptr; instruction++)
1944     {
1945         int check_opcode = _PyOpcode_Deopt[instruction->op.code];
1946         check_oparg |= instruction->op.arg;
1947         if (check_opcode == opcode && check_oparg == oparg) {
1948             return 1;
1949         }
1950         if (check_opcode == EXTENDED_ARG) {
1951             check_oparg <<= 8;
1952         }
1953         else {
1954             check_oparg = 0;
1955         }
1956         instruction += _PyOpcode_Caches[check_opcode];
1957     }
1958     return 0;
1959 }
1960 
1961 
1962 // Initialize frame free variables if needed
1963 static void
frame_init_get_vars(_PyInterpreterFrame * frame)1964 frame_init_get_vars(_PyInterpreterFrame *frame)
1965 {
1966     // COPY_FREE_VARS has no quickened forms, so no need to use _PyOpcode_Deopt
1967     // here:
1968     PyCodeObject *co = _PyFrame_GetCode(frame);
1969     int lasti = _PyInterpreterFrame_LASTI(frame);
1970     if (!(lasti < 0 && _PyCode_CODE(co)->op.code == COPY_FREE_VARS
1971           && PyFunction_Check(frame->f_funcobj)))
1972     {
1973         /* Free vars are initialized */
1974         return;
1975     }
1976 
1977     /* Free vars have not been initialized -- Do that */
1978     PyObject *closure = ((PyFunctionObject *)frame->f_funcobj)->func_closure;
1979     int offset = PyUnstable_Code_GetFirstFree(co);
1980     for (int i = 0; i < co->co_nfreevars; ++i) {
1981         PyObject *o = PyTuple_GET_ITEM(closure, i);
1982         frame->localsplus[offset + i] = Py_NewRef(o);
1983     }
1984     // COPY_FREE_VARS doesn't have inline CACHEs, either:
1985     frame->instr_ptr = _PyCode_CODE(_PyFrame_GetCode(frame));
1986 }
1987 
1988 
1989 static int
frame_get_var(_PyInterpreterFrame * frame,PyCodeObject * co,int i,PyObject ** pvalue)1990 frame_get_var(_PyInterpreterFrame *frame, PyCodeObject *co, int i,
1991               PyObject **pvalue)
1992 {
1993     _PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i);
1994 
1995     /* If the namespace is unoptimized, then one of the
1996        following cases applies:
1997        1. It does not contain free variables, because it
1998           uses import * or is a top-level namespace.
1999        2. It is a class namespace.
2000        We don't want to accidentally copy free variables
2001        into the locals dict used by the class.
2002     */
2003     if (kind & CO_FAST_FREE && !(co->co_flags & CO_OPTIMIZED)) {
2004         return 0;
2005     }
2006 
2007     PyObject *value = frame->localsplus[i];
2008     if (frame->stacktop) {
2009         if (kind & CO_FAST_FREE) {
2010             // The cell was set by COPY_FREE_VARS.
2011             assert(value != NULL && PyCell_Check(value));
2012             value = PyCell_GET(value);
2013         }
2014         else if (kind & CO_FAST_CELL) {
2015             // Note that no *_DEREF ops can happen before MAKE_CELL
2016             // executes.  So there's no need to duplicate the work
2017             // that MAKE_CELL would otherwise do later, if it hasn't
2018             // run yet.
2019             if (value != NULL) {
2020                 if (PyCell_Check(value) &&
2021                         _PyFrame_OpAlreadyRan(frame, MAKE_CELL, i)) {
2022                     // (likely) MAKE_CELL must have executed already.
2023                     value = PyCell_GET(value);
2024                 }
2025                 // (likely) Otherwise it is an arg (kind & CO_FAST_LOCAL),
2026                 // with the initial value set when the frame was created...
2027                 // (unlikely) ...or it was set via the f_locals proxy.
2028             }
2029         }
2030     }
2031     else {
2032         assert(value == NULL);
2033     }
2034     *pvalue = value;
2035     return 1;
2036 }
2037 
2038 
2039 bool
_PyFrame_HasHiddenLocals(_PyInterpreterFrame * frame)2040 _PyFrame_HasHiddenLocals(_PyInterpreterFrame *frame)
2041 {
2042     /*
2043      * This function returns if there are hidden locals introduced by PEP 709,
2044      * which are the isolated fast locals for inline comprehensions
2045      */
2046     PyCodeObject* co = _PyFrame_GetCode(frame);
2047 
2048     for (int i = 0; i < co->co_nlocalsplus; i++) {
2049         _PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i);
2050 
2051         if (kind & CO_FAST_HIDDEN) {
2052             PyObject* value = framelocalsproxy_getval(frame, co, i);
2053 
2054             if (value != NULL) {
2055                 return true;
2056             }
2057         }
2058     }
2059 
2060     return false;
2061 }
2062 
2063 
2064 PyObject *
_PyFrame_GetLocals(_PyInterpreterFrame * frame)2065 _PyFrame_GetLocals(_PyInterpreterFrame *frame)
2066 {
2067     // We should try to avoid creating the FrameObject if possible.
2068     // So we check if the frame is a module or class level scope
2069     PyCodeObject *co = _PyFrame_GetCode(frame);
2070 
2071     if (!(co->co_flags & CO_OPTIMIZED) && !_PyFrame_HasHiddenLocals(frame)) {
2072         if (frame->f_locals == NULL) {
2073             // We found cases when f_locals is NULL for non-optimized code.
2074             // We fill the f_locals with an empty dict to avoid crash until
2075             // we find the root cause.
2076             frame->f_locals = PyDict_New();
2077             if (frame->f_locals == NULL) {
2078                 return NULL;
2079             }
2080         }
2081         return Py_NewRef(frame->f_locals);
2082     }
2083 
2084     PyFrameObject* f = _PyFrame_GetFrameObject(frame);
2085 
2086     return _PyFrameLocalsProxy_New(f);
2087 }
2088 
2089 
2090 PyObject *
PyFrame_GetVar(PyFrameObject * frame_obj,PyObject * name)2091 PyFrame_GetVar(PyFrameObject *frame_obj, PyObject *name)
2092 {
2093     if (!PyUnicode_Check(name)) {
2094         PyErr_Format(PyExc_TypeError, "name must be str, not %s",
2095                      Py_TYPE(name)->tp_name);
2096         return NULL;
2097     }
2098 
2099     _PyInterpreterFrame *frame = frame_obj->f_frame;
2100     frame_init_get_vars(frame);
2101 
2102     PyCodeObject *co = _PyFrame_GetCode(frame);
2103     for (int i = 0; i < co->co_nlocalsplus; i++) {
2104         PyObject *var_name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
2105         if (!_PyUnicode_Equal(var_name, name)) {
2106             continue;
2107         }
2108 
2109         PyObject *value;  // borrowed reference
2110         if (!frame_get_var(frame, co, i, &value)) {
2111             break;
2112         }
2113         if (value == NULL) {
2114             break;
2115         }
2116         return Py_NewRef(value);
2117     }
2118 
2119     PyErr_Format(PyExc_NameError, "variable %R does not exist", name);
2120     return NULL;
2121 }
2122 
2123 
2124 PyObject *
PyFrame_GetVarString(PyFrameObject * frame,const char * name)2125 PyFrame_GetVarString(PyFrameObject *frame, const char *name)
2126 {
2127     PyObject *name_obj = PyUnicode_FromString(name);
2128     if (name_obj == NULL) {
2129         return NULL;
2130     }
2131     PyObject *value = PyFrame_GetVar(frame, name_obj);
2132     Py_DECREF(name_obj);
2133     return value;
2134 }
2135 
2136 
2137 int
PyFrame_FastToLocalsWithError(PyFrameObject * f)2138 PyFrame_FastToLocalsWithError(PyFrameObject *f)
2139 {
2140     // Nothing to do here, as f_locals is now a write-through proxy in
2141     // optimized frames. Soft-deprecated, since there's no maintenance hassle.
2142     return 0;
2143 }
2144 
2145 void
PyFrame_FastToLocals(PyFrameObject * f)2146 PyFrame_FastToLocals(PyFrameObject *f)
2147 {
2148     // Nothing to do here, as f_locals is now a write-through proxy in
2149     // optimized frames. Soft-deprecated, since there's no maintenance hassle.
2150     return;
2151 }
2152 
2153 void
PyFrame_LocalsToFast(PyFrameObject * f,int clear)2154 PyFrame_LocalsToFast(PyFrameObject *f, int clear)
2155 {
2156     // Nothing to do here, as f_locals is now a write-through proxy in
2157     // optimized frames. Soft-deprecated, since there's no maintenance hassle.
2158     return;
2159 }
2160 
2161 int
_PyFrame_IsEntryFrame(PyFrameObject * frame)2162 _PyFrame_IsEntryFrame(PyFrameObject *frame)
2163 {
2164     assert(frame != NULL);
2165     _PyInterpreterFrame *f = frame->f_frame;
2166     assert(!_PyFrame_IsIncomplete(f));
2167     return f->previous && f->previous->owner == FRAME_OWNED_BY_CSTACK;
2168 }
2169 
2170 PyCodeObject *
PyFrame_GetCode(PyFrameObject * frame)2171 PyFrame_GetCode(PyFrameObject *frame)
2172 {
2173     assert(frame != NULL);
2174     assert(!_PyFrame_IsIncomplete(frame->f_frame));
2175     PyCodeObject *code = _PyFrame_GetCode(frame->f_frame);
2176     assert(code != NULL);
2177     return (PyCodeObject*)Py_NewRef(code);
2178 }
2179 
2180 
2181 PyFrameObject*
PyFrame_GetBack(PyFrameObject * frame)2182 PyFrame_GetBack(PyFrameObject *frame)
2183 {
2184     assert(frame != NULL);
2185     assert(!_PyFrame_IsIncomplete(frame->f_frame));
2186     PyFrameObject *back = frame->f_back;
2187     if (back == NULL) {
2188         _PyInterpreterFrame *prev = frame->f_frame->previous;
2189         prev = _PyFrame_GetFirstComplete(prev);
2190         if (prev) {
2191             back = _PyFrame_GetFrameObject(prev);
2192         }
2193     }
2194     return (PyFrameObject*)Py_XNewRef(back);
2195 }
2196 
2197 PyObject*
PyFrame_GetLocals(PyFrameObject * frame)2198 PyFrame_GetLocals(PyFrameObject *frame)
2199 {
2200     assert(!_PyFrame_IsIncomplete(frame->f_frame));
2201     return frame_getlocals(frame, NULL);
2202 }
2203 
2204 PyObject*
PyFrame_GetGlobals(PyFrameObject * frame)2205 PyFrame_GetGlobals(PyFrameObject *frame)
2206 {
2207     assert(!_PyFrame_IsIncomplete(frame->f_frame));
2208     return frame_getglobals(frame, NULL);
2209 }
2210 
2211 PyObject*
PyFrame_GetBuiltins(PyFrameObject * frame)2212 PyFrame_GetBuiltins(PyFrameObject *frame)
2213 {
2214     assert(!_PyFrame_IsIncomplete(frame->f_frame));
2215     return frame_getbuiltins(frame, NULL);
2216 }
2217 
2218 int
PyFrame_GetLasti(PyFrameObject * frame)2219 PyFrame_GetLasti(PyFrameObject *frame)
2220 {
2221     assert(!_PyFrame_IsIncomplete(frame->f_frame));
2222     int lasti = _PyInterpreterFrame_LASTI(frame->f_frame);
2223     if (lasti < 0) {
2224         return -1;
2225     }
2226     return lasti * sizeof(_Py_CODEUNIT);
2227 }
2228 
2229 PyObject *
PyFrame_GetGenerator(PyFrameObject * frame)2230 PyFrame_GetGenerator(PyFrameObject *frame)
2231 {
2232     assert(!_PyFrame_IsIncomplete(frame->f_frame));
2233     if (frame->f_frame->owner != FRAME_OWNED_BY_GENERATOR) {
2234         return NULL;
2235     }
2236     PyGenObject *gen = _PyFrame_GetGenerator(frame->f_frame);
2237     return Py_NewRef(gen);
2238 }
2239 
2240 PyObject*
_PyEval_BuiltinsFromGlobals(PyThreadState * tstate,PyObject * globals)2241 _PyEval_BuiltinsFromGlobals(PyThreadState *tstate, PyObject *globals)
2242 {
2243     PyObject *builtins = PyDict_GetItemWithError(globals, &_Py_ID(__builtins__));
2244     if (builtins) {
2245         if (PyModule_Check(builtins)) {
2246             builtins = _PyModule_GetDict(builtins);
2247             assert(builtins != NULL);
2248         }
2249         return builtins;
2250     }
2251     if (PyErr_Occurred()) {
2252         return NULL;
2253     }
2254 
2255     return _PyEval_GetBuiltins(tstate);
2256 }
2257