This commit was manufactured by cvs2svn to create tag 'release101'.
[python/dscho.git] / Objects / funcobject.c
blob0a18345dae2a91b7f3d969d626750dda1a79381b
1 /***********************************************************
2 Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
3 Amsterdam, 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 INCREF(code);
39 op->func_code = code;
40 INCREF(globals);
41 op->func_globals = globals;
42 op->func_name = ((codeobject*)(op->func_code))->co_name;
43 INCREF(op->func_name);
45 return (object *)op;
48 object *
49 getfunccode(op)
50 object *op;
52 if (!is_funcobject(op)) {
53 err_badcall();
54 return NULL;
56 return ((funcobject *) op) -> func_code;
59 object *
60 getfuncglobals(op)
61 object *op;
63 if (!is_funcobject(op)) {
64 err_badcall();
65 return NULL;
67 return ((funcobject *) op) -> func_globals;
70 /* Methods */
72 #define OFF(x) offsetof(funcobject, x)
74 static struct memberlist func_memberlist[] = {
75 {"func_code", T_OBJECT, OFF(func_code), READONLY},
76 {"func_globals",T_OBJECT, OFF(func_globals), READONLY},
77 {"func_name", T_OBJECT, OFF(func_name), READONLY},
78 {NULL} /* Sentinel */
81 static object *
82 func_getattr(op, name)
83 funcobject *op;
84 char *name;
86 return getmember((char *)op, func_memberlist, name);
89 static void
90 func_dealloc(op)
91 funcobject *op;
93 DECREF(op->func_code);
94 DECREF(op->func_globals);
95 DEL(op);
98 static object*
99 func_repr(op)
100 funcobject *op;
102 char buf[140];
103 if (op->func_name == None)
104 sprintf(buf, "<anonymous function at %lx>", (long)op);
105 else
106 sprintf(buf, "<function %.100s at %lx>",
107 getstringvalue(op->func_name),
108 (long)op);
109 return newstringobject(buf);
112 static int
113 func_compare(f, g)
114 funcobject *f, *g;
116 if (f->func_globals != g->func_globals)
117 return (f->func_globals < g->func_globals) ? -1 : 1;
118 return cmpobject(f->func_code, g->func_code);
121 static long
122 func_hash(f)
123 funcobject *f;
125 long h;
126 h = hashobject(f->func_code);
127 if (h == -1) return h;
128 h = h ^ (long)f->func_globals;
129 if (h == -1) h = -2;
130 return h;
133 typeobject Functype = {
134 OB_HEAD_INIT(&Typetype)
136 "function",
137 sizeof(funcobject),
139 (destructor)func_dealloc, /*tp_dealloc*/
140 0, /*tp_print*/
141 (getattrfunc)func_getattr, /*tp_getattr*/
142 0, /*tp_setattr*/
143 (cmpfunc)func_compare, /*tp_compare*/
144 (reprfunc)func_repr, /*tp_repr*/
145 0, /*tp_as_number*/
146 0, /*tp_as_sequence*/
147 0, /*tp_as_mapping*/
148 (hashfunc)func_hash, /*tp_hash*/