Merged revisions 73809 via svnmerge from
[python/dscho.git] / Objects / classobject.c
blobd10ab298be4429aec7f2b0803ee06ac7028a55e2
1 /* Class object implementation (dead now except for methods) */
3 #include "Python.h"
4 #include "structmember.h"
6 #define TP_DESCR_GET(t) ((t)->tp_descr_get)
8 /* Free list for method objects to safe malloc/free overhead
9 * The im_self element is used to chain the elements.
11 static PyMethodObject *free_list;
12 static int numfree = 0;
13 #ifndef PyMethod_MAXFREELIST
14 #define PyMethod_MAXFREELIST 256
15 #endif
17 PyObject *
18 PyMethod_Function(PyObject *im)
20 if (!PyMethod_Check(im)) {
21 PyErr_BadInternalCall();
22 return NULL;
24 return ((PyMethodObject *)im)->im_func;
27 PyObject *
28 PyMethod_Self(PyObject *im)
30 if (!PyMethod_Check(im)) {
31 PyErr_BadInternalCall();
32 return NULL;
34 return ((PyMethodObject *)im)->im_self;
37 /* Method objects are used for bound instance methods returned by
38 instancename.methodname. ClassName.methodname returns an ordinary
39 function.
42 PyObject *
43 PyMethod_New(PyObject *func, PyObject *self)
45 register PyMethodObject *im;
46 if (!PyCallable_Check(func)) {
47 PyErr_BadInternalCall();
48 return NULL;
50 if (self == NULL) {
51 PyErr_BadInternalCall();
52 return NULL;
54 im = free_list;
55 if (im != NULL) {
56 free_list = (PyMethodObject *)(im->im_self);
57 PyObject_INIT(im, &PyMethod_Type);
58 numfree--;
60 else {
61 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
62 if (im == NULL)
63 return NULL;
65 im->im_weakreflist = NULL;
66 Py_INCREF(func);
67 im->im_func = func;
68 Py_XINCREF(self);
69 im->im_self = self;
70 _PyObject_GC_TRACK(im);
71 return (PyObject *)im;
74 /* Descriptors for PyMethod attributes */
76 /* im_func and im_self are stored in the PyMethod object */
78 #define MO_OFF(x) offsetof(PyMethodObject, x)
80 static PyMemberDef method_memberlist[] = {
81 {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
82 "the function (or other callable) implementing a method"},
83 {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
84 "the instance to which a method is bound"},
85 {NULL} /* Sentinel */
88 /* Christian Tismer argued convincingly that method attributes should
89 (nearly) always override function attributes.
90 The one exception is __doc__; there's a default __doc__ which
91 should only be used for the class, not for instances */
93 static PyObject *
94 method_get_doc(PyMethodObject *im, void *context)
96 static PyObject *docstr;
97 if (docstr == NULL) {
98 docstr= PyUnicode_InternFromString("__doc__");
99 if (docstr == NULL)
100 return NULL;
102 return PyObject_GetAttr(im->im_func, docstr);
105 static PyGetSetDef method_getset[] = {
106 {"__doc__", (getter)method_get_doc, NULL, NULL},
110 static PyObject *
111 method_getattro(PyObject *obj, PyObject *name)
113 PyMethodObject *im = (PyMethodObject *)obj;
114 PyTypeObject *tp = obj->ob_type;
115 PyObject *descr = NULL;
118 if (tp->tp_dict == NULL) {
119 if (PyType_Ready(tp) < 0)
120 return NULL;
122 descr = _PyType_Lookup(tp, name);
125 if (descr != NULL) {
126 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
127 if (f != NULL)
128 return f(descr, obj, (PyObject *)obj->ob_type);
129 else {
130 Py_INCREF(descr);
131 return descr;
135 return PyObject_GetAttr(im->im_func, name);
138 PyDoc_STRVAR(method_doc,
139 "method(function, instance)\n\
141 Create a bound instance method object.");
143 static PyObject *
144 method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
146 PyObject *func;
147 PyObject *self;
149 if (!_PyArg_NoKeywords("method", kw))
150 return NULL;
151 if (!PyArg_UnpackTuple(args, "method", 2, 2,
152 &func, &self))
153 return NULL;
154 if (!PyCallable_Check(func)) {
155 PyErr_SetString(PyExc_TypeError,
156 "first argument must be callable");
157 return NULL;
159 if (self == NULL || self == Py_None) {
160 PyErr_SetString(PyExc_TypeError,
161 "self must not be None");
162 return NULL;
165 return PyMethod_New(func, self);
168 static void
169 method_dealloc(register PyMethodObject *im)
171 _PyObject_GC_UNTRACK(im);
172 if (im->im_weakreflist != NULL)
173 PyObject_ClearWeakRefs((PyObject *)im);
174 Py_DECREF(im->im_func);
175 Py_XDECREF(im->im_self);
176 if (numfree < PyMethod_MAXFREELIST) {
177 im->im_self = (PyObject *)free_list;
178 free_list = im;
179 numfree++;
181 else {
182 PyObject_GC_Del(im);
186 static PyObject *
187 method_richcompare(PyObject *self, PyObject *other, int op)
189 PyMethodObject *a, *b;
190 PyObject *res;
191 int eq;
193 if ((op != Py_EQ && op != Py_NE) ||
194 !PyMethod_Check(self) ||
195 !PyMethod_Check(other))
197 Py_INCREF(Py_NotImplemented);
198 return Py_NotImplemented;
200 a = (PyMethodObject *)self;
201 b = (PyMethodObject *)other;
202 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
203 if (eq == 1) {
204 if (a->im_self == NULL || b->im_self == NULL)
205 eq = a->im_self == b->im_self;
206 else
207 eq = PyObject_RichCompareBool(a->im_self, b->im_self,
208 Py_EQ);
210 if (eq < 0)
211 return NULL;
212 if (op == Py_EQ)
213 res = eq ? Py_True : Py_False;
214 else
215 res = eq ? Py_False : Py_True;
216 Py_INCREF(res);
217 return res;
220 static PyObject *
221 method_repr(PyMethodObject *a)
223 PyObject *self = a->im_self;
224 PyObject *func = a->im_func;
225 PyObject *klass = (PyObject*)Py_TYPE(self);
226 PyObject *funcname = NULL ,*klassname = NULL, *result = NULL;
227 char *defname = "?";
229 if (self == NULL) {
230 PyErr_BadInternalCall();
231 return NULL;
234 funcname = PyObject_GetAttrString(func, "__name__");
235 if (funcname == NULL) {
236 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
237 return NULL;
238 PyErr_Clear();
240 else if (!PyUnicode_Check(funcname)) {
241 Py_DECREF(funcname);
242 funcname = NULL;
245 if (klass == NULL)
246 klassname = NULL;
247 else {
248 klassname = PyObject_GetAttrString(klass, "__name__");
249 if (klassname == NULL) {
250 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
251 return NULL;
252 PyErr_Clear();
254 else if (!PyUnicode_Check(klassname)) {
255 Py_DECREF(klassname);
256 klassname = NULL;
260 /* XXX Shouldn't use repr()/%R here! */
261 result = PyUnicode_FromFormat("<bound method %V.%V of %R>",
262 klassname, defname,
263 funcname, defname, self);
265 Py_XDECREF(funcname);
266 Py_XDECREF(klassname);
267 return result;
270 static long
271 method_hash(PyMethodObject *a)
273 long x, y;
274 if (a->im_self == NULL)
275 x = PyObject_Hash(Py_None);
276 else
277 x = PyObject_Hash(a->im_self);
278 if (x == -1)
279 return -1;
280 y = PyObject_Hash(a->im_func);
281 if (y == -1)
282 return -1;
283 x = x ^ y;
284 if (x == -1)
285 x = -2;
286 return x;
289 static int
290 method_traverse(PyMethodObject *im, visitproc visit, void *arg)
292 Py_VISIT(im->im_func);
293 Py_VISIT(im->im_self);
294 return 0;
297 static PyObject *
298 method_call(PyObject *func, PyObject *arg, PyObject *kw)
300 PyObject *self = PyMethod_GET_SELF(func);
301 PyObject *result;
303 func = PyMethod_GET_FUNCTION(func);
304 if (self == NULL) {
305 PyErr_BadInternalCall();
306 return NULL;
308 else {
309 Py_ssize_t argcount = PyTuple_Size(arg);
310 PyObject *newarg = PyTuple_New(argcount + 1);
311 int i;
312 if (newarg == NULL)
313 return NULL;
314 Py_INCREF(self);
315 PyTuple_SET_ITEM(newarg, 0, self);
316 for (i = 0; i < argcount; i++) {
317 PyObject *v = PyTuple_GET_ITEM(arg, i);
318 Py_XINCREF(v);
319 PyTuple_SET_ITEM(newarg, i+1, v);
321 arg = newarg;
323 result = PyObject_Call((PyObject *)func, arg, kw);
324 Py_DECREF(arg);
325 return result;
328 static PyObject *
329 method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
331 /* Don't rebind an already bound method of a class that's not a base
332 class of cls. */
333 if (PyMethod_GET_SELF(meth) != NULL) {
334 /* Already bound */
335 Py_INCREF(meth);
336 return meth;
338 /* Bind it to obj */
339 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
342 PyTypeObject PyMethod_Type = {
343 PyVarObject_HEAD_INIT(&PyType_Type, 0)
344 "method",
345 sizeof(PyMethodObject),
347 (destructor)method_dealloc, /* tp_dealloc */
348 0, /* tp_print */
349 0, /* tp_getattr */
350 0, /* tp_setattr */
351 0, /* tp_reserved */
352 (reprfunc)method_repr, /* tp_repr */
353 0, /* tp_as_number */
354 0, /* tp_as_sequence */
355 0, /* tp_as_mapping */
356 (hashfunc)method_hash, /* tp_hash */
357 method_call, /* tp_call */
358 0, /* tp_str */
359 method_getattro, /* tp_getattro */
360 PyObject_GenericSetAttr, /* tp_setattro */
361 0, /* tp_as_buffer */
362 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
363 method_doc, /* tp_doc */
364 (traverseproc)method_traverse, /* tp_traverse */
365 0, /* tp_clear */
366 method_richcompare, /* tp_richcompare */
367 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
368 0, /* tp_iter */
369 0, /* tp_iternext */
370 0, /* tp_methods */
371 method_memberlist, /* tp_members */
372 method_getset, /* tp_getset */
373 0, /* tp_base */
374 0, /* tp_dict */
375 method_descr_get, /* tp_descr_get */
376 0, /* tp_descr_set */
377 0, /* tp_dictoffset */
378 0, /* tp_init */
379 0, /* tp_alloc */
380 method_new, /* tp_new */
383 /* Clear out the free list */
386 PyMethod_ClearFreeList(void)
388 int freelist_size = numfree;
390 while (free_list) {
391 PyMethodObject *im = free_list;
392 free_list = (PyMethodObject *)(im->im_self);
393 PyObject_GC_Del(im);
394 numfree--;
396 assert(numfree == 0);
397 return freelist_size;
400 void
401 PyMethod_Fini(void)
403 (void)PyMethod_ClearFreeList();
406 /* ------------------------------------------------------------------------
407 * instance method
410 PyObject *
411 PyInstanceMethod_New(PyObject *func) {
412 PyInstanceMethodObject *method;
413 method = PyObject_GC_New(PyInstanceMethodObject,
414 &PyInstanceMethod_Type);
415 if (method == NULL) return NULL;
416 Py_INCREF(func);
417 method->func = func;
418 _PyObject_GC_TRACK(method);
419 return (PyObject *)method;
422 PyObject *
423 PyInstanceMethod_Function(PyObject *im)
425 if (!PyInstanceMethod_Check(im)) {
426 PyErr_BadInternalCall();
427 return NULL;
429 return PyInstanceMethod_GET_FUNCTION(im);
432 #define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
434 static PyMemberDef instancemethod_memberlist[] = {
435 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
436 "the function (or other callable) implementing a method"},
437 {NULL} /* Sentinel */
440 static PyObject *
441 instancemethod_get_doc(PyObject *self, void *context)
443 static PyObject *docstr;
444 if (docstr == NULL) {
445 docstr = PyUnicode_InternFromString("__doc__");
446 if (docstr == NULL)
447 return NULL;
449 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
452 static PyGetSetDef instancemethod_getset[] = {
453 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
457 static PyObject *
458 instancemethod_getattro(PyObject *self, PyObject *name)
460 PyTypeObject *tp = self->ob_type;
461 PyObject *descr = NULL;
463 if (tp->tp_dict == NULL) {
464 if (PyType_Ready(tp) < 0)
465 return NULL;
467 descr = _PyType_Lookup(tp, name);
469 if (descr != NULL) {
470 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
471 if (f != NULL)
472 return f(descr, self, (PyObject *)self->ob_type);
473 else {
474 Py_INCREF(descr);
475 return descr;
479 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
482 static void
483 instancemethod_dealloc(PyObject *self) {
484 _PyObject_GC_UNTRACK(self);
485 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
486 PyObject_GC_Del(self);
489 static int
490 instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
491 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
492 return 0;
495 static PyObject *
496 instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
498 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
501 static PyObject *
502 instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
503 register PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
504 if (obj == NULL) {
505 Py_INCREF(func);
506 return func;
508 else
509 return PyMethod_New(func, obj);
512 static PyObject *
513 instancemethod_richcompare(PyObject *self, PyObject *other, int op)
515 PyInstanceMethodObject *a, *b;
516 PyObject *res;
517 int eq;
519 if ((op != Py_EQ && op != Py_NE) ||
520 !PyInstanceMethod_Check(self) ||
521 !PyInstanceMethod_Check(other))
523 Py_INCREF(Py_NotImplemented);
524 return Py_NotImplemented;
526 a = (PyInstanceMethodObject *)self;
527 b = (PyInstanceMethodObject *)other;
528 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
529 if (eq < 0)
530 return NULL;
531 if (op == Py_EQ)
532 res = eq ? Py_True : Py_False;
533 else
534 res = eq ? Py_False : Py_True;
535 Py_INCREF(res);
536 return res;
539 static PyObject *
540 instancemethod_repr(PyObject *self)
542 PyObject *func = PyInstanceMethod_Function(self);
543 PyObject *funcname = NULL , *result = NULL;
544 char *defname = "?";
546 if (func == NULL) {
547 PyErr_BadInternalCall();
548 return NULL;
551 funcname = PyObject_GetAttrString(func, "__name__");
552 if (funcname == NULL) {
553 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
554 return NULL;
555 PyErr_Clear();
557 else if (!PyUnicode_Check(funcname)) {
558 Py_DECREF(funcname);
559 funcname = NULL;
562 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
563 funcname, defname, self);
565 Py_XDECREF(funcname);
566 return result;
570 static long
571 instancemethod_hash(PyObject *self)
573 long x, y;
574 x = (long)self;
575 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
576 if (y == -1)
577 return -1;
578 x = x ^ y;
579 if (x == -1)
580 x = -2;
581 return x;
585 PyDoc_STRVAR(instancemethod_doc,
586 "instancemethod(function)\n\
588 Bind a function to a class.");
590 static PyObject *
591 instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
593 PyObject *func;
595 if (!_PyArg_NoKeywords("instancemethod", kw))
596 return NULL;
597 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
598 return NULL;
599 if (!PyCallable_Check(func)) {
600 PyErr_SetString(PyExc_TypeError,
601 "first argument must be callable");
602 return NULL;
605 return PyInstanceMethod_New(func);
608 PyTypeObject PyInstanceMethod_Type = {
609 PyVarObject_HEAD_INIT(&PyType_Type, 0)
610 "instancemethod", /* tp_name */
611 sizeof(PyInstanceMethodObject), /* tp_basicsize */
612 0, /* tp_itemsize */
613 instancemethod_dealloc, /* tp_dealloc */
614 0, /* tp_print */
615 0, /* tp_getattr */
616 0, /* tp_setattr */
617 0, /* tp_reserved */
618 (reprfunc)instancemethod_repr, /* tp_repr */
619 0, /* tp_as_number */
620 0, /* tp_as_sequence */
621 0, /* tp_as_mapping */
622 0, /*(hashfunc)instancemethod_hash, tp_hash */
623 instancemethod_call, /* tp_call */
624 0, /* tp_str */
625 instancemethod_getattro, /* tp_getattro */
626 PyObject_GenericSetAttr, /* tp_setattro */
627 0, /* tp_as_buffer */
628 Py_TPFLAGS_DEFAULT
629 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
630 instancemethod_doc, /* tp_doc */
631 instancemethod_traverse, /* tp_traverse */
632 0, /* tp_clear */
633 instancemethod_richcompare, /* tp_richcompare */
634 0, /* tp_weaklistoffset */
635 0, /* tp_iter */
636 0, /* tp_iternext */
637 0, /* tp_methods */
638 instancemethod_memberlist, /* tp_members */
639 instancemethod_getset, /* tp_getset */
640 0, /* tp_base */
641 0, /* tp_dict */
642 instancemethod_descr_get, /* tp_descr_get */
643 0, /* tp_descr_set */
644 0, /* tp_dictoffset */
645 0, /* tp_init */
646 0, /* tp_alloc */
647 instancemethod_new, /* tp_new */