Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* Type object implementation */ |
| 2 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3 | #include "Python.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 6 | #include <ctype.h> |
| 7 | |
| 8 | /* The *real* layout of a type object when allocated on the heap */ |
| 9 | /* XXX Should we publish this in a header file? */ |
| 10 | typedef struct { |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 11 | /* Note: there's a dependency on the order of these members |
| 12 | in slotptr() below. */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 13 | PyTypeObject type; |
| 14 | PyNumberMethods as_number; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 15 | PyMappingMethods as_mapping; |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 16 | PySequenceMethods as_sequence; /* as_sequence comes after as_mapping, |
| 17 | so that the mapping wins when both |
| 18 | the mapping and the sequence define |
| 19 | a given operator (e.g. __getitem__). |
| 20 | see add_operators() below. */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 21 | PyBufferProcs as_buffer; |
| 22 | PyObject *name, *slots; |
| 23 | PyMemberDef members[1]; |
| 24 | } etype; |
| 25 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 26 | static PyMemberDef type_members[] = { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 27 | {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY}, |
| 28 | {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY}, |
| 29 | {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY}, |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 30 | {"__weakrefoffset__", T_LONG, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 31 | offsetof(PyTypeObject, tp_weaklistoffset), READONLY}, |
| 32 | {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY}, |
| 33 | {"__dictoffset__", T_LONG, |
| 34 | offsetof(PyTypeObject, tp_dictoffset), READONLY}, |
| 35 | {"__bases__", T_OBJECT, offsetof(PyTypeObject, tp_bases), READONLY}, |
| 36 | {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY}, |
| 37 | {0} |
| 38 | }; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 40 | static PyObject * |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 41 | type_name(PyTypeObject *type, void *context) |
| 42 | { |
| 43 | char *s; |
| 44 | |
| 45 | s = strrchr(type->tp_name, '.'); |
| 46 | if (s == NULL) |
| 47 | s = type->tp_name; |
| 48 | else |
| 49 | s++; |
| 50 | return PyString_FromString(s); |
| 51 | } |
| 52 | |
| 53 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 54 | type_module(PyTypeObject *type, void *context) |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 55 | { |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 56 | PyObject *mod; |
| 57 | char *s; |
| 58 | |
| 59 | s = strrchr(type->tp_name, '.'); |
| 60 | if (s != NULL) |
| 61 | return PyString_FromStringAndSize(type->tp_name, |
| 62 | (int)(s - type->tp_name)); |
| 63 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 64 | return PyString_FromString("__builtin__"); |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 65 | mod = PyDict_GetItemString(type->tp_dict, "__module__"); |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 66 | if (mod != NULL && PyString_Check(mod)) { |
| 67 | Py_INCREF(mod); |
| 68 | return mod; |
| 69 | } |
| 70 | PyErr_SetString(PyExc_AttributeError, "__module__"); |
| 71 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 74 | static int |
| 75 | type_set_module(PyTypeObject *type, PyObject *value, void *context) |
| 76 | { |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 77 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) || |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 78 | strrchr(type->tp_name, '.')) { |
| 79 | PyErr_Format(PyExc_TypeError, |
| 80 | "can't set %s.__module__", type->tp_name); |
| 81 | return -1; |
| 82 | } |
| 83 | if (!value) { |
| 84 | PyErr_Format(PyExc_TypeError, |
| 85 | "can't delete %s.__module__", type->tp_name); |
| 86 | return -1; |
| 87 | } |
| 88 | return PyDict_SetItemString(type->tp_dict, "__module__", value); |
| 89 | } |
| 90 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 91 | static PyObject * |
| 92 | type_dict(PyTypeObject *type, void *context) |
| 93 | { |
| 94 | if (type->tp_dict == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 95 | Py_INCREF(Py_None); |
| 96 | return Py_None; |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 97 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 98 | return PyDictProxy_New(type->tp_dict); |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 101 | static PyObject * |
| 102 | type_get_doc(PyTypeObject *type, void *context) |
| 103 | { |
| 104 | PyObject *result; |
Guido van Rossum | 6ca7d41 | 2002-04-18 00:22:00 +0000 | [diff] [blame] | 105 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 106 | return PyString_FromString(type->tp_doc); |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 107 | result = PyDict_GetItemString(type->tp_dict, "__doc__"); |
Guido van Rossum | 6ca7d41 | 2002-04-18 00:22:00 +0000 | [diff] [blame] | 108 | if (result == NULL) { |
| 109 | result = Py_None; |
| 110 | Py_INCREF(result); |
| 111 | } |
| 112 | else if (result->ob_type->tp_descr_get) { |
Tim Peters | 2b85897 | 2002-04-18 04:12:28 +0000 | [diff] [blame] | 113 | result = result->ob_type->tp_descr_get(result, NULL, |
| 114 | (PyObject *)type); |
Guido van Rossum | 6ca7d41 | 2002-04-18 00:22:00 +0000 | [diff] [blame] | 115 | } |
| 116 | else { |
| 117 | Py_INCREF(result); |
| 118 | } |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 119 | return result; |
| 120 | } |
| 121 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 122 | static PyGetSetDef type_getsets[] = { |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 123 | {"__name__", (getter)type_name, NULL, NULL}, |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 124 | {"__module__", (getter)type_module, (setter)type_set_module, NULL}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 125 | {"__dict__", (getter)type_dict, NULL, NULL}, |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 126 | {"__doc__", (getter)type_get_doc, NULL, NULL}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 127 | {0} |
| 128 | }; |
| 129 | |
Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 130 | static int |
| 131 | type_compare(PyObject *v, PyObject *w) |
| 132 | { |
| 133 | /* This is called with type objects only. So we |
| 134 | can just compare the addresses. */ |
| 135 | Py_uintptr_t vv = (Py_uintptr_t)v; |
| 136 | Py_uintptr_t ww = (Py_uintptr_t)w; |
| 137 | return (vv < ww) ? -1 : (vv > ww) ? 1 : 0; |
| 138 | } |
| 139 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 140 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 141 | type_repr(PyTypeObject *type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 142 | { |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 143 | PyObject *mod, *name, *rtn; |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 144 | char *kind; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 145 | |
| 146 | mod = type_module(type, NULL); |
| 147 | if (mod == NULL) |
| 148 | PyErr_Clear(); |
| 149 | else if (!PyString_Check(mod)) { |
| 150 | Py_DECREF(mod); |
| 151 | mod = NULL; |
| 152 | } |
| 153 | name = type_name(type, NULL); |
| 154 | if (name == NULL) |
| 155 | return NULL; |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 156 | |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 157 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 158 | kind = "class"; |
| 159 | else |
| 160 | kind = "type"; |
| 161 | |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 162 | if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) { |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 163 | rtn = PyString_FromFormat("<%s '%s.%s'>", |
| 164 | kind, |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 165 | PyString_AS_STRING(mod), |
| 166 | PyString_AS_STRING(name)); |
| 167 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 168 | else |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 169 | rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 170 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 171 | Py_XDECREF(mod); |
| 172 | Py_DECREF(name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 173 | return rtn; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 176 | static PyObject * |
| 177 | type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 178 | { |
| 179 | PyObject *obj; |
| 180 | |
| 181 | if (type->tp_new == NULL) { |
| 182 | PyErr_Format(PyExc_TypeError, |
| 183 | "cannot create '%.100s' instances", |
| 184 | type->tp_name); |
| 185 | return NULL; |
| 186 | } |
| 187 | |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 188 | obj = type->tp_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 189 | if (obj != NULL) { |
Guido van Rossum | f76de62 | 2001-10-18 15:49:21 +0000 | [diff] [blame] | 190 | /* Ugly exception: when the call was type(something), |
| 191 | don't call tp_init on the result. */ |
| 192 | if (type == &PyType_Type && |
| 193 | PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && |
| 194 | (kwds == NULL || |
| 195 | (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) |
| 196 | return obj; |
Guido van Rossum | 8ace1ab | 2002-04-06 01:05:01 +0000 | [diff] [blame] | 197 | /* If the returned object is not an instance of type, |
| 198 | it won't be initialized. */ |
| 199 | if (!PyType_IsSubtype(obj->ob_type, type)) |
| 200 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 201 | type = obj->ob_type; |
Jeremy Hylton | 719841e | 2002-07-16 19:39:38 +0000 | [diff] [blame] | 202 | if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) && |
| 203 | type->tp_init != NULL && |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 204 | type->tp_init(obj, args, kwds) < 0) { |
| 205 | Py_DECREF(obj); |
| 206 | obj = NULL; |
| 207 | } |
| 208 | } |
| 209 | return obj; |
| 210 | } |
| 211 | |
| 212 | PyObject * |
| 213 | PyType_GenericAlloc(PyTypeObject *type, int nitems) |
| 214 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 215 | PyObject *obj; |
Tim Peters | f2a67da | 2001-10-07 03:54:51 +0000 | [diff] [blame] | 216 | const size_t size = _PyObject_VAR_SIZE(type, nitems); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 217 | |
| 218 | if (PyType_IS_GC(type)) |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 219 | obj = _PyObject_GC_Malloc(size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 220 | else |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 221 | obj = PyObject_MALLOC(size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 222 | |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 223 | if (obj == NULL) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 224 | return PyErr_NoMemory(); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 225 | |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 226 | memset(obj, '\0', size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 227 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 228 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 229 | Py_INCREF(type); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 230 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 231 | if (type->tp_itemsize == 0) |
| 232 | PyObject_INIT(obj, type); |
| 233 | else |
| 234 | (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 235 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 236 | if (PyType_IS_GC(type)) |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 237 | _PyObject_GC_TRACK(obj); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 238 | return obj; |
| 239 | } |
| 240 | |
| 241 | PyObject * |
| 242 | PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 243 | { |
| 244 | return type->tp_alloc(type, 0); |
| 245 | } |
| 246 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 247 | /* Helpers for subtyping */ |
| 248 | |
| 249 | static int |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 250 | traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg) |
| 251 | { |
| 252 | int i, n; |
| 253 | PyMemberDef *mp; |
| 254 | |
| 255 | n = type->ob_size; |
| 256 | mp = ((etype *)type)->members; |
| 257 | for (i = 0; i < n; i++, mp++) { |
| 258 | if (mp->type == T_OBJECT_EX) { |
| 259 | char *addr = (char *)self + mp->offset; |
| 260 | PyObject *obj = *(PyObject **)addr; |
| 261 | if (obj != NULL) { |
| 262 | int err = visit(obj, arg); |
| 263 | if (err) |
| 264 | return err; |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static int |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 272 | subtype_traverse(PyObject *self, visitproc visit, void *arg) |
| 273 | { |
| 274 | PyTypeObject *type, *base; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 275 | traverseproc basetraverse; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 276 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 277 | /* Find the nearest base with a different tp_traverse, |
| 278 | and traverse slots while we're at it */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 279 | type = self->ob_type; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 280 | base = type; |
| 281 | while ((basetraverse = base->tp_traverse) == subtype_traverse) { |
| 282 | if (base->ob_size) { |
| 283 | int err = traverse_slots(base, self, visit, arg); |
| 284 | if (err) |
| 285 | return err; |
| 286 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 287 | base = base->tp_base; |
| 288 | assert(base); |
| 289 | } |
| 290 | |
| 291 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 292 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 293 | if (dictptr && *dictptr) { |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 294 | int err = visit(*dictptr, arg); |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 295 | if (err) |
| 296 | return err; |
| 297 | } |
| 298 | } |
| 299 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 300 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 301 | /* For a heaptype, the instances count as references |
| 302 | to the type. Traverse the type so the collector |
| 303 | can find cycles involving this link. */ |
| 304 | int err = visit((PyObject *)type, arg); |
| 305 | if (err) |
| 306 | return err; |
| 307 | } |
| 308 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 309 | if (basetraverse) |
| 310 | return basetraverse(self, visit, arg); |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | static void |
| 315 | clear_slots(PyTypeObject *type, PyObject *self) |
| 316 | { |
| 317 | int i, n; |
| 318 | PyMemberDef *mp; |
| 319 | |
| 320 | n = type->ob_size; |
| 321 | mp = ((etype *)type)->members; |
| 322 | for (i = 0; i < n; i++, mp++) { |
| 323 | if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) { |
| 324 | char *addr = (char *)self + mp->offset; |
| 325 | PyObject *obj = *(PyObject **)addr; |
| 326 | if (obj != NULL) { |
| 327 | Py_DECREF(obj); |
| 328 | *(PyObject **)addr = NULL; |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | static int |
| 335 | subtype_clear(PyObject *self) |
| 336 | { |
| 337 | PyTypeObject *type, *base; |
| 338 | inquiry baseclear; |
| 339 | |
| 340 | /* Find the nearest base with a different tp_clear |
| 341 | and clear slots while we're at it */ |
| 342 | type = self->ob_type; |
| 343 | base = type; |
| 344 | while ((baseclear = base->tp_clear) == subtype_clear) { |
| 345 | if (base->ob_size) |
| 346 | clear_slots(base, self); |
| 347 | base = base->tp_base; |
| 348 | assert(base); |
| 349 | } |
| 350 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 351 | /* There's no need to clear the instance dict (if any); |
| 352 | the collector will call its tp_clear handler. */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 353 | |
| 354 | if (baseclear) |
| 355 | return baseclear(self); |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 356 | return 0; |
| 357 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 358 | |
| 359 | static void |
| 360 | subtype_dealloc(PyObject *self) |
| 361 | { |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 362 | PyTypeObject *type, *base; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 363 | destructor basedealloc; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 364 | |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 365 | /* Extract the type; we expect it to be a heap type */ |
| 366 | type = self->ob_type; |
| 367 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 368 | |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 369 | /* Test whether the type has GC exactly once */ |
| 370 | |
| 371 | if (!PyType_IS_GC(type)) { |
| 372 | /* It's really rare to find a dynamic type that doesn't have |
| 373 | GC; it can only happen when deriving from 'object' and not |
| 374 | adding any slots or instance variables. This allows |
| 375 | certain simplifications: there's no need to call |
| 376 | clear_slots(), or DECREF the dict, or clear weakrefs. */ |
| 377 | |
| 378 | /* Maybe call finalizer; exit early if resurrected */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 379 | if (type->tp_del) { |
| 380 | type->tp_del(self); |
| 381 | if (self->ob_refcnt > 0) |
| 382 | return; |
| 383 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 384 | |
| 385 | /* Find the nearest base with a different tp_dealloc */ |
| 386 | base = type; |
| 387 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 388 | assert(base->ob_size == 0); |
| 389 | base = base->tp_base; |
| 390 | assert(base); |
| 391 | } |
| 392 | |
| 393 | /* Call the base tp_dealloc() */ |
| 394 | assert(basedealloc); |
| 395 | basedealloc(self); |
| 396 | |
| 397 | /* Can't reference self beyond this point */ |
| 398 | Py_DECREF(type); |
| 399 | |
| 400 | /* Done */ |
| 401 | return; |
| 402 | } |
| 403 | |
| 404 | /* We get here only if the type has GC */ |
| 405 | |
| 406 | /* UnTrack and re-Track around the trashcan macro, alas */ |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 407 | PyObject_GC_UnTrack(self); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 408 | Py_TRASHCAN_SAFE_BEGIN(self); |
| 409 | _PyObject_GC_TRACK(self); /* We'll untrack for real later */ |
| 410 | |
| 411 | /* Maybe call finalizer; exit early if resurrected */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 412 | if (type->tp_del) { |
| 413 | type->tp_del(self); |
| 414 | if (self->ob_refcnt > 0) |
| 415 | goto endlabel; |
| 416 | } |
Guido van Rossum | 7ad2d1e | 2001-10-29 22:11:00 +0000 | [diff] [blame] | 417 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 418 | /* Find the nearest base with a different tp_dealloc |
| 419 | and clear slots while we're at it */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 420 | base = type; |
| 421 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 422 | if (base->ob_size) |
| 423 | clear_slots(base, self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 424 | base = base->tp_base; |
| 425 | assert(base); |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 428 | /* If we added a dict, DECREF it */ |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 429 | if (type->tp_dictoffset && !base->tp_dictoffset) { |
| 430 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 431 | if (dictptr != NULL) { |
| 432 | PyObject *dict = *dictptr; |
| 433 | if (dict != NULL) { |
| 434 | Py_DECREF(dict); |
| 435 | *dictptr = NULL; |
| 436 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 440 | /* If we added weaklist, we clear it */ |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 441 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 442 | PyObject_ClearWeakRefs(self); |
| 443 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 444 | /* Finalize GC if the base doesn't do GC and we do */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 445 | if (!PyType_IS_GC(base)) |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 446 | _PyObject_GC_UNTRACK(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 447 | |
| 448 | /* Call the base tp_dealloc() */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 449 | assert(basedealloc); |
| 450 | basedealloc(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 451 | |
| 452 | /* Can't reference self beyond this point */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 453 | Py_DECREF(type); |
| 454 | |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 455 | endlabel: |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 456 | Py_TRASHCAN_SAFE_END(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 459 | static PyTypeObject *solid_base(PyTypeObject *type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 460 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 461 | /* type test with subclassing support */ |
| 462 | |
| 463 | int |
| 464 | PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
| 465 | { |
| 466 | PyObject *mro; |
| 467 | |
Guido van Rossum | 9478d07 | 2001-09-07 18:52:13 +0000 | [diff] [blame] | 468 | if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS)) |
| 469 | return b == a || b == &PyBaseObject_Type; |
| 470 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 471 | mro = a->tp_mro; |
| 472 | if (mro != NULL) { |
| 473 | /* Deal with multiple inheritance without recursion |
| 474 | by walking the MRO tuple */ |
| 475 | int i, n; |
| 476 | assert(PyTuple_Check(mro)); |
| 477 | n = PyTuple_GET_SIZE(mro); |
| 478 | for (i = 0; i < n; i++) { |
| 479 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) |
| 480 | return 1; |
| 481 | } |
| 482 | return 0; |
| 483 | } |
| 484 | else { |
| 485 | /* a is not completely initilized yet; follow tp_base */ |
| 486 | do { |
| 487 | if (a == b) |
| 488 | return 1; |
| 489 | a = a->tp_base; |
| 490 | } while (a != NULL); |
| 491 | return b == &PyBaseObject_Type; |
| 492 | } |
| 493 | } |
| 494 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 495 | /* Internal routines to do a method lookup in the type |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 496 | without looking in the instance dictionary |
| 497 | (so we can't use PyObject_GetAttr) but still binding |
| 498 | it to the instance. The arguments are the object, |
| 499 | the method name as a C string, and the address of a |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 500 | static variable used to cache the interned Python string. |
| 501 | |
| 502 | Two variants: |
| 503 | |
| 504 | - lookup_maybe() returns NULL without raising an exception |
| 505 | when the _PyType_Lookup() call fails; |
| 506 | |
| 507 | - lookup_method() always raises an exception upon errors. |
| 508 | */ |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 509 | |
| 510 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 511 | lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 512 | { |
| 513 | PyObject *res; |
| 514 | |
| 515 | if (*attrobj == NULL) { |
| 516 | *attrobj = PyString_InternFromString(attrstr); |
| 517 | if (*attrobj == NULL) |
| 518 | return NULL; |
| 519 | } |
| 520 | res = _PyType_Lookup(self->ob_type, *attrobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 521 | if (res != NULL) { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 522 | descrgetfunc f; |
| 523 | if ((f = res->ob_type->tp_descr_get) == NULL) |
| 524 | Py_INCREF(res); |
| 525 | else |
| 526 | res = f(res, self, (PyObject *)(self->ob_type)); |
| 527 | } |
| 528 | return res; |
| 529 | } |
| 530 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 531 | static PyObject * |
| 532 | lookup_method(PyObject *self, char *attrstr, PyObject **attrobj) |
| 533 | { |
| 534 | PyObject *res = lookup_maybe(self, attrstr, attrobj); |
| 535 | if (res == NULL && !PyErr_Occurred()) |
| 536 | PyErr_SetObject(PyExc_AttributeError, *attrobj); |
| 537 | return res; |
| 538 | } |
| 539 | |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 540 | /* A variation of PyObject_CallMethod that uses lookup_method() |
| 541 | instead of PyObject_GetAttrString(). This uses the same convention |
| 542 | as lookup_method to cache the interned name string object. */ |
| 543 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 544 | static PyObject * |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 545 | call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 546 | { |
| 547 | va_list va; |
| 548 | PyObject *args, *func = 0, *retval; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 549 | va_start(va, format); |
| 550 | |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 551 | func = lookup_maybe(o, name, nameobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 552 | if (func == NULL) { |
| 553 | va_end(va); |
| 554 | if (!PyErr_Occurred()) |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 555 | PyErr_SetObject(PyExc_AttributeError, *nameobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 556 | return NULL; |
| 557 | } |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 558 | |
| 559 | if (format && *format) |
| 560 | args = Py_VaBuildValue(format, va); |
| 561 | else |
| 562 | args = PyTuple_New(0); |
| 563 | |
| 564 | va_end(va); |
| 565 | |
| 566 | if (args == NULL) |
| 567 | return NULL; |
| 568 | |
| 569 | assert(PyTuple_Check(args)); |
| 570 | retval = PyObject_Call(func, args, NULL); |
| 571 | |
| 572 | Py_DECREF(args); |
| 573 | Py_DECREF(func); |
| 574 | |
| 575 | return retval; |
| 576 | } |
| 577 | |
| 578 | /* Clone of call_method() that returns NotImplemented when the lookup fails. */ |
| 579 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 580 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 581 | call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 582 | { |
| 583 | va_list va; |
| 584 | PyObject *args, *func = 0, *retval; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 585 | va_start(va, format); |
| 586 | |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 587 | func = lookup_maybe(o, name, nameobj); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 588 | if (func == NULL) { |
| 589 | va_end(va); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 590 | if (!PyErr_Occurred()) { |
| 591 | Py_INCREF(Py_NotImplemented); |
| 592 | return Py_NotImplemented; |
| 593 | } |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 594 | return NULL; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | if (format && *format) |
| 598 | args = Py_VaBuildValue(format, va); |
| 599 | else |
| 600 | args = PyTuple_New(0); |
| 601 | |
| 602 | va_end(va); |
| 603 | |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 604 | if (args == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 605 | return NULL; |
| 606 | |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 607 | assert(PyTuple_Check(args)); |
| 608 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 609 | |
| 610 | Py_DECREF(args); |
| 611 | Py_DECREF(func); |
| 612 | |
| 613 | return retval; |
| 614 | } |
| 615 | |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 616 | static int |
| 617 | fill_classic_mro(PyObject *mro, PyObject *cls) |
| 618 | { |
| 619 | PyObject *bases, *base; |
| 620 | int i, n; |
| 621 | |
| 622 | assert(PyList_Check(mro)); |
| 623 | assert(PyClass_Check(cls)); |
| 624 | i = PySequence_Contains(mro, cls); |
| 625 | if (i < 0) |
| 626 | return -1; |
| 627 | if (!i) { |
| 628 | if (PyList_Append(mro, cls) < 0) |
| 629 | return -1; |
| 630 | } |
| 631 | bases = ((PyClassObject *)cls)->cl_bases; |
| 632 | assert(bases && PyTuple_Check(bases)); |
| 633 | n = PyTuple_GET_SIZE(bases); |
| 634 | for (i = 0; i < n; i++) { |
| 635 | base = PyTuple_GET_ITEM(bases, i); |
| 636 | if (fill_classic_mro(mro, base) < 0) |
| 637 | return -1; |
| 638 | } |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | static PyObject * |
| 643 | classic_mro(PyObject *cls) |
| 644 | { |
| 645 | PyObject *mro; |
| 646 | |
| 647 | assert(PyClass_Check(cls)); |
| 648 | mro = PyList_New(0); |
| 649 | if (mro != NULL) { |
| 650 | if (fill_classic_mro(mro, cls) == 0) |
| 651 | return mro; |
| 652 | Py_DECREF(mro); |
| 653 | } |
| 654 | return NULL; |
| 655 | } |
| 656 | |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 657 | /* |
| 658 | Method resolution order algorithm C3 described in |
| 659 | "A Monotonic Superclass Linearization for Dylan", |
| 660 | by Kim Barrett, Bob Cassel, Paul Haahr, |
| 661 | David A. Moon, Keith Playford, and P. Tucker Withington. |
| 662 | (OOPSLA 1996) |
| 663 | |
| 664 | */ |
| 665 | |
| 666 | static int |
| 667 | tail_contains(PyObject *list, int whence, PyObject *o) { |
| 668 | int j, size; |
| 669 | size = PyList_GET_SIZE(list); |
| 670 | |
| 671 | for (j = whence+1; j < size; j++) { |
| 672 | if (PyList_GET_ITEM(list, j) == o) |
| 673 | return 1; |
| 674 | } |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | static int |
| 679 | pmerge(PyObject *acc, PyObject* to_merge) { |
| 680 | int i, j, to_merge_size; |
| 681 | int *remain; |
| 682 | int ok, empty_cnt; |
| 683 | |
| 684 | to_merge_size = PyList_GET_SIZE(to_merge); |
| 685 | |
| 686 | remain = PyMem_MALLOC(SIZEOF_INT*to_merge_size); |
| 687 | if (remain == NULL) |
| 688 | return -1; |
| 689 | for (i = 0; i < to_merge_size; i++) |
| 690 | remain[i] = 0; |
| 691 | |
| 692 | again: |
| 693 | empty_cnt = 0; |
| 694 | for (i = 0; i < to_merge_size; i++) { |
| 695 | PyObject *candidate; |
| 696 | |
| 697 | PyObject *cur_list = PyList_GET_ITEM(to_merge, i); |
| 698 | |
| 699 | if (remain[i] >= PyList_GET_SIZE(cur_list)) { |
| 700 | empty_cnt++; |
| 701 | continue; |
| 702 | } |
| 703 | |
| 704 | candidate = PyList_GET_ITEM(cur_list, remain[i]); |
| 705 | for (j = 0; j < to_merge_size; j++) { |
| 706 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
| 707 | if (tail_contains(j_lst, remain[j], candidate)) |
| 708 | goto skip; /* continue outer loop */ |
| 709 | } |
| 710 | ok = PyList_Append(acc, candidate); |
| 711 | if (ok < 0) { |
| 712 | PyMem_Free(remain); |
| 713 | return -1; |
| 714 | } |
| 715 | for (j = 0; j < to_merge_size; j++) { |
| 716 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
| 717 | if (PyList_GET_ITEM(j_lst, remain[j]) == candidate) { |
| 718 | remain[j]++; |
| 719 | } |
| 720 | } |
| 721 | goto again; |
| 722 | skip: |
| 723 | } |
| 724 | |
| 725 | PyMem_FREE(remain); |
| 726 | if (empty_cnt == to_merge_size) |
| 727 | return 0; |
| 728 | PyErr_SetString(PyExc_TypeError, "MRO order disagreement"); |
| 729 | return -1; |
| 730 | } |
| 731 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 732 | static PyObject * |
| 733 | mro_implementation(PyTypeObject *type) |
| 734 | { |
| 735 | int i, n, ok; |
| 736 | PyObject *bases, *result; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 737 | PyObject *to_merge, *bases_aslist; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 738 | |
Guido van Rossum | 6351757 | 2002-06-18 16:44:57 +0000 | [diff] [blame] | 739 | if(type->tp_dict == NULL) { |
| 740 | if(PyType_Ready(type) < 0) |
| 741 | return NULL; |
| 742 | } |
| 743 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 744 | bases = type->tp_bases; |
| 745 | n = PyTuple_GET_SIZE(bases); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 746 | |
| 747 | to_merge = PyList_New(n+1); |
| 748 | if (to_merge == NULL) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 749 | return NULL; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 750 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 751 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 752 | PyObject *base = PyTuple_GET_ITEM(bases, i); |
| 753 | PyObject *parentMRO; |
| 754 | if (PyType_Check(base)) |
| 755 | parentMRO = PySequence_List( |
| 756 | ((PyTypeObject*)base)->tp_mro); |
| 757 | else |
| 758 | parentMRO = classic_mro(base); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 759 | if (parentMRO == NULL) { |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 760 | Py_DECREF(to_merge); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 761 | return NULL; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | PyList_SET_ITEM(to_merge, i, parentMRO); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 765 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 766 | |
| 767 | bases_aslist = PySequence_List(bases); |
| 768 | if (bases_aslist == NULL) { |
| 769 | Py_DECREF(to_merge); |
| 770 | return NULL; |
| 771 | } |
| 772 | PyList_SET_ITEM(to_merge, n, bases_aslist); |
| 773 | |
| 774 | result = Py_BuildValue("[O]", (PyObject *)type); |
| 775 | if (result == NULL) { |
| 776 | Py_DECREF(to_merge); |
| 777 | return NULL; |
| 778 | } |
| 779 | |
| 780 | ok = pmerge(result, to_merge); |
| 781 | Py_DECREF(to_merge); |
| 782 | if (ok < 0) { |
| 783 | Py_DECREF(result); |
| 784 | return NULL; |
| 785 | } |
| 786 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 787 | return result; |
| 788 | } |
| 789 | |
| 790 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 791 | mro_external(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 792 | { |
| 793 | PyTypeObject *type = (PyTypeObject *)self; |
| 794 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 795 | return mro_implementation(type); |
| 796 | } |
| 797 | |
| 798 | static int |
| 799 | mro_internal(PyTypeObject *type) |
| 800 | { |
| 801 | PyObject *mro, *result, *tuple; |
| 802 | |
| 803 | if (type->ob_type == &PyType_Type) { |
| 804 | result = mro_implementation(type); |
| 805 | } |
| 806 | else { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 807 | static PyObject *mro_str; |
| 808 | mro = lookup_method((PyObject *)type, "mro", &mro_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 809 | if (mro == NULL) |
| 810 | return -1; |
| 811 | result = PyObject_CallObject(mro, NULL); |
| 812 | Py_DECREF(mro); |
| 813 | } |
| 814 | if (result == NULL) |
| 815 | return -1; |
| 816 | tuple = PySequence_Tuple(result); |
| 817 | Py_DECREF(result); |
| 818 | type->tp_mro = tuple; |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | |
| 823 | /* Calculate the best base amongst multiple base classes. |
| 824 | This is the first one that's on the path to the "solid base". */ |
| 825 | |
| 826 | static PyTypeObject * |
| 827 | best_base(PyObject *bases) |
| 828 | { |
| 829 | int i, n; |
| 830 | PyTypeObject *base, *winner, *candidate, *base_i; |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 831 | PyObject *base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 832 | |
| 833 | assert(PyTuple_Check(bases)); |
| 834 | n = PyTuple_GET_SIZE(bases); |
| 835 | assert(n > 0); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 836 | base = NULL; |
| 837 | winner = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 838 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 839 | base_proto = PyTuple_GET_ITEM(bases, i); |
| 840 | if (PyClass_Check(base_proto)) |
| 841 | continue; |
| 842 | if (!PyType_Check(base_proto)) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 843 | PyErr_SetString( |
| 844 | PyExc_TypeError, |
| 845 | "bases must be types"); |
| 846 | return NULL; |
| 847 | } |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 848 | base_i = (PyTypeObject *)base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 849 | if (base_i->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 850 | if (PyType_Ready(base_i) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 851 | return NULL; |
| 852 | } |
| 853 | candidate = solid_base(base_i); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 854 | if (winner == NULL) { |
| 855 | winner = candidate; |
| 856 | base = base_i; |
| 857 | } |
| 858 | else if (PyType_IsSubtype(winner, candidate)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 859 | ; |
| 860 | else if (PyType_IsSubtype(candidate, winner)) { |
| 861 | winner = candidate; |
| 862 | base = base_i; |
| 863 | } |
| 864 | else { |
| 865 | PyErr_SetString( |
| 866 | PyExc_TypeError, |
| 867 | "multiple bases have " |
| 868 | "instance lay-out conflict"); |
| 869 | return NULL; |
| 870 | } |
| 871 | } |
Guido van Rossum | e54616c | 2001-12-14 04:19:56 +0000 | [diff] [blame] | 872 | if (base == NULL) |
| 873 | PyErr_SetString(PyExc_TypeError, |
| 874 | "a new-style class can't have only classic bases"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 875 | return base; |
| 876 | } |
| 877 | |
| 878 | static int |
| 879 | extra_ivars(PyTypeObject *type, PyTypeObject *base) |
| 880 | { |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 881 | size_t t_size = type->tp_basicsize; |
| 882 | size_t b_size = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 883 | |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 884 | assert(t_size >= b_size); /* Else type smaller than base! */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 885 | if (type->tp_itemsize || base->tp_itemsize) { |
| 886 | /* If itemsize is involved, stricter rules */ |
| 887 | return t_size != b_size || |
| 888 | type->tp_itemsize != base->tp_itemsize; |
| 889 | } |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 890 | if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 && |
| 891 | type->tp_weaklistoffset + sizeof(PyObject *) == t_size) |
| 892 | t_size -= sizeof(PyObject *); |
| 893 | if (type->tp_dictoffset && base->tp_dictoffset == 0 && |
| 894 | type->tp_dictoffset + sizeof(PyObject *) == t_size) |
| 895 | t_size -= sizeof(PyObject *); |
| 896 | |
| 897 | return t_size != b_size; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | static PyTypeObject * |
| 901 | solid_base(PyTypeObject *type) |
| 902 | { |
| 903 | PyTypeObject *base; |
| 904 | |
| 905 | if (type->tp_base) |
| 906 | base = solid_base(type->tp_base); |
| 907 | else |
| 908 | base = &PyBaseObject_Type; |
| 909 | if (extra_ivars(type, base)) |
| 910 | return type; |
| 911 | else |
| 912 | return base; |
| 913 | } |
| 914 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 915 | static void object_dealloc(PyObject *); |
| 916 | static int object_init(PyObject *, PyObject *, PyObject *); |
| 917 | static int update_slot(PyTypeObject *, PyObject *); |
| 918 | static void fixup_slot_dispatchers(PyTypeObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 919 | |
| 920 | static PyObject * |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 921 | subtype_dict(PyObject *obj, void *context) |
| 922 | { |
| 923 | PyObject **dictptr = _PyObject_GetDictPtr(obj); |
| 924 | PyObject *dict; |
| 925 | |
| 926 | if (dictptr == NULL) { |
| 927 | PyErr_SetString(PyExc_AttributeError, |
| 928 | "This object has no __dict__"); |
| 929 | return NULL; |
| 930 | } |
| 931 | dict = *dictptr; |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 932 | if (dict == NULL) |
| 933 | *dictptr = dict = PyDict_New(); |
| 934 | Py_XINCREF(dict); |
| 935 | return dict; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 938 | static int |
| 939 | subtype_setdict(PyObject *obj, PyObject *value, void *context) |
| 940 | { |
| 941 | PyObject **dictptr = _PyObject_GetDictPtr(obj); |
| 942 | PyObject *dict; |
| 943 | |
| 944 | if (dictptr == NULL) { |
| 945 | PyErr_SetString(PyExc_AttributeError, |
| 946 | "This object has no __dict__"); |
| 947 | return -1; |
| 948 | } |
Guido van Rossum | d331cb5 | 2001-12-05 19:46:42 +0000 | [diff] [blame] | 949 | if (value != NULL && !PyDict_Check(value)) { |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 950 | PyErr_SetString(PyExc_TypeError, |
| 951 | "__dict__ must be set to a dictionary"); |
| 952 | return -1; |
| 953 | } |
| 954 | dict = *dictptr; |
Guido van Rossum | d331cb5 | 2001-12-05 19:46:42 +0000 | [diff] [blame] | 955 | Py_XINCREF(value); |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 956 | *dictptr = value; |
| 957 | Py_XDECREF(dict); |
| 958 | return 0; |
| 959 | } |
| 960 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 961 | static PyObject * |
| 962 | subtype_getweakref(PyObject *obj, void *context) |
| 963 | { |
| 964 | PyObject **weaklistptr; |
| 965 | PyObject *result; |
| 966 | |
| 967 | if (obj->ob_type->tp_weaklistoffset == 0) { |
| 968 | PyErr_SetString(PyExc_AttributeError, |
| 969 | "This object has no __weaklist__"); |
| 970 | return NULL; |
| 971 | } |
| 972 | assert(obj->ob_type->tp_weaklistoffset > 0); |
| 973 | assert(obj->ob_type->tp_weaklistoffset + sizeof(PyObject *) <= |
Guido van Rossum | 3747a0f | 2002-08-12 19:25:08 +0000 | [diff] [blame] | 974 | (size_t)(obj->ob_type->tp_basicsize)); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 975 | weaklistptr = (PyObject **) |
Guido van Rossum | 3747a0f | 2002-08-12 19:25:08 +0000 | [diff] [blame] | 976 | ((char *)obj + obj->ob_type->tp_weaklistoffset); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 977 | if (*weaklistptr == NULL) |
| 978 | result = Py_None; |
| 979 | else |
| 980 | result = *weaklistptr; |
| 981 | Py_INCREF(result); |
| 982 | return result; |
| 983 | } |
| 984 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 985 | static PyGetSetDef subtype_getsets[] = { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 986 | /* Not all objects have these attributes! |
| 987 | The descriptor's __get__ method may raise AttributeError. */ |
| 988 | {"__dict__", subtype_dict, subtype_setdict, |
Neal Norwitz | 858e34f | 2002-08-13 17:18:45 +0000 | [diff] [blame] | 989 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 990 | {"__weakref__", subtype_getweakref, NULL, |
Neal Norwitz | 858e34f | 2002-08-13 17:18:45 +0000 | [diff] [blame] | 991 | PyDoc_STR("list of weak references to the object (if defined)")}, |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 992 | {0} |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 993 | }; |
| 994 | |
Guido van Rossum | 0628dcf | 2002-03-14 23:03:14 +0000 | [diff] [blame] | 995 | /* bozo: __getstate__ that raises TypeError */ |
| 996 | |
| 997 | static PyObject * |
| 998 | bozo_func(PyObject *self, PyObject *args) |
| 999 | { |
| 1000 | PyErr_SetString(PyExc_TypeError, |
| 1001 | "a class that defines __slots__ without " |
| 1002 | "defining __getstate__ cannot be pickled"); |
| 1003 | return NULL; |
| 1004 | } |
| 1005 | |
Neal Norwitz | 93c1e23 | 2002-03-31 16:06:11 +0000 | [diff] [blame] | 1006 | static PyMethodDef bozo_ml = {"__getstate__", bozo_func, METH_VARARGS}; |
Guido van Rossum | 0628dcf | 2002-03-14 23:03:14 +0000 | [diff] [blame] | 1007 | |
| 1008 | static PyObject *bozo_obj = NULL; |
| 1009 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1010 | static int |
| 1011 | valid_identifier(PyObject *s) |
| 1012 | { |
Guido van Rossum | 03013a0 | 2002-07-16 14:30:28 +0000 | [diff] [blame] | 1013 | unsigned char *p; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1014 | int i, n; |
| 1015 | |
| 1016 | if (!PyString_Check(s)) { |
| 1017 | PyErr_SetString(PyExc_TypeError, |
| 1018 | "__slots__ must be strings"); |
| 1019 | return 0; |
| 1020 | } |
Guido van Rossum | 03013a0 | 2002-07-16 14:30:28 +0000 | [diff] [blame] | 1021 | p = (unsigned char *) PyString_AS_STRING(s); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1022 | n = PyString_GET_SIZE(s); |
| 1023 | /* We must reject an empty name. As a hack, we bump the |
| 1024 | length to 1 so that the loop will balk on the trailing \0. */ |
| 1025 | if (n == 0) |
| 1026 | n = 1; |
| 1027 | for (i = 0; i < n; i++, p++) { |
| 1028 | if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') { |
| 1029 | PyErr_SetString(PyExc_TypeError, |
| 1030 | "__slots__ must be identifiers"); |
| 1031 | return 0; |
| 1032 | } |
| 1033 | } |
| 1034 | return 1; |
| 1035 | } |
| 1036 | |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1037 | #ifdef Py_USING_UNICODE |
| 1038 | /* Replace Unicode objects in slots. */ |
| 1039 | |
| 1040 | static PyObject * |
| 1041 | _unicode_to_string(PyObject *slots, int nslots) |
| 1042 | { |
| 1043 | PyObject *tmp = slots; |
| 1044 | PyObject *o, *o1; |
| 1045 | int i; |
| 1046 | intintargfunc copy = slots->ob_type->tp_as_sequence->sq_slice; |
| 1047 | for (i = 0; i < nslots; i++) { |
| 1048 | if (PyUnicode_Check(o = PyTuple_GET_ITEM(tmp, i))) { |
| 1049 | if (tmp == slots) { |
| 1050 | tmp = copy(slots, 0, PyTuple_GET_SIZE(slots)); |
| 1051 | if (tmp == NULL) |
| 1052 | return NULL; |
| 1053 | } |
| 1054 | o1 = _PyUnicode_AsDefaultEncodedString |
| 1055 | (o, NULL); |
| 1056 | if (o1 == NULL) { |
| 1057 | Py_DECREF(tmp); |
| 1058 | return 0; |
| 1059 | } |
| 1060 | Py_INCREF(o1); |
| 1061 | Py_DECREF(o); |
| 1062 | PyTuple_SET_ITEM(tmp, i, o1); |
| 1063 | } |
| 1064 | } |
| 1065 | return tmp; |
| 1066 | } |
| 1067 | #endif |
| 1068 | |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1069 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1070 | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) |
| 1071 | { |
| 1072 | PyObject *name, *bases, *dict; |
| 1073 | static char *kwlist[] = {"name", "bases", "dict", 0}; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1074 | PyObject *slots, *tmp, *newslots; |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1075 | PyTypeObject *type, *base, *tmptype, *winner; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1076 | etype *et; |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 1077 | PyMemberDef *mp; |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1078 | int i, nbases, nslots, slotoffset, add_dict, add_weak; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1079 | int j, may_add_dict, may_add_weak; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1080 | |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1081 | assert(args != NULL && PyTuple_Check(args)); |
| 1082 | assert(kwds == NULL || PyDict_Check(kwds)); |
| 1083 | |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1084 | /* Special case: type(x) should return x->ob_type */ |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1085 | { |
| 1086 | const int nargs = PyTuple_GET_SIZE(args); |
| 1087 | const int nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); |
| 1088 | |
| 1089 | if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) { |
| 1090 | PyObject *x = PyTuple_GET_ITEM(args, 0); |
| 1091 | Py_INCREF(x->ob_type); |
| 1092 | return (PyObject *) x->ob_type; |
| 1093 | } |
| 1094 | |
| 1095 | /* SF bug 475327 -- if that didn't trigger, we need 3 |
| 1096 | arguments. but PyArg_ParseTupleAndKeywords below may give |
| 1097 | a msg saying type() needs exactly 3. */ |
| 1098 | if (nargs + nkwds != 3) { |
| 1099 | PyErr_SetString(PyExc_TypeError, |
| 1100 | "type() takes 1 or 3 arguments"); |
| 1101 | return NULL; |
| 1102 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1105 | /* Check arguments: (name, bases, dict) */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1106 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist, |
| 1107 | &name, |
| 1108 | &PyTuple_Type, &bases, |
| 1109 | &PyDict_Type, &dict)) |
| 1110 | return NULL; |
| 1111 | |
| 1112 | /* Determine the proper metatype to deal with this, |
| 1113 | and check for metatype conflicts while we're at it. |
| 1114 | Note that if some other metatype wins to contract, |
| 1115 | it's possible that its instances are not types. */ |
| 1116 | nbases = PyTuple_GET_SIZE(bases); |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1117 | winner = metatype; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1118 | for (i = 0; i < nbases; i++) { |
| 1119 | tmp = PyTuple_GET_ITEM(bases, i); |
| 1120 | tmptype = tmp->ob_type; |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1121 | if (tmptype == &PyClass_Type) |
| 1122 | continue; /* Special case classic classes */ |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1123 | if (PyType_IsSubtype(winner, tmptype)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1124 | continue; |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1125 | if (PyType_IsSubtype(tmptype, winner)) { |
| 1126 | winner = tmptype; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1127 | continue; |
| 1128 | } |
| 1129 | PyErr_SetString(PyExc_TypeError, |
| 1130 | "metatype conflict among bases"); |
| 1131 | return NULL; |
| 1132 | } |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1133 | if (winner != metatype) { |
| 1134 | if (winner->tp_new != type_new) /* Pass it to the winner */ |
| 1135 | return winner->tp_new(winner, args, kwds); |
| 1136 | metatype = winner; |
| 1137 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1138 | |
| 1139 | /* Adjust for empty tuple bases */ |
| 1140 | if (nbases == 0) { |
| 1141 | bases = Py_BuildValue("(O)", &PyBaseObject_Type); |
| 1142 | if (bases == NULL) |
| 1143 | return NULL; |
| 1144 | nbases = 1; |
| 1145 | } |
| 1146 | else |
| 1147 | Py_INCREF(bases); |
| 1148 | |
| 1149 | /* XXX From here until type is allocated, "return NULL" leaks bases! */ |
| 1150 | |
| 1151 | /* Calculate best base, and check that all bases are type objects */ |
| 1152 | base = best_base(bases); |
| 1153 | if (base == NULL) |
| 1154 | return NULL; |
| 1155 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 1156 | PyErr_Format(PyExc_TypeError, |
| 1157 | "type '%.100s' is not an acceptable base type", |
| 1158 | base->tp_name); |
| 1159 | return NULL; |
| 1160 | } |
| 1161 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1162 | /* Check for a __slots__ sequence variable in dict, and count it */ |
| 1163 | slots = PyDict_GetItemString(dict, "__slots__"); |
| 1164 | nslots = 0; |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1165 | add_dict = 0; |
| 1166 | add_weak = 0; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1167 | may_add_dict = base->tp_dictoffset == 0; |
| 1168 | may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0; |
| 1169 | if (slots == NULL) { |
| 1170 | if (may_add_dict) { |
| 1171 | add_dict++; |
| 1172 | } |
| 1173 | if (may_add_weak) { |
| 1174 | add_weak++; |
| 1175 | } |
| 1176 | } |
| 1177 | else { |
| 1178 | /* Have slots */ |
| 1179 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1180 | /* Make it into a tuple */ |
| 1181 | if (PyString_Check(slots)) |
| 1182 | slots = Py_BuildValue("(O)", slots); |
| 1183 | else |
| 1184 | slots = PySequence_Tuple(slots); |
| 1185 | if (slots == NULL) |
| 1186 | return NULL; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1187 | assert(PyTuple_Check(slots)); |
| 1188 | |
| 1189 | /* Are slots allowed? */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1190 | nslots = PyTuple_GET_SIZE(slots); |
Guido van Rossum | c414187 | 2001-08-30 04:43:35 +0000 | [diff] [blame] | 1191 | if (nslots > 0 && base->tp_itemsize != 0) { |
| 1192 | PyErr_Format(PyExc_TypeError, |
| 1193 | "nonempty __slots__ " |
| 1194 | "not supported for subtype of '%s'", |
| 1195 | base->tp_name); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1196 | bad_slots: |
| 1197 | Py_DECREF(slots); |
Guido van Rossum | c414187 | 2001-08-30 04:43:35 +0000 | [diff] [blame] | 1198 | return NULL; |
| 1199 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1200 | |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1201 | #ifdef Py_USING_UNICODE |
| 1202 | tmp = _unicode_to_string(slots, nslots); |
Martin v. Löwis | 13b1a5c | 2002-10-14 21:11:34 +0000 | [diff] [blame] | 1203 | if (tmp != slots) { |
| 1204 | Py_DECREF(slots); |
| 1205 | slots = tmp; |
| 1206 | } |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1207 | if (!tmp) |
| 1208 | return NULL; |
| 1209 | #endif |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1210 | /* Check for valid slot names and two special cases */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1211 | for (i = 0; i < nslots; i++) { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1212 | PyObject *tmp = PyTuple_GET_ITEM(slots, i); |
| 1213 | char *s; |
| 1214 | if (!valid_identifier(tmp)) |
| 1215 | goto bad_slots; |
| 1216 | assert(PyString_Check(tmp)); |
| 1217 | s = PyString_AS_STRING(tmp); |
| 1218 | if (strcmp(s, "__dict__") == 0) { |
| 1219 | if (!may_add_dict || add_dict) { |
| 1220 | PyErr_SetString(PyExc_TypeError, |
| 1221 | "__dict__ slot disallowed: " |
| 1222 | "we already got one"); |
| 1223 | goto bad_slots; |
| 1224 | } |
| 1225 | add_dict++; |
| 1226 | } |
| 1227 | if (strcmp(s, "__weakref__") == 0) { |
| 1228 | if (!may_add_weak || add_weak) { |
| 1229 | PyErr_SetString(PyExc_TypeError, |
| 1230 | "__weakref__ slot disallowed: " |
| 1231 | "either we already got one, " |
| 1232 | "or __itemsize__ != 0"); |
| 1233 | goto bad_slots; |
| 1234 | } |
| 1235 | add_weak++; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1236 | } |
| 1237 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1238 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1239 | /* Copy slots into yet another tuple, demangling names */ |
| 1240 | newslots = PyTuple_New(nslots - add_dict - add_weak); |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1241 | if (newslots == NULL) |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1242 | goto bad_slots; |
| 1243 | for (i = j = 0; i < nslots; i++) { |
| 1244 | char *s; |
Guido van Rossum | 8e82920 | 2002-08-16 03:47:49 +0000 | [diff] [blame] | 1245 | char buffer[256]; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1246 | tmp = PyTuple_GET_ITEM(slots, i); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1247 | s = PyString_AS_STRING(tmp); |
| 1248 | if ((add_dict && strcmp(s, "__dict__") == 0) || |
| 1249 | (add_weak && strcmp(s, "__weakref__") == 0)) |
| 1250 | continue; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1251 | if (_Py_Mangle(PyString_AS_STRING(name), |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1252 | PyString_AS_STRING(tmp), |
| 1253 | buffer, sizeof(buffer))) |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1254 | { |
| 1255 | tmp = PyString_FromString(buffer); |
| 1256 | } else { |
| 1257 | Py_INCREF(tmp); |
| 1258 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1259 | PyTuple_SET_ITEM(newslots, j, tmp); |
| 1260 | j++; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1261 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1262 | assert(j == nslots - add_dict - add_weak); |
| 1263 | nslots = j; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1264 | Py_DECREF(slots); |
| 1265 | slots = newslots; |
| 1266 | |
Guido van Rossum | 0628dcf | 2002-03-14 23:03:14 +0000 | [diff] [blame] | 1267 | /* See if *this* class defines __getstate__ */ |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1268 | if (PyDict_GetItemString(dict, "__getstate__") == NULL) { |
Guido van Rossum | 0628dcf | 2002-03-14 23:03:14 +0000 | [diff] [blame] | 1269 | /* If not, provide a bozo that raises TypeError */ |
| 1270 | if (bozo_obj == NULL) { |
| 1271 | bozo_obj = PyCFunction_New(&bozo_ml, NULL); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1272 | if (bozo_obj == NULL) |
| 1273 | goto bad_slots; |
Guido van Rossum | 0628dcf | 2002-03-14 23:03:14 +0000 | [diff] [blame] | 1274 | } |
| 1275 | if (PyDict_SetItemString(dict, |
| 1276 | "__getstate__", |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1277 | bozo_obj) < 0) |
| 1278 | { |
| 1279 | Py_DECREF(bozo_obj); |
| 1280 | goto bad_slots; |
Guido van Rossum | 0628dcf | 2002-03-14 23:03:14 +0000 | [diff] [blame] | 1281 | } |
| 1282 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1283 | |
| 1284 | /* Secondary bases may provide weakrefs or dict */ |
| 1285 | if (nbases > 1 && |
| 1286 | ((may_add_dict && !add_dict) || |
| 1287 | (may_add_weak && !add_weak))) { |
| 1288 | for (i = 0; i < nbases; i++) { |
| 1289 | tmp = PyTuple_GET_ITEM(bases, i); |
| 1290 | if (tmp == (PyObject *)base) |
| 1291 | continue; /* Skip primary base */ |
| 1292 | if (PyClass_Check(tmp)) { |
| 1293 | /* Classic base class provides both */ |
| 1294 | if (may_add_dict && !add_dict) |
| 1295 | add_dict++; |
| 1296 | if (may_add_weak && !add_weak) |
| 1297 | add_weak++; |
| 1298 | break; |
| 1299 | } |
| 1300 | assert(PyType_Check(tmp)); |
| 1301 | tmptype = (PyTypeObject *)tmp; |
| 1302 | if (may_add_dict && !add_dict && |
| 1303 | tmptype->tp_dictoffset != 0) |
| 1304 | add_dict++; |
| 1305 | if (may_add_weak && !add_weak && |
| 1306 | tmptype->tp_weaklistoffset != 0) |
| 1307 | add_weak++; |
| 1308 | if (may_add_dict && !add_dict) |
| 1309 | continue; |
| 1310 | if (may_add_weak && !add_weak) |
| 1311 | continue; |
| 1312 | /* Nothing more to check */ |
| 1313 | break; |
| 1314 | } |
| 1315 | } |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1316 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1317 | |
| 1318 | /* XXX From here until type is safely allocated, |
| 1319 | "return NULL" may leak slots! */ |
| 1320 | |
| 1321 | /* Allocate the type object */ |
| 1322 | type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1323 | if (type == NULL) { |
| 1324 | Py_XDECREF(slots); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1325 | return NULL; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1326 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1327 | |
| 1328 | /* Keep name and slots alive in the extended type object */ |
| 1329 | et = (etype *)type; |
| 1330 | Py_INCREF(name); |
| 1331 | et->name = name; |
| 1332 | et->slots = slots; |
| 1333 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 1334 | /* Initialize tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1335 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | |
| 1336 | Py_TPFLAGS_BASETYPE; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1337 | if (base->tp_flags & Py_TPFLAGS_HAVE_GC) |
| 1338 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 1339 | |
| 1340 | /* It's a new-style number unless it specifically inherits any |
| 1341 | old-style numeric behavior */ |
| 1342 | if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) || |
| 1343 | (base->tp_as_number == NULL)) |
| 1344 | type->tp_flags |= Py_TPFLAGS_CHECKTYPES; |
| 1345 | |
| 1346 | /* Initialize essential fields */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1347 | type->tp_as_number = &et->as_number; |
| 1348 | type->tp_as_sequence = &et->as_sequence; |
| 1349 | type->tp_as_mapping = &et->as_mapping; |
| 1350 | type->tp_as_buffer = &et->as_buffer; |
| 1351 | type->tp_name = PyString_AS_STRING(name); |
| 1352 | |
| 1353 | /* Set tp_base and tp_bases */ |
| 1354 | type->tp_bases = bases; |
| 1355 | Py_INCREF(base); |
| 1356 | type->tp_base = base; |
| 1357 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 1358 | /* Initialize tp_dict from passed-in dict */ |
| 1359 | type->tp_dict = dict = PyDict_Copy(dict); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1360 | if (dict == NULL) { |
| 1361 | Py_DECREF(type); |
| 1362 | return NULL; |
| 1363 | } |
| 1364 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 1365 | /* Set __module__ in the dict */ |
| 1366 | if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 1367 | tmp = PyEval_GetGlobals(); |
| 1368 | if (tmp != NULL) { |
| 1369 | tmp = PyDict_GetItemString(tmp, "__name__"); |
| 1370 | if (tmp != NULL) { |
| 1371 | if (PyDict_SetItemString(dict, "__module__", |
| 1372 | tmp) < 0) |
| 1373 | return NULL; |
| 1374 | } |
| 1375 | } |
| 1376 | } |
| 1377 | |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 1378 | /* Set tp_doc to a copy of dict['__doc__'], if the latter is there |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 1379 | and is a string. The __doc__ accessor will first look for tp_doc; |
| 1380 | if that fails, it will still look into __dict__. |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 1381 | */ |
| 1382 | { |
| 1383 | PyObject *doc = PyDict_GetItemString(dict, "__doc__"); |
| 1384 | if (doc != NULL && PyString_Check(doc)) { |
| 1385 | const size_t n = (size_t)PyString_GET_SIZE(doc); |
Tim Peters | 59f809d | 2001-10-04 05:43:02 +0000 | [diff] [blame] | 1386 | type->tp_doc = (char *)PyObject_MALLOC(n+1); |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 1387 | if (type->tp_doc == NULL) { |
| 1388 | Py_DECREF(type); |
| 1389 | return NULL; |
| 1390 | } |
| 1391 | memcpy(type->tp_doc, PyString_AS_STRING(doc), n+1); |
| 1392 | } |
| 1393 | } |
| 1394 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1395 | /* Special-case __new__: if it's a plain function, |
| 1396 | make it a static function */ |
| 1397 | tmp = PyDict_GetItemString(dict, "__new__"); |
| 1398 | if (tmp != NULL && PyFunction_Check(tmp)) { |
| 1399 | tmp = PyStaticMethod_New(tmp); |
| 1400 | if (tmp == NULL) { |
| 1401 | Py_DECREF(type); |
| 1402 | return NULL; |
| 1403 | } |
| 1404 | PyDict_SetItemString(dict, "__new__", tmp); |
| 1405 | Py_DECREF(tmp); |
| 1406 | } |
| 1407 | |
| 1408 | /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
| 1409 | mp = et->members; |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 1410 | slotoffset = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1411 | if (slots != NULL) { |
| 1412 | for (i = 0; i < nslots; i++, mp++) { |
| 1413 | mp->name = PyString_AS_STRING( |
| 1414 | PyTuple_GET_ITEM(slots, i)); |
Guido van Rossum | 64b206c | 2001-12-04 17:13:22 +0000 | [diff] [blame] | 1415 | mp->type = T_OBJECT_EX; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1416 | mp->offset = slotoffset; |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1417 | if (base->tp_weaklistoffset == 0 && |
Guido van Rossum | 64b206c | 2001-12-04 17:13:22 +0000 | [diff] [blame] | 1418 | strcmp(mp->name, "__weakref__") == 0) { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1419 | add_weak++; |
Guido van Rossum | 64b206c | 2001-12-04 17:13:22 +0000 | [diff] [blame] | 1420 | mp->type = T_OBJECT; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1421 | mp->flags = READONLY; |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1422 | type->tp_weaklistoffset = slotoffset; |
Guido van Rossum | 64b206c | 2001-12-04 17:13:22 +0000 | [diff] [blame] | 1423 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1424 | slotoffset += sizeof(PyObject *); |
| 1425 | } |
| 1426 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1427 | if (add_dict) { |
| 1428 | if (base->tp_itemsize) |
| 1429 | type->tp_dictoffset = -(long)sizeof(PyObject *); |
| 1430 | else |
| 1431 | type->tp_dictoffset = slotoffset; |
| 1432 | slotoffset += sizeof(PyObject *); |
| 1433 | } |
| 1434 | if (add_weak) { |
| 1435 | assert(!base->tp_itemsize); |
| 1436 | type->tp_weaklistoffset = slotoffset; |
| 1437 | slotoffset += sizeof(PyObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1438 | } |
| 1439 | type->tp_basicsize = slotoffset; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1440 | type->tp_itemsize = base->tp_itemsize; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 1441 | type->tp_members = et->members; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1442 | type->tp_getset = subtype_getsets; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1443 | |
| 1444 | /* Special case some slots */ |
| 1445 | if (type->tp_dictoffset != 0 || nslots > 0) { |
| 1446 | if (base->tp_getattr == NULL && base->tp_getattro == NULL) |
| 1447 | type->tp_getattro = PyObject_GenericGetAttr; |
| 1448 | if (base->tp_setattr == NULL && base->tp_setattro == NULL) |
| 1449 | type->tp_setattro = PyObject_GenericSetAttr; |
| 1450 | } |
| 1451 | type->tp_dealloc = subtype_dealloc; |
| 1452 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 1453 | /* Enable GC unless there are really no instance variables possible */ |
| 1454 | if (!(type->tp_basicsize == sizeof(PyObject) && |
| 1455 | type->tp_itemsize == 0)) |
| 1456 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
| 1457 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1458 | /* Always override allocation strategy to use regular heap */ |
| 1459 | type->tp_alloc = PyType_GenericAlloc; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1460 | if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 1461 | type->tp_free = PyObject_GC_Del; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 1462 | type->tp_traverse = subtype_traverse; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1463 | type->tp_clear = subtype_clear; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1464 | } |
| 1465 | else |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 1466 | type->tp_free = PyObject_Del; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1467 | |
| 1468 | /* Initialize the rest */ |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1469 | if (PyType_Ready(type) < 0) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1470 | Py_DECREF(type); |
| 1471 | return NULL; |
| 1472 | } |
| 1473 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 1474 | /* Put the proper slots in place */ |
| 1475 | fixup_slot_dispatchers(type); |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 1476 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1477 | return (PyObject *)type; |
| 1478 | } |
| 1479 | |
| 1480 | /* Internal API to look for a name through the MRO. |
| 1481 | This returns a borrowed reference, and doesn't set an exception! */ |
| 1482 | PyObject * |
| 1483 | _PyType_Lookup(PyTypeObject *type, PyObject *name) |
| 1484 | { |
| 1485 | int i, n; |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1486 | PyObject *mro, *res, *base, *dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1487 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 1488 | /* Look in tp_dict of types in MRO */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1489 | mro = type->tp_mro; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 1490 | |
| 1491 | /* If mro is NULL, the type is either not yet initialized |
| 1492 | by PyType_Ready(), or already cleared by type_clear(). |
| 1493 | Either way the safest thing to do is to return NULL. */ |
| 1494 | if (mro == NULL) |
| 1495 | return NULL; |
| 1496 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1497 | assert(PyTuple_Check(mro)); |
| 1498 | n = PyTuple_GET_SIZE(mro); |
| 1499 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1500 | base = PyTuple_GET_ITEM(mro, i); |
| 1501 | if (PyClass_Check(base)) |
| 1502 | dict = ((PyClassObject *)base)->cl_dict; |
| 1503 | else { |
| 1504 | assert(PyType_Check(base)); |
| 1505 | dict = ((PyTypeObject *)base)->tp_dict; |
| 1506 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1507 | assert(dict && PyDict_Check(dict)); |
| 1508 | res = PyDict_GetItem(dict, name); |
| 1509 | if (res != NULL) |
| 1510 | return res; |
| 1511 | } |
| 1512 | return NULL; |
| 1513 | } |
| 1514 | |
| 1515 | /* This is similar to PyObject_GenericGetAttr(), |
| 1516 | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ |
| 1517 | static PyObject * |
| 1518 | type_getattro(PyTypeObject *type, PyObject *name) |
| 1519 | { |
| 1520 | PyTypeObject *metatype = type->ob_type; |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 1521 | PyObject *meta_attribute, *attribute; |
| 1522 | descrgetfunc meta_get; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1523 | |
| 1524 | /* Initialize this type (we'll assume the metatype is initialized) */ |
| 1525 | if (type->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1526 | if (PyType_Ready(type) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1527 | return NULL; |
| 1528 | } |
| 1529 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 1530 | /* No readable descriptor found yet */ |
| 1531 | meta_get = NULL; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1532 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 1533 | /* Look for the attribute in the metatype */ |
| 1534 | meta_attribute = _PyType_Lookup(metatype, name); |
| 1535 | |
| 1536 | if (meta_attribute != NULL) { |
| 1537 | meta_get = meta_attribute->ob_type->tp_descr_get; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1538 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 1539 | if (meta_get != NULL && PyDescr_IsData(meta_attribute)) { |
| 1540 | /* Data descriptors implement tp_descr_set to intercept |
| 1541 | * writes. Assume the attribute is not overridden in |
| 1542 | * type's tp_dict (and bases): call the descriptor now. |
| 1543 | */ |
| 1544 | return meta_get(meta_attribute, (PyObject *)type, |
| 1545 | (PyObject *)metatype); |
| 1546 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 1549 | /* No data descriptor found on metatype. Look in tp_dict of this |
| 1550 | * type and its bases */ |
| 1551 | attribute = _PyType_Lookup(type, name); |
| 1552 | if (attribute != NULL) { |
| 1553 | /* Implement descriptor functionality, if any */ |
| 1554 | descrgetfunc local_get = attribute->ob_type->tp_descr_get; |
| 1555 | if (local_get != NULL) { |
| 1556 | /* NULL 2nd argument indicates the descriptor was |
| 1557 | * found on the target object itself (or a base) */ |
| 1558 | return local_get(attribute, (PyObject *)NULL, |
| 1559 | (PyObject *)type); |
| 1560 | } |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1561 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 1562 | Py_INCREF(attribute); |
| 1563 | return attribute; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 1566 | /* No attribute found in local __dict__ (or bases): use the |
| 1567 | * descriptor from the metatype, if any */ |
| 1568 | if (meta_get != NULL) |
| 1569 | return meta_get(meta_attribute, (PyObject *)type, |
| 1570 | (PyObject *)metatype); |
| 1571 | |
| 1572 | /* If an ordinary attribute was found on the metatype, return it now */ |
| 1573 | if (meta_attribute != NULL) { |
| 1574 | Py_INCREF(meta_attribute); |
| 1575 | return meta_attribute; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | /* Give up */ |
| 1579 | PyErr_Format(PyExc_AttributeError, |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 1580 | "type object '%.50s' has no attribute '%.400s'", |
| 1581 | type->tp_name, PyString_AS_STRING(name)); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1582 | return NULL; |
| 1583 | } |
| 1584 | |
| 1585 | static int |
| 1586 | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) |
| 1587 | { |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1588 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 1589 | PyErr_Format( |
| 1590 | PyExc_TypeError, |
| 1591 | "can't set attributes of built-in/extension type '%s'", |
| 1592 | type->tp_name); |
| 1593 | return -1; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 1594 | } |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1595 | if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0) |
| 1596 | return -1; |
| 1597 | return update_slot(type, name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | static void |
| 1601 | type_dealloc(PyTypeObject *type) |
| 1602 | { |
| 1603 | etype *et; |
| 1604 | |
| 1605 | /* Assert this is a heap-allocated type object */ |
| 1606 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1607 | _PyObject_GC_UNTRACK(type); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1608 | PyObject_ClearWeakRefs((PyObject *)type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1609 | et = (etype *)type; |
| 1610 | Py_XDECREF(type->tp_base); |
| 1611 | Py_XDECREF(type->tp_dict); |
| 1612 | Py_XDECREF(type->tp_bases); |
| 1613 | Py_XDECREF(type->tp_mro); |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 1614 | Py_XDECREF(type->tp_cache); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1615 | Py_XDECREF(type->tp_subclasses); |
Neal Norwitz | cee5ca0 | 2002-07-30 00:42:06 +0000 | [diff] [blame] | 1616 | PyObject_Free(type->tp_doc); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1617 | Py_XDECREF(et->name); |
| 1618 | Py_XDECREF(et->slots); |
| 1619 | type->ob_type->tp_free((PyObject *)type); |
| 1620 | } |
| 1621 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1622 | static PyObject * |
| 1623 | type_subclasses(PyTypeObject *type, PyObject *args_ignored) |
| 1624 | { |
| 1625 | PyObject *list, *raw, *ref; |
| 1626 | int i, n; |
| 1627 | |
| 1628 | list = PyList_New(0); |
| 1629 | if (list == NULL) |
| 1630 | return NULL; |
| 1631 | raw = type->tp_subclasses; |
| 1632 | if (raw == NULL) |
| 1633 | return list; |
| 1634 | assert(PyList_Check(raw)); |
| 1635 | n = PyList_GET_SIZE(raw); |
| 1636 | for (i = 0; i < n; i++) { |
| 1637 | ref = PyList_GET_ITEM(raw, i); |
Tim Peters | 4438338 | 2001-10-08 16:49:26 +0000 | [diff] [blame] | 1638 | assert(PyWeakref_CheckRef(ref)); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1639 | ref = PyWeakref_GET_OBJECT(ref); |
| 1640 | if (ref != Py_None) { |
| 1641 | if (PyList_Append(list, ref) < 0) { |
| 1642 | Py_DECREF(list); |
| 1643 | return NULL; |
| 1644 | } |
| 1645 | } |
| 1646 | } |
| 1647 | return list; |
| 1648 | } |
| 1649 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1650 | static PyMethodDef type_methods[] = { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1651 | {"mro", (PyCFunction)mro_external, METH_NOARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 1652 | PyDoc_STR("mro() -> list\nreturn a type's method resolution order")}, |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1653 | {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 1654 | PyDoc_STR("__subclasses__() -> list of immediate subclasses")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1655 | {0} |
| 1656 | }; |
| 1657 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1658 | PyDoc_STRVAR(type_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1659 | "type(object) -> the object's type\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1660 | "type(name, bases, dict) -> a new type"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1661 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1662 | static int |
| 1663 | type_traverse(PyTypeObject *type, visitproc visit, void *arg) |
| 1664 | { |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1665 | int err; |
| 1666 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 1667 | /* Because of type_is_gc(), the collector only calls this |
| 1668 | for heaptypes. */ |
| 1669 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1670 | |
| 1671 | #define VISIT(SLOT) \ |
| 1672 | if (SLOT) { \ |
| 1673 | err = visit((PyObject *)(SLOT), arg); \ |
| 1674 | if (err) \ |
| 1675 | return err; \ |
| 1676 | } |
| 1677 | |
| 1678 | VISIT(type->tp_dict); |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 1679 | VISIT(type->tp_cache); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1680 | VISIT(type->tp_mro); |
| 1681 | VISIT(type->tp_bases); |
| 1682 | VISIT(type->tp_base); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 1683 | |
| 1684 | /* There's no need to visit type->tp_subclasses or |
| 1685 | ((etype *)type)->slots, because they can't be involved |
| 1686 | in cycles; tp_subclasses is a list of weak references, |
| 1687 | and slots is a tuple of strings. */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1688 | |
| 1689 | #undef VISIT |
| 1690 | |
| 1691 | return 0; |
| 1692 | } |
| 1693 | |
| 1694 | static int |
| 1695 | type_clear(PyTypeObject *type) |
| 1696 | { |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1697 | PyObject *tmp; |
| 1698 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 1699 | /* Because of type_is_gc(), the collector only calls this |
| 1700 | for heaptypes. */ |
| 1701 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1702 | |
| 1703 | #define CLEAR(SLOT) \ |
| 1704 | if (SLOT) { \ |
| 1705 | tmp = (PyObject *)(SLOT); \ |
| 1706 | SLOT = NULL; \ |
| 1707 | Py_DECREF(tmp); \ |
| 1708 | } |
| 1709 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 1710 | /* The only field we need to clear is tp_mro, which is part of a |
| 1711 | hard cycle (its first element is the class itself) that won't |
| 1712 | be broken otherwise (it's a tuple and tuples don't have a |
| 1713 | tp_clear handler). None of the other fields need to be |
| 1714 | cleared, and here's why: |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1715 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 1716 | tp_dict: |
| 1717 | It is a dict, so the collector will call its tp_clear. |
| 1718 | |
| 1719 | tp_cache: |
| 1720 | Not used; if it were, it would be a dict. |
| 1721 | |
| 1722 | tp_bases, tp_base: |
| 1723 | If these are involved in a cycle, there must be at least |
| 1724 | one other, mutable object in the cycle, e.g. a base |
| 1725 | class's dict; the cycle will be broken that way. |
| 1726 | |
| 1727 | tp_subclasses: |
| 1728 | A list of weak references can't be part of a cycle; and |
| 1729 | lists have their own tp_clear. |
| 1730 | |
| 1731 | slots (in etype): |
| 1732 | A tuple of strings can't be part of a cycle. |
| 1733 | */ |
| 1734 | |
| 1735 | CLEAR(type->tp_mro); |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 1736 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1737 | #undef CLEAR |
| 1738 | |
| 1739 | return 0; |
| 1740 | } |
| 1741 | |
| 1742 | static int |
| 1743 | type_is_gc(PyTypeObject *type) |
| 1744 | { |
| 1745 | return type->tp_flags & Py_TPFLAGS_HEAPTYPE; |
| 1746 | } |
| 1747 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1748 | PyTypeObject PyType_Type = { |
| 1749 | PyObject_HEAD_INIT(&PyType_Type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1750 | 0, /* ob_size */ |
| 1751 | "type", /* tp_name */ |
| 1752 | sizeof(etype), /* tp_basicsize */ |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 1753 | sizeof(PyMemberDef), /* tp_itemsize */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1754 | (destructor)type_dealloc, /* tp_dealloc */ |
| 1755 | 0, /* tp_print */ |
| 1756 | 0, /* tp_getattr */ |
| 1757 | 0, /* tp_setattr */ |
| 1758 | type_compare, /* tp_compare */ |
| 1759 | (reprfunc)type_repr, /* tp_repr */ |
| 1760 | 0, /* tp_as_number */ |
| 1761 | 0, /* tp_as_sequence */ |
| 1762 | 0, /* tp_as_mapping */ |
| 1763 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
| 1764 | (ternaryfunc)type_call, /* tp_call */ |
| 1765 | 0, /* tp_str */ |
| 1766 | (getattrofunc)type_getattro, /* tp_getattro */ |
| 1767 | (setattrofunc)type_setattro, /* tp_setattro */ |
| 1768 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1769 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1770 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1771 | type_doc, /* tp_doc */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1772 | (traverseproc)type_traverse, /* tp_traverse */ |
| 1773 | (inquiry)type_clear, /* tp_clear */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1774 | 0, /* tp_richcompare */ |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1775 | offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1776 | 0, /* tp_iter */ |
| 1777 | 0, /* tp_iternext */ |
| 1778 | type_methods, /* tp_methods */ |
| 1779 | type_members, /* tp_members */ |
| 1780 | type_getsets, /* tp_getset */ |
| 1781 | 0, /* tp_base */ |
| 1782 | 0, /* tp_dict */ |
| 1783 | 0, /* tp_descr_get */ |
| 1784 | 0, /* tp_descr_set */ |
| 1785 | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ |
| 1786 | 0, /* tp_init */ |
| 1787 | 0, /* tp_alloc */ |
| 1788 | type_new, /* tp_new */ |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 1789 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1790 | (inquiry)type_is_gc, /* tp_is_gc */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1791 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1792 | |
| 1793 | |
| 1794 | /* The base type of all types (eventually)... except itself. */ |
| 1795 | |
| 1796 | static int |
| 1797 | object_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1798 | { |
| 1799 | return 0; |
| 1800 | } |
| 1801 | |
| 1802 | static void |
| 1803 | object_dealloc(PyObject *self) |
| 1804 | { |
| 1805 | self->ob_type->tp_free(self); |
| 1806 | } |
| 1807 | |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 1808 | static PyObject * |
| 1809 | object_repr(PyObject *self) |
| 1810 | { |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 1811 | PyTypeObject *type; |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 1812 | PyObject *mod, *name, *rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 1813 | |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 1814 | type = self->ob_type; |
| 1815 | mod = type_module(type, NULL); |
| 1816 | if (mod == NULL) |
| 1817 | PyErr_Clear(); |
| 1818 | else if (!PyString_Check(mod)) { |
| 1819 | Py_DECREF(mod); |
| 1820 | mod = NULL; |
| 1821 | } |
| 1822 | name = type_name(type, NULL); |
| 1823 | if (name == NULL) |
| 1824 | return NULL; |
| 1825 | if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) |
Guido van Rossum | ff0e6d6 | 2001-09-24 16:03:59 +0000 | [diff] [blame] | 1826 | rtn = PyString_FromFormat("<%s.%s object at %p>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 1827 | PyString_AS_STRING(mod), |
| 1828 | PyString_AS_STRING(name), |
| 1829 | self); |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 1830 | else |
Guido van Rossum | ff0e6d6 | 2001-09-24 16:03:59 +0000 | [diff] [blame] | 1831 | rtn = PyString_FromFormat("<%s object at %p>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 1832 | type->tp_name, self); |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 1833 | Py_XDECREF(mod); |
| 1834 | Py_DECREF(name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 1835 | return rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 1838 | static PyObject * |
| 1839 | object_str(PyObject *self) |
| 1840 | { |
| 1841 | unaryfunc f; |
| 1842 | |
| 1843 | f = self->ob_type->tp_repr; |
| 1844 | if (f == NULL) |
| 1845 | f = object_repr; |
| 1846 | return f(self); |
| 1847 | } |
| 1848 | |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 1849 | static long |
| 1850 | object_hash(PyObject *self) |
| 1851 | { |
| 1852 | return _Py_HashPointer(self); |
| 1853 | } |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 1854 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 1855 | static PyObject * |
| 1856 | object_get_class(PyObject *self, void *closure) |
| 1857 | { |
| 1858 | Py_INCREF(self->ob_type); |
| 1859 | return (PyObject *)(self->ob_type); |
| 1860 | } |
| 1861 | |
| 1862 | static int |
| 1863 | equiv_structs(PyTypeObject *a, PyTypeObject *b) |
| 1864 | { |
| 1865 | return a == b || |
| 1866 | (a != NULL && |
| 1867 | b != NULL && |
| 1868 | a->tp_basicsize == b->tp_basicsize && |
| 1869 | a->tp_itemsize == b->tp_itemsize && |
| 1870 | a->tp_dictoffset == b->tp_dictoffset && |
| 1871 | a->tp_weaklistoffset == b->tp_weaklistoffset && |
| 1872 | ((a->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 1873 | (b->tp_flags & Py_TPFLAGS_HAVE_GC))); |
| 1874 | } |
| 1875 | |
| 1876 | static int |
| 1877 | same_slots_added(PyTypeObject *a, PyTypeObject *b) |
| 1878 | { |
| 1879 | PyTypeObject *base = a->tp_base; |
| 1880 | int size; |
| 1881 | |
| 1882 | if (base != b->tp_base) |
| 1883 | return 0; |
| 1884 | if (equiv_structs(a, base) && equiv_structs(b, base)) |
| 1885 | return 1; |
| 1886 | size = base->tp_basicsize; |
| 1887 | if (a->tp_dictoffset == size && b->tp_dictoffset == size) |
| 1888 | size += sizeof(PyObject *); |
| 1889 | if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size) |
| 1890 | size += sizeof(PyObject *); |
| 1891 | return size == a->tp_basicsize && size == b->tp_basicsize; |
| 1892 | } |
| 1893 | |
| 1894 | static int |
| 1895 | object_set_class(PyObject *self, PyObject *value, void *closure) |
| 1896 | { |
| 1897 | PyTypeObject *old = self->ob_type; |
| 1898 | PyTypeObject *new, *newbase, *oldbase; |
| 1899 | |
Guido van Rossum | b6b8942 | 2002-04-15 01:03:30 +0000 | [diff] [blame] | 1900 | if (value == NULL) { |
| 1901 | PyErr_SetString(PyExc_TypeError, |
| 1902 | "can't delete __class__ attribute"); |
| 1903 | return -1; |
| 1904 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 1905 | if (!PyType_Check(value)) { |
| 1906 | PyErr_Format(PyExc_TypeError, |
| 1907 | "__class__ must be set to new-style class, not '%s' object", |
| 1908 | value->ob_type->tp_name); |
| 1909 | return -1; |
| 1910 | } |
| 1911 | new = (PyTypeObject *)value; |
Guido van Rossum | 40af889 | 2002-08-10 05:42:07 +0000 | [diff] [blame] | 1912 | if (!(new->tp_flags & Py_TPFLAGS_HEAPTYPE) || |
| 1913 | !(old->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 1914 | { |
| 1915 | PyErr_Format(PyExc_TypeError, |
| 1916 | "__class__ assignment: only for heap types"); |
| 1917 | return -1; |
| 1918 | } |
Guido van Rossum | 9ee4b94 | 2002-05-24 18:47:47 +0000 | [diff] [blame] | 1919 | if (new->tp_dealloc != old->tp_dealloc || |
| 1920 | new->tp_free != old->tp_free) |
| 1921 | { |
| 1922 | PyErr_Format(PyExc_TypeError, |
| 1923 | "__class__ assignment: " |
| 1924 | "'%s' deallocator differs from '%s'", |
| 1925 | new->tp_name, |
| 1926 | old->tp_name); |
| 1927 | return -1; |
| 1928 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 1929 | newbase = new; |
| 1930 | oldbase = old; |
| 1931 | while (equiv_structs(newbase, newbase->tp_base)) |
| 1932 | newbase = newbase->tp_base; |
| 1933 | while (equiv_structs(oldbase, oldbase->tp_base)) |
| 1934 | oldbase = oldbase->tp_base; |
| 1935 | if (newbase != oldbase && |
| 1936 | (newbase->tp_base != oldbase->tp_base || |
| 1937 | !same_slots_added(newbase, oldbase))) { |
| 1938 | PyErr_Format(PyExc_TypeError, |
| 1939 | "__class__ assignment: " |
| 1940 | "'%s' object layout differs from '%s'", |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 1941 | new->tp_name, |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 1942 | old->tp_name); |
| 1943 | return -1; |
| 1944 | } |
Guido van Rossum | 40af889 | 2002-08-10 05:42:07 +0000 | [diff] [blame] | 1945 | Py_INCREF(new); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 1946 | self->ob_type = new; |
Guido van Rossum | 40af889 | 2002-08-10 05:42:07 +0000 | [diff] [blame] | 1947 | Py_DECREF(old); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 1948 | return 0; |
| 1949 | } |
| 1950 | |
| 1951 | static PyGetSetDef object_getsets[] = { |
| 1952 | {"__class__", object_get_class, object_set_class, |
Neal Norwitz | 858e34f | 2002-08-13 17:18:45 +0000 | [diff] [blame] | 1953 | PyDoc_STR("the object's class")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1954 | {0} |
| 1955 | }; |
| 1956 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 1957 | static PyObject * |
| 1958 | object_reduce(PyObject *self, PyObject *args) |
| 1959 | { |
| 1960 | /* Call copy_reg._reduce(self) */ |
| 1961 | static PyObject *copy_reg_str; |
| 1962 | PyObject *copy_reg, *res; |
| 1963 | |
| 1964 | if (!copy_reg_str) { |
| 1965 | copy_reg_str = PyString_InternFromString("copy_reg"); |
| 1966 | if (copy_reg_str == NULL) |
| 1967 | return NULL; |
| 1968 | } |
| 1969 | copy_reg = PyImport_Import(copy_reg_str); |
| 1970 | if (!copy_reg) |
| 1971 | return NULL; |
| 1972 | res = PyEval_CallMethod(copy_reg, "_reduce", "(O)", self); |
| 1973 | Py_DECREF(copy_reg); |
| 1974 | return res; |
| 1975 | } |
| 1976 | |
| 1977 | static PyMethodDef object_methods[] = { |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 1978 | {"__reduce__", object_reduce, METH_NOARGS, |
| 1979 | PyDoc_STR("helper for pickle")}, |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 1980 | {0} |
| 1981 | }; |
| 1982 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1983 | PyTypeObject PyBaseObject_Type = { |
| 1984 | PyObject_HEAD_INIT(&PyType_Type) |
| 1985 | 0, /* ob_size */ |
| 1986 | "object", /* tp_name */ |
| 1987 | sizeof(PyObject), /* tp_basicsize */ |
| 1988 | 0, /* tp_itemsize */ |
| 1989 | (destructor)object_dealloc, /* tp_dealloc */ |
| 1990 | 0, /* tp_print */ |
| 1991 | 0, /* tp_getattr */ |
| 1992 | 0, /* tp_setattr */ |
| 1993 | 0, /* tp_compare */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 1994 | object_repr, /* tp_repr */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1995 | 0, /* tp_as_number */ |
| 1996 | 0, /* tp_as_sequence */ |
| 1997 | 0, /* tp_as_mapping */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 1998 | object_hash, /* tp_hash */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1999 | 0, /* tp_call */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2000 | object_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2001 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2002 | PyObject_GenericSetAttr, /* tp_setattro */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2003 | 0, /* tp_as_buffer */ |
| 2004 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 2005 | PyDoc_STR("The most base type"), /* tp_doc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2006 | 0, /* tp_traverse */ |
| 2007 | 0, /* tp_clear */ |
| 2008 | 0, /* tp_richcompare */ |
| 2009 | 0, /* tp_weaklistoffset */ |
| 2010 | 0, /* tp_iter */ |
| 2011 | 0, /* tp_iternext */ |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2012 | object_methods, /* tp_methods */ |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2013 | 0, /* tp_members */ |
| 2014 | object_getsets, /* tp_getset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2015 | 0, /* tp_base */ |
| 2016 | 0, /* tp_dict */ |
| 2017 | 0, /* tp_descr_get */ |
| 2018 | 0, /* tp_descr_set */ |
| 2019 | 0, /* tp_dictoffset */ |
| 2020 | object_init, /* tp_init */ |
| 2021 | PyType_GenericAlloc, /* tp_alloc */ |
Guido van Rossum | c11e192 | 2001-08-09 19:38:15 +0000 | [diff] [blame] | 2022 | PyType_GenericNew, /* tp_new */ |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 2023 | PyObject_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2024 | }; |
| 2025 | |
| 2026 | |
| 2027 | /* Initialize the __dict__ in a type object */ |
| 2028 | |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 2029 | static PyObject * |
| 2030 | create_specialmethod(PyMethodDef *meth, PyObject *(*func)(PyObject *)) |
| 2031 | { |
| 2032 | PyObject *cfunc; |
| 2033 | PyObject *result; |
| 2034 | |
| 2035 | cfunc = PyCFunction_New(meth, NULL); |
| 2036 | if (cfunc == NULL) |
| 2037 | return NULL; |
| 2038 | result = func(cfunc); |
| 2039 | Py_DECREF(cfunc); |
| 2040 | return result; |
| 2041 | } |
| 2042 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2043 | static int |
| 2044 | add_methods(PyTypeObject *type, PyMethodDef *meth) |
| 2045 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2046 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2047 | |
| 2048 | for (; meth->ml_name != NULL; meth++) { |
| 2049 | PyObject *descr; |
| 2050 | if (PyDict_GetItemString(dict, meth->ml_name)) |
| 2051 | continue; |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 2052 | if (meth->ml_flags & METH_CLASS) { |
| 2053 | if (meth->ml_flags & METH_STATIC) { |
| 2054 | PyErr_SetString(PyExc_ValueError, |
| 2055 | "method cannot be both class and static"); |
| 2056 | return -1; |
| 2057 | } |
| 2058 | descr = create_specialmethod(meth, PyClassMethod_New); |
| 2059 | } |
| 2060 | else if (meth->ml_flags & METH_STATIC) { |
| 2061 | descr = create_specialmethod(meth, PyStaticMethod_New); |
| 2062 | } |
| 2063 | else { |
| 2064 | descr = PyDescr_NewMethod(type, meth); |
| 2065 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2066 | if (descr == NULL) |
| 2067 | return -1; |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 2068 | if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2069 | return -1; |
| 2070 | Py_DECREF(descr); |
| 2071 | } |
| 2072 | return 0; |
| 2073 | } |
| 2074 | |
| 2075 | static int |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 2076 | add_members(PyTypeObject *type, PyMemberDef *memb) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2077 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2078 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2079 | |
| 2080 | for (; memb->name != NULL; memb++) { |
| 2081 | PyObject *descr; |
| 2082 | if (PyDict_GetItemString(dict, memb->name)) |
| 2083 | continue; |
| 2084 | descr = PyDescr_NewMember(type, memb); |
| 2085 | if (descr == NULL) |
| 2086 | return -1; |
| 2087 | if (PyDict_SetItemString(dict, memb->name, descr) < 0) |
| 2088 | return -1; |
| 2089 | Py_DECREF(descr); |
| 2090 | } |
| 2091 | return 0; |
| 2092 | } |
| 2093 | |
| 2094 | static int |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 2095 | add_getset(PyTypeObject *type, PyGetSetDef *gsp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2096 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2097 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2098 | |
| 2099 | for (; gsp->name != NULL; gsp++) { |
| 2100 | PyObject *descr; |
| 2101 | if (PyDict_GetItemString(dict, gsp->name)) |
| 2102 | continue; |
| 2103 | descr = PyDescr_NewGetSet(type, gsp); |
| 2104 | |
| 2105 | if (descr == NULL) |
| 2106 | return -1; |
| 2107 | if (PyDict_SetItemString(dict, gsp->name, descr) < 0) |
| 2108 | return -1; |
| 2109 | Py_DECREF(descr); |
| 2110 | } |
| 2111 | return 0; |
| 2112 | } |
| 2113 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2114 | static void |
| 2115 | inherit_special(PyTypeObject *type, PyTypeObject *base) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2116 | { |
| 2117 | int oldsize, newsize; |
| 2118 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2119 | /* Special flag magic */ |
| 2120 | if (!type->tp_as_buffer && base->tp_as_buffer) { |
| 2121 | type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER; |
| 2122 | type->tp_flags |= |
| 2123 | base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER; |
| 2124 | } |
| 2125 | if (!type->tp_as_sequence && base->tp_as_sequence) { |
| 2126 | type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN; |
| 2127 | type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN; |
| 2128 | } |
| 2129 | if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) != |
| 2130 | (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) { |
| 2131 | if ((!type->tp_as_number && base->tp_as_number) || |
| 2132 | (!type->tp_as_sequence && base->tp_as_sequence)) { |
| 2133 | type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS; |
| 2134 | if (!type->tp_as_number && !type->tp_as_sequence) { |
| 2135 | type->tp_flags |= base->tp_flags & |
| 2136 | Py_TPFLAGS_HAVE_INPLACEOPS; |
| 2137 | } |
| 2138 | } |
| 2139 | /* Wow */ |
| 2140 | } |
| 2141 | if (!type->tp_as_number && base->tp_as_number) { |
| 2142 | type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES; |
| 2143 | type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES; |
| 2144 | } |
| 2145 | |
| 2146 | /* Copying basicsize is connected to the GC flags */ |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 2147 | oldsize = base->tp_basicsize; |
| 2148 | newsize = type->tp_basicsize ? type->tp_basicsize : oldsize; |
| 2149 | if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 2150 | (base->tp_flags & Py_TPFLAGS_HAVE_GC) && |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2151 | (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) && |
| 2152 | (!type->tp_traverse && !type->tp_clear)) { |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 2153 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2154 | if (type->tp_traverse == NULL) |
| 2155 | type->tp_traverse = base->tp_traverse; |
| 2156 | if (type->tp_clear == NULL) |
| 2157 | type->tp_clear = base->tp_clear; |
| 2158 | } |
| 2159 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
Guido van Rossum | f884b74 | 2001-12-17 17:14:22 +0000 | [diff] [blame] | 2160 | /* The condition below could use some explanation. |
| 2161 | It appears that tp_new is not inherited for static types |
| 2162 | whose base class is 'object'; this seems to be a precaution |
| 2163 | so that old extension types don't suddenly become |
| 2164 | callable (object.__new__ wouldn't insure the invariants |
| 2165 | that the extension type's own factory function ensures). |
| 2166 | Heap types, of course, are under our control, so they do |
| 2167 | inherit tp_new; static extension types that specify some |
| 2168 | other built-in type as the default are considered |
| 2169 | new-style-aware so they also inherit object.__new__. */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2170 | if (base != &PyBaseObject_Type || |
| 2171 | (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 2172 | if (type->tp_new == NULL) |
| 2173 | type->tp_new = base->tp_new; |
| 2174 | } |
| 2175 | } |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 2176 | type->tp_basicsize = newsize; |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 2177 | |
| 2178 | /* Copy other non-function slots */ |
| 2179 | |
| 2180 | #undef COPYVAL |
| 2181 | #define COPYVAL(SLOT) \ |
| 2182 | if (type->SLOT == 0) type->SLOT = base->SLOT |
| 2183 | |
| 2184 | COPYVAL(tp_itemsize); |
| 2185 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) { |
| 2186 | COPYVAL(tp_weaklistoffset); |
| 2187 | } |
| 2188 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
| 2189 | COPYVAL(tp_dictoffset); |
| 2190 | } |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
| 2193 | static void |
| 2194 | inherit_slots(PyTypeObject *type, PyTypeObject *base) |
| 2195 | { |
| 2196 | PyTypeObject *basebase; |
| 2197 | |
| 2198 | #undef SLOTDEFINED |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2199 | #undef COPYSLOT |
| 2200 | #undef COPYNUM |
| 2201 | #undef COPYSEQ |
| 2202 | #undef COPYMAP |
Guido van Rossum | 5af588b | 2001-10-12 14:13:21 +0000 | [diff] [blame] | 2203 | #undef COPYBUF |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2204 | |
| 2205 | #define SLOTDEFINED(SLOT) \ |
| 2206 | (base->SLOT != 0 && \ |
| 2207 | (basebase == NULL || base->SLOT != basebase->SLOT)) |
| 2208 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2209 | #define COPYSLOT(SLOT) \ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2210 | if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2211 | |
| 2212 | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT) |
| 2213 | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT) |
| 2214 | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT) |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 2215 | #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2216 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2217 | /* This won't inherit indirect slots (from tp_as_number etc.) |
| 2218 | if type doesn't provide the space. */ |
| 2219 | |
| 2220 | if (type->tp_as_number != NULL && base->tp_as_number != NULL) { |
| 2221 | basebase = base->tp_base; |
| 2222 | if (basebase->tp_as_number == NULL) |
| 2223 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2224 | COPYNUM(nb_add); |
| 2225 | COPYNUM(nb_subtract); |
| 2226 | COPYNUM(nb_multiply); |
| 2227 | COPYNUM(nb_divide); |
| 2228 | COPYNUM(nb_remainder); |
| 2229 | COPYNUM(nb_divmod); |
| 2230 | COPYNUM(nb_power); |
| 2231 | COPYNUM(nb_negative); |
| 2232 | COPYNUM(nb_positive); |
| 2233 | COPYNUM(nb_absolute); |
| 2234 | COPYNUM(nb_nonzero); |
| 2235 | COPYNUM(nb_invert); |
| 2236 | COPYNUM(nb_lshift); |
| 2237 | COPYNUM(nb_rshift); |
| 2238 | COPYNUM(nb_and); |
| 2239 | COPYNUM(nb_xor); |
| 2240 | COPYNUM(nb_or); |
| 2241 | COPYNUM(nb_coerce); |
| 2242 | COPYNUM(nb_int); |
| 2243 | COPYNUM(nb_long); |
| 2244 | COPYNUM(nb_float); |
| 2245 | COPYNUM(nb_oct); |
| 2246 | COPYNUM(nb_hex); |
| 2247 | COPYNUM(nb_inplace_add); |
| 2248 | COPYNUM(nb_inplace_subtract); |
| 2249 | COPYNUM(nb_inplace_multiply); |
| 2250 | COPYNUM(nb_inplace_divide); |
| 2251 | COPYNUM(nb_inplace_remainder); |
| 2252 | COPYNUM(nb_inplace_power); |
| 2253 | COPYNUM(nb_inplace_lshift); |
| 2254 | COPYNUM(nb_inplace_rshift); |
| 2255 | COPYNUM(nb_inplace_and); |
| 2256 | COPYNUM(nb_inplace_xor); |
| 2257 | COPYNUM(nb_inplace_or); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2258 | if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) { |
| 2259 | COPYNUM(nb_true_divide); |
| 2260 | COPYNUM(nb_floor_divide); |
| 2261 | COPYNUM(nb_inplace_true_divide); |
| 2262 | COPYNUM(nb_inplace_floor_divide); |
| 2263 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2266 | if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) { |
| 2267 | basebase = base->tp_base; |
| 2268 | if (basebase->tp_as_sequence == NULL) |
| 2269 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2270 | COPYSEQ(sq_length); |
| 2271 | COPYSEQ(sq_concat); |
| 2272 | COPYSEQ(sq_repeat); |
| 2273 | COPYSEQ(sq_item); |
| 2274 | COPYSEQ(sq_slice); |
| 2275 | COPYSEQ(sq_ass_item); |
| 2276 | COPYSEQ(sq_ass_slice); |
| 2277 | COPYSEQ(sq_contains); |
| 2278 | COPYSEQ(sq_inplace_concat); |
| 2279 | COPYSEQ(sq_inplace_repeat); |
| 2280 | } |
| 2281 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2282 | if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) { |
| 2283 | basebase = base->tp_base; |
| 2284 | if (basebase->tp_as_mapping == NULL) |
| 2285 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2286 | COPYMAP(mp_length); |
| 2287 | COPYMAP(mp_subscript); |
| 2288 | COPYMAP(mp_ass_subscript); |
| 2289 | } |
| 2290 | |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 2291 | if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) { |
| 2292 | basebase = base->tp_base; |
| 2293 | if (basebase->tp_as_buffer == NULL) |
| 2294 | basebase = NULL; |
| 2295 | COPYBUF(bf_getreadbuffer); |
| 2296 | COPYBUF(bf_getwritebuffer); |
| 2297 | COPYBUF(bf_getsegcount); |
| 2298 | COPYBUF(bf_getcharbuffer); |
| 2299 | } |
| 2300 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2301 | basebase = base->tp_base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2302 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2303 | COPYSLOT(tp_dealloc); |
| 2304 | COPYSLOT(tp_print); |
| 2305 | if (type->tp_getattr == NULL && type->tp_getattro == NULL) { |
| 2306 | type->tp_getattr = base->tp_getattr; |
| 2307 | type->tp_getattro = base->tp_getattro; |
| 2308 | } |
| 2309 | if (type->tp_setattr == NULL && type->tp_setattro == NULL) { |
| 2310 | type->tp_setattr = base->tp_setattr; |
| 2311 | type->tp_setattro = base->tp_setattro; |
| 2312 | } |
| 2313 | /* tp_compare see tp_richcompare */ |
| 2314 | COPYSLOT(tp_repr); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2315 | /* tp_hash see tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2316 | COPYSLOT(tp_call); |
| 2317 | COPYSLOT(tp_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2318 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2319 | if (type->tp_compare == NULL && |
| 2320 | type->tp_richcompare == NULL && |
| 2321 | type->tp_hash == NULL) |
| 2322 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2323 | type->tp_compare = base->tp_compare; |
| 2324 | type->tp_richcompare = base->tp_richcompare; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2325 | type->tp_hash = base->tp_hash; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2326 | } |
| 2327 | } |
| 2328 | else { |
| 2329 | COPYSLOT(tp_compare); |
| 2330 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2331 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) { |
| 2332 | COPYSLOT(tp_iter); |
| 2333 | COPYSLOT(tp_iternext); |
| 2334 | } |
| 2335 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
| 2336 | COPYSLOT(tp_descr_get); |
| 2337 | COPYSLOT(tp_descr_set); |
| 2338 | COPYSLOT(tp_dictoffset); |
| 2339 | COPYSLOT(tp_init); |
| 2340 | COPYSLOT(tp_alloc); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2341 | COPYSLOT(tp_free); |
Guido van Rossum | cc8fe04 | 2002-04-05 17:10:16 +0000 | [diff] [blame] | 2342 | COPYSLOT(tp_is_gc); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2343 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2344 | } |
| 2345 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 2346 | static int add_operators(PyTypeObject *); |
| 2347 | static int add_subclass(PyTypeObject *base, PyTypeObject *type); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2348 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2349 | int |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 2350 | PyType_Ready(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2351 | { |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 2352 | PyObject *dict, *bases; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2353 | PyTypeObject *base; |
| 2354 | int i, n; |
| 2355 | |
Guido van Rossum | cab0580 | 2002-06-10 15:29:03 +0000 | [diff] [blame] | 2356 | if (type->tp_flags & Py_TPFLAGS_READY) { |
| 2357 | assert(type->tp_dict != NULL); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2358 | return 0; |
Guido van Rossum | cab0580 | 2002-06-10 15:29:03 +0000 | [diff] [blame] | 2359 | } |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2360 | assert((type->tp_flags & Py_TPFLAGS_READYING) == 0); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2361 | |
| 2362 | type->tp_flags |= Py_TPFLAGS_READYING; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2363 | |
| 2364 | /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
| 2365 | base = type->tp_base; |
| 2366 | if (base == NULL && type != &PyBaseObject_Type) |
| 2367 | base = type->tp_base = &PyBaseObject_Type; |
| 2368 | |
Guido van Rossum | 323a9cf | 2002-08-14 17:26:30 +0000 | [diff] [blame] | 2369 | /* Initialize the base class */ |
| 2370 | if (base && base->tp_dict == NULL) { |
| 2371 | if (PyType_Ready(base) < 0) |
| 2372 | goto error; |
| 2373 | } |
| 2374 | |
Guido van Rossum | 0986d82 | 2002-04-08 01:38:42 +0000 | [diff] [blame] | 2375 | /* Initialize ob_type if NULL. This means extensions that want to be |
| 2376 | compilable separately on Windows can call PyType_Ready() instead of |
| 2377 | initializing the ob_type field of their type objects. */ |
| 2378 | if (type->ob_type == NULL) |
| 2379 | type->ob_type = base->ob_type; |
| 2380 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2381 | /* Initialize tp_bases */ |
| 2382 | bases = type->tp_bases; |
| 2383 | if (bases == NULL) { |
| 2384 | if (base == NULL) |
| 2385 | bases = PyTuple_New(0); |
| 2386 | else |
| 2387 | bases = Py_BuildValue("(O)", base); |
| 2388 | if (bases == NULL) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2389 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2390 | type->tp_bases = bases; |
| 2391 | } |
| 2392 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2393 | /* Initialize tp_dict */ |
| 2394 | dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2395 | if (dict == NULL) { |
| 2396 | dict = PyDict_New(); |
| 2397 | if (dict == NULL) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2398 | goto error; |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2399 | type->tp_dict = dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2402 | /* Add type-specific descriptors to tp_dict */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2403 | if (add_operators(type) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2404 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2405 | if (type->tp_methods != NULL) { |
| 2406 | if (add_methods(type, type->tp_methods) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2407 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2408 | } |
| 2409 | if (type->tp_members != NULL) { |
| 2410 | if (add_members(type, type->tp_members) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2411 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2412 | } |
| 2413 | if (type->tp_getset != NULL) { |
| 2414 | if (add_getset(type, type->tp_getset) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2415 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2418 | /* Calculate method resolution order */ |
| 2419 | if (mro_internal(type) < 0) { |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2420 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2421 | } |
| 2422 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2423 | /* Inherit special flags from dominant base */ |
| 2424 | if (type->tp_base != NULL) |
| 2425 | inherit_special(type, type->tp_base); |
| 2426 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2427 | /* Initialize tp_dict properly */ |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 2428 | bases = type->tp_mro; |
| 2429 | assert(bases != NULL); |
| 2430 | assert(PyTuple_Check(bases)); |
| 2431 | n = PyTuple_GET_SIZE(bases); |
| 2432 | for (i = 1; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 2433 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 2434 | if (PyType_Check(b)) |
| 2435 | inherit_slots(type, (PyTypeObject *)b); |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 2436 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2437 | |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 2438 | /* if the type dictionary doesn't contain a __doc__, set it from |
| 2439 | the tp_doc slot. |
| 2440 | */ |
| 2441 | if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) { |
| 2442 | if (type->tp_doc != NULL) { |
| 2443 | PyObject *doc = PyString_FromString(type->tp_doc); |
| 2444 | PyDict_SetItemString(type->tp_dict, "__doc__", doc); |
| 2445 | Py_DECREF(doc); |
| 2446 | } else { |
Guido van Rossum | d464107 | 2002-04-03 02:13:37 +0000 | [diff] [blame] | 2447 | PyDict_SetItemString(type->tp_dict, |
| 2448 | "__doc__", Py_None); |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 2449 | } |
| 2450 | } |
| 2451 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2452 | /* Some more special stuff */ |
| 2453 | base = type->tp_base; |
| 2454 | if (base != NULL) { |
| 2455 | if (type->tp_as_number == NULL) |
| 2456 | type->tp_as_number = base->tp_as_number; |
| 2457 | if (type->tp_as_sequence == NULL) |
| 2458 | type->tp_as_sequence = base->tp_as_sequence; |
| 2459 | if (type->tp_as_mapping == NULL) |
| 2460 | type->tp_as_mapping = base->tp_as_mapping; |
| 2461 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2462 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2463 | /* Link into each base class's list of subclasses */ |
| 2464 | bases = type->tp_bases; |
| 2465 | n = PyTuple_GET_SIZE(bases); |
| 2466 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 2467 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 2468 | if (PyType_Check(b) && |
| 2469 | add_subclass((PyTypeObject *)b, type) < 0) |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2470 | goto error; |
| 2471 | } |
| 2472 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2473 | /* All done -- set the ready flag */ |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2474 | assert(type->tp_dict != NULL); |
| 2475 | type->tp_flags = |
| 2476 | (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2477 | return 0; |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2478 | |
| 2479 | error: |
| 2480 | type->tp_flags &= ~Py_TPFLAGS_READYING; |
| 2481 | return -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2484 | static int |
| 2485 | add_subclass(PyTypeObject *base, PyTypeObject *type) |
| 2486 | { |
| 2487 | int i; |
| 2488 | PyObject *list, *ref, *new; |
| 2489 | |
| 2490 | list = base->tp_subclasses; |
| 2491 | if (list == NULL) { |
| 2492 | base->tp_subclasses = list = PyList_New(0); |
| 2493 | if (list == NULL) |
| 2494 | return -1; |
| 2495 | } |
| 2496 | assert(PyList_Check(list)); |
| 2497 | new = PyWeakref_NewRef((PyObject *)type, NULL); |
| 2498 | i = PyList_GET_SIZE(list); |
| 2499 | while (--i >= 0) { |
| 2500 | ref = PyList_GET_ITEM(list, i); |
| 2501 | assert(PyWeakref_CheckRef(ref)); |
Guido van Rossum | 3930bc3 | 2002-10-18 13:51:49 +0000 | [diff] [blame] | 2502 | if (PyWeakref_GET_OBJECT(ref) == Py_None) |
| 2503 | return PyList_SetItem(list, i, new); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2504 | } |
| 2505 | i = PyList_Append(list, new); |
| 2506 | Py_DECREF(new); |
| 2507 | return i; |
| 2508 | } |
| 2509 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2510 | |
| 2511 | /* Generic wrappers for overloadable 'operators' such as __getitem__ */ |
| 2512 | |
| 2513 | /* There's a wrapper *function* for each distinct function typedef used |
| 2514 | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a |
| 2515 | wrapper *table* for each distinct operation (e.g. __len__, __add__). |
| 2516 | Most tables have only one entry; the tables for binary operators have two |
| 2517 | entries, one regular and one with reversed arguments. */ |
| 2518 | |
| 2519 | static PyObject * |
| 2520 | wrap_inquiry(PyObject *self, PyObject *args, void *wrapped) |
| 2521 | { |
| 2522 | inquiry func = (inquiry)wrapped; |
| 2523 | int res; |
| 2524 | |
| 2525 | if (!PyArg_ParseTuple(args, "")) |
| 2526 | return NULL; |
| 2527 | res = (*func)(self); |
| 2528 | if (res == -1 && PyErr_Occurred()) |
| 2529 | return NULL; |
| 2530 | return PyInt_FromLong((long)res); |
| 2531 | } |
| 2532 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2533 | static PyObject * |
| 2534 | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 2535 | { |
| 2536 | binaryfunc func = (binaryfunc)wrapped; |
| 2537 | PyObject *other; |
| 2538 | |
| 2539 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 2540 | return NULL; |
| 2541 | return (*func)(self, other); |
| 2542 | } |
| 2543 | |
| 2544 | static PyObject * |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 2545 | wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped) |
| 2546 | { |
| 2547 | binaryfunc func = (binaryfunc)wrapped; |
| 2548 | PyObject *other; |
| 2549 | |
| 2550 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 2551 | return NULL; |
| 2552 | if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) && |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 2553 | !PyType_IsSubtype(other->ob_type, self->ob_type)) { |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 2554 | Py_INCREF(Py_NotImplemented); |
| 2555 | return Py_NotImplemented; |
| 2556 | } |
| 2557 | return (*func)(self, other); |
| 2558 | } |
| 2559 | |
| 2560 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2561 | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 2562 | { |
| 2563 | binaryfunc func = (binaryfunc)wrapped; |
| 2564 | PyObject *other; |
| 2565 | |
| 2566 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 2567 | return NULL; |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 2568 | if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) && |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 2569 | !PyType_IsSubtype(other->ob_type, self->ob_type)) { |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 2570 | Py_INCREF(Py_NotImplemented); |
| 2571 | return Py_NotImplemented; |
| 2572 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2573 | return (*func)(other, self); |
| 2574 | } |
| 2575 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 2576 | static PyObject * |
| 2577 | wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped) |
| 2578 | { |
| 2579 | coercion func = (coercion)wrapped; |
| 2580 | PyObject *other, *res; |
| 2581 | int ok; |
| 2582 | |
| 2583 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 2584 | return NULL; |
| 2585 | ok = func(&self, &other); |
| 2586 | if (ok < 0) |
| 2587 | return NULL; |
| 2588 | if (ok > 0) { |
| 2589 | Py_INCREF(Py_NotImplemented); |
| 2590 | return Py_NotImplemented; |
| 2591 | } |
| 2592 | res = PyTuple_New(2); |
| 2593 | if (res == NULL) { |
| 2594 | Py_DECREF(self); |
| 2595 | Py_DECREF(other); |
| 2596 | return NULL; |
| 2597 | } |
| 2598 | PyTuple_SET_ITEM(res, 0, self); |
| 2599 | PyTuple_SET_ITEM(res, 1, other); |
| 2600 | return res; |
| 2601 | } |
| 2602 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2603 | static PyObject * |
| 2604 | wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 2605 | { |
| 2606 | ternaryfunc func = (ternaryfunc)wrapped; |
| 2607 | PyObject *other; |
| 2608 | PyObject *third = Py_None; |
| 2609 | |
| 2610 | /* Note: This wrapper only works for __pow__() */ |
| 2611 | |
| 2612 | if (!PyArg_ParseTuple(args, "O|O", &other, &third)) |
| 2613 | return NULL; |
| 2614 | return (*func)(self, other, third); |
| 2615 | } |
| 2616 | |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 2617 | static PyObject * |
| 2618 | wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 2619 | { |
| 2620 | ternaryfunc func = (ternaryfunc)wrapped; |
| 2621 | PyObject *other; |
| 2622 | PyObject *third = Py_None; |
| 2623 | |
| 2624 | /* Note: This wrapper only works for __pow__() */ |
| 2625 | |
| 2626 | if (!PyArg_ParseTuple(args, "O|O", &other, &third)) |
| 2627 | return NULL; |
| 2628 | return (*func)(other, self, third); |
| 2629 | } |
| 2630 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2631 | static PyObject * |
| 2632 | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 2633 | { |
| 2634 | unaryfunc func = (unaryfunc)wrapped; |
| 2635 | |
| 2636 | if (!PyArg_ParseTuple(args, "")) |
| 2637 | return NULL; |
| 2638 | return (*func)(self); |
| 2639 | } |
| 2640 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2641 | static PyObject * |
| 2642 | wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped) |
| 2643 | { |
| 2644 | intargfunc func = (intargfunc)wrapped; |
| 2645 | int i; |
| 2646 | |
| 2647 | if (!PyArg_ParseTuple(args, "i", &i)) |
| 2648 | return NULL; |
| 2649 | return (*func)(self, i); |
| 2650 | } |
| 2651 | |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 2652 | static int |
| 2653 | getindex(PyObject *self, PyObject *arg) |
| 2654 | { |
| 2655 | int i; |
| 2656 | |
| 2657 | i = PyInt_AsLong(arg); |
| 2658 | if (i == -1 && PyErr_Occurred()) |
| 2659 | return -1; |
| 2660 | if (i < 0) { |
| 2661 | PySequenceMethods *sq = self->ob_type->tp_as_sequence; |
| 2662 | if (sq && sq->sq_length) { |
| 2663 | int n = (*sq->sq_length)(self); |
| 2664 | if (n < 0) |
| 2665 | return -1; |
| 2666 | i += n; |
| 2667 | } |
| 2668 | } |
| 2669 | return i; |
| 2670 | } |
| 2671 | |
| 2672 | static PyObject * |
| 2673 | wrap_sq_item(PyObject *self, PyObject *args, void *wrapped) |
| 2674 | { |
| 2675 | intargfunc func = (intargfunc)wrapped; |
| 2676 | PyObject *arg; |
| 2677 | int i; |
| 2678 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 2679 | if (PyTuple_GET_SIZE(args) == 1) { |
| 2680 | arg = PyTuple_GET_ITEM(args, 0); |
| 2681 | i = getindex(self, arg); |
| 2682 | if (i == -1 && PyErr_Occurred()) |
| 2683 | return NULL; |
| 2684 | return (*func)(self, i); |
| 2685 | } |
| 2686 | PyArg_ParseTuple(args, "O", &arg); |
| 2687 | assert(PyErr_Occurred()); |
| 2688 | return NULL; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 2689 | } |
| 2690 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2691 | static PyObject * |
| 2692 | wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped) |
| 2693 | { |
| 2694 | intintargfunc func = (intintargfunc)wrapped; |
| 2695 | int i, j; |
| 2696 | |
| 2697 | if (!PyArg_ParseTuple(args, "ii", &i, &j)) |
| 2698 | return NULL; |
| 2699 | return (*func)(self, i, j); |
| 2700 | } |
| 2701 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2702 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 2703 | wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2704 | { |
| 2705 | intobjargproc func = (intobjargproc)wrapped; |
| 2706 | int i, res; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 2707 | PyObject *arg, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2708 | |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 2709 | if (!PyArg_ParseTuple(args, "OO", &arg, &value)) |
| 2710 | return NULL; |
| 2711 | i = getindex(self, arg); |
| 2712 | if (i == -1 && PyErr_Occurred()) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2713 | return NULL; |
| 2714 | res = (*func)(self, i, value); |
| 2715 | if (res == -1 && PyErr_Occurred()) |
| 2716 | return NULL; |
| 2717 | Py_INCREF(Py_None); |
| 2718 | return Py_None; |
| 2719 | } |
| 2720 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 2721 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 2722 | wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 2723 | { |
| 2724 | intobjargproc func = (intobjargproc)wrapped; |
| 2725 | int i, res; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 2726 | PyObject *arg; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 2727 | |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 2728 | if (!PyArg_ParseTuple(args, "O", &arg)) |
| 2729 | return NULL; |
| 2730 | i = getindex(self, arg); |
| 2731 | if (i == -1 && PyErr_Occurred()) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 2732 | return NULL; |
| 2733 | res = (*func)(self, i, NULL); |
| 2734 | if (res == -1 && PyErr_Occurred()) |
| 2735 | return NULL; |
| 2736 | Py_INCREF(Py_None); |
| 2737 | return Py_None; |
| 2738 | } |
| 2739 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2740 | static PyObject * |
| 2741 | wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 2742 | { |
| 2743 | intintobjargproc func = (intintobjargproc)wrapped; |
| 2744 | int i, j, res; |
| 2745 | PyObject *value; |
| 2746 | |
| 2747 | if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value)) |
| 2748 | return NULL; |
| 2749 | res = (*func)(self, i, j, value); |
| 2750 | if (res == -1 && PyErr_Occurred()) |
| 2751 | return NULL; |
| 2752 | Py_INCREF(Py_None); |
| 2753 | return Py_None; |
| 2754 | } |
| 2755 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 2756 | static PyObject * |
| 2757 | wrap_delslice(PyObject *self, PyObject *args, void *wrapped) |
| 2758 | { |
| 2759 | intintobjargproc func = (intintobjargproc)wrapped; |
| 2760 | int i, j, res; |
| 2761 | |
| 2762 | if (!PyArg_ParseTuple(args, "ii", &i, &j)) |
| 2763 | return NULL; |
| 2764 | res = (*func)(self, i, j, NULL); |
| 2765 | if (res == -1 && PyErr_Occurred()) |
| 2766 | return NULL; |
| 2767 | Py_INCREF(Py_None); |
| 2768 | return Py_None; |
| 2769 | } |
| 2770 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2771 | /* XXX objobjproc is a misnomer; should be objargpred */ |
| 2772 | static PyObject * |
| 2773 | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) |
| 2774 | { |
| 2775 | objobjproc func = (objobjproc)wrapped; |
| 2776 | int res; |
| 2777 | PyObject *value; |
| 2778 | |
| 2779 | if (!PyArg_ParseTuple(args, "O", &value)) |
| 2780 | return NULL; |
| 2781 | res = (*func)(self, value); |
| 2782 | if (res == -1 && PyErr_Occurred()) |
| 2783 | return NULL; |
| 2784 | return PyInt_FromLong((long)res); |
| 2785 | } |
| 2786 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2787 | static PyObject * |
| 2788 | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 2789 | { |
| 2790 | objobjargproc func = (objobjargproc)wrapped; |
| 2791 | int res; |
| 2792 | PyObject *key, *value; |
| 2793 | |
| 2794 | if (!PyArg_ParseTuple(args, "OO", &key, &value)) |
| 2795 | return NULL; |
| 2796 | res = (*func)(self, key, value); |
| 2797 | if (res == -1 && PyErr_Occurred()) |
| 2798 | return NULL; |
| 2799 | Py_INCREF(Py_None); |
| 2800 | return Py_None; |
| 2801 | } |
| 2802 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 2803 | static PyObject * |
| 2804 | wrap_delitem(PyObject *self, PyObject *args, void *wrapped) |
| 2805 | { |
| 2806 | objobjargproc func = (objobjargproc)wrapped; |
| 2807 | int res; |
| 2808 | PyObject *key; |
| 2809 | |
| 2810 | if (!PyArg_ParseTuple(args, "O", &key)) |
| 2811 | return NULL; |
| 2812 | res = (*func)(self, key, NULL); |
| 2813 | if (res == -1 && PyErr_Occurred()) |
| 2814 | return NULL; |
| 2815 | Py_INCREF(Py_None); |
| 2816 | return Py_None; |
| 2817 | } |
| 2818 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2819 | static PyObject * |
| 2820 | wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped) |
| 2821 | { |
| 2822 | cmpfunc func = (cmpfunc)wrapped; |
| 2823 | int res; |
| 2824 | PyObject *other; |
| 2825 | |
| 2826 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 2827 | return NULL; |
Guido van Rossum | 3d45d8f | 2001-09-24 18:47:40 +0000 | [diff] [blame] | 2828 | if (other->ob_type->tp_compare != func && |
| 2829 | !PyType_IsSubtype(other->ob_type, self->ob_type)) { |
Guido van Rossum | ceccae5 | 2001-09-18 20:03:57 +0000 | [diff] [blame] | 2830 | PyErr_Format( |
| 2831 | PyExc_TypeError, |
| 2832 | "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'", |
| 2833 | self->ob_type->tp_name, |
| 2834 | self->ob_type->tp_name, |
| 2835 | other->ob_type->tp_name); |
| 2836 | return NULL; |
| 2837 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2838 | res = (*func)(self, other); |
| 2839 | if (PyErr_Occurred()) |
| 2840 | return NULL; |
| 2841 | return PyInt_FromLong((long)res); |
| 2842 | } |
| 2843 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2844 | static PyObject * |
| 2845 | wrap_setattr(PyObject *self, PyObject *args, void *wrapped) |
| 2846 | { |
| 2847 | setattrofunc func = (setattrofunc)wrapped; |
| 2848 | int res; |
| 2849 | PyObject *name, *value; |
| 2850 | |
| 2851 | if (!PyArg_ParseTuple(args, "OO", &name, &value)) |
| 2852 | return NULL; |
| 2853 | res = (*func)(self, name, value); |
| 2854 | if (res < 0) |
| 2855 | return NULL; |
| 2856 | Py_INCREF(Py_None); |
| 2857 | return Py_None; |
| 2858 | } |
| 2859 | |
| 2860 | static PyObject * |
| 2861 | wrap_delattr(PyObject *self, PyObject *args, void *wrapped) |
| 2862 | { |
| 2863 | setattrofunc func = (setattrofunc)wrapped; |
| 2864 | int res; |
| 2865 | PyObject *name; |
| 2866 | |
| 2867 | if (!PyArg_ParseTuple(args, "O", &name)) |
| 2868 | return NULL; |
| 2869 | res = (*func)(self, name, NULL); |
| 2870 | if (res < 0) |
| 2871 | return NULL; |
| 2872 | Py_INCREF(Py_None); |
| 2873 | return Py_None; |
| 2874 | } |
| 2875 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2876 | static PyObject * |
| 2877 | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) |
| 2878 | { |
| 2879 | hashfunc func = (hashfunc)wrapped; |
| 2880 | long res; |
| 2881 | |
| 2882 | if (!PyArg_ParseTuple(args, "")) |
| 2883 | return NULL; |
| 2884 | res = (*func)(self); |
| 2885 | if (res == -1 && PyErr_Occurred()) |
| 2886 | return NULL; |
| 2887 | return PyInt_FromLong(res); |
| 2888 | } |
| 2889 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2890 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 2891 | wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2892 | { |
| 2893 | ternaryfunc func = (ternaryfunc)wrapped; |
| 2894 | |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 2895 | return (*func)(self, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2896 | } |
| 2897 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2898 | static PyObject * |
| 2899 | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) |
| 2900 | { |
| 2901 | richcmpfunc func = (richcmpfunc)wrapped; |
| 2902 | PyObject *other; |
| 2903 | |
| 2904 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 2905 | return NULL; |
| 2906 | return (*func)(self, other, op); |
| 2907 | } |
| 2908 | |
| 2909 | #undef RICHCMP_WRAPPER |
| 2910 | #define RICHCMP_WRAPPER(NAME, OP) \ |
| 2911 | static PyObject * \ |
| 2912 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ |
| 2913 | { \ |
| 2914 | return wrap_richcmpfunc(self, args, wrapped, OP); \ |
| 2915 | } |
| 2916 | |
Jack Jansen | 8e938b4 | 2001-08-08 15:29:49 +0000 | [diff] [blame] | 2917 | RICHCMP_WRAPPER(lt, Py_LT) |
| 2918 | RICHCMP_WRAPPER(le, Py_LE) |
| 2919 | RICHCMP_WRAPPER(eq, Py_EQ) |
| 2920 | RICHCMP_WRAPPER(ne, Py_NE) |
| 2921 | RICHCMP_WRAPPER(gt, Py_GT) |
| 2922 | RICHCMP_WRAPPER(ge, Py_GE) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2923 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2924 | static PyObject * |
| 2925 | wrap_next(PyObject *self, PyObject *args, void *wrapped) |
| 2926 | { |
| 2927 | unaryfunc func = (unaryfunc)wrapped; |
| 2928 | PyObject *res; |
| 2929 | |
| 2930 | if (!PyArg_ParseTuple(args, "")) |
| 2931 | return NULL; |
| 2932 | res = (*func)(self); |
| 2933 | if (res == NULL && !PyErr_Occurred()) |
| 2934 | PyErr_SetNone(PyExc_StopIteration); |
| 2935 | return res; |
| 2936 | } |
| 2937 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2938 | static PyObject * |
| 2939 | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped) |
| 2940 | { |
| 2941 | descrgetfunc func = (descrgetfunc)wrapped; |
| 2942 | PyObject *obj; |
| 2943 | PyObject *type = NULL; |
| 2944 | |
| 2945 | if (!PyArg_ParseTuple(args, "O|O", &obj, &type)) |
| 2946 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2947 | return (*func)(self, obj, type); |
| 2948 | } |
| 2949 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2950 | static PyObject * |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 2951 | wrap_descr_set(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2952 | { |
| 2953 | descrsetfunc func = (descrsetfunc)wrapped; |
| 2954 | PyObject *obj, *value; |
| 2955 | int ret; |
| 2956 | |
| 2957 | if (!PyArg_ParseTuple(args, "OO", &obj, &value)) |
| 2958 | return NULL; |
| 2959 | ret = (*func)(self, obj, value); |
| 2960 | if (ret < 0) |
| 2961 | return NULL; |
| 2962 | Py_INCREF(Py_None); |
| 2963 | return Py_None; |
| 2964 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 2965 | |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 2966 | static PyObject * |
| 2967 | wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped) |
| 2968 | { |
| 2969 | descrsetfunc func = (descrsetfunc)wrapped; |
| 2970 | PyObject *obj; |
| 2971 | int ret; |
| 2972 | |
| 2973 | if (!PyArg_ParseTuple(args, "O", &obj)) |
| 2974 | return NULL; |
| 2975 | ret = (*func)(self, obj, NULL); |
| 2976 | if (ret < 0) |
| 2977 | return NULL; |
| 2978 | Py_INCREF(Py_None); |
| 2979 | return Py_None; |
| 2980 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2981 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2982 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 2983 | wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2984 | { |
| 2985 | initproc func = (initproc)wrapped; |
| 2986 | |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 2987 | if (func(self, args, kwds) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2988 | return NULL; |
| 2989 | Py_INCREF(Py_None); |
| 2990 | return Py_None; |
| 2991 | } |
| 2992 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2993 | static PyObject * |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 2994 | tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2995 | { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 2996 | PyTypeObject *type, *subtype, *staticbase; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 2997 | PyObject *arg0, *res; |
| 2998 | |
| 2999 | if (self == NULL || !PyType_Check(self)) |
| 3000 | Py_FatalError("__new__() called with non-type 'self'"); |
| 3001 | type = (PyTypeObject *)self; |
| 3002 | if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 3003 | PyErr_Format(PyExc_TypeError, |
| 3004 | "%s.__new__(): not enough arguments", |
| 3005 | type->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 3006 | return NULL; |
| 3007 | } |
| 3008 | arg0 = PyTuple_GET_ITEM(args, 0); |
| 3009 | if (!PyType_Check(arg0)) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 3010 | PyErr_Format(PyExc_TypeError, |
| 3011 | "%s.__new__(X): X is not a type object (%s)", |
| 3012 | type->tp_name, |
| 3013 | arg0->ob_type->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 3014 | return NULL; |
| 3015 | } |
| 3016 | subtype = (PyTypeObject *)arg0; |
| 3017 | if (!PyType_IsSubtype(subtype, type)) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 3018 | PyErr_Format(PyExc_TypeError, |
| 3019 | "%s.__new__(%s): %s is not a subtype of %s", |
| 3020 | type->tp_name, |
| 3021 | subtype->tp_name, |
| 3022 | subtype->tp_name, |
| 3023 | type->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 3024 | return NULL; |
| 3025 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 3026 | |
| 3027 | /* Check that the use doesn't do something silly and unsafe like |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 3028 | object.__new__(dict). To do this, we check that the |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 3029 | most derived base that's not a heap type is this type. */ |
| 3030 | staticbase = subtype; |
| 3031 | while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 3032 | staticbase = staticbase->tp_base; |
Guido van Rossum | a8c60f4 | 2001-09-14 19:43:36 +0000 | [diff] [blame] | 3033 | if (staticbase->tp_new != type->tp_new) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 3034 | PyErr_Format(PyExc_TypeError, |
| 3035 | "%s.__new__(%s) is not safe, use %s.__new__()", |
| 3036 | type->tp_name, |
| 3037 | subtype->tp_name, |
| 3038 | staticbase == NULL ? "?" : staticbase->tp_name); |
| 3039 | return NULL; |
| 3040 | } |
| 3041 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 3042 | args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 3043 | if (args == NULL) |
| 3044 | return NULL; |
| 3045 | res = type->tp_new(subtype, args, kwds); |
| 3046 | Py_DECREF(args); |
| 3047 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3048 | } |
| 3049 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 3050 | static struct PyMethodDef tp_new_methoddef[] = { |
| 3051 | {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 3052 | PyDoc_STR("T.__new__(S, ...) -> " |
| 3053 | "a new object with type S, a subtype of T")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3054 | {0} |
| 3055 | }; |
| 3056 | |
| 3057 | static int |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 3058 | add_tp_new_wrapper(PyTypeObject *type) |
| 3059 | { |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 3060 | PyObject *func; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 3061 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3062 | if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL) |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 3063 | return 0; |
| 3064 | func = PyCFunction_New(tp_new_methoddef, (PyObject *)type); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 3065 | if (func == NULL) |
| 3066 | return -1; |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3067 | return PyDict_SetItemString(type->tp_dict, "__new__", func); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 3070 | /* Slot wrappers that call the corresponding __foo__ slot. See comments |
| 3071 | below at override_slots() for more explanation. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3072 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3073 | #define SLOT0(FUNCNAME, OPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3074 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3075 | FUNCNAME(PyObject *self) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3076 | { \ |
Guido van Rossum | 5592e4d | 2001-08-28 18:28:21 +0000 | [diff] [blame] | 3077 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3078 | return call_method(self, OPSTR, &cache_str, "()"); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3079 | } |
| 3080 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3081 | #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3082 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3083 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3084 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3085 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3086 | return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3087 | } |
| 3088 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3089 | |
| 3090 | #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3091 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3092 | FUNCNAME(PyObject *self, PyObject *other) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3093 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3094 | static PyObject *cache_str, *rcache_str; \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3095 | int do_other = self->ob_type != other->ob_type && \ |
| 3096 | other->ob_type->tp_as_number != NULL && \ |
| 3097 | other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3098 | if (self->ob_type->tp_as_number != NULL && \ |
| 3099 | self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \ |
| 3100 | PyObject *r; \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3101 | if (do_other && \ |
| 3102 | PyType_IsSubtype(other->ob_type, self->ob_type)) { \ |
| 3103 | r = call_maybe( \ |
| 3104 | other, ROPSTR, &rcache_str, "(O)", self); \ |
| 3105 | if (r != Py_NotImplemented) \ |
| 3106 | return r; \ |
| 3107 | Py_DECREF(r); \ |
| 3108 | do_other = 0; \ |
| 3109 | } \ |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 3110 | r = call_maybe( \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3111 | self, OPSTR, &cache_str, "(O)", other); \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3112 | if (r != Py_NotImplemented || \ |
| 3113 | other->ob_type == self->ob_type) \ |
| 3114 | return r; \ |
| 3115 | Py_DECREF(r); \ |
| 3116 | } \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3117 | if (do_other) { \ |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 3118 | return call_maybe( \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3119 | other, ROPSTR, &rcache_str, "(O)", self); \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3120 | } \ |
| 3121 | Py_INCREF(Py_NotImplemented); \ |
| 3122 | return Py_NotImplemented; \ |
| 3123 | } |
| 3124 | |
| 3125 | #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ |
| 3126 | SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR) |
| 3127 | |
| 3128 | #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \ |
| 3129 | static PyObject * \ |
| 3130 | FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ |
| 3131 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3132 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3133 | return call_method(self, OPSTR, &cache_str, \ |
| 3134 | "(" ARGCODES ")", arg1, arg2); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3135 | } |
| 3136 | |
| 3137 | static int |
| 3138 | slot_sq_length(PyObject *self) |
| 3139 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3140 | static PyObject *len_str; |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3141 | PyObject *res = call_method(self, "__len__", &len_str, "()"); |
Guido van Rossum | 2611162 | 2001-10-01 16:42:49 +0000 | [diff] [blame] | 3142 | int len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3143 | |
| 3144 | if (res == NULL) |
| 3145 | return -1; |
Guido van Rossum | 2611162 | 2001-10-01 16:42:49 +0000 | [diff] [blame] | 3146 | len = (int)PyInt_AsLong(res); |
| 3147 | Py_DECREF(res); |
Jeremy Hylton | 73a088e | 2002-07-25 16:43:29 +0000 | [diff] [blame] | 3148 | if (len == -1 && PyErr_Occurred()) |
| 3149 | return -1; |
Jeremy Hylton | f20fcf9 | 2002-07-25 16:06:15 +0000 | [diff] [blame] | 3150 | if (len < 0) { |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 3151 | PyErr_SetString(PyExc_ValueError, |
Jeremy Hylton | f20fcf9 | 2002-07-25 16:06:15 +0000 | [diff] [blame] | 3152 | "__len__() should return >= 0"); |
| 3153 | return -1; |
| 3154 | } |
Guido van Rossum | 2611162 | 2001-10-01 16:42:49 +0000 | [diff] [blame] | 3155 | return len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3156 | } |
| 3157 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3158 | SLOT1(slot_sq_concat, "__add__", PyObject *, "O") |
| 3159 | SLOT1(slot_sq_repeat, "__mul__", int, "i") |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 3160 | |
| 3161 | /* Super-optimized version of slot_sq_item. |
| 3162 | Other slots could do the same... */ |
| 3163 | static PyObject * |
| 3164 | slot_sq_item(PyObject *self, int i) |
| 3165 | { |
| 3166 | static PyObject *getitem_str; |
| 3167 | PyObject *func, *args = NULL, *ival = NULL, *retval = NULL; |
| 3168 | descrgetfunc f; |
| 3169 | |
| 3170 | if (getitem_str == NULL) { |
| 3171 | getitem_str = PyString_InternFromString("__getitem__"); |
| 3172 | if (getitem_str == NULL) |
| 3173 | return NULL; |
| 3174 | } |
| 3175 | func = _PyType_Lookup(self->ob_type, getitem_str); |
| 3176 | if (func != NULL) { |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 3177 | if ((f = func->ob_type->tp_descr_get) == NULL) |
| 3178 | Py_INCREF(func); |
Neal Norwitz | 673cd82 | 2002-10-18 16:33:13 +0000 | [diff] [blame] | 3179 | else { |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 3180 | func = f(func, self, (PyObject *)(self->ob_type)); |
Neal Norwitz | 673cd82 | 2002-10-18 16:33:13 +0000 | [diff] [blame] | 3181 | if (func == NULL) { |
| 3182 | return NULL; |
| 3183 | } |
| 3184 | } |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 3185 | ival = PyInt_FromLong(i); |
| 3186 | if (ival != NULL) { |
| 3187 | args = PyTuple_New(1); |
| 3188 | if (args != NULL) { |
| 3189 | PyTuple_SET_ITEM(args, 0, ival); |
| 3190 | retval = PyObject_Call(func, args, NULL); |
| 3191 | Py_XDECREF(args); |
| 3192 | Py_XDECREF(func); |
| 3193 | return retval; |
| 3194 | } |
| 3195 | } |
| 3196 | } |
| 3197 | else { |
| 3198 | PyErr_SetObject(PyExc_AttributeError, getitem_str); |
| 3199 | } |
| 3200 | Py_XDECREF(args); |
| 3201 | Py_XDECREF(ival); |
| 3202 | Py_XDECREF(func); |
| 3203 | return NULL; |
| 3204 | } |
| 3205 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3206 | SLOT2(slot_sq_slice, "__getslice__", int, int, "ii") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3207 | |
| 3208 | static int |
| 3209 | slot_sq_ass_item(PyObject *self, int index, PyObject *value) |
| 3210 | { |
| 3211 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3212 | static PyObject *delitem_str, *setitem_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3213 | |
| 3214 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3215 | res = call_method(self, "__delitem__", &delitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3216 | "(i)", index); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3217 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3218 | res = call_method(self, "__setitem__", &setitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3219 | "(iO)", index, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3220 | if (res == NULL) |
| 3221 | return -1; |
| 3222 | Py_DECREF(res); |
| 3223 | return 0; |
| 3224 | } |
| 3225 | |
| 3226 | static int |
| 3227 | slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value) |
| 3228 | { |
| 3229 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3230 | static PyObject *delslice_str, *setslice_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3231 | |
| 3232 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3233 | res = call_method(self, "__delslice__", &delslice_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3234 | "(ii)", i, j); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3235 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3236 | res = call_method(self, "__setslice__", &setslice_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3237 | "(iiO)", i, j, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3238 | if (res == NULL) |
| 3239 | return -1; |
| 3240 | Py_DECREF(res); |
| 3241 | return 0; |
| 3242 | } |
| 3243 | |
| 3244 | static int |
| 3245 | slot_sq_contains(PyObject *self, PyObject *value) |
| 3246 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3247 | PyObject *func, *res, *args; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3248 | static PyObject *contains_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3249 | |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3250 | func = lookup_maybe(self, "__contains__", &contains_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3251 | |
| 3252 | if (func != NULL) { |
| 3253 | args = Py_BuildValue("(O)", value); |
| 3254 | if (args == NULL) |
| 3255 | res = NULL; |
| 3256 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3257 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3258 | Py_DECREF(args); |
| 3259 | } |
| 3260 | Py_DECREF(func); |
| 3261 | if (res == NULL) |
| 3262 | return -1; |
| 3263 | return PyObject_IsTrue(res); |
| 3264 | } |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3265 | else if (PyErr_Occurred()) |
| 3266 | return -1; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3267 | else { |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 3268 | return _PySequence_IterSearch(self, value, |
| 3269 | PY_ITERSEARCH_CONTAINS); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3270 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3271 | } |
| 3272 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3273 | SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O") |
| 3274 | SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3275 | |
| 3276 | #define slot_mp_length slot_sq_length |
| 3277 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3278 | SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3279 | |
| 3280 | static int |
| 3281 | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) |
| 3282 | { |
| 3283 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3284 | static PyObject *delitem_str, *setitem_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3285 | |
| 3286 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3287 | res = call_method(self, "__delitem__", &delitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3288 | "(O)", key); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3289 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3290 | res = call_method(self, "__setitem__", &setitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3291 | "(OO)", key, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3292 | if (res == NULL) |
| 3293 | return -1; |
| 3294 | Py_DECREF(res); |
| 3295 | return 0; |
| 3296 | } |
| 3297 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3298 | SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__") |
| 3299 | SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__") |
| 3300 | SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__") |
| 3301 | SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__") |
| 3302 | SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__") |
| 3303 | SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__") |
| 3304 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 3305 | static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3306 | |
| 3307 | SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, |
| 3308 | nb_power, "__pow__", "__rpow__") |
| 3309 | |
| 3310 | static PyObject * |
| 3311 | slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) |
| 3312 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3313 | static PyObject *pow_str; |
| 3314 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3315 | if (modulus == Py_None) |
| 3316 | return slot_nb_power_binary(self, other); |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 3317 | /* Three-arg power doesn't use __rpow__. But ternary_op |
| 3318 | can call this when the second argument's type uses |
| 3319 | slot_nb_power, so check before calling self.__pow__. */ |
| 3320 | if (self->ob_type->tp_as_number != NULL && |
| 3321 | self->ob_type->tp_as_number->nb_power == slot_nb_power) { |
| 3322 | return call_method(self, "__pow__", &pow_str, |
| 3323 | "(OO)", other, modulus); |
| 3324 | } |
| 3325 | Py_INCREF(Py_NotImplemented); |
| 3326 | return Py_NotImplemented; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
| 3329 | SLOT0(slot_nb_negative, "__neg__") |
| 3330 | SLOT0(slot_nb_positive, "__pos__") |
| 3331 | SLOT0(slot_nb_absolute, "__abs__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3332 | |
| 3333 | static int |
| 3334 | slot_nb_nonzero(PyObject *self) |
| 3335 | { |
Guido van Rossum | 84b2bed | 2002-08-16 17:01:09 +0000 | [diff] [blame] | 3336 | PyObject *func, *res, *args; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3337 | static PyObject *nonzero_str, *len_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3338 | |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3339 | func = lookup_maybe(self, "__nonzero__", &nonzero_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3340 | if (func == NULL) { |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3341 | if (PyErr_Occurred()) |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3342 | return -1; |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3343 | func = lookup_maybe(self, "__len__", &len_str); |
| 3344 | if (func == NULL) { |
| 3345 | if (PyErr_Occurred()) |
| 3346 | return -1; |
| 3347 | else |
| 3348 | return 1; |
| 3349 | } |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3350 | } |
Guido van Rossum | 84b2bed | 2002-08-16 17:01:09 +0000 | [diff] [blame] | 3351 | args = res = PyTuple_New(0); |
| 3352 | if (args != NULL) { |
| 3353 | res = PyObject_Call(func, args, NULL); |
| 3354 | Py_DECREF(args); |
| 3355 | } |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3356 | Py_DECREF(func); |
| 3357 | if (res == NULL) |
| 3358 | return -1; |
| 3359 | return PyObject_IsTrue(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3360 | } |
| 3361 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3362 | SLOT0(slot_nb_invert, "__invert__") |
| 3363 | SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") |
| 3364 | SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") |
| 3365 | SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") |
| 3366 | SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") |
| 3367 | SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 3368 | |
| 3369 | static int |
| 3370 | slot_nb_coerce(PyObject **a, PyObject **b) |
| 3371 | { |
| 3372 | static PyObject *coerce_str; |
| 3373 | PyObject *self = *a, *other = *b; |
| 3374 | |
| 3375 | if (self->ob_type->tp_as_number != NULL && |
| 3376 | self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) { |
| 3377 | PyObject *r; |
| 3378 | r = call_maybe( |
| 3379 | self, "__coerce__", &coerce_str, "(O)", other); |
| 3380 | if (r == NULL) |
| 3381 | return -1; |
| 3382 | if (r == Py_NotImplemented) { |
| 3383 | Py_DECREF(r); |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 3384 | } |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3385 | else { |
| 3386 | if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) { |
| 3387 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 3388 | "__coerce__ didn't return a 2-tuple"); |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3389 | Py_DECREF(r); |
| 3390 | return -1; |
| 3391 | } |
| 3392 | *a = PyTuple_GET_ITEM(r, 0); |
| 3393 | Py_INCREF(*a); |
| 3394 | *b = PyTuple_GET_ITEM(r, 1); |
| 3395 | Py_INCREF(*b); |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 3396 | Py_DECREF(r); |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3397 | return 0; |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 3398 | } |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 3399 | } |
| 3400 | if (other->ob_type->tp_as_number != NULL && |
| 3401 | other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) { |
| 3402 | PyObject *r; |
| 3403 | r = call_maybe( |
| 3404 | other, "__coerce__", &coerce_str, "(O)", self); |
| 3405 | if (r == NULL) |
| 3406 | return -1; |
| 3407 | if (r == Py_NotImplemented) { |
| 3408 | Py_DECREF(r); |
| 3409 | return 1; |
| 3410 | } |
| 3411 | if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) { |
| 3412 | PyErr_SetString(PyExc_TypeError, |
| 3413 | "__coerce__ didn't return a 2-tuple"); |
| 3414 | Py_DECREF(r); |
| 3415 | return -1; |
| 3416 | } |
| 3417 | *a = PyTuple_GET_ITEM(r, 1); |
| 3418 | Py_INCREF(*a); |
| 3419 | *b = PyTuple_GET_ITEM(r, 0); |
| 3420 | Py_INCREF(*b); |
| 3421 | Py_DECREF(r); |
| 3422 | return 0; |
| 3423 | } |
| 3424 | return 1; |
| 3425 | } |
| 3426 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3427 | SLOT0(slot_nb_int, "__int__") |
| 3428 | SLOT0(slot_nb_long, "__long__") |
| 3429 | SLOT0(slot_nb_float, "__float__") |
| 3430 | SLOT0(slot_nb_oct, "__oct__") |
| 3431 | SLOT0(slot_nb_hex, "__hex__") |
| 3432 | SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O") |
| 3433 | SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O") |
| 3434 | SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O") |
| 3435 | SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O") |
| 3436 | SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O") |
Guido van Rossum | 6e5680f | 2002-10-15 01:01:53 +0000 | [diff] [blame] | 3437 | SLOT1(slot_nb_inplace_power, "__ipow__", PyObject *, "O") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3438 | SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O") |
| 3439 | SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O") |
| 3440 | SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O") |
| 3441 | SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O") |
| 3442 | SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O") |
| 3443 | SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, |
| 3444 | "__floordiv__", "__rfloordiv__") |
| 3445 | SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__") |
| 3446 | SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O") |
| 3447 | SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3448 | |
| 3449 | static int |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3450 | half_compare(PyObject *self, PyObject *other) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3451 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3452 | PyObject *func, *args, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3453 | static PyObject *cmp_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3454 | int c; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3455 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3456 | func = lookup_method(self, "__cmp__", &cmp_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3457 | if (func == NULL) { |
| 3458 | PyErr_Clear(); |
| 3459 | } |
| 3460 | else { |
| 3461 | args = Py_BuildValue("(O)", other); |
| 3462 | if (args == NULL) |
| 3463 | res = NULL; |
| 3464 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3465 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3466 | Py_DECREF(args); |
| 3467 | } |
Raymond Hettinger | ab5dae3 | 2002-06-24 13:08:16 +0000 | [diff] [blame] | 3468 | Py_DECREF(func); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3469 | if (res != Py_NotImplemented) { |
| 3470 | if (res == NULL) |
| 3471 | return -2; |
| 3472 | c = PyInt_AsLong(res); |
| 3473 | Py_DECREF(res); |
| 3474 | if (c == -1 && PyErr_Occurred()) |
| 3475 | return -2; |
| 3476 | return (c < 0) ? -1 : (c > 0) ? 1 : 0; |
| 3477 | } |
| 3478 | Py_DECREF(res); |
| 3479 | } |
| 3480 | return 2; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3481 | } |
| 3482 | |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 3483 | /* This slot is published for the benefit of try_3way_compare in object.c */ |
| 3484 | int |
| 3485 | _PyObject_SlotCompare(PyObject *self, PyObject *other) |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3486 | { |
| 3487 | int c; |
| 3488 | |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 3489 | if (self->ob_type->tp_compare == _PyObject_SlotCompare) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3490 | c = half_compare(self, other); |
| 3491 | if (c <= 1) |
| 3492 | return c; |
| 3493 | } |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 3494 | if (other->ob_type->tp_compare == _PyObject_SlotCompare) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3495 | c = half_compare(other, self); |
| 3496 | if (c < -1) |
| 3497 | return -2; |
| 3498 | if (c <= 1) |
| 3499 | return -c; |
| 3500 | } |
| 3501 | return (void *)self < (void *)other ? -1 : |
| 3502 | (void *)self > (void *)other ? 1 : 0; |
| 3503 | } |
| 3504 | |
| 3505 | static PyObject * |
| 3506 | slot_tp_repr(PyObject *self) |
| 3507 | { |
| 3508 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3509 | static PyObject *repr_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3510 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3511 | func = lookup_method(self, "__repr__", &repr_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3512 | if (func != NULL) { |
| 3513 | res = PyEval_CallObject(func, NULL); |
| 3514 | Py_DECREF(func); |
| 3515 | return res; |
| 3516 | } |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 3517 | PyErr_Clear(); |
| 3518 | return PyString_FromFormat("<%s object at %p>", |
| 3519 | self->ob_type->tp_name, self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3520 | } |
| 3521 | |
| 3522 | static PyObject * |
| 3523 | slot_tp_str(PyObject *self) |
| 3524 | { |
| 3525 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3526 | static PyObject *str_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3527 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3528 | func = lookup_method(self, "__str__", &str_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3529 | if (func != NULL) { |
| 3530 | res = PyEval_CallObject(func, NULL); |
| 3531 | Py_DECREF(func); |
| 3532 | return res; |
| 3533 | } |
| 3534 | else { |
| 3535 | PyErr_Clear(); |
| 3536 | return slot_tp_repr(self); |
| 3537 | } |
| 3538 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3539 | |
| 3540 | static long |
| 3541 | slot_tp_hash(PyObject *self) |
| 3542 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3543 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3544 | static PyObject *hash_str, *eq_str, *cmp_str; |
| 3545 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3546 | long h; |
| 3547 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3548 | func = lookup_method(self, "__hash__", &hash_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3549 | |
| 3550 | if (func != NULL) { |
| 3551 | res = PyEval_CallObject(func, NULL); |
| 3552 | Py_DECREF(func); |
| 3553 | if (res == NULL) |
| 3554 | return -1; |
| 3555 | h = PyInt_AsLong(res); |
| 3556 | } |
| 3557 | else { |
| 3558 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3559 | func = lookup_method(self, "__eq__", &eq_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3560 | if (func == NULL) { |
| 3561 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3562 | func = lookup_method(self, "__cmp__", &cmp_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3563 | } |
| 3564 | if (func != NULL) { |
| 3565 | Py_DECREF(func); |
| 3566 | PyErr_SetString(PyExc_TypeError, "unhashable type"); |
| 3567 | return -1; |
| 3568 | } |
| 3569 | PyErr_Clear(); |
| 3570 | h = _Py_HashPointer((void *)self); |
| 3571 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3572 | if (h == -1 && !PyErr_Occurred()) |
| 3573 | h = -2; |
| 3574 | return h; |
| 3575 | } |
| 3576 | |
| 3577 | static PyObject * |
| 3578 | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 3579 | { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3580 | static PyObject *call_str; |
| 3581 | PyObject *meth = lookup_method(self, "__call__", &call_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3582 | PyObject *res; |
| 3583 | |
| 3584 | if (meth == NULL) |
| 3585 | return NULL; |
| 3586 | res = PyObject_Call(meth, args, kwds); |
| 3587 | Py_DECREF(meth); |
| 3588 | return res; |
| 3589 | } |
| 3590 | |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 3591 | /* There are two slot dispatch functions for tp_getattro. |
| 3592 | |
| 3593 | - slot_tp_getattro() is used when __getattribute__ is overridden |
| 3594 | but no __getattr__ hook is present; |
| 3595 | |
| 3596 | - slot_tp_getattr_hook() is used when a __getattr__ hook is present. |
| 3597 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 3598 | The code in update_one_slot() always installs slot_tp_getattr_hook(); this |
| 3599 | detects the absence of __getattr__ and then installs the simpler slot if |
| 3600 | necessary. */ |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 3601 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3602 | static PyObject * |
| 3603 | slot_tp_getattro(PyObject *self, PyObject *name) |
| 3604 | { |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 3605 | static PyObject *getattribute_str = NULL; |
| 3606 | return call_method(self, "__getattribute__", &getattribute_str, |
| 3607 | "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3608 | } |
| 3609 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 3610 | static PyObject * |
| 3611 | slot_tp_getattr_hook(PyObject *self, PyObject *name) |
| 3612 | { |
| 3613 | PyTypeObject *tp = self->ob_type; |
| 3614 | PyObject *getattr, *getattribute, *res; |
| 3615 | static PyObject *getattribute_str = NULL; |
| 3616 | static PyObject *getattr_str = NULL; |
| 3617 | |
| 3618 | if (getattr_str == NULL) { |
| 3619 | getattr_str = PyString_InternFromString("__getattr__"); |
| 3620 | if (getattr_str == NULL) |
| 3621 | return NULL; |
| 3622 | } |
| 3623 | if (getattribute_str == NULL) { |
| 3624 | getattribute_str = |
| 3625 | PyString_InternFromString("__getattribute__"); |
| 3626 | if (getattribute_str == NULL) |
| 3627 | return NULL; |
| 3628 | } |
| 3629 | getattr = _PyType_Lookup(tp, getattr_str); |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 3630 | if (getattr == NULL) { |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 3631 | /* No __getattr__ hook: use a simpler dispatcher */ |
| 3632 | tp->tp_getattro = slot_tp_getattro; |
| 3633 | return slot_tp_getattro(self, name); |
| 3634 | } |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 3635 | getattribute = _PyType_Lookup(tp, getattribute_str); |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 3636 | if (getattribute == NULL || |
| 3637 | (getattribute->ob_type == &PyWrapperDescr_Type && |
| 3638 | ((PyWrapperDescrObject *)getattribute)->d_wrapped == |
| 3639 | (void *)PyObject_GenericGetAttr)) |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 3640 | res = PyObject_GenericGetAttr(self, name); |
| 3641 | else |
| 3642 | res = PyObject_CallFunction(getattribute, "OO", self, name); |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 3643 | if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 3644 | PyErr_Clear(); |
| 3645 | res = PyObject_CallFunction(getattr, "OO", self, name); |
| 3646 | } |
| 3647 | return res; |
| 3648 | } |
| 3649 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3650 | static int |
| 3651 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) |
| 3652 | { |
| 3653 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3654 | static PyObject *delattr_str, *setattr_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3655 | |
| 3656 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3657 | res = call_method(self, "__delattr__", &delattr_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3658 | "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3659 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3660 | res = call_method(self, "__setattr__", &setattr_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3661 | "(OO)", name, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3662 | if (res == NULL) |
| 3663 | return -1; |
| 3664 | Py_DECREF(res); |
| 3665 | return 0; |
| 3666 | } |
| 3667 | |
| 3668 | /* Map rich comparison operators to their __xx__ namesakes */ |
| 3669 | static char *name_op[] = { |
| 3670 | "__lt__", |
| 3671 | "__le__", |
| 3672 | "__eq__", |
| 3673 | "__ne__", |
| 3674 | "__gt__", |
| 3675 | "__ge__", |
| 3676 | }; |
| 3677 | |
| 3678 | static PyObject * |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3679 | half_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3680 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3681 | PyObject *func, *args, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3682 | static PyObject *op_str[6]; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3683 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3684 | func = lookup_method(self, name_op[op], &op_str[op]); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3685 | if (func == NULL) { |
| 3686 | PyErr_Clear(); |
| 3687 | Py_INCREF(Py_NotImplemented); |
| 3688 | return Py_NotImplemented; |
| 3689 | } |
| 3690 | args = Py_BuildValue("(O)", other); |
| 3691 | if (args == NULL) |
| 3692 | res = NULL; |
| 3693 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3694 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3695 | Py_DECREF(args); |
| 3696 | } |
| 3697 | Py_DECREF(func); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3698 | return res; |
| 3699 | } |
| 3700 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3701 | /* Map rich comparison operators to their swapped version, e.g. LT --> GT */ |
| 3702 | static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE}; |
| 3703 | |
| 3704 | static PyObject * |
| 3705 | slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
| 3706 | { |
| 3707 | PyObject *res; |
| 3708 | |
| 3709 | if (self->ob_type->tp_richcompare == slot_tp_richcompare) { |
| 3710 | res = half_richcompare(self, other, op); |
| 3711 | if (res != Py_NotImplemented) |
| 3712 | return res; |
| 3713 | Py_DECREF(res); |
| 3714 | } |
| 3715 | if (other->ob_type->tp_richcompare == slot_tp_richcompare) { |
| 3716 | res = half_richcompare(other, self, swapped_op[op]); |
| 3717 | if (res != Py_NotImplemented) { |
| 3718 | return res; |
| 3719 | } |
| 3720 | Py_DECREF(res); |
| 3721 | } |
| 3722 | Py_INCREF(Py_NotImplemented); |
| 3723 | return Py_NotImplemented; |
| 3724 | } |
| 3725 | |
| 3726 | static PyObject * |
| 3727 | slot_tp_iter(PyObject *self) |
| 3728 | { |
| 3729 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3730 | static PyObject *iter_str, *getitem_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3731 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3732 | func = lookup_method(self, "__iter__", &iter_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3733 | if (func != NULL) { |
Guido van Rossum | 84b2bed | 2002-08-16 17:01:09 +0000 | [diff] [blame] | 3734 | PyObject *args; |
| 3735 | args = res = PyTuple_New(0); |
| 3736 | if (args != NULL) { |
| 3737 | res = PyObject_Call(func, args, NULL); |
| 3738 | Py_DECREF(args); |
| 3739 | } |
| 3740 | Py_DECREF(func); |
| 3741 | return res; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3742 | } |
| 3743 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3744 | func = lookup_method(self, "__getitem__", &getitem_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3745 | if (func == NULL) { |
Guido van Rossum | d464107 | 2002-04-03 02:13:37 +0000 | [diff] [blame] | 3746 | PyErr_SetString(PyExc_TypeError, |
| 3747 | "iteration over non-sequence"); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3748 | return NULL; |
| 3749 | } |
| 3750 | Py_DECREF(func); |
| 3751 | return PySeqIter_New(self); |
| 3752 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3753 | |
| 3754 | static PyObject * |
| 3755 | slot_tp_iternext(PyObject *self) |
| 3756 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3757 | static PyObject *next_str; |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3758 | return call_method(self, "next", &next_str, "()"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3759 | } |
| 3760 | |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 3761 | static PyObject * |
| 3762 | slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 3763 | { |
| 3764 | PyTypeObject *tp = self->ob_type; |
| 3765 | PyObject *get; |
| 3766 | static PyObject *get_str = NULL; |
| 3767 | |
| 3768 | if (get_str == NULL) { |
| 3769 | get_str = PyString_InternFromString("__get__"); |
| 3770 | if (get_str == NULL) |
| 3771 | return NULL; |
| 3772 | } |
| 3773 | get = _PyType_Lookup(tp, get_str); |
| 3774 | if (get == NULL) { |
| 3775 | /* Avoid further slowdowns */ |
| 3776 | if (tp->tp_descr_get == slot_tp_descr_get) |
| 3777 | tp->tp_descr_get = NULL; |
| 3778 | Py_INCREF(self); |
| 3779 | return self; |
| 3780 | } |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 3781 | if (obj == NULL) |
| 3782 | obj = Py_None; |
| 3783 | if (type == NULL) |
| 3784 | type = Py_None; |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 3785 | return PyObject_CallFunction(get, "OOO", self, obj, type); |
| 3786 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3787 | |
| 3788 | static int |
| 3789 | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) |
| 3790 | { |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 3791 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3792 | static PyObject *del_str, *set_str; |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 3793 | |
| 3794 | if (value == NULL) |
Guido van Rossum | 1d5b3f2 | 2001-12-03 00:08:33 +0000 | [diff] [blame] | 3795 | res = call_method(self, "__delete__", &del_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3796 | "(O)", target); |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 3797 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3798 | res = call_method(self, "__set__", &set_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3799 | "(OO)", target, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3800 | if (res == NULL) |
| 3801 | return -1; |
| 3802 | Py_DECREF(res); |
| 3803 | return 0; |
| 3804 | } |
| 3805 | |
| 3806 | static int |
| 3807 | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 3808 | { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3809 | static PyObject *init_str; |
| 3810 | PyObject *meth = lookup_method(self, "__init__", &init_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3811 | PyObject *res; |
| 3812 | |
| 3813 | if (meth == NULL) |
| 3814 | return -1; |
| 3815 | res = PyObject_Call(meth, args, kwds); |
| 3816 | Py_DECREF(meth); |
| 3817 | if (res == NULL) |
| 3818 | return -1; |
| 3819 | Py_DECREF(res); |
| 3820 | return 0; |
| 3821 | } |
| 3822 | |
| 3823 | static PyObject * |
| 3824 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3825 | { |
Guido van Rossum | 7bed213 | 2002-08-08 21:57:53 +0000 | [diff] [blame] | 3826 | static PyObject *new_str; |
| 3827 | PyObject *func; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3828 | PyObject *newargs, *x; |
| 3829 | int i, n; |
| 3830 | |
Guido van Rossum | 7bed213 | 2002-08-08 21:57:53 +0000 | [diff] [blame] | 3831 | if (new_str == NULL) { |
| 3832 | new_str = PyString_InternFromString("__new__"); |
| 3833 | if (new_str == NULL) |
| 3834 | return NULL; |
| 3835 | } |
| 3836 | func = PyObject_GetAttr((PyObject *)type, new_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3837 | if (func == NULL) |
| 3838 | return NULL; |
| 3839 | assert(PyTuple_Check(args)); |
| 3840 | n = PyTuple_GET_SIZE(args); |
| 3841 | newargs = PyTuple_New(n+1); |
| 3842 | if (newargs == NULL) |
| 3843 | return NULL; |
| 3844 | Py_INCREF(type); |
| 3845 | PyTuple_SET_ITEM(newargs, 0, (PyObject *)type); |
| 3846 | for (i = 0; i < n; i++) { |
| 3847 | x = PyTuple_GET_ITEM(args, i); |
| 3848 | Py_INCREF(x); |
| 3849 | PyTuple_SET_ITEM(newargs, i+1, x); |
| 3850 | } |
| 3851 | x = PyObject_Call(func, newargs, kwds); |
Guido van Rossum | 25d1807 | 2001-10-01 15:55:28 +0000 | [diff] [blame] | 3852 | Py_DECREF(newargs); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3853 | Py_DECREF(func); |
| 3854 | return x; |
| 3855 | } |
| 3856 | |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 3857 | static void |
| 3858 | slot_tp_del(PyObject *self) |
| 3859 | { |
| 3860 | static PyObject *del_str = NULL; |
| 3861 | PyObject *del, *res; |
| 3862 | PyObject *error_type, *error_value, *error_traceback; |
| 3863 | |
| 3864 | /* Temporarily resurrect the object. */ |
| 3865 | assert(self->ob_refcnt == 0); |
| 3866 | self->ob_refcnt = 1; |
| 3867 | |
| 3868 | /* Save the current exception, if any. */ |
| 3869 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 3870 | |
| 3871 | /* Execute __del__ method, if any. */ |
| 3872 | del = lookup_maybe(self, "__del__", &del_str); |
| 3873 | if (del != NULL) { |
| 3874 | res = PyEval_CallObject(del, NULL); |
| 3875 | if (res == NULL) |
| 3876 | PyErr_WriteUnraisable(del); |
| 3877 | else |
| 3878 | Py_DECREF(res); |
| 3879 | Py_DECREF(del); |
| 3880 | } |
| 3881 | |
| 3882 | /* Restore the saved exception. */ |
| 3883 | PyErr_Restore(error_type, error_value, error_traceback); |
| 3884 | |
| 3885 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 3886 | * cause a recursive call. |
| 3887 | */ |
| 3888 | assert(self->ob_refcnt > 0); |
| 3889 | if (--self->ob_refcnt == 0) |
| 3890 | return; /* this is the normal path out */ |
| 3891 | |
| 3892 | /* __del__ resurrected it! Make it look like the original Py_DECREF |
| 3893 | * never happened. |
| 3894 | */ |
| 3895 | { |
| 3896 | int refcnt = self->ob_refcnt; |
| 3897 | _Py_NewReference(self); |
| 3898 | self->ob_refcnt = refcnt; |
| 3899 | } |
| 3900 | assert(!PyType_IS_GC(self->ob_type) || |
| 3901 | _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED); |
| 3902 | /* If Py_REF_DEBUG, the original decref dropped _Py_RefTotal, but |
| 3903 | * _Py_NewReference bumped it again, so that's a wash. |
| 3904 | * If Py_TRACE_REFS, _Py_NewReference re-added self to the object |
| 3905 | * chain, so no more to do there either. |
| 3906 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
| 3907 | * _Py_NewReference bumped tp_allocs: both of those need to be |
| 3908 | * undone. |
| 3909 | */ |
| 3910 | #ifdef COUNT_ALLOCS |
| 3911 | --self->ob_type->tp_frees; |
| 3912 | --self->ob_type->tp_allocs; |
| 3913 | #endif |
| 3914 | } |
| 3915 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3916 | |
| 3917 | /* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper |
| 3918 | functions. The offsets here are relative to the 'etype' structure, which |
| 3919 | incorporates the additional structures used for numbers, sequences and |
| 3920 | mappings. Note that multiple names may map to the same slot (e.g. __eq__, |
| 3921 | __ne__ etc. all map to tp_richcompare) and one name may map to multiple |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 3922 | slots (e.g. __str__ affects tp_str as well as tp_repr). The table is |
| 3923 | terminated with an all-zero entry. (This table is further initialized and |
| 3924 | sorted in init_slotdefs() below.) */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3925 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3926 | typedef struct wrapperbase slotdef; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3927 | |
| 3928 | #undef TPSLOT |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 3929 | #undef FLSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3930 | #undef ETSLOT |
| 3931 | #undef SQSLOT |
| 3932 | #undef MPSLOT |
| 3933 | #undef NBSLOT |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3934 | #undef UNSLOT |
| 3935 | #undef IBSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3936 | #undef BINSLOT |
| 3937 | #undef RBINSLOT |
| 3938 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3939 | #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Neal Norwitz | d47714a | 2002-08-13 19:01:38 +0000 | [diff] [blame] | 3940 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 3941 | PyDoc_STR(DOC)} |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 3942 | #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ |
| 3943 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
Neal Norwitz | d47714a | 2002-08-13 19:01:38 +0000 | [diff] [blame] | 3944 | PyDoc_STR(DOC), FLAGS} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3945 | #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Neal Norwitz | d47714a | 2002-08-13 19:01:38 +0000 | [diff] [blame] | 3946 | {NAME, offsetof(etype, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 3947 | PyDoc_STR(DOC)} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3948 | #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 3949 | ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) |
| 3950 | #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 3951 | ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) |
| 3952 | #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 3953 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) |
| 3954 | #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 3955 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 3956 | "x." NAME "() <==> " DOC) |
| 3957 | #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 3958 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 3959 | "x." NAME "(y) <==> x" DOC "y") |
| 3960 | #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
| 3961 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 3962 | "x." NAME "(y) <==> x" DOC "y") |
| 3963 | #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
| 3964 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 3965 | "x." NAME "(y) <==> y" DOC "x") |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3966 | |
| 3967 | static slotdef slotdefs[] = { |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3968 | SQSLOT("__len__", sq_length, slot_sq_length, wrap_inquiry, |
| 3969 | "x.__len__() <==> len(x)"), |
| 3970 | SQSLOT("__add__", sq_concat, slot_sq_concat, wrap_binaryfunc, |
| 3971 | "x.__add__(y) <==> x+y"), |
| 3972 | SQSLOT("__mul__", sq_repeat, slot_sq_repeat, wrap_intargfunc, |
| 3973 | "x.__mul__(n) <==> x*n"), |
| 3974 | SQSLOT("__rmul__", sq_repeat, slot_sq_repeat, wrap_intargfunc, |
| 3975 | "x.__rmul__(n) <==> n*x"), |
| 3976 | SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, |
| 3977 | "x.__getitem__(y) <==> x[y]"), |
| 3978 | SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_intintargfunc, |
| 3979 | "x.__getslice__(i, j) <==> x[i:j]"), |
| 3980 | SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, |
| 3981 | "x.__setitem__(i, y) <==> x[i]=y"), |
| 3982 | SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, |
| 3983 | "x.__delitem__(y) <==> del x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3984 | SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3985 | wrap_intintobjargproc, |
| 3986 | "x.__setslice__(i, j, y) <==> x[i:j]=y"), |
| 3987 | SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice, |
| 3988 | "x.__delslice__(i, j) <==> del x[i:j]"), |
| 3989 | SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, |
| 3990 | "x.__contains__(y) <==> y in x"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3991 | SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3992 | wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3993 | SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3994 | wrap_intargfunc, "x.__imul__(y) <==> x*=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3995 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3996 | MPSLOT("__len__", mp_length, slot_mp_length, wrap_inquiry, |
| 3997 | "x.__len__() <==> len(x)"), |
Guido van Rossum | fd38f8e | 2001-10-09 20:17:57 +0000 | [diff] [blame] | 3998 | MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3999 | wrap_binaryfunc, |
| 4000 | "x.__getitem__(y) <==> x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4001 | MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4002 | wrap_objobjargproc, |
| 4003 | "x.__setitem__(i, y) <==> x[i]=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4004 | MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4005 | wrap_delitem, |
| 4006 | "x.__delitem__(y) <==> del x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4007 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4008 | BINSLOT("__add__", nb_add, slot_nb_add, |
| 4009 | "+"), |
| 4010 | RBINSLOT("__radd__", nb_add, slot_nb_add, |
| 4011 | "+"), |
| 4012 | BINSLOT("__sub__", nb_subtract, slot_nb_subtract, |
| 4013 | "-"), |
| 4014 | RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract, |
| 4015 | "-"), |
| 4016 | BINSLOT("__mul__", nb_multiply, slot_nb_multiply, |
| 4017 | "*"), |
| 4018 | RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply, |
| 4019 | "*"), |
| 4020 | BINSLOT("__div__", nb_divide, slot_nb_divide, |
| 4021 | "/"), |
| 4022 | RBINSLOT("__rdiv__", nb_divide, slot_nb_divide, |
| 4023 | "/"), |
| 4024 | BINSLOT("__mod__", nb_remainder, slot_nb_remainder, |
| 4025 | "%"), |
| 4026 | RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder, |
| 4027 | "%"), |
| 4028 | BINSLOT("__divmod__", nb_divmod, slot_nb_divmod, |
| 4029 | "divmod(x, y)"), |
| 4030 | RBINSLOT("__rdivmod__", nb_divmod, slot_nb_divmod, |
| 4031 | "divmod(y, x)"), |
| 4032 | NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, |
| 4033 | "x.__pow__(y[, z]) <==> pow(x, y[, z])"), |
| 4034 | NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, |
| 4035 | "y.__rpow__(x[, z]) <==> pow(x, y[, z])"), |
| 4036 | UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"), |
| 4037 | UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"), |
| 4038 | UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, |
| 4039 | "abs(x)"), |
Guido van Rossum | dfce3bf | 2002-03-10 14:11:16 +0000 | [diff] [blame] | 4040 | UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquiry, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4041 | "x != 0"), |
| 4042 | UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"), |
| 4043 | BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 4044 | RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 4045 | BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 4046 | RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 4047 | BINSLOT("__and__", nb_and, slot_nb_and, "&"), |
| 4048 | RBINSLOT("__rand__", nb_and, slot_nb_and, "&"), |
| 4049 | BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"), |
| 4050 | RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), |
| 4051 | BINSLOT("__or__", nb_or, slot_nb_or, "|"), |
| 4052 | RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), |
| 4053 | NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc, |
| 4054 | "x.__coerce__(y) <==> coerce(x, y)"), |
| 4055 | UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, |
| 4056 | "int(x)"), |
| 4057 | UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc, |
| 4058 | "long(x)"), |
| 4059 | UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, |
| 4060 | "float(x)"), |
| 4061 | UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc, |
| 4062 | "oct(x)"), |
| 4063 | UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc, |
| 4064 | "hex(x)"), |
| 4065 | IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, |
| 4066 | wrap_binaryfunc, "+"), |
| 4067 | IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, |
| 4068 | wrap_binaryfunc, "-"), |
| 4069 | IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply, |
| 4070 | wrap_binaryfunc, "*"), |
| 4071 | IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide, |
| 4072 | wrap_binaryfunc, "/"), |
| 4073 | IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder, |
| 4074 | wrap_binaryfunc, "%"), |
| 4075 | IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power, |
Guido van Rossum | 6e5680f | 2002-10-15 01:01:53 +0000 | [diff] [blame] | 4076 | wrap_binaryfunc, "**"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4077 | IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift, |
| 4078 | wrap_binaryfunc, "<<"), |
| 4079 | IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift, |
| 4080 | wrap_binaryfunc, ">>"), |
| 4081 | IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and, |
| 4082 | wrap_binaryfunc, "&"), |
| 4083 | IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor, |
| 4084 | wrap_binaryfunc, "^"), |
| 4085 | IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or, |
| 4086 | wrap_binaryfunc, "|"), |
| 4087 | BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 4088 | RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 4089 | BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 4090 | RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 4091 | IBSLOT("__ifloordiv__", nb_inplace_floor_divide, |
| 4092 | slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"), |
| 4093 | IBSLOT("__itruediv__", nb_inplace_true_divide, |
| 4094 | slot_nb_inplace_true_divide, wrap_binaryfunc, "/"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4095 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4096 | TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, |
| 4097 | "x.__str__() <==> str(x)"), |
Guido van Rossum | afe7a94 | 2001-10-29 14:33:44 +0000 | [diff] [blame] | 4098 | TPSLOT("__str__", tp_print, NULL, NULL, ""), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4099 | TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, |
| 4100 | "x.__repr__() <==> repr(x)"), |
Guido van Rossum | afe7a94 | 2001-10-29 14:33:44 +0000 | [diff] [blame] | 4101 | TPSLOT("__repr__", tp_print, NULL, NULL, ""), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4102 | TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc, |
| 4103 | "x.__cmp__(y) <==> cmp(x,y)"), |
| 4104 | TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, |
| 4105 | "x.__hash__() <==> hash(x)"), |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4106 | FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, |
| 4107 | "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS), |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 4108 | TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4109 | wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"), |
| 4110 | TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), |
| 4111 | TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), |
| 4112 | TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), |
| 4113 | TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, |
| 4114 | "x.__setattr__('name', value) <==> x.name = value"), |
| 4115 | TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), |
| 4116 | TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, |
| 4117 | "x.__delattr__('name') <==> del x.name"), |
| 4118 | TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), |
| 4119 | TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, |
| 4120 | "x.__lt__(y) <==> x<y"), |
| 4121 | TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, |
| 4122 | "x.__le__(y) <==> x<=y"), |
| 4123 | TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, |
| 4124 | "x.__eq__(y) <==> x==y"), |
| 4125 | TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, |
| 4126 | "x.__ne__(y) <==> x!=y"), |
| 4127 | TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, |
| 4128 | "x.__gt__(y) <==> x>y"), |
| 4129 | TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, |
| 4130 | "x.__ge__(y) <==> x>=y"), |
| 4131 | TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, |
| 4132 | "x.__iter__() <==> iter(x)"), |
| 4133 | TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next, |
| 4134 | "x.next() -> the next value, or raise StopIteration"), |
| 4135 | TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, |
| 4136 | "descr.__get__(obj[, type]) -> value"), |
| 4137 | TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, |
| 4138 | "descr.__set__(obj, value)"), |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4139 | TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set, |
| 4140 | wrap_descr_delete, "descr.__delete__(obj)"), |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4141 | FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4142 | "x.__init__(...) initializes x; " |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4143 | "see x.__class__.__doc__ for signature", |
| 4144 | PyWrapperFlag_KEYWORDS), |
| 4145 | TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""), |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 4146 | TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4147 | {NULL} |
| 4148 | }; |
| 4149 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4150 | /* Given a type pointer and an offset gotten from a slotdef entry, return a |
| 4151 | pointer to the actual slot. This is not quite the same as simply adding |
| 4152 | the offset to the type pointer, since it takes care to indirect through the |
| 4153 | proper indirection pointer (as_buffer, etc.); it returns NULL if the |
| 4154 | indirection pointer is NULL. */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4155 | static void ** |
| 4156 | slotptr(PyTypeObject *type, int offset) |
| 4157 | { |
| 4158 | char *ptr; |
| 4159 | |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 4160 | /* Note: this depends on the order of the members of etype! */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4161 | assert(offset >= 0); |
| 4162 | assert(offset < offsetof(etype, as_buffer)); |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 4163 | if (offset >= offsetof(etype, as_sequence)) { |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4164 | ptr = (void *)type->tp_as_sequence; |
| 4165 | offset -= offsetof(etype, as_sequence); |
| 4166 | } |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 4167 | else if (offset >= offsetof(etype, as_mapping)) { |
| 4168 | ptr = (void *)type->tp_as_mapping; |
| 4169 | offset -= offsetof(etype, as_mapping); |
| 4170 | } |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4171 | else if (offset >= offsetof(etype, as_number)) { |
| 4172 | ptr = (void *)type->tp_as_number; |
| 4173 | offset -= offsetof(etype, as_number); |
| 4174 | } |
| 4175 | else { |
| 4176 | ptr = (void *)type; |
| 4177 | } |
| 4178 | if (ptr != NULL) |
| 4179 | ptr += offset; |
| 4180 | return (void **)ptr; |
| 4181 | } |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4182 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4183 | /* Length of array of slotdef pointers used to store slots with the |
| 4184 | same __name__. There should be at most MAX_EQUIV-1 slotdef entries with |
| 4185 | the same __name__, for any __name__. Since that's a static property, it is |
| 4186 | appropriate to declare fixed-size arrays for this. */ |
| 4187 | #define MAX_EQUIV 10 |
| 4188 | |
| 4189 | /* Return a slot pointer for a given name, but ONLY if the attribute has |
| 4190 | exactly one slot function. The name must be an interned string. */ |
| 4191 | static void ** |
| 4192 | resolve_slotdups(PyTypeObject *type, PyObject *name) |
| 4193 | { |
| 4194 | /* XXX Maybe this could be optimized more -- but is it worth it? */ |
| 4195 | |
| 4196 | /* pname and ptrs act as a little cache */ |
| 4197 | static PyObject *pname; |
| 4198 | static slotdef *ptrs[MAX_EQUIV]; |
| 4199 | slotdef *p, **pp; |
| 4200 | void **res, **ptr; |
| 4201 | |
| 4202 | if (pname != name) { |
| 4203 | /* Collect all slotdefs that match name into ptrs. */ |
| 4204 | pname = name; |
| 4205 | pp = ptrs; |
| 4206 | for (p = slotdefs; p->name_strobj; p++) { |
| 4207 | if (p->name_strobj == name) |
| 4208 | *pp++ = p; |
| 4209 | } |
| 4210 | *pp = NULL; |
| 4211 | } |
| 4212 | |
| 4213 | /* Look in all matching slots of the type; if exactly one of these has |
| 4214 | a filled-in slot, return its value. Otherwise return NULL. */ |
| 4215 | res = NULL; |
| 4216 | for (pp = ptrs; *pp; pp++) { |
| 4217 | ptr = slotptr(type, (*pp)->offset); |
| 4218 | if (ptr == NULL || *ptr == NULL) |
| 4219 | continue; |
| 4220 | if (res != NULL) |
| 4221 | return NULL; |
| 4222 | res = ptr; |
| 4223 | } |
| 4224 | return res; |
| 4225 | } |
| 4226 | |
| 4227 | /* Common code for update_these_slots() and fixup_slot_dispatchers(). This |
| 4228 | does some incredibly complex thinking and then sticks something into the |
| 4229 | slot. (It sees if the adjacent slotdefs for the same slot have conflicting |
| 4230 | interests, and then stores a generic wrapper or a specific function into |
| 4231 | the slot.) Return a pointer to the next slotdef with a different offset, |
| 4232 | because that's convenient for fixup_slot_dispatchers(). */ |
| 4233 | static slotdef * |
| 4234 | update_one_slot(PyTypeObject *type, slotdef *p) |
| 4235 | { |
| 4236 | PyObject *descr; |
| 4237 | PyWrapperDescrObject *d; |
| 4238 | void *generic = NULL, *specific = NULL; |
| 4239 | int use_generic = 0; |
| 4240 | int offset = p->offset; |
| 4241 | void **ptr = slotptr(type, offset); |
| 4242 | |
| 4243 | if (ptr == NULL) { |
| 4244 | do { |
| 4245 | ++p; |
| 4246 | } while (p->offset == offset); |
| 4247 | return p; |
| 4248 | } |
| 4249 | do { |
| 4250 | descr = _PyType_Lookup(type, p->name_strobj); |
| 4251 | if (descr == NULL) |
| 4252 | continue; |
| 4253 | if (descr->ob_type == &PyWrapperDescr_Type) { |
| 4254 | void **tptr = resolve_slotdups(type, p->name_strobj); |
| 4255 | if (tptr == NULL || tptr == ptr) |
| 4256 | generic = p->function; |
| 4257 | d = (PyWrapperDescrObject *)descr; |
| 4258 | if (d->d_base->wrapper == p->wrapper && |
| 4259 | PyType_IsSubtype(type, d->d_type)) |
| 4260 | { |
| 4261 | if (specific == NULL || |
| 4262 | specific == d->d_wrapped) |
| 4263 | specific = d->d_wrapped; |
| 4264 | else |
| 4265 | use_generic = 1; |
| 4266 | } |
| 4267 | } |
Guido van Rossum | 721f62e | 2002-08-09 02:14:34 +0000 | [diff] [blame] | 4268 | else if (descr->ob_type == &PyCFunction_Type && |
| 4269 | PyCFunction_GET_FUNCTION(descr) == |
| 4270 | (PyCFunction)tp_new_wrapper && |
| 4271 | strcmp(p->name, "__new__") == 0) |
| 4272 | { |
| 4273 | /* The __new__ wrapper is not a wrapper descriptor, |
| 4274 | so must be special-cased differently. |
| 4275 | If we don't do this, creating an instance will |
| 4276 | always use slot_tp_new which will look up |
| 4277 | __new__ in the MRO which will call tp_new_wrapper |
| 4278 | which will look through the base classes looking |
| 4279 | for a static base and call its tp_new (usually |
| 4280 | PyType_GenericNew), after performing various |
| 4281 | sanity checks and constructing a new argument |
| 4282 | list. Cut all that nonsense short -- this speeds |
| 4283 | up instance creation tremendously. */ |
| 4284 | specific = type->tp_new; |
| 4285 | /* XXX I'm not 100% sure that there isn't a hole |
| 4286 | in this reasoning that requires additional |
| 4287 | sanity checks. I'll buy the first person to |
| 4288 | point out a bug in this reasoning a beer. */ |
| 4289 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4290 | else { |
| 4291 | use_generic = 1; |
| 4292 | generic = p->function; |
| 4293 | } |
| 4294 | } while ((++p)->offset == offset); |
| 4295 | if (specific && !use_generic) |
| 4296 | *ptr = specific; |
| 4297 | else |
| 4298 | *ptr = generic; |
| 4299 | return p; |
| 4300 | } |
| 4301 | |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 4302 | static int recurse_down_subclasses(PyTypeObject *type, slotdef **pp, |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 4303 | PyObject *name); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4304 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4305 | /* In the type, update the slots whose slotdefs are gathered in the pp0 array, |
| 4306 | and then do the same for all this type's subtypes. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4307 | static int |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4308 | update_these_slots(PyTypeObject *type, slotdef **pp0, PyObject *name) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4309 | { |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4310 | slotdef **pp; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4311 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4312 | for (pp = pp0; *pp; pp++) |
| 4313 | update_one_slot(type, *pp); |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4314 | return recurse_down_subclasses(type, pp0, name); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4315 | } |
| 4316 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4317 | /* Update the slots whose slotdefs are gathered in the pp array in all (direct |
| 4318 | or indirect) subclasses of type. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4319 | static int |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4320 | recurse_down_subclasses(PyTypeObject *type, slotdef **pp, PyObject *name) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4321 | { |
| 4322 | PyTypeObject *subclass; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4323 | PyObject *ref, *subclasses, *dict; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4324 | int i, n; |
| 4325 | |
| 4326 | subclasses = type->tp_subclasses; |
| 4327 | if (subclasses == NULL) |
| 4328 | return 0; |
| 4329 | assert(PyList_Check(subclasses)); |
| 4330 | n = PyList_GET_SIZE(subclasses); |
| 4331 | for (i = 0; i < n; i++) { |
| 4332 | ref = PyList_GET_ITEM(subclasses, i); |
| 4333 | assert(PyWeakref_CheckRef(ref)); |
| 4334 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
Guido van Rossum | 59e6c53 | 2002-06-14 02:27:07 +0000 | [diff] [blame] | 4335 | assert(subclass != NULL); |
| 4336 | if ((PyObject *)subclass == Py_None) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4337 | continue; |
| 4338 | assert(PyType_Check(subclass)); |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4339 | /* Avoid recursing down into unaffected classes */ |
| 4340 | dict = subclass->tp_dict; |
| 4341 | if (dict != NULL && PyDict_Check(dict) && |
| 4342 | PyDict_GetItem(dict, name) != NULL) |
| 4343 | continue; |
| 4344 | if (update_these_slots(subclass, pp, name) < 0) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4345 | return -1; |
| 4346 | } |
| 4347 | return 0; |
| 4348 | } |
| 4349 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4350 | /* Comparison function for qsort() to compare slotdefs by their offset, and |
| 4351 | for equal offset by their address (to force a stable sort). */ |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 4352 | static int |
| 4353 | slotdef_cmp(const void *aa, const void *bb) |
| 4354 | { |
| 4355 | const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb; |
| 4356 | int c = a->offset - b->offset; |
| 4357 | if (c != 0) |
| 4358 | return c; |
| 4359 | else |
| 4360 | return a - b; |
| 4361 | } |
| 4362 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4363 | /* Initialize the slotdefs table by adding interned string objects for the |
| 4364 | names and sorting the entries. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4365 | static void |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 4366 | init_slotdefs(void) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4367 | { |
| 4368 | slotdef *p; |
| 4369 | static int initialized = 0; |
| 4370 | |
| 4371 | if (initialized) |
| 4372 | return; |
| 4373 | for (p = slotdefs; p->name; p++) { |
| 4374 | p->name_strobj = PyString_InternFromString(p->name); |
| 4375 | if (!p->name_strobj) |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4376 | Py_FatalError("Out of memory interning slotdef names"); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4377 | } |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4378 | qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef), |
| 4379 | slotdef_cmp); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4380 | initialized = 1; |
| 4381 | } |
| 4382 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4383 | /* Update the slots after assignment to a class (type) attribute. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4384 | static int |
| 4385 | update_slot(PyTypeObject *type, PyObject *name) |
| 4386 | { |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4387 | slotdef *ptrs[MAX_EQUIV]; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 4388 | slotdef *p; |
| 4389 | slotdef **pp; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4390 | int offset; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4391 | |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4392 | init_slotdefs(); |
| 4393 | pp = ptrs; |
| 4394 | for (p = slotdefs; p->name; p++) { |
| 4395 | /* XXX assume name is interned! */ |
| 4396 | if (p->name_strobj == name) |
| 4397 | *pp++ = p; |
| 4398 | } |
| 4399 | *pp = NULL; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 4400 | for (pp = ptrs; *pp; pp++) { |
| 4401 | p = *pp; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4402 | offset = p->offset; |
| 4403 | while (p > slotdefs && (p-1)->offset == offset) |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 4404 | --p; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4405 | *pp = p; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4406 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4407 | if (ptrs[0] == NULL) |
| 4408 | return 0; /* Not an attribute that affects any slots */ |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4409 | return update_these_slots(type, ptrs, name); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4410 | } |
| 4411 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4412 | /* Store the proper functions in the slot dispatches at class (type) |
| 4413 | definition time, based upon which operations the class overrides in its |
| 4414 | dict. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4415 | static void |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4416 | fixup_slot_dispatchers(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4417 | { |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4418 | slotdef *p; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4419 | |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 4420 | init_slotdefs(); |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4421 | for (p = slotdefs; p->name; ) |
| 4422 | p = update_one_slot(type, p); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4423 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4424 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4425 | /* This function is called by PyType_Ready() to populate the type's |
| 4426 | dictionary with method descriptors for function slots. For each |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 4427 | function slot (like tp_repr) that's defined in the type, one or more |
| 4428 | corresponding descriptors are added in the type's tp_dict dictionary |
| 4429 | under the appropriate name (like __repr__). Some function slots |
| 4430 | cause more than one descriptor to be added (for example, the nb_add |
| 4431 | slot adds both __add__ and __radd__ descriptors) and some function |
| 4432 | slots compete for the same descriptor (for example both sq_item and |
| 4433 | mp_subscript generate a __getitem__ descriptor). |
| 4434 | |
| 4435 | In the latter case, the first slotdef entry encoutered wins. Since |
| 4436 | slotdef entries are sorted by the offset of the slot in the etype |
| 4437 | struct, this gives us some control over disambiguating between |
| 4438 | competing slots: the members of struct etype are listed from most |
| 4439 | general to least general, so the most general slot is preferred. In |
| 4440 | particular, because as_mapping comes before as_sequence, for a type |
| 4441 | that defines both mp_subscript and sq_item, mp_subscript wins. |
| 4442 | |
| 4443 | This only adds new descriptors and doesn't overwrite entries in |
| 4444 | tp_dict that were previously defined. The descriptors contain a |
| 4445 | reference to the C function they must call, so that it's safe if they |
| 4446 | are copied into a subtype's __dict__ and the subtype has a different |
| 4447 | C function in its slot -- calling the method defined by the |
| 4448 | descriptor will call the C function that was used to create it, |
| 4449 | rather than the C function present in the slot when it is called. |
| 4450 | (This is important because a subtype may have a C function in the |
| 4451 | slot that calls the method from the dictionary, and we want to avoid |
| 4452 | infinite recursion here.) */ |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4453 | |
| 4454 | static int |
| 4455 | add_operators(PyTypeObject *type) |
| 4456 | { |
| 4457 | PyObject *dict = type->tp_dict; |
| 4458 | slotdef *p; |
| 4459 | PyObject *descr; |
| 4460 | void **ptr; |
| 4461 | |
| 4462 | init_slotdefs(); |
| 4463 | for (p = slotdefs; p->name; p++) { |
| 4464 | if (p->wrapper == NULL) |
| 4465 | continue; |
| 4466 | ptr = slotptr(type, p->offset); |
| 4467 | if (!ptr || !*ptr) |
| 4468 | continue; |
| 4469 | if (PyDict_GetItem(dict, p->name_strobj)) |
| 4470 | continue; |
| 4471 | descr = PyDescr_NewWrapper(type, p, *ptr); |
| 4472 | if (descr == NULL) |
| 4473 | return -1; |
| 4474 | if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) |
| 4475 | return -1; |
| 4476 | Py_DECREF(descr); |
| 4477 | } |
| 4478 | if (type->tp_new != NULL) { |
| 4479 | if (add_tp_new_wrapper(type) < 0) |
| 4480 | return -1; |
| 4481 | } |
| 4482 | return 0; |
| 4483 | } |
| 4484 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4485 | |
| 4486 | /* Cooperative 'super' */ |
| 4487 | |
| 4488 | typedef struct { |
| 4489 | PyObject_HEAD |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 4490 | PyTypeObject *type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4491 | PyObject *obj; |
| 4492 | } superobject; |
| 4493 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 4494 | static PyMemberDef super_members[] = { |
| 4495 | {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY, |
| 4496 | "the class invoking super()"}, |
| 4497 | {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY, |
| 4498 | "the instance invoking super(); may be None"}, |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 4499 | {0} |
| 4500 | }; |
| 4501 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4502 | static void |
| 4503 | super_dealloc(PyObject *self) |
| 4504 | { |
| 4505 | superobject *su = (superobject *)self; |
| 4506 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 4507 | _PyObject_GC_UNTRACK(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4508 | Py_XDECREF(su->obj); |
| 4509 | Py_XDECREF(su->type); |
| 4510 | self->ob_type->tp_free(self); |
| 4511 | } |
| 4512 | |
| 4513 | static PyObject * |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 4514 | super_repr(PyObject *self) |
| 4515 | { |
| 4516 | superobject *su = (superobject *)self; |
| 4517 | |
| 4518 | if (su->obj) |
| 4519 | return PyString_FromFormat( |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 4520 | "<super: <class '%s'>, <%s object>>", |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 4521 | su->type ? su->type->tp_name : "NULL", |
| 4522 | su->obj->ob_type->tp_name); |
| 4523 | else |
| 4524 | return PyString_FromFormat( |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 4525 | "<super: <class '%s'>, NULL>", |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 4526 | su->type ? su->type->tp_name : "NULL"); |
| 4527 | } |
| 4528 | |
| 4529 | static PyObject * |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4530 | super_getattro(PyObject *self, PyObject *name) |
| 4531 | { |
| 4532 | superobject *su = (superobject *)self; |
| 4533 | |
| 4534 | if (su->obj != NULL) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 4535 | PyObject *mro, *res, *tmp, *dict; |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 4536 | PyTypeObject *starttype; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4537 | descrgetfunc f; |
| 4538 | int i, n; |
| 4539 | |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 4540 | starttype = su->obj->ob_type; |
| 4541 | mro = starttype->tp_mro; |
| 4542 | |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 4543 | if (mro == NULL) |
| 4544 | n = 0; |
| 4545 | else { |
| 4546 | assert(PyTuple_Check(mro)); |
| 4547 | n = PyTuple_GET_SIZE(mro); |
| 4548 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4549 | for (i = 0; i < n; i++) { |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 4550 | if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4551 | break; |
| 4552 | } |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 4553 | if (i >= n && PyType_Check(su->obj)) { |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 4554 | starttype = (PyTypeObject *)(su->obj); |
| 4555 | mro = starttype->tp_mro; |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 4556 | if (mro == NULL) |
| 4557 | n = 0; |
| 4558 | else { |
| 4559 | assert(PyTuple_Check(mro)); |
| 4560 | n = PyTuple_GET_SIZE(mro); |
| 4561 | } |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 4562 | for (i = 0; i < n; i++) { |
| 4563 | if ((PyObject *)(su->type) == |
| 4564 | PyTuple_GET_ITEM(mro, i)) |
| 4565 | break; |
| 4566 | } |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 4567 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4568 | i++; |
| 4569 | res = NULL; |
| 4570 | for (; i < n; i++) { |
| 4571 | tmp = PyTuple_GET_ITEM(mro, i); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 4572 | if (PyType_Check(tmp)) |
| 4573 | dict = ((PyTypeObject *)tmp)->tp_dict; |
| 4574 | else if (PyClass_Check(tmp)) |
| 4575 | dict = ((PyClassObject *)tmp)->cl_dict; |
| 4576 | else |
| 4577 | continue; |
| 4578 | res = PyDict_GetItem(dict, name); |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 4579 | if (res != NULL && !PyDescr_IsData(res)) { |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4580 | Py_INCREF(res); |
| 4581 | f = res->ob_type->tp_descr_get; |
| 4582 | if (f != NULL) { |
Guido van Rossum | d464107 | 2002-04-03 02:13:37 +0000 | [diff] [blame] | 4583 | tmp = f(res, su->obj, |
| 4584 | (PyObject *)starttype); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4585 | Py_DECREF(res); |
| 4586 | res = tmp; |
| 4587 | } |
| 4588 | return res; |
| 4589 | } |
| 4590 | } |
| 4591 | } |
| 4592 | return PyObject_GenericGetAttr(self, name); |
| 4593 | } |
| 4594 | |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 4595 | static int |
| 4596 | supercheck(PyTypeObject *type, PyObject *obj) |
| 4597 | { |
| 4598 | if (!PyType_IsSubtype(obj->ob_type, type) && |
| 4599 | !(PyType_Check(obj) && |
| 4600 | PyType_IsSubtype((PyTypeObject *)obj, type))) { |
| 4601 | PyErr_SetString(PyExc_TypeError, |
| 4602 | "super(type, obj): " |
| 4603 | "obj must be an instance or subtype of type"); |
| 4604 | return -1; |
| 4605 | } |
| 4606 | else |
| 4607 | return 0; |
| 4608 | } |
| 4609 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4610 | static PyObject * |
| 4611 | super_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 4612 | { |
| 4613 | superobject *su = (superobject *)self; |
| 4614 | superobject *new; |
| 4615 | |
| 4616 | if (obj == NULL || obj == Py_None || su->obj != NULL) { |
| 4617 | /* Not binding to an object, or already bound */ |
| 4618 | Py_INCREF(self); |
| 4619 | return self; |
| 4620 | } |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 4621 | if (su->ob_type != &PySuper_Type) |
| 4622 | /* If su is an instance of a subclass of super, |
| 4623 | call its type */ |
| 4624 | return PyObject_CallFunction((PyObject *)su->ob_type, |
| 4625 | "OO", su->type, obj); |
| 4626 | else { |
| 4627 | /* Inline the common case */ |
| 4628 | if (supercheck(su->type, obj) < 0) |
| 4629 | return NULL; |
| 4630 | new = (superobject *)PySuper_Type.tp_new(&PySuper_Type, |
| 4631 | NULL, NULL); |
| 4632 | if (new == NULL) |
| 4633 | return NULL; |
| 4634 | Py_INCREF(su->type); |
| 4635 | Py_INCREF(obj); |
| 4636 | new->type = su->type; |
| 4637 | new->obj = obj; |
| 4638 | return (PyObject *)new; |
| 4639 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4640 | } |
| 4641 | |
| 4642 | static int |
| 4643 | super_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 4644 | { |
| 4645 | superobject *su = (superobject *)self; |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 4646 | PyTypeObject *type; |
| 4647 | PyObject *obj = NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4648 | |
| 4649 | if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj)) |
| 4650 | return -1; |
| 4651 | if (obj == Py_None) |
| 4652 | obj = NULL; |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 4653 | if (obj != NULL && supercheck(type, obj) < 0) |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4654 | return -1; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4655 | Py_INCREF(type); |
| 4656 | Py_XINCREF(obj); |
| 4657 | su->type = type; |
| 4658 | su->obj = obj; |
| 4659 | return 0; |
| 4660 | } |
| 4661 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4662 | PyDoc_STRVAR(super_doc, |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4663 | "super(type) -> unbound super object\n" |
| 4664 | "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 4665 | "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4666 | "Typical use to call a cooperative superclass method:\n" |
| 4667 | "class C(B):\n" |
| 4668 | " def meth(self, arg):\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4669 | " super(C, self).meth(arg)"); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4670 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 4671 | static int |
| 4672 | super_traverse(PyObject *self, visitproc visit, void *arg) |
| 4673 | { |
| 4674 | superobject *su = (superobject *)self; |
| 4675 | int err; |
| 4676 | |
| 4677 | #define VISIT(SLOT) \ |
| 4678 | if (SLOT) { \ |
| 4679 | err = visit((PyObject *)(SLOT), arg); \ |
| 4680 | if (err) \ |
| 4681 | return err; \ |
| 4682 | } |
| 4683 | |
| 4684 | VISIT(su->obj); |
| 4685 | VISIT(su->type); |
| 4686 | |
| 4687 | #undef VISIT |
| 4688 | |
| 4689 | return 0; |
| 4690 | } |
| 4691 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4692 | PyTypeObject PySuper_Type = { |
| 4693 | PyObject_HEAD_INIT(&PyType_Type) |
| 4694 | 0, /* ob_size */ |
| 4695 | "super", /* tp_name */ |
| 4696 | sizeof(superobject), /* tp_basicsize */ |
| 4697 | 0, /* tp_itemsize */ |
| 4698 | /* methods */ |
| 4699 | super_dealloc, /* tp_dealloc */ |
| 4700 | 0, /* tp_print */ |
| 4701 | 0, /* tp_getattr */ |
| 4702 | 0, /* tp_setattr */ |
| 4703 | 0, /* tp_compare */ |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 4704 | super_repr, /* tp_repr */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4705 | 0, /* tp_as_number */ |
| 4706 | 0, /* tp_as_sequence */ |
| 4707 | 0, /* tp_as_mapping */ |
| 4708 | 0, /* tp_hash */ |
| 4709 | 0, /* tp_call */ |
| 4710 | 0, /* tp_str */ |
| 4711 | super_getattro, /* tp_getattro */ |
| 4712 | 0, /* tp_setattro */ |
| 4713 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 4714 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 4715 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4716 | super_doc, /* tp_doc */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 4717 | super_traverse, /* tp_traverse */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4718 | 0, /* tp_clear */ |
| 4719 | 0, /* tp_richcompare */ |
| 4720 | 0, /* tp_weaklistoffset */ |
| 4721 | 0, /* tp_iter */ |
| 4722 | 0, /* tp_iternext */ |
| 4723 | 0, /* tp_methods */ |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 4724 | super_members, /* tp_members */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4725 | 0, /* tp_getset */ |
| 4726 | 0, /* tp_base */ |
| 4727 | 0, /* tp_dict */ |
| 4728 | super_descr_get, /* tp_descr_get */ |
| 4729 | 0, /* tp_descr_set */ |
| 4730 | 0, /* tp_dictoffset */ |
| 4731 | super_init, /* tp_init */ |
| 4732 | PyType_GenericAlloc, /* tp_alloc */ |
| 4733 | PyType_GenericNew, /* tp_new */ |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 4734 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4735 | }; |