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