1991-02-19 20:39:46 +08:00
|
|
|
|
1990-10-14 20:07:46 +08:00
|
|
|
/* Function object interface */
|
2010-12-04 04:14:31 +08:00
|
|
|
#ifndef Py_LIMITED_API
|
2000-07-09 08:20:36 +08:00
|
|
|
#ifndef Py_FUNCOBJECT_H
|
|
|
|
#define Py_FUNCOBJECT_H
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2004-10-29 00:32:00 +08: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-26 01:01:41 +08:00
|
|
|
* They reference a code object in their __code__ attribute, which is a
|
2004-10-29 00:32:00 +08: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 22:50:45 +08:00
|
|
|
typedef struct {
|
2000-07-09 08:20:36 +08:00
|
|
|
PyObject_HEAD
|
2017-11-28 23:56:10 +08:00
|
|
|
PyObject *func_code; /* A code object, the __code__ attribute */
|
|
|
|
PyObject *func_globals; /* A dictionary (other mappings won't do) */
|
|
|
|
PyObject *func_defaults; /* NULL or a tuple */
|
|
|
|
PyObject *func_kwdefaults; /* NULL or a dict */
|
|
|
|
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 */
|
|
|
|
PyObject *func_annotations; /* Annotations, a dict or NULL */
|
2011-11-26 01:56:07 +08:00
|
|
|
PyObject *func_qualname; /* The qualified name */
|
2019-05-30 02:31:52 +08:00
|
|
|
vectorcallfunc vectorcall;
|
2004-10-29 00:32:00 +08: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 19:45:45 +08:00
|
|
|
} PyFunctionObject;
|
1993-05-19 22:50:45 +08:00
|
|
|
|
2002-08-12 15:21:58 +08:00
|
|
|
PyAPI_DATA(PyTypeObject) PyFunction_Type;
|
1990-10-14 20:07:46 +08:00
|
|
|
|
2020-02-14 01:37:17 +08:00
|
|
|
#define PyFunction_Check(op) Py_IS_TYPE(op, &PyFunction_Type)
|
1990-10-14 20:07:46 +08:00
|
|
|
|
2002-08-12 15:21:58 +08:00
|
|
|
PyAPI_FUNC(PyObject *) PyFunction_New(PyObject *, PyObject *);
|
2011-11-26 01:56:07 +08:00
|
|
|
PyAPI_FUNC(PyObject *) PyFunction_NewWithQualName(PyObject *, PyObject *, PyObject *);
|
2002-08-12 15:21:58 +08:00
|
|
|
PyAPI_FUNC(PyObject *) PyFunction_GetCode(PyObject *);
|
|
|
|
PyAPI_FUNC(PyObject *) PyFunction_GetGlobals(PyObject *);
|
2003-02-01 02:33:18 +08:00
|
|
|
PyAPI_FUNC(PyObject *) PyFunction_GetModule(PyObject *);
|
2002-08-12 15:21:58 +08:00
|
|
|
PyAPI_FUNC(PyObject *) PyFunction_GetDefaults(PyObject *);
|
|
|
|
PyAPI_FUNC(int) PyFunction_SetDefaults(PyObject *, PyObject *);
|
2006-10-28 07:31:49 +08:00
|
|
|
PyAPI_FUNC(PyObject *) PyFunction_GetKwDefaults(PyObject *);
|
|
|
|
PyAPI_FUNC(int) PyFunction_SetKwDefaults(PyObject *, PyObject *);
|
2002-08-12 15:21:58 +08:00
|
|
|
PyAPI_FUNC(PyObject *) PyFunction_GetClosure(PyObject *);
|
|
|
|
PyAPI_FUNC(int) PyFunction_SetClosure(PyObject *, PyObject *);
|
2006-12-28 14:47:50 +08:00
|
|
|
PyAPI_FUNC(PyObject *) PyFunction_GetAnnotations(PyObject *);
|
|
|
|
PyAPI_FUNC(int) PyFunction_SetAnnotations(PyObject *, PyObject *);
|
1993-07-28 17:05:47 +08:00
|
|
|
|
2016-08-19 22:11:43 +08:00
|
|
|
#ifndef Py_LIMITED_API
|
2019-05-30 21:11:22 +08:00
|
|
|
PyAPI_FUNC(PyObject *) _PyFunction_Vectorcall(
|
2016-09-10 03:36:44 +08:00
|
|
|
PyObject *func,
|
2017-12-15 19:11:11 +08:00
|
|
|
PyObject *const *stack,
|
2019-05-30 02:31:52 +08:00
|
|
|
size_t nargsf,
|
2016-09-10 03:36:44 +08:00
|
|
|
PyObject *kwnames);
|
2016-08-19 22:11:43 +08:00
|
|
|
#endif
|
|
|
|
|
1998-07-10 23:47:08 +08: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) \
|
2017-11-28 23:56:10 +08:00
|
|
|
(((PyFunctionObject *)func) -> func_globals)
|
2003-02-01 02:33:18 +08:00
|
|
|
#define PyFunction_GET_MODULE(func) \
|
2017-11-28 23:56:10 +08:00
|
|
|
(((PyFunctionObject *)func) -> func_module)
|
1998-07-10 23:47:08 +08:00
|
|
|
#define PyFunction_GET_DEFAULTS(func) \
|
2017-11-28 23:56:10 +08:00
|
|
|
(((PyFunctionObject *)func) -> func_defaults)
|
2006-10-28 07:31:49 +08:00
|
|
|
#define PyFunction_GET_KW_DEFAULTS(func) \
|
2017-11-28 23:56:10 +08:00
|
|
|
(((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-26 04:06:59 +08:00
|
|
|
#define PyFunction_GET_CLOSURE(func) \
|
2017-11-28 23:56:10 +08:00
|
|
|
(((PyFunctionObject *)func) -> func_closure)
|
2006-12-28 14:47:50 +08:00
|
|
|
#define PyFunction_GET_ANNOTATIONS(func) \
|
2017-11-28 23:56:10 +08:00
|
|
|
(((PyFunctionObject *)func) -> func_annotations)
|
1998-07-10 23:47:08 +08:00
|
|
|
|
2001-08-02 12:15:00 +08:00
|
|
|
/* The classmethod and staticmethod types lives here, too */
|
2002-08-12 15:21:58 +08:00
|
|
|
PyAPI_DATA(PyTypeObject) PyClassMethod_Type;
|
|
|
|
PyAPI_DATA(PyTypeObject) PyStaticMethod_Type;
|
2001-08-02 12:15:00 +08:00
|
|
|
|
2002-08-12 15:21:58 +08:00
|
|
|
PyAPI_FUNC(PyObject *) PyClassMethod_New(PyObject *);
|
|
|
|
PyAPI_FUNC(PyObject *) PyStaticMethod_New(PyObject *);
|
2001-08-02 12:15:00 +08:00
|
|
|
|
1993-07-28 17:05:47 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* !Py_FUNCOBJECT_H */
|
2010-12-04 04:14:31 +08:00
|
|
|
#endif /* Py_LIMITED_API */
|