1991-02-19 20:39:46 +08:00
|
|
|
|
1990-10-14 20:07:46 +08:00
|
|
|
/* Function object implementation */
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
#include "Python.h"
|
1993-03-29 18:43:31 +08:00
|
|
|
#include "compile.h"
|
2001-08-02 12:15:00 +08:00
|
|
|
#include "eval.h"
|
1990-12-20 23:06:42 +08:00
|
|
|
#include "structmember.h"
|
1990-10-14 20:07:46 +08:00
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject *
|
2000-07-09 14:03:25 +08:00
|
|
|
PyFunction_New(PyObject *code, PyObject *globals)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
PyFunctionObject *op = PyObject_NEW(PyFunctionObject,
|
|
|
|
&PyFunction_Type);
|
1990-10-14 20:07:46 +08:00
|
|
|
if (op != NULL) {
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject *doc;
|
|
|
|
PyObject *consts;
|
2001-03-23 12:19:27 +08:00
|
|
|
op->func_weakreflist = NULL;
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_INCREF(code);
|
1990-11-19 01:44:06 +08:00
|
|
|
op->func_code = code;
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_INCREF(globals);
|
1990-10-14 20:07:46 +08:00
|
|
|
op->func_globals = globals;
|
1997-05-02 11:12:38 +08:00
|
|
|
op->func_name = ((PyCodeObject *)code)->co_name;
|
|
|
|
Py_INCREF(op->func_name);
|
1995-07-18 22:30:34 +08:00
|
|
|
op->func_defaults = NULL; /* No default arguments */
|
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
|
|
|
op->func_closure = NULL;
|
1997-05-02 11:12:38 +08:00
|
|
|
consts = ((PyCodeObject *)code)->co_consts;
|
|
|
|
if (PyTuple_Size(consts) >= 1) {
|
|
|
|
doc = PyTuple_GetItem(consts, 0);
|
2000-04-28 04:14:13 +08:00
|
|
|
if (!PyString_Check(doc) && !PyUnicode_Check(doc))
|
1997-05-02 11:12:38 +08:00
|
|
|
doc = Py_None;
|
1995-01-07 20:01:30 +08:00
|
|
|
}
|
|
|
|
else
|
1997-05-02 11:12:38 +08:00
|
|
|
doc = Py_None;
|
|
|
|
Py_INCREF(doc);
|
1995-01-07 20:01:30 +08:00
|
|
|
op->func_doc = doc;
|
2001-01-16 04:40:19 +08:00
|
|
|
op->func_dict = NULL;
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
2001-01-16 04:40:19 +08:00
|
|
|
else
|
|
|
|
return NULL;
|
2000-06-30 13:02:53 +08:00
|
|
|
PyObject_GC_Init(op);
|
1997-05-02 11:12:38 +08:00
|
|
|
return (PyObject *)op;
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject *
|
2000-07-09 14:03:25 +08:00
|
|
|
PyFunction_GetCode(PyObject *op)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
1990-10-14 20:07:46 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 11:12:38 +08:00
|
|
|
return ((PyFunctionObject *) op) -> func_code;
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject *
|
2000-07-09 14:03:25 +08:00
|
|
|
PyFunction_GetGlobals(PyObject *op)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
1990-10-14 20:07:46 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 11:12:38 +08:00
|
|
|
return ((PyFunctionObject *) op) -> func_globals;
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject *
|
2000-07-09 14:03:25 +08:00
|
|
|
PyFunction_GetDefaults(PyObject *op)
|
1994-08-30 16:27:36 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
1994-08-30 16:27:36 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 11:12:38 +08:00
|
|
|
return ((PyFunctionObject *) op) -> func_defaults;
|
1994-08-30 16:27:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-07-09 14:03:25 +08:00
|
|
|
PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
|
1994-08-30 16:27:36 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
1994-08-30 16:27:36 +08:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 11:12:38 +08:00
|
|
|
if (defaults == Py_None)
|
1995-07-18 22:30:34 +08:00
|
|
|
defaults = NULL;
|
1998-04-11 06:16:39 +08:00
|
|
|
else if (PyTuple_Check(defaults)) {
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_XINCREF(defaults);
|
1998-04-11 06:16:39 +08:00
|
|
|
}
|
1994-08-30 16:27:36 +08:00
|
|
|
else {
|
1997-05-02 11:12:38 +08:00
|
|
|
PyErr_SetString(PyExc_SystemError, "non-tuple default args");
|
1994-08-30 16:27:36 +08:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
|
|
|
|
((PyFunctionObject *) op) -> func_defaults = defaults;
|
1994-08-30 16:27:36 +08: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-26 04:06:59 +08:00
|
|
|
PyObject *
|
|
|
|
PyFunction_GetClosure(PyObject *op)
|
|
|
|
{
|
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyFunctionObject *) op) -> func_closure;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PyFunction_SetClosure(PyObject *op, PyObject *closure)
|
|
|
|
{
|
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (closure == Py_None)
|
|
|
|
closure = NULL;
|
|
|
|
else if (PyTuple_Check(closure)) {
|
|
|
|
Py_XINCREF(closure);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_SystemError, "non-tuple closure");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
|
|
|
|
((PyFunctionObject *) op) -> func_closure = closure;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1990-10-14 20:07:46 +08:00
|
|
|
/* Methods */
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
#define OFF(x) offsetof(PyFunctionObject, x)
|
1990-12-20 23:06:42 +08:00
|
|
|
|
|
|
|
static struct memberlist func_memberlist[] = {
|
2001-01-20 03:53:29 +08:00
|
|
|
{"func_code", T_OBJECT, OFF(func_code)},
|
|
|
|
{"func_globals", T_OBJECT, OFF(func_globals), READONLY},
|
|
|
|
{"func_name", T_OBJECT, OFF(func_name), READONLY},
|
|
|
|
{"__name__", T_OBJECT, OFF(func_name), READONLY},
|
2001-02-28 10:42:56 +08:00
|
|
|
{"func_closure", T_OBJECT, OFF(func_closure), READONLY},
|
2001-01-20 03:53:29 +08:00
|
|
|
{"func_defaults", T_OBJECT, OFF(func_defaults)},
|
|
|
|
{"func_doc", T_OBJECT, OFF(func_doc)},
|
|
|
|
{"__doc__", T_OBJECT, OFF(func_doc)},
|
|
|
|
{"func_dict", T_OBJECT, OFF(func_dict)},
|
|
|
|
{"__dict__", T_OBJECT, OFF(func_dict)},
|
|
|
|
{NULL} /* Sentinel */
|
1990-12-20 23:06:42 +08:00
|
|
|
};
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2001-08-02 12:15:00 +08:00
|
|
|
func_getattro(PyObject *op, PyObject *name)
|
1990-12-20 23:06:42 +08:00
|
|
|
{
|
2001-01-16 04:40:19 +08:00
|
|
|
char *sname = PyString_AsString(name);
|
|
|
|
|
|
|
|
if (sname[0] != '_' && PyEval_GetRestricted()) {
|
1997-05-02 11:12:38 +08:00
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
1995-01-10 18:39:49 +08:00
|
|
|
"function attributes not accessible in restricted mode");
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-08-15 02:23:58 +08:00
|
|
|
/* If func_dict is being accessed but no attribute has been set
|
|
|
|
* yet, then initialize it to the empty dictionary.
|
|
|
|
*/
|
|
|
|
if ((!strcmp(sname, "func_dict") || !strcmp(sname, "__dict__"))
|
|
|
|
&& ((PyFunctionObject*)op)->func_dict == NULL)
|
|
|
|
{
|
|
|
|
PyFunctionObject* funcop = (PyFunctionObject*)op;
|
|
|
|
|
|
|
|
funcop->func_dict = PyDict_New();
|
|
|
|
if (funcop->func_dict == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-08-02 12:15:00 +08:00
|
|
|
return PyObject_GenericGetAttr(op, name);
|
1990-12-20 23:06:42 +08:00
|
|
|
}
|
|
|
|
|
1998-05-22 08:55:34 +08:00
|
|
|
static int
|
2001-08-02 12:15:00 +08:00
|
|
|
func_setattro(PyObject *op, PyObject *name, PyObject *value)
|
1998-05-22 08:55:34 +08:00
|
|
|
{
|
2001-01-16 04:40:19 +08:00
|
|
|
char *sname = PyString_AsString(name);
|
|
|
|
|
1998-05-22 08:55:34 +08:00
|
|
|
if (PyEval_GetRestricted()) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"function attributes not settable in restricted mode");
|
|
|
|
return -1;
|
|
|
|
}
|
2001-01-16 04:40:19 +08:00
|
|
|
if (strcmp(sname, "func_code") == 0) {
|
2001-01-20 03:53:29 +08:00
|
|
|
/* not legal to del f.func_code or to set it to anything
|
|
|
|
* other than a code object.
|
|
|
|
*/
|
1998-05-22 08:55:34 +08:00
|
|
|
if (value == NULL || !PyCode_Check(value)) {
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_TypeError,
|
|
|
|
"func_code must be set to a code object");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2001-01-16 04:40:19 +08:00
|
|
|
else if (strcmp(sname, "func_defaults") == 0) {
|
2001-01-20 03:53:29 +08: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)) {
|
1998-05-22 08:55:34 +08:00
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_TypeError,
|
|
|
|
"func_defaults must be set to a tuple object");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2001-01-16 04:40:19 +08:00
|
|
|
else if (!strcmp(sname, "func_dict") || !strcmp(sname, "__dict__")) {
|
2001-08-15 02:23:58 +08:00
|
|
|
/* It is illegal to del f.func_dict. Can only set
|
|
|
|
* func_dict to a dictionary.
|
2001-01-20 03:53:29 +08:00
|
|
|
*/
|
2001-08-15 02:23:58 +08:00
|
|
|
if (value == NULL) {
|
2001-01-16 04:40:19 +08:00
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_TypeError,
|
2001-08-15 02:23:58 +08:00
|
|
|
"function's dictionary may not be deleted");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!PyDict_Check(value)) {
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_TypeError,
|
|
|
|
"setting function's dictionary to a non-dict");
|
2001-01-16 04:40:19 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2001-08-02 12:15:00 +08:00
|
|
|
return PyObject_GenericSetAttr(op, name, value);
|
1998-05-22 08:55:34 +08:00
|
|
|
}
|
|
|
|
|
1990-10-14 20:07:46 +08:00
|
|
|
static void
|
2000-07-09 14:03:25 +08:00
|
|
|
func_dealloc(PyFunctionObject *op)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
2001-03-23 12:19:27 +08:00
|
|
|
PyObject_ClearWeakRefs((PyObject *) op);
|
2000-06-30 13:02:53 +08:00
|
|
|
PyObject_GC_Fini(op);
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_DECREF(op->func_code);
|
|
|
|
Py_DECREF(op->func_globals);
|
|
|
|
Py_DECREF(op->func_name);
|
|
|
|
Py_XDECREF(op->func_defaults);
|
|
|
|
Py_XDECREF(op->func_doc);
|
2001-01-16 04:40:19 +08:00
|
|
|
Py_XDECREF(op->func_dict);
|
2001-03-01 14:06:37 +08:00
|
|
|
Py_XDECREF(op->func_closure);
|
2000-07-01 09:00:38 +08:00
|
|
|
op = (PyFunctionObject *) PyObject_AS_GC(op);
|
2000-05-04 07:44:39 +08:00
|
|
|
PyObject_DEL(op);
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject*
|
2000-07-09 14:03:25 +08:00
|
|
|
func_repr(PyFunctionObject *op)
|
1993-03-29 18:43:31 +08:00
|
|
|
{
|
|
|
|
char buf[140];
|
1997-05-02 11:12:38 +08:00
|
|
|
if (op->func_name == Py_None)
|
2000-06-30 23:01:00 +08:00
|
|
|
sprintf(buf, "<anonymous function at %p>", op);
|
1993-11-30 21:40:46 +08:00
|
|
|
else
|
2000-06-30 23:01:00 +08:00
|
|
|
sprintf(buf, "<function %.100s at %p>",
|
1997-05-02 11:12:38 +08:00
|
|
|
PyString_AsString(op->func_name),
|
2000-06-30 23:01:00 +08:00
|
|
|
op);
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyString_FromString(buf);
|
1993-03-29 18:43:31 +08:00
|
|
|
}
|
|
|
|
|
2000-06-23 22:18:11 +08:00
|
|
|
static int
|
|
|
|
func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
if (f->func_code) {
|
|
|
|
err = visit(f->func_code, arg);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
if (f->func_globals) {
|
|
|
|
err = visit(f->func_globals, arg);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
if (f->func_defaults) {
|
|
|
|
err = visit(f->func_defaults, arg);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
if (f->func_doc) {
|
|
|
|
err = visit(f->func_doc, arg);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
if (f->func_name) {
|
|
|
|
err = visit(f->func_name, arg);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
2001-01-16 04:40:19 +08:00
|
|
|
if (f->func_dict) {
|
|
|
|
err = visit(f->func_dict, arg);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
2001-03-01 14:06:37 +08:00
|
|
|
if (f->func_closure) {
|
|
|
|
err = visit(f->func_closure, arg);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
2000-06-23 22:18:11 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-08-02 12:15:00 +08:00
|
|
|
static PyObject *
|
|
|
|
function_call(PyObject *func, PyObject *arg, PyObject *kw)
|
|
|
|
{
|
|
|
|
PyObject *result;
|
|
|
|
PyObject *argdefs;
|
|
|
|
PyObject **d, **k;
|
|
|
|
int nk, nd;
|
|
|
|
|
|
|
|
argdefs = PyFunction_GET_DEFAULTS(func);
|
|
|
|
if (argdefs != NULL && PyTuple_Check(argdefs)) {
|
|
|
|
d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
|
|
|
|
nd = PyTuple_Size(argdefs);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
d = NULL;
|
|
|
|
nd = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kw != NULL && PyDict_Check(kw)) {
|
|
|
|
int pos, i;
|
|
|
|
nk = PyDict_Size(kw);
|
|
|
|
k = PyMem_NEW(PyObject *, 2*nk);
|
|
|
|
if (k == NULL) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
Py_DECREF(arg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
pos = i = 0;
|
|
|
|
while (PyDict_Next(kw, &pos, &k[i], &k[i+1]))
|
|
|
|
i += 2;
|
|
|
|
nk = i/2;
|
|
|
|
/* XXX This is broken if the caller deletes dict items! */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
k = NULL;
|
|
|
|
nk = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = PyEval_EvalCodeEx(
|
|
|
|
(PyCodeObject *)PyFunction_GET_CODE(func),
|
|
|
|
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)
|
|
|
|
PyMem_DEL(k);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bind a function to an object */
|
|
|
|
static PyObject *
|
|
|
|
func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
|
|
|
|
{
|
|
|
|
if (obj == Py_None)
|
|
|
|
obj = NULL;
|
|
|
|
return PyMethod_New(func, obj, type);
|
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
PyTypeObject PyFunction_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1990-10-14 20:07:46 +08:00
|
|
|
0,
|
|
|
|
"function",
|
2000-06-30 13:02:53 +08:00
|
|
|
sizeof(PyFunctionObject) + PyGC_HEAD_SIZE,
|
1990-10-14 20:07:46 +08:00
|
|
|
0,
|
2001-08-02 12:15:00 +08:00
|
|
|
(destructor)func_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_compare */
|
|
|
|
(reprfunc)func_repr, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
function_call, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
func_getattro, /* tp_getattro */
|
|
|
|
func_setattro, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
|
|
|
|
0, /* tp_doc */
|
|
|
|
(traverseproc)func_traverse, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
2001-03-23 12:19:27 +08:00
|
|
|
offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
|
2001-08-02 12:15:00 +08:00
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
0, /* tp_methods */
|
|
|
|
func_memberlist, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
func_descr_get, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* 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:
|
|
|
|
def f(cls, arg1, arg2, ...): ...
|
|
|
|
f = classmethod(f)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
PyObject_HEAD
|
|
|
|
PyObject *cm_callable;
|
|
|
|
} classmethod;
|
|
|
|
|
|
|
|
static void
|
|
|
|
cm_dealloc(classmethod *cm)
|
|
|
|
{
|
|
|
|
Py_XDECREF(cm->cm_callable);
|
|
|
|
PyObject_DEL(cm);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
|
|
|
|
{
|
|
|
|
classmethod *cm = (classmethod *)self;
|
|
|
|
|
|
|
|
if (cm->cm_callable == NULL) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"uninitialized classmethod object");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyMethod_New(cm->cm_callable,
|
|
|
|
type, (PyObject *)(type->ob_type));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cm_init(PyObject *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
|
|
|
classmethod *cm = (classmethod *)self;
|
|
|
|
PyObject *callable;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "O:callable", &callable))
|
|
|
|
return -1;
|
|
|
|
Py_INCREF(callable);
|
|
|
|
cm->cm_callable = callable;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyTypeObject PyClassMethod_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
|
|
|
0,
|
|
|
|
"classmethod",
|
|
|
|
sizeof(classmethod),
|
|
|
|
0,
|
|
|
|
(destructor)cm_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_compare */
|
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
|
|
|
|
0, /* tp_doc */
|
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
0, /* tp_methods */
|
|
|
|
0, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
cm_descr_get, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
|
|
|
cm_init, /* tp_init */
|
|
|
|
PyType_GenericAlloc, /* tp_alloc */
|
|
|
|
PyType_GenericNew, /* tp_new */
|
|
|
|
};
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyClassMethod_New(PyObject *callable)
|
|
|
|
{
|
|
|
|
classmethod *cm = (classmethod *)
|
|
|
|
PyType_GenericAlloc(&PyClassMethod_Type, 0);
|
|
|
|
if (cm != NULL) {
|
|
|
|
Py_INCREF(callable);
|
|
|
|
cm->cm_callable = callable;
|
|
|
|
}
|
|
|
|
return (PyObject *)cm;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Static method object */
|
|
|
|
|
|
|
|
/* A static method does not receive an implicit first argument.
|
|
|
|
To declare a static method, use this idiom:
|
|
|
|
|
|
|
|
class C:
|
|
|
|
def f(arg1, arg2, ...): ...
|
|
|
|
f = staticmethod(f)
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
Static methods in Python are similar to those found in Java or C++.
|
|
|
|
For a more advanced concept, see class methods above.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
PyObject *sm_callable;
|
|
|
|
} staticmethod;
|
|
|
|
|
|
|
|
static void
|
|
|
|
sm_dealloc(staticmethod *sm)
|
|
|
|
{
|
|
|
|
Py_XDECREF(sm->sm_callable);
|
|
|
|
PyObject_DEL(sm);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
|
|
|
|
{
|
|
|
|
staticmethod *sm = (staticmethod *)self;
|
|
|
|
|
|
|
|
if (sm->sm_callable == NULL) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"uninitialized staticmethod object");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_INCREF(sm->sm_callable);
|
|
|
|
return sm->sm_callable;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sm_init(PyObject *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
|
|
|
staticmethod *sm = (staticmethod *)self;
|
|
|
|
PyObject *callable;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "O:callable", &callable))
|
|
|
|
return -1;
|
|
|
|
Py_INCREF(callable);
|
|
|
|
sm->sm_callable = callable;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyTypeObject PyStaticMethod_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
|
|
|
0,
|
|
|
|
"staticmethod",
|
|
|
|
sizeof(staticmethod),
|
|
|
|
0,
|
|
|
|
(destructor)sm_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_compare */
|
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
|
|
|
|
0, /* tp_doc */
|
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
0, /* tp_methods */
|
|
|
|
0, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
sm_descr_get, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
|
|
|
sm_init, /* tp_init */
|
|
|
|
PyType_GenericAlloc, /* tp_alloc */
|
|
|
|
PyType_GenericNew, /* tp_new */
|
1990-10-14 20:07:46 +08:00
|
|
|
};
|
2001-08-02 12:15:00 +08:00
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyStaticMethod_New(PyObject *callable)
|
|
|
|
{
|
|
|
|
staticmethod *sm = (staticmethod *)
|
|
|
|
PyType_GenericAlloc(&PyStaticMethod_Type, 0);
|
|
|
|
if (sm != NULL) {
|
|
|
|
Py_INCREF(callable);
|
|
|
|
sm->sm_callable = callable;
|
|
|
|
}
|
|
|
|
return (PyObject *)sm;
|
|
|
|
}
|