这与 'fast locals' 有关,它们存储为匹配的元组对,用于快速整数索引(一个用于 names f->f_code->co_varnames
,一个用于 values f->f_localsplus
)。当locals()
被调用时,fast-locals 被转换成一个标准的 dict 并附加到框架结构上。cpython 代码的相关位如下。
这是locals()
. 它只不过是 call 而已PyEval_GetLocals
。
static PyObject *
builtin_locals(PyObject *self)
{
PyObject *d;
d = PyEval_GetLocals();
Py_XINCREF(d);
return d;
}
反过来PyEval_GetLocals
,只做 call PyFrame_FastToLocals
。
PyObject *
PyEval_GetLocals(void)
{
PyFrameObject *current_frame = PyEval_GetFrame();
if (current_frame == NULL)
return NULL;
PyFrame_FastToLocals(current_frame);
return current_frame->f_locals;
}
这是为框架的局部变量分配一个普通的旧字典并将任何“快速”变量填充到其中的位。由于新 dict 被附加到框架结构(as f->f_locals
)上,因此任何“快速”变量在调用 locals() 时都会获得额外的引用。
void
PyFrame_FastToLocals(PyFrameObject *f)
{
/* Merge fast locals into f->f_locals */
PyObject *locals, *map;
PyObject **fast;
PyObject *error_type, *error_value, *error_traceback;
PyCodeObject *co;
Py_ssize_t j;
int ncells, nfreevars;
if (f == NULL)
return;
locals = f->f_locals;
if (locals == NULL) {
/* This is the dict that holds the new, additional reference! */
locals = f->f_locals = PyDict_New();
if (locals == NULL) {
PyErr_Clear(); /* Can't report it :-( */
return;
}
}
co = f->f_code;
map = co->co_varnames;
if (!PyTuple_Check(map))
return;
PyErr_Fetch(&error_type, &error_value, &error_traceback);
fast = f->f_localsplus;
j = PyTuple_GET_SIZE(map);
if (j > co->co_nlocals)
j = co->co_nlocals;
if (co->co_nlocals)
map_to_dict(map, j, locals, fast, 0);
ncells = PyTuple_GET_SIZE(co->co_cellvars);
nfreevars = PyTuple_GET_SIZE(co->co_freevars);
if (ncells || nfreevars) {
map_to_dict(co->co_cellvars, ncells,
locals, fast + co->co_nlocals, 1);
/* If the namespace is unoptimized, then one of the
following cases applies:
1. It does not contain free variables, because it
uses import * or is a top-level namespace.
2. It is a class namespace.
We don't want to accidentally copy free variables
into the locals dict used by the class.
*/
if (co->co_flags & CO_OPTIMIZED) {
map_to_dict(co->co_freevars, nfreevars,
locals, fast + co->co_nlocals + ncells, 1);
}
}
PyErr_Restore(error_type, error_value, error_traceback);
}