1991-02-19 08:39:46 -04:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Function object implementation */
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#include "Python.h"
|
2018-11-21 17:27:47 -04:00
|
|
|
#include "pycore_object.h"
|
2018-11-12 11:53:38 -04:00
|
|
|
#include "pycore_pymem.h"
|
|
|
|
#include "pycore_pystate.h"
|
2018-11-25 18:56:17 -04:00
|
|
|
#include "pycore_tupleobject.h"
|
2005-10-20 16:59:25 -03:00
|
|
|
#include "code.h"
|
1990-12-20 11:06:42 -04:00
|
|
|
#include "structmember.h"
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2011-11-25 13:56:07 -04:00
|
|
|
PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2013-07-22 18:02:05 -03:00
|
|
|
PyFunctionObject *op;
|
|
|
|
PyObject *doc, *consts, *module;
|
|
|
|
static PyObject *__name__ = NULL;
|
|
|
|
|
2013-07-22 18:04:55 -03:00
|
|
|
if (__name__ == NULL) {
|
|
|
|
__name__ = PyUnicode_InternFromString("__name__");
|
|
|
|
if (__name__ == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-22 18:02:05 -03:00
|
|
|
op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
|
|
|
|
if (op == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
op->func_weakreflist = NULL;
|
|
|
|
Py_INCREF(code);
|
|
|
|
op->func_code = code;
|
|
|
|
Py_INCREF(globals);
|
|
|
|
op->func_globals = globals;
|
|
|
|
op->func_name = ((PyCodeObject *)code)->co_name;
|
|
|
|
Py_INCREF(op->func_name);
|
|
|
|
op->func_defaults = NULL; /* No default arguments */
|
|
|
|
op->func_kwdefaults = NULL; /* No keyword only defaults */
|
|
|
|
op->func_closure = NULL;
|
2019-05-30 10:11:22 -03:00
|
|
|
op->vectorcall = _PyFunction_Vectorcall;
|
2013-07-22 18:04:55 -03:00
|
|
|
|
2013-07-22 18:02:05 -03:00
|
|
|
consts = ((PyCodeObject *)code)->co_consts;
|
|
|
|
if (PyTuple_Size(consts) >= 1) {
|
|
|
|
doc = PyTuple_GetItem(consts, 0);
|
|
|
|
if (!PyUnicode_Check(doc))
|
2010-05-09 12:52:27 -03:00
|
|
|
doc = Py_None;
|
2013-07-22 18:02:05 -03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
doc = Py_None;
|
|
|
|
Py_INCREF(doc);
|
|
|
|
op->func_doc = doc;
|
2013-07-22 18:04:55 -03:00
|
|
|
|
2013-07-22 18:02:05 -03:00
|
|
|
op->func_dict = NULL;
|
|
|
|
op->func_module = NULL;
|
|
|
|
op->func_annotations = NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2013-07-22 18:02:05 -03:00
|
|
|
/* __module__: If module name is in globals, use it.
|
2013-07-22 18:04:55 -03:00
|
|
|
Otherwise, use None. */
|
2019-02-25 11:59:46 -04:00
|
|
|
module = PyDict_GetItemWithError(globals, __name__);
|
2013-07-22 18:02:05 -03:00
|
|
|
if (module) {
|
|
|
|
Py_INCREF(module);
|
|
|
|
op->func_module = module;
|
|
|
|
}
|
2019-02-25 11:59:46 -04:00
|
|
|
else if (PyErr_Occurred()) {
|
|
|
|
Py_DECREF(op);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-07-22 18:02:05 -03:00
|
|
|
if (qualname)
|
|
|
|
op->func_qualname = qualname;
|
2010-05-09 12:52:27 -03:00
|
|
|
else
|
2013-07-22 18:02:05 -03:00
|
|
|
op->func_qualname = op->func_name;
|
|
|
|
Py_INCREF(op->func_qualname);
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
_PyObject_GC_TRACK(op);
|
|
|
|
return (PyObject *)op;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2011-11-25 13:56:07 -04:00
|
|
|
PyObject *
|
|
|
|
PyFunction_New(PyObject *code, PyObject *globals)
|
|
|
|
{
|
|
|
|
return PyFunction_NewWithQualName(code, globals, NULL);
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2000-07-09 03:03:25 -03:00
|
|
|
PyFunction_GetCode(PyObject *op)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyFunctionObject *) op) -> func_code;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2000-07-09 03:03:25 -03:00
|
|
|
PyFunction_GetGlobals(PyObject *op)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyFunctionObject *) op) -> func_globals;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2003-01-31 14:33:18 -04:00
|
|
|
PyObject *
|
|
|
|
PyFunction_GetModule(PyObject *op)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyFunctionObject *) op) -> func_module;
|
2003-01-31 14:33:18 -04:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2000-07-09 03:03:25 -03:00
|
|
|
PyFunction_GetDefaults(PyObject *op)
|
1994-08-30 05:27:36 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyFunctionObject *) op) -> func_defaults;
|
1994-08-30 05:27:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-07-09 03:03:25 -03:00
|
|
|
PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
|
1994-08-30 05:27:36 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (defaults == Py_None)
|
|
|
|
defaults = NULL;
|
|
|
|
else if (defaults && PyTuple_Check(defaults)) {
|
|
|
|
Py_INCREF(defaults);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_SystemError, "non-tuple default args");
|
|
|
|
return -1;
|
|
|
|
}
|
2016-04-06 03:45:48 -03:00
|
|
|
Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
1994-08-30 05:27:36 -03:00
|
|
|
}
|
|
|
|
|
2006-10-27 20:31:49 -03:00
|
|
|
PyObject *
|
|
|
|
PyFunction_GetKwDefaults(PyObject *op)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyFunctionObject *) op) -> func_kwdefaults;
|
2006-10-27 20:31:49 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (defaults == Py_None)
|
|
|
|
defaults = NULL;
|
|
|
|
else if (defaults && PyDict_Check(defaults)) {
|
|
|
|
Py_INCREF(defaults);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_SystemError,
|
|
|
|
"non-dict keyword only default args");
|
|
|
|
return -1;
|
|
|
|
}
|
2016-04-06 03:45:48 -03:00
|
|
|
Py_XSETREF(((PyFunctionObject *)op)->func_kwdefaults, defaults);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2006-10-27 20:31:49 -03:00
|
|
|
}
|
|
|
|
|
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 16:06:59 -04:00
|
|
|
PyObject *
|
|
|
|
PyFunction_GetClosure(PyObject *op)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyFunctionObject *) op) -> func_closure;
|
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 16:06:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PyFunction_SetClosure(PyObject *op, PyObject *closure)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (closure == Py_None)
|
|
|
|
closure = NULL;
|
|
|
|
else if (PyTuple_Check(closure)) {
|
|
|
|
Py_INCREF(closure);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Format(PyExc_SystemError,
|
|
|
|
"expected tuple for closure, got '%.100s'",
|
|
|
|
closure->ob_type->tp_name);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-04-06 03:45:48 -03:00
|
|
|
Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
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 16:06:59 -04:00
|
|
|
}
|
|
|
|
|
2006-12-28 02:47:50 -04:00
|
|
|
PyObject *
|
|
|
|
PyFunction_GetAnnotations(PyObject *op)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyFunctionObject *) op) -> func_annotations;
|
2006-12-28 02:47:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (annotations == Py_None)
|
|
|
|
annotations = NULL;
|
|
|
|
else if (annotations && PyDict_Check(annotations)) {
|
|
|
|
Py_INCREF(annotations);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_SystemError,
|
|
|
|
"non-dict annotations");
|
|
|
|
return -1;
|
|
|
|
}
|
2016-04-06 03:45:48 -03:00
|
|
|
Py_XSETREF(((PyFunctionObject *)op)->func_annotations, annotations);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2006-12-28 02:47:50 -04:00
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Methods */
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#define OFF(x) offsetof(PyFunctionObject, x)
|
1990-12-20 11:06:42 -04:00
|
|
|
|
2001-09-20 17:46:19 -03:00
|
|
|
static PyMemberDef func_memberlist[] = {
|
2010-05-09 12:52:27 -03:00
|
|
|
{"__closure__", T_OBJECT, OFF(func_closure),
|
|
|
|
RESTRICTED|READONLY},
|
|
|
|
{"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
|
|
|
|
{"__globals__", T_OBJECT, OFF(func_globals),
|
|
|
|
RESTRICTED|READONLY},
|
|
|
|
{"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED},
|
|
|
|
{NULL} /* Sentinel */
|
1990-12-20 11:06:42 -04:00
|
|
|
};
|
|
|
|
|
2001-09-17 20:46:56 -03:00
|
|
|
static PyObject *
|
2018-11-27 13:34:35 -04:00
|
|
|
func_get_code(PyFunctionObject *op, void *Py_UNUSED(ignored))
|
2001-09-17 20:46:56 -03:00
|
|
|
{
|
2019-05-23 12:45:22 -03:00
|
|
|
if (PySys_Audit("object.__getattr__", "Os", op, "__code__") < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_INCREF(op->func_code);
|
|
|
|
return op->func_code;
|
2001-09-17 20:46:56 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-11-27 13:34:35 -04:00
|
|
|
func_set_code(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
|
2001-09-17 20:46:56 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t nfree, nclosure;
|
|
|
|
|
|
|
|
/* Not legal to del f.func_code or to set it to anything
|
|
|
|
* other than a code object. */
|
|
|
|
if (value == NULL || !PyCode_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__code__ must be set to a code object");
|
|
|
|
return -1;
|
|
|
|
}
|
2019-05-23 12:45:22 -03:00
|
|
|
|
|
|
|
if (PySys_Audit("object.__setattr__", "OsO",
|
|
|
|
op, "__code__", value) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
nfree = PyCode_GetNumFree((PyCodeObject *)value);
|
|
|
|
nclosure = (op->func_closure == NULL ? 0 :
|
|
|
|
PyTuple_GET_SIZE(op->func_closure));
|
|
|
|
if (nclosure != nfree) {
|
|
|
|
PyErr_Format(PyExc_ValueError,
|
|
|
|
"%U() requires a code object with %zd free vars,"
|
|
|
|
" not %zd",
|
|
|
|
op->func_name,
|
|
|
|
nclosure, nfree);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_INCREF(value);
|
2016-04-06 03:50:03 -03:00
|
|
|
Py_XSETREF(op->func_code, value);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2001-09-17 20:46:56 -03:00
|
|
|
}
|
|
|
|
|
2004-08-12 15:12:44 -03:00
|
|
|
static PyObject *
|
2018-11-27 13:34:35 -04:00
|
|
|
func_get_name(PyFunctionObject *op, void *Py_UNUSED(ignored))
|
2004-08-12 15:12:44 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_INCREF(op->func_name);
|
|
|
|
return op->func_name;
|
2004-08-12 15:12:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-11-27 13:34:35 -04:00
|
|
|
func_set_name(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
|
2004-08-12 15:12:44 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Not legal to del f.func_name or to set it to anything
|
|
|
|
* other than a string object. */
|
|
|
|
if (value == NULL || !PyUnicode_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__name__ must be set to a string object");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_INCREF(value);
|
2016-04-06 03:50:03 -03:00
|
|
|
Py_XSETREF(op->func_name, value);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2004-08-12 15:12:44 -03:00
|
|
|
}
|
|
|
|
|
2011-11-25 13:56:07 -04:00
|
|
|
static PyObject *
|
2018-11-27 13:34:35 -04:00
|
|
|
func_get_qualname(PyFunctionObject *op, void *Py_UNUSED(ignored))
|
2011-11-25 13:56:07 -04:00
|
|
|
{
|
|
|
|
Py_INCREF(op->func_qualname);
|
|
|
|
return op->func_qualname;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-11-27 13:34:35 -04:00
|
|
|
func_set_qualname(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
|
2011-11-25 13:56:07 -04:00
|
|
|
{
|
|
|
|
/* Not legal to del f.__qualname__ or to set it to anything
|
|
|
|
* other than a string object. */
|
|
|
|
if (value == NULL || !PyUnicode_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__qualname__ must be set to a string object");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_INCREF(value);
|
2016-04-06 03:50:03 -03:00
|
|
|
Py_XSETREF(op->func_qualname, value);
|
2011-11-25 13:56:07 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-09-17 20:46:56 -03:00
|
|
|
static PyObject *
|
2018-11-27 13:34:35 -04:00
|
|
|
func_get_defaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
|
2001-09-17 20:46:56 -03:00
|
|
|
{
|
2019-05-23 12:45:22 -03:00
|
|
|
if (PySys_Audit("object.__getattr__", "Os", op, "__defaults__") < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
if (op->func_defaults == NULL) {
|
2017-01-23 03:47:21 -04:00
|
|
|
Py_RETURN_NONE;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
Py_INCREF(op->func_defaults);
|
|
|
|
return op->func_defaults;
|
2001-09-17 20:46:56 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-11-27 13:34:35 -04:00
|
|
|
func_set_defaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
|
2001-09-17 20:46:56 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Legal to del f.func_defaults.
|
|
|
|
* Can only set func_defaults to NULL or a tuple. */
|
|
|
|
if (value == Py_None)
|
|
|
|
value = NULL;
|
|
|
|
if (value != NULL && !PyTuple_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__defaults__ must be set to a tuple object");
|
|
|
|
return -1;
|
|
|
|
}
|
2019-05-23 12:45:22 -03:00
|
|
|
if (value) {
|
|
|
|
if (PySys_Audit("object.__setattr__", "OsO",
|
|
|
|
op, "__defaults__", value) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (PySys_Audit("object.__delattr__", "Os",
|
|
|
|
op, "__defaults__") < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_XINCREF(value);
|
2016-04-06 03:50:03 -03:00
|
|
|
Py_XSETREF(op->func_defaults, value);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
1998-05-21 21:55:34 -03:00
|
|
|
}
|
|
|
|
|
2006-10-27 20:31:49 -03:00
|
|
|
static PyObject *
|
2018-11-27 13:34:35 -04:00
|
|
|
func_get_kwdefaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
|
2006-10-27 20:31:49 -03:00
|
|
|
{
|
2019-05-23 12:45:22 -03:00
|
|
|
if (PySys_Audit("object.__getattr__", "Os",
|
|
|
|
op, "__kwdefaults__") < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
if (op->func_kwdefaults == NULL) {
|
2017-01-23 03:47:21 -04:00
|
|
|
Py_RETURN_NONE;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
Py_INCREF(op->func_kwdefaults);
|
|
|
|
return op->func_kwdefaults;
|
2006-10-27 20:31:49 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-11-27 13:34:35 -04:00
|
|
|
func_set_kwdefaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
|
2006-10-27 20:31:49 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (value == Py_None)
|
|
|
|
value = NULL;
|
|
|
|
/* Legal to del f.func_kwdefaults.
|
|
|
|
* Can only set func_kwdefaults to NULL or a dict. */
|
|
|
|
if (value != NULL && !PyDict_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__kwdefaults__ must be set to a dict object");
|
|
|
|
return -1;
|
|
|
|
}
|
2019-05-23 12:45:22 -03:00
|
|
|
if (value) {
|
|
|
|
if (PySys_Audit("object.__setattr__", "OsO",
|
|
|
|
op, "__kwdefaults__", value) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (PySys_Audit("object.__delattr__", "Os",
|
|
|
|
op, "__kwdefaults__") < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_XINCREF(value);
|
2016-04-06 03:50:03 -03:00
|
|
|
Py_XSETREF(op->func_kwdefaults, value);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2006-10-27 20:31:49 -03:00
|
|
|
}
|
|
|
|
|
2006-12-28 02:47:50 -04:00
|
|
|
static PyObject *
|
2018-11-27 13:34:35 -04:00
|
|
|
func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
|
2006-12-28 02:47:50 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (op->func_annotations == NULL) {
|
|
|
|
op->func_annotations = PyDict_New();
|
|
|
|
if (op->func_annotations == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_INCREF(op->func_annotations);
|
|
|
|
return op->func_annotations;
|
2006-12-28 02:47:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-11-27 13:34:35 -04:00
|
|
|
func_set_annotations(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
|
2006-12-28 02:47:50 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (value == Py_None)
|
|
|
|
value = NULL;
|
|
|
|
/* Legal to del f.func_annotations.
|
|
|
|
* Can only set func_annotations to NULL (through C api)
|
|
|
|
* or a dict. */
|
|
|
|
if (value != NULL && !PyDict_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__annotations__ must be set to a dict object");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_XINCREF(value);
|
2016-04-06 03:50:03 -03:00
|
|
|
Py_XSETREF(op->func_annotations, value);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2006-12-28 02:47:50 -04:00
|
|
|
}
|
|
|
|
|
2001-09-20 18:45:26 -03:00
|
|
|
static PyGetSetDef func_getsetlist[] = {
|
2010-05-09 12:52:27 -03:00
|
|
|
{"__code__", (getter)func_get_code, (setter)func_set_code},
|
|
|
|
{"__defaults__", (getter)func_get_defaults,
|
|
|
|
(setter)func_set_defaults},
|
|
|
|
{"__kwdefaults__", (getter)func_get_kwdefaults,
|
|
|
|
(setter)func_set_kwdefaults},
|
|
|
|
{"__annotations__", (getter)func_get_annotations,
|
|
|
|
(setter)func_set_annotations},
|
2012-02-19 21:02:57 -04:00
|
|
|
{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
|
2010-05-09 12:52:27 -03:00
|
|
|
{"__name__", (getter)func_get_name, (setter)func_set_name},
|
2011-11-25 13:56:07 -04:00
|
|
|
{"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL} /* Sentinel */
|
2001-09-17 20:46:56 -03:00
|
|
|
};
|
|
|
|
|
2017-03-19 03:51:07 -03:00
|
|
|
/*[clinic input]
|
|
|
|
class function "PyFunctionObject *" "&PyFunction_Type"
|
|
|
|
[clinic start generated code]*/
|
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=70af9c90aa2e71b0]*/
|
2002-07-11 15:30:27 -03:00
|
|
|
|
2017-03-19 03:51:07 -03:00
|
|
|
#include "clinic/funcobject.c.h"
|
|
|
|
|
|
|
|
/* function.__new__() maintains the following invariants for closures.
|
|
|
|
The closure must correspond to the free variables of the code object.
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
if len(code.co_freevars) == 0:
|
|
|
|
closure = NULL
|
2002-07-11 15:30:27 -03:00
|
|
|
else:
|
2010-05-09 12:52:27 -03:00
|
|
|
len(closure) == len(code.co_freevars)
|
2002-07-11 15:30:27 -03:00
|
|
|
for every elt in closure, type(elt) == cell
|
|
|
|
*/
|
2002-06-14 17:41:17 -03:00
|
|
|
|
2017-03-19 03:51:07 -03:00
|
|
|
/*[clinic input]
|
|
|
|
@classmethod
|
|
|
|
function.__new__ as func_new
|
|
|
|
code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
|
|
|
|
a code object
|
|
|
|
globals: object(subclass_of="&PyDict_Type")
|
|
|
|
the globals dictionary
|
|
|
|
name: object = None
|
|
|
|
a string that overrides the name from the code object
|
|
|
|
argdefs as defaults: object = None
|
|
|
|
a tuple that specifies the default argument values
|
|
|
|
closure: object = None
|
|
|
|
a tuple that supplies the bindings for free variables
|
|
|
|
|
|
|
|
Create a function object.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2002-06-14 17:41:17 -03:00
|
|
|
static PyObject *
|
2017-03-19 03:51:07 -03:00
|
|
|
func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
|
|
|
|
PyObject *name, PyObject *defaults, PyObject *closure)
|
|
|
|
/*[clinic end generated code: output=99c6d9da3a24e3be input=93611752fc2daf11]*/
|
2002-06-14 17:41:17 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyFunctionObject *newfunc;
|
|
|
|
Py_ssize_t nfree, nclosure;
|
2017-03-19 03:51:07 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (name != Py_None && !PyUnicode_Check(name)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"arg 3 (name) must be None or string");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (defaults != Py_None && !PyTuple_Check(defaults)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"arg 4 (defaults) must be None or tuple");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
nfree = PyTuple_GET_SIZE(code->co_freevars);
|
|
|
|
if (!PyTuple_Check(closure)) {
|
|
|
|
if (nfree && closure == Py_None) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"arg 5 (closure) must be tuple");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (closure != Py_None) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"arg 5 (closure) must be None or tuple");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check that the closure is well-formed */
|
|
|
|
nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
|
|
|
|
if (nfree != nclosure)
|
|
|
|
return PyErr_Format(PyExc_ValueError,
|
|
|
|
"%U requires closure of length %zd, not %zd",
|
|
|
|
code->co_name, nfree, nclosure);
|
|
|
|
if (nclosure) {
|
|
|
|
Py_ssize_t i;
|
|
|
|
for (i = 0; i < nclosure; i++) {
|
|
|
|
PyObject *o = PyTuple_GET_ITEM(closure, i);
|
|
|
|
if (!PyCell_Check(o)) {
|
|
|
|
return PyErr_Format(PyExc_TypeError,
|
|
|
|
"arg 5 (closure) expected cell, found %s",
|
|
|
|
o->ob_type->tp_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-23 12:45:22 -03:00
|
|
|
if (PySys_Audit("function.__new__", "O", code) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
|
|
|
|
globals);
|
|
|
|
if (newfunc == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (name != Py_None) {
|
|
|
|
Py_INCREF(name);
|
2016-04-10 12:05:40 -03:00
|
|
|
Py_SETREF(newfunc->func_name, name);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
if (defaults != Py_None) {
|
|
|
|
Py_INCREF(defaults);
|
|
|
|
newfunc->func_defaults = defaults;
|
|
|
|
}
|
|
|
|
if (closure != Py_None) {
|
|
|
|
Py_INCREF(closure);
|
|
|
|
newfunc->func_closure = closure;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (PyObject *)newfunc;
|
2002-06-14 17:41:17 -03:00
|
|
|
}
|
|
|
|
|
2018-07-03 23:15:50 -03:00
|
|
|
static int
|
|
|
|
func_clear(PyFunctionObject *op)
|
|
|
|
{
|
|
|
|
Py_CLEAR(op->func_code);
|
|
|
|
Py_CLEAR(op->func_globals);
|
|
|
|
Py_CLEAR(op->func_module);
|
|
|
|
Py_CLEAR(op->func_name);
|
|
|
|
Py_CLEAR(op->func_defaults);
|
|
|
|
Py_CLEAR(op->func_kwdefaults);
|
|
|
|
Py_CLEAR(op->func_doc);
|
|
|
|
Py_CLEAR(op->func_dict);
|
|
|
|
Py_CLEAR(op->func_closure);
|
|
|
|
Py_CLEAR(op->func_annotations);
|
|
|
|
Py_CLEAR(op->func_qualname);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
static void
|
2000-07-09 03:03:25 -03:00
|
|
|
func_dealloc(PyFunctionObject *op)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
_PyObject_GC_UNTRACK(op);
|
2018-07-03 23:15:50 -03:00
|
|
|
if (op->func_weakreflist != NULL) {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_ClearWeakRefs((PyObject *) op);
|
2018-07-03 23:15:50 -03:00
|
|
|
}
|
|
|
|
(void)func_clear(op);
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_GC_Del(op);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject*
|
2000-07-09 03:03:25 -03:00
|
|
|
func_repr(PyFunctionObject *op)
|
1993-03-29 06:43:31 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
return PyUnicode_FromFormat("<function %U at %p>",
|
2011-11-25 13:56:07 -04:00
|
|
|
op->func_qualname, op);
|
1993-03-29 06:43:31 -04:00
|
|
|
}
|
|
|
|
|
2000-06-23 11:18:11 -03:00
|
|
|
static int
|
|
|
|
func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_VISIT(f->func_code);
|
|
|
|
Py_VISIT(f->func_globals);
|
|
|
|
Py_VISIT(f->func_module);
|
|
|
|
Py_VISIT(f->func_defaults);
|
|
|
|
Py_VISIT(f->func_kwdefaults);
|
|
|
|
Py_VISIT(f->func_doc);
|
|
|
|
Py_VISIT(f->func_name);
|
|
|
|
Py_VISIT(f->func_dict);
|
|
|
|
Py_VISIT(f->func_closure);
|
|
|
|
Py_VISIT(f->func_annotations);
|
2011-11-25 13:56:07 -04:00
|
|
|
Py_VISIT(f->func_qualname);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2000-06-23 11:18:11 -03:00
|
|
|
}
|
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
/* Bind a function to an object */
|
|
|
|
static PyObject *
|
|
|
|
func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (obj == Py_None || obj == NULL) {
|
|
|
|
Py_INCREF(func);
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
return PyMethod_New(func, obj);
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject PyFunction_Type = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"function",
|
|
|
|
sizeof(PyFunctionObject),
|
|
|
|
0,
|
|
|
|
(destructor)func_dealloc, /* tp_dealloc */
|
2019-05-29 15:31:52 -03:00
|
|
|
offsetof(PyFunctionObject, vectorcall), /* tp_vectorcall_offset */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_as_async */
|
2010-05-09 12:52:27 -03:00
|
|
|
(reprfunc)func_repr, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
2019-06-18 08:05:41 -03:00
|
|
|
PyVectorcall_Call, /* tp_call */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_str */
|
2012-02-19 02:16:13 -04:00
|
|
|
0, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_as_buffer */
|
2019-05-28 09:42:53 -03:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|
2019-05-29 15:31:52 -03:00
|
|
|
_Py_TPFLAGS_HAVE_VECTORCALL |
|
2019-05-28 09:42:53 -03:00
|
|
|
Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */
|
2017-03-19 03:51:07 -03:00
|
|
|
func_new__doc__, /* tp_doc */
|
2010-05-09 12:52:27 -03:00
|
|
|
(traverseproc)func_traverse, /* tp_traverse */
|
2018-07-03 23:15:50 -03:00
|
|
|
(inquiry)func_clear, /* tp_clear */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_richcompare */
|
|
|
|
offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
0, /* tp_methods */
|
|
|
|
func_memberlist, /* tp_members */
|
|
|
|
func_getsetlist, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
func_descr_get, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
|
|
|
|
0, /* tp_init */
|
|
|
|
0, /* tp_alloc */
|
|
|
|
func_new, /* tp_new */
|
2001-08-02 01:15:00 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Class method object */
|
|
|
|
|
|
|
|
/* A class method receives the class as implicit first argument,
|
|
|
|
just like an instance method receives the instance.
|
|
|
|
To declare a class method, use this idiom:
|
|
|
|
|
|
|
|
class C:
|
2016-09-17 00:26:16 -03:00
|
|
|
@classmethod
|
|
|
|
def f(cls, arg1, arg2, ...):
|
|
|
|
...
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
It can be called either on the class (e.g. C.f()) or on an instance
|
|
|
|
(e.g. C().f()); the instance is ignored except for its class.
|
|
|
|
If a class method is called for a derived class, the derived class
|
|
|
|
object is passed as the implied first argument.
|
|
|
|
|
|
|
|
Class methods are different than C++ or Java static methods.
|
|
|
|
If you want those, see static methods below.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
PyObject *cm_callable;
|
2012-02-19 02:10:25 -04:00
|
|
|
PyObject *cm_dict;
|
2001-08-02 01:15:00 -03:00
|
|
|
} classmethod;
|
|
|
|
|
|
|
|
static void
|
|
|
|
cm_dealloc(classmethod *cm)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
_PyObject_GC_UNTRACK((PyObject *)cm);
|
|
|
|
Py_XDECREF(cm->cm_callable);
|
2012-02-19 02:10:25 -04:00
|
|
|
Py_XDECREF(cm->cm_dict);
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_TYPE(cm)->tp_free((PyObject *)cm);
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
2003-04-08 18:28:47 -03:00
|
|
|
static int
|
|
|
|
cm_traverse(classmethod *cm, visitproc visit, void *arg)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_VISIT(cm->cm_callable);
|
2012-02-19 02:10:25 -04:00
|
|
|
Py_VISIT(cm->cm_dict);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2003-04-08 18:28:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cm_clear(classmethod *cm)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_CLEAR(cm->cm_callable);
|
2012-02-19 02:10:25 -04:00
|
|
|
Py_CLEAR(cm->cm_dict);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2003-04-08 18:28:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
static PyObject *
|
|
|
|
cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
classmethod *cm = (classmethod *)self;
|
2001-08-02 01:15:00 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (cm->cm_callable == NULL) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"uninitialized classmethod object");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (type == NULL)
|
|
|
|
type = (PyObject *)(Py_TYPE(obj));
|
2019-08-24 19:37:25 -03:00
|
|
|
if (Py_TYPE(cm->cm_callable)->tp_descr_get != NULL) {
|
|
|
|
return Py_TYPE(cm->cm_callable)->tp_descr_get(cm->cm_callable, type,
|
|
|
|
NULL);
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
return PyMethod_New(cm->cm_callable, type);
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cm_init(PyObject *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
classmethod *cm = (classmethod *)self;
|
|
|
|
PyObject *callable;
|
2001-08-02 01:15:00 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!_PyArg_NoKeywords("classmethod", kwds))
|
|
|
|
return -1;
|
2017-07-09 00:45:06 -03:00
|
|
|
if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
|
|
|
|
return -1;
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_INCREF(callable);
|
2018-02-13 06:28:33 -04:00
|
|
|
Py_XSETREF(cm->cm_callable, callable);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
2009-05-29 01:52:27 -03:00
|
|
|
static PyMemberDef cm_memberlist[] = {
|
2010-05-09 12:52:27 -03:00
|
|
|
{"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
|
|
|
|
{NULL} /* Sentinel */
|
2009-05-29 01:52:27 -03:00
|
|
|
};
|
|
|
|
|
2011-12-15 16:34:02 -04:00
|
|
|
static PyObject *
|
|
|
|
cm_get___isabstractmethod__(classmethod *cm, void *closure)
|
|
|
|
{
|
|
|
|
int res = _PyObject_IsAbstract(cm->cm_callable);
|
|
|
|
if (res == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (res) {
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
}
|
|
|
|
Py_RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyGetSetDef cm_getsetlist[] = {
|
|
|
|
{"__isabstractmethod__",
|
|
|
|
(getter)cm_get___isabstractmethod__, NULL,
|
|
|
|
NULL,
|
|
|
|
NULL},
|
2012-02-19 21:02:57 -04:00
|
|
|
{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
|
2011-12-15 16:34:02 -04:00
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(classmethod_doc,
|
2001-12-16 22:53:53 -04:00
|
|
|
"classmethod(function) -> method\n\
|
|
|
|
\n\
|
|
|
|
Convert a function to be a class method.\n\
|
|
|
|
\n\
|
|
|
|
A class method receives the class as implicit first argument,\n\
|
|
|
|
just like an instance method receives the instance.\n\
|
|
|
|
To declare a class method, use this idiom:\n\
|
|
|
|
\n\
|
|
|
|
class C:\n\
|
2016-09-17 00:26:16 -03:00
|
|
|
@classmethod\n\
|
|
|
|
def f(cls, arg1, arg2, ...):\n\
|
|
|
|
...\n\
|
2001-12-16 22:53:53 -04:00
|
|
|
\n\
|
|
|
|
It can be called either on the class (e.g. C.f()) or on an instance\n\
|
|
|
|
(e.g. C().f()). The instance is ignored except for its class.\n\
|
|
|
|
If a class method is called for a derived class, the derived class\n\
|
|
|
|
object is passed as the implied first argument.\n\
|
2001-12-17 07:39:56 -04:00
|
|
|
\n\
|
2001-12-16 22:53:53 -04:00
|
|
|
Class methods are different than C++ or Java static methods.\n\
|
2002-06-13 17:33:02 -03:00
|
|
|
If you want those, see the staticmethod builtin.");
|
2001-12-16 22:53:53 -04:00
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
PyTypeObject PyClassMethod_Type = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"classmethod",
|
|
|
|
sizeof(classmethod),
|
|
|
|
0,
|
|
|
|
(destructor)cm_dealloc, /* tp_dealloc */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_vectorcall_offset */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_as_async */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
2012-02-19 02:16:13 -04:00
|
|
|
0, /* tp_getattro */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
|
|
|
|
classmethod_doc, /* tp_doc */
|
|
|
|
(traverseproc)cm_traverse, /* tp_traverse */
|
|
|
|
(inquiry)cm_clear, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
0, /* tp_methods */
|
|
|
|
cm_memberlist, /* tp_members */
|
2011-12-15 16:34:02 -04:00
|
|
|
cm_getsetlist, /* tp_getset */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
cm_descr_get, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
2012-02-19 02:10:25 -04:00
|
|
|
offsetof(classmethod, cm_dict), /* tp_dictoffset */
|
2010-05-09 12:52:27 -03:00
|
|
|
cm_init, /* tp_init */
|
|
|
|
PyType_GenericAlloc, /* tp_alloc */
|
|
|
|
PyType_GenericNew, /* tp_new */
|
|
|
|
PyObject_GC_Del, /* tp_free */
|
2001-08-02 01:15:00 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyClassMethod_New(PyObject *callable)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
classmethod *cm = (classmethod *)
|
|
|
|
PyType_GenericAlloc(&PyClassMethod_Type, 0);
|
|
|
|
if (cm != NULL) {
|
|
|
|
Py_INCREF(callable);
|
|
|
|
cm->cm_callable = callable;
|
|
|
|
}
|
|
|
|
return (PyObject *)cm;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Static method object */
|
|
|
|
|
|
|
|
/* A static method does not receive an implicit first argument.
|
|
|
|
To declare a static method, use this idiom:
|
|
|
|
|
|
|
|
class C:
|
2016-09-17 00:26:16 -03:00
|
|
|
@staticmethod
|
|
|
|
def f(arg1, arg2, ...):
|
|
|
|
...
|
2001-08-02 01:15:00 -03:00
|
|
|
|
|
|
|
It can be called either on the class (e.g. C.f()) or on an instance
|
2018-12-24 03:47:38 -04:00
|
|
|
(e.g. C().f()). Both the class and the instance are ignored, and
|
|
|
|
neither is passed implicitly as the first argument to the method.
|
2001-08-02 01:15:00 -03:00
|
|
|
|
|
|
|
Static methods in Python are similar to those found in Java or C++.
|
|
|
|
For a more advanced concept, see class methods above.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
PyObject *sm_callable;
|
2012-02-19 02:10:25 -04:00
|
|
|
PyObject *sm_dict;
|
2001-08-02 01:15:00 -03:00
|
|
|
} staticmethod;
|
|
|
|
|
|
|
|
static void
|
|
|
|
sm_dealloc(staticmethod *sm)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
_PyObject_GC_UNTRACK((PyObject *)sm);
|
|
|
|
Py_XDECREF(sm->sm_callable);
|
2012-02-19 02:10:25 -04:00
|
|
|
Py_XDECREF(sm->sm_dict);
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_TYPE(sm)->tp_free((PyObject *)sm);
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
2003-04-08 18:28:47 -03:00
|
|
|
static int
|
|
|
|
sm_traverse(staticmethod *sm, visitproc visit, void *arg)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_VISIT(sm->sm_callable);
|
2012-02-19 02:10:25 -04:00
|
|
|
Py_VISIT(sm->sm_dict);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2003-04-08 18:28:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sm_clear(staticmethod *sm)
|
|
|
|
{
|
2012-02-19 02:11:56 -04:00
|
|
|
Py_CLEAR(sm->sm_callable);
|
2012-02-19 02:10:25 -04:00
|
|
|
Py_CLEAR(sm->sm_dict);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2003-04-08 18:28:47 -03:00
|
|
|
}
|
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
static PyObject *
|
|
|
|
sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
staticmethod *sm = (staticmethod *)self;
|
2001-08-02 01:15:00 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (sm->sm_callable == NULL) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"uninitialized staticmethod object");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_INCREF(sm->sm_callable);
|
|
|
|
return sm->sm_callable;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sm_init(PyObject *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
staticmethod *sm = (staticmethod *)self;
|
|
|
|
PyObject *callable;
|
2001-08-02 01:15:00 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!_PyArg_NoKeywords("staticmethod", kwds))
|
|
|
|
return -1;
|
2017-07-09 00:45:06 -03:00
|
|
|
if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
|
|
|
|
return -1;
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_INCREF(callable);
|
2018-02-13 06:28:33 -04:00
|
|
|
Py_XSETREF(sm->sm_callable, callable);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
2009-05-29 01:52:27 -03:00
|
|
|
static PyMemberDef sm_memberlist[] = {
|
2010-05-09 12:52:27 -03:00
|
|
|
{"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
|
|
|
|
{NULL} /* Sentinel */
|
2009-05-29 01:52:27 -03:00
|
|
|
};
|
|
|
|
|
2011-12-15 16:34:02 -04:00
|
|
|
static PyObject *
|
|
|
|
sm_get___isabstractmethod__(staticmethod *sm, void *closure)
|
|
|
|
{
|
|
|
|
int res = _PyObject_IsAbstract(sm->sm_callable);
|
|
|
|
if (res == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (res) {
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
}
|
|
|
|
Py_RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyGetSetDef sm_getsetlist[] = {
|
|
|
|
{"__isabstractmethod__",
|
|
|
|
(getter)sm_get___isabstractmethod__, NULL,
|
|
|
|
NULL,
|
|
|
|
NULL},
|
2012-02-19 21:02:57 -04:00
|
|
|
{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
|
2011-12-15 16:34:02 -04:00
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(staticmethod_doc,
|
2001-12-16 22:53:53 -04:00
|
|
|
"staticmethod(function) -> method\n\
|
|
|
|
\n\
|
|
|
|
Convert a function to be a static method.\n\
|
|
|
|
\n\
|
|
|
|
A static method does not receive an implicit first argument.\n\
|
|
|
|
To declare a static method, use this idiom:\n\
|
|
|
|
\n\
|
|
|
|
class C:\n\
|
2016-09-17 00:26:16 -03:00
|
|
|
@staticmethod\n\
|
|
|
|
def f(arg1, arg2, ...):\n\
|
|
|
|
...\n\
|
2001-12-16 22:53:53 -04:00
|
|
|
\n\
|
|
|
|
It can be called either on the class (e.g. C.f()) or on an instance\n\
|
2018-12-24 03:47:38 -04:00
|
|
|
(e.g. C().f()). Both the class and the instance are ignored, and\n\
|
|
|
|
neither is passed implicitly as the first argument to the method.\n\
|
2001-12-16 22:53:53 -04:00
|
|
|
\n\
|
|
|
|
Static methods in Python are similar to those found in Java or C++.\n\
|
2002-06-13 17:33:02 -03:00
|
|
|
For a more advanced concept, see the classmethod builtin.");
|
2001-12-16 22:53:53 -04:00
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
PyTypeObject PyStaticMethod_Type = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"staticmethod",
|
|
|
|
sizeof(staticmethod),
|
|
|
|
0,
|
|
|
|
(destructor)sm_dealloc, /* tp_dealloc */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_vectorcall_offset */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_as_async */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
2012-02-19 02:10:25 -04:00
|
|
|
0, /* tp_getattro */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
|
|
|
|
staticmethod_doc, /* tp_doc */
|
|
|
|
(traverseproc)sm_traverse, /* tp_traverse */
|
|
|
|
(inquiry)sm_clear, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
0, /* tp_methods */
|
|
|
|
sm_memberlist, /* tp_members */
|
2011-12-15 16:34:02 -04:00
|
|
|
sm_getsetlist, /* tp_getset */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
sm_descr_get, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
2012-02-19 02:10:25 -04:00
|
|
|
offsetof(staticmethod, sm_dict), /* tp_dictoffset */
|
2010-05-09 12:52:27 -03:00
|
|
|
sm_init, /* tp_init */
|
|
|
|
PyType_GenericAlloc, /* tp_alloc */
|
|
|
|
PyType_GenericNew, /* tp_new */
|
|
|
|
PyObject_GC_Del, /* tp_free */
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
2001-08-02 01:15:00 -03:00
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyStaticMethod_New(PyObject *callable)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
staticmethod *sm = (staticmethod *)
|
|
|
|
PyType_GenericAlloc(&PyStaticMethod_Type, 0);
|
|
|
|
if (sm != NULL) {
|
|
|
|
Py_INCREF(callable);
|
|
|
|
sm->sm_callable = callable;
|
|
|
|
}
|
|
|
|
return (PyObject *)sm;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|