Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* Type object implementation */ |
| 2 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3 | #include "Python.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 6 | #include <ctype.h> |
| 7 | |
| 8 | /* The *real* layout of a type object when allocated on the heap */ |
| 9 | /* XXX Should we publish this in a header file? */ |
| 10 | typedef struct { |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 11 | /* Note: there's a dependency on the order of these members |
| 12 | in slotptr() below. */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 13 | PyTypeObject type; |
| 14 | PyNumberMethods as_number; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 15 | PyMappingMethods as_mapping; |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 16 | PySequenceMethods as_sequence; /* as_sequence comes after as_mapping, |
| 17 | so that the mapping wins when both |
| 18 | the mapping and the sequence define |
| 19 | a given operator (e.g. __getitem__). |
| 20 | see add_operators() below. */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 21 | PyBufferProcs as_buffer; |
| 22 | PyObject *name, *slots; |
| 23 | PyMemberDef members[1]; |
| 24 | } etype; |
| 25 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 26 | static PyMemberDef type_members[] = { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 27 | {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY}, |
| 28 | {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY}, |
| 29 | {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY}, |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 30 | {"__weakrefoffset__", T_LONG, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 31 | offsetof(PyTypeObject, tp_weaklistoffset), READONLY}, |
| 32 | {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY}, |
| 33 | {"__dictoffset__", T_LONG, |
| 34 | offsetof(PyTypeObject, tp_dictoffset), READONLY}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 35 | {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY}, |
| 36 | {0} |
| 37 | }; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 39 | static PyObject * |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 40 | type_name(PyTypeObject *type, void *context) |
| 41 | { |
| 42 | char *s; |
| 43 | |
| 44 | s = strrchr(type->tp_name, '.'); |
| 45 | if (s == NULL) |
| 46 | s = type->tp_name; |
| 47 | else |
| 48 | s++; |
| 49 | return PyString_FromString(s); |
| 50 | } |
| 51 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 52 | static int |
| 53 | type_set_name(PyTypeObject *type, PyObject *value, void *context) |
| 54 | { |
| 55 | etype* et; |
| 56 | |
| 57 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 58 | PyErr_Format(PyExc_TypeError, |
| 59 | "can't set %s.__name__", type->tp_name); |
| 60 | return -1; |
| 61 | } |
| 62 | if (!value) { |
| 63 | PyErr_Format(PyExc_TypeError, |
| 64 | "can't delete %s.__name__", type->tp_name); |
| 65 | return -1; |
| 66 | } |
| 67 | if (!PyString_Check(value)) { |
| 68 | PyErr_Format(PyExc_TypeError, |
| 69 | "can only assign string to %s.__name__, not '%s'", |
| 70 | type->tp_name, value->ob_type->tp_name); |
| 71 | return -1; |
| 72 | } |
| 73 | if (strlen(PyString_AS_STRING(value)) |
| 74 | != (size_t)PyString_GET_SIZE(value)) { |
| 75 | PyErr_Format(PyExc_ValueError, |
| 76 | "__name__ must not contain null bytes"); |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | et = (etype*)type; |
| 81 | |
| 82 | Py_INCREF(value); |
| 83 | |
| 84 | Py_DECREF(et->name); |
| 85 | et->name = value; |
| 86 | |
| 87 | type->tp_name = PyString_AS_STRING(value); |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 92 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 93 | type_module(PyTypeObject *type, void *context) |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 94 | { |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 95 | PyObject *mod; |
| 96 | char *s; |
| 97 | |
| 98 | s = strrchr(type->tp_name, '.'); |
| 99 | if (s != NULL) |
| 100 | return PyString_FromStringAndSize(type->tp_name, |
| 101 | (int)(s - type->tp_name)); |
| 102 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 103 | return PyString_FromString("__builtin__"); |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 104 | mod = PyDict_GetItemString(type->tp_dict, "__module__"); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 105 | if (mod != NULL) { |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 106 | Py_INCREF(mod); |
| 107 | return mod; |
| 108 | } |
| 109 | PyErr_SetString(PyExc_AttributeError, "__module__"); |
| 110 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 113 | static int |
| 114 | type_set_module(PyTypeObject *type, PyObject *value, void *context) |
| 115 | { |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 116 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 117 | PyErr_Format(PyExc_TypeError, |
| 118 | "can't set %s.__module__", type->tp_name); |
| 119 | return -1; |
| 120 | } |
| 121 | if (!value) { |
| 122 | PyErr_Format(PyExc_TypeError, |
| 123 | "can't delete %s.__module__", type->tp_name); |
| 124 | return -1; |
| 125 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 126 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 127 | return PyDict_SetItemString(type->tp_dict, "__module__", value); |
| 128 | } |
| 129 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 130 | static PyObject * |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 131 | type_get_bases(PyTypeObject *type, void *context) |
| 132 | { |
| 133 | Py_INCREF(type->tp_bases); |
| 134 | return type->tp_bases; |
| 135 | } |
| 136 | |
| 137 | static PyTypeObject *best_base(PyObject *); |
| 138 | static int mro_internal(PyTypeObject *); |
| 139 | static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *); |
| 140 | static int add_subclass(PyTypeObject*, PyTypeObject*); |
| 141 | static void remove_subclass(PyTypeObject *, PyTypeObject *); |
| 142 | static void update_all_slots(PyTypeObject *); |
| 143 | |
| 144 | static int |
| 145 | mro_subclasses(PyTypeObject *type) |
| 146 | { |
| 147 | PyTypeObject *subclass; |
| 148 | PyObject *ref, *subclasses, *old_mro; |
Michael W. Hudson | ac74f5d | 2002-11-26 17:49:11 +0000 | [diff] [blame] | 149 | int i, n, r = 0; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 150 | |
| 151 | subclasses = type->tp_subclasses; |
| 152 | if (subclasses == NULL) |
| 153 | return 0; |
| 154 | assert(PyList_Check(subclasses)); |
| 155 | n = PyList_GET_SIZE(subclasses); |
| 156 | for (i = 0; i < n; i++) { |
| 157 | ref = PyList_GET_ITEM(subclasses, i); |
| 158 | assert(PyWeakref_CheckRef(ref)); |
| 159 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 160 | assert(subclass != NULL); |
| 161 | if ((PyObject *)subclass == Py_None) |
| 162 | continue; |
| 163 | assert(PyType_Check(subclass)); |
| 164 | old_mro = subclass->tp_mro; |
| 165 | if (mro_internal(subclass) < 0) { |
| 166 | subclass->tp_mro = old_mro; |
| 167 | r = -1; |
| 168 | } |
| 169 | else { |
| 170 | Py_DECREF(old_mro); |
| 171 | } |
| 172 | if (mro_subclasses(subclass) < 0) |
| 173 | r = -1; |
| 174 | } |
| 175 | return r; |
| 176 | } |
| 177 | |
| 178 | static int |
| 179 | type_set_bases(PyTypeObject *type, PyObject *value, void *context) |
| 180 | { |
| 181 | int i, r = 0; |
| 182 | PyObject* ob; |
| 183 | PyTypeObject *new_base, *old_base; |
| 184 | PyObject *old_bases, *old_mro; |
| 185 | |
| 186 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 187 | PyErr_Format(PyExc_TypeError, |
| 188 | "can't set %s.__bases__", type->tp_name); |
| 189 | return -1; |
| 190 | } |
| 191 | if (!value) { |
| 192 | PyErr_Format(PyExc_TypeError, |
| 193 | "can't delete %s.__bases__", type->tp_name); |
| 194 | return -1; |
| 195 | } |
| 196 | if (!PyTuple_Check(value)) { |
| 197 | PyErr_Format(PyExc_TypeError, |
| 198 | "can only assign tuple to %s.__bases__, not %s", |
| 199 | type->tp_name, value->ob_type->tp_name); |
| 200 | return -1; |
| 201 | } |
| 202 | for (i = 0; i < PyTuple_GET_SIZE(value); i++) { |
| 203 | ob = PyTuple_GET_ITEM(value, i); |
| 204 | if (!PyClass_Check(ob) && !PyType_Check(ob)) { |
| 205 | PyErr_Format( |
| 206 | PyExc_TypeError, |
| 207 | "%s.__bases__ must be tuple of old- or new-style classes, not '%s'", |
| 208 | type->tp_name, ob->ob_type->tp_name); |
| 209 | return -1; |
| 210 | } |
Michael W. Hudson | caf17be | 2002-11-27 10:24:44 +0000 | [diff] [blame^] | 211 | if (PyType_Check(ob)) { |
| 212 | if (PyType_IsSubtype((PyTypeObject*)ob, type)) { |
| 213 | PyErr_SetString(PyExc_TypeError, |
| 214 | "a __bases__ item causes an inheritance cycle"); |
| 215 | return -1; |
| 216 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | new_base = best_base(value); |
| 221 | |
| 222 | if (!new_base) { |
| 223 | return -1; |
| 224 | } |
| 225 | |
| 226 | if (!compatible_for_assignment(type->tp_base, new_base, "__bases__")) |
| 227 | return -1; |
| 228 | |
| 229 | Py_INCREF(new_base); |
| 230 | Py_INCREF(value); |
| 231 | |
| 232 | old_bases = type->tp_bases; |
| 233 | old_base = type->tp_base; |
| 234 | old_mro = type->tp_mro; |
| 235 | |
| 236 | type->tp_bases = value; |
| 237 | type->tp_base = new_base; |
| 238 | |
| 239 | if (mro_internal(type) < 0) { |
| 240 | type->tp_bases = old_bases; |
| 241 | type->tp_base = old_base; |
| 242 | type->tp_mro = old_mro; |
| 243 | |
| 244 | Py_DECREF(value); |
| 245 | Py_DECREF(new_base); |
| 246 | |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | if (mro_subclasses(type) < 0) |
| 251 | r = -1; |
| 252 | |
| 253 | /* any base that was in __bases__ but now isn't, we |
| 254 | need to remove |type| from it's tp_subclasses. |
| 255 | conversely, any class now in __bases__ that wasn't |
| 256 | needs to have |type| added to it's subclasses. */ |
| 257 | |
| 258 | /* for now, sod that: just remove from all old_bases, |
| 259 | add to all new_bases */ |
| 260 | |
| 261 | for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) { |
| 262 | ob = PyTuple_GET_ITEM(old_bases, i); |
| 263 | if (PyType_Check(ob)) { |
| 264 | remove_subclass( |
| 265 | (PyTypeObject*)ob, type); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) { |
| 270 | ob = PyTuple_GET_ITEM(value, i); |
| 271 | if (PyType_Check(ob)) { |
| 272 | if (add_subclass((PyTypeObject*)ob, type) < 0) |
| 273 | r = -1; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | update_all_slots(type); |
| 278 | |
| 279 | Py_DECREF(old_bases); |
| 280 | Py_DECREF(old_base); |
| 281 | Py_DECREF(old_mro); |
| 282 | |
| 283 | return r; |
| 284 | } |
| 285 | |
| 286 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 287 | type_dict(PyTypeObject *type, void *context) |
| 288 | { |
| 289 | if (type->tp_dict == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 290 | Py_INCREF(Py_None); |
| 291 | return Py_None; |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 292 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 293 | return PyDictProxy_New(type->tp_dict); |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 296 | static PyObject * |
| 297 | type_get_doc(PyTypeObject *type, void *context) |
| 298 | { |
| 299 | PyObject *result; |
Guido van Rossum | 6ca7d41 | 2002-04-18 00:22:00 +0000 | [diff] [blame] | 300 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 301 | return PyString_FromString(type->tp_doc); |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 302 | result = PyDict_GetItemString(type->tp_dict, "__doc__"); |
Guido van Rossum | 6ca7d41 | 2002-04-18 00:22:00 +0000 | [diff] [blame] | 303 | if (result == NULL) { |
| 304 | result = Py_None; |
| 305 | Py_INCREF(result); |
| 306 | } |
| 307 | else if (result->ob_type->tp_descr_get) { |
Tim Peters | 2b85897 | 2002-04-18 04:12:28 +0000 | [diff] [blame] | 308 | result = result->ob_type->tp_descr_get(result, NULL, |
| 309 | (PyObject *)type); |
Guido van Rossum | 6ca7d41 | 2002-04-18 00:22:00 +0000 | [diff] [blame] | 310 | } |
| 311 | else { |
| 312 | Py_INCREF(result); |
| 313 | } |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 314 | return result; |
| 315 | } |
| 316 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 317 | static PyGetSetDef type_getsets[] = { |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 318 | {"__name__", (getter)type_name, (setter)type_set_name, NULL}, |
| 319 | {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL}, |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 320 | {"__module__", (getter)type_module, (setter)type_set_module, NULL}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 321 | {"__dict__", (getter)type_dict, NULL, NULL}, |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 322 | {"__doc__", (getter)type_get_doc, NULL, NULL}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 323 | {0} |
| 324 | }; |
| 325 | |
Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 326 | static int |
| 327 | type_compare(PyObject *v, PyObject *w) |
| 328 | { |
| 329 | /* This is called with type objects only. So we |
| 330 | can just compare the addresses. */ |
| 331 | Py_uintptr_t vv = (Py_uintptr_t)v; |
| 332 | Py_uintptr_t ww = (Py_uintptr_t)w; |
| 333 | return (vv < ww) ? -1 : (vv > ww) ? 1 : 0; |
| 334 | } |
| 335 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 336 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 337 | type_repr(PyTypeObject *type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 338 | { |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 339 | PyObject *mod, *name, *rtn; |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 340 | char *kind; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 341 | |
| 342 | mod = type_module(type, NULL); |
| 343 | if (mod == NULL) |
| 344 | PyErr_Clear(); |
| 345 | else if (!PyString_Check(mod)) { |
| 346 | Py_DECREF(mod); |
| 347 | mod = NULL; |
| 348 | } |
| 349 | name = type_name(type, NULL); |
| 350 | if (name == NULL) |
| 351 | return NULL; |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 352 | |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 353 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 354 | kind = "class"; |
| 355 | else |
| 356 | kind = "type"; |
| 357 | |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 358 | if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) { |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 359 | rtn = PyString_FromFormat("<%s '%s.%s'>", |
| 360 | kind, |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 361 | PyString_AS_STRING(mod), |
| 362 | PyString_AS_STRING(name)); |
| 363 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 364 | else |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 365 | rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 366 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 367 | Py_XDECREF(mod); |
| 368 | Py_DECREF(name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 369 | return rtn; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 372 | static PyObject * |
| 373 | type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 374 | { |
| 375 | PyObject *obj; |
| 376 | |
| 377 | if (type->tp_new == NULL) { |
| 378 | PyErr_Format(PyExc_TypeError, |
| 379 | "cannot create '%.100s' instances", |
| 380 | type->tp_name); |
| 381 | return NULL; |
| 382 | } |
| 383 | |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 384 | obj = type->tp_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 385 | if (obj != NULL) { |
Guido van Rossum | f76de62 | 2001-10-18 15:49:21 +0000 | [diff] [blame] | 386 | /* Ugly exception: when the call was type(something), |
| 387 | don't call tp_init on the result. */ |
| 388 | if (type == &PyType_Type && |
| 389 | PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && |
| 390 | (kwds == NULL || |
| 391 | (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) |
| 392 | return obj; |
Guido van Rossum | 8ace1ab | 2002-04-06 01:05:01 +0000 | [diff] [blame] | 393 | /* If the returned object is not an instance of type, |
| 394 | it won't be initialized. */ |
| 395 | if (!PyType_IsSubtype(obj->ob_type, type)) |
| 396 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 397 | type = obj->ob_type; |
Jeremy Hylton | 719841e | 2002-07-16 19:39:38 +0000 | [diff] [blame] | 398 | if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) && |
| 399 | type->tp_init != NULL && |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 400 | type->tp_init(obj, args, kwds) < 0) { |
| 401 | Py_DECREF(obj); |
| 402 | obj = NULL; |
| 403 | } |
| 404 | } |
| 405 | return obj; |
| 406 | } |
| 407 | |
| 408 | PyObject * |
| 409 | PyType_GenericAlloc(PyTypeObject *type, int nitems) |
| 410 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 411 | PyObject *obj; |
Tim Peters | f2a67da | 2001-10-07 03:54:51 +0000 | [diff] [blame] | 412 | const size_t size = _PyObject_VAR_SIZE(type, nitems); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 413 | |
| 414 | if (PyType_IS_GC(type)) |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 415 | obj = _PyObject_GC_Malloc(size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 416 | else |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 417 | obj = PyObject_MALLOC(size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 418 | |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 419 | if (obj == NULL) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 420 | return PyErr_NoMemory(); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 421 | |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 422 | memset(obj, '\0', size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 423 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 424 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 425 | Py_INCREF(type); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 426 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 427 | if (type->tp_itemsize == 0) |
| 428 | PyObject_INIT(obj, type); |
| 429 | else |
| 430 | (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 431 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 432 | if (PyType_IS_GC(type)) |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 433 | _PyObject_GC_TRACK(obj); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 434 | return obj; |
| 435 | } |
| 436 | |
| 437 | PyObject * |
| 438 | PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 439 | { |
| 440 | return type->tp_alloc(type, 0); |
| 441 | } |
| 442 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 443 | /* Helpers for subtyping */ |
| 444 | |
| 445 | static int |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 446 | traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg) |
| 447 | { |
| 448 | int i, n; |
| 449 | PyMemberDef *mp; |
| 450 | |
| 451 | n = type->ob_size; |
| 452 | mp = ((etype *)type)->members; |
| 453 | for (i = 0; i < n; i++, mp++) { |
| 454 | if (mp->type == T_OBJECT_EX) { |
| 455 | char *addr = (char *)self + mp->offset; |
| 456 | PyObject *obj = *(PyObject **)addr; |
| 457 | if (obj != NULL) { |
| 458 | int err = visit(obj, arg); |
| 459 | if (err) |
| 460 | return err; |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | static int |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 468 | subtype_traverse(PyObject *self, visitproc visit, void *arg) |
| 469 | { |
| 470 | PyTypeObject *type, *base; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 471 | traverseproc basetraverse; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 472 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 473 | /* Find the nearest base with a different tp_traverse, |
| 474 | and traverse slots while we're at it */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 475 | type = self->ob_type; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 476 | base = type; |
| 477 | while ((basetraverse = base->tp_traverse) == subtype_traverse) { |
| 478 | if (base->ob_size) { |
| 479 | int err = traverse_slots(base, self, visit, arg); |
| 480 | if (err) |
| 481 | return err; |
| 482 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 483 | base = base->tp_base; |
| 484 | assert(base); |
| 485 | } |
| 486 | |
| 487 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 488 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 489 | if (dictptr && *dictptr) { |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 490 | int err = visit(*dictptr, arg); |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 491 | if (err) |
| 492 | return err; |
| 493 | } |
| 494 | } |
| 495 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 496 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 497 | /* For a heaptype, the instances count as references |
| 498 | to the type. Traverse the type so the collector |
| 499 | can find cycles involving this link. */ |
| 500 | int err = visit((PyObject *)type, arg); |
| 501 | if (err) |
| 502 | return err; |
| 503 | } |
| 504 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 505 | if (basetraverse) |
| 506 | return basetraverse(self, visit, arg); |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | static void |
| 511 | clear_slots(PyTypeObject *type, PyObject *self) |
| 512 | { |
| 513 | int i, n; |
| 514 | PyMemberDef *mp; |
| 515 | |
| 516 | n = type->ob_size; |
| 517 | mp = ((etype *)type)->members; |
| 518 | for (i = 0; i < n; i++, mp++) { |
| 519 | if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) { |
| 520 | char *addr = (char *)self + mp->offset; |
| 521 | PyObject *obj = *(PyObject **)addr; |
| 522 | if (obj != NULL) { |
| 523 | Py_DECREF(obj); |
| 524 | *(PyObject **)addr = NULL; |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | static int |
| 531 | subtype_clear(PyObject *self) |
| 532 | { |
| 533 | PyTypeObject *type, *base; |
| 534 | inquiry baseclear; |
| 535 | |
| 536 | /* Find the nearest base with a different tp_clear |
| 537 | and clear slots while we're at it */ |
| 538 | type = self->ob_type; |
| 539 | base = type; |
| 540 | while ((baseclear = base->tp_clear) == subtype_clear) { |
| 541 | if (base->ob_size) |
| 542 | clear_slots(base, self); |
| 543 | base = base->tp_base; |
| 544 | assert(base); |
| 545 | } |
| 546 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 547 | /* There's no need to clear the instance dict (if any); |
| 548 | the collector will call its tp_clear handler. */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 549 | |
| 550 | if (baseclear) |
| 551 | return baseclear(self); |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 552 | return 0; |
| 553 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 554 | |
| 555 | static void |
| 556 | subtype_dealloc(PyObject *self) |
| 557 | { |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 558 | PyTypeObject *type, *base; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 559 | destructor basedealloc; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 560 | |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 561 | /* Extract the type; we expect it to be a heap type */ |
| 562 | type = self->ob_type; |
| 563 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 564 | |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 565 | /* Test whether the type has GC exactly once */ |
| 566 | |
| 567 | if (!PyType_IS_GC(type)) { |
| 568 | /* It's really rare to find a dynamic type that doesn't have |
| 569 | GC; it can only happen when deriving from 'object' and not |
| 570 | adding any slots or instance variables. This allows |
| 571 | certain simplifications: there's no need to call |
| 572 | clear_slots(), or DECREF the dict, or clear weakrefs. */ |
| 573 | |
| 574 | /* Maybe call finalizer; exit early if resurrected */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 575 | if (type->tp_del) { |
| 576 | type->tp_del(self); |
| 577 | if (self->ob_refcnt > 0) |
| 578 | return; |
| 579 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 580 | |
| 581 | /* Find the nearest base with a different tp_dealloc */ |
| 582 | base = type; |
| 583 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 584 | assert(base->ob_size == 0); |
| 585 | base = base->tp_base; |
| 586 | assert(base); |
| 587 | } |
| 588 | |
| 589 | /* Call the base tp_dealloc() */ |
| 590 | assert(basedealloc); |
| 591 | basedealloc(self); |
| 592 | |
| 593 | /* Can't reference self beyond this point */ |
| 594 | Py_DECREF(type); |
| 595 | |
| 596 | /* Done */ |
| 597 | return; |
| 598 | } |
| 599 | |
| 600 | /* We get here only if the type has GC */ |
| 601 | |
| 602 | /* UnTrack and re-Track around the trashcan macro, alas */ |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 603 | PyObject_GC_UnTrack(self); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 604 | Py_TRASHCAN_SAFE_BEGIN(self); |
| 605 | _PyObject_GC_TRACK(self); /* We'll untrack for real later */ |
| 606 | |
| 607 | /* Maybe call finalizer; exit early if resurrected */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 608 | if (type->tp_del) { |
| 609 | type->tp_del(self); |
| 610 | if (self->ob_refcnt > 0) |
| 611 | goto endlabel; |
| 612 | } |
Guido van Rossum | 7ad2d1e | 2001-10-29 22:11:00 +0000 | [diff] [blame] | 613 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 614 | /* Find the nearest base with a different tp_dealloc |
| 615 | and clear slots while we're at it */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 616 | base = type; |
| 617 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 618 | if (base->ob_size) |
| 619 | clear_slots(base, self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 620 | base = base->tp_base; |
| 621 | assert(base); |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 624 | /* If we added a dict, DECREF it */ |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 625 | if (type->tp_dictoffset && !base->tp_dictoffset) { |
| 626 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 627 | if (dictptr != NULL) { |
| 628 | PyObject *dict = *dictptr; |
| 629 | if (dict != NULL) { |
| 630 | Py_DECREF(dict); |
| 631 | *dictptr = NULL; |
| 632 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 636 | /* If we added weaklist, we clear it */ |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 637 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 638 | PyObject_ClearWeakRefs(self); |
| 639 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 640 | /* Finalize GC if the base doesn't do GC and we do */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 641 | if (!PyType_IS_GC(base)) |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 642 | _PyObject_GC_UNTRACK(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 643 | |
| 644 | /* Call the base tp_dealloc() */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 645 | assert(basedealloc); |
| 646 | basedealloc(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 647 | |
| 648 | /* Can't reference self beyond this point */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 649 | Py_DECREF(type); |
| 650 | |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 651 | endlabel: |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 652 | Py_TRASHCAN_SAFE_END(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 655 | static PyTypeObject *solid_base(PyTypeObject *type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 656 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 657 | /* type test with subclassing support */ |
| 658 | |
| 659 | int |
| 660 | PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
| 661 | { |
| 662 | PyObject *mro; |
| 663 | |
Guido van Rossum | 9478d07 | 2001-09-07 18:52:13 +0000 | [diff] [blame] | 664 | if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS)) |
| 665 | return b == a || b == &PyBaseObject_Type; |
| 666 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 667 | mro = a->tp_mro; |
| 668 | if (mro != NULL) { |
| 669 | /* Deal with multiple inheritance without recursion |
| 670 | by walking the MRO tuple */ |
| 671 | int i, n; |
| 672 | assert(PyTuple_Check(mro)); |
| 673 | n = PyTuple_GET_SIZE(mro); |
| 674 | for (i = 0; i < n; i++) { |
| 675 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) |
| 676 | return 1; |
| 677 | } |
| 678 | return 0; |
| 679 | } |
| 680 | else { |
| 681 | /* a is not completely initilized yet; follow tp_base */ |
| 682 | do { |
| 683 | if (a == b) |
| 684 | return 1; |
| 685 | a = a->tp_base; |
| 686 | } while (a != NULL); |
| 687 | return b == &PyBaseObject_Type; |
| 688 | } |
| 689 | } |
| 690 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 691 | /* Internal routines to do a method lookup in the type |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 692 | without looking in the instance dictionary |
| 693 | (so we can't use PyObject_GetAttr) but still binding |
| 694 | it to the instance. The arguments are the object, |
| 695 | the method name as a C string, and the address of a |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 696 | static variable used to cache the interned Python string. |
| 697 | |
| 698 | Two variants: |
| 699 | |
| 700 | - lookup_maybe() returns NULL without raising an exception |
| 701 | when the _PyType_Lookup() call fails; |
| 702 | |
| 703 | - lookup_method() always raises an exception upon errors. |
| 704 | */ |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 705 | |
| 706 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 707 | lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 708 | { |
| 709 | PyObject *res; |
| 710 | |
| 711 | if (*attrobj == NULL) { |
| 712 | *attrobj = PyString_InternFromString(attrstr); |
| 713 | if (*attrobj == NULL) |
| 714 | return NULL; |
| 715 | } |
| 716 | res = _PyType_Lookup(self->ob_type, *attrobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 717 | if (res != NULL) { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 718 | descrgetfunc f; |
| 719 | if ((f = res->ob_type->tp_descr_get) == NULL) |
| 720 | Py_INCREF(res); |
| 721 | else |
| 722 | res = f(res, self, (PyObject *)(self->ob_type)); |
| 723 | } |
| 724 | return res; |
| 725 | } |
| 726 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 727 | static PyObject * |
| 728 | lookup_method(PyObject *self, char *attrstr, PyObject **attrobj) |
| 729 | { |
| 730 | PyObject *res = lookup_maybe(self, attrstr, attrobj); |
| 731 | if (res == NULL && !PyErr_Occurred()) |
| 732 | PyErr_SetObject(PyExc_AttributeError, *attrobj); |
| 733 | return res; |
| 734 | } |
| 735 | |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 736 | /* A variation of PyObject_CallMethod that uses lookup_method() |
| 737 | instead of PyObject_GetAttrString(). This uses the same convention |
| 738 | as lookup_method to cache the interned name string object. */ |
| 739 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 740 | static PyObject * |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 741 | call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 742 | { |
| 743 | va_list va; |
| 744 | PyObject *args, *func = 0, *retval; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 745 | va_start(va, format); |
| 746 | |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 747 | func = lookup_maybe(o, name, nameobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 748 | if (func == NULL) { |
| 749 | va_end(va); |
| 750 | if (!PyErr_Occurred()) |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 751 | PyErr_SetObject(PyExc_AttributeError, *nameobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 752 | return NULL; |
| 753 | } |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 754 | |
| 755 | if (format && *format) |
| 756 | args = Py_VaBuildValue(format, va); |
| 757 | else |
| 758 | args = PyTuple_New(0); |
| 759 | |
| 760 | va_end(va); |
| 761 | |
| 762 | if (args == NULL) |
| 763 | return NULL; |
| 764 | |
| 765 | assert(PyTuple_Check(args)); |
| 766 | retval = PyObject_Call(func, args, NULL); |
| 767 | |
| 768 | Py_DECREF(args); |
| 769 | Py_DECREF(func); |
| 770 | |
| 771 | return retval; |
| 772 | } |
| 773 | |
| 774 | /* Clone of call_method() that returns NotImplemented when the lookup fails. */ |
| 775 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 776 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 777 | call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 778 | { |
| 779 | va_list va; |
| 780 | PyObject *args, *func = 0, *retval; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 781 | va_start(va, format); |
| 782 | |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 783 | func = lookup_maybe(o, name, nameobj); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 784 | if (func == NULL) { |
| 785 | va_end(va); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 786 | if (!PyErr_Occurred()) { |
| 787 | Py_INCREF(Py_NotImplemented); |
| 788 | return Py_NotImplemented; |
| 789 | } |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 790 | return NULL; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | if (format && *format) |
| 794 | args = Py_VaBuildValue(format, va); |
| 795 | else |
| 796 | args = PyTuple_New(0); |
| 797 | |
| 798 | va_end(va); |
| 799 | |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 800 | if (args == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 801 | return NULL; |
| 802 | |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 803 | assert(PyTuple_Check(args)); |
| 804 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 805 | |
| 806 | Py_DECREF(args); |
| 807 | Py_DECREF(func); |
| 808 | |
| 809 | return retval; |
| 810 | } |
| 811 | |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 812 | static int |
| 813 | fill_classic_mro(PyObject *mro, PyObject *cls) |
| 814 | { |
| 815 | PyObject *bases, *base; |
| 816 | int i, n; |
| 817 | |
| 818 | assert(PyList_Check(mro)); |
| 819 | assert(PyClass_Check(cls)); |
| 820 | i = PySequence_Contains(mro, cls); |
| 821 | if (i < 0) |
| 822 | return -1; |
| 823 | if (!i) { |
| 824 | if (PyList_Append(mro, cls) < 0) |
| 825 | return -1; |
| 826 | } |
| 827 | bases = ((PyClassObject *)cls)->cl_bases; |
| 828 | assert(bases && PyTuple_Check(bases)); |
| 829 | n = PyTuple_GET_SIZE(bases); |
| 830 | for (i = 0; i < n; i++) { |
| 831 | base = PyTuple_GET_ITEM(bases, i); |
| 832 | if (fill_classic_mro(mro, base) < 0) |
| 833 | return -1; |
| 834 | } |
| 835 | return 0; |
| 836 | } |
| 837 | |
| 838 | static PyObject * |
| 839 | classic_mro(PyObject *cls) |
| 840 | { |
| 841 | PyObject *mro; |
| 842 | |
| 843 | assert(PyClass_Check(cls)); |
| 844 | mro = PyList_New(0); |
| 845 | if (mro != NULL) { |
| 846 | if (fill_classic_mro(mro, cls) == 0) |
| 847 | return mro; |
| 848 | Py_DECREF(mro); |
| 849 | } |
| 850 | return NULL; |
| 851 | } |
| 852 | |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 853 | /* |
| 854 | Method resolution order algorithm C3 described in |
| 855 | "A Monotonic Superclass Linearization for Dylan", |
| 856 | by Kim Barrett, Bob Cassel, Paul Haahr, |
| 857 | David A. Moon, Keith Playford, and P. Tucker Withington. |
| 858 | (OOPSLA 1996) |
| 859 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 860 | Some notes about the rules implied by C3: |
| 861 | |
| 862 | No duplicate bases. |
| 863 | It isn't legal to repeat a class in a list of base classes. |
| 864 | |
| 865 | The next three properties are the 3 constraints in "C3". |
| 866 | |
| 867 | Local precendece order. |
| 868 | If A precedes B in C's MRO, then A will precede B in the MRO of all |
| 869 | subclasses of C. |
| 870 | |
| 871 | Monotonicity. |
| 872 | The MRO of a class must be an extension without reordering of the |
| 873 | MRO of each of its superclasses. |
| 874 | |
| 875 | Extended Precedence Graph (EPG). |
| 876 | Linearization is consistent if there is a path in the EPG from |
| 877 | each class to all its successors in the linearization. See |
| 878 | the paper for definition of EPG. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 879 | */ |
| 880 | |
| 881 | static int |
| 882 | tail_contains(PyObject *list, int whence, PyObject *o) { |
| 883 | int j, size; |
| 884 | size = PyList_GET_SIZE(list); |
| 885 | |
| 886 | for (j = whence+1; j < size; j++) { |
| 887 | if (PyList_GET_ITEM(list, j) == o) |
| 888 | return 1; |
| 889 | } |
| 890 | return 0; |
| 891 | } |
| 892 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 893 | static PyObject * |
| 894 | class_name(PyObject *cls) |
| 895 | { |
| 896 | PyObject *name = PyObject_GetAttrString(cls, "__name__"); |
| 897 | if (name == NULL) { |
| 898 | PyErr_Clear(); |
| 899 | Py_XDECREF(name); |
| 900 | name = PyObject_Repr(cls); |
| 901 | } |
| 902 | if (name == NULL) |
| 903 | return NULL; |
| 904 | if (!PyString_Check(name)) { |
| 905 | Py_DECREF(name); |
| 906 | return NULL; |
| 907 | } |
| 908 | return name; |
| 909 | } |
| 910 | |
| 911 | static int |
| 912 | check_duplicates(PyObject *list) |
| 913 | { |
| 914 | int i, j, n; |
| 915 | /* Let's use a quadratic time algorithm, |
| 916 | assuming that the bases lists is short. |
| 917 | */ |
| 918 | n = PyList_GET_SIZE(list); |
| 919 | for (i = 0; i < n; i++) { |
| 920 | PyObject *o = PyList_GET_ITEM(list, i); |
| 921 | for (j = i + 1; j < n; j++) { |
| 922 | if (PyList_GET_ITEM(list, j) == o) { |
| 923 | o = class_name(o); |
| 924 | PyErr_Format(PyExc_TypeError, |
| 925 | "duplicate base class %s", |
| 926 | o ? PyString_AS_STRING(o) : "?"); |
| 927 | Py_XDECREF(o); |
| 928 | return -1; |
| 929 | } |
| 930 | } |
| 931 | } |
| 932 | return 0; |
| 933 | } |
| 934 | |
| 935 | /* Raise a TypeError for an MRO order disagreement. |
| 936 | |
| 937 | It's hard to produce a good error message. In the absence of better |
| 938 | insight into error reporting, report the classes that were candidates |
| 939 | to be put next into the MRO. There is some conflict between the |
| 940 | order in which they should be put in the MRO, but it's hard to |
| 941 | diagnose what constraint can't be satisfied. |
| 942 | */ |
| 943 | |
| 944 | static void |
| 945 | set_mro_error(PyObject *to_merge, int *remain) |
| 946 | { |
| 947 | int i, n, off, to_merge_size; |
| 948 | char buf[1000]; |
| 949 | PyObject *k, *v; |
| 950 | PyObject *set = PyDict_New(); |
| 951 | |
| 952 | to_merge_size = PyList_GET_SIZE(to_merge); |
| 953 | for (i = 0; i < to_merge_size; i++) { |
| 954 | PyObject *L = PyList_GET_ITEM(to_merge, i); |
| 955 | if (remain[i] < PyList_GET_SIZE(L)) { |
| 956 | PyObject *c = PyList_GET_ITEM(L, remain[i]); |
| 957 | if (PyDict_SetItem(set, c, Py_None) < 0) |
| 958 | return; |
| 959 | } |
| 960 | } |
| 961 | n = PyDict_Size(set); |
| 962 | |
| 963 | off = PyOS_snprintf(buf, sizeof(buf), "MRO conflict among bases"); |
| 964 | i = 0; |
| 965 | while (PyDict_Next(set, &i, &k, &v) && off < sizeof(buf)) { |
| 966 | PyObject *name = class_name(k); |
| 967 | off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", |
| 968 | name ? PyString_AS_STRING(name) : "?"); |
| 969 | Py_XDECREF(name); |
| 970 | if (--n && off+1 < sizeof(buf)) { |
| 971 | buf[off++] = ','; |
| 972 | buf[off] = '\0'; |
| 973 | } |
| 974 | } |
| 975 | PyErr_SetString(PyExc_TypeError, buf); |
| 976 | Py_DECREF(set); |
| 977 | } |
| 978 | |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 979 | static int |
| 980 | pmerge(PyObject *acc, PyObject* to_merge) { |
| 981 | int i, j, to_merge_size; |
| 982 | int *remain; |
| 983 | int ok, empty_cnt; |
| 984 | |
| 985 | to_merge_size = PyList_GET_SIZE(to_merge); |
| 986 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 987 | /* remain stores an index into each sublist of to_merge. |
| 988 | remain[i] is the index of the next base in to_merge[i] |
| 989 | that is not included in acc. |
| 990 | */ |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 991 | remain = PyMem_MALLOC(SIZEOF_INT*to_merge_size); |
| 992 | if (remain == NULL) |
| 993 | return -1; |
| 994 | for (i = 0; i < to_merge_size; i++) |
| 995 | remain[i] = 0; |
| 996 | |
| 997 | again: |
| 998 | empty_cnt = 0; |
| 999 | for (i = 0; i < to_merge_size; i++) { |
| 1000 | PyObject *candidate; |
| 1001 | |
| 1002 | PyObject *cur_list = PyList_GET_ITEM(to_merge, i); |
| 1003 | |
| 1004 | if (remain[i] >= PyList_GET_SIZE(cur_list)) { |
| 1005 | empty_cnt++; |
| 1006 | continue; |
| 1007 | } |
| 1008 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1009 | /* Choose next candidate for MRO. |
| 1010 | |
| 1011 | The input sequences alone can determine the choice. |
| 1012 | If not, choose the class which appears in the MRO |
| 1013 | of the earliest direct superclass of the new class. |
| 1014 | */ |
| 1015 | |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1016 | candidate = PyList_GET_ITEM(cur_list, remain[i]); |
| 1017 | for (j = 0; j < to_merge_size; j++) { |
| 1018 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1019 | if (tail_contains(j_lst, remain[j], candidate)) { |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1020 | goto skip; /* continue outer loop */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1021 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1022 | } |
| 1023 | ok = PyList_Append(acc, candidate); |
| 1024 | if (ok < 0) { |
| 1025 | PyMem_Free(remain); |
| 1026 | return -1; |
| 1027 | } |
| 1028 | for (j = 0; j < to_merge_size; j++) { |
| 1029 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
| 1030 | if (PyList_GET_ITEM(j_lst, remain[j]) == candidate) { |
| 1031 | remain[j]++; |
| 1032 | } |
| 1033 | } |
| 1034 | goto again; |
Tim Peters | 9a6b8d8 | 2002-11-14 23:22:33 +0000 | [diff] [blame] | 1035 | skip: ; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1038 | if (empty_cnt == to_merge_size) { |
| 1039 | PyMem_FREE(remain); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1040 | return 0; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1041 | } |
| 1042 | set_mro_error(to_merge, remain); |
| 1043 | PyMem_FREE(remain); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1044 | return -1; |
| 1045 | } |
| 1046 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1047 | static PyObject * |
| 1048 | mro_implementation(PyTypeObject *type) |
| 1049 | { |
| 1050 | int i, n, ok; |
| 1051 | PyObject *bases, *result; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1052 | PyObject *to_merge, *bases_aslist; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1053 | |
Guido van Rossum | 6351757 | 2002-06-18 16:44:57 +0000 | [diff] [blame] | 1054 | if(type->tp_dict == NULL) { |
| 1055 | if(PyType_Ready(type) < 0) |
| 1056 | return NULL; |
| 1057 | } |
| 1058 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1059 | /* Find a superclass linearization that honors the constraints |
| 1060 | of the explicit lists of bases and the constraints implied by |
| 1061 | each base class. |
| 1062 | |
| 1063 | to_merge is a list of lists, where each list is a superclass |
| 1064 | linearization implied by a base class. The last element of |
| 1065 | to_merge is the declared list of bases. |
| 1066 | */ |
| 1067 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1068 | bases = type->tp_bases; |
| 1069 | n = PyTuple_GET_SIZE(bases); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1070 | |
| 1071 | to_merge = PyList_New(n+1); |
| 1072 | if (to_merge == NULL) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1073 | return NULL; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1074 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1075 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1076 | PyObject *base = PyTuple_GET_ITEM(bases, i); |
| 1077 | PyObject *parentMRO; |
| 1078 | if (PyType_Check(base)) |
| 1079 | parentMRO = PySequence_List( |
| 1080 | ((PyTypeObject*)base)->tp_mro); |
| 1081 | else |
| 1082 | parentMRO = classic_mro(base); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1083 | if (parentMRO == NULL) { |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1084 | Py_DECREF(to_merge); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1085 | return NULL; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | PyList_SET_ITEM(to_merge, i, parentMRO); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1089 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1090 | |
| 1091 | bases_aslist = PySequence_List(bases); |
| 1092 | if (bases_aslist == NULL) { |
| 1093 | Py_DECREF(to_merge); |
| 1094 | return NULL; |
| 1095 | } |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1096 | /* This is just a basic sanity check. */ |
| 1097 | if (check_duplicates(bases_aslist) < 0) { |
| 1098 | Py_DECREF(to_merge); |
| 1099 | Py_DECREF(bases_aslist); |
| 1100 | return NULL; |
| 1101 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1102 | PyList_SET_ITEM(to_merge, n, bases_aslist); |
| 1103 | |
| 1104 | result = Py_BuildValue("[O]", (PyObject *)type); |
| 1105 | if (result == NULL) { |
| 1106 | Py_DECREF(to_merge); |
| 1107 | return NULL; |
| 1108 | } |
| 1109 | |
| 1110 | ok = pmerge(result, to_merge); |
| 1111 | Py_DECREF(to_merge); |
| 1112 | if (ok < 0) { |
| 1113 | Py_DECREF(result); |
| 1114 | return NULL; |
| 1115 | } |
| 1116 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1117 | return result; |
| 1118 | } |
| 1119 | |
| 1120 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1121 | mro_external(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1122 | { |
| 1123 | PyTypeObject *type = (PyTypeObject *)self; |
| 1124 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1125 | return mro_implementation(type); |
| 1126 | } |
| 1127 | |
| 1128 | static int |
| 1129 | mro_internal(PyTypeObject *type) |
| 1130 | { |
| 1131 | PyObject *mro, *result, *tuple; |
| 1132 | |
| 1133 | if (type->ob_type == &PyType_Type) { |
| 1134 | result = mro_implementation(type); |
| 1135 | } |
| 1136 | else { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1137 | static PyObject *mro_str; |
| 1138 | mro = lookup_method((PyObject *)type, "mro", &mro_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1139 | if (mro == NULL) |
| 1140 | return -1; |
| 1141 | result = PyObject_CallObject(mro, NULL); |
| 1142 | Py_DECREF(mro); |
| 1143 | } |
| 1144 | if (result == NULL) |
| 1145 | return -1; |
| 1146 | tuple = PySequence_Tuple(result); |
| 1147 | Py_DECREF(result); |
| 1148 | type->tp_mro = tuple; |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | |
| 1153 | /* Calculate the best base amongst multiple base classes. |
| 1154 | This is the first one that's on the path to the "solid base". */ |
| 1155 | |
| 1156 | static PyTypeObject * |
| 1157 | best_base(PyObject *bases) |
| 1158 | { |
| 1159 | int i, n; |
| 1160 | PyTypeObject *base, *winner, *candidate, *base_i; |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1161 | PyObject *base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1162 | |
| 1163 | assert(PyTuple_Check(bases)); |
| 1164 | n = PyTuple_GET_SIZE(bases); |
| 1165 | assert(n > 0); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1166 | base = NULL; |
| 1167 | winner = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1168 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1169 | base_proto = PyTuple_GET_ITEM(bases, i); |
| 1170 | if (PyClass_Check(base_proto)) |
| 1171 | continue; |
| 1172 | if (!PyType_Check(base_proto)) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1173 | PyErr_SetString( |
| 1174 | PyExc_TypeError, |
| 1175 | "bases must be types"); |
| 1176 | return NULL; |
| 1177 | } |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1178 | base_i = (PyTypeObject *)base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1179 | if (base_i->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1180 | if (PyType_Ready(base_i) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1181 | return NULL; |
| 1182 | } |
| 1183 | candidate = solid_base(base_i); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1184 | if (winner == NULL) { |
| 1185 | winner = candidate; |
| 1186 | base = base_i; |
| 1187 | } |
| 1188 | else if (PyType_IsSubtype(winner, candidate)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1189 | ; |
| 1190 | else if (PyType_IsSubtype(candidate, winner)) { |
| 1191 | winner = candidate; |
| 1192 | base = base_i; |
| 1193 | } |
| 1194 | else { |
| 1195 | PyErr_SetString( |
| 1196 | PyExc_TypeError, |
| 1197 | "multiple bases have " |
| 1198 | "instance lay-out conflict"); |
| 1199 | return NULL; |
| 1200 | } |
| 1201 | } |
Guido van Rossum | e54616c | 2001-12-14 04:19:56 +0000 | [diff] [blame] | 1202 | if (base == NULL) |
| 1203 | PyErr_SetString(PyExc_TypeError, |
| 1204 | "a new-style class can't have only classic bases"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1205 | return base; |
| 1206 | } |
| 1207 | |
| 1208 | static int |
| 1209 | extra_ivars(PyTypeObject *type, PyTypeObject *base) |
| 1210 | { |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 1211 | size_t t_size = type->tp_basicsize; |
| 1212 | size_t b_size = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1213 | |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1214 | assert(t_size >= b_size); /* Else type smaller than base! */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1215 | if (type->tp_itemsize || base->tp_itemsize) { |
| 1216 | /* If itemsize is involved, stricter rules */ |
| 1217 | return t_size != b_size || |
| 1218 | type->tp_itemsize != base->tp_itemsize; |
| 1219 | } |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1220 | if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 && |
| 1221 | type->tp_weaklistoffset + sizeof(PyObject *) == t_size) |
| 1222 | t_size -= sizeof(PyObject *); |
| 1223 | if (type->tp_dictoffset && base->tp_dictoffset == 0 && |
| 1224 | type->tp_dictoffset + sizeof(PyObject *) == t_size) |
| 1225 | t_size -= sizeof(PyObject *); |
| 1226 | |
| 1227 | return t_size != b_size; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | static PyTypeObject * |
| 1231 | solid_base(PyTypeObject *type) |
| 1232 | { |
| 1233 | PyTypeObject *base; |
| 1234 | |
| 1235 | if (type->tp_base) |
| 1236 | base = solid_base(type->tp_base); |
| 1237 | else |
| 1238 | base = &PyBaseObject_Type; |
| 1239 | if (extra_ivars(type, base)) |
| 1240 | return type; |
| 1241 | else |
| 1242 | return base; |
| 1243 | } |
| 1244 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1245 | static void object_dealloc(PyObject *); |
| 1246 | static int object_init(PyObject *, PyObject *, PyObject *); |
| 1247 | static int update_slot(PyTypeObject *, PyObject *); |
| 1248 | static void fixup_slot_dispatchers(PyTypeObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1249 | |
| 1250 | static PyObject * |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1251 | subtype_dict(PyObject *obj, void *context) |
| 1252 | { |
| 1253 | PyObject **dictptr = _PyObject_GetDictPtr(obj); |
| 1254 | PyObject *dict; |
| 1255 | |
| 1256 | if (dictptr == NULL) { |
| 1257 | PyErr_SetString(PyExc_AttributeError, |
| 1258 | "This object has no __dict__"); |
| 1259 | return NULL; |
| 1260 | } |
| 1261 | dict = *dictptr; |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 1262 | if (dict == NULL) |
| 1263 | *dictptr = dict = PyDict_New(); |
| 1264 | Py_XINCREF(dict); |
| 1265 | return dict; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1268 | static int |
| 1269 | subtype_setdict(PyObject *obj, PyObject *value, void *context) |
| 1270 | { |
| 1271 | PyObject **dictptr = _PyObject_GetDictPtr(obj); |
| 1272 | PyObject *dict; |
| 1273 | |
| 1274 | if (dictptr == NULL) { |
| 1275 | PyErr_SetString(PyExc_AttributeError, |
| 1276 | "This object has no __dict__"); |
| 1277 | return -1; |
| 1278 | } |
Guido van Rossum | d331cb5 | 2001-12-05 19:46:42 +0000 | [diff] [blame] | 1279 | if (value != NULL && !PyDict_Check(value)) { |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1280 | PyErr_SetString(PyExc_TypeError, |
| 1281 | "__dict__ must be set to a dictionary"); |
| 1282 | return -1; |
| 1283 | } |
| 1284 | dict = *dictptr; |
Guido van Rossum | d331cb5 | 2001-12-05 19:46:42 +0000 | [diff] [blame] | 1285 | Py_XINCREF(value); |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1286 | *dictptr = value; |
| 1287 | Py_XDECREF(dict); |
| 1288 | return 0; |
| 1289 | } |
| 1290 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1291 | static PyObject * |
| 1292 | subtype_getweakref(PyObject *obj, void *context) |
| 1293 | { |
| 1294 | PyObject **weaklistptr; |
| 1295 | PyObject *result; |
| 1296 | |
| 1297 | if (obj->ob_type->tp_weaklistoffset == 0) { |
| 1298 | PyErr_SetString(PyExc_AttributeError, |
| 1299 | "This object has no __weaklist__"); |
| 1300 | return NULL; |
| 1301 | } |
| 1302 | assert(obj->ob_type->tp_weaklistoffset > 0); |
| 1303 | assert(obj->ob_type->tp_weaklistoffset + sizeof(PyObject *) <= |
Guido van Rossum | 3747a0f | 2002-08-12 19:25:08 +0000 | [diff] [blame] | 1304 | (size_t)(obj->ob_type->tp_basicsize)); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1305 | weaklistptr = (PyObject **) |
Guido van Rossum | 3747a0f | 2002-08-12 19:25:08 +0000 | [diff] [blame] | 1306 | ((char *)obj + obj->ob_type->tp_weaklistoffset); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1307 | if (*weaklistptr == NULL) |
| 1308 | result = Py_None; |
| 1309 | else |
| 1310 | result = *weaklistptr; |
| 1311 | Py_INCREF(result); |
| 1312 | return result; |
| 1313 | } |
| 1314 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1315 | static PyGetSetDef subtype_getsets[] = { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1316 | /* Not all objects have these attributes! |
| 1317 | The descriptor's __get__ method may raise AttributeError. */ |
| 1318 | {"__dict__", subtype_dict, subtype_setdict, |
Neal Norwitz | 858e34f | 2002-08-13 17:18:45 +0000 | [diff] [blame] | 1319 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1320 | {"__weakref__", subtype_getweakref, NULL, |
Neal Norwitz | 858e34f | 2002-08-13 17:18:45 +0000 | [diff] [blame] | 1321 | PyDoc_STR("list of weak references to the object (if defined)")}, |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1322 | {0} |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1323 | }; |
| 1324 | |
Guido van Rossum | 0628dcf | 2002-03-14 23:03:14 +0000 | [diff] [blame] | 1325 | /* bozo: __getstate__ that raises TypeError */ |
| 1326 | |
| 1327 | static PyObject * |
| 1328 | bozo_func(PyObject *self, PyObject *args) |
| 1329 | { |
| 1330 | PyErr_SetString(PyExc_TypeError, |
| 1331 | "a class that defines __slots__ without " |
| 1332 | "defining __getstate__ cannot be pickled"); |
| 1333 | return NULL; |
| 1334 | } |
| 1335 | |
Neal Norwitz | 93c1e23 | 2002-03-31 16:06:11 +0000 | [diff] [blame] | 1336 | static PyMethodDef bozo_ml = {"__getstate__", bozo_func, METH_VARARGS}; |
Guido van Rossum | 0628dcf | 2002-03-14 23:03:14 +0000 | [diff] [blame] | 1337 | |
| 1338 | static PyObject *bozo_obj = NULL; |
| 1339 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1340 | static int |
| 1341 | valid_identifier(PyObject *s) |
| 1342 | { |
Guido van Rossum | 03013a0 | 2002-07-16 14:30:28 +0000 | [diff] [blame] | 1343 | unsigned char *p; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1344 | int i, n; |
| 1345 | |
| 1346 | if (!PyString_Check(s)) { |
| 1347 | PyErr_SetString(PyExc_TypeError, |
| 1348 | "__slots__ must be strings"); |
| 1349 | return 0; |
| 1350 | } |
Guido van Rossum | 03013a0 | 2002-07-16 14:30:28 +0000 | [diff] [blame] | 1351 | p = (unsigned char *) PyString_AS_STRING(s); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1352 | n = PyString_GET_SIZE(s); |
| 1353 | /* We must reject an empty name. As a hack, we bump the |
| 1354 | length to 1 so that the loop will balk on the trailing \0. */ |
| 1355 | if (n == 0) |
| 1356 | n = 1; |
| 1357 | for (i = 0; i < n; i++, p++) { |
| 1358 | if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') { |
| 1359 | PyErr_SetString(PyExc_TypeError, |
| 1360 | "__slots__ must be identifiers"); |
| 1361 | return 0; |
| 1362 | } |
| 1363 | } |
| 1364 | return 1; |
| 1365 | } |
| 1366 | |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1367 | #ifdef Py_USING_UNICODE |
| 1368 | /* Replace Unicode objects in slots. */ |
| 1369 | |
| 1370 | static PyObject * |
| 1371 | _unicode_to_string(PyObject *slots, int nslots) |
| 1372 | { |
| 1373 | PyObject *tmp = slots; |
| 1374 | PyObject *o, *o1; |
| 1375 | int i; |
| 1376 | intintargfunc copy = slots->ob_type->tp_as_sequence->sq_slice; |
| 1377 | for (i = 0; i < nslots; i++) { |
| 1378 | if (PyUnicode_Check(o = PyTuple_GET_ITEM(tmp, i))) { |
| 1379 | if (tmp == slots) { |
| 1380 | tmp = copy(slots, 0, PyTuple_GET_SIZE(slots)); |
| 1381 | if (tmp == NULL) |
| 1382 | return NULL; |
| 1383 | } |
| 1384 | o1 = _PyUnicode_AsDefaultEncodedString |
| 1385 | (o, NULL); |
| 1386 | if (o1 == NULL) { |
| 1387 | Py_DECREF(tmp); |
| 1388 | return 0; |
| 1389 | } |
| 1390 | Py_INCREF(o1); |
| 1391 | Py_DECREF(o); |
| 1392 | PyTuple_SET_ITEM(tmp, i, o1); |
| 1393 | } |
| 1394 | } |
| 1395 | return tmp; |
| 1396 | } |
| 1397 | #endif |
| 1398 | |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1399 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1400 | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) |
| 1401 | { |
| 1402 | PyObject *name, *bases, *dict; |
| 1403 | static char *kwlist[] = {"name", "bases", "dict", 0}; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1404 | PyObject *slots, *tmp, *newslots; |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1405 | PyTypeObject *type, *base, *tmptype, *winner; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1406 | etype *et; |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 1407 | PyMemberDef *mp; |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1408 | int i, nbases, nslots, slotoffset, add_dict, add_weak; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1409 | int j, may_add_dict, may_add_weak; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1410 | |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1411 | assert(args != NULL && PyTuple_Check(args)); |
| 1412 | assert(kwds == NULL || PyDict_Check(kwds)); |
| 1413 | |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1414 | /* Special case: type(x) should return x->ob_type */ |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1415 | { |
| 1416 | const int nargs = PyTuple_GET_SIZE(args); |
| 1417 | const int nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); |
| 1418 | |
| 1419 | if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) { |
| 1420 | PyObject *x = PyTuple_GET_ITEM(args, 0); |
| 1421 | Py_INCREF(x->ob_type); |
| 1422 | return (PyObject *) x->ob_type; |
| 1423 | } |
| 1424 | |
| 1425 | /* SF bug 475327 -- if that didn't trigger, we need 3 |
| 1426 | arguments. but PyArg_ParseTupleAndKeywords below may give |
| 1427 | a msg saying type() needs exactly 3. */ |
| 1428 | if (nargs + nkwds != 3) { |
| 1429 | PyErr_SetString(PyExc_TypeError, |
| 1430 | "type() takes 1 or 3 arguments"); |
| 1431 | return NULL; |
| 1432 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1435 | /* Check arguments: (name, bases, dict) */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1436 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist, |
| 1437 | &name, |
| 1438 | &PyTuple_Type, &bases, |
| 1439 | &PyDict_Type, &dict)) |
| 1440 | return NULL; |
| 1441 | |
| 1442 | /* Determine the proper metatype to deal with this, |
| 1443 | and check for metatype conflicts while we're at it. |
| 1444 | Note that if some other metatype wins to contract, |
| 1445 | it's possible that its instances are not types. */ |
| 1446 | nbases = PyTuple_GET_SIZE(bases); |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1447 | winner = metatype; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1448 | for (i = 0; i < nbases; i++) { |
| 1449 | tmp = PyTuple_GET_ITEM(bases, i); |
| 1450 | tmptype = tmp->ob_type; |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1451 | if (tmptype == &PyClass_Type) |
| 1452 | continue; /* Special case classic classes */ |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1453 | if (PyType_IsSubtype(winner, tmptype)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1454 | continue; |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1455 | if (PyType_IsSubtype(tmptype, winner)) { |
| 1456 | winner = tmptype; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1457 | continue; |
| 1458 | } |
| 1459 | PyErr_SetString(PyExc_TypeError, |
| 1460 | "metatype conflict among bases"); |
| 1461 | return NULL; |
| 1462 | } |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1463 | if (winner != metatype) { |
| 1464 | if (winner->tp_new != type_new) /* Pass it to the winner */ |
| 1465 | return winner->tp_new(winner, args, kwds); |
| 1466 | metatype = winner; |
| 1467 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1468 | |
| 1469 | /* Adjust for empty tuple bases */ |
| 1470 | if (nbases == 0) { |
| 1471 | bases = Py_BuildValue("(O)", &PyBaseObject_Type); |
| 1472 | if (bases == NULL) |
| 1473 | return NULL; |
| 1474 | nbases = 1; |
| 1475 | } |
| 1476 | else |
| 1477 | Py_INCREF(bases); |
| 1478 | |
| 1479 | /* XXX From here until type is allocated, "return NULL" leaks bases! */ |
| 1480 | |
| 1481 | /* Calculate best base, and check that all bases are type objects */ |
| 1482 | base = best_base(bases); |
| 1483 | if (base == NULL) |
| 1484 | return NULL; |
| 1485 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 1486 | PyErr_Format(PyExc_TypeError, |
| 1487 | "type '%.100s' is not an acceptable base type", |
| 1488 | base->tp_name); |
| 1489 | return NULL; |
| 1490 | } |
| 1491 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1492 | /* Check for a __slots__ sequence variable in dict, and count it */ |
| 1493 | slots = PyDict_GetItemString(dict, "__slots__"); |
| 1494 | nslots = 0; |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1495 | add_dict = 0; |
| 1496 | add_weak = 0; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1497 | may_add_dict = base->tp_dictoffset == 0; |
| 1498 | may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0; |
| 1499 | if (slots == NULL) { |
| 1500 | if (may_add_dict) { |
| 1501 | add_dict++; |
| 1502 | } |
| 1503 | if (may_add_weak) { |
| 1504 | add_weak++; |
| 1505 | } |
| 1506 | } |
| 1507 | else { |
| 1508 | /* Have slots */ |
| 1509 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1510 | /* Make it into a tuple */ |
| 1511 | if (PyString_Check(slots)) |
| 1512 | slots = Py_BuildValue("(O)", slots); |
| 1513 | else |
| 1514 | slots = PySequence_Tuple(slots); |
| 1515 | if (slots == NULL) |
| 1516 | return NULL; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1517 | assert(PyTuple_Check(slots)); |
| 1518 | |
| 1519 | /* Are slots allowed? */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1520 | nslots = PyTuple_GET_SIZE(slots); |
Guido van Rossum | c414187 | 2001-08-30 04:43:35 +0000 | [diff] [blame] | 1521 | if (nslots > 0 && base->tp_itemsize != 0) { |
| 1522 | PyErr_Format(PyExc_TypeError, |
| 1523 | "nonempty __slots__ " |
| 1524 | "not supported for subtype of '%s'", |
| 1525 | base->tp_name); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1526 | bad_slots: |
| 1527 | Py_DECREF(slots); |
Guido van Rossum | c414187 | 2001-08-30 04:43:35 +0000 | [diff] [blame] | 1528 | return NULL; |
| 1529 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1530 | |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1531 | #ifdef Py_USING_UNICODE |
| 1532 | tmp = _unicode_to_string(slots, nslots); |
Martin v. Löwis | 13b1a5c | 2002-10-14 21:11:34 +0000 | [diff] [blame] | 1533 | if (tmp != slots) { |
| 1534 | Py_DECREF(slots); |
| 1535 | slots = tmp; |
| 1536 | } |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1537 | if (!tmp) |
| 1538 | return NULL; |
| 1539 | #endif |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1540 | /* Check for valid slot names and two special cases */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1541 | for (i = 0; i < nslots; i++) { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1542 | PyObject *tmp = PyTuple_GET_ITEM(slots, i); |
| 1543 | char *s; |
| 1544 | if (!valid_identifier(tmp)) |
| 1545 | goto bad_slots; |
| 1546 | assert(PyString_Check(tmp)); |
| 1547 | s = PyString_AS_STRING(tmp); |
| 1548 | if (strcmp(s, "__dict__") == 0) { |
| 1549 | if (!may_add_dict || add_dict) { |
| 1550 | PyErr_SetString(PyExc_TypeError, |
| 1551 | "__dict__ slot disallowed: " |
| 1552 | "we already got one"); |
| 1553 | goto bad_slots; |
| 1554 | } |
| 1555 | add_dict++; |
| 1556 | } |
| 1557 | if (strcmp(s, "__weakref__") == 0) { |
| 1558 | if (!may_add_weak || add_weak) { |
| 1559 | PyErr_SetString(PyExc_TypeError, |
| 1560 | "__weakref__ slot disallowed: " |
| 1561 | "either we already got one, " |
| 1562 | "or __itemsize__ != 0"); |
| 1563 | goto bad_slots; |
| 1564 | } |
| 1565 | add_weak++; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1566 | } |
| 1567 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1568 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1569 | /* Copy slots into yet another tuple, demangling names */ |
| 1570 | newslots = PyTuple_New(nslots - add_dict - add_weak); |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1571 | if (newslots == NULL) |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1572 | goto bad_slots; |
| 1573 | for (i = j = 0; i < nslots; i++) { |
| 1574 | char *s; |
Guido van Rossum | 8e82920 | 2002-08-16 03:47:49 +0000 | [diff] [blame] | 1575 | char buffer[256]; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1576 | tmp = PyTuple_GET_ITEM(slots, i); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1577 | s = PyString_AS_STRING(tmp); |
| 1578 | if ((add_dict && strcmp(s, "__dict__") == 0) || |
| 1579 | (add_weak && strcmp(s, "__weakref__") == 0)) |
| 1580 | continue; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1581 | if (_Py_Mangle(PyString_AS_STRING(name), |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1582 | PyString_AS_STRING(tmp), |
| 1583 | buffer, sizeof(buffer))) |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1584 | { |
| 1585 | tmp = PyString_FromString(buffer); |
| 1586 | } else { |
| 1587 | Py_INCREF(tmp); |
| 1588 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1589 | PyTuple_SET_ITEM(newslots, j, tmp); |
| 1590 | j++; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1591 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1592 | assert(j == nslots - add_dict - add_weak); |
| 1593 | nslots = j; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1594 | Py_DECREF(slots); |
| 1595 | slots = newslots; |
| 1596 | |
Guido van Rossum | 0628dcf | 2002-03-14 23:03:14 +0000 | [diff] [blame] | 1597 | /* See if *this* class defines __getstate__ */ |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1598 | if (PyDict_GetItemString(dict, "__getstate__") == NULL) { |
Guido van Rossum | 0628dcf | 2002-03-14 23:03:14 +0000 | [diff] [blame] | 1599 | /* If not, provide a bozo that raises TypeError */ |
| 1600 | if (bozo_obj == NULL) { |
| 1601 | bozo_obj = PyCFunction_New(&bozo_ml, NULL); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1602 | if (bozo_obj == NULL) |
| 1603 | goto bad_slots; |
Guido van Rossum | 0628dcf | 2002-03-14 23:03:14 +0000 | [diff] [blame] | 1604 | } |
| 1605 | if (PyDict_SetItemString(dict, |
| 1606 | "__getstate__", |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1607 | bozo_obj) < 0) |
| 1608 | { |
| 1609 | Py_DECREF(bozo_obj); |
| 1610 | goto bad_slots; |
Guido van Rossum | 0628dcf | 2002-03-14 23:03:14 +0000 | [diff] [blame] | 1611 | } |
| 1612 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1613 | |
| 1614 | /* Secondary bases may provide weakrefs or dict */ |
| 1615 | if (nbases > 1 && |
| 1616 | ((may_add_dict && !add_dict) || |
| 1617 | (may_add_weak && !add_weak))) { |
| 1618 | for (i = 0; i < nbases; i++) { |
| 1619 | tmp = PyTuple_GET_ITEM(bases, i); |
| 1620 | if (tmp == (PyObject *)base) |
| 1621 | continue; /* Skip primary base */ |
| 1622 | if (PyClass_Check(tmp)) { |
| 1623 | /* Classic base class provides both */ |
| 1624 | if (may_add_dict && !add_dict) |
| 1625 | add_dict++; |
| 1626 | if (may_add_weak && !add_weak) |
| 1627 | add_weak++; |
| 1628 | break; |
| 1629 | } |
| 1630 | assert(PyType_Check(tmp)); |
| 1631 | tmptype = (PyTypeObject *)tmp; |
| 1632 | if (may_add_dict && !add_dict && |
| 1633 | tmptype->tp_dictoffset != 0) |
| 1634 | add_dict++; |
| 1635 | if (may_add_weak && !add_weak && |
| 1636 | tmptype->tp_weaklistoffset != 0) |
| 1637 | add_weak++; |
| 1638 | if (may_add_dict && !add_dict) |
| 1639 | continue; |
| 1640 | if (may_add_weak && !add_weak) |
| 1641 | continue; |
| 1642 | /* Nothing more to check */ |
| 1643 | break; |
| 1644 | } |
| 1645 | } |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1646 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1647 | |
| 1648 | /* XXX From here until type is safely allocated, |
| 1649 | "return NULL" may leak slots! */ |
| 1650 | |
| 1651 | /* Allocate the type object */ |
| 1652 | type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1653 | if (type == NULL) { |
| 1654 | Py_XDECREF(slots); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1655 | return NULL; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1656 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1657 | |
| 1658 | /* Keep name and slots alive in the extended type object */ |
| 1659 | et = (etype *)type; |
| 1660 | Py_INCREF(name); |
| 1661 | et->name = name; |
| 1662 | et->slots = slots; |
| 1663 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 1664 | /* Initialize tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1665 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | |
| 1666 | Py_TPFLAGS_BASETYPE; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1667 | if (base->tp_flags & Py_TPFLAGS_HAVE_GC) |
| 1668 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 1669 | |
| 1670 | /* It's a new-style number unless it specifically inherits any |
| 1671 | old-style numeric behavior */ |
| 1672 | if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) || |
| 1673 | (base->tp_as_number == NULL)) |
| 1674 | type->tp_flags |= Py_TPFLAGS_CHECKTYPES; |
| 1675 | |
| 1676 | /* Initialize essential fields */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1677 | type->tp_as_number = &et->as_number; |
| 1678 | type->tp_as_sequence = &et->as_sequence; |
| 1679 | type->tp_as_mapping = &et->as_mapping; |
| 1680 | type->tp_as_buffer = &et->as_buffer; |
| 1681 | type->tp_name = PyString_AS_STRING(name); |
| 1682 | |
| 1683 | /* Set tp_base and tp_bases */ |
| 1684 | type->tp_bases = bases; |
| 1685 | Py_INCREF(base); |
| 1686 | type->tp_base = base; |
| 1687 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 1688 | /* Initialize tp_dict from passed-in dict */ |
| 1689 | type->tp_dict = dict = PyDict_Copy(dict); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1690 | if (dict == NULL) { |
| 1691 | Py_DECREF(type); |
| 1692 | return NULL; |
| 1693 | } |
| 1694 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 1695 | /* Set __module__ in the dict */ |
| 1696 | if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 1697 | tmp = PyEval_GetGlobals(); |
| 1698 | if (tmp != NULL) { |
| 1699 | tmp = PyDict_GetItemString(tmp, "__name__"); |
| 1700 | if (tmp != NULL) { |
| 1701 | if (PyDict_SetItemString(dict, "__module__", |
| 1702 | tmp) < 0) |
| 1703 | return NULL; |
| 1704 | } |
| 1705 | } |
| 1706 | } |
| 1707 | |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 1708 | /* Set tp_doc to a copy of dict['__doc__'], if the latter is there |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 1709 | and is a string. The __doc__ accessor will first look for tp_doc; |
| 1710 | if that fails, it will still look into __dict__. |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 1711 | */ |
| 1712 | { |
| 1713 | PyObject *doc = PyDict_GetItemString(dict, "__doc__"); |
| 1714 | if (doc != NULL && PyString_Check(doc)) { |
| 1715 | const size_t n = (size_t)PyString_GET_SIZE(doc); |
Tim Peters | 59f809d | 2001-10-04 05:43:02 +0000 | [diff] [blame] | 1716 | type->tp_doc = (char *)PyObject_MALLOC(n+1); |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 1717 | if (type->tp_doc == NULL) { |
| 1718 | Py_DECREF(type); |
| 1719 | return NULL; |
| 1720 | } |
| 1721 | memcpy(type->tp_doc, PyString_AS_STRING(doc), n+1); |
| 1722 | } |
| 1723 | } |
| 1724 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1725 | /* Special-case __new__: if it's a plain function, |
| 1726 | make it a static function */ |
| 1727 | tmp = PyDict_GetItemString(dict, "__new__"); |
| 1728 | if (tmp != NULL && PyFunction_Check(tmp)) { |
| 1729 | tmp = PyStaticMethod_New(tmp); |
| 1730 | if (tmp == NULL) { |
| 1731 | Py_DECREF(type); |
| 1732 | return NULL; |
| 1733 | } |
| 1734 | PyDict_SetItemString(dict, "__new__", tmp); |
| 1735 | Py_DECREF(tmp); |
| 1736 | } |
| 1737 | |
| 1738 | /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
| 1739 | mp = et->members; |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 1740 | slotoffset = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1741 | if (slots != NULL) { |
| 1742 | for (i = 0; i < nslots; i++, mp++) { |
| 1743 | mp->name = PyString_AS_STRING( |
| 1744 | PyTuple_GET_ITEM(slots, i)); |
Guido van Rossum | 64b206c | 2001-12-04 17:13:22 +0000 | [diff] [blame] | 1745 | mp->type = T_OBJECT_EX; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1746 | mp->offset = slotoffset; |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1747 | if (base->tp_weaklistoffset == 0 && |
Guido van Rossum | 64b206c | 2001-12-04 17:13:22 +0000 | [diff] [blame] | 1748 | strcmp(mp->name, "__weakref__") == 0) { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1749 | add_weak++; |
Guido van Rossum | 64b206c | 2001-12-04 17:13:22 +0000 | [diff] [blame] | 1750 | mp->type = T_OBJECT; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1751 | mp->flags = READONLY; |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1752 | type->tp_weaklistoffset = slotoffset; |
Guido van Rossum | 64b206c | 2001-12-04 17:13:22 +0000 | [diff] [blame] | 1753 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1754 | slotoffset += sizeof(PyObject *); |
| 1755 | } |
| 1756 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1757 | if (add_dict) { |
| 1758 | if (base->tp_itemsize) |
| 1759 | type->tp_dictoffset = -(long)sizeof(PyObject *); |
| 1760 | else |
| 1761 | type->tp_dictoffset = slotoffset; |
| 1762 | slotoffset += sizeof(PyObject *); |
| 1763 | } |
| 1764 | if (add_weak) { |
| 1765 | assert(!base->tp_itemsize); |
| 1766 | type->tp_weaklistoffset = slotoffset; |
| 1767 | slotoffset += sizeof(PyObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1768 | } |
| 1769 | type->tp_basicsize = slotoffset; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1770 | type->tp_itemsize = base->tp_itemsize; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 1771 | type->tp_members = et->members; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1772 | type->tp_getset = subtype_getsets; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1773 | |
| 1774 | /* Special case some slots */ |
| 1775 | if (type->tp_dictoffset != 0 || nslots > 0) { |
| 1776 | if (base->tp_getattr == NULL && base->tp_getattro == NULL) |
| 1777 | type->tp_getattro = PyObject_GenericGetAttr; |
| 1778 | if (base->tp_setattr == NULL && base->tp_setattro == NULL) |
| 1779 | type->tp_setattro = PyObject_GenericSetAttr; |
| 1780 | } |
| 1781 | type->tp_dealloc = subtype_dealloc; |
| 1782 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 1783 | /* Enable GC unless there are really no instance variables possible */ |
| 1784 | if (!(type->tp_basicsize == sizeof(PyObject) && |
| 1785 | type->tp_itemsize == 0)) |
| 1786 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
| 1787 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1788 | /* Always override allocation strategy to use regular heap */ |
| 1789 | type->tp_alloc = PyType_GenericAlloc; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1790 | if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 1791 | type->tp_free = PyObject_GC_Del; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 1792 | type->tp_traverse = subtype_traverse; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1793 | type->tp_clear = subtype_clear; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1794 | } |
| 1795 | else |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 1796 | type->tp_free = PyObject_Del; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1797 | |
| 1798 | /* Initialize the rest */ |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1799 | if (PyType_Ready(type) < 0) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1800 | Py_DECREF(type); |
| 1801 | return NULL; |
| 1802 | } |
| 1803 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 1804 | /* Put the proper slots in place */ |
| 1805 | fixup_slot_dispatchers(type); |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 1806 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1807 | return (PyObject *)type; |
| 1808 | } |
| 1809 | |
| 1810 | /* Internal API to look for a name through the MRO. |
| 1811 | This returns a borrowed reference, and doesn't set an exception! */ |
| 1812 | PyObject * |
| 1813 | _PyType_Lookup(PyTypeObject *type, PyObject *name) |
| 1814 | { |
| 1815 | int i, n; |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1816 | PyObject *mro, *res, *base, *dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1817 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 1818 | /* Look in tp_dict of types in MRO */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1819 | mro = type->tp_mro; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 1820 | |
| 1821 | /* If mro is NULL, the type is either not yet initialized |
| 1822 | by PyType_Ready(), or already cleared by type_clear(). |
| 1823 | Either way the safest thing to do is to return NULL. */ |
| 1824 | if (mro == NULL) |
| 1825 | return NULL; |
| 1826 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1827 | assert(PyTuple_Check(mro)); |
| 1828 | n = PyTuple_GET_SIZE(mro); |
| 1829 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1830 | base = PyTuple_GET_ITEM(mro, i); |
| 1831 | if (PyClass_Check(base)) |
| 1832 | dict = ((PyClassObject *)base)->cl_dict; |
| 1833 | else { |
| 1834 | assert(PyType_Check(base)); |
| 1835 | dict = ((PyTypeObject *)base)->tp_dict; |
| 1836 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1837 | assert(dict && PyDict_Check(dict)); |
| 1838 | res = PyDict_GetItem(dict, name); |
| 1839 | if (res != NULL) |
| 1840 | return res; |
| 1841 | } |
| 1842 | return NULL; |
| 1843 | } |
| 1844 | |
| 1845 | /* This is similar to PyObject_GenericGetAttr(), |
| 1846 | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ |
| 1847 | static PyObject * |
| 1848 | type_getattro(PyTypeObject *type, PyObject *name) |
| 1849 | { |
| 1850 | PyTypeObject *metatype = type->ob_type; |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 1851 | PyObject *meta_attribute, *attribute; |
| 1852 | descrgetfunc meta_get; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1853 | |
| 1854 | /* Initialize this type (we'll assume the metatype is initialized) */ |
| 1855 | if (type->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1856 | if (PyType_Ready(type) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1857 | return NULL; |
| 1858 | } |
| 1859 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 1860 | /* No readable descriptor found yet */ |
| 1861 | meta_get = NULL; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1862 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 1863 | /* Look for the attribute in the metatype */ |
| 1864 | meta_attribute = _PyType_Lookup(metatype, name); |
| 1865 | |
| 1866 | if (meta_attribute != NULL) { |
| 1867 | meta_get = meta_attribute->ob_type->tp_descr_get; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1868 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 1869 | if (meta_get != NULL && PyDescr_IsData(meta_attribute)) { |
| 1870 | /* Data descriptors implement tp_descr_set to intercept |
| 1871 | * writes. Assume the attribute is not overridden in |
| 1872 | * type's tp_dict (and bases): call the descriptor now. |
| 1873 | */ |
| 1874 | return meta_get(meta_attribute, (PyObject *)type, |
| 1875 | (PyObject *)metatype); |
| 1876 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 1879 | /* No data descriptor found on metatype. Look in tp_dict of this |
| 1880 | * type and its bases */ |
| 1881 | attribute = _PyType_Lookup(type, name); |
| 1882 | if (attribute != NULL) { |
| 1883 | /* Implement descriptor functionality, if any */ |
| 1884 | descrgetfunc local_get = attribute->ob_type->tp_descr_get; |
| 1885 | if (local_get != NULL) { |
| 1886 | /* NULL 2nd argument indicates the descriptor was |
| 1887 | * found on the target object itself (or a base) */ |
| 1888 | return local_get(attribute, (PyObject *)NULL, |
| 1889 | (PyObject *)type); |
| 1890 | } |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1891 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 1892 | Py_INCREF(attribute); |
| 1893 | return attribute; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 1896 | /* No attribute found in local __dict__ (or bases): use the |
| 1897 | * descriptor from the metatype, if any */ |
| 1898 | if (meta_get != NULL) |
| 1899 | return meta_get(meta_attribute, (PyObject *)type, |
| 1900 | (PyObject *)metatype); |
| 1901 | |
| 1902 | /* If an ordinary attribute was found on the metatype, return it now */ |
| 1903 | if (meta_attribute != NULL) { |
| 1904 | Py_INCREF(meta_attribute); |
| 1905 | return meta_attribute; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | /* Give up */ |
| 1909 | PyErr_Format(PyExc_AttributeError, |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 1910 | "type object '%.50s' has no attribute '%.400s'", |
| 1911 | type->tp_name, PyString_AS_STRING(name)); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1912 | return NULL; |
| 1913 | } |
| 1914 | |
| 1915 | static int |
| 1916 | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) |
| 1917 | { |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1918 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 1919 | PyErr_Format( |
| 1920 | PyExc_TypeError, |
| 1921 | "can't set attributes of built-in/extension type '%s'", |
| 1922 | type->tp_name); |
| 1923 | return -1; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 1924 | } |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1925 | if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0) |
| 1926 | return -1; |
| 1927 | return update_slot(type, name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
| 1930 | static void |
| 1931 | type_dealloc(PyTypeObject *type) |
| 1932 | { |
| 1933 | etype *et; |
| 1934 | |
| 1935 | /* Assert this is a heap-allocated type object */ |
| 1936 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1937 | _PyObject_GC_UNTRACK(type); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1938 | PyObject_ClearWeakRefs((PyObject *)type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1939 | et = (etype *)type; |
| 1940 | Py_XDECREF(type->tp_base); |
| 1941 | Py_XDECREF(type->tp_dict); |
| 1942 | Py_XDECREF(type->tp_bases); |
| 1943 | Py_XDECREF(type->tp_mro); |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 1944 | Py_XDECREF(type->tp_cache); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1945 | Py_XDECREF(type->tp_subclasses); |
Neal Norwitz | cee5ca0 | 2002-07-30 00:42:06 +0000 | [diff] [blame] | 1946 | PyObject_Free(type->tp_doc); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1947 | Py_XDECREF(et->name); |
| 1948 | Py_XDECREF(et->slots); |
| 1949 | type->ob_type->tp_free((PyObject *)type); |
| 1950 | } |
| 1951 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1952 | static PyObject * |
| 1953 | type_subclasses(PyTypeObject *type, PyObject *args_ignored) |
| 1954 | { |
| 1955 | PyObject *list, *raw, *ref; |
| 1956 | int i, n; |
| 1957 | |
| 1958 | list = PyList_New(0); |
| 1959 | if (list == NULL) |
| 1960 | return NULL; |
| 1961 | raw = type->tp_subclasses; |
| 1962 | if (raw == NULL) |
| 1963 | return list; |
| 1964 | assert(PyList_Check(raw)); |
| 1965 | n = PyList_GET_SIZE(raw); |
| 1966 | for (i = 0; i < n; i++) { |
| 1967 | ref = PyList_GET_ITEM(raw, i); |
Tim Peters | 4438338 | 2001-10-08 16:49:26 +0000 | [diff] [blame] | 1968 | assert(PyWeakref_CheckRef(ref)); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1969 | ref = PyWeakref_GET_OBJECT(ref); |
| 1970 | if (ref != Py_None) { |
| 1971 | if (PyList_Append(list, ref) < 0) { |
| 1972 | Py_DECREF(list); |
| 1973 | return NULL; |
| 1974 | } |
| 1975 | } |
| 1976 | } |
| 1977 | return list; |
| 1978 | } |
| 1979 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1980 | static PyMethodDef type_methods[] = { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1981 | {"mro", (PyCFunction)mro_external, METH_NOARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 1982 | PyDoc_STR("mro() -> list\nreturn a type's method resolution order")}, |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1983 | {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 1984 | PyDoc_STR("__subclasses__() -> list of immediate subclasses")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1985 | {0} |
| 1986 | }; |
| 1987 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1988 | PyDoc_STRVAR(type_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1989 | "type(object) -> the object's type\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1990 | "type(name, bases, dict) -> a new type"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1991 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1992 | static int |
| 1993 | type_traverse(PyTypeObject *type, visitproc visit, void *arg) |
| 1994 | { |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1995 | int err; |
| 1996 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 1997 | /* Because of type_is_gc(), the collector only calls this |
| 1998 | for heaptypes. */ |
| 1999 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2000 | |
| 2001 | #define VISIT(SLOT) \ |
| 2002 | if (SLOT) { \ |
| 2003 | err = visit((PyObject *)(SLOT), arg); \ |
| 2004 | if (err) \ |
| 2005 | return err; \ |
| 2006 | } |
| 2007 | |
| 2008 | VISIT(type->tp_dict); |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2009 | VISIT(type->tp_cache); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2010 | VISIT(type->tp_mro); |
| 2011 | VISIT(type->tp_bases); |
| 2012 | VISIT(type->tp_base); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2013 | |
| 2014 | /* There's no need to visit type->tp_subclasses or |
| 2015 | ((etype *)type)->slots, because they can't be involved |
| 2016 | in cycles; tp_subclasses is a list of weak references, |
| 2017 | and slots is a tuple of strings. */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2018 | |
| 2019 | #undef VISIT |
| 2020 | |
| 2021 | return 0; |
| 2022 | } |
| 2023 | |
| 2024 | static int |
| 2025 | type_clear(PyTypeObject *type) |
| 2026 | { |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2027 | PyObject *tmp; |
| 2028 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2029 | /* Because of type_is_gc(), the collector only calls this |
| 2030 | for heaptypes. */ |
| 2031 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2032 | |
| 2033 | #define CLEAR(SLOT) \ |
| 2034 | if (SLOT) { \ |
| 2035 | tmp = (PyObject *)(SLOT); \ |
| 2036 | SLOT = NULL; \ |
| 2037 | Py_DECREF(tmp); \ |
| 2038 | } |
| 2039 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2040 | /* The only field we need to clear is tp_mro, which is part of a |
| 2041 | hard cycle (its first element is the class itself) that won't |
| 2042 | be broken otherwise (it's a tuple and tuples don't have a |
| 2043 | tp_clear handler). None of the other fields need to be |
| 2044 | cleared, and here's why: |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2045 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2046 | tp_dict: |
| 2047 | It is a dict, so the collector will call its tp_clear. |
| 2048 | |
| 2049 | tp_cache: |
| 2050 | Not used; if it were, it would be a dict. |
| 2051 | |
| 2052 | tp_bases, tp_base: |
| 2053 | If these are involved in a cycle, there must be at least |
| 2054 | one other, mutable object in the cycle, e.g. a base |
| 2055 | class's dict; the cycle will be broken that way. |
| 2056 | |
| 2057 | tp_subclasses: |
| 2058 | A list of weak references can't be part of a cycle; and |
| 2059 | lists have their own tp_clear. |
| 2060 | |
| 2061 | slots (in etype): |
| 2062 | A tuple of strings can't be part of a cycle. |
| 2063 | */ |
| 2064 | |
| 2065 | CLEAR(type->tp_mro); |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2066 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2067 | #undef CLEAR |
| 2068 | |
| 2069 | return 0; |
| 2070 | } |
| 2071 | |
| 2072 | static int |
| 2073 | type_is_gc(PyTypeObject *type) |
| 2074 | { |
| 2075 | return type->tp_flags & Py_TPFLAGS_HEAPTYPE; |
| 2076 | } |
| 2077 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2078 | PyTypeObject PyType_Type = { |
| 2079 | PyObject_HEAD_INIT(&PyType_Type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2080 | 0, /* ob_size */ |
| 2081 | "type", /* tp_name */ |
| 2082 | sizeof(etype), /* tp_basicsize */ |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 2083 | sizeof(PyMemberDef), /* tp_itemsize */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2084 | (destructor)type_dealloc, /* tp_dealloc */ |
| 2085 | 0, /* tp_print */ |
| 2086 | 0, /* tp_getattr */ |
| 2087 | 0, /* tp_setattr */ |
| 2088 | type_compare, /* tp_compare */ |
| 2089 | (reprfunc)type_repr, /* tp_repr */ |
| 2090 | 0, /* tp_as_number */ |
| 2091 | 0, /* tp_as_sequence */ |
| 2092 | 0, /* tp_as_mapping */ |
| 2093 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
| 2094 | (ternaryfunc)type_call, /* tp_call */ |
| 2095 | 0, /* tp_str */ |
| 2096 | (getattrofunc)type_getattro, /* tp_getattro */ |
| 2097 | (setattrofunc)type_setattro, /* tp_setattro */ |
| 2098 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2099 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2100 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2101 | type_doc, /* tp_doc */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2102 | (traverseproc)type_traverse, /* tp_traverse */ |
| 2103 | (inquiry)type_clear, /* tp_clear */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2104 | 0, /* tp_richcompare */ |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2105 | offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2106 | 0, /* tp_iter */ |
| 2107 | 0, /* tp_iternext */ |
| 2108 | type_methods, /* tp_methods */ |
| 2109 | type_members, /* tp_members */ |
| 2110 | type_getsets, /* tp_getset */ |
| 2111 | 0, /* tp_base */ |
| 2112 | 0, /* tp_dict */ |
| 2113 | 0, /* tp_descr_get */ |
| 2114 | 0, /* tp_descr_set */ |
| 2115 | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ |
| 2116 | 0, /* tp_init */ |
| 2117 | 0, /* tp_alloc */ |
| 2118 | type_new, /* tp_new */ |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 2119 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2120 | (inquiry)type_is_gc, /* tp_is_gc */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2121 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2122 | |
| 2123 | |
| 2124 | /* The base type of all types (eventually)... except itself. */ |
| 2125 | |
| 2126 | static int |
| 2127 | object_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2128 | { |
| 2129 | return 0; |
| 2130 | } |
| 2131 | |
| 2132 | static void |
| 2133 | object_dealloc(PyObject *self) |
| 2134 | { |
| 2135 | self->ob_type->tp_free(self); |
| 2136 | } |
| 2137 | |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2138 | static PyObject * |
| 2139 | object_repr(PyObject *self) |
| 2140 | { |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2141 | PyTypeObject *type; |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2142 | PyObject *mod, *name, *rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2143 | |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2144 | type = self->ob_type; |
| 2145 | mod = type_module(type, NULL); |
| 2146 | if (mod == NULL) |
| 2147 | PyErr_Clear(); |
| 2148 | else if (!PyString_Check(mod)) { |
| 2149 | Py_DECREF(mod); |
| 2150 | mod = NULL; |
| 2151 | } |
| 2152 | name = type_name(type, NULL); |
| 2153 | if (name == NULL) |
| 2154 | return NULL; |
| 2155 | if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) |
Guido van Rossum | ff0e6d6 | 2001-09-24 16:03:59 +0000 | [diff] [blame] | 2156 | rtn = PyString_FromFormat("<%s.%s object at %p>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2157 | PyString_AS_STRING(mod), |
| 2158 | PyString_AS_STRING(name), |
| 2159 | self); |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2160 | else |
Guido van Rossum | ff0e6d6 | 2001-09-24 16:03:59 +0000 | [diff] [blame] | 2161 | rtn = PyString_FromFormat("<%s object at %p>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2162 | type->tp_name, self); |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2163 | Py_XDECREF(mod); |
| 2164 | Py_DECREF(name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2165 | return rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2168 | static PyObject * |
| 2169 | object_str(PyObject *self) |
| 2170 | { |
| 2171 | unaryfunc f; |
| 2172 | |
| 2173 | f = self->ob_type->tp_repr; |
| 2174 | if (f == NULL) |
| 2175 | f = object_repr; |
| 2176 | return f(self); |
| 2177 | } |
| 2178 | |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2179 | static long |
| 2180 | object_hash(PyObject *self) |
| 2181 | { |
| 2182 | return _Py_HashPointer(self); |
| 2183 | } |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2184 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2185 | static PyObject * |
| 2186 | object_get_class(PyObject *self, void *closure) |
| 2187 | { |
| 2188 | Py_INCREF(self->ob_type); |
| 2189 | return (PyObject *)(self->ob_type); |
| 2190 | } |
| 2191 | |
| 2192 | static int |
| 2193 | equiv_structs(PyTypeObject *a, PyTypeObject *b) |
| 2194 | { |
| 2195 | return a == b || |
| 2196 | (a != NULL && |
| 2197 | b != NULL && |
| 2198 | a->tp_basicsize == b->tp_basicsize && |
| 2199 | a->tp_itemsize == b->tp_itemsize && |
| 2200 | a->tp_dictoffset == b->tp_dictoffset && |
| 2201 | a->tp_weaklistoffset == b->tp_weaklistoffset && |
| 2202 | ((a->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 2203 | (b->tp_flags & Py_TPFLAGS_HAVE_GC))); |
| 2204 | } |
| 2205 | |
| 2206 | static int |
| 2207 | same_slots_added(PyTypeObject *a, PyTypeObject *b) |
| 2208 | { |
| 2209 | PyTypeObject *base = a->tp_base; |
| 2210 | int size; |
| 2211 | |
| 2212 | if (base != b->tp_base) |
| 2213 | return 0; |
| 2214 | if (equiv_structs(a, base) && equiv_structs(b, base)) |
| 2215 | return 1; |
| 2216 | size = base->tp_basicsize; |
| 2217 | if (a->tp_dictoffset == size && b->tp_dictoffset == size) |
| 2218 | size += sizeof(PyObject *); |
| 2219 | if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size) |
| 2220 | size += sizeof(PyObject *); |
| 2221 | return size == a->tp_basicsize && size == b->tp_basicsize; |
| 2222 | } |
| 2223 | |
| 2224 | static int |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2225 | compatible_for_assignment(PyTypeObject* old, PyTypeObject* new, char* attr) |
| 2226 | { |
| 2227 | PyTypeObject *newbase, *oldbase; |
| 2228 | |
| 2229 | if (new->tp_dealloc != old->tp_dealloc || |
| 2230 | new->tp_free != old->tp_free) |
| 2231 | { |
| 2232 | PyErr_Format(PyExc_TypeError, |
| 2233 | "%s assignment: " |
| 2234 | "'%s' deallocator differs from '%s'", |
| 2235 | attr, |
| 2236 | new->tp_name, |
| 2237 | old->tp_name); |
| 2238 | return 0; |
| 2239 | } |
| 2240 | newbase = new; |
| 2241 | oldbase = old; |
| 2242 | while (equiv_structs(newbase, newbase->tp_base)) |
| 2243 | newbase = newbase->tp_base; |
| 2244 | while (equiv_structs(oldbase, oldbase->tp_base)) |
| 2245 | oldbase = oldbase->tp_base; |
| 2246 | if (newbase != oldbase && |
| 2247 | (newbase->tp_base != oldbase->tp_base || |
| 2248 | !same_slots_added(newbase, oldbase))) { |
| 2249 | PyErr_Format(PyExc_TypeError, |
| 2250 | "%s assignment: " |
| 2251 | "'%s' object layout differs from '%s'", |
| 2252 | attr, |
| 2253 | new->tp_name, |
| 2254 | old->tp_name); |
| 2255 | return 0; |
| 2256 | } |
| 2257 | |
| 2258 | return 1; |
| 2259 | } |
| 2260 | |
| 2261 | static int |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2262 | object_set_class(PyObject *self, PyObject *value, void *closure) |
| 2263 | { |
| 2264 | PyTypeObject *old = self->ob_type; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2265 | PyTypeObject *new; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2266 | |
Guido van Rossum | b6b8942 | 2002-04-15 01:03:30 +0000 | [diff] [blame] | 2267 | if (value == NULL) { |
| 2268 | PyErr_SetString(PyExc_TypeError, |
| 2269 | "can't delete __class__ attribute"); |
| 2270 | return -1; |
| 2271 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2272 | if (!PyType_Check(value)) { |
| 2273 | PyErr_Format(PyExc_TypeError, |
| 2274 | "__class__ must be set to new-style class, not '%s' object", |
| 2275 | value->ob_type->tp_name); |
| 2276 | return -1; |
| 2277 | } |
| 2278 | new = (PyTypeObject *)value; |
Guido van Rossum | 40af889 | 2002-08-10 05:42:07 +0000 | [diff] [blame] | 2279 | if (!(new->tp_flags & Py_TPFLAGS_HEAPTYPE) || |
| 2280 | !(old->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 2281 | { |
| 2282 | PyErr_Format(PyExc_TypeError, |
| 2283 | "__class__ assignment: only for heap types"); |
| 2284 | return -1; |
| 2285 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2286 | if (compatible_for_assignment(new, old, "__class__")) { |
| 2287 | Py_INCREF(new); |
| 2288 | self->ob_type = new; |
| 2289 | Py_DECREF(old); |
| 2290 | return 0; |
| 2291 | } |
| 2292 | else { |
Guido van Rossum | 9ee4b94 | 2002-05-24 18:47:47 +0000 | [diff] [blame] | 2293 | return -1; |
| 2294 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
| 2297 | static PyGetSetDef object_getsets[] = { |
| 2298 | {"__class__", object_get_class, object_set_class, |
Neal Norwitz | 858e34f | 2002-08-13 17:18:45 +0000 | [diff] [blame] | 2299 | PyDoc_STR("the object's class")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2300 | {0} |
| 2301 | }; |
| 2302 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2303 | static PyObject * |
| 2304 | object_reduce(PyObject *self, PyObject *args) |
| 2305 | { |
| 2306 | /* Call copy_reg._reduce(self) */ |
| 2307 | static PyObject *copy_reg_str; |
| 2308 | PyObject *copy_reg, *res; |
| 2309 | |
| 2310 | if (!copy_reg_str) { |
| 2311 | copy_reg_str = PyString_InternFromString("copy_reg"); |
| 2312 | if (copy_reg_str == NULL) |
| 2313 | return NULL; |
| 2314 | } |
| 2315 | copy_reg = PyImport_Import(copy_reg_str); |
| 2316 | if (!copy_reg) |
| 2317 | return NULL; |
| 2318 | res = PyEval_CallMethod(copy_reg, "_reduce", "(O)", self); |
| 2319 | Py_DECREF(copy_reg); |
| 2320 | return res; |
| 2321 | } |
| 2322 | |
| 2323 | static PyMethodDef object_methods[] = { |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 2324 | {"__reduce__", object_reduce, METH_NOARGS, |
| 2325 | PyDoc_STR("helper for pickle")}, |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2326 | {0} |
| 2327 | }; |
| 2328 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2329 | PyTypeObject PyBaseObject_Type = { |
| 2330 | PyObject_HEAD_INIT(&PyType_Type) |
| 2331 | 0, /* ob_size */ |
| 2332 | "object", /* tp_name */ |
| 2333 | sizeof(PyObject), /* tp_basicsize */ |
| 2334 | 0, /* tp_itemsize */ |
| 2335 | (destructor)object_dealloc, /* tp_dealloc */ |
| 2336 | 0, /* tp_print */ |
| 2337 | 0, /* tp_getattr */ |
| 2338 | 0, /* tp_setattr */ |
| 2339 | 0, /* tp_compare */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2340 | object_repr, /* tp_repr */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2341 | 0, /* tp_as_number */ |
| 2342 | 0, /* tp_as_sequence */ |
| 2343 | 0, /* tp_as_mapping */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2344 | object_hash, /* tp_hash */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2345 | 0, /* tp_call */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2346 | object_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2347 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2348 | PyObject_GenericSetAttr, /* tp_setattro */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2349 | 0, /* tp_as_buffer */ |
| 2350 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 2351 | PyDoc_STR("The most base type"), /* tp_doc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2352 | 0, /* tp_traverse */ |
| 2353 | 0, /* tp_clear */ |
| 2354 | 0, /* tp_richcompare */ |
| 2355 | 0, /* tp_weaklistoffset */ |
| 2356 | 0, /* tp_iter */ |
| 2357 | 0, /* tp_iternext */ |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2358 | object_methods, /* tp_methods */ |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2359 | 0, /* tp_members */ |
| 2360 | object_getsets, /* tp_getset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2361 | 0, /* tp_base */ |
| 2362 | 0, /* tp_dict */ |
| 2363 | 0, /* tp_descr_get */ |
| 2364 | 0, /* tp_descr_set */ |
| 2365 | 0, /* tp_dictoffset */ |
| 2366 | object_init, /* tp_init */ |
| 2367 | PyType_GenericAlloc, /* tp_alloc */ |
Guido van Rossum | c11e192 | 2001-08-09 19:38:15 +0000 | [diff] [blame] | 2368 | PyType_GenericNew, /* tp_new */ |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 2369 | PyObject_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2370 | }; |
| 2371 | |
| 2372 | |
| 2373 | /* Initialize the __dict__ in a type object */ |
| 2374 | |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 2375 | static PyObject * |
| 2376 | create_specialmethod(PyMethodDef *meth, PyObject *(*func)(PyObject *)) |
| 2377 | { |
| 2378 | PyObject *cfunc; |
| 2379 | PyObject *result; |
| 2380 | |
| 2381 | cfunc = PyCFunction_New(meth, NULL); |
| 2382 | if (cfunc == NULL) |
| 2383 | return NULL; |
| 2384 | result = func(cfunc); |
| 2385 | Py_DECREF(cfunc); |
| 2386 | return result; |
| 2387 | } |
| 2388 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2389 | static int |
| 2390 | add_methods(PyTypeObject *type, PyMethodDef *meth) |
| 2391 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2392 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2393 | |
| 2394 | for (; meth->ml_name != NULL; meth++) { |
| 2395 | PyObject *descr; |
| 2396 | if (PyDict_GetItemString(dict, meth->ml_name)) |
| 2397 | continue; |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 2398 | if (meth->ml_flags & METH_CLASS) { |
| 2399 | if (meth->ml_flags & METH_STATIC) { |
| 2400 | PyErr_SetString(PyExc_ValueError, |
| 2401 | "method cannot be both class and static"); |
| 2402 | return -1; |
| 2403 | } |
| 2404 | descr = create_specialmethod(meth, PyClassMethod_New); |
| 2405 | } |
| 2406 | else if (meth->ml_flags & METH_STATIC) { |
| 2407 | descr = create_specialmethod(meth, PyStaticMethod_New); |
| 2408 | } |
| 2409 | else { |
| 2410 | descr = PyDescr_NewMethod(type, meth); |
| 2411 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2412 | if (descr == NULL) |
| 2413 | return -1; |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 2414 | if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2415 | return -1; |
| 2416 | Py_DECREF(descr); |
| 2417 | } |
| 2418 | return 0; |
| 2419 | } |
| 2420 | |
| 2421 | static int |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 2422 | add_members(PyTypeObject *type, PyMemberDef *memb) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2423 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2424 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2425 | |
| 2426 | for (; memb->name != NULL; memb++) { |
| 2427 | PyObject *descr; |
| 2428 | if (PyDict_GetItemString(dict, memb->name)) |
| 2429 | continue; |
| 2430 | descr = PyDescr_NewMember(type, memb); |
| 2431 | if (descr == NULL) |
| 2432 | return -1; |
| 2433 | if (PyDict_SetItemString(dict, memb->name, descr) < 0) |
| 2434 | return -1; |
| 2435 | Py_DECREF(descr); |
| 2436 | } |
| 2437 | return 0; |
| 2438 | } |
| 2439 | |
| 2440 | static int |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 2441 | add_getset(PyTypeObject *type, PyGetSetDef *gsp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2442 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2443 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2444 | |
| 2445 | for (; gsp->name != NULL; gsp++) { |
| 2446 | PyObject *descr; |
| 2447 | if (PyDict_GetItemString(dict, gsp->name)) |
| 2448 | continue; |
| 2449 | descr = PyDescr_NewGetSet(type, gsp); |
| 2450 | |
| 2451 | if (descr == NULL) |
| 2452 | return -1; |
| 2453 | if (PyDict_SetItemString(dict, gsp->name, descr) < 0) |
| 2454 | return -1; |
| 2455 | Py_DECREF(descr); |
| 2456 | } |
| 2457 | return 0; |
| 2458 | } |
| 2459 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2460 | static void |
| 2461 | inherit_special(PyTypeObject *type, PyTypeObject *base) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2462 | { |
| 2463 | int oldsize, newsize; |
| 2464 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2465 | /* Special flag magic */ |
| 2466 | if (!type->tp_as_buffer && base->tp_as_buffer) { |
| 2467 | type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER; |
| 2468 | type->tp_flags |= |
| 2469 | base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER; |
| 2470 | } |
| 2471 | if (!type->tp_as_sequence && base->tp_as_sequence) { |
| 2472 | type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN; |
| 2473 | type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN; |
| 2474 | } |
| 2475 | if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) != |
| 2476 | (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) { |
| 2477 | if ((!type->tp_as_number && base->tp_as_number) || |
| 2478 | (!type->tp_as_sequence && base->tp_as_sequence)) { |
| 2479 | type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS; |
| 2480 | if (!type->tp_as_number && !type->tp_as_sequence) { |
| 2481 | type->tp_flags |= base->tp_flags & |
| 2482 | Py_TPFLAGS_HAVE_INPLACEOPS; |
| 2483 | } |
| 2484 | } |
| 2485 | /* Wow */ |
| 2486 | } |
| 2487 | if (!type->tp_as_number && base->tp_as_number) { |
| 2488 | type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES; |
| 2489 | type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES; |
| 2490 | } |
| 2491 | |
| 2492 | /* Copying basicsize is connected to the GC flags */ |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 2493 | oldsize = base->tp_basicsize; |
| 2494 | newsize = type->tp_basicsize ? type->tp_basicsize : oldsize; |
| 2495 | if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 2496 | (base->tp_flags & Py_TPFLAGS_HAVE_GC) && |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2497 | (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) && |
| 2498 | (!type->tp_traverse && !type->tp_clear)) { |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 2499 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2500 | if (type->tp_traverse == NULL) |
| 2501 | type->tp_traverse = base->tp_traverse; |
| 2502 | if (type->tp_clear == NULL) |
| 2503 | type->tp_clear = base->tp_clear; |
| 2504 | } |
| 2505 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
Guido van Rossum | f884b74 | 2001-12-17 17:14:22 +0000 | [diff] [blame] | 2506 | /* The condition below could use some explanation. |
| 2507 | It appears that tp_new is not inherited for static types |
| 2508 | whose base class is 'object'; this seems to be a precaution |
| 2509 | so that old extension types don't suddenly become |
| 2510 | callable (object.__new__ wouldn't insure the invariants |
| 2511 | that the extension type's own factory function ensures). |
| 2512 | Heap types, of course, are under our control, so they do |
| 2513 | inherit tp_new; static extension types that specify some |
| 2514 | other built-in type as the default are considered |
| 2515 | new-style-aware so they also inherit object.__new__. */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2516 | if (base != &PyBaseObject_Type || |
| 2517 | (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 2518 | if (type->tp_new == NULL) |
| 2519 | type->tp_new = base->tp_new; |
| 2520 | } |
| 2521 | } |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 2522 | type->tp_basicsize = newsize; |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 2523 | |
| 2524 | /* Copy other non-function slots */ |
| 2525 | |
| 2526 | #undef COPYVAL |
| 2527 | #define COPYVAL(SLOT) \ |
| 2528 | if (type->SLOT == 0) type->SLOT = base->SLOT |
| 2529 | |
| 2530 | COPYVAL(tp_itemsize); |
| 2531 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) { |
| 2532 | COPYVAL(tp_weaklistoffset); |
| 2533 | } |
| 2534 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
| 2535 | COPYVAL(tp_dictoffset); |
| 2536 | } |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
| 2539 | static void |
| 2540 | inherit_slots(PyTypeObject *type, PyTypeObject *base) |
| 2541 | { |
| 2542 | PyTypeObject *basebase; |
| 2543 | |
| 2544 | #undef SLOTDEFINED |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2545 | #undef COPYSLOT |
| 2546 | #undef COPYNUM |
| 2547 | #undef COPYSEQ |
| 2548 | #undef COPYMAP |
Guido van Rossum | 5af588b | 2001-10-12 14:13:21 +0000 | [diff] [blame] | 2549 | #undef COPYBUF |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2550 | |
| 2551 | #define SLOTDEFINED(SLOT) \ |
| 2552 | (base->SLOT != 0 && \ |
| 2553 | (basebase == NULL || base->SLOT != basebase->SLOT)) |
| 2554 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2555 | #define COPYSLOT(SLOT) \ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2556 | if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2557 | |
| 2558 | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT) |
| 2559 | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT) |
| 2560 | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT) |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 2561 | #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2562 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2563 | /* This won't inherit indirect slots (from tp_as_number etc.) |
| 2564 | if type doesn't provide the space. */ |
| 2565 | |
| 2566 | if (type->tp_as_number != NULL && base->tp_as_number != NULL) { |
| 2567 | basebase = base->tp_base; |
| 2568 | if (basebase->tp_as_number == NULL) |
| 2569 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2570 | COPYNUM(nb_add); |
| 2571 | COPYNUM(nb_subtract); |
| 2572 | COPYNUM(nb_multiply); |
| 2573 | COPYNUM(nb_divide); |
| 2574 | COPYNUM(nb_remainder); |
| 2575 | COPYNUM(nb_divmod); |
| 2576 | COPYNUM(nb_power); |
| 2577 | COPYNUM(nb_negative); |
| 2578 | COPYNUM(nb_positive); |
| 2579 | COPYNUM(nb_absolute); |
| 2580 | COPYNUM(nb_nonzero); |
| 2581 | COPYNUM(nb_invert); |
| 2582 | COPYNUM(nb_lshift); |
| 2583 | COPYNUM(nb_rshift); |
| 2584 | COPYNUM(nb_and); |
| 2585 | COPYNUM(nb_xor); |
| 2586 | COPYNUM(nb_or); |
| 2587 | COPYNUM(nb_coerce); |
| 2588 | COPYNUM(nb_int); |
| 2589 | COPYNUM(nb_long); |
| 2590 | COPYNUM(nb_float); |
| 2591 | COPYNUM(nb_oct); |
| 2592 | COPYNUM(nb_hex); |
| 2593 | COPYNUM(nb_inplace_add); |
| 2594 | COPYNUM(nb_inplace_subtract); |
| 2595 | COPYNUM(nb_inplace_multiply); |
| 2596 | COPYNUM(nb_inplace_divide); |
| 2597 | COPYNUM(nb_inplace_remainder); |
| 2598 | COPYNUM(nb_inplace_power); |
| 2599 | COPYNUM(nb_inplace_lshift); |
| 2600 | COPYNUM(nb_inplace_rshift); |
| 2601 | COPYNUM(nb_inplace_and); |
| 2602 | COPYNUM(nb_inplace_xor); |
| 2603 | COPYNUM(nb_inplace_or); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2604 | if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) { |
| 2605 | COPYNUM(nb_true_divide); |
| 2606 | COPYNUM(nb_floor_divide); |
| 2607 | COPYNUM(nb_inplace_true_divide); |
| 2608 | COPYNUM(nb_inplace_floor_divide); |
| 2609 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2612 | if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) { |
| 2613 | basebase = base->tp_base; |
| 2614 | if (basebase->tp_as_sequence == NULL) |
| 2615 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2616 | COPYSEQ(sq_length); |
| 2617 | COPYSEQ(sq_concat); |
| 2618 | COPYSEQ(sq_repeat); |
| 2619 | COPYSEQ(sq_item); |
| 2620 | COPYSEQ(sq_slice); |
| 2621 | COPYSEQ(sq_ass_item); |
| 2622 | COPYSEQ(sq_ass_slice); |
| 2623 | COPYSEQ(sq_contains); |
| 2624 | COPYSEQ(sq_inplace_concat); |
| 2625 | COPYSEQ(sq_inplace_repeat); |
| 2626 | } |
| 2627 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2628 | if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) { |
| 2629 | basebase = base->tp_base; |
| 2630 | if (basebase->tp_as_mapping == NULL) |
| 2631 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2632 | COPYMAP(mp_length); |
| 2633 | COPYMAP(mp_subscript); |
| 2634 | COPYMAP(mp_ass_subscript); |
| 2635 | } |
| 2636 | |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 2637 | if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) { |
| 2638 | basebase = base->tp_base; |
| 2639 | if (basebase->tp_as_buffer == NULL) |
| 2640 | basebase = NULL; |
| 2641 | COPYBUF(bf_getreadbuffer); |
| 2642 | COPYBUF(bf_getwritebuffer); |
| 2643 | COPYBUF(bf_getsegcount); |
| 2644 | COPYBUF(bf_getcharbuffer); |
| 2645 | } |
| 2646 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2647 | basebase = base->tp_base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2648 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2649 | COPYSLOT(tp_dealloc); |
| 2650 | COPYSLOT(tp_print); |
| 2651 | if (type->tp_getattr == NULL && type->tp_getattro == NULL) { |
| 2652 | type->tp_getattr = base->tp_getattr; |
| 2653 | type->tp_getattro = base->tp_getattro; |
| 2654 | } |
| 2655 | if (type->tp_setattr == NULL && type->tp_setattro == NULL) { |
| 2656 | type->tp_setattr = base->tp_setattr; |
| 2657 | type->tp_setattro = base->tp_setattro; |
| 2658 | } |
| 2659 | /* tp_compare see tp_richcompare */ |
| 2660 | COPYSLOT(tp_repr); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2661 | /* tp_hash see tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2662 | COPYSLOT(tp_call); |
| 2663 | COPYSLOT(tp_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2664 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2665 | if (type->tp_compare == NULL && |
| 2666 | type->tp_richcompare == NULL && |
| 2667 | type->tp_hash == NULL) |
| 2668 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2669 | type->tp_compare = base->tp_compare; |
| 2670 | type->tp_richcompare = base->tp_richcompare; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2671 | type->tp_hash = base->tp_hash; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2672 | } |
| 2673 | } |
| 2674 | else { |
| 2675 | COPYSLOT(tp_compare); |
| 2676 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2677 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) { |
| 2678 | COPYSLOT(tp_iter); |
| 2679 | COPYSLOT(tp_iternext); |
| 2680 | } |
| 2681 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
| 2682 | COPYSLOT(tp_descr_get); |
| 2683 | COPYSLOT(tp_descr_set); |
| 2684 | COPYSLOT(tp_dictoffset); |
| 2685 | COPYSLOT(tp_init); |
| 2686 | COPYSLOT(tp_alloc); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2687 | COPYSLOT(tp_free); |
Guido van Rossum | cc8fe04 | 2002-04-05 17:10:16 +0000 | [diff] [blame] | 2688 | COPYSLOT(tp_is_gc); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2689 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 2692 | static int add_operators(PyTypeObject *); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2693 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2694 | int |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 2695 | PyType_Ready(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2696 | { |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 2697 | PyObject *dict, *bases; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2698 | PyTypeObject *base; |
| 2699 | int i, n; |
| 2700 | |
Guido van Rossum | cab0580 | 2002-06-10 15:29:03 +0000 | [diff] [blame] | 2701 | if (type->tp_flags & Py_TPFLAGS_READY) { |
| 2702 | assert(type->tp_dict != NULL); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2703 | return 0; |
Guido van Rossum | cab0580 | 2002-06-10 15:29:03 +0000 | [diff] [blame] | 2704 | } |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2705 | assert((type->tp_flags & Py_TPFLAGS_READYING) == 0); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2706 | |
| 2707 | type->tp_flags |= Py_TPFLAGS_READYING; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2708 | |
| 2709 | /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
| 2710 | base = type->tp_base; |
| 2711 | if (base == NULL && type != &PyBaseObject_Type) |
| 2712 | base = type->tp_base = &PyBaseObject_Type; |
| 2713 | |
Guido van Rossum | 323a9cf | 2002-08-14 17:26:30 +0000 | [diff] [blame] | 2714 | /* Initialize the base class */ |
| 2715 | if (base && base->tp_dict == NULL) { |
| 2716 | if (PyType_Ready(base) < 0) |
| 2717 | goto error; |
| 2718 | } |
| 2719 | |
Guido van Rossum | 0986d82 | 2002-04-08 01:38:42 +0000 | [diff] [blame] | 2720 | /* Initialize ob_type if NULL. This means extensions that want to be |
| 2721 | compilable separately on Windows can call PyType_Ready() instead of |
| 2722 | initializing the ob_type field of their type objects. */ |
| 2723 | if (type->ob_type == NULL) |
| 2724 | type->ob_type = base->ob_type; |
| 2725 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2726 | /* Initialize tp_bases */ |
| 2727 | bases = type->tp_bases; |
| 2728 | if (bases == NULL) { |
| 2729 | if (base == NULL) |
| 2730 | bases = PyTuple_New(0); |
| 2731 | else |
| 2732 | bases = Py_BuildValue("(O)", base); |
| 2733 | if (bases == NULL) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2734 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2735 | type->tp_bases = bases; |
| 2736 | } |
| 2737 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2738 | /* Initialize tp_dict */ |
| 2739 | dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2740 | if (dict == NULL) { |
| 2741 | dict = PyDict_New(); |
| 2742 | if (dict == NULL) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2743 | goto error; |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2744 | type->tp_dict = dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2747 | /* Add type-specific descriptors to tp_dict */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2748 | if (add_operators(type) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2749 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2750 | if (type->tp_methods != NULL) { |
| 2751 | if (add_methods(type, type->tp_methods) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2752 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2753 | } |
| 2754 | if (type->tp_members != NULL) { |
| 2755 | if (add_members(type, type->tp_members) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2756 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2757 | } |
| 2758 | if (type->tp_getset != NULL) { |
| 2759 | if (add_getset(type, type->tp_getset) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2760 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2761 | } |
| 2762 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2763 | /* Calculate method resolution order */ |
| 2764 | if (mro_internal(type) < 0) { |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2765 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2768 | /* Inherit special flags from dominant base */ |
| 2769 | if (type->tp_base != NULL) |
| 2770 | inherit_special(type, type->tp_base); |
| 2771 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2772 | /* Initialize tp_dict properly */ |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 2773 | bases = type->tp_mro; |
| 2774 | assert(bases != NULL); |
| 2775 | assert(PyTuple_Check(bases)); |
| 2776 | n = PyTuple_GET_SIZE(bases); |
| 2777 | for (i = 1; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 2778 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 2779 | if (PyType_Check(b)) |
| 2780 | inherit_slots(type, (PyTypeObject *)b); |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 2781 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2782 | |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 2783 | /* if the type dictionary doesn't contain a __doc__, set it from |
| 2784 | the tp_doc slot. |
| 2785 | */ |
| 2786 | if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) { |
| 2787 | if (type->tp_doc != NULL) { |
| 2788 | PyObject *doc = PyString_FromString(type->tp_doc); |
| 2789 | PyDict_SetItemString(type->tp_dict, "__doc__", doc); |
| 2790 | Py_DECREF(doc); |
| 2791 | } else { |
Guido van Rossum | d464107 | 2002-04-03 02:13:37 +0000 | [diff] [blame] | 2792 | PyDict_SetItemString(type->tp_dict, |
| 2793 | "__doc__", Py_None); |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 2794 | } |
| 2795 | } |
| 2796 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2797 | /* Some more special stuff */ |
| 2798 | base = type->tp_base; |
| 2799 | if (base != NULL) { |
| 2800 | if (type->tp_as_number == NULL) |
| 2801 | type->tp_as_number = base->tp_as_number; |
| 2802 | if (type->tp_as_sequence == NULL) |
| 2803 | type->tp_as_sequence = base->tp_as_sequence; |
| 2804 | if (type->tp_as_mapping == NULL) |
| 2805 | type->tp_as_mapping = base->tp_as_mapping; |
| 2806 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2807 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2808 | /* Link into each base class's list of subclasses */ |
| 2809 | bases = type->tp_bases; |
| 2810 | n = PyTuple_GET_SIZE(bases); |
| 2811 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 2812 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 2813 | if (PyType_Check(b) && |
| 2814 | add_subclass((PyTypeObject *)b, type) < 0) |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2815 | goto error; |
| 2816 | } |
| 2817 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 2818 | /* All done -- set the ready flag */ |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2819 | assert(type->tp_dict != NULL); |
| 2820 | type->tp_flags = |
| 2821 | (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2822 | return 0; |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 2823 | |
| 2824 | error: |
| 2825 | type->tp_flags &= ~Py_TPFLAGS_READYING; |
| 2826 | return -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2829 | static int |
| 2830 | add_subclass(PyTypeObject *base, PyTypeObject *type) |
| 2831 | { |
| 2832 | int i; |
| 2833 | PyObject *list, *ref, *new; |
| 2834 | |
| 2835 | list = base->tp_subclasses; |
| 2836 | if (list == NULL) { |
| 2837 | base->tp_subclasses = list = PyList_New(0); |
| 2838 | if (list == NULL) |
| 2839 | return -1; |
| 2840 | } |
| 2841 | assert(PyList_Check(list)); |
| 2842 | new = PyWeakref_NewRef((PyObject *)type, NULL); |
| 2843 | i = PyList_GET_SIZE(list); |
| 2844 | while (--i >= 0) { |
| 2845 | ref = PyList_GET_ITEM(list, i); |
| 2846 | assert(PyWeakref_CheckRef(ref)); |
Guido van Rossum | 3930bc3 | 2002-10-18 13:51:49 +0000 | [diff] [blame] | 2847 | if (PyWeakref_GET_OBJECT(ref) == Py_None) |
| 2848 | return PyList_SetItem(list, i, new); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2849 | } |
| 2850 | i = PyList_Append(list, new); |
| 2851 | Py_DECREF(new); |
| 2852 | return i; |
| 2853 | } |
| 2854 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2855 | static void |
| 2856 | remove_subclass(PyTypeObject *base, PyTypeObject *type) |
| 2857 | { |
| 2858 | int i; |
| 2859 | PyObject *list, *ref; |
| 2860 | |
| 2861 | list = base->tp_subclasses; |
| 2862 | if (list == NULL) { |
| 2863 | return; |
| 2864 | } |
| 2865 | assert(PyList_Check(list)); |
| 2866 | i = PyList_GET_SIZE(list); |
| 2867 | while (--i >= 0) { |
| 2868 | ref = PyList_GET_ITEM(list, i); |
| 2869 | assert(PyWeakref_CheckRef(ref)); |
| 2870 | if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) { |
| 2871 | /* this can't fail, right? */ |
| 2872 | PySequence_DelItem(list, i); |
| 2873 | return; |
| 2874 | } |
| 2875 | } |
| 2876 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2877 | |
| 2878 | /* Generic wrappers for overloadable 'operators' such as __getitem__ */ |
| 2879 | |
| 2880 | /* There's a wrapper *function* for each distinct function typedef used |
| 2881 | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a |
| 2882 | wrapper *table* for each distinct operation (e.g. __len__, __add__). |
| 2883 | Most tables have only one entry; the tables for binary operators have two |
| 2884 | entries, one regular and one with reversed arguments. */ |
| 2885 | |
| 2886 | static PyObject * |
| 2887 | wrap_inquiry(PyObject *self, PyObject *args, void *wrapped) |
| 2888 | { |
| 2889 | inquiry func = (inquiry)wrapped; |
| 2890 | int res; |
| 2891 | |
| 2892 | if (!PyArg_ParseTuple(args, "")) |
| 2893 | return NULL; |
| 2894 | res = (*func)(self); |
| 2895 | if (res == -1 && PyErr_Occurred()) |
| 2896 | return NULL; |
| 2897 | return PyInt_FromLong((long)res); |
| 2898 | } |
| 2899 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2900 | static PyObject * |
| 2901 | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 2902 | { |
| 2903 | binaryfunc func = (binaryfunc)wrapped; |
| 2904 | PyObject *other; |
| 2905 | |
| 2906 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 2907 | return NULL; |
| 2908 | return (*func)(self, other); |
| 2909 | } |
| 2910 | |
| 2911 | static PyObject * |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 2912 | wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped) |
| 2913 | { |
| 2914 | binaryfunc func = (binaryfunc)wrapped; |
| 2915 | PyObject *other; |
| 2916 | |
| 2917 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 2918 | return NULL; |
| 2919 | if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) && |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 2920 | !PyType_IsSubtype(other->ob_type, self->ob_type)) { |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 2921 | Py_INCREF(Py_NotImplemented); |
| 2922 | return Py_NotImplemented; |
| 2923 | } |
| 2924 | return (*func)(self, other); |
| 2925 | } |
| 2926 | |
| 2927 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2928 | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 2929 | { |
| 2930 | binaryfunc func = (binaryfunc)wrapped; |
| 2931 | PyObject *other; |
| 2932 | |
| 2933 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 2934 | return NULL; |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 2935 | if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) && |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 2936 | !PyType_IsSubtype(other->ob_type, self->ob_type)) { |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 2937 | Py_INCREF(Py_NotImplemented); |
| 2938 | return Py_NotImplemented; |
| 2939 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2940 | return (*func)(other, self); |
| 2941 | } |
| 2942 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 2943 | static PyObject * |
| 2944 | wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped) |
| 2945 | { |
| 2946 | coercion func = (coercion)wrapped; |
| 2947 | PyObject *other, *res; |
| 2948 | int ok; |
| 2949 | |
| 2950 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 2951 | return NULL; |
| 2952 | ok = func(&self, &other); |
| 2953 | if (ok < 0) |
| 2954 | return NULL; |
| 2955 | if (ok > 0) { |
| 2956 | Py_INCREF(Py_NotImplemented); |
| 2957 | return Py_NotImplemented; |
| 2958 | } |
| 2959 | res = PyTuple_New(2); |
| 2960 | if (res == NULL) { |
| 2961 | Py_DECREF(self); |
| 2962 | Py_DECREF(other); |
| 2963 | return NULL; |
| 2964 | } |
| 2965 | PyTuple_SET_ITEM(res, 0, self); |
| 2966 | PyTuple_SET_ITEM(res, 1, other); |
| 2967 | return res; |
| 2968 | } |
| 2969 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2970 | static PyObject * |
| 2971 | wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 2972 | { |
| 2973 | ternaryfunc func = (ternaryfunc)wrapped; |
| 2974 | PyObject *other; |
| 2975 | PyObject *third = Py_None; |
| 2976 | |
| 2977 | /* Note: This wrapper only works for __pow__() */ |
| 2978 | |
| 2979 | if (!PyArg_ParseTuple(args, "O|O", &other, &third)) |
| 2980 | return NULL; |
| 2981 | return (*func)(self, other, third); |
| 2982 | } |
| 2983 | |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 2984 | static PyObject * |
| 2985 | wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 2986 | { |
| 2987 | ternaryfunc func = (ternaryfunc)wrapped; |
| 2988 | PyObject *other; |
| 2989 | PyObject *third = Py_None; |
| 2990 | |
| 2991 | /* Note: This wrapper only works for __pow__() */ |
| 2992 | |
| 2993 | if (!PyArg_ParseTuple(args, "O|O", &other, &third)) |
| 2994 | return NULL; |
| 2995 | return (*func)(other, self, third); |
| 2996 | } |
| 2997 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2998 | static PyObject * |
| 2999 | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 3000 | { |
| 3001 | unaryfunc func = (unaryfunc)wrapped; |
| 3002 | |
| 3003 | if (!PyArg_ParseTuple(args, "")) |
| 3004 | return NULL; |
| 3005 | return (*func)(self); |
| 3006 | } |
| 3007 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3008 | static PyObject * |
| 3009 | wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped) |
| 3010 | { |
| 3011 | intargfunc func = (intargfunc)wrapped; |
| 3012 | int i; |
| 3013 | |
| 3014 | if (!PyArg_ParseTuple(args, "i", &i)) |
| 3015 | return NULL; |
| 3016 | return (*func)(self, i); |
| 3017 | } |
| 3018 | |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3019 | static int |
| 3020 | getindex(PyObject *self, PyObject *arg) |
| 3021 | { |
| 3022 | int i; |
| 3023 | |
| 3024 | i = PyInt_AsLong(arg); |
| 3025 | if (i == -1 && PyErr_Occurred()) |
| 3026 | return -1; |
| 3027 | if (i < 0) { |
| 3028 | PySequenceMethods *sq = self->ob_type->tp_as_sequence; |
| 3029 | if (sq && sq->sq_length) { |
| 3030 | int n = (*sq->sq_length)(self); |
| 3031 | if (n < 0) |
| 3032 | return -1; |
| 3033 | i += n; |
| 3034 | } |
| 3035 | } |
| 3036 | return i; |
| 3037 | } |
| 3038 | |
| 3039 | static PyObject * |
| 3040 | wrap_sq_item(PyObject *self, PyObject *args, void *wrapped) |
| 3041 | { |
| 3042 | intargfunc func = (intargfunc)wrapped; |
| 3043 | PyObject *arg; |
| 3044 | int i; |
| 3045 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 3046 | if (PyTuple_GET_SIZE(args) == 1) { |
| 3047 | arg = PyTuple_GET_ITEM(args, 0); |
| 3048 | i = getindex(self, arg); |
| 3049 | if (i == -1 && PyErr_Occurred()) |
| 3050 | return NULL; |
| 3051 | return (*func)(self, i); |
| 3052 | } |
| 3053 | PyArg_ParseTuple(args, "O", &arg); |
| 3054 | assert(PyErr_Occurred()); |
| 3055 | return NULL; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3056 | } |
| 3057 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3058 | static PyObject * |
| 3059 | wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped) |
| 3060 | { |
| 3061 | intintargfunc func = (intintargfunc)wrapped; |
| 3062 | int i, j; |
| 3063 | |
| 3064 | if (!PyArg_ParseTuple(args, "ii", &i, &j)) |
| 3065 | return NULL; |
| 3066 | return (*func)(self, i, j); |
| 3067 | } |
| 3068 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3069 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3070 | wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3071 | { |
| 3072 | intobjargproc func = (intobjargproc)wrapped; |
| 3073 | int i, res; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3074 | PyObject *arg, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3075 | |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3076 | if (!PyArg_ParseTuple(args, "OO", &arg, &value)) |
| 3077 | return NULL; |
| 3078 | i = getindex(self, arg); |
| 3079 | if (i == -1 && PyErr_Occurred()) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3080 | return NULL; |
| 3081 | res = (*func)(self, i, value); |
| 3082 | if (res == -1 && PyErr_Occurred()) |
| 3083 | return NULL; |
| 3084 | Py_INCREF(Py_None); |
| 3085 | return Py_None; |
| 3086 | } |
| 3087 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 3088 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3089 | wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 3090 | { |
| 3091 | intobjargproc func = (intobjargproc)wrapped; |
| 3092 | int i, res; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3093 | PyObject *arg; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 3094 | |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3095 | if (!PyArg_ParseTuple(args, "O", &arg)) |
| 3096 | return NULL; |
| 3097 | i = getindex(self, arg); |
| 3098 | if (i == -1 && PyErr_Occurred()) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 3099 | return NULL; |
| 3100 | res = (*func)(self, i, NULL); |
| 3101 | if (res == -1 && PyErr_Occurred()) |
| 3102 | return NULL; |
| 3103 | Py_INCREF(Py_None); |
| 3104 | return Py_None; |
| 3105 | } |
| 3106 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3107 | static PyObject * |
| 3108 | wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 3109 | { |
| 3110 | intintobjargproc func = (intintobjargproc)wrapped; |
| 3111 | int i, j, res; |
| 3112 | PyObject *value; |
| 3113 | |
| 3114 | if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value)) |
| 3115 | return NULL; |
| 3116 | res = (*func)(self, i, j, value); |
| 3117 | if (res == -1 && PyErr_Occurred()) |
| 3118 | return NULL; |
| 3119 | Py_INCREF(Py_None); |
| 3120 | return Py_None; |
| 3121 | } |
| 3122 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3123 | static PyObject * |
| 3124 | wrap_delslice(PyObject *self, PyObject *args, void *wrapped) |
| 3125 | { |
| 3126 | intintobjargproc func = (intintobjargproc)wrapped; |
| 3127 | int i, j, res; |
| 3128 | |
| 3129 | if (!PyArg_ParseTuple(args, "ii", &i, &j)) |
| 3130 | return NULL; |
| 3131 | res = (*func)(self, i, j, NULL); |
| 3132 | if (res == -1 && PyErr_Occurred()) |
| 3133 | return NULL; |
| 3134 | Py_INCREF(Py_None); |
| 3135 | return Py_None; |
| 3136 | } |
| 3137 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3138 | /* XXX objobjproc is a misnomer; should be objargpred */ |
| 3139 | static PyObject * |
| 3140 | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) |
| 3141 | { |
| 3142 | objobjproc func = (objobjproc)wrapped; |
| 3143 | int res; |
| 3144 | PyObject *value; |
| 3145 | |
| 3146 | if (!PyArg_ParseTuple(args, "O", &value)) |
| 3147 | return NULL; |
| 3148 | res = (*func)(self, value); |
| 3149 | if (res == -1 && PyErr_Occurred()) |
| 3150 | return NULL; |
| 3151 | return PyInt_FromLong((long)res); |
| 3152 | } |
| 3153 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3154 | static PyObject * |
| 3155 | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 3156 | { |
| 3157 | objobjargproc func = (objobjargproc)wrapped; |
| 3158 | int res; |
| 3159 | PyObject *key, *value; |
| 3160 | |
| 3161 | if (!PyArg_ParseTuple(args, "OO", &key, &value)) |
| 3162 | return NULL; |
| 3163 | res = (*func)(self, key, value); |
| 3164 | if (res == -1 && PyErr_Occurred()) |
| 3165 | return NULL; |
| 3166 | Py_INCREF(Py_None); |
| 3167 | return Py_None; |
| 3168 | } |
| 3169 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 3170 | static PyObject * |
| 3171 | wrap_delitem(PyObject *self, PyObject *args, void *wrapped) |
| 3172 | { |
| 3173 | objobjargproc func = (objobjargproc)wrapped; |
| 3174 | int res; |
| 3175 | PyObject *key; |
| 3176 | |
| 3177 | if (!PyArg_ParseTuple(args, "O", &key)) |
| 3178 | return NULL; |
| 3179 | res = (*func)(self, key, NULL); |
| 3180 | if (res == -1 && PyErr_Occurred()) |
| 3181 | return NULL; |
| 3182 | Py_INCREF(Py_None); |
| 3183 | return Py_None; |
| 3184 | } |
| 3185 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3186 | static PyObject * |
| 3187 | wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped) |
| 3188 | { |
| 3189 | cmpfunc func = (cmpfunc)wrapped; |
| 3190 | int res; |
| 3191 | PyObject *other; |
| 3192 | |
| 3193 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 3194 | return NULL; |
Guido van Rossum | 3d45d8f | 2001-09-24 18:47:40 +0000 | [diff] [blame] | 3195 | if (other->ob_type->tp_compare != func && |
| 3196 | !PyType_IsSubtype(other->ob_type, self->ob_type)) { |
Guido van Rossum | ceccae5 | 2001-09-18 20:03:57 +0000 | [diff] [blame] | 3197 | PyErr_Format( |
| 3198 | PyExc_TypeError, |
| 3199 | "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'", |
| 3200 | self->ob_type->tp_name, |
| 3201 | self->ob_type->tp_name, |
| 3202 | other->ob_type->tp_name); |
| 3203 | return NULL; |
| 3204 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3205 | res = (*func)(self, other); |
| 3206 | if (PyErr_Occurred()) |
| 3207 | return NULL; |
| 3208 | return PyInt_FromLong((long)res); |
| 3209 | } |
| 3210 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3211 | static PyObject * |
| 3212 | wrap_setattr(PyObject *self, PyObject *args, void *wrapped) |
| 3213 | { |
| 3214 | setattrofunc func = (setattrofunc)wrapped; |
| 3215 | int res; |
| 3216 | PyObject *name, *value; |
| 3217 | |
| 3218 | if (!PyArg_ParseTuple(args, "OO", &name, &value)) |
| 3219 | return NULL; |
| 3220 | res = (*func)(self, name, value); |
| 3221 | if (res < 0) |
| 3222 | return NULL; |
| 3223 | Py_INCREF(Py_None); |
| 3224 | return Py_None; |
| 3225 | } |
| 3226 | |
| 3227 | static PyObject * |
| 3228 | wrap_delattr(PyObject *self, PyObject *args, void *wrapped) |
| 3229 | { |
| 3230 | setattrofunc func = (setattrofunc)wrapped; |
| 3231 | int res; |
| 3232 | PyObject *name; |
| 3233 | |
| 3234 | if (!PyArg_ParseTuple(args, "O", &name)) |
| 3235 | return NULL; |
| 3236 | res = (*func)(self, name, NULL); |
| 3237 | if (res < 0) |
| 3238 | return NULL; |
| 3239 | Py_INCREF(Py_None); |
| 3240 | return Py_None; |
| 3241 | } |
| 3242 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3243 | static PyObject * |
| 3244 | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) |
| 3245 | { |
| 3246 | hashfunc func = (hashfunc)wrapped; |
| 3247 | long res; |
| 3248 | |
| 3249 | if (!PyArg_ParseTuple(args, "")) |
| 3250 | return NULL; |
| 3251 | res = (*func)(self); |
| 3252 | if (res == -1 && PyErr_Occurred()) |
| 3253 | return NULL; |
| 3254 | return PyInt_FromLong(res); |
| 3255 | } |
| 3256 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3257 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 3258 | wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3259 | { |
| 3260 | ternaryfunc func = (ternaryfunc)wrapped; |
| 3261 | |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 3262 | return (*func)(self, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3265 | static PyObject * |
| 3266 | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) |
| 3267 | { |
| 3268 | richcmpfunc func = (richcmpfunc)wrapped; |
| 3269 | PyObject *other; |
| 3270 | |
| 3271 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 3272 | return NULL; |
| 3273 | return (*func)(self, other, op); |
| 3274 | } |
| 3275 | |
| 3276 | #undef RICHCMP_WRAPPER |
| 3277 | #define RICHCMP_WRAPPER(NAME, OP) \ |
| 3278 | static PyObject * \ |
| 3279 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ |
| 3280 | { \ |
| 3281 | return wrap_richcmpfunc(self, args, wrapped, OP); \ |
| 3282 | } |
| 3283 | |
Jack Jansen | 8e938b4 | 2001-08-08 15:29:49 +0000 | [diff] [blame] | 3284 | RICHCMP_WRAPPER(lt, Py_LT) |
| 3285 | RICHCMP_WRAPPER(le, Py_LE) |
| 3286 | RICHCMP_WRAPPER(eq, Py_EQ) |
| 3287 | RICHCMP_WRAPPER(ne, Py_NE) |
| 3288 | RICHCMP_WRAPPER(gt, Py_GT) |
| 3289 | RICHCMP_WRAPPER(ge, Py_GE) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3290 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3291 | static PyObject * |
| 3292 | wrap_next(PyObject *self, PyObject *args, void *wrapped) |
| 3293 | { |
| 3294 | unaryfunc func = (unaryfunc)wrapped; |
| 3295 | PyObject *res; |
| 3296 | |
| 3297 | if (!PyArg_ParseTuple(args, "")) |
| 3298 | return NULL; |
| 3299 | res = (*func)(self); |
| 3300 | if (res == NULL && !PyErr_Occurred()) |
| 3301 | PyErr_SetNone(PyExc_StopIteration); |
| 3302 | return res; |
| 3303 | } |
| 3304 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3305 | static PyObject * |
| 3306 | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped) |
| 3307 | { |
| 3308 | descrgetfunc func = (descrgetfunc)wrapped; |
| 3309 | PyObject *obj; |
| 3310 | PyObject *type = NULL; |
| 3311 | |
| 3312 | if (!PyArg_ParseTuple(args, "O|O", &obj, &type)) |
| 3313 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3314 | return (*func)(self, obj, type); |
| 3315 | } |
| 3316 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3317 | static PyObject * |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3318 | wrap_descr_set(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3319 | { |
| 3320 | descrsetfunc func = (descrsetfunc)wrapped; |
| 3321 | PyObject *obj, *value; |
| 3322 | int ret; |
| 3323 | |
| 3324 | if (!PyArg_ParseTuple(args, "OO", &obj, &value)) |
| 3325 | return NULL; |
| 3326 | ret = (*func)(self, obj, value); |
| 3327 | if (ret < 0) |
| 3328 | return NULL; |
| 3329 | Py_INCREF(Py_None); |
| 3330 | return Py_None; |
| 3331 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 3332 | |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 3333 | static PyObject * |
| 3334 | wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped) |
| 3335 | { |
| 3336 | descrsetfunc func = (descrsetfunc)wrapped; |
| 3337 | PyObject *obj; |
| 3338 | int ret; |
| 3339 | |
| 3340 | if (!PyArg_ParseTuple(args, "O", &obj)) |
| 3341 | return NULL; |
| 3342 | ret = (*func)(self, obj, NULL); |
| 3343 | if (ret < 0) |
| 3344 | return NULL; |
| 3345 | Py_INCREF(Py_None); |
| 3346 | return Py_None; |
| 3347 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3348 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3349 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 3350 | wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3351 | { |
| 3352 | initproc func = (initproc)wrapped; |
| 3353 | |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 3354 | if (func(self, args, kwds) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3355 | return NULL; |
| 3356 | Py_INCREF(Py_None); |
| 3357 | return Py_None; |
| 3358 | } |
| 3359 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3360 | static PyObject * |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 3361 | tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3362 | { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 3363 | PyTypeObject *type, *subtype, *staticbase; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 3364 | PyObject *arg0, *res; |
| 3365 | |
| 3366 | if (self == NULL || !PyType_Check(self)) |
| 3367 | Py_FatalError("__new__() called with non-type 'self'"); |
| 3368 | type = (PyTypeObject *)self; |
| 3369 | if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 3370 | PyErr_Format(PyExc_TypeError, |
| 3371 | "%s.__new__(): not enough arguments", |
| 3372 | type->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 3373 | return NULL; |
| 3374 | } |
| 3375 | arg0 = PyTuple_GET_ITEM(args, 0); |
| 3376 | if (!PyType_Check(arg0)) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 3377 | PyErr_Format(PyExc_TypeError, |
| 3378 | "%s.__new__(X): X is not a type object (%s)", |
| 3379 | type->tp_name, |
| 3380 | arg0->ob_type->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 3381 | return NULL; |
| 3382 | } |
| 3383 | subtype = (PyTypeObject *)arg0; |
| 3384 | if (!PyType_IsSubtype(subtype, type)) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 3385 | PyErr_Format(PyExc_TypeError, |
| 3386 | "%s.__new__(%s): %s is not a subtype of %s", |
| 3387 | type->tp_name, |
| 3388 | subtype->tp_name, |
| 3389 | subtype->tp_name, |
| 3390 | type->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 3391 | return NULL; |
| 3392 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 3393 | |
| 3394 | /* Check that the use doesn't do something silly and unsafe like |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 3395 | object.__new__(dict). To do this, we check that the |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 3396 | most derived base that's not a heap type is this type. */ |
| 3397 | staticbase = subtype; |
| 3398 | while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 3399 | staticbase = staticbase->tp_base; |
Guido van Rossum | a8c60f4 | 2001-09-14 19:43:36 +0000 | [diff] [blame] | 3400 | if (staticbase->tp_new != type->tp_new) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 3401 | PyErr_Format(PyExc_TypeError, |
| 3402 | "%s.__new__(%s) is not safe, use %s.__new__()", |
| 3403 | type->tp_name, |
| 3404 | subtype->tp_name, |
| 3405 | staticbase == NULL ? "?" : staticbase->tp_name); |
| 3406 | return NULL; |
| 3407 | } |
| 3408 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 3409 | args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 3410 | if (args == NULL) |
| 3411 | return NULL; |
| 3412 | res = type->tp_new(subtype, args, kwds); |
| 3413 | Py_DECREF(args); |
| 3414 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3415 | } |
| 3416 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 3417 | static struct PyMethodDef tp_new_methoddef[] = { |
| 3418 | {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 3419 | PyDoc_STR("T.__new__(S, ...) -> " |
| 3420 | "a new object with type S, a subtype of T")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3421 | {0} |
| 3422 | }; |
| 3423 | |
| 3424 | static int |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 3425 | add_tp_new_wrapper(PyTypeObject *type) |
| 3426 | { |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 3427 | PyObject *func; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 3428 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3429 | if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL) |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 3430 | return 0; |
| 3431 | func = PyCFunction_New(tp_new_methoddef, (PyObject *)type); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 3432 | if (func == NULL) |
| 3433 | return -1; |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3434 | return PyDict_SetItemString(type->tp_dict, "__new__", func); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 3435 | } |
| 3436 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 3437 | /* Slot wrappers that call the corresponding __foo__ slot. See comments |
| 3438 | below at override_slots() for more explanation. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3439 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3440 | #define SLOT0(FUNCNAME, OPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3441 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3442 | FUNCNAME(PyObject *self) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3443 | { \ |
Guido van Rossum | 5592e4d | 2001-08-28 18:28:21 +0000 | [diff] [blame] | 3444 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3445 | return call_method(self, OPSTR, &cache_str, "()"); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3446 | } |
| 3447 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3448 | #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3449 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3450 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3451 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3452 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3453 | return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3454 | } |
| 3455 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3456 | |
| 3457 | #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3458 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3459 | FUNCNAME(PyObject *self, PyObject *other) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3460 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3461 | static PyObject *cache_str, *rcache_str; \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3462 | int do_other = self->ob_type != other->ob_type && \ |
| 3463 | other->ob_type->tp_as_number != NULL && \ |
| 3464 | other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3465 | if (self->ob_type->tp_as_number != NULL && \ |
| 3466 | self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \ |
| 3467 | PyObject *r; \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3468 | if (do_other && \ |
| 3469 | PyType_IsSubtype(other->ob_type, self->ob_type)) { \ |
| 3470 | r = call_maybe( \ |
| 3471 | other, ROPSTR, &rcache_str, "(O)", self); \ |
| 3472 | if (r != Py_NotImplemented) \ |
| 3473 | return r; \ |
| 3474 | Py_DECREF(r); \ |
| 3475 | do_other = 0; \ |
| 3476 | } \ |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 3477 | r = call_maybe( \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3478 | self, OPSTR, &cache_str, "(O)", other); \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3479 | if (r != Py_NotImplemented || \ |
| 3480 | other->ob_type == self->ob_type) \ |
| 3481 | return r; \ |
| 3482 | Py_DECREF(r); \ |
| 3483 | } \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3484 | if (do_other) { \ |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 3485 | return call_maybe( \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3486 | other, ROPSTR, &rcache_str, "(O)", self); \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3487 | } \ |
| 3488 | Py_INCREF(Py_NotImplemented); \ |
| 3489 | return Py_NotImplemented; \ |
| 3490 | } |
| 3491 | |
| 3492 | #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ |
| 3493 | SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR) |
| 3494 | |
| 3495 | #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \ |
| 3496 | static PyObject * \ |
| 3497 | FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ |
| 3498 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3499 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3500 | return call_method(self, OPSTR, &cache_str, \ |
| 3501 | "(" ARGCODES ")", arg1, arg2); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
| 3504 | static int |
| 3505 | slot_sq_length(PyObject *self) |
| 3506 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3507 | static PyObject *len_str; |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3508 | PyObject *res = call_method(self, "__len__", &len_str, "()"); |
Guido van Rossum | 2611162 | 2001-10-01 16:42:49 +0000 | [diff] [blame] | 3509 | int len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3510 | |
| 3511 | if (res == NULL) |
| 3512 | return -1; |
Guido van Rossum | 2611162 | 2001-10-01 16:42:49 +0000 | [diff] [blame] | 3513 | len = (int)PyInt_AsLong(res); |
| 3514 | Py_DECREF(res); |
Jeremy Hylton | 73a088e | 2002-07-25 16:43:29 +0000 | [diff] [blame] | 3515 | if (len == -1 && PyErr_Occurred()) |
| 3516 | return -1; |
Jeremy Hylton | f20fcf9 | 2002-07-25 16:06:15 +0000 | [diff] [blame] | 3517 | if (len < 0) { |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 3518 | PyErr_SetString(PyExc_ValueError, |
Jeremy Hylton | f20fcf9 | 2002-07-25 16:06:15 +0000 | [diff] [blame] | 3519 | "__len__() should return >= 0"); |
| 3520 | return -1; |
| 3521 | } |
Guido van Rossum | 2611162 | 2001-10-01 16:42:49 +0000 | [diff] [blame] | 3522 | return len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3523 | } |
| 3524 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3525 | SLOT1(slot_sq_concat, "__add__", PyObject *, "O") |
| 3526 | SLOT1(slot_sq_repeat, "__mul__", int, "i") |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 3527 | |
| 3528 | /* Super-optimized version of slot_sq_item. |
| 3529 | Other slots could do the same... */ |
| 3530 | static PyObject * |
| 3531 | slot_sq_item(PyObject *self, int i) |
| 3532 | { |
| 3533 | static PyObject *getitem_str; |
| 3534 | PyObject *func, *args = NULL, *ival = NULL, *retval = NULL; |
| 3535 | descrgetfunc f; |
| 3536 | |
| 3537 | if (getitem_str == NULL) { |
| 3538 | getitem_str = PyString_InternFromString("__getitem__"); |
| 3539 | if (getitem_str == NULL) |
| 3540 | return NULL; |
| 3541 | } |
| 3542 | func = _PyType_Lookup(self->ob_type, getitem_str); |
| 3543 | if (func != NULL) { |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 3544 | if ((f = func->ob_type->tp_descr_get) == NULL) |
| 3545 | Py_INCREF(func); |
Neal Norwitz | 673cd82 | 2002-10-18 16:33:13 +0000 | [diff] [blame] | 3546 | else { |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 3547 | func = f(func, self, (PyObject *)(self->ob_type)); |
Neal Norwitz | 673cd82 | 2002-10-18 16:33:13 +0000 | [diff] [blame] | 3548 | if (func == NULL) { |
| 3549 | return NULL; |
| 3550 | } |
| 3551 | } |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 3552 | ival = PyInt_FromLong(i); |
| 3553 | if (ival != NULL) { |
| 3554 | args = PyTuple_New(1); |
| 3555 | if (args != NULL) { |
| 3556 | PyTuple_SET_ITEM(args, 0, ival); |
| 3557 | retval = PyObject_Call(func, args, NULL); |
| 3558 | Py_XDECREF(args); |
| 3559 | Py_XDECREF(func); |
| 3560 | return retval; |
| 3561 | } |
| 3562 | } |
| 3563 | } |
| 3564 | else { |
| 3565 | PyErr_SetObject(PyExc_AttributeError, getitem_str); |
| 3566 | } |
| 3567 | Py_XDECREF(args); |
| 3568 | Py_XDECREF(ival); |
| 3569 | Py_XDECREF(func); |
| 3570 | return NULL; |
| 3571 | } |
| 3572 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3573 | SLOT2(slot_sq_slice, "__getslice__", int, int, "ii") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3574 | |
| 3575 | static int |
| 3576 | slot_sq_ass_item(PyObject *self, int index, PyObject *value) |
| 3577 | { |
| 3578 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3579 | static PyObject *delitem_str, *setitem_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3580 | |
| 3581 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3582 | res = call_method(self, "__delitem__", &delitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3583 | "(i)", index); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3584 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3585 | res = call_method(self, "__setitem__", &setitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3586 | "(iO)", index, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3587 | if (res == NULL) |
| 3588 | return -1; |
| 3589 | Py_DECREF(res); |
| 3590 | return 0; |
| 3591 | } |
| 3592 | |
| 3593 | static int |
| 3594 | slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value) |
| 3595 | { |
| 3596 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3597 | static PyObject *delslice_str, *setslice_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3598 | |
| 3599 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3600 | res = call_method(self, "__delslice__", &delslice_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3601 | "(ii)", i, j); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3602 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3603 | res = call_method(self, "__setslice__", &setslice_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3604 | "(iiO)", i, j, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3605 | if (res == NULL) |
| 3606 | return -1; |
| 3607 | Py_DECREF(res); |
| 3608 | return 0; |
| 3609 | } |
| 3610 | |
| 3611 | static int |
| 3612 | slot_sq_contains(PyObject *self, PyObject *value) |
| 3613 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3614 | PyObject *func, *res, *args; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3615 | static PyObject *contains_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3616 | |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3617 | func = lookup_maybe(self, "__contains__", &contains_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3618 | |
| 3619 | if (func != NULL) { |
| 3620 | args = Py_BuildValue("(O)", value); |
| 3621 | if (args == NULL) |
| 3622 | res = NULL; |
| 3623 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3624 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3625 | Py_DECREF(args); |
| 3626 | } |
| 3627 | Py_DECREF(func); |
| 3628 | if (res == NULL) |
| 3629 | return -1; |
| 3630 | return PyObject_IsTrue(res); |
| 3631 | } |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3632 | else if (PyErr_Occurred()) |
| 3633 | return -1; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3634 | else { |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 3635 | return _PySequence_IterSearch(self, value, |
| 3636 | PY_ITERSEARCH_CONTAINS); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3637 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3638 | } |
| 3639 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3640 | SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O") |
| 3641 | SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3642 | |
| 3643 | #define slot_mp_length slot_sq_length |
| 3644 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3645 | SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3646 | |
| 3647 | static int |
| 3648 | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) |
| 3649 | { |
| 3650 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3651 | static PyObject *delitem_str, *setitem_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3652 | |
| 3653 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3654 | res = call_method(self, "__delitem__", &delitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3655 | "(O)", key); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3656 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3657 | res = call_method(self, "__setitem__", &setitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3658 | "(OO)", key, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3659 | if (res == NULL) |
| 3660 | return -1; |
| 3661 | Py_DECREF(res); |
| 3662 | return 0; |
| 3663 | } |
| 3664 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3665 | SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__") |
| 3666 | SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__") |
| 3667 | SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__") |
| 3668 | SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__") |
| 3669 | SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__") |
| 3670 | SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__") |
| 3671 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 3672 | static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3673 | |
| 3674 | SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, |
| 3675 | nb_power, "__pow__", "__rpow__") |
| 3676 | |
| 3677 | static PyObject * |
| 3678 | slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) |
| 3679 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3680 | static PyObject *pow_str; |
| 3681 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3682 | if (modulus == Py_None) |
| 3683 | return slot_nb_power_binary(self, other); |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 3684 | /* Three-arg power doesn't use __rpow__. But ternary_op |
| 3685 | can call this when the second argument's type uses |
| 3686 | slot_nb_power, so check before calling self.__pow__. */ |
| 3687 | if (self->ob_type->tp_as_number != NULL && |
| 3688 | self->ob_type->tp_as_number->nb_power == slot_nb_power) { |
| 3689 | return call_method(self, "__pow__", &pow_str, |
| 3690 | "(OO)", other, modulus); |
| 3691 | } |
| 3692 | Py_INCREF(Py_NotImplemented); |
| 3693 | return Py_NotImplemented; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3694 | } |
| 3695 | |
| 3696 | SLOT0(slot_nb_negative, "__neg__") |
| 3697 | SLOT0(slot_nb_positive, "__pos__") |
| 3698 | SLOT0(slot_nb_absolute, "__abs__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3699 | |
| 3700 | static int |
| 3701 | slot_nb_nonzero(PyObject *self) |
| 3702 | { |
Guido van Rossum | 84b2bed | 2002-08-16 17:01:09 +0000 | [diff] [blame] | 3703 | PyObject *func, *res, *args; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3704 | static PyObject *nonzero_str, *len_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3705 | |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3706 | func = lookup_maybe(self, "__nonzero__", &nonzero_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3707 | if (func == NULL) { |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3708 | if (PyErr_Occurred()) |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3709 | return -1; |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3710 | func = lookup_maybe(self, "__len__", &len_str); |
| 3711 | if (func == NULL) { |
| 3712 | if (PyErr_Occurred()) |
| 3713 | return -1; |
| 3714 | else |
| 3715 | return 1; |
| 3716 | } |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3717 | } |
Guido van Rossum | 84b2bed | 2002-08-16 17:01:09 +0000 | [diff] [blame] | 3718 | args = res = PyTuple_New(0); |
| 3719 | if (args != NULL) { |
| 3720 | res = PyObject_Call(func, args, NULL); |
| 3721 | Py_DECREF(args); |
| 3722 | } |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3723 | Py_DECREF(func); |
| 3724 | if (res == NULL) |
| 3725 | return -1; |
| 3726 | return PyObject_IsTrue(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3729 | SLOT0(slot_nb_invert, "__invert__") |
| 3730 | SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") |
| 3731 | SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") |
| 3732 | SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") |
| 3733 | SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") |
| 3734 | SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 3735 | |
| 3736 | static int |
| 3737 | slot_nb_coerce(PyObject **a, PyObject **b) |
| 3738 | { |
| 3739 | static PyObject *coerce_str; |
| 3740 | PyObject *self = *a, *other = *b; |
| 3741 | |
| 3742 | if (self->ob_type->tp_as_number != NULL && |
| 3743 | self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) { |
| 3744 | PyObject *r; |
| 3745 | r = call_maybe( |
| 3746 | self, "__coerce__", &coerce_str, "(O)", other); |
| 3747 | if (r == NULL) |
| 3748 | return -1; |
| 3749 | if (r == Py_NotImplemented) { |
| 3750 | Py_DECREF(r); |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 3751 | } |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3752 | else { |
| 3753 | if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) { |
| 3754 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 3755 | "__coerce__ didn't return a 2-tuple"); |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3756 | Py_DECREF(r); |
| 3757 | return -1; |
| 3758 | } |
| 3759 | *a = PyTuple_GET_ITEM(r, 0); |
| 3760 | Py_INCREF(*a); |
| 3761 | *b = PyTuple_GET_ITEM(r, 1); |
| 3762 | Py_INCREF(*b); |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 3763 | Py_DECREF(r); |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 3764 | return 0; |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 3765 | } |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 3766 | } |
| 3767 | if (other->ob_type->tp_as_number != NULL && |
| 3768 | other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) { |
| 3769 | PyObject *r; |
| 3770 | r = call_maybe( |
| 3771 | other, "__coerce__", &coerce_str, "(O)", self); |
| 3772 | if (r == NULL) |
| 3773 | return -1; |
| 3774 | if (r == Py_NotImplemented) { |
| 3775 | Py_DECREF(r); |
| 3776 | return 1; |
| 3777 | } |
| 3778 | if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) { |
| 3779 | PyErr_SetString(PyExc_TypeError, |
| 3780 | "__coerce__ didn't return a 2-tuple"); |
| 3781 | Py_DECREF(r); |
| 3782 | return -1; |
| 3783 | } |
| 3784 | *a = PyTuple_GET_ITEM(r, 1); |
| 3785 | Py_INCREF(*a); |
| 3786 | *b = PyTuple_GET_ITEM(r, 0); |
| 3787 | Py_INCREF(*b); |
| 3788 | Py_DECREF(r); |
| 3789 | return 0; |
| 3790 | } |
| 3791 | return 1; |
| 3792 | } |
| 3793 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3794 | SLOT0(slot_nb_int, "__int__") |
| 3795 | SLOT0(slot_nb_long, "__long__") |
| 3796 | SLOT0(slot_nb_float, "__float__") |
| 3797 | SLOT0(slot_nb_oct, "__oct__") |
| 3798 | SLOT0(slot_nb_hex, "__hex__") |
| 3799 | SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O") |
| 3800 | SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O") |
| 3801 | SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O") |
| 3802 | SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O") |
| 3803 | SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O") |
Guido van Rossum | 6e5680f | 2002-10-15 01:01:53 +0000 | [diff] [blame] | 3804 | SLOT1(slot_nb_inplace_power, "__ipow__", PyObject *, "O") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3805 | SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O") |
| 3806 | SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O") |
| 3807 | SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O") |
| 3808 | SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O") |
| 3809 | SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O") |
| 3810 | SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, |
| 3811 | "__floordiv__", "__rfloordiv__") |
| 3812 | SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__") |
| 3813 | SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O") |
| 3814 | SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3815 | |
| 3816 | static int |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3817 | half_compare(PyObject *self, PyObject *other) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3818 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3819 | PyObject *func, *args, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3820 | static PyObject *cmp_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3821 | int c; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3822 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3823 | func = lookup_method(self, "__cmp__", &cmp_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3824 | if (func == NULL) { |
| 3825 | PyErr_Clear(); |
| 3826 | } |
| 3827 | else { |
| 3828 | args = Py_BuildValue("(O)", other); |
| 3829 | if (args == NULL) |
| 3830 | res = NULL; |
| 3831 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3832 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3833 | Py_DECREF(args); |
| 3834 | } |
Raymond Hettinger | ab5dae3 | 2002-06-24 13:08:16 +0000 | [diff] [blame] | 3835 | Py_DECREF(func); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3836 | if (res != Py_NotImplemented) { |
| 3837 | if (res == NULL) |
| 3838 | return -2; |
| 3839 | c = PyInt_AsLong(res); |
| 3840 | Py_DECREF(res); |
| 3841 | if (c == -1 && PyErr_Occurred()) |
| 3842 | return -2; |
| 3843 | return (c < 0) ? -1 : (c > 0) ? 1 : 0; |
| 3844 | } |
| 3845 | Py_DECREF(res); |
| 3846 | } |
| 3847 | return 2; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3848 | } |
| 3849 | |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 3850 | /* This slot is published for the benefit of try_3way_compare in object.c */ |
| 3851 | int |
| 3852 | _PyObject_SlotCompare(PyObject *self, PyObject *other) |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3853 | { |
| 3854 | int c; |
| 3855 | |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 3856 | if (self->ob_type->tp_compare == _PyObject_SlotCompare) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3857 | c = half_compare(self, other); |
| 3858 | if (c <= 1) |
| 3859 | return c; |
| 3860 | } |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 3861 | if (other->ob_type->tp_compare == _PyObject_SlotCompare) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3862 | c = half_compare(other, self); |
| 3863 | if (c < -1) |
| 3864 | return -2; |
| 3865 | if (c <= 1) |
| 3866 | return -c; |
| 3867 | } |
| 3868 | return (void *)self < (void *)other ? -1 : |
| 3869 | (void *)self > (void *)other ? 1 : 0; |
| 3870 | } |
| 3871 | |
| 3872 | static PyObject * |
| 3873 | slot_tp_repr(PyObject *self) |
| 3874 | { |
| 3875 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3876 | static PyObject *repr_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3877 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3878 | func = lookup_method(self, "__repr__", &repr_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3879 | if (func != NULL) { |
| 3880 | res = PyEval_CallObject(func, NULL); |
| 3881 | Py_DECREF(func); |
| 3882 | return res; |
| 3883 | } |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 3884 | PyErr_Clear(); |
| 3885 | return PyString_FromFormat("<%s object at %p>", |
| 3886 | self->ob_type->tp_name, self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3887 | } |
| 3888 | |
| 3889 | static PyObject * |
| 3890 | slot_tp_str(PyObject *self) |
| 3891 | { |
| 3892 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3893 | static PyObject *str_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3894 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3895 | func = lookup_method(self, "__str__", &str_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3896 | if (func != NULL) { |
| 3897 | res = PyEval_CallObject(func, NULL); |
| 3898 | Py_DECREF(func); |
| 3899 | return res; |
| 3900 | } |
| 3901 | else { |
| 3902 | PyErr_Clear(); |
| 3903 | return slot_tp_repr(self); |
| 3904 | } |
| 3905 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3906 | |
| 3907 | static long |
| 3908 | slot_tp_hash(PyObject *self) |
| 3909 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3910 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3911 | static PyObject *hash_str, *eq_str, *cmp_str; |
| 3912 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3913 | long h; |
| 3914 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3915 | func = lookup_method(self, "__hash__", &hash_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3916 | |
| 3917 | if (func != NULL) { |
| 3918 | res = PyEval_CallObject(func, NULL); |
| 3919 | Py_DECREF(func); |
| 3920 | if (res == NULL) |
| 3921 | return -1; |
| 3922 | h = PyInt_AsLong(res); |
| 3923 | } |
| 3924 | else { |
| 3925 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3926 | func = lookup_method(self, "__eq__", &eq_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3927 | if (func == NULL) { |
| 3928 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3929 | func = lookup_method(self, "__cmp__", &cmp_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3930 | } |
| 3931 | if (func != NULL) { |
| 3932 | Py_DECREF(func); |
| 3933 | PyErr_SetString(PyExc_TypeError, "unhashable type"); |
| 3934 | return -1; |
| 3935 | } |
| 3936 | PyErr_Clear(); |
| 3937 | h = _Py_HashPointer((void *)self); |
| 3938 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3939 | if (h == -1 && !PyErr_Occurred()) |
| 3940 | h = -2; |
| 3941 | return h; |
| 3942 | } |
| 3943 | |
| 3944 | static PyObject * |
| 3945 | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 3946 | { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3947 | static PyObject *call_str; |
| 3948 | PyObject *meth = lookup_method(self, "__call__", &call_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3949 | PyObject *res; |
| 3950 | |
| 3951 | if (meth == NULL) |
| 3952 | return NULL; |
| 3953 | res = PyObject_Call(meth, args, kwds); |
| 3954 | Py_DECREF(meth); |
| 3955 | return res; |
| 3956 | } |
| 3957 | |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 3958 | /* There are two slot dispatch functions for tp_getattro. |
| 3959 | |
| 3960 | - slot_tp_getattro() is used when __getattribute__ is overridden |
| 3961 | but no __getattr__ hook is present; |
| 3962 | |
| 3963 | - slot_tp_getattr_hook() is used when a __getattr__ hook is present. |
| 3964 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 3965 | The code in update_one_slot() always installs slot_tp_getattr_hook(); this |
| 3966 | detects the absence of __getattr__ and then installs the simpler slot if |
| 3967 | necessary. */ |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 3968 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3969 | static PyObject * |
| 3970 | slot_tp_getattro(PyObject *self, PyObject *name) |
| 3971 | { |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 3972 | static PyObject *getattribute_str = NULL; |
| 3973 | return call_method(self, "__getattribute__", &getattribute_str, |
| 3974 | "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 3977 | static PyObject * |
| 3978 | slot_tp_getattr_hook(PyObject *self, PyObject *name) |
| 3979 | { |
| 3980 | PyTypeObject *tp = self->ob_type; |
| 3981 | PyObject *getattr, *getattribute, *res; |
| 3982 | static PyObject *getattribute_str = NULL; |
| 3983 | static PyObject *getattr_str = NULL; |
| 3984 | |
| 3985 | if (getattr_str == NULL) { |
| 3986 | getattr_str = PyString_InternFromString("__getattr__"); |
| 3987 | if (getattr_str == NULL) |
| 3988 | return NULL; |
| 3989 | } |
| 3990 | if (getattribute_str == NULL) { |
| 3991 | getattribute_str = |
| 3992 | PyString_InternFromString("__getattribute__"); |
| 3993 | if (getattribute_str == NULL) |
| 3994 | return NULL; |
| 3995 | } |
| 3996 | getattr = _PyType_Lookup(tp, getattr_str); |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 3997 | if (getattr == NULL) { |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 3998 | /* No __getattr__ hook: use a simpler dispatcher */ |
| 3999 | tp->tp_getattro = slot_tp_getattro; |
| 4000 | return slot_tp_getattro(self, name); |
| 4001 | } |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 4002 | getattribute = _PyType_Lookup(tp, getattribute_str); |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 4003 | if (getattribute == NULL || |
| 4004 | (getattribute->ob_type == &PyWrapperDescr_Type && |
| 4005 | ((PyWrapperDescrObject *)getattribute)->d_wrapped == |
| 4006 | (void *)PyObject_GenericGetAttr)) |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 4007 | res = PyObject_GenericGetAttr(self, name); |
| 4008 | else |
| 4009 | res = PyObject_CallFunction(getattribute, "OO", self, name); |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 4010 | if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 4011 | PyErr_Clear(); |
| 4012 | res = PyObject_CallFunction(getattr, "OO", self, name); |
| 4013 | } |
| 4014 | return res; |
| 4015 | } |
| 4016 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4017 | static int |
| 4018 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) |
| 4019 | { |
| 4020 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4021 | static PyObject *delattr_str, *setattr_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4022 | |
| 4023 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4024 | res = call_method(self, "__delattr__", &delattr_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4025 | "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4026 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4027 | res = call_method(self, "__setattr__", &setattr_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4028 | "(OO)", name, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4029 | if (res == NULL) |
| 4030 | return -1; |
| 4031 | Py_DECREF(res); |
| 4032 | return 0; |
| 4033 | } |
| 4034 | |
| 4035 | /* Map rich comparison operators to their __xx__ namesakes */ |
| 4036 | static char *name_op[] = { |
| 4037 | "__lt__", |
| 4038 | "__le__", |
| 4039 | "__eq__", |
| 4040 | "__ne__", |
| 4041 | "__gt__", |
| 4042 | "__ge__", |
| 4043 | }; |
| 4044 | |
| 4045 | static PyObject * |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4046 | half_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4047 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4048 | PyObject *func, *args, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4049 | static PyObject *op_str[6]; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4050 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4051 | func = lookup_method(self, name_op[op], &op_str[op]); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4052 | if (func == NULL) { |
| 4053 | PyErr_Clear(); |
| 4054 | Py_INCREF(Py_NotImplemented); |
| 4055 | return Py_NotImplemented; |
| 4056 | } |
| 4057 | args = Py_BuildValue("(O)", other); |
| 4058 | if (args == NULL) |
| 4059 | res = NULL; |
| 4060 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4061 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4062 | Py_DECREF(args); |
| 4063 | } |
| 4064 | Py_DECREF(func); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4065 | return res; |
| 4066 | } |
| 4067 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4068 | /* Map rich comparison operators to their swapped version, e.g. LT --> GT */ |
| 4069 | static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE}; |
| 4070 | |
| 4071 | static PyObject * |
| 4072 | slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
| 4073 | { |
| 4074 | PyObject *res; |
| 4075 | |
| 4076 | if (self->ob_type->tp_richcompare == slot_tp_richcompare) { |
| 4077 | res = half_richcompare(self, other, op); |
| 4078 | if (res != Py_NotImplemented) |
| 4079 | return res; |
| 4080 | Py_DECREF(res); |
| 4081 | } |
| 4082 | if (other->ob_type->tp_richcompare == slot_tp_richcompare) { |
| 4083 | res = half_richcompare(other, self, swapped_op[op]); |
| 4084 | if (res != Py_NotImplemented) { |
| 4085 | return res; |
| 4086 | } |
| 4087 | Py_DECREF(res); |
| 4088 | } |
| 4089 | Py_INCREF(Py_NotImplemented); |
| 4090 | return Py_NotImplemented; |
| 4091 | } |
| 4092 | |
| 4093 | static PyObject * |
| 4094 | slot_tp_iter(PyObject *self) |
| 4095 | { |
| 4096 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4097 | static PyObject *iter_str, *getitem_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4098 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4099 | func = lookup_method(self, "__iter__", &iter_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4100 | if (func != NULL) { |
Guido van Rossum | 84b2bed | 2002-08-16 17:01:09 +0000 | [diff] [blame] | 4101 | PyObject *args; |
| 4102 | args = res = PyTuple_New(0); |
| 4103 | if (args != NULL) { |
| 4104 | res = PyObject_Call(func, args, NULL); |
| 4105 | Py_DECREF(args); |
| 4106 | } |
| 4107 | Py_DECREF(func); |
| 4108 | return res; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4109 | } |
| 4110 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4111 | func = lookup_method(self, "__getitem__", &getitem_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4112 | if (func == NULL) { |
Guido van Rossum | d464107 | 2002-04-03 02:13:37 +0000 | [diff] [blame] | 4113 | PyErr_SetString(PyExc_TypeError, |
| 4114 | "iteration over non-sequence"); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4115 | return NULL; |
| 4116 | } |
| 4117 | Py_DECREF(func); |
| 4118 | return PySeqIter_New(self); |
| 4119 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4120 | |
| 4121 | static PyObject * |
| 4122 | slot_tp_iternext(PyObject *self) |
| 4123 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4124 | static PyObject *next_str; |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4125 | return call_method(self, "next", &next_str, "()"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4126 | } |
| 4127 | |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 4128 | static PyObject * |
| 4129 | slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 4130 | { |
| 4131 | PyTypeObject *tp = self->ob_type; |
| 4132 | PyObject *get; |
| 4133 | static PyObject *get_str = NULL; |
| 4134 | |
| 4135 | if (get_str == NULL) { |
| 4136 | get_str = PyString_InternFromString("__get__"); |
| 4137 | if (get_str == NULL) |
| 4138 | return NULL; |
| 4139 | } |
| 4140 | get = _PyType_Lookup(tp, get_str); |
| 4141 | if (get == NULL) { |
| 4142 | /* Avoid further slowdowns */ |
| 4143 | if (tp->tp_descr_get == slot_tp_descr_get) |
| 4144 | tp->tp_descr_get = NULL; |
| 4145 | Py_INCREF(self); |
| 4146 | return self; |
| 4147 | } |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 4148 | if (obj == NULL) |
| 4149 | obj = Py_None; |
| 4150 | if (type == NULL) |
| 4151 | type = Py_None; |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 4152 | return PyObject_CallFunction(get, "OOO", self, obj, type); |
| 4153 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4154 | |
| 4155 | static int |
| 4156 | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) |
| 4157 | { |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 4158 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4159 | static PyObject *del_str, *set_str; |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 4160 | |
| 4161 | if (value == NULL) |
Guido van Rossum | 1d5b3f2 | 2001-12-03 00:08:33 +0000 | [diff] [blame] | 4162 | res = call_method(self, "__delete__", &del_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4163 | "(O)", target); |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 4164 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4165 | res = call_method(self, "__set__", &set_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4166 | "(OO)", target, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4167 | if (res == NULL) |
| 4168 | return -1; |
| 4169 | Py_DECREF(res); |
| 4170 | return 0; |
| 4171 | } |
| 4172 | |
| 4173 | static int |
| 4174 | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 4175 | { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4176 | static PyObject *init_str; |
| 4177 | PyObject *meth = lookup_method(self, "__init__", &init_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4178 | PyObject *res; |
| 4179 | |
| 4180 | if (meth == NULL) |
| 4181 | return -1; |
| 4182 | res = PyObject_Call(meth, args, kwds); |
| 4183 | Py_DECREF(meth); |
| 4184 | if (res == NULL) |
| 4185 | return -1; |
| 4186 | Py_DECREF(res); |
| 4187 | return 0; |
| 4188 | } |
| 4189 | |
| 4190 | static PyObject * |
| 4191 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 4192 | { |
Guido van Rossum | 7bed213 | 2002-08-08 21:57:53 +0000 | [diff] [blame] | 4193 | static PyObject *new_str; |
| 4194 | PyObject *func; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4195 | PyObject *newargs, *x; |
| 4196 | int i, n; |
| 4197 | |
Guido van Rossum | 7bed213 | 2002-08-08 21:57:53 +0000 | [diff] [blame] | 4198 | if (new_str == NULL) { |
| 4199 | new_str = PyString_InternFromString("__new__"); |
| 4200 | if (new_str == NULL) |
| 4201 | return NULL; |
| 4202 | } |
| 4203 | func = PyObject_GetAttr((PyObject *)type, new_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4204 | if (func == NULL) |
| 4205 | return NULL; |
| 4206 | assert(PyTuple_Check(args)); |
| 4207 | n = PyTuple_GET_SIZE(args); |
| 4208 | newargs = PyTuple_New(n+1); |
| 4209 | if (newargs == NULL) |
| 4210 | return NULL; |
| 4211 | Py_INCREF(type); |
| 4212 | PyTuple_SET_ITEM(newargs, 0, (PyObject *)type); |
| 4213 | for (i = 0; i < n; i++) { |
| 4214 | x = PyTuple_GET_ITEM(args, i); |
| 4215 | Py_INCREF(x); |
| 4216 | PyTuple_SET_ITEM(newargs, i+1, x); |
| 4217 | } |
| 4218 | x = PyObject_Call(func, newargs, kwds); |
Guido van Rossum | 25d1807 | 2001-10-01 15:55:28 +0000 | [diff] [blame] | 4219 | Py_DECREF(newargs); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4220 | Py_DECREF(func); |
| 4221 | return x; |
| 4222 | } |
| 4223 | |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 4224 | static void |
| 4225 | slot_tp_del(PyObject *self) |
| 4226 | { |
| 4227 | static PyObject *del_str = NULL; |
| 4228 | PyObject *del, *res; |
| 4229 | PyObject *error_type, *error_value, *error_traceback; |
| 4230 | |
| 4231 | /* Temporarily resurrect the object. */ |
| 4232 | assert(self->ob_refcnt == 0); |
| 4233 | self->ob_refcnt = 1; |
| 4234 | |
| 4235 | /* Save the current exception, if any. */ |
| 4236 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 4237 | |
| 4238 | /* Execute __del__ method, if any. */ |
| 4239 | del = lookup_maybe(self, "__del__", &del_str); |
| 4240 | if (del != NULL) { |
| 4241 | res = PyEval_CallObject(del, NULL); |
| 4242 | if (res == NULL) |
| 4243 | PyErr_WriteUnraisable(del); |
| 4244 | else |
| 4245 | Py_DECREF(res); |
| 4246 | Py_DECREF(del); |
| 4247 | } |
| 4248 | |
| 4249 | /* Restore the saved exception. */ |
| 4250 | PyErr_Restore(error_type, error_value, error_traceback); |
| 4251 | |
| 4252 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 4253 | * cause a recursive call. |
| 4254 | */ |
| 4255 | assert(self->ob_refcnt > 0); |
| 4256 | if (--self->ob_refcnt == 0) |
| 4257 | return; /* this is the normal path out */ |
| 4258 | |
| 4259 | /* __del__ resurrected it! Make it look like the original Py_DECREF |
| 4260 | * never happened. |
| 4261 | */ |
| 4262 | { |
| 4263 | int refcnt = self->ob_refcnt; |
| 4264 | _Py_NewReference(self); |
| 4265 | self->ob_refcnt = refcnt; |
| 4266 | } |
| 4267 | assert(!PyType_IS_GC(self->ob_type) || |
| 4268 | _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED); |
| 4269 | /* If Py_REF_DEBUG, the original decref dropped _Py_RefTotal, but |
| 4270 | * _Py_NewReference bumped it again, so that's a wash. |
| 4271 | * If Py_TRACE_REFS, _Py_NewReference re-added self to the object |
| 4272 | * chain, so no more to do there either. |
| 4273 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
| 4274 | * _Py_NewReference bumped tp_allocs: both of those need to be |
| 4275 | * undone. |
| 4276 | */ |
| 4277 | #ifdef COUNT_ALLOCS |
| 4278 | --self->ob_type->tp_frees; |
| 4279 | --self->ob_type->tp_allocs; |
| 4280 | #endif |
| 4281 | } |
| 4282 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4283 | |
| 4284 | /* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper |
| 4285 | functions. The offsets here are relative to the 'etype' structure, which |
| 4286 | incorporates the additional structures used for numbers, sequences and |
| 4287 | mappings. Note that multiple names may map to the same slot (e.g. __eq__, |
| 4288 | __ne__ etc. all map to tp_richcompare) and one name may map to multiple |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4289 | slots (e.g. __str__ affects tp_str as well as tp_repr). The table is |
| 4290 | terminated with an all-zero entry. (This table is further initialized and |
| 4291 | sorted in init_slotdefs() below.) */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4292 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4293 | typedef struct wrapperbase slotdef; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4294 | |
| 4295 | #undef TPSLOT |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4296 | #undef FLSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4297 | #undef ETSLOT |
| 4298 | #undef SQSLOT |
| 4299 | #undef MPSLOT |
| 4300 | #undef NBSLOT |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4301 | #undef UNSLOT |
| 4302 | #undef IBSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4303 | #undef BINSLOT |
| 4304 | #undef RBINSLOT |
| 4305 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4306 | #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Neal Norwitz | d47714a | 2002-08-13 19:01:38 +0000 | [diff] [blame] | 4307 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 4308 | PyDoc_STR(DOC)} |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4309 | #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ |
| 4310 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
Neal Norwitz | d47714a | 2002-08-13 19:01:38 +0000 | [diff] [blame] | 4311 | PyDoc_STR(DOC), FLAGS} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4312 | #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Neal Norwitz | d47714a | 2002-08-13 19:01:38 +0000 | [diff] [blame] | 4313 | {NAME, offsetof(etype, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 4314 | PyDoc_STR(DOC)} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4315 | #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 4316 | ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) |
| 4317 | #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 4318 | ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) |
| 4319 | #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 4320 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) |
| 4321 | #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 4322 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 4323 | "x." NAME "() <==> " DOC) |
| 4324 | #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 4325 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 4326 | "x." NAME "(y) <==> x" DOC "y") |
| 4327 | #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
| 4328 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 4329 | "x." NAME "(y) <==> x" DOC "y") |
| 4330 | #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
| 4331 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 4332 | "x." NAME "(y) <==> y" DOC "x") |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4333 | |
| 4334 | static slotdef slotdefs[] = { |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4335 | SQSLOT("__len__", sq_length, slot_sq_length, wrap_inquiry, |
| 4336 | "x.__len__() <==> len(x)"), |
| 4337 | SQSLOT("__add__", sq_concat, slot_sq_concat, wrap_binaryfunc, |
| 4338 | "x.__add__(y) <==> x+y"), |
| 4339 | SQSLOT("__mul__", sq_repeat, slot_sq_repeat, wrap_intargfunc, |
| 4340 | "x.__mul__(n) <==> x*n"), |
| 4341 | SQSLOT("__rmul__", sq_repeat, slot_sq_repeat, wrap_intargfunc, |
| 4342 | "x.__rmul__(n) <==> n*x"), |
| 4343 | SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, |
| 4344 | "x.__getitem__(y) <==> x[y]"), |
| 4345 | SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_intintargfunc, |
| 4346 | "x.__getslice__(i, j) <==> x[i:j]"), |
| 4347 | SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, |
| 4348 | "x.__setitem__(i, y) <==> x[i]=y"), |
| 4349 | SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, |
| 4350 | "x.__delitem__(y) <==> del x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4351 | SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4352 | wrap_intintobjargproc, |
| 4353 | "x.__setslice__(i, j, y) <==> x[i:j]=y"), |
| 4354 | SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice, |
| 4355 | "x.__delslice__(i, j) <==> del x[i:j]"), |
| 4356 | SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, |
| 4357 | "x.__contains__(y) <==> y in x"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4358 | SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4359 | wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4360 | SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4361 | wrap_intargfunc, "x.__imul__(y) <==> x*=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4362 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4363 | MPSLOT("__len__", mp_length, slot_mp_length, wrap_inquiry, |
| 4364 | "x.__len__() <==> len(x)"), |
Guido van Rossum | fd38f8e | 2001-10-09 20:17:57 +0000 | [diff] [blame] | 4365 | MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4366 | wrap_binaryfunc, |
| 4367 | "x.__getitem__(y) <==> x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4368 | MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4369 | wrap_objobjargproc, |
| 4370 | "x.__setitem__(i, y) <==> x[i]=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4371 | MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4372 | wrap_delitem, |
| 4373 | "x.__delitem__(y) <==> del x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4374 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4375 | BINSLOT("__add__", nb_add, slot_nb_add, |
| 4376 | "+"), |
| 4377 | RBINSLOT("__radd__", nb_add, slot_nb_add, |
| 4378 | "+"), |
| 4379 | BINSLOT("__sub__", nb_subtract, slot_nb_subtract, |
| 4380 | "-"), |
| 4381 | RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract, |
| 4382 | "-"), |
| 4383 | BINSLOT("__mul__", nb_multiply, slot_nb_multiply, |
| 4384 | "*"), |
| 4385 | RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply, |
| 4386 | "*"), |
| 4387 | BINSLOT("__div__", nb_divide, slot_nb_divide, |
| 4388 | "/"), |
| 4389 | RBINSLOT("__rdiv__", nb_divide, slot_nb_divide, |
| 4390 | "/"), |
| 4391 | BINSLOT("__mod__", nb_remainder, slot_nb_remainder, |
| 4392 | "%"), |
| 4393 | RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder, |
| 4394 | "%"), |
| 4395 | BINSLOT("__divmod__", nb_divmod, slot_nb_divmod, |
| 4396 | "divmod(x, y)"), |
| 4397 | RBINSLOT("__rdivmod__", nb_divmod, slot_nb_divmod, |
| 4398 | "divmod(y, x)"), |
| 4399 | NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, |
| 4400 | "x.__pow__(y[, z]) <==> pow(x, y[, z])"), |
| 4401 | NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, |
| 4402 | "y.__rpow__(x[, z]) <==> pow(x, y[, z])"), |
| 4403 | UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"), |
| 4404 | UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"), |
| 4405 | UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, |
| 4406 | "abs(x)"), |
Guido van Rossum | dfce3bf | 2002-03-10 14:11:16 +0000 | [diff] [blame] | 4407 | UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquiry, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4408 | "x != 0"), |
| 4409 | UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"), |
| 4410 | BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 4411 | RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 4412 | BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 4413 | RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 4414 | BINSLOT("__and__", nb_and, slot_nb_and, "&"), |
| 4415 | RBINSLOT("__rand__", nb_and, slot_nb_and, "&"), |
| 4416 | BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"), |
| 4417 | RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), |
| 4418 | BINSLOT("__or__", nb_or, slot_nb_or, "|"), |
| 4419 | RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), |
| 4420 | NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc, |
| 4421 | "x.__coerce__(y) <==> coerce(x, y)"), |
| 4422 | UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, |
| 4423 | "int(x)"), |
| 4424 | UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc, |
| 4425 | "long(x)"), |
| 4426 | UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, |
| 4427 | "float(x)"), |
| 4428 | UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc, |
| 4429 | "oct(x)"), |
| 4430 | UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc, |
| 4431 | "hex(x)"), |
| 4432 | IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, |
| 4433 | wrap_binaryfunc, "+"), |
| 4434 | IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, |
| 4435 | wrap_binaryfunc, "-"), |
| 4436 | IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply, |
| 4437 | wrap_binaryfunc, "*"), |
| 4438 | IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide, |
| 4439 | wrap_binaryfunc, "/"), |
| 4440 | IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder, |
| 4441 | wrap_binaryfunc, "%"), |
| 4442 | IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power, |
Guido van Rossum | 6e5680f | 2002-10-15 01:01:53 +0000 | [diff] [blame] | 4443 | wrap_binaryfunc, "**"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4444 | IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift, |
| 4445 | wrap_binaryfunc, "<<"), |
| 4446 | IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift, |
| 4447 | wrap_binaryfunc, ">>"), |
| 4448 | IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and, |
| 4449 | wrap_binaryfunc, "&"), |
| 4450 | IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor, |
| 4451 | wrap_binaryfunc, "^"), |
| 4452 | IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or, |
| 4453 | wrap_binaryfunc, "|"), |
| 4454 | BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 4455 | RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 4456 | BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 4457 | RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 4458 | IBSLOT("__ifloordiv__", nb_inplace_floor_divide, |
| 4459 | slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"), |
| 4460 | IBSLOT("__itruediv__", nb_inplace_true_divide, |
| 4461 | slot_nb_inplace_true_divide, wrap_binaryfunc, "/"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4462 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4463 | TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, |
| 4464 | "x.__str__() <==> str(x)"), |
Guido van Rossum | afe7a94 | 2001-10-29 14:33:44 +0000 | [diff] [blame] | 4465 | TPSLOT("__str__", tp_print, NULL, NULL, ""), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4466 | TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, |
| 4467 | "x.__repr__() <==> repr(x)"), |
Guido van Rossum | afe7a94 | 2001-10-29 14:33:44 +0000 | [diff] [blame] | 4468 | TPSLOT("__repr__", tp_print, NULL, NULL, ""), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4469 | TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc, |
| 4470 | "x.__cmp__(y) <==> cmp(x,y)"), |
| 4471 | TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, |
| 4472 | "x.__hash__() <==> hash(x)"), |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4473 | FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, |
| 4474 | "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS), |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 4475 | TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4476 | wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"), |
| 4477 | TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), |
| 4478 | TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), |
| 4479 | TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), |
| 4480 | TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, |
| 4481 | "x.__setattr__('name', value) <==> x.name = value"), |
| 4482 | TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), |
| 4483 | TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, |
| 4484 | "x.__delattr__('name') <==> del x.name"), |
| 4485 | TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), |
| 4486 | TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, |
| 4487 | "x.__lt__(y) <==> x<y"), |
| 4488 | TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, |
| 4489 | "x.__le__(y) <==> x<=y"), |
| 4490 | TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, |
| 4491 | "x.__eq__(y) <==> x==y"), |
| 4492 | TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, |
| 4493 | "x.__ne__(y) <==> x!=y"), |
| 4494 | TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, |
| 4495 | "x.__gt__(y) <==> x>y"), |
| 4496 | TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, |
| 4497 | "x.__ge__(y) <==> x>=y"), |
| 4498 | TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, |
| 4499 | "x.__iter__() <==> iter(x)"), |
| 4500 | TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next, |
| 4501 | "x.next() -> the next value, or raise StopIteration"), |
| 4502 | TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, |
| 4503 | "descr.__get__(obj[, type]) -> value"), |
| 4504 | TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, |
| 4505 | "descr.__set__(obj, value)"), |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4506 | TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set, |
| 4507 | wrap_descr_delete, "descr.__delete__(obj)"), |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4508 | FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4509 | "x.__init__(...) initializes x; " |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4510 | "see x.__class__.__doc__ for signature", |
| 4511 | PyWrapperFlag_KEYWORDS), |
| 4512 | TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""), |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 4513 | TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4514 | {NULL} |
| 4515 | }; |
| 4516 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4517 | /* Given a type pointer and an offset gotten from a slotdef entry, return a |
| 4518 | pointer to the actual slot. This is not quite the same as simply adding |
| 4519 | the offset to the type pointer, since it takes care to indirect through the |
| 4520 | proper indirection pointer (as_buffer, etc.); it returns NULL if the |
| 4521 | indirection pointer is NULL. */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4522 | static void ** |
| 4523 | slotptr(PyTypeObject *type, int offset) |
| 4524 | { |
| 4525 | char *ptr; |
| 4526 | |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 4527 | /* Note: this depends on the order of the members of etype! */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4528 | assert(offset >= 0); |
| 4529 | assert(offset < offsetof(etype, as_buffer)); |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 4530 | if (offset >= offsetof(etype, as_sequence)) { |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4531 | ptr = (void *)type->tp_as_sequence; |
| 4532 | offset -= offsetof(etype, as_sequence); |
| 4533 | } |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 4534 | else if (offset >= offsetof(etype, as_mapping)) { |
| 4535 | ptr = (void *)type->tp_as_mapping; |
| 4536 | offset -= offsetof(etype, as_mapping); |
| 4537 | } |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4538 | else if (offset >= offsetof(etype, as_number)) { |
| 4539 | ptr = (void *)type->tp_as_number; |
| 4540 | offset -= offsetof(etype, as_number); |
| 4541 | } |
| 4542 | else { |
| 4543 | ptr = (void *)type; |
| 4544 | } |
| 4545 | if (ptr != NULL) |
| 4546 | ptr += offset; |
| 4547 | return (void **)ptr; |
| 4548 | } |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4549 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4550 | /* Length of array of slotdef pointers used to store slots with the |
| 4551 | same __name__. There should be at most MAX_EQUIV-1 slotdef entries with |
| 4552 | the same __name__, for any __name__. Since that's a static property, it is |
| 4553 | appropriate to declare fixed-size arrays for this. */ |
| 4554 | #define MAX_EQUIV 10 |
| 4555 | |
| 4556 | /* Return a slot pointer for a given name, but ONLY if the attribute has |
| 4557 | exactly one slot function. The name must be an interned string. */ |
| 4558 | static void ** |
| 4559 | resolve_slotdups(PyTypeObject *type, PyObject *name) |
| 4560 | { |
| 4561 | /* XXX Maybe this could be optimized more -- but is it worth it? */ |
| 4562 | |
| 4563 | /* pname and ptrs act as a little cache */ |
| 4564 | static PyObject *pname; |
| 4565 | static slotdef *ptrs[MAX_EQUIV]; |
| 4566 | slotdef *p, **pp; |
| 4567 | void **res, **ptr; |
| 4568 | |
| 4569 | if (pname != name) { |
| 4570 | /* Collect all slotdefs that match name into ptrs. */ |
| 4571 | pname = name; |
| 4572 | pp = ptrs; |
| 4573 | for (p = slotdefs; p->name_strobj; p++) { |
| 4574 | if (p->name_strobj == name) |
| 4575 | *pp++ = p; |
| 4576 | } |
| 4577 | *pp = NULL; |
| 4578 | } |
| 4579 | |
| 4580 | /* Look in all matching slots of the type; if exactly one of these has |
| 4581 | a filled-in slot, return its value. Otherwise return NULL. */ |
| 4582 | res = NULL; |
| 4583 | for (pp = ptrs; *pp; pp++) { |
| 4584 | ptr = slotptr(type, (*pp)->offset); |
| 4585 | if (ptr == NULL || *ptr == NULL) |
| 4586 | continue; |
| 4587 | if (res != NULL) |
| 4588 | return NULL; |
| 4589 | res = ptr; |
| 4590 | } |
| 4591 | return res; |
| 4592 | } |
| 4593 | |
| 4594 | /* Common code for update_these_slots() and fixup_slot_dispatchers(). This |
| 4595 | does some incredibly complex thinking and then sticks something into the |
| 4596 | slot. (It sees if the adjacent slotdefs for the same slot have conflicting |
| 4597 | interests, and then stores a generic wrapper or a specific function into |
| 4598 | the slot.) Return a pointer to the next slotdef with a different offset, |
| 4599 | because that's convenient for fixup_slot_dispatchers(). */ |
| 4600 | static slotdef * |
| 4601 | update_one_slot(PyTypeObject *type, slotdef *p) |
| 4602 | { |
| 4603 | PyObject *descr; |
| 4604 | PyWrapperDescrObject *d; |
| 4605 | void *generic = NULL, *specific = NULL; |
| 4606 | int use_generic = 0; |
| 4607 | int offset = p->offset; |
| 4608 | void **ptr = slotptr(type, offset); |
| 4609 | |
| 4610 | if (ptr == NULL) { |
| 4611 | do { |
| 4612 | ++p; |
| 4613 | } while (p->offset == offset); |
| 4614 | return p; |
| 4615 | } |
| 4616 | do { |
| 4617 | descr = _PyType_Lookup(type, p->name_strobj); |
| 4618 | if (descr == NULL) |
| 4619 | continue; |
| 4620 | if (descr->ob_type == &PyWrapperDescr_Type) { |
| 4621 | void **tptr = resolve_slotdups(type, p->name_strobj); |
| 4622 | if (tptr == NULL || tptr == ptr) |
| 4623 | generic = p->function; |
| 4624 | d = (PyWrapperDescrObject *)descr; |
| 4625 | if (d->d_base->wrapper == p->wrapper && |
| 4626 | PyType_IsSubtype(type, d->d_type)) |
| 4627 | { |
| 4628 | if (specific == NULL || |
| 4629 | specific == d->d_wrapped) |
| 4630 | specific = d->d_wrapped; |
| 4631 | else |
| 4632 | use_generic = 1; |
| 4633 | } |
| 4634 | } |
Guido van Rossum | 721f62e | 2002-08-09 02:14:34 +0000 | [diff] [blame] | 4635 | else if (descr->ob_type == &PyCFunction_Type && |
| 4636 | PyCFunction_GET_FUNCTION(descr) == |
| 4637 | (PyCFunction)tp_new_wrapper && |
| 4638 | strcmp(p->name, "__new__") == 0) |
| 4639 | { |
| 4640 | /* The __new__ wrapper is not a wrapper descriptor, |
| 4641 | so must be special-cased differently. |
| 4642 | If we don't do this, creating an instance will |
| 4643 | always use slot_tp_new which will look up |
| 4644 | __new__ in the MRO which will call tp_new_wrapper |
| 4645 | which will look through the base classes looking |
| 4646 | for a static base and call its tp_new (usually |
| 4647 | PyType_GenericNew), after performing various |
| 4648 | sanity checks and constructing a new argument |
| 4649 | list. Cut all that nonsense short -- this speeds |
| 4650 | up instance creation tremendously. */ |
| 4651 | specific = type->tp_new; |
| 4652 | /* XXX I'm not 100% sure that there isn't a hole |
| 4653 | in this reasoning that requires additional |
| 4654 | sanity checks. I'll buy the first person to |
| 4655 | point out a bug in this reasoning a beer. */ |
| 4656 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4657 | else { |
| 4658 | use_generic = 1; |
| 4659 | generic = p->function; |
| 4660 | } |
| 4661 | } while ((++p)->offset == offset); |
| 4662 | if (specific && !use_generic) |
| 4663 | *ptr = specific; |
| 4664 | else |
| 4665 | *ptr = generic; |
| 4666 | return p; |
| 4667 | } |
| 4668 | |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 4669 | static int recurse_down_subclasses(PyTypeObject *type, slotdef **pp, |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 4670 | PyObject *name); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4671 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4672 | /* In the type, update the slots whose slotdefs are gathered in the pp0 array, |
| 4673 | and then do the same for all this type's subtypes. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4674 | static int |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4675 | update_these_slots(PyTypeObject *type, slotdef **pp0, PyObject *name) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4676 | { |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4677 | slotdef **pp; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4678 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4679 | for (pp = pp0; *pp; pp++) |
| 4680 | update_one_slot(type, *pp); |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4681 | return recurse_down_subclasses(type, pp0, name); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4682 | } |
| 4683 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4684 | /* Update the slots whose slotdefs are gathered in the pp array in all (direct |
| 4685 | or indirect) subclasses of type. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4686 | static int |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4687 | recurse_down_subclasses(PyTypeObject *type, slotdef **pp, PyObject *name) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4688 | { |
| 4689 | PyTypeObject *subclass; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4690 | PyObject *ref, *subclasses, *dict; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4691 | int i, n; |
| 4692 | |
| 4693 | subclasses = type->tp_subclasses; |
| 4694 | if (subclasses == NULL) |
| 4695 | return 0; |
| 4696 | assert(PyList_Check(subclasses)); |
| 4697 | n = PyList_GET_SIZE(subclasses); |
| 4698 | for (i = 0; i < n; i++) { |
| 4699 | ref = PyList_GET_ITEM(subclasses, i); |
| 4700 | assert(PyWeakref_CheckRef(ref)); |
| 4701 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
Guido van Rossum | 59e6c53 | 2002-06-14 02:27:07 +0000 | [diff] [blame] | 4702 | assert(subclass != NULL); |
| 4703 | if ((PyObject *)subclass == Py_None) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4704 | continue; |
| 4705 | assert(PyType_Check(subclass)); |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4706 | /* Avoid recursing down into unaffected classes */ |
| 4707 | dict = subclass->tp_dict; |
| 4708 | if (dict != NULL && PyDict_Check(dict) && |
| 4709 | PyDict_GetItem(dict, name) != NULL) |
| 4710 | continue; |
| 4711 | if (update_these_slots(subclass, pp, name) < 0) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4712 | return -1; |
| 4713 | } |
| 4714 | return 0; |
| 4715 | } |
| 4716 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4717 | /* Comparison function for qsort() to compare slotdefs by their offset, and |
| 4718 | for equal offset by their address (to force a stable sort). */ |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 4719 | static int |
| 4720 | slotdef_cmp(const void *aa, const void *bb) |
| 4721 | { |
| 4722 | const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb; |
| 4723 | int c = a->offset - b->offset; |
| 4724 | if (c != 0) |
| 4725 | return c; |
| 4726 | else |
| 4727 | return a - b; |
| 4728 | } |
| 4729 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4730 | /* Initialize the slotdefs table by adding interned string objects for the |
| 4731 | names and sorting the entries. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4732 | static void |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 4733 | init_slotdefs(void) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4734 | { |
| 4735 | slotdef *p; |
| 4736 | static int initialized = 0; |
| 4737 | |
| 4738 | if (initialized) |
| 4739 | return; |
| 4740 | for (p = slotdefs; p->name; p++) { |
| 4741 | p->name_strobj = PyString_InternFromString(p->name); |
| 4742 | if (!p->name_strobj) |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4743 | Py_FatalError("Out of memory interning slotdef names"); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4744 | } |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4745 | qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef), |
| 4746 | slotdef_cmp); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4747 | initialized = 1; |
| 4748 | } |
| 4749 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4750 | /* Update the slots after assignment to a class (type) attribute. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4751 | static int |
| 4752 | update_slot(PyTypeObject *type, PyObject *name) |
| 4753 | { |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4754 | slotdef *ptrs[MAX_EQUIV]; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 4755 | slotdef *p; |
| 4756 | slotdef **pp; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4757 | int offset; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4758 | |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4759 | init_slotdefs(); |
| 4760 | pp = ptrs; |
| 4761 | for (p = slotdefs; p->name; p++) { |
| 4762 | /* XXX assume name is interned! */ |
| 4763 | if (p->name_strobj == name) |
| 4764 | *pp++ = p; |
| 4765 | } |
| 4766 | *pp = NULL; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 4767 | for (pp = ptrs; *pp; pp++) { |
| 4768 | p = *pp; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4769 | offset = p->offset; |
| 4770 | while (p > slotdefs && (p-1)->offset == offset) |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 4771 | --p; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4772 | *pp = p; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4773 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4774 | if (ptrs[0] == NULL) |
| 4775 | return 0; /* Not an attribute that affects any slots */ |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 4776 | return update_these_slots(type, ptrs, name); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4777 | } |
| 4778 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4779 | /* Store the proper functions in the slot dispatches at class (type) |
| 4780 | definition time, based upon which operations the class overrides in its |
| 4781 | dict. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4782 | static void |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4783 | fixup_slot_dispatchers(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4784 | { |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4785 | slotdef *p; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4786 | |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 4787 | init_slotdefs(); |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4788 | for (p = slotdefs; p->name; ) |
| 4789 | p = update_one_slot(type, p); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4790 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4791 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4792 | static void |
| 4793 | update_all_slots(PyTypeObject* type) |
| 4794 | { |
| 4795 | slotdef *p; |
| 4796 | |
| 4797 | init_slotdefs(); |
| 4798 | for (p = slotdefs; p->name; p++) { |
| 4799 | /* update_slot returns int but can't actually fail */ |
| 4800 | update_slot(type, p->name_strobj); |
| 4801 | } |
| 4802 | } |
| 4803 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4804 | /* This function is called by PyType_Ready() to populate the type's |
| 4805 | dictionary with method descriptors for function slots. For each |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 4806 | function slot (like tp_repr) that's defined in the type, one or more |
| 4807 | corresponding descriptors are added in the type's tp_dict dictionary |
| 4808 | under the appropriate name (like __repr__). Some function slots |
| 4809 | cause more than one descriptor to be added (for example, the nb_add |
| 4810 | slot adds both __add__ and __radd__ descriptors) and some function |
| 4811 | slots compete for the same descriptor (for example both sq_item and |
| 4812 | mp_subscript generate a __getitem__ descriptor). |
| 4813 | |
| 4814 | In the latter case, the first slotdef entry encoutered wins. Since |
| 4815 | slotdef entries are sorted by the offset of the slot in the etype |
| 4816 | struct, this gives us some control over disambiguating between |
| 4817 | competing slots: the members of struct etype are listed from most |
| 4818 | general to least general, so the most general slot is preferred. In |
| 4819 | particular, because as_mapping comes before as_sequence, for a type |
| 4820 | that defines both mp_subscript and sq_item, mp_subscript wins. |
| 4821 | |
| 4822 | This only adds new descriptors and doesn't overwrite entries in |
| 4823 | tp_dict that were previously defined. The descriptors contain a |
| 4824 | reference to the C function they must call, so that it's safe if they |
| 4825 | are copied into a subtype's __dict__ and the subtype has a different |
| 4826 | C function in its slot -- calling the method defined by the |
| 4827 | descriptor will call the C function that was used to create it, |
| 4828 | rather than the C function present in the slot when it is called. |
| 4829 | (This is important because a subtype may have a C function in the |
| 4830 | slot that calls the method from the dictionary, and we want to avoid |
| 4831 | infinite recursion here.) */ |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 4832 | |
| 4833 | static int |
| 4834 | add_operators(PyTypeObject *type) |
| 4835 | { |
| 4836 | PyObject *dict = type->tp_dict; |
| 4837 | slotdef *p; |
| 4838 | PyObject *descr; |
| 4839 | void **ptr; |
| 4840 | |
| 4841 | init_slotdefs(); |
| 4842 | for (p = slotdefs; p->name; p++) { |
| 4843 | if (p->wrapper == NULL) |
| 4844 | continue; |
| 4845 | ptr = slotptr(type, p->offset); |
| 4846 | if (!ptr || !*ptr) |
| 4847 | continue; |
| 4848 | if (PyDict_GetItem(dict, p->name_strobj)) |
| 4849 | continue; |
| 4850 | descr = PyDescr_NewWrapper(type, p, *ptr); |
| 4851 | if (descr == NULL) |
| 4852 | return -1; |
| 4853 | if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) |
| 4854 | return -1; |
| 4855 | Py_DECREF(descr); |
| 4856 | } |
| 4857 | if (type->tp_new != NULL) { |
| 4858 | if (add_tp_new_wrapper(type) < 0) |
| 4859 | return -1; |
| 4860 | } |
| 4861 | return 0; |
| 4862 | } |
| 4863 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4864 | |
| 4865 | /* Cooperative 'super' */ |
| 4866 | |
| 4867 | typedef struct { |
| 4868 | PyObject_HEAD |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 4869 | PyTypeObject *type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4870 | PyObject *obj; |
| 4871 | } superobject; |
| 4872 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 4873 | static PyMemberDef super_members[] = { |
| 4874 | {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY, |
| 4875 | "the class invoking super()"}, |
| 4876 | {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY, |
| 4877 | "the instance invoking super(); may be None"}, |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 4878 | {0} |
| 4879 | }; |
| 4880 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4881 | static void |
| 4882 | super_dealloc(PyObject *self) |
| 4883 | { |
| 4884 | superobject *su = (superobject *)self; |
| 4885 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 4886 | _PyObject_GC_UNTRACK(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4887 | Py_XDECREF(su->obj); |
| 4888 | Py_XDECREF(su->type); |
| 4889 | self->ob_type->tp_free(self); |
| 4890 | } |
| 4891 | |
| 4892 | static PyObject * |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 4893 | super_repr(PyObject *self) |
| 4894 | { |
| 4895 | superobject *su = (superobject *)self; |
| 4896 | |
| 4897 | if (su->obj) |
| 4898 | return PyString_FromFormat( |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 4899 | "<super: <class '%s'>, <%s object>>", |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 4900 | su->type ? su->type->tp_name : "NULL", |
| 4901 | su->obj->ob_type->tp_name); |
| 4902 | else |
| 4903 | return PyString_FromFormat( |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 4904 | "<super: <class '%s'>, NULL>", |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 4905 | su->type ? su->type->tp_name : "NULL"); |
| 4906 | } |
| 4907 | |
| 4908 | static PyObject * |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4909 | super_getattro(PyObject *self, PyObject *name) |
| 4910 | { |
| 4911 | superobject *su = (superobject *)self; |
| 4912 | |
| 4913 | if (su->obj != NULL) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 4914 | PyObject *mro, *res, *tmp, *dict; |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 4915 | PyTypeObject *starttype; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4916 | descrgetfunc f; |
| 4917 | int i, n; |
| 4918 | |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 4919 | starttype = su->obj->ob_type; |
| 4920 | mro = starttype->tp_mro; |
| 4921 | |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 4922 | if (mro == NULL) |
| 4923 | n = 0; |
| 4924 | else { |
| 4925 | assert(PyTuple_Check(mro)); |
| 4926 | n = PyTuple_GET_SIZE(mro); |
| 4927 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4928 | for (i = 0; i < n; i++) { |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 4929 | if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4930 | break; |
| 4931 | } |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 4932 | if (i >= n && PyType_Check(su->obj)) { |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 4933 | starttype = (PyTypeObject *)(su->obj); |
| 4934 | mro = starttype->tp_mro; |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 4935 | if (mro == NULL) |
| 4936 | n = 0; |
| 4937 | else { |
| 4938 | assert(PyTuple_Check(mro)); |
| 4939 | n = PyTuple_GET_SIZE(mro); |
| 4940 | } |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 4941 | for (i = 0; i < n; i++) { |
| 4942 | if ((PyObject *)(su->type) == |
| 4943 | PyTuple_GET_ITEM(mro, i)) |
| 4944 | break; |
| 4945 | } |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 4946 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4947 | i++; |
| 4948 | res = NULL; |
| 4949 | for (; i < n; i++) { |
| 4950 | tmp = PyTuple_GET_ITEM(mro, i); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 4951 | if (PyType_Check(tmp)) |
| 4952 | dict = ((PyTypeObject *)tmp)->tp_dict; |
| 4953 | else if (PyClass_Check(tmp)) |
| 4954 | dict = ((PyClassObject *)tmp)->cl_dict; |
| 4955 | else |
| 4956 | continue; |
| 4957 | res = PyDict_GetItem(dict, name); |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 4958 | if (res != NULL && !PyDescr_IsData(res)) { |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4959 | Py_INCREF(res); |
| 4960 | f = res->ob_type->tp_descr_get; |
| 4961 | if (f != NULL) { |
Guido van Rossum | d464107 | 2002-04-03 02:13:37 +0000 | [diff] [blame] | 4962 | tmp = f(res, su->obj, |
| 4963 | (PyObject *)starttype); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4964 | Py_DECREF(res); |
| 4965 | res = tmp; |
| 4966 | } |
| 4967 | return res; |
| 4968 | } |
| 4969 | } |
| 4970 | } |
| 4971 | return PyObject_GenericGetAttr(self, name); |
| 4972 | } |
| 4973 | |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 4974 | static int |
| 4975 | supercheck(PyTypeObject *type, PyObject *obj) |
| 4976 | { |
| 4977 | if (!PyType_IsSubtype(obj->ob_type, type) && |
| 4978 | !(PyType_Check(obj) && |
| 4979 | PyType_IsSubtype((PyTypeObject *)obj, type))) { |
| 4980 | PyErr_SetString(PyExc_TypeError, |
| 4981 | "super(type, obj): " |
| 4982 | "obj must be an instance or subtype of type"); |
| 4983 | return -1; |
| 4984 | } |
| 4985 | else |
| 4986 | return 0; |
| 4987 | } |
| 4988 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 4989 | static PyObject * |
| 4990 | super_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 4991 | { |
| 4992 | superobject *su = (superobject *)self; |
| 4993 | superobject *new; |
| 4994 | |
| 4995 | if (obj == NULL || obj == Py_None || su->obj != NULL) { |
| 4996 | /* Not binding to an object, or already bound */ |
| 4997 | Py_INCREF(self); |
| 4998 | return self; |
| 4999 | } |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 5000 | if (su->ob_type != &PySuper_Type) |
| 5001 | /* If su is an instance of a subclass of super, |
| 5002 | call its type */ |
| 5003 | return PyObject_CallFunction((PyObject *)su->ob_type, |
| 5004 | "OO", su->type, obj); |
| 5005 | else { |
| 5006 | /* Inline the common case */ |
| 5007 | if (supercheck(su->type, obj) < 0) |
| 5008 | return NULL; |
| 5009 | new = (superobject *)PySuper_Type.tp_new(&PySuper_Type, |
| 5010 | NULL, NULL); |
| 5011 | if (new == NULL) |
| 5012 | return NULL; |
| 5013 | Py_INCREF(su->type); |
| 5014 | Py_INCREF(obj); |
| 5015 | new->type = su->type; |
| 5016 | new->obj = obj; |
| 5017 | return (PyObject *)new; |
| 5018 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5019 | } |
| 5020 | |
| 5021 | static int |
| 5022 | super_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 5023 | { |
| 5024 | superobject *su = (superobject *)self; |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 5025 | PyTypeObject *type; |
| 5026 | PyObject *obj = NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5027 | |
| 5028 | if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj)) |
| 5029 | return -1; |
| 5030 | if (obj == Py_None) |
| 5031 | obj = NULL; |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 5032 | if (obj != NULL && supercheck(type, obj) < 0) |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5033 | return -1; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5034 | Py_INCREF(type); |
| 5035 | Py_XINCREF(obj); |
| 5036 | su->type = type; |
| 5037 | su->obj = obj; |
| 5038 | return 0; |
| 5039 | } |
| 5040 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5041 | PyDoc_STRVAR(super_doc, |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5042 | "super(type) -> unbound super object\n" |
| 5043 | "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 5044 | "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5045 | "Typical use to call a cooperative superclass method:\n" |
| 5046 | "class C(B):\n" |
| 5047 | " def meth(self, arg):\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5048 | " super(C, self).meth(arg)"); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5049 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 5050 | static int |
| 5051 | super_traverse(PyObject *self, visitproc visit, void *arg) |
| 5052 | { |
| 5053 | superobject *su = (superobject *)self; |
| 5054 | int err; |
| 5055 | |
| 5056 | #define VISIT(SLOT) \ |
| 5057 | if (SLOT) { \ |
| 5058 | err = visit((PyObject *)(SLOT), arg); \ |
| 5059 | if (err) \ |
| 5060 | return err; \ |
| 5061 | } |
| 5062 | |
| 5063 | VISIT(su->obj); |
| 5064 | VISIT(su->type); |
| 5065 | |
| 5066 | #undef VISIT |
| 5067 | |
| 5068 | return 0; |
| 5069 | } |
| 5070 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5071 | PyTypeObject PySuper_Type = { |
| 5072 | PyObject_HEAD_INIT(&PyType_Type) |
| 5073 | 0, /* ob_size */ |
| 5074 | "super", /* tp_name */ |
| 5075 | sizeof(superobject), /* tp_basicsize */ |
| 5076 | 0, /* tp_itemsize */ |
| 5077 | /* methods */ |
| 5078 | super_dealloc, /* tp_dealloc */ |
| 5079 | 0, /* tp_print */ |
| 5080 | 0, /* tp_getattr */ |
| 5081 | 0, /* tp_setattr */ |
| 5082 | 0, /* tp_compare */ |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 5083 | super_repr, /* tp_repr */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5084 | 0, /* tp_as_number */ |
| 5085 | 0, /* tp_as_sequence */ |
| 5086 | 0, /* tp_as_mapping */ |
| 5087 | 0, /* tp_hash */ |
| 5088 | 0, /* tp_call */ |
| 5089 | 0, /* tp_str */ |
| 5090 | super_getattro, /* tp_getattro */ |
| 5091 | 0, /* tp_setattro */ |
| 5092 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 5093 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 5094 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5095 | super_doc, /* tp_doc */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 5096 | super_traverse, /* tp_traverse */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5097 | 0, /* tp_clear */ |
| 5098 | 0, /* tp_richcompare */ |
| 5099 | 0, /* tp_weaklistoffset */ |
| 5100 | 0, /* tp_iter */ |
| 5101 | 0, /* tp_iternext */ |
| 5102 | 0, /* tp_methods */ |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 5103 | super_members, /* tp_members */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5104 | 0, /* tp_getset */ |
| 5105 | 0, /* tp_base */ |
| 5106 | 0, /* tp_dict */ |
| 5107 | super_descr_get, /* tp_descr_get */ |
| 5108 | 0, /* tp_descr_set */ |
| 5109 | 0, /* tp_dictoffset */ |
| 5110 | super_init, /* tp_init */ |
| 5111 | PyType_GenericAlloc, /* tp_alloc */ |
| 5112 | PyType_GenericNew, /* tp_new */ |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 5113 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5114 | }; |