[Python-checkins] CVS: python/dist/src/Include compile.h,2.23,2.24 funcobject.h,2.21,2.22 frameobject.h,2.28,2.29 opcode.h,2.32,2.33

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


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

Modified Files:
	compile.h funcobject.h frameobject.h opcode.h 
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.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/compile.h,v
retrieving revision 2.23
retrieving revision 2.24
diff -C2 -r2.23 -r2.24
*** compile.h	2000/09/01 23:29:26	2.23
--- compile.h	2001/01/25 20:06:58	2.24
***************
*** 19,22 ****
--- 19,24 ----
      PyObject *co_names;		/* list of strings (names used) */
      PyObject *co_varnames;	/* tuple of strings (local variable names) */
+     PyObject *co_freevars;	/* tuple of strings (free variable names) */
+     PyObject *co_cellvars;      /* tuple of strings (cell variable names) */
      /* The rest doesn't count for hash/cmp */
      PyObject *co_filename;	/* string (where it was loaded from) */
***************
*** 43,47 ****
  DL_IMPORT(PyCodeObject *) PyCode_New(
  	int, int, int, int, PyObject *, PyObject *, PyObject *, PyObject *,
! 	PyObject *, PyObject *, int, PyObject *); /* same as struct above */
  DL_IMPORT(int) PyCode_Addr2Line(PyCodeObject *, int);
  
--- 45,50 ----
  DL_IMPORT(PyCodeObject *) PyCode_New(
  	int, int, int, int, PyObject *, PyObject *, PyObject *, PyObject *,
! 	PyObject *, PyObject *, PyObject *, PyObject *, int, PyObject *); 
!         /* same as struct above */
  DL_IMPORT(int) PyCode_Addr2Line(PyCodeObject *, int);
  

Index: funcobject.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/funcobject.h,v
retrieving revision 2.21
retrieving revision 2.22
diff -C2 -r2.21 -r2.22
*** funcobject.h	2001/01/15 20:40:19	2.21
--- funcobject.h	2001/01/25 20:06:58	2.22
***************
*** 13,16 ****
--- 13,17 ----
      PyObject *func_globals;
      PyObject *func_defaults;
+     PyObject *func_closure;
      PyObject *func_doc;
      PyObject *func_name;
***************
*** 27,30 ****
--- 28,33 ----
  extern DL_IMPORT(PyObject *) PyFunction_GetDefaults(PyObject *);
  extern DL_IMPORT(int) PyFunction_SetDefaults(PyObject *, PyObject *);
+ extern DL_IMPORT(PyObject *) PyFunction_GetClosure(PyObject *);
+ extern DL_IMPORT(int) PyFunction_SetClosure(PyObject *, PyObject *);
  
  /* Macros for direct access to these values. Type checks are *not*
***************
*** 36,39 ****
--- 39,44 ----
  #define PyFunction_GET_DEFAULTS(func) \
  	(((PyFunctionObject *)func) -> func_defaults)
+ #define PyFunction_GET_CLOSURE(func) \
+ 	(((PyFunctionObject *)func) -> func_closure)
  
  #ifdef __cplusplus

Index: frameobject.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/frameobject.h,v
retrieving revision 2.28
retrieving revision 2.29
diff -C2 -r2.28 -r2.29
*** frameobject.h	2000/09/01 23:29:26	2.28
--- frameobject.h	2001/01/25 20:06:58	2.29
***************
*** 21,24 ****
--- 21,25 ----
      PyObject *f_globals;	/* global symbol table (PyDictObject) */
      PyObject *f_locals;		/* local symbol table (PyDictObject) */
+     PyObject *f_closure;        /* environment for free variables */
      PyObject **f_valuestack;	/* points after the last local */
      PyObject *f_trace;		/* Trace function */
***************
*** 44,48 ****
  
  DL_IMPORT(PyFrameObject *) PyFrame_New(PyThreadState *, PyCodeObject *,
!                                        PyObject *, PyObject *);
  
  
--- 45,50 ----
  
  DL_IMPORT(PyFrameObject *) PyFrame_New(PyThreadState *, PyCodeObject *,
!                                        PyObject *, PyObject *,
! 				       PyObject *);
  
  

Index: opcode.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/opcode.h,v
retrieving revision 2.32
retrieving revision 2.33
diff -C2 -r2.32 -r2.33
*** opcode.h	2001/01/17 15:21:09	2.32
--- opcode.h	2001/01/25 20:06:58	2.33
***************
*** 115,123 ****
  #define SET_LINENO	127	/* Current line number */
  
- /* It used to be the case that opcodes should fit in 7 bits.  This is
-    no longer the case -- 8 bits is fine (the instruction stream is now
-    a sequence of unsigned characters).  We gladly use the new space
-    for new opcodes. */
- 
  #define RAISE_VARARGS	130	/* Number of raise arguments (1, 2 or 3) */
  /* CALL_FUNCTION_XXX opcodes defined below depend on this definition */
--- 115,118 ----
***************
*** 125,128 ****
--- 120,128 ----
  #define MAKE_FUNCTION	132	/* #defaults */
  #define BUILD_SLICE 	133	/* Number of items */
+ 
+ #define MAKE_CLOSURE    134     /* #free vars */
+ #define LOAD_CLOSURE    135     /* Load free variable from closure */
+ #define LOAD_DEREF      136     /* Load and dereference from closure cell */ 
+ #define STORE_DEREF     137     /* Store into cell */ 
  
  /* The next 3 opcodes must be contiguous and satisfy