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

Python code coverage for Objects/classobject.c

#countcontent
1n/a/* Class object implementation (dead now except for methods) */
2n/a
3n/a#include "Python.h"
4n/a#include "structmember.h"
5n/a
6n/a#define TP_DESCR_GET(t) ((t)->tp_descr_get)
7n/a
8n/a/* Free list for method objects to safe malloc/free overhead
9n/a * The im_self element is used to chain the elements.
10n/a */
11n/astatic PyMethodObject *free_list;
12n/astatic int numfree = 0;
13n/a#ifndef PyMethod_MAXFREELIST
14n/a#define PyMethod_MAXFREELIST 256
15n/a#endif
16n/a
17n/a_Py_IDENTIFIER(__name__);
18n/a_Py_IDENTIFIER(__qualname__);
19n/a
20n/aPyObject *
21n/aPyMethod_Function(PyObject *im)
22n/a{
23n/a if (!PyMethod_Check(im)) {
24n/a PyErr_BadInternalCall();
25n/a return NULL;
26n/a }
27n/a return ((PyMethodObject *)im)->im_func;
28n/a}
29n/a
30n/aPyObject *
31n/aPyMethod_Self(PyObject *im)
32n/a{
33n/a if (!PyMethod_Check(im)) {
34n/a PyErr_BadInternalCall();
35n/a return NULL;
36n/a }
37n/a return ((PyMethodObject *)im)->im_self;
38n/a}
39n/a
40n/a/* Method objects are used for bound instance methods returned by
41n/a instancename.methodname. ClassName.methodname returns an ordinary
42n/a function.
43n/a*/
44n/a
45n/aPyObject *
46n/aPyMethod_New(PyObject *func, PyObject *self)
47n/a{
48n/a PyMethodObject *im;
49n/a if (self == NULL) {
50n/a PyErr_BadInternalCall();
51n/a return NULL;
52n/a }
53n/a im = free_list;
54n/a if (im != NULL) {
55n/a free_list = (PyMethodObject *)(im->im_self);
56n/a (void)PyObject_INIT(im, &PyMethod_Type);
57n/a numfree--;
58n/a }
59n/a else {
60n/a im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
61n/a if (im == NULL)
62n/a return NULL;
63n/a }
64n/a im->im_weakreflist = NULL;
65n/a Py_INCREF(func);
66n/a im->im_func = func;
67n/a Py_XINCREF(self);
68n/a im->im_self = self;
69n/a _PyObject_GC_TRACK(im);
70n/a return (PyObject *)im;
71n/a}
72n/a
73n/astatic PyObject *
74n/amethod_reduce(PyMethodObject *im)
75n/a{
76n/a PyObject *self = PyMethod_GET_SELF(im);
77n/a PyObject *func = PyMethod_GET_FUNCTION(im);
78n/a PyObject *builtins;
79n/a PyObject *getattr;
80n/a PyObject *funcname;
81n/a _Py_IDENTIFIER(getattr);
82n/a
83n/a funcname = _PyObject_GetAttrId(func, &PyId___name__);
84n/a if (funcname == NULL) {
85n/a return NULL;
86n/a }
87n/a builtins = PyEval_GetBuiltins();
88n/a getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
89n/a return Py_BuildValue("O(ON)", getattr, self, funcname);
90n/a}
91n/a
92n/astatic PyMethodDef method_methods[] = {
93n/a {"__reduce__", (PyCFunction)method_reduce, METH_NOARGS, NULL},
94n/a {NULL, NULL}
95n/a};
96n/a
97n/a/* Descriptors for PyMethod attributes */
98n/a
99n/a/* im_func and im_self are stored in the PyMethod object */
100n/a
101n/a#define MO_OFF(x) offsetof(PyMethodObject, x)
102n/a
103n/astatic PyMemberDef method_memberlist[] = {
104n/a {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
105n/a "the function (or other callable) implementing a method"},
106n/a {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
107n/a "the instance to which a method is bound"},
108n/a {NULL} /* Sentinel */
109n/a};
110n/a
111n/a/* Christian Tismer argued convincingly that method attributes should
112n/a (nearly) always override function attributes.
113n/a The one exception is __doc__; there's a default __doc__ which
114n/a should only be used for the class, not for instances */
115n/a
116n/astatic PyObject *
117n/amethod_get_doc(PyMethodObject *im, void *context)
118n/a{
119n/a static PyObject *docstr;
120n/a if (docstr == NULL) {
121n/a docstr= PyUnicode_InternFromString("__doc__");
122n/a if (docstr == NULL)
123n/a return NULL;
124n/a }
125n/a return PyObject_GetAttr(im->im_func, docstr);
126n/a}
127n/a
128n/astatic PyGetSetDef method_getset[] = {
129n/a {"__doc__", (getter)method_get_doc, NULL, NULL},
130n/a {0}
131n/a};
132n/a
133n/astatic PyObject *
134n/amethod_getattro(PyObject *obj, PyObject *name)
135n/a{
136n/a PyMethodObject *im = (PyMethodObject *)obj;
137n/a PyTypeObject *tp = obj->ob_type;
138n/a PyObject *descr = NULL;
139n/a
140n/a {
141n/a if (tp->tp_dict == NULL) {
142n/a if (PyType_Ready(tp) < 0)
143n/a return NULL;
144n/a }
145n/a descr = _PyType_Lookup(tp, name);
146n/a }
147n/a
148n/a if (descr != NULL) {
149n/a descrgetfunc f = TP_DESCR_GET(descr->ob_type);
150n/a if (f != NULL)
151n/a return f(descr, obj, (PyObject *)obj->ob_type);
152n/a else {
153n/a Py_INCREF(descr);
154n/a return descr;
155n/a }
156n/a }
157n/a
158n/a return PyObject_GetAttr(im->im_func, name);
159n/a}
160n/a
161n/aPyDoc_STRVAR(method_doc,
162n/a"method(function, instance)\n\
163n/a\n\
164n/aCreate a bound instance method object.");
165n/a
166n/astatic PyObject *
167n/amethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
168n/a{
169n/a PyObject *func;
170n/a PyObject *self;
171n/a
172n/a if (!_PyArg_NoKeywords("method", kw))
173n/a return NULL;
174n/a if (!PyArg_UnpackTuple(args, "method", 2, 2,
175n/a &func, &self))
176n/a return NULL;
177n/a if (!PyCallable_Check(func)) {
178n/a PyErr_SetString(PyExc_TypeError,
179n/a "first argument must be callable");
180n/a return NULL;
181n/a }
182n/a if (self == NULL || self == Py_None) {
183n/a PyErr_SetString(PyExc_TypeError,
184n/a "self must not be None");
185n/a return NULL;
186n/a }
187n/a
188n/a return PyMethod_New(func, self);
189n/a}
190n/a
191n/astatic void
192n/amethod_dealloc(PyMethodObject *im)
193n/a{
194n/a _PyObject_GC_UNTRACK(im);
195n/a if (im->im_weakreflist != NULL)
196n/a PyObject_ClearWeakRefs((PyObject *)im);
197n/a Py_DECREF(im->im_func);
198n/a Py_XDECREF(im->im_self);
199n/a if (numfree < PyMethod_MAXFREELIST) {
200n/a im->im_self = (PyObject *)free_list;
201n/a free_list = im;
202n/a numfree++;
203n/a }
204n/a else {
205n/a PyObject_GC_Del(im);
206n/a }
207n/a}
208n/a
209n/astatic PyObject *
210n/amethod_richcompare(PyObject *self, PyObject *other, int op)
211n/a{
212n/a PyMethodObject *a, *b;
213n/a PyObject *res;
214n/a int eq;
215n/a
216n/a if ((op != Py_EQ && op != Py_NE) ||
217n/a !PyMethod_Check(self) ||
218n/a !PyMethod_Check(other))
219n/a {
220n/a Py_RETURN_NOTIMPLEMENTED;
221n/a }
222n/a a = (PyMethodObject *)self;
223n/a b = (PyMethodObject *)other;
224n/a eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
225n/a if (eq == 1) {
226n/a if (a->im_self == NULL || b->im_self == NULL)
227n/a eq = a->im_self == b->im_self;
228n/a else
229n/a eq = PyObject_RichCompareBool(a->im_self, b->im_self,
230n/a Py_EQ);
231n/a }
232n/a if (eq < 0)
233n/a return NULL;
234n/a if (op == Py_EQ)
235n/a res = eq ? Py_True : Py_False;
236n/a else
237n/a res = eq ? Py_False : Py_True;
238n/a Py_INCREF(res);
239n/a return res;
240n/a}
241n/a
242n/astatic PyObject *
243n/amethod_repr(PyMethodObject *a)
244n/a{
245n/a PyObject *self = a->im_self;
246n/a PyObject *func = a->im_func;
247n/a PyObject *funcname = NULL, *result = NULL;
248n/a const char *defname = "?";
249n/a
250n/a funcname = _PyObject_GetAttrId(func, &PyId___qualname__);
251n/a if (funcname == NULL) {
252n/a if (!PyErr_ExceptionMatches(PyExc_AttributeError))
253n/a return NULL;
254n/a PyErr_Clear();
255n/a
256n/a funcname = _PyObject_GetAttrId(func, &PyId___name__);
257n/a if (funcname == NULL) {
258n/a if (!PyErr_ExceptionMatches(PyExc_AttributeError))
259n/a return NULL;
260n/a PyErr_Clear();
261n/a }
262n/a }
263n/a
264n/a if (funcname != NULL && !PyUnicode_Check(funcname)) {
265n/a Py_DECREF(funcname);
266n/a funcname = NULL;
267n/a }
268n/a
269n/a /* XXX Shouldn't use repr()/%R here! */
270n/a result = PyUnicode_FromFormat("<bound method %V of %R>",
271n/a funcname, defname, self);
272n/a
273n/a Py_XDECREF(funcname);
274n/a return result;
275n/a}
276n/a
277n/astatic Py_hash_t
278n/amethod_hash(PyMethodObject *a)
279n/a{
280n/a Py_hash_t x, y;
281n/a if (a->im_self == NULL)
282n/a x = PyObject_Hash(Py_None);
283n/a else
284n/a x = PyObject_Hash(a->im_self);
285n/a if (x == -1)
286n/a return -1;
287n/a y = PyObject_Hash(a->im_func);
288n/a if (y == -1)
289n/a return -1;
290n/a x = x ^ y;
291n/a if (x == -1)
292n/a x = -2;
293n/a return x;
294n/a}
295n/a
296n/astatic int
297n/amethod_traverse(PyMethodObject *im, visitproc visit, void *arg)
298n/a{
299n/a Py_VISIT(im->im_func);
300n/a Py_VISIT(im->im_self);
301n/a return 0;
302n/a}
303n/a
304n/astatic PyObject *
305n/amethod_call(PyObject *method, PyObject *args, PyObject *kwargs)
306n/a{
307n/a PyObject *self, *func;
308n/a
309n/a self = PyMethod_GET_SELF(method);
310n/a if (self == NULL) {
311n/a PyErr_BadInternalCall();
312n/a return NULL;
313n/a }
314n/a
315n/a func = PyMethod_GET_FUNCTION(method);
316n/a
317n/a return _PyObject_Call_Prepend(func, self, args, kwargs);
318n/a}
319n/a
320n/astatic PyObject *
321n/amethod_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
322n/a{
323n/a /* Don't rebind an already bound method of a class that's not a base
324n/a class of cls. */
325n/a if (PyMethod_GET_SELF(meth) != NULL) {
326n/a /* Already bound */
327n/a Py_INCREF(meth);
328n/a return meth;
329n/a }
330n/a /* Bind it to obj */
331n/a return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
332n/a}
333n/a
334n/aPyTypeObject PyMethod_Type = {
335n/a PyVarObject_HEAD_INIT(&PyType_Type, 0)
336n/a "method",
337n/a sizeof(PyMethodObject),
338n/a 0,
339n/a (destructor)method_dealloc, /* tp_dealloc */
340n/a 0, /* tp_print */
341n/a 0, /* tp_getattr */
342n/a 0, /* tp_setattr */
343n/a 0, /* tp_reserved */
344n/a (reprfunc)method_repr, /* tp_repr */
345n/a 0, /* tp_as_number */
346n/a 0, /* tp_as_sequence */
347n/a 0, /* tp_as_mapping */
348n/a (hashfunc)method_hash, /* tp_hash */
349n/a method_call, /* tp_call */
350n/a 0, /* tp_str */
351n/a method_getattro, /* tp_getattro */
352n/a PyObject_GenericSetAttr, /* tp_setattro */
353n/a 0, /* tp_as_buffer */
354n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
355n/a method_doc, /* tp_doc */
356n/a (traverseproc)method_traverse, /* tp_traverse */
357n/a 0, /* tp_clear */
358n/a method_richcompare, /* tp_richcompare */
359n/a offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
360n/a 0, /* tp_iter */
361n/a 0, /* tp_iternext */
362n/a method_methods, /* tp_methods */
363n/a method_memberlist, /* tp_members */
364n/a method_getset, /* tp_getset */
365n/a 0, /* tp_base */
366n/a 0, /* tp_dict */
367n/a method_descr_get, /* tp_descr_get */
368n/a 0, /* tp_descr_set */
369n/a 0, /* tp_dictoffset */
370n/a 0, /* tp_init */
371n/a 0, /* tp_alloc */
372n/a method_new, /* tp_new */
373n/a};
374n/a
375n/a/* Clear out the free list */
376n/a
377n/aint
378n/aPyMethod_ClearFreeList(void)
379n/a{
380n/a int freelist_size = numfree;
381n/a
382n/a while (free_list) {
383n/a PyMethodObject *im = free_list;
384n/a free_list = (PyMethodObject *)(im->im_self);
385n/a PyObject_GC_Del(im);
386n/a numfree--;
387n/a }
388n/a assert(numfree == 0);
389n/a return freelist_size;
390n/a}
391n/a
392n/avoid
393n/aPyMethod_Fini(void)
394n/a{
395n/a (void)PyMethod_ClearFreeList();
396n/a}
397n/a
398n/a/* Print summary info about the state of the optimized allocator */
399n/avoid
400n/a_PyMethod_DebugMallocStats(FILE *out)
401n/a{
402n/a _PyDebugAllocatorStats(out,
403n/a "free PyMethodObject",
404n/a numfree, sizeof(PyMethodObject));
405n/a}
406n/a
407n/a/* ------------------------------------------------------------------------
408n/a * instance method
409n/a */
410n/a
411n/aPyObject *
412n/aPyInstanceMethod_New(PyObject *func) {
413n/a PyInstanceMethodObject *method;
414n/a method = PyObject_GC_New(PyInstanceMethodObject,
415n/a &PyInstanceMethod_Type);
416n/a if (method == NULL) return NULL;
417n/a Py_INCREF(func);
418n/a method->func = func;
419n/a _PyObject_GC_TRACK(method);
420n/a return (PyObject *)method;
421n/a}
422n/a
423n/aPyObject *
424n/aPyInstanceMethod_Function(PyObject *im)
425n/a{
426n/a if (!PyInstanceMethod_Check(im)) {
427n/a PyErr_BadInternalCall();
428n/a return NULL;
429n/a }
430n/a return PyInstanceMethod_GET_FUNCTION(im);
431n/a}
432n/a
433n/a#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
434n/a
435n/astatic PyMemberDef instancemethod_memberlist[] = {
436n/a {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
437n/a "the function (or other callable) implementing a method"},
438n/a {NULL} /* Sentinel */
439n/a};
440n/a
441n/astatic PyObject *
442n/ainstancemethod_get_doc(PyObject *self, void *context)
443n/a{
444n/a static PyObject *docstr;
445n/a if (docstr == NULL) {
446n/a docstr = PyUnicode_InternFromString("__doc__");
447n/a if (docstr == NULL)
448n/a return NULL;
449n/a }
450n/a return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
451n/a}
452n/a
453n/astatic PyGetSetDef instancemethod_getset[] = {
454n/a {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
455n/a {0}
456n/a};
457n/a
458n/astatic PyObject *
459n/ainstancemethod_getattro(PyObject *self, PyObject *name)
460n/a{
461n/a PyTypeObject *tp = self->ob_type;
462n/a PyObject *descr = NULL;
463n/a
464n/a if (tp->tp_dict == NULL) {
465n/a if (PyType_Ready(tp) < 0)
466n/a return NULL;
467n/a }
468n/a descr = _PyType_Lookup(tp, name);
469n/a
470n/a if (descr != NULL) {
471n/a descrgetfunc f = TP_DESCR_GET(descr->ob_type);
472n/a if (f != NULL)
473n/a return f(descr, self, (PyObject *)self->ob_type);
474n/a else {
475n/a Py_INCREF(descr);
476n/a return descr;
477n/a }
478n/a }
479n/a
480n/a return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
481n/a}
482n/a
483n/astatic void
484n/ainstancemethod_dealloc(PyObject *self) {
485n/a _PyObject_GC_UNTRACK(self);
486n/a Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
487n/a PyObject_GC_Del(self);
488n/a}
489n/a
490n/astatic int
491n/ainstancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
492n/a Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
493n/a return 0;
494n/a}
495n/a
496n/astatic PyObject *
497n/ainstancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
498n/a{
499n/a return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
500n/a}
501n/a
502n/astatic PyObject *
503n/ainstancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
504n/a PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
505n/a if (obj == NULL) {
506n/a Py_INCREF(func);
507n/a return func;
508n/a }
509n/a else
510n/a return PyMethod_New(func, obj);
511n/a}
512n/a
513n/astatic PyObject *
514n/ainstancemethod_richcompare(PyObject *self, PyObject *other, int op)
515n/a{
516n/a PyInstanceMethodObject *a, *b;
517n/a PyObject *res;
518n/a int eq;
519n/a
520n/a if ((op != Py_EQ && op != Py_NE) ||
521n/a !PyInstanceMethod_Check(self) ||
522n/a !PyInstanceMethod_Check(other))
523n/a {
524n/a Py_RETURN_NOTIMPLEMENTED;
525n/a }
526n/a a = (PyInstanceMethodObject *)self;
527n/a b = (PyInstanceMethodObject *)other;
528n/a eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
529n/a if (eq < 0)
530n/a return NULL;
531n/a if (op == Py_EQ)
532n/a res = eq ? Py_True : Py_False;
533n/a else
534n/a res = eq ? Py_False : Py_True;
535n/a Py_INCREF(res);
536n/a return res;
537n/a}
538n/a
539n/astatic PyObject *
540n/ainstancemethod_repr(PyObject *self)
541n/a{
542n/a PyObject *func = PyInstanceMethod_Function(self);
543n/a PyObject *funcname = NULL , *result = NULL;
544n/a char *defname = "?";
545n/a
546n/a if (func == NULL) {
547n/a PyErr_BadInternalCall();
548n/a return NULL;
549n/a }
550n/a
551n/a funcname = _PyObject_GetAttrId(func, &PyId___name__);
552n/a if (funcname == NULL) {
553n/a if (!PyErr_ExceptionMatches(PyExc_AttributeError))
554n/a return NULL;
555n/a PyErr_Clear();
556n/a }
557n/a else if (!PyUnicode_Check(funcname)) {
558n/a Py_DECREF(funcname);
559n/a funcname = NULL;
560n/a }
561n/a
562n/a result = PyUnicode_FromFormat("<instancemethod %V at %p>",
563n/a funcname, defname, self);
564n/a
565n/a Py_XDECREF(funcname);
566n/a return result;
567n/a}
568n/a
569n/a/*
570n/astatic long
571n/ainstancemethod_hash(PyObject *self)
572n/a{
573n/a long x, y;
574n/a x = (long)self;
575n/a y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
576n/a if (y == -1)
577n/a return -1;
578n/a x = x ^ y;
579n/a if (x == -1)
580n/a x = -2;
581n/a return x;
582n/a}
583n/a*/
584n/a
585n/aPyDoc_STRVAR(instancemethod_doc,
586n/a"instancemethod(function)\n\
587n/a\n\
588n/aBind a function to a class.");
589n/a
590n/astatic PyObject *
591n/ainstancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
592n/a{
593n/a PyObject *func;
594n/a
595n/a if (!_PyArg_NoKeywords("instancemethod", kw))
596n/a return NULL;
597n/a if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
598n/a return NULL;
599n/a if (!PyCallable_Check(func)) {
600n/a PyErr_SetString(PyExc_TypeError,
601n/a "first argument must be callable");
602n/a return NULL;
603n/a }
604n/a
605n/a return PyInstanceMethod_New(func);
606n/a}
607n/a
608n/aPyTypeObject PyInstanceMethod_Type = {
609n/a PyVarObject_HEAD_INIT(&PyType_Type, 0)
610n/a "instancemethod", /* tp_name */
611n/a sizeof(PyInstanceMethodObject), /* tp_basicsize */
612n/a 0, /* tp_itemsize */
613n/a instancemethod_dealloc, /* tp_dealloc */
614n/a 0, /* tp_print */
615n/a 0, /* tp_getattr */
616n/a 0, /* tp_setattr */
617n/a 0, /* tp_reserved */
618n/a (reprfunc)instancemethod_repr, /* tp_repr */
619n/a 0, /* tp_as_number */
620n/a 0, /* tp_as_sequence */
621n/a 0, /* tp_as_mapping */
622n/a 0, /*(hashfunc)instancemethod_hash, tp_hash */
623n/a instancemethod_call, /* tp_call */
624n/a 0, /* tp_str */
625n/a instancemethod_getattro, /* tp_getattro */
626n/a PyObject_GenericSetAttr, /* tp_setattro */
627n/a 0, /* tp_as_buffer */
628n/a Py_TPFLAGS_DEFAULT
629n/a | Py_TPFLAGS_HAVE_GC, /* tp_flags */
630n/a instancemethod_doc, /* tp_doc */
631n/a instancemethod_traverse, /* tp_traverse */
632n/a 0, /* tp_clear */
633n/a instancemethod_richcompare, /* tp_richcompare */
634n/a 0, /* tp_weaklistoffset */
635n/a 0, /* tp_iter */
636n/a 0, /* tp_iternext */
637n/a 0, /* tp_methods */
638n/a instancemethod_memberlist, /* tp_members */
639n/a instancemethod_getset, /* tp_getset */
640n/a 0, /* tp_base */
641n/a 0, /* tp_dict */
642n/a instancemethod_descr_get, /* tp_descr_get */
643n/a 0, /* tp_descr_set */
644n/a 0, /* tp_dictoffset */
645n/a 0, /* tp_init */
646n/a 0, /* tp_alloc */
647n/a instancemethod_new, /* tp_new */
648n/a};