Mercurial > cpython-lru_cache-weakref
annotate Objects/frameobject.c @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | b7871ca930ad |
| children | 800a5217e67e |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
2059
866c81068dbd
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2053
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
866c81068dbd
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2053
diff
changeset
|
3 The Netherlands. |
| 145 | 4 |
| 5 All Rights Reserved | |
| 6 | |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2856
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2856
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
| 145 | 9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2856
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
| 145 | 11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2856
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2856
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2856
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2856
diff
changeset
|
15 permission. |
| 145 | 16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2856
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2856
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2856
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2856
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2856
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2856
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2856
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2856
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2856
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2856
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2856
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2856
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
| 145 | 29 |
| 30 ******************************************************************/ | |
| 31 | |
| 86 | 32 /* Frame object implementation */ |
| 33 | |
| 5350 | 34 #include "Python.h" |
| 86 | 35 |
| 36 #include "compile.h" | |
| 37 #include "frameobject.h" | |
| 38 #include "opcode.h" | |
| 39 #include "structmember.h" | |
| 40 | |
| 5350 | 41 #define OFF(x) offsetof(PyFrameObject, x) |
| 86 | 42 |
| 43 static struct memberlist frame_memberlist[] = { | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
44 {"f_back", T_OBJECT, OFF(f_back), RO}, |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
45 {"f_code", T_OBJECT, OFF(f_code), RO}, |
|
2104
7493c6805dc6
add builtins handling and restricted flag
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
46 {"f_builtins", T_OBJECT, OFF(f_builtins),RO}, |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
47 {"f_globals", T_OBJECT, OFF(f_globals), RO}, |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
48 {"f_locals", T_OBJECT, OFF(f_locals), RO}, |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
49 {"f_lasti", T_INT, OFF(f_lasti), RO}, |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
50 {"f_lineno", T_INT, OFF(f_lineno), RO}, |
|
2104
7493c6805dc6
add builtins handling and restricted flag
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
51 {"f_restricted",T_INT, OFF(f_restricted),RO}, |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
52 {"f_trace", T_OBJECT, OFF(f_trace)}, |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5350
diff
changeset
|
53 {"f_exc_type", T_OBJECT, OFF(f_exc_type)}, |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5350
diff
changeset
|
54 {"f_exc_value", T_OBJECT, OFF(f_exc_value)}, |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5350
diff
changeset
|
55 {"f_exc_traceback", T_OBJECT, OFF(f_exc_traceback)}, |
| 86 | 56 {NULL} /* Sentinel */ |
| 57 }; | |
| 58 | |
| 5350 | 59 static PyObject * |
| 86 | 60 frame_getattr(f, name) |
| 5350 | 61 PyFrameObject *f; |
| 86 | 62 char *name; |
| 63 { | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
64 if (strcmp(name, "f_locals") == 0) |
| 5350 | 65 PyFrame_FastToLocals(f); |
| 66 return PyMember_Get((char *)f, frame_memberlist, name); | |
| 86 | 67 } |
| 68 | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
69 static int |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
70 frame_setattr(f, name, value) |
| 5350 | 71 PyFrameObject *f; |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
72 char *name; |
| 5350 | 73 PyObject *value; |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
74 { |
| 5350 | 75 return PyMember_Set((char *)f, frame_memberlist, name, value); |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
76 } |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
77 |
|
1079
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
78 /* Stack frames are allocated and deallocated at a considerable rate. |
|
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
79 In an attempt to improve the speed of function calls, we maintain a |
|
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
80 separate free list of stack frames (just like integers are |
|
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
81 allocated in a special way -- see intobject.c). When a stack frame |
|
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
82 is on the free list, only the following members have a meaning: |
|
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
83 ob_type == &Frametype |
|
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
84 f_back next item on free list, or NULL |
|
5013
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
85 f_nlocals number of locals |
|
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
86 f_stacksize size of value stack |
|
1079
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
87 Note that the value and block stacks are preserved -- this can save |
|
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
88 another malloc() call or two (and two free() calls as well!). |
|
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
89 Also note that, unlike for integers, each frame object is a |
|
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
90 malloc'ed object in its own right -- it is only the actual calls to |
|
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
91 malloc() that we are trying to save here, not the administration. |
|
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
92 After all, while a typical program may make millions of calls, a |
|
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
93 call depth of more than 20 or 30 is probably already exceptional |
|
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
94 unless the program contains run-away recursion. I hope. |
|
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
95 */ |
|
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
96 |
| 5350 | 97 static PyFrameObject *free_list = NULL; |
|
1079
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
98 |
| 86 | 99 static void |
| 100 frame_dealloc(f) | |
| 5350 | 101 PyFrameObject *f; |
| 86 | 102 { |
|
5097
57b7a03a3252
Kill all local variables when the frame is deallocated (moved here
Guido van Rossum <guido@python.org>
parents:
5045
diff
changeset
|
103 int i; |
|
57b7a03a3252
Kill all local variables when the frame is deallocated (moved here
Guido van Rossum <guido@python.org>
parents:
5045
diff
changeset
|
104 PyObject **fastlocals; |
|
57b7a03a3252
Kill all local variables when the frame is deallocated (moved here
Guido van Rossum <guido@python.org>
parents:
5045
diff
changeset
|
105 |
|
57b7a03a3252
Kill all local variables when the frame is deallocated (moved here
Guido van Rossum <guido@python.org>
parents:
5045
diff
changeset
|
106 /* Kill all local variables */ |
|
57b7a03a3252
Kill all local variables when the frame is deallocated (moved here
Guido van Rossum <guido@python.org>
parents:
5045
diff
changeset
|
107 fastlocals = f->f_localsplus; |
|
57b7a03a3252
Kill all local variables when the frame is deallocated (moved here
Guido van Rossum <guido@python.org>
parents:
5045
diff
changeset
|
108 for (i = f->f_nlocals; --i >= 0; ++fastlocals) { |
| 5350 | 109 Py_XDECREF(*fastlocals); |
|
5097
57b7a03a3252
Kill all local variables when the frame is deallocated (moved here
Guido van Rossum <guido@python.org>
parents:
5045
diff
changeset
|
110 } |
|
57b7a03a3252
Kill all local variables when the frame is deallocated (moved here
Guido van Rossum <guido@python.org>
parents:
5045
diff
changeset
|
111 |
| 5350 | 112 Py_XDECREF(f->f_back); |
| 113 Py_XDECREF(f->f_code); | |
| 114 Py_XDECREF(f->f_builtins); | |
| 115 Py_XDECREF(f->f_globals); | |
| 116 Py_XDECREF(f->f_locals); | |
| 117 Py_XDECREF(f->f_trace); | |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5350
diff
changeset
|
118 Py_XDECREF(f->f_exc_type); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5350
diff
changeset
|
119 Py_XDECREF(f->f_exc_value); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5350
diff
changeset
|
120 Py_XDECREF(f->f_exc_traceback); |
|
1079
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
121 f->f_back = free_list; |
|
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
122 free_list = f; |
| 86 | 123 } |
| 124 | |
| 5350 | 125 PyTypeObject PyFrame_Type = { |
| 126 PyObject_HEAD_INIT(&PyType_Type) | |
| 86 | 127 0, |
| 128 "frame", | |
| 5350 | 129 sizeof(PyFrameObject), |
| 86 | 130 0, |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
131 (destructor)frame_dealloc, /*tp_dealloc*/ |
| 86 | 132 0, /*tp_print*/ |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
133 (getattrfunc)frame_getattr, /*tp_getattr*/ |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
134 (setattrfunc)frame_setattr, /*tp_setattr*/ |
| 86 | 135 0, /*tp_compare*/ |
| 136 0, /*tp_repr*/ | |
| 137 0, /*tp_as_number*/ | |
| 138 0, /*tp_as_sequence*/ | |
| 139 0, /*tp_as_mapping*/ | |
| 140 }; | |
| 141 | |
| 5350 | 142 PyFrameObject * |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5350
diff
changeset
|
143 PyFrame_New(tstate, code, globals, locals) |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5350
diff
changeset
|
144 PyThreadState *tstate; |
| 5350 | 145 PyCodeObject *code; |
| 146 PyObject *globals; | |
| 147 PyObject *locals; | |
| 86 | 148 { |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5350
diff
changeset
|
149 PyFrameObject *back = tstate->frame; |
| 5350 | 150 static PyObject *builtin_object; |
| 151 PyFrameObject *f; | |
| 152 PyObject *builtins; | |
|
5013
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
153 int extras = code->co_stacksize + code->co_nlocals; |
|
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
154 |
|
2627
e9ea05702676
Use mappinglookup instead of dictlookup for looking up __builtin__.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2151
diff
changeset
|
155 if (builtin_object == NULL) { |
|
5007
e4f7c331bb1a
Intern the strings "__builtins__".
Guido van Rossum <guido@python.org>
parents:
4996
diff
changeset
|
156 builtin_object = PyString_InternFromString("__builtins__"); |
|
2627
e9ea05702676
Use mappinglookup instead of dictlookup for looking up __builtin__.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2151
diff
changeset
|
157 if (builtin_object == NULL) |
|
e9ea05702676
Use mappinglookup instead of dictlookup for looking up __builtin__.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2151
diff
changeset
|
158 return NULL; |
|
e9ea05702676
Use mappinglookup instead of dictlookup for looking up __builtin__.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2151
diff
changeset
|
159 } |
| 5350 | 160 if ((back != NULL && !PyFrame_Check(back)) || |
| 161 code == NULL || !PyCode_Check(code) || | |
| 162 globals == NULL || !PyDict_Check(globals) || | |
| 163 (locals != NULL && !PyDict_Check(locals))) { | |
| 164 PyErr_BadInternalCall(); | |
| 86 | 165 return NULL; |
| 166 } | |
| 5350 | 167 builtins = PyDict_GetItem(globals, builtin_object); |
| 168 if (builtins != NULL && PyModule_Check(builtins)) | |
| 169 builtins = PyModule_GetDict(builtins); | |
| 170 if (builtins == NULL || !PyDict_Check(builtins)) { | |
| 171 PyErr_SetString(PyExc_TypeError, | |
| 172 "bad __builtins__ dictionary"); | |
|
2104
7493c6805dc6
add builtins handling and restricted flag
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
173 return NULL; |
|
7493c6805dc6
add builtins handling and restricted flag
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
174 } |
|
1079
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
175 if (free_list == NULL) { |
| 5350 | 176 f = (PyFrameObject *) |
| 177 malloc(sizeof(PyFrameObject) + | |
| 178 extras*sizeof(PyObject *)); | |
|
2842
420874f7d8bd
changes for keyword arguments and fast function call; added abstract.c
Guido van Rossum <guido@python.org>
parents:
2627
diff
changeset
|
179 if (f == NULL) |
| 5350 | 180 return (PyFrameObject *)PyErr_NoMemory(); |
| 181 f->ob_type = &PyFrame_Type; | |
| 182 _Py_NewReference(f); | |
|
1079
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
183 } |
|
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
184 else { |
|
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
185 f = free_list; |
|
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
186 free_list = free_list->f_back; |
|
5013
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
187 if (f->f_nlocals + f->f_stacksize < extras) { |
| 5350 | 188 f = realloc(f, sizeof(PyFrameObject) + |
| 189 extras*sizeof(PyObject *)); | |
|
5013
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
190 if (f == NULL) |
| 5350 | 191 return (PyFrameObject *)PyErr_NoMemory(); |
|
5013
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
192 } |
|
5045
b1d82697c0b2
When a recycled frame has more local+stack slots than needed,
Guido van Rossum <guido@python.org>
parents:
5015
diff
changeset
|
193 else |
|
b1d82697c0b2
When a recycled frame has more local+stack slots than needed,
Guido van Rossum <guido@python.org>
parents:
5015
diff
changeset
|
194 extras = f->f_nlocals + f->f_stacksize; |
| 5350 | 195 f->ob_type = &PyFrame_Type; |
| 196 _Py_NewReference(f); | |
|
1079
e4c6a0dd5c2b
* bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
197 } |
| 5350 | 198 Py_XINCREF(back); |
|
2842
420874f7d8bd
changes for keyword arguments and fast function call; added abstract.c
Guido van Rossum <guido@python.org>
parents:
2627
diff
changeset
|
199 f->f_back = back; |
| 5350 | 200 Py_INCREF(code); |
|
2842
420874f7d8bd
changes for keyword arguments and fast function call; added abstract.c
Guido van Rossum <guido@python.org>
parents:
2627
diff
changeset
|
201 f->f_code = code; |
| 5350 | 202 Py_XINCREF(builtins); |
|
2842
420874f7d8bd
changes for keyword arguments and fast function call; added abstract.c
Guido van Rossum <guido@python.org>
parents:
2627
diff
changeset
|
203 f->f_builtins = builtins; |
| 5350 | 204 Py_INCREF(globals); |
|
2842
420874f7d8bd
changes for keyword arguments and fast function call; added abstract.c
Guido van Rossum <guido@python.org>
parents:
2627
diff
changeset
|
205 f->f_globals = globals; |
|
2856
a7af970a94a4
better policy regarding NULL locals
Guido van Rossum <guido@python.org>
parents:
2842
diff
changeset
|
206 if (code->co_flags & CO_NEWLOCALS) { |
|
a7af970a94a4
better policy regarding NULL locals
Guido van Rossum <guido@python.org>
parents:
2842
diff
changeset
|
207 if (code->co_flags & CO_OPTIMIZED) |
|
a7af970a94a4
better policy regarding NULL locals
Guido van Rossum <guido@python.org>
parents:
2842
diff
changeset
|
208 locals = NULL; /* Let fast_2_locals handle it */ |
|
a7af970a94a4
better policy regarding NULL locals
Guido van Rossum <guido@python.org>
parents:
2842
diff
changeset
|
209 else { |
| 5350 | 210 locals = PyDict_New(); |
|
2856
a7af970a94a4
better policy regarding NULL locals
Guido van Rossum <guido@python.org>
parents:
2842
diff
changeset
|
211 if (locals == NULL) { |
| 5350 | 212 Py_DECREF(f); |
|
2856
a7af970a94a4
better policy regarding NULL locals
Guido van Rossum <guido@python.org>
parents:
2842
diff
changeset
|
213 return NULL; |
|
a7af970a94a4
better policy regarding NULL locals
Guido van Rossum <guido@python.org>
parents:
2842
diff
changeset
|
214 } |
|
2842
420874f7d8bd
changes for keyword arguments and fast function call; added abstract.c
Guido van Rossum <guido@python.org>
parents:
2627
diff
changeset
|
215 } |
|
420874f7d8bd
changes for keyword arguments and fast function call; added abstract.c
Guido van Rossum <guido@python.org>
parents:
2627
diff
changeset
|
216 } |
|
420874f7d8bd
changes for keyword arguments and fast function call; added abstract.c
Guido van Rossum <guido@python.org>
parents:
2627
diff
changeset
|
217 else { |
|
420874f7d8bd
changes for keyword arguments and fast function call; added abstract.c
Guido van Rossum <guido@python.org>
parents:
2627
diff
changeset
|
218 if (locals == NULL) |
|
420874f7d8bd
changes for keyword arguments and fast function call; added abstract.c
Guido van Rossum <guido@python.org>
parents:
2627
diff
changeset
|
219 locals = globals; |
| 5350 | 220 Py_INCREF(locals); |
|
2842
420874f7d8bd
changes for keyword arguments and fast function call; added abstract.c
Guido van Rossum <guido@python.org>
parents:
2627
diff
changeset
|
221 } |
|
420874f7d8bd
changes for keyword arguments and fast function call; added abstract.c
Guido van Rossum <guido@python.org>
parents:
2627
diff
changeset
|
222 f->f_locals = locals; |
|
5013
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
223 f->f_trace = NULL; |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5350
diff
changeset
|
224 f->f_exc_type = f->f_exc_value = f->f_exc_traceback = NULL; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5350
diff
changeset
|
225 f->f_tstate = PyThreadState_Get(); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5350
diff
changeset
|
226 if (f->f_tstate == NULL) |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5350
diff
changeset
|
227 Py_FatalError("can't create new frame without thread"); |
|
5013
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
228 |
|
2842
420874f7d8bd
changes for keyword arguments and fast function call; added abstract.c
Guido van Rossum <guido@python.org>
parents:
2627
diff
changeset
|
229 f->f_lasti = 0; |
|
5045
b1d82697c0b2
When a recycled frame has more local+stack slots than needed,
Guido van Rossum <guido@python.org>
parents:
5015
diff
changeset
|
230 f->f_lineno = code->co_firstlineno; |
| 5350 | 231 f->f_restricted = (builtins != PyBuiltin_GetDict()); |
|
5013
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
232 f->f_iblock = 0; |
|
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
233 f->f_nlocals = code->co_nlocals; |
|
5045
b1d82697c0b2
When a recycled frame has more local+stack slots than needed,
Guido van Rossum <guido@python.org>
parents:
5015
diff
changeset
|
234 f->f_stacksize = extras - code->co_nlocals; |
|
5013
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
235 |
|
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
236 while (--extras >= 0) |
|
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
237 f->f_localsplus[extras] = NULL; |
|
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
238 |
|
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
239 f->f_valuestack = f->f_localsplus + f->f_nlocals; |
|
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
240 |
| 86 | 241 return f; |
| 242 } | |
| 243 | |
| 244 /* Block management */ | |
| 245 | |
| 246 void | |
| 5350 | 247 PyFrame_BlockSetup(f, type, handler, level) |
| 248 PyFrameObject *f; | |
| 86 | 249 int type; |
| 250 int handler; | |
| 251 int level; | |
| 252 { | |
| 5350 | 253 PyTryBlock *b; |
|
5013
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
254 if (f->f_iblock >= CO_MAXBLOCKS) |
| 5350 | 255 Py_FatalError("XXX block stack overflow"); |
| 86 | 256 b = &f->f_blockstack[f->f_iblock++]; |
| 257 b->b_type = type; | |
| 258 b->b_level = level; | |
| 259 b->b_handler = handler; | |
| 260 } | |
| 261 | |
| 5350 | 262 PyTryBlock * |
| 263 PyFrame_BlockPop(f) | |
| 264 PyFrameObject *f; | |
| 86 | 265 { |
| 5350 | 266 PyTryBlock *b; |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
267 if (f->f_iblock <= 0) |
| 5350 | 268 Py_FatalError("XXX block stack underflow"); |
| 86 | 269 b = &f->f_blockstack[--f->f_iblock]; |
| 270 return b; | |
| 271 } | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
272 |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
273 /* Convert between "fast" version of locals and dictionary version */ |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
274 |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
275 void |
| 5350 | 276 PyFrame_FastToLocals(f) |
| 277 PyFrameObject *f; | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
278 { |
|
5013
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
279 /* Merge fast locals into f->f_locals */ |
| 5350 | 280 PyObject *locals, *map; |
| 281 PyObject **fast; | |
| 282 PyObject *error_type, *error_value, *error_traceback; | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
283 int j; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
284 if (f == NULL) |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
285 return; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
286 locals = f->f_locals; |
|
2842
420874f7d8bd
changes for keyword arguments and fast function call; added abstract.c
Guido van Rossum <guido@python.org>
parents:
2627
diff
changeset
|
287 if (locals == NULL) { |
| 5350 | 288 locals = f->f_locals = PyDict_New(); |
|
2842
420874f7d8bd
changes for keyword arguments and fast function call; added abstract.c
Guido van Rossum <guido@python.org>
parents:
2627
diff
changeset
|
289 if (locals == NULL) { |
| 5350 | 290 PyErr_Clear(); /* Can't report it :-( */ |
|
2842
420874f7d8bd
changes for keyword arguments and fast function call; added abstract.c
Guido van Rossum <guido@python.org>
parents:
2627
diff
changeset
|
291 return; |
|
420874f7d8bd
changes for keyword arguments and fast function call; added abstract.c
Guido van Rossum <guido@python.org>
parents:
2627
diff
changeset
|
292 } |
|
420874f7d8bd
changes for keyword arguments and fast function call; added abstract.c
Guido van Rossum <guido@python.org>
parents:
2627
diff
changeset
|
293 } |
|
5013
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
294 if (f->f_nlocals == 0) |
|
2856
a7af970a94a4
better policy regarding NULL locals
Guido van Rossum <guido@python.org>
parents:
2842
diff
changeset
|
295 return; |
|
a7af970a94a4
better policy regarding NULL locals
Guido van Rossum <guido@python.org>
parents:
2842
diff
changeset
|
296 map = f->f_code->co_varnames; |
| 5350 | 297 if (!PyDict_Check(locals) || !PyTuple_Check(map)) |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
298 return; |
| 5350 | 299 PyErr_Fetch(&error_type, &error_value, &error_traceback); |
|
5013
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
300 fast = f->f_localsplus; |
| 5350 | 301 j = PyTuple_Size(map); |
|
5013
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
302 if (j > f->f_nlocals) |
|
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
303 j = f->f_nlocals; |
|
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
304 for (; --j >= 0; ) { |
| 5350 | 305 PyObject *key = PyTuple_GetItem(map, j); |
| 306 PyObject *value = fast[j]; | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
307 if (value == NULL) { |
| 5350 | 308 PyErr_Clear(); |
| 309 if (PyDict_DelItem(locals, key) != 0) | |
| 310 PyErr_Clear(); | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
311 } |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
312 else { |
| 5350 | 313 if (PyDict_SetItem(locals, key, value) != 0) |
| 314 PyErr_Clear(); | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
315 } |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
316 } |
| 5350 | 317 PyErr_Restore(error_type, error_value, error_traceback); |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
318 } |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
319 |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
320 void |
| 5350 | 321 PyFrame_LocalsToFast(f, clear) |
| 322 PyFrameObject *f; | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
323 int clear; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
324 { |
|
5013
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
325 /* Merge f->f_locals into fast locals */ |
| 5350 | 326 PyObject *locals, *map; |
| 327 PyObject **fast; | |
| 328 PyObject *error_type, *error_value, *error_traceback; | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
329 int j; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
330 if (f == NULL) |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
331 return; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
332 locals = f->f_locals; |
|
2842
420874f7d8bd
changes for keyword arguments and fast function call; added abstract.c
Guido van Rossum <guido@python.org>
parents:
2627
diff
changeset
|
333 map = f->f_code->co_varnames; |
|
5015
b1e2eea66cb6
Bugfix: remove ref to fast before it's defined.
Guido van Rossum <guido@python.org>
parents:
5013
diff
changeset
|
334 if (locals == NULL || f->f_code->co_nlocals == 0) |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
335 return; |
| 5350 | 336 if (!PyDict_Check(locals) || !PyTuple_Check(map)) |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
337 return; |
| 5350 | 338 PyErr_Fetch(&error_type, &error_value, &error_traceback); |
|
5013
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
339 fast = f->f_localsplus; |
| 5350 | 340 j = PyTuple_Size(map); |
|
5013
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
341 if (j > f->f_nlocals) |
|
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
342 j = f->f_nlocals; |
|
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
343 for (; --j >= 0; ) { |
| 5350 | 344 PyObject *key = PyTuple_GetItem(map, j); |
| 345 PyObject *value = PyDict_GetItem(locals, key); | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
346 if (value == NULL) |
| 5350 | 347 PyErr_Clear(); |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
348 else |
| 5350 | 349 Py_INCREF(value); |
|
5013
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
350 if (value != NULL || clear) { |
| 5350 | 351 Py_XDECREF(fast[j]); |
|
5013
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
352 fast[j] = value; |
|
4ca4e54c9cd8
Changes that appear to give another 12% speedup.
Guido van Rossum <guido@python.org>
parents:
5007
diff
changeset
|
353 } |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
354 } |
| 5350 | 355 PyErr_Restore(error_type, error_value, error_traceback); |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1334
diff
changeset
|
356 } |
