2 /* Function object implementation */
6 #include "structmember.h"
9 PyFunction_New(PyObject
*code
, PyObject
*globals
)
11 PyFunctionObject
*op
= PyObject_NEW(PyFunctionObject
,
19 op
->func_globals
= globals
;
20 op
->func_name
= ((PyCodeObject
*)code
)->co_name
;
21 Py_INCREF(op
->func_name
);
22 op
->func_defaults
= NULL
; /* No default arguments */
23 op
->func_closure
= NULL
;
24 consts
= ((PyCodeObject
*)code
)->co_consts
;
25 if (PyTuple_Size(consts
) >= 1) {
26 doc
= PyTuple_GetItem(consts
, 0);
27 if (!PyString_Check(doc
) && !PyUnicode_Check(doc
))
39 return (PyObject
*)op
;
43 PyFunction_GetCode(PyObject
*op
)
45 if (!PyFunction_Check(op
)) {
46 PyErr_BadInternalCall();
49 return ((PyFunctionObject
*) op
) -> func_code
;
53 PyFunction_GetGlobals(PyObject
*op
)
55 if (!PyFunction_Check(op
)) {
56 PyErr_BadInternalCall();
59 return ((PyFunctionObject
*) op
) -> func_globals
;
63 PyFunction_GetDefaults(PyObject
*op
)
65 if (!PyFunction_Check(op
)) {
66 PyErr_BadInternalCall();
69 return ((PyFunctionObject
*) op
) -> func_defaults
;
73 PyFunction_SetDefaults(PyObject
*op
, PyObject
*defaults
)
75 if (!PyFunction_Check(op
)) {
76 PyErr_BadInternalCall();
79 if (defaults
== Py_None
)
81 else if (PyTuple_Check(defaults
)) {
85 PyErr_SetString(PyExc_SystemError
, "non-tuple default args");
88 Py_XDECREF(((PyFunctionObject
*) op
) -> func_defaults
);
89 ((PyFunctionObject
*) op
) -> func_defaults
= defaults
;
94 PyFunction_GetClosure(PyObject
*op
)
96 if (!PyFunction_Check(op
)) {
97 PyErr_BadInternalCall();
100 return ((PyFunctionObject
*) op
) -> func_closure
;
104 PyFunction_SetClosure(PyObject
*op
, PyObject
*closure
)
106 if (!PyFunction_Check(op
)) {
107 PyErr_BadInternalCall();
110 if (closure
== Py_None
)
112 else if (PyTuple_Check(closure
)) {
116 PyErr_SetString(PyExc_SystemError
, "non-tuple closure");
119 Py_XDECREF(((PyFunctionObject
*) op
) -> func_closure
);
120 ((PyFunctionObject
*) op
) -> func_closure
= closure
;
126 #define OFF(x) offsetof(PyFunctionObject, x)
128 static struct memberlist func_memberlist
[] = {
129 {"func_code", T_OBJECT
, OFF(func_code
)},
130 {"func_globals", T_OBJECT
, OFF(func_globals
), READONLY
},
131 {"func_name", T_OBJECT
, OFF(func_name
), READONLY
},
132 {"__name__", T_OBJECT
, OFF(func_name
), READONLY
},
133 {"func_closure", T_OBJECT
, OFF(func_closure
)},
134 {"func_defaults", T_OBJECT
, OFF(func_defaults
)},
135 {"func_doc", T_OBJECT
, OFF(func_doc
)},
136 {"__doc__", T_OBJECT
, OFF(func_doc
)},
137 {"func_dict", T_OBJECT
, OFF(func_dict
)},
138 {"__dict__", T_OBJECT
, OFF(func_dict
)},
139 {NULL
} /* Sentinel */
143 func_getattro(PyFunctionObject
*op
, PyObject
*name
)
146 char *sname
= PyString_AsString(name
);
148 if (sname
[0] != '_' && PyEval_GetRestricted()) {
149 PyErr_SetString(PyExc_RuntimeError
,
150 "function attributes not accessible in restricted mode");
154 /* no API for PyMember_HasAttr() */
155 rtn
= PyMember_Get((char *)op
, func_memberlist
, sname
);
157 if (rtn
== NULL
&& PyErr_ExceptionMatches(PyExc_AttributeError
)) {
159 if (op
->func_dict
!= NULL
) {
160 rtn
= PyDict_GetItem(op
->func_dict
, name
);
164 PyErr_SetObject(PyExc_AttributeError
, name
);
170 func_setattro(PyFunctionObject
*op
, PyObject
*name
, PyObject
*value
)
173 char *sname
= PyString_AsString(name
);
175 if (PyEval_GetRestricted()) {
176 PyErr_SetString(PyExc_RuntimeError
,
177 "function attributes not settable in restricted mode");
180 if (strcmp(sname
, "func_code") == 0) {
181 /* not legal to del f.func_code or to set it to anything
182 * other than a code object.
184 if (value
== NULL
|| !PyCode_Check(value
)) {
187 "func_code must be set to a code object");
191 else if (strcmp(sname
, "func_defaults") == 0) {
192 /* legal to del f.func_defaults. Can only set
193 * func_defaults to NULL or a tuple.
195 if (value
== Py_None
)
197 if (value
!= NULL
&& !PyTuple_Check(value
)) {
200 "func_defaults must be set to a tuple object");
204 else if (!strcmp(sname
, "func_dict") || !strcmp(sname
, "__dict__")) {
205 /* legal to del f.func_dict. Can only set func_dict to
206 * NULL or a dictionary.
208 if (value
== Py_None
)
210 if (value
!= NULL
&& !PyDict_Check(value
)) {
213 "func_dict must be set to a dict object");
218 rtn
= PyMember_Set((char *)op
, func_memberlist
, sname
, value
);
219 if (rtn
< 0 && PyErr_ExceptionMatches(PyExc_AttributeError
)) {
221 if (op
->func_dict
== NULL
) {
222 /* don't create the dict if we're deleting an
223 * attribute. In that case, we know we'll get an
227 PyErr_SetString(PyExc_AttributeError
, sname
);
230 op
->func_dict
= PyDict_New();
231 if (op
->func_dict
== NULL
)
235 rtn
= PyDict_DelItem(op
->func_dict
, name
);
237 rtn
= PyDict_SetItem(op
->func_dict
, name
, value
);
238 /* transform KeyError into AttributeError */
239 if (rtn
< 0 && PyErr_ExceptionMatches(PyExc_KeyError
))
240 PyErr_SetString(PyExc_AttributeError
, sname
);
246 func_dealloc(PyFunctionObject
*op
)
248 PyObject_GC_Fini(op
);
249 Py_DECREF(op
->func_code
);
250 Py_DECREF(op
->func_globals
);
251 Py_DECREF(op
->func_name
);
252 Py_XDECREF(op
->func_defaults
);
253 Py_XDECREF(op
->func_doc
);
254 Py_XDECREF(op
->func_dict
);
255 op
= (PyFunctionObject
*) PyObject_AS_GC(op
);
260 func_repr(PyFunctionObject
*op
)
263 if (op
->func_name
== Py_None
)
264 sprintf(buf
, "<anonymous function at %p>", op
);
266 sprintf(buf
, "<function %.100s at %p>",
267 PyString_AsString(op
->func_name
),
269 return PyString_FromString(buf
);
273 func_traverse(PyFunctionObject
*f
, visitproc visit
, void *arg
)
277 err
= visit(f
->func_code
, arg
);
281 if (f
->func_globals
) {
282 err
= visit(f
->func_globals
, arg
);
286 if (f
->func_defaults
) {
287 err
= visit(f
->func_defaults
, arg
);
292 err
= visit(f
->func_doc
, arg
);
297 err
= visit(f
->func_name
, arg
);
302 err
= visit(f
->func_dict
, arg
);
309 PyTypeObject PyFunction_Type
= {
310 PyObject_HEAD_INIT(&PyType_Type
)
313 sizeof(PyFunctionObject
) + PyGC_HEAD_SIZE
,
315 (destructor
)func_dealloc
, /*tp_dealloc*/
320 (reprfunc
)func_repr
, /*tp_repr*/
322 0, /*tp_as_sequence*/
327 (getattrofunc
)func_getattro
, /*tp_getattro*/
328 (setattrofunc
)func_setattro
, /*tp_setattro*/
329 0, /* tp_as_buffer */
330 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_GC
, /*tp_flags*/
332 (traverseproc
)func_traverse
, /* tp_traverse */