Bump version to 0.9.1.
[python/dscho.git] / Objects / funcobject.c
blob32d9a61f2819a36f6b774e87f84f8ef775f997f0
1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5 All rights reserved.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
11 /* Function object implementation */
13 #include "Python.h"
14 #include "compile.h"
15 #include "structmember.h"
17 PyObject *
18 PyFunction_New(PyObject *code, PyObject *globals)
20 PyFunctionObject *op = PyObject_NEW(PyFunctionObject,
21 &PyFunction_Type);
22 if (op != NULL) {
23 PyObject *doc;
24 PyObject *consts;
25 Py_INCREF(code);
26 op->func_code = code;
27 Py_INCREF(globals);
28 op->func_globals = globals;
29 op->func_name = ((PyCodeObject *)code)->co_name;
30 Py_INCREF(op->func_name);
31 op->func_defaults = NULL; /* No default arguments */
32 consts = ((PyCodeObject *)code)->co_consts;
33 if (PyTuple_Size(consts) >= 1) {
34 doc = PyTuple_GetItem(consts, 0);
35 if (!PyString_Check(doc) && !PyUnicode_Check(doc))
36 doc = Py_None;
38 else
39 doc = Py_None;
40 Py_INCREF(doc);
41 op->func_doc = doc;
43 PyObject_GC_Init(op);
44 return (PyObject *)op;
47 PyObject *
48 PyFunction_GetCode(PyObject *op)
50 if (!PyFunction_Check(op)) {
51 PyErr_BadInternalCall();
52 return NULL;
54 return ((PyFunctionObject *) op) -> func_code;
57 PyObject *
58 PyFunction_GetGlobals(PyObject *op)
60 if (!PyFunction_Check(op)) {
61 PyErr_BadInternalCall();
62 return NULL;
64 return ((PyFunctionObject *) op) -> func_globals;
67 PyObject *
68 PyFunction_GetDefaults(PyObject *op)
70 if (!PyFunction_Check(op)) {
71 PyErr_BadInternalCall();
72 return NULL;
74 return ((PyFunctionObject *) op) -> func_defaults;
77 int
78 PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
80 if (!PyFunction_Check(op)) {
81 PyErr_BadInternalCall();
82 return -1;
84 if (defaults == Py_None)
85 defaults = NULL;
86 else if (PyTuple_Check(defaults)) {
87 Py_XINCREF(defaults);
89 else {
90 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
91 return -1;
93 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
94 ((PyFunctionObject *) op) -> func_defaults = defaults;
95 return 0;
98 /* Methods */
100 #define OFF(x) offsetof(PyFunctionObject, x)
102 static struct memberlist func_memberlist[] = {
103 {"func_code", T_OBJECT, OFF(func_code)},
104 {"func_globals",T_OBJECT, OFF(func_globals), READONLY},
105 {"func_name", T_OBJECT, OFF(func_name), READONLY},
106 {"__name__", T_OBJECT, OFF(func_name), READONLY},
107 {"func_defaults",T_OBJECT, OFF(func_defaults)},
108 {"func_doc", T_OBJECT, OFF(func_doc)},
109 {"__doc__", T_OBJECT, OFF(func_doc)},
110 {NULL} /* Sentinel */
113 static PyObject *
114 func_getattr(PyFunctionObject *op, char *name)
116 if (name[0] != '_' && PyEval_GetRestricted()) {
117 PyErr_SetString(PyExc_RuntimeError,
118 "function attributes not accessible in restricted mode");
119 return NULL;
121 return PyMember_Get((char *)op, func_memberlist, name);
124 static int
125 func_setattr(PyFunctionObject *op, char *name, PyObject *value)
127 if (PyEval_GetRestricted()) {
128 PyErr_SetString(PyExc_RuntimeError,
129 "function attributes not settable in restricted mode");
130 return -1;
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;
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;
147 if (value == Py_None)
148 value = NULL;
150 return PyMember_Set((char *)op, func_memberlist, name, value);
153 static void
154 func_dealloc(PyFunctionObject *op)
156 PyObject_GC_Fini(op);
157 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 op = (PyFunctionObject *) PyObject_AS_GC(op);
163 PyObject_DEL(op);
166 static PyObject*
167 func_repr(PyFunctionObject *op)
169 char buf[140];
170 if (op->func_name == Py_None)
171 sprintf(buf, "<anonymous function at %p>", op);
172 else
173 sprintf(buf, "<function %.100s at %p>",
174 PyString_AsString(op->func_name),
175 op);
176 return PyString_FromString(buf);
179 static int
180 func_compare(PyFunctionObject *f, PyFunctionObject *g)
182 int c;
183 if (f->func_globals != g->func_globals)
184 return (f->func_globals < g->func_globals) ? -1 : 1;
185 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;
194 return PyObject_Compare(f->func_code, g->func_code);
197 static long
198 func_hash(PyFunctionObject *f)
200 long h,x;
201 h = PyObject_Hash(f->func_code);
202 if (h == -1) return h;
203 x = _Py_HashPointer(f->func_globals);
204 if (x == -1) return x;
205 h ^= x;
206 if (h == -1) h = -2;
207 return h;
210 static int
211 func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
213 int err;
214 if (f->func_code) {
215 err = visit(f->func_code, arg);
216 if (err)
217 return err;
219 if (f->func_globals) {
220 err = visit(f->func_globals, arg);
221 if (err)
222 return err;
224 if (f->func_defaults) {
225 err = visit(f->func_defaults, arg);
226 if (err)
227 return err;
229 if (f->func_doc) {
230 err = visit(f->func_doc, arg);
231 if (err)
232 return err;
234 if (f->func_name) {
235 err = visit(f->func_name, arg);
236 if (err)
237 return err;
239 return 0;
242 PyTypeObject PyFunction_Type = {
243 PyObject_HEAD_INIT(&PyType_Type)
245 "function",
246 sizeof(PyFunctionObject) + PyGC_HEAD_SIZE,
248 (destructor)func_dealloc, /*tp_dealloc*/
249 0, /*tp_print*/
250 (getattrfunc)func_getattr, /*tp_getattr*/
251 (setattrfunc)func_setattr, /*tp_setattr*/
252 (cmpfunc)func_compare, /*tp_compare*/
253 (reprfunc)func_repr, /*tp_repr*/
254 0, /*tp_as_number*/
255 0, /*tp_as_sequence*/
256 0, /*tp_as_mapping*/
257 (hashfunc)func_hash, /*tp_hash*/
258 0, /*tp_call*/
259 0, /*tp_str*/
260 0, /*tp_getattro*/
261 0, /*tp_setattro*/
262 0, /* tp_as_buffer */
263 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
264 0, /* tp_doc */
265 (traverseproc)func_traverse, /* tp_traverse */