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"
|
1993-03-29 06:43:31 -04:00
|
|
|
#include "compile.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 *
|
2000-07-09 03:03:25 -03:00
|
|
|
PyFunction_New(PyObject *code, PyObject *globals)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyFunctionObject *op = PyObject_NEW(PyFunctionObject,
|
|
|
|
&PyFunction_Type);
|
1990-10-14 09:07:46 -03:00
|
|
|
if (op != NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *doc;
|
|
|
|
PyObject *consts;
|
|
|
|
Py_INCREF(code);
|
1990-11-18 13:44:06 -04:00
|
|
|
op->func_code = code;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(globals);
|
1990-10-14 09:07:46 -03:00
|
|
|
op->func_globals = globals;
|
1997-05-02 00:12:38 -03:00
|
|
|
op->func_name = ((PyCodeObject *)code)->co_name;
|
|
|
|
Py_INCREF(op->func_name);
|
1995-07-18 11:30:34 -03:00
|
|
|
op->func_defaults = NULL; /* No default arguments */
|
1997-05-02 00:12:38 -03:00
|
|
|
consts = ((PyCodeObject *)code)->co_consts;
|
|
|
|
if (PyTuple_Size(consts) >= 1) {
|
|
|
|
doc = PyTuple_GetItem(consts, 0);
|
2000-04-27 17:14:13 -03:00
|
|
|
if (!PyString_Check(doc) && !PyUnicode_Check(doc))
|
1997-05-02 00:12:38 -03:00
|
|
|
doc = Py_None;
|
1995-01-07 08:01:30 -04:00
|
|
|
}
|
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
doc = Py_None;
|
|
|
|
Py_INCREF(doc);
|
1995-01-07 08:01:30 -04:00
|
|
|
op->func_doc = doc;
|
2001-01-15 16:40:19 -04:00
|
|
|
op->func_dict = NULL;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
2001-01-15 16:40:19 -04:00
|
|
|
else
|
|
|
|
return NULL;
|
2000-06-30 02:02:53 -03:00
|
|
|
PyObject_GC_Init(op);
|
1997-05-02 00:12:38 -03:00
|
|
|
return (PyObject *)op;
|
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_GetCode(PyObject *op)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
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
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return ((PyFunctionObject *) op) -> func_globals;
|
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_GetDefaults(PyObject *op)
|
1994-08-30 05:27:36 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
1994-08-30 05:27:36 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
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
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
1994-08-30 05:27:36 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
if (defaults == Py_None)
|
1995-07-18 11:30:34 -03:00
|
|
|
defaults = NULL;
|
1998-04-10 19:16:39 -03:00
|
|
|
else if (PyTuple_Check(defaults)) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_XINCREF(defaults);
|
1998-04-10 19:16:39 -03:00
|
|
|
}
|
1994-08-30 05:27:36 -03:00
|
|
|
else {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_SystemError, "non-tuple default args");
|
1994-08-30 05:27:36 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
|
|
|
|
((PyFunctionObject *) op) -> func_defaults = defaults;
|
1994-08-30 05:27:36 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
static struct memberlist func_memberlist[] = {
|
1998-05-21 21:55:34 -03:00
|
|
|
{"func_code", T_OBJECT, OFF(func_code)},
|
1992-01-14 14:32:20 -04:00
|
|
|
{"func_globals",T_OBJECT, OFF(func_globals), READONLY},
|
1993-05-19 11:50:45 -03:00
|
|
|
{"func_name", T_OBJECT, OFF(func_name), READONLY},
|
1995-01-10 06:39:49 -04:00
|
|
|
{"__name__", T_OBJECT, OFF(func_name), READONLY},
|
1998-05-21 21:55:34 -03:00
|
|
|
{"func_defaults",T_OBJECT, OFF(func_defaults)},
|
1995-01-07 08:01:30 -04:00
|
|
|
{"func_doc", T_OBJECT, OFF(func_doc)},
|
|
|
|
{"__doc__", T_OBJECT, OFF(func_doc)},
|
1990-12-20 11:06:42 -04:00
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2001-01-15 16:40:19 -04:00
|
|
|
func_getattro(PyFunctionObject *op, PyObject *name)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
2001-01-15 16:40:19 -04:00
|
|
|
PyObject *rtn;
|
|
|
|
char *sname = PyString_AsString(name);
|
|
|
|
|
|
|
|
if (sname[0] != '_' && PyEval_GetRestricted()) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
1995-01-10 06:39:49 -04:00
|
|
|
"function attributes not accessible in restricted mode");
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-01-15 16:40:19 -04:00
|
|
|
|
|
|
|
if (!strcmp(sname, "__dict__") || !strcmp(sname, "func_dict")) {
|
|
|
|
if (op->func_dict == NULL)
|
|
|
|
rtn = Py_None;
|
|
|
|
else
|
|
|
|
rtn = op->func_dict;
|
|
|
|
|
|
|
|
Py_INCREF(rtn);
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no API for PyMember_HasAttr() */
|
|
|
|
rtn = PyMember_Get((char *)op, func_memberlist, sname);
|
|
|
|
|
|
|
|
if (rtn == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
|
|
|
|
PyErr_Clear();
|
|
|
|
if (op->func_dict != NULL) {
|
|
|
|
rtn = PyDict_GetItem(op->func_dict, name);
|
|
|
|
Py_XINCREF(rtn);
|
|
|
|
}
|
|
|
|
if (rtn == NULL)
|
|
|
|
PyErr_SetObject(PyExc_AttributeError, name);
|
|
|
|
}
|
|
|
|
return rtn;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1998-05-21 21:55:34 -03:00
|
|
|
static int
|
2001-01-15 16:40:19 -04:00
|
|
|
func_setattro(PyFunctionObject *op, PyObject *name, PyObject *value)
|
1998-05-21 21:55:34 -03:00
|
|
|
{
|
2001-01-15 16:40:19 -04:00
|
|
|
int rtn;
|
|
|
|
char *sname = PyString_AsString(name);
|
|
|
|
|
1998-05-21 21:55:34 -03:00
|
|
|
if (PyEval_GetRestricted()) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"function attributes not settable in restricted mode");
|
|
|
|
return -1;
|
|
|
|
}
|
2001-01-15 16:40:19 -04:00
|
|
|
if (strcmp(sname, "func_code") == 0) {
|
1998-05-21 21:55:34 -03:00
|
|
|
if (value == NULL || !PyCode_Check(value)) {
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_TypeError,
|
|
|
|
"func_code must be set to a code object");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2001-01-15 16:40:19 -04:00
|
|
|
else if (strcmp(sname, "func_defaults") == 0) {
|
1998-05-21 21:55:34 -03:00
|
|
|
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;
|
|
|
|
}
|
2001-01-15 16:40:19 -04:00
|
|
|
else if (!strcmp(sname, "func_dict") || !strcmp(sname, "__dict__")) {
|
|
|
|
if (value != Py_None && !PyDict_Check(value)) {
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_TypeError,
|
|
|
|
"func_dict must be set to a dict object");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (value == Py_None)
|
|
|
|
value = NULL;
|
|
|
|
|
|
|
|
Py_XDECREF(op->func_dict);
|
|
|
|
Py_XINCREF(value);
|
|
|
|
op->func_dict = value;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rtn = PyMember_Set((char *)op, func_memberlist, sname, value);
|
|
|
|
if (rtn < 0 && PyErr_ExceptionMatches(PyExc_AttributeError)) {
|
|
|
|
PyErr_Clear();
|
|
|
|
if (op->func_dict == NULL) {
|
|
|
|
op->func_dict = PyDict_New();
|
|
|
|
if (op->func_dict == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
rtn = PyDict_SetItem(op->func_dict, name, value);
|
|
|
|
}
|
|
|
|
return rtn;
|
1998-05-21 21:55:34 -03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2000-06-30 02:02:53 -03:00
|
|
|
PyObject_GC_Fini(op);
|
1997-05-02 00:12:38 -03: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-15 16:40:19 -04:00
|
|
|
Py_XDECREF(op->func_dict);
|
2000-06-30 22:00:38 -03:00
|
|
|
op = (PyFunctionObject *) PyObject_AS_GC(op);
|
2000-05-03 20:44:39 -03:00
|
|
|
PyObject_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
|
|
|
{
|
|
|
|
char buf[140];
|
1997-05-02 00:12:38 -03:00
|
|
|
if (op->func_name == Py_None)
|
2000-06-30 12:01:00 -03:00
|
|
|
sprintf(buf, "<anonymous function at %p>", op);
|
1993-11-30 09:40:46 -04:00
|
|
|
else
|
2000-06-30 12:01:00 -03:00
|
|
|
sprintf(buf, "<function %.100s at %p>",
|
1997-05-02 00:12:38 -03:00
|
|
|
PyString_AsString(op->func_name),
|
2000-06-30 12:01:00 -03:00
|
|
|
op);
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyString_FromString(buf);
|
1993-03-29 06:43:31 -04:00
|
|
|
}
|
|
|
|
|
1993-11-05 06:20:10 -04:00
|
|
|
static int
|
2000-07-09 03:03:25 -03:00
|
|
|
func_compare(PyFunctionObject *f, PyFunctionObject *g)
|
1993-11-05 06:20:10 -04:00
|
|
|
{
|
1994-08-30 05:27:36 -03:00
|
|
|
int c;
|
1993-11-05 06:20:10 -04:00
|
|
|
if (f->func_globals != g->func_globals)
|
|
|
|
return (f->func_globals < g->func_globals) ? -1 : 1;
|
1997-08-05 13:51:05 -03: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 00:12:38 -03:00
|
|
|
return PyObject_Compare(f->func_code, g->func_code);
|
1993-11-05 06:20:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static long
|
2000-07-09 03:03:25 -03:00
|
|
|
func_hash(PyFunctionObject *f)
|
1993-11-05 06:20:10 -04:00
|
|
|
{
|
2000-06-29 16:17:04 -03:00
|
|
|
long h,x;
|
1997-05-02 00:12:38 -03:00
|
|
|
h = PyObject_Hash(f->func_code);
|
1993-11-05 06:20:10 -04:00
|
|
|
if (h == -1) return h;
|
2000-06-29 16:17:04 -03:00
|
|
|
x = _Py_HashPointer(f->func_globals);
|
|
|
|
if (x == -1) return x;
|
|
|
|
h ^= x;
|
1993-11-05 06:20:10 -04:00
|
|
|
if (h == -1) h = -2;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2000-06-23 11:18:11 -03: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-15 16:40:19 -04:00
|
|
|
if (f->func_dict) {
|
|
|
|
err = visit(f->func_dict, arg);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
2000-06-23 11:18:11 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject PyFunction_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
|
|
|
"function",
|
2000-06-30 02:02:53 -03:00
|
|
|
sizeof(PyFunctionObject) + PyGC_HEAD_SIZE,
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
1994-08-30 05:27:36 -03:00
|
|
|
(destructor)func_dealloc, /*tp_dealloc*/
|
1990-11-18 13:44:06 -04:00
|
|
|
0, /*tp_print*/
|
2001-01-15 16:40:19 -04:00
|
|
|
0, /*tp_getattr*/
|
|
|
|
0, /*tp_setattr*/
|
1994-08-30 05:27:36 -03:00
|
|
|
(cmpfunc)func_compare, /*tp_compare*/
|
|
|
|
(reprfunc)func_repr, /*tp_repr*/
|
1993-11-05 06:20:10 -04:00
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
1994-08-30 05:27:36 -03:00
|
|
|
(hashfunc)func_hash, /*tp_hash*/
|
2000-06-23 11:18:11 -03:00
|
|
|
0, /*tp_call*/
|
|
|
|
0, /*tp_str*/
|
2001-01-15 16:40:19 -04:00
|
|
|
(getattrofunc)func_getattro, /*tp_getattro*/
|
|
|
|
(setattrofunc)func_setattro, /*tp_setattro*/
|
2000-06-23 11:18:11 -03:00
|
|
|
0, /* tp_as_buffer */
|
2000-06-23 16:37:02 -03:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
|
2000-06-23 11:18:11 -03:00
|
|
|
0, /* tp_doc */
|
|
|
|
(traverseproc)func_traverse, /* tp_traverse */
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|