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