1991-02-19 12:39:46 +00:00
|
|
|
/***********************************************************
|
1995-01-04 19:07:38 +00:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1991-02-19 12:39:46 +00:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 14:44:06 +00:00
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
1991-02-19 12:39:46 +00:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 14:44:06 +00:00
|
|
|
both that copyright notice and this permission notice appear in
|
1991-02-19 12:39:46 +00:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 14:44:06 +00:00
|
|
|
Centrum or CWI or Corporation for National Research Initiatives or
|
|
|
|
CNRI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior
|
|
|
|
permission.
|
|
|
|
|
|
|
|
While CWI is the initial source for this software, a modified version
|
|
|
|
is made available by the Corporation for National Research Initiatives
|
|
|
|
(CNRI) at the Internet address ftp://ftp.python.org.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
|
|
|
|
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
|
|
|
|
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
PERFORMANCE OF THIS SOFTWARE.
|
1991-02-19 12:39:46 +00:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
1990-10-14 12:07:46 +00:00
|
|
|
/* Function object implementation */
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
#include "Python.h"
|
1993-03-29 10:43:31 +00:00
|
|
|
#include "compile.h"
|
1990-12-20 15:06:42 +00:00
|
|
|
#include "structmember.h"
|
1990-10-14 12:07:46 +00:00
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *
|
|
|
|
PyFunction_New(code, globals)
|
|
|
|
PyObject *code;
|
|
|
|
PyObject *globals;
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFunctionObject *op = PyObject_NEW(PyFunctionObject,
|
|
|
|
&PyFunction_Type);
|
1990-10-14 12:07:46 +00:00
|
|
|
if (op != NULL) {
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *doc;
|
|
|
|
PyObject *consts;
|
|
|
|
Py_INCREF(code);
|
1990-11-18 17:44:06 +00:00
|
|
|
op->func_code = code;
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_INCREF(globals);
|
1990-10-14 12:07:46 +00:00
|
|
|
op->func_globals = globals;
|
1997-05-02 03:12:38 +00:00
|
|
|
op->func_name = ((PyCodeObject *)code)->co_name;
|
|
|
|
Py_INCREF(op->func_name);
|
1995-07-18 14:30:34 +00:00
|
|
|
op->func_defaults = NULL; /* No default arguments */
|
1997-05-02 03:12:38 +00:00
|
|
|
consts = ((PyCodeObject *)code)->co_consts;
|
|
|
|
if (PyTuple_Size(consts) >= 1) {
|
|
|
|
doc = PyTuple_GetItem(consts, 0);
|
2000-04-27 20:14:13 +00:00
|
|
|
if (!PyString_Check(doc) && !PyUnicode_Check(doc))
|
1997-05-02 03:12:38 +00:00
|
|
|
doc = Py_None;
|
1995-01-07 12:01:30 +00:00
|
|
|
}
|
|
|
|
else
|
1997-05-02 03:12:38 +00:00
|
|
|
doc = Py_None;
|
|
|
|
Py_INCREF(doc);
|
1995-01-07 12:01:30 +00:00
|
|
|
op->func_doc = doc;
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
return (PyObject *)op;
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *
|
|
|
|
PyFunction_GetCode(op)
|
|
|
|
PyObject *op;
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1997-05-02 03:12:38 +00:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
return ((PyFunctionObject *) op) -> func_code;
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *
|
|
|
|
PyFunction_GetGlobals(op)
|
|
|
|
PyObject *op;
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1997-05-02 03:12:38 +00:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
return ((PyFunctionObject *) op) -> func_globals;
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *
|
1995-07-18 14:30:34 +00:00
|
|
|
PyFunction_GetDefaults(op)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *op;
|
1994-08-30 08:27:36 +00:00
|
|
|
{
|
1997-05-02 03:12:38 +00:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
1994-08-30 08:27:36 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
return ((PyFunctionObject *) op) -> func_defaults;
|
1994-08-30 08:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1995-07-18 14:30:34 +00:00
|
|
|
PyFunction_SetDefaults(op, defaults)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *op;
|
|
|
|
PyObject *defaults;
|
1994-08-30 08:27:36 +00:00
|
|
|
{
|
1997-05-02 03:12:38 +00:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
1994-08-30 08:27:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
if (defaults == Py_None)
|
1995-07-18 14:30:34 +00:00
|
|
|
defaults = NULL;
|
1998-04-10 22:16:39 +00:00
|
|
|
else if (PyTuple_Check(defaults)) {
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_XINCREF(defaults);
|
1998-04-10 22:16:39 +00:00
|
|
|
}
|
1994-08-30 08:27:36 +00:00
|
|
|
else {
|
1997-05-02 03:12:38 +00:00
|
|
|
PyErr_SetString(PyExc_SystemError, "non-tuple default args");
|
1994-08-30 08:27:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
|
|
|
|
((PyFunctionObject *) op) -> func_defaults = defaults;
|
1994-08-30 08:27:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1990-10-14 12:07:46 +00:00
|
|
|
/* Methods */
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
#define OFF(x) offsetof(PyFunctionObject, x)
|
1990-12-20 15:06:42 +00:00
|
|
|
|
|
|
|
static struct memberlist func_memberlist[] = {
|
1998-05-22 00:55:34 +00:00
|
|
|
{"func_code", T_OBJECT, OFF(func_code)},
|
1992-01-14 18:32:20 +00:00
|
|
|
{"func_globals",T_OBJECT, OFF(func_globals), READONLY},
|
1993-05-19 14:50:45 +00:00
|
|
|
{"func_name", T_OBJECT, OFF(func_name), READONLY},
|
1995-01-10 10:39:49 +00:00
|
|
|
{"__name__", T_OBJECT, OFF(func_name), READONLY},
|
1998-05-22 00:55:34 +00:00
|
|
|
{"func_defaults",T_OBJECT, OFF(func_defaults)},
|
1995-01-07 12:01:30 +00:00
|
|
|
{"func_doc", T_OBJECT, OFF(func_doc)},
|
|
|
|
{"__doc__", T_OBJECT, OFF(func_doc)},
|
1990-12-20 15:06:42 +00:00
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
static PyObject *
|
1990-12-20 15:06:42 +00:00
|
|
|
func_getattr(op, name)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFunctionObject *op;
|
1990-12-20 15:06:42 +00:00
|
|
|
char *name;
|
|
|
|
{
|
1997-05-02 03:12:38 +00:00
|
|
|
if (name[0] != '_' && PyEval_GetRestricted()) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
1995-01-10 10:39:49 +00:00
|
|
|
"function attributes not accessible in restricted mode");
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
return PyMember_Get((char *)op, func_memberlist, name);
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
|
|
|
|
1998-05-22 00:55:34 +00:00
|
|
|
static int
|
|
|
|
func_setattr(op, name, value)
|
|
|
|
PyFunctionObject *op;
|
|
|
|
char *name;
|
|
|
|
PyObject *value;
|
|
|
|
{
|
|
|
|
if (PyEval_GetRestricted()) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"function attributes not settable in restricted mode");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (strcmp(name, "func_code") == 0) {
|
|
|
|
if (value == NULL || !PyCode_Check(value)) {
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_TypeError,
|
|
|
|
"func_code must be set to a code object");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strcmp(name, "func_defaults") == 0) {
|
|
|
|
if (value != Py_None && !PyTuple_Check(value)) {
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_TypeError,
|
|
|
|
"func_defaults must be set to a tuple object");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (value == Py_None)
|
|
|
|
value = NULL;
|
|
|
|
}
|
|
|
|
return PyMember_Set((char *)op, func_memberlist, name, value);
|
|
|
|
}
|
|
|
|
|
1990-10-14 12:07:46 +00:00
|
|
|
static void
|
1990-12-20 15:06:42 +00:00
|
|
|
func_dealloc(op)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFunctionObject *op;
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1997-05-02 03:12:38 +00: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);
|
2000-05-03 23:44:39 +00:00
|
|
|
PyObject_DEL(op);
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
static PyObject*
|
1993-03-29 10:43:31 +00:00
|
|
|
func_repr(op)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFunctionObject *op;
|
1993-03-29 10:43:31 +00:00
|
|
|
{
|
|
|
|
char buf[140];
|
1997-05-02 03:12:38 +00:00
|
|
|
if (op->func_name == Py_None)
|
1993-11-30 13:40:46 +00:00
|
|
|
sprintf(buf, "<anonymous function at %lx>", (long)op);
|
|
|
|
else
|
|
|
|
sprintf(buf, "<function %.100s at %lx>",
|
1997-05-02 03:12:38 +00:00
|
|
|
PyString_AsString(op->func_name),
|
1993-11-30 13:40:46 +00:00
|
|
|
(long)op);
|
1997-05-02 03:12:38 +00:00
|
|
|
return PyString_FromString(buf);
|
1993-03-29 10:43:31 +00:00
|
|
|
}
|
|
|
|
|
1993-11-05 10:20:10 +00:00
|
|
|
static int
|
|
|
|
func_compare(f, g)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFunctionObject *f, *g;
|
1993-11-05 10:20:10 +00:00
|
|
|
{
|
1994-08-30 08:27:36 +00:00
|
|
|
int c;
|
1993-11-05 10:20:10 +00:00
|
|
|
if (f->func_globals != g->func_globals)
|
|
|
|
return (f->func_globals < g->func_globals) ? -1 : 1;
|
1997-08-05 16:51:05 +00:00
|
|
|
if (f->func_defaults != g->func_defaults) {
|
|
|
|
if (f->func_defaults == NULL)
|
|
|
|
return -1;
|
|
|
|
if (g->func_defaults == NULL)
|
|
|
|
return 1;
|
|
|
|
c = PyObject_Compare(f->func_defaults, g->func_defaults);
|
|
|
|
if (c != 0)
|
|
|
|
return c;
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
return PyObject_Compare(f->func_code, g->func_code);
|
1993-11-05 10:20:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static long
|
|
|
|
func_hash(f)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFunctionObject *f;
|
1993-11-05 10:20:10 +00:00
|
|
|
{
|
|
|
|
long h;
|
1997-05-02 03:12:38 +00:00
|
|
|
h = PyObject_Hash(f->func_code);
|
1993-11-05 10:20:10 +00:00
|
|
|
if (h == -1) return h;
|
|
|
|
h = h ^ (long)f->func_globals;
|
|
|
|
if (h == -1) h = -2;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2000-06-23 14:18:11 +00: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;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
PyTypeObject PyFunction_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1990-10-14 12:07:46 +00:00
|
|
|
0,
|
|
|
|
"function",
|
2000-06-23 19:37:02 +00:00
|
|
|
sizeof(PyFunctionObject) + PyGC_INFO_SIZE,
|
1990-10-14 12:07:46 +00:00
|
|
|
0,
|
1994-08-30 08:27:36 +00:00
|
|
|
(destructor)func_dealloc, /*tp_dealloc*/
|
1990-11-18 17:44:06 +00:00
|
|
|
0, /*tp_print*/
|
1994-08-30 08:27:36 +00:00
|
|
|
(getattrfunc)func_getattr, /*tp_getattr*/
|
1998-05-22 00:55:34 +00:00
|
|
|
(setattrfunc)func_setattr, /*tp_setattr*/
|
1994-08-30 08:27:36 +00:00
|
|
|
(cmpfunc)func_compare, /*tp_compare*/
|
|
|
|
(reprfunc)func_repr, /*tp_repr*/
|
1993-11-05 10:20:10 +00:00
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
1994-08-30 08:27:36 +00:00
|
|
|
(hashfunc)func_hash, /*tp_hash*/
|
2000-06-23 14:18:11 +00:00
|
|
|
0, /*tp_call*/
|
|
|
|
0, /*tp_str*/
|
|
|
|
0, /*tp_getattro*/
|
|
|
|
0, /*tp_setattro*/
|
|
|
|
0, /* tp_as_buffer */
|
2000-06-23 19:37:02 +00:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
|
2000-06-23 14:18:11 +00:00
|
|
|
0, /* tp_doc */
|
|
|
|
(traverseproc)func_traverse, /* tp_traverse */
|
1990-10-14 12:07:46 +00:00
|
|
|
};
|