blob: d62dd08c190b14d1cbff2f79fc28277883c989e2 [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 Rossuma38c0ff1992-01-14 18:32:20 +0000130 {"func_code", T_OBJECT, OFF(func_code), READONLY},
131 {"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 Rossum2271bf71995-07-18 14:30:34 +0000134 {"func_defaults",T_OBJECT, OFF(func_defaults), READONLY},
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 Rossum85a5fbb1990-10-14 12:07:46 +0000153static void
Guido van Rossum3f5da241990-12-20 15:06:42 +0000154func_dealloc(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000155 PyFunctionObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157 Py_DECREF(op->func_code);
158 Py_DECREF(op->func_globals);
159 Py_DECREF(op->func_name);
160 Py_XDECREF(op->func_defaults);
161 Py_XDECREF(op->func_doc);
162 PyMem_DEL(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000163}
164
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000165static PyObject*
Guido van Rossum9bfef441993-03-29 10:43:31 +0000166func_repr(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000167 PyFunctionObject *op;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000168{
169 char buf[140];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000170 if (op->func_name == Py_None)
Guido van Rossum590baa41993-11-30 13:40:46 +0000171 sprintf(buf, "<anonymous function at %lx>", (long)op);
172 else
173 sprintf(buf, "<function %.100s at %lx>",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000174 PyString_AsString(op->func_name),
Guido van Rossum590baa41993-11-30 13:40:46 +0000175 (long)op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000176 return PyString_FromString(buf);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000177}
178
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000179static int
180func_compare(f, g)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000181 PyFunctionObject *f, *g;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000182{
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000183 int c;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000184 if (f->func_globals != g->func_globals)
185 return (f->func_globals < g->func_globals) ? -1 : 1;
Guido van Rossum83f9ad81997-08-05 16:51:05 +0000186 if (f->func_defaults != g->func_defaults) {
187 if (f->func_defaults == NULL)
188 return -1;
189 if (g->func_defaults == NULL)
190 return 1;
191 c = PyObject_Compare(f->func_defaults, g->func_defaults);
192 if (c != 0)
193 return c;
194 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000195 return PyObject_Compare(f->func_code, g->func_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000196}
197
198static long
199func_hash(f)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000200 PyFunctionObject *f;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000201{
202 long h;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000203 h = PyObject_Hash(f->func_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000204 if (h == -1) return h;
205 h = h ^ (long)f->func_globals;
206 if (h == -1) h = -2;
207 return h;
208}
209
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000210PyTypeObject PyFunction_Type = {
211 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000212 0,
213 "function",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214 sizeof(PyFunctionObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000215 0,
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000216 (destructor)func_dealloc, /*tp_dealloc*/
Guido van Rossum846e4311990-11-18 17:44:06 +0000217 0, /*tp_print*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000218 (getattrfunc)func_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000219 0, /*tp_setattr*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000220 (cmpfunc)func_compare, /*tp_compare*/
221 (reprfunc)func_repr, /*tp_repr*/
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000222 0, /*tp_as_number*/
223 0, /*tp_as_sequence*/
224 0, /*tp_as_mapping*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000225 (hashfunc)func_hash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226};