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