Coverage Report

Created: 2025-08-26 06:26

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