Don't reference removed files in Makefile
[python/dscho.git] / Objects / funcobject.c
blobdcceb72c5d44975745e4e7bd460f1adda1a198f3
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* Function object implementation */
27 #include "allobjects.h"
28 #include "compile.h"
29 #include "structmember.h"
31 object *
32 newfuncobject(code, globals)
33 object *code;
34 object *globals;
36 funcobject *op = NEWOBJ(funcobject, &Functype);
37 if (op != NULL) {
38 object *doc;
39 object *consts;
40 INCREF(code);
41 op->func_code = code;
42 INCREF(globals);
43 op->func_globals = globals;
44 op->func_name = ((codeobject *)code)->co_name;
45 INCREF(op->func_name);
46 op->func_argcount = -1; /* Unknown */
47 op->func_argdefs = NULL; /* No default arguments */
48 consts = ((codeobject *)code)->co_consts;
49 if (gettuplesize(consts) >= 1) {
50 doc = gettupleitem(consts, 0);
51 if (!is_stringobject(doc))
52 doc = None;
54 else
55 doc = None;
56 INCREF(doc);
57 op->func_doc = doc;
59 return (object *)op;
62 object *
63 getfunccode(op)
64 object *op;
66 if (!is_funcobject(op)) {
67 err_badcall();
68 return NULL;
70 return ((funcobject *) op) -> func_code;
73 object *
74 getfuncglobals(op)
75 object *op;
77 if (!is_funcobject(op)) {
78 err_badcall();
79 return NULL;
81 return ((funcobject *) op) -> func_globals;
84 object *
85 getfuncargstuff(op, argcount_return)
86 object *op;
87 int *argcount_return;
89 if (!is_funcobject(op)) {
90 err_badcall();
91 return NULL;
93 *argcount_return = ((funcobject *) op) -> func_argcount;
94 return ((funcobject *) op) -> func_argdefs;
97 int
98 setfuncargstuff(op, argcount, argdefs)
99 object *op;
100 int argcount;
101 object *argdefs;
103 if (!is_funcobject(op) ||
104 argdefs != NULL && !is_tupleobject(argdefs)) {
105 err_badcall();
106 return -1;
108 if (argdefs == None)
109 argdefs = NULL;
110 else if (is_tupleobject(argdefs))
111 XINCREF(argdefs);
112 else {
113 err_setstr(SystemError, "non-tuple default args");
114 return -1;
116 ((funcobject *) op) -> func_argcount = argcount;
117 XDECREF(((funcobject *) op) -> func_argdefs);
118 ((funcobject *) op) -> func_argdefs = argdefs;
119 return 0;
122 /* Methods */
124 #define OFF(x) offsetof(funcobject, x)
126 static struct memberlist func_memberlist[] = {
127 {"func_code", T_OBJECT, OFF(func_code), READONLY},
128 {"func_globals",T_OBJECT, OFF(func_globals), READONLY},
129 {"func_name", T_OBJECT, OFF(func_name), READONLY},
130 {"__name__", T_OBJECT, OFF(func_name), READONLY},
131 {"func_argcount",T_INT, OFF(func_argcount), READONLY},
132 {"func_argdefs",T_OBJECT, OFF(func_argdefs), READONLY},
133 {"func_doc", T_OBJECT, OFF(func_doc)},
134 {"__doc__", T_OBJECT, OFF(func_doc)},
135 {NULL} /* Sentinel */
138 static object *
139 func_getattr(op, name)
140 funcobject *op;
141 char *name;
143 if (name[0] != '_' && getrestricted()) {
144 err_setstr(RuntimeError,
145 "function attributes not accessible in restricted mode");
146 return NULL;
148 return getmember((char *)op, func_memberlist, name);
151 static void
152 func_dealloc(op)
153 funcobject *op;
155 DECREF(op->func_code);
156 DECREF(op->func_globals);
157 DECREF(op->func_name);
158 XDECREF(op->func_argdefs);
159 XDECREF(op->func_doc);
160 DEL(op);
163 static object*
164 func_repr(op)
165 funcobject *op;
167 char buf[140];
168 if (op->func_name == None)
169 sprintf(buf, "<anonymous function at %lx>", (long)op);
170 else
171 sprintf(buf, "<function %.100s at %lx>",
172 getstringvalue(op->func_name),
173 (long)op);
174 return newstringobject(buf);
177 static int
178 func_compare(f, g)
179 funcobject *f, *g;
181 int c;
182 if (f->func_globals != g->func_globals)
183 return (f->func_globals < g->func_globals) ? -1 : 1;
184 c = f->func_argcount < g->func_argcount;
185 if (c != 0)
186 return c < 0 ? -1 : 1;
187 c = cmpobject(f->func_argdefs, g->func_argdefs);
188 if (c != 0)
189 return c;
190 return cmpobject(f->func_code, g->func_code);
193 static long
194 func_hash(f)
195 funcobject *f;
197 long h;
198 h = hashobject(f->func_code);
199 if (h == -1) return h;
200 h = h ^ (long)f->func_globals;
201 if (h == -1) h = -2;
202 return h;
205 typeobject Functype = {
206 OB_HEAD_INIT(&Typetype)
208 "function",
209 sizeof(funcobject),
211 (destructor)func_dealloc, /*tp_dealloc*/
212 0, /*tp_print*/
213 (getattrfunc)func_getattr, /*tp_getattr*/
214 0, /*tp_setattr*/
215 (cmpfunc)func_compare, /*tp_compare*/
216 (reprfunc)func_repr, /*tp_repr*/
217 0, /*tp_as_number*/
218 0, /*tp_as_sequence*/
219 0, /*tp_as_mapping*/
220 (hashfunc)func_hash, /*tp_hash*/