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