| 
									
										
										
										
											1991-02-19 12:39:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | /* Function object interface */ | 
					
						
							| 
									
										
										
										
											2010-12-03 20:14:31 +00:00
										 |  |  | #ifndef Py_LIMITED_API
 | 
					
						
							| 
									
										
										
										
											2000-07-09 00:20:36 +00:00
										 |  |  | #ifndef Py_FUNCOBJECT_H
 | 
					
						
							|  |  |  | #define Py_FUNCOBJECT_H
 | 
					
						
							|  |  |  | #ifdef __cplusplus
 | 
					
						
							|  |  |  | extern "C" { | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-28 16:32:00 +00:00
										 |  |  | /* Function objects and code objects should not be confused with each other:
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Function objects are created by the execution of the 'def' statement. | 
					
						
							| 
									
										
										
										
											2007-08-25 17:01:41 +00:00
										 |  |  |  * They reference a code object in their __code__ attribute, which is a | 
					
						
							| 
									
										
										
										
											2004-10-28 16:32:00 +00:00
										 |  |  |  * purely syntactic object, i.e. nothing more than a compiled version of some | 
					
						
							|  |  |  |  * source code lines.  There is one code object per source code "fragment", | 
					
						
							|  |  |  |  * but each code object can be referenced by zero or many function objects | 
					
						
							|  |  |  |  * depending only on how many times the 'def' statement in the source was | 
					
						
							|  |  |  |  * executed so far. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1993-05-19 14:50:45 +00:00
										 |  |  | typedef struct { | 
					
						
							| 
									
										
										
										
											2000-07-09 00:20:36 +00:00
										 |  |  |     PyObject_HEAD | 
					
						
							| 
									
										
										
										
											2007-08-25 17:01:41 +00:00
										 |  |  |     PyObject *func_code;	/* A code object, the __code__ attribute */ | 
					
						
							| 
									
										
										
										
											2004-10-28 16:32:00 +00:00
										 |  |  |     PyObject *func_globals;	/* A dictionary (other mappings won't do) */ | 
					
						
							|  |  |  |     PyObject *func_defaults;	/* NULL or a tuple */ | 
					
						
							| 
									
										
										
										
											2006-10-27 23:31:49 +00:00
										 |  |  |     PyObject *func_kwdefaults;	/* NULL or a dict */ | 
					
						
							| 
									
										
										
										
											2004-10-28 16:32:00 +00:00
										 |  |  |     PyObject *func_closure;	/* NULL or a tuple of cell objects */ | 
					
						
							|  |  |  |     PyObject *func_doc;		/* The __doc__ attribute, can be anything */ | 
					
						
							|  |  |  |     PyObject *func_name;	/* The __name__ attribute, a string object */ | 
					
						
							|  |  |  |     PyObject *func_dict;	/* The __dict__ attribute, a dict or NULL */ | 
					
						
							|  |  |  |     PyObject *func_weakreflist;	/* List of weak references */ | 
					
						
							|  |  |  |     PyObject *func_module;	/* The __module__ attribute, can be anything */ | 
					
						
							| 
									
										
										
										
											2006-12-28 06:47:50 +00:00
										 |  |  |     PyObject *func_annotations;	/* Annotations, a dict or NULL */ | 
					
						
							| 
									
										
										
										
											2011-11-25 18:56:07 +01:00
										 |  |  |     PyObject *func_qualname;    /* The qualified name */ | 
					
						
							| 
									
										
										
										
											2004-10-28 16:32:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Invariant:
 | 
					
						
							|  |  |  |      *     func_closure contains the bindings for func_code->co_freevars, so | 
					
						
							|  |  |  |      *     PyTuple_Size(func_closure) == PyCode_GetNumFree(func_code) | 
					
						
							|  |  |  |      *     (func_closure may be NULL if PyCode_GetNumFree(func_code) == 0). | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											1995-01-12 11:45:45 +00:00
										 |  |  | } PyFunctionObject; | 
					
						
							| 
									
										
										
										
											1993-05-19 14:50:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-08-12 07:21:58 +00:00
										 |  |  | PyAPI_DATA(PyTypeObject) PyFunction_Type; | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-19 02:45:37 +00:00
										 |  |  | #define PyFunction_Check(op) (Py_TYPE(op) == &PyFunction_Type)
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-08-12 07:21:58 +00:00
										 |  |  | PyAPI_FUNC(PyObject *) PyFunction_New(PyObject *, PyObject *); | 
					
						
							| 
									
										
										
										
											2011-11-25 18:56:07 +01:00
										 |  |  | PyAPI_FUNC(PyObject *) PyFunction_NewWithQualName(PyObject *, PyObject *, PyObject *); | 
					
						
							| 
									
										
										
										
											2002-08-12 07:21:58 +00:00
										 |  |  | PyAPI_FUNC(PyObject *) PyFunction_GetCode(PyObject *); | 
					
						
							|  |  |  | PyAPI_FUNC(PyObject *) PyFunction_GetGlobals(PyObject *); | 
					
						
							| 
									
										
										
										
											2003-01-31 18:33:18 +00:00
										 |  |  | PyAPI_FUNC(PyObject *) PyFunction_GetModule(PyObject *); | 
					
						
							| 
									
										
										
										
											2002-08-12 07:21:58 +00:00
										 |  |  | PyAPI_FUNC(PyObject *) PyFunction_GetDefaults(PyObject *); | 
					
						
							|  |  |  | PyAPI_FUNC(int) PyFunction_SetDefaults(PyObject *, PyObject *); | 
					
						
							| 
									
										
										
										
											2006-10-27 23:31:49 +00:00
										 |  |  | PyAPI_FUNC(PyObject *) PyFunction_GetKwDefaults(PyObject *); | 
					
						
							|  |  |  | PyAPI_FUNC(int) PyFunction_SetKwDefaults(PyObject *, PyObject *); | 
					
						
							| 
									
										
										
										
											2002-08-12 07:21:58 +00:00
										 |  |  | PyAPI_FUNC(PyObject *) PyFunction_GetClosure(PyObject *); | 
					
						
							|  |  |  | PyAPI_FUNC(int) PyFunction_SetClosure(PyObject *, PyObject *); | 
					
						
							| 
									
										
										
										
											2006-12-28 06:47:50 +00:00
										 |  |  | PyAPI_FUNC(PyObject *) PyFunction_GetAnnotations(PyObject *); | 
					
						
							|  |  |  | PyAPI_FUNC(int) PyFunction_SetAnnotations(PyObject *, PyObject *); | 
					
						
							| 
									
										
										
										
											1993-07-28 09:05:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 16:11:43 +02:00
										 |  |  | #ifndef Py_LIMITED_API
 | 
					
						
							| 
									
										
										
										
											2016-08-22 23:15:44 +02:00
										 |  |  | PyAPI_FUNC(PyObject *) _PyFunction_FastCallDict( | 
					
						
							| 
									
										
										
										
											2016-08-19 16:11:43 +02:00
										 |  |  |     PyObject *func, | 
					
						
							| 
									
										
										
										
											2016-08-25 00:04:09 +02:00
										 |  |  |     PyObject **args, | 
					
						
							|  |  |  |     Py_ssize_t nargs, | 
					
						
							| 
									
										
										
										
											2016-08-19 16:11:43 +02:00
										 |  |  |     PyObject *kwargs); | 
					
						
							| 
									
										
										
										
											2016-09-09 12:36:44 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | PyAPI_FUNC(PyObject *) _PyFunction_FastCallKeywords( | 
					
						
							|  |  |  |     PyObject *func, | 
					
						
							|  |  |  |     PyObject **stack, | 
					
						
							|  |  |  |     Py_ssize_t nargs, | 
					
						
							|  |  |  |     PyObject *kwnames); | 
					
						
							| 
									
										
										
										
											2016-08-19 16:11:43 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-07-10 15:47:08 +00:00
										 |  |  | /* Macros for direct access to these values. Type checks are *not*
 | 
					
						
							|  |  |  |    done, so use with care. */ | 
					
						
							|  |  |  | #define PyFunction_GET_CODE(func) \
 | 
					
						
							|  |  |  |         (((PyFunctionObject *)func) -> func_code) | 
					
						
							|  |  |  | #define PyFunction_GET_GLOBALS(func) \
 | 
					
						
							|  |  |  | 	(((PyFunctionObject *)func) -> func_globals) | 
					
						
							| 
									
										
										
										
											2003-01-31 18:33:18 +00:00
										 |  |  | #define PyFunction_GET_MODULE(func) \
 | 
					
						
							|  |  |  | 	(((PyFunctionObject *)func) -> func_module) | 
					
						
							| 
									
										
										
										
											1998-07-10 15:47:08 +00:00
										 |  |  | #define PyFunction_GET_DEFAULTS(func) \
 | 
					
						
							|  |  |  | 	(((PyFunctionObject *)func) -> func_defaults) | 
					
						
							| 
									
										
										
										
											2006-10-27 23:31:49 +00:00
										 |  |  | #define PyFunction_GET_KW_DEFAULTS(func) \
 | 
					
						
							|  |  |  | 	(((PyFunctionObject *)func) -> func_kwdefaults) | 
					
						
							| 
									
										
											  
											
												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.
											
										 
											2001-01-25 20:06:59 +00:00
										 |  |  | #define PyFunction_GET_CLOSURE(func) \
 | 
					
						
							|  |  |  | 	(((PyFunctionObject *)func) -> func_closure) | 
					
						
							| 
									
										
										
										
											2006-12-28 06:47:50 +00:00
										 |  |  | #define PyFunction_GET_ANNOTATIONS(func) \
 | 
					
						
							|  |  |  | 	(((PyFunctionObject *)func) -> func_annotations) | 
					
						
							| 
									
										
										
										
											1998-07-10 15:47:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-02 04:15:00 +00:00
										 |  |  | /* The classmethod and staticmethod types lives here, too */ | 
					
						
							| 
									
										
										
										
											2002-08-12 07:21:58 +00:00
										 |  |  | PyAPI_DATA(PyTypeObject) PyClassMethod_Type; | 
					
						
							|  |  |  | PyAPI_DATA(PyTypeObject) PyStaticMethod_Type; | 
					
						
							| 
									
										
										
										
											2001-08-02 04:15:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-08-12 07:21:58 +00:00
										 |  |  | PyAPI_FUNC(PyObject *) PyClassMethod_New(PyObject *); | 
					
						
							|  |  |  | PyAPI_FUNC(PyObject *) PyStaticMethod_New(PyObject *); | 
					
						
							| 
									
										
										
										
											2001-08-02 04:15:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1993-07-28 09:05:47 +00:00
										 |  |  | #ifdef __cplusplus
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #endif /* !Py_FUNCOBJECT_H */
 | 
					
						
							| 
									
										
										
										
											2010-12-03 20:14:31 +00:00
										 |  |  | #endif /* Py_LIMITED_API */
 |