2 /* Type object implementation */
5 #include "structmember.h"
7 static struct memberlist type_members
[] = {
8 {"__name__", T_STRING
, offsetof(PyTypeObject
, tp_name
), READONLY
},
9 {"__basicsize__", T_INT
, offsetof(PyTypeObject
,tp_basicsize
),READONLY
},
10 {"__itemsize__", T_INT
, offsetof(PyTypeObject
, tp_itemsize
), READONLY
},
11 {"__flags__", T_LONG
, offsetof(PyTypeObject
, tp_flags
), READONLY
},
12 {"__doc__", T_STRING
, offsetof(PyTypeObject
, tp_doc
), READONLY
},
13 {"__weaklistoffset__", T_LONG
,
14 offsetof(PyTypeObject
, tp_weaklistoffset
), READONLY
},
15 {"__base__", T_OBJECT
, offsetof(PyTypeObject
, tp_base
), READONLY
},
16 {"__dictoffset__", T_LONG
,
17 offsetof(PyTypeObject
, tp_dictoffset
), READONLY
},
18 {"__bases__", T_OBJECT
, offsetof(PyTypeObject
, tp_bases
), READONLY
},
19 {"__mro__", T_OBJECT
, offsetof(PyTypeObject
, tp_mro
), READONLY
},
24 type_module(PyTypeObject
*type
, void *context
)
26 return PyString_FromString("__builtin__");
30 type_dict(PyTypeObject
*type
, void *context
)
32 if (type
->tp_dict
== NULL
) {
36 if (type
->tp_flags
& Py_TPFLAGS_DYNAMICTYPE
) {
37 Py_INCREF(type
->tp_dict
);
40 return PyDictProxy_New(type
->tp_dict
);
44 type_defined(PyTypeObject
*type
, void *context
)
46 if (type
->tp_defined
== NULL
) {
50 if (type
->tp_flags
& Py_TPFLAGS_DYNAMICTYPE
) {
51 Py_INCREF(type
->tp_defined
);
52 return type
->tp_defined
;
54 return PyDictProxy_New(type
->tp_defined
);
58 type_dynamic(PyTypeObject
*type
, void *context
)
62 res
= (type
->tp_flags
& Py_TPFLAGS_DYNAMICTYPE
) ? Py_True
: Py_False
;
67 struct getsetlist type_getsets
[] = {
68 {"__module__", (getter
)type_module
, NULL
, NULL
},
69 {"__dict__", (getter
)type_dict
, NULL
, NULL
},
70 {"__defined__", (getter
)type_defined
, NULL
, NULL
},
71 {"__dynamic__", (getter
)type_dynamic
, NULL
, NULL
},
76 type_compare(PyObject
*v
, PyObject
*w
)
78 /* This is called with type objects only. So we
79 can just compare the addresses. */
80 Py_uintptr_t vv
= (Py_uintptr_t
)v
;
81 Py_uintptr_t ww
= (Py_uintptr_t
)w
;
82 return (vv
< ww
) ? -1 : (vv
> ww
) ? 1 : 0;
86 type_repr(PyTypeObject
*type
)
89 sprintf(buf
, "<type '%.80s'>", type
->tp_name
);
90 return PyString_FromString(buf
);
94 type_call(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
98 if (type
->tp_new
== NULL
) {
99 PyErr_Format(PyExc_TypeError
,
100 "cannot create '%.100s' instances",
105 obj
= type
->tp_new(type
, args
, NULL
);
108 if (type
->tp_init
!= NULL
&&
109 type
->tp_init(obj
, args
, kwds
) < 0) {
118 PyType_GenericAlloc(PyTypeObject
*type
, int nitems
)
124 /* Inline PyObject_New() so we can zero the memory */
125 size
= _PyObject_VAR_SIZE(type
, nitems
);
126 mem
= PyObject_MALLOC(size
);
128 return PyErr_NoMemory();
129 memset(mem
, '\0', size
);
130 if (PyType_IS_GC(type
))
131 obj
= PyObject_FROM_GC(mem
);
133 obj
= (PyObject
*)mem
;
134 if (type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)
136 if (type
->tp_itemsize
== 0)
137 PyObject_INIT(obj
, type
);
139 (void) PyObject_INIT_VAR((PyVarObject
*)obj
, type
, nitems
);
140 if (PyType_IS_GC(type
))
141 PyObject_GC_Init(obj
);
146 PyType_GenericNew(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
148 return type
->tp_alloc(type
, 0);
151 /* Helper for subtyping */
154 subtype_dealloc(PyObject
*self
)
156 int dictoffset
= self
->ob_type
->tp_dictoffset
;
157 PyTypeObject
*type
, *base
;
160 /* This exists so we can DECREF self->ob_type */
162 /* Find the nearest base with a different tp_dealloc */
163 type
= self
->ob_type
;
164 base
= type
->tp_base
;
165 while ((f
= base
->tp_dealloc
) == subtype_dealloc
) {
166 base
= base
->tp_base
;
170 /* If we added a dict, DECREF it */
171 if (dictoffset
&& !base
->tp_dictoffset
) {
172 PyObject
**dictptr
= (PyObject
**) ((char *)self
+ dictoffset
);
173 PyObject
*dict
= *dictptr
;
180 /* Finalize GC if the base doesn't do GC and we do */
181 if (PyType_IS_GC(type
) && !PyType_IS_GC(base
))
182 PyObject_GC_Fini(self
);
184 /* Call the base tp_dealloc() */
188 /* Can't reference self beyond this point */
189 if (type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
) {
194 staticforward
void override_slots(PyTypeObject
*type
, PyObject
*dict
);
195 staticforward PyTypeObject
*solid_base(PyTypeObject
*type
);
199 PyNumberMethods as_number
;
200 PySequenceMethods as_sequence
;
201 PyMappingMethods as_mapping
;
202 PyBufferProcs as_buffer
;
203 PyObject
*name
, *slots
;
204 struct memberlist members
[1];
207 /* type test with subclassing support */
210 PyType_IsSubtype(PyTypeObject
*a
, PyTypeObject
*b
)
216 /* Deal with multiple inheritance without recursion
217 by walking the MRO tuple */
219 assert(PyTuple_Check(mro
));
220 n
= PyTuple_GET_SIZE(mro
);
221 for (i
= 0; i
< n
; i
++) {
222 if (PyTuple_GET_ITEM(mro
, i
) == (PyObject
*)b
)
228 /* a is not completely initilized yet; follow tp_base */
234 return b
== &PyBaseObject_Type
;
238 /* Method resolution order algorithm from "Putting Metaclasses to Work"
239 by Forman and Danforth (Addison-Wesley 1999). */
242 conservative_merge(PyObject
*left
, PyObject
*right
)
249 assert(PyList_Check(left
));
250 assert(PyList_Check(right
));
253 left_size
= PyList_GET_SIZE(left
);
254 right_size
= PyList_GET_SIZE(right
);
255 for (i
= 0; i
< left_size
; i
++) {
256 for (j
= 0; j
< right_size
; j
++) {
257 if (PyList_GET_ITEM(left
, i
) ==
258 PyList_GET_ITEM(right
, j
)) {
259 /* found a merge point */
260 temp
= PyList_New(0);
263 for (r
= 0; r
< j
; r
++) {
264 rr
= PyList_GET_ITEM(right
, r
);
265 ok
= PySequence_Contains(left
, rr
);
271 ok
= PyList_Append(temp
, rr
);
278 ok
= PyList_SetSlice(left
, i
, i
, temp
);
282 ok
= PyList_SetSlice(right
, 0, j
+1, NULL
);
289 return PyList_SetSlice(left
, left_size
, left_size
, right
);
293 serious_order_disagreements(PyObject
*left
, PyObject
*right
)
295 return 0; /* XXX later -- for now, we cheat: "don't do that" */
299 mro_implementation(PyTypeObject
*type
)
302 PyObject
*bases
, *result
;
304 bases
= type
->tp_bases
;
305 n
= PyTuple_GET_SIZE(bases
);
306 result
= Py_BuildValue("[O]", (PyObject
*)type
);
309 for (i
= 0; i
< n
; i
++) {
311 (PyTypeObject
*) PyTuple_GET_ITEM(bases
, i
);
312 PyObject
*parentMRO
= PySequence_List(base
->tp_mro
);
313 if (parentMRO
== NULL
) {
317 if (serious_order_disagreements(result
, parentMRO
)) {
321 ok
= conservative_merge(result
, parentMRO
);
322 Py_DECREF(parentMRO
);
332 mro_external(PyObject
*self
, PyObject
*args
)
334 PyTypeObject
*type
= (PyTypeObject
*)self
;
336 if (!PyArg_ParseTuple(args
, ""))
338 return mro_implementation(type
);
342 mro_internal(PyTypeObject
*type
)
344 PyObject
*mro
, *result
, *tuple
;
346 if (type
->ob_type
== &PyType_Type
) {
347 result
= mro_implementation(type
);
350 mro
= PyObject_GetAttrString((PyObject
*)type
, "mro");
353 result
= PyObject_CallObject(mro
, NULL
);
358 tuple
= PySequence_Tuple(result
);
360 type
->tp_mro
= tuple
;
365 /* Calculate the best base amongst multiple base classes.
366 This is the first one that's on the path to the "solid base". */
368 static PyTypeObject
*
369 best_base(PyObject
*bases
)
372 PyTypeObject
*base
, *winner
, *candidate
, *base_i
;
374 assert(PyTuple_Check(bases
));
375 n
= PyTuple_GET_SIZE(bases
);
377 base
= (PyTypeObject
*)PyTuple_GET_ITEM(bases
, 0);
378 winner
= &PyBaseObject_Type
;
379 for (i
= 0; i
< n
; i
++) {
380 base_i
= (PyTypeObject
*)PyTuple_GET_ITEM(bases
, i
);
381 if (!PyType_Check((PyObject
*)base_i
)) {
384 "bases must be types");
387 if (base_i
->tp_dict
== NULL
) {
388 if (PyType_Ready(base_i
) < 0)
391 candidate
= solid_base(base_i
);
392 if (PyType_IsSubtype(winner
, candidate
))
394 else if (PyType_IsSubtype(candidate
, winner
)) {
401 "multiple bases have "
402 "instance lay-out conflict");
406 assert(base
!= NULL
);
411 extra_ivars(PyTypeObject
*type
, PyTypeObject
*base
)
413 int t_size
= PyType_BASICSIZE(type
);
414 int b_size
= PyType_BASICSIZE(base
);
416 assert(t_size
>= b_size
); /* type smaller than base! */
417 if (type
->tp_itemsize
|| base
->tp_itemsize
) {
418 /* If itemsize is involved, stricter rules */
419 return t_size
!= b_size
||
420 type
->tp_itemsize
!= base
->tp_itemsize
;
422 if (t_size
== b_size
)
424 if (type
->tp_dictoffset
!= 0 && base
->tp_dictoffset
== 0 &&
425 type
->tp_dictoffset
== b_size
&&
426 (size_t)t_size
== b_size
+ sizeof(PyObject
*))
427 return 0; /* "Forgive" adding a __dict__ only */
431 static PyTypeObject
*
432 solid_base(PyTypeObject
*type
)
437 base
= solid_base(type
->tp_base
);
439 base
= &PyBaseObject_Type
;
440 if (extra_ivars(type
, base
))
446 staticforward
void object_dealloc(PyObject
*);
447 staticforward
int object_init(PyObject
*, PyObject
*, PyObject
*);
450 type_new(PyTypeObject
*metatype
, PyObject
*args
, PyObject
*kwds
)
452 PyObject
*name
, *bases
, *dict
;
453 static char *kwlist
[] = {"name", "bases", "dict", 0};
454 PyObject
*slots
, *tmp
;
455 PyTypeObject
*type
, *base
, *tmptype
;
457 struct memberlist
*mp
;
458 int i
, nbases
, nslots
, slotoffset
, dynamic
;
460 if (metatype
== &PyType_Type
&&
461 PyTuple_Check(args
) && PyTuple_GET_SIZE(args
) == 1 &&
462 (kwds
== NULL
|| (PyDict_Check(kwds
) && PyDict_Size(kwds
) == 0))) {
463 /* type(x) -> x.__class__ */
464 PyObject
*x
= PyTuple_GET_ITEM(args
, 0);
465 Py_INCREF(x
->ob_type
);
466 return (PyObject
*) x
->ob_type
;
469 /* Check arguments */
470 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "SO!O!:type", kwlist
,
472 &PyTuple_Type
, &bases
,
473 &PyDict_Type
, &dict
))
476 /* Determine the proper metatype to deal with this,
477 and check for metatype conflicts while we're at it.
478 Note that if some other metatype wins to contract,
479 it's possible that its instances are not types. */
480 nbases
= PyTuple_GET_SIZE(bases
);
481 for (i
= 0; i
< nbases
; i
++) {
482 tmp
= PyTuple_GET_ITEM(bases
, i
);
483 tmptype
= tmp
->ob_type
;
484 if (PyType_IsSubtype(metatype
, tmptype
))
486 if (PyType_IsSubtype(tmptype
, metatype
)) {
490 PyErr_SetString(PyExc_TypeError
,
491 "metatype conflict among bases");
494 if (metatype
->tp_new
!= type_new
) /* Pass it to the winner */
495 return metatype
->tp_new(metatype
, args
, kwds
);
497 /* Adjust for empty tuple bases */
499 bases
= Py_BuildValue("(O)", &PyBaseObject_Type
);
507 /* XXX From here until type is allocated, "return NULL" leaks bases! */
509 /* Calculate best base, and check that all bases are type objects */
510 base
= best_base(bases
);
513 if (!PyType_HasFeature(base
, Py_TPFLAGS_BASETYPE
)) {
514 PyErr_Format(PyExc_TypeError
,
515 "type '%.100s' is not an acceptable base type",
520 /* Should this be a dynamic class (i.e. modifiable __dict__)? */
521 tmp
= PyDict_GetItemString(dict
, "__dynamic__");
523 /* The class author has a preference */
524 dynamic
= PyObject_IsTrue(tmp
);
530 /* Make a new class dynamic if any of its bases is dynamic.
531 This is not always the same as inheriting the __dynamic__
534 for (i
= 0; i
< nbases
; i
++) {
535 tmptype
= (PyTypeObject
*)PyTuple_GET_ITEM(bases
, i
);
536 if (tmptype
->tp_flags
& Py_TPFLAGS_DYNAMICTYPE
) {
543 /* Check for a __slots__ sequence variable in dict, and count it */
544 slots
= PyDict_GetItemString(dict
, "__slots__");
547 /* Make it into a tuple */
548 if (PyString_Check(slots
))
549 slots
= Py_BuildValue("(O)", slots
);
551 slots
= PySequence_Tuple(slots
);
554 nslots
= PyTuple_GET_SIZE(slots
);
555 for (i
= 0; i
< nslots
; i
++) {
556 if (!PyString_Check(PyTuple_GET_ITEM(slots
, i
))) {
557 PyErr_SetString(PyExc_TypeError
,
558 "__slots__ must be a sequence of strings");
564 if (slots
== NULL
&& base
->tp_dictoffset
== 0 &&
565 (base
->tp_setattro
== PyObject_GenericSetAttr
||
566 base
->tp_setattro
== NULL
))
569 /* XXX From here until type is safely allocated,
570 "return NULL" may leak slots! */
572 /* Allocate the type object */
573 type
= (PyTypeObject
*)metatype
->tp_alloc(metatype
, nslots
);
577 /* Keep name and slots alive in the extended type object */
583 /* Initialize tp_flags */
584 type
->tp_flags
= Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HEAPTYPE
|
587 type
->tp_flags
|= Py_TPFLAGS_DYNAMICTYPE
;
589 /* It's a new-style number unless it specifically inherits any
590 old-style numeric behavior */
591 if ((base
->tp_flags
& Py_TPFLAGS_CHECKTYPES
) ||
592 (base
->tp_as_number
== NULL
))
593 type
->tp_flags
|= Py_TPFLAGS_CHECKTYPES
;
595 /* Initialize essential fields */
596 type
->tp_as_number
= &et
->as_number
;
597 type
->tp_as_sequence
= &et
->as_sequence
;
598 type
->tp_as_mapping
= &et
->as_mapping
;
599 type
->tp_as_buffer
= &et
->as_buffer
;
600 type
->tp_name
= PyString_AS_STRING(name
);
602 /* Set tp_base and tp_bases */
603 type
->tp_bases
= bases
;
605 type
->tp_base
= base
;
607 /* Initialize tp_defined from passed-in dict */
608 type
->tp_defined
= dict
= PyDict_Copy(dict
);
614 /* Special-case __new__: if it's a plain function,
615 make it a static function */
616 tmp
= PyDict_GetItemString(dict
, "__new__");
617 if (tmp
!= NULL
&& PyFunction_Check(tmp
)) {
618 tmp
= PyStaticMethod_New(tmp
);
623 PyDict_SetItemString(dict
, "__new__", tmp
);
627 /* Add descriptors for custom slots from __slots__, or for __dict__ */
629 slotoffset
= PyType_BASICSIZE(base
);
631 for (i
= 0; i
< nslots
; i
++, mp
++) {
632 mp
->name
= PyString_AS_STRING(
633 PyTuple_GET_ITEM(slots
, i
));
635 mp
->offset
= slotoffset
;
636 slotoffset
+= sizeof(PyObject
*);
640 type
->tp_dictoffset
= slotoffset
;
641 mp
->name
= "__dict__";
643 mp
->offset
= slotoffset
;
645 slotoffset
+= sizeof(PyObject
*);
647 type
->tp_basicsize
= slotoffset
;
648 type
->tp_members
= et
->members
;
650 /* Special case some slots */
651 if (type
->tp_dictoffset
!= 0 || nslots
> 0) {
652 if (base
->tp_getattr
== NULL
&& base
->tp_getattro
== NULL
)
653 type
->tp_getattro
= PyObject_GenericGetAttr
;
654 if (base
->tp_setattr
== NULL
&& base
->tp_setattro
== NULL
)
655 type
->tp_setattro
= PyObject_GenericSetAttr
;
657 type
->tp_dealloc
= subtype_dealloc
;
659 /* Always override allocation strategy to use regular heap */
660 type
->tp_alloc
= PyType_GenericAlloc
;
661 type
->tp_free
= _PyObject_Del
;
663 /* Initialize the rest */
664 if (PyType_Ready(type
) < 0) {
669 /* Override slots that deserve it */
670 override_slots(type
, type
->tp_defined
);
672 return (PyObject
*)type
;
675 /* Internal API to look for a name through the MRO.
676 This returns a borrowed reference, and doesn't set an exception! */
678 _PyType_Lookup(PyTypeObject
*type
, PyObject
*name
)
681 PyObject
*mro
, *res
, *dict
;
683 /* For static types, look in tp_dict */
684 if (!(type
->tp_flags
& Py_TPFLAGS_DYNAMICTYPE
)) {
685 dict
= type
->tp_dict
;
686 assert(dict
&& PyDict_Check(dict
));
687 return PyDict_GetItem(dict
, name
);
690 /* For dynamic types, look in tp_defined of types in MRO */
692 assert(PyTuple_Check(mro
));
693 n
= PyTuple_GET_SIZE(mro
);
694 for (i
= 0; i
< n
; i
++) {
695 type
= (PyTypeObject
*) PyTuple_GET_ITEM(mro
, i
);
696 assert(PyType_Check(type
));
697 dict
= type
->tp_defined
;
698 assert(dict
&& PyDict_Check(dict
));
699 res
= PyDict_GetItem(dict
, name
);
706 /* This is similar to PyObject_GenericGetAttr(),
707 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
709 type_getattro(PyTypeObject
*type
, PyObject
*name
)
711 PyTypeObject
*metatype
= type
->ob_type
;
712 PyObject
*descr
, *res
;
715 /* Initialize this type (we'll assume the metatype is initialized) */
716 if (type
->tp_dict
== NULL
) {
717 if (PyType_Ready(type
) < 0)
721 /* Get a descriptor from the metatype */
722 descr
= _PyType_Lookup(metatype
, name
);
725 f
= descr
->ob_type
->tp_descr_get
;
726 if (f
!= NULL
&& PyDescr_IsData(descr
))
728 (PyObject
*)type
, (PyObject
*)metatype
);
731 /* Look in tp_defined of this type and its bases */
732 res
= _PyType_Lookup(type
, name
);
734 f
= res
->ob_type
->tp_descr_get
;
736 return f(res
, (PyObject
*)NULL
, (PyObject
*)type
);
741 /* Use the descriptor from the metatype */
743 res
= f(descr
, (PyObject
*)type
, (PyObject
*)metatype
);
752 PyErr_Format(PyExc_AttributeError
,
753 "type object '%.50s' has no attribute '%.400s'",
754 type
->tp_name
, PyString_AS_STRING(name
));
759 type_setattro(PyTypeObject
*type
, PyObject
*name
, PyObject
*value
)
761 if (type
->tp_flags
& Py_TPFLAGS_DYNAMICTYPE
)
762 return PyObject_GenericSetAttr((PyObject
*)type
, name
, value
);
763 PyErr_SetString(PyExc_TypeError
, "can't set type attributes");
768 type_dealloc(PyTypeObject
*type
)
772 /* Assert this is a heap-allocated type object */
773 assert(type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
);
775 Py_XDECREF(type
->tp_base
);
776 Py_XDECREF(type
->tp_dict
);
777 Py_XDECREF(type
->tp_bases
);
778 Py_XDECREF(type
->tp_mro
);
779 Py_XDECREF(type
->tp_defined
);
781 Py_XDECREF(et
->name
);
782 Py_XDECREF(et
->slots
);
783 type
->ob_type
->tp_free((PyObject
*)type
);
786 static PyMethodDef type_methods
[] = {
787 {"mro", mro_external
, METH_VARARGS
,
788 "mro() -> list\nreturn a type's method resolution order"},
792 static char type_doc
[] =
793 "type(object) -> the object's type\n"
794 "type(name, bases, dict) -> a new type";
796 PyTypeObject PyType_Type
= {
797 PyObject_HEAD_INIT(&PyType_Type
)
799 "type", /* tp_name */
800 sizeof(etype
), /* tp_basicsize */
801 sizeof(struct memberlist
), /* tp_itemsize */
802 (destructor
)type_dealloc
, /* tp_dealloc */
806 type_compare
, /* tp_compare */
807 (reprfunc
)type_repr
, /* tp_repr */
808 0, /* tp_as_number */
809 0, /* tp_as_sequence */
810 0, /* tp_as_mapping */
811 (hashfunc
)_Py_HashPointer
, /* tp_hash */
812 (ternaryfunc
)type_call
, /* tp_call */
814 (getattrofunc
)type_getattro
, /* tp_getattro */
815 (setattrofunc
)type_setattro
, /* tp_setattro */
816 0, /* tp_as_buffer */
817 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
818 type_doc
, /* tp_doc */
821 0, /* tp_richcompare */
822 0, /* tp_weaklistoffset */
825 type_methods
, /* tp_methods */
826 type_members
, /* tp_members */
827 type_getsets
, /* tp_getset */
830 0, /* tp_descr_get */
831 0, /* tp_descr_set */
832 offsetof(PyTypeObject
, tp_dict
), /* tp_dictoffset */
835 type_new
, /* tp_new */
839 /* The base type of all types (eventually)... except itself. */
842 object_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
848 object_dealloc(PyObject
*self
)
850 self
->ob_type
->tp_free(self
);
854 object_free(PyObject
*self
)
859 static struct memberlist object_members
[] = {
860 {"__class__", T_OBJECT
, offsetof(PyObject
, ob_type
), READONLY
},
864 PyTypeObject PyBaseObject_Type
= {
865 PyObject_HEAD_INIT(&PyType_Type
)
867 "object", /* tp_name */
868 sizeof(PyObject
), /* tp_basicsize */
870 (destructor
)object_dealloc
, /* tp_dealloc */
876 0, /* tp_as_number */
877 0, /* tp_as_sequence */
878 0, /* tp_as_mapping */
882 PyObject_GenericGetAttr
, /* tp_getattro */
883 PyObject_GenericSetAttr
, /* tp_setattro */
884 0, /* tp_as_buffer */
885 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
886 "The most base type", /* tp_doc */
889 0, /* tp_richcompare */
890 0, /* tp_weaklistoffset */
894 object_members
, /* tp_members */
898 0, /* tp_descr_get */
899 0, /* tp_descr_set */
900 0, /* tp_dictoffset */
901 object_init
, /* tp_init */
902 PyType_GenericAlloc
, /* tp_alloc */
903 PyType_GenericNew
, /* tp_new */
904 object_free
, /* tp_free */
908 /* Initialize the __dict__ in a type object */
911 add_methods(PyTypeObject
*type
, PyMethodDef
*meth
)
913 PyObject
*dict
= type
->tp_defined
;
915 for (; meth
->ml_name
!= NULL
; meth
++) {
917 if (PyDict_GetItemString(dict
, meth
->ml_name
))
919 descr
= PyDescr_NewMethod(type
, meth
);
922 if (PyDict_SetItemString(dict
,meth
->ml_name
,descr
) < 0)
930 add_members(PyTypeObject
*type
, struct memberlist
*memb
)
932 PyObject
*dict
= type
->tp_defined
;
934 for (; memb
->name
!= NULL
; memb
++) {
936 if (PyDict_GetItemString(dict
, memb
->name
))
938 descr
= PyDescr_NewMember(type
, memb
);
941 if (PyDict_SetItemString(dict
, memb
->name
, descr
) < 0)
949 add_getset(PyTypeObject
*type
, struct getsetlist
*gsp
)
951 PyObject
*dict
= type
->tp_defined
;
953 for (; gsp
->name
!= NULL
; gsp
++) {
955 if (PyDict_GetItemString(dict
, gsp
->name
))
957 descr
= PyDescr_NewGetSet(type
, gsp
);
961 if (PyDict_SetItemString(dict
, gsp
->name
, descr
) < 0)
969 inherit_special(PyTypeObject
*type
, PyTypeObject
*base
)
971 int oldsize
, newsize
;
973 /* Special flag magic */
974 if (!type
->tp_as_buffer
&& base
->tp_as_buffer
) {
975 type
->tp_flags
&= ~Py_TPFLAGS_HAVE_GETCHARBUFFER
;
977 base
->tp_flags
& Py_TPFLAGS_HAVE_GETCHARBUFFER
;
979 if (!type
->tp_as_sequence
&& base
->tp_as_sequence
) {
980 type
->tp_flags
&= ~Py_TPFLAGS_HAVE_SEQUENCE_IN
;
981 type
->tp_flags
|= base
->tp_flags
& Py_TPFLAGS_HAVE_SEQUENCE_IN
;
983 if ((type
->tp_flags
& Py_TPFLAGS_HAVE_INPLACEOPS
) !=
984 (base
->tp_flags
& Py_TPFLAGS_HAVE_INPLACEOPS
)) {
985 if ((!type
->tp_as_number
&& base
->tp_as_number
) ||
986 (!type
->tp_as_sequence
&& base
->tp_as_sequence
)) {
987 type
->tp_flags
&= ~Py_TPFLAGS_HAVE_INPLACEOPS
;
988 if (!type
->tp_as_number
&& !type
->tp_as_sequence
) {
989 type
->tp_flags
|= base
->tp_flags
&
990 Py_TPFLAGS_HAVE_INPLACEOPS
;
995 if (!type
->tp_as_number
&& base
->tp_as_number
) {
996 type
->tp_flags
&= ~Py_TPFLAGS_CHECKTYPES
;
997 type
->tp_flags
|= base
->tp_flags
& Py_TPFLAGS_CHECKTYPES
;
1000 /* Copying basicsize is connected to the GC flags */
1001 oldsize
= PyType_BASICSIZE(base
);
1002 newsize
= type
->tp_basicsize
? PyType_BASICSIZE(type
) : oldsize
;
1003 if (!(type
->tp_flags
& Py_TPFLAGS_GC
) &&
1004 (base
->tp_flags
& Py_TPFLAGS_GC
) &&
1005 (type
->tp_flags
& Py_TPFLAGS_HAVE_RICHCOMPARE
/*GC slots exist*/) &&
1006 (!type
->tp_traverse
&& !type
->tp_clear
)) {
1007 type
->tp_flags
|= Py_TPFLAGS_GC
;
1008 if (type
->tp_traverse
== NULL
)
1009 type
->tp_traverse
= base
->tp_traverse
;
1010 if (type
->tp_clear
== NULL
)
1011 type
->tp_clear
= base
->tp_clear
;
1013 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_CLASS
) {
1014 if (base
!= &PyBaseObject_Type
||
1015 (type
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)) {
1016 if (type
->tp_new
== NULL
)
1017 type
->tp_new
= base
->tp_new
;
1020 PyType_SET_BASICSIZE(type
, newsize
);
1024 inherit_slots(PyTypeObject
*type
, PyTypeObject
*base
)
1026 PyTypeObject
*basebase
;
1034 #define SLOTDEFINED(SLOT) \
1035 (base->SLOT != 0 && \
1036 (basebase == NULL || base->SLOT != basebase->SLOT))
1038 #define COPYSLOT(SLOT) \
1039 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
1041 #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
1042 #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
1043 #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
1045 /* This won't inherit indirect slots (from tp_as_number etc.)
1046 if type doesn't provide the space. */
1048 if (type
->tp_as_number
!= NULL
&& base
->tp_as_number
!= NULL
) {
1049 basebase
= base
->tp_base
;
1050 if (basebase
->tp_as_number
== NULL
)
1053 COPYNUM(nb_subtract
);
1054 COPYNUM(nb_multiply
);
1056 COPYNUM(nb_remainder
);
1059 COPYNUM(nb_negative
);
1060 COPYNUM(nb_positive
);
1061 COPYNUM(nb_absolute
);
1062 COPYNUM(nb_nonzero
);
1075 COPYNUM(nb_inplace_add
);
1076 COPYNUM(nb_inplace_subtract
);
1077 COPYNUM(nb_inplace_multiply
);
1078 COPYNUM(nb_inplace_divide
);
1079 COPYNUM(nb_inplace_remainder
);
1080 COPYNUM(nb_inplace_power
);
1081 COPYNUM(nb_inplace_lshift
);
1082 COPYNUM(nb_inplace_rshift
);
1083 COPYNUM(nb_inplace_and
);
1084 COPYNUM(nb_inplace_xor
);
1085 COPYNUM(nb_inplace_or
);
1086 if (base
->tp_flags
& Py_TPFLAGS_CHECKTYPES
) {
1087 COPYNUM(nb_true_divide
);
1088 COPYNUM(nb_floor_divide
);
1089 COPYNUM(nb_inplace_true_divide
);
1090 COPYNUM(nb_inplace_floor_divide
);
1094 if (type
->tp_as_sequence
!= NULL
&& base
->tp_as_sequence
!= NULL
) {
1095 basebase
= base
->tp_base
;
1096 if (basebase
->tp_as_sequence
== NULL
)
1103 COPYSEQ(sq_ass_item
);
1104 COPYSEQ(sq_ass_slice
);
1105 COPYSEQ(sq_contains
);
1106 COPYSEQ(sq_inplace_concat
);
1107 COPYSEQ(sq_inplace_repeat
);
1110 if (type
->tp_as_mapping
!= NULL
&& base
->tp_as_mapping
!= NULL
) {
1111 basebase
= base
->tp_base
;
1112 if (basebase
->tp_as_mapping
== NULL
)
1115 COPYMAP(mp_subscript
);
1116 COPYMAP(mp_ass_subscript
);
1119 basebase
= base
->tp_base
;
1121 COPYSLOT(tp_itemsize
);
1122 COPYSLOT(tp_dealloc
);
1124 if (type
->tp_getattr
== NULL
&& type
->tp_getattro
== NULL
) {
1125 type
->tp_getattr
= base
->tp_getattr
;
1126 type
->tp_getattro
= base
->tp_getattro
;
1128 if (type
->tp_setattr
== NULL
&& type
->tp_setattro
== NULL
) {
1129 type
->tp_setattr
= base
->tp_setattr
;
1130 type
->tp_setattro
= base
->tp_setattro
;
1132 /* tp_compare see tp_richcompare */
1137 COPYSLOT(tp_as_buffer
);
1139 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_RICHCOMPARE
) {
1140 if (type
->tp_compare
== NULL
&& type
->tp_richcompare
== NULL
) {
1141 type
->tp_compare
= base
->tp_compare
;
1142 type
->tp_richcompare
= base
->tp_richcompare
;
1146 COPYSLOT(tp_compare
);
1148 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_WEAKREFS
) {
1149 COPYSLOT(tp_weaklistoffset
);
1151 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_ITER
) {
1153 COPYSLOT(tp_iternext
);
1155 if (type
->tp_flags
& base
->tp_flags
& Py_TPFLAGS_HAVE_CLASS
) {
1156 COPYSLOT(tp_descr_get
);
1157 COPYSLOT(tp_descr_set
);
1158 COPYSLOT(tp_dictoffset
);
1165 staticforward
int add_operators(PyTypeObject
*);
1168 PyType_Ready(PyTypeObject
*type
)
1170 PyObject
*dict
, *bases
, *x
;
1174 if (type
->tp_flags
& Py_TPFLAGS_READY
) {
1175 assert(type
->tp_dict
!= NULL
);
1178 assert((type
->tp_flags
& Py_TPFLAGS_READYING
) == 0);
1179 assert(type
->tp_dict
== NULL
);
1181 type
->tp_flags
|= Py_TPFLAGS_READYING
;
1183 /* Initialize tp_base (defaults to BaseObject unless that's us) */
1184 base
= type
->tp_base
;
1185 if (base
== NULL
&& type
!= &PyBaseObject_Type
)
1186 base
= type
->tp_base
= &PyBaseObject_Type
;
1188 /* Initialize tp_bases */
1189 bases
= type
->tp_bases
;
1190 if (bases
== NULL
) {
1192 bases
= PyTuple_New(0);
1194 bases
= Py_BuildValue("(O)", base
);
1197 type
->tp_bases
= bases
;
1200 /* Initialize the base class */
1201 if (base
&& base
->tp_dict
== NULL
) {
1202 if (PyType_Ready(base
) < 0)
1206 /* Initialize tp_defined */
1207 dict
= type
->tp_defined
;
1209 dict
= PyDict_New();
1212 type
->tp_defined
= dict
;
1215 /* Add type-specific descriptors to tp_defined */
1216 if (add_operators(type
) < 0)
1218 if (type
->tp_methods
!= NULL
) {
1219 if (add_methods(type
, type
->tp_methods
) < 0)
1222 if (type
->tp_members
!= NULL
) {
1223 if (add_members(type
, type
->tp_members
) < 0)
1226 if (type
->tp_getset
!= NULL
) {
1227 if (add_getset(type
, type
->tp_getset
) < 0)
1231 /* Temporarily make tp_dict the same object as tp_defined.
1232 (This is needed to call mro(), and can stay this way for
1234 Py_INCREF(type
->tp_defined
);
1235 type
->tp_dict
= type
->tp_defined
;
1237 /* Calculate method resolution order */
1238 if (mro_internal(type
) < 0) {
1242 /* Inherit special flags from dominant base */
1243 if (type
->tp_base
!= NULL
)
1244 inherit_special(type
, type
->tp_base
);
1246 /* Initialize tp_dict properly */
1247 if (!PyType_HasFeature(type
, Py_TPFLAGS_DYNAMICTYPE
)) {
1248 /* For a static type, tp_dict is the consolidation
1249 of the tp_defined of its bases in MRO. */
1250 Py_DECREF(type
->tp_dict
);
1251 type
->tp_dict
= PyDict_Copy(type
->tp_defined
);
1252 if (type
->tp_dict
== NULL
)
1254 bases
= type
->tp_mro
;
1255 assert(bases
!= NULL
);
1256 assert(PyTuple_Check(bases
));
1257 n
= PyTuple_GET_SIZE(bases
);
1258 for (i
= 1; i
< n
; i
++) {
1259 base
= (PyTypeObject
*)PyTuple_GET_ITEM(bases
, i
);
1260 assert(PyType_Check(base
));
1261 x
= base
->tp_defined
;
1262 if (x
!= NULL
&& PyDict_Merge(type
->tp_dict
, x
, 0) < 0)
1264 inherit_slots(type
, base
);
1268 /* Some more special stuff */
1269 base
= type
->tp_base
;
1271 if (type
->tp_as_number
== NULL
)
1272 type
->tp_as_number
= base
->tp_as_number
;
1273 if (type
->tp_as_sequence
== NULL
)
1274 type
->tp_as_sequence
= base
->tp_as_sequence
;
1275 if (type
->tp_as_mapping
== NULL
)
1276 type
->tp_as_mapping
= base
->tp_as_mapping
;
1279 /* All done -- set the ready flag */
1280 assert(type
->tp_dict
!= NULL
);
1282 (type
->tp_flags
& ~Py_TPFLAGS_READYING
) | Py_TPFLAGS_READY
;
1286 type
->tp_flags
&= ~Py_TPFLAGS_READYING
;
1291 /* Generic wrappers for overloadable 'operators' such as __getitem__ */
1293 /* There's a wrapper *function* for each distinct function typedef used
1294 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
1295 wrapper *table* for each distinct operation (e.g. __len__, __add__).
1296 Most tables have only one entry; the tables for binary operators have two
1297 entries, one regular and one with reversed arguments. */
1300 wrap_inquiry(PyObject
*self
, PyObject
*args
, void *wrapped
)
1302 inquiry func
= (inquiry
)wrapped
;
1305 if (!PyArg_ParseTuple(args
, ""))
1307 res
= (*func
)(self
);
1308 if (res
== -1 && PyErr_Occurred())
1310 return PyInt_FromLong((long)res
);
1313 static struct wrapperbase tab_len
[] = {
1314 {"__len__", (wrapperfunc
)wrap_inquiry
, "x.__len__() <==> len(x)"},
1319 wrap_binaryfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
1321 binaryfunc func
= (binaryfunc
)wrapped
;
1324 if (!PyArg_ParseTuple(args
, "O", &other
))
1326 return (*func
)(self
, other
);
1330 wrap_binaryfunc_r(PyObject
*self
, PyObject
*args
, void *wrapped
)
1332 binaryfunc func
= (binaryfunc
)wrapped
;
1335 if (!PyArg_ParseTuple(args
, "O", &other
))
1337 return (*func
)(other
, self
);
1341 #define BINARY(NAME, OP) \
1342 static struct wrapperbase tab_##NAME[] = { \
1344 (wrapperfunc)wrap_binaryfunc, \
1345 "x.__" #NAME "__(y) <==> " #OP}, \
1346 {"__r" #NAME "__", \
1347 (wrapperfunc)wrap_binaryfunc_r, \
1348 "y.__r" #NAME "__(x) <==> " #OP}, \
1357 BINARY(divmod
, "divmod(x,y)");
1358 BINARY(lshift
, "x<<y");
1359 BINARY(rshift
, "x>>y");
1365 wrap_ternaryfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
1367 ternaryfunc func
= (ternaryfunc
)wrapped
;
1369 PyObject
*third
= Py_None
;
1371 /* Note: This wrapper only works for __pow__() */
1373 if (!PyArg_ParseTuple(args
, "O|O", &other
, &third
))
1375 return (*func
)(self
, other
, third
);
1379 #define TERNARY(NAME, OP) \
1380 static struct wrapperbase tab_##NAME[] = { \
1382 (wrapperfunc)wrap_ternaryfunc, \
1383 "x.__" #NAME "__(y, z) <==> " #OP}, \
1384 {"__r" #NAME "__", \
1385 (wrapperfunc)wrap_ternaryfunc, \
1386 "y.__r" #NAME "__(x, z) <==> " #OP}, \
1390 TERNARY(pow
, "(x**y) % z");
1393 #define UNARY(NAME, OP) \
1394 static struct wrapperbase tab_##NAME[] = { \
1396 (wrapperfunc)wrap_unaryfunc, \
1397 "x.__" #NAME "__() <==> " #OP}, \
1402 wrap_unaryfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
1404 unaryfunc func
= (unaryfunc
)wrapped
;
1406 if (!PyArg_ParseTuple(args
, ""))
1408 return (*func
)(self
);
1413 UNARY(abs
, "abs(x)");
1414 UNARY(nonzero
, "x != 0");
1415 UNARY(invert
, "~x");
1416 UNARY(int, "int(x)");
1417 UNARY(long, "long(x)");
1418 UNARY(float, "float(x)");
1419 UNARY(oct
, "oct(x)");
1420 UNARY(hex
, "hex(x)");
1423 #define IBINARY(NAME, OP) \
1424 static struct wrapperbase tab_##NAME[] = { \
1426 (wrapperfunc)wrap_binaryfunc, \
1427 "x.__" #NAME "__(y) <==> " #OP}, \
1431 IBINARY(iadd
, "x+=y");
1432 IBINARY(isub
, "x-=y");
1433 IBINARY(imul
, "x*=y");
1434 IBINARY(idiv
, "x/=y");
1435 IBINARY(imod
, "x%=y");
1436 IBINARY(ilshift
, "x<<=y");
1437 IBINARY(irshift
, "x>>=y");
1438 IBINARY(iand
, "x&=y");
1439 IBINARY(ixor
, "x^=y");
1440 IBINARY(ior
, "x|=y");
1443 #define ITERNARY(NAME, OP) \
1444 static struct wrapperbase tab_##NAME[] = { \
1446 (wrapperfunc)wrap_ternaryfunc, \
1447 "x.__" #NAME "__(y) <==> " #OP}, \
1451 ITERNARY(ipow
, "x = (x**y) % z");
1453 static struct wrapperbase tab_getitem
[] = {
1454 {"__getitem__", (wrapperfunc
)wrap_binaryfunc
,
1455 "x.__getitem__(y) <==> x[y]"},
1460 wrap_intargfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
1462 intargfunc func
= (intargfunc
)wrapped
;
1465 if (!PyArg_ParseTuple(args
, "i", &i
))
1467 return (*func
)(self
, i
);
1470 static struct wrapperbase tab_mul_int
[] = {
1471 {"__mul__", (wrapperfunc
)wrap_intargfunc
, "x.__mul__(n) <==> x*n"},
1472 {"__rmul__", (wrapperfunc
)wrap_intargfunc
, "x.__rmul__(n) <==> n*x"},
1476 static struct wrapperbase tab_concat
[] = {
1477 {"__add__", (wrapperfunc
)wrap_binaryfunc
, "x.__add__(y) <==> x+y"},
1481 static struct wrapperbase tab_imul_int
[] = {
1482 {"__imul__", (wrapperfunc
)wrap_intargfunc
, "x.__imul__(n) <==> x*=n"},
1486 static struct wrapperbase tab_getitem_int
[] = {
1487 {"__getitem__", (wrapperfunc
)wrap_intargfunc
,
1488 "x.__getitem__(i) <==> x[i]"},
1493 wrap_intintargfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
1495 intintargfunc func
= (intintargfunc
)wrapped
;
1498 if (!PyArg_ParseTuple(args
, "ii", &i
, &j
))
1500 return (*func
)(self
, i
, j
);
1503 static struct wrapperbase tab_getslice
[] = {
1504 {"__getslice__", (wrapperfunc
)wrap_intintargfunc
,
1505 "x.__getslice__(i, j) <==> x[i:j]"},
1510 wrap_intobjargproc(PyObject
*self
, PyObject
*args
, void *wrapped
)
1512 intobjargproc func
= (intobjargproc
)wrapped
;
1516 if (!PyArg_ParseTuple(args
, "iO", &i
, &value
))
1518 res
= (*func
)(self
, i
, value
);
1519 if (res
== -1 && PyErr_Occurred())
1526 wrap_delitem_int(PyObject
*self
, PyObject
*args
, void *wrapped
)
1528 intobjargproc func
= (intobjargproc
)wrapped
;
1531 if (!PyArg_ParseTuple(args
, "i", &i
))
1533 res
= (*func
)(self
, i
, NULL
);
1534 if (res
== -1 && PyErr_Occurred())
1540 static struct wrapperbase tab_setitem_int
[] = {
1541 {"__setitem__", (wrapperfunc
)wrap_intobjargproc
,
1542 "x.__setitem__(i, y) <==> x[i]=y"},
1543 {"__delitem__", (wrapperfunc
)wrap_delitem_int
,
1544 "x.__delitem__(y) <==> del x[y]"},
1549 wrap_intintobjargproc(PyObject
*self
, PyObject
*args
, void *wrapped
)
1551 intintobjargproc func
= (intintobjargproc
)wrapped
;
1555 if (!PyArg_ParseTuple(args
, "iiO", &i
, &j
, &value
))
1557 res
= (*func
)(self
, i
, j
, value
);
1558 if (res
== -1 && PyErr_Occurred())
1564 static struct wrapperbase tab_setslice
[] = {
1565 {"__setslice__", (wrapperfunc
)wrap_intintobjargproc
,
1566 "x.__setslice__(i, j, y) <==> x[i:j]=y"},
1570 /* XXX objobjproc is a misnomer; should be objargpred */
1572 wrap_objobjproc(PyObject
*self
, PyObject
*args
, void *wrapped
)
1574 objobjproc func
= (objobjproc
)wrapped
;
1578 if (!PyArg_ParseTuple(args
, "O", &value
))
1580 res
= (*func
)(self
, value
);
1581 if (res
== -1 && PyErr_Occurred())
1583 return PyInt_FromLong((long)res
);
1586 static struct wrapperbase tab_contains
[] = {
1587 {"__contains__", (wrapperfunc
)wrap_objobjproc
,
1588 "x.__contains__(y) <==> y in x"},
1593 wrap_objobjargproc(PyObject
*self
, PyObject
*args
, void *wrapped
)
1595 objobjargproc func
= (objobjargproc
)wrapped
;
1597 PyObject
*key
, *value
;
1599 if (!PyArg_ParseTuple(args
, "OO", &key
, &value
))
1601 res
= (*func
)(self
, key
, value
);
1602 if (res
== -1 && PyErr_Occurred())
1609 wrap_delitem(PyObject
*self
, PyObject
*args
, void *wrapped
)
1611 objobjargproc func
= (objobjargproc
)wrapped
;
1615 if (!PyArg_ParseTuple(args
, "O", &key
))
1617 res
= (*func
)(self
, key
, NULL
);
1618 if (res
== -1 && PyErr_Occurred())
1624 static struct wrapperbase tab_setitem
[] = {
1625 {"__setitem__", (wrapperfunc
)wrap_objobjargproc
,
1626 "x.__setitem__(y, z) <==> x[y]=z"},
1627 {"__delitem__", (wrapperfunc
)wrap_delitem
,
1628 "x.__delitem__(y) <==> del x[y]"},
1633 wrap_cmpfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
1635 cmpfunc func
= (cmpfunc
)wrapped
;
1639 if (!PyArg_ParseTuple(args
, "O", &other
))
1641 res
= (*func
)(self
, other
);
1642 if (PyErr_Occurred())
1644 return PyInt_FromLong((long)res
);
1647 static struct wrapperbase tab_cmp
[] = {
1648 {"__cmp__", (wrapperfunc
)wrap_cmpfunc
,
1649 "x.__cmp__(y) <==> cmp(x,y)"},
1653 static struct wrapperbase tab_repr
[] = {
1654 {"__repr__", (wrapperfunc
)wrap_unaryfunc
,
1655 "x.__repr__() <==> repr(x)"},
1659 static struct wrapperbase tab_getattr
[] = {
1660 {"__getattr__", (wrapperfunc
)wrap_binaryfunc
,
1661 "x.__getattr__('name') <==> x.name"},
1666 wrap_setattr(PyObject
*self
, PyObject
*args
, void *wrapped
)
1668 setattrofunc func
= (setattrofunc
)wrapped
;
1670 PyObject
*name
, *value
;
1672 if (!PyArg_ParseTuple(args
, "OO", &name
, &value
))
1674 res
= (*func
)(self
, name
, value
);
1682 wrap_delattr(PyObject
*self
, PyObject
*args
, void *wrapped
)
1684 setattrofunc func
= (setattrofunc
)wrapped
;
1688 if (!PyArg_ParseTuple(args
, "O", &name
))
1690 res
= (*func
)(self
, name
, NULL
);
1697 static struct wrapperbase tab_setattr
[] = {
1698 {"__setattr__", (wrapperfunc
)wrap_setattr
,
1699 "x.__setattr__('name', value) <==> x.name = value"},
1700 {"__delattr__", (wrapperfunc
)wrap_delattr
,
1701 "x.__delattr__('name') <==> del x.name"},
1706 wrap_hashfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
1708 hashfunc func
= (hashfunc
)wrapped
;
1711 if (!PyArg_ParseTuple(args
, ""))
1713 res
= (*func
)(self
);
1714 if (res
== -1 && PyErr_Occurred())
1716 return PyInt_FromLong(res
);
1719 static struct wrapperbase tab_hash
[] = {
1720 {"__hash__", (wrapperfunc
)wrap_hashfunc
,
1721 "x.__hash__() <==> hash(x)"},
1726 wrap_call(PyObject
*self
, PyObject
*args
, void *wrapped
)
1728 ternaryfunc func
= (ternaryfunc
)wrapped
;
1730 /* XXX What about keyword arguments? */
1731 return (*func
)(self
, args
, NULL
);
1734 static struct wrapperbase tab_call
[] = {
1735 {"__call__", (wrapperfunc
)wrap_call
,
1736 "x.__call__(...) <==> x(...)"},
1740 static struct wrapperbase tab_str
[] = {
1741 {"__str__", (wrapperfunc
)wrap_unaryfunc
,
1742 "x.__str__() <==> str(x)"},
1747 wrap_richcmpfunc(PyObject
*self
, PyObject
*args
, void *wrapped
, int op
)
1749 richcmpfunc func
= (richcmpfunc
)wrapped
;
1752 if (!PyArg_ParseTuple(args
, "O", &other
))
1754 return (*func
)(self
, other
, op
);
1757 #undef RICHCMP_WRAPPER
1758 #define RICHCMP_WRAPPER(NAME, OP) \
1760 richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
1762 return wrap_richcmpfunc(self, args, wrapped, OP); \
1765 RICHCMP_WRAPPER(lt
, Py_LT
)
1766 RICHCMP_WRAPPER(le
, Py_LE
)
1767 RICHCMP_WRAPPER(eq
, Py_EQ
)
1768 RICHCMP_WRAPPER(ne
, Py_NE
)
1769 RICHCMP_WRAPPER(gt
, Py_GT
)
1770 RICHCMP_WRAPPER(ge
, Py_GE
)
1772 #undef RICHCMP_ENTRY
1773 #define RICHCMP_ENTRY(NAME, EXPR) \
1774 {"__" #NAME "__", (wrapperfunc)richcmp_##NAME, \
1775 "x.__" #NAME "__(y) <==> " EXPR}
1777 static struct wrapperbase tab_richcmp
[] = {
1778 RICHCMP_ENTRY(lt
, "x<y"),
1779 RICHCMP_ENTRY(le
, "x<=y"),
1780 RICHCMP_ENTRY(eq
, "x==y"),
1781 RICHCMP_ENTRY(ne
, "x!=y"),
1782 RICHCMP_ENTRY(gt
, "x>y"),
1783 RICHCMP_ENTRY(ge
, "x>=y"),
1787 static struct wrapperbase tab_iter
[] = {
1788 {"__iter__", (wrapperfunc
)wrap_unaryfunc
, "x.__iter__() <==> iter(x)"},
1793 wrap_next(PyObject
*self
, PyObject
*args
, void *wrapped
)
1795 unaryfunc func
= (unaryfunc
)wrapped
;
1798 if (!PyArg_ParseTuple(args
, ""))
1800 res
= (*func
)(self
);
1801 if (res
== NULL
&& !PyErr_Occurred())
1802 PyErr_SetNone(PyExc_StopIteration
);
1806 static struct wrapperbase tab_next
[] = {
1807 {"next", (wrapperfunc
)wrap_next
,
1808 "x.next() -> the next value, or raise StopIteration"},
1813 wrap_descr_get(PyObject
*self
, PyObject
*args
, void *wrapped
)
1815 descrgetfunc func
= (descrgetfunc
)wrapped
;
1817 PyObject
*type
= NULL
;
1819 if (!PyArg_ParseTuple(args
, "O|O", &obj
, &type
))
1822 type
= (PyObject
*)obj
->ob_type
;
1823 return (*func
)(self
, obj
, type
);
1826 static struct wrapperbase tab_descr_get
[] = {
1827 {"__get__", (wrapperfunc
)wrap_descr_get
,
1828 "descr.__get__(obj, type) -> value"},
1833 wrap_descrsetfunc(PyObject
*self
, PyObject
*args
, void *wrapped
)
1835 descrsetfunc func
= (descrsetfunc
)wrapped
;
1836 PyObject
*obj
, *value
;
1839 if (!PyArg_ParseTuple(args
, "OO", &obj
, &value
))
1841 ret
= (*func
)(self
, obj
, value
);
1848 static struct wrapperbase tab_descr_set
[] = {
1849 {"__set__", (wrapperfunc
)wrap_descrsetfunc
,
1850 "descr.__set__(obj, value)"},
1855 wrap_init(PyObject
*self
, PyObject
*args
, void *wrapped
)
1857 initproc func
= (initproc
)wrapped
;
1859 /* XXX What about keyword arguments? */
1860 if (func(self
, args
, NULL
) < 0)
1866 static struct wrapperbase tab_init
[] = {
1867 {"__init__", (wrapperfunc
)wrap_init
,
1868 "x.__init__(...) initializes x; "
1869 "see x.__type__.__doc__ for signature"},
1874 tp_new_wrapper(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
1876 PyTypeObject
*type
, *subtype
;
1877 PyObject
*arg0
, *res
;
1879 if (self
== NULL
|| !PyType_Check(self
))
1880 Py_FatalError("__new__() called with non-type 'self'");
1881 type
= (PyTypeObject
*)self
;
1882 if (!PyTuple_Check(args
) || PyTuple_GET_SIZE(args
) < 1) {
1883 PyErr_SetString(PyExc_TypeError
,
1884 "T.__new__(): not enough arguments");
1887 arg0
= PyTuple_GET_ITEM(args
, 0);
1888 if (!PyType_Check(arg0
)) {
1889 PyErr_SetString(PyExc_TypeError
,
1890 "T.__new__(S): S is not a type object");
1893 subtype
= (PyTypeObject
*)arg0
;
1894 if (!PyType_IsSubtype(subtype
, type
)) {
1895 PyErr_SetString(PyExc_TypeError
,
1896 "T.__new__(S): S is not a subtype of T");
1899 args
= PyTuple_GetSlice(args
, 1, PyTuple_GET_SIZE(args
));
1902 res
= type
->tp_new(subtype
, args
, kwds
);
1907 static struct PyMethodDef tp_new_methoddef
[] = {
1908 {"__new__", (PyCFunction
)tp_new_wrapper
, METH_KEYWORDS
,
1909 "T.__new__(S, ...) -> a new object with type S, a subtype of T"},
1914 add_tp_new_wrapper(PyTypeObject
*type
)
1918 if (PyDict_GetItemString(type
->tp_defined
, "__new__") != NULL
)
1920 func
= PyCFunction_New(tp_new_methoddef
, (PyObject
*)type
);
1923 return PyDict_SetItemString(type
->tp_defined
, "__new__", func
);
1927 add_wrappers(PyTypeObject
*type
, struct wrapperbase
*wraps
, void *wrapped
)
1929 PyObject
*dict
= type
->tp_defined
;
1931 for (; wraps
->name
!= NULL
; wraps
++) {
1933 if (PyDict_GetItemString(dict
, wraps
->name
))
1935 descr
= PyDescr_NewWrapper(type
, wraps
, wrapped
);
1938 if (PyDict_SetItemString(dict
, wraps
->name
, descr
) < 0)
1945 /* This function is called by PyType_Ready() to populate the type's
1946 dictionary with method descriptors for function slots. For each
1947 function slot (like tp_repr) that's defined in the type, one or
1948 more corresponding descriptors are added in the type's tp_defined
1949 dictionary under the appropriate name (like __repr__). Some
1950 function slots cause more than one descriptor to be added (for
1951 example, the nb_add slot adds both __add__ and __radd__
1952 descriptors) and some function slots compete for the same
1953 descriptor (for example both sq_item and mp_subscript generate a
1954 __getitem__ descriptor). This only adds new descriptors and
1955 doesn't overwrite entries in tp_defined that were previously
1956 defined. The descriptors contain a reference to the C function
1957 they must call, so that it's safe if they are copied into a
1958 subtype's __dict__ and the subtype has a different C function in
1959 its slot -- calling the method defined by the descriptor will call
1960 the C function that was used to create it, rather than the C
1961 function present in the slot when it is called. (This is important
1962 because a subtype may have a C function in the slot that calls the
1963 method from the dictionary, and we want to avoid infinite recursion
1967 add_operators(PyTypeObject
*type
)
1969 PySequenceMethods
*sq
;
1970 PyMappingMethods
*mp
;
1971 PyNumberMethods
*nb
;
1974 #define ADD(SLOT, TABLE) \
1976 if (add_wrappers(type, TABLE, (void *)(SLOT)) < 0) \
1980 if ((sq
= type
->tp_as_sequence
) != NULL
) {
1981 ADD(sq
->sq_length
, tab_len
);
1982 ADD(sq
->sq_concat
, tab_concat
);
1983 ADD(sq
->sq_repeat
, tab_mul_int
);
1984 ADD(sq
->sq_item
, tab_getitem_int
);
1985 ADD(sq
->sq_slice
, tab_getslice
);
1986 ADD(sq
->sq_ass_item
, tab_setitem_int
);
1987 ADD(sq
->sq_ass_slice
, tab_setslice
);
1988 ADD(sq
->sq_contains
, tab_contains
);
1989 ADD(sq
->sq_inplace_concat
, tab_iadd
);
1990 ADD(sq
->sq_inplace_repeat
, tab_imul_int
);
1993 if ((mp
= type
->tp_as_mapping
) != NULL
) {
1994 if (sq
->sq_length
== NULL
)
1995 ADD(mp
->mp_length
, tab_len
);
1996 ADD(mp
->mp_subscript
, tab_getitem
);
1997 ADD(mp
->mp_ass_subscript
, tab_setitem
);
2000 /* We don't support "old-style numbers" because their binary
2001 operators require that both arguments have the same type;
2002 the wrappers here only work for new-style numbers. */
2003 if ((type
->tp_flags
& Py_TPFLAGS_CHECKTYPES
) &&
2004 (nb
= type
->tp_as_number
) != NULL
) {
2005 ADD(nb
->nb_add
, tab_add
);
2006 ADD(nb
->nb_subtract
, tab_sub
);
2007 ADD(nb
->nb_multiply
, tab_mul
);
2008 ADD(nb
->nb_divide
, tab_div
);
2009 ADD(nb
->nb_remainder
, tab_mod
);
2010 ADD(nb
->nb_divmod
, tab_divmod
);
2011 ADD(nb
->nb_power
, tab_pow
);
2012 ADD(nb
->nb_negative
, tab_neg
);
2013 ADD(nb
->nb_positive
, tab_pos
);
2014 ADD(nb
->nb_absolute
, tab_abs
);
2015 ADD(nb
->nb_nonzero
, tab_nonzero
);
2016 ADD(nb
->nb_invert
, tab_invert
);
2017 ADD(nb
->nb_lshift
, tab_lshift
);
2018 ADD(nb
->nb_rshift
, tab_rshift
);
2019 ADD(nb
->nb_and
, tab_and
);
2020 ADD(nb
->nb_xor
, tab_xor
);
2021 ADD(nb
->nb_or
, tab_or
);
2022 /* We don't support coerce() -- see above comment */
2023 ADD(nb
->nb_int
, tab_int
);
2024 ADD(nb
->nb_long
, tab_long
);
2025 ADD(nb
->nb_float
, tab_float
);
2026 ADD(nb
->nb_oct
, tab_oct
);
2027 ADD(nb
->nb_hex
, tab_hex
);
2028 ADD(nb
->nb_inplace_add
, tab_iadd
);
2029 ADD(nb
->nb_inplace_subtract
, tab_isub
);
2030 ADD(nb
->nb_inplace_multiply
, tab_imul
);
2031 ADD(nb
->nb_inplace_divide
, tab_idiv
);
2032 ADD(nb
->nb_inplace_remainder
, tab_imod
);
2033 ADD(nb
->nb_inplace_power
, tab_ipow
);
2034 ADD(nb
->nb_inplace_lshift
, tab_ilshift
);
2035 ADD(nb
->nb_inplace_rshift
, tab_irshift
);
2036 ADD(nb
->nb_inplace_and
, tab_iand
);
2037 ADD(nb
->nb_inplace_xor
, tab_ixor
);
2038 ADD(nb
->nb_inplace_or
, tab_ior
);
2041 ADD(type
->tp_getattro
, tab_getattr
);
2042 ADD(type
->tp_setattro
, tab_setattr
);
2043 ADD(type
->tp_compare
, tab_cmp
);
2044 ADD(type
->tp_repr
, tab_repr
);
2045 ADD(type
->tp_hash
, tab_hash
);
2046 ADD(type
->tp_call
, tab_call
);
2047 ADD(type
->tp_str
, tab_str
);
2048 ADD(type
->tp_richcompare
, tab_richcmp
);
2049 ADD(type
->tp_iter
, tab_iter
);
2050 ADD(type
->tp_iternext
, tab_next
);
2051 ADD(type
->tp_descr_get
, tab_descr_get
);
2052 ADD(type
->tp_descr_set
, tab_descr_set
);
2053 ADD(type
->tp_init
, tab_init
);
2055 if (type
->tp_new
!= NULL
) {
2056 if (add_tp_new_wrapper(type
) < 0)
2063 /* Slot wrappers that call the corresponding __foo__ slot. See comments
2064 below at override_slots() for more explanation. */
2066 #define SLOT0(FUNCNAME, OPSTR) \
2068 FUNCNAME(PyObject *self) \
2070 return PyObject_CallMethod(self, OPSTR, ""); \
2073 #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
2075 FUNCNAME(PyObject *self, ARG1TYPE arg1) \
2077 return PyObject_CallMethod(self, OPSTR, ARGCODES, arg1); \
2081 #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
2083 FUNCNAME(PyObject *self, PyObject *other) \
2085 if (self->ob_type->tp_as_number != NULL && \
2086 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2088 r = PyObject_CallMethod( \
2089 self, OPSTR, "O", other); \
2090 if (r != Py_NotImplemented || \
2091 other->ob_type == self->ob_type) \
2095 if (other->ob_type->tp_as_number != NULL && \
2096 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2097 return PyObject_CallMethod( \
2098 other, ROPSTR, "O", self); \
2100 Py_INCREF(Py_NotImplemented); \
2101 return Py_NotImplemented; \
2104 #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
2105 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
2107 #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
2109 FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
2111 return PyObject_CallMethod(self, OPSTR, ARGCODES, arg1, arg2); \
2115 slot_sq_length(PyObject
*self
)
2117 PyObject
*res
= PyObject_CallMethod(self
, "__len__", "");
2121 return (int)PyInt_AsLong(res
);
2124 SLOT1(slot_sq_concat
, "__add__", PyObject
*, "O")
2125 SLOT1(slot_sq_repeat
, "__mul__", int, "i")
2126 SLOT1(slot_sq_item
, "__getitem__", int, "i")
2127 SLOT2(slot_sq_slice
, "__getslice__", int, int, "ii")
2130 slot_sq_ass_item(PyObject
*self
, int index
, PyObject
*value
)
2135 res
= PyObject_CallMethod(self
, "__delitem__", "i", index
);
2137 res
= PyObject_CallMethod(self
, "__setitem__",
2138 "iO", index
, value
);
2146 slot_sq_ass_slice(PyObject
*self
, int i
, int j
, PyObject
*value
)
2151 res
= PyObject_CallMethod(self
, "__delslice__", "ii", i
, j
);
2153 res
= PyObject_CallMethod(self
, "__setslice__",
2154 "iiO", i
, j
, value
);
2162 slot_sq_contains(PyObject
*self
, PyObject
*value
)
2164 PyObject
*res
= PyObject_CallMethod(self
, "__contains__", "O", value
);
2169 r
= PyInt_AsLong(res
);
2174 SLOT1(slot_sq_inplace_concat
, "__iadd__", PyObject
*, "O")
2175 SLOT1(slot_sq_inplace_repeat
, "__imul__", int, "i")
2177 #define slot_mp_length slot_sq_length
2179 SLOT1(slot_mp_subscript
, "__getitem__", PyObject
*, "O")
2182 slot_mp_ass_subscript(PyObject
*self
, PyObject
*key
, PyObject
*value
)
2187 res
= PyObject_CallMethod(self
, "__delitem__", "O", key
);
2189 res
= PyObject_CallMethod(self
, "__setitem__",
2197 SLOT1BIN(slot_nb_add
, nb_add
, "__add__", "__radd__")
2198 SLOT1BIN(slot_nb_subtract
, nb_subtract
, "__sub__", "__rsub__")
2199 SLOT1BIN(slot_nb_multiply
, nb_multiply
, "__mul__", "__rmul__")
2200 SLOT1BIN(slot_nb_divide
, nb_divide
, "__div__", "__rdiv__")
2201 SLOT1BIN(slot_nb_remainder
, nb_remainder
, "__mod__", "__rmod__")
2202 SLOT1BIN(slot_nb_divmod
, nb_divmod
, "__divmod__", "__rdivmod__")
2204 staticforward PyObject
*slot_nb_power(PyObject
*, PyObject
*, PyObject
*);
2206 SLOT1BINFULL(slot_nb_power_binary
, slot_nb_power
,
2207 nb_power
, "__pow__", "__rpow__")
2210 slot_nb_power(PyObject
*self
, PyObject
*other
, PyObject
*modulus
)
2212 if (modulus
== Py_None
)
2213 return slot_nb_power_binary(self
, other
);
2214 /* Three-arg power doesn't use __rpow__ */
2215 return PyObject_CallMethod(self
, "__pow__", "OO", other
, modulus
);
2218 SLOT0(slot_nb_negative
, "__neg__")
2219 SLOT0(slot_nb_positive
, "__pos__")
2220 SLOT0(slot_nb_absolute
, "__abs__")
2223 slot_nb_nonzero(PyObject
*self
)
2225 PyObject
*res
= PyObject_CallMethod(self
, "__nonzero__", "");
2229 return (int)PyInt_AsLong(res
);
2232 SLOT0(slot_nb_invert
, "__invert__")
2233 SLOT1BIN(slot_nb_lshift
, nb_lshift
, "__lshift__", "__rlshift__")
2234 SLOT1BIN(slot_nb_rshift
, nb_rshift
, "__rshift__", "__rrshift__")
2235 SLOT1BIN(slot_nb_and
, nb_and
, "__and__", "__rand__")
2236 SLOT1BIN(slot_nb_xor
, nb_xor
, "__xor__", "__rxor__")
2237 SLOT1BIN(slot_nb_or
, nb_or
, "__or__", "__ror__")
2239 SLOT0(slot_nb_int
, "__int__")
2240 SLOT0(slot_nb_long
, "__long__")
2241 SLOT0(slot_nb_float
, "__float__")
2242 SLOT0(slot_nb_oct
, "__oct__")
2243 SLOT0(slot_nb_hex
, "__hex__")
2244 SLOT1(slot_nb_inplace_add
, "__iadd__", PyObject
*, "O")
2245 SLOT1(slot_nb_inplace_subtract
, "__isub__", PyObject
*, "O")
2246 SLOT1(slot_nb_inplace_multiply
, "__imul__", PyObject
*, "O")
2247 SLOT1(slot_nb_inplace_divide
, "__idiv__", PyObject
*, "O")
2248 SLOT1(slot_nb_inplace_remainder
, "__imod__", PyObject
*, "O")
2249 SLOT2(slot_nb_inplace_power
, "__ipow__", PyObject
*, PyObject
*, "OO")
2250 SLOT1(slot_nb_inplace_lshift
, "__ilshift__", PyObject
*, "O")
2251 SLOT1(slot_nb_inplace_rshift
, "__irshift__", PyObject
*, "O")
2252 SLOT1(slot_nb_inplace_and
, "__iand__", PyObject
*, "O")
2253 SLOT1(slot_nb_inplace_xor
, "__ixor__", PyObject
*, "O")
2254 SLOT1(slot_nb_inplace_or
, "__ior__", PyObject
*, "O")
2255 SLOT1BIN(slot_nb_floor_divide
, nb_floor_divide
,
2256 "__floordiv__", "__rfloordiv__")
2257 SLOT1BIN(slot_nb_true_divide
, nb_true_divide
, "__truediv__", "__rtruediv__")
2258 SLOT1(slot_nb_inplace_floor_divide
, "__ifloordiv__", PyObject
*, "O")
2259 SLOT1(slot_nb_inplace_true_divide
, "__itruediv__", PyObject
*, "O")
2262 slot_tp_compare(PyObject
*self
, PyObject
*other
)
2264 PyObject
*res
= PyObject_CallMethod(self
, "__cmp__", "O", other
);
2269 r
= PyInt_AsLong(res
);
2274 SLOT0(slot_tp_repr
, "__repr__")
2277 slot_tp_hash(PyObject
*self
)
2279 PyObject
*res
= PyObject_CallMethod(self
, "__hash__", "");
2284 h
= PyInt_AsLong(res
);
2285 if (h
== -1 && !PyErr_Occurred())
2291 slot_tp_call(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
2293 PyObject
*meth
= PyObject_GetAttrString(self
, "__call__");
2298 res
= PyObject_Call(meth
, args
, kwds
);
2303 SLOT0(slot_tp_str
, "__str__")
2306 slot_tp_getattro(PyObject
*self
, PyObject
*name
)
2308 PyTypeObject
*tp
= self
->ob_type
;
2309 PyObject
*dict
= NULL
;
2312 if (tp
->tp_flags
& Py_TPFLAGS_HEAPTYPE
)
2315 PyErr_Format(PyExc_SystemError
,
2316 "'%.100s' type object has no __dict__???",
2320 getattr
= PyDict_GetItemString(dict
, "__getattr__");
2321 if (getattr
== NULL
) {
2322 PyErr_SetString(PyExc_AttributeError
, "__getattr__");
2325 return PyObject_CallFunction(getattr
, "OO", self
, name
);
2329 slot_tp_setattro(PyObject
*self
, PyObject
*name
, PyObject
*value
)
2334 res
= PyObject_CallMethod(self
, "__delattr__", "O", name
);
2336 res
= PyObject_CallMethod(self
, "__setattr__",
2344 /* Map rich comparison operators to their __xx__ namesakes */
2345 static char *name_op
[] = {
2355 slot_tp_richcompare(PyObject
*self
, PyObject
*other
, int op
)
2357 PyObject
*meth
= PyObject_GetAttrString(self
, name_op
[op
]);
2362 res
= PyObject_CallFunction(meth
, "O", other
);
2367 SLOT0(slot_tp_iter
, "__iter__")
2370 slot_tp_iternext(PyObject
*self
)
2372 return PyObject_CallMethod(self
, "next", "");
2375 SLOT2(slot_tp_descr_get
, "__get__", PyObject
*, PyObject
*, "OO")
2378 slot_tp_descr_set(PyObject
*self
, PyObject
*target
, PyObject
*value
)
2380 PyObject
*res
= PyObject_CallMethod(self
, "__set__",
2381 "OO", target
, value
);
2389 slot_tp_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
2391 PyObject
*meth
= PyObject_GetAttrString(self
, "__init__");
2396 res
= PyObject_Call(meth
, args
, kwds
);
2405 slot_tp_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
2407 PyObject
*func
= PyObject_GetAttrString((PyObject
*)type
, "__new__");
2408 PyObject
*newargs
, *x
;
2413 assert(PyTuple_Check(args
));
2414 n
= PyTuple_GET_SIZE(args
);
2415 newargs
= PyTuple_New(n
+1);
2416 if (newargs
== NULL
)
2419 PyTuple_SET_ITEM(newargs
, 0, (PyObject
*)type
);
2420 for (i
= 0; i
< n
; i
++) {
2421 x
= PyTuple_GET_ITEM(args
, i
);
2423 PyTuple_SET_ITEM(newargs
, i
+1, x
);
2425 x
= PyObject_Call(func
, newargs
, kwds
);
2430 /* This is called at the very end of type_new() (even after
2431 PyType_Ready()) to complete the initialization of dynamic types.
2432 The dict argument is the dictionary argument passed to type_new(),
2433 which is the local namespace of the class statement, in other
2434 words, it contains the methods. For each special method (like
2435 __repr__) defined in the dictionary, the corresponding function
2436 slot in the type object (like tp_repr) is set to a special function
2437 whose name is 'slot_' followed by the slot name and whose signature
2438 is whatever is required for that slot. These slot functions look
2439 up the corresponding method in the type's dictionary and call it.
2440 The slot functions have to take care of the various peculiarities
2441 of the mapping between slots and special methods, such as mapping
2442 one slot to multiple methods (tp_richcompare <--> __le__, __lt__
2443 etc.) or mapping multiple slots to a single method (sq_item,
2444 mp_subscript <--> __getitem__). */
2447 override_slots(PyTypeObject
*type
, PyObject
*dict
)
2449 PySequenceMethods
*sq
= type
->tp_as_sequence
;
2450 PyMappingMethods
*mp
= type
->tp_as_mapping
;
2451 PyNumberMethods
*nb
= type
->tp_as_number
;
2453 #define SQSLOT(OPNAME, SLOTNAME, FUNCNAME) \
2454 if (PyDict_GetItemString(dict, OPNAME)) { \
2455 sq->SLOTNAME = FUNCNAME; \
2458 #define MPSLOT(OPNAME, SLOTNAME, FUNCNAME) \
2459 if (PyDict_GetItemString(dict, OPNAME)) { \
2460 mp->SLOTNAME = FUNCNAME; \
2463 #define NBSLOT(OPNAME, SLOTNAME, FUNCNAME) \
2464 if (PyDict_GetItemString(dict, OPNAME)) { \
2465 nb->SLOTNAME = FUNCNAME; \
2468 #define TPSLOT(OPNAME, SLOTNAME, FUNCNAME) \
2469 if (PyDict_GetItemString(dict, OPNAME)) { \
2470 type->SLOTNAME = FUNCNAME; \
2473 SQSLOT("__len__", sq_length
, slot_sq_length
);
2474 SQSLOT("__add__", sq_concat
, slot_sq_concat
);
2475 SQSLOT("__mul__", sq_repeat
, slot_sq_repeat
);
2476 SQSLOT("__getitem__", sq_item
, slot_sq_item
);
2477 SQSLOT("__getslice__", sq_slice
, slot_sq_slice
);
2478 SQSLOT("__setitem__", sq_ass_item
, slot_sq_ass_item
);
2479 SQSLOT("__delitem__", sq_ass_item
, slot_sq_ass_item
);
2480 SQSLOT("__setslice__", sq_ass_slice
, slot_sq_ass_slice
);
2481 SQSLOT("__delslice__", sq_ass_slice
, slot_sq_ass_slice
);
2482 SQSLOT("__contains__", sq_contains
, slot_sq_contains
);
2483 SQSLOT("__iadd__", sq_inplace_concat
, slot_sq_inplace_concat
);
2484 SQSLOT("__imul__", sq_inplace_repeat
, slot_sq_inplace_repeat
);
2486 MPSLOT("__len__", mp_length
, slot_mp_length
);
2487 MPSLOT("__getitem__", mp_subscript
, slot_mp_subscript
);
2488 MPSLOT("__setitem__", mp_ass_subscript
, slot_mp_ass_subscript
);
2489 MPSLOT("__delitem__", mp_ass_subscript
, slot_mp_ass_subscript
);
2491 NBSLOT("__add__", nb_add
, slot_nb_add
);
2492 NBSLOT("__sub__", nb_subtract
, slot_nb_subtract
);
2493 NBSLOT("__mul__", nb_multiply
, slot_nb_multiply
);
2494 NBSLOT("__div__", nb_divide
, slot_nb_divide
);
2495 NBSLOT("__mod__", nb_remainder
, slot_nb_remainder
);
2496 NBSLOT("__divmod__", nb_divmod
, slot_nb_divmod
);
2497 NBSLOT("__pow__", nb_power
, slot_nb_power
);
2498 NBSLOT("__neg__", nb_negative
, slot_nb_negative
);
2499 NBSLOT("__pos__", nb_positive
, slot_nb_positive
);
2500 NBSLOT("__abs__", nb_absolute
, slot_nb_absolute
);
2501 NBSLOT("__nonzero__", nb_nonzero
, slot_nb_nonzero
);
2502 NBSLOT("__invert__", nb_invert
, slot_nb_invert
);
2503 NBSLOT("__lshift__", nb_lshift
, slot_nb_lshift
);
2504 NBSLOT("__rshift__", nb_rshift
, slot_nb_rshift
);
2505 NBSLOT("__and__", nb_and
, slot_nb_and
);
2506 NBSLOT("__xor__", nb_xor
, slot_nb_xor
);
2507 NBSLOT("__or__", nb_or
, slot_nb_or
);
2509 NBSLOT("__int__", nb_int
, slot_nb_int
);
2510 NBSLOT("__long__", nb_long
, slot_nb_long
);
2511 NBSLOT("__float__", nb_float
, slot_nb_float
);
2512 NBSLOT("__oct__", nb_oct
, slot_nb_oct
);
2513 NBSLOT("__hex__", nb_hex
, slot_nb_hex
);
2514 NBSLOT("__iadd__", nb_inplace_add
, slot_nb_inplace_add
);
2515 NBSLOT("__isub__", nb_inplace_subtract
, slot_nb_inplace_subtract
);
2516 NBSLOT("__imul__", nb_inplace_multiply
, slot_nb_inplace_multiply
);
2517 NBSLOT("__idiv__", nb_inplace_divide
, slot_nb_inplace_divide
);
2518 NBSLOT("__imod__", nb_inplace_remainder
, slot_nb_inplace_remainder
);
2519 NBSLOT("__ipow__", nb_inplace_power
, slot_nb_inplace_power
);
2520 NBSLOT("__ilshift__", nb_inplace_lshift
, slot_nb_inplace_lshift
);
2521 NBSLOT("__irshift__", nb_inplace_rshift
, slot_nb_inplace_rshift
);
2522 NBSLOT("__iand__", nb_inplace_and
, slot_nb_inplace_and
);
2523 NBSLOT("__ixor__", nb_inplace_xor
, slot_nb_inplace_xor
);
2524 NBSLOT("__ior__", nb_inplace_or
, slot_nb_inplace_or
);
2525 NBSLOT("__floordiv__", nb_floor_divide
, slot_nb_floor_divide
);
2526 NBSLOT("__truediv__", nb_true_divide
, slot_nb_true_divide
);
2527 NBSLOT("__ifloordiv__", nb_inplace_floor_divide
,
2528 slot_nb_inplace_floor_divide
);
2529 NBSLOT("__itruediv__", nb_inplace_true_divide
,
2530 slot_nb_inplace_true_divide
);
2532 if (PyDict_GetItemString(dict
, "__str__") ||
2533 PyDict_GetItemString(dict
, "__repr__"))
2534 type
->tp_print
= NULL
;
2536 TPSLOT("__cmp__", tp_compare
, slot_tp_compare
);
2537 TPSLOT("__repr__", tp_repr
, slot_tp_repr
);
2538 TPSLOT("__hash__", tp_hash
, slot_tp_hash
);
2539 TPSLOT("__call__", tp_call
, slot_tp_call
);
2540 TPSLOT("__str__", tp_str
, slot_tp_str
);
2541 TPSLOT("__getattr__", tp_getattro
, slot_tp_getattro
);
2542 TPSLOT("__setattr__", tp_setattro
, slot_tp_setattro
);
2543 TPSLOT("__lt__", tp_richcompare
, slot_tp_richcompare
);
2544 TPSLOT("__le__", tp_richcompare
, slot_tp_richcompare
);
2545 TPSLOT("__eq__", tp_richcompare
, slot_tp_richcompare
);
2546 TPSLOT("__ne__", tp_richcompare
, slot_tp_richcompare
);
2547 TPSLOT("__gt__", tp_richcompare
, slot_tp_richcompare
);
2548 TPSLOT("__ge__", tp_richcompare
, slot_tp_richcompare
);
2549 TPSLOT("__iter__", tp_iter
, slot_tp_iter
);
2550 TPSLOT("next", tp_iternext
, slot_tp_iternext
);
2551 TPSLOT("__get__", tp_descr_get
, slot_tp_descr_get
);
2552 TPSLOT("__set__", tp_descr_set
, slot_tp_descr_set
);
2553 TPSLOT("__init__", tp_init
, slot_tp_init
);
2554 TPSLOT("__new__", tp_new
, slot_tp_new
);