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