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