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