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