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