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