[Python-checkins] CVS: python/dist/src/Python compile.c,2.151,2.152 ceval.c,2.222,2.223 import.c,2.156,2.157 marshal.c,1.59,1.60

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


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

Modified Files:
	compile.c ceval.c import.c marshal.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: compile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/compile.c,v
retrieving revision 2.151
retrieving revision 2.152
diff -C2 -r2.151 -r2.152
*** compile.c	2001/01/25 17:01:49	2.151
--- compile.c	2001/01/25 20:06:58	2.152
***************
*** 20,23 ****
--- 20,25 ----
  #include "structmember.h"
  
+ #define REPR(O) PyString_AS_STRING(PyObject_Repr(O))
+ 
  #include <ctype.h>
  
***************
*** 45,59 ****
  #define VAR_STORE 1
[...1970 lines suppressed...]
! 							 0), 0)),
! 					 DEF_LOCAL);
! 		return;
! 	case dotted_name:
! 		symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL);
! 		return;
! 	case NAME:
! 		symtable_add_def(st, STR(n), DEF_LOCAL);
! 		return;
! 	default:
! 		if (NCH(n) == 0)
  			return;
! 		if (NCH(n) != 1) {
! 			DUMP(n);
! 			Py_FatalError("too many children in default case\n");
  		}
+ 		n = CHILD(n, 0);
+ 		goto loop;
  	}
  }

Index: ceval.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/ceval.c,v
retrieving revision 2.222
retrieving revision 2.223
diff -C2 -r2.222 -r2.223
*** ceval.c	2001/01/19 03:25:05	2.222
--- ceval.c	2001/01/25 20:06:58	2.223
***************
*** 34,37 ****
--- 34,39 ----
  typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
  
+ #define REPR(ob) PyString_AS_STRING(PyObject_Repr(ob))
+ 
  /* Forward declarations */
  
***************
*** 39,44 ****
  			    PyObject *, PyObject *,
  			    PyObject **, int,
  			    PyObject **, int,
! 			    PyObject **, int);
  
  static PyObject *call_object(PyObject *, PyObject *, PyObject *);
--- 41,47 ----
  			    PyObject *, PyObject *,
  			    PyObject **, int,
+ 			    PyObject **, int,
  			    PyObject **, int,
! 			    PyObject *);
  
  static PyObject *call_object(PyObject *, PyObject *, PyObject *);
***************
*** 79,82 ****
--- 82,87 ----
  #define NAME_ERROR_MSG \
  	"name '%.200s' is not defined"
+ #define GLOBAL_NAME_ERROR_MSG \
+ 	"global name '%.200s' is not defined"
  #define UNBOUNDLOCAL_ERROR_MSG \
  	"local variable '%.200s' referenced before assignment"
***************
*** 335,340 ****
  			  globals, locals,
  			  (PyObject **)NULL, 0,
  			  (PyObject **)NULL, 0,
! 			  (PyObject **)NULL, 0);
  }
  
--- 340,346 ----
  			  globals, locals,
  			  (PyObject **)NULL, 0,
+ 			  (PyObject **)NULL, 0,
  			  (PyObject **)NULL, 0,
! 			  NULL);
  }
  
***************
*** 345,349 ****
  eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
  	   PyObject **args, int argcount, PyObject **kws, int kwcount,
! 	   PyObject **defs, int defcount)
  {
  #ifdef DXPAIRS
--- 351,355 ----
  eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
  	   PyObject **args, int argcount, PyObject **kws, int kwcount,
! 	   PyObject **defs, int defcount, PyObject *closure)
  {
  #ifdef DXPAIRS
***************
*** 426,434 ****
  #endif
  
! 	f = PyFrame_New(
! 			tstate,			/*back*/
  			co,			/*code*/
! 			globals,		/*globals*/
! 			locals);		/*locals*/
  	if (f == NULL)
  		return NULL;
--- 432,438 ----
  #endif
  
! 	f = PyFrame_New(tstate,			/*back*/
  			co,			/*code*/
! 			globals, locals, closure);
  	if (f == NULL)
  		return NULL;
***************
*** 1536,1540 ****
  			if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
  				format_exc_check_arg(
! 				    PyExc_NameError, NAME_ERROR_MSG ,w);
  			break;
  
--- 1540,1544 ----
  			if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
  				format_exc_check_arg(
! 				    PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
  			break;
  
***************
*** 1578,1582 ****
  					format_exc_check_arg(
  						    PyExc_NameError,
! 						    NAME_ERROR_MSG ,w);
  					break;
  				}
--- 1582,1586 ----
  					format_exc_check_arg(
  						    PyExc_NameError,
! 						    GLOBAL_NAME_ERROR_MSG ,w);
  					break;
  				}
***************
*** 1619,1622 ****
--- 1623,1645 ----
  			continue;
  
+ 		case LOAD_CLOSURE:
+ 			x = PyTuple_GET_ITEM(f->f_closure, oparg);
+ 			Py_INCREF(x);
+ 			PUSH(x);
+ 			break;
+ 
+ 		case LOAD_DEREF:
+ 			x = PyTuple_GET_ITEM(f->f_closure, oparg);
+ 			w = PyCell_Get(x);
+ 			Py_INCREF(w);
+ 			PUSH(w);
+ 			break;
+ 
+ 		case STORE_DEREF:
+ 			w = POP();
+ 			x = PyTuple_GET_ITEM(f->f_closure, oparg);
+ 			PyCell_Set(x, w);
+ 			continue;
+ 
  		case BUILD_TUPLE:
  			x = PyTuple_New(oparg);
***************
*** 1940,1943 ****
--- 1963,2006 ----
  			break;
  
+ 		case MAKE_CLOSURE:
+ 		{
+ 			int nfree;
+ 			v = POP(); /* code object */
+ 			x = PyFunction_New(v, f->f_globals);
+ 			nfree = PyTuple_GET_SIZE(((PyCodeObject *)v)->co_freevars);
+ 			Py_DECREF(v);
+ 			/* XXX Maybe this should be a separate opcode? */
+ 			if (x != NULL && nfree > 0) {
+ 				v = PyTuple_New(nfree);
+ 				if (v == NULL) {
+ 					Py_DECREF(x);
+ 					x = NULL;
+ 					break;
+ 				}
+ 				while (--nfree >= 0) {
+ 					w = POP();
+ 					PyTuple_SET_ITEM(v, nfree, w);
+ 				}
+ 				err = PyFunction_SetClosure(x, v);
+ 				Py_DECREF(v);
+ 			}
+ 			if (x != NULL && oparg > 0) {
+ 				v = PyTuple_New(oparg);
+ 				if (v == NULL) {
+ 					Py_DECREF(x);
+ 					x = NULL;
+ 					break;
+ 				}
+ 				while (--oparg >= 0) {
+ 					w = POP();
+ 					PyTuple_SET_ITEM(v, oparg, w);
+ 				}
+ 				err = PyFunction_SetDefaults(x, v);
+ 				Py_DECREF(v);
+ 			}
+ 			PUSH(x);
+ 			break;
+ 		}
+ 
  		case BUILD_SLICE:
  			if (oparg == 3)
***************
*** 2762,2767 ****
  		PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
  		&PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg),
! 		k, nk,
! 		d, nd);
  
  	if (k != NULL)
--- 2825,2830 ----
  		PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
  		&PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg),
! 		k, nk, d, nd,
! 		PyFunction_GET_CLOSURE(func));
  
  	if (k != NULL)
***************
*** 2806,2809 ****
--- 2869,2873 ----
  	PyObject *globals = PyFunction_GET_GLOBALS(func);
  	PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
+ 	PyObject *closure = PyFunction_GET_CLOSURE(func);
  	PyObject **d = NULL;
  	int nd = 0;
***************
*** 2815,2819 ****
  	return eval_code2((PyCodeObject *)co, globals,
  			  (PyObject *)NULL, (*pp_stack)-n, na,
! 			  (*pp_stack)-2*nk, nk, d, nd);
  }
  
--- 2879,2884 ----
  	return eval_code2((PyCodeObject *)co, globals,
  			  (PyObject *)NULL, (*pp_stack)-n, na,
! 			  (*pp_stack)-2*nk, nk, d, nd,
! 			  closure);
  }
  

Index: import.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/import.c,v
retrieving revision 2.156
retrieving revision 2.157
diff -C2 -r2.156 -r2.157
*** import.c	2001/01/18 03:03:16	2.156
--- import.c	2001/01/25 20:06:58	2.157
***************
*** 44,48 ****
     added to the .pyc file header? */
  /* New way to come up with the magic number: (YEAR-1995), MONTH, DAY */
! #define MAGIC (50823 | ((long)'\r'<<16) | ((long)'\n'<<24))
  
  /* Magic word as global; note that _PyImport_Init() can change the
--- 44,48 ----
     added to the .pyc file header? */
  /* New way to come up with the magic number: (YEAR-1995), MONTH, DAY */
! #define MAGIC (60124 | ((long)'\r'<<16) | ((long)'\n'<<24))
  
  /* Magic word as global; note that _PyImport_Init() can change the

Index: marshal.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/marshal.c,v
retrieving revision 1.59
retrieving revision 1.60
diff -C2 -r1.59 -r1.60
*** marshal.c	2001/01/18 04:39:16	1.59
--- marshal.c	2001/01/25 20:06:58	1.60
***************
*** 239,242 ****
--- 239,244 ----
  		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);
***************
*** 555,558 ****
--- 557,562 ----
  			PyObject *names = NULL;
  			PyObject *varnames = NULL;
+ 			PyObject *freevars = NULL;
+ 			PyObject *cellvars = NULL;
  			PyObject *filename = NULL;
  			PyObject *name = NULL;
***************
*** 564,568 ****
  			if (consts) names = r_object(p);
  			if (names) varnames = r_object(p);
! 			if (varnames) filename = r_object(p);
  			if (filename) name = r_object(p);
  			if (name) {
--- 568,574 ----
  			if (consts) names = r_object(p);
  			if (names) varnames = 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) {
***************
*** 573,579 ****
  			if (!PyErr_Occurred()) {
  				v = (PyObject *) PyCode_New(
! 					argcount, nlocals, stacksize, flags, 
  					code, consts, names, varnames,
! 					filename, name, firstlineno, lnotab);
  			}
  			else
--- 579,586 ----
  			if (!PyErr_Occurred()) {
  				v = (PyObject *) PyCode_New(
! 					argcount, nlocals, stacksize, flags,  
  					code, consts, names, varnames,
! 					freevars, cellvars, filename, name, 
! 					firstlineno, lnotab);  
  			}
  			else
***************
*** 583,586 ****
--- 590,595 ----
  			Py_XDECREF(names);
  			Py_XDECREF(varnames);
+ 			Py_XDECREF(freevars);
+ 			Py_XDECREF(cellvars);
  			Py_XDECREF(filename);
  			Py_XDECREF(name);