cvs -z9 diff -u Include/object.h Objects/typeobject.c Index: Include/object.h =================================================================== RCS file: /cvsroot/python/python/dist/src/Include/object.h,v retrieving revision 2.111 diff -u -r2.111 object.h --- Include/object.h 8 Aug 2002 20:55:20 -0000 2.111 +++ Include/object.h 4 Sep 2002 17:29:58 -0000 @@ -327,6 +327,28 @@ } PyTypeObject; +/* The *real* layout of a type object when allocated on the heap */ +typedef struct _heaptypeobject { + /* Note: there's a dependency on the order of these members + in slotptr() in typeobject.c . */ + PyTypeObject type; + PyNumberMethods as_number; + PyMappingMethods as_mapping; + PySequenceMethods as_sequence; /* as_sequence comes after as_mapping, + so that the mapping wins when both + the mapping and the sequence define + a given operator (e.g. __getitem__). + see add_operators() in typeobject.c . */ + PyBufferProcs as_buffer; + PyObject *name, *slots; + /* here are optional user slots, followed by the members. */ +} PyHeaptypeObject; + +/* access macro to the members which are floating "behind" the object */ +#define PyHeaptype_GET_MEMBERS(etype) \ + ((PyMemberDef *)(((char *)etype) + (etype)->type.ob_type->tp_basicsize)-1) + + /* Generic type check */ PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *); #define PyObject_TypeCheck(ob, tp) \ Index: Objects/typeobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v retrieving revision 2.179 diff -u -r2.179 typeobject.c --- Objects/typeobject.c 16 Aug 2002 17:01:08 -0000 2.179 +++ Objects/typeobject.c 4 Sep 2002 17:29:59 -0000 @@ -5,24 +5,6 @@ #include -/* The *real* layout of a type object when allocated on the heap */ -/* XXX Should we publish this in a header file? */ -typedef struct { - /* Note: there's a dependency on the order of these members - in slotptr() below. */ - PyTypeObject type; - PyNumberMethods as_number; - PyMappingMethods as_mapping; - PySequenceMethods as_sequence; /* as_sequence comes after as_mapping, - so that the mapping wins when both - the mapping and the sequence define - a given operator (e.g. __getitem__). - see add_operators() below. */ - PyBufferProcs as_buffer; - PyObject *name, *slots; - PyMemberDef members[1]; -} etype; - static PyMemberDef type_members[] = { {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY}, {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY}, @@ -213,7 +195,8 @@ PyType_GenericAlloc(PyTypeObject *type, int nitems) { PyObject *obj; - const size_t size = _PyObject_VAR_SIZE(type, nitems); + const size_t size = _PyObject_VAR_SIZE(type, nitems+1); + /* note that we need to add one, for the sentinel */ if (PyType_IS_GC(type)) obj = _PyObject_GC_Malloc(size); @@ -253,7 +236,7 @@ PyMemberDef *mp; n = type->ob_size; - mp = ((etype *)type)->members; + mp = PyHeaptype_GET_MEMBERS((PyHeaptypeObject *)type); for (i = 0; i < n; i++, mp++) { if (mp->type == T_OBJECT_EX) { char *addr = (char *)self + mp->offset; @@ -318,7 +301,7 @@ PyMemberDef *mp; n = type->ob_size; - mp = ((etype *)type)->members; + mp = PyHeaptype_GET_MEMBERS((PyHeaptypeObject *)type); for (i = 0; i < n; i++, mp++) { if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) { char *addr = (char *)self + mp->offset; @@ -1010,7 +993,7 @@ static char *kwlist[] = {"name", "bases", "dict", 0}; PyObject *slots, *tmp, *newslots; PyTypeObject *type, *base, *tmptype, *winner; - etype *et; + PyHeaptypeObject *et; PyMemberDef *mp; int i, nbases, nslots, slotoffset, add_dict, add_weak; int j, may_add_dict, may_add_weak; @@ -1125,7 +1108,8 @@ /* Are slots allowed? */ nslots = PyTuple_GET_SIZE(slots); - if (nslots > 0 && base->tp_itemsize != 0) { + if (nslots > 0 && base->tp_itemsize != 0 && !PyType_Check(base)) { + /* for the special case of meta types, allow slots */ PyErr_Format(PyExc_TypeError, "nonempty __slots__ " "not supported for subtype of '%s'", @@ -1254,7 +1238,7 @@ } /* Keep name and slots alive in the extended type object */ - et = (etype *)type; + et = (PyHeaptypeObject *)type; Py_INCREF(name); et->name = name; et->slots = slots; @@ -1334,7 +1318,7 @@ } /* Add descriptors for custom slots from __slots__, or for __dict__ */ - mp = et->members; + mp = PyHeaptype_GET_MEMBERS(et); slotoffset = base->tp_basicsize; if (slots != NULL) { for (i = 0; i < nslots; i++, mp++) { @@ -1366,7 +1350,7 @@ } type->tp_basicsize = slotoffset; type->tp_itemsize = base->tp_itemsize; - type->tp_members = et->members; + type->tp_members = PyHeaptype_GET_MEMBERS(et); type->tp_getset = subtype_getsets; /* Special case some slots */ @@ -1528,13 +1512,13 @@ static void type_dealloc(PyTypeObject *type) { - etype *et; + PyHeaptypeObject *et; /* Assert this is a heap-allocated type object */ assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); _PyObject_GC_UNTRACK(type); PyObject_ClearWeakRefs((PyObject *)type); - et = (etype *)type; + et = (PyHeaptypeObject *)type; Py_XDECREF(type->tp_base); Py_XDECREF(type->tp_dict); Py_XDECREF(type->tp_bases); @@ -1610,7 +1594,7 @@ VISIT(type->tp_base); /* There's no need to visit type->tp_subclasses or - ((etype *)type)->slots, because they can't be involved + ((PyHeaptypeObject *)type)->slots, because they can't be involved in cycles; tp_subclasses is a list of weak references, and slots is a tuple of strings. */ @@ -1656,7 +1640,7 @@ A list of weak references can't be part of a cycle; and lists have their own tp_clear. - slots (in etype): + slots (in PyHeaptypeObject): A tuple of strings can't be part of a cycle. */ @@ -1677,7 +1661,7 @@ PyObject_HEAD_INIT(&PyType_Type) 0, /* ob_size */ "type", /* tp_name */ - sizeof(etype), /* tp_basicsize */ + sizeof(PyHeaptypeObject), /* tp_basicsize */ sizeof(PyMemberDef), /* tp_itemsize */ (destructor)type_dealloc, /* tp_dealloc */ 0, /* tp_print */ @@ -3839,9 +3823,10 @@ /* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper - functions. The offsets here are relative to the 'etype' structure, which - incorporates the additional structures used for numbers, sequences and - mappings. Note that multiple names may map to the same slot (e.g. __eq__, + functions. The offsets here are relative to the 'PyHeaptypeObject' + structure, which incorporates the additional structures used for numbers, + sequences and mappings. + Note that multiple names may map to the same slot (e.g. __eq__, __ne__ etc. all map to tp_richcompare) and one name may map to multiple slots (e.g. __str__ affects tp_str as well as tp_repr). The table is terminated with an all-zero entry. (This table is further initialized and @@ -3867,7 +3852,7 @@ {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ PyDoc_STR(DOC), FLAGS} #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ - {NAME, offsetof(etype, SLOT), (void *)(FUNCTION), WRAPPER, \ + {NAME, offsetof(PyHeaptypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ PyDoc_STR(DOC)} #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) @@ -4081,20 +4066,20 @@ { char *ptr; - /* Note: this depends on the order of the members of etype! */ + /* Note: this depends on the order of the members of PyHeaptypeObject! */ assert(offset >= 0); - assert(offset < offsetof(etype, as_buffer)); - if (offset >= offsetof(etype, as_sequence)) { + assert(offset < offsetof(PyHeaptypeObject, as_buffer)); + if (offset >= offsetof(PyHeaptypeObject, as_sequence)) { ptr = (void *)type->tp_as_sequence; - offset -= offsetof(etype, as_sequence); + offset -= offsetof(PyHeaptypeObject, as_sequence); } - else if (offset >= offsetof(etype, as_mapping)) { + else if (offset >= offsetof(PyHeaptypeObject, as_mapping)) { ptr = (void *)type->tp_as_mapping; - offset -= offsetof(etype, as_mapping); + offset -= offsetof(PyHeaptypeObject, as_mapping); } - else if (offset >= offsetof(etype, as_number)) { + else if (offset >= offsetof(PyHeaptypeObject, as_number)) { ptr = (void *)type->tp_as_number; - offset -= offsetof(etype, as_number); + offset -= offsetof(PyHeaptypeObject, as_number); } else { ptr = (void *)type; @@ -4357,9 +4342,9 @@ mp_subscript generate a __getitem__ descriptor). In the latter case, the first slotdef entry encoutered wins. Since - slotdef entries are sorted by the offset of the slot in the etype - struct, this gives us some control over disambiguating between - competing slots: the members of struct etype are listed from most + slotdef entries are sorted by the offset of the slot in the + PyHeaptypeObject, this gives us some control over disambiguating + between competing slots: the members of PyHeaptypeObject are listed from most general to least general, so the most general slot is preferred. In particular, because as_mapping comes before as_sequence, for a type that defines both mp_subscript and sq_item, mp_subscript wins. *****CVS exited normally with code 1*****