1 /***********************************************************
2 Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
3 Amsterdam, The Netherlands.
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
);
41 op
->func_globals
= globals
;
42 op
->func_name
= ((codeobject
*)(op
->func_code
))->co_name
;
43 INCREF(op
->func_name
);
52 if (!is_funcobject(op
)) {
56 return ((funcobject
*) op
) -> func_code
;
63 if (!is_funcobject(op
)) {
67 return ((funcobject
*) op
) -> func_globals
;
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
},
82 func_getattr(op
, name
)
86 return getmember((char *)op
, func_memberlist
, name
);
93 DECREF(op
->func_code
);
94 DECREF(op
->func_globals
);
103 if (op
->func_name
== None
)
104 sprintf(buf
, "<anonymous function at %lx>", (long)op
);
106 sprintf(buf
, "<function %.100s at %lx>",
107 getstringvalue(op
->func_name
),
109 return newstringobject(buf
);
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
);
126 h
= hashobject(f
->func_code
);
127 if (h
== -1) return h
;
128 h
= h
^ (long)f
->func_globals
;
133 typeobject Functype
= {
134 OB_HEAD_INIT(&Typetype
)
139 (destructor
)func_dealloc
, /*tp_dealloc*/
141 (getattrfunc
)func_getattr
, /*tp_getattr*/
143 (cmpfunc
)func_compare
, /*tp_compare*/
144 (reprfunc
)func_repr
, /*tp_repr*/
146 0, /*tp_as_sequence*/
148 (hashfunc
)func_hash
, /*tp_hash*/