blob: 32d9a61f2819a36f6b774e87f84f8ef775f997f0 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009******************************************************************/
10
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011/* Function object implementation */
12
Guido van Rossumc0b618a1997-05-02 03:12:38 +000013#include "Python.h"
Guido van Rossum9bfef441993-03-29 10:43:31 +000014#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000016
Guido van Rossumc0b618a1997-05-02 03:12:38 +000017PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000018PyFunction_New(PyObject *code, PyObject *globals)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000019{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000020 PyFunctionObject *op = PyObject_NEW(PyFunctionObject,
21 &PyFunction_Type);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000022 if (op != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000023 PyObject *doc;
24 PyObject *consts;
25 Py_INCREF(code);
Guido van Rossum846e4311990-11-18 17:44:06 +000026 op->func_code = code;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000027 Py_INCREF(globals);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028 op->func_globals = globals;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000029 op->func_name = ((PyCodeObject *)code)->co_name;
30 Py_INCREF(op->func_name);
Guido van Rossum2271bf71995-07-18 14:30:34 +000031 op->func_defaults = NULL; /* No default arguments */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000032 consts = ((PyCodeObject *)code)->co_consts;
33 if (PyTuple_Size(consts) >= 1) {
34 doc = PyTuple_GetItem(consts, 0);
Guido van Rossumec5b7762000-04-27 20:14:13 +000035 if (!PyString_Check(doc) && !PyUnicode_Check(doc))
Guido van Rossumc0b618a1997-05-02 03:12:38 +000036 doc = Py_None;
Guido van Rossum5bd38051995-01-07 12:01:30 +000037 }
38 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000039 doc = Py_None;
40 Py_INCREF(doc);
Guido van Rossum5bd38051995-01-07 12:01:30 +000041 op->func_doc = doc;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000043 PyObject_GC_Init(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000044 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045}
46
Guido van Rossumc0b618a1997-05-02 03:12:38 +000047PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000048PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000049{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000050 if (!PyFunction_Check(op)) {
51 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052 return NULL;
53 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000054 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055}
56
Guido van Rossumc0b618a1997-05-02 03:12:38 +000057PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000058PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000060 if (!PyFunction_Check(op)) {
61 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062 return NULL;
63 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000064 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065}
66
Guido van Rossumc0b618a1997-05-02 03:12:38 +000067PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000068PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000069{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000070 if (!PyFunction_Check(op)) {
71 PyErr_BadInternalCall();
Guido van Rossum1d5735e1994-08-30 08:27:36 +000072 return NULL;
73 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000074 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +000075}
76
77int
Fred Drakeee238b92000-07-09 06:03:25 +000078PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000079{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000080 if (!PyFunction_Check(op)) {
81 PyErr_BadInternalCall();
Guido van Rossum1d5735e1994-08-30 08:27:36 +000082 return -1;
83 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000084 if (defaults == Py_None)
Guido van Rossum2271bf71995-07-18 14:30:34 +000085 defaults = NULL;
Guido van Rossum1109fbc1998-04-10 22:16:39 +000086 else if (PyTuple_Check(defaults)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000087 Py_XINCREF(defaults);
Guido van Rossum1109fbc1998-04-10 22:16:39 +000088 }
Guido van Rossum1d5735e1994-08-30 08:27:36 +000089 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000090 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
Guido van Rossum1d5735e1994-08-30 08:27:36 +000091 return -1;
92 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000093 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
94 ((PyFunctionObject *) op) -> func_defaults = defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +000095 return 0;
96}
97
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098/* Methods */
99
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000100#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000101
102static struct memberlist func_memberlist[] = {
Guido van Rossum0dabace1998-05-22 00:55:34 +0000103 {"func_code", T_OBJECT, OFF(func_code)},
Guido van Rossuma38c0ff1992-01-14 18:32:20 +0000104 {"func_globals",T_OBJECT, OFF(func_globals), READONLY},
Guido van Rossum25831651993-05-19 14:50:45 +0000105 {"func_name", T_OBJECT, OFF(func_name), READONLY},
Guido van Rossum10393b11995-01-10 10:39:49 +0000106 {"__name__", T_OBJECT, OFF(func_name), READONLY},
Guido van Rossum0dabace1998-05-22 00:55:34 +0000107 {"func_defaults",T_OBJECT, OFF(func_defaults)},
Guido van Rossum5bd38051995-01-07 12:01:30 +0000108 {"func_doc", T_OBJECT, OFF(func_doc)},
109 {"__doc__", T_OBJECT, OFF(func_doc)},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000110 {NULL} /* Sentinel */
111};
112
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000113static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000114func_getattr(PyFunctionObject *op, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000115{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000116 if (name[0] != '_' && PyEval_GetRestricted()) {
117 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossum10393b11995-01-10 10:39:49 +0000118 "function attributes not accessible in restricted mode");
119 return NULL;
120 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000121 return PyMember_Get((char *)op, func_memberlist, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000122}
123
Guido van Rossum0dabace1998-05-22 00:55:34 +0000124static int
Fred Drakeee238b92000-07-09 06:03:25 +0000125func_setattr(PyFunctionObject *op, char *name, PyObject *value)
Guido van Rossum0dabace1998-05-22 00:55:34 +0000126{
127 if (PyEval_GetRestricted()) {
128 PyErr_SetString(PyExc_RuntimeError,
129 "function attributes not settable in restricted mode");
130 return -1;
131 }
132 if (strcmp(name, "func_code") == 0) {
133 if (value == NULL || !PyCode_Check(value)) {
134 PyErr_SetString(
135 PyExc_TypeError,
136 "func_code must be set to a code object");
137 return -1;
138 }
139 }
140 else if (strcmp(name, "func_defaults") == 0) {
141 if (value != Py_None && !PyTuple_Check(value)) {
142 PyErr_SetString(
143 PyExc_TypeError,
144 "func_defaults must be set to a tuple object");
145 return -1;
146 }
147 if (value == Py_None)
148 value = NULL;
149 }
150 return PyMember_Set((char *)op, func_memberlist, name, value);
151}
152
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153static void
Fred Drakeee238b92000-07-09 06:03:25 +0000154func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000156 PyObject_GC_Fini(op);
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);
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000162 op = (PyFunctionObject *) PyObject_AS_GC(op);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000163 PyObject_DEL(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164}
165
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000166static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000167func_repr(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)
Fred Drakea44d3532000-06-30 15:01:00 +0000171 sprintf(buf, "<anonymous function at %p>", op);
Guido van Rossum590baa41993-11-30 13:40:46 +0000172 else
Fred Drakea44d3532000-06-30 15:01:00 +0000173 sprintf(buf, "<function %.100s at %p>",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000174 PyString_AsString(op->func_name),
Fred Drakea44d3532000-06-30 15:01:00 +0000175 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
Fred Drakeee238b92000-07-09 06:03:25 +0000180func_compare(PyFunctionObject *f, PyFunctionObject *g)
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000181{
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000182 int c;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000183 if (f->func_globals != g->func_globals)
184 return (f->func_globals < g->func_globals) ? -1 : 1;
Guido van Rossum83f9ad81997-08-05 16:51:05 +0000185 if (f->func_defaults != g->func_defaults) {
186 if (f->func_defaults == NULL)
187 return -1;
188 if (g->func_defaults == NULL)
189 return 1;
190 c = PyObject_Compare(f->func_defaults, g->func_defaults);
191 if (c != 0)
192 return c;
193 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000194 return PyObject_Compare(f->func_code, g->func_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000195}
196
197static long
Fred Drakeee238b92000-07-09 06:03:25 +0000198func_hash(PyFunctionObject *f)
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000199{
Fred Drake13634cf2000-06-29 19:17:04 +0000200 long h,x;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000201 h = PyObject_Hash(f->func_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000202 if (h == -1) return h;
Fred Drake13634cf2000-06-29 19:17:04 +0000203 x = _Py_HashPointer(f->func_globals);
204 if (x == -1) return x;
205 h ^= x;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000206 if (h == -1) h = -2;
207 return h;
208}
209
Jeremy Hylton8caad492000-06-23 14:18:11 +0000210static int
211func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
212{
213 int err;
214 if (f->func_code) {
215 err = visit(f->func_code, arg);
216 if (err)
217 return err;
218 }
219 if (f->func_globals) {
220 err = visit(f->func_globals, arg);
221 if (err)
222 return err;
223 }
224 if (f->func_defaults) {
225 err = visit(f->func_defaults, arg);
226 if (err)
227 return err;
228 }
229 if (f->func_doc) {
230 err = visit(f->func_doc, arg);
231 if (err)
232 return err;
233 }
234 if (f->func_name) {
235 err = visit(f->func_name, arg);
236 if (err)
237 return err;
238 }
239 return 0;
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",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000246 sizeof(PyFunctionObject) + PyGC_HEAD_SIZE,
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*/
Jeremy Hylton8caad492000-06-23 14:18:11 +0000258 0, /*tp_call*/
259 0, /*tp_str*/
260 0, /*tp_getattro*/
261 0, /*tp_setattro*/
262 0, /* tp_as_buffer */
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000263 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Jeremy Hylton8caad492000-06-23 14:18:11 +0000264 0, /* tp_doc */
265 (traverseproc)func_traverse, /* tp_traverse */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266};