forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frameobject.c
2115 lines (1876 loc) · 60.7 KB
/
frameobject.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Frame object implementation */
#include "Python.h"
#include "pycore_ceval.h" // _PyEval_BuiltinsFromGlobals()
#include "pycore_code.h" // CO_FAST_LOCAL, etc.
#include "pycore_function.h" // _PyFunction_FromConstructor()
#include "pycore_moduleobject.h" // _PyModule_GetDict()
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "pycore_opcode_metadata.h" // _PyOpcode_Deopt, _PyOpcode_Caches
#include "frameobject.h" // PyFrameObject
#include "pycore_frame.h"
#include "opcode.h" // EXTENDED_ARG
#define OFF(x) offsetof(PyFrameObject, x)
// Returns borrowed reference or NULL
static PyObject *
framelocalsproxy_getval(_PyInterpreterFrame *frame, PyCodeObject *co, int i)
{
PyObject **fast = _PyFrame_GetLocalsArray(frame);
_PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i);
PyObject *value = fast[i];
PyObject *cell = NULL;
if (value == NULL) {
return NULL;
}
if (kind == CO_FAST_FREE || kind & CO_FAST_CELL) {
// The cell was set when the frame was created from
// the function's closure.
assert(PyCell_Check(value));
cell = value;
}
if (cell != NULL) {
value = PyCell_GET(cell);
}
if (value == NULL) {
return NULL;
}
return value;
}
static int
framelocalsproxy_getkeyindex(PyFrameObject *frame, PyObject* key, bool read)
{
/*
* Returns the fast locals index of the key
* - if read == true, returns the index if the value is not NULL
* - if read == false, returns the index if the value is not hidden
*/
assert(PyUnicode_CheckExact(key));
PyCodeObject *co = _PyFrame_GetCode(frame->f_frame);
int found_key = false;
// We do 2 loops here because it's highly possible the key is interned
// and we can do a pointer comparison.
for (int i = 0; i < co->co_nlocalsplus; i++) {
PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
if (name == key) {
found_key = true;
if (read) {
if (framelocalsproxy_getval(frame->f_frame, co, i) != NULL) {
return i;
}
} else {
if (!(_PyLocals_GetKind(co->co_localspluskinds, i) & CO_FAST_HIDDEN)) {
return i;
}
}
}
}
if (!found_key) {
// This is unlikely, but we need to make sure. This means the key
// is not interned.
for (int i = 0; i < co->co_nlocalsplus; i++) {
PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
if (_PyUnicode_EQ(name, key)) {
if (read) {
if (framelocalsproxy_getval(frame->f_frame, co, i) != NULL) {
return i;
}
} else {
if (!(_PyLocals_GetKind(co->co_localspluskinds, i) & CO_FAST_HIDDEN)) {
return i;
}
}
}
}
}
return -1;
}
static PyObject *
framelocalsproxy_getitem(PyObject *self, PyObject *key)
{
PyFrameObject* frame = ((PyFrameLocalsProxyObject*)self)->frame;
PyCodeObject* co = _PyFrame_GetCode(frame->f_frame);
if (PyUnicode_CheckExact(key)) {
int i = framelocalsproxy_getkeyindex(frame, key, true);
if (i >= 0) {
PyObject *value = framelocalsproxy_getval(frame->f_frame, co, i);
assert(value != NULL);
return Py_NewRef(value);
}
}
// Okay not in the fast locals, try extra locals
PyObject *extra = frame->f_extra_locals;
if (extra != NULL) {
PyObject *value = PyDict_GetItem(extra, key);
if (value != NULL) {
return Py_NewRef(value);
}
}
PyErr_Format(PyExc_KeyError, "local variable '%R' is not defined", key);
return NULL;
}
static int
framelocalsproxy_setitem(PyObject *self, PyObject *key, PyObject *value)
{
/* Merge locals into fast locals */
PyFrameObject* frame = ((PyFrameLocalsProxyObject*)self)->frame;
PyObject** fast = _PyFrame_GetLocalsArray(frame->f_frame);
PyCodeObject* co = _PyFrame_GetCode(frame->f_frame);
if (value == NULL) {
PyErr_SetString(PyExc_TypeError, "cannot remove variables from FrameLocalsProxy");
return -1;
}
if (PyUnicode_CheckExact(key)) {
int i = framelocalsproxy_getkeyindex(frame, key, false);
if (i >= 0) {
_Py_Executors_InvalidateDependency(PyInterpreterState_Get(), co, 1);
_PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i);
PyObject *oldvalue = fast[i];
PyObject *cell = NULL;
if (kind == CO_FAST_FREE) {
// The cell was set when the frame was created from
// the function's closure.
assert(oldvalue != NULL && PyCell_Check(oldvalue));
cell = oldvalue;
} else if (kind & CO_FAST_CELL && oldvalue != NULL) {
if (PyCell_Check(oldvalue)) {
cell = oldvalue;
}
}
if (cell != NULL) {
oldvalue = PyCell_GET(cell);
if (value != oldvalue) {
PyCell_SET(cell, Py_XNewRef(value));
Py_XDECREF(oldvalue);
}
} else if (value != oldvalue) {
Py_XSETREF(fast[i], Py_NewRef(value));
}
return 0;
}
}
// Okay not in the fast locals, try extra locals
PyObject *extra = frame->f_extra_locals;
if (extra == NULL) {
extra = PyDict_New();
if (extra == NULL) {
return -1;
}
frame->f_extra_locals = extra;
}
assert(PyDict_Check(extra));
return PyDict_SetItem(extra, key, value);
}
static int
framelocalsproxy_merge(PyObject* self, PyObject* other)
{
if (!PyDict_Check(other) && !PyFrameLocalsProxy_Check(other)) {
return -1;
}
PyObject *keys = PyMapping_Keys(other);
if (keys == NULL) {
return -1;
}
PyObject *iter = PyObject_GetIter(keys);
Py_DECREF(keys);
if (iter == NULL) {
return -1;
}
PyObject *key = NULL;
PyObject *value = NULL;
while ((key = PyIter_Next(iter)) != NULL) {
value = PyObject_GetItem(other, key);
if (value == NULL) {
Py_DECREF(key);
Py_DECREF(iter);
return -1;
}
if (framelocalsproxy_setitem(self, key, value) < 0) {
Py_DECREF(key);
Py_DECREF(value);
Py_DECREF(iter);
return -1;
}
Py_DECREF(key);
Py_DECREF(value);
}
Py_DECREF(iter);
return 0;
}
static PyObject *
framelocalsproxy_keys(PyObject *self, void *Py_UNUSED(ignored))
{
PyFrameObject *frame = ((PyFrameLocalsProxyObject*)self)->frame;
PyCodeObject *co = _PyFrame_GetCode(frame->f_frame);
PyObject *names = PyList_New(0);
if (names == NULL) {
return NULL;
}
for (int i = 0; i < co->co_nlocalsplus; i++) {
PyObject *val = framelocalsproxy_getval(frame->f_frame, co, i);
if (val) {
PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
if (PyList_Append(names, name) < 0) {
Py_DECREF(names);
return NULL;
}
}
}
// Iterate through the extra locals
if (frame->f_extra_locals) {
assert(PyDict_Check(frame->f_extra_locals));
Py_ssize_t i = 0;
PyObject *key = NULL;
PyObject *value = NULL;
while (PyDict_Next(frame->f_extra_locals, &i, &key, &value)) {
if (PyList_Append(names, key) < 0) {
Py_DECREF(names);
return NULL;
}
}
}
return names;
}
static void
framelocalsproxy_dealloc(PyObject *self)
{
PyObject_GC_UnTrack(self);
Py_CLEAR(((PyFrameLocalsProxyObject*)self)->frame);
Py_TYPE(self)->tp_free(self);
}
static PyObject *
framelocalsproxy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyFrameLocalsProxyObject *self = (PyFrameLocalsProxyObject *)type->tp_alloc(type, 0);
if (self == NULL) {
return NULL;
}
PyFrameObject *frame = (PyFrameObject*)PyTuple_GET_ITEM(args, 0);
assert(PyFrame_Check(frame));
((PyFrameLocalsProxyObject*)self)->frame = (PyFrameObject*)Py_NewRef(frame);
return (PyObject *)self;
}
static int
framelocalsproxy_tp_clear(PyObject *self)
{
Py_CLEAR(((PyFrameLocalsProxyObject*)self)->frame);
return 0;
}
static int
framelocalsproxy_visit(PyObject *self, visitproc visit, void *arg)
{
Py_VISIT(((PyFrameLocalsProxyObject*)self)->frame);
return 0;
}
static PyObject *
framelocalsproxy_iter(PyObject *self)
{
PyObject* keys = framelocalsproxy_keys(self, NULL);
if (keys == NULL) {
return NULL;
}
PyObject* iter = PyObject_GetIter(keys);
Py_XDECREF(keys);
return iter;
}
static PyObject *
framelocalsproxy_richcompare(PyObject *self, PyObject *other, int op)
{
if (PyFrameLocalsProxy_Check(other)) {
bool result = ((PyFrameLocalsProxyObject*)self)->frame == ((PyFrameLocalsProxyObject*)other)->frame;
if (op == Py_EQ) {
return PyBool_FromLong(result);
} else if (op == Py_NE) {
return PyBool_FromLong(!result);
}
} else if (PyDict_Check(other)) {
PyObject *dct = PyDict_New();
if (dct == NULL) {
return NULL;
}
if (PyDict_Update(dct, self) < 0) {
Py_DECREF(dct);
return NULL;
}
PyObject *result = PyObject_RichCompare(dct, other, op);
Py_DECREF(dct);
return result;
}
Py_RETURN_NOTIMPLEMENTED;
}
static PyObject *
framelocalsproxy_repr(PyObject *self)
{
int i = Py_ReprEnter(self);
if (i != 0) {
return i > 0 ? PyUnicode_FromString("{...}") : NULL;
}
PyObject *dct = PyDict_New();
if (dct == NULL) {
Py_ReprLeave(self);
return NULL;
}
if (PyDict_Update(dct, self) < 0) {
Py_DECREF(dct);
Py_ReprLeave(self);
return NULL;
}
PyObject *repr = PyObject_Repr(dct);
Py_DECREF(dct);
Py_ReprLeave(self);
return repr;
}
static PyObject*
framelocalsproxy_or(PyObject *self, PyObject *other)
{
if (!PyDict_Check(other) && !PyFrameLocalsProxy_Check(other)) {
Py_RETURN_NOTIMPLEMENTED;
}
PyObject *result = PyDict_New();
if (result == NULL) {
return NULL;
}
if (PyDict_Update(result, self) < 0) {
Py_DECREF(result);
return NULL;
}
if (PyDict_Update(result, other) < 0) {
Py_DECREF(result);
return NULL;
}
return result;
}
static PyObject*
framelocalsproxy_inplace_or(PyObject *self, PyObject *other)
{
if (!PyDict_Check(other) && !PyFrameLocalsProxy_Check(other)) {
Py_RETURN_NOTIMPLEMENTED;
}
if (framelocalsproxy_merge(self, other) < 0) {
Py_RETURN_NOTIMPLEMENTED;
}
return Py_NewRef(self);
}
static PyObject*
framelocalsproxy_values(PyObject *self, void *Py_UNUSED(ignored))
{
PyFrameObject *frame = ((PyFrameLocalsProxyObject*)self)->frame;
PyCodeObject *co = _PyFrame_GetCode(frame->f_frame);
PyObject *values = PyList_New(0);
if (values == NULL) {
return NULL;
}
for (int i = 0; i < co->co_nlocalsplus; i++) {
PyObject *value = framelocalsproxy_getval(frame->f_frame, co, i);
if (value) {
if (PyList_Append(values, value) < 0) {
Py_DECREF(values);
return NULL;
}
}
}
// Iterate through the extra locals
if (frame->f_extra_locals) {
Py_ssize_t j = 0;
PyObject *key = NULL;
PyObject *value = NULL;
while (PyDict_Next(frame->f_extra_locals, &j, &key, &value)) {
if (PyList_Append(values, value) < 0) {
Py_DECREF(values);
return NULL;
}
}
}
return values;
}
static PyObject *
framelocalsproxy_items(PyObject *self, void *Py_UNUSED(ignored))
{
PyFrameObject *frame = ((PyFrameLocalsProxyObject*)self)->frame;
PyCodeObject *co = _PyFrame_GetCode(frame->f_frame);
PyObject *items = PyList_New(0);
if (items == NULL) {
return NULL;
}
for (int i = 0; i < co->co_nlocalsplus; i++) {
PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
PyObject *value = framelocalsproxy_getval(frame->f_frame, co, i);
if (value) {
PyObject *pair = PyTuple_Pack(2, name, value);
if (pair == NULL) {
Py_DECREF(items);
return NULL;
}
if (PyList_Append(items, pair) < 0) {
Py_DECREF(items);
Py_DECREF(pair);
return NULL;
}
Py_DECREF(pair);
}
}
// Iterate through the extra locals
if (frame->f_extra_locals) {
Py_ssize_t j = 0;
PyObject *key = NULL;
PyObject *value = NULL;
while (PyDict_Next(frame->f_extra_locals, &j, &key, &value)) {
PyObject *pair = PyTuple_Pack(2, key, value);
if (pair == NULL) {
Py_DECREF(items);
return NULL;
}
if (PyList_Append(items, pair) < 0) {
Py_DECREF(items);
Py_DECREF(pair);
return NULL;
}
Py_DECREF(pair);
}
}
return items;
}
static Py_ssize_t
framelocalsproxy_length(PyObject *self)
{
PyFrameObject *frame = ((PyFrameLocalsProxyObject*)self)->frame;
PyCodeObject *co = _PyFrame_GetCode(frame->f_frame);
Py_ssize_t size = 0;
if (frame->f_extra_locals != NULL) {
assert(PyDict_Check(frame->f_extra_locals));
size += PyDict_Size(frame->f_extra_locals);
}
for (int i = 0; i < co->co_nlocalsplus; i++) {
if (framelocalsproxy_getval(frame->f_frame, co, i) != NULL) {
size++;
}
}
return size;
}
static int
framelocalsproxy_contains(PyObject *self, PyObject *key)
{
PyFrameObject *frame = ((PyFrameLocalsProxyObject*)self)->frame;
if (PyUnicode_CheckExact(key)) {
int i = framelocalsproxy_getkeyindex(frame, key, true);
if (i >= 0) {
return 1;
}
}
PyObject *extra = ((PyFrameObject*)frame)->f_extra_locals;
if (extra != NULL) {
return PyDict_Contains(extra, key);
}
return 0;
}
static PyObject* framelocalsproxy___contains__(PyObject *self, PyObject *key)
{
int result = framelocalsproxy_contains(self, key);
if (result < 0) {
return NULL;
}
return PyBool_FromLong(result);
}
static PyObject*
framelocalsproxy_update(PyObject *self, PyObject *other)
{
if (framelocalsproxy_merge(self, other) < 0) {
PyErr_SetString(PyExc_TypeError, "update() argument must be dict or another FrameLocalsProxy");
return NULL;
}
Py_RETURN_NONE;
}
static PyObject*
framelocalsproxy_get(PyObject* self, PyObject *const *args, Py_ssize_t nargs)
{
if (nargs < 1 || nargs > 2) {
PyErr_SetString(PyExc_TypeError, "get expected 1 or 2 arguments");
return NULL;
}
PyObject *key = args[0];
PyObject *default_value = Py_None;
if (nargs == 2) {
default_value = args[1];
}
PyObject *result = framelocalsproxy_getitem(self, key);
if (result == NULL) {
if (PyErr_ExceptionMatches(PyExc_KeyError)) {
PyErr_Clear();
return Py_XNewRef(default_value);
}
return NULL;
}
return result;
}
static PyObject*
framelocalsproxy_setdefault(PyObject* self, PyObject *const *args, Py_ssize_t nargs)
{
if (nargs < 1 || nargs > 2) {
PyErr_SetString(PyExc_TypeError, "setdefault expected 1 or 2 arguments");
return NULL;
}
PyObject *key = args[0];
PyObject *default_value = Py_None;
if (nargs == 2) {
default_value = args[1];
}
PyObject *result = framelocalsproxy_getitem(self, key);
if (result == NULL) {
if (PyErr_ExceptionMatches(PyExc_KeyError)) {
PyErr_Clear();
if (framelocalsproxy_setitem(self, key, default_value) < 0) {
return NULL;
}
return Py_XNewRef(default_value);
}
return NULL;
}
return result;
}
static PyObject*
framelocalsproxy_copy(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject* result = PyDict_New();
if (result == NULL) {
return NULL;
}
if (PyDict_Update(result, self) < 0) {
Py_DECREF(result);
return NULL;
}
return result;
}
static PyObject*
framelocalsproxy_reversed(PyObject *self, void *Py_UNUSED(ignored))
{
PyObject *result = framelocalsproxy_keys(self, NULL);
if (result == NULL) {
return NULL;
}
if (PyList_Reverse(result) < 0) {
Py_DECREF(result);
return NULL;
}
return result;
}
static PyNumberMethods framelocalsproxy_as_number = {
.nb_or = framelocalsproxy_or,
.nb_inplace_or = framelocalsproxy_inplace_or,
};
static PySequenceMethods framelocalsproxy_as_sequence = {
.sq_contains = framelocalsproxy_contains,
};
static PyMappingMethods framelocalsproxy_as_mapping = {
framelocalsproxy_length, // mp_length
framelocalsproxy_getitem, // mp_subscript
framelocalsproxy_setitem, // mp_ass_subscript
};
static PyMethodDef framelocalsproxy_methods[] = {
{"__contains__", framelocalsproxy___contains__, METH_O | METH_COEXIST,
NULL},
{"__getitem__", framelocalsproxy_getitem, METH_O | METH_COEXIST,
NULL},
{"update", framelocalsproxy_update, METH_O,
NULL},
{"__reversed__", _PyCFunction_CAST(framelocalsproxy_reversed), METH_NOARGS,
NULL},
{"copy", _PyCFunction_CAST(framelocalsproxy_copy), METH_NOARGS,
NULL},
{"keys", _PyCFunction_CAST(framelocalsproxy_keys), METH_NOARGS,
NULL},
{"values", _PyCFunction_CAST(framelocalsproxy_values), METH_NOARGS,
NULL},
{"items", _PyCFunction_CAST(framelocalsproxy_items), METH_NOARGS,
NULL},
{"get", _PyCFunction_CAST(framelocalsproxy_get), METH_FASTCALL,
NULL},
{"setdefault", _PyCFunction_CAST(framelocalsproxy_setdefault), METH_FASTCALL,
NULL},
{NULL, NULL} /* sentinel */
};
PyTypeObject PyFrameLocalsProxy_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "FrameLocalsProxy",
.tp_basicsize = sizeof(PyFrameLocalsProxyObject),
.tp_dealloc = (destructor)framelocalsproxy_dealloc,
.tp_repr = &framelocalsproxy_repr,
.tp_as_number = &framelocalsproxy_as_number,
.tp_as_sequence = &framelocalsproxy_as_sequence,
.tp_as_mapping = &framelocalsproxy_as_mapping,
.tp_getattro = PyObject_GenericGetAttr,
.tp_setattro = PyObject_GenericSetAttr,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
.tp_traverse = framelocalsproxy_visit,
.tp_clear = framelocalsproxy_tp_clear,
.tp_richcompare = framelocalsproxy_richcompare,
.tp_iter = framelocalsproxy_iter,
.tp_methods = framelocalsproxy_methods,
.tp_alloc = PyType_GenericAlloc,
.tp_new = framelocalsproxy_new,
.tp_free = PyObject_GC_Del,
};
PyObject *
_PyFrameLocalsProxy_New(PyFrameObject *frame)
{
PyObject* args = PyTuple_Pack(1, frame);
if (args == NULL) {
return NULL;
}
PyObject* proxy = (PyObject*)framelocalsproxy_new(&PyFrameLocalsProxy_Type, args, NULL);
Py_DECREF(args);
return proxy;
}
static PyMemberDef frame_memberlist[] = {
{"f_trace_lines", Py_T_BOOL, OFF(f_trace_lines), 0},
{NULL} /* Sentinel */
};
static PyObject *
frame_getlocals(PyFrameObject *f, void *closure)
{
if (f == NULL) {
PyErr_BadInternalCall();
return NULL;
}
assert(!_PyFrame_IsIncomplete(f->f_frame));
PyCodeObject *co = _PyFrame_GetCode(f->f_frame);
if (!(co->co_flags & CO_OPTIMIZED) && !_PyFrame_HasHiddenLocals(f->f_frame)) {
if (f->f_frame->f_locals == NULL) {
// We found cases when f_locals is NULL for non-optimized code.
// We fill the f_locals with an empty dict to avoid crash until
// we find the root cause.
f->f_frame->f_locals = PyDict_New();
if (f->f_frame->f_locals == NULL) {
return NULL;
}
}
return Py_NewRef(f->f_frame->f_locals);
}
return _PyFrameLocalsProxy_New(f);
}
int
PyFrame_GetLineNumber(PyFrameObject *f)
{
assert(f != NULL);
if (f->f_lineno == -1) {
// We should calculate it once. If we can't get the line number,
// set f->f_lineno to 0.
f->f_lineno = PyUnstable_InterpreterFrame_GetLine(f->f_frame);
if (f->f_lineno < 0) {
f->f_lineno = 0;
return -1;
}
}
if (f->f_lineno > 0) {
return f->f_lineno;
}
return PyUnstable_InterpreterFrame_GetLine(f->f_frame);
}
static PyObject *
frame_getlineno(PyFrameObject *f, void *closure)
{
int lineno = PyFrame_GetLineNumber(f);
if (lineno < 0) {
Py_RETURN_NONE;
}
else {
return PyLong_FromLong(lineno);
}
}
static PyObject *
frame_getlasti(PyFrameObject *f, void *closure)
{
int lasti = _PyInterpreterFrame_LASTI(f->f_frame);
if (lasti < 0) {
return PyLong_FromLong(-1);
}
return PyLong_FromLong(lasti * sizeof(_Py_CODEUNIT));
}
static PyObject *
frame_getglobals(PyFrameObject *f, void *closure)
{
PyObject *globals = f->f_frame->f_globals;
if (globals == NULL) {
globals = Py_None;
}
return Py_NewRef(globals);
}
static PyObject *
frame_getbuiltins(PyFrameObject *f, void *closure)
{
PyObject *builtins = f->f_frame->f_builtins;
if (builtins == NULL) {
builtins = Py_None;
}
return Py_NewRef(builtins);
}
static PyObject *
frame_getcode(PyFrameObject *f, void *closure)
{
if (PySys_Audit("object.__getattr__", "Os", f, "f_code") < 0) {
return NULL;
}
return (PyObject *)PyFrame_GetCode(f);
}
static PyObject *
frame_getback(PyFrameObject *f, void *closure)
{
PyObject *res = (PyObject *)PyFrame_GetBack(f);
if (res == NULL) {
Py_RETURN_NONE;
}
return res;
}
static PyObject *
frame_gettrace_opcodes(PyFrameObject *f, void *closure)
{
PyObject *result = f->f_trace_opcodes ? Py_True : Py_False;
return Py_NewRef(result);
}
static int
frame_settrace_opcodes(PyFrameObject *f, PyObject* value, void *Py_UNUSED(ignored))
{
if (!PyBool_Check(value)) {
PyErr_SetString(PyExc_TypeError,
"attribute value type must be bool");
return -1;
}
if (value == Py_True) {
f->f_trace_opcodes = 1;
if (f->f_trace) {
return _PyEval_SetOpcodeTrace(f, true);
}
}
else {
f->f_trace_opcodes = 0;
return _PyEval_SetOpcodeTrace(f, false);
}
return 0;
}
/* Model the evaluation stack, to determine which jumps
* are safe and how many values needs to be popped.
* The stack is modelled by a 64 integer, treating any
* stack that can't fit into 64 bits as "overflowed".
*/
typedef enum kind {
Iterator = 1,
Except = 2,
Object = 3,
Null = 4,
Lasti = 5,
} Kind;
static int
compatible_kind(Kind from, Kind to) {
if (to == 0) {
return 0;
}
if (to == Object) {
return from != Null;
}
if (to == Null) {
return 1;
}
return from == to;
}
#define BITS_PER_BLOCK 3
#define UNINITIALIZED -2
#define OVERFLOWED -1
#define MAX_STACK_ENTRIES (63/BITS_PER_BLOCK)
#define WILL_OVERFLOW (1ULL<<((MAX_STACK_ENTRIES-1)*BITS_PER_BLOCK))
#define EMPTY_STACK 0
static inline int64_t
push_value(int64_t stack, Kind kind)
{
if (((uint64_t)stack) >= WILL_OVERFLOW) {
return OVERFLOWED;
}
else {
return (stack << BITS_PER_BLOCK) | kind;
}
}
static inline int64_t
pop_value(int64_t stack)
{
return Py_ARITHMETIC_RIGHT_SHIFT(int64_t, stack, BITS_PER_BLOCK);
}
#define MASK ((1<<BITS_PER_BLOCK)-1)
static inline Kind
top_of_stack(int64_t stack)
{
return stack & MASK;
}
static inline Kind
peek(int64_t stack, int n)
{
assert(n >= 1);
return (stack>>(BITS_PER_BLOCK*(n-1))) & MASK;
}
static Kind
stack_swap(int64_t stack, int n)
{
assert(n >= 1);
Kind to_swap = peek(stack, n);
Kind top = top_of_stack(stack);
int shift = BITS_PER_BLOCK*(n-1);
int64_t replaced_low = (stack & ~(MASK << shift)) | (top << shift);
int64_t replaced_top = (replaced_low & ~MASK) | to_swap;
return replaced_top;
}
static int64_t
pop_to_level(int64_t stack, int level) {
if (level == 0) {
return EMPTY_STACK;
}
int64_t max_item = (1<<BITS_PER_BLOCK) - 1;
int64_t level_max_stack = max_item << ((level-1) * BITS_PER_BLOCK);
while (stack > level_max_stack) {
stack = pop_value(stack);
}
return stack;
}
#if 0
/* These functions are useful for debugging the stack marking code */
static char
tos_char(int64_t stack) {
switch(top_of_stack(stack)) {
case Iterator:
return 'I';
case Except:
return 'E';
case Object:
return 'O';
case Lasti:
return 'L';
case Null:
return 'N';
}
return '?';