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) { |
| 390 | if (PyType_InitDict(base_i) < 0) |
| 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 | |
| 585 | /* Initialize essential fields */ |
| 586 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | |
| 587 | Py_TPFLAGS_BASETYPE; |
| 588 | if (dynamic) |
| 589 | type->tp_flags |= Py_TPFLAGS_DYNAMICTYPE; |
| 590 | type->tp_as_number = &et->as_number; |
| 591 | type->tp_as_sequence = &et->as_sequence; |
| 592 | type->tp_as_mapping = &et->as_mapping; |
| 593 | type->tp_as_buffer = &et->as_buffer; |
| 594 | type->tp_name = PyString_AS_STRING(name); |
| 595 | |
| 596 | /* Set tp_base and tp_bases */ |
| 597 | type->tp_bases = bases; |
| 598 | Py_INCREF(base); |
| 599 | type->tp_base = base; |
| 600 | |
| 601 | /* Initialize tp_defined from passed-in dict */ |
| 602 | type->tp_defined = dict = PyDict_Copy(dict); |
| 603 | if (dict == NULL) { |
| 604 | Py_DECREF(type); |
| 605 | return NULL; |
| 606 | } |
| 607 | |
| 608 | /* Special-case __new__: if it's a plain function, |
| 609 | make it a static function */ |
| 610 | tmp = PyDict_GetItemString(dict, "__new__"); |
| 611 | if (tmp != NULL && PyFunction_Check(tmp)) { |
| 612 | tmp = PyStaticMethod_New(tmp); |
| 613 | if (tmp == NULL) { |
| 614 | Py_DECREF(type); |
| 615 | return NULL; |
| 616 | } |
| 617 | PyDict_SetItemString(dict, "__new__", tmp); |
| 618 | Py_DECREF(tmp); |
| 619 | } |
| 620 | |
| 621 | /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
| 622 | mp = et->members; |
| 623 | slotoffset = PyType_BASICSIZE(base); |
| 624 | if (slots != NULL) { |
| 625 | for (i = 0; i < nslots; i++, mp++) { |
| 626 | mp->name = PyString_AS_STRING( |
| 627 | PyTuple_GET_ITEM(slots, i)); |
| 628 | mp->type = T_OBJECT; |
| 629 | mp->offset = slotoffset; |
| 630 | slotoffset += sizeof(PyObject *); |
| 631 | } |
| 632 | } |
| 633 | else if (nslots) { |
| 634 | type->tp_dictoffset = slotoffset; |
| 635 | mp->name = "__dict__"; |
| 636 | mp->type = T_OBJECT; |
| 637 | mp->offset = slotoffset; |
| 638 | mp->readonly = 1; |
| 639 | slotoffset += sizeof(PyObject *); |
| 640 | } |
| 641 | type->tp_basicsize = slotoffset; |
| 642 | add_members(type, et->members); |
| 643 | |
| 644 | /* Special case some slots */ |
| 645 | if (type->tp_dictoffset != 0 || nslots > 0) { |
| 646 | if (base->tp_getattr == NULL && base->tp_getattro == NULL) |
| 647 | type->tp_getattro = PyObject_GenericGetAttr; |
| 648 | if (base->tp_setattr == NULL && base->tp_setattro == NULL) |
| 649 | type->tp_setattro = PyObject_GenericSetAttr; |
| 650 | } |
| 651 | type->tp_dealloc = subtype_dealloc; |
| 652 | |
| 653 | /* Always override allocation strategy to use regular heap */ |
| 654 | type->tp_alloc = PyType_GenericAlloc; |
| 655 | type->tp_free = _PyObject_Del; |
| 656 | |
| 657 | /* Initialize the rest */ |
| 658 | if (PyType_InitDict(type) < 0) { |
| 659 | Py_DECREF(type); |
| 660 | return NULL; |
| 661 | } |
| 662 | |
| 663 | /* Override slots that deserve it */ |
| 664 | override_slots(type, type->tp_defined); |
| 665 | return (PyObject *)type; |
| 666 | } |
| 667 | |
| 668 | /* Internal API to look for a name through the MRO. |
| 669 | This returns a borrowed reference, and doesn't set an exception! */ |
| 670 | PyObject * |
| 671 | _PyType_Lookup(PyTypeObject *type, PyObject *name) |
| 672 | { |
| 673 | int i, n; |
| 674 | PyObject *mro, *res, *dict; |
| 675 | |
| 676 | /* For static types, look in tp_dict */ |
| 677 | if (!(type->tp_flags & Py_TPFLAGS_DYNAMICTYPE)) { |
| 678 | dict = type->tp_dict; |
| 679 | assert(dict && PyDict_Check(dict)); |
| 680 | return PyDict_GetItem(dict, name); |
| 681 | } |
| 682 | |
| 683 | /* For dynamic types, look in tp_defined of types in MRO */ |
| 684 | mro = type->tp_mro; |
| 685 | assert(PyTuple_Check(mro)); |
| 686 | n = PyTuple_GET_SIZE(mro); |
| 687 | for (i = 0; i < n; i++) { |
| 688 | type = (PyTypeObject *) PyTuple_GET_ITEM(mro, i); |
| 689 | assert(PyType_Check(type)); |
| 690 | dict = type->tp_defined; |
| 691 | assert(dict && PyDict_Check(dict)); |
| 692 | res = PyDict_GetItem(dict, name); |
| 693 | if (res != NULL) |
| 694 | return res; |
| 695 | } |
| 696 | return NULL; |
| 697 | } |
| 698 | |
| 699 | /* This is similar to PyObject_GenericGetAttr(), |
| 700 | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ |
| 701 | static PyObject * |
| 702 | type_getattro(PyTypeObject *type, PyObject *name) |
| 703 | { |
| 704 | PyTypeObject *metatype = type->ob_type; |
| 705 | PyObject *descr, *res; |
| 706 | descrgetfunc f; |
| 707 | |
| 708 | /* Initialize this type (we'll assume the metatype is initialized) */ |
| 709 | if (type->tp_dict == NULL) { |
| 710 | if (PyType_InitDict(type) < 0) |
| 711 | return NULL; |
| 712 | } |
| 713 | |
| 714 | /* Get a descriptor from the metatype */ |
| 715 | descr = _PyType_Lookup(metatype, name); |
| 716 | f = NULL; |
| 717 | if (descr != NULL) { |
| 718 | f = descr->ob_type->tp_descr_get; |
| 719 | if (f != NULL && PyDescr_IsData(descr)) |
| 720 | return f(descr, |
| 721 | (PyObject *)type, (PyObject *)metatype); |
| 722 | } |
| 723 | |
| 724 | /* Look in tp_defined of this type and its bases */ |
| 725 | res = _PyType_Lookup(type, name); |
| 726 | if (res != NULL) { |
| 727 | f = res->ob_type->tp_descr_get; |
| 728 | if (f != NULL) |
| 729 | return f(res, (PyObject *)NULL, (PyObject *)type); |
| 730 | Py_INCREF(res); |
| 731 | return res; |
| 732 | } |
| 733 | |
| 734 | /* Use the descriptor from the metatype */ |
| 735 | if (f != NULL) { |
| 736 | res = f(descr, (PyObject *)type, (PyObject *)metatype); |
| 737 | return res; |
| 738 | } |
| 739 | if (descr != NULL) { |
| 740 | Py_INCREF(descr); |
| 741 | return descr; |
| 742 | } |
| 743 | |
| 744 | /* Give up */ |
| 745 | PyErr_Format(PyExc_AttributeError, |
| 746 | "type object '%.50s' has no attribute '%.400s'", |
| 747 | type->tp_name, PyString_AS_STRING(name)); |
| 748 | return NULL; |
| 749 | } |
| 750 | |
| 751 | static int |
| 752 | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) |
| 753 | { |
| 754 | if (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE) |
| 755 | return PyObject_GenericSetAttr((PyObject *)type, name, value); |
| 756 | PyErr_SetString(PyExc_TypeError, "can't set type attributes"); |
| 757 | return -1; |
| 758 | } |
| 759 | |
| 760 | static void |
| 761 | type_dealloc(PyTypeObject *type) |
| 762 | { |
| 763 | etype *et; |
| 764 | |
| 765 | /* Assert this is a heap-allocated type object */ |
| 766 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
| 767 | et = (etype *)type; |
| 768 | Py_XDECREF(type->tp_base); |
| 769 | Py_XDECREF(type->tp_dict); |
| 770 | Py_XDECREF(type->tp_bases); |
| 771 | Py_XDECREF(type->tp_mro); |
| 772 | Py_XDECREF(type->tp_defined); |
| 773 | /* XXX more? */ |
| 774 | Py_XDECREF(et->name); |
| 775 | Py_XDECREF(et->slots); |
| 776 | type->ob_type->tp_free((PyObject *)type); |
| 777 | } |
| 778 | |
| 779 | static PyMethodDef type_methods[] = { |
| 780 | {"mro", mro_external, METH_VARARGS, |
| 781 | "mro() -> list\nreturn a type's method resolution order"}, |
| 782 | {0} |
| 783 | }; |
| 784 | |
| 785 | static char type_doc[] = |
| 786 | "type(object) -> the object's type\n" |
| 787 | "type(name, bases, dict) -> a new type"; |
| 788 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 789 | PyTypeObject PyType_Type = { |
| 790 | PyObject_HEAD_INIT(&PyType_Type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 791 | 0, /* ob_size */ |
| 792 | "type", /* tp_name */ |
| 793 | sizeof(etype), /* tp_basicsize */ |
| 794 | sizeof(struct memberlist), /* tp_itemsize */ |
| 795 | (destructor)type_dealloc, /* tp_dealloc */ |
| 796 | 0, /* tp_print */ |
| 797 | 0, /* tp_getattr */ |
| 798 | 0, /* tp_setattr */ |
| 799 | type_compare, /* tp_compare */ |
| 800 | (reprfunc)type_repr, /* tp_repr */ |
| 801 | 0, /* tp_as_number */ |
| 802 | 0, /* tp_as_sequence */ |
| 803 | 0, /* tp_as_mapping */ |
| 804 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
| 805 | (ternaryfunc)type_call, /* tp_call */ |
| 806 | 0, /* tp_str */ |
| 807 | (getattrofunc)type_getattro, /* tp_getattro */ |
| 808 | (setattrofunc)type_setattro, /* tp_setattro */ |
| 809 | 0, /* tp_as_buffer */ |
| 810 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 811 | type_doc, /* tp_doc */ |
| 812 | 0, /* tp_traverse */ |
| 813 | 0, /* tp_clear */ |
| 814 | 0, /* tp_richcompare */ |
| 815 | 0, /* tp_weaklistoffset */ |
| 816 | 0, /* tp_iter */ |
| 817 | 0, /* tp_iternext */ |
| 818 | type_methods, /* tp_methods */ |
| 819 | type_members, /* tp_members */ |
| 820 | type_getsets, /* tp_getset */ |
| 821 | 0, /* tp_base */ |
| 822 | 0, /* tp_dict */ |
| 823 | 0, /* tp_descr_get */ |
| 824 | 0, /* tp_descr_set */ |
| 825 | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ |
| 826 | 0, /* tp_init */ |
| 827 | 0, /* tp_alloc */ |
| 828 | type_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 829 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 830 | |
| 831 | |
| 832 | /* The base type of all types (eventually)... except itself. */ |
| 833 | |
| 834 | static int |
| 835 | object_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 836 | { |
| 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | static void |
| 841 | object_dealloc(PyObject *self) |
| 842 | { |
| 843 | self->ob_type->tp_free(self); |
| 844 | } |
| 845 | |
| 846 | static void |
| 847 | object_free(PyObject *self) |
| 848 | { |
| 849 | PyObject_Del(self); |
| 850 | } |
| 851 | |
| 852 | static struct memberlist object_members[] = { |
| 853 | {"__class__", T_OBJECT, offsetof(PyObject, ob_type), READONLY}, |
| 854 | {0} |
| 855 | }; |
| 856 | |
| 857 | PyTypeObject PyBaseObject_Type = { |
| 858 | PyObject_HEAD_INIT(&PyType_Type) |
| 859 | 0, /* ob_size */ |
| 860 | "object", /* tp_name */ |
| 861 | sizeof(PyObject), /* tp_basicsize */ |
| 862 | 0, /* tp_itemsize */ |
| 863 | (destructor)object_dealloc, /* tp_dealloc */ |
| 864 | 0, /* tp_print */ |
| 865 | 0, /* tp_getattr */ |
| 866 | 0, /* tp_setattr */ |
| 867 | 0, /* tp_compare */ |
| 868 | 0, /* tp_repr */ |
| 869 | 0, /* tp_as_number */ |
| 870 | 0, /* tp_as_sequence */ |
| 871 | 0, /* tp_as_mapping */ |
| 872 | 0, /* tp_hash */ |
| 873 | 0, /* tp_call */ |
| 874 | 0, /* tp_str */ |
| 875 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 876 | 0, /* tp_setattro */ |
| 877 | 0, /* tp_as_buffer */ |
| 878 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 879 | "The most base type", /* tp_doc */ |
| 880 | 0, /* tp_traverse */ |
| 881 | 0, /* tp_clear */ |
| 882 | 0, /* tp_richcompare */ |
| 883 | 0, /* tp_weaklistoffset */ |
| 884 | 0, /* tp_iter */ |
| 885 | 0, /* tp_iternext */ |
| 886 | 0, /* tp_methods */ |
| 887 | object_members, /* tp_members */ |
| 888 | 0, /* tp_getset */ |
| 889 | 0, /* tp_base */ |
| 890 | 0, /* tp_dict */ |
| 891 | 0, /* tp_descr_get */ |
| 892 | 0, /* tp_descr_set */ |
| 893 | 0, /* tp_dictoffset */ |
| 894 | object_init, /* tp_init */ |
| 895 | PyType_GenericAlloc, /* tp_alloc */ |
| 896 | PyType_GenericNew, /* tp_new */ |
| 897 | object_free, /* tp_free */ |
| 898 | }; |
| 899 | |
| 900 | |
| 901 | /* Initialize the __dict__ in a type object */ |
| 902 | |
| 903 | static int |
| 904 | add_methods(PyTypeObject *type, PyMethodDef *meth) |
| 905 | { |
| 906 | PyObject *dict = type->tp_defined; |
| 907 | |
| 908 | for (; meth->ml_name != NULL; meth++) { |
| 909 | PyObject *descr; |
| 910 | if (PyDict_GetItemString(dict, meth->ml_name)) |
| 911 | continue; |
| 912 | descr = PyDescr_NewMethod(type, meth); |
| 913 | if (descr == NULL) |
| 914 | return -1; |
| 915 | if (PyDict_SetItemString(dict,meth->ml_name,descr) < 0) |
| 916 | return -1; |
| 917 | Py_DECREF(descr); |
| 918 | } |
| 919 | return 0; |
| 920 | } |
| 921 | |
| 922 | static int |
| 923 | add_wrappers(PyTypeObject *type, struct wrapperbase *base, void *wrapped) |
| 924 | { |
| 925 | PyObject *dict = type->tp_defined; |
| 926 | |
| 927 | for (; base->name != NULL; base++) { |
| 928 | PyObject *descr; |
| 929 | if (PyDict_GetItemString(dict, base->name)) |
| 930 | continue; |
| 931 | descr = PyDescr_NewWrapper(type, base, wrapped); |
| 932 | if (descr == NULL) |
| 933 | return -1; |
| 934 | if (PyDict_SetItemString(dict, base->name, descr) < 0) |
| 935 | return -1; |
| 936 | Py_DECREF(descr); |
| 937 | } |
| 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | static int |
| 942 | add_staticmethodwrappers(PyTypeObject *type, struct wrapperbase *base, |
| 943 | void *wrapped) |
| 944 | { |
| 945 | PyObject *dict = type->tp_defined; |
| 946 | PyObject *sm; |
| 947 | |
| 948 | for (; base->name != NULL; base++) { |
| 949 | PyObject *descr; |
| 950 | if (PyDict_GetItemString(dict, base->name)) |
| 951 | continue; |
| 952 | descr = PyDescr_NewWrapper(type->ob_type, base, wrapped); |
| 953 | if (descr == NULL) |
| 954 | return -1; |
| 955 | sm = PyStaticMethod_New(descr); |
| 956 | Py_DECREF(descr); |
| 957 | if (sm == NULL) |
| 958 | return -1; |
| 959 | if (PyDict_SetItemString(dict, base->name, sm) < 0) |
| 960 | return -1; |
| 961 | Py_DECREF(sm); |
| 962 | } |
| 963 | return 0; |
| 964 | } |
| 965 | |
| 966 | static int |
| 967 | add_members(PyTypeObject *type, struct memberlist *memb) |
| 968 | { |
| 969 | PyObject *dict = type->tp_defined; |
| 970 | |
| 971 | for (; memb->name != NULL; memb++) { |
| 972 | PyObject *descr; |
| 973 | if (PyDict_GetItemString(dict, memb->name)) |
| 974 | continue; |
| 975 | descr = PyDescr_NewMember(type, memb); |
| 976 | if (descr == NULL) |
| 977 | return -1; |
| 978 | if (PyDict_SetItemString(dict, memb->name, descr) < 0) |
| 979 | return -1; |
| 980 | Py_DECREF(descr); |
| 981 | } |
| 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | static int |
| 986 | add_getset(PyTypeObject *type, struct getsetlist *gsp) |
| 987 | { |
| 988 | PyObject *dict = type->tp_defined; |
| 989 | |
| 990 | for (; gsp->name != NULL; gsp++) { |
| 991 | PyObject *descr; |
| 992 | if (PyDict_GetItemString(dict, gsp->name)) |
| 993 | continue; |
| 994 | descr = PyDescr_NewGetSet(type, gsp); |
| 995 | |
| 996 | if (descr == NULL) |
| 997 | return -1; |
| 998 | if (PyDict_SetItemString(dict, gsp->name, descr) < 0) |
| 999 | return -1; |
| 1000 | Py_DECREF(descr); |
| 1001 | } |
| 1002 | return 0; |
| 1003 | } |
| 1004 | |
| 1005 | staticforward int add_operators(PyTypeObject *); |
| 1006 | |
| 1007 | static int |
| 1008 | inherit_slots(PyTypeObject *type, PyTypeObject *base) |
| 1009 | { |
| 1010 | int oldsize, newsize; |
| 1011 | |
| 1012 | #undef COPYSLOT |
| 1013 | #undef COPYNUM |
| 1014 | #undef COPYSEQ |
| 1015 | #undef COPYMAP |
| 1016 | #define COPYSLOT(SLOT) \ |
| 1017 | if (!type->SLOT) type->SLOT = base->SLOT |
| 1018 | |
| 1019 | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT) |
| 1020 | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT) |
| 1021 | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT) |
| 1022 | |
| 1023 | if (type->tp_as_number == NULL) |
| 1024 | type->tp_as_number = base->tp_as_number; |
| 1025 | else if (base->tp_as_number) { |
| 1026 | COPYNUM(nb_add); |
| 1027 | COPYNUM(nb_subtract); |
| 1028 | COPYNUM(nb_multiply); |
| 1029 | COPYNUM(nb_divide); |
| 1030 | COPYNUM(nb_remainder); |
| 1031 | COPYNUM(nb_divmod); |
| 1032 | COPYNUM(nb_power); |
| 1033 | COPYNUM(nb_negative); |
| 1034 | COPYNUM(nb_positive); |
| 1035 | COPYNUM(nb_absolute); |
| 1036 | COPYNUM(nb_nonzero); |
| 1037 | COPYNUM(nb_invert); |
| 1038 | COPYNUM(nb_lshift); |
| 1039 | COPYNUM(nb_rshift); |
| 1040 | COPYNUM(nb_and); |
| 1041 | COPYNUM(nb_xor); |
| 1042 | COPYNUM(nb_or); |
| 1043 | COPYNUM(nb_coerce); |
| 1044 | COPYNUM(nb_int); |
| 1045 | COPYNUM(nb_long); |
| 1046 | COPYNUM(nb_float); |
| 1047 | COPYNUM(nb_oct); |
| 1048 | COPYNUM(nb_hex); |
| 1049 | COPYNUM(nb_inplace_add); |
| 1050 | COPYNUM(nb_inplace_subtract); |
| 1051 | COPYNUM(nb_inplace_multiply); |
| 1052 | COPYNUM(nb_inplace_divide); |
| 1053 | COPYNUM(nb_inplace_remainder); |
| 1054 | COPYNUM(nb_inplace_power); |
| 1055 | COPYNUM(nb_inplace_lshift); |
| 1056 | COPYNUM(nb_inplace_rshift); |
| 1057 | COPYNUM(nb_inplace_and); |
| 1058 | COPYNUM(nb_inplace_xor); |
| 1059 | COPYNUM(nb_inplace_or); |
| 1060 | } |
| 1061 | |
| 1062 | if (type->tp_as_sequence == NULL) |
| 1063 | type->tp_as_sequence = base->tp_as_sequence; |
| 1064 | else if (base->tp_as_sequence) { |
| 1065 | COPYSEQ(sq_length); |
| 1066 | COPYSEQ(sq_concat); |
| 1067 | COPYSEQ(sq_repeat); |
| 1068 | COPYSEQ(sq_item); |
| 1069 | COPYSEQ(sq_slice); |
| 1070 | COPYSEQ(sq_ass_item); |
| 1071 | COPYSEQ(sq_ass_slice); |
| 1072 | COPYSEQ(sq_contains); |
| 1073 | COPYSEQ(sq_inplace_concat); |
| 1074 | COPYSEQ(sq_inplace_repeat); |
| 1075 | } |
| 1076 | |
| 1077 | if (type->tp_as_mapping == NULL) |
| 1078 | type->tp_as_mapping = base->tp_as_mapping; |
| 1079 | else if (base->tp_as_mapping) { |
| 1080 | COPYMAP(mp_length); |
| 1081 | COPYMAP(mp_subscript); |
| 1082 | COPYMAP(mp_ass_subscript); |
| 1083 | } |
| 1084 | |
| 1085 | /* Special flag magic */ |
| 1086 | if (!type->tp_as_buffer && base->tp_as_buffer) { |
| 1087 | type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER; |
| 1088 | type->tp_flags |= |
| 1089 | base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER; |
| 1090 | } |
| 1091 | if (!type->tp_as_sequence && base->tp_as_sequence) { |
| 1092 | type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN; |
| 1093 | type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN; |
| 1094 | } |
| 1095 | if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) != |
| 1096 | (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) { |
| 1097 | if ((!type->tp_as_number && base->tp_as_number) || |
| 1098 | (!type->tp_as_sequence && base->tp_as_sequence)) { |
| 1099 | type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS; |
| 1100 | if (!type->tp_as_number && !type->tp_as_sequence) { |
| 1101 | type->tp_flags |= base->tp_flags & |
| 1102 | Py_TPFLAGS_HAVE_INPLACEOPS; |
| 1103 | } |
| 1104 | } |
| 1105 | /* Wow */ |
| 1106 | } |
| 1107 | if (!type->tp_as_number && base->tp_as_number) { |
| 1108 | type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES; |
| 1109 | type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES; |
| 1110 | } |
| 1111 | |
| 1112 | /* Copying basicsize is connected to the GC flags */ |
| 1113 | oldsize = PyType_BASICSIZE(base); |
| 1114 | newsize = type->tp_basicsize ? PyType_BASICSIZE(type) : oldsize; |
| 1115 | if (!(type->tp_flags & Py_TPFLAGS_GC) && |
| 1116 | (base->tp_flags & Py_TPFLAGS_GC) && |
| 1117 | (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) && |
| 1118 | (!type->tp_traverse && !type->tp_clear)) { |
| 1119 | type->tp_flags |= Py_TPFLAGS_GC; |
| 1120 | COPYSLOT(tp_traverse); |
| 1121 | COPYSLOT(tp_clear); |
| 1122 | } |
| 1123 | PyType_SET_BASICSIZE(type, newsize); |
| 1124 | |
| 1125 | COPYSLOT(tp_itemsize); |
| 1126 | COPYSLOT(tp_dealloc); |
| 1127 | COPYSLOT(tp_print); |
| 1128 | if (type->tp_getattr == NULL && type->tp_getattro == NULL) { |
| 1129 | type->tp_getattr = base->tp_getattr; |
| 1130 | type->tp_getattro = base->tp_getattro; |
| 1131 | } |
| 1132 | if (type->tp_setattr == NULL && type->tp_setattro == NULL) { |
| 1133 | type->tp_setattr = base->tp_setattr; |
| 1134 | type->tp_setattro = base->tp_setattro; |
| 1135 | } |
| 1136 | /* tp_compare see tp_richcompare */ |
| 1137 | COPYSLOT(tp_repr); |
| 1138 | COPYSLOT(tp_hash); |
| 1139 | COPYSLOT(tp_call); |
| 1140 | COPYSLOT(tp_str); |
| 1141 | COPYSLOT(tp_as_buffer); |
| 1142 | COPYSLOT(tp_flags); |
| 1143 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) { |
| 1144 | if (type->tp_compare == NULL && type->tp_richcompare == NULL) { |
| 1145 | type->tp_compare = base->tp_compare; |
| 1146 | type->tp_richcompare = base->tp_richcompare; |
| 1147 | } |
| 1148 | } |
| 1149 | else { |
| 1150 | COPYSLOT(tp_compare); |
| 1151 | } |
| 1152 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) { |
| 1153 | COPYSLOT(tp_weaklistoffset); |
| 1154 | } |
| 1155 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) { |
| 1156 | COPYSLOT(tp_iter); |
| 1157 | COPYSLOT(tp_iternext); |
| 1158 | } |
| 1159 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
| 1160 | COPYSLOT(tp_descr_get); |
| 1161 | COPYSLOT(tp_descr_set); |
| 1162 | COPYSLOT(tp_dictoffset); |
| 1163 | COPYSLOT(tp_init); |
| 1164 | COPYSLOT(tp_alloc); |
| 1165 | COPYSLOT(tp_new); |
| 1166 | COPYSLOT(tp_free); |
| 1167 | } |
| 1168 | |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | int |
| 1173 | PyType_InitDict(PyTypeObject *type) |
| 1174 | { |
| 1175 | PyObject *dict, *bases, *x; |
| 1176 | PyTypeObject *base; |
| 1177 | int i, n; |
| 1178 | |
| 1179 | if (type->tp_dict != NULL) |
| 1180 | return 0; /* Already initialized */ |
| 1181 | |
| 1182 | /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
| 1183 | base = type->tp_base; |
| 1184 | if (base == NULL && type != &PyBaseObject_Type) |
| 1185 | base = type->tp_base = &PyBaseObject_Type; |
| 1186 | |
| 1187 | /* Initialize tp_bases */ |
| 1188 | bases = type->tp_bases; |
| 1189 | if (bases == NULL) { |
| 1190 | if (base == NULL) |
| 1191 | bases = PyTuple_New(0); |
| 1192 | else |
| 1193 | bases = Py_BuildValue("(O)", base); |
| 1194 | if (bases == NULL) |
| 1195 | return -1; |
| 1196 | type->tp_bases = bases; |
| 1197 | } |
| 1198 | |
| 1199 | /* Initialize the base class */ |
| 1200 | if (base) { |
| 1201 | if (PyType_InitDict(base) < 0) |
| 1202 | return -1; |
| 1203 | } |
| 1204 | |
| 1205 | /* Initialize tp_defined */ |
| 1206 | dict = type->tp_defined; |
| 1207 | if (dict == NULL) { |
| 1208 | dict = PyDict_New(); |
| 1209 | if (dict == NULL) |
| 1210 | return -1; |
| 1211 | type->tp_defined = dict; |
| 1212 | } |
| 1213 | |
| 1214 | /* Add type-specific descriptors to tp_defined */ |
| 1215 | if (add_operators(type) < 0) |
| 1216 | return -1; |
| 1217 | if (type->tp_methods != NULL) { |
| 1218 | if (add_methods(type, type->tp_methods) < 0) |
| 1219 | return -1; |
| 1220 | } |
| 1221 | if (type->tp_members != NULL) { |
| 1222 | if (add_members(type, type->tp_members) < 0) |
| 1223 | return -1; |
| 1224 | } |
| 1225 | if (type->tp_getset != NULL) { |
| 1226 | if (add_getset(type, type->tp_getset) < 0) |
| 1227 | return -1; |
| 1228 | } |
| 1229 | |
| 1230 | /* Temporarily make tp_dict the same object as tp_defined. |
| 1231 | (This is needed to call mro(), and can stay this way for |
| 1232 | dynamic types). */ |
| 1233 | Py_INCREF(type->tp_defined); |
| 1234 | type->tp_dict = type->tp_defined; |
| 1235 | |
| 1236 | /* Calculate method resolution order */ |
| 1237 | if (mro_internal(type) < 0) { |
| 1238 | return -1; |
| 1239 | } |
| 1240 | |
| 1241 | /* Initialize tp_dict properly */ |
| 1242 | if (!PyType_HasFeature(type, Py_TPFLAGS_DYNAMICTYPE)) { |
| 1243 | /* For a static type, tp_dict is the consolidation |
| 1244 | of the tp_defined of its bases in MRO. Earlier |
| 1245 | bases override later bases; since d.update() works |
| 1246 | the other way, we walk the MRO sequence backwards. */ |
| 1247 | Py_DECREF(type->tp_dict); |
| 1248 | type->tp_dict = PyDict_New(); |
| 1249 | if (type->tp_dict == NULL) |
| 1250 | return -1; |
| 1251 | bases = type->tp_mro; |
| 1252 | assert(bases != NULL); |
| 1253 | assert(PyTuple_Check(bases)); |
| 1254 | n = PyTuple_GET_SIZE(bases); |
| 1255 | for (i = n; --i >= 0; ) { |
| 1256 | base = (PyTypeObject *)PyTuple_GET_ITEM(bases, i); |
| 1257 | assert(PyType_Check(base)); |
| 1258 | x = base->tp_defined; |
| 1259 | if (x != NULL && PyDict_Update(type->tp_dict, x) < 0) |
| 1260 | return -1; |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | /* Inherit slots from direct base */ |
| 1265 | if (type->tp_base != NULL) |
| 1266 | if (inherit_slots(type, type->tp_base) < 0) |
| 1267 | return -1; |
| 1268 | |
| 1269 | return 0; |
| 1270 | } |
| 1271 | |
| 1272 | |
| 1273 | /* Generic wrappers for overloadable 'operators' such as __getitem__ */ |
| 1274 | |
| 1275 | /* There's a wrapper *function* for each distinct function typedef used |
| 1276 | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a |
| 1277 | wrapper *table* for each distinct operation (e.g. __len__, __add__). |
| 1278 | Most tables have only one entry; the tables for binary operators have two |
| 1279 | entries, one regular and one with reversed arguments. */ |
| 1280 | |
| 1281 | static PyObject * |
| 1282 | wrap_inquiry(PyObject *self, PyObject *args, void *wrapped) |
| 1283 | { |
| 1284 | inquiry func = (inquiry)wrapped; |
| 1285 | int res; |
| 1286 | |
| 1287 | if (!PyArg_ParseTuple(args, "")) |
| 1288 | return NULL; |
| 1289 | res = (*func)(self); |
| 1290 | if (res == -1 && PyErr_Occurred()) |
| 1291 | return NULL; |
| 1292 | return PyInt_FromLong((long)res); |
| 1293 | } |
| 1294 | |
| 1295 | static struct wrapperbase tab_len[] = { |
| 1296 | {"__len__", (wrapperfunc)wrap_inquiry, "x.__len__() <==> len(x)"}, |
| 1297 | {0} |
| 1298 | }; |
| 1299 | |
| 1300 | static PyObject * |
| 1301 | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 1302 | { |
| 1303 | binaryfunc func = (binaryfunc)wrapped; |
| 1304 | PyObject *other; |
| 1305 | |
| 1306 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 1307 | return NULL; |
| 1308 | return (*func)(self, other); |
| 1309 | } |
| 1310 | |
| 1311 | static PyObject * |
| 1312 | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 1313 | { |
| 1314 | binaryfunc func = (binaryfunc)wrapped; |
| 1315 | PyObject *other; |
| 1316 | |
| 1317 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 1318 | return NULL; |
| 1319 | return (*func)(other, self); |
| 1320 | } |
| 1321 | |
| 1322 | #undef BINARY |
| 1323 | #define BINARY(NAME, OP) \ |
| 1324 | static struct wrapperbase tab_##NAME[] = { \ |
| 1325 | {"__" #NAME "__", \ |
| 1326 | (wrapperfunc)wrap_binaryfunc, \ |
| 1327 | "x.__" #NAME "__(y) <==> " #OP}, \ |
| 1328 | {"__r" #NAME "__", \ |
| 1329 | (wrapperfunc)wrap_binaryfunc_r, \ |
| 1330 | "y.__r" #NAME "__(x) <==> " #OP}, \ |
| 1331 | {0} \ |
| 1332 | } |
| 1333 | |
| 1334 | BINARY(add, "x+y"); |
| 1335 | BINARY(sub, "x-y"); |
| 1336 | BINARY(mul, "x*y"); |
| 1337 | BINARY(div, "x/y"); |
| 1338 | BINARY(mod, "x%y"); |
| 1339 | BINARY(divmod, "divmod(x,y)"); |
| 1340 | BINARY(lshift, "x<<y"); |
| 1341 | BINARY(rshift, "x>>y"); |
| 1342 | BINARY(and, "x&y"); |
| 1343 | BINARY(xor, "x^y"); |
| 1344 | BINARY(or, "x|y"); |
| 1345 | |
| 1346 | static PyObject * |
| 1347 | wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 1348 | { |
| 1349 | ternaryfunc func = (ternaryfunc)wrapped; |
| 1350 | PyObject *other; |
| 1351 | PyObject *third = Py_None; |
| 1352 | |
| 1353 | /* Note: This wrapper only works for __pow__() */ |
| 1354 | |
| 1355 | if (!PyArg_ParseTuple(args, "O|O", &other, &third)) |
| 1356 | return NULL; |
| 1357 | return (*func)(self, other, third); |
| 1358 | } |
| 1359 | |
| 1360 | #undef TERNARY |
| 1361 | #define TERNARY(NAME, OP) \ |
| 1362 | static struct wrapperbase tab_##NAME[] = { \ |
| 1363 | {"__" #NAME "__", \ |
| 1364 | (wrapperfunc)wrap_ternaryfunc, \ |
| 1365 | "x.__" #NAME "__(y, z) <==> " #OP}, \ |
| 1366 | {"__r" #NAME "__", \ |
| 1367 | (wrapperfunc)wrap_ternaryfunc, \ |
| 1368 | "y.__r" #NAME "__(x, z) <==> " #OP}, \ |
| 1369 | {0} \ |
| 1370 | } |
| 1371 | |
| 1372 | TERNARY(pow, "(x**y) % z"); |
| 1373 | |
| 1374 | #undef UNARY |
| 1375 | #define UNARY(NAME, OP) \ |
| 1376 | static struct wrapperbase tab_##NAME[] = { \ |
| 1377 | {"__" #NAME "__", \ |
| 1378 | (wrapperfunc)wrap_unaryfunc, \ |
| 1379 | "x.__" #NAME "__() <==> " #OP}, \ |
| 1380 | {0} \ |
| 1381 | } |
| 1382 | |
| 1383 | static PyObject * |
| 1384 | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 1385 | { |
| 1386 | unaryfunc func = (unaryfunc)wrapped; |
| 1387 | |
| 1388 | if (!PyArg_ParseTuple(args, "")) |
| 1389 | return NULL; |
| 1390 | return (*func)(self); |
| 1391 | } |
| 1392 | |
| 1393 | UNARY(neg, "-x"); |
| 1394 | UNARY(pos, "+x"); |
| 1395 | UNARY(abs, "abs(x)"); |
| 1396 | UNARY(nonzero, "x != 0"); |
| 1397 | UNARY(invert, "~x"); |
| 1398 | UNARY(int, "int(x)"); |
| 1399 | UNARY(long, "long(x)"); |
| 1400 | UNARY(float, "float(x)"); |
| 1401 | UNARY(oct, "oct(x)"); |
| 1402 | UNARY(hex, "hex(x)"); |
| 1403 | |
| 1404 | #undef IBINARY |
| 1405 | #define IBINARY(NAME, OP) \ |
| 1406 | static struct wrapperbase tab_##NAME[] = { \ |
| 1407 | {"__" #NAME "__", \ |
| 1408 | (wrapperfunc)wrap_binaryfunc, \ |
| 1409 | "x.__" #NAME "__(y) <==> " #OP}, \ |
| 1410 | {0} \ |
| 1411 | } |
| 1412 | |
| 1413 | IBINARY(iadd, "x+=y"); |
| 1414 | IBINARY(isub, "x-=y"); |
| 1415 | IBINARY(imul, "x*=y"); |
| 1416 | IBINARY(idiv, "x/=y"); |
| 1417 | IBINARY(imod, "x%=y"); |
| 1418 | IBINARY(ilshift, "x<<=y"); |
| 1419 | IBINARY(irshift, "x>>=y"); |
| 1420 | IBINARY(iand, "x&=y"); |
| 1421 | IBINARY(ixor, "x^=y"); |
| 1422 | IBINARY(ior, "x|=y"); |
| 1423 | |
| 1424 | #undef ITERNARY |
| 1425 | #define ITERNARY(NAME, OP) \ |
| 1426 | static struct wrapperbase tab_##NAME[] = { \ |
| 1427 | {"__" #NAME "__", \ |
| 1428 | (wrapperfunc)wrap_ternaryfunc, \ |
| 1429 | "x.__" #NAME "__(y) <==> " #OP}, \ |
| 1430 | {0} \ |
| 1431 | } |
| 1432 | |
| 1433 | ITERNARY(ipow, "x = (x**y) % z"); |
| 1434 | |
| 1435 | static struct wrapperbase tab_getitem[] = { |
| 1436 | {"__getitem__", (wrapperfunc)wrap_binaryfunc, |
| 1437 | "x.__getitem__(y) <==> x[y]"}, |
| 1438 | {0} |
| 1439 | }; |
| 1440 | |
| 1441 | static PyObject * |
| 1442 | wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped) |
| 1443 | { |
| 1444 | intargfunc func = (intargfunc)wrapped; |
| 1445 | int i; |
| 1446 | |
| 1447 | if (!PyArg_ParseTuple(args, "i", &i)) |
| 1448 | return NULL; |
| 1449 | return (*func)(self, i); |
| 1450 | } |
| 1451 | |
| 1452 | static struct wrapperbase tab_mul_int[] = { |
| 1453 | {"__mul__", (wrapperfunc)wrap_intargfunc, "x.__mul__(n) <==> x*n"}, |
| 1454 | {"__rmul__", (wrapperfunc)wrap_intargfunc, "x.__rmul__(n) <==> n*x"}, |
| 1455 | {0} |
| 1456 | }; |
| 1457 | |
| 1458 | static struct wrapperbase tab_concat[] = { |
| 1459 | {"__add__", (wrapperfunc)wrap_binaryfunc, "x.__add__(y) <==> x+y"}, |
| 1460 | {0} |
| 1461 | }; |
| 1462 | |
| 1463 | static struct wrapperbase tab_imul_int[] = { |
| 1464 | {"__imul__", (wrapperfunc)wrap_intargfunc, "x.__imul__(n) <==> x*=n"}, |
| 1465 | {0} |
| 1466 | }; |
| 1467 | |
| 1468 | static struct wrapperbase tab_getitem_int[] = { |
| 1469 | {"__getitem__", (wrapperfunc)wrap_intargfunc, |
| 1470 | "x.__getitem__(i) <==> x[i]"}, |
| 1471 | {0} |
| 1472 | }; |
| 1473 | |
| 1474 | static PyObject * |
| 1475 | wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped) |
| 1476 | { |
| 1477 | intintargfunc func = (intintargfunc)wrapped; |
| 1478 | int i, j; |
| 1479 | |
| 1480 | if (!PyArg_ParseTuple(args, "ii", &i, &j)) |
| 1481 | return NULL; |
| 1482 | return (*func)(self, i, j); |
| 1483 | } |
| 1484 | |
| 1485 | static struct wrapperbase tab_getslice[] = { |
| 1486 | {"__getslice__", (wrapperfunc)wrap_intintargfunc, |
| 1487 | "x.__getslice__(i, j) <==> x[i:j]"}, |
| 1488 | {0} |
| 1489 | }; |
| 1490 | |
| 1491 | static PyObject * |
| 1492 | wrap_intobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 1493 | { |
| 1494 | intobjargproc func = (intobjargproc)wrapped; |
| 1495 | int i, res; |
| 1496 | PyObject *value; |
| 1497 | |
| 1498 | if (!PyArg_ParseTuple(args, "iO", &i, &value)) |
| 1499 | return NULL; |
| 1500 | res = (*func)(self, i, value); |
| 1501 | if (res == -1 && PyErr_Occurred()) |
| 1502 | return NULL; |
| 1503 | Py_INCREF(Py_None); |
| 1504 | return Py_None; |
| 1505 | } |
| 1506 | |
| 1507 | static struct wrapperbase tab_setitem_int[] = { |
| 1508 | {"__setitem__", (wrapperfunc)wrap_intobjargproc, |
| 1509 | "x.__setitem__(i, y) <==> x[i]=y"}, |
| 1510 | {0} |
| 1511 | }; |
| 1512 | |
| 1513 | static PyObject * |
| 1514 | wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 1515 | { |
| 1516 | intintobjargproc func = (intintobjargproc)wrapped; |
| 1517 | int i, j, res; |
| 1518 | PyObject *value; |
| 1519 | |
| 1520 | if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value)) |
| 1521 | return NULL; |
| 1522 | res = (*func)(self, i, j, value); |
| 1523 | if (res == -1 && PyErr_Occurred()) |
| 1524 | return NULL; |
| 1525 | Py_INCREF(Py_None); |
| 1526 | return Py_None; |
| 1527 | } |
| 1528 | |
| 1529 | static struct wrapperbase tab_setslice[] = { |
| 1530 | {"__setslice__", (wrapperfunc)wrap_intintobjargproc, |
| 1531 | "x.__setslice__(i, j, y) <==> x[i:j]=y"}, |
| 1532 | {0} |
| 1533 | }; |
| 1534 | |
| 1535 | /* XXX objobjproc is a misnomer; should be objargpred */ |
| 1536 | static PyObject * |
| 1537 | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) |
| 1538 | { |
| 1539 | objobjproc func = (objobjproc)wrapped; |
| 1540 | int res; |
| 1541 | PyObject *value; |
| 1542 | |
| 1543 | if (!PyArg_ParseTuple(args, "O", &value)) |
| 1544 | return NULL; |
| 1545 | res = (*func)(self, value); |
| 1546 | if (res == -1 && PyErr_Occurred()) |
| 1547 | return NULL; |
| 1548 | return PyInt_FromLong((long)res); |
| 1549 | } |
| 1550 | |
| 1551 | static struct wrapperbase tab_contains[] = { |
| 1552 | {"__contains__", (wrapperfunc)wrap_objobjproc, |
| 1553 | "x.__contains__(y) <==> y in x"}, |
| 1554 | {0} |
| 1555 | }; |
| 1556 | |
| 1557 | static PyObject * |
| 1558 | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 1559 | { |
| 1560 | objobjargproc func = (objobjargproc)wrapped; |
| 1561 | int res; |
| 1562 | PyObject *key, *value; |
| 1563 | |
| 1564 | if (!PyArg_ParseTuple(args, "OO", &key, &value)) |
| 1565 | return NULL; |
| 1566 | res = (*func)(self, key, value); |
| 1567 | if (res == -1 && PyErr_Occurred()) |
| 1568 | return NULL; |
| 1569 | Py_INCREF(Py_None); |
| 1570 | return Py_None; |
| 1571 | } |
| 1572 | |
| 1573 | static struct wrapperbase tab_setitem[] = { |
| 1574 | {"__setitem__", (wrapperfunc)wrap_objobjargproc, |
| 1575 | "x.__setitem__(y, z) <==> x[y]=z"}, |
| 1576 | {0} |
| 1577 | }; |
| 1578 | |
| 1579 | static PyObject * |
| 1580 | wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped) |
| 1581 | { |
| 1582 | cmpfunc func = (cmpfunc)wrapped; |
| 1583 | int res; |
| 1584 | PyObject *other; |
| 1585 | |
| 1586 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 1587 | return NULL; |
| 1588 | res = (*func)(self, other); |
| 1589 | if (PyErr_Occurred()) |
| 1590 | return NULL; |
| 1591 | return PyInt_FromLong((long)res); |
| 1592 | } |
| 1593 | |
| 1594 | static struct wrapperbase tab_cmp[] = { |
| 1595 | {"__cmp__", (wrapperfunc)wrap_cmpfunc, |
| 1596 | "x.__cmp__(y) <==> cmp(x,y)"}, |
| 1597 | {0} |
| 1598 | }; |
| 1599 | |
| 1600 | static struct wrapperbase tab_repr[] = { |
| 1601 | {"__repr__", (wrapperfunc)wrap_unaryfunc, |
| 1602 | "x.__repr__() <==> repr(x)"}, |
| 1603 | {0} |
| 1604 | }; |
| 1605 | |
| 1606 | static struct wrapperbase tab_getattr[] = { |
| 1607 | {"__getattr__", (wrapperfunc)wrap_binaryfunc, |
| 1608 | "x.__getattr__('name') <==> x.name"}, |
| 1609 | {0} |
| 1610 | }; |
| 1611 | |
| 1612 | static PyObject * |
| 1613 | wrap_setattr(PyObject *self, PyObject *args, void *wrapped) |
| 1614 | { |
| 1615 | setattrofunc func = (setattrofunc)wrapped; |
| 1616 | int res; |
| 1617 | PyObject *name, *value; |
| 1618 | |
| 1619 | if (!PyArg_ParseTuple(args, "OO", &name, &value)) |
| 1620 | return NULL; |
| 1621 | res = (*func)(self, name, value); |
| 1622 | if (res < 0) |
| 1623 | return NULL; |
| 1624 | Py_INCREF(Py_None); |
| 1625 | return Py_None; |
| 1626 | } |
| 1627 | |
| 1628 | static PyObject * |
| 1629 | wrap_delattr(PyObject *self, PyObject *args, void *wrapped) |
| 1630 | { |
| 1631 | setattrofunc func = (setattrofunc)wrapped; |
| 1632 | int res; |
| 1633 | PyObject *name; |
| 1634 | |
| 1635 | if (!PyArg_ParseTuple(args, "O", &name)) |
| 1636 | return NULL; |
| 1637 | res = (*func)(self, name, NULL); |
| 1638 | if (res < 0) |
| 1639 | return NULL; |
| 1640 | Py_INCREF(Py_None); |
| 1641 | return Py_None; |
| 1642 | } |
| 1643 | |
| 1644 | static struct wrapperbase tab_setattr[] = { |
| 1645 | {"__setattr__", (wrapperfunc)wrap_setattr, |
| 1646 | "x.__setattr__('name', value) <==> x.name = value"}, |
| 1647 | {"__delattr__", (wrapperfunc)wrap_delattr, |
| 1648 | "x.__delattr__('name') <==> del x.name"}, |
| 1649 | {0} |
| 1650 | }; |
| 1651 | |
| 1652 | static PyObject * |
| 1653 | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) |
| 1654 | { |
| 1655 | hashfunc func = (hashfunc)wrapped; |
| 1656 | long res; |
| 1657 | |
| 1658 | if (!PyArg_ParseTuple(args, "")) |
| 1659 | return NULL; |
| 1660 | res = (*func)(self); |
| 1661 | if (res == -1 && PyErr_Occurred()) |
| 1662 | return NULL; |
| 1663 | return PyInt_FromLong(res); |
| 1664 | } |
| 1665 | |
| 1666 | static struct wrapperbase tab_hash[] = { |
| 1667 | {"__hash__", (wrapperfunc)wrap_hashfunc, |
| 1668 | "x.__hash__() <==> hash(x)"}, |
| 1669 | {0} |
| 1670 | }; |
| 1671 | |
| 1672 | static PyObject * |
| 1673 | wrap_call(PyObject *self, PyObject *args, void *wrapped) |
| 1674 | { |
| 1675 | ternaryfunc func = (ternaryfunc)wrapped; |
| 1676 | |
| 1677 | /* XXX What about keyword arguments? */ |
| 1678 | return (*func)(self, args, NULL); |
| 1679 | } |
| 1680 | |
| 1681 | static struct wrapperbase tab_call[] = { |
| 1682 | {"__call__", (wrapperfunc)wrap_call, |
| 1683 | "x.__call__(...) <==> x(...)"}, |
| 1684 | {0} |
| 1685 | }; |
| 1686 | |
| 1687 | static struct wrapperbase tab_str[] = { |
| 1688 | {"__str__", (wrapperfunc)wrap_unaryfunc, |
| 1689 | "x.__str__() <==> str(x)"}, |
| 1690 | {0} |
| 1691 | }; |
| 1692 | |
| 1693 | static PyObject * |
| 1694 | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) |
| 1695 | { |
| 1696 | richcmpfunc func = (richcmpfunc)wrapped; |
| 1697 | PyObject *other; |
| 1698 | |
| 1699 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 1700 | return NULL; |
| 1701 | return (*func)(self, other, op); |
| 1702 | } |
| 1703 | |
| 1704 | #undef RICHCMP_WRAPPER |
| 1705 | #define RICHCMP_WRAPPER(NAME, OP) \ |
| 1706 | static PyObject * \ |
| 1707 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ |
| 1708 | { \ |
| 1709 | return wrap_richcmpfunc(self, args, wrapped, OP); \ |
| 1710 | } |
| 1711 | |
| 1712 | RICHCMP_WRAPPER(lt, Py_LT); |
| 1713 | RICHCMP_WRAPPER(le, Py_LE); |
| 1714 | RICHCMP_WRAPPER(eq, Py_EQ); |
| 1715 | RICHCMP_WRAPPER(ne, Py_NE); |
| 1716 | RICHCMP_WRAPPER(gt, Py_GT); |
| 1717 | RICHCMP_WRAPPER(ge, Py_GE); |
| 1718 | |
| 1719 | #undef RICHCMP_ENTRY |
| 1720 | #define RICHCMP_ENTRY(NAME, EXPR) \ |
| 1721 | {"__" #NAME "__", (wrapperfunc)richcmp_##NAME, \ |
| 1722 | "x.__" #NAME "__(y) <==> " EXPR} |
| 1723 | |
| 1724 | static struct wrapperbase tab_richcmp[] = { |
| 1725 | RICHCMP_ENTRY(lt, "x<y"), |
| 1726 | RICHCMP_ENTRY(le, "x<=y"), |
| 1727 | RICHCMP_ENTRY(eq, "x==y"), |
| 1728 | RICHCMP_ENTRY(ne, "x!=y"), |
| 1729 | RICHCMP_ENTRY(gt, "x>y"), |
| 1730 | RICHCMP_ENTRY(ge, "x>=y"), |
| 1731 | {0} |
| 1732 | }; |
| 1733 | |
| 1734 | static struct wrapperbase tab_iter[] = { |
| 1735 | {"__iter__", (wrapperfunc)wrap_unaryfunc, "x.__iter__() <==> iter(x)"}, |
| 1736 | {0} |
| 1737 | }; |
| 1738 | |
| 1739 | static PyObject * |
| 1740 | wrap_next(PyObject *self, PyObject *args, void *wrapped) |
| 1741 | { |
| 1742 | unaryfunc func = (unaryfunc)wrapped; |
| 1743 | PyObject *res; |
| 1744 | |
| 1745 | if (!PyArg_ParseTuple(args, "")) |
| 1746 | return NULL; |
| 1747 | res = (*func)(self); |
| 1748 | if (res == NULL && !PyErr_Occurred()) |
| 1749 | PyErr_SetNone(PyExc_StopIteration); |
| 1750 | return res; |
| 1751 | } |
| 1752 | |
| 1753 | static struct wrapperbase tab_next[] = { |
| 1754 | {"next", (wrapperfunc)wrap_next, |
| 1755 | "x.next() -> the next value, or raise StopIteration"}, |
| 1756 | {0} |
| 1757 | }; |
| 1758 | |
| 1759 | static PyObject * |
| 1760 | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped) |
| 1761 | { |
| 1762 | descrgetfunc func = (descrgetfunc)wrapped; |
| 1763 | PyObject *obj; |
| 1764 | PyObject *type = NULL; |
| 1765 | |
| 1766 | if (!PyArg_ParseTuple(args, "O|O", &obj, &type)) |
| 1767 | return NULL; |
| 1768 | if (type == NULL) |
| 1769 | type = (PyObject *)obj->ob_type; |
| 1770 | return (*func)(self, obj, type); |
| 1771 | } |
| 1772 | |
| 1773 | static struct wrapperbase tab_descr_get[] = { |
| 1774 | {"__get__", (wrapperfunc)wrap_descr_get, |
| 1775 | "descr.__get__(obj, type) -> value"}, |
| 1776 | {0} |
| 1777 | }; |
| 1778 | |
| 1779 | static PyObject * |
| 1780 | wrap_descrsetfunc(PyObject *self, PyObject *args, void *wrapped) |
| 1781 | { |
| 1782 | descrsetfunc func = (descrsetfunc)wrapped; |
| 1783 | PyObject *obj, *value; |
| 1784 | int ret; |
| 1785 | |
| 1786 | if (!PyArg_ParseTuple(args, "OO", &obj, &value)) |
| 1787 | return NULL; |
| 1788 | ret = (*func)(self, obj, value); |
| 1789 | if (ret < 0) |
| 1790 | return NULL; |
| 1791 | Py_INCREF(Py_None); |
| 1792 | return Py_None; |
| 1793 | } |
| 1794 | |
| 1795 | static struct wrapperbase tab_descr_set[] = { |
| 1796 | {"__set__", (wrapperfunc)wrap_descrsetfunc, |
| 1797 | "descr.__set__(obj, value)"}, |
| 1798 | {0} |
| 1799 | }; |
| 1800 | |
| 1801 | static PyObject * |
| 1802 | wrap_init(PyObject *self, PyObject *args, void *wrapped) |
| 1803 | { |
| 1804 | initproc func = (initproc)wrapped; |
| 1805 | |
| 1806 | /* XXX What about keyword arguments? */ |
| 1807 | if (func(self, args, NULL) < 0) |
| 1808 | return NULL; |
| 1809 | Py_INCREF(Py_None); |
| 1810 | return Py_None; |
| 1811 | } |
| 1812 | |
| 1813 | static struct wrapperbase tab_init[] = { |
| 1814 | {"__init__", (wrapperfunc)wrap_init, |
| 1815 | "x.__init__(...) initializes x; " |
| 1816 | "see x.__type__.__doc__ for signature"}, |
| 1817 | {0} |
| 1818 | }; |
| 1819 | |
| 1820 | static PyObject * |
| 1821 | wrap_new(PyObject *type, PyObject *args, void *wrapped) |
| 1822 | { |
| 1823 | newfunc new = (newfunc)wrapped; |
| 1824 | return new((PyTypeObject *)type, args, NULL); |
| 1825 | } |
| 1826 | |
| 1827 | static struct wrapperbase tab_new[] = { |
| 1828 | {"__new__", (wrapperfunc)wrap_new, |
| 1829 | "T.__new__() -> an object with type T"}, |
| 1830 | {0} |
| 1831 | }; |
| 1832 | |
| 1833 | static int |
| 1834 | add_operators(PyTypeObject *type) |
| 1835 | { |
| 1836 | PySequenceMethods *sq; |
| 1837 | PyMappingMethods *mp; |
| 1838 | PyNumberMethods *nb; |
| 1839 | |
| 1840 | #undef ADD |
| 1841 | #define ADD(SLOT, TABLE) \ |
| 1842 | if (SLOT) { \ |
| 1843 | if (add_wrappers(type, TABLE, (void *)(SLOT)) < 0) \ |
| 1844 | return -1; \ |
| 1845 | } |
| 1846 | |
| 1847 | if ((sq = type->tp_as_sequence) != NULL) { |
| 1848 | ADD(sq->sq_length, tab_len); |
| 1849 | ADD(sq->sq_concat, tab_concat); |
| 1850 | ADD(sq->sq_repeat, tab_mul_int); |
| 1851 | ADD(sq->sq_item, tab_getitem_int); |
| 1852 | ADD(sq->sq_slice, tab_getslice); |
| 1853 | ADD(sq->sq_ass_item, tab_setitem_int); |
| 1854 | ADD(sq->sq_ass_slice, tab_setslice); |
| 1855 | ADD(sq->sq_contains, tab_contains); |
| 1856 | ADD(sq->sq_inplace_concat, tab_iadd); |
| 1857 | ADD(sq->sq_inplace_repeat, tab_imul_int); |
| 1858 | } |
| 1859 | |
| 1860 | if ((mp = type->tp_as_mapping) != NULL) { |
| 1861 | if (sq->sq_length == NULL) |
| 1862 | ADD(mp->mp_length, tab_len); |
| 1863 | ADD(mp->mp_subscript, tab_getitem); |
| 1864 | ADD(mp->mp_ass_subscript, tab_setitem); |
| 1865 | } |
| 1866 | |
| 1867 | /* We don't support "old-style numbers" because their binary |
| 1868 | operators require that both arguments have the same type; |
| 1869 | the wrappers here only work for new-style numbers. */ |
| 1870 | if ((type->tp_flags & Py_TPFLAGS_CHECKTYPES) && |
| 1871 | (nb = type->tp_as_number) != NULL) { |
| 1872 | ADD(nb->nb_add, tab_add); |
| 1873 | ADD(nb->nb_subtract, tab_sub); |
| 1874 | ADD(nb->nb_multiply, tab_mul); |
| 1875 | ADD(nb->nb_divide, tab_div); |
| 1876 | ADD(nb->nb_remainder, tab_mod); |
| 1877 | ADD(nb->nb_divmod, tab_divmod); |
| 1878 | ADD(nb->nb_power, tab_pow); |
| 1879 | ADD(nb->nb_negative, tab_neg); |
| 1880 | ADD(nb->nb_positive, tab_pos); |
| 1881 | ADD(nb->nb_absolute, tab_abs); |
| 1882 | ADD(nb->nb_nonzero, tab_nonzero); |
| 1883 | ADD(nb->nb_invert, tab_invert); |
| 1884 | ADD(nb->nb_lshift, tab_lshift); |
| 1885 | ADD(nb->nb_rshift, tab_rshift); |
| 1886 | ADD(nb->nb_and, tab_and); |
| 1887 | ADD(nb->nb_xor, tab_xor); |
| 1888 | ADD(nb->nb_or, tab_or); |
| 1889 | /* We don't support coerce() -- see above comment */ |
| 1890 | ADD(nb->nb_int, tab_int); |
| 1891 | ADD(nb->nb_long, tab_long); |
| 1892 | ADD(nb->nb_float, tab_float); |
| 1893 | ADD(nb->nb_oct, tab_oct); |
| 1894 | ADD(nb->nb_hex, tab_hex); |
| 1895 | ADD(nb->nb_inplace_add, tab_iadd); |
| 1896 | ADD(nb->nb_inplace_subtract, tab_isub); |
| 1897 | ADD(nb->nb_inplace_multiply, tab_imul); |
| 1898 | ADD(nb->nb_inplace_divide, tab_idiv); |
| 1899 | ADD(nb->nb_inplace_remainder, tab_imod); |
| 1900 | ADD(nb->nb_inplace_power, tab_ipow); |
| 1901 | ADD(nb->nb_inplace_lshift, tab_ilshift); |
| 1902 | ADD(nb->nb_inplace_rshift, tab_irshift); |
| 1903 | ADD(nb->nb_inplace_and, tab_iand); |
| 1904 | ADD(nb->nb_inplace_xor, tab_ixor); |
| 1905 | ADD(nb->nb_inplace_or, tab_ior); |
| 1906 | } |
| 1907 | |
| 1908 | ADD(type->tp_getattro, tab_getattr); |
| 1909 | ADD(type->tp_setattro, tab_setattr); |
| 1910 | ADD(type->tp_compare, tab_cmp); |
| 1911 | ADD(type->tp_repr, tab_repr); |
| 1912 | ADD(type->tp_hash, tab_hash); |
| 1913 | ADD(type->tp_call, tab_call); |
| 1914 | ADD(type->tp_str, tab_str); |
| 1915 | ADD(type->tp_richcompare, tab_richcmp); |
| 1916 | ADD(type->tp_iter, tab_iter); |
| 1917 | ADD(type->tp_iternext, tab_next); |
| 1918 | ADD(type->tp_descr_get, tab_descr_get); |
| 1919 | ADD(type->tp_descr_set, tab_descr_set); |
| 1920 | ADD(type->tp_init, tab_init); |
| 1921 | |
| 1922 | if (type->tp_new != NULL) |
| 1923 | add_staticmethodwrappers(type, tab_new, |
| 1924 | (void *)(type->tp_new)); |
| 1925 | |
| 1926 | return 0; |
| 1927 | } |
| 1928 | |
| 1929 | /* Slot wrappers that call the corresponding __foo__ slot */ |
| 1930 | |
| 1931 | #define SLOT0(SLOTNAME, OPNAME) \ |
| 1932 | static PyObject * \ |
| 1933 | slot_##SLOTNAME(PyObject *self) \ |
| 1934 | { \ |
| 1935 | return PyObject_CallMethod(self, "__" #OPNAME "__", ""); \ |
| 1936 | } |
| 1937 | |
| 1938 | #define SLOT1(SLOTNAME, OPNAME, ARG1TYPE, ARGCODES) \ |
| 1939 | static PyObject * \ |
| 1940 | slot_##SLOTNAME(PyObject *self, ARG1TYPE arg1) \ |
| 1941 | { \ |
| 1942 | return PyObject_CallMethod(self, "__" #OPNAME "__", #ARGCODES, arg1); \ |
| 1943 | } |
| 1944 | |
| 1945 | #define SLOT2(SLOTNAME, OPNAME, ARG1TYPE, ARG2TYPE, ARGCODES) \ |
| 1946 | static PyObject * \ |
| 1947 | slot_##SLOTNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ |
| 1948 | { \ |
| 1949 | return PyObject_CallMethod(self, "__" #OPNAME "__", \ |
| 1950 | #ARGCODES, arg1, arg2); \ |
| 1951 | } |
| 1952 | |
| 1953 | static int |
| 1954 | slot_sq_length(PyObject *self) |
| 1955 | { |
| 1956 | PyObject *res = PyObject_CallMethod(self, "__len__", ""); |
| 1957 | |
| 1958 | if (res == NULL) |
| 1959 | return -1; |
| 1960 | return (int)PyInt_AsLong(res); |
| 1961 | } |
| 1962 | |
| 1963 | SLOT1(sq_concat, add, PyObject *, O); |
| 1964 | SLOT1(sq_repeat, mul, int, i); |
| 1965 | SLOT1(sq_item, getitem, int, i); |
| 1966 | SLOT2(sq_slice, getslice, int, int, ii); |
| 1967 | |
| 1968 | static int |
| 1969 | slot_sq_ass_item(PyObject *self, int index, PyObject *value) |
| 1970 | { |
| 1971 | PyObject *res; |
| 1972 | |
| 1973 | if (value == NULL) |
| 1974 | res = PyObject_CallMethod(self, "__delitem__", "i", index); |
| 1975 | else |
| 1976 | res = PyObject_CallMethod(self, "__setitem__", |
| 1977 | "iO", index, value); |
| 1978 | if (res == NULL) |
| 1979 | return -1; |
| 1980 | Py_DECREF(res); |
| 1981 | return 0; |
| 1982 | } |
| 1983 | |
| 1984 | static int |
| 1985 | slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value) |
| 1986 | { |
| 1987 | PyObject *res; |
| 1988 | |
| 1989 | if (value == NULL) |
| 1990 | res = PyObject_CallMethod(self, "__delslice__", "ii", i, j); |
| 1991 | else |
| 1992 | res = PyObject_CallMethod(self, "__setslice__", |
| 1993 | "iiO", i, j, value); |
| 1994 | if (res == NULL) |
| 1995 | return -1; |
| 1996 | Py_DECREF(res); |
| 1997 | return 0; |
| 1998 | } |
| 1999 | |
| 2000 | static int |
| 2001 | slot_sq_contains(PyObject *self, PyObject *value) |
| 2002 | { |
| 2003 | PyObject *res = PyObject_CallMethod(self, "__contains__", "O", value); |
| 2004 | int r; |
| 2005 | |
| 2006 | if (res == NULL) |
| 2007 | return -1; |
| 2008 | r = PyInt_AsLong(res); |
| 2009 | Py_DECREF(res); |
| 2010 | return r; |
| 2011 | } |
| 2012 | |
| 2013 | SLOT1(sq_inplace_concat, iadd, PyObject *, O); |
| 2014 | SLOT1(sq_inplace_repeat, imul, int, i); |
| 2015 | |
| 2016 | #define slot_mp_length slot_sq_length |
| 2017 | |
| 2018 | SLOT1(mp_subscript, getitem, PyObject *, O); |
| 2019 | |
| 2020 | static int |
| 2021 | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) |
| 2022 | { |
| 2023 | PyObject *res; |
| 2024 | |
| 2025 | if (value == NULL) |
| 2026 | res = PyObject_CallMethod(self, "__delitem__", "O", key); |
| 2027 | else |
| 2028 | res = PyObject_CallMethod(self, "__setitem__", |
| 2029 | "OO", key, value); |
| 2030 | if (res == NULL) |
| 2031 | return -1; |
| 2032 | Py_DECREF(res); |
| 2033 | return 0; |
| 2034 | } |
| 2035 | |
| 2036 | /* XXX the numerical slots should call the reverse operators too; |
| 2037 | but how do they know their type? */ |
| 2038 | SLOT1(nb_add, add, PyObject *, O); |
| 2039 | SLOT1(nb_subtract, sub, PyObject *, O); |
| 2040 | SLOT1(nb_multiply, mul, PyObject *, O); |
| 2041 | SLOT1(nb_divide, div, PyObject *, O); |
| 2042 | SLOT1(nb_remainder, mod, PyObject *, O); |
| 2043 | SLOT1(nb_divmod, divmod, PyObject *, O); |
| 2044 | SLOT2(nb_power, pow, PyObject *, PyObject *, OO); |
| 2045 | SLOT0(nb_negative, neg); |
| 2046 | SLOT0(nb_positive, pos); |
| 2047 | SLOT0(nb_absolute, abs); |
| 2048 | |
| 2049 | static int |
| 2050 | slot_nb_nonzero(PyObject *self) |
| 2051 | { |
| 2052 | PyObject *res = PyObject_CallMethod(self, "__nonzero__", ""); |
| 2053 | |
| 2054 | if (res == NULL) |
| 2055 | return -1; |
| 2056 | return (int)PyInt_AsLong(res); |
| 2057 | } |
| 2058 | |
| 2059 | SLOT0(nb_invert, invert); |
| 2060 | SLOT1(nb_lshift, lshift, PyObject *, O); |
| 2061 | SLOT1(nb_rshift, rshift, PyObject *, O); |
| 2062 | SLOT1(nb_and, and, PyObject *, O); |
| 2063 | SLOT1(nb_xor, xor, PyObject *, O); |
| 2064 | SLOT1(nb_or, or, PyObject *, O); |
| 2065 | /* Not coerce() */ |
| 2066 | SLOT0(nb_int, int); |
| 2067 | SLOT0(nb_long, long); |
| 2068 | SLOT0(nb_float, float); |
| 2069 | SLOT0(nb_oct, oct); |
| 2070 | SLOT0(nb_hex, hex); |
| 2071 | SLOT1(nb_inplace_add, iadd, PyObject *, O); |
| 2072 | SLOT1(nb_inplace_subtract, isub, PyObject *, O); |
| 2073 | SLOT1(nb_inplace_multiply, imul, PyObject *, O); |
| 2074 | SLOT1(nb_inplace_divide, idiv, PyObject *, O); |
| 2075 | SLOT1(nb_inplace_remainder, imod, PyObject *, O); |
| 2076 | SLOT2(nb_inplace_power, ipow, PyObject *, PyObject *, OO); |
| 2077 | SLOT1(nb_inplace_lshift, ilshift, PyObject *, O); |
| 2078 | SLOT1(nb_inplace_rshift, irshift, PyObject *, O); |
| 2079 | SLOT1(nb_inplace_and, iand, PyObject *, O); |
| 2080 | SLOT1(nb_inplace_xor, ixor, PyObject *, O); |
| 2081 | SLOT1(nb_inplace_or, ior, PyObject *, O); |
| 2082 | |
| 2083 | static int |
| 2084 | slot_tp_compare(PyObject *self, PyObject *other) |
| 2085 | { |
| 2086 | PyObject *res = PyObject_CallMethod(self, "__cmp__", "O", other); |
| 2087 | long r; |
| 2088 | |
| 2089 | if (res == NULL) |
| 2090 | return -1; |
| 2091 | r = PyInt_AsLong(res); |
| 2092 | Py_DECREF(res); |
| 2093 | return (int)r; |
| 2094 | } |
| 2095 | |
| 2096 | SLOT0(tp_repr, repr); |
| 2097 | |
| 2098 | static long |
| 2099 | slot_tp_hash(PyObject *self) |
| 2100 | { |
| 2101 | PyObject *res = PyObject_CallMethod(self, "__hash__", ""); |
| 2102 | long h; |
| 2103 | |
| 2104 | if (res == NULL) |
| 2105 | return -1; |
| 2106 | h = PyInt_AsLong(res); |
| 2107 | if (h == -1 && !PyErr_Occurred()) |
| 2108 | h = -2; |
| 2109 | return h; |
| 2110 | } |
| 2111 | |
| 2112 | static PyObject * |
| 2113 | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 2114 | { |
| 2115 | PyObject *meth = PyObject_GetAttrString(self, "__call__"); |
| 2116 | PyObject *res; |
| 2117 | |
| 2118 | if (meth == NULL) |
| 2119 | return NULL; |
| 2120 | res = PyObject_Call(meth, args, kwds); |
| 2121 | Py_DECREF(meth); |
| 2122 | return res; |
| 2123 | } |
| 2124 | |
| 2125 | SLOT0(tp_str, str); |
| 2126 | |
| 2127 | static PyObject * |
| 2128 | slot_tp_getattro(PyObject *self, PyObject *name) |
| 2129 | { |
| 2130 | PyTypeObject *tp = self->ob_type; |
| 2131 | PyObject *dict = NULL; |
| 2132 | PyObject *getattr; |
| 2133 | |
| 2134 | if (tp->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 2135 | dict = tp->tp_dict; |
| 2136 | if (dict == NULL) { |
| 2137 | PyErr_Format(PyExc_SystemError, |
| 2138 | "'%.100s' type object has no __dict__???", |
| 2139 | tp->tp_name); |
| 2140 | return NULL; |
| 2141 | } |
| 2142 | getattr = PyDict_GetItemString(dict, "__getattr__"); |
| 2143 | if (getattr == NULL) { |
| 2144 | PyErr_SetString(PyExc_AttributeError, "__getattr__"); |
| 2145 | return NULL; |
| 2146 | } |
| 2147 | return PyObject_CallFunction(getattr, "OO", self, name); |
| 2148 | } |
| 2149 | |
| 2150 | static int |
| 2151 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) |
| 2152 | { |
| 2153 | PyObject *res; |
| 2154 | |
| 2155 | if (value == NULL) |
| 2156 | res = PyObject_CallMethod(self, "__delattr__", "O", name); |
| 2157 | else |
| 2158 | res = PyObject_CallMethod(self, "__setattr__", |
| 2159 | "OO", name, value); |
| 2160 | if (res == NULL) |
| 2161 | return -1; |
| 2162 | Py_DECREF(res); |
| 2163 | return 0; |
| 2164 | } |
| 2165 | |
| 2166 | /* Map rich comparison operators to their __xx__ namesakes */ |
| 2167 | static char *name_op[] = { |
| 2168 | "__lt__", |
| 2169 | "__le__", |
| 2170 | "__eq__", |
| 2171 | "__ne__", |
| 2172 | "__gt__", |
| 2173 | "__ge__", |
| 2174 | }; |
| 2175 | |
| 2176 | static PyObject * |
| 2177 | slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
| 2178 | { |
| 2179 | PyObject *meth = PyObject_GetAttrString(self, name_op[op]); |
| 2180 | PyObject *res; |
| 2181 | |
| 2182 | if (meth == NULL) |
| 2183 | return NULL; |
| 2184 | res = PyObject_CallFunction(meth, "O", other); |
| 2185 | Py_DECREF(meth); |
| 2186 | return res; |
| 2187 | } |
| 2188 | |
| 2189 | SLOT0(tp_iter, iter); |
| 2190 | |
| 2191 | static PyObject * |
| 2192 | slot_tp_iternext(PyObject *self) |
| 2193 | { |
| 2194 | return PyObject_CallMethod(self, "next", ""); |
| 2195 | } |
| 2196 | |
| 2197 | SLOT2(tp_descr_get, get, PyObject *, PyObject *, OO); |
| 2198 | |
| 2199 | static int |
| 2200 | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) |
| 2201 | { |
| 2202 | PyObject *res = PyObject_CallMethod(self, "__set__", |
| 2203 | "OO", target, value); |
| 2204 | if (res == NULL) |
| 2205 | return -1; |
| 2206 | Py_DECREF(res); |
| 2207 | return 0; |
| 2208 | } |
| 2209 | |
| 2210 | static int |
| 2211 | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2212 | { |
| 2213 | PyObject *meth = PyObject_GetAttrString(self, "__init__"); |
| 2214 | PyObject *res; |
| 2215 | |
| 2216 | if (meth == NULL) |
| 2217 | return -1; |
| 2218 | res = PyObject_Call(meth, args, kwds); |
| 2219 | Py_DECREF(meth); |
| 2220 | if (res == NULL) |
| 2221 | return -1; |
| 2222 | Py_DECREF(res); |
| 2223 | return 0; |
| 2224 | } |
| 2225 | |
| 2226 | static PyObject * |
| 2227 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2228 | { |
| 2229 | PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__"); |
| 2230 | PyObject *newargs, *x; |
| 2231 | int i, n; |
| 2232 | |
| 2233 | if (func == NULL) |
| 2234 | return NULL; |
| 2235 | assert(PyTuple_Check(args)); |
| 2236 | n = PyTuple_GET_SIZE(args); |
| 2237 | newargs = PyTuple_New(n+1); |
| 2238 | if (newargs == NULL) |
| 2239 | return NULL; |
| 2240 | Py_INCREF(type); |
| 2241 | PyTuple_SET_ITEM(newargs, 0, (PyObject *)type); |
| 2242 | for (i = 0; i < n; i++) { |
| 2243 | x = PyTuple_GET_ITEM(args, i); |
| 2244 | Py_INCREF(x); |
| 2245 | PyTuple_SET_ITEM(newargs, i+1, x); |
| 2246 | } |
| 2247 | x = PyObject_Call(func, newargs, kwds); |
| 2248 | Py_DECREF(func); |
| 2249 | return x; |
| 2250 | } |
| 2251 | |
| 2252 | static void |
| 2253 | override_slots(PyTypeObject *type, PyObject *dict) |
| 2254 | { |
| 2255 | PySequenceMethods *sq = type->tp_as_sequence; |
| 2256 | PyMappingMethods *mp = type->tp_as_mapping; |
| 2257 | PyNumberMethods *nb = type->tp_as_number; |
| 2258 | |
| 2259 | #define SQSLOT(OPNAME, SLOTNAME) \ |
| 2260 | if (PyDict_GetItemString(dict, OPNAME)) { \ |
| 2261 | sq->SLOTNAME = slot_##SLOTNAME; \ |
| 2262 | } |
| 2263 | |
| 2264 | #define MPSLOT(OPNAME, SLOTNAME) \ |
| 2265 | if (PyDict_GetItemString(dict, OPNAME)) { \ |
| 2266 | mp->SLOTNAME = slot_##SLOTNAME; \ |
| 2267 | } |
| 2268 | |
| 2269 | #define NBSLOT(OPNAME, SLOTNAME) \ |
| 2270 | if (PyDict_GetItemString(dict, OPNAME)) { \ |
| 2271 | nb->SLOTNAME = slot_##SLOTNAME; \ |
| 2272 | } |
| 2273 | |
| 2274 | #define TPSLOT(OPNAME, SLOTNAME) \ |
| 2275 | if (PyDict_GetItemString(dict, OPNAME)) { \ |
| 2276 | type->SLOTNAME = slot_##SLOTNAME; \ |
| 2277 | } |
| 2278 | |
| 2279 | SQSLOT("__len__", sq_length); |
| 2280 | SQSLOT("__add__", sq_concat); |
| 2281 | SQSLOT("__mul__", sq_repeat); |
| 2282 | SQSLOT("__getitem__", sq_item); |
| 2283 | SQSLOT("__getslice__", sq_slice); |
| 2284 | SQSLOT("__setitem__", sq_ass_item); |
| 2285 | SQSLOT("__delitem__", sq_ass_item); |
| 2286 | SQSLOT("__setslice__", sq_ass_slice); |
| 2287 | SQSLOT("__delslice__", sq_ass_slice); |
| 2288 | SQSLOT("__contains__", sq_contains); |
| 2289 | SQSLOT("__iadd__", sq_inplace_concat); |
| 2290 | SQSLOT("__imul__", sq_inplace_repeat); |
| 2291 | |
| 2292 | MPSLOT("__len__", mp_length); |
| 2293 | MPSLOT("__getitem__", mp_subscript); |
| 2294 | MPSLOT("__setitem__", mp_ass_subscript); |
| 2295 | MPSLOT("__delitem__", mp_ass_subscript); |
| 2296 | |
| 2297 | NBSLOT("__add__", nb_add); |
| 2298 | NBSLOT("__sub__", nb_subtract); |
| 2299 | NBSLOT("__mul__", nb_multiply); |
| 2300 | NBSLOT("__div__", nb_divide); |
| 2301 | NBSLOT("__mod__", nb_remainder); |
| 2302 | NBSLOT("__divmod__", nb_divmod); |
| 2303 | NBSLOT("__pow__", nb_power); |
| 2304 | NBSLOT("__neg__", nb_negative); |
| 2305 | NBSLOT("__pos__", nb_positive); |
| 2306 | NBSLOT("__abs__", nb_absolute); |
| 2307 | NBSLOT("__nonzero__", nb_nonzero); |
| 2308 | NBSLOT("__invert__", nb_invert); |
| 2309 | NBSLOT("__lshift__", nb_lshift); |
| 2310 | NBSLOT("__rshift__", nb_rshift); |
| 2311 | NBSLOT("__and__", nb_and); |
| 2312 | NBSLOT("__xor__", nb_xor); |
| 2313 | NBSLOT("__or__", nb_or); |
| 2314 | /* Not coerce() */ |
| 2315 | NBSLOT("__int__", nb_int); |
| 2316 | NBSLOT("__long__", nb_long); |
| 2317 | NBSLOT("__float__", nb_float); |
| 2318 | NBSLOT("__oct__", nb_oct); |
| 2319 | NBSLOT("__hex__", nb_hex); |
| 2320 | NBSLOT("__iadd__", nb_inplace_add); |
| 2321 | NBSLOT("__isub__", nb_inplace_subtract); |
| 2322 | NBSLOT("__imul__", nb_inplace_multiply); |
| 2323 | NBSLOT("__idiv__", nb_inplace_divide); |
| 2324 | NBSLOT("__imod__", nb_inplace_remainder); |
| 2325 | NBSLOT("__ipow__", nb_inplace_power); |
| 2326 | NBSLOT("__ilshift__", nb_inplace_lshift); |
| 2327 | NBSLOT("__irshift__", nb_inplace_rshift); |
| 2328 | NBSLOT("__iand__", nb_inplace_and); |
| 2329 | NBSLOT("__ixor__", nb_inplace_xor); |
| 2330 | NBSLOT("__ior__", nb_inplace_or); |
| 2331 | |
| 2332 | if (PyDict_GetItemString(dict, "__str__") || |
| 2333 | PyDict_GetItemString(dict, "__repr__")) |
| 2334 | type->tp_print = NULL; |
| 2335 | |
| 2336 | TPSLOT("__cmp__", tp_compare); |
| 2337 | TPSLOT("__repr__", tp_repr); |
| 2338 | TPSLOT("__hash__", tp_hash); |
| 2339 | TPSLOT("__call__", tp_call); |
| 2340 | TPSLOT("__str__", tp_str); |
| 2341 | TPSLOT("__getattr__", tp_getattro); |
| 2342 | TPSLOT("__setattr__", tp_setattro); |
| 2343 | TPSLOT("__lt__", tp_richcompare); |
| 2344 | TPSLOT("__le__", tp_richcompare); |
| 2345 | TPSLOT("__eq__", tp_richcompare); |
| 2346 | TPSLOT("__ne__", tp_richcompare); |
| 2347 | TPSLOT("__gt__", tp_richcompare); |
| 2348 | TPSLOT("__ge__", tp_richcompare); |
| 2349 | TPSLOT("__iter__", tp_iter); |
| 2350 | TPSLOT("next", tp_iternext); |
| 2351 | TPSLOT("__get__", tp_descr_get); |
| 2352 | TPSLOT("__set__", tp_descr_set); |
| 2353 | TPSLOT("__init__", tp_init); |
| 2354 | TPSLOT("__new__", tp_new); |
| 2355 | } |