blob: c9dd1398a2bae88fe4c93e913baf1a6197143461 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6610ad91995-01-04 19:07:38 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Function object implementation */
33
Guido van Rossumc0b618a1997-05-02 03:12:38 +000034#include "Python.h"
Guido van Rossum9bfef441993-03-29 10:43:31 +000035#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000036#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037
Guido van Rossumc0b618a1997-05-02 03:12:38 +000038PyObject *
39PyFunction_New(code, globals)
40 PyObject *code;
41 PyObject *globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000043 PyFunctionObject *op = PyObject_NEW(PyFunctionObject,
44 &PyFunction_Type);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045 if (op != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000046 PyObject *doc;
47 PyObject *consts;
48 Py_INCREF(code);
Guido van Rossum846e4311990-11-18 17:44:06 +000049 op->func_code = code;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000050 Py_INCREF(globals);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051 op->func_globals = globals;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000052 op->func_name = ((PyCodeObject *)code)->co_name;
53 Py_INCREF(op->func_name);
Guido van Rossum2271bf71995-07-18 14:30:34 +000054 op->func_defaults = NULL; /* No default arguments */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000055 consts = ((PyCodeObject *)code)->co_consts;
56 if (PyTuple_Size(consts) >= 1) {
57 doc = PyTuple_GetItem(consts, 0);
58 if (!PyString_Check(doc))
59 doc = Py_None;
Guido van Rossum5bd38051995-01-07 12:01:30 +000060 }
61 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000062 doc = Py_None;
63 Py_INCREF(doc);
Guido van Rossum5bd38051995-01-07 12:01:30 +000064 op->func_doc = doc;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000066 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067}
68
Guido van Rossumc0b618a1997-05-02 03:12:38 +000069PyObject *
70PyFunction_GetCode(op)
71 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000073 if (!PyFunction_Check(op)) {
74 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075 return NULL;
76 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000077 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078}
79
Guido van Rossumc0b618a1997-05-02 03:12:38 +000080PyObject *
81PyFunction_GetGlobals(op)
82 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000084 if (!PyFunction_Check(op)) {
85 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086 return NULL;
87 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000088 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089}
90
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091PyObject *
Guido van Rossum2271bf71995-07-18 14:30:34 +000092PyFunction_GetDefaults(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +000093 PyObject *op;
Guido van Rossum1d5735e1994-08-30 08:27:36 +000094{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095 if (!PyFunction_Check(op)) {
96 PyErr_BadInternalCall();
Guido van Rossum1d5735e1994-08-30 08:27:36 +000097 return NULL;
98 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000099 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000100}
101
102int
Guido van Rossum2271bf71995-07-18 14:30:34 +0000103PyFunction_SetDefaults(op, defaults)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000104 PyObject *op;
105 PyObject *defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000106{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000107 if (!PyFunction_Check(op)) {
108 PyErr_BadInternalCall();
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000109 return -1;
110 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000111 if (defaults == Py_None)
Guido van Rossum2271bf71995-07-18 14:30:34 +0000112 defaults = NULL;
Guido van Rossum1109fbc1998-04-10 22:16:39 +0000113 else if (PyTuple_Check(defaults)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000114 Py_XINCREF(defaults);
Guido van Rossum1109fbc1998-04-10 22:16:39 +0000115 }
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000116 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000117 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000118 return -1;
119 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000120 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
121 ((PyFunctionObject *) op) -> func_defaults = defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000122 return 0;
123}
124
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125/* Methods */
126
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000127#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000128
129static struct memberlist func_memberlist[] = {
Guido van Rossum0dabace1998-05-22 00:55:34 +0000130 {"func_code", T_OBJECT, OFF(func_code)},
Guido van Rossuma38c0ff1992-01-14 18:32:20 +0000131 {"func_globals",T_OBJECT, OFF(func_globals), READONLY},
Guido van Rossum25831651993-05-19 14:50:45 +0000132 {"func_name", T_OBJECT, OFF(func_name), READONLY},
Guido van Rossum10393b11995-01-10 10:39:49 +0000133 {"__name__", T_OBJECT, OFF(func_name), READONLY},
Guido van Rossum0dabace1998-05-22 00:55:34 +0000134 {"func_defaults",T_OBJECT, OFF(func_defaults)},
Guido van Rossum5bd38051995-01-07 12:01:30 +0000135 {"func_doc", T_OBJECT, OFF(func_doc)},
136 {"__doc__", T_OBJECT, OFF(func_doc)},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000137 {NULL} /* Sentinel */
138};
139
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000140static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000141func_getattr(op, name)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000142 PyFunctionObject *op;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000143 char *name;
144{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000145 if (name[0] != '_' && PyEval_GetRestricted()) {
146 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossum10393b11995-01-10 10:39:49 +0000147 "function attributes not accessible in restricted mode");
148 return NULL;
149 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000150 return PyMember_Get((char *)op, func_memberlist, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000151}
152
Guido van Rossum0dabace1998-05-22 00:55:34 +0000153static int
154func_setattr(op, name, value)
155 PyFunctionObject *op;
156 char *name;
157 PyObject *value;
158{
159 if (PyEval_GetRestricted()) {
160 PyErr_SetString(PyExc_RuntimeError,
161 "function attributes not settable in restricted mode");
162 return -1;
163 }
164 if (strcmp(name, "func_code") == 0) {
165 if (value == NULL || !PyCode_Check(value)) {
166 PyErr_SetString(
167 PyExc_TypeError,
168 "func_code must be set to a code object");
169 return -1;
170 }
171 }
172 else if (strcmp(name, "func_defaults") == 0) {
173 if (value != Py_None && !PyTuple_Check(value)) {
174 PyErr_SetString(
175 PyExc_TypeError,
176 "func_defaults must be set to a tuple object");
177 return -1;
178 }
179 if (value == Py_None)
180 value = NULL;
181 }
182 return PyMember_Set((char *)op, func_memberlist, name, value);
183}
184
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000185static void
Guido van Rossum3f5da241990-12-20 15:06:42 +0000186func_dealloc(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000187 PyFunctionObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000188{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000189 Py_DECREF(op->func_code);
190 Py_DECREF(op->func_globals);
191 Py_DECREF(op->func_name);
192 Py_XDECREF(op->func_defaults);
193 Py_XDECREF(op->func_doc);
194 PyMem_DEL(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000195}
196
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000197static PyObject*
Guido van Rossum9bfef441993-03-29 10:43:31 +0000198func_repr(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000199 PyFunctionObject *op;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000200{
201 char buf[140];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000202 if (op->func_name == Py_None)
Guido van Rossum590baa41993-11-30 13:40:46 +0000203 sprintf(buf, "<anonymous function at %lx>", (long)op);
204 else
205 sprintf(buf, "<function %.100s at %lx>",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000206 PyString_AsString(op->func_name),
Guido van Rossum590baa41993-11-30 13:40:46 +0000207 (long)op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000208 return PyString_FromString(buf);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000209}
210
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000211static int
212func_compare(f, g)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000213 PyFunctionObject *f, *g;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000214{
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000215 int c;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000216 if (f->func_globals != g->func_globals)
217 return (f->func_globals < g->func_globals) ? -1 : 1;
Guido van Rossum83f9ad81997-08-05 16:51:05 +0000218 if (f->func_defaults != g->func_defaults) {
219 if (f->func_defaults == NULL)
220 return -1;
221 if (g->func_defaults == NULL)
222 return 1;
223 c = PyObject_Compare(f->func_defaults, g->func_defaults);
224 if (c != 0)
225 return c;
226 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000227 return PyObject_Compare(f->func_code, g->func_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000228}
229
230static long
231func_hash(f)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000232 PyFunctionObject *f;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000233{
234 long h;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000235 h = PyObject_Hash(f->func_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000236 if (h == -1) return h;
237 h = h ^ (long)f->func_globals;
238 if (h == -1) h = -2;
239 return h;
240}
241
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000242PyTypeObject PyFunction_Type = {
243 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244 0,
245 "function",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000246 sizeof(PyFunctionObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247 0,
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000248 (destructor)func_dealloc, /*tp_dealloc*/
Guido van Rossum846e4311990-11-18 17:44:06 +0000249 0, /*tp_print*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000250 (getattrfunc)func_getattr, /*tp_getattr*/
Guido van Rossum0dabace1998-05-22 00:55:34 +0000251 (setattrfunc)func_setattr, /*tp_setattr*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000252 (cmpfunc)func_compare, /*tp_compare*/
253 (reprfunc)func_repr, /*tp_repr*/
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000254 0, /*tp_as_number*/
255 0, /*tp_as_sequence*/
256 0, /*tp_as_mapping*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000257 (hashfunc)func_hash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258};