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