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