ยปCore Development>Code coverage>Objects/frameobject.c

Python code coverage for Objects/frameobject.c

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