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