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