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 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7 | staticforward int add_members(PyTypeObject *, struct memberlist *); |
| 8 | |
| 9 | static struct memberlist type_members[] = { |
| 10 | {"__name__", T_STRING, offsetof(PyTypeObject, tp_name), READONLY}, |
| 11 | {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY}, |
| 12 | {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY}, |
| 13 | {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY}, |
| 14 | {"__doc__", T_STRING, offsetof(PyTypeObject, tp_doc), READONLY}, |
| 15 | {"__weaklistoffset__", T_LONG, |
| 16 | offsetof(PyTypeObject, tp_weaklistoffset), READONLY}, |
| 17 | {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY}, |
| 18 | {"__dictoffset__", T_LONG, |
| 19 | offsetof(PyTypeObject, tp_dictoffset), READONLY}, |
| 20 | {"__bases__", T_OBJECT, offsetof(PyTypeObject, tp_bases), READONLY}, |
| 21 | {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY}, |
| 22 | {0} |
| 23 | }; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 25 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 26 | type_module(PyTypeObject *type, void *context) |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 27 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 28 | return PyString_FromString("__builtin__"); |
| 29 | } |
| 30 | |
| 31 | static PyObject * |
| 32 | type_dict(PyTypeObject *type, void *context) |
| 33 | { |
| 34 | if (type->tp_dict == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 35 | Py_INCREF(Py_None); |
| 36 | return Py_None; |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 37 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 38 | if (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE) { |
| 39 | Py_INCREF(type->tp_dict); |
| 40 | return type->tp_dict; |
| 41 | } |
| 42 | return PyDictProxy_New(type->tp_dict); |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 45 | static PyObject * |
| 46 | type_defined(PyTypeObject *type, void *context) |
| 47 | { |
| 48 | if (type->tp_defined == NULL) { |
| 49 | Py_INCREF(Py_None); |
| 50 | return Py_None; |
| 51 | } |
| 52 | if (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE) { |
| 53 | Py_INCREF(type->tp_defined); |
| 54 | return type->tp_defined; |
| 55 | } |
| 56 | return PyDictProxy_New(type->tp_defined); |
| 57 | } |
| 58 | |
| 59 | static PyObject * |
| 60 | type_dynamic(PyTypeObject *type, void *context) |
| 61 | { |
| 62 | PyObject *res; |
| 63 | |
| 64 | res = (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE) ? Py_True : Py_False; |
| 65 | Py_INCREF(res); |
| 66 | return res; |
| 67 | } |
| 68 | |
| 69 | struct getsetlist type_getsets[] = { |
| 70 | {"__module__", (getter)type_module, NULL, NULL}, |
| 71 | {"__dict__", (getter)type_dict, NULL, NULL}, |
| 72 | {"__defined__", (getter)type_defined, NULL, NULL}, |
| 73 | {"__dynamic__", (getter)type_dynamic, NULL, NULL}, |
| 74 | {0} |
| 75 | }; |
| 76 | |
Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 77 | static int |
| 78 | type_compare(PyObject *v, PyObject *w) |
| 79 | { |
| 80 | /* This is called with type objects only. So we |
| 81 | can just compare the addresses. */ |
| 82 | Py_uintptr_t vv = (Py_uintptr_t)v; |
| 83 | Py_uintptr_t ww = (Py_uintptr_t)w; |
| 84 | return (vv < ww) ? -1 : (vv > ww) ? 1 : 0; |
| 85 | } |
| 86 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 87 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 88 | type_repr(PyTypeObject *type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 89 | { |
| 90 | char buf[100]; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 91 | sprintf(buf, "<type '%.80s'>", type->tp_name); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 92 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 95 | static PyObject * |
| 96 | type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 97 | { |
| 98 | PyObject *obj; |
| 99 | |
| 100 | if (type->tp_new == NULL) { |
| 101 | PyErr_Format(PyExc_TypeError, |
| 102 | "cannot create '%.100s' instances", |
| 103 | type->tp_name); |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | obj = type->tp_new(type, args, NULL); |
| 108 | if (obj != NULL) { |
| 109 | type = obj->ob_type; |
| 110 | if (type->tp_init != NULL && |
| 111 | type->tp_init(obj, args, kwds) < 0) { |
| 112 | Py_DECREF(obj); |
| 113 | obj = NULL; |
| 114 | } |
| 115 | } |
| 116 | return obj; |
| 117 | } |
| 118 | |
| 119 | PyObject * |
| 120 | PyType_GenericAlloc(PyTypeObject *type, int nitems) |
| 121 | { |
| 122 | int size; |
| 123 | void *mem; |
| 124 | PyObject *obj; |
| 125 | |
| 126 | /* Inline PyObject_New() so we can zero the memory */ |
| 127 | size = _PyObject_VAR_SIZE(type, nitems); |
| 128 | mem = PyObject_MALLOC(size); |
| 129 | if (mem == NULL) |
| 130 | return PyErr_NoMemory(); |
| 131 | memset(mem, '\0', size); |
| 132 | if (PyType_IS_GC(type)) |
| 133 | obj = PyObject_FROM_GC(mem); |
| 134 | else |
| 135 | obj = (PyObject *)mem; |
| 136 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 137 | Py_INCREF(type); |
| 138 | if (type->tp_itemsize == 0) |
| 139 | PyObject_INIT(obj, type); |
| 140 | else |
| 141 | (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems); |
| 142 | if (PyType_IS_GC(type)) |
| 143 | PyObject_GC_Init(obj); |
| 144 | return obj; |
| 145 | } |
| 146 | |
| 147 | PyObject * |
| 148 | PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 149 | { |
| 150 | return type->tp_alloc(type, 0); |
| 151 | } |
| 152 | |
| 153 | /* Helper for subtyping */ |
| 154 | |
| 155 | static void |
| 156 | subtype_dealloc(PyObject *self) |
| 157 | { |
| 158 | int dictoffset = self->ob_type->tp_dictoffset; |
| 159 | PyTypeObject *type, *base; |
| 160 | destructor f; |
| 161 | |
| 162 | /* This exists so we can DECREF self->ob_type */ |
| 163 | |
| 164 | /* Find the nearest base with a different tp_dealloc */ |
| 165 | type = self->ob_type; |
| 166 | base = type->tp_base; |
| 167 | while ((f = base->tp_dealloc) == subtype_dealloc) { |
| 168 | base = base->tp_base; |
| 169 | assert(base); |
| 170 | } |
| 171 | |
| 172 | /* If we added a dict, DECREF it */ |
| 173 | if (dictoffset && !base->tp_dictoffset) { |
| 174 | PyObject **dictptr = (PyObject **) ((char *)self + dictoffset); |
| 175 | PyObject *dict = *dictptr; |
| 176 | if (dict != NULL) { |
| 177 | Py_DECREF(dict); |
| 178 | *dictptr = NULL; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* Finalize GC if the base doesn't do GC and we do */ |
| 183 | if (PyType_IS_GC(type) && !PyType_IS_GC(base)) |
| 184 | PyObject_GC_Fini(self); |
| 185 | |
| 186 | /* Call the base tp_dealloc() */ |
| 187 | assert(f); |
| 188 | f(self); |
| 189 | |
| 190 | /* Can't reference self beyond this point */ |
| 191 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 192 | Py_DECREF(type); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | staticforward void override_slots(PyTypeObject *type, PyObject *dict); |
| 197 | staticforward PyTypeObject *solid_base(PyTypeObject *type); |
| 198 | |
| 199 | typedef struct { |
| 200 | PyTypeObject type; |
| 201 | PyNumberMethods as_number; |
| 202 | PySequenceMethods as_sequence; |
| 203 | PyMappingMethods as_mapping; |
| 204 | PyBufferProcs as_buffer; |
| 205 | PyObject *name, *slots; |
| 206 | struct memberlist members[1]; |
| 207 | } etype; |
| 208 | |
| 209 | /* type test with subclassing support */ |
| 210 | |
| 211 | int |
| 212 | PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
| 213 | { |
| 214 | PyObject *mro; |
| 215 | |
| 216 | mro = a->tp_mro; |
| 217 | if (mro != NULL) { |
| 218 | /* Deal with multiple inheritance without recursion |
| 219 | by walking the MRO tuple */ |
| 220 | int i, n; |
| 221 | assert(PyTuple_Check(mro)); |
| 222 | n = PyTuple_GET_SIZE(mro); |
| 223 | for (i = 0; i < n; i++) { |
| 224 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) |
| 225 | return 1; |
| 226 | } |
| 227 | return 0; |
| 228 | } |
| 229 | else { |
| 230 | /* a is not completely initilized yet; follow tp_base */ |
| 231 | do { |
| 232 | if (a == b) |
| 233 | return 1; |
| 234 | a = a->tp_base; |
| 235 | } while (a != NULL); |
| 236 | return b == &PyBaseObject_Type; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /* Method resolution order algorithm from "Putting Metaclasses to Work" |
| 241 | by Forman and Danforth (Addison-Wesley 1999). */ |
| 242 | |
| 243 | static int |
| 244 | conservative_merge(PyObject *left, PyObject *right) |
| 245 | { |
| 246 | int left_size; |
| 247 | int right_size; |
| 248 | int i, j, r, ok; |
| 249 | PyObject *temp, *rr; |
| 250 | |
| 251 | assert(PyList_Check(left)); |
| 252 | assert(PyList_Check(right)); |
| 253 | |
| 254 | again: |
| 255 | left_size = PyList_GET_SIZE(left); |
| 256 | right_size = PyList_GET_SIZE(right); |
| 257 | for (i = 0; i < left_size; i++) { |
| 258 | for (j = 0; j < right_size; j++) { |
| 259 | if (PyList_GET_ITEM(left, i) == |
| 260 | PyList_GET_ITEM(right, j)) { |
| 261 | /* found a merge point */ |
| 262 | temp = PyList_New(0); |
| 263 | if (temp == NULL) |
| 264 | return -1; |
| 265 | for (r = 0; r < j; r++) { |
| 266 | rr = PyList_GET_ITEM(right, r); |
| 267 | ok = PySequence_Contains(left, rr); |
| 268 | if (ok < 0) { |
| 269 | Py_DECREF(temp); |
| 270 | return -1; |
| 271 | } |
| 272 | if (!ok) { |
| 273 | ok = PyList_Append(temp, rr); |
| 274 | if (ok < 0) { |
| 275 | Py_DECREF(temp); |
| 276 | return -1; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | ok = PyList_SetSlice(left, i, i, temp); |
| 281 | Py_DECREF(temp); |
| 282 | if (ok < 0) |
| 283 | return -1; |
| 284 | ok = PyList_SetSlice(right, 0, j+1, NULL); |
| 285 | if (ok < 0) |
| 286 | return -1; |
| 287 | goto again; |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | return PyList_SetSlice(left, left_size, left_size, right); |
| 292 | } |
| 293 | |
| 294 | static int |
| 295 | serious_order_disagreements(PyObject *left, PyObject *right) |
| 296 | { |
| 297 | return 0; /* XXX later -- for now, we cheat: "don't do that" */ |
| 298 | } |
| 299 | |
| 300 | static PyObject * |
| 301 | mro_implementation(PyTypeObject *type) |
| 302 | { |
| 303 | int i, n, ok; |
| 304 | PyObject *bases, *result; |
| 305 | |
| 306 | bases = type->tp_bases; |
| 307 | n = PyTuple_GET_SIZE(bases); |
| 308 | result = Py_BuildValue("[O]", (PyObject *)type); |
| 309 | if (result == NULL) |
| 310 | return NULL; |
| 311 | for (i = 0; i < n; i++) { |
| 312 | PyTypeObject *base = |
| 313 | (PyTypeObject *) PyTuple_GET_ITEM(bases, i); |
| 314 | PyObject *parentMRO = PySequence_List(base->tp_mro); |
| 315 | if (parentMRO == NULL) { |
| 316 | Py_DECREF(result); |
| 317 | return NULL; |
| 318 | } |
| 319 | if (serious_order_disagreements(result, parentMRO)) { |
| 320 | Py_DECREF(result); |
| 321 | return NULL; |
| 322 | } |
| 323 | ok = conservative_merge(result, parentMRO); |
| 324 | Py_DECREF(parentMRO); |
| 325 | if (ok < 0) { |
| 326 | Py_DECREF(result); |
| 327 | return NULL; |
| 328 | } |
| 329 | } |
| 330 | return result; |
| 331 | } |
| 332 | |
| 333 | static PyObject * |
| 334 | mro_external(PyObject *self, PyObject *args) |
| 335 | { |
| 336 | PyTypeObject *type = (PyTypeObject *)self; |
| 337 | |
| 338 | if (!PyArg_ParseTuple(args, "")) |
| 339 | return NULL; |
| 340 | return mro_implementation(type); |
| 341 | } |
| 342 | |
| 343 | static int |
| 344 | mro_internal(PyTypeObject *type) |
| 345 | { |
| 346 | PyObject *mro, *result, *tuple; |
| 347 | |
| 348 | if (type->ob_type == &PyType_Type) { |
| 349 | result = mro_implementation(type); |
| 350 | } |
| 351 | else { |
| 352 | mro = PyObject_GetAttrString((PyObject *)type, "mro"); |
| 353 | if (mro == NULL) |
| 354 | return -1; |
| 355 | result = PyObject_CallObject(mro, NULL); |
| 356 | Py_DECREF(mro); |
| 357 | } |
| 358 | if (result == NULL) |
| 359 | return -1; |
| 360 | tuple = PySequence_Tuple(result); |
| 361 | Py_DECREF(result); |
| 362 | type->tp_mro = tuple; |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | |
| 367 | /* Calculate the best base amongst multiple base classes. |
| 368 | This is the first one that's on the path to the "solid base". */ |
| 369 | |
| 370 | static PyTypeObject * |
| 371 | best_base(PyObject *bases) |
| 372 | { |
| 373 | int i, n; |
| 374 | PyTypeObject *base, *winner, *candidate, *base_i; |
| 375 | |
| 376 | assert(PyTuple_Check(bases)); |
| 377 | n = PyTuple_GET_SIZE(bases); |
| 378 | assert(n > 0); |
| 379 | base = (PyTypeObject *)PyTuple_GET_ITEM(bases, 0); |
| 380 | winner = &PyBaseObject_Type; |
| 381 | for (i = 0; i < n; i++) { |
| 382 | base_i = (PyTypeObject *)PyTuple_GET_ITEM(bases, i); |
| 383 | if (!PyType_Check((PyObject *)base_i)) { |
| 384 | PyErr_SetString( |
| 385 | PyExc_TypeError, |
| 386 | "bases must be types"); |
| 387 | return NULL; |
| 388 | } |
| 389 | if (base_i->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 390 | if (PyType_Ready(base_i) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 391 | return NULL; |
| 392 | } |
| 393 | candidate = solid_base(base_i); |
| 394 | if (PyType_IsSubtype(winner, candidate)) |
| 395 | ; |
| 396 | else if (PyType_IsSubtype(candidate, winner)) { |
| 397 | winner = candidate; |
| 398 | base = base_i; |
| 399 | } |
| 400 | else { |
| 401 | PyErr_SetString( |
| 402 | PyExc_TypeError, |
| 403 | "multiple bases have " |
| 404 | "instance lay-out conflict"); |
| 405 | return NULL; |
| 406 | } |
| 407 | } |
| 408 | assert(base != NULL); |
| 409 | return base; |
| 410 | } |
| 411 | |
| 412 | static int |
| 413 | extra_ivars(PyTypeObject *type, PyTypeObject *base) |
| 414 | { |
| 415 | int t_size = PyType_BASICSIZE(type); |
| 416 | int b_size = PyType_BASICSIZE(base); |
| 417 | |
| 418 | assert(t_size >= b_size); /* type smaller than base! */ |
| 419 | if (type->tp_itemsize || base->tp_itemsize) { |
| 420 | /* If itemsize is involved, stricter rules */ |
| 421 | return t_size != b_size || |
| 422 | type->tp_itemsize != base->tp_itemsize; |
| 423 | } |
| 424 | if (t_size == b_size) |
| 425 | return 0; |
| 426 | if (type->tp_dictoffset != 0 && base->tp_dictoffset == 0 && |
| 427 | type->tp_dictoffset == b_size && |
| 428 | (size_t)t_size == b_size + sizeof(PyObject *)) |
| 429 | return 0; /* "Forgive" adding a __dict__ only */ |
| 430 | return 1; |
| 431 | } |
| 432 | |
| 433 | static PyTypeObject * |
| 434 | solid_base(PyTypeObject *type) |
| 435 | { |
| 436 | PyTypeObject *base; |
| 437 | |
| 438 | if (type->tp_base) |
| 439 | base = solid_base(type->tp_base); |
| 440 | else |
| 441 | base = &PyBaseObject_Type; |
| 442 | if (extra_ivars(type, base)) |
| 443 | return type; |
| 444 | else |
| 445 | return base; |
| 446 | } |
| 447 | |
| 448 | staticforward void object_dealloc(PyObject *); |
| 449 | staticforward int object_init(PyObject *, PyObject *, PyObject *); |
| 450 | |
| 451 | static PyObject * |
| 452 | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) |
| 453 | { |
| 454 | PyObject *name, *bases, *dict; |
| 455 | static char *kwlist[] = {"name", "bases", "dict", 0}; |
| 456 | PyObject *slots, *tmp; |
| 457 | PyTypeObject *type, *base, *tmptype; |
| 458 | etype *et; |
| 459 | struct memberlist *mp; |
| 460 | int i, nbases, nslots, slotoffset, dynamic; |
| 461 | |
| 462 | if (metatype == &PyType_Type && |
| 463 | PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && |
| 464 | (kwds == NULL || (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) { |
| 465 | /* type(x) -> x.__class__ */ |
| 466 | PyObject *x = PyTuple_GET_ITEM(args, 0); |
| 467 | Py_INCREF(x->ob_type); |
| 468 | return (PyObject *) x->ob_type; |
| 469 | } |
| 470 | |
| 471 | /* Check arguments */ |
| 472 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist, |
| 473 | &name, |
| 474 | &PyTuple_Type, &bases, |
| 475 | &PyDict_Type, &dict)) |
| 476 | return NULL; |
| 477 | |
| 478 | /* Determine the proper metatype to deal with this, |
| 479 | and check for metatype conflicts while we're at it. |
| 480 | Note that if some other metatype wins to contract, |
| 481 | it's possible that its instances are not types. */ |
| 482 | nbases = PyTuple_GET_SIZE(bases); |
| 483 | for (i = 0; i < nbases; i++) { |
| 484 | tmp = PyTuple_GET_ITEM(bases, i); |
| 485 | tmptype = tmp->ob_type; |
| 486 | if (PyType_IsSubtype(metatype, tmptype)) |
| 487 | continue; |
| 488 | if (PyType_IsSubtype(tmptype, metatype)) { |
| 489 | metatype = tmptype; |
| 490 | continue; |
| 491 | } |
| 492 | PyErr_SetString(PyExc_TypeError, |
| 493 | "metatype conflict among bases"); |
| 494 | return NULL; |
| 495 | } |
| 496 | if (metatype->tp_new != type_new) /* Pass it to the winner */ |
| 497 | return metatype->tp_new(metatype, args, kwds); |
| 498 | |
| 499 | /* Adjust for empty tuple bases */ |
| 500 | if (nbases == 0) { |
| 501 | bases = Py_BuildValue("(O)", &PyBaseObject_Type); |
| 502 | if (bases == NULL) |
| 503 | return NULL; |
| 504 | nbases = 1; |
| 505 | } |
| 506 | else |
| 507 | Py_INCREF(bases); |
| 508 | |
| 509 | /* XXX From here until type is allocated, "return NULL" leaks bases! */ |
| 510 | |
| 511 | /* Calculate best base, and check that all bases are type objects */ |
| 512 | base = best_base(bases); |
| 513 | if (base == NULL) |
| 514 | return NULL; |
| 515 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 516 | PyErr_Format(PyExc_TypeError, |
| 517 | "type '%.100s' is not an acceptable base type", |
| 518 | base->tp_name); |
| 519 | return NULL; |
| 520 | } |
| 521 | |
| 522 | /* Should this be a dynamic class (i.e. modifiable __dict__)? */ |
| 523 | tmp = PyDict_GetItemString(dict, "__dynamic__"); |
| 524 | if (tmp != NULL) { |
| 525 | /* The class author has a preference */ |
| 526 | dynamic = PyObject_IsTrue(tmp); |
| 527 | Py_DECREF(tmp); |
| 528 | if (dynamic < 0) |
| 529 | return NULL; |
| 530 | } |
| 531 | else { |
| 532 | /* Make a new class dynamic if any of its bases is dynamic. |
| 533 | This is not always the same as inheriting the __dynamic__ |
| 534 | class attribute! */ |
| 535 | dynamic = 0; |
| 536 | for (i = 0; i < nbases; i++) { |
| 537 | tmptype = (PyTypeObject *)PyTuple_GET_ITEM(bases, i); |
| 538 | if (tmptype->tp_flags & Py_TPFLAGS_DYNAMICTYPE) { |
| 539 | dynamic = 1; |
| 540 | break; |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /* Check for a __slots__ sequence variable in dict, and count it */ |
| 546 | slots = PyDict_GetItemString(dict, "__slots__"); |
| 547 | nslots = 0; |
| 548 | if (slots != NULL) { |
| 549 | /* Make it into a tuple */ |
| 550 | if (PyString_Check(slots)) |
| 551 | slots = Py_BuildValue("(O)", slots); |
| 552 | else |
| 553 | slots = PySequence_Tuple(slots); |
| 554 | if (slots == NULL) |
| 555 | return NULL; |
| 556 | nslots = PyTuple_GET_SIZE(slots); |
| 557 | for (i = 0; i < nslots; i++) { |
| 558 | if (!PyString_Check(PyTuple_GET_ITEM(slots, i))) { |
| 559 | PyErr_SetString(PyExc_TypeError, |
| 560 | "__slots__ must be a sequence of strings"); |
| 561 | Py_DECREF(slots); |
| 562 | return NULL; |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | if (slots == NULL && base->tp_dictoffset == 0 && |
| 567 | (base->tp_setattro == PyObject_GenericSetAttr || |
| 568 | base->tp_setattro == NULL)) |
| 569 | nslots = 1; |
| 570 | |
| 571 | /* XXX From here until type is safely allocated, |
| 572 | "return NULL" may leak slots! */ |
| 573 | |
| 574 | /* Allocate the type object */ |
| 575 | type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots); |
| 576 | if (type == NULL) |
| 577 | return NULL; |
| 578 | |
| 579 | /* Keep name and slots alive in the extended type object */ |
| 580 | et = (etype *)type; |
| 581 | Py_INCREF(name); |
| 582 | et->name = name; |
| 583 | et->slots = slots; |
| 584 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 585 | /* Initialize tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 586 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | |
| 587 | Py_TPFLAGS_BASETYPE; |
| 588 | if (dynamic) |
| 589 | type->tp_flags |= Py_TPFLAGS_DYNAMICTYPE; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 590 | |
| 591 | /* It's a new-style number unless it specifically inherits any |
| 592 | old-style numeric behavior */ |
| 593 | if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) || |
| 594 | (base->tp_as_number == NULL)) |
| 595 | type->tp_flags |= Py_TPFLAGS_CHECKTYPES; |
| 596 | |
| 597 | /* Initialize essential fields */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 598 | type->tp_as_number = &et->as_number; |
| 599 | type->tp_as_sequence = &et->as_sequence; |
| 600 | type->tp_as_mapping = &et->as_mapping; |
| 601 | type->tp_as_buffer = &et->as_buffer; |
| 602 | type->tp_name = PyString_AS_STRING(name); |
| 603 | |
| 604 | /* Set tp_base and tp_bases */ |
| 605 | type->tp_bases = bases; |
| 606 | Py_INCREF(base); |
| 607 | type->tp_base = base; |
| 608 | |
| 609 | /* Initialize tp_defined from passed-in dict */ |
| 610 | type->tp_defined = dict = PyDict_Copy(dict); |
| 611 | if (dict == NULL) { |
| 612 | Py_DECREF(type); |
| 613 | return NULL; |
| 614 | } |
| 615 | |
| 616 | /* Special-case __new__: if it's a plain function, |
| 617 | make it a static function */ |
| 618 | tmp = PyDict_GetItemString(dict, "__new__"); |
| 619 | if (tmp != NULL && PyFunction_Check(tmp)) { |
| 620 | tmp = PyStaticMethod_New(tmp); |
| 621 | if (tmp == NULL) { |
| 622 | Py_DECREF(type); |
| 623 | return NULL; |
| 624 | } |
| 625 | PyDict_SetItemString(dict, "__new__", tmp); |
| 626 | Py_DECREF(tmp); |
| 627 | } |
| 628 | |
| 629 | /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
| 630 | mp = et->members; |
| 631 | slotoffset = PyType_BASICSIZE(base); |
| 632 | if (slots != NULL) { |
| 633 | for (i = 0; i < nslots; i++, mp++) { |
| 634 | mp->name = PyString_AS_STRING( |
| 635 | PyTuple_GET_ITEM(slots, i)); |
| 636 | mp->type = T_OBJECT; |
| 637 | mp->offset = slotoffset; |
| 638 | slotoffset += sizeof(PyObject *); |
| 639 | } |
| 640 | } |
| 641 | else if (nslots) { |
| 642 | type->tp_dictoffset = slotoffset; |
| 643 | mp->name = "__dict__"; |
| 644 | mp->type = T_OBJECT; |
| 645 | mp->offset = slotoffset; |
| 646 | mp->readonly = 1; |
| 647 | slotoffset += sizeof(PyObject *); |
| 648 | } |
| 649 | type->tp_basicsize = slotoffset; |
| 650 | add_members(type, et->members); |
| 651 | |
| 652 | /* Special case some slots */ |
| 653 | if (type->tp_dictoffset != 0 || nslots > 0) { |
| 654 | if (base->tp_getattr == NULL && base->tp_getattro == NULL) |
| 655 | type->tp_getattro = PyObject_GenericGetAttr; |
| 656 | if (base->tp_setattr == NULL && base->tp_setattro == NULL) |
| 657 | type->tp_setattro = PyObject_GenericSetAttr; |
| 658 | } |
| 659 | type->tp_dealloc = subtype_dealloc; |
| 660 | |
| 661 | /* Always override allocation strategy to use regular heap */ |
| 662 | type->tp_alloc = PyType_GenericAlloc; |
| 663 | type->tp_free = _PyObject_Del; |
| 664 | |
| 665 | /* Initialize the rest */ |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 666 | if (PyType_Ready(type) < 0) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 667 | Py_DECREF(type); |
| 668 | return NULL; |
| 669 | } |
| 670 | |
| 671 | /* Override slots that deserve it */ |
| 672 | override_slots(type, type->tp_defined); |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 673 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 674 | return (PyObject *)type; |
| 675 | } |
| 676 | |
| 677 | /* Internal API to look for a name through the MRO. |
| 678 | This returns a borrowed reference, and doesn't set an exception! */ |
| 679 | PyObject * |
| 680 | _PyType_Lookup(PyTypeObject *type, PyObject *name) |
| 681 | { |
| 682 | int i, n; |
| 683 | PyObject *mro, *res, *dict; |
| 684 | |
| 685 | /* For static types, look in tp_dict */ |
| 686 | if (!(type->tp_flags & Py_TPFLAGS_DYNAMICTYPE)) { |
| 687 | dict = type->tp_dict; |
| 688 | assert(dict && PyDict_Check(dict)); |
| 689 | return PyDict_GetItem(dict, name); |
| 690 | } |
| 691 | |
| 692 | /* For dynamic types, look in tp_defined of types in MRO */ |
| 693 | mro = type->tp_mro; |
| 694 | assert(PyTuple_Check(mro)); |
| 695 | n = PyTuple_GET_SIZE(mro); |
| 696 | for (i = 0; i < n; i++) { |
| 697 | type = (PyTypeObject *) PyTuple_GET_ITEM(mro, i); |
| 698 | assert(PyType_Check(type)); |
| 699 | dict = type->tp_defined; |
| 700 | assert(dict && PyDict_Check(dict)); |
| 701 | res = PyDict_GetItem(dict, name); |
| 702 | if (res != NULL) |
| 703 | return res; |
| 704 | } |
| 705 | return NULL; |
| 706 | } |
| 707 | |
| 708 | /* This is similar to PyObject_GenericGetAttr(), |
| 709 | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ |
| 710 | static PyObject * |
| 711 | type_getattro(PyTypeObject *type, PyObject *name) |
| 712 | { |
| 713 | PyTypeObject *metatype = type->ob_type; |
| 714 | PyObject *descr, *res; |
| 715 | descrgetfunc f; |
| 716 | |
| 717 | /* Initialize this type (we'll assume the metatype is initialized) */ |
| 718 | if (type->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 719 | if (PyType_Ready(type) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 720 | return NULL; |
| 721 | } |
| 722 | |
| 723 | /* Get a descriptor from the metatype */ |
| 724 | descr = _PyType_Lookup(metatype, name); |
| 725 | f = NULL; |
| 726 | if (descr != NULL) { |
| 727 | f = descr->ob_type->tp_descr_get; |
| 728 | if (f != NULL && PyDescr_IsData(descr)) |
| 729 | return f(descr, |
| 730 | (PyObject *)type, (PyObject *)metatype); |
| 731 | } |
| 732 | |
| 733 | /* Look in tp_defined of this type and its bases */ |
| 734 | res = _PyType_Lookup(type, name); |
| 735 | if (res != NULL) { |
| 736 | f = res->ob_type->tp_descr_get; |
| 737 | if (f != NULL) |
| 738 | return f(res, (PyObject *)NULL, (PyObject *)type); |
| 739 | Py_INCREF(res); |
| 740 | return res; |
| 741 | } |
| 742 | |
| 743 | /* Use the descriptor from the metatype */ |
| 744 | if (f != NULL) { |
| 745 | res = f(descr, (PyObject *)type, (PyObject *)metatype); |
| 746 | return res; |
| 747 | } |
| 748 | if (descr != NULL) { |
| 749 | Py_INCREF(descr); |
| 750 | return descr; |
| 751 | } |
| 752 | |
| 753 | /* Give up */ |
| 754 | PyErr_Format(PyExc_AttributeError, |
| 755 | "type object '%.50s' has no attribute '%.400s'", |
| 756 | type->tp_name, PyString_AS_STRING(name)); |
| 757 | return NULL; |
| 758 | } |
| 759 | |
| 760 | static int |
| 761 | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) |
| 762 | { |
| 763 | if (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE) |
| 764 | return PyObject_GenericSetAttr((PyObject *)type, name, value); |
| 765 | PyErr_SetString(PyExc_TypeError, "can't set type attributes"); |
| 766 | return -1; |
| 767 | } |
| 768 | |
| 769 | static void |
| 770 | type_dealloc(PyTypeObject *type) |
| 771 | { |
| 772 | etype *et; |
| 773 | |
| 774 | /* Assert this is a heap-allocated type object */ |
| 775 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
| 776 | et = (etype *)type; |
| 777 | Py_XDECREF(type->tp_base); |
| 778 | Py_XDECREF(type->tp_dict); |
| 779 | Py_XDECREF(type->tp_bases); |
| 780 | Py_XDECREF(type->tp_mro); |
| 781 | Py_XDECREF(type->tp_defined); |
| 782 | /* XXX more? */ |
| 783 | Py_XDECREF(et->name); |
| 784 | Py_XDECREF(et->slots); |
| 785 | type->ob_type->tp_free((PyObject *)type); |
| 786 | } |
| 787 | |
| 788 | static PyMethodDef type_methods[] = { |
| 789 | {"mro", mro_external, METH_VARARGS, |
| 790 | "mro() -> list\nreturn a type's method resolution order"}, |
| 791 | {0} |
| 792 | }; |
| 793 | |
| 794 | static char type_doc[] = |
| 795 | "type(object) -> the object's type\n" |
| 796 | "type(name, bases, dict) -> a new type"; |
| 797 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 798 | PyTypeObject PyType_Type = { |
| 799 | PyObject_HEAD_INIT(&PyType_Type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 800 | 0, /* ob_size */ |
| 801 | "type", /* tp_name */ |
| 802 | sizeof(etype), /* tp_basicsize */ |
| 803 | sizeof(struct memberlist), /* tp_itemsize */ |
| 804 | (destructor)type_dealloc, /* tp_dealloc */ |
| 805 | 0, /* tp_print */ |
| 806 | 0, /* tp_getattr */ |
| 807 | 0, /* tp_setattr */ |
| 808 | type_compare, /* tp_compare */ |
| 809 | (reprfunc)type_repr, /* tp_repr */ |
| 810 | 0, /* tp_as_number */ |
| 811 | 0, /* tp_as_sequence */ |
| 812 | 0, /* tp_as_mapping */ |
| 813 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
| 814 | (ternaryfunc)type_call, /* tp_call */ |
| 815 | 0, /* tp_str */ |
| 816 | (getattrofunc)type_getattro, /* tp_getattro */ |
| 817 | (setattrofunc)type_setattro, /* tp_setattro */ |
| 818 | 0, /* tp_as_buffer */ |
| 819 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 820 | type_doc, /* tp_doc */ |
| 821 | 0, /* tp_traverse */ |
| 822 | 0, /* tp_clear */ |
| 823 | 0, /* tp_richcompare */ |
| 824 | 0, /* tp_weaklistoffset */ |
| 825 | 0, /* tp_iter */ |
| 826 | 0, /* tp_iternext */ |
| 827 | type_methods, /* tp_methods */ |
| 828 | type_members, /* tp_members */ |
| 829 | type_getsets, /* tp_getset */ |
| 830 | 0, /* tp_base */ |
| 831 | 0, /* tp_dict */ |
| 832 | 0, /* tp_descr_get */ |
| 833 | 0, /* tp_descr_set */ |
| 834 | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ |
| 835 | 0, /* tp_init */ |
| 836 | 0, /* tp_alloc */ |
| 837 | type_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 838 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 839 | |
| 840 | |
| 841 | /* The base type of all types (eventually)... except itself. */ |
| 842 | |
| 843 | static int |
| 844 | object_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 845 | { |
| 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | static void |
| 850 | object_dealloc(PyObject *self) |
| 851 | { |
| 852 | self->ob_type->tp_free(self); |
| 853 | } |
| 854 | |
| 855 | static void |
| 856 | object_free(PyObject *self) |
| 857 | { |
| 858 | PyObject_Del(self); |
| 859 | } |
| 860 | |
| 861 | static struct memberlist object_members[] = { |
| 862 | {"__class__", T_OBJECT, offsetof(PyObject, ob_type), READONLY}, |
| 863 | {0} |
| 864 | }; |
| 865 | |
| 866 | PyTypeObject PyBaseObject_Type = { |
| 867 | PyObject_HEAD_INIT(&PyType_Type) |
| 868 | 0, /* ob_size */ |
| 869 | "object", /* tp_name */ |
| 870 | sizeof(PyObject), /* tp_basicsize */ |
| 871 | 0, /* tp_itemsize */ |
| 872 | (destructor)object_dealloc, /* tp_dealloc */ |
| 873 | 0, /* tp_print */ |
| 874 | 0, /* tp_getattr */ |
| 875 | 0, /* tp_setattr */ |
| 876 | 0, /* tp_compare */ |
| 877 | 0, /* tp_repr */ |
| 878 | 0, /* tp_as_number */ |
| 879 | 0, /* tp_as_sequence */ |
| 880 | 0, /* tp_as_mapping */ |
| 881 | 0, /* tp_hash */ |
| 882 | 0, /* tp_call */ |
| 883 | 0, /* tp_str */ |
| 884 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 885 | 0, /* tp_setattro */ |
| 886 | 0, /* tp_as_buffer */ |
| 887 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 888 | "The most base type", /* tp_doc */ |
| 889 | 0, /* tp_traverse */ |
| 890 | 0, /* tp_clear */ |
| 891 | 0, /* tp_richcompare */ |
| 892 | 0, /* tp_weaklistoffset */ |
| 893 | 0, /* tp_iter */ |
| 894 | 0, /* tp_iternext */ |
| 895 | 0, /* tp_methods */ |
| 896 | object_members, /* tp_members */ |
| 897 | 0, /* tp_getset */ |
| 898 | 0, /* tp_base */ |
| 899 | 0, /* tp_dict */ |
| 900 | 0, /* tp_descr_get */ |
| 901 | 0, /* tp_descr_set */ |
| 902 | 0, /* tp_dictoffset */ |
| 903 | object_init, /* tp_init */ |
| 904 | PyType_GenericAlloc, /* tp_alloc */ |
Guido van Rossum | c11e192 | 2001-08-09 19:38:15 +0000 | [diff] [blame^] | 905 | PyType_GenericNew, /* tp_new */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 906 | object_free, /* tp_free */ |
| 907 | }; |
| 908 | |
| 909 | |
| 910 | /* Initialize the __dict__ in a type object */ |
| 911 | |
| 912 | static int |
| 913 | add_methods(PyTypeObject *type, PyMethodDef *meth) |
| 914 | { |
| 915 | PyObject *dict = type->tp_defined; |
| 916 | |
| 917 | for (; meth->ml_name != NULL; meth++) { |
| 918 | PyObject *descr; |
| 919 | if (PyDict_GetItemString(dict, meth->ml_name)) |
| 920 | continue; |
| 921 | descr = PyDescr_NewMethod(type, meth); |
| 922 | if (descr == NULL) |
| 923 | return -1; |
| 924 | if (PyDict_SetItemString(dict,meth->ml_name,descr) < 0) |
| 925 | return -1; |
| 926 | Py_DECREF(descr); |
| 927 | } |
| 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | static int |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 932 | add_wrappers(PyTypeObject *type, struct wrapperbase *wraps, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 933 | { |
| 934 | PyObject *dict = type->tp_defined; |
| 935 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 936 | for (; wraps->name != NULL; wraps++) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 937 | PyObject *descr; |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 938 | if (PyDict_GetItemString(dict, wraps->name)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 939 | continue; |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 940 | descr = PyDescr_NewWrapper(type, wraps, wrapped); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 941 | if (descr == NULL) |
| 942 | return -1; |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 943 | if (PyDict_SetItemString(dict, wraps->name, descr) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 944 | return -1; |
| 945 | Py_DECREF(descr); |
| 946 | } |
| 947 | return 0; |
| 948 | } |
| 949 | |
| 950 | static int |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 951 | add_members(PyTypeObject *type, struct memberlist *memb) |
| 952 | { |
| 953 | PyObject *dict = type->tp_defined; |
| 954 | |
| 955 | for (; memb->name != NULL; memb++) { |
| 956 | PyObject *descr; |
| 957 | if (PyDict_GetItemString(dict, memb->name)) |
| 958 | continue; |
| 959 | descr = PyDescr_NewMember(type, memb); |
| 960 | if (descr == NULL) |
| 961 | return -1; |
| 962 | if (PyDict_SetItemString(dict, memb->name, descr) < 0) |
| 963 | return -1; |
| 964 | Py_DECREF(descr); |
| 965 | } |
| 966 | return 0; |
| 967 | } |
| 968 | |
| 969 | static int |
| 970 | add_getset(PyTypeObject *type, struct getsetlist *gsp) |
| 971 | { |
| 972 | PyObject *dict = type->tp_defined; |
| 973 | |
| 974 | for (; gsp->name != NULL; gsp++) { |
| 975 | PyObject *descr; |
| 976 | if (PyDict_GetItemString(dict, gsp->name)) |
| 977 | continue; |
| 978 | descr = PyDescr_NewGetSet(type, gsp); |
| 979 | |
| 980 | if (descr == NULL) |
| 981 | return -1; |
| 982 | if (PyDict_SetItemString(dict, gsp->name, descr) < 0) |
| 983 | return -1; |
| 984 | Py_DECREF(descr); |
| 985 | } |
| 986 | return 0; |
| 987 | } |
| 988 | |
| 989 | staticforward int add_operators(PyTypeObject *); |
| 990 | |
| 991 | static int |
| 992 | inherit_slots(PyTypeObject *type, PyTypeObject *base) |
| 993 | { |
| 994 | int oldsize, newsize; |
| 995 | |
| 996 | #undef COPYSLOT |
| 997 | #undef COPYNUM |
| 998 | #undef COPYSEQ |
| 999 | #undef COPYMAP |
| 1000 | #define COPYSLOT(SLOT) \ |
| 1001 | if (!type->SLOT) type->SLOT = base->SLOT |
| 1002 | |
| 1003 | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT) |
| 1004 | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT) |
| 1005 | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT) |
| 1006 | |
| 1007 | if (type->tp_as_number == NULL) |
| 1008 | type->tp_as_number = base->tp_as_number; |
| 1009 | else if (base->tp_as_number) { |
| 1010 | COPYNUM(nb_add); |
| 1011 | COPYNUM(nb_subtract); |
| 1012 | COPYNUM(nb_multiply); |
| 1013 | COPYNUM(nb_divide); |
| 1014 | COPYNUM(nb_remainder); |
| 1015 | COPYNUM(nb_divmod); |
| 1016 | COPYNUM(nb_power); |
| 1017 | COPYNUM(nb_negative); |
| 1018 | COPYNUM(nb_positive); |
| 1019 | COPYNUM(nb_absolute); |
| 1020 | COPYNUM(nb_nonzero); |
| 1021 | COPYNUM(nb_invert); |
| 1022 | COPYNUM(nb_lshift); |
| 1023 | COPYNUM(nb_rshift); |
| 1024 | COPYNUM(nb_and); |
| 1025 | COPYNUM(nb_xor); |
| 1026 | COPYNUM(nb_or); |
| 1027 | COPYNUM(nb_coerce); |
| 1028 | COPYNUM(nb_int); |
| 1029 | COPYNUM(nb_long); |
| 1030 | COPYNUM(nb_float); |
| 1031 | COPYNUM(nb_oct); |
| 1032 | COPYNUM(nb_hex); |
| 1033 | COPYNUM(nb_inplace_add); |
| 1034 | COPYNUM(nb_inplace_subtract); |
| 1035 | COPYNUM(nb_inplace_multiply); |
| 1036 | COPYNUM(nb_inplace_divide); |
| 1037 | COPYNUM(nb_inplace_remainder); |
| 1038 | COPYNUM(nb_inplace_power); |
| 1039 | COPYNUM(nb_inplace_lshift); |
| 1040 | COPYNUM(nb_inplace_rshift); |
| 1041 | COPYNUM(nb_inplace_and); |
| 1042 | COPYNUM(nb_inplace_xor); |
| 1043 | COPYNUM(nb_inplace_or); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 1044 | if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) { |
| 1045 | COPYNUM(nb_true_divide); |
| 1046 | COPYNUM(nb_floor_divide); |
| 1047 | COPYNUM(nb_inplace_true_divide); |
| 1048 | COPYNUM(nb_inplace_floor_divide); |
| 1049 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | if (type->tp_as_sequence == NULL) |
| 1053 | type->tp_as_sequence = base->tp_as_sequence; |
| 1054 | else if (base->tp_as_sequence) { |
| 1055 | COPYSEQ(sq_length); |
| 1056 | COPYSEQ(sq_concat); |
| 1057 | COPYSEQ(sq_repeat); |
| 1058 | COPYSEQ(sq_item); |
| 1059 | COPYSEQ(sq_slice); |
| 1060 | COPYSEQ(sq_ass_item); |
| 1061 | COPYSEQ(sq_ass_slice); |
| 1062 | COPYSEQ(sq_contains); |
| 1063 | COPYSEQ(sq_inplace_concat); |
| 1064 | COPYSEQ(sq_inplace_repeat); |
| 1065 | } |
| 1066 | |
| 1067 | if (type->tp_as_mapping == NULL) |
| 1068 | type->tp_as_mapping = base->tp_as_mapping; |
| 1069 | else if (base->tp_as_mapping) { |
| 1070 | COPYMAP(mp_length); |
| 1071 | COPYMAP(mp_subscript); |
| 1072 | COPYMAP(mp_ass_subscript); |
| 1073 | } |
| 1074 | |
| 1075 | /* Special flag magic */ |
| 1076 | if (!type->tp_as_buffer && base->tp_as_buffer) { |
| 1077 | type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER; |
| 1078 | type->tp_flags |= |
| 1079 | base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER; |
| 1080 | } |
| 1081 | if (!type->tp_as_sequence && base->tp_as_sequence) { |
| 1082 | type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN; |
| 1083 | type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN; |
| 1084 | } |
| 1085 | if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) != |
| 1086 | (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) { |
| 1087 | if ((!type->tp_as_number && base->tp_as_number) || |
| 1088 | (!type->tp_as_sequence && base->tp_as_sequence)) { |
| 1089 | type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS; |
| 1090 | if (!type->tp_as_number && !type->tp_as_sequence) { |
| 1091 | type->tp_flags |= base->tp_flags & |
| 1092 | Py_TPFLAGS_HAVE_INPLACEOPS; |
| 1093 | } |
| 1094 | } |
| 1095 | /* Wow */ |
| 1096 | } |
| 1097 | if (!type->tp_as_number && base->tp_as_number) { |
| 1098 | type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES; |
| 1099 | type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES; |
| 1100 | } |
| 1101 | |
| 1102 | /* Copying basicsize is connected to the GC flags */ |
| 1103 | oldsize = PyType_BASICSIZE(base); |
| 1104 | newsize = type->tp_basicsize ? PyType_BASICSIZE(type) : oldsize; |
| 1105 | if (!(type->tp_flags & Py_TPFLAGS_GC) && |
| 1106 | (base->tp_flags & Py_TPFLAGS_GC) && |
| 1107 | (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) && |
| 1108 | (!type->tp_traverse && !type->tp_clear)) { |
| 1109 | type->tp_flags |= Py_TPFLAGS_GC; |
| 1110 | COPYSLOT(tp_traverse); |
| 1111 | COPYSLOT(tp_clear); |
| 1112 | } |
| 1113 | PyType_SET_BASICSIZE(type, newsize); |
| 1114 | |
| 1115 | COPYSLOT(tp_itemsize); |
| 1116 | COPYSLOT(tp_dealloc); |
| 1117 | COPYSLOT(tp_print); |
| 1118 | if (type->tp_getattr == NULL && type->tp_getattro == NULL) { |
| 1119 | type->tp_getattr = base->tp_getattr; |
| 1120 | type->tp_getattro = base->tp_getattro; |
| 1121 | } |
| 1122 | if (type->tp_setattr == NULL && type->tp_setattro == NULL) { |
| 1123 | type->tp_setattr = base->tp_setattr; |
| 1124 | type->tp_setattro = base->tp_setattro; |
| 1125 | } |
| 1126 | /* tp_compare see tp_richcompare */ |
| 1127 | COPYSLOT(tp_repr); |
| 1128 | COPYSLOT(tp_hash); |
| 1129 | COPYSLOT(tp_call); |
| 1130 | COPYSLOT(tp_str); |
| 1131 | COPYSLOT(tp_as_buffer); |
| 1132 | COPYSLOT(tp_flags); |
| 1133 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) { |
| 1134 | if (type->tp_compare == NULL && type->tp_richcompare == NULL) { |
| 1135 | type->tp_compare = base->tp_compare; |
| 1136 | type->tp_richcompare = base->tp_richcompare; |
| 1137 | } |
| 1138 | } |
| 1139 | else { |
| 1140 | COPYSLOT(tp_compare); |
| 1141 | } |
| 1142 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) { |
| 1143 | COPYSLOT(tp_weaklistoffset); |
| 1144 | } |
| 1145 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) { |
| 1146 | COPYSLOT(tp_iter); |
| 1147 | COPYSLOT(tp_iternext); |
| 1148 | } |
| 1149 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
| 1150 | COPYSLOT(tp_descr_get); |
| 1151 | COPYSLOT(tp_descr_set); |
| 1152 | COPYSLOT(tp_dictoffset); |
| 1153 | COPYSLOT(tp_init); |
| 1154 | COPYSLOT(tp_alloc); |
Guido van Rossum | c11e192 | 2001-08-09 19:38:15 +0000 | [diff] [blame^] | 1155 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 1156 | COPYSLOT(tp_new); |
| 1157 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1158 | COPYSLOT(tp_free); |
| 1159 | } |
| 1160 | |
| 1161 | return 0; |
| 1162 | } |
| 1163 | |
| 1164 | int |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1165 | PyType_Ready(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1166 | { |
| 1167 | PyObject *dict, *bases, *x; |
| 1168 | PyTypeObject *base; |
| 1169 | int i, n; |
| 1170 | |
| 1171 | if (type->tp_dict != NULL) |
| 1172 | return 0; /* Already initialized */ |
| 1173 | |
| 1174 | /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
| 1175 | base = type->tp_base; |
| 1176 | if (base == NULL && type != &PyBaseObject_Type) |
| 1177 | base = type->tp_base = &PyBaseObject_Type; |
| 1178 | |
| 1179 | /* Initialize tp_bases */ |
| 1180 | bases = type->tp_bases; |
| 1181 | if (bases == NULL) { |
| 1182 | if (base == NULL) |
| 1183 | bases = PyTuple_New(0); |
| 1184 | else |
| 1185 | bases = Py_BuildValue("(O)", base); |
| 1186 | if (bases == NULL) |
| 1187 | return -1; |
| 1188 | type->tp_bases = bases; |
| 1189 | } |
| 1190 | |
| 1191 | /* Initialize the base class */ |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 1192 | if (base && base->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1193 | if (PyType_Ready(base) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1194 | return -1; |
| 1195 | } |
| 1196 | |
| 1197 | /* Initialize tp_defined */ |
| 1198 | dict = type->tp_defined; |
| 1199 | if (dict == NULL) { |
| 1200 | dict = PyDict_New(); |
| 1201 | if (dict == NULL) |
| 1202 | return -1; |
| 1203 | type->tp_defined = dict; |
| 1204 | } |
| 1205 | |
| 1206 | /* Add type-specific descriptors to tp_defined */ |
| 1207 | if (add_operators(type) < 0) |
| 1208 | return -1; |
| 1209 | if (type->tp_methods != NULL) { |
| 1210 | if (add_methods(type, type->tp_methods) < 0) |
| 1211 | return -1; |
| 1212 | } |
| 1213 | if (type->tp_members != NULL) { |
| 1214 | if (add_members(type, type->tp_members) < 0) |
| 1215 | return -1; |
| 1216 | } |
| 1217 | if (type->tp_getset != NULL) { |
| 1218 | if (add_getset(type, type->tp_getset) < 0) |
| 1219 | return -1; |
| 1220 | } |
| 1221 | |
| 1222 | /* Temporarily make tp_dict the same object as tp_defined. |
| 1223 | (This is needed to call mro(), and can stay this way for |
| 1224 | dynamic types). */ |
| 1225 | Py_INCREF(type->tp_defined); |
| 1226 | type->tp_dict = type->tp_defined; |
| 1227 | |
| 1228 | /* Calculate method resolution order */ |
| 1229 | if (mro_internal(type) < 0) { |
| 1230 | return -1; |
| 1231 | } |
| 1232 | |
| 1233 | /* Initialize tp_dict properly */ |
| 1234 | if (!PyType_HasFeature(type, Py_TPFLAGS_DYNAMICTYPE)) { |
| 1235 | /* For a static type, tp_dict is the consolidation |
| 1236 | of the tp_defined of its bases in MRO. Earlier |
| 1237 | bases override later bases; since d.update() works |
| 1238 | the other way, we walk the MRO sequence backwards. */ |
| 1239 | Py_DECREF(type->tp_dict); |
| 1240 | type->tp_dict = PyDict_New(); |
| 1241 | if (type->tp_dict == NULL) |
| 1242 | return -1; |
| 1243 | bases = type->tp_mro; |
| 1244 | assert(bases != NULL); |
| 1245 | assert(PyTuple_Check(bases)); |
| 1246 | n = PyTuple_GET_SIZE(bases); |
| 1247 | for (i = n; --i >= 0; ) { |
| 1248 | base = (PyTypeObject *)PyTuple_GET_ITEM(bases, i); |
| 1249 | assert(PyType_Check(base)); |
| 1250 | x = base->tp_defined; |
| 1251 | if (x != NULL && PyDict_Update(type->tp_dict, x) < 0) |
| 1252 | return -1; |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | /* Inherit slots from direct base */ |
| 1257 | if (type->tp_base != NULL) |
| 1258 | if (inherit_slots(type, type->tp_base) < 0) |
| 1259 | return -1; |
| 1260 | |
| 1261 | return 0; |
| 1262 | } |
| 1263 | |
| 1264 | |
| 1265 | /* Generic wrappers for overloadable 'operators' such as __getitem__ */ |
| 1266 | |
| 1267 | /* There's a wrapper *function* for each distinct function typedef used |
| 1268 | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a |
| 1269 | wrapper *table* for each distinct operation (e.g. __len__, __add__). |
| 1270 | Most tables have only one entry; the tables for binary operators have two |
| 1271 | entries, one regular and one with reversed arguments. */ |
| 1272 | |
| 1273 | static PyObject * |
| 1274 | wrap_inquiry(PyObject *self, PyObject *args, void *wrapped) |
| 1275 | { |
| 1276 | inquiry func = (inquiry)wrapped; |
| 1277 | int res; |
| 1278 | |
| 1279 | if (!PyArg_ParseTuple(args, "")) |
| 1280 | return NULL; |
| 1281 | res = (*func)(self); |
| 1282 | if (res == -1 && PyErr_Occurred()) |
| 1283 | return NULL; |
| 1284 | return PyInt_FromLong((long)res); |
| 1285 | } |
| 1286 | |
| 1287 | static struct wrapperbase tab_len[] = { |
| 1288 | {"__len__", (wrapperfunc)wrap_inquiry, "x.__len__() <==> len(x)"}, |
| 1289 | {0} |
| 1290 | }; |
| 1291 | |
| 1292 | static PyObject * |
| 1293 | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 1294 | { |
| 1295 | binaryfunc func = (binaryfunc)wrapped; |
| 1296 | PyObject *other; |
| 1297 | |
| 1298 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 1299 | return NULL; |
| 1300 | return (*func)(self, other); |
| 1301 | } |
| 1302 | |
| 1303 | static PyObject * |
| 1304 | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 1305 | { |
| 1306 | binaryfunc func = (binaryfunc)wrapped; |
| 1307 | PyObject *other; |
| 1308 | |
| 1309 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 1310 | return NULL; |
| 1311 | return (*func)(other, self); |
| 1312 | } |
| 1313 | |
| 1314 | #undef BINARY |
| 1315 | #define BINARY(NAME, OP) \ |
| 1316 | static struct wrapperbase tab_##NAME[] = { \ |
| 1317 | {"__" #NAME "__", \ |
| 1318 | (wrapperfunc)wrap_binaryfunc, \ |
| 1319 | "x.__" #NAME "__(y) <==> " #OP}, \ |
| 1320 | {"__r" #NAME "__", \ |
| 1321 | (wrapperfunc)wrap_binaryfunc_r, \ |
| 1322 | "y.__r" #NAME "__(x) <==> " #OP}, \ |
| 1323 | {0} \ |
| 1324 | } |
| 1325 | |
| 1326 | BINARY(add, "x+y"); |
| 1327 | BINARY(sub, "x-y"); |
| 1328 | BINARY(mul, "x*y"); |
| 1329 | BINARY(div, "x/y"); |
| 1330 | BINARY(mod, "x%y"); |
| 1331 | BINARY(divmod, "divmod(x,y)"); |
| 1332 | BINARY(lshift, "x<<y"); |
| 1333 | BINARY(rshift, "x>>y"); |
| 1334 | BINARY(and, "x&y"); |
| 1335 | BINARY(xor, "x^y"); |
| 1336 | BINARY(or, "x|y"); |
| 1337 | |
| 1338 | static PyObject * |
| 1339 | wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 1340 | { |
| 1341 | ternaryfunc func = (ternaryfunc)wrapped; |
| 1342 | PyObject *other; |
| 1343 | PyObject *third = Py_None; |
| 1344 | |
| 1345 | /* Note: This wrapper only works for __pow__() */ |
| 1346 | |
| 1347 | if (!PyArg_ParseTuple(args, "O|O", &other, &third)) |
| 1348 | return NULL; |
| 1349 | return (*func)(self, other, third); |
| 1350 | } |
| 1351 | |
| 1352 | #undef TERNARY |
| 1353 | #define TERNARY(NAME, OP) \ |
| 1354 | static struct wrapperbase tab_##NAME[] = { \ |
| 1355 | {"__" #NAME "__", \ |
| 1356 | (wrapperfunc)wrap_ternaryfunc, \ |
| 1357 | "x.__" #NAME "__(y, z) <==> " #OP}, \ |
| 1358 | {"__r" #NAME "__", \ |
| 1359 | (wrapperfunc)wrap_ternaryfunc, \ |
| 1360 | "y.__r" #NAME "__(x, z) <==> " #OP}, \ |
| 1361 | {0} \ |
| 1362 | } |
| 1363 | |
| 1364 | TERNARY(pow, "(x**y) % z"); |
| 1365 | |
| 1366 | #undef UNARY |
| 1367 | #define UNARY(NAME, OP) \ |
| 1368 | static struct wrapperbase tab_##NAME[] = { \ |
| 1369 | {"__" #NAME "__", \ |
| 1370 | (wrapperfunc)wrap_unaryfunc, \ |
| 1371 | "x.__" #NAME "__() <==> " #OP}, \ |
| 1372 | {0} \ |
| 1373 | } |
| 1374 | |
| 1375 | static PyObject * |
| 1376 | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 1377 | { |
| 1378 | unaryfunc func = (unaryfunc)wrapped; |
| 1379 | |
| 1380 | if (!PyArg_ParseTuple(args, "")) |
| 1381 | return NULL; |
| 1382 | return (*func)(self); |
| 1383 | } |
| 1384 | |
| 1385 | UNARY(neg, "-x"); |
| 1386 | UNARY(pos, "+x"); |
| 1387 | UNARY(abs, "abs(x)"); |
| 1388 | UNARY(nonzero, "x != 0"); |
| 1389 | UNARY(invert, "~x"); |
| 1390 | UNARY(int, "int(x)"); |
| 1391 | UNARY(long, "long(x)"); |
| 1392 | UNARY(float, "float(x)"); |
| 1393 | UNARY(oct, "oct(x)"); |
| 1394 | UNARY(hex, "hex(x)"); |
| 1395 | |
| 1396 | #undef IBINARY |
| 1397 | #define IBINARY(NAME, OP) \ |
| 1398 | static struct wrapperbase tab_##NAME[] = { \ |
| 1399 | {"__" #NAME "__", \ |
| 1400 | (wrapperfunc)wrap_binaryfunc, \ |
| 1401 | "x.__" #NAME "__(y) <==> " #OP}, \ |
| 1402 | {0} \ |
| 1403 | } |
| 1404 | |
| 1405 | IBINARY(iadd, "x+=y"); |
| 1406 | IBINARY(isub, "x-=y"); |
| 1407 | IBINARY(imul, "x*=y"); |
| 1408 | IBINARY(idiv, "x/=y"); |
| 1409 | IBINARY(imod, "x%=y"); |
| 1410 | IBINARY(ilshift, "x<<=y"); |
| 1411 | IBINARY(irshift, "x>>=y"); |
| 1412 | IBINARY(iand, "x&=y"); |
| 1413 | IBINARY(ixor, "x^=y"); |
| 1414 | IBINARY(ior, "x|=y"); |
| 1415 | |
| 1416 | #undef ITERNARY |
| 1417 | #define ITERNARY(NAME, OP) \ |
| 1418 | static struct wrapperbase tab_##NAME[] = { \ |
| 1419 | {"__" #NAME "__", \ |
| 1420 | (wrapperfunc)wrap_ternaryfunc, \ |
| 1421 | "x.__" #NAME "__(y) <==> " #OP}, \ |
| 1422 | {0} \ |
| 1423 | } |
| 1424 | |
| 1425 | ITERNARY(ipow, "x = (x**y) % z"); |
| 1426 | |
| 1427 | static struct wrapperbase tab_getitem[] = { |
| 1428 | {"__getitem__", (wrapperfunc)wrap_binaryfunc, |
| 1429 | "x.__getitem__(y) <==> x[y]"}, |
| 1430 | {0} |
| 1431 | }; |
| 1432 | |
| 1433 | static PyObject * |
| 1434 | wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped) |
| 1435 | { |
| 1436 | intargfunc func = (intargfunc)wrapped; |
| 1437 | int i; |
| 1438 | |
| 1439 | if (!PyArg_ParseTuple(args, "i", &i)) |
| 1440 | return NULL; |
| 1441 | return (*func)(self, i); |
| 1442 | } |
| 1443 | |
| 1444 | static struct wrapperbase tab_mul_int[] = { |
| 1445 | {"__mul__", (wrapperfunc)wrap_intargfunc, "x.__mul__(n) <==> x*n"}, |
| 1446 | {"__rmul__", (wrapperfunc)wrap_intargfunc, "x.__rmul__(n) <==> n*x"}, |
| 1447 | {0} |
| 1448 | }; |
| 1449 | |
| 1450 | static struct wrapperbase tab_concat[] = { |
| 1451 | {"__add__", (wrapperfunc)wrap_binaryfunc, "x.__add__(y) <==> x+y"}, |
| 1452 | {0} |
| 1453 | }; |
| 1454 | |
| 1455 | static struct wrapperbase tab_imul_int[] = { |
| 1456 | {"__imul__", (wrapperfunc)wrap_intargfunc, "x.__imul__(n) <==> x*=n"}, |
| 1457 | {0} |
| 1458 | }; |
| 1459 | |
| 1460 | static struct wrapperbase tab_getitem_int[] = { |
| 1461 | {"__getitem__", (wrapperfunc)wrap_intargfunc, |
| 1462 | "x.__getitem__(i) <==> x[i]"}, |
| 1463 | {0} |
| 1464 | }; |
| 1465 | |
| 1466 | static PyObject * |
| 1467 | wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped) |
| 1468 | { |
| 1469 | intintargfunc func = (intintargfunc)wrapped; |
| 1470 | int i, j; |
| 1471 | |
| 1472 | if (!PyArg_ParseTuple(args, "ii", &i, &j)) |
| 1473 | return NULL; |
| 1474 | return (*func)(self, i, j); |
| 1475 | } |
| 1476 | |
| 1477 | static struct wrapperbase tab_getslice[] = { |
| 1478 | {"__getslice__", (wrapperfunc)wrap_intintargfunc, |
| 1479 | "x.__getslice__(i, j) <==> x[i:j]"}, |
| 1480 | {0} |
| 1481 | }; |
| 1482 | |
| 1483 | static PyObject * |
| 1484 | wrap_intobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 1485 | { |
| 1486 | intobjargproc func = (intobjargproc)wrapped; |
| 1487 | int i, res; |
| 1488 | PyObject *value; |
| 1489 | |
| 1490 | if (!PyArg_ParseTuple(args, "iO", &i, &value)) |
| 1491 | return NULL; |
| 1492 | res = (*func)(self, i, value); |
| 1493 | if (res == -1 && PyErr_Occurred()) |
| 1494 | return NULL; |
| 1495 | Py_INCREF(Py_None); |
| 1496 | return Py_None; |
| 1497 | } |
| 1498 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 1499 | static PyObject * |
| 1500 | wrap_delitem_int(PyObject *self, PyObject *args, void *wrapped) |
| 1501 | { |
| 1502 | intobjargproc func = (intobjargproc)wrapped; |
| 1503 | int i, res; |
| 1504 | |
| 1505 | if (!PyArg_ParseTuple(args, "i", &i)) |
| 1506 | return NULL; |
| 1507 | res = (*func)(self, i, NULL); |
| 1508 | if (res == -1 && PyErr_Occurred()) |
| 1509 | return NULL; |
| 1510 | Py_INCREF(Py_None); |
| 1511 | return Py_None; |
| 1512 | } |
| 1513 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1514 | static struct wrapperbase tab_setitem_int[] = { |
| 1515 | {"__setitem__", (wrapperfunc)wrap_intobjargproc, |
| 1516 | "x.__setitem__(i, y) <==> x[i]=y"}, |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 1517 | {"__delitem__", (wrapperfunc)wrap_delitem_int, |
| 1518 | "x.__delitem__(y) <==> del x[y]"}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1519 | {0} |
| 1520 | }; |
| 1521 | |
| 1522 | static PyObject * |
| 1523 | wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 1524 | { |
| 1525 | intintobjargproc func = (intintobjargproc)wrapped; |
| 1526 | int i, j, res; |
| 1527 | PyObject *value; |
| 1528 | |
| 1529 | if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value)) |
| 1530 | return NULL; |
| 1531 | res = (*func)(self, i, j, value); |
| 1532 | if (res == -1 && PyErr_Occurred()) |
| 1533 | return NULL; |
| 1534 | Py_INCREF(Py_None); |
| 1535 | return Py_None; |
| 1536 | } |
| 1537 | |
| 1538 | static struct wrapperbase tab_setslice[] = { |
| 1539 | {"__setslice__", (wrapperfunc)wrap_intintobjargproc, |
| 1540 | "x.__setslice__(i, j, y) <==> x[i:j]=y"}, |
| 1541 | {0} |
| 1542 | }; |
| 1543 | |
| 1544 | /* XXX objobjproc is a misnomer; should be objargpred */ |
| 1545 | static PyObject * |
| 1546 | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) |
| 1547 | { |
| 1548 | objobjproc func = (objobjproc)wrapped; |
| 1549 | int res; |
| 1550 | PyObject *value; |
| 1551 | |
| 1552 | if (!PyArg_ParseTuple(args, "O", &value)) |
| 1553 | return NULL; |
| 1554 | res = (*func)(self, value); |
| 1555 | if (res == -1 && PyErr_Occurred()) |
| 1556 | return NULL; |
| 1557 | return PyInt_FromLong((long)res); |
| 1558 | } |
| 1559 | |
| 1560 | static struct wrapperbase tab_contains[] = { |
| 1561 | {"__contains__", (wrapperfunc)wrap_objobjproc, |
| 1562 | "x.__contains__(y) <==> y in x"}, |
| 1563 | {0} |
| 1564 | }; |
| 1565 | |
| 1566 | static PyObject * |
| 1567 | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 1568 | { |
| 1569 | objobjargproc func = (objobjargproc)wrapped; |
| 1570 | int res; |
| 1571 | PyObject *key, *value; |
| 1572 | |
| 1573 | if (!PyArg_ParseTuple(args, "OO", &key, &value)) |
| 1574 | return NULL; |
| 1575 | res = (*func)(self, key, value); |
| 1576 | if (res == -1 && PyErr_Occurred()) |
| 1577 | return NULL; |
| 1578 | Py_INCREF(Py_None); |
| 1579 | return Py_None; |
| 1580 | } |
| 1581 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 1582 | static PyObject * |
| 1583 | wrap_delitem(PyObject *self, PyObject *args, void *wrapped) |
| 1584 | { |
| 1585 | objobjargproc func = (objobjargproc)wrapped; |
| 1586 | int res; |
| 1587 | PyObject *key; |
| 1588 | |
| 1589 | if (!PyArg_ParseTuple(args, "O", &key)) |
| 1590 | return NULL; |
| 1591 | res = (*func)(self, key, NULL); |
| 1592 | if (res == -1 && PyErr_Occurred()) |
| 1593 | return NULL; |
| 1594 | Py_INCREF(Py_None); |
| 1595 | return Py_None; |
| 1596 | } |
| 1597 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1598 | static struct wrapperbase tab_setitem[] = { |
| 1599 | {"__setitem__", (wrapperfunc)wrap_objobjargproc, |
| 1600 | "x.__setitem__(y, z) <==> x[y]=z"}, |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 1601 | {"__delitem__", (wrapperfunc)wrap_delitem, |
| 1602 | "x.__delitem__(y) <==> del x[y]"}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1603 | {0} |
| 1604 | }; |
| 1605 | |
| 1606 | static PyObject * |
| 1607 | wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped) |
| 1608 | { |
| 1609 | cmpfunc func = (cmpfunc)wrapped; |
| 1610 | int res; |
| 1611 | PyObject *other; |
| 1612 | |
| 1613 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 1614 | return NULL; |
| 1615 | res = (*func)(self, other); |
| 1616 | if (PyErr_Occurred()) |
| 1617 | return NULL; |
| 1618 | return PyInt_FromLong((long)res); |
| 1619 | } |
| 1620 | |
| 1621 | static struct wrapperbase tab_cmp[] = { |
| 1622 | {"__cmp__", (wrapperfunc)wrap_cmpfunc, |
| 1623 | "x.__cmp__(y) <==> cmp(x,y)"}, |
| 1624 | {0} |
| 1625 | }; |
| 1626 | |
| 1627 | static struct wrapperbase tab_repr[] = { |
| 1628 | {"__repr__", (wrapperfunc)wrap_unaryfunc, |
| 1629 | "x.__repr__() <==> repr(x)"}, |
| 1630 | {0} |
| 1631 | }; |
| 1632 | |
| 1633 | static struct wrapperbase tab_getattr[] = { |
| 1634 | {"__getattr__", (wrapperfunc)wrap_binaryfunc, |
| 1635 | "x.__getattr__('name') <==> x.name"}, |
| 1636 | {0} |
| 1637 | }; |
| 1638 | |
| 1639 | static PyObject * |
| 1640 | wrap_setattr(PyObject *self, PyObject *args, void *wrapped) |
| 1641 | { |
| 1642 | setattrofunc func = (setattrofunc)wrapped; |
| 1643 | int res; |
| 1644 | PyObject *name, *value; |
| 1645 | |
| 1646 | if (!PyArg_ParseTuple(args, "OO", &name, &value)) |
| 1647 | return NULL; |
| 1648 | res = (*func)(self, name, value); |
| 1649 | if (res < 0) |
| 1650 | return NULL; |
| 1651 | Py_INCREF(Py_None); |
| 1652 | return Py_None; |
| 1653 | } |
| 1654 | |
| 1655 | static PyObject * |
| 1656 | wrap_delattr(PyObject *self, PyObject *args, void *wrapped) |
| 1657 | { |
| 1658 | setattrofunc func = (setattrofunc)wrapped; |
| 1659 | int res; |
| 1660 | PyObject *name; |
| 1661 | |
| 1662 | if (!PyArg_ParseTuple(args, "O", &name)) |
| 1663 | return NULL; |
| 1664 | res = (*func)(self, name, NULL); |
| 1665 | if (res < 0) |
| 1666 | return NULL; |
| 1667 | Py_INCREF(Py_None); |
| 1668 | return Py_None; |
| 1669 | } |
| 1670 | |
| 1671 | static struct wrapperbase tab_setattr[] = { |
| 1672 | {"__setattr__", (wrapperfunc)wrap_setattr, |
| 1673 | "x.__setattr__('name', value) <==> x.name = value"}, |
| 1674 | {"__delattr__", (wrapperfunc)wrap_delattr, |
| 1675 | "x.__delattr__('name') <==> del x.name"}, |
| 1676 | {0} |
| 1677 | }; |
| 1678 | |
| 1679 | static PyObject * |
| 1680 | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) |
| 1681 | { |
| 1682 | hashfunc func = (hashfunc)wrapped; |
| 1683 | long res; |
| 1684 | |
| 1685 | if (!PyArg_ParseTuple(args, "")) |
| 1686 | return NULL; |
| 1687 | res = (*func)(self); |
| 1688 | if (res == -1 && PyErr_Occurred()) |
| 1689 | return NULL; |
| 1690 | return PyInt_FromLong(res); |
| 1691 | } |
| 1692 | |
| 1693 | static struct wrapperbase tab_hash[] = { |
| 1694 | {"__hash__", (wrapperfunc)wrap_hashfunc, |
| 1695 | "x.__hash__() <==> hash(x)"}, |
| 1696 | {0} |
| 1697 | }; |
| 1698 | |
| 1699 | static PyObject * |
| 1700 | wrap_call(PyObject *self, PyObject *args, void *wrapped) |
| 1701 | { |
| 1702 | ternaryfunc func = (ternaryfunc)wrapped; |
| 1703 | |
| 1704 | /* XXX What about keyword arguments? */ |
| 1705 | return (*func)(self, args, NULL); |
| 1706 | } |
| 1707 | |
| 1708 | static struct wrapperbase tab_call[] = { |
| 1709 | {"__call__", (wrapperfunc)wrap_call, |
| 1710 | "x.__call__(...) <==> x(...)"}, |
| 1711 | {0} |
| 1712 | }; |
| 1713 | |
| 1714 | static struct wrapperbase tab_str[] = { |
| 1715 | {"__str__", (wrapperfunc)wrap_unaryfunc, |
| 1716 | "x.__str__() <==> str(x)"}, |
| 1717 | {0} |
| 1718 | }; |
| 1719 | |
| 1720 | static PyObject * |
| 1721 | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) |
| 1722 | { |
| 1723 | richcmpfunc func = (richcmpfunc)wrapped; |
| 1724 | PyObject *other; |
| 1725 | |
| 1726 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 1727 | return NULL; |
| 1728 | return (*func)(self, other, op); |
| 1729 | } |
| 1730 | |
| 1731 | #undef RICHCMP_WRAPPER |
| 1732 | #define RICHCMP_WRAPPER(NAME, OP) \ |
| 1733 | static PyObject * \ |
| 1734 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ |
| 1735 | { \ |
| 1736 | return wrap_richcmpfunc(self, args, wrapped, OP); \ |
| 1737 | } |
| 1738 | |
Jack Jansen | 8e938b4 | 2001-08-08 15:29:49 +0000 | [diff] [blame] | 1739 | RICHCMP_WRAPPER(lt, Py_LT) |
| 1740 | RICHCMP_WRAPPER(le, Py_LE) |
| 1741 | RICHCMP_WRAPPER(eq, Py_EQ) |
| 1742 | RICHCMP_WRAPPER(ne, Py_NE) |
| 1743 | RICHCMP_WRAPPER(gt, Py_GT) |
| 1744 | RICHCMP_WRAPPER(ge, Py_GE) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1745 | |
| 1746 | #undef RICHCMP_ENTRY |
| 1747 | #define RICHCMP_ENTRY(NAME, EXPR) \ |
| 1748 | {"__" #NAME "__", (wrapperfunc)richcmp_##NAME, \ |
| 1749 | "x.__" #NAME "__(y) <==> " EXPR} |
| 1750 | |
| 1751 | static struct wrapperbase tab_richcmp[] = { |
| 1752 | RICHCMP_ENTRY(lt, "x<y"), |
| 1753 | RICHCMP_ENTRY(le, "x<=y"), |
| 1754 | RICHCMP_ENTRY(eq, "x==y"), |
| 1755 | RICHCMP_ENTRY(ne, "x!=y"), |
| 1756 | RICHCMP_ENTRY(gt, "x>y"), |
| 1757 | RICHCMP_ENTRY(ge, "x>=y"), |
| 1758 | {0} |
| 1759 | }; |
| 1760 | |
| 1761 | static struct wrapperbase tab_iter[] = { |
| 1762 | {"__iter__", (wrapperfunc)wrap_unaryfunc, "x.__iter__() <==> iter(x)"}, |
| 1763 | {0} |
| 1764 | }; |
| 1765 | |
| 1766 | static PyObject * |
| 1767 | wrap_next(PyObject *self, PyObject *args, void *wrapped) |
| 1768 | { |
| 1769 | unaryfunc func = (unaryfunc)wrapped; |
| 1770 | PyObject *res; |
| 1771 | |
| 1772 | if (!PyArg_ParseTuple(args, "")) |
| 1773 | return NULL; |
| 1774 | res = (*func)(self); |
| 1775 | if (res == NULL && !PyErr_Occurred()) |
| 1776 | PyErr_SetNone(PyExc_StopIteration); |
| 1777 | return res; |
| 1778 | } |
| 1779 | |
| 1780 | static struct wrapperbase tab_next[] = { |
| 1781 | {"next", (wrapperfunc)wrap_next, |
| 1782 | "x.next() -> the next value, or raise StopIteration"}, |
| 1783 | {0} |
| 1784 | }; |
| 1785 | |
| 1786 | static PyObject * |
| 1787 | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped) |
| 1788 | { |
| 1789 | descrgetfunc func = (descrgetfunc)wrapped; |
| 1790 | PyObject *obj; |
| 1791 | PyObject *type = NULL; |
| 1792 | |
| 1793 | if (!PyArg_ParseTuple(args, "O|O", &obj, &type)) |
| 1794 | return NULL; |
| 1795 | if (type == NULL) |
| 1796 | type = (PyObject *)obj->ob_type; |
| 1797 | return (*func)(self, obj, type); |
| 1798 | } |
| 1799 | |
| 1800 | static struct wrapperbase tab_descr_get[] = { |
| 1801 | {"__get__", (wrapperfunc)wrap_descr_get, |
| 1802 | "descr.__get__(obj, type) -> value"}, |
| 1803 | {0} |
| 1804 | }; |
| 1805 | |
| 1806 | static PyObject * |
| 1807 | wrap_descrsetfunc(PyObject *self, PyObject *args, void *wrapped) |
| 1808 | { |
| 1809 | descrsetfunc func = (descrsetfunc)wrapped; |
| 1810 | PyObject *obj, *value; |
| 1811 | int ret; |
| 1812 | |
| 1813 | if (!PyArg_ParseTuple(args, "OO", &obj, &value)) |
| 1814 | return NULL; |
| 1815 | ret = (*func)(self, obj, value); |
| 1816 | if (ret < 0) |
| 1817 | return NULL; |
| 1818 | Py_INCREF(Py_None); |
| 1819 | return Py_None; |
| 1820 | } |
| 1821 | |
| 1822 | static struct wrapperbase tab_descr_set[] = { |
| 1823 | {"__set__", (wrapperfunc)wrap_descrsetfunc, |
| 1824 | "descr.__set__(obj, value)"}, |
| 1825 | {0} |
| 1826 | }; |
| 1827 | |
| 1828 | static PyObject * |
| 1829 | wrap_init(PyObject *self, PyObject *args, void *wrapped) |
| 1830 | { |
| 1831 | initproc func = (initproc)wrapped; |
| 1832 | |
| 1833 | /* XXX What about keyword arguments? */ |
| 1834 | if (func(self, args, NULL) < 0) |
| 1835 | return NULL; |
| 1836 | Py_INCREF(Py_None); |
| 1837 | return Py_None; |
| 1838 | } |
| 1839 | |
| 1840 | static struct wrapperbase tab_init[] = { |
| 1841 | {"__init__", (wrapperfunc)wrap_init, |
| 1842 | "x.__init__(...) initializes x; " |
| 1843 | "see x.__type__.__doc__ for signature"}, |
| 1844 | {0} |
| 1845 | }; |
| 1846 | |
| 1847 | static PyObject * |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 1848 | tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1849 | { |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 1850 | PyTypeObject *type, *subtype; |
| 1851 | PyObject *arg0, *res; |
| 1852 | |
| 1853 | if (self == NULL || !PyType_Check(self)) |
| 1854 | Py_FatalError("__new__() called with non-type 'self'"); |
| 1855 | type = (PyTypeObject *)self; |
| 1856 | if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) { |
| 1857 | PyErr_SetString(PyExc_TypeError, |
| 1858 | "T.__new__(): not enough arguments"); |
| 1859 | return NULL; |
| 1860 | } |
| 1861 | arg0 = PyTuple_GET_ITEM(args, 0); |
| 1862 | if (!PyType_Check(arg0)) { |
| 1863 | PyErr_SetString(PyExc_TypeError, |
| 1864 | "T.__new__(S): S is not a type object"); |
| 1865 | return NULL; |
| 1866 | } |
| 1867 | subtype = (PyTypeObject *)arg0; |
| 1868 | if (!PyType_IsSubtype(subtype, type)) { |
| 1869 | PyErr_SetString(PyExc_TypeError, |
| 1870 | "T.__new__(S): S is not a subtype of T"); |
| 1871 | return NULL; |
| 1872 | } |
| 1873 | args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 1874 | if (args == NULL) |
| 1875 | return NULL; |
| 1876 | res = type->tp_new(subtype, args, kwds); |
| 1877 | Py_DECREF(args); |
| 1878 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 1881 | static struct PyMethodDef tp_new_methoddef[] = { |
| 1882 | {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS, |
| 1883 | "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] | 1884 | {0} |
| 1885 | }; |
| 1886 | |
| 1887 | static int |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 1888 | add_tp_new_wrapper(PyTypeObject *type) |
| 1889 | { |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 1890 | PyObject *func; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 1891 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 1892 | if (PyDict_GetItemString(type->tp_defined, "__new__") != NULL) |
| 1893 | return 0; |
| 1894 | func = PyCFunction_New(tp_new_methoddef, (PyObject *)type); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 1895 | if (func == NULL) |
| 1896 | return -1; |
| 1897 | return PyDict_SetItemString(type->tp_defined, "__new__", func); |
| 1898 | } |
| 1899 | |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1900 | /* 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] | 1901 | dictionary with method descriptors for function slots. For each |
| 1902 | function slot (like tp_repr) that's defined in the type, one or |
| 1903 | more corresponding descriptors are added in the type's tp_defined |
| 1904 | dictionary under the appropriate name (like __repr__). Some |
| 1905 | function slots cause more than one descriptor to be added (for |
| 1906 | example, the nb_add slot adds both __add__ and __radd__ |
| 1907 | descriptors) and some function slots compete for the same |
| 1908 | descriptor (for example both sq_item and mp_subscript generate a |
| 1909 | __getitem__ descriptor). This only adds new descriptors and |
| 1910 | doesn't overwrite entries in tp_defined that were previously |
| 1911 | defined. The descriptors contain a reference to the C function |
| 1912 | they must call, so that it's safe if they are copied into a |
| 1913 | subtype's __dict__ and the subtype has a different C function in |
| 1914 | its slot -- calling the method defined by the descriptor will call |
| 1915 | the C function that was used to create it, rather than the C |
| 1916 | function present in the slot when it is called. (This is important |
| 1917 | because a subtype may have a C function in the slot that calls the |
| 1918 | method from the dictionary, and we want to avoid infinite recursion |
| 1919 | here.) */ |
| 1920 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 1921 | static int |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1922 | add_operators(PyTypeObject *type) |
| 1923 | { |
| 1924 | PySequenceMethods *sq; |
| 1925 | PyMappingMethods *mp; |
| 1926 | PyNumberMethods *nb; |
| 1927 | |
| 1928 | #undef ADD |
| 1929 | #define ADD(SLOT, TABLE) \ |
| 1930 | if (SLOT) { \ |
| 1931 | if (add_wrappers(type, TABLE, (void *)(SLOT)) < 0) \ |
| 1932 | return -1; \ |
| 1933 | } |
| 1934 | |
| 1935 | if ((sq = type->tp_as_sequence) != NULL) { |
| 1936 | ADD(sq->sq_length, tab_len); |
| 1937 | ADD(sq->sq_concat, tab_concat); |
| 1938 | ADD(sq->sq_repeat, tab_mul_int); |
| 1939 | ADD(sq->sq_item, tab_getitem_int); |
| 1940 | ADD(sq->sq_slice, tab_getslice); |
| 1941 | ADD(sq->sq_ass_item, tab_setitem_int); |
| 1942 | ADD(sq->sq_ass_slice, tab_setslice); |
| 1943 | ADD(sq->sq_contains, tab_contains); |
| 1944 | ADD(sq->sq_inplace_concat, tab_iadd); |
| 1945 | ADD(sq->sq_inplace_repeat, tab_imul_int); |
| 1946 | } |
| 1947 | |
| 1948 | if ((mp = type->tp_as_mapping) != NULL) { |
| 1949 | if (sq->sq_length == NULL) |
| 1950 | ADD(mp->mp_length, tab_len); |
| 1951 | ADD(mp->mp_subscript, tab_getitem); |
| 1952 | ADD(mp->mp_ass_subscript, tab_setitem); |
| 1953 | } |
| 1954 | |
| 1955 | /* We don't support "old-style numbers" because their binary |
| 1956 | operators require that both arguments have the same type; |
| 1957 | the wrappers here only work for new-style numbers. */ |
| 1958 | if ((type->tp_flags & Py_TPFLAGS_CHECKTYPES) && |
| 1959 | (nb = type->tp_as_number) != NULL) { |
| 1960 | ADD(nb->nb_add, tab_add); |
| 1961 | ADD(nb->nb_subtract, tab_sub); |
| 1962 | ADD(nb->nb_multiply, tab_mul); |
| 1963 | ADD(nb->nb_divide, tab_div); |
| 1964 | ADD(nb->nb_remainder, tab_mod); |
| 1965 | ADD(nb->nb_divmod, tab_divmod); |
| 1966 | ADD(nb->nb_power, tab_pow); |
| 1967 | ADD(nb->nb_negative, tab_neg); |
| 1968 | ADD(nb->nb_positive, tab_pos); |
| 1969 | ADD(nb->nb_absolute, tab_abs); |
| 1970 | ADD(nb->nb_nonzero, tab_nonzero); |
| 1971 | ADD(nb->nb_invert, tab_invert); |
| 1972 | ADD(nb->nb_lshift, tab_lshift); |
| 1973 | ADD(nb->nb_rshift, tab_rshift); |
| 1974 | ADD(nb->nb_and, tab_and); |
| 1975 | ADD(nb->nb_xor, tab_xor); |
| 1976 | ADD(nb->nb_or, tab_or); |
| 1977 | /* We don't support coerce() -- see above comment */ |
| 1978 | ADD(nb->nb_int, tab_int); |
| 1979 | ADD(nb->nb_long, tab_long); |
| 1980 | ADD(nb->nb_float, tab_float); |
| 1981 | ADD(nb->nb_oct, tab_oct); |
| 1982 | ADD(nb->nb_hex, tab_hex); |
| 1983 | ADD(nb->nb_inplace_add, tab_iadd); |
| 1984 | ADD(nb->nb_inplace_subtract, tab_isub); |
| 1985 | ADD(nb->nb_inplace_multiply, tab_imul); |
| 1986 | ADD(nb->nb_inplace_divide, tab_idiv); |
| 1987 | ADD(nb->nb_inplace_remainder, tab_imod); |
| 1988 | ADD(nb->nb_inplace_power, tab_ipow); |
| 1989 | ADD(nb->nb_inplace_lshift, tab_ilshift); |
| 1990 | ADD(nb->nb_inplace_rshift, tab_irshift); |
| 1991 | ADD(nb->nb_inplace_and, tab_iand); |
| 1992 | ADD(nb->nb_inplace_xor, tab_ixor); |
| 1993 | ADD(nb->nb_inplace_or, tab_ior); |
| 1994 | } |
| 1995 | |
| 1996 | ADD(type->tp_getattro, tab_getattr); |
| 1997 | ADD(type->tp_setattro, tab_setattr); |
| 1998 | ADD(type->tp_compare, tab_cmp); |
| 1999 | ADD(type->tp_repr, tab_repr); |
| 2000 | ADD(type->tp_hash, tab_hash); |
| 2001 | ADD(type->tp_call, tab_call); |
| 2002 | ADD(type->tp_str, tab_str); |
| 2003 | ADD(type->tp_richcompare, tab_richcmp); |
| 2004 | ADD(type->tp_iter, tab_iter); |
| 2005 | ADD(type->tp_iternext, tab_next); |
| 2006 | ADD(type->tp_descr_get, tab_descr_get); |
| 2007 | ADD(type->tp_descr_set, tab_descr_set); |
| 2008 | ADD(type->tp_init, tab_init); |
| 2009 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 2010 | if (type->tp_new != NULL) { |
| 2011 | if (add_tp_new_wrapper(type) < 0) |
| 2012 | return -1; |
| 2013 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2014 | |
| 2015 | return 0; |
| 2016 | } |
| 2017 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 2018 | /* Slot wrappers that call the corresponding __foo__ slot. See comments |
| 2019 | below at override_slots() for more explanation. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2020 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2021 | #define SLOT0(FUNCNAME, OPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2022 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2023 | FUNCNAME(PyObject *self) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2024 | { \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2025 | return PyObject_CallMethod(self, OPSTR, ""); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2028 | #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2029 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2030 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2031 | { \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2032 | return PyObject_CallMethod(self, OPSTR, ARGCODES, arg1); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2035 | |
| 2036 | #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2037 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2038 | FUNCNAME(PyObject *self, PyObject *other) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2039 | { \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2040 | if (self->ob_type->tp_as_number != NULL && \ |
| 2041 | self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \ |
| 2042 | PyObject *r; \ |
| 2043 | r = PyObject_CallMethod( \ |
| 2044 | self, OPSTR, "O", other); \ |
| 2045 | if (r != Py_NotImplemented || \ |
| 2046 | other->ob_type == self->ob_type) \ |
| 2047 | return r; \ |
| 2048 | Py_DECREF(r); \ |
| 2049 | } \ |
| 2050 | if (other->ob_type->tp_as_number != NULL && \ |
| 2051 | other->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \ |
| 2052 | return PyObject_CallMethod( \ |
| 2053 | other, ROPSTR, "O", self); \ |
| 2054 | } \ |
| 2055 | Py_INCREF(Py_NotImplemented); \ |
| 2056 | return Py_NotImplemented; \ |
| 2057 | } |
| 2058 | |
| 2059 | #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ |
| 2060 | SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR) |
| 2061 | |
| 2062 | #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \ |
| 2063 | static PyObject * \ |
| 2064 | FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ |
| 2065 | { \ |
| 2066 | return PyObject_CallMethod(self, OPSTR, ARGCODES, arg1, arg2); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | static int |
| 2070 | slot_sq_length(PyObject *self) |
| 2071 | { |
| 2072 | PyObject *res = PyObject_CallMethod(self, "__len__", ""); |
| 2073 | |
| 2074 | if (res == NULL) |
| 2075 | return -1; |
| 2076 | return (int)PyInt_AsLong(res); |
| 2077 | } |
| 2078 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2079 | SLOT1(slot_sq_concat, "__add__", PyObject *, "O") |
| 2080 | SLOT1(slot_sq_repeat, "__mul__", int, "i") |
| 2081 | SLOT1(slot_sq_item, "__getitem__", int, "i") |
| 2082 | SLOT2(slot_sq_slice, "__getslice__", int, int, "ii") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2083 | |
| 2084 | static int |
| 2085 | slot_sq_ass_item(PyObject *self, int index, PyObject *value) |
| 2086 | { |
| 2087 | PyObject *res; |
| 2088 | |
| 2089 | if (value == NULL) |
| 2090 | res = PyObject_CallMethod(self, "__delitem__", "i", index); |
| 2091 | else |
| 2092 | res = PyObject_CallMethod(self, "__setitem__", |
| 2093 | "iO", index, value); |
| 2094 | if (res == NULL) |
| 2095 | return -1; |
| 2096 | Py_DECREF(res); |
| 2097 | return 0; |
| 2098 | } |
| 2099 | |
| 2100 | static int |
| 2101 | slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value) |
| 2102 | { |
| 2103 | PyObject *res; |
| 2104 | |
| 2105 | if (value == NULL) |
| 2106 | res = PyObject_CallMethod(self, "__delslice__", "ii", i, j); |
| 2107 | else |
| 2108 | res = PyObject_CallMethod(self, "__setslice__", |
| 2109 | "iiO", i, j, value); |
| 2110 | if (res == NULL) |
| 2111 | return -1; |
| 2112 | Py_DECREF(res); |
| 2113 | return 0; |
| 2114 | } |
| 2115 | |
| 2116 | static int |
| 2117 | slot_sq_contains(PyObject *self, PyObject *value) |
| 2118 | { |
| 2119 | PyObject *res = PyObject_CallMethod(self, "__contains__", "O", value); |
| 2120 | int r; |
| 2121 | |
| 2122 | if (res == NULL) |
| 2123 | return -1; |
| 2124 | r = PyInt_AsLong(res); |
| 2125 | Py_DECREF(res); |
| 2126 | return r; |
| 2127 | } |
| 2128 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2129 | SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O") |
| 2130 | SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2131 | |
| 2132 | #define slot_mp_length slot_sq_length |
| 2133 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2134 | SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2135 | |
| 2136 | static int |
| 2137 | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) |
| 2138 | { |
| 2139 | PyObject *res; |
| 2140 | |
| 2141 | if (value == NULL) |
| 2142 | res = PyObject_CallMethod(self, "__delitem__", "O", key); |
| 2143 | else |
| 2144 | res = PyObject_CallMethod(self, "__setitem__", |
| 2145 | "OO", key, value); |
| 2146 | if (res == NULL) |
| 2147 | return -1; |
| 2148 | Py_DECREF(res); |
| 2149 | return 0; |
| 2150 | } |
| 2151 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2152 | SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__") |
| 2153 | SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__") |
| 2154 | SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__") |
| 2155 | SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__") |
| 2156 | SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__") |
| 2157 | SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__") |
| 2158 | |
| 2159 | staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); |
| 2160 | |
| 2161 | SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, |
| 2162 | nb_power, "__pow__", "__rpow__") |
| 2163 | |
| 2164 | static PyObject * |
| 2165 | slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) |
| 2166 | { |
| 2167 | if (modulus == Py_None) |
| 2168 | return slot_nb_power_binary(self, other); |
| 2169 | /* Three-arg power doesn't use __rpow__ */ |
| 2170 | return PyObject_CallMethod(self, "__pow__", "OO", other, modulus); |
| 2171 | } |
| 2172 | |
| 2173 | SLOT0(slot_nb_negative, "__neg__") |
| 2174 | SLOT0(slot_nb_positive, "__pos__") |
| 2175 | SLOT0(slot_nb_absolute, "__abs__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2176 | |
| 2177 | static int |
| 2178 | slot_nb_nonzero(PyObject *self) |
| 2179 | { |
| 2180 | PyObject *res = PyObject_CallMethod(self, "__nonzero__", ""); |
| 2181 | |
| 2182 | if (res == NULL) |
| 2183 | return -1; |
| 2184 | return (int)PyInt_AsLong(res); |
| 2185 | } |
| 2186 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2187 | SLOT0(slot_nb_invert, "__invert__") |
| 2188 | SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") |
| 2189 | SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") |
| 2190 | SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") |
| 2191 | SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") |
| 2192 | SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2193 | /* Not coerce() */ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2194 | SLOT0(slot_nb_int, "__int__") |
| 2195 | SLOT0(slot_nb_long, "__long__") |
| 2196 | SLOT0(slot_nb_float, "__float__") |
| 2197 | SLOT0(slot_nb_oct, "__oct__") |
| 2198 | SLOT0(slot_nb_hex, "__hex__") |
| 2199 | SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O") |
| 2200 | SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O") |
| 2201 | SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O") |
| 2202 | SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O") |
| 2203 | SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O") |
| 2204 | SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO") |
| 2205 | SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O") |
| 2206 | SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O") |
| 2207 | SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O") |
| 2208 | SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O") |
| 2209 | SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O") |
| 2210 | SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, |
| 2211 | "__floordiv__", "__rfloordiv__") |
| 2212 | SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__") |
| 2213 | SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O") |
| 2214 | SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2215 | |
| 2216 | static int |
| 2217 | slot_tp_compare(PyObject *self, PyObject *other) |
| 2218 | { |
| 2219 | PyObject *res = PyObject_CallMethod(self, "__cmp__", "O", other); |
| 2220 | long r; |
| 2221 | |
| 2222 | if (res == NULL) |
| 2223 | return -1; |
| 2224 | r = PyInt_AsLong(res); |
| 2225 | Py_DECREF(res); |
| 2226 | return (int)r; |
| 2227 | } |
| 2228 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2229 | SLOT0(slot_tp_repr, "__repr__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2230 | |
| 2231 | static long |
| 2232 | slot_tp_hash(PyObject *self) |
| 2233 | { |
| 2234 | PyObject *res = PyObject_CallMethod(self, "__hash__", ""); |
| 2235 | long h; |
| 2236 | |
| 2237 | if (res == NULL) |
| 2238 | return -1; |
| 2239 | h = PyInt_AsLong(res); |
| 2240 | if (h == -1 && !PyErr_Occurred()) |
| 2241 | h = -2; |
| 2242 | return h; |
| 2243 | } |
| 2244 | |
| 2245 | static PyObject * |
| 2246 | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 2247 | { |
| 2248 | PyObject *meth = PyObject_GetAttrString(self, "__call__"); |
| 2249 | PyObject *res; |
| 2250 | |
| 2251 | if (meth == NULL) |
| 2252 | return NULL; |
| 2253 | res = PyObject_Call(meth, args, kwds); |
| 2254 | Py_DECREF(meth); |
| 2255 | return res; |
| 2256 | } |
| 2257 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2258 | SLOT0(slot_tp_str, "__str__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2259 | |
| 2260 | static PyObject * |
| 2261 | slot_tp_getattro(PyObject *self, PyObject *name) |
| 2262 | { |
| 2263 | PyTypeObject *tp = self->ob_type; |
| 2264 | PyObject *dict = NULL; |
| 2265 | PyObject *getattr; |
| 2266 | |
| 2267 | if (tp->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 2268 | dict = tp->tp_dict; |
| 2269 | if (dict == NULL) { |
| 2270 | PyErr_Format(PyExc_SystemError, |
| 2271 | "'%.100s' type object has no __dict__???", |
| 2272 | tp->tp_name); |
| 2273 | return NULL; |
| 2274 | } |
| 2275 | getattr = PyDict_GetItemString(dict, "__getattr__"); |
| 2276 | if (getattr == NULL) { |
| 2277 | PyErr_SetString(PyExc_AttributeError, "__getattr__"); |
| 2278 | return NULL; |
| 2279 | } |
| 2280 | return PyObject_CallFunction(getattr, "OO", self, name); |
| 2281 | } |
| 2282 | |
| 2283 | static int |
| 2284 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) |
| 2285 | { |
| 2286 | PyObject *res; |
| 2287 | |
| 2288 | if (value == NULL) |
| 2289 | res = PyObject_CallMethod(self, "__delattr__", "O", name); |
| 2290 | else |
| 2291 | res = PyObject_CallMethod(self, "__setattr__", |
| 2292 | "OO", name, value); |
| 2293 | if (res == NULL) |
| 2294 | return -1; |
| 2295 | Py_DECREF(res); |
| 2296 | return 0; |
| 2297 | } |
| 2298 | |
| 2299 | /* Map rich comparison operators to their __xx__ namesakes */ |
| 2300 | static char *name_op[] = { |
| 2301 | "__lt__", |
| 2302 | "__le__", |
| 2303 | "__eq__", |
| 2304 | "__ne__", |
| 2305 | "__gt__", |
| 2306 | "__ge__", |
| 2307 | }; |
| 2308 | |
| 2309 | static PyObject * |
| 2310 | slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
| 2311 | { |
| 2312 | PyObject *meth = PyObject_GetAttrString(self, name_op[op]); |
| 2313 | PyObject *res; |
| 2314 | |
| 2315 | if (meth == NULL) |
| 2316 | return NULL; |
| 2317 | res = PyObject_CallFunction(meth, "O", other); |
| 2318 | Py_DECREF(meth); |
| 2319 | return res; |
| 2320 | } |
| 2321 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2322 | SLOT0(slot_tp_iter, "__iter__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2323 | |
| 2324 | static PyObject * |
| 2325 | slot_tp_iternext(PyObject *self) |
| 2326 | { |
| 2327 | return PyObject_CallMethod(self, "next", ""); |
| 2328 | } |
| 2329 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2330 | SLOT2(slot_tp_descr_get, "__get__", PyObject *, PyObject *, "OO") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2331 | |
| 2332 | static int |
| 2333 | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) |
| 2334 | { |
| 2335 | PyObject *res = PyObject_CallMethod(self, "__set__", |
| 2336 | "OO", target, value); |
| 2337 | if (res == NULL) |
| 2338 | return -1; |
| 2339 | Py_DECREF(res); |
| 2340 | return 0; |
| 2341 | } |
| 2342 | |
| 2343 | static int |
| 2344 | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2345 | { |
| 2346 | PyObject *meth = PyObject_GetAttrString(self, "__init__"); |
| 2347 | PyObject *res; |
| 2348 | |
| 2349 | if (meth == NULL) |
| 2350 | return -1; |
| 2351 | res = PyObject_Call(meth, args, kwds); |
| 2352 | Py_DECREF(meth); |
| 2353 | if (res == NULL) |
| 2354 | return -1; |
| 2355 | Py_DECREF(res); |
| 2356 | return 0; |
| 2357 | } |
| 2358 | |
| 2359 | static PyObject * |
| 2360 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2361 | { |
| 2362 | PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__"); |
| 2363 | PyObject *newargs, *x; |
| 2364 | int i, n; |
| 2365 | |
| 2366 | if (func == NULL) |
| 2367 | return NULL; |
| 2368 | assert(PyTuple_Check(args)); |
| 2369 | n = PyTuple_GET_SIZE(args); |
| 2370 | newargs = PyTuple_New(n+1); |
| 2371 | if (newargs == NULL) |
| 2372 | return NULL; |
| 2373 | Py_INCREF(type); |
| 2374 | PyTuple_SET_ITEM(newargs, 0, (PyObject *)type); |
| 2375 | for (i = 0; i < n; i++) { |
| 2376 | x = PyTuple_GET_ITEM(args, i); |
| 2377 | Py_INCREF(x); |
| 2378 | PyTuple_SET_ITEM(newargs, i+1, x); |
| 2379 | } |
| 2380 | x = PyObject_Call(func, newargs, kwds); |
| 2381 | Py_DECREF(func); |
| 2382 | return x; |
| 2383 | } |
| 2384 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 2385 | /* 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] | 2386 | PyType_Ready()) to complete the initialization of dynamic types. |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 2387 | The dict argument is the dictionary argument passed to type_new(), |
| 2388 | which is the local namespace of the class statement, in other |
| 2389 | words, it contains the methods. For each special method (like |
| 2390 | __repr__) defined in the dictionary, the corresponding function |
| 2391 | slot in the type object (like tp_repr) is set to a special function |
| 2392 | whose name is 'slot_' followed by the slot name and whose signature |
| 2393 | is whatever is required for that slot. These slot functions look |
| 2394 | up the corresponding method in the type's dictionary and call it. |
| 2395 | The slot functions have to take care of the various peculiarities |
| 2396 | of the mapping between slots and special methods, such as mapping |
| 2397 | one slot to multiple methods (tp_richcompare <--> __le__, __lt__ |
| 2398 | etc.) or mapping multiple slots to a single method (sq_item, |
| 2399 | mp_subscript <--> __getitem__). */ |
| 2400 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2401 | static void |
| 2402 | override_slots(PyTypeObject *type, PyObject *dict) |
| 2403 | { |
| 2404 | PySequenceMethods *sq = type->tp_as_sequence; |
| 2405 | PyMappingMethods *mp = type->tp_as_mapping; |
| 2406 | PyNumberMethods *nb = type->tp_as_number; |
| 2407 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2408 | #define SQSLOT(OPNAME, SLOTNAME, FUNCNAME) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2409 | if (PyDict_GetItemString(dict, OPNAME)) { \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2410 | sq->SLOTNAME = FUNCNAME; \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2413 | #define MPSLOT(OPNAME, SLOTNAME, FUNCNAME) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2414 | if (PyDict_GetItemString(dict, OPNAME)) { \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2415 | mp->SLOTNAME = FUNCNAME; \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2418 | #define NBSLOT(OPNAME, SLOTNAME, FUNCNAME) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2419 | if (PyDict_GetItemString(dict, OPNAME)) { \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2420 | nb->SLOTNAME = FUNCNAME; \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2421 | } |
| 2422 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2423 | #define TPSLOT(OPNAME, SLOTNAME, FUNCNAME) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2424 | if (PyDict_GetItemString(dict, OPNAME)) { \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2425 | type->SLOTNAME = FUNCNAME; \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2428 | SQSLOT("__len__", sq_length, slot_sq_length); |
| 2429 | SQSLOT("__add__", sq_concat, slot_sq_concat); |
| 2430 | SQSLOT("__mul__", sq_repeat, slot_sq_repeat); |
| 2431 | SQSLOT("__getitem__", sq_item, slot_sq_item); |
| 2432 | SQSLOT("__getslice__", sq_slice, slot_sq_slice); |
| 2433 | SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item); |
| 2434 | SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item); |
| 2435 | SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice); |
| 2436 | SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice); |
| 2437 | SQSLOT("__contains__", sq_contains, slot_sq_contains); |
| 2438 | SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat); |
| 2439 | SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2440 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2441 | MPSLOT("__len__", mp_length, slot_mp_length); |
| 2442 | MPSLOT("__getitem__", mp_subscript, slot_mp_subscript); |
| 2443 | MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript); |
| 2444 | MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2445 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2446 | NBSLOT("__add__", nb_add, slot_nb_add); |
| 2447 | NBSLOT("__sub__", nb_subtract, slot_nb_subtract); |
| 2448 | NBSLOT("__mul__", nb_multiply, slot_nb_multiply); |
| 2449 | NBSLOT("__div__", nb_divide, slot_nb_divide); |
| 2450 | NBSLOT("__mod__", nb_remainder, slot_nb_remainder); |
| 2451 | NBSLOT("__divmod__", nb_divmod, slot_nb_divmod); |
| 2452 | NBSLOT("__pow__", nb_power, slot_nb_power); |
| 2453 | NBSLOT("__neg__", nb_negative, slot_nb_negative); |
| 2454 | NBSLOT("__pos__", nb_positive, slot_nb_positive); |
| 2455 | NBSLOT("__abs__", nb_absolute, slot_nb_absolute); |
| 2456 | NBSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero); |
| 2457 | NBSLOT("__invert__", nb_invert, slot_nb_invert); |
| 2458 | NBSLOT("__lshift__", nb_lshift, slot_nb_lshift); |
| 2459 | NBSLOT("__rshift__", nb_rshift, slot_nb_rshift); |
| 2460 | NBSLOT("__and__", nb_and, slot_nb_and); |
| 2461 | NBSLOT("__xor__", nb_xor, slot_nb_xor); |
| 2462 | NBSLOT("__or__", nb_or, slot_nb_or); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2463 | /* Not coerce() */ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2464 | NBSLOT("__int__", nb_int, slot_nb_int); |
| 2465 | NBSLOT("__long__", nb_long, slot_nb_long); |
| 2466 | NBSLOT("__float__", nb_float, slot_nb_float); |
| 2467 | NBSLOT("__oct__", nb_oct, slot_nb_oct); |
| 2468 | NBSLOT("__hex__", nb_hex, slot_nb_hex); |
| 2469 | NBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add); |
| 2470 | NBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract); |
| 2471 | NBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply); |
| 2472 | NBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide); |
| 2473 | NBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder); |
| 2474 | NBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power); |
| 2475 | NBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift); |
| 2476 | NBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift); |
| 2477 | NBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and); |
| 2478 | NBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor); |
| 2479 | NBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or); |
| 2480 | NBSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide); |
| 2481 | NBSLOT("__truediv__", nb_true_divide, slot_nb_true_divide); |
| 2482 | NBSLOT("__ifloordiv__", nb_inplace_floor_divide, |
| 2483 | slot_nb_inplace_floor_divide); |
| 2484 | NBSLOT("__itruediv__", nb_inplace_true_divide, |
| 2485 | slot_nb_inplace_true_divide); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2486 | |
| 2487 | if (PyDict_GetItemString(dict, "__str__") || |
| 2488 | PyDict_GetItemString(dict, "__repr__")) |
| 2489 | type->tp_print = NULL; |
| 2490 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2491 | TPSLOT("__cmp__", tp_compare, slot_tp_compare); |
| 2492 | TPSLOT("__repr__", tp_repr, slot_tp_repr); |
| 2493 | TPSLOT("__hash__", tp_hash, slot_tp_hash); |
| 2494 | TPSLOT("__call__", tp_call, slot_tp_call); |
| 2495 | TPSLOT("__str__", tp_str, slot_tp_str); |
| 2496 | TPSLOT("__getattr__", tp_getattro, slot_tp_getattro); |
| 2497 | TPSLOT("__setattr__", tp_setattro, slot_tp_setattro); |
| 2498 | TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare); |
| 2499 | TPSLOT("__le__", tp_richcompare, slot_tp_richcompare); |
| 2500 | TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare); |
| 2501 | TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare); |
| 2502 | TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare); |
| 2503 | TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare); |
| 2504 | TPSLOT("__iter__", tp_iter, slot_tp_iter); |
| 2505 | TPSLOT("next", tp_iternext, slot_tp_iternext); |
| 2506 | TPSLOT("__get__", tp_descr_get, slot_tp_descr_get); |
| 2507 | TPSLOT("__set__", tp_descr_set, slot_tp_descr_set); |
| 2508 | TPSLOT("__init__", tp_init, slot_tp_init); |
| 2509 | TPSLOT("__new__", tp_new, slot_tp_new); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2510 | } |