1 /* Frame object implementation */
2 
3 #include "Python.h"
4 
5 #include "code.h"
6 #include "frameobject.h"
7 #include "opcode.h"
8 #include "structmember.h"
9 
10 #undef MIN
11 #undef MAX
12 #define MIN(a, b) ((a) < (b) ? (a) : (b))
13 #define MAX(a, b) ((a) > (b) ? (a) : (b))
14 
15 #define OFF(x) offsetof(PyFrameObject, x)
16 
17 static PyMemberDef frame_memberlist[] = {
18     {"f_back",          T_OBJECT,       OFF(f_back),    RO},
19     {"f_code",          T_OBJECT,       OFF(f_code),    RO},
20     {"f_builtins",      T_OBJECT,       OFF(f_builtins),RO},
21     {"f_globals",       T_OBJECT,       OFF(f_globals), RO},
22     {"f_lasti",         T_INT,          OFF(f_lasti),   RO},
23     {NULL}      /* Sentinel */
24 };
25 
26 #define WARN_GET_SET(NAME) \
27 static PyObject * frame_get_ ## NAME(PyFrameObject *f) { \
28     if (PyErr_WarnPy3k(#NAME " has been removed in 3.x", 2) < 0) \
29         return NULL; \
30     if (f->NAME) { \
31         Py_INCREF(f->NAME); \
32         return f->NAME; \
33     } \
34     Py_RETURN_NONE;     \
35 } \
36 static int frame_set_ ## NAME(PyFrameObject *f, PyObject *new) { \
37     if (PyErr_WarnPy3k(#NAME " has been removed in 3.x", 2) < 0) \
38         return -1; \
39     if (f->NAME) { \
40         Py_CLEAR(f->NAME); \
41     } \
42     if (new == Py_None) \
43         new = NULL; \
44     Py_XINCREF(new); \
45     f->NAME = new; \
46     return 0; \
47 }
48 
49 
50 WARN_GET_SET(f_exc_traceback)
WARN_GET_SET(f_exc_type)51 WARN_GET_SET(f_exc_type)
52 WARN_GET_SET(f_exc_value)
53 
54 
55 static PyObject *
56 frame_getlocals(PyFrameObject *f, void *closure)
57 {
58     PyFrame_FastToLocals(f);
59     Py_INCREF(f->f_locals);
60     return f->f_locals;
61 }
62 
63 int
PyFrame_GetLineNumber(PyFrameObject * f)64 PyFrame_GetLineNumber(PyFrameObject *f)
65 {
66     if (f->f_trace)
67         return f->f_lineno;
68     else
69         return PyCode_Addr2Line(f->f_code, f->f_lasti);
70 }
71 
72 static PyObject *
frame_getlineno(PyFrameObject * f,void * closure)73 frame_getlineno(PyFrameObject *f, void *closure)
74 {
75     return PyInt_FromLong(PyFrame_GetLineNumber(f));
76 }
77 
78 /* Setter for f_lineno - you can set f_lineno from within a trace function in
79  * order to jump to a given line of code, subject to some restrictions.  Most
80  * lines are OK to jump to because they don't make any assumptions about the
81  * state of the stack (obvious because you could remove the line and the code
82  * would still work without any stack errors), but there are some constructs
83  * that limit jumping:
84  *
85  *  o Lines with an 'except' statement on them can't be jumped to, because
86  *    they expect an exception to be on the top of the stack.
87  *  o Lines that live in a 'finally' block can't be jumped from or to, since
88  *    the END_FINALLY expects to clean up the stack after the 'try' block.
89  *  o 'try'/'for'/'while' blocks can't be jumped into because the blockstack
90  *    needs to be set up before their code runs, and for 'for' loops the
91  *    iterator needs to be on the stack.
92  *  o Jumps cannot be made from within a trace function invoked with a
93  *    'return' or 'exception' event since the eval loop has been exited at
94  *    that time.
95  */
96 static int
frame_setlineno(PyFrameObject * f,PyObject * p_new_lineno)97 frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno)
98 {
99     int new_lineno = 0;                 /* The new value of f_lineno */
100     int new_lasti = 0;                  /* The new value of f_lasti */
101     int new_iblock = 0;                 /* The new value of f_iblock */
102     unsigned char *code = NULL;         /* The bytecode for the frame... */
103     Py_ssize_t code_len = 0;            /* ...and its length */
104     unsigned char *lnotab = NULL;       /* Iterating over co_lnotab */
105     Py_ssize_t lnotab_len = 0;          /* (ditto) */
106     int offset = 0;                     /* (ditto) */
107     int line = 0;                       /* (ditto) */
108     int addr = 0;                       /* (ditto) */
109     int min_addr = 0;                   /* Scanning the SETUPs and POPs */
110     int max_addr = 0;                   /* (ditto) */
111     int delta_iblock = 0;               /* (ditto) */
112     int min_delta_iblock = 0;           /* (ditto) */
113     int min_iblock = 0;                 /* (ditto) */
114     int f_lasti_setup_addr = 0;         /* Policing no-jump-into-finally */
115     int new_lasti_setup_addr = 0;       /* (ditto) */
116     int blockstack[CO_MAXBLOCKS];       /* Walking the 'finally' blocks */
117     int in_finally[CO_MAXBLOCKS];       /* (ditto) */
118     int blockstack_top = 0;             /* (ditto) */
119     unsigned char setup_op = 0;         /* (ditto) */
120 
121     if (p_new_lineno == NULL) {
122         PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
123         return -1;
124     }
125     /* f_lineno must be an integer. */
126     if (!PyInt_Check(p_new_lineno)) {
127         PyErr_SetString(PyExc_ValueError,
128                         "lineno must be an integer");
129         return -1;
130     }
131 
132     /* Upon the 'call' trace event of a new frame, f->f_lasti is -1 and
133      * f->f_trace is NULL, check first on the first condition.
134      * Forbidding jumps from the 'call' event of a new frame is a side effect
135      * of allowing to set f_lineno only from trace functions. */
136     if (f->f_lasti == -1) {
137         PyErr_Format(PyExc_ValueError,
138                      "can't jump from the 'call' trace event of a new frame");
139         return -1;
140     }
141 
142     /* You can only do this from within a trace function, not via
143      * _getframe or similar hackery. */
144     if (!f->f_trace) {
145         PyErr_Format(PyExc_ValueError,
146                      "f_lineno can only be set by a trace function");
147         return -1;
148     }
149 
150     /* Forbid jumps upon a 'return' trace event (except after executing a
151      * YIELD_VALUE opcode, f_stacktop is not NULL in that case) and upon an
152      * 'exception' trace event.
153      * Jumps from 'call' trace events have already been forbidden above for new
154      * frames, so this check does not change anything for 'call' events. */
155     if (f->f_stacktop == NULL) {
156         PyErr_SetString(PyExc_ValueError,
157                 "can only jump from a 'line' trace event");
158         return -1;
159     }
160 
161     /* Fail if the line comes before the start of the code block. */
162     new_lineno = (int) PyInt_AsLong(p_new_lineno);
163     if (new_lineno < f->f_code->co_firstlineno) {
164         PyErr_Format(PyExc_ValueError,
165                      "line %d comes before the current code block",
166                      new_lineno);
167         return -1;
168     }
169     else if (new_lineno == f->f_code->co_firstlineno) {
170         new_lasti = 0;
171         new_lineno = f->f_code->co_firstlineno;
172     }
173     else {
174         /* Find the bytecode offset for the start of the given
175          * line, or the first code-owning line after it. */
176         char *tmp;
177         PyString_AsStringAndSize(f->f_code->co_lnotab,
178                                  &tmp, &lnotab_len);
179         lnotab = (unsigned char *) tmp;
180         addr = 0;
181         line = f->f_code->co_firstlineno;
182         new_lasti = -1;
183         for (offset = 0; offset < lnotab_len; offset += 2) {
184             addr += lnotab[offset];
185             line += lnotab[offset+1];
186             if (line >= new_lineno) {
187                 new_lasti = addr;
188                 new_lineno = line;
189                 break;
190             }
191         }
192     }
193 
194     /* If we didn't reach the requested line, return an error. */
195     if (new_lasti == -1) {
196         PyErr_Format(PyExc_ValueError,
197                      "line %d comes after the current code block",
198                      new_lineno);
199         return -1;
200     }
201 
202     /* We're now ready to look at the bytecode. */
203     PyString_AsStringAndSize(f->f_code->co_code, (char **)&code, &code_len);
204     min_addr = MIN(new_lasti, f->f_lasti);
205     max_addr = MAX(new_lasti, f->f_lasti);
206 
207     /* The trace function is called with a 'return' trace event after the
208      * execution of a yield statement. */
209     assert(f->f_lasti != -1);
210     if (code[f->f_lasti] == YIELD_VALUE) {
211         PyErr_SetString(PyExc_ValueError,
212                 "can't jump from a yield statement");
213         return -1;
214     }
215 
216     /* You can't jump onto a line with an 'except' statement on it -
217      * they expect to have an exception on the top of the stack, which
218      * won't be true if you jump to them.  They always start with code
219      * that either pops the exception using POP_TOP (plain 'except:'
220      * lines do this) or duplicates the exception on the stack using
221      * DUP_TOP (if there's an exception type specified).  See compile.c,
222      * 'com_try_except' for the full details.  There aren't any other
223      * cases (AFAIK) where a line's code can start with DUP_TOP or
224      * POP_TOP, but if any ever appear, they'll be subject to the same
225      * restriction (but with a different error message). */
226     if (code[new_lasti] == DUP_TOP || code[new_lasti] == POP_TOP) {
227         PyErr_SetString(PyExc_ValueError,
228             "can't jump to 'except' line as there's no exception");
229         return -1;
230     }
231 
232     /* You can't jump into or out of a 'finally' block because the 'try'
233      * block leaves something on the stack for the END_FINALLY to clean
234      * up.      So we walk the bytecode, maintaining a simulated blockstack.
235      * When we reach the old or new address and it's in a 'finally' block
236      * we note the address of the corresponding SETUP_FINALLY.  The jump
237      * is only legal if neither address is in a 'finally' block or
238      * they're both in the same one.  'blockstack' is a stack of the
239      * bytecode addresses of the SETUP_X opcodes, and 'in_finally' tracks
240      * whether we're in a 'finally' block at each blockstack level. */
241     f_lasti_setup_addr = -1;
242     new_lasti_setup_addr = -1;
243     memset(blockstack, '\0', sizeof(blockstack));
244     memset(in_finally, '\0', sizeof(in_finally));
245     blockstack_top = 0;
246     for (addr = 0; addr < code_len; addr++) {
247         unsigned char op = code[addr];
248         switch (op) {
249         case SETUP_LOOP:
250         case SETUP_EXCEPT:
251         case SETUP_FINALLY:
252         case SETUP_WITH:
253             blockstack[blockstack_top++] = addr;
254             in_finally[blockstack_top-1] = 0;
255             break;
256 
257         case POP_BLOCK:
258             assert(blockstack_top > 0);
259             setup_op = code[blockstack[blockstack_top-1]];
260             if (setup_op == SETUP_FINALLY || setup_op == SETUP_WITH) {
261                 in_finally[blockstack_top-1] = 1;
262             }
263             else {
264                 blockstack_top--;
265             }
266             break;
267 
268         case END_FINALLY:
269             /* Ignore END_FINALLYs for SETUP_EXCEPTs - they exist
270              * in the bytecode but don't correspond to an actual
271              * 'finally' block.  (If blockstack_top is 0, we must
272              * be seeing such an END_FINALLY.) */
273             if (blockstack_top > 0) {
274                 setup_op = code[blockstack[blockstack_top-1]];
275                 if (setup_op == SETUP_FINALLY || setup_op == SETUP_WITH) {
276                     blockstack_top--;
277                 }
278             }
279             break;
280         }
281 
282         /* For the addresses we're interested in, see whether they're
283          * within a 'finally' block and if so, remember the address
284          * of the SETUP_FINALLY. */
285         if (addr == new_lasti || addr == f->f_lasti) {
286             int i = 0;
287             int setup_addr = -1;
288             for (i = blockstack_top-1; i >= 0; i--) {
289                 if (in_finally[i]) {
290                     setup_addr = blockstack[i];
291                     break;
292                 }
293             }
294 
295             if (setup_addr != -1) {
296                 if (addr == new_lasti) {
297                     new_lasti_setup_addr = setup_addr;
298                 }
299 
300                 if (addr == f->f_lasti) {
301                     f_lasti_setup_addr = setup_addr;
302                 }
303             }
304         }
305 
306         if (op >= HAVE_ARGUMENT) {
307             addr += 2;
308         }
309     }
310 
311     /* Verify that the blockstack tracking code didn't get lost. */
312     assert(blockstack_top == 0);
313 
314     /* After all that, are we jumping into / out of a 'finally' block? */
315     if (new_lasti_setup_addr != f_lasti_setup_addr) {
316         PyErr_SetString(PyExc_ValueError,
317                     "can't jump into or out of a 'finally' block");
318         return -1;
319     }
320 
321 
322     /* Police block-jumping (you can't jump into the middle of a block)
323      * and ensure that the blockstack finishes up in a sensible state (by
324      * popping any blocks we're jumping out of).  We look at all the
325      * blockstack operations between the current position and the new
326      * one, and keep track of how many blocks we drop out of on the way.
327      * By also keeping track of the lowest blockstack position we see, we
328      * can tell whether the jump goes into any blocks without coming out
329      * again - in that case we raise an exception below. */
330     delta_iblock = 0;
331     for (addr = min_addr; addr < max_addr; addr++) {
332         unsigned char op = code[addr];
333         switch (op) {
334         case SETUP_LOOP:
335         case SETUP_EXCEPT:
336         case SETUP_FINALLY:
337         case SETUP_WITH:
338             delta_iblock++;
339             break;
340 
341         case POP_BLOCK:
342             delta_iblock--;
343             break;
344         }
345 
346         min_delta_iblock = MIN(min_delta_iblock, delta_iblock);
347 
348         if (op >= HAVE_ARGUMENT) {
349             addr += 2;
350         }
351     }
352 
353     /* Derive the absolute iblock values from the deltas. */
354     min_iblock = f->f_iblock + min_delta_iblock;
355     if (new_lasti > f->f_lasti) {
356         /* Forwards jump. */
357         new_iblock = f->f_iblock + delta_iblock;
358     }
359     else {
360         /* Backwards jump. */
361         new_iblock = f->f_iblock - delta_iblock;
362     }
363 
364     /* Are we jumping into a block? */
365     if (new_iblock > min_iblock) {
366         PyErr_SetString(PyExc_ValueError,
367                         "can't jump into the middle of a block");
368         return -1;
369     }
370 
371     /* Pop any blocks that we're jumping out of. */
372     while (f->f_iblock > new_iblock) {
373         PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
374         while ((f->f_stacktop - f->f_valuestack) > b->b_level) {
375             PyObject *v = (*--f->f_stacktop);
376             Py_DECREF(v);
377         }
378         if (b->b_type == SETUP_WITH) {
379             /* Pop the exit function. */
380             PyObject *v = (*--f->f_stacktop);
381             Py_DECREF(v);
382         }
383     }
384 
385     /* Finally set the new f_lineno and f_lasti and return OK. */
386     f->f_lineno = new_lineno;
387     f->f_lasti = new_lasti;
388     return 0;
389 }
390 
391 static PyObject *
frame_gettrace(PyFrameObject * f,void * closure)392 frame_gettrace(PyFrameObject *f, void *closure)
393 {
394     PyObject* trace = f->f_trace;
395 
396     if (trace == NULL)
397         trace = Py_None;
398 
399     Py_INCREF(trace);
400 
401     return trace;
402 }
403 
404 static int
frame_settrace(PyFrameObject * f,PyObject * v,void * closure)405 frame_settrace(PyFrameObject *f, PyObject* v, void *closure)
406 {
407     /* We rely on f_lineno being accurate when f_trace is set. */
408     f->f_lineno = PyFrame_GetLineNumber(f);
409 
410     if (v == Py_None)
411         v = NULL;
412     Py_XINCREF(v);
413     Py_XSETREF(f->f_trace, v);
414 
415     return 0;
416 }
417 
418 static PyObject *
frame_getrestricted(PyFrameObject * f,void * closure)419 frame_getrestricted(PyFrameObject *f, void *closure)
420 {
421     return PyBool_FromLong(PyFrame_IsRestricted(f));
422 }
423 
424 static PyGetSetDef frame_getsetlist[] = {
425     {"f_locals",        (getter)frame_getlocals, NULL, NULL},
426     {"f_lineno",        (getter)frame_getlineno,
427                     (setter)frame_setlineno, NULL},
428     {"f_trace",         (getter)frame_gettrace, (setter)frame_settrace, NULL},
429     {"f_restricted",(getter)frame_getrestricted,NULL, NULL},
430     {"f_exc_traceback", (getter)frame_get_f_exc_traceback,
431                     (setter)frame_set_f_exc_traceback, NULL},
432     {"f_exc_type",  (getter)frame_get_f_exc_type,
433                     (setter)frame_set_f_exc_type, NULL},
434     {"f_exc_value", (getter)frame_get_f_exc_value,
435                     (setter)frame_set_f_exc_value, NULL},
436     {0}
437 };
438 
439 /* Stack frames are allocated and deallocated at a considerable rate.
440    In an attempt to improve the speed of function calls, we:
441 
442    1. Hold a single "zombie" frame on each code object. This retains
443    the allocated and initialised frame object from an invocation of
444    the code object. The zombie is reanimated the next time we need a
445    frame object for that code object. Doing this saves the malloc/
446    realloc required when using a free_list frame that isn't the
447    correct size. It also saves some field initialisation.
448 
449    In zombie mode, no field of PyFrameObject holds a reference, but
450    the following fields are still valid:
451 
452      * ob_type, ob_size, f_code, f_valuestack;
453 
454      * f_locals, f_trace,
455        f_exc_type, f_exc_value, f_exc_traceback are NULL;
456 
457      * f_localsplus does not require re-allocation and
458        the local variables in f_localsplus are NULL.
459 
460    2. We also maintain a separate free list of stack frames (just like
461    integers are allocated in a special way -- see intobject.c).  When
462    a stack frame is on the free list, only the following members have
463    a meaning:
464     ob_type             == &Frametype
465     f_back              next item on free list, or NULL
466     f_stacksize         size of value stack
467     ob_size             size of localsplus
468    Note that the value and block stacks are preserved -- this can save
469    another malloc() call or two (and two free() calls as well!).
470    Also note that, unlike for integers, each frame object is a
471    malloc'ed object in its own right -- it is only the actual calls to
472    malloc() that we are trying to save here, not the administration.
473    After all, while a typical program may make millions of calls, a
474    call depth of more than 20 or 30 is probably already exceptional
475    unless the program contains run-away recursion.  I hope.
476 
477    Later, PyFrame_MAXFREELIST was added to bound the # of frames saved on
478    free_list.  Else programs creating lots of cyclic trash involving
479    frames could provoke free_list into growing without bound.
480 */
481 
482 static PyFrameObject *free_list = NULL;
483 static int numfree = 0;         /* number of frames currently in free_list */
484 /* max value for numfree */
485 #define PyFrame_MAXFREELIST 200
486 
487 static void
frame_dealloc(PyFrameObject * f)488 frame_dealloc(PyFrameObject *f)
489 {
490     PyObject **p, **valuestack;
491     PyCodeObject *co;
492 
493     PyObject_GC_UnTrack(f);
494     Py_TRASHCAN_SAFE_BEGIN(f)
495     /* Kill all local variables */
496     valuestack = f->f_valuestack;
497     for (p = f->f_localsplus; p < valuestack; p++)
498         Py_CLEAR(*p);
499 
500     /* Free stack */
501     if (f->f_stacktop != NULL) {
502         for (p = valuestack; p < f->f_stacktop; p++)
503             Py_XDECREF(*p);
504     }
505 
506     Py_XDECREF(f->f_back);
507     Py_DECREF(f->f_builtins);
508     Py_DECREF(f->f_globals);
509     Py_CLEAR(f->f_locals);
510     Py_CLEAR(f->f_trace);
511     Py_CLEAR(f->f_exc_type);
512     Py_CLEAR(f->f_exc_value);
513     Py_CLEAR(f->f_exc_traceback);
514 
515     co = f->f_code;
516     if (co->co_zombieframe == NULL)
517         co->co_zombieframe = f;
518     else if (numfree < PyFrame_MAXFREELIST) {
519         ++numfree;
520         f->f_back = free_list;
521         free_list = f;
522     }
523     else
524         PyObject_GC_Del(f);
525 
526     Py_DECREF(co);
527     Py_TRASHCAN_SAFE_END(f)
528 }
529 
530 static int
frame_traverse(PyFrameObject * f,visitproc visit,void * arg)531 frame_traverse(PyFrameObject *f, visitproc visit, void *arg)
532 {
533     PyObject **fastlocals, **p;
534     int i, slots;
535 
536     Py_VISIT(f->f_back);
537     Py_VISIT(f->f_code);
538     Py_VISIT(f->f_builtins);
539     Py_VISIT(f->f_globals);
540     Py_VISIT(f->f_locals);
541     Py_VISIT(f->f_trace);
542     Py_VISIT(f->f_exc_type);
543     Py_VISIT(f->f_exc_value);
544     Py_VISIT(f->f_exc_traceback);
545 
546     /* locals */
547     slots = f->f_code->co_nlocals + PyTuple_GET_SIZE(f->f_code->co_cellvars) + PyTuple_GET_SIZE(f->f_code->co_freevars);
548     fastlocals = f->f_localsplus;
549     for (i = slots; --i >= 0; ++fastlocals)
550         Py_VISIT(*fastlocals);
551 
552     /* stack */
553     if (f->f_stacktop != NULL) {
554         for (p = f->f_valuestack; p < f->f_stacktop; p++)
555             Py_VISIT(*p);
556     }
557     return 0;
558 }
559 
560 static void
frame_clear(PyFrameObject * f)561 frame_clear(PyFrameObject *f)
562 {
563     PyObject **fastlocals, **p, **oldtop;
564     int i, slots;
565 
566     /* Before anything else, make sure that this frame is clearly marked
567      * as being defunct!  Else, e.g., a generator reachable from this
568      * frame may also point to this frame, believe itself to still be
569      * active, and try cleaning up this frame again.
570      */
571     oldtop = f->f_stacktop;
572     f->f_stacktop = NULL;
573 
574     Py_CLEAR(f->f_exc_type);
575     Py_CLEAR(f->f_exc_value);
576     Py_CLEAR(f->f_exc_traceback);
577     Py_CLEAR(f->f_trace);
578 
579     /* locals */
580     slots = f->f_code->co_nlocals + PyTuple_GET_SIZE(f->f_code->co_cellvars) + PyTuple_GET_SIZE(f->f_code->co_freevars);
581     fastlocals = f->f_localsplus;
582     for (i = slots; --i >= 0; ++fastlocals)
583         Py_CLEAR(*fastlocals);
584 
585     /* stack */
586     if (oldtop != NULL) {
587         for (p = f->f_valuestack; p < oldtop; p++)
588             Py_CLEAR(*p);
589     }
590 }
591 
592 static PyObject *
frame_sizeof(PyFrameObject * f)593 frame_sizeof(PyFrameObject *f)
594 {
595     Py_ssize_t res, extras, ncells, nfrees;
596 
597     ncells = PyTuple_GET_SIZE(f->f_code->co_cellvars);
598     nfrees = PyTuple_GET_SIZE(f->f_code->co_freevars);
599     extras = f->f_code->co_stacksize + f->f_code->co_nlocals +
600              ncells + nfrees;
601     /* subtract one as it is already included in PyFrameObject */
602     res = sizeof(PyFrameObject) + (extras-1) * sizeof(PyObject *);
603 
604     return PyInt_FromSsize_t(res);
605 }
606 
607 PyDoc_STRVAR(sizeof__doc__,
608 "F.__sizeof__() -> size of F in memory, in bytes");
609 
610 static PyMethodDef frame_methods[] = {
611     {"__sizeof__",      (PyCFunction)frame_sizeof,      METH_NOARGS,
612      sizeof__doc__},
613     {NULL,              NULL}   /* sentinel */
614 };
615 
616 PyTypeObject PyFrame_Type = {
617     PyVarObject_HEAD_INIT(&PyType_Type, 0)
618     "frame",
619     sizeof(PyFrameObject),
620     sizeof(PyObject *),
621     (destructor)frame_dealloc,                  /* tp_dealloc */
622     0,                                          /* tp_print */
623     0,                                          /* tp_getattr */
624     0,                                          /* tp_setattr */
625     0,                                          /* tp_compare */
626     0,                                          /* tp_repr */
627     0,                                          /* tp_as_number */
628     0,                                          /* tp_as_sequence */
629     0,                                          /* tp_as_mapping */
630     0,                                          /* tp_hash */
631     0,                                          /* tp_call */
632     0,                                          /* tp_str */
633     PyObject_GenericGetAttr,                    /* tp_getattro */
634     PyObject_GenericSetAttr,                    /* tp_setattro */
635     0,                                          /* tp_as_buffer */
636     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
637     0,                                          /* tp_doc */
638     (traverseproc)frame_traverse,               /* tp_traverse */
639     (inquiry)frame_clear,                       /* tp_clear */
640     0,                                          /* tp_richcompare */
641     0,                                          /* tp_weaklistoffset */
642     0,                                          /* tp_iter */
643     0,                                          /* tp_iternext */
644     frame_methods,                              /* tp_methods */
645     frame_memberlist,                           /* tp_members */
646     frame_getsetlist,                           /* tp_getset */
647     0,                                          /* tp_base */
648     0,                                          /* tp_dict */
649 };
650 
651 static PyObject *builtin_object;
652 
_PyFrame_Init()653 int _PyFrame_Init()
654 {
655     builtin_object = PyString_InternFromString("__builtins__");
656     if (builtin_object == NULL)
657         return 0;
658     return 1;
659 }
660 
661 PyFrameObject *
PyFrame_New(PyThreadState * tstate,PyCodeObject * code,PyObject * globals,PyObject * locals)662 PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals,
663             PyObject *locals)
664 {
665     PyFrameObject *back = tstate->frame;
666     PyFrameObject *f;
667     PyObject *builtins;
668     Py_ssize_t i;
669 
670 #ifdef Py_DEBUG
671     if (code == NULL || globals == NULL || !PyDict_Check(globals) ||
672         (locals != NULL && !PyMapping_Check(locals))) {
673         PyErr_BadInternalCall();
674         return NULL;
675     }
676 #endif
677     if (back == NULL || back->f_globals != globals) {
678         builtins = PyDict_GetItem(globals, builtin_object);
679         if (builtins) {
680             if (PyModule_Check(builtins)) {
681                 builtins = PyModule_GetDict(builtins);
682                 assert(!builtins || PyDict_Check(builtins));
683             }
684             else if (!PyDict_Check(builtins))
685                 builtins = NULL;
686         }
687         if (builtins == NULL) {
688             /* No builtins!              Make up a minimal one
689                Give them 'None', at least. */
690             builtins = PyDict_New();
691             if (builtins == NULL ||
692                 PyDict_SetItemString(
693                     builtins, "None", Py_None) < 0)
694                 return NULL;
695         }
696         else
697             Py_INCREF(builtins);
698 
699     }
700     else {
701         /* If we share the globals, we share the builtins.
702            Save a lookup and a call. */
703         builtins = back->f_builtins;
704         assert(builtins != NULL && PyDict_Check(builtins));
705         Py_INCREF(builtins);
706     }
707     if (code->co_zombieframe != NULL) {
708         f = code->co_zombieframe;
709         code->co_zombieframe = NULL;
710         _Py_NewReference((PyObject *)f);
711         assert(f->f_code == code);
712     }
713     else {
714         Py_ssize_t extras, ncells, nfrees;
715         ncells = PyTuple_GET_SIZE(code->co_cellvars);
716         nfrees = PyTuple_GET_SIZE(code->co_freevars);
717         extras = code->co_stacksize + code->co_nlocals + ncells +
718             nfrees;
719         if (free_list == NULL) {
720             f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type,
721             extras);
722             if (f == NULL) {
723                 Py_DECREF(builtins);
724                 return NULL;
725             }
726         }
727         else {
728             assert(numfree > 0);
729             --numfree;
730             f = free_list;
731             free_list = free_list->f_back;
732             if (Py_SIZE(f) < extras) {
733                 f = PyObject_GC_Resize(PyFrameObject, f, extras);
734                 if (f == NULL) {
735                     Py_DECREF(builtins);
736                     return NULL;
737                 }
738             }
739             _Py_NewReference((PyObject *)f);
740         }
741 
742         f->f_code = code;
743         extras = code->co_nlocals + ncells + nfrees;
744         f->f_valuestack = f->f_localsplus + extras;
745         for (i=0; i<extras; i++)
746             f->f_localsplus[i] = NULL;
747         f->f_locals = NULL;
748         f->f_trace = NULL;
749         f->f_exc_type = f->f_exc_value = f->f_exc_traceback = NULL;
750     }
751     f->f_stacktop = f->f_valuestack;
752     f->f_builtins = builtins;
753     Py_XINCREF(back);
754     f->f_back = back;
755     Py_INCREF(code);
756     Py_INCREF(globals);
757     f->f_globals = globals;
758     /* Most functions have CO_NEWLOCALS and CO_OPTIMIZED set. */
759     if ((code->co_flags & (CO_NEWLOCALS | CO_OPTIMIZED)) ==
760         (CO_NEWLOCALS | CO_OPTIMIZED))
761         ; /* f_locals = NULL; will be set by PyFrame_FastToLocals() */
762     else if (code->co_flags & CO_NEWLOCALS) {
763         locals = PyDict_New();
764         if (locals == NULL) {
765             Py_DECREF(f);
766             return NULL;
767         }
768         f->f_locals = locals;
769     }
770     else {
771         if (locals == NULL)
772             locals = globals;
773         Py_INCREF(locals);
774         f->f_locals = locals;
775     }
776     f->f_tstate = tstate;
777 
778     f->f_lasti = -1;
779     f->f_lineno = code->co_firstlineno;
780     f->f_iblock = 0;
781 
782     _PyObject_GC_TRACK(f);
783     return f;
784 }
785 
786 /* Block management */
787 
788 void
PyFrame_BlockSetup(PyFrameObject * f,int type,int handler,int level)789 PyFrame_BlockSetup(PyFrameObject *f, int type, int handler, int level)
790 {
791     PyTryBlock *b;
792     if (f->f_iblock >= CO_MAXBLOCKS)
793         Py_FatalError("XXX block stack overflow");
794     b = &f->f_blockstack[f->f_iblock++];
795     b->b_type = type;
796     b->b_level = level;
797     b->b_handler = handler;
798 }
799 
800 PyTryBlock *
PyFrame_BlockPop(PyFrameObject * f)801 PyFrame_BlockPop(PyFrameObject *f)
802 {
803     PyTryBlock *b;
804     if (f->f_iblock <= 0)
805         Py_FatalError("XXX block stack underflow");
806     b = &f->f_blockstack[--f->f_iblock];
807     return b;
808 }
809 
810 /* Convert between "fast" version of locals and dictionary version.
811 
812    map and values are input arguments.  map is a tuple of strings.
813    values is an array of PyObject*.  At index i, map[i] is the name of
814    the variable with value values[i].  The function copies the first
815    nmap variable from map/values into dict.  If values[i] is NULL,
816    the variable is deleted from dict.
817 
818    If deref is true, then the values being copied are cell variables
819    and the value is extracted from the cell variable before being put
820    in dict.
821 
822    Exceptions raised while modifying the dict are silently ignored,
823    because there is no good way to report them.
824  */
825 
826 static void
map_to_dict(PyObject * map,Py_ssize_t nmap,PyObject * dict,PyObject ** values,int deref)827 map_to_dict(PyObject *map, Py_ssize_t nmap, PyObject *dict, PyObject **values,
828             int deref)
829 {
830     Py_ssize_t j;
831     assert(PyTuple_Check(map));
832     assert(PyDict_Check(dict));
833     assert(PyTuple_Size(map) >= nmap);
834     for (j = nmap; --j >= 0; ) {
835         PyObject *key = PyTuple_GET_ITEM(map, j);
836         PyObject *value = values[j];
837         assert(PyString_Check(key));
838         if (deref) {
839             assert(PyCell_Check(value));
840             value = PyCell_GET(value);
841         }
842         if (value == NULL) {
843             if (PyObject_DelItem(dict, key) != 0)
844                 PyErr_Clear();
845         }
846         else {
847             if (PyObject_SetItem(dict, key, value) != 0)
848                 PyErr_Clear();
849         }
850     }
851 }
852 
853 /* Copy values from the "locals" dict into the fast locals.
854 
855    dict is an input argument containing string keys representing
856    variables names and arbitrary PyObject* as values.
857 
858    map and values are input arguments.  map is a tuple of strings.
859    values is an array of PyObject*.  At index i, map[i] is the name of
860    the variable with value values[i].  The function copies the first
861    nmap variable from map/values into dict.  If values[i] is NULL,
862    the variable is deleted from dict.
863 
864    If deref is true, then the values being copied are cell variables
865    and the value is extracted from the cell variable before being put
866    in dict.  If clear is true, then variables in map but not in dict
867    are set to NULL in map; if clear is false, variables missing in
868    dict are ignored.
869 
870    Exceptions raised while modifying the dict are silently ignored,
871    because there is no good way to report them.
872 */
873 
874 static void
dict_to_map(PyObject * map,Py_ssize_t nmap,PyObject * dict,PyObject ** values,int deref,int clear)875 dict_to_map(PyObject *map, Py_ssize_t nmap, PyObject *dict, PyObject **values,
876             int deref, int clear)
877 {
878     Py_ssize_t j;
879     assert(PyTuple_Check(map));
880     assert(PyDict_Check(dict));
881     assert(PyTuple_Size(map) >= nmap);
882     for (j = nmap; --j >= 0; ) {
883         PyObject *key = PyTuple_GET_ITEM(map, j);
884         PyObject *value = PyObject_GetItem(dict, key);
885         assert(PyString_Check(key));
886         /* We only care about NULLs if clear is true. */
887         if (value == NULL) {
888             PyErr_Clear();
889             if (!clear)
890                 continue;
891         }
892         if (deref) {
893             assert(PyCell_Check(values[j]));
894             if (PyCell_GET(values[j]) != value) {
895                 if (PyCell_Set(values[j], value) < 0)
896                     PyErr_Clear();
897             }
898         } else if (values[j] != value) {
899             Py_XINCREF(value);
900             Py_XSETREF(values[j], value);
901         }
902         Py_XDECREF(value);
903     }
904 }
905 
906 void
PyFrame_FastToLocals(PyFrameObject * f)907 PyFrame_FastToLocals(PyFrameObject *f)
908 {
909     /* Merge fast locals into f->f_locals */
910     PyObject *locals, *map;
911     PyObject **fast;
912     PyObject *error_type, *error_value, *error_traceback;
913     PyCodeObject *co;
914     Py_ssize_t j;
915     int ncells, nfreevars;
916     if (f == NULL)
917         return;
918     locals = f->f_locals;
919     if (locals == NULL) {
920         locals = f->f_locals = PyDict_New();
921         if (locals == NULL) {
922             PyErr_Clear(); /* Can't report it :-( */
923             return;
924         }
925     }
926     co = f->f_code;
927     map = co->co_varnames;
928     if (!PyTuple_Check(map))
929         return;
930     PyErr_Fetch(&error_type, &error_value, &error_traceback);
931     fast = f->f_localsplus;
932     j = PyTuple_GET_SIZE(map);
933     if (j > co->co_nlocals)
934         j = co->co_nlocals;
935     if (co->co_nlocals)
936         map_to_dict(map, j, locals, fast, 0);
937     ncells = PyTuple_GET_SIZE(co->co_cellvars);
938     nfreevars = PyTuple_GET_SIZE(co->co_freevars);
939     if (ncells || nfreevars) {
940         map_to_dict(co->co_cellvars, ncells,
941                     locals, fast + co->co_nlocals, 1);
942         /* If the namespace is unoptimized, then one of the
943            following cases applies:
944            1. It does not contain free variables, because it
945               uses import * or is a top-level namespace.
946            2. It is a class namespace.
947            We don't want to accidentally copy free variables
948            into the locals dict used by the class.
949         */
950         if (co->co_flags & CO_OPTIMIZED) {
951             map_to_dict(co->co_freevars, nfreevars,
952                         locals, fast + co->co_nlocals + ncells, 1);
953         }
954     }
955     PyErr_Restore(error_type, error_value, error_traceback);
956 }
957 
958 void
PyFrame_LocalsToFast(PyFrameObject * f,int clear)959 PyFrame_LocalsToFast(PyFrameObject *f, int clear)
960 {
961     /* Merge f->f_locals into fast locals */
962     PyObject *locals, *map;
963     PyObject **fast;
964     PyObject *error_type, *error_value, *error_traceback;
965     PyCodeObject *co;
966     Py_ssize_t j;
967     int ncells, nfreevars;
968     if (f == NULL)
969         return;
970     locals = f->f_locals;
971     co = f->f_code;
972     map = co->co_varnames;
973     if (locals == NULL)
974         return;
975     if (!PyTuple_Check(map))
976         return;
977     PyErr_Fetch(&error_type, &error_value, &error_traceback);
978     fast = f->f_localsplus;
979     j = PyTuple_GET_SIZE(map);
980     if (j > co->co_nlocals)
981         j = co->co_nlocals;
982     if (co->co_nlocals)
983         dict_to_map(co->co_varnames, j, locals, fast, 0, clear);
984     ncells = PyTuple_GET_SIZE(co->co_cellvars);
985     nfreevars = PyTuple_GET_SIZE(co->co_freevars);
986     if (ncells || nfreevars) {
987         dict_to_map(co->co_cellvars, ncells,
988                     locals, fast + co->co_nlocals, 1, clear);
989         /* Same test as in PyFrame_FastToLocals() above. */
990         if (co->co_flags & CO_OPTIMIZED) {
991             dict_to_map(co->co_freevars, nfreevars,
992                 locals, fast + co->co_nlocals + ncells, 1,
993                 clear);
994         }
995     }
996     PyErr_Restore(error_type, error_value, error_traceback);
997 }
998 
999 /* Clear out the free list */
1000 int
PyFrame_ClearFreeList(void)1001 PyFrame_ClearFreeList(void)
1002 {
1003     int freelist_size = numfree;
1004 
1005     while (free_list != NULL) {
1006         PyFrameObject *f = free_list;
1007         free_list = free_list->f_back;
1008         PyObject_GC_Del(f);
1009         --numfree;
1010     }
1011     assert(numfree == 0);
1012     return freelist_size;
1013 }
1014 
1015 void
PyFrame_Fini(void)1016 PyFrame_Fini(void)
1017 {
1018     (void)PyFrame_ClearFreeList();
1019     Py_XDECREF(builtin_object);
1020     builtin_object = NULL;
1021 }
1022