ยปCore Development>Code coverage>Objects/funcobject.c

Python code coverage for Objects/funcobject.c

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