[Python-checkins] CVS: python/dist/src/Objects frameobject.c,2.43,2.44 funcobject.c,2.32,2.33

Jeremy Hylton jhylton@users.sourceforge.net
Thu, 25 Jan 2001 12:07:01 -0800


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv24002/Objects

Modified Files:
	frameobject.c funcobject.c 
Log Message:
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.



Index: frameobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/frameobject.c,v
retrieving revision 2.43
retrieving revision 2.44
diff -C2 -r2.43 -r2.44
*** frameobject.c	2000/09/01 23:29:27	2.43
--- frameobject.c	2001/01/25 20:06:59	2.44
***************
*** 80,83 ****
--- 80,84 ----
  	Py_XDECREF(f->f_globals);
  	Py_XDECREF(f->f_locals);
+ 	Py_XDECREF(f->f_closure);
  	Py_XDECREF(f->f_trace);
  	Py_XDECREF(f->f_exc_type);
***************
*** 107,112 ****
  
  PyFrameObject *
! PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
!             PyObject *globals, PyObject *locals)
  {
  	PyFrameObject *back = tstate->frame;
--- 108,113 ----
  
  PyFrameObject *
! PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals, 
! 	    PyObject *locals, PyObject *closure)
  {
  	PyFrameObject *back = tstate->frame;
***************
*** 114,118 ****
  	PyFrameObject *f;
  	PyObject *builtins;
! 	int extras;
  
  	if (builtin_object == NULL) {
--- 115,119 ----
  	PyFrameObject *f;
  	PyObject *builtins;
! 	int extras, ncells;
  
  	if (builtin_object == NULL) {
***************
*** 129,132 ****
--- 130,134 ----
  	}
  	extras = code->co_stacksize + code->co_nlocals;
+ 	ncells = PyTuple_GET_SIZE(code->co_cellvars);
  	if (back == NULL || back->f_globals != globals) {
  		builtins = PyDict_GetItem(globals, builtin_object);
***************
*** 198,201 ****
--- 200,219 ----
  		Py_INCREF(locals);
  	}
+ 	if (closure || ncells) {
+ 		int i, size;
+ 		size = ncells;
+ 		if (closure)
+ 			size += PyTuple_GET_SIZE(closure);
+ 		f->f_closure = PyTuple_New(size);
+ 		for (i = 0; i < ncells; ++i)
+ 			PyTuple_SET_ITEM(f->f_closure, i, PyCell_New(NULL));
+ 		for (i = ncells; i < size; ++i) {
+ 			PyObject *o = PyTuple_GET_ITEM(closure, i - ncells);
+ 			Py_INCREF(o);
+ 			PyTuple_SET_ITEM(f->f_closure, i, o);
+ 		}
+ 	}
+ 	else
+ 		f->f_closure = NULL;
  	f->f_locals = locals;
  	f->f_trace = NULL;

Index: funcobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/funcobject.c,v
retrieving revision 2.32
retrieving revision 2.33
diff -C2 -r2.32 -r2.33
*** funcobject.c	2001/01/19 19:53:29	2.32
--- funcobject.c	2001/01/25 20:06:59	2.33
***************
*** 21,24 ****
--- 21,25 ----
  		Py_INCREF(op->func_name);
  		op->func_defaults = NULL; /* No default arguments */
+ 		op->func_closure = NULL;
  		consts = ((PyCodeObject *)code)->co_consts;
  		if (PyTuple_Size(consts) >= 1) {
***************
*** 90,93 ****
--- 91,125 ----
  }
  
+ PyObject *
+ PyFunction_GetClosure(PyObject *op)
+ {
+ 	if (!PyFunction_Check(op)) {
+ 		PyErr_BadInternalCall();
+ 		return NULL;
+ 	}
+ 	return ((PyFunctionObject *) op) -> func_closure;
+ }
+ 
+ int
+ PyFunction_SetClosure(PyObject *op, PyObject *closure)
+ {
+ 	if (!PyFunction_Check(op)) {
+ 		PyErr_BadInternalCall();
+ 		return -1;
+ 	}
+ 	if (closure == Py_None)
+ 		closure = NULL;
+ 	else if (PyTuple_Check(closure)) {
+ 		Py_XINCREF(closure);
+ 	}
+ 	else {
+ 		PyErr_SetString(PyExc_SystemError, "non-tuple closure");
+ 		return -1;
+ 	}
+ 	Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
+ 	((PyFunctionObject *) op) -> func_closure = closure;
+ 	return 0;
+ }
+ 
  /* Methods */
  
***************
*** 99,102 ****
--- 131,135 ----
          {"func_name",     T_OBJECT,     OFF(func_name),         READONLY},
          {"__name__",      T_OBJECT,     OFF(func_name),         READONLY},
+         {"func_closure",  T_OBJECT,     OFF(func_closure)},
          {"func_defaults", T_OBJECT,     OFF(func_defaults)},
          {"func_doc",      T_OBJECT,     OFF(func_doc)},