mirror of
https://github.com/python/cpython.git
synced 2025-11-24 20:30:18 +00:00
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
This commit is contained in:
parent
fbd849f201
commit
64949cb753
10 changed files with 933 additions and 342 deletions
|
|
@ -238,6 +238,8 @@ w_object(PyObject *v, WFILE *p)
|
|||
w_object(co->co_consts, p);
|
||||
w_object(co->co_names, p);
|
||||
w_object(co->co_varnames, p);
|
||||
w_object(co->co_freevars, p);
|
||||
w_object(co->co_cellvars, p);
|
||||
w_object(co->co_filename, p);
|
||||
w_object(co->co_name, p);
|
||||
w_short(co->co_firstlineno, p);
|
||||
|
|
@ -554,6 +556,8 @@ r_object(RFILE *p)
|
|||
PyObject *consts = NULL;
|
||||
PyObject *names = NULL;
|
||||
PyObject *varnames = NULL;
|
||||
PyObject *freevars = NULL;
|
||||
PyObject *cellvars = NULL;
|
||||
PyObject *filename = NULL;
|
||||
PyObject *name = NULL;
|
||||
int firstlineno = 0;
|
||||
|
|
@ -563,7 +567,9 @@ r_object(RFILE *p)
|
|||
if (code) consts = r_object(p);
|
||||
if (consts) names = r_object(p);
|
||||
if (names) varnames = r_object(p);
|
||||
if (varnames) filename = r_object(p);
|
||||
if (varnames) freevars = r_object(p);
|
||||
if (freevars) cellvars = r_object(p);
|
||||
if (cellvars) filename = r_object(p);
|
||||
if (filename) name = r_object(p);
|
||||
if (name) {
|
||||
firstlineno = r_short(p);
|
||||
|
|
@ -572,9 +578,10 @@ r_object(RFILE *p)
|
|||
|
||||
if (!PyErr_Occurred()) {
|
||||
v = (PyObject *) PyCode_New(
|
||||
argcount, nlocals, stacksize, flags,
|
||||
argcount, nlocals, stacksize, flags,
|
||||
code, consts, names, varnames,
|
||||
filename, name, firstlineno, lnotab);
|
||||
freevars, cellvars, filename, name,
|
||||
firstlineno, lnotab);
|
||||
}
|
||||
else
|
||||
v = NULL;
|
||||
|
|
@ -582,6 +589,8 @@ r_object(RFILE *p)
|
|||
Py_XDECREF(consts);
|
||||
Py_XDECREF(names);
|
||||
Py_XDECREF(varnames);
|
||||
Py_XDECREF(freevars);
|
||||
Py_XDECREF(cellvars);
|
||||
Py_XDECREF(filename);
|
||||
Py_XDECREF(name);
|
||||
Py_XDECREF(lnotab);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue