summaryrefslogtreecommitdiffstats
path: root/Objects/frameobject.c
diff options
context:
space:
mode:
authorJeremy Hylton <jeremy@alum.mit.edu>2003-02-05 22:39:29 (GMT)
committerJeremy Hylton <jeremy@alum.mit.edu>2003-02-05 22:39:29 (GMT)
commitbd5cbf866f011dc170841f6eb121d74610fcc7b6 (patch)
tree4cc3635e881c97d32f63013ef08f450d2d9d023e /Objects/frameobject.c
parent64edd6ac1ddb29f7817ee44e9b376b096ffa1d55 (diff)
downloadcpython-bd5cbf866f011dc170841f6eb121d74610fcc7b6.zip
cpython-bd5cbf866f011dc170841f6eb121d74610fcc7b6.tar.gz
cpython-bd5cbf866f011dc170841f6eb121d74610fcc7b6.tar.bz2
Refactor the logic for setting f_builtins.
For the case where the current globals match the previous frame's globals, eliminates three tests in two if statements. For the case where we just get __builtins__ from a module, eliminate a couple of tests.
Diffstat (limited to 'Objects/frameobject.c')
-rw-r--r--Objects/frameobject.c55
1 files changed, 31 insertions, 24 deletions
diff --git a/Objects/frameobject.c b/Objects/frameobject.c
index 2c62b67..bdff3c4 100644
--- a/Objects/frameobject.c
+++ b/Objects/frameobject.c
@@ -554,16 +554,34 @@ PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals,
extras = code->co_stacksize + code->co_nlocals + ncells + nfrees;
if (back == NULL || back->f_globals != globals) {
builtins = PyDict_GetItem(globals, builtin_object);
- if (builtins != NULL && PyModule_Check(builtins))
- builtins = PyModule_GetDict(builtins);
+ if (builtins) {
+ if (PyModule_Check(builtins)) {
+ builtins = PyModule_GetDict(builtins);
+ assert(!builtins || PyDict_Check(builtins));
+ }
+ else if (!PyDict_Check(builtins))
+ builtins = NULL;
+ }
+ if (builtins == NULL) {
+ /* No builtins! Make up a minimal one
+ Give them 'None', at least. */
+ builtins = PyDict_New();
+ if (builtins == NULL ||
+ PyDict_SetItemString(
+ builtins, "None", Py_None) < 0)
+ return NULL;
+ }
+ else
+ Py_INCREF(builtins);
+
}
else {
/* If we share the globals, we share the builtins.
Save a lookup and a call. */
builtins = back->f_builtins;
+ assert(builtins != NULL && PyDict_Check(builtins));
+ Py_INCREF(builtins);
}
- if (builtins != NULL && !PyDict_Check(builtins))
- builtins = NULL;
if (free_list == NULL) {
f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type, extras);
if (f == NULL)
@@ -581,17 +599,6 @@ PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals,
}
_Py_NewReference((PyObject *)f);
}
- if (builtins == NULL) {
- /* No builtins! Make up a minimal one. */
- builtins = PyDict_New();
- if (builtins == NULL || /* Give them 'None', at least. */
- PyDict_SetItemString(builtins, "None", Py_None) < 0) {
- Py_DECREF(f);
- return NULL;
- }
- }
- else
- Py_INCREF(builtins);
f->f_builtins = builtins;
Py_XINCREF(back);
f->f_back = back;
@@ -599,15 +606,15 @@ PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals,
f->f_code = code;
Py_INCREF(globals);
f->f_globals = globals;
- if (code->co_flags & CO_NEWLOCALS) {
- if (code->co_flags & CO_OPTIMIZED)
- locals = NULL; /* Let fast_2_locals handle it */
- else {
- locals = PyDict_New();
- if (locals == NULL) {
- Py_DECREF(f);
- return NULL;
- }
+ /* Most functions have CO_NEWLOCALS and CO_OPTIMIZED set. */
+ if ((code->co_flags & (CO_NEWLOCALS | CO_OPTIMIZED)) ==
+ (CO_NEWLOCALS | CO_OPTIMIZED))
+ locals = NULL; /* PyFrame_Fast2Locals() will set. */
+ else if (code->co_flags & CO_NEWLOCALS) {
+ locals = PyDict_New();
+ if (locals == NULL) {
+ Py_DECREF(f);
+ return NULL;
}
}
else {