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