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