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 or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Function object implementation */
36 #include "structmember.h"
39 PyFunction_New(code
, globals
)
43 PyFunctionObject
*op
= PyObject_NEW(PyFunctionObject
,
51 op
->func_globals
= globals
;
52 op
->func_name
= ((PyCodeObject
*)code
)->co_name
;
53 Py_INCREF(op
->func_name
);
54 op
->func_defaults
= NULL
; /* No default arguments */
55 consts
= ((PyCodeObject
*)code
)->co_consts
;
56 if (PyTuple_Size(consts
) >= 1) {
57 doc
= PyTuple_GetItem(consts
, 0);
58 if (!PyString_Check(doc
))
66 return (PyObject
*)op
;
70 PyFunction_GetCode(op
)
73 if (!PyFunction_Check(op
)) {
74 PyErr_BadInternalCall();
77 return ((PyFunctionObject
*) op
) -> func_code
;
81 PyFunction_GetGlobals(op
)
84 if (!PyFunction_Check(op
)) {
85 PyErr_BadInternalCall();
88 return ((PyFunctionObject
*) op
) -> func_globals
;
92 PyFunction_GetDefaults(op
)
95 if (!PyFunction_Check(op
)) {
96 PyErr_BadInternalCall();
99 return ((PyFunctionObject
*) op
) -> func_defaults
;
103 PyFunction_SetDefaults(op
, defaults
)
107 if (!PyFunction_Check(op
)) {
108 PyErr_BadInternalCall();
111 if (defaults
== Py_None
)
113 else if (PyTuple_Check(defaults
)) {
114 Py_XINCREF(defaults
);
117 PyErr_SetString(PyExc_SystemError
, "non-tuple default args");
120 Py_XDECREF(((PyFunctionObject
*) op
) -> func_defaults
);
121 ((PyFunctionObject
*) op
) -> func_defaults
= defaults
;
127 #define OFF(x) offsetof(PyFunctionObject, x)
129 static struct memberlist func_memberlist
[] = {
130 {"func_code", T_OBJECT
, OFF(func_code
)},
131 {"func_globals",T_OBJECT
, OFF(func_globals
), READONLY
},
132 {"func_name", T_OBJECT
, OFF(func_name
), READONLY
},
133 {"__name__", T_OBJECT
, OFF(func_name
), READONLY
},
134 {"func_defaults",T_OBJECT
, OFF(func_defaults
)},
135 {"func_doc", T_OBJECT
, OFF(func_doc
)},
136 {"__doc__", T_OBJECT
, OFF(func_doc
)},
137 {NULL
} /* Sentinel */
141 func_getattr(op
, name
)
142 PyFunctionObject
*op
;
145 if (name
[0] != '_' && PyEval_GetRestricted()) {
146 PyErr_SetString(PyExc_RuntimeError
,
147 "function attributes not accessible in restricted mode");
150 return PyMember_Get((char *)op
, func_memberlist
, name
);
154 func_setattr(op
, name
, value
)
155 PyFunctionObject
*op
;
159 if (PyEval_GetRestricted()) {
160 PyErr_SetString(PyExc_RuntimeError
,
161 "function attributes not settable in restricted mode");
164 if (strcmp(name
, "func_code") == 0) {
165 if (value
== NULL
|| !PyCode_Check(value
)) {
168 "func_code must be set to a code object");
172 else if (strcmp(name
, "func_defaults") == 0) {
173 if (value
!= Py_None
&& !PyTuple_Check(value
)) {
176 "func_defaults must be set to a tuple object");
179 if (value
== Py_None
)
182 return PyMember_Set((char *)op
, func_memberlist
, name
, value
);
187 PyFunctionObject
*op
;
189 Py_DECREF(op
->func_code
);
190 Py_DECREF(op
->func_globals
);
191 Py_DECREF(op
->func_name
);
192 Py_XDECREF(op
->func_defaults
);
193 Py_XDECREF(op
->func_doc
);
199 PyFunctionObject
*op
;
202 if (op
->func_name
== Py_None
)
203 sprintf(buf
, "<anonymous function at %lx>", (long)op
);
205 sprintf(buf
, "<function %.100s at %lx>",
206 PyString_AsString(op
->func_name
),
208 return PyString_FromString(buf
);
213 PyFunctionObject
*f
, *g
;
216 if (f
->func_globals
!= g
->func_globals
)
217 return (f
->func_globals
< g
->func_globals
) ? -1 : 1;
218 if (f
->func_defaults
!= g
->func_defaults
) {
219 if (f
->func_defaults
== NULL
)
221 if (g
->func_defaults
== NULL
)
223 c
= PyObject_Compare(f
->func_defaults
, g
->func_defaults
);
227 return PyObject_Compare(f
->func_code
, g
->func_code
);
235 h
= PyObject_Hash(f
->func_code
);
236 if (h
== -1) return h
;
237 h
= h
^ (long)f
->func_globals
;
242 PyTypeObject PyFunction_Type
= {
243 PyObject_HEAD_INIT(&PyType_Type
)
246 sizeof(PyFunctionObject
),
248 (destructor
)func_dealloc
, /*tp_dealloc*/
250 (getattrfunc
)func_getattr
, /*tp_getattr*/
251 (setattrfunc
)func_setattr
, /*tp_setattr*/
252 (cmpfunc
)func_compare
, /*tp_compare*/
253 (reprfunc
)func_repr
, /*tp_repr*/
255 0, /*tp_as_sequence*/
257 (hashfunc
)func_hash
, /*tp_hash*/