1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
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"
29 #include "structmember.h"
32 newfuncobject(code
, globals
)
36 funcobject
*op
= NEWOBJ(funcobject
, &Functype
);
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
))
66 if (!is_funcobject(op
)) {
70 return ((funcobject
*) op
) -> func_code
;
77 if (!is_funcobject(op
)) {
81 return ((funcobject
*) op
) -> func_globals
;
85 getfuncargstuff(op
, argcount_return
)
89 if (!is_funcobject(op
)) {
93 *argcount_return
= ((funcobject
*) op
) -> func_argcount
;
94 return ((funcobject
*) op
) -> func_argdefs
;
98 setfuncargstuff(op
, argcount
, argdefs
)
103 if (!is_funcobject(op
) ||
104 argdefs
!= NULL
&& !is_tupleobject(argdefs
)) {
110 else if (is_tupleobject(argdefs
))
113 err_setstr(SystemError
, "non-tuple default args");
116 ((funcobject
*) op
) -> func_argcount
= argcount
;
117 XDECREF(((funcobject
*) op
) -> func_argdefs
);
118 ((funcobject
*) op
) -> func_argdefs
= argdefs
;
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 */
139 func_getattr(op
, name
)
143 if (name
[0] != '_' && getrestricted()) {
144 err_setstr(RuntimeError
,
145 "function attributes not accessible in restricted mode");
148 return getmember((char *)op
, func_memberlist
, name
);
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
);
168 if (op
->func_name
== None
)
169 sprintf(buf
, "<anonymous function at %lx>", (long)op
);
171 sprintf(buf
, "<function %.100s at %lx>",
172 getstringvalue(op
->func_name
),
174 return newstringobject(buf
);
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
;
186 return c
< 0 ? -1 : 1;
187 c
= cmpobject(f
->func_argdefs
, g
->func_argdefs
);
190 return cmpobject(f
->func_code
, g
->func_code
);
198 h
= hashobject(f
->func_code
);
199 if (h
== -1) return h
;
200 h
= h
^ (long)f
->func_globals
;
205 typeobject Functype
= {
206 OB_HEAD_INIT(&Typetype
)
211 (destructor
)func_dealloc
, /*tp_dealloc*/
213 (getattrfunc
)func_getattr
, /*tp_getattr*/
215 (cmpfunc
)func_compare
, /*tp_compare*/
216 (reprfunc
)func_repr
, /*tp_repr*/
218 0, /*tp_as_sequence*/
220 (hashfunc
)func_hash
, /*tp_hash*/