1 | n/a | |
---|
2 | n/a | /* Function object implementation */ |
---|
3 | n/a | |
---|
4 | n/a | #include "Python.h" |
---|
5 | n/a | #include "code.h" |
---|
6 | n/a | #include "structmember.h" |
---|
7 | n/a | |
---|
8 | n/a | PyObject * |
---|
9 | n/a | PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname) |
---|
10 | n/a | { |
---|
11 | n/a | PyFunctionObject *op; |
---|
12 | n/a | PyObject *doc, *consts, *module; |
---|
13 | n/a | static PyObject *__name__ = NULL; |
---|
14 | n/a | |
---|
15 | n/a | if (__name__ == NULL) { |
---|
16 | n/a | __name__ = PyUnicode_InternFromString("__name__"); |
---|
17 | n/a | if (__name__ == NULL) |
---|
18 | n/a | return NULL; |
---|
19 | n/a | } |
---|
20 | n/a | |
---|
21 | n/a | op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type); |
---|
22 | n/a | if (op == NULL) |
---|
23 | n/a | return NULL; |
---|
24 | n/a | |
---|
25 | n/a | op->func_weakreflist = NULL; |
---|
26 | n/a | Py_INCREF(code); |
---|
27 | n/a | op->func_code = code; |
---|
28 | n/a | Py_INCREF(globals); |
---|
29 | n/a | op->func_globals = globals; |
---|
30 | n/a | op->func_name = ((PyCodeObject *)code)->co_name; |
---|
31 | n/a | Py_INCREF(op->func_name); |
---|
32 | n/a | op->func_defaults = NULL; /* No default arguments */ |
---|
33 | n/a | op->func_kwdefaults = NULL; /* No keyword only defaults */ |
---|
34 | n/a | op->func_closure = NULL; |
---|
35 | n/a | |
---|
36 | n/a | consts = ((PyCodeObject *)code)->co_consts; |
---|
37 | n/a | if (PyTuple_Size(consts) >= 1) { |
---|
38 | n/a | doc = PyTuple_GetItem(consts, 0); |
---|
39 | n/a | if (!PyUnicode_Check(doc)) |
---|
40 | n/a | doc = Py_None; |
---|
41 | n/a | } |
---|
42 | n/a | else |
---|
43 | n/a | doc = Py_None; |
---|
44 | n/a | Py_INCREF(doc); |
---|
45 | n/a | op->func_doc = doc; |
---|
46 | n/a | |
---|
47 | n/a | op->func_dict = NULL; |
---|
48 | n/a | op->func_module = NULL; |
---|
49 | n/a | op->func_annotations = NULL; |
---|
50 | n/a | |
---|
51 | n/a | /* __module__: If module name is in globals, use it. |
---|
52 | n/a | Otherwise, use None. */ |
---|
53 | n/a | module = PyDict_GetItem(globals, __name__); |
---|
54 | n/a | if (module) { |
---|
55 | n/a | Py_INCREF(module); |
---|
56 | n/a | op->func_module = module; |
---|
57 | n/a | } |
---|
58 | n/a | if (qualname) |
---|
59 | n/a | op->func_qualname = qualname; |
---|
60 | n/a | else |
---|
61 | n/a | op->func_qualname = op->func_name; |
---|
62 | n/a | Py_INCREF(op->func_qualname); |
---|
63 | n/a | |
---|
64 | n/a | _PyObject_GC_TRACK(op); |
---|
65 | n/a | return (PyObject *)op; |
---|
66 | n/a | } |
---|
67 | n/a | |
---|
68 | n/a | PyObject * |
---|
69 | n/a | PyFunction_New(PyObject *code, PyObject *globals) |
---|
70 | n/a | { |
---|
71 | n/a | return PyFunction_NewWithQualName(code, globals, NULL); |
---|
72 | n/a | } |
---|
73 | n/a | |
---|
74 | n/a | PyObject * |
---|
75 | n/a | PyFunction_GetCode(PyObject *op) |
---|
76 | n/a | { |
---|
77 | n/a | if (!PyFunction_Check(op)) { |
---|
78 | n/a | PyErr_BadInternalCall(); |
---|
79 | n/a | return NULL; |
---|
80 | n/a | } |
---|
81 | n/a | return ((PyFunctionObject *) op) -> func_code; |
---|
82 | n/a | } |
---|
83 | n/a | |
---|
84 | n/a | PyObject * |
---|
85 | n/a | PyFunction_GetGlobals(PyObject *op) |
---|
86 | n/a | { |
---|
87 | n/a | if (!PyFunction_Check(op)) { |
---|
88 | n/a | PyErr_BadInternalCall(); |
---|
89 | n/a | return NULL; |
---|
90 | n/a | } |
---|
91 | n/a | return ((PyFunctionObject *) op) -> func_globals; |
---|
92 | n/a | } |
---|
93 | n/a | |
---|
94 | n/a | PyObject * |
---|
95 | n/a | PyFunction_GetModule(PyObject *op) |
---|
96 | n/a | { |
---|
97 | n/a | if (!PyFunction_Check(op)) { |
---|
98 | n/a | PyErr_BadInternalCall(); |
---|
99 | n/a | return NULL; |
---|
100 | n/a | } |
---|
101 | n/a | return ((PyFunctionObject *) op) -> func_module; |
---|
102 | n/a | } |
---|
103 | n/a | |
---|
104 | n/a | PyObject * |
---|
105 | n/a | PyFunction_GetDefaults(PyObject *op) |
---|
106 | n/a | { |
---|
107 | n/a | if (!PyFunction_Check(op)) { |
---|
108 | n/a | PyErr_BadInternalCall(); |
---|
109 | n/a | return NULL; |
---|
110 | n/a | } |
---|
111 | n/a | return ((PyFunctionObject *) op) -> func_defaults; |
---|
112 | n/a | } |
---|
113 | n/a | |
---|
114 | n/a | int |
---|
115 | n/a | PyFunction_SetDefaults(PyObject *op, PyObject *defaults) |
---|
116 | n/a | { |
---|
117 | n/a | if (!PyFunction_Check(op)) { |
---|
118 | n/a | PyErr_BadInternalCall(); |
---|
119 | n/a | return -1; |
---|
120 | n/a | } |
---|
121 | n/a | if (defaults == Py_None) |
---|
122 | n/a | defaults = NULL; |
---|
123 | n/a | else if (defaults && PyTuple_Check(defaults)) { |
---|
124 | n/a | Py_INCREF(defaults); |
---|
125 | n/a | } |
---|
126 | n/a | else { |
---|
127 | n/a | PyErr_SetString(PyExc_SystemError, "non-tuple default args"); |
---|
128 | n/a | return -1; |
---|
129 | n/a | } |
---|
130 | n/a | Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults); |
---|
131 | n/a | return 0; |
---|
132 | n/a | } |
---|
133 | n/a | |
---|
134 | n/a | PyObject * |
---|
135 | n/a | PyFunction_GetKwDefaults(PyObject *op) |
---|
136 | n/a | { |
---|
137 | n/a | if (!PyFunction_Check(op)) { |
---|
138 | n/a | PyErr_BadInternalCall(); |
---|
139 | n/a | return NULL; |
---|
140 | n/a | } |
---|
141 | n/a | return ((PyFunctionObject *) op) -> func_kwdefaults; |
---|
142 | n/a | } |
---|
143 | n/a | |
---|
144 | n/a | int |
---|
145 | n/a | PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults) |
---|
146 | n/a | { |
---|
147 | n/a | if (!PyFunction_Check(op)) { |
---|
148 | n/a | PyErr_BadInternalCall(); |
---|
149 | n/a | return -1; |
---|
150 | n/a | } |
---|
151 | n/a | if (defaults == Py_None) |
---|
152 | n/a | defaults = NULL; |
---|
153 | n/a | else if (defaults && PyDict_Check(defaults)) { |
---|
154 | n/a | Py_INCREF(defaults); |
---|
155 | n/a | } |
---|
156 | n/a | else { |
---|
157 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
158 | n/a | "non-dict keyword only default args"); |
---|
159 | n/a | return -1; |
---|
160 | n/a | } |
---|
161 | n/a | Py_XSETREF(((PyFunctionObject *)op)->func_kwdefaults, defaults); |
---|
162 | n/a | return 0; |
---|
163 | n/a | } |
---|
164 | n/a | |
---|
165 | n/a | PyObject * |
---|
166 | n/a | PyFunction_GetClosure(PyObject *op) |
---|
167 | n/a | { |
---|
168 | n/a | if (!PyFunction_Check(op)) { |
---|
169 | n/a | PyErr_BadInternalCall(); |
---|
170 | n/a | return NULL; |
---|
171 | n/a | } |
---|
172 | n/a | return ((PyFunctionObject *) op) -> func_closure; |
---|
173 | n/a | } |
---|
174 | n/a | |
---|
175 | n/a | int |
---|
176 | n/a | PyFunction_SetClosure(PyObject *op, PyObject *closure) |
---|
177 | n/a | { |
---|
178 | n/a | if (!PyFunction_Check(op)) { |
---|
179 | n/a | PyErr_BadInternalCall(); |
---|
180 | n/a | return -1; |
---|
181 | n/a | } |
---|
182 | n/a | if (closure == Py_None) |
---|
183 | n/a | closure = NULL; |
---|
184 | n/a | else if (PyTuple_Check(closure)) { |
---|
185 | n/a | Py_INCREF(closure); |
---|
186 | n/a | } |
---|
187 | n/a | else { |
---|
188 | n/a | PyErr_Format(PyExc_SystemError, |
---|
189 | n/a | "expected tuple for closure, got '%.100s'", |
---|
190 | n/a | closure->ob_type->tp_name); |
---|
191 | n/a | return -1; |
---|
192 | n/a | } |
---|
193 | n/a | Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure); |
---|
194 | n/a | return 0; |
---|
195 | n/a | } |
---|
196 | n/a | |
---|
197 | n/a | PyObject * |
---|
198 | n/a | PyFunction_GetAnnotations(PyObject *op) |
---|
199 | n/a | { |
---|
200 | n/a | if (!PyFunction_Check(op)) { |
---|
201 | n/a | PyErr_BadInternalCall(); |
---|
202 | n/a | return NULL; |
---|
203 | n/a | } |
---|
204 | n/a | return ((PyFunctionObject *) op) -> func_annotations; |
---|
205 | n/a | } |
---|
206 | n/a | |
---|
207 | n/a | int |
---|
208 | n/a | PyFunction_SetAnnotations(PyObject *op, PyObject *annotations) |
---|
209 | n/a | { |
---|
210 | n/a | if (!PyFunction_Check(op)) { |
---|
211 | n/a | PyErr_BadInternalCall(); |
---|
212 | n/a | return -1; |
---|
213 | n/a | } |
---|
214 | n/a | if (annotations == Py_None) |
---|
215 | n/a | annotations = NULL; |
---|
216 | n/a | else if (annotations && PyDict_Check(annotations)) { |
---|
217 | n/a | Py_INCREF(annotations); |
---|
218 | n/a | } |
---|
219 | n/a | else { |
---|
220 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
221 | n/a | "non-dict annotations"); |
---|
222 | n/a | return -1; |
---|
223 | n/a | } |
---|
224 | n/a | Py_XSETREF(((PyFunctionObject *)op)->func_annotations, annotations); |
---|
225 | n/a | return 0; |
---|
226 | n/a | } |
---|
227 | n/a | |
---|
228 | n/a | /* Methods */ |
---|
229 | n/a | |
---|
230 | n/a | #define OFF(x) offsetof(PyFunctionObject, x) |
---|
231 | n/a | |
---|
232 | n/a | static PyMemberDef func_memberlist[] = { |
---|
233 | n/a | {"__closure__", T_OBJECT, OFF(func_closure), |
---|
234 | n/a | RESTRICTED|READONLY}, |
---|
235 | n/a | {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED}, |
---|
236 | n/a | {"__globals__", T_OBJECT, OFF(func_globals), |
---|
237 | n/a | RESTRICTED|READONLY}, |
---|
238 | n/a | {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED}, |
---|
239 | n/a | {NULL} /* Sentinel */ |
---|
240 | n/a | }; |
---|
241 | n/a | |
---|
242 | n/a | static PyObject * |
---|
243 | n/a | func_get_code(PyFunctionObject *op) |
---|
244 | n/a | { |
---|
245 | n/a | Py_INCREF(op->func_code); |
---|
246 | n/a | return op->func_code; |
---|
247 | n/a | } |
---|
248 | n/a | |
---|
249 | n/a | static int |
---|
250 | n/a | func_set_code(PyFunctionObject *op, PyObject *value) |
---|
251 | n/a | { |
---|
252 | n/a | Py_ssize_t nfree, nclosure; |
---|
253 | n/a | |
---|
254 | n/a | /* Not legal to del f.func_code or to set it to anything |
---|
255 | n/a | * other than a code object. */ |
---|
256 | n/a | if (value == NULL || !PyCode_Check(value)) { |
---|
257 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
258 | n/a | "__code__ must be set to a code object"); |
---|
259 | n/a | return -1; |
---|
260 | n/a | } |
---|
261 | n/a | nfree = PyCode_GetNumFree((PyCodeObject *)value); |
---|
262 | n/a | nclosure = (op->func_closure == NULL ? 0 : |
---|
263 | n/a | PyTuple_GET_SIZE(op->func_closure)); |
---|
264 | n/a | if (nclosure != nfree) { |
---|
265 | n/a | PyErr_Format(PyExc_ValueError, |
---|
266 | n/a | "%U() requires a code object with %zd free vars," |
---|
267 | n/a | " not %zd", |
---|
268 | n/a | op->func_name, |
---|
269 | n/a | nclosure, nfree); |
---|
270 | n/a | return -1; |
---|
271 | n/a | } |
---|
272 | n/a | Py_INCREF(value); |
---|
273 | n/a | Py_XSETREF(op->func_code, value); |
---|
274 | n/a | return 0; |
---|
275 | n/a | } |
---|
276 | n/a | |
---|
277 | n/a | static PyObject * |
---|
278 | n/a | func_get_name(PyFunctionObject *op) |
---|
279 | n/a | { |
---|
280 | n/a | Py_INCREF(op->func_name); |
---|
281 | n/a | return op->func_name; |
---|
282 | n/a | } |
---|
283 | n/a | |
---|
284 | n/a | static int |
---|
285 | n/a | func_set_name(PyFunctionObject *op, PyObject *value) |
---|
286 | n/a | { |
---|
287 | n/a | /* Not legal to del f.func_name or to set it to anything |
---|
288 | n/a | * other than a string object. */ |
---|
289 | n/a | if (value == NULL || !PyUnicode_Check(value)) { |
---|
290 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
291 | n/a | "__name__ must be set to a string object"); |
---|
292 | n/a | return -1; |
---|
293 | n/a | } |
---|
294 | n/a | Py_INCREF(value); |
---|
295 | n/a | Py_XSETREF(op->func_name, value); |
---|
296 | n/a | return 0; |
---|
297 | n/a | } |
---|
298 | n/a | |
---|
299 | n/a | static PyObject * |
---|
300 | n/a | func_get_qualname(PyFunctionObject *op) |
---|
301 | n/a | { |
---|
302 | n/a | Py_INCREF(op->func_qualname); |
---|
303 | n/a | return op->func_qualname; |
---|
304 | n/a | } |
---|
305 | n/a | |
---|
306 | n/a | static int |
---|
307 | n/a | func_set_qualname(PyFunctionObject *op, PyObject *value) |
---|
308 | n/a | { |
---|
309 | n/a | /* Not legal to del f.__qualname__ or to set it to anything |
---|
310 | n/a | * other than a string object. */ |
---|
311 | n/a | if (value == NULL || !PyUnicode_Check(value)) { |
---|
312 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
313 | n/a | "__qualname__ must be set to a string object"); |
---|
314 | n/a | return -1; |
---|
315 | n/a | } |
---|
316 | n/a | Py_INCREF(value); |
---|
317 | n/a | Py_XSETREF(op->func_qualname, value); |
---|
318 | n/a | return 0; |
---|
319 | n/a | } |
---|
320 | n/a | |
---|
321 | n/a | static PyObject * |
---|
322 | n/a | func_get_defaults(PyFunctionObject *op) |
---|
323 | n/a | { |
---|
324 | n/a | if (op->func_defaults == NULL) { |
---|
325 | n/a | Py_RETURN_NONE; |
---|
326 | n/a | } |
---|
327 | n/a | Py_INCREF(op->func_defaults); |
---|
328 | n/a | return op->func_defaults; |
---|
329 | n/a | } |
---|
330 | n/a | |
---|
331 | n/a | static int |
---|
332 | n/a | func_set_defaults(PyFunctionObject *op, PyObject *value) |
---|
333 | n/a | { |
---|
334 | n/a | /* Legal to del f.func_defaults. |
---|
335 | n/a | * Can only set func_defaults to NULL or a tuple. */ |
---|
336 | n/a | if (value == Py_None) |
---|
337 | n/a | value = NULL; |
---|
338 | n/a | if (value != NULL && !PyTuple_Check(value)) { |
---|
339 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
340 | n/a | "__defaults__ must be set to a tuple object"); |
---|
341 | n/a | return -1; |
---|
342 | n/a | } |
---|
343 | n/a | Py_XINCREF(value); |
---|
344 | n/a | Py_XSETREF(op->func_defaults, value); |
---|
345 | n/a | return 0; |
---|
346 | n/a | } |
---|
347 | n/a | |
---|
348 | n/a | static PyObject * |
---|
349 | n/a | func_get_kwdefaults(PyFunctionObject *op) |
---|
350 | n/a | { |
---|
351 | n/a | if (op->func_kwdefaults == NULL) { |
---|
352 | n/a | Py_RETURN_NONE; |
---|
353 | n/a | } |
---|
354 | n/a | Py_INCREF(op->func_kwdefaults); |
---|
355 | n/a | return op->func_kwdefaults; |
---|
356 | n/a | } |
---|
357 | n/a | |
---|
358 | n/a | static int |
---|
359 | n/a | func_set_kwdefaults(PyFunctionObject *op, PyObject *value) |
---|
360 | n/a | { |
---|
361 | n/a | if (value == Py_None) |
---|
362 | n/a | value = NULL; |
---|
363 | n/a | /* Legal to del f.func_kwdefaults. |
---|
364 | n/a | * Can only set func_kwdefaults to NULL or a dict. */ |
---|
365 | n/a | if (value != NULL && !PyDict_Check(value)) { |
---|
366 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
367 | n/a | "__kwdefaults__ must be set to a dict object"); |
---|
368 | n/a | return -1; |
---|
369 | n/a | } |
---|
370 | n/a | Py_XINCREF(value); |
---|
371 | n/a | Py_XSETREF(op->func_kwdefaults, value); |
---|
372 | n/a | return 0; |
---|
373 | n/a | } |
---|
374 | n/a | |
---|
375 | n/a | static PyObject * |
---|
376 | n/a | func_get_annotations(PyFunctionObject *op) |
---|
377 | n/a | { |
---|
378 | n/a | if (op->func_annotations == NULL) { |
---|
379 | n/a | op->func_annotations = PyDict_New(); |
---|
380 | n/a | if (op->func_annotations == NULL) |
---|
381 | n/a | return NULL; |
---|
382 | n/a | } |
---|
383 | n/a | Py_INCREF(op->func_annotations); |
---|
384 | n/a | return op->func_annotations; |
---|
385 | n/a | } |
---|
386 | n/a | |
---|
387 | n/a | static int |
---|
388 | n/a | func_set_annotations(PyFunctionObject *op, PyObject *value) |
---|
389 | n/a | { |
---|
390 | n/a | if (value == Py_None) |
---|
391 | n/a | value = NULL; |
---|
392 | n/a | /* Legal to del f.func_annotations. |
---|
393 | n/a | * Can only set func_annotations to NULL (through C api) |
---|
394 | n/a | * or a dict. */ |
---|
395 | n/a | if (value != NULL && !PyDict_Check(value)) { |
---|
396 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
397 | n/a | "__annotations__ must be set to a dict object"); |
---|
398 | n/a | return -1; |
---|
399 | n/a | } |
---|
400 | n/a | Py_XINCREF(value); |
---|
401 | n/a | Py_XSETREF(op->func_annotations, value); |
---|
402 | n/a | return 0; |
---|
403 | n/a | } |
---|
404 | n/a | |
---|
405 | n/a | static PyGetSetDef func_getsetlist[] = { |
---|
406 | n/a | {"__code__", (getter)func_get_code, (setter)func_set_code}, |
---|
407 | n/a | {"__defaults__", (getter)func_get_defaults, |
---|
408 | n/a | (setter)func_set_defaults}, |
---|
409 | n/a | {"__kwdefaults__", (getter)func_get_kwdefaults, |
---|
410 | n/a | (setter)func_set_kwdefaults}, |
---|
411 | n/a | {"__annotations__", (getter)func_get_annotations, |
---|
412 | n/a | (setter)func_set_annotations}, |
---|
413 | n/a | {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict}, |
---|
414 | n/a | {"__name__", (getter)func_get_name, (setter)func_set_name}, |
---|
415 | n/a | {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname}, |
---|
416 | n/a | {NULL} /* Sentinel */ |
---|
417 | n/a | }; |
---|
418 | n/a | |
---|
419 | n/a | PyDoc_STRVAR(func_doc, |
---|
420 | n/a | "function(code, globals[, name[, argdefs[, closure]]])\n\ |
---|
421 | n/a | \n\ |
---|
422 | n/a | Create a function object from a code object and a dictionary.\n\ |
---|
423 | n/a | The optional name string overrides the name from the code object.\n\ |
---|
424 | n/a | The optional argdefs tuple specifies the default argument values.\n\ |
---|
425 | n/a | The optional closure tuple supplies the bindings for free variables."); |
---|
426 | n/a | |
---|
427 | n/a | /* func_new() maintains the following invariants for closures. The |
---|
428 | n/a | closure must correspond to the free variables of the code object. |
---|
429 | n/a | |
---|
430 | n/a | if len(code.co_freevars) == 0: |
---|
431 | n/a | closure = NULL |
---|
432 | n/a | else: |
---|
433 | n/a | len(closure) == len(code.co_freevars) |
---|
434 | n/a | for every elt in closure, type(elt) == cell |
---|
435 | n/a | */ |
---|
436 | n/a | |
---|
437 | n/a | static PyObject * |
---|
438 | n/a | func_new(PyTypeObject* type, PyObject* args, PyObject* kw) |
---|
439 | n/a | { |
---|
440 | n/a | PyCodeObject *code; |
---|
441 | n/a | PyObject *globals; |
---|
442 | n/a | PyObject *name = Py_None; |
---|
443 | n/a | PyObject *defaults = Py_None; |
---|
444 | n/a | PyObject *closure = Py_None; |
---|
445 | n/a | PyFunctionObject *newfunc; |
---|
446 | n/a | Py_ssize_t nfree, nclosure; |
---|
447 | n/a | static char *kwlist[] = {"code", "globals", "name", |
---|
448 | n/a | "argdefs", "closure", 0}; |
---|
449 | n/a | |
---|
450 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function", |
---|
451 | n/a | kwlist, |
---|
452 | n/a | &PyCode_Type, &code, |
---|
453 | n/a | &PyDict_Type, &globals, |
---|
454 | n/a | &name, &defaults, &closure)) |
---|
455 | n/a | return NULL; |
---|
456 | n/a | if (name != Py_None && !PyUnicode_Check(name)) { |
---|
457 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
458 | n/a | "arg 3 (name) must be None or string"); |
---|
459 | n/a | return NULL; |
---|
460 | n/a | } |
---|
461 | n/a | if (defaults != Py_None && !PyTuple_Check(defaults)) { |
---|
462 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
463 | n/a | "arg 4 (defaults) must be None or tuple"); |
---|
464 | n/a | return NULL; |
---|
465 | n/a | } |
---|
466 | n/a | nfree = PyTuple_GET_SIZE(code->co_freevars); |
---|
467 | n/a | if (!PyTuple_Check(closure)) { |
---|
468 | n/a | if (nfree && closure == Py_None) { |
---|
469 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
470 | n/a | "arg 5 (closure) must be tuple"); |
---|
471 | n/a | return NULL; |
---|
472 | n/a | } |
---|
473 | n/a | else if (closure != Py_None) { |
---|
474 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
475 | n/a | "arg 5 (closure) must be None or tuple"); |
---|
476 | n/a | return NULL; |
---|
477 | n/a | } |
---|
478 | n/a | } |
---|
479 | n/a | |
---|
480 | n/a | /* check that the closure is well-formed */ |
---|
481 | n/a | nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure); |
---|
482 | n/a | if (nfree != nclosure) |
---|
483 | n/a | return PyErr_Format(PyExc_ValueError, |
---|
484 | n/a | "%U requires closure of length %zd, not %zd", |
---|
485 | n/a | code->co_name, nfree, nclosure); |
---|
486 | n/a | if (nclosure) { |
---|
487 | n/a | Py_ssize_t i; |
---|
488 | n/a | for (i = 0; i < nclosure; i++) { |
---|
489 | n/a | PyObject *o = PyTuple_GET_ITEM(closure, i); |
---|
490 | n/a | if (!PyCell_Check(o)) { |
---|
491 | n/a | return PyErr_Format(PyExc_TypeError, |
---|
492 | n/a | "arg 5 (closure) expected cell, found %s", |
---|
493 | n/a | o->ob_type->tp_name); |
---|
494 | n/a | } |
---|
495 | n/a | } |
---|
496 | n/a | } |
---|
497 | n/a | |
---|
498 | n/a | newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code, |
---|
499 | n/a | globals); |
---|
500 | n/a | if (newfunc == NULL) |
---|
501 | n/a | return NULL; |
---|
502 | n/a | |
---|
503 | n/a | if (name != Py_None) { |
---|
504 | n/a | Py_INCREF(name); |
---|
505 | n/a | Py_SETREF(newfunc->func_name, name); |
---|
506 | n/a | } |
---|
507 | n/a | if (defaults != Py_None) { |
---|
508 | n/a | Py_INCREF(defaults); |
---|
509 | n/a | newfunc->func_defaults = defaults; |
---|
510 | n/a | } |
---|
511 | n/a | if (closure != Py_None) { |
---|
512 | n/a | Py_INCREF(closure); |
---|
513 | n/a | newfunc->func_closure = closure; |
---|
514 | n/a | } |
---|
515 | n/a | |
---|
516 | n/a | return (PyObject *)newfunc; |
---|
517 | n/a | } |
---|
518 | n/a | |
---|
519 | n/a | static void |
---|
520 | n/a | func_dealloc(PyFunctionObject *op) |
---|
521 | n/a | { |
---|
522 | n/a | _PyObject_GC_UNTRACK(op); |
---|
523 | n/a | if (op->func_weakreflist != NULL) |
---|
524 | n/a | PyObject_ClearWeakRefs((PyObject *) op); |
---|
525 | n/a | Py_DECREF(op->func_code); |
---|
526 | n/a | Py_DECREF(op->func_globals); |
---|
527 | n/a | Py_XDECREF(op->func_module); |
---|
528 | n/a | Py_DECREF(op->func_name); |
---|
529 | n/a | Py_XDECREF(op->func_defaults); |
---|
530 | n/a | Py_XDECREF(op->func_kwdefaults); |
---|
531 | n/a | Py_XDECREF(op->func_doc); |
---|
532 | n/a | Py_XDECREF(op->func_dict); |
---|
533 | n/a | Py_XDECREF(op->func_closure); |
---|
534 | n/a | Py_XDECREF(op->func_annotations); |
---|
535 | n/a | Py_XDECREF(op->func_qualname); |
---|
536 | n/a | PyObject_GC_Del(op); |
---|
537 | n/a | } |
---|
538 | n/a | |
---|
539 | n/a | static PyObject* |
---|
540 | n/a | func_repr(PyFunctionObject *op) |
---|
541 | n/a | { |
---|
542 | n/a | return PyUnicode_FromFormat("<function %U at %p>", |
---|
543 | n/a | op->func_qualname, op); |
---|
544 | n/a | } |
---|
545 | n/a | |
---|
546 | n/a | static int |
---|
547 | n/a | func_traverse(PyFunctionObject *f, visitproc visit, void *arg) |
---|
548 | n/a | { |
---|
549 | n/a | Py_VISIT(f->func_code); |
---|
550 | n/a | Py_VISIT(f->func_globals); |
---|
551 | n/a | Py_VISIT(f->func_module); |
---|
552 | n/a | Py_VISIT(f->func_defaults); |
---|
553 | n/a | Py_VISIT(f->func_kwdefaults); |
---|
554 | n/a | Py_VISIT(f->func_doc); |
---|
555 | n/a | Py_VISIT(f->func_name); |
---|
556 | n/a | Py_VISIT(f->func_dict); |
---|
557 | n/a | Py_VISIT(f->func_closure); |
---|
558 | n/a | Py_VISIT(f->func_annotations); |
---|
559 | n/a | Py_VISIT(f->func_qualname); |
---|
560 | n/a | return 0; |
---|
561 | n/a | } |
---|
562 | n/a | |
---|
563 | n/a | static PyObject * |
---|
564 | n/a | function_call(PyObject *func, PyObject *args, PyObject *kwargs) |
---|
565 | n/a | { |
---|
566 | n/a | PyObject **stack; |
---|
567 | n/a | Py_ssize_t nargs; |
---|
568 | n/a | |
---|
569 | n/a | stack = &PyTuple_GET_ITEM(args, 0); |
---|
570 | n/a | nargs = PyTuple_GET_SIZE(args); |
---|
571 | n/a | return _PyFunction_FastCallDict(func, stack, nargs, kwargs); |
---|
572 | n/a | } |
---|
573 | n/a | |
---|
574 | n/a | /* Bind a function to an object */ |
---|
575 | n/a | static PyObject * |
---|
576 | n/a | func_descr_get(PyObject *func, PyObject *obj, PyObject *type) |
---|
577 | n/a | { |
---|
578 | n/a | if (obj == Py_None || obj == NULL) { |
---|
579 | n/a | Py_INCREF(func); |
---|
580 | n/a | return func; |
---|
581 | n/a | } |
---|
582 | n/a | return PyMethod_New(func, obj); |
---|
583 | n/a | } |
---|
584 | n/a | |
---|
585 | n/a | PyTypeObject PyFunction_Type = { |
---|
586 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
587 | n/a | "function", |
---|
588 | n/a | sizeof(PyFunctionObject), |
---|
589 | n/a | 0, |
---|
590 | n/a | (destructor)func_dealloc, /* tp_dealloc */ |
---|
591 | n/a | 0, /* tp_print */ |
---|
592 | n/a | 0, /* tp_getattr */ |
---|
593 | n/a | 0, /* tp_setattr */ |
---|
594 | n/a | 0, /* tp_reserved */ |
---|
595 | n/a | (reprfunc)func_repr, /* tp_repr */ |
---|
596 | n/a | 0, /* tp_as_number */ |
---|
597 | n/a | 0, /* tp_as_sequence */ |
---|
598 | n/a | 0, /* tp_as_mapping */ |
---|
599 | n/a | 0, /* tp_hash */ |
---|
600 | n/a | function_call, /* tp_call */ |
---|
601 | n/a | 0, /* tp_str */ |
---|
602 | n/a | 0, /* tp_getattro */ |
---|
603 | n/a | 0, /* tp_setattro */ |
---|
604 | n/a | 0, /* tp_as_buffer */ |
---|
605 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
---|
606 | n/a | func_doc, /* tp_doc */ |
---|
607 | n/a | (traverseproc)func_traverse, /* tp_traverse */ |
---|
608 | n/a | 0, /* tp_clear */ |
---|
609 | n/a | 0, /* tp_richcompare */ |
---|
610 | n/a | offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */ |
---|
611 | n/a | 0, /* tp_iter */ |
---|
612 | n/a | 0, /* tp_iternext */ |
---|
613 | n/a | 0, /* tp_methods */ |
---|
614 | n/a | func_memberlist, /* tp_members */ |
---|
615 | n/a | func_getsetlist, /* tp_getset */ |
---|
616 | n/a | 0, /* tp_base */ |
---|
617 | n/a | 0, /* tp_dict */ |
---|
618 | n/a | func_descr_get, /* tp_descr_get */ |
---|
619 | n/a | 0, /* tp_descr_set */ |
---|
620 | n/a | offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */ |
---|
621 | n/a | 0, /* tp_init */ |
---|
622 | n/a | 0, /* tp_alloc */ |
---|
623 | n/a | func_new, /* tp_new */ |
---|
624 | n/a | }; |
---|
625 | n/a | |
---|
626 | n/a | |
---|
627 | n/a | /* Class method object */ |
---|
628 | n/a | |
---|
629 | n/a | /* A class method receives the class as implicit first argument, |
---|
630 | n/a | just like an instance method receives the instance. |
---|
631 | n/a | To declare a class method, use this idiom: |
---|
632 | n/a | |
---|
633 | n/a | class C: |
---|
634 | n/a | @classmethod |
---|
635 | n/a | def f(cls, arg1, arg2, ...): |
---|
636 | n/a | ... |
---|
637 | n/a | |
---|
638 | n/a | It can be called either on the class (e.g. C.f()) or on an instance |
---|
639 | n/a | (e.g. C().f()); the instance is ignored except for its class. |
---|
640 | n/a | If a class method is called for a derived class, the derived class |
---|
641 | n/a | object is passed as the implied first argument. |
---|
642 | n/a | |
---|
643 | n/a | Class methods are different than C++ or Java static methods. |
---|
644 | n/a | If you want those, see static methods below. |
---|
645 | n/a | */ |
---|
646 | n/a | |
---|
647 | n/a | typedef struct { |
---|
648 | n/a | PyObject_HEAD |
---|
649 | n/a | PyObject *cm_callable; |
---|
650 | n/a | PyObject *cm_dict; |
---|
651 | n/a | } classmethod; |
---|
652 | n/a | |
---|
653 | n/a | static void |
---|
654 | n/a | cm_dealloc(classmethod *cm) |
---|
655 | n/a | { |
---|
656 | n/a | _PyObject_GC_UNTRACK((PyObject *)cm); |
---|
657 | n/a | Py_XDECREF(cm->cm_callable); |
---|
658 | n/a | Py_XDECREF(cm->cm_dict); |
---|
659 | n/a | Py_TYPE(cm)->tp_free((PyObject *)cm); |
---|
660 | n/a | } |
---|
661 | n/a | |
---|
662 | n/a | static int |
---|
663 | n/a | cm_traverse(classmethod *cm, visitproc visit, void *arg) |
---|
664 | n/a | { |
---|
665 | n/a | Py_VISIT(cm->cm_callable); |
---|
666 | n/a | Py_VISIT(cm->cm_dict); |
---|
667 | n/a | return 0; |
---|
668 | n/a | } |
---|
669 | n/a | |
---|
670 | n/a | static int |
---|
671 | n/a | cm_clear(classmethod *cm) |
---|
672 | n/a | { |
---|
673 | n/a | Py_CLEAR(cm->cm_callable); |
---|
674 | n/a | Py_CLEAR(cm->cm_dict); |
---|
675 | n/a | return 0; |
---|
676 | n/a | } |
---|
677 | n/a | |
---|
678 | n/a | |
---|
679 | n/a | static PyObject * |
---|
680 | n/a | cm_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
---|
681 | n/a | { |
---|
682 | n/a | classmethod *cm = (classmethod *)self; |
---|
683 | n/a | |
---|
684 | n/a | if (cm->cm_callable == NULL) { |
---|
685 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
686 | n/a | "uninitialized classmethod object"); |
---|
687 | n/a | return NULL; |
---|
688 | n/a | } |
---|
689 | n/a | if (type == NULL) |
---|
690 | n/a | type = (PyObject *)(Py_TYPE(obj)); |
---|
691 | n/a | return PyMethod_New(cm->cm_callable, type); |
---|
692 | n/a | } |
---|
693 | n/a | |
---|
694 | n/a | static int |
---|
695 | n/a | cm_init(PyObject *self, PyObject *args, PyObject *kwds) |
---|
696 | n/a | { |
---|
697 | n/a | classmethod *cm = (classmethod *)self; |
---|
698 | n/a | PyObject *callable; |
---|
699 | n/a | |
---|
700 | n/a | if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable)) |
---|
701 | n/a | return -1; |
---|
702 | n/a | if (!_PyArg_NoKeywords("classmethod", kwds)) |
---|
703 | n/a | return -1; |
---|
704 | n/a | Py_INCREF(callable); |
---|
705 | n/a | cm->cm_callable = callable; |
---|
706 | n/a | return 0; |
---|
707 | n/a | } |
---|
708 | n/a | |
---|
709 | n/a | static PyMemberDef cm_memberlist[] = { |
---|
710 | n/a | {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY}, |
---|
711 | n/a | {NULL} /* Sentinel */ |
---|
712 | n/a | }; |
---|
713 | n/a | |
---|
714 | n/a | static PyObject * |
---|
715 | n/a | cm_get___isabstractmethod__(classmethod *cm, void *closure) |
---|
716 | n/a | { |
---|
717 | n/a | int res = _PyObject_IsAbstract(cm->cm_callable); |
---|
718 | n/a | if (res == -1) { |
---|
719 | n/a | return NULL; |
---|
720 | n/a | } |
---|
721 | n/a | else if (res) { |
---|
722 | n/a | Py_RETURN_TRUE; |
---|
723 | n/a | } |
---|
724 | n/a | Py_RETURN_FALSE; |
---|
725 | n/a | } |
---|
726 | n/a | |
---|
727 | n/a | static PyGetSetDef cm_getsetlist[] = { |
---|
728 | n/a | {"__isabstractmethod__", |
---|
729 | n/a | (getter)cm_get___isabstractmethod__, NULL, |
---|
730 | n/a | NULL, |
---|
731 | n/a | NULL}, |
---|
732 | n/a | {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL}, |
---|
733 | n/a | {NULL} /* Sentinel */ |
---|
734 | n/a | }; |
---|
735 | n/a | |
---|
736 | n/a | PyDoc_STRVAR(classmethod_doc, |
---|
737 | n/a | "classmethod(function) -> method\n\ |
---|
738 | n/a | \n\ |
---|
739 | n/a | Convert a function to be a class method.\n\ |
---|
740 | n/a | \n\ |
---|
741 | n/a | A class method receives the class as implicit first argument,\n\ |
---|
742 | n/a | just like an instance method receives the instance.\n\ |
---|
743 | n/a | To declare a class method, use this idiom:\n\ |
---|
744 | n/a | \n\ |
---|
745 | n/a | class C:\n\ |
---|
746 | n/a | @classmethod\n\ |
---|
747 | n/a | def f(cls, arg1, arg2, ...):\n\ |
---|
748 | n/a | ...\n\ |
---|
749 | n/a | \n\ |
---|
750 | n/a | It can be called either on the class (e.g. C.f()) or on an instance\n\ |
---|
751 | n/a | (e.g. C().f()). The instance is ignored except for its class.\n\ |
---|
752 | n/a | If a class method is called for a derived class, the derived class\n\ |
---|
753 | n/a | object is passed as the implied first argument.\n\ |
---|
754 | n/a | \n\ |
---|
755 | n/a | Class methods are different than C++ or Java static methods.\n\ |
---|
756 | n/a | If you want those, see the staticmethod builtin."); |
---|
757 | n/a | |
---|
758 | n/a | PyTypeObject PyClassMethod_Type = { |
---|
759 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
760 | n/a | "classmethod", |
---|
761 | n/a | sizeof(classmethod), |
---|
762 | n/a | 0, |
---|
763 | n/a | (destructor)cm_dealloc, /* tp_dealloc */ |
---|
764 | n/a | 0, /* tp_print */ |
---|
765 | n/a | 0, /* tp_getattr */ |
---|
766 | n/a | 0, /* tp_setattr */ |
---|
767 | n/a | 0, /* tp_reserved */ |
---|
768 | n/a | 0, /* tp_repr */ |
---|
769 | n/a | 0, /* tp_as_number */ |
---|
770 | n/a | 0, /* tp_as_sequence */ |
---|
771 | n/a | 0, /* tp_as_mapping */ |
---|
772 | n/a | 0, /* tp_hash */ |
---|
773 | n/a | 0, /* tp_call */ |
---|
774 | n/a | 0, /* tp_str */ |
---|
775 | n/a | 0, /* tp_getattro */ |
---|
776 | n/a | 0, /* tp_setattro */ |
---|
777 | n/a | 0, /* tp_as_buffer */ |
---|
778 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
---|
779 | n/a | classmethod_doc, /* tp_doc */ |
---|
780 | n/a | (traverseproc)cm_traverse, /* tp_traverse */ |
---|
781 | n/a | (inquiry)cm_clear, /* tp_clear */ |
---|
782 | n/a | 0, /* tp_richcompare */ |
---|
783 | n/a | 0, /* tp_weaklistoffset */ |
---|
784 | n/a | 0, /* tp_iter */ |
---|
785 | n/a | 0, /* tp_iternext */ |
---|
786 | n/a | 0, /* tp_methods */ |
---|
787 | n/a | cm_memberlist, /* tp_members */ |
---|
788 | n/a | cm_getsetlist, /* tp_getset */ |
---|
789 | n/a | 0, /* tp_base */ |
---|
790 | n/a | 0, /* tp_dict */ |
---|
791 | n/a | cm_descr_get, /* tp_descr_get */ |
---|
792 | n/a | 0, /* tp_descr_set */ |
---|
793 | n/a | offsetof(classmethod, cm_dict), /* tp_dictoffset */ |
---|
794 | n/a | cm_init, /* tp_init */ |
---|
795 | n/a | PyType_GenericAlloc, /* tp_alloc */ |
---|
796 | n/a | PyType_GenericNew, /* tp_new */ |
---|
797 | n/a | PyObject_GC_Del, /* tp_free */ |
---|
798 | n/a | }; |
---|
799 | n/a | |
---|
800 | n/a | PyObject * |
---|
801 | n/a | PyClassMethod_New(PyObject *callable) |
---|
802 | n/a | { |
---|
803 | n/a | classmethod *cm = (classmethod *) |
---|
804 | n/a | PyType_GenericAlloc(&PyClassMethod_Type, 0); |
---|
805 | n/a | if (cm != NULL) { |
---|
806 | n/a | Py_INCREF(callable); |
---|
807 | n/a | cm->cm_callable = callable; |
---|
808 | n/a | } |
---|
809 | n/a | return (PyObject *)cm; |
---|
810 | n/a | } |
---|
811 | n/a | |
---|
812 | n/a | |
---|
813 | n/a | /* Static method object */ |
---|
814 | n/a | |
---|
815 | n/a | /* A static method does not receive an implicit first argument. |
---|
816 | n/a | To declare a static method, use this idiom: |
---|
817 | n/a | |
---|
818 | n/a | class C: |
---|
819 | n/a | @staticmethod |
---|
820 | n/a | def f(arg1, arg2, ...): |
---|
821 | n/a | ... |
---|
822 | n/a | |
---|
823 | n/a | It can be called either on the class (e.g. C.f()) or on an instance |
---|
824 | n/a | (e.g. C().f()); the instance is ignored except for its class. |
---|
825 | n/a | |
---|
826 | n/a | Static methods in Python are similar to those found in Java or C++. |
---|
827 | n/a | For a more advanced concept, see class methods above. |
---|
828 | n/a | */ |
---|
829 | n/a | |
---|
830 | n/a | typedef struct { |
---|
831 | n/a | PyObject_HEAD |
---|
832 | n/a | PyObject *sm_callable; |
---|
833 | n/a | PyObject *sm_dict; |
---|
834 | n/a | } staticmethod; |
---|
835 | n/a | |
---|
836 | n/a | static void |
---|
837 | n/a | sm_dealloc(staticmethod *sm) |
---|
838 | n/a | { |
---|
839 | n/a | _PyObject_GC_UNTRACK((PyObject *)sm); |
---|
840 | n/a | Py_XDECREF(sm->sm_callable); |
---|
841 | n/a | Py_XDECREF(sm->sm_dict); |
---|
842 | n/a | Py_TYPE(sm)->tp_free((PyObject *)sm); |
---|
843 | n/a | } |
---|
844 | n/a | |
---|
845 | n/a | static int |
---|
846 | n/a | sm_traverse(staticmethod *sm, visitproc visit, void *arg) |
---|
847 | n/a | { |
---|
848 | n/a | Py_VISIT(sm->sm_callable); |
---|
849 | n/a | Py_VISIT(sm->sm_dict); |
---|
850 | n/a | return 0; |
---|
851 | n/a | } |
---|
852 | n/a | |
---|
853 | n/a | static int |
---|
854 | n/a | sm_clear(staticmethod *sm) |
---|
855 | n/a | { |
---|
856 | n/a | Py_CLEAR(sm->sm_callable); |
---|
857 | n/a | Py_CLEAR(sm->sm_dict); |
---|
858 | n/a | return 0; |
---|
859 | n/a | } |
---|
860 | n/a | |
---|
861 | n/a | static PyObject * |
---|
862 | n/a | sm_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
---|
863 | n/a | { |
---|
864 | n/a | staticmethod *sm = (staticmethod *)self; |
---|
865 | n/a | |
---|
866 | n/a | if (sm->sm_callable == NULL) { |
---|
867 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
868 | n/a | "uninitialized staticmethod object"); |
---|
869 | n/a | return NULL; |
---|
870 | n/a | } |
---|
871 | n/a | Py_INCREF(sm->sm_callable); |
---|
872 | n/a | return sm->sm_callable; |
---|
873 | n/a | } |
---|
874 | n/a | |
---|
875 | n/a | static int |
---|
876 | n/a | sm_init(PyObject *self, PyObject *args, PyObject *kwds) |
---|
877 | n/a | { |
---|
878 | n/a | staticmethod *sm = (staticmethod *)self; |
---|
879 | n/a | PyObject *callable; |
---|
880 | n/a | |
---|
881 | n/a | if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable)) |
---|
882 | n/a | return -1; |
---|
883 | n/a | if (!_PyArg_NoKeywords("staticmethod", kwds)) |
---|
884 | n/a | return -1; |
---|
885 | n/a | Py_INCREF(callable); |
---|
886 | n/a | sm->sm_callable = callable; |
---|
887 | n/a | return 0; |
---|
888 | n/a | } |
---|
889 | n/a | |
---|
890 | n/a | static PyMemberDef sm_memberlist[] = { |
---|
891 | n/a | {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY}, |
---|
892 | n/a | {NULL} /* Sentinel */ |
---|
893 | n/a | }; |
---|
894 | n/a | |
---|
895 | n/a | static PyObject * |
---|
896 | n/a | sm_get___isabstractmethod__(staticmethod *sm, void *closure) |
---|
897 | n/a | { |
---|
898 | n/a | int res = _PyObject_IsAbstract(sm->sm_callable); |
---|
899 | n/a | if (res == -1) { |
---|
900 | n/a | return NULL; |
---|
901 | n/a | } |
---|
902 | n/a | else if (res) { |
---|
903 | n/a | Py_RETURN_TRUE; |
---|
904 | n/a | } |
---|
905 | n/a | Py_RETURN_FALSE; |
---|
906 | n/a | } |
---|
907 | n/a | |
---|
908 | n/a | static PyGetSetDef sm_getsetlist[] = { |
---|
909 | n/a | {"__isabstractmethod__", |
---|
910 | n/a | (getter)sm_get___isabstractmethod__, NULL, |
---|
911 | n/a | NULL, |
---|
912 | n/a | NULL}, |
---|
913 | n/a | {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL}, |
---|
914 | n/a | {NULL} /* Sentinel */ |
---|
915 | n/a | }; |
---|
916 | n/a | |
---|
917 | n/a | PyDoc_STRVAR(staticmethod_doc, |
---|
918 | n/a | "staticmethod(function) -> method\n\ |
---|
919 | n/a | \n\ |
---|
920 | n/a | Convert a function to be a static method.\n\ |
---|
921 | n/a | \n\ |
---|
922 | n/a | A static method does not receive an implicit first argument.\n\ |
---|
923 | n/a | To declare a static method, use this idiom:\n\ |
---|
924 | n/a | \n\ |
---|
925 | n/a | class C:\n\ |
---|
926 | n/a | @staticmethod\n\ |
---|
927 | n/a | def f(arg1, arg2, ...):\n\ |
---|
928 | n/a | ...\n\ |
---|
929 | n/a | \n\ |
---|
930 | n/a | It can be called either on the class (e.g. C.f()) or on an instance\n\ |
---|
931 | n/a | (e.g. C().f()). The instance is ignored except for its class.\n\ |
---|
932 | n/a | \n\ |
---|
933 | n/a | Static methods in Python are similar to those found in Java or C++.\n\ |
---|
934 | n/a | For a more advanced concept, see the classmethod builtin."); |
---|
935 | n/a | |
---|
936 | n/a | PyTypeObject PyStaticMethod_Type = { |
---|
937 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
938 | n/a | "staticmethod", |
---|
939 | n/a | sizeof(staticmethod), |
---|
940 | n/a | 0, |
---|
941 | n/a | (destructor)sm_dealloc, /* tp_dealloc */ |
---|
942 | n/a | 0, /* tp_print */ |
---|
943 | n/a | 0, /* tp_getattr */ |
---|
944 | n/a | 0, /* tp_setattr */ |
---|
945 | n/a | 0, /* tp_reserved */ |
---|
946 | n/a | 0, /* tp_repr */ |
---|
947 | n/a | 0, /* tp_as_number */ |
---|
948 | n/a | 0, /* tp_as_sequence */ |
---|
949 | n/a | 0, /* tp_as_mapping */ |
---|
950 | n/a | 0, /* tp_hash */ |
---|
951 | n/a | 0, /* tp_call */ |
---|
952 | n/a | 0, /* tp_str */ |
---|
953 | n/a | 0, /* tp_getattro */ |
---|
954 | n/a | 0, /* tp_setattro */ |
---|
955 | n/a | 0, /* tp_as_buffer */ |
---|
956 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
---|
957 | n/a | staticmethod_doc, /* tp_doc */ |
---|
958 | n/a | (traverseproc)sm_traverse, /* tp_traverse */ |
---|
959 | n/a | (inquiry)sm_clear, /* tp_clear */ |
---|
960 | n/a | 0, /* tp_richcompare */ |
---|
961 | n/a | 0, /* tp_weaklistoffset */ |
---|
962 | n/a | 0, /* tp_iter */ |
---|
963 | n/a | 0, /* tp_iternext */ |
---|
964 | n/a | 0, /* tp_methods */ |
---|
965 | n/a | sm_memberlist, /* tp_members */ |
---|
966 | n/a | sm_getsetlist, /* tp_getset */ |
---|
967 | n/a | 0, /* tp_base */ |
---|
968 | n/a | 0, /* tp_dict */ |
---|
969 | n/a | sm_descr_get, /* tp_descr_get */ |
---|
970 | n/a | 0, /* tp_descr_set */ |
---|
971 | n/a | offsetof(staticmethod, sm_dict), /* tp_dictoffset */ |
---|
972 | n/a | sm_init, /* tp_init */ |
---|
973 | n/a | PyType_GenericAlloc, /* tp_alloc */ |
---|
974 | n/a | PyType_GenericNew, /* tp_new */ |
---|
975 | n/a | PyObject_GC_Del, /* tp_free */ |
---|
976 | n/a | }; |
---|
977 | n/a | |
---|
978 | n/a | PyObject * |
---|
979 | n/a | PyStaticMethod_New(PyObject *callable) |
---|
980 | n/a | { |
---|
981 | n/a | staticmethod *sm = (staticmethod *) |
---|
982 | n/a | PyType_GenericAlloc(&PyStaticMethod_Type, 0); |
---|
983 | n/a | if (sm != NULL) { |
---|
984 | n/a | Py_INCREF(callable); |
---|
985 | n/a | sm->sm_callable = callable; |
---|
986 | n/a | } |
---|
987 | n/a | return (PyObject *)sm; |
---|
988 | n/a | } |
---|