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 | ade8c8b | 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 | ade8c8b | 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'", |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 60 | type->tp_name, Py_Type(value)->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 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 | ade8c8b | 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 | ade8c8b | 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 | ade8c8b | 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( |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 101 | type->tp_name, (Py_ssize_t)(s - type->tp_name)); |
Michael W. Hudson | ade8c8b | 2002-11-27 16:29:26 +0000 | [diff] [blame] | 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; |
Armin Rigo | c0ba52d | 2007-04-19 14:44:48 +0000 | [diff] [blame] | 190 | PyTypeObject *new_base, *old_base; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 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", |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 206 | type->tp_name, Py_Type(value)->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 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'", |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 221 | type->tp_name, Py_Type(ob)->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 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); |
Armin Rigo | 796fc99 | 2007-04-19 14:56:48 +0000 | [diff] [blame] | 268 | Py_INCREF(mro); |
| 269 | ob = cls->tp_mro; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 270 | cls->tp_mro = mro; |
Armin Rigo | 796fc99 | 2007-04-19 14:56:48 +0000 | [diff] [blame] | 271 | Py_DECREF(ob); |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 272 | } |
| 273 | Py_DECREF(temp); |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 274 | goto bail; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | Py_DECREF(temp); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 278 | |
| 279 | /* any base that was in __bases__ but now isn't, we |
Raymond Hettinger | a828586 | 2002-12-14 17:17:56 +0000 | [diff] [blame] | 280 | need to remove |type| from its tp_subclasses. |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 281 | conversely, any class now in __bases__ that wasn't |
Raymond Hettinger | a828586 | 2002-12-14 17:17:56 +0000 | [diff] [blame] | 282 | needs to have |type| added to its subclasses. */ |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 283 | |
| 284 | /* for now, sod that: just remove from all old_bases, |
| 285 | add to all new_bases */ |
| 286 | |
| 287 | for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) { |
| 288 | ob = PyTuple_GET_ITEM(old_bases, i); |
| 289 | if (PyType_Check(ob)) { |
| 290 | remove_subclass( |
| 291 | (PyTypeObject*)ob, type); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) { |
| 296 | ob = PyTuple_GET_ITEM(value, i); |
| 297 | if (PyType_Check(ob)) { |
| 298 | if (add_subclass((PyTypeObject*)ob, type) < 0) |
| 299 | r = -1; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | update_all_slots(type); |
| 304 | |
| 305 | Py_DECREF(old_bases); |
| 306 | Py_DECREF(old_base); |
| 307 | Py_DECREF(old_mro); |
| 308 | |
| 309 | return r; |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 310 | |
| 311 | bail: |
Michael W. Hudson | e723e45 | 2003-08-07 14:58:10 +0000 | [diff] [blame] | 312 | Py_DECREF(type->tp_bases); |
| 313 | Py_DECREF(type->tp_base); |
| 314 | if (type->tp_mro != old_mro) { |
| 315 | Py_DECREF(type->tp_mro); |
| 316 | } |
| 317 | |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 318 | type->tp_bases = old_bases; |
| 319 | type->tp_base = old_base; |
| 320 | type->tp_mro = old_mro; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 321 | |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 322 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 326 | type_dict(PyTypeObject *type, void *context) |
| 327 | { |
| 328 | if (type->tp_dict == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 329 | Py_INCREF(Py_None); |
| 330 | return Py_None; |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 331 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 332 | return PyDictProxy_New(type->tp_dict); |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 335 | static PyObject * |
| 336 | type_get_doc(PyTypeObject *type, void *context) |
| 337 | { |
| 338 | PyObject *result; |
Guido van Rossum | 6ca7d41 | 2002-04-18 00:22:00 +0000 | [diff] [blame] | 339 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 340 | return PyString_FromString(type->tp_doc); |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 341 | result = PyDict_GetItemString(type->tp_dict, "__doc__"); |
Guido van Rossum | 6ca7d41 | 2002-04-18 00:22:00 +0000 | [diff] [blame] | 342 | if (result == NULL) { |
| 343 | result = Py_None; |
| 344 | Py_INCREF(result); |
| 345 | } |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 346 | else if (Py_Type(result)->tp_descr_get) { |
| 347 | result = Py_Type(result)->tp_descr_get(result, NULL, |
Tim Peters | 2b85897 | 2002-04-18 04:12:28 +0000 | [diff] [blame] | 348 | (PyObject *)type); |
Guido van Rossum | 6ca7d41 | 2002-04-18 00:22:00 +0000 | [diff] [blame] | 349 | } |
| 350 | else { |
| 351 | Py_INCREF(result); |
| 352 | } |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 353 | return result; |
| 354 | } |
| 355 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 356 | static PyGetSetDef type_getsets[] = { |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 357 | {"__name__", (getter)type_name, (setter)type_set_name, NULL}, |
| 358 | {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL}, |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 359 | {"__module__", (getter)type_module, (setter)type_set_module, NULL}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 360 | {"__dict__", (getter)type_dict, NULL, NULL}, |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 361 | {"__doc__", (getter)type_get_doc, NULL, NULL}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 362 | {0} |
| 363 | }; |
| 364 | |
Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 365 | static int |
| 366 | type_compare(PyObject *v, PyObject *w) |
| 367 | { |
| 368 | /* This is called with type objects only. So we |
| 369 | can just compare the addresses. */ |
| 370 | Py_uintptr_t vv = (Py_uintptr_t)v; |
| 371 | Py_uintptr_t ww = (Py_uintptr_t)w; |
| 372 | return (vv < ww) ? -1 : (vv > ww) ? 1 : 0; |
| 373 | } |
| 374 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 375 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 376 | type_repr(PyTypeObject *type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 377 | { |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 378 | PyObject *mod, *name, *rtn; |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 379 | char *kind; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 380 | |
| 381 | mod = type_module(type, NULL); |
| 382 | if (mod == NULL) |
| 383 | PyErr_Clear(); |
| 384 | else if (!PyString_Check(mod)) { |
| 385 | Py_DECREF(mod); |
| 386 | mod = NULL; |
| 387 | } |
| 388 | name = type_name(type, NULL); |
| 389 | if (name == NULL) |
| 390 | return NULL; |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 391 | |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 392 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 393 | kind = "class"; |
| 394 | else |
| 395 | kind = "type"; |
| 396 | |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 397 | if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) { |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 398 | rtn = PyString_FromFormat("<%s '%s.%s'>", |
| 399 | kind, |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 400 | PyString_AS_STRING(mod), |
| 401 | PyString_AS_STRING(name)); |
| 402 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 403 | else |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 404 | rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 405 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 406 | Py_XDECREF(mod); |
| 407 | Py_DECREF(name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 408 | return rtn; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 411 | static PyObject * |
| 412 | type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 413 | { |
| 414 | PyObject *obj; |
| 415 | |
| 416 | if (type->tp_new == NULL) { |
| 417 | PyErr_Format(PyExc_TypeError, |
| 418 | "cannot create '%.100s' instances", |
| 419 | type->tp_name); |
| 420 | return NULL; |
| 421 | } |
| 422 | |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 423 | obj = type->tp_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 424 | if (obj != NULL) { |
Guido van Rossum | f76de62 | 2001-10-18 15:49:21 +0000 | [diff] [blame] | 425 | /* Ugly exception: when the call was type(something), |
| 426 | don't call tp_init on the result. */ |
| 427 | if (type == &PyType_Type && |
| 428 | PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && |
| 429 | (kwds == NULL || |
| 430 | (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) |
| 431 | return obj; |
Guido van Rossum | 8ace1ab | 2002-04-06 01:05:01 +0000 | [diff] [blame] | 432 | /* If the returned object is not an instance of type, |
| 433 | it won't be initialized. */ |
| 434 | if (!PyType_IsSubtype(obj->ob_type, type)) |
| 435 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 436 | type = obj->ob_type; |
Jeremy Hylton | 719841e | 2002-07-16 19:39:38 +0000 | [diff] [blame] | 437 | if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) && |
| 438 | type->tp_init != NULL && |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 439 | type->tp_init(obj, args, kwds) < 0) { |
| 440 | Py_DECREF(obj); |
| 441 | obj = NULL; |
| 442 | } |
| 443 | } |
| 444 | return obj; |
| 445 | } |
| 446 | |
| 447 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 448 | PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 449 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 450 | PyObject *obj; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 451 | const size_t size = _PyObject_VAR_SIZE(type, nitems+1); |
| 452 | /* note that we need to add one, for the sentinel */ |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 453 | |
| 454 | if (PyType_IS_GC(type)) |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 455 | obj = _PyObject_GC_Malloc(size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 456 | else |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 457 | obj = (PyObject *)PyObject_MALLOC(size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 458 | |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 459 | if (obj == NULL) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 460 | return PyErr_NoMemory(); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 461 | |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 462 | memset(obj, '\0', size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 463 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 464 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 465 | Py_INCREF(type); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 466 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 467 | if (type->tp_itemsize == 0) |
| 468 | PyObject_INIT(obj, type); |
| 469 | else |
| 470 | (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 471 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 472 | if (PyType_IS_GC(type)) |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 473 | _PyObject_GC_TRACK(obj); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 474 | return obj; |
| 475 | } |
| 476 | |
| 477 | PyObject * |
| 478 | PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 479 | { |
| 480 | return type->tp_alloc(type, 0); |
| 481 | } |
| 482 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 483 | /* Helpers for subtyping */ |
| 484 | |
| 485 | static int |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 486 | traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg) |
| 487 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 488 | Py_ssize_t i, n; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 489 | PyMemberDef *mp; |
| 490 | |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 491 | n = Py_Size(type); |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 492 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 493 | for (i = 0; i < n; i++, mp++) { |
| 494 | if (mp->type == T_OBJECT_EX) { |
| 495 | char *addr = (char *)self + mp->offset; |
| 496 | PyObject *obj = *(PyObject **)addr; |
| 497 | if (obj != NULL) { |
| 498 | int err = visit(obj, arg); |
| 499 | if (err) |
| 500 | return err; |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static int |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 508 | subtype_traverse(PyObject *self, visitproc visit, void *arg) |
| 509 | { |
| 510 | PyTypeObject *type, *base; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 511 | traverseproc basetraverse; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 512 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 513 | /* Find the nearest base with a different tp_traverse, |
| 514 | and traverse slots while we're at it */ |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 515 | type = Py_Type(self); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 516 | base = type; |
| 517 | while ((basetraverse = base->tp_traverse) == subtype_traverse) { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 518 | if (Py_Size(base)) { |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 519 | int err = traverse_slots(base, self, visit, arg); |
| 520 | if (err) |
| 521 | return err; |
| 522 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 523 | base = base->tp_base; |
| 524 | assert(base); |
| 525 | } |
| 526 | |
| 527 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 528 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 529 | if (dictptr && *dictptr) |
| 530 | Py_VISIT(*dictptr); |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 533 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 534 | /* For a heaptype, the instances count as references |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 535 | to the type. Traverse the type so the collector |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 536 | can find cycles involving this link. */ |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 537 | Py_VISIT(type); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 538 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 539 | if (basetraverse) |
| 540 | return basetraverse(self, visit, arg); |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | static void |
| 545 | clear_slots(PyTypeObject *type, PyObject *self) |
| 546 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 547 | Py_ssize_t i, n; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 548 | PyMemberDef *mp; |
| 549 | |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 550 | n = Py_Size(type); |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 551 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 552 | for (i = 0; i < n; i++, mp++) { |
| 553 | if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) { |
| 554 | char *addr = (char *)self + mp->offset; |
| 555 | PyObject *obj = *(PyObject **)addr; |
| 556 | if (obj != NULL) { |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 557 | *(PyObject **)addr = NULL; |
Thomas Wouters | edf17d8 | 2006-04-15 17:28:34 +0000 | [diff] [blame] | 558 | Py_DECREF(obj); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | static int |
| 565 | subtype_clear(PyObject *self) |
| 566 | { |
| 567 | PyTypeObject *type, *base; |
| 568 | inquiry baseclear; |
| 569 | |
| 570 | /* Find the nearest base with a different tp_clear |
| 571 | and clear slots while we're at it */ |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 572 | type = Py_Type(self); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 573 | base = type; |
| 574 | while ((baseclear = base->tp_clear) == subtype_clear) { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 575 | if (Py_Size(base)) |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 576 | clear_slots(base, self); |
| 577 | base = base->tp_base; |
| 578 | assert(base); |
| 579 | } |
| 580 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 581 | /* There's no need to clear the instance dict (if any); |
| 582 | the collector will call its tp_clear handler. */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 583 | |
| 584 | if (baseclear) |
| 585 | return baseclear(self); |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 586 | return 0; |
| 587 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 588 | |
| 589 | static void |
| 590 | subtype_dealloc(PyObject *self) |
| 591 | { |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 592 | PyTypeObject *type, *base; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 593 | destructor basedealloc; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 594 | |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 595 | /* Extract the type; we expect it to be a heap type */ |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 596 | type = Py_Type(self); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 597 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 598 | |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 599 | /* Test whether the type has GC exactly once */ |
| 600 | |
| 601 | if (!PyType_IS_GC(type)) { |
| 602 | /* It's really rare to find a dynamic type that doesn't have |
| 603 | GC; it can only happen when deriving from 'object' and not |
| 604 | adding any slots or instance variables. This allows |
| 605 | certain simplifications: there's no need to call |
| 606 | clear_slots(), or DECREF the dict, or clear weakrefs. */ |
| 607 | |
| 608 | /* Maybe call finalizer; exit early if resurrected */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 609 | if (type->tp_del) { |
| 610 | type->tp_del(self); |
| 611 | if (self->ob_refcnt > 0) |
| 612 | return; |
| 613 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 614 | |
| 615 | /* Find the nearest base with a different tp_dealloc */ |
| 616 | base = type; |
| 617 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 618 | assert(Py_Size(base) == 0); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 619 | base = base->tp_base; |
| 620 | assert(base); |
| 621 | } |
| 622 | |
| 623 | /* Call the base tp_dealloc() */ |
| 624 | assert(basedealloc); |
| 625 | basedealloc(self); |
| 626 | |
| 627 | /* Can't reference self beyond this point */ |
| 628 | Py_DECREF(type); |
| 629 | |
| 630 | /* Done */ |
| 631 | return; |
| 632 | } |
| 633 | |
| 634 | /* We get here only if the type has GC */ |
| 635 | |
| 636 | /* UnTrack and re-Track around the trashcan macro, alas */ |
Andrew M. Kuchling | c9172d3 | 2003-02-06 15:22:49 +0000 | [diff] [blame] | 637 | /* See explanation at end of function for full disclosure */ |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 638 | PyObject_GC_UnTrack(self); |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 639 | ++_PyTrash_delete_nesting; |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 640 | Py_TRASHCAN_SAFE_BEGIN(self); |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 641 | --_PyTrash_delete_nesting; |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 642 | /* DO NOT restore GC tracking at this point. weakref callbacks |
| 643 | * (if any, and whether directly here or indirectly in something we |
| 644 | * call) may trigger GC, and if self is tracked at that point, it |
| 645 | * 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] | 646 | */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 647 | |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 648 | /* Find the nearest base with a different tp_dealloc */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 649 | base = type; |
| 650 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 651 | base = base->tp_base; |
| 652 | assert(base); |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 655 | /* 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] | 656 | the finalizer (__del__), clearing slots, or clearing the instance |
| 657 | dict. */ |
| 658 | |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 659 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) |
| 660 | PyObject_ClearWeakRefs(self); |
| 661 | |
| 662 | /* Maybe call finalizer; exit early if resurrected */ |
| 663 | if (type->tp_del) { |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 664 | _PyObject_GC_TRACK(self); |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 665 | type->tp_del(self); |
| 666 | if (self->ob_refcnt > 0) |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 667 | goto endlabel; /* resurrected */ |
| 668 | else |
| 669 | _PyObject_GC_UNTRACK(self); |
Brett Cannon | f5bee30 | 2007-01-23 23:21:22 +0000 | [diff] [blame] | 670 | /* New weakrefs could be created during the finalizer call. |
| 671 | If this occurs, clear them out without calling their |
| 672 | finalizers since they might rely on part of the object |
| 673 | being finalized that has already been destroyed. */ |
| 674 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) { |
| 675 | /* Modeled after GET_WEAKREFS_LISTPTR() */ |
| 676 | PyWeakReference **list = (PyWeakReference **) \ |
| 677 | PyObject_GET_WEAKREFS_LISTPTR(self); |
| 678 | while (*list) |
| 679 | _PyWeakref_ClearRef(*list); |
| 680 | } |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 683 | /* Clear slots up to the nearest base with a different tp_dealloc */ |
| 684 | base = type; |
| 685 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 686 | if (Py_Size(base)) |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 687 | clear_slots(base, self); |
| 688 | base = base->tp_base; |
| 689 | assert(base); |
| 690 | } |
| 691 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 692 | /* If we added a dict, DECREF it */ |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 693 | if (type->tp_dictoffset && !base->tp_dictoffset) { |
| 694 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 695 | if (dictptr != NULL) { |
| 696 | PyObject *dict = *dictptr; |
| 697 | if (dict != NULL) { |
| 698 | Py_DECREF(dict); |
| 699 | *dictptr = NULL; |
| 700 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
Tim Peters | 0bd743c | 2003-11-13 22:50:00 +0000 | [diff] [blame] | 704 | /* Call the base tp_dealloc(); first retrack self if |
| 705 | * basedealloc knows about gc. |
| 706 | */ |
| 707 | if (PyType_IS_GC(base)) |
| 708 | _PyObject_GC_TRACK(self); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 709 | assert(basedealloc); |
| 710 | basedealloc(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 711 | |
| 712 | /* Can't reference self beyond this point */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 713 | Py_DECREF(type); |
| 714 | |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 715 | endlabel: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 716 | ++_PyTrash_delete_nesting; |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 717 | Py_TRASHCAN_SAFE_END(self); |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 718 | --_PyTrash_delete_nesting; |
| 719 | |
| 720 | /* Explanation of the weirdness around the trashcan macros: |
| 721 | |
| 722 | Q. What do the trashcan macros do? |
| 723 | |
| 724 | A. Read the comment titled "Trashcan mechanism" in object.h. |
| 725 | For one, this explains why there must be a call to GC-untrack |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 726 | before the trashcan begin macro. Without understanding the |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 727 | trashcan code, the answers to the following questions don't make |
| 728 | sense. |
| 729 | |
| 730 | Q. Why do we GC-untrack before the trashcan and then immediately |
| 731 | GC-track again afterward? |
| 732 | |
| 733 | A. In the case that the base class is GC-aware, the base class |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 734 | probably GC-untracks the object. If it does that using the |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 735 | UNTRACK macro, this will crash when the object is already |
| 736 | untracked. Because we don't know what the base class does, the |
| 737 | only safe thing is to make sure the object is tracked when we |
| 738 | call the base class dealloc. But... The trashcan begin macro |
| 739 | requires that the object is *untracked* before it is called. So |
| 740 | the dance becomes: |
| 741 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 742 | GC untrack |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 743 | trashcan begin |
| 744 | GC track |
| 745 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 746 | Q. Why did the last question say "immediately GC-track again"? |
| 747 | It's nowhere near immediately. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 748 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 749 | A. Because the code *used* to re-track immediately. Bad Idea. |
| 750 | self has a refcount of 0, and if gc ever gets its hands on it |
| 751 | (which can happen if any weakref callback gets invoked), it |
| 752 | looks like trash to gc too, and gc also tries to delete self |
| 753 | then. But we're already deleting self. Double dealloction is |
| 754 | a subtle disaster. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 755 | |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 756 | Q. Why the bizarre (net-zero) manipulation of |
| 757 | _PyTrash_delete_nesting around the trashcan macros? |
| 758 | |
| 759 | A. Some base classes (e.g. list) also use the trashcan mechanism. |
| 760 | The following scenario used to be possible: |
| 761 | |
| 762 | - suppose the trashcan level is one below the trashcan limit |
| 763 | |
| 764 | - subtype_dealloc() is called |
| 765 | |
| 766 | - the trashcan limit is not yet reached, so the trashcan level |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 767 | is incremented and the code between trashcan begin and end is |
| 768 | executed |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 769 | |
| 770 | - this destroys much of the object's contents, including its |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 771 | slots and __dict__ |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 772 | |
| 773 | - basedealloc() is called; this is really list_dealloc(), or |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 774 | some other type which also uses the trashcan macros |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 775 | |
| 776 | - the trashcan limit is now reached, so the object is put on the |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 777 | trashcan's to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 778 | |
| 779 | - basedealloc() returns |
| 780 | |
| 781 | - subtype_dealloc() decrefs the object's type |
| 782 | |
| 783 | - subtype_dealloc() returns |
| 784 | |
| 785 | - later, the trashcan code starts deleting the objects from its |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 786 | to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 787 | |
| 788 | - subtype_dealloc() is called *AGAIN* for the same object |
| 789 | |
| 790 | - at the very least (if the destroyed slots and __dict__ don't |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 791 | cause problems) the object's type gets decref'ed a second |
| 792 | time, which is *BAD*!!! |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 793 | |
| 794 | The remedy is to make sure that if the code between trashcan |
| 795 | begin and end in subtype_dealloc() is called, the code between |
| 796 | trashcan begin and end in basedealloc() will also be called. |
| 797 | This is done by decrementing the level after passing into the |
| 798 | trashcan block, and incrementing it just before leaving the |
| 799 | block. |
| 800 | |
| 801 | But now it's possible that a chain of objects consisting solely |
| 802 | of objects whose deallocator is subtype_dealloc() will defeat |
| 803 | the trashcan mechanism completely: the decremented level means |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 804 | that the effective level never reaches the limit. Therefore, we |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 805 | *increment* the level *before* entering the trashcan block, and |
| 806 | matchingly decrement it after leaving. This means the trashcan |
| 807 | code will trigger a little early, but that's no big deal. |
| 808 | |
| 809 | Q. Are there any live examples of code in need of all this |
| 810 | complexity? |
| 811 | |
| 812 | A. Yes. See SF bug 668433 for code that crashed (when Python was |
| 813 | compiled in debug mode) before the trashcan level manipulations |
| 814 | were added. For more discussion, see SF patches 581742, 575073 |
| 815 | and bug 574207. |
| 816 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 819 | static PyTypeObject *solid_base(PyTypeObject *type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 820 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 821 | /* type test with subclassing support */ |
| 822 | |
| 823 | int |
| 824 | PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
| 825 | { |
| 826 | PyObject *mro; |
| 827 | |
Guido van Rossum | 9478d07 | 2001-09-07 18:52:13 +0000 | [diff] [blame] | 828 | if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS)) |
| 829 | return b == a || b == &PyBaseObject_Type; |
| 830 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 831 | mro = a->tp_mro; |
| 832 | if (mro != NULL) { |
| 833 | /* Deal with multiple inheritance without recursion |
| 834 | by walking the MRO tuple */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 835 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 836 | assert(PyTuple_Check(mro)); |
| 837 | n = PyTuple_GET_SIZE(mro); |
| 838 | for (i = 0; i < n; i++) { |
| 839 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) |
| 840 | return 1; |
| 841 | } |
| 842 | return 0; |
| 843 | } |
| 844 | else { |
| 845 | /* a is not completely initilized yet; follow tp_base */ |
| 846 | do { |
| 847 | if (a == b) |
| 848 | return 1; |
| 849 | a = a->tp_base; |
| 850 | } while (a != NULL); |
| 851 | return b == &PyBaseObject_Type; |
| 852 | } |
| 853 | } |
| 854 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 855 | /* Internal routines to do a method lookup in the type |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 856 | without looking in the instance dictionary |
| 857 | (so we can't use PyObject_GetAttr) but still binding |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 858 | it to the instance. The arguments are the object, |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 859 | 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] | 860 | static variable used to cache the interned Python string. |
| 861 | |
| 862 | Two variants: |
| 863 | |
| 864 | - lookup_maybe() returns NULL without raising an exception |
| 865 | when the _PyType_Lookup() call fails; |
| 866 | |
| 867 | - lookup_method() always raises an exception upon errors. |
| 868 | */ |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 869 | |
| 870 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 871 | lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 872 | { |
| 873 | PyObject *res; |
| 874 | |
| 875 | if (*attrobj == NULL) { |
| 876 | *attrobj = PyString_InternFromString(attrstr); |
| 877 | if (*attrobj == NULL) |
| 878 | return NULL; |
| 879 | } |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 880 | res = _PyType_Lookup(Py_Type(self), *attrobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 881 | if (res != NULL) { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 882 | descrgetfunc f; |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 883 | if ((f = Py_Type(res)->tp_descr_get) == NULL) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 884 | Py_INCREF(res); |
| 885 | else |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 886 | res = f(res, self, (PyObject *)(Py_Type(self))); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 887 | } |
| 888 | return res; |
| 889 | } |
| 890 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 891 | static PyObject * |
| 892 | lookup_method(PyObject *self, char *attrstr, PyObject **attrobj) |
| 893 | { |
| 894 | PyObject *res = lookup_maybe(self, attrstr, attrobj); |
| 895 | if (res == NULL && !PyErr_Occurred()) |
| 896 | PyErr_SetObject(PyExc_AttributeError, *attrobj); |
| 897 | return res; |
| 898 | } |
| 899 | |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 900 | /* A variation of PyObject_CallMethod that uses lookup_method() |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 901 | instead of PyObject_GetAttrString(). This uses the same convention |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 902 | as lookup_method to cache the interned name string object. */ |
| 903 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 904 | static PyObject * |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 905 | call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 906 | { |
| 907 | va_list va; |
| 908 | PyObject *args, *func = 0, *retval; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 909 | va_start(va, format); |
| 910 | |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 911 | func = lookup_maybe(o, name, nameobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 912 | if (func == NULL) { |
| 913 | va_end(va); |
| 914 | if (!PyErr_Occurred()) |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 915 | PyErr_SetObject(PyExc_AttributeError, *nameobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 916 | return NULL; |
| 917 | } |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 918 | |
| 919 | if (format && *format) |
| 920 | args = Py_VaBuildValue(format, va); |
| 921 | else |
| 922 | args = PyTuple_New(0); |
| 923 | |
| 924 | va_end(va); |
| 925 | |
| 926 | if (args == NULL) |
| 927 | return NULL; |
| 928 | |
| 929 | assert(PyTuple_Check(args)); |
| 930 | retval = PyObject_Call(func, args, NULL); |
| 931 | |
| 932 | Py_DECREF(args); |
| 933 | Py_DECREF(func); |
| 934 | |
| 935 | return retval; |
| 936 | } |
| 937 | |
| 938 | /* Clone of call_method() that returns NotImplemented when the lookup fails. */ |
| 939 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 940 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 941 | call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 942 | { |
| 943 | va_list va; |
| 944 | PyObject *args, *func = 0, *retval; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 945 | va_start(va, format); |
| 946 | |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 947 | func = lookup_maybe(o, name, nameobj); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 948 | if (func == NULL) { |
| 949 | va_end(va); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 950 | if (!PyErr_Occurred()) { |
| 951 | Py_INCREF(Py_NotImplemented); |
| 952 | return Py_NotImplemented; |
| 953 | } |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 954 | return NULL; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | if (format && *format) |
| 958 | args = Py_VaBuildValue(format, va); |
| 959 | else |
| 960 | args = PyTuple_New(0); |
| 961 | |
| 962 | va_end(va); |
| 963 | |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 964 | if (args == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 965 | return NULL; |
| 966 | |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 967 | assert(PyTuple_Check(args)); |
| 968 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 969 | |
| 970 | Py_DECREF(args); |
| 971 | Py_DECREF(func); |
| 972 | |
| 973 | return retval; |
| 974 | } |
| 975 | |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 976 | static int |
| 977 | fill_classic_mro(PyObject *mro, PyObject *cls) |
| 978 | { |
| 979 | PyObject *bases, *base; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 980 | Py_ssize_t i, n; |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 981 | |
| 982 | assert(PyList_Check(mro)); |
| 983 | assert(PyClass_Check(cls)); |
| 984 | i = PySequence_Contains(mro, cls); |
| 985 | if (i < 0) |
| 986 | return -1; |
| 987 | if (!i) { |
| 988 | if (PyList_Append(mro, cls) < 0) |
| 989 | return -1; |
| 990 | } |
| 991 | bases = ((PyClassObject *)cls)->cl_bases; |
| 992 | assert(bases && PyTuple_Check(bases)); |
| 993 | n = PyTuple_GET_SIZE(bases); |
| 994 | for (i = 0; i < n; i++) { |
| 995 | base = PyTuple_GET_ITEM(bases, i); |
| 996 | if (fill_classic_mro(mro, base) < 0) |
| 997 | return -1; |
| 998 | } |
| 999 | return 0; |
| 1000 | } |
| 1001 | |
| 1002 | static PyObject * |
| 1003 | classic_mro(PyObject *cls) |
| 1004 | { |
| 1005 | PyObject *mro; |
| 1006 | |
| 1007 | assert(PyClass_Check(cls)); |
| 1008 | mro = PyList_New(0); |
| 1009 | if (mro != NULL) { |
| 1010 | if (fill_classic_mro(mro, cls) == 0) |
| 1011 | return mro; |
| 1012 | Py_DECREF(mro); |
| 1013 | } |
| 1014 | return NULL; |
| 1015 | } |
| 1016 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1017 | /* |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1018 | Method resolution order algorithm C3 described in |
| 1019 | "A Monotonic Superclass Linearization for Dylan", |
| 1020 | by Kim Barrett, Bob Cassel, Paul Haahr, |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1021 | David A. Moon, Keith Playford, and P. Tucker Withington. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1022 | (OOPSLA 1996) |
| 1023 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1024 | Some notes about the rules implied by C3: |
| 1025 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1026 | No duplicate bases. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1027 | It isn't legal to repeat a class in a list of base classes. |
| 1028 | |
| 1029 | The next three properties are the 3 constraints in "C3". |
| 1030 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1031 | Local precendece order. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1032 | If A precedes B in C's MRO, then A will precede B in the MRO of all |
| 1033 | subclasses of C. |
| 1034 | |
| 1035 | Monotonicity. |
| 1036 | The MRO of a class must be an extension without reordering of the |
| 1037 | MRO of each of its superclasses. |
| 1038 | |
| 1039 | Extended Precedence Graph (EPG). |
| 1040 | Linearization is consistent if there is a path in the EPG from |
| 1041 | each class to all its successors in the linearization. See |
| 1042 | the paper for definition of EPG. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1043 | */ |
| 1044 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1045 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1046 | tail_contains(PyObject *list, int whence, PyObject *o) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1047 | Py_ssize_t j, size; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1048 | size = PyList_GET_SIZE(list); |
| 1049 | |
| 1050 | for (j = whence+1; j < size; j++) { |
| 1051 | if (PyList_GET_ITEM(list, j) == o) |
| 1052 | return 1; |
| 1053 | } |
| 1054 | return 0; |
| 1055 | } |
| 1056 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1057 | static PyObject * |
| 1058 | class_name(PyObject *cls) |
| 1059 | { |
| 1060 | PyObject *name = PyObject_GetAttrString(cls, "__name__"); |
| 1061 | if (name == NULL) { |
| 1062 | PyErr_Clear(); |
| 1063 | Py_XDECREF(name); |
| 1064 | name = PyObject_Repr(cls); |
| 1065 | } |
| 1066 | if (name == NULL) |
| 1067 | return NULL; |
| 1068 | if (!PyString_Check(name)) { |
| 1069 | Py_DECREF(name); |
| 1070 | return NULL; |
| 1071 | } |
| 1072 | return name; |
| 1073 | } |
| 1074 | |
| 1075 | static int |
| 1076 | check_duplicates(PyObject *list) |
| 1077 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1078 | Py_ssize_t i, j, n; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1079 | /* Let's use a quadratic time algorithm, |
| 1080 | assuming that the bases lists is short. |
| 1081 | */ |
| 1082 | n = PyList_GET_SIZE(list); |
| 1083 | for (i = 0; i < n; i++) { |
| 1084 | PyObject *o = PyList_GET_ITEM(list, i); |
| 1085 | for (j = i + 1; j < n; j++) { |
| 1086 | if (PyList_GET_ITEM(list, j) == o) { |
| 1087 | o = class_name(o); |
| 1088 | PyErr_Format(PyExc_TypeError, |
| 1089 | "duplicate base class %s", |
| 1090 | o ? PyString_AS_STRING(o) : "?"); |
| 1091 | Py_XDECREF(o); |
| 1092 | return -1; |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
| 1099 | /* Raise a TypeError for an MRO order disagreement. |
| 1100 | |
| 1101 | It's hard to produce a good error message. In the absence of better |
| 1102 | insight into error reporting, report the classes that were candidates |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1103 | to be put next into the MRO. There is some conflict between the |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1104 | order in which they should be put in the MRO, but it's hard to |
| 1105 | diagnose what constraint can't be satisfied. |
| 1106 | */ |
| 1107 | |
| 1108 | static void |
| 1109 | set_mro_error(PyObject *to_merge, int *remain) |
| 1110 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1111 | Py_ssize_t i, n, off, to_merge_size; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1112 | char buf[1000]; |
| 1113 | PyObject *k, *v; |
| 1114 | PyObject *set = PyDict_New(); |
Georg Brandl | 5c170fd | 2006-03-17 19:03:25 +0000 | [diff] [blame] | 1115 | if (!set) return; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1116 | |
| 1117 | to_merge_size = PyList_GET_SIZE(to_merge); |
| 1118 | for (i = 0; i < to_merge_size; i++) { |
| 1119 | PyObject *L = PyList_GET_ITEM(to_merge, i); |
| 1120 | if (remain[i] < PyList_GET_SIZE(L)) { |
| 1121 | PyObject *c = PyList_GET_ITEM(L, remain[i]); |
Georg Brandl | 5c170fd | 2006-03-17 19:03:25 +0000 | [diff] [blame] | 1122 | if (PyDict_SetItem(set, c, Py_None) < 0) { |
| 1123 | Py_DECREF(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1124 | return; |
Georg Brandl | 5c170fd | 2006-03-17 19:03:25 +0000 | [diff] [blame] | 1125 | } |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1126 | } |
| 1127 | } |
| 1128 | n = PyDict_Size(set); |
| 1129 | |
Raymond Hettinger | f394df4 | 2003-04-06 19:13:41 +0000 | [diff] [blame] | 1130 | off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \ |
| 1131 | consistent method resolution\norder (MRO) for bases"); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1132 | i = 0; |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 1133 | 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] | 1134 | PyObject *name = class_name(k); |
| 1135 | off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", |
| 1136 | name ? PyString_AS_STRING(name) : "?"); |
| 1137 | Py_XDECREF(name); |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 1138 | if (--n && (size_t)(off+1) < sizeof(buf)) { |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1139 | buf[off++] = ','; |
| 1140 | buf[off] = '\0'; |
| 1141 | } |
| 1142 | } |
| 1143 | PyErr_SetString(PyExc_TypeError, buf); |
| 1144 | Py_DECREF(set); |
| 1145 | } |
| 1146 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1147 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1148 | pmerge(PyObject *acc, PyObject* to_merge) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1149 | Py_ssize_t i, j, to_merge_size, empty_cnt; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1150 | int *remain; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1151 | int ok; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1152 | |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1153 | to_merge_size = PyList_GET_SIZE(to_merge); |
| 1154 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1155 | /* remain stores an index into each sublist of to_merge. |
| 1156 | remain[i] is the index of the next base in to_merge[i] |
| 1157 | that is not included in acc. |
| 1158 | */ |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 1159 | remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1160 | if (remain == NULL) |
| 1161 | return -1; |
| 1162 | for (i = 0; i < to_merge_size; i++) |
| 1163 | remain[i] = 0; |
| 1164 | |
| 1165 | again: |
| 1166 | empty_cnt = 0; |
| 1167 | for (i = 0; i < to_merge_size; i++) { |
| 1168 | PyObject *candidate; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1169 | |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1170 | PyObject *cur_list = PyList_GET_ITEM(to_merge, i); |
| 1171 | |
| 1172 | if (remain[i] >= PyList_GET_SIZE(cur_list)) { |
| 1173 | empty_cnt++; |
| 1174 | continue; |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1175 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1176 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1177 | /* Choose next candidate for MRO. |
| 1178 | |
| 1179 | The input sequences alone can determine the choice. |
| 1180 | If not, choose the class which appears in the MRO |
| 1181 | of the earliest direct superclass of the new class. |
| 1182 | */ |
| 1183 | |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1184 | candidate = PyList_GET_ITEM(cur_list, remain[i]); |
| 1185 | for (j = 0; j < to_merge_size; j++) { |
| 1186 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1187 | if (tail_contains(j_lst, remain[j], candidate)) { |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1188 | goto skip; /* continue outer loop */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1189 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1190 | } |
| 1191 | ok = PyList_Append(acc, candidate); |
| 1192 | if (ok < 0) { |
| 1193 | PyMem_Free(remain); |
| 1194 | return -1; |
| 1195 | } |
| 1196 | for (j = 0; j < to_merge_size; j++) { |
| 1197 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
Guido van Rossum | 768158c | 2002-12-31 16:33:01 +0000 | [diff] [blame] | 1198 | if (remain[j] < PyList_GET_SIZE(j_lst) && |
| 1199 | PyList_GET_ITEM(j_lst, remain[j]) == candidate) { |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1200 | remain[j]++; |
| 1201 | } |
| 1202 | } |
| 1203 | goto again; |
Tim Peters | 9a6b8d8 | 2002-11-14 23:22:33 +0000 | [diff] [blame] | 1204 | skip: ; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1207 | if (empty_cnt == to_merge_size) { |
| 1208 | PyMem_FREE(remain); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1209 | return 0; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1210 | } |
| 1211 | set_mro_error(to_merge, remain); |
| 1212 | PyMem_FREE(remain); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1213 | return -1; |
| 1214 | } |
| 1215 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1216 | static PyObject * |
| 1217 | mro_implementation(PyTypeObject *type) |
| 1218 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1219 | Py_ssize_t i, n; |
| 1220 | int ok; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1221 | PyObject *bases, *result; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1222 | PyObject *to_merge, *bases_aslist; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1223 | |
Guido van Rossum | 6351757 | 2002-06-18 16:44:57 +0000 | [diff] [blame] | 1224 | if(type->tp_dict == NULL) { |
| 1225 | if(PyType_Ready(type) < 0) |
| 1226 | return NULL; |
| 1227 | } |
| 1228 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1229 | /* Find a superclass linearization that honors the constraints |
| 1230 | of the explicit lists of bases and the constraints implied by |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1231 | each base class. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1232 | |
| 1233 | to_merge is a list of lists, where each list is a superclass |
| 1234 | linearization implied by a base class. The last element of |
| 1235 | to_merge is the declared list of bases. |
| 1236 | */ |
| 1237 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1238 | bases = type->tp_bases; |
| 1239 | n = PyTuple_GET_SIZE(bases); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1240 | |
| 1241 | to_merge = PyList_New(n+1); |
| 1242 | if (to_merge == NULL) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1243 | return NULL; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1244 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1245 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1246 | PyObject *base = PyTuple_GET_ITEM(bases, i); |
| 1247 | PyObject *parentMRO; |
| 1248 | if (PyType_Check(base)) |
| 1249 | parentMRO = PySequence_List( |
| 1250 | ((PyTypeObject*)base)->tp_mro); |
| 1251 | else |
| 1252 | parentMRO = classic_mro(base); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1253 | if (parentMRO == NULL) { |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1254 | Py_DECREF(to_merge); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1255 | return NULL; |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1256 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1257 | |
| 1258 | PyList_SET_ITEM(to_merge, i, parentMRO); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1259 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1260 | |
| 1261 | bases_aslist = PySequence_List(bases); |
| 1262 | if (bases_aslist == NULL) { |
| 1263 | Py_DECREF(to_merge); |
| 1264 | return NULL; |
| 1265 | } |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1266 | /* This is just a basic sanity check. */ |
| 1267 | if (check_duplicates(bases_aslist) < 0) { |
| 1268 | Py_DECREF(to_merge); |
| 1269 | Py_DECREF(bases_aslist); |
| 1270 | return NULL; |
| 1271 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1272 | PyList_SET_ITEM(to_merge, n, bases_aslist); |
| 1273 | |
| 1274 | result = Py_BuildValue("[O]", (PyObject *)type); |
| 1275 | if (result == NULL) { |
| 1276 | Py_DECREF(to_merge); |
| 1277 | return NULL; |
| 1278 | } |
| 1279 | |
| 1280 | ok = pmerge(result, to_merge); |
| 1281 | Py_DECREF(to_merge); |
| 1282 | if (ok < 0) { |
| 1283 | Py_DECREF(result); |
| 1284 | return NULL; |
| 1285 | } |
| 1286 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1287 | return result; |
| 1288 | } |
| 1289 | |
| 1290 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1291 | mro_external(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1292 | { |
| 1293 | PyTypeObject *type = (PyTypeObject *)self; |
| 1294 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1295 | return mro_implementation(type); |
| 1296 | } |
| 1297 | |
| 1298 | static int |
| 1299 | mro_internal(PyTypeObject *type) |
| 1300 | { |
| 1301 | PyObject *mro, *result, *tuple; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1302 | int checkit = 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1303 | |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 1304 | if (Py_Type(type) == &PyType_Type) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1305 | result = mro_implementation(type); |
| 1306 | } |
| 1307 | else { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1308 | static PyObject *mro_str; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1309 | checkit = 1; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1310 | mro = lookup_method((PyObject *)type, "mro", &mro_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1311 | if (mro == NULL) |
| 1312 | return -1; |
| 1313 | result = PyObject_CallObject(mro, NULL); |
| 1314 | Py_DECREF(mro); |
| 1315 | } |
| 1316 | if (result == NULL) |
| 1317 | return -1; |
| 1318 | tuple = PySequence_Tuple(result); |
| 1319 | Py_DECREF(result); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1320 | if (tuple == NULL) |
| 1321 | return -1; |
| 1322 | if (checkit) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1323 | Py_ssize_t i, len; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1324 | PyObject *cls; |
| 1325 | PyTypeObject *solid; |
| 1326 | |
| 1327 | solid = solid_base(type); |
| 1328 | |
| 1329 | len = PyTuple_GET_SIZE(tuple); |
| 1330 | |
| 1331 | for (i = 0; i < len; i++) { |
| 1332 | PyTypeObject *t; |
| 1333 | cls = PyTuple_GET_ITEM(tuple, i); |
| 1334 | if (PyClass_Check(cls)) |
| 1335 | continue; |
| 1336 | else if (!PyType_Check(cls)) { |
| 1337 | PyErr_Format(PyExc_TypeError, |
| 1338 | "mro() returned a non-class ('%.500s')", |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 1339 | Py_Type(cls)->tp_name); |
Neal Norwitz | 50bf51a | 2006-01-02 02:46:54 +0000 | [diff] [blame] | 1340 | Py_DECREF(tuple); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1341 | return -1; |
| 1342 | } |
| 1343 | t = (PyTypeObject*)cls; |
| 1344 | if (!PyType_IsSubtype(solid, solid_base(t))) { |
| 1345 | PyErr_Format(PyExc_TypeError, |
| 1346 | "mro() returned base with unsuitable layout ('%.500s')", |
| 1347 | t->tp_name); |
Neal Norwitz | 50bf51a | 2006-01-02 02:46:54 +0000 | [diff] [blame] | 1348 | Py_DECREF(tuple); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1349 | return -1; |
| 1350 | } |
| 1351 | } |
| 1352 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1353 | type->tp_mro = tuple; |
| 1354 | return 0; |
| 1355 | } |
| 1356 | |
| 1357 | |
| 1358 | /* Calculate the best base amongst multiple base classes. |
Armin Rigo | c0ba52d | 2007-04-19 14:44:48 +0000 | [diff] [blame] | 1359 | This is the first one that's on the path to the "solid base". */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1360 | |
| 1361 | static PyTypeObject * |
| 1362 | best_base(PyObject *bases) |
| 1363 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1364 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1365 | PyTypeObject *base, *winner, *candidate, *base_i; |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1366 | PyObject *base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1367 | |
| 1368 | assert(PyTuple_Check(bases)); |
| 1369 | n = PyTuple_GET_SIZE(bases); |
| 1370 | assert(n > 0); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1371 | base = NULL; |
| 1372 | winner = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1373 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1374 | base_proto = PyTuple_GET_ITEM(bases, i); |
| 1375 | if (PyClass_Check(base_proto)) |
| 1376 | continue; |
Armin Rigo | c0ba52d | 2007-04-19 14:44:48 +0000 | [diff] [blame] | 1377 | if (!PyType_Check(base_proto)) { |
| 1378 | PyErr_SetString( |
| 1379 | PyExc_TypeError, |
| 1380 | "bases must be types"); |
| 1381 | return NULL; |
| 1382 | } |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1383 | base_i = (PyTypeObject *)base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1384 | if (base_i->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1385 | if (PyType_Ready(base_i) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1386 | return NULL; |
| 1387 | } |
| 1388 | candidate = solid_base(base_i); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1389 | if (winner == NULL) { |
| 1390 | winner = candidate; |
| 1391 | base = base_i; |
| 1392 | } |
| 1393 | else if (PyType_IsSubtype(winner, candidate)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1394 | ; |
| 1395 | else if (PyType_IsSubtype(candidate, winner)) { |
| 1396 | winner = candidate; |
| 1397 | base = base_i; |
| 1398 | } |
| 1399 | else { |
| 1400 | PyErr_SetString( |
| 1401 | PyExc_TypeError, |
| 1402 | "multiple bases have " |
| 1403 | "instance lay-out conflict"); |
| 1404 | return NULL; |
| 1405 | } |
| 1406 | } |
Guido van Rossum | e54616c | 2001-12-14 04:19:56 +0000 | [diff] [blame] | 1407 | if (base == NULL) |
| 1408 | PyErr_SetString(PyExc_TypeError, |
| 1409 | "a new-style class can't have only classic bases"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1410 | return base; |
| 1411 | } |
| 1412 | |
| 1413 | static int |
| 1414 | extra_ivars(PyTypeObject *type, PyTypeObject *base) |
| 1415 | { |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 1416 | size_t t_size = type->tp_basicsize; |
| 1417 | size_t b_size = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1418 | |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1419 | assert(t_size >= b_size); /* Else type smaller than base! */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1420 | if (type->tp_itemsize || base->tp_itemsize) { |
| 1421 | /* If itemsize is involved, stricter rules */ |
| 1422 | return t_size != b_size || |
| 1423 | type->tp_itemsize != base->tp_itemsize; |
| 1424 | } |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1425 | if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 && |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1426 | type->tp_weaklistoffset + sizeof(PyObject *) == t_size && |
| 1427 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1428 | t_size -= sizeof(PyObject *); |
| 1429 | if (type->tp_dictoffset && base->tp_dictoffset == 0 && |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1430 | type->tp_dictoffset + sizeof(PyObject *) == t_size && |
| 1431 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1432 | t_size -= sizeof(PyObject *); |
| 1433 | |
| 1434 | return t_size != b_size; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | static PyTypeObject * |
| 1438 | solid_base(PyTypeObject *type) |
| 1439 | { |
| 1440 | PyTypeObject *base; |
| 1441 | |
| 1442 | if (type->tp_base) |
| 1443 | base = solid_base(type->tp_base); |
| 1444 | else |
| 1445 | base = &PyBaseObject_Type; |
| 1446 | if (extra_ivars(type, base)) |
| 1447 | return type; |
| 1448 | else |
| 1449 | return base; |
| 1450 | } |
| 1451 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1452 | static void object_dealloc(PyObject *); |
| 1453 | static int object_init(PyObject *, PyObject *, PyObject *); |
| 1454 | static int update_slot(PyTypeObject *, PyObject *); |
| 1455 | static void fixup_slot_dispatchers(PyTypeObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1456 | |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1457 | /* |
| 1458 | * Helpers for __dict__ descriptor. We don't want to expose the dicts |
| 1459 | * inherited from various builtin types. The builtin base usually provides |
| 1460 | * its own __dict__ descriptor, so we use that when we can. |
| 1461 | */ |
| 1462 | static PyTypeObject * |
| 1463 | get_builtin_base_with_dict(PyTypeObject *type) |
| 1464 | { |
| 1465 | while (type->tp_base != NULL) { |
| 1466 | if (type->tp_dictoffset != 0 && |
| 1467 | !(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 1468 | return type; |
| 1469 | type = type->tp_base; |
| 1470 | } |
| 1471 | return NULL; |
| 1472 | } |
| 1473 | |
| 1474 | static PyObject * |
| 1475 | get_dict_descriptor(PyTypeObject *type) |
| 1476 | { |
| 1477 | static PyObject *dict_str; |
| 1478 | PyObject *descr; |
| 1479 | |
| 1480 | if (dict_str == NULL) { |
| 1481 | dict_str = PyString_InternFromString("__dict__"); |
| 1482 | if (dict_str == NULL) |
| 1483 | return NULL; |
| 1484 | } |
| 1485 | descr = _PyType_Lookup(type, dict_str); |
| 1486 | if (descr == NULL || !PyDescr_IsData(descr)) |
| 1487 | return NULL; |
| 1488 | |
| 1489 | return descr; |
| 1490 | } |
| 1491 | |
| 1492 | static void |
| 1493 | raise_dict_descr_error(PyObject *obj) |
| 1494 | { |
| 1495 | PyErr_Format(PyExc_TypeError, |
| 1496 | "this __dict__ descriptor does not support " |
| 1497 | "'%.200s' objects", obj->ob_type->tp_name); |
| 1498 | } |
| 1499 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1500 | static PyObject * |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1501 | subtype_dict(PyObject *obj, void *context) |
| 1502 | { |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1503 | PyObject **dictptr; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1504 | PyObject *dict; |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1505 | PyTypeObject *base; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1506 | |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1507 | base = get_builtin_base_with_dict(obj->ob_type); |
| 1508 | if (base != NULL) { |
| 1509 | descrgetfunc func; |
| 1510 | PyObject *descr = get_dict_descriptor(base); |
| 1511 | if (descr == NULL) { |
| 1512 | raise_dict_descr_error(obj); |
| 1513 | return NULL; |
| 1514 | } |
| 1515 | func = descr->ob_type->tp_descr_get; |
| 1516 | if (func == NULL) { |
| 1517 | raise_dict_descr_error(obj); |
| 1518 | return NULL; |
| 1519 | } |
| 1520 | return func(descr, obj, (PyObject *)(obj->ob_type)); |
| 1521 | } |
| 1522 | |
| 1523 | dictptr = _PyObject_GetDictPtr(obj); |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1524 | if (dictptr == NULL) { |
| 1525 | PyErr_SetString(PyExc_AttributeError, |
| 1526 | "This object has no __dict__"); |
| 1527 | return NULL; |
| 1528 | } |
| 1529 | dict = *dictptr; |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 1530 | if (dict == NULL) |
| 1531 | *dictptr = dict = PyDict_New(); |
| 1532 | Py_XINCREF(dict); |
| 1533 | return dict; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1536 | static int |
| 1537 | subtype_setdict(PyObject *obj, PyObject *value, void *context) |
| 1538 | { |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1539 | PyObject **dictptr; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1540 | PyObject *dict; |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1541 | PyTypeObject *base; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1542 | |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1543 | base = get_builtin_base_with_dict(obj->ob_type); |
| 1544 | if (base != NULL) { |
| 1545 | descrsetfunc func; |
| 1546 | PyObject *descr = get_dict_descriptor(base); |
| 1547 | if (descr == NULL) { |
| 1548 | raise_dict_descr_error(obj); |
| 1549 | return -1; |
| 1550 | } |
| 1551 | func = descr->ob_type->tp_descr_set; |
| 1552 | if (func == NULL) { |
| 1553 | raise_dict_descr_error(obj); |
| 1554 | return -1; |
| 1555 | } |
| 1556 | return func(descr, obj, value); |
| 1557 | } |
| 1558 | |
| 1559 | dictptr = _PyObject_GetDictPtr(obj); |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1560 | if (dictptr == NULL) { |
| 1561 | PyErr_SetString(PyExc_AttributeError, |
| 1562 | "This object has no __dict__"); |
| 1563 | return -1; |
| 1564 | } |
Guido van Rossum | d331cb5 | 2001-12-05 19:46:42 +0000 | [diff] [blame] | 1565 | if (value != NULL && !PyDict_Check(value)) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 1566 | PyErr_Format(PyExc_TypeError, |
| 1567 | "__dict__ must be set to a dictionary, " |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 1568 | "not a '%.200s'", Py_Type(value)->tp_name); |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1569 | return -1; |
| 1570 | } |
| 1571 | dict = *dictptr; |
Guido van Rossum | d331cb5 | 2001-12-05 19:46:42 +0000 | [diff] [blame] | 1572 | Py_XINCREF(value); |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1573 | *dictptr = value; |
| 1574 | Py_XDECREF(dict); |
| 1575 | return 0; |
| 1576 | } |
| 1577 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1578 | static PyObject * |
| 1579 | subtype_getweakref(PyObject *obj, void *context) |
| 1580 | { |
| 1581 | PyObject **weaklistptr; |
| 1582 | PyObject *result; |
| 1583 | |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 1584 | if (Py_Type(obj)->tp_weaklistoffset == 0) { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1585 | PyErr_SetString(PyExc_AttributeError, |
Fred Drake | 7a36f5f | 2006-08-04 05:17:21 +0000 | [diff] [blame] | 1586 | "This object has no __weakref__"); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1587 | return NULL; |
| 1588 | } |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 1589 | assert(Py_Type(obj)->tp_weaklistoffset > 0); |
| 1590 | assert(Py_Type(obj)->tp_weaklistoffset + sizeof(PyObject *) <= |
| 1591 | (size_t)(Py_Type(obj)->tp_basicsize)); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1592 | weaklistptr = (PyObject **) |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 1593 | ((char *)obj + Py_Type(obj)->tp_weaklistoffset); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1594 | if (*weaklistptr == NULL) |
| 1595 | result = Py_None; |
| 1596 | else |
| 1597 | result = *weaklistptr; |
| 1598 | Py_INCREF(result); |
| 1599 | return result; |
| 1600 | } |
| 1601 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1602 | /* Three variants on the subtype_getsets list. */ |
| 1603 | |
| 1604 | static PyGetSetDef subtype_getsets_full[] = { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1605 | {"__dict__", subtype_dict, subtype_setdict, |
Neal Norwitz | 858e34f | 2002-08-13 17:18:45 +0000 | [diff] [blame] | 1606 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1607 | {"__weakref__", subtype_getweakref, NULL, |
Neal Norwitz | 858e34f | 2002-08-13 17:18:45 +0000 | [diff] [blame] | 1608 | PyDoc_STR("list of weak references to the object (if defined)")}, |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1609 | {0} |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1610 | }; |
| 1611 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1612 | static PyGetSetDef subtype_getsets_dict_only[] = { |
| 1613 | {"__dict__", subtype_dict, subtype_setdict, |
| 1614 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 1615 | {0} |
| 1616 | }; |
| 1617 | |
| 1618 | static PyGetSetDef subtype_getsets_weakref_only[] = { |
| 1619 | {"__weakref__", subtype_getweakref, NULL, |
| 1620 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 1621 | {0} |
| 1622 | }; |
| 1623 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1624 | static int |
| 1625 | valid_identifier(PyObject *s) |
| 1626 | { |
Guido van Rossum | 03013a0 | 2002-07-16 14:30:28 +0000 | [diff] [blame] | 1627 | unsigned char *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1628 | Py_ssize_t i, n; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1629 | |
| 1630 | if (!PyString_Check(s)) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 1631 | PyErr_Format(PyExc_TypeError, |
| 1632 | "__slots__ items must be strings, not '%.200s'", |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 1633 | Py_Type(s)->tp_name); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1634 | return 0; |
| 1635 | } |
Guido van Rossum | 03013a0 | 2002-07-16 14:30:28 +0000 | [diff] [blame] | 1636 | p = (unsigned char *) PyString_AS_STRING(s); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1637 | n = PyString_GET_SIZE(s); |
| 1638 | /* We must reject an empty name. As a hack, we bump the |
| 1639 | length to 1 so that the loop will balk on the trailing \0. */ |
| 1640 | if (n == 0) |
| 1641 | n = 1; |
| 1642 | for (i = 0; i < n; i++, p++) { |
| 1643 | if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') { |
| 1644 | PyErr_SetString(PyExc_TypeError, |
| 1645 | "__slots__ must be identifiers"); |
| 1646 | return 0; |
| 1647 | } |
| 1648 | } |
| 1649 | return 1; |
| 1650 | } |
| 1651 | |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1652 | #ifdef Py_USING_UNICODE |
| 1653 | /* Replace Unicode objects in slots. */ |
| 1654 | |
| 1655 | static PyObject * |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 1656 | _unicode_to_string(PyObject *slots, Py_ssize_t nslots) |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1657 | { |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1658 | PyObject *tmp = NULL; |
| 1659 | PyObject *slot_name, *new_name; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 1660 | Py_ssize_t i; |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1661 | |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1662 | for (i = 0; i < nslots; i++) { |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1663 | if (PyUnicode_Check(slot_name = PyTuple_GET_ITEM(slots, i))) { |
| 1664 | if (tmp == NULL) { |
| 1665 | tmp = PySequence_List(slots); |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1666 | if (tmp == NULL) |
| 1667 | return NULL; |
| 1668 | } |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1669 | new_name = _PyUnicode_AsDefaultEncodedString(slot_name, |
| 1670 | NULL); |
| 1671 | if (new_name == NULL) { |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1672 | Py_DECREF(tmp); |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1673 | return NULL; |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1674 | } |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1675 | Py_INCREF(new_name); |
| 1676 | PyList_SET_ITEM(tmp, i, new_name); |
| 1677 | Py_DECREF(slot_name); |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1678 | } |
| 1679 | } |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1680 | if (tmp != NULL) { |
| 1681 | slots = PyList_AsTuple(tmp); |
| 1682 | Py_DECREF(tmp); |
| 1683 | } |
| 1684 | return slots; |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1685 | } |
| 1686 | #endif |
| 1687 | |
Guido van Rossum | f102e24 | 2007-03-23 18:53:03 +0000 | [diff] [blame] | 1688 | /* Forward */ |
| 1689 | static int |
| 1690 | object_init(PyObject *self, PyObject *args, PyObject *kwds); |
| 1691 | |
| 1692 | static int |
| 1693 | type_init(PyObject *cls, PyObject *args, PyObject *kwds) |
| 1694 | { |
| 1695 | int res; |
| 1696 | |
| 1697 | assert(args != NULL && PyTuple_Check(args)); |
| 1698 | assert(kwds == NULL || PyDict_Check(kwds)); |
| 1699 | |
| 1700 | if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) { |
| 1701 | PyErr_SetString(PyExc_TypeError, |
| 1702 | "type.__init__() takes no keyword arguments"); |
| 1703 | return -1; |
| 1704 | } |
| 1705 | |
| 1706 | if (args != NULL && PyTuple_Check(args) && |
| 1707 | (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) { |
| 1708 | PyErr_SetString(PyExc_TypeError, |
| 1709 | "type.__init__() takes 1 or 3 arguments"); |
| 1710 | return -1; |
| 1711 | } |
| 1712 | |
| 1713 | /* Call object.__init__(self) now. */ |
| 1714 | /* XXX Could call super(type, cls).__init__() but what's the point? */ |
| 1715 | args = PyTuple_GetSlice(args, 0, 0); |
| 1716 | res = object_init(cls, args, NULL); |
| 1717 | Py_DECREF(args); |
| 1718 | return res; |
| 1719 | } |
| 1720 | |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1721 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1722 | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) |
| 1723 | { |
| 1724 | PyObject *name, *bases, *dict; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 1725 | static char *kwlist[] = {"name", "bases", "dict", 0}; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1726 | PyObject *slots, *tmp, *newslots; |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1727 | PyTypeObject *type, *base, *tmptype, *winner; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 1728 | PyHeapTypeObject *et; |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 1729 | PyMemberDef *mp; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1730 | Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1731 | int j, may_add_dict, may_add_weak; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1732 | |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1733 | assert(args != NULL && PyTuple_Check(args)); |
| 1734 | assert(kwds == NULL || PyDict_Check(kwds)); |
| 1735 | |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1736 | /* Special case: type(x) should return x->ob_type */ |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1737 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1738 | const Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 1739 | const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1740 | |
| 1741 | if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) { |
| 1742 | PyObject *x = PyTuple_GET_ITEM(args, 0); |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 1743 | Py_INCREF(Py_Type(x)); |
| 1744 | return (PyObject *) Py_Type(x); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | /* SF bug 475327 -- if that didn't trigger, we need 3 |
| 1748 | arguments. but PyArg_ParseTupleAndKeywords below may give |
| 1749 | a msg saying type() needs exactly 3. */ |
| 1750 | if (nargs + nkwds != 3) { |
| 1751 | PyErr_SetString(PyExc_TypeError, |
| 1752 | "type() takes 1 or 3 arguments"); |
| 1753 | return NULL; |
| 1754 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1757 | /* Check arguments: (name, bases, dict) */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1758 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist, |
| 1759 | &name, |
| 1760 | &PyTuple_Type, &bases, |
| 1761 | &PyDict_Type, &dict)) |
| 1762 | return NULL; |
| 1763 | |
Armin Rigo | c0ba52d | 2007-04-19 14:44:48 +0000 | [diff] [blame] | 1764 | /* Determine the proper metatype to deal with this, |
| 1765 | and check for metatype conflicts while we're at it. |
| 1766 | Note that if some other metatype wins to contract, |
| 1767 | it's possible that its instances are not types. */ |
| 1768 | nbases = PyTuple_GET_SIZE(bases); |
| 1769 | winner = metatype; |
| 1770 | for (i = 0; i < nbases; i++) { |
| 1771 | tmp = PyTuple_GET_ITEM(bases, i); |
| 1772 | tmptype = tmp->ob_type; |
| 1773 | if (tmptype == &PyClass_Type) |
| 1774 | continue; /* Special case classic classes */ |
| 1775 | if (PyType_IsSubtype(winner, tmptype)) |
| 1776 | continue; |
| 1777 | if (PyType_IsSubtype(tmptype, winner)) { |
| 1778 | winner = tmptype; |
| 1779 | continue; |
Jeremy Hylton | fa95569 | 2007-02-27 18:29:45 +0000 | [diff] [blame] | 1780 | } |
Armin Rigo | c0ba52d | 2007-04-19 14:44:48 +0000 | [diff] [blame] | 1781 | PyErr_SetString(PyExc_TypeError, |
| 1782 | "metaclass conflict: " |
| 1783 | "the metaclass of a derived class " |
| 1784 | "must be a (non-strict) subclass " |
| 1785 | "of the metaclasses of all its bases"); |
| 1786 | return NULL; |
| 1787 | } |
| 1788 | if (winner != metatype) { |
| 1789 | if (winner->tp_new != type_new) /* Pass it to the winner */ |
| 1790 | return winner->tp_new(winner, args, kwds); |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1791 | metatype = winner; |
| 1792 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1793 | |
| 1794 | /* Adjust for empty tuple bases */ |
| 1795 | if (nbases == 0) { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1796 | bases = PyTuple_Pack(1, &PyBaseObject_Type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1797 | if (bases == NULL) |
| 1798 | return NULL; |
| 1799 | nbases = 1; |
| 1800 | } |
| 1801 | else |
| 1802 | Py_INCREF(bases); |
| 1803 | |
| 1804 | /* XXX From here until type is allocated, "return NULL" leaks bases! */ |
| 1805 | |
| 1806 | /* Calculate best base, and check that all bases are type objects */ |
| 1807 | base = best_base(bases); |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 1808 | if (base == NULL) { |
| 1809 | Py_DECREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1810 | return NULL; |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 1811 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1812 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 1813 | PyErr_Format(PyExc_TypeError, |
| 1814 | "type '%.100s' is not an acceptable base type", |
| 1815 | base->tp_name); |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 1816 | Py_DECREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1817 | return NULL; |
| 1818 | } |
| 1819 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1820 | /* Check for a __slots__ sequence variable in dict, and count it */ |
| 1821 | slots = PyDict_GetItemString(dict, "__slots__"); |
| 1822 | nslots = 0; |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1823 | add_dict = 0; |
| 1824 | add_weak = 0; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1825 | may_add_dict = base->tp_dictoffset == 0; |
| 1826 | may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0; |
| 1827 | if (slots == NULL) { |
| 1828 | if (may_add_dict) { |
| 1829 | add_dict++; |
| 1830 | } |
| 1831 | if (may_add_weak) { |
| 1832 | add_weak++; |
| 1833 | } |
| 1834 | } |
| 1835 | else { |
| 1836 | /* Have slots */ |
| 1837 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1838 | /* Make it into a tuple */ |
Neal Norwitz | cbd9ee6 | 2007-04-14 05:25:50 +0000 | [diff] [blame] | 1839 | if (PyString_Check(slots) || PyUnicode_Check(slots)) |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1840 | slots = PyTuple_Pack(1, slots); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1841 | else |
| 1842 | slots = PySequence_Tuple(slots); |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 1843 | if (slots == NULL) { |
| 1844 | Py_DECREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1845 | return NULL; |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 1846 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1847 | assert(PyTuple_Check(slots)); |
| 1848 | |
| 1849 | /* Are slots allowed? */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1850 | nslots = PyTuple_GET_SIZE(slots); |
Jeremy Hylton | 1c7a0ea | 2003-07-16 16:08:23 +0000 | [diff] [blame] | 1851 | if (nslots > 0 && base->tp_itemsize != 0) { |
Guido van Rossum | c414187 | 2001-08-30 04:43:35 +0000 | [diff] [blame] | 1852 | PyErr_Format(PyExc_TypeError, |
| 1853 | "nonempty __slots__ " |
| 1854 | "not supported for subtype of '%s'", |
| 1855 | base->tp_name); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1856 | bad_slots: |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 1857 | Py_DECREF(bases); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1858 | Py_DECREF(slots); |
Guido van Rossum | c414187 | 2001-08-30 04:43:35 +0000 | [diff] [blame] | 1859 | return NULL; |
| 1860 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1861 | |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1862 | #ifdef Py_USING_UNICODE |
| 1863 | tmp = _unicode_to_string(slots, nslots); |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1864 | if (tmp == NULL) |
| 1865 | goto bad_slots; |
Martin v. Löwis | 13b1a5c | 2002-10-14 21:11:34 +0000 | [diff] [blame] | 1866 | if (tmp != slots) { |
| 1867 | Py_DECREF(slots); |
| 1868 | slots = tmp; |
| 1869 | } |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1870 | #endif |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1871 | /* Check for valid slot names and two special cases */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1872 | for (i = 0; i < nslots; i++) { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1873 | PyObject *tmp = PyTuple_GET_ITEM(slots, i); |
| 1874 | char *s; |
| 1875 | if (!valid_identifier(tmp)) |
| 1876 | goto bad_slots; |
| 1877 | assert(PyString_Check(tmp)); |
| 1878 | s = PyString_AS_STRING(tmp); |
| 1879 | if (strcmp(s, "__dict__") == 0) { |
| 1880 | if (!may_add_dict || add_dict) { |
| 1881 | PyErr_SetString(PyExc_TypeError, |
| 1882 | "__dict__ slot disallowed: " |
| 1883 | "we already got one"); |
| 1884 | goto bad_slots; |
| 1885 | } |
| 1886 | add_dict++; |
| 1887 | } |
| 1888 | if (strcmp(s, "__weakref__") == 0) { |
| 1889 | if (!may_add_weak || add_weak) { |
| 1890 | PyErr_SetString(PyExc_TypeError, |
| 1891 | "__weakref__ slot disallowed: " |
| 1892 | "either we already got one, " |
| 1893 | "or __itemsize__ != 0"); |
| 1894 | goto bad_slots; |
| 1895 | } |
| 1896 | add_weak++; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1897 | } |
| 1898 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1899 | |
Žiga Seilnacht | 6f2d09c | 2007-03-16 11:59:38 +0000 | [diff] [blame] | 1900 | /* Copy slots into a list, mangle names and sort them. |
| 1901 | Sorted names are needed for __class__ assignment. |
| 1902 | Convert them back to tuple at the end. |
| 1903 | */ |
| 1904 | newslots = PyList_New(nslots - add_dict - add_weak); |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1905 | if (newslots == NULL) |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1906 | goto bad_slots; |
| 1907 | for (i = j = 0; i < nslots; i++) { |
| 1908 | char *s; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1909 | tmp = PyTuple_GET_ITEM(slots, i); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1910 | s = PyString_AS_STRING(tmp); |
| 1911 | if ((add_dict && strcmp(s, "__dict__") == 0) || |
| 1912 | (add_weak && strcmp(s, "__weakref__") == 0)) |
| 1913 | continue; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1914 | tmp =_Py_Mangle(name, tmp); |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1915 | if (!tmp) |
| 1916 | goto bad_slots; |
Žiga Seilnacht | 6f2d09c | 2007-03-16 11:59:38 +0000 | [diff] [blame] | 1917 | PyList_SET_ITEM(newslots, j, tmp); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1918 | j++; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1919 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1920 | assert(j == nslots - add_dict - add_weak); |
| 1921 | nslots = j; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1922 | Py_DECREF(slots); |
Žiga Seilnacht | 6f2d09c | 2007-03-16 11:59:38 +0000 | [diff] [blame] | 1923 | if (PyList_Sort(newslots) == -1) { |
| 1924 | Py_DECREF(bases); |
| 1925 | Py_DECREF(newslots); |
| 1926 | return NULL; |
| 1927 | } |
| 1928 | slots = PyList_AsTuple(newslots); |
| 1929 | Py_DECREF(newslots); |
| 1930 | if (slots == NULL) { |
| 1931 | Py_DECREF(bases); |
| 1932 | return NULL; |
| 1933 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1934 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1935 | /* Secondary bases may provide weakrefs or dict */ |
| 1936 | if (nbases > 1 && |
| 1937 | ((may_add_dict && !add_dict) || |
| 1938 | (may_add_weak && !add_weak))) { |
| 1939 | for (i = 0; i < nbases; i++) { |
| 1940 | tmp = PyTuple_GET_ITEM(bases, i); |
| 1941 | if (tmp == (PyObject *)base) |
| 1942 | continue; /* Skip primary base */ |
| 1943 | if (PyClass_Check(tmp)) { |
| 1944 | /* Classic base class provides both */ |
| 1945 | if (may_add_dict && !add_dict) |
| 1946 | add_dict++; |
| 1947 | if (may_add_weak && !add_weak) |
| 1948 | add_weak++; |
| 1949 | break; |
| 1950 | } |
| 1951 | assert(PyType_Check(tmp)); |
| 1952 | tmptype = (PyTypeObject *)tmp; |
| 1953 | if (may_add_dict && !add_dict && |
| 1954 | tmptype->tp_dictoffset != 0) |
| 1955 | add_dict++; |
| 1956 | if (may_add_weak && !add_weak && |
| 1957 | tmptype->tp_weaklistoffset != 0) |
| 1958 | add_weak++; |
| 1959 | if (may_add_dict && !add_dict) |
| 1960 | continue; |
| 1961 | if (may_add_weak && !add_weak) |
| 1962 | continue; |
| 1963 | /* Nothing more to check */ |
| 1964 | break; |
| 1965 | } |
| 1966 | } |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1967 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1968 | |
| 1969 | /* XXX From here until type is safely allocated, |
| 1970 | "return NULL" may leak slots! */ |
| 1971 | |
| 1972 | /* Allocate the type object */ |
| 1973 | type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1974 | if (type == NULL) { |
| 1975 | Py_XDECREF(slots); |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 1976 | Py_DECREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1977 | return NULL; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1978 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1979 | |
| 1980 | /* Keep name and slots alive in the extended type object */ |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 1981 | et = (PyHeapTypeObject *)type; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1982 | Py_INCREF(name); |
Georg Brandl | c255c7b | 2006-02-20 22:27:28 +0000 | [diff] [blame] | 1983 | et->ht_name = name; |
| 1984 | et->ht_slots = slots; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1985 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 1986 | /* Initialize tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1987 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | |
| 1988 | Py_TPFLAGS_BASETYPE; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1989 | if (base->tp_flags & Py_TPFLAGS_HAVE_GC) |
| 1990 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 1991 | |
| 1992 | /* It's a new-style number unless it specifically inherits any |
| 1993 | old-style numeric behavior */ |
| 1994 | if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) || |
| 1995 | (base->tp_as_number == NULL)) |
| 1996 | type->tp_flags |= Py_TPFLAGS_CHECKTYPES; |
| 1997 | |
| 1998 | /* Initialize essential fields */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1999 | type->tp_as_number = &et->as_number; |
| 2000 | type->tp_as_sequence = &et->as_sequence; |
| 2001 | type->tp_as_mapping = &et->as_mapping; |
| 2002 | type->tp_as_buffer = &et->as_buffer; |
| 2003 | type->tp_name = PyString_AS_STRING(name); |
| 2004 | |
| 2005 | /* Set tp_base and tp_bases */ |
| 2006 | type->tp_bases = bases; |
| 2007 | Py_INCREF(base); |
| 2008 | type->tp_base = base; |
| 2009 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2010 | /* Initialize tp_dict from passed-in dict */ |
| 2011 | type->tp_dict = dict = PyDict_Copy(dict); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2012 | if (dict == NULL) { |
| 2013 | Py_DECREF(type); |
| 2014 | return NULL; |
| 2015 | } |
| 2016 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 2017 | /* Set __module__ in the dict */ |
| 2018 | if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 2019 | tmp = PyEval_GetGlobals(); |
| 2020 | if (tmp != NULL) { |
| 2021 | tmp = PyDict_GetItemString(tmp, "__name__"); |
| 2022 | if (tmp != NULL) { |
| 2023 | if (PyDict_SetItemString(dict, "__module__", |
| 2024 | tmp) < 0) |
| 2025 | return NULL; |
| 2026 | } |
| 2027 | } |
| 2028 | } |
| 2029 | |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2030 | /* 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] | 2031 | and is a string. The __doc__ accessor will first look for tp_doc; |
| 2032 | if that fails, it will still look into __dict__. |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2033 | */ |
| 2034 | { |
| 2035 | PyObject *doc = PyDict_GetItemString(dict, "__doc__"); |
| 2036 | if (doc != NULL && PyString_Check(doc)) { |
| 2037 | const size_t n = (size_t)PyString_GET_SIZE(doc); |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2038 | char *tp_doc = (char *)PyObject_MALLOC(n+1); |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 2039 | if (tp_doc == NULL) { |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2040 | Py_DECREF(type); |
| 2041 | return NULL; |
| 2042 | } |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 2043 | memcpy(tp_doc, PyString_AS_STRING(doc), n+1); |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2044 | type->tp_doc = tp_doc; |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2045 | } |
| 2046 | } |
| 2047 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2048 | /* Special-case __new__: if it's a plain function, |
| 2049 | make it a static function */ |
| 2050 | tmp = PyDict_GetItemString(dict, "__new__"); |
| 2051 | if (tmp != NULL && PyFunction_Check(tmp)) { |
| 2052 | tmp = PyStaticMethod_New(tmp); |
| 2053 | if (tmp == NULL) { |
| 2054 | Py_DECREF(type); |
| 2055 | return NULL; |
| 2056 | } |
| 2057 | PyDict_SetItemString(dict, "__new__", tmp); |
| 2058 | Py_DECREF(tmp); |
| 2059 | } |
| 2060 | |
| 2061 | /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2062 | mp = PyHeapType_GET_MEMBERS(et); |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 2063 | slotoffset = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2064 | if (slots != NULL) { |
| 2065 | for (i = 0; i < nslots; i++, mp++) { |
| 2066 | mp->name = PyString_AS_STRING( |
| 2067 | PyTuple_GET_ITEM(slots, i)); |
Guido van Rossum | 64b206c | 2001-12-04 17:13:22 +0000 | [diff] [blame] | 2068 | mp->type = T_OBJECT_EX; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2069 | mp->offset = slotoffset; |
Žiga Seilnacht | 8903208 | 2007-03-11 15:54:54 +0000 | [diff] [blame] | 2070 | |
| 2071 | /* __dict__ and __weakref__ are already filtered out */ |
| 2072 | assert(strcmp(mp->name, "__dict__") != 0); |
| 2073 | assert(strcmp(mp->name, "__weakref__") != 0); |
| 2074 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2075 | slotoffset += sizeof(PyObject *); |
| 2076 | } |
| 2077 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2078 | if (add_dict) { |
| 2079 | if (base->tp_itemsize) |
| 2080 | type->tp_dictoffset = -(long)sizeof(PyObject *); |
| 2081 | else |
| 2082 | type->tp_dictoffset = slotoffset; |
| 2083 | slotoffset += sizeof(PyObject *); |
| 2084 | } |
| 2085 | if (add_weak) { |
| 2086 | assert(!base->tp_itemsize); |
| 2087 | type->tp_weaklistoffset = slotoffset; |
| 2088 | slotoffset += sizeof(PyObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2089 | } |
| 2090 | type->tp_basicsize = slotoffset; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 2091 | type->tp_itemsize = base->tp_itemsize; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2092 | type->tp_members = PyHeapType_GET_MEMBERS(et); |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2093 | |
| 2094 | if (type->tp_weaklistoffset && type->tp_dictoffset) |
| 2095 | type->tp_getset = subtype_getsets_full; |
| 2096 | else if (type->tp_weaklistoffset && !type->tp_dictoffset) |
| 2097 | type->tp_getset = subtype_getsets_weakref_only; |
| 2098 | else if (!type->tp_weaklistoffset && type->tp_dictoffset) |
| 2099 | type->tp_getset = subtype_getsets_dict_only; |
| 2100 | else |
| 2101 | type->tp_getset = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2102 | |
| 2103 | /* Special case some slots */ |
| 2104 | if (type->tp_dictoffset != 0 || nslots > 0) { |
| 2105 | if (base->tp_getattr == NULL && base->tp_getattro == NULL) |
| 2106 | type->tp_getattro = PyObject_GenericGetAttr; |
| 2107 | if (base->tp_setattr == NULL && base->tp_setattro == NULL) |
| 2108 | type->tp_setattro = PyObject_GenericSetAttr; |
| 2109 | } |
| 2110 | type->tp_dealloc = subtype_dealloc; |
| 2111 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 2112 | /* Enable GC unless there are really no instance variables possible */ |
| 2113 | if (!(type->tp_basicsize == sizeof(PyObject) && |
| 2114 | type->tp_itemsize == 0)) |
| 2115 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
| 2116 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2117 | /* Always override allocation strategy to use regular heap */ |
| 2118 | type->tp_alloc = PyType_GenericAlloc; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2119 | if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 2120 | type->tp_free = PyObject_GC_Del; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 2121 | type->tp_traverse = subtype_traverse; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 2122 | type->tp_clear = subtype_clear; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2123 | } |
| 2124 | else |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 2125 | type->tp_free = PyObject_Del; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2126 | |
| 2127 | /* Initialize the rest */ |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 2128 | if (PyType_Ready(type) < 0) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2129 | Py_DECREF(type); |
| 2130 | return NULL; |
| 2131 | } |
| 2132 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 2133 | /* Put the proper slots in place */ |
| 2134 | fixup_slot_dispatchers(type); |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 2135 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2136 | return (PyObject *)type; |
| 2137 | } |
| 2138 | |
| 2139 | /* Internal API to look for a name through the MRO. |
| 2140 | This returns a borrowed reference, and doesn't set an exception! */ |
| 2141 | PyObject * |
| 2142 | _PyType_Lookup(PyTypeObject *type, PyObject *name) |
| 2143 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2144 | Py_ssize_t i, n; |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 2145 | PyObject *mro, *res, *base, *dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2146 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2147 | /* Look in tp_dict of types in MRO */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2148 | mro = type->tp_mro; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 2149 | |
| 2150 | /* If mro is NULL, the type is either not yet initialized |
| 2151 | by PyType_Ready(), or already cleared by type_clear(). |
| 2152 | Either way the safest thing to do is to return NULL. */ |
| 2153 | if (mro == NULL) |
| 2154 | return NULL; |
| 2155 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2156 | assert(PyTuple_Check(mro)); |
| 2157 | n = PyTuple_GET_SIZE(mro); |
| 2158 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 2159 | base = PyTuple_GET_ITEM(mro, i); |
| 2160 | if (PyClass_Check(base)) |
| 2161 | dict = ((PyClassObject *)base)->cl_dict; |
| 2162 | else { |
| 2163 | assert(PyType_Check(base)); |
| 2164 | dict = ((PyTypeObject *)base)->tp_dict; |
| 2165 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2166 | assert(dict && PyDict_Check(dict)); |
| 2167 | res = PyDict_GetItem(dict, name); |
| 2168 | if (res != NULL) |
| 2169 | return res; |
| 2170 | } |
| 2171 | return NULL; |
| 2172 | } |
| 2173 | |
| 2174 | /* This is similar to PyObject_GenericGetAttr(), |
| 2175 | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ |
| 2176 | static PyObject * |
| 2177 | type_getattro(PyTypeObject *type, PyObject *name) |
| 2178 | { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2179 | PyTypeObject *metatype = Py_Type(type); |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2180 | PyObject *meta_attribute, *attribute; |
| 2181 | descrgetfunc meta_get; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2182 | |
| 2183 | /* Initialize this type (we'll assume the metatype is initialized) */ |
| 2184 | if (type->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 2185 | if (PyType_Ready(type) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2186 | return NULL; |
| 2187 | } |
| 2188 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2189 | /* No readable descriptor found yet */ |
| 2190 | meta_get = NULL; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2191 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2192 | /* Look for the attribute in the metatype */ |
| 2193 | meta_attribute = _PyType_Lookup(metatype, name); |
| 2194 | |
| 2195 | if (meta_attribute != NULL) { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2196 | meta_get = Py_Type(meta_attribute)->tp_descr_get; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2197 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2198 | if (meta_get != NULL && PyDescr_IsData(meta_attribute)) { |
| 2199 | /* Data descriptors implement tp_descr_set to intercept |
| 2200 | * writes. Assume the attribute is not overridden in |
| 2201 | * type's tp_dict (and bases): call the descriptor now. |
| 2202 | */ |
| 2203 | return meta_get(meta_attribute, (PyObject *)type, |
| 2204 | (PyObject *)metatype); |
| 2205 | } |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2206 | Py_INCREF(meta_attribute); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2209 | /* No data descriptor found on metatype. Look in tp_dict of this |
| 2210 | * type and its bases */ |
| 2211 | attribute = _PyType_Lookup(type, name); |
| 2212 | if (attribute != NULL) { |
| 2213 | /* Implement descriptor functionality, if any */ |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2214 | descrgetfunc local_get = Py_Type(attribute)->tp_descr_get; |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2215 | |
| 2216 | Py_XDECREF(meta_attribute); |
| 2217 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2218 | if (local_get != NULL) { |
| 2219 | /* NULL 2nd argument indicates the descriptor was |
| 2220 | * found on the target object itself (or a base) */ |
| 2221 | return local_get(attribute, (PyObject *)NULL, |
| 2222 | (PyObject *)type); |
| 2223 | } |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2224 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2225 | Py_INCREF(attribute); |
| 2226 | return attribute; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2229 | /* No attribute found in local __dict__ (or bases): use the |
| 2230 | * descriptor from the metatype, if any */ |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2231 | if (meta_get != NULL) { |
| 2232 | PyObject *res; |
| 2233 | res = meta_get(meta_attribute, (PyObject *)type, |
| 2234 | (PyObject *)metatype); |
| 2235 | Py_DECREF(meta_attribute); |
| 2236 | return res; |
| 2237 | } |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2238 | |
| 2239 | /* If an ordinary attribute was found on the metatype, return it now */ |
| 2240 | if (meta_attribute != NULL) { |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2241 | return meta_attribute; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
| 2244 | /* Give up */ |
| 2245 | PyErr_Format(PyExc_AttributeError, |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2246 | "type object '%.50s' has no attribute '%.400s'", |
| 2247 | type->tp_name, PyString_AS_STRING(name)); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2248 | return NULL; |
| 2249 | } |
| 2250 | |
| 2251 | static int |
| 2252 | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) |
| 2253 | { |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 2254 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 2255 | PyErr_Format( |
| 2256 | PyExc_TypeError, |
| 2257 | "can't set attributes of built-in/extension type '%s'", |
| 2258 | type->tp_name); |
| 2259 | return -1; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 2260 | } |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 2261 | /* XXX Example of how I expect this to be used... |
| 2262 | if (update_subclasses(type, name, invalidate_cache, NULL) < 0) |
| 2263 | return -1; |
| 2264 | */ |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 2265 | if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0) |
| 2266 | return -1; |
| 2267 | return update_slot(type, name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | static void |
| 2271 | type_dealloc(PyTypeObject *type) |
| 2272 | { |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2273 | PyHeapTypeObject *et; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2274 | |
| 2275 | /* Assert this is a heap-allocated type object */ |
| 2276 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2277 | _PyObject_GC_UNTRACK(type); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2278 | PyObject_ClearWeakRefs((PyObject *)type); |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2279 | et = (PyHeapTypeObject *)type; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2280 | Py_XDECREF(type->tp_base); |
| 2281 | Py_XDECREF(type->tp_dict); |
| 2282 | Py_XDECREF(type->tp_bases); |
| 2283 | Py_XDECREF(type->tp_mro); |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2284 | Py_XDECREF(type->tp_cache); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2285 | Py_XDECREF(type->tp_subclasses); |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2286 | /* A type's tp_doc is heap allocated, unlike the tp_doc slots |
| 2287 | * of most other objects. It's okay to cast it to char *. |
| 2288 | */ |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 2289 | PyObject_Free((char *)type->tp_doc); |
Georg Brandl | c255c7b | 2006-02-20 22:27:28 +0000 | [diff] [blame] | 2290 | Py_XDECREF(et->ht_name); |
| 2291 | Py_XDECREF(et->ht_slots); |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2292 | Py_Type(type)->tp_free((PyObject *)type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2295 | static PyObject * |
| 2296 | type_subclasses(PyTypeObject *type, PyObject *args_ignored) |
| 2297 | { |
| 2298 | PyObject *list, *raw, *ref; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2299 | Py_ssize_t i, n; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2300 | |
| 2301 | list = PyList_New(0); |
| 2302 | if (list == NULL) |
| 2303 | return NULL; |
| 2304 | raw = type->tp_subclasses; |
| 2305 | if (raw == NULL) |
| 2306 | return list; |
| 2307 | assert(PyList_Check(raw)); |
| 2308 | n = PyList_GET_SIZE(raw); |
| 2309 | for (i = 0; i < n; i++) { |
| 2310 | ref = PyList_GET_ITEM(raw, i); |
Tim Peters | 4438338 | 2001-10-08 16:49:26 +0000 | [diff] [blame] | 2311 | assert(PyWeakref_CheckRef(ref)); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2312 | ref = PyWeakref_GET_OBJECT(ref); |
| 2313 | if (ref != Py_None) { |
| 2314 | if (PyList_Append(list, ref) < 0) { |
| 2315 | Py_DECREF(list); |
| 2316 | return NULL; |
| 2317 | } |
| 2318 | } |
| 2319 | } |
| 2320 | return list; |
| 2321 | } |
| 2322 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2323 | static PyMethodDef type_methods[] = { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2324 | {"mro", (PyCFunction)mro_external, METH_NOARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 2325 | PyDoc_STR("mro() -> list\nreturn a type's method resolution order")}, |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2326 | {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 2327 | PyDoc_STR("__subclasses__() -> list of immediate subclasses")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2328 | {0} |
| 2329 | }; |
| 2330 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2331 | PyDoc_STRVAR(type_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2332 | "type(object) -> the object's type\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2333 | "type(name, bases, dict) -> a new type"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2334 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2335 | static int |
| 2336 | type_traverse(PyTypeObject *type, visitproc visit, void *arg) |
| 2337 | { |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2338 | /* Because of type_is_gc(), the collector only calls this |
| 2339 | for heaptypes. */ |
| 2340 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2341 | |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 2342 | Py_VISIT(type->tp_dict); |
| 2343 | Py_VISIT(type->tp_cache); |
| 2344 | Py_VISIT(type->tp_mro); |
| 2345 | Py_VISIT(type->tp_bases); |
| 2346 | Py_VISIT(type->tp_base); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2347 | |
| 2348 | /* There's no need to visit type->tp_subclasses or |
Georg Brandl | c255c7b | 2006-02-20 22:27:28 +0000 | [diff] [blame] | 2349 | ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2350 | in cycles; tp_subclasses is a list of weak references, |
| 2351 | and slots is a tuple of strings. */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2352 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2353 | return 0; |
| 2354 | } |
| 2355 | |
| 2356 | static int |
| 2357 | type_clear(PyTypeObject *type) |
| 2358 | { |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2359 | /* Because of type_is_gc(), the collector only calls this |
| 2360 | for heaptypes. */ |
| 2361 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2362 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2363 | /* The only field we need to clear is tp_mro, which is part of a |
| 2364 | hard cycle (its first element is the class itself) that won't |
| 2365 | be broken otherwise (it's a tuple and tuples don't have a |
| 2366 | tp_clear handler). None of the other fields need to be |
| 2367 | cleared, and here's why: |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2368 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2369 | tp_dict: |
| 2370 | It is a dict, so the collector will call its tp_clear. |
| 2371 | |
| 2372 | tp_cache: |
| 2373 | Not used; if it were, it would be a dict. |
| 2374 | |
| 2375 | tp_bases, tp_base: |
| 2376 | If these are involved in a cycle, there must be at least |
| 2377 | one other, mutable object in the cycle, e.g. a base |
| 2378 | class's dict; the cycle will be broken that way. |
| 2379 | |
| 2380 | tp_subclasses: |
| 2381 | A list of weak references can't be part of a cycle; and |
| 2382 | lists have their own tp_clear. |
| 2383 | |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2384 | slots (in PyHeapTypeObject): |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2385 | A tuple of strings can't be part of a cycle. |
| 2386 | */ |
| 2387 | |
Thomas Wouters | edf17d8 | 2006-04-15 17:28:34 +0000 | [diff] [blame] | 2388 | Py_CLEAR(type->tp_mro); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2389 | |
| 2390 | return 0; |
| 2391 | } |
| 2392 | |
| 2393 | static int |
| 2394 | type_is_gc(PyTypeObject *type) |
| 2395 | { |
| 2396 | return type->tp_flags & Py_TPFLAGS_HEAPTYPE; |
| 2397 | } |
| 2398 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2399 | PyTypeObject PyType_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2400 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2401 | "type", /* tp_name */ |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2402 | sizeof(PyHeapTypeObject), /* tp_basicsize */ |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 2403 | sizeof(PyMemberDef), /* tp_itemsize */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2404 | (destructor)type_dealloc, /* tp_dealloc */ |
| 2405 | 0, /* tp_print */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2406 | 0, /* tp_getattr */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2407 | 0, /* tp_setattr */ |
| 2408 | type_compare, /* tp_compare */ |
| 2409 | (reprfunc)type_repr, /* tp_repr */ |
| 2410 | 0, /* tp_as_number */ |
| 2411 | 0, /* tp_as_sequence */ |
| 2412 | 0, /* tp_as_mapping */ |
| 2413 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
| 2414 | (ternaryfunc)type_call, /* tp_call */ |
| 2415 | 0, /* tp_str */ |
| 2416 | (getattrofunc)type_getattro, /* tp_getattro */ |
| 2417 | (setattrofunc)type_setattro, /* tp_setattro */ |
| 2418 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2419 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 2420 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2421 | type_doc, /* tp_doc */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2422 | (traverseproc)type_traverse, /* tp_traverse */ |
| 2423 | (inquiry)type_clear, /* tp_clear */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2424 | 0, /* tp_richcompare */ |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2425 | offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2426 | 0, /* tp_iter */ |
| 2427 | 0, /* tp_iternext */ |
| 2428 | type_methods, /* tp_methods */ |
| 2429 | type_members, /* tp_members */ |
| 2430 | type_getsets, /* tp_getset */ |
| 2431 | 0, /* tp_base */ |
| 2432 | 0, /* tp_dict */ |
| 2433 | 0, /* tp_descr_get */ |
| 2434 | 0, /* tp_descr_set */ |
| 2435 | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ |
Guido van Rossum | f102e24 | 2007-03-23 18:53:03 +0000 | [diff] [blame] | 2436 | type_init, /* tp_init */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2437 | 0, /* tp_alloc */ |
| 2438 | type_new, /* tp_new */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2439 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2440 | (inquiry)type_is_gc, /* tp_is_gc */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2441 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2442 | |
| 2443 | |
| 2444 | /* The base type of all types (eventually)... except itself. */ |
| 2445 | |
Guido van Rossum | 143b564 | 2007-03-23 04:58:42 +0000 | [diff] [blame] | 2446 | /* You may wonder why object.__new__() only complains about arguments |
| 2447 | when object.__init__() is not overridden, and vice versa. |
| 2448 | |
| 2449 | Consider the use cases: |
| 2450 | |
| 2451 | 1. When neither is overridden, we want to hear complaints about |
| 2452 | excess (i.e., any) arguments, since their presence could |
| 2453 | indicate there's a bug. |
| 2454 | |
| 2455 | 2. When defining an Immutable type, we are likely to override only |
| 2456 | __new__(), since __init__() is called too late to initialize an |
| 2457 | Immutable object. Since __new__() defines the signature for the |
| 2458 | type, it would be a pain to have to override __init__() just to |
| 2459 | stop it from complaining about excess arguments. |
| 2460 | |
| 2461 | 3. When defining a Mutable type, we are likely to override only |
| 2462 | __init__(). So here the converse reasoning applies: we don't |
| 2463 | want to have to override __new__() just to stop it from |
| 2464 | complaining. |
| 2465 | |
| 2466 | 4. When __init__() is overridden, and the subclass __init__() calls |
| 2467 | object.__init__(), the latter should complain about excess |
| 2468 | arguments; ditto for __new__(). |
| 2469 | |
| 2470 | Use cases 2 and 3 make it unattractive to unconditionally check for |
| 2471 | excess arguments. The best solution that addresses all four use |
| 2472 | cases is as follows: __init__() complains about excess arguments |
| 2473 | unless __new__() is overridden and __init__() is not overridden |
| 2474 | (IOW, if __init__() is overridden or __new__() is not overridden); |
| 2475 | symmetrically, __new__() complains about excess arguments unless |
| 2476 | __init__() is overridden and __new__() is not overridden |
| 2477 | (IOW, if __new__() is overridden or __init__() is not overridden). |
| 2478 | |
| 2479 | However, for backwards compatibility, this breaks too much code. |
| 2480 | Therefore, in 2.6, we'll *warn* about excess arguments when both |
| 2481 | methods are overridden; for all other cases we'll use the above |
| 2482 | rules. |
| 2483 | |
| 2484 | */ |
| 2485 | |
| 2486 | /* Forward */ |
| 2487 | static PyObject * |
| 2488 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 2489 | |
| 2490 | static int |
| 2491 | excess_args(PyObject *args, PyObject *kwds) |
| 2492 | { |
| 2493 | return PyTuple_GET_SIZE(args) || |
| 2494 | (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)); |
| 2495 | } |
| 2496 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2497 | static int |
| 2498 | object_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2499 | { |
Guido van Rossum | 143b564 | 2007-03-23 04:58:42 +0000 | [diff] [blame] | 2500 | int err = 0; |
| 2501 | if (excess_args(args, kwds)) { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2502 | PyTypeObject *type = Py_Type(self); |
Guido van Rossum | 143b564 | 2007-03-23 04:58:42 +0000 | [diff] [blame] | 2503 | if (type->tp_init != object_init && |
| 2504 | type->tp_new != object_new) |
| 2505 | { |
| 2506 | err = PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2507 | "object.__init__() takes no parameters", |
| 2508 | 1); |
| 2509 | } |
| 2510 | else if (type->tp_init != object_init || |
| 2511 | type->tp_new == object_new) |
| 2512 | { |
| 2513 | PyErr_SetString(PyExc_TypeError, |
| 2514 | "object.__init__() takes no parameters"); |
| 2515 | err = -1; |
| 2516 | } |
| 2517 | } |
| 2518 | return err; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2519 | } |
| 2520 | |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2521 | static PyObject * |
| 2522 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2523 | { |
Guido van Rossum | 143b564 | 2007-03-23 04:58:42 +0000 | [diff] [blame] | 2524 | int err = 0; |
| 2525 | if (excess_args(args, kwds)) { |
| 2526 | if (type->tp_new != object_new && |
| 2527 | type->tp_init != object_init) |
| 2528 | { |
| 2529 | err = PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2530 | "object.__new__() takes no parameters", |
| 2531 | 1); |
| 2532 | } |
| 2533 | else if (type->tp_new != object_new || |
| 2534 | type->tp_init == object_init) |
| 2535 | { |
| 2536 | PyErr_SetString(PyExc_TypeError, |
| 2537 | "object.__new__() takes no parameters"); |
| 2538 | err = -1; |
| 2539 | } |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2540 | } |
Guido van Rossum | 143b564 | 2007-03-23 04:58:42 +0000 | [diff] [blame] | 2541 | if (err < 0) |
| 2542 | return NULL; |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2543 | return type->tp_alloc(type, 0); |
| 2544 | } |
| 2545 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2546 | static void |
| 2547 | object_dealloc(PyObject *self) |
| 2548 | { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2549 | Py_Type(self)->tp_free(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2552 | static PyObject * |
| 2553 | object_repr(PyObject *self) |
| 2554 | { |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2555 | PyTypeObject *type; |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2556 | PyObject *mod, *name, *rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2557 | |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2558 | type = Py_Type(self); |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2559 | mod = type_module(type, NULL); |
| 2560 | if (mod == NULL) |
| 2561 | PyErr_Clear(); |
| 2562 | else if (!PyString_Check(mod)) { |
| 2563 | Py_DECREF(mod); |
| 2564 | mod = NULL; |
| 2565 | } |
| 2566 | name = type_name(type, NULL); |
| 2567 | if (name == NULL) |
| 2568 | return NULL; |
| 2569 | if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) |
Guido van Rossum | ff0e6d6 | 2001-09-24 16:03:59 +0000 | [diff] [blame] | 2570 | rtn = PyString_FromFormat("<%s.%s object at %p>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2571 | PyString_AS_STRING(mod), |
| 2572 | PyString_AS_STRING(name), |
| 2573 | self); |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2574 | else |
Guido van Rossum | ff0e6d6 | 2001-09-24 16:03:59 +0000 | [diff] [blame] | 2575 | rtn = PyString_FromFormat("<%s object at %p>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2576 | type->tp_name, self); |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2577 | Py_XDECREF(mod); |
| 2578 | Py_DECREF(name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2579 | return rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2582 | static PyObject * |
| 2583 | object_str(PyObject *self) |
| 2584 | { |
| 2585 | unaryfunc f; |
| 2586 | |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2587 | f = Py_Type(self)->tp_repr; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2588 | if (f == NULL) |
| 2589 | f = object_repr; |
| 2590 | return f(self); |
| 2591 | } |
| 2592 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2593 | static PyObject * |
| 2594 | object_get_class(PyObject *self, void *closure) |
| 2595 | { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2596 | Py_INCREF(Py_Type(self)); |
| 2597 | return (PyObject *)(Py_Type(self)); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
| 2600 | static int |
| 2601 | equiv_structs(PyTypeObject *a, PyTypeObject *b) |
| 2602 | { |
| 2603 | return a == b || |
| 2604 | (a != NULL && |
| 2605 | b != NULL && |
| 2606 | a->tp_basicsize == b->tp_basicsize && |
| 2607 | a->tp_itemsize == b->tp_itemsize && |
| 2608 | a->tp_dictoffset == b->tp_dictoffset && |
| 2609 | a->tp_weaklistoffset == b->tp_weaklistoffset && |
| 2610 | ((a->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 2611 | (b->tp_flags & Py_TPFLAGS_HAVE_GC))); |
| 2612 | } |
| 2613 | |
| 2614 | static int |
| 2615 | same_slots_added(PyTypeObject *a, PyTypeObject *b) |
| 2616 | { |
| 2617 | PyTypeObject *base = a->tp_base; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 2618 | Py_ssize_t size; |
Žiga Seilnacht | 6f2d09c | 2007-03-16 11:59:38 +0000 | [diff] [blame] | 2619 | PyObject *slots_a, *slots_b; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2620 | |
| 2621 | if (base != b->tp_base) |
| 2622 | return 0; |
| 2623 | if (equiv_structs(a, base) && equiv_structs(b, base)) |
| 2624 | return 1; |
| 2625 | size = base->tp_basicsize; |
| 2626 | if (a->tp_dictoffset == size && b->tp_dictoffset == size) |
| 2627 | size += sizeof(PyObject *); |
| 2628 | if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size) |
| 2629 | size += sizeof(PyObject *); |
Žiga Seilnacht | 6f2d09c | 2007-03-16 11:59:38 +0000 | [diff] [blame] | 2630 | |
| 2631 | /* Check slots compliance */ |
| 2632 | slots_a = ((PyHeapTypeObject *)a)->ht_slots; |
| 2633 | slots_b = ((PyHeapTypeObject *)b)->ht_slots; |
| 2634 | if (slots_a && slots_b) { |
| 2635 | if (PyObject_Compare(slots_a, slots_b) != 0) |
| 2636 | return 0; |
| 2637 | size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a); |
| 2638 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2639 | return size == a->tp_basicsize && size == b->tp_basicsize; |
| 2640 | } |
| 2641 | |
| 2642 | static int |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2643 | compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2644 | { |
| 2645 | PyTypeObject *newbase, *oldbase; |
| 2646 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2647 | if (newto->tp_dealloc != oldto->tp_dealloc || |
| 2648 | newto->tp_free != oldto->tp_free) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2649 | { |
| 2650 | PyErr_Format(PyExc_TypeError, |
| 2651 | "%s assignment: " |
| 2652 | "'%s' deallocator differs from '%s'", |
| 2653 | attr, |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2654 | newto->tp_name, |
| 2655 | oldto->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2656 | return 0; |
| 2657 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2658 | newbase = newto; |
| 2659 | oldbase = oldto; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2660 | while (equiv_structs(newbase, newbase->tp_base)) |
| 2661 | newbase = newbase->tp_base; |
| 2662 | while (equiv_structs(oldbase, oldbase->tp_base)) |
| 2663 | oldbase = oldbase->tp_base; |
| 2664 | if (newbase != oldbase && |
| 2665 | (newbase->tp_base != oldbase->tp_base || |
| 2666 | !same_slots_added(newbase, oldbase))) { |
| 2667 | PyErr_Format(PyExc_TypeError, |
| 2668 | "%s assignment: " |
| 2669 | "'%s' object layout differs from '%s'", |
| 2670 | attr, |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2671 | newto->tp_name, |
| 2672 | oldto->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2673 | return 0; |
| 2674 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 2675 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2676 | return 1; |
| 2677 | } |
| 2678 | |
| 2679 | static int |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2680 | object_set_class(PyObject *self, PyObject *value, void *closure) |
| 2681 | { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2682 | PyTypeObject *oldto = Py_Type(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2683 | PyTypeObject *newto; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2684 | |
Guido van Rossum | b6b8942 | 2002-04-15 01:03:30 +0000 | [diff] [blame] | 2685 | if (value == NULL) { |
| 2686 | PyErr_SetString(PyExc_TypeError, |
| 2687 | "can't delete __class__ attribute"); |
| 2688 | return -1; |
| 2689 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2690 | if (!PyType_Check(value)) { |
| 2691 | PyErr_Format(PyExc_TypeError, |
| 2692 | "__class__ must be set to new-style class, not '%s' object", |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2693 | Py_Type(value)->tp_name); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2694 | return -1; |
| 2695 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2696 | newto = (PyTypeObject *)value; |
| 2697 | if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) || |
| 2698 | !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
Guido van Rossum | 40af889 | 2002-08-10 05:42:07 +0000 | [diff] [blame] | 2699 | { |
| 2700 | PyErr_Format(PyExc_TypeError, |
| 2701 | "__class__ assignment: only for heap types"); |
| 2702 | return -1; |
| 2703 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2704 | if (compatible_for_assignment(newto, oldto, "__class__")) { |
| 2705 | Py_INCREF(newto); |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2706 | Py_Type(self) = newto; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2707 | Py_DECREF(oldto); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2708 | return 0; |
| 2709 | } |
| 2710 | else { |
Guido van Rossum | 9ee4b94 | 2002-05-24 18:47:47 +0000 | [diff] [blame] | 2711 | return -1; |
| 2712 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2713 | } |
| 2714 | |
| 2715 | static PyGetSetDef object_getsets[] = { |
| 2716 | {"__class__", object_get_class, object_set_class, |
Neal Norwitz | 858e34f | 2002-08-13 17:18:45 +0000 | [diff] [blame] | 2717 | PyDoc_STR("the object's class")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2718 | {0} |
| 2719 | }; |
| 2720 | |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 2721 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 2722 | /* Stuff to implement __reduce_ex__ for pickle protocols >= 2. |
| 2723 | We fall back to helpers in copy_reg for: |
| 2724 | - pickle protocols < 2 |
| 2725 | - calculating the list of slot names (done only once per class) |
| 2726 | - the __newobj__ function (which is used as a token but never called) |
| 2727 | */ |
| 2728 | |
| 2729 | static PyObject * |
| 2730 | import_copy_reg(void) |
| 2731 | { |
| 2732 | static PyObject *copy_reg_str; |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2733 | |
| 2734 | if (!copy_reg_str) { |
| 2735 | copy_reg_str = PyString_InternFromString("copy_reg"); |
| 2736 | if (copy_reg_str == NULL) |
| 2737 | return NULL; |
| 2738 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 2739 | |
| 2740 | return PyImport_Import(copy_reg_str); |
| 2741 | } |
| 2742 | |
| 2743 | static PyObject * |
| 2744 | slotnames(PyObject *cls) |
| 2745 | { |
| 2746 | PyObject *clsdict; |
| 2747 | PyObject *copy_reg; |
| 2748 | PyObject *slotnames; |
| 2749 | |
| 2750 | if (!PyType_Check(cls)) { |
| 2751 | Py_INCREF(Py_None); |
| 2752 | return Py_None; |
| 2753 | } |
| 2754 | |
| 2755 | clsdict = ((PyTypeObject *)cls)->tp_dict; |
| 2756 | slotnames = PyDict_GetItemString(clsdict, "__slotnames__"); |
Armin Rigo | ec862b9 | 2005-09-24 22:58:41 +0000 | [diff] [blame] | 2757 | if (slotnames != NULL && PyList_Check(slotnames)) { |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 2758 | Py_INCREF(slotnames); |
| 2759 | return slotnames; |
| 2760 | } |
| 2761 | |
| 2762 | copy_reg = import_copy_reg(); |
| 2763 | if (copy_reg == NULL) |
| 2764 | return NULL; |
| 2765 | |
| 2766 | slotnames = PyObject_CallMethod(copy_reg, "_slotnames", "O", cls); |
| 2767 | Py_DECREF(copy_reg); |
| 2768 | if (slotnames != NULL && |
| 2769 | slotnames != Py_None && |
| 2770 | !PyList_Check(slotnames)) |
| 2771 | { |
| 2772 | PyErr_SetString(PyExc_TypeError, |
| 2773 | "copy_reg._slotnames didn't return a list or None"); |
| 2774 | Py_DECREF(slotnames); |
| 2775 | slotnames = NULL; |
| 2776 | } |
| 2777 | |
| 2778 | return slotnames; |
| 2779 | } |
| 2780 | |
| 2781 | static PyObject * |
| 2782 | reduce_2(PyObject *obj) |
| 2783 | { |
| 2784 | PyObject *cls, *getnewargs; |
| 2785 | PyObject *args = NULL, *args2 = NULL; |
| 2786 | PyObject *getstate = NULL, *state = NULL, *names = NULL; |
| 2787 | PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL; |
| 2788 | PyObject *copy_reg = NULL, *newobj = NULL, *res = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2789 | Py_ssize_t i, n; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 2790 | |
| 2791 | cls = PyObject_GetAttrString(obj, "__class__"); |
| 2792 | if (cls == NULL) |
| 2793 | return NULL; |
| 2794 | |
| 2795 | getnewargs = PyObject_GetAttrString(obj, "__getnewargs__"); |
| 2796 | if (getnewargs != NULL) { |
| 2797 | args = PyObject_CallObject(getnewargs, NULL); |
| 2798 | Py_DECREF(getnewargs); |
| 2799 | if (args != NULL && !PyTuple_Check(args)) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 2800 | PyErr_Format(PyExc_TypeError, |
| 2801 | "__getnewargs__ should return a tuple, " |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2802 | "not '%.200s'", Py_Type(args)->tp_name); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 2803 | goto end; |
| 2804 | } |
| 2805 | } |
| 2806 | else { |
| 2807 | PyErr_Clear(); |
| 2808 | args = PyTuple_New(0); |
| 2809 | } |
| 2810 | if (args == NULL) |
| 2811 | goto end; |
| 2812 | |
| 2813 | getstate = PyObject_GetAttrString(obj, "__getstate__"); |
| 2814 | if (getstate != NULL) { |
| 2815 | state = PyObject_CallObject(getstate, NULL); |
| 2816 | Py_DECREF(getstate); |
Neal Norwitz | e2fdc61 | 2003-06-08 13:19:58 +0000 | [diff] [blame] | 2817 | if (state == NULL) |
| 2818 | goto end; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 2819 | } |
| 2820 | else { |
Jim Fulton | 8a1a594 | 2004-02-08 04:21:26 +0000 | [diff] [blame] | 2821 | PyErr_Clear(); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 2822 | state = PyObject_GetAttrString(obj, "__dict__"); |
| 2823 | if (state == NULL) { |
| 2824 | PyErr_Clear(); |
| 2825 | state = Py_None; |
| 2826 | Py_INCREF(state); |
| 2827 | } |
| 2828 | names = slotnames(cls); |
| 2829 | if (names == NULL) |
| 2830 | goto end; |
| 2831 | if (names != Py_None) { |
| 2832 | assert(PyList_Check(names)); |
| 2833 | slots = PyDict_New(); |
| 2834 | if (slots == NULL) |
| 2835 | goto end; |
| 2836 | n = 0; |
| 2837 | /* Can't pre-compute the list size; the list |
| 2838 | is stored on the class so accessible to other |
| 2839 | threads, which may be run by DECREF */ |
| 2840 | for (i = 0; i < PyList_GET_SIZE(names); i++) { |
| 2841 | PyObject *name, *value; |
| 2842 | name = PyList_GET_ITEM(names, i); |
| 2843 | value = PyObject_GetAttr(obj, name); |
| 2844 | if (value == NULL) |
| 2845 | PyErr_Clear(); |
| 2846 | else { |
| 2847 | int err = PyDict_SetItem(slots, name, |
| 2848 | value); |
| 2849 | Py_DECREF(value); |
| 2850 | if (err) |
| 2851 | goto end; |
| 2852 | n++; |
| 2853 | } |
| 2854 | } |
| 2855 | if (n) { |
| 2856 | state = Py_BuildValue("(NO)", state, slots); |
| 2857 | if (state == NULL) |
| 2858 | goto end; |
| 2859 | } |
| 2860 | } |
| 2861 | } |
| 2862 | |
| 2863 | if (!PyList_Check(obj)) { |
| 2864 | listitems = Py_None; |
| 2865 | Py_INCREF(listitems); |
| 2866 | } |
| 2867 | else { |
| 2868 | listitems = PyObject_GetIter(obj); |
| 2869 | if (listitems == NULL) |
| 2870 | goto end; |
| 2871 | } |
| 2872 | |
| 2873 | if (!PyDict_Check(obj)) { |
| 2874 | dictitems = Py_None; |
| 2875 | Py_INCREF(dictitems); |
| 2876 | } |
| 2877 | else { |
| 2878 | dictitems = PyObject_CallMethod(obj, "iteritems", ""); |
| 2879 | if (dictitems == NULL) |
| 2880 | goto end; |
| 2881 | } |
| 2882 | |
| 2883 | copy_reg = import_copy_reg(); |
| 2884 | if (copy_reg == NULL) |
| 2885 | goto end; |
| 2886 | newobj = PyObject_GetAttrString(copy_reg, "__newobj__"); |
| 2887 | if (newobj == NULL) |
| 2888 | goto end; |
| 2889 | |
| 2890 | n = PyTuple_GET_SIZE(args); |
| 2891 | args2 = PyTuple_New(n+1); |
| 2892 | if (args2 == NULL) |
| 2893 | goto end; |
| 2894 | PyTuple_SET_ITEM(args2, 0, cls); |
| 2895 | cls = NULL; |
| 2896 | for (i = 0; i < n; i++) { |
| 2897 | PyObject *v = PyTuple_GET_ITEM(args, i); |
| 2898 | Py_INCREF(v); |
| 2899 | PyTuple_SET_ITEM(args2, i+1, v); |
| 2900 | } |
| 2901 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2902 | res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 2903 | |
| 2904 | end: |
| 2905 | Py_XDECREF(cls); |
| 2906 | Py_XDECREF(args); |
| 2907 | Py_XDECREF(args2); |
Jeremy Hylton | d06483c | 2003-04-09 21:01:42 +0000 | [diff] [blame] | 2908 | Py_XDECREF(slots); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 2909 | Py_XDECREF(state); |
| 2910 | Py_XDECREF(names); |
| 2911 | Py_XDECREF(listitems); |
| 2912 | Py_XDECREF(dictitems); |
| 2913 | Py_XDECREF(copy_reg); |
| 2914 | Py_XDECREF(newobj); |
| 2915 | return res; |
| 2916 | } |
| 2917 | |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 2918 | /* |
| 2919 | * There were two problems when object.__reduce__ and object.__reduce_ex__ |
| 2920 | * were implemented in the same function: |
| 2921 | * - trying to pickle an object with a custom __reduce__ method that |
| 2922 | * fell back to object.__reduce__ in certain circumstances led to |
| 2923 | * infinite recursion at Python level and eventual RuntimeError. |
| 2924 | * - Pickling objects that lied about their type by overwriting the |
| 2925 | * __class__ descriptor could lead to infinite recursion at C level |
| 2926 | * and eventual segfault. |
| 2927 | * |
| 2928 | * Because of backwards compatibility, the two methods still have to |
| 2929 | * behave in the same way, even if this is not required by the pickle |
| 2930 | * protocol. This common functionality was moved to the _common_reduce |
| 2931 | * function. |
| 2932 | */ |
| 2933 | static PyObject * |
| 2934 | _common_reduce(PyObject *self, int proto) |
| 2935 | { |
| 2936 | PyObject *copy_reg, *res; |
| 2937 | |
| 2938 | if (proto >= 2) |
| 2939 | return reduce_2(self); |
| 2940 | |
| 2941 | copy_reg = import_copy_reg(); |
| 2942 | if (!copy_reg) |
| 2943 | return NULL; |
| 2944 | |
| 2945 | res = PyEval_CallMethod(copy_reg, "_reduce_ex", "(Oi)", self, proto); |
| 2946 | Py_DECREF(copy_reg); |
| 2947 | |
| 2948 | return res; |
| 2949 | } |
| 2950 | |
| 2951 | static PyObject * |
| 2952 | object_reduce(PyObject *self, PyObject *args) |
| 2953 | { |
| 2954 | int proto = 0; |
| 2955 | |
| 2956 | if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto)) |
| 2957 | return NULL; |
| 2958 | |
| 2959 | return _common_reduce(self, proto); |
| 2960 | } |
| 2961 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 2962 | static PyObject * |
| 2963 | object_reduce_ex(PyObject *self, PyObject *args) |
| 2964 | { |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 2965 | PyObject *reduce, *res; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 2966 | int proto = 0; |
| 2967 | |
| 2968 | if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto)) |
| 2969 | return NULL; |
| 2970 | |
| 2971 | reduce = PyObject_GetAttrString(self, "__reduce__"); |
| 2972 | if (reduce == NULL) |
| 2973 | PyErr_Clear(); |
| 2974 | else { |
| 2975 | PyObject *cls, *clsreduce, *objreduce; |
| 2976 | int override; |
| 2977 | cls = PyObject_GetAttrString(self, "__class__"); |
| 2978 | if (cls == NULL) { |
| 2979 | Py_DECREF(reduce); |
| 2980 | return NULL; |
| 2981 | } |
| 2982 | clsreduce = PyObject_GetAttrString(cls, "__reduce__"); |
| 2983 | Py_DECREF(cls); |
| 2984 | if (clsreduce == NULL) { |
| 2985 | Py_DECREF(reduce); |
| 2986 | return NULL; |
| 2987 | } |
| 2988 | objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict, |
| 2989 | "__reduce__"); |
| 2990 | override = (clsreduce != objreduce); |
| 2991 | Py_DECREF(clsreduce); |
| 2992 | if (override) { |
| 2993 | res = PyObject_CallObject(reduce, NULL); |
| 2994 | Py_DECREF(reduce); |
| 2995 | return res; |
| 2996 | } |
| 2997 | else |
| 2998 | Py_DECREF(reduce); |
| 2999 | } |
| 3000 | |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 3001 | return _common_reduce(self, proto); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | static PyMethodDef object_methods[] = { |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 3005 | {"__reduce_ex__", object_reduce_ex, METH_VARARGS, |
| 3006 | PyDoc_STR("helper for pickle")}, |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 3007 | {"__reduce__", object_reduce, METH_VARARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 3008 | PyDoc_STR("helper for pickle")}, |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3009 | {0} |
| 3010 | }; |
| 3011 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3012 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3013 | PyTypeObject PyBaseObject_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 3014 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3015 | "object", /* tp_name */ |
| 3016 | sizeof(PyObject), /* tp_basicsize */ |
| 3017 | 0, /* tp_itemsize */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 3018 | object_dealloc, /* tp_dealloc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3019 | 0, /* tp_print */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3020 | 0, /* tp_getattr */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3021 | 0, /* tp_setattr */ |
| 3022 | 0, /* tp_compare */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3023 | object_repr, /* tp_repr */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3024 | 0, /* tp_as_number */ |
| 3025 | 0, /* tp_as_sequence */ |
| 3026 | 0, /* tp_as_mapping */ |
Guido van Rossum | 64c06e3 | 2007-11-22 00:55:51 +0000 | [diff] [blame] | 3027 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3028 | 0, /* tp_call */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3029 | object_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3030 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3031 | PyObject_GenericSetAttr, /* tp_setattro */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3032 | 0, /* tp_as_buffer */ |
| 3033 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 3034 | PyDoc_STR("The most base type"), /* tp_doc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3035 | 0, /* tp_traverse */ |
| 3036 | 0, /* tp_clear */ |
| 3037 | 0, /* tp_richcompare */ |
| 3038 | 0, /* tp_weaklistoffset */ |
| 3039 | 0, /* tp_iter */ |
| 3040 | 0, /* tp_iternext */ |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3041 | object_methods, /* tp_methods */ |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3042 | 0, /* tp_members */ |
| 3043 | object_getsets, /* tp_getset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3044 | 0, /* tp_base */ |
| 3045 | 0, /* tp_dict */ |
| 3046 | 0, /* tp_descr_get */ |
| 3047 | 0, /* tp_descr_set */ |
| 3048 | 0, /* tp_dictoffset */ |
| 3049 | object_init, /* tp_init */ |
| 3050 | PyType_GenericAlloc, /* tp_alloc */ |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 3051 | object_new, /* tp_new */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3052 | PyObject_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3053 | }; |
| 3054 | |
| 3055 | |
| 3056 | /* Initialize the __dict__ in a type object */ |
| 3057 | |
| 3058 | static int |
| 3059 | add_methods(PyTypeObject *type, PyMethodDef *meth) |
| 3060 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3061 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3062 | |
| 3063 | for (; meth->ml_name != NULL; meth++) { |
| 3064 | PyObject *descr; |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 3065 | if (PyDict_GetItemString(dict, meth->ml_name) && |
| 3066 | !(meth->ml_flags & METH_COEXIST)) |
| 3067 | continue; |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 3068 | if (meth->ml_flags & METH_CLASS) { |
| 3069 | if (meth->ml_flags & METH_STATIC) { |
| 3070 | PyErr_SetString(PyExc_ValueError, |
| 3071 | "method cannot be both class and static"); |
| 3072 | return -1; |
| 3073 | } |
Tim Peters | bca1cbc | 2002-12-09 22:56:13 +0000 | [diff] [blame] | 3074 | descr = PyDescr_NewClassMethod(type, meth); |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 3075 | } |
| 3076 | else if (meth->ml_flags & METH_STATIC) { |
Guido van Rossum | 9af48ff | 2003-02-11 17:12:46 +0000 | [diff] [blame] | 3077 | PyObject *cfunc = PyCFunction_New(meth, NULL); |
| 3078 | if (cfunc == NULL) |
| 3079 | return -1; |
| 3080 | descr = PyStaticMethod_New(cfunc); |
| 3081 | Py_DECREF(cfunc); |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 3082 | } |
| 3083 | else { |
| 3084 | descr = PyDescr_NewMethod(type, meth); |
| 3085 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3086 | if (descr == NULL) |
| 3087 | return -1; |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 3088 | if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3089 | return -1; |
| 3090 | Py_DECREF(descr); |
| 3091 | } |
| 3092 | return 0; |
| 3093 | } |
| 3094 | |
| 3095 | static int |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 3096 | add_members(PyTypeObject *type, PyMemberDef *memb) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3097 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3098 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3099 | |
| 3100 | for (; memb->name != NULL; memb++) { |
| 3101 | PyObject *descr; |
| 3102 | if (PyDict_GetItemString(dict, memb->name)) |
| 3103 | continue; |
| 3104 | descr = PyDescr_NewMember(type, memb); |
| 3105 | if (descr == NULL) |
| 3106 | return -1; |
| 3107 | if (PyDict_SetItemString(dict, memb->name, descr) < 0) |
| 3108 | return -1; |
| 3109 | Py_DECREF(descr); |
| 3110 | } |
| 3111 | return 0; |
| 3112 | } |
| 3113 | |
| 3114 | static int |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 3115 | add_getset(PyTypeObject *type, PyGetSetDef *gsp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3116 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3117 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3118 | |
| 3119 | for (; gsp->name != NULL; gsp++) { |
| 3120 | PyObject *descr; |
| 3121 | if (PyDict_GetItemString(dict, gsp->name)) |
| 3122 | continue; |
| 3123 | descr = PyDescr_NewGetSet(type, gsp); |
| 3124 | |
| 3125 | if (descr == NULL) |
| 3126 | return -1; |
| 3127 | if (PyDict_SetItemString(dict, gsp->name, descr) < 0) |
| 3128 | return -1; |
| 3129 | Py_DECREF(descr); |
| 3130 | } |
| 3131 | return 0; |
| 3132 | } |
| 3133 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3134 | static void |
| 3135 | inherit_special(PyTypeObject *type, PyTypeObject *base) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3136 | { |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 3137 | Py_ssize_t oldsize, newsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3138 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3139 | /* Special flag magic */ |
| 3140 | if (!type->tp_as_buffer && base->tp_as_buffer) { |
| 3141 | type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER; |
| 3142 | type->tp_flags |= |
| 3143 | base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER; |
| 3144 | } |
| 3145 | if (!type->tp_as_sequence && base->tp_as_sequence) { |
| 3146 | type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN; |
| 3147 | type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN; |
| 3148 | } |
| 3149 | if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) != |
| 3150 | (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) { |
| 3151 | if ((!type->tp_as_number && base->tp_as_number) || |
| 3152 | (!type->tp_as_sequence && base->tp_as_sequence)) { |
| 3153 | type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS; |
| 3154 | if (!type->tp_as_number && !type->tp_as_sequence) { |
| 3155 | type->tp_flags |= base->tp_flags & |
| 3156 | Py_TPFLAGS_HAVE_INPLACEOPS; |
| 3157 | } |
| 3158 | } |
| 3159 | /* Wow */ |
| 3160 | } |
| 3161 | if (!type->tp_as_number && base->tp_as_number) { |
| 3162 | type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES; |
| 3163 | type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES; |
| 3164 | } |
| 3165 | |
| 3166 | /* Copying basicsize is connected to the GC flags */ |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 3167 | oldsize = base->tp_basicsize; |
| 3168 | newsize = type->tp_basicsize ? type->tp_basicsize : oldsize; |
| 3169 | if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3170 | (base->tp_flags & Py_TPFLAGS_HAVE_GC) && |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3171 | (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) && |
| 3172 | (!type->tp_traverse && !type->tp_clear)) { |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 3173 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3174 | if (type->tp_traverse == NULL) |
| 3175 | type->tp_traverse = base->tp_traverse; |
| 3176 | if (type->tp_clear == NULL) |
| 3177 | type->tp_clear = base->tp_clear; |
| 3178 | } |
| 3179 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
Guido van Rossum | f884b74 | 2001-12-17 17:14:22 +0000 | [diff] [blame] | 3180 | /* The condition below could use some explanation. |
| 3181 | It appears that tp_new is not inherited for static types |
| 3182 | whose base class is 'object'; this seems to be a precaution |
| 3183 | so that old extension types don't suddenly become |
| 3184 | callable (object.__new__ wouldn't insure the invariants |
| 3185 | that the extension type's own factory function ensures). |
| 3186 | Heap types, of course, are under our control, so they do |
| 3187 | inherit tp_new; static extension types that specify some |
| 3188 | other built-in type as the default are considered |
| 3189 | new-style-aware so they also inherit object.__new__. */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3190 | if (base != &PyBaseObject_Type || |
| 3191 | (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 3192 | if (type->tp_new == NULL) |
| 3193 | type->tp_new = base->tp_new; |
| 3194 | } |
| 3195 | } |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 3196 | type->tp_basicsize = newsize; |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3197 | |
| 3198 | /* Copy other non-function slots */ |
| 3199 | |
| 3200 | #undef COPYVAL |
| 3201 | #define COPYVAL(SLOT) \ |
| 3202 | if (type->SLOT == 0) type->SLOT = base->SLOT |
| 3203 | |
| 3204 | COPYVAL(tp_itemsize); |
| 3205 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) { |
| 3206 | COPYVAL(tp_weaklistoffset); |
| 3207 | } |
| 3208 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
| 3209 | COPYVAL(tp_dictoffset); |
| 3210 | } |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 3211 | |
| 3212 | /* Setup fast subclass flags */ |
| 3213 | if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException)) |
| 3214 | type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS; |
| 3215 | else if (PyType_IsSubtype(base, &PyType_Type)) |
| 3216 | type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS; |
| 3217 | else if (PyType_IsSubtype(base, &PyInt_Type)) |
| 3218 | type->tp_flags |= Py_TPFLAGS_INT_SUBCLASS; |
| 3219 | else if (PyType_IsSubtype(base, &PyLong_Type)) |
| 3220 | type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS; |
| 3221 | else if (PyType_IsSubtype(base, &PyString_Type)) |
| 3222 | type->tp_flags |= Py_TPFLAGS_STRING_SUBCLASS; |
| 3223 | else if (PyType_IsSubtype(base, &PyUnicode_Type)) |
| 3224 | type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS; |
| 3225 | else if (PyType_IsSubtype(base, &PyTuple_Type)) |
| 3226 | type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS; |
| 3227 | else if (PyType_IsSubtype(base, &PyList_Type)) |
| 3228 | type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS; |
| 3229 | else if (PyType_IsSubtype(base, &PyDict_Type)) |
| 3230 | type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3231 | } |
| 3232 | |
Guido van Rossum | 64c06e3 | 2007-11-22 00:55:51 +0000 | [diff] [blame] | 3233 | /* Map rich comparison operators to their __xx__ namesakes */ |
| 3234 | static char *name_op[] = { |
| 3235 | "__lt__", |
| 3236 | "__le__", |
| 3237 | "__eq__", |
| 3238 | "__ne__", |
| 3239 | "__gt__", |
| 3240 | "__ge__", |
| 3241 | "__cmp__", |
| 3242 | /* These are only for overrides_hash(): */ |
| 3243 | "__hash__", |
| 3244 | }; |
| 3245 | |
| 3246 | static int |
| 3247 | overrides_hash(PyTypeObject *type) |
| 3248 | { |
| 3249 | int i; |
| 3250 | PyObject *dict = type->tp_dict; |
| 3251 | |
| 3252 | assert(dict != NULL); |
| 3253 | for (i = 0; i < 8; i++) { |
| 3254 | if (PyDict_GetItemString(dict, name_op[i]) != NULL) |
| 3255 | return 1; |
| 3256 | } |
| 3257 | return 0; |
| 3258 | } |
| 3259 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3260 | static void |
| 3261 | inherit_slots(PyTypeObject *type, PyTypeObject *base) |
| 3262 | { |
| 3263 | PyTypeObject *basebase; |
| 3264 | |
| 3265 | #undef SLOTDEFINED |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3266 | #undef COPYSLOT |
| 3267 | #undef COPYNUM |
| 3268 | #undef COPYSEQ |
| 3269 | #undef COPYMAP |
Guido van Rossum | 5af588b | 2001-10-12 14:13:21 +0000 | [diff] [blame] | 3270 | #undef COPYBUF |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3271 | |
| 3272 | #define SLOTDEFINED(SLOT) \ |
| 3273 | (base->SLOT != 0 && \ |
| 3274 | (basebase == NULL || base->SLOT != basebase->SLOT)) |
| 3275 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3276 | #define COPYSLOT(SLOT) \ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3277 | if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3278 | |
| 3279 | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT) |
| 3280 | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT) |
| 3281 | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT) |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3282 | #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3283 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3284 | /* This won't inherit indirect slots (from tp_as_number etc.) |
| 3285 | if type doesn't provide the space. */ |
| 3286 | |
| 3287 | if (type->tp_as_number != NULL && base->tp_as_number != NULL) { |
| 3288 | basebase = base->tp_base; |
| 3289 | if (basebase->tp_as_number == NULL) |
| 3290 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3291 | COPYNUM(nb_add); |
| 3292 | COPYNUM(nb_subtract); |
| 3293 | COPYNUM(nb_multiply); |
| 3294 | COPYNUM(nb_divide); |
| 3295 | COPYNUM(nb_remainder); |
| 3296 | COPYNUM(nb_divmod); |
| 3297 | COPYNUM(nb_power); |
| 3298 | COPYNUM(nb_negative); |
| 3299 | COPYNUM(nb_positive); |
| 3300 | COPYNUM(nb_absolute); |
| 3301 | COPYNUM(nb_nonzero); |
| 3302 | COPYNUM(nb_invert); |
| 3303 | COPYNUM(nb_lshift); |
| 3304 | COPYNUM(nb_rshift); |
| 3305 | COPYNUM(nb_and); |
| 3306 | COPYNUM(nb_xor); |
| 3307 | COPYNUM(nb_or); |
| 3308 | COPYNUM(nb_coerce); |
| 3309 | COPYNUM(nb_int); |
| 3310 | COPYNUM(nb_long); |
| 3311 | COPYNUM(nb_float); |
| 3312 | COPYNUM(nb_oct); |
| 3313 | COPYNUM(nb_hex); |
| 3314 | COPYNUM(nb_inplace_add); |
| 3315 | COPYNUM(nb_inplace_subtract); |
| 3316 | COPYNUM(nb_inplace_multiply); |
| 3317 | COPYNUM(nb_inplace_divide); |
| 3318 | COPYNUM(nb_inplace_remainder); |
| 3319 | COPYNUM(nb_inplace_power); |
| 3320 | COPYNUM(nb_inplace_lshift); |
| 3321 | COPYNUM(nb_inplace_rshift); |
| 3322 | COPYNUM(nb_inplace_and); |
| 3323 | COPYNUM(nb_inplace_xor); |
| 3324 | COPYNUM(nb_inplace_or); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3325 | if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) { |
| 3326 | COPYNUM(nb_true_divide); |
| 3327 | COPYNUM(nb_floor_divide); |
| 3328 | COPYNUM(nb_inplace_true_divide); |
| 3329 | COPYNUM(nb_inplace_floor_divide); |
| 3330 | } |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 3331 | if (base->tp_flags & Py_TPFLAGS_HAVE_INDEX) { |
| 3332 | COPYNUM(nb_index); |
| 3333 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3334 | } |
| 3335 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3336 | if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) { |
| 3337 | basebase = base->tp_base; |
| 3338 | if (basebase->tp_as_sequence == NULL) |
| 3339 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3340 | COPYSEQ(sq_length); |
| 3341 | COPYSEQ(sq_concat); |
| 3342 | COPYSEQ(sq_repeat); |
| 3343 | COPYSEQ(sq_item); |
| 3344 | COPYSEQ(sq_slice); |
| 3345 | COPYSEQ(sq_ass_item); |
| 3346 | COPYSEQ(sq_ass_slice); |
| 3347 | COPYSEQ(sq_contains); |
| 3348 | COPYSEQ(sq_inplace_concat); |
| 3349 | COPYSEQ(sq_inplace_repeat); |
| 3350 | } |
| 3351 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3352 | if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) { |
| 3353 | basebase = base->tp_base; |
| 3354 | if (basebase->tp_as_mapping == NULL) |
| 3355 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3356 | COPYMAP(mp_length); |
| 3357 | COPYMAP(mp_subscript); |
| 3358 | COPYMAP(mp_ass_subscript); |
| 3359 | } |
| 3360 | |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3361 | if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) { |
| 3362 | basebase = base->tp_base; |
| 3363 | if (basebase->tp_as_buffer == NULL) |
| 3364 | basebase = NULL; |
| 3365 | COPYBUF(bf_getreadbuffer); |
| 3366 | COPYBUF(bf_getwritebuffer); |
| 3367 | COPYBUF(bf_getsegcount); |
| 3368 | COPYBUF(bf_getcharbuffer); |
| 3369 | } |
| 3370 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3371 | basebase = base->tp_base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3372 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3373 | COPYSLOT(tp_dealloc); |
| 3374 | COPYSLOT(tp_print); |
| 3375 | if (type->tp_getattr == NULL && type->tp_getattro == NULL) { |
| 3376 | type->tp_getattr = base->tp_getattr; |
| 3377 | type->tp_getattro = base->tp_getattro; |
| 3378 | } |
| 3379 | if (type->tp_setattr == NULL && type->tp_setattro == NULL) { |
| 3380 | type->tp_setattr = base->tp_setattr; |
| 3381 | type->tp_setattro = base->tp_setattro; |
| 3382 | } |
| 3383 | /* tp_compare see tp_richcompare */ |
| 3384 | COPYSLOT(tp_repr); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3385 | /* tp_hash see tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3386 | COPYSLOT(tp_call); |
| 3387 | COPYSLOT(tp_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3388 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3389 | if (type->tp_compare == NULL && |
| 3390 | type->tp_richcompare == NULL && |
Guido van Rossum | 64c06e3 | 2007-11-22 00:55:51 +0000 | [diff] [blame] | 3391 | type->tp_hash == NULL && |
| 3392 | !overrides_hash(type)) |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3393 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3394 | type->tp_compare = base->tp_compare; |
| 3395 | type->tp_richcompare = base->tp_richcompare; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3396 | type->tp_hash = base->tp_hash; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3397 | } |
| 3398 | } |
| 3399 | else { |
| 3400 | COPYSLOT(tp_compare); |
| 3401 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3402 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) { |
| 3403 | COPYSLOT(tp_iter); |
| 3404 | COPYSLOT(tp_iternext); |
| 3405 | } |
| 3406 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
| 3407 | COPYSLOT(tp_descr_get); |
| 3408 | COPYSLOT(tp_descr_set); |
| 3409 | COPYSLOT(tp_dictoffset); |
| 3410 | COPYSLOT(tp_init); |
| 3411 | COPYSLOT(tp_alloc); |
Guido van Rossum | cc8fe04 | 2002-04-05 17:10:16 +0000 | [diff] [blame] | 3412 | COPYSLOT(tp_is_gc); |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 3413 | if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 3414 | (base->tp_flags & Py_TPFLAGS_HAVE_GC)) { |
| 3415 | /* They agree about gc. */ |
| 3416 | COPYSLOT(tp_free); |
| 3417 | } |
| 3418 | else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3419 | type->tp_free == NULL && |
| 3420 | base->tp_free == _PyObject_Del) { |
| 3421 | /* A bit of magic to plug in the correct default |
| 3422 | * tp_free function when a derived class adds gc, |
| 3423 | * didn't define tp_free, and the base uses the |
| 3424 | * default non-gc tp_free. |
| 3425 | */ |
| 3426 | type->tp_free = PyObject_GC_Del; |
| 3427 | } |
| 3428 | /* else they didn't agree about gc, and there isn't something |
| 3429 | * obvious to be done -- the type is on its own. |
| 3430 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3431 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3432 | } |
| 3433 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 3434 | static int add_operators(PyTypeObject *); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3435 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3436 | int |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 3437 | PyType_Ready(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3438 | { |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 3439 | PyObject *dict, *bases; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3440 | PyTypeObject *base; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3441 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3442 | |
Guido van Rossum | cab0580 | 2002-06-10 15:29:03 +0000 | [diff] [blame] | 3443 | if (type->tp_flags & Py_TPFLAGS_READY) { |
| 3444 | assert(type->tp_dict != NULL); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3445 | return 0; |
Guido van Rossum | cab0580 | 2002-06-10 15:29:03 +0000 | [diff] [blame] | 3446 | } |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3447 | assert((type->tp_flags & Py_TPFLAGS_READYING) == 0); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3448 | |
| 3449 | type->tp_flags |= Py_TPFLAGS_READYING; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3450 | |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 3451 | #ifdef Py_TRACE_REFS |
| 3452 | /* PyType_Ready is the closest thing we have to a choke point |
| 3453 | * for type objects, so is the best place I can think of to try |
| 3454 | * to get type objects into the doubly-linked list of all objects. |
| 3455 | * Still, not all type objects go thru PyType_Ready. |
| 3456 | */ |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 3457 | _Py_AddToAllObjects((PyObject *)type, 0); |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 3458 | #endif |
| 3459 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3460 | /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
| 3461 | base = type->tp_base; |
Martin v. Löwis | bf60875 | 2004-08-18 13:16:54 +0000 | [diff] [blame] | 3462 | if (base == NULL && type != &PyBaseObject_Type) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3463 | base = type->tp_base = &PyBaseObject_Type; |
Martin v. Löwis | bf60875 | 2004-08-18 13:16:54 +0000 | [diff] [blame] | 3464 | Py_INCREF(base); |
| 3465 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3466 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3467 | /* Now the only way base can still be NULL is if type is |
| 3468 | * &PyBaseObject_Type. |
| 3469 | */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 3470 | |
Guido van Rossum | 323a9cf | 2002-08-14 17:26:30 +0000 | [diff] [blame] | 3471 | /* Initialize the base class */ |
| 3472 | if (base && base->tp_dict == NULL) { |
| 3473 | if (PyType_Ready(base) < 0) |
| 3474 | goto error; |
| 3475 | } |
| 3476 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3477 | /* Initialize ob_type if NULL. This means extensions that want to be |
Guido van Rossum | 0986d82 | 2002-04-08 01:38:42 +0000 | [diff] [blame] | 3478 | compilable separately on Windows can call PyType_Ready() instead of |
| 3479 | initializing the ob_type field of their type objects. */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3480 | /* The test for base != NULL is really unnecessary, since base is only |
| 3481 | NULL when type is &PyBaseObject_Type, and we know its ob_type is |
| 3482 | not NULL (it's initialized to &PyType_Type). But coverity doesn't |
| 3483 | know that. */ |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 3484 | if (Py_Type(type) == NULL && base != NULL) |
| 3485 | Py_Type(type) = Py_Type(base); |
Guido van Rossum | 0986d82 | 2002-04-08 01:38:42 +0000 | [diff] [blame] | 3486 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3487 | /* Initialize tp_bases */ |
| 3488 | bases = type->tp_bases; |
| 3489 | if (bases == NULL) { |
| 3490 | if (base == NULL) |
| 3491 | bases = PyTuple_New(0); |
| 3492 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3493 | bases = PyTuple_Pack(1, base); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3494 | if (bases == NULL) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3495 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3496 | type->tp_bases = bases; |
| 3497 | } |
| 3498 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3499 | /* Initialize tp_dict */ |
| 3500 | dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3501 | if (dict == NULL) { |
| 3502 | dict = PyDict_New(); |
| 3503 | if (dict == NULL) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3504 | goto error; |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3505 | type->tp_dict = dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3506 | } |
| 3507 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3508 | /* Add type-specific descriptors to tp_dict */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3509 | if (add_operators(type) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3510 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3511 | if (type->tp_methods != NULL) { |
| 3512 | if (add_methods(type, type->tp_methods) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3513 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3514 | } |
| 3515 | if (type->tp_members != NULL) { |
| 3516 | if (add_members(type, type->tp_members) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3517 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3518 | } |
| 3519 | if (type->tp_getset != NULL) { |
| 3520 | if (add_getset(type, type->tp_getset) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3521 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3522 | } |
| 3523 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3524 | /* Calculate method resolution order */ |
| 3525 | if (mro_internal(type) < 0) { |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3526 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3527 | } |
| 3528 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3529 | /* Inherit special flags from dominant base */ |
| 3530 | if (type->tp_base != NULL) |
| 3531 | inherit_special(type, type->tp_base); |
| 3532 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3533 | /* Initialize tp_dict properly */ |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 3534 | bases = type->tp_mro; |
| 3535 | assert(bases != NULL); |
| 3536 | assert(PyTuple_Check(bases)); |
| 3537 | n = PyTuple_GET_SIZE(bases); |
| 3538 | for (i = 1; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 3539 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 3540 | if (PyType_Check(b)) |
| 3541 | inherit_slots(type, (PyTypeObject *)b); |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3542 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3543 | |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 3544 | /* Sanity check for tp_free. */ |
| 3545 | if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) && |
| 3546 | (type->tp_free == NULL || type->tp_free == PyObject_Del)) { |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3547 | /* This base class needs to call tp_free, but doesn't have |
| 3548 | * one, or its tp_free is for non-gc'ed objects. |
| 3549 | */ |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 3550 | PyErr_Format(PyExc_TypeError, "type '%.100s' participates in " |
| 3551 | "gc and is a base type but has inappropriate " |
| 3552 | "tp_free slot", |
| 3553 | type->tp_name); |
| 3554 | goto error; |
| 3555 | } |
| 3556 | |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 3557 | /* if the type dictionary doesn't contain a __doc__, set it from |
| 3558 | the tp_doc slot. |
| 3559 | */ |
| 3560 | if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) { |
| 3561 | if (type->tp_doc != NULL) { |
| 3562 | PyObject *doc = PyString_FromString(type->tp_doc); |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 3563 | if (doc == NULL) |
| 3564 | goto error; |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 3565 | PyDict_SetItemString(type->tp_dict, "__doc__", doc); |
| 3566 | Py_DECREF(doc); |
| 3567 | } else { |
Guido van Rossum | d464107 | 2002-04-03 02:13:37 +0000 | [diff] [blame] | 3568 | PyDict_SetItemString(type->tp_dict, |
| 3569 | "__doc__", Py_None); |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 3570 | } |
| 3571 | } |
| 3572 | |
Guido van Rossum | 64c06e3 | 2007-11-22 00:55:51 +0000 | [diff] [blame] | 3573 | /* Hack for tp_hash and __hash__. |
| 3574 | If after all that, tp_hash is still NULL, and __hash__ is not in |
| 3575 | tp_dict, set tp_dict['__hash__'] equal to None. |
| 3576 | This signals that __hash__ is not inherited. |
| 3577 | */ |
| 3578 | if (type->tp_hash == NULL && |
| 3579 | PyDict_GetItemString(type->tp_dict, "__hash__") == NULL && |
| 3580 | PyDict_SetItemString(type->tp_dict, "__hash__", Py_None) < 0) |
| 3581 | { |
| 3582 | goto error; |
| 3583 | } |
| 3584 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3585 | /* Some more special stuff */ |
| 3586 | base = type->tp_base; |
| 3587 | if (base != NULL) { |
| 3588 | if (type->tp_as_number == NULL) |
| 3589 | type->tp_as_number = base->tp_as_number; |
| 3590 | if (type->tp_as_sequence == NULL) |
| 3591 | type->tp_as_sequence = base->tp_as_sequence; |
| 3592 | if (type->tp_as_mapping == NULL) |
| 3593 | type->tp_as_mapping = base->tp_as_mapping; |
Guido van Rossum | eea4718 | 2003-02-11 20:39:59 +0000 | [diff] [blame] | 3594 | if (type->tp_as_buffer == NULL) |
| 3595 | type->tp_as_buffer = base->tp_as_buffer; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3596 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3597 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3598 | /* Link into each base class's list of subclasses */ |
| 3599 | bases = type->tp_bases; |
| 3600 | n = PyTuple_GET_SIZE(bases); |
| 3601 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 3602 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 3603 | if (PyType_Check(b) && |
| 3604 | add_subclass((PyTypeObject *)b, type) < 0) |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3605 | goto error; |
| 3606 | } |
| 3607 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3608 | /* All done -- set the ready flag */ |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3609 | assert(type->tp_dict != NULL); |
| 3610 | type->tp_flags = |
| 3611 | (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3612 | return 0; |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3613 | |
| 3614 | error: |
| 3615 | type->tp_flags &= ~Py_TPFLAGS_READYING; |
| 3616 | return -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3617 | } |
| 3618 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3619 | static int |
| 3620 | add_subclass(PyTypeObject *base, PyTypeObject *type) |
| 3621 | { |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 3622 | Py_ssize_t i; |
| 3623 | int result; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3624 | PyObject *list, *ref, *newobj; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3625 | |
| 3626 | list = base->tp_subclasses; |
| 3627 | if (list == NULL) { |
| 3628 | base->tp_subclasses = list = PyList_New(0); |
| 3629 | if (list == NULL) |
| 3630 | return -1; |
| 3631 | } |
| 3632 | assert(PyList_Check(list)); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3633 | newobj = PyWeakref_NewRef((PyObject *)type, NULL); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3634 | i = PyList_GET_SIZE(list); |
| 3635 | while (--i >= 0) { |
| 3636 | ref = PyList_GET_ITEM(list, i); |
| 3637 | assert(PyWeakref_CheckRef(ref)); |
Guido van Rossum | 3930bc3 | 2002-10-18 13:51:49 +0000 | [diff] [blame] | 3638 | if (PyWeakref_GET_OBJECT(ref) == Py_None) |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3639 | return PyList_SetItem(list, i, newobj); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3640 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3641 | result = PyList_Append(list, newobj); |
| 3642 | Py_DECREF(newobj); |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 3643 | return result; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3644 | } |
| 3645 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3646 | static void |
| 3647 | remove_subclass(PyTypeObject *base, PyTypeObject *type) |
| 3648 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3649 | Py_ssize_t i; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3650 | PyObject *list, *ref; |
| 3651 | |
| 3652 | list = base->tp_subclasses; |
| 3653 | if (list == NULL) { |
| 3654 | return; |
| 3655 | } |
| 3656 | assert(PyList_Check(list)); |
| 3657 | i = PyList_GET_SIZE(list); |
| 3658 | while (--i >= 0) { |
| 3659 | ref = PyList_GET_ITEM(list, i); |
| 3660 | assert(PyWeakref_CheckRef(ref)); |
| 3661 | if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) { |
| 3662 | /* this can't fail, right? */ |
| 3663 | PySequence_DelItem(list, i); |
| 3664 | return; |
| 3665 | } |
| 3666 | } |
| 3667 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3668 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3669 | static int |
| 3670 | check_num_args(PyObject *ob, int n) |
| 3671 | { |
| 3672 | if (!PyTuple_CheckExact(ob)) { |
| 3673 | PyErr_SetString(PyExc_SystemError, |
| 3674 | "PyArg_UnpackTuple() argument list is not a tuple"); |
| 3675 | return 0; |
| 3676 | } |
| 3677 | if (n == PyTuple_GET_SIZE(ob)) |
| 3678 | return 1; |
| 3679 | PyErr_Format( |
| 3680 | PyExc_TypeError, |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3681 | "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob)); |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3682 | return 0; |
| 3683 | } |
| 3684 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3685 | /* Generic wrappers for overloadable 'operators' such as __getitem__ */ |
| 3686 | |
| 3687 | /* There's a wrapper *function* for each distinct function typedef used |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3688 | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3689 | wrapper *table* for each distinct operation (e.g. __len__, __add__). |
| 3690 | Most tables have only one entry; the tables for binary operators have two |
| 3691 | entries, one regular and one with reversed arguments. */ |
| 3692 | |
| 3693 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3694 | wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3695 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3696 | lenfunc func = (lenfunc)wrapped; |
| 3697 | Py_ssize_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3698 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3699 | if (!check_num_args(args, 0)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3700 | return NULL; |
| 3701 | res = (*func)(self); |
| 3702 | if (res == -1 && PyErr_Occurred()) |
| 3703 | return NULL; |
| 3704 | return PyInt_FromLong((long)res); |
| 3705 | } |
| 3706 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3707 | static PyObject * |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 3708 | wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped) |
| 3709 | { |
| 3710 | inquiry func = (inquiry)wrapped; |
| 3711 | int res; |
| 3712 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3713 | if (!check_num_args(args, 0)) |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 3714 | return NULL; |
| 3715 | res = (*func)(self); |
| 3716 | if (res == -1 && PyErr_Occurred()) |
| 3717 | return NULL; |
| 3718 | return PyBool_FromLong((long)res); |
| 3719 | } |
| 3720 | |
| 3721 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3722 | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 3723 | { |
| 3724 | binaryfunc func = (binaryfunc)wrapped; |
| 3725 | PyObject *other; |
| 3726 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3727 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3728 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3729 | other = PyTuple_GET_ITEM(args, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3730 | return (*func)(self, other); |
| 3731 | } |
| 3732 | |
| 3733 | static PyObject * |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 3734 | wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped) |
| 3735 | { |
| 3736 | binaryfunc func = (binaryfunc)wrapped; |
| 3737 | PyObject *other; |
| 3738 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3739 | if (!check_num_args(args, 1)) |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 3740 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3741 | other = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 3742 | if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) && |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3743 | !PyType_IsSubtype(other->ob_type, self->ob_type)) { |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 3744 | Py_INCREF(Py_NotImplemented); |
| 3745 | return Py_NotImplemented; |
| 3746 | } |
| 3747 | return (*func)(self, other); |
| 3748 | } |
| 3749 | |
| 3750 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3751 | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 3752 | { |
| 3753 | binaryfunc func = (binaryfunc)wrapped; |
| 3754 | PyObject *other; |
| 3755 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3756 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3757 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3758 | other = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 3759 | if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) && |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3760 | !PyType_IsSubtype(other->ob_type, self->ob_type)) { |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 3761 | Py_INCREF(Py_NotImplemented); |
| 3762 | return Py_NotImplemented; |
| 3763 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3764 | return (*func)(other, self); |
| 3765 | } |
| 3766 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 3767 | static PyObject * |
| 3768 | wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped) |
| 3769 | { |
| 3770 | coercion func = (coercion)wrapped; |
| 3771 | PyObject *other, *res; |
| 3772 | int ok; |
| 3773 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3774 | if (!check_num_args(args, 1)) |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 3775 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3776 | other = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 3777 | ok = func(&self, &other); |
| 3778 | if (ok < 0) |
| 3779 | return NULL; |
| 3780 | if (ok > 0) { |
| 3781 | Py_INCREF(Py_NotImplemented); |
| 3782 | return Py_NotImplemented; |
| 3783 | } |
| 3784 | res = PyTuple_New(2); |
| 3785 | if (res == NULL) { |
| 3786 | Py_DECREF(self); |
| 3787 | Py_DECREF(other); |
| 3788 | return NULL; |
| 3789 | } |
| 3790 | PyTuple_SET_ITEM(res, 0, self); |
| 3791 | PyTuple_SET_ITEM(res, 1, other); |
| 3792 | return res; |
| 3793 | } |
| 3794 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3795 | static PyObject * |
| 3796 | wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 3797 | { |
| 3798 | ternaryfunc func = (ternaryfunc)wrapped; |
| 3799 | PyObject *other; |
| 3800 | PyObject *third = Py_None; |
| 3801 | |
| 3802 | /* Note: This wrapper only works for __pow__() */ |
| 3803 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 3804 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3805 | return NULL; |
| 3806 | return (*func)(self, other, third); |
| 3807 | } |
| 3808 | |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 3809 | static PyObject * |
| 3810 | wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 3811 | { |
| 3812 | ternaryfunc func = (ternaryfunc)wrapped; |
| 3813 | PyObject *other; |
| 3814 | PyObject *third = Py_None; |
| 3815 | |
| 3816 | /* Note: This wrapper only works for __pow__() */ |
| 3817 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 3818 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 3819 | return NULL; |
| 3820 | return (*func)(other, self, third); |
| 3821 | } |
| 3822 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3823 | static PyObject * |
| 3824 | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 3825 | { |
| 3826 | unaryfunc func = (unaryfunc)wrapped; |
| 3827 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3828 | if (!check_num_args(args, 0)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3829 | return NULL; |
| 3830 | return (*func)(self); |
| 3831 | } |
| 3832 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3833 | static PyObject * |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 3834 | wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3835 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3836 | ssizeargfunc func = (ssizeargfunc)wrapped; |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 3837 | PyObject* o; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3838 | Py_ssize_t i; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3839 | |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 3840 | if (!PyArg_UnpackTuple(args, "", 1, 1, &o)) |
| 3841 | return NULL; |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 3842 | i = PyNumber_AsSsize_t(o, PyExc_OverflowError); |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 3843 | if (i == -1 && PyErr_Occurred()) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3844 | return NULL; |
| 3845 | return (*func)(self, i); |
| 3846 | } |
| 3847 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3848 | static Py_ssize_t |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3849 | getindex(PyObject *self, PyObject *arg) |
| 3850 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3851 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3852 | |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 3853 | i = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3854 | if (i == -1 && PyErr_Occurred()) |
| 3855 | return -1; |
| 3856 | if (i < 0) { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 3857 | PySequenceMethods *sq = Py_Type(self)->tp_as_sequence; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3858 | if (sq && sq->sq_length) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3859 | Py_ssize_t n = (*sq->sq_length)(self); |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3860 | if (n < 0) |
| 3861 | return -1; |
| 3862 | i += n; |
| 3863 | } |
| 3864 | } |
| 3865 | return i; |
| 3866 | } |
| 3867 | |
| 3868 | static PyObject * |
| 3869 | wrap_sq_item(PyObject *self, PyObject *args, void *wrapped) |
| 3870 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3871 | ssizeargfunc func = (ssizeargfunc)wrapped; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3872 | PyObject *arg; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3873 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3874 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 3875 | if (PyTuple_GET_SIZE(args) == 1) { |
| 3876 | arg = PyTuple_GET_ITEM(args, 0); |
| 3877 | i = getindex(self, arg); |
| 3878 | if (i == -1 && PyErr_Occurred()) |
| 3879 | return NULL; |
| 3880 | return (*func)(self, i); |
| 3881 | } |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3882 | check_num_args(args, 1); |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 3883 | assert(PyErr_Occurred()); |
| 3884 | return NULL; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3887 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3888 | wrap_ssizessizeargfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3889 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3890 | ssizessizeargfunc func = (ssizessizeargfunc)wrapped; |
| 3891 | Py_ssize_t i, j; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3892 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3893 | if (!PyArg_ParseTuple(args, "nn", &i, &j)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3894 | return NULL; |
| 3895 | return (*func)(self, i, j); |
| 3896 | } |
| 3897 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3898 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3899 | wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3900 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3901 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 3902 | Py_ssize_t i; |
| 3903 | int res; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3904 | PyObject *arg, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3905 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 3906 | if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value)) |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3907 | return NULL; |
| 3908 | i = getindex(self, arg); |
| 3909 | if (i == -1 && PyErr_Occurred()) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3910 | return NULL; |
| 3911 | res = (*func)(self, i, value); |
| 3912 | if (res == -1 && PyErr_Occurred()) |
| 3913 | return NULL; |
| 3914 | Py_INCREF(Py_None); |
| 3915 | return Py_None; |
| 3916 | } |
| 3917 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 3918 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3919 | wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 3920 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3921 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 3922 | Py_ssize_t i; |
| 3923 | int res; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3924 | PyObject *arg; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 3925 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3926 | if (!check_num_args(args, 1)) |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3927 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3928 | arg = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 3929 | i = getindex(self, arg); |
| 3930 | if (i == -1 && PyErr_Occurred()) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 3931 | return NULL; |
| 3932 | res = (*func)(self, i, NULL); |
| 3933 | if (res == -1 && PyErr_Occurred()) |
| 3934 | return NULL; |
| 3935 | Py_INCREF(Py_None); |
| 3936 | return Py_None; |
| 3937 | } |
| 3938 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3939 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3940 | wrap_ssizessizeobjargproc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3941 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3942 | ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped; |
| 3943 | Py_ssize_t i, j; |
| 3944 | int res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3945 | PyObject *value; |
| 3946 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3947 | if (!PyArg_ParseTuple(args, "nnO", &i, &j, &value)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3948 | return NULL; |
| 3949 | res = (*func)(self, i, j, value); |
| 3950 | if (res == -1 && PyErr_Occurred()) |
| 3951 | return NULL; |
| 3952 | Py_INCREF(Py_None); |
| 3953 | return Py_None; |
| 3954 | } |
| 3955 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3956 | static PyObject * |
| 3957 | wrap_delslice(PyObject *self, PyObject *args, void *wrapped) |
| 3958 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3959 | ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped; |
| 3960 | Py_ssize_t i, j; |
| 3961 | int res; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3962 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3963 | if (!PyArg_ParseTuple(args, "nn", &i, &j)) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3964 | return NULL; |
| 3965 | res = (*func)(self, i, j, NULL); |
| 3966 | if (res == -1 && PyErr_Occurred()) |
| 3967 | return NULL; |
| 3968 | Py_INCREF(Py_None); |
| 3969 | return Py_None; |
| 3970 | } |
| 3971 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3972 | /* XXX objobjproc is a misnomer; should be objargpred */ |
| 3973 | static PyObject * |
| 3974 | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) |
| 3975 | { |
| 3976 | objobjproc func = (objobjproc)wrapped; |
| 3977 | int res; |
Guido van Rossum | 22c3dda | 2003-10-09 03:46:35 +0000 | [diff] [blame] | 3978 | PyObject *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3979 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3980 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3981 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3982 | value = PyTuple_GET_ITEM(args, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3983 | res = (*func)(self, value); |
| 3984 | if (res == -1 && PyErr_Occurred()) |
| 3985 | return NULL; |
Guido van Rossum | 22c3dda | 2003-10-09 03:46:35 +0000 | [diff] [blame] | 3986 | else |
| 3987 | return PyBool_FromLong(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3988 | } |
| 3989 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3990 | static PyObject * |
| 3991 | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 3992 | { |
| 3993 | objobjargproc func = (objobjargproc)wrapped; |
| 3994 | int res; |
| 3995 | PyObject *key, *value; |
| 3996 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 3997 | if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3998 | return NULL; |
| 3999 | res = (*func)(self, key, value); |
| 4000 | if (res == -1 && PyErr_Occurred()) |
| 4001 | return NULL; |
| 4002 | Py_INCREF(Py_None); |
| 4003 | return Py_None; |
| 4004 | } |
| 4005 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4006 | static PyObject * |
| 4007 | wrap_delitem(PyObject *self, PyObject *args, void *wrapped) |
| 4008 | { |
| 4009 | objobjargproc func = (objobjargproc)wrapped; |
| 4010 | int res; |
| 4011 | PyObject *key; |
| 4012 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4013 | if (!check_num_args(args, 1)) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4014 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4015 | key = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4016 | res = (*func)(self, key, NULL); |
| 4017 | if (res == -1 && PyErr_Occurred()) |
| 4018 | return NULL; |
| 4019 | Py_INCREF(Py_None); |
| 4020 | return Py_None; |
| 4021 | } |
| 4022 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4023 | static PyObject * |
| 4024 | wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4025 | { |
| 4026 | cmpfunc func = (cmpfunc)wrapped; |
| 4027 | int res; |
| 4028 | PyObject *other; |
| 4029 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4030 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4031 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4032 | other = PyTuple_GET_ITEM(args, 0); |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4033 | if (Py_Type(other)->tp_compare != func && |
| 4034 | !PyType_IsSubtype(Py_Type(other), Py_Type(self))) { |
Guido van Rossum | ceccae5 | 2001-09-18 20:03:57 +0000 | [diff] [blame] | 4035 | PyErr_Format( |
| 4036 | PyExc_TypeError, |
| 4037 | "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'", |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4038 | Py_Type(self)->tp_name, |
| 4039 | Py_Type(self)->tp_name, |
| 4040 | Py_Type(other)->tp_name); |
Guido van Rossum | ceccae5 | 2001-09-18 20:03:57 +0000 | [diff] [blame] | 4041 | return NULL; |
| 4042 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4043 | res = (*func)(self, other); |
| 4044 | if (PyErr_Occurred()) |
| 4045 | return NULL; |
| 4046 | return PyInt_FromLong((long)res); |
| 4047 | } |
| 4048 | |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4049 | /* 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] | 4050 | This is called the Carlo Verre hack after its discoverer. */ |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4051 | static int |
| 4052 | hackcheck(PyObject *self, setattrofunc func, char *what) |
| 4053 | { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4054 | PyTypeObject *type = Py_Type(self); |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4055 | while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 4056 | type = type->tp_base; |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 4057 | /* If type is NULL now, this is a really weird type. |
| 4058 | In the spirit of backwards compatibility (?), just shut up. */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 4059 | if (type && type->tp_setattro != func) { |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4060 | PyErr_Format(PyExc_TypeError, |
| 4061 | "can't apply this %s to %s object", |
| 4062 | what, |
| 4063 | type->tp_name); |
| 4064 | return 0; |
| 4065 | } |
| 4066 | return 1; |
| 4067 | } |
| 4068 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4069 | static PyObject * |
| 4070 | wrap_setattr(PyObject *self, PyObject *args, void *wrapped) |
| 4071 | { |
| 4072 | setattrofunc func = (setattrofunc)wrapped; |
| 4073 | int res; |
| 4074 | PyObject *name, *value; |
| 4075 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4076 | if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4077 | return NULL; |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4078 | if (!hackcheck(self, func, "__setattr__")) |
| 4079 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4080 | res = (*func)(self, name, value); |
| 4081 | if (res < 0) |
| 4082 | return NULL; |
| 4083 | Py_INCREF(Py_None); |
| 4084 | return Py_None; |
| 4085 | } |
| 4086 | |
| 4087 | static PyObject * |
| 4088 | wrap_delattr(PyObject *self, PyObject *args, void *wrapped) |
| 4089 | { |
| 4090 | setattrofunc func = (setattrofunc)wrapped; |
| 4091 | int res; |
| 4092 | PyObject *name; |
| 4093 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4094 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4095 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4096 | name = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4097 | if (!hackcheck(self, func, "__delattr__")) |
| 4098 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4099 | res = (*func)(self, name, NULL); |
| 4100 | if (res < 0) |
| 4101 | return NULL; |
| 4102 | Py_INCREF(Py_None); |
| 4103 | return Py_None; |
| 4104 | } |
| 4105 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4106 | static PyObject * |
| 4107 | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4108 | { |
| 4109 | hashfunc func = (hashfunc)wrapped; |
| 4110 | long res; |
| 4111 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4112 | if (!check_num_args(args, 0)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4113 | return NULL; |
| 4114 | res = (*func)(self); |
| 4115 | if (res == -1 && PyErr_Occurred()) |
| 4116 | return NULL; |
| 4117 | return PyInt_FromLong(res); |
| 4118 | } |
| 4119 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4120 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4121 | wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4122 | { |
| 4123 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4124 | |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4125 | return (*func)(self, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4126 | } |
| 4127 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4128 | static PyObject * |
| 4129 | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) |
| 4130 | { |
| 4131 | richcmpfunc func = (richcmpfunc)wrapped; |
| 4132 | PyObject *other; |
| 4133 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4134 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4135 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4136 | other = PyTuple_GET_ITEM(args, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4137 | return (*func)(self, other, op); |
| 4138 | } |
| 4139 | |
| 4140 | #undef RICHCMP_WRAPPER |
| 4141 | #define RICHCMP_WRAPPER(NAME, OP) \ |
| 4142 | static PyObject * \ |
| 4143 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ |
| 4144 | { \ |
| 4145 | return wrap_richcmpfunc(self, args, wrapped, OP); \ |
| 4146 | } |
| 4147 | |
Jack Jansen | 8e938b4 | 2001-08-08 15:29:49 +0000 | [diff] [blame] | 4148 | RICHCMP_WRAPPER(lt, Py_LT) |
| 4149 | RICHCMP_WRAPPER(le, Py_LE) |
| 4150 | RICHCMP_WRAPPER(eq, Py_EQ) |
| 4151 | RICHCMP_WRAPPER(ne, Py_NE) |
| 4152 | RICHCMP_WRAPPER(gt, Py_GT) |
| 4153 | RICHCMP_WRAPPER(ge, Py_GE) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4154 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4155 | static PyObject * |
| 4156 | wrap_next(PyObject *self, PyObject *args, void *wrapped) |
| 4157 | { |
| 4158 | unaryfunc func = (unaryfunc)wrapped; |
| 4159 | PyObject *res; |
| 4160 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4161 | if (!check_num_args(args, 0)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4162 | return NULL; |
| 4163 | res = (*func)(self); |
| 4164 | if (res == NULL && !PyErr_Occurred()) |
| 4165 | PyErr_SetNone(PyExc_StopIteration); |
| 4166 | return res; |
| 4167 | } |
| 4168 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4169 | static PyObject * |
| 4170 | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped) |
| 4171 | { |
| 4172 | descrgetfunc func = (descrgetfunc)wrapped; |
| 4173 | PyObject *obj; |
| 4174 | PyObject *type = NULL; |
| 4175 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4176 | if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4177 | return NULL; |
Guido van Rossum | 82ed25c | 2003-02-11 16:25:43 +0000 | [diff] [blame] | 4178 | if (obj == Py_None) |
| 4179 | obj = NULL; |
| 4180 | if (type == Py_None) |
| 4181 | type = NULL; |
| 4182 | if (type == NULL &&obj == NULL) { |
| 4183 | PyErr_SetString(PyExc_TypeError, |
| 4184 | "__get__(None, None) is invalid"); |
| 4185 | return NULL; |
| 4186 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4187 | return (*func)(self, obj, type); |
| 4188 | } |
| 4189 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4190 | static PyObject * |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4191 | wrap_descr_set(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4192 | { |
| 4193 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4194 | PyObject *obj, *value; |
| 4195 | int ret; |
| 4196 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4197 | if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4198 | return NULL; |
| 4199 | ret = (*func)(self, obj, value); |
| 4200 | if (ret < 0) |
| 4201 | return NULL; |
| 4202 | Py_INCREF(Py_None); |
| 4203 | return Py_None; |
| 4204 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 4205 | |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4206 | static PyObject * |
| 4207 | wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped) |
| 4208 | { |
| 4209 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4210 | PyObject *obj; |
| 4211 | int ret; |
| 4212 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4213 | if (!check_num_args(args, 1)) |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4214 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4215 | obj = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4216 | ret = (*func)(self, obj, NULL); |
| 4217 | if (ret < 0) |
| 4218 | return NULL; |
| 4219 | Py_INCREF(Py_None); |
| 4220 | return Py_None; |
| 4221 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4222 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4223 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4224 | wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4225 | { |
| 4226 | initproc func = (initproc)wrapped; |
| 4227 | |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4228 | if (func(self, args, kwds) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4229 | return NULL; |
| 4230 | Py_INCREF(Py_None); |
| 4231 | return Py_None; |
| 4232 | } |
| 4233 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4234 | static PyObject * |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4235 | tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4236 | { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4237 | PyTypeObject *type, *subtype, *staticbase; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4238 | PyObject *arg0, *res; |
| 4239 | |
| 4240 | if (self == NULL || !PyType_Check(self)) |
| 4241 | Py_FatalError("__new__() called with non-type 'self'"); |
| 4242 | type = (PyTypeObject *)self; |
| 4243 | if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4244 | PyErr_Format(PyExc_TypeError, |
| 4245 | "%s.__new__(): not enough arguments", |
| 4246 | type->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4247 | return NULL; |
| 4248 | } |
| 4249 | arg0 = PyTuple_GET_ITEM(args, 0); |
| 4250 | if (!PyType_Check(arg0)) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4251 | PyErr_Format(PyExc_TypeError, |
| 4252 | "%s.__new__(X): X is not a type object (%s)", |
| 4253 | type->tp_name, |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4254 | Py_Type(arg0)->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4255 | return NULL; |
| 4256 | } |
| 4257 | subtype = (PyTypeObject *)arg0; |
| 4258 | if (!PyType_IsSubtype(subtype, type)) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4259 | PyErr_Format(PyExc_TypeError, |
| 4260 | "%s.__new__(%s): %s is not a subtype of %s", |
| 4261 | type->tp_name, |
| 4262 | subtype->tp_name, |
| 4263 | subtype->tp_name, |
| 4264 | type->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4265 | return NULL; |
| 4266 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4267 | |
| 4268 | /* Check that the use doesn't do something silly and unsafe like |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 4269 | object.__new__(dict). To do this, we check that the |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4270 | most derived base that's not a heap type is this type. */ |
| 4271 | staticbase = subtype; |
| 4272 | while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 4273 | staticbase = staticbase->tp_base; |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 4274 | /* If staticbase is NULL now, it is a really weird type. |
| 4275 | In the spirit of backwards compatibility (?), just shut up. */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 4276 | if (staticbase && staticbase->tp_new != type->tp_new) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4277 | PyErr_Format(PyExc_TypeError, |
| 4278 | "%s.__new__(%s) is not safe, use %s.__new__()", |
| 4279 | type->tp_name, |
| 4280 | subtype->tp_name, |
| 4281 | staticbase == NULL ? "?" : staticbase->tp_name); |
| 4282 | return NULL; |
| 4283 | } |
| 4284 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4285 | args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 4286 | if (args == NULL) |
| 4287 | return NULL; |
| 4288 | res = type->tp_new(subtype, args, kwds); |
| 4289 | Py_DECREF(args); |
| 4290 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4291 | } |
| 4292 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4293 | static struct PyMethodDef tp_new_methoddef[] = { |
Neal Norwitz | a84dcd7 | 2007-05-22 07:16:44 +0000 | [diff] [blame] | 4294 | {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 4295 | PyDoc_STR("T.__new__(S, ...) -> " |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 4296 | "a new object with type S, a subtype of T")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4297 | {0} |
| 4298 | }; |
| 4299 | |
| 4300 | static int |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4301 | add_tp_new_wrapper(PyTypeObject *type) |
| 4302 | { |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4303 | PyObject *func; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4304 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 4305 | if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL) |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4306 | return 0; |
| 4307 | func = PyCFunction_New(tp_new_methoddef, (PyObject *)type); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4308 | if (func == NULL) |
| 4309 | return -1; |
Raymond Hettinger | 8d726ee | 2004-06-25 22:24:35 +0000 | [diff] [blame] | 4310 | if (PyDict_SetItemString(type->tp_dict, "__new__", func)) { |
Raymond Hettinger | d56cbe5 | 2004-06-25 22:17:39 +0000 | [diff] [blame] | 4311 | Py_DECREF(func); |
| 4312 | return -1; |
| 4313 | } |
| 4314 | Py_DECREF(func); |
| 4315 | return 0; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4316 | } |
| 4317 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4318 | /* Slot wrappers that call the corresponding __foo__ slot. See comments |
| 4319 | below at override_slots() for more explanation. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4320 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4321 | #define SLOT0(FUNCNAME, OPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4322 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4323 | FUNCNAME(PyObject *self) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4324 | { \ |
Guido van Rossum | 5592e4d | 2001-08-28 18:28:21 +0000 | [diff] [blame] | 4325 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4326 | return call_method(self, OPSTR, &cache_str, "()"); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4327 | } |
| 4328 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4329 | #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4330 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4331 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4332 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4333 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4334 | return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4335 | } |
| 4336 | |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4337 | /* Boolean helper for SLOT1BINFULL(). |
| 4338 | right.__class__ is a nontrivial subclass of left.__class__. */ |
| 4339 | static int |
| 4340 | method_is_overloaded(PyObject *left, PyObject *right, char *name) |
| 4341 | { |
| 4342 | PyObject *a, *b; |
| 4343 | int ok; |
| 4344 | |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4345 | b = PyObject_GetAttrString((PyObject *)(Py_Type(right)), name); |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4346 | if (b == NULL) { |
| 4347 | PyErr_Clear(); |
| 4348 | /* If right doesn't have it, it's not overloaded */ |
| 4349 | return 0; |
| 4350 | } |
| 4351 | |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4352 | a = PyObject_GetAttrString((PyObject *)(Py_Type(left)), name); |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4353 | if (a == NULL) { |
| 4354 | PyErr_Clear(); |
| 4355 | Py_DECREF(b); |
| 4356 | /* If right has it but left doesn't, it's overloaded */ |
| 4357 | return 1; |
| 4358 | } |
| 4359 | |
| 4360 | ok = PyObject_RichCompareBool(a, b, Py_NE); |
| 4361 | Py_DECREF(a); |
| 4362 | Py_DECREF(b); |
| 4363 | if (ok < 0) { |
| 4364 | PyErr_Clear(); |
| 4365 | return 0; |
| 4366 | } |
| 4367 | |
| 4368 | return ok; |
| 4369 | } |
| 4370 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4371 | |
| 4372 | #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4373 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4374 | FUNCNAME(PyObject *self, PyObject *other) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4375 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4376 | static PyObject *cache_str, *rcache_str; \ |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4377 | int do_other = Py_Type(self) != Py_Type(other) && \ |
| 4378 | Py_Type(other)->tp_as_number != NULL && \ |
| 4379 | Py_Type(other)->tp_as_number->SLOTNAME == TESTFUNC; \ |
| 4380 | if (Py_Type(self)->tp_as_number != NULL && \ |
| 4381 | Py_Type(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4382 | PyObject *r; \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4383 | if (do_other && \ |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4384 | PyType_IsSubtype(Py_Type(other), Py_Type(self)) && \ |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4385 | method_is_overloaded(self, other, ROPSTR)) { \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4386 | r = call_maybe( \ |
| 4387 | other, ROPSTR, &rcache_str, "(O)", self); \ |
| 4388 | if (r != Py_NotImplemented) \ |
| 4389 | return r; \ |
| 4390 | Py_DECREF(r); \ |
| 4391 | do_other = 0; \ |
| 4392 | } \ |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 4393 | r = call_maybe( \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4394 | self, OPSTR, &cache_str, "(O)", other); \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4395 | if (r != Py_NotImplemented || \ |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4396 | Py_Type(other) == Py_Type(self)) \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4397 | return r; \ |
| 4398 | Py_DECREF(r); \ |
| 4399 | } \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4400 | if (do_other) { \ |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 4401 | return call_maybe( \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4402 | other, ROPSTR, &rcache_str, "(O)", self); \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4403 | } \ |
| 4404 | Py_INCREF(Py_NotImplemented); \ |
| 4405 | return Py_NotImplemented; \ |
| 4406 | } |
| 4407 | |
| 4408 | #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ |
| 4409 | SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR) |
| 4410 | |
| 4411 | #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \ |
| 4412 | static PyObject * \ |
| 4413 | FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ |
| 4414 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4415 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4416 | return call_method(self, OPSTR, &cache_str, \ |
| 4417 | "(" ARGCODES ")", arg1, arg2); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4420 | static Py_ssize_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4421 | slot_sq_length(PyObject *self) |
| 4422 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4423 | static PyObject *len_str; |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4424 | PyObject *res = call_method(self, "__len__", &len_str, "()"); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4425 | Py_ssize_t len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4426 | |
| 4427 | if (res == NULL) |
| 4428 | return -1; |
Neal Norwitz | 1872b1c | 2006-08-12 18:44:06 +0000 | [diff] [blame] | 4429 | len = PyInt_AsSsize_t(res); |
Guido van Rossum | 2611162 | 2001-10-01 16:42:49 +0000 | [diff] [blame] | 4430 | Py_DECREF(res); |
Jeremy Hylton | f20fcf9 | 2002-07-25 16:06:15 +0000 | [diff] [blame] | 4431 | if (len < 0) { |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 4432 | if (!PyErr_Occurred()) |
| 4433 | PyErr_SetString(PyExc_ValueError, |
| 4434 | "__len__() should return >= 0"); |
Jeremy Hylton | f20fcf9 | 2002-07-25 16:06:15 +0000 | [diff] [blame] | 4435 | return -1; |
| 4436 | } |
Guido van Rossum | 2611162 | 2001-10-01 16:42:49 +0000 | [diff] [blame] | 4437 | return len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4438 | } |
| 4439 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4440 | /* Super-optimized version of slot_sq_item. |
| 4441 | Other slots could do the same... */ |
| 4442 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4443 | slot_sq_item(PyObject *self, Py_ssize_t i) |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4444 | { |
| 4445 | static PyObject *getitem_str; |
| 4446 | PyObject *func, *args = NULL, *ival = NULL, *retval = NULL; |
| 4447 | descrgetfunc f; |
| 4448 | |
| 4449 | if (getitem_str == NULL) { |
| 4450 | getitem_str = PyString_InternFromString("__getitem__"); |
| 4451 | if (getitem_str == NULL) |
| 4452 | return NULL; |
| 4453 | } |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4454 | func = _PyType_Lookup(Py_Type(self), getitem_str); |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4455 | if (func != NULL) { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4456 | if ((f = Py_Type(func)->tp_descr_get) == NULL) |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4457 | Py_INCREF(func); |
Neal Norwitz | 673cd82 | 2002-10-18 16:33:13 +0000 | [diff] [blame] | 4458 | else { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4459 | func = f(func, self, (PyObject *)(Py_Type(self))); |
Neal Norwitz | 673cd82 | 2002-10-18 16:33:13 +0000 | [diff] [blame] | 4460 | if (func == NULL) { |
| 4461 | return NULL; |
| 4462 | } |
| 4463 | } |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 4464 | ival = PyInt_FromSsize_t(i); |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4465 | if (ival != NULL) { |
| 4466 | args = PyTuple_New(1); |
| 4467 | if (args != NULL) { |
| 4468 | PyTuple_SET_ITEM(args, 0, ival); |
| 4469 | retval = PyObject_Call(func, args, NULL); |
| 4470 | Py_XDECREF(args); |
| 4471 | Py_XDECREF(func); |
| 4472 | return retval; |
| 4473 | } |
| 4474 | } |
| 4475 | } |
| 4476 | else { |
| 4477 | PyErr_SetObject(PyExc_AttributeError, getitem_str); |
| 4478 | } |
| 4479 | Py_XDECREF(args); |
| 4480 | Py_XDECREF(ival); |
| 4481 | Py_XDECREF(func); |
| 4482 | return NULL; |
| 4483 | } |
| 4484 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4485 | SLOT2(slot_sq_slice, "__getslice__", Py_ssize_t, Py_ssize_t, "nn") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4486 | |
| 4487 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4488 | slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4489 | { |
| 4490 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4491 | static PyObject *delitem_str, *setitem_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4492 | |
| 4493 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4494 | res = call_method(self, "__delitem__", &delitem_str, |
Thomas Wouters | 4e90810 | 2006-04-21 11:26:56 +0000 | [diff] [blame] | 4495 | "(n)", index); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4496 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4497 | res = call_method(self, "__setitem__", &setitem_str, |
Thomas Wouters | 4e90810 | 2006-04-21 11:26:56 +0000 | [diff] [blame] | 4498 | "(nO)", index, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4499 | if (res == NULL) |
| 4500 | return -1; |
| 4501 | Py_DECREF(res); |
| 4502 | return 0; |
| 4503 | } |
| 4504 | |
| 4505 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4506 | 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] | 4507 | { |
| 4508 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4509 | static PyObject *delslice_str, *setslice_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4510 | |
| 4511 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4512 | res = call_method(self, "__delslice__", &delslice_str, |
Thomas Wouters | 4e90810 | 2006-04-21 11:26:56 +0000 | [diff] [blame] | 4513 | "(nn)", i, j); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4514 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4515 | res = call_method(self, "__setslice__", &setslice_str, |
Thomas Wouters | 4e90810 | 2006-04-21 11:26:56 +0000 | [diff] [blame] | 4516 | "(nnO)", i, j, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4517 | if (res == NULL) |
| 4518 | return -1; |
| 4519 | Py_DECREF(res); |
| 4520 | return 0; |
| 4521 | } |
| 4522 | |
| 4523 | static int |
| 4524 | slot_sq_contains(PyObject *self, PyObject *value) |
| 4525 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4526 | PyObject *func, *res, *args; |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 4527 | int result = -1; |
| 4528 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4529 | static PyObject *contains_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4530 | |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4531 | func = lookup_maybe(self, "__contains__", &contains_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4532 | if (func != NULL) { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4533 | args = PyTuple_Pack(1, value); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4534 | if (args == NULL) |
| 4535 | res = NULL; |
| 4536 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4537 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4538 | Py_DECREF(args); |
| 4539 | } |
| 4540 | Py_DECREF(func); |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 4541 | if (res != NULL) { |
| 4542 | result = PyObject_IsTrue(res); |
| 4543 | Py_DECREF(res); |
| 4544 | } |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4545 | } |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 4546 | else if (! PyErr_Occurred()) { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4547 | /* Possible results: -1 and 1 */ |
| 4548 | result = (int)_PySequence_IterSearch(self, value, |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 4549 | PY_ITERSEARCH_CONTAINS); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4550 | } |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 4551 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4552 | } |
| 4553 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4554 | #define slot_mp_length slot_sq_length |
| 4555 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4556 | SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4557 | |
| 4558 | static int |
| 4559 | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) |
| 4560 | { |
| 4561 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4562 | static PyObject *delitem_str, *setitem_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4563 | |
| 4564 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4565 | res = call_method(self, "__delitem__", &delitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4566 | "(O)", key); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4567 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4568 | res = call_method(self, "__setitem__", &setitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4569 | "(OO)", key, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4570 | if (res == NULL) |
| 4571 | return -1; |
| 4572 | Py_DECREF(res); |
| 4573 | return 0; |
| 4574 | } |
| 4575 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4576 | SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__") |
| 4577 | SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__") |
| 4578 | SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__") |
| 4579 | SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__") |
| 4580 | SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__") |
| 4581 | SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__") |
| 4582 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 4583 | static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4584 | |
| 4585 | SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, |
| 4586 | nb_power, "__pow__", "__rpow__") |
| 4587 | |
| 4588 | static PyObject * |
| 4589 | slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) |
| 4590 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4591 | static PyObject *pow_str; |
| 4592 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4593 | if (modulus == Py_None) |
| 4594 | return slot_nb_power_binary(self, other); |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 4595 | /* Three-arg power doesn't use __rpow__. But ternary_op |
| 4596 | can call this when the second argument's type uses |
| 4597 | slot_nb_power, so check before calling self.__pow__. */ |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4598 | if (Py_Type(self)->tp_as_number != NULL && |
| 4599 | Py_Type(self)->tp_as_number->nb_power == slot_nb_power) { |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 4600 | return call_method(self, "__pow__", &pow_str, |
| 4601 | "(OO)", other, modulus); |
| 4602 | } |
| 4603 | Py_INCREF(Py_NotImplemented); |
| 4604 | return Py_NotImplemented; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4605 | } |
| 4606 | |
| 4607 | SLOT0(slot_nb_negative, "__neg__") |
| 4608 | SLOT0(slot_nb_positive, "__pos__") |
| 4609 | SLOT0(slot_nb_absolute, "__abs__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4610 | |
| 4611 | static int |
| 4612 | slot_nb_nonzero(PyObject *self) |
| 4613 | { |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 4614 | PyObject *func, *args; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4615 | static PyObject *nonzero_str, *len_str; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 4616 | int result = -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4617 | |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4618 | func = lookup_maybe(self, "__nonzero__", &nonzero_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4619 | if (func == NULL) { |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4620 | if (PyErr_Occurred()) |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4621 | return -1; |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4622 | func = lookup_maybe(self, "__len__", &len_str); |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 4623 | if (func == NULL) |
| 4624 | return PyErr_Occurred() ? -1 : 1; |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 4625 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 4626 | args = PyTuple_New(0); |
| 4627 | if (args != NULL) { |
| 4628 | PyObject *temp = PyObject_Call(func, args, NULL); |
| 4629 | Py_DECREF(args); |
| 4630 | if (temp != NULL) { |
Jeremy Hylton | 3e3159c | 2003-06-27 17:38:27 +0000 | [diff] [blame] | 4631 | if (PyInt_CheckExact(temp) || PyBool_Check(temp)) |
Jeremy Hylton | 090a349 | 2003-06-27 16:46:45 +0000 | [diff] [blame] | 4632 | result = PyObject_IsTrue(temp); |
| 4633 | else { |
| 4634 | PyErr_Format(PyExc_TypeError, |
| 4635 | "__nonzero__ should return " |
| 4636 | "bool or int, returned %s", |
| 4637 | temp->ob_type->tp_name); |
Jeremy Hylton | 3e3159c | 2003-06-27 17:38:27 +0000 | [diff] [blame] | 4638 | result = -1; |
Jeremy Hylton | 090a349 | 2003-06-27 16:46:45 +0000 | [diff] [blame] | 4639 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 4640 | Py_DECREF(temp); |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4641 | } |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4642 | } |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4643 | Py_DECREF(func); |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 4644 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4645 | } |
| 4646 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4647 | |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 4648 | static PyObject * |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4649 | slot_nb_index(PyObject *self) |
| 4650 | { |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4651 | static PyObject *index_str; |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 4652 | return call_method(self, "__index__", &index_str, "()"); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4653 | } |
| 4654 | |
| 4655 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4656 | SLOT0(slot_nb_invert, "__invert__") |
| 4657 | SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") |
| 4658 | SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") |
| 4659 | SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") |
| 4660 | SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") |
| 4661 | SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4662 | |
| 4663 | static int |
| 4664 | slot_nb_coerce(PyObject **a, PyObject **b) |
| 4665 | { |
| 4666 | static PyObject *coerce_str; |
| 4667 | PyObject *self = *a, *other = *b; |
| 4668 | |
| 4669 | if (self->ob_type->tp_as_number != NULL && |
| 4670 | self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) { |
| 4671 | PyObject *r; |
| 4672 | r = call_maybe( |
| 4673 | self, "__coerce__", &coerce_str, "(O)", other); |
| 4674 | if (r == NULL) |
| 4675 | return -1; |
| 4676 | if (r == Py_NotImplemented) { |
| 4677 | Py_DECREF(r); |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4678 | } |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4679 | else { |
| 4680 | if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) { |
| 4681 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4682 | "__coerce__ didn't return a 2-tuple"); |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4683 | Py_DECREF(r); |
| 4684 | return -1; |
| 4685 | } |
| 4686 | *a = PyTuple_GET_ITEM(r, 0); |
| 4687 | Py_INCREF(*a); |
| 4688 | *b = PyTuple_GET_ITEM(r, 1); |
| 4689 | Py_INCREF(*b); |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4690 | Py_DECREF(r); |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4691 | return 0; |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4692 | } |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4693 | } |
| 4694 | if (other->ob_type->tp_as_number != NULL && |
| 4695 | other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) { |
| 4696 | PyObject *r; |
| 4697 | r = call_maybe( |
| 4698 | other, "__coerce__", &coerce_str, "(O)", self); |
| 4699 | if (r == NULL) |
| 4700 | return -1; |
| 4701 | if (r == Py_NotImplemented) { |
| 4702 | Py_DECREF(r); |
| 4703 | return 1; |
| 4704 | } |
| 4705 | if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) { |
| 4706 | PyErr_SetString(PyExc_TypeError, |
| 4707 | "__coerce__ didn't return a 2-tuple"); |
| 4708 | Py_DECREF(r); |
| 4709 | return -1; |
| 4710 | } |
| 4711 | *a = PyTuple_GET_ITEM(r, 1); |
| 4712 | Py_INCREF(*a); |
| 4713 | *b = PyTuple_GET_ITEM(r, 0); |
| 4714 | Py_INCREF(*b); |
| 4715 | Py_DECREF(r); |
| 4716 | return 0; |
| 4717 | } |
| 4718 | return 1; |
| 4719 | } |
| 4720 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4721 | SLOT0(slot_nb_int, "__int__") |
| 4722 | SLOT0(slot_nb_long, "__long__") |
| 4723 | SLOT0(slot_nb_float, "__float__") |
| 4724 | SLOT0(slot_nb_oct, "__oct__") |
| 4725 | SLOT0(slot_nb_hex, "__hex__") |
| 4726 | SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O") |
| 4727 | SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O") |
| 4728 | SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O") |
| 4729 | SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O") |
| 4730 | SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O") |
Martin v. Löwis | fd96326 | 2007-02-09 12:19:32 +0000 | [diff] [blame] | 4731 | /* Can't use SLOT1 here, because nb_inplace_power is ternary */ |
| 4732 | static PyObject * |
| 4733 | slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2) |
| 4734 | { |
| 4735 | static PyObject *cache_str; |
| 4736 | return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1); |
| 4737 | } |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4738 | SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O") |
| 4739 | SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O") |
| 4740 | SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O") |
| 4741 | SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O") |
| 4742 | SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O") |
| 4743 | SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, |
| 4744 | "__floordiv__", "__rfloordiv__") |
| 4745 | SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__") |
| 4746 | SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O") |
| 4747 | SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4748 | |
| 4749 | static int |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4750 | half_compare(PyObject *self, PyObject *other) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4751 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4752 | PyObject *func, *args, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4753 | static PyObject *cmp_str; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4754 | Py_ssize_t c; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4755 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4756 | func = lookup_method(self, "__cmp__", &cmp_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4757 | if (func == NULL) { |
| 4758 | PyErr_Clear(); |
| 4759 | } |
| 4760 | else { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4761 | args = PyTuple_Pack(1, other); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4762 | if (args == NULL) |
| 4763 | res = NULL; |
| 4764 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4765 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4766 | Py_DECREF(args); |
| 4767 | } |
Raymond Hettinger | ab5dae3 | 2002-06-24 13:08:16 +0000 | [diff] [blame] | 4768 | Py_DECREF(func); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4769 | if (res != Py_NotImplemented) { |
| 4770 | if (res == NULL) |
| 4771 | return -2; |
| 4772 | c = PyInt_AsLong(res); |
| 4773 | Py_DECREF(res); |
| 4774 | if (c == -1 && PyErr_Occurred()) |
| 4775 | return -2; |
| 4776 | return (c < 0) ? -1 : (c > 0) ? 1 : 0; |
| 4777 | } |
| 4778 | Py_DECREF(res); |
| 4779 | } |
| 4780 | return 2; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4781 | } |
| 4782 | |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 4783 | /* This slot is published for the benefit of try_3way_compare in object.c */ |
| 4784 | int |
| 4785 | _PyObject_SlotCompare(PyObject *self, PyObject *other) |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4786 | { |
| 4787 | int c; |
| 4788 | |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4789 | if (Py_Type(self)->tp_compare == _PyObject_SlotCompare) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4790 | c = half_compare(self, other); |
| 4791 | if (c <= 1) |
| 4792 | return c; |
| 4793 | } |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4794 | if (Py_Type(other)->tp_compare == _PyObject_SlotCompare) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4795 | c = half_compare(other, self); |
| 4796 | if (c < -1) |
| 4797 | return -2; |
| 4798 | if (c <= 1) |
| 4799 | return -c; |
| 4800 | } |
| 4801 | return (void *)self < (void *)other ? -1 : |
| 4802 | (void *)self > (void *)other ? 1 : 0; |
| 4803 | } |
| 4804 | |
| 4805 | static PyObject * |
| 4806 | slot_tp_repr(PyObject *self) |
| 4807 | { |
| 4808 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4809 | static PyObject *repr_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4810 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4811 | func = lookup_method(self, "__repr__", &repr_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4812 | if (func != NULL) { |
| 4813 | res = PyEval_CallObject(func, NULL); |
| 4814 | Py_DECREF(func); |
| 4815 | return res; |
| 4816 | } |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 4817 | PyErr_Clear(); |
| 4818 | return PyString_FromFormat("<%s object at %p>", |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4819 | Py_Type(self)->tp_name, self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4820 | } |
| 4821 | |
| 4822 | static PyObject * |
| 4823 | slot_tp_str(PyObject *self) |
| 4824 | { |
| 4825 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4826 | static PyObject *str_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4827 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4828 | func = lookup_method(self, "__str__", &str_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4829 | if (func != NULL) { |
| 4830 | res = PyEval_CallObject(func, NULL); |
| 4831 | Py_DECREF(func); |
| 4832 | return res; |
| 4833 | } |
| 4834 | else { |
| 4835 | PyErr_Clear(); |
| 4836 | return slot_tp_repr(self); |
| 4837 | } |
| 4838 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4839 | |
| 4840 | static long |
| 4841 | slot_tp_hash(PyObject *self) |
| 4842 | { |
Tim Peters | 61ce0a9 | 2002-12-06 23:38:02 +0000 | [diff] [blame] | 4843 | PyObject *func; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4844 | static PyObject *hash_str, *eq_str, *cmp_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4845 | long h; |
| 4846 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4847 | func = lookup_method(self, "__hash__", &hash_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4848 | |
| 4849 | if (func != NULL) { |
Tim Peters | 61ce0a9 | 2002-12-06 23:38:02 +0000 | [diff] [blame] | 4850 | PyObject *res = PyEval_CallObject(func, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4851 | Py_DECREF(func); |
| 4852 | if (res == NULL) |
| 4853 | return -1; |
Martin v. Löwis | ab2f8f7 | 2006-08-09 07:57:39 +0000 | [diff] [blame] | 4854 | if (PyLong_Check(res)) |
Armin Rigo | 51fc8c4 | 2006-08-09 14:55:26 +0000 | [diff] [blame] | 4855 | h = PyLong_Type.tp_hash(res); |
Martin v. Löwis | ab2f8f7 | 2006-08-09 07:57:39 +0000 | [diff] [blame] | 4856 | else |
| 4857 | h = PyInt_AsLong(res); |
Tim Peters | 61ce0a9 | 2002-12-06 23:38:02 +0000 | [diff] [blame] | 4858 | Py_DECREF(res); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4859 | } |
| 4860 | else { |
| 4861 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4862 | func = lookup_method(self, "__eq__", &eq_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4863 | if (func == NULL) { |
| 4864 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4865 | func = lookup_method(self, "__cmp__", &cmp_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4866 | } |
| 4867 | if (func != NULL) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 4868 | PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'", |
| 4869 | self->ob_type->tp_name); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4870 | Py_DECREF(func); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4871 | return -1; |
| 4872 | } |
| 4873 | PyErr_Clear(); |
| 4874 | h = _Py_HashPointer((void *)self); |
| 4875 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4876 | if (h == -1 && !PyErr_Occurred()) |
| 4877 | h = -2; |
| 4878 | return h; |
| 4879 | } |
| 4880 | |
| 4881 | static PyObject * |
| 4882 | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 4883 | { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4884 | static PyObject *call_str; |
| 4885 | PyObject *meth = lookup_method(self, "__call__", &call_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4886 | PyObject *res; |
| 4887 | |
| 4888 | if (meth == NULL) |
| 4889 | return NULL; |
Armin Rigo | 53c1692f | 2006-06-21 21:58:50 +0000 | [diff] [blame] | 4890 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4891 | res = PyObject_Call(meth, args, kwds); |
Armin Rigo | 53c1692f | 2006-06-21 21:58:50 +0000 | [diff] [blame] | 4892 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4893 | Py_DECREF(meth); |
| 4894 | return res; |
| 4895 | } |
| 4896 | |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 4897 | /* There are two slot dispatch functions for tp_getattro. |
| 4898 | |
| 4899 | - slot_tp_getattro() is used when __getattribute__ is overridden |
| 4900 | but no __getattr__ hook is present; |
| 4901 | |
| 4902 | - slot_tp_getattr_hook() is used when a __getattr__ hook is present. |
| 4903 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4904 | The code in update_one_slot() always installs slot_tp_getattr_hook(); this |
| 4905 | detects the absence of __getattr__ and then installs the simpler slot if |
| 4906 | necessary. */ |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 4907 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4908 | static PyObject * |
| 4909 | slot_tp_getattro(PyObject *self, PyObject *name) |
| 4910 | { |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 4911 | static PyObject *getattribute_str = NULL; |
| 4912 | return call_method(self, "__getattribute__", &getattribute_str, |
| 4913 | "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 4916 | static PyObject * |
| 4917 | slot_tp_getattr_hook(PyObject *self, PyObject *name) |
| 4918 | { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4919 | PyTypeObject *tp = Py_Type(self); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 4920 | PyObject *getattr, *getattribute, *res; |
| 4921 | static PyObject *getattribute_str = NULL; |
| 4922 | static PyObject *getattr_str = NULL; |
| 4923 | |
| 4924 | if (getattr_str == NULL) { |
| 4925 | getattr_str = PyString_InternFromString("__getattr__"); |
| 4926 | if (getattr_str == NULL) |
| 4927 | return NULL; |
| 4928 | } |
| 4929 | if (getattribute_str == NULL) { |
| 4930 | getattribute_str = |
| 4931 | PyString_InternFromString("__getattribute__"); |
| 4932 | if (getattribute_str == NULL) |
| 4933 | return NULL; |
| 4934 | } |
| 4935 | getattr = _PyType_Lookup(tp, getattr_str); |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 4936 | if (getattr == NULL) { |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 4937 | /* No __getattr__ hook: use a simpler dispatcher */ |
| 4938 | tp->tp_getattro = slot_tp_getattro; |
| 4939 | return slot_tp_getattro(self, name); |
| 4940 | } |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 4941 | getattribute = _PyType_Lookup(tp, getattribute_str); |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 4942 | if (getattribute == NULL || |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4943 | (Py_Type(getattribute) == &PyWrapperDescr_Type && |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 4944 | ((PyWrapperDescrObject *)getattribute)->d_wrapped == |
| 4945 | (void *)PyObject_GenericGetAttr)) |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 4946 | res = PyObject_GenericGetAttr(self, name); |
| 4947 | else |
Georg Brandl | 684fd0c | 2006-05-25 19:15:31 +0000 | [diff] [blame] | 4948 | res = PyObject_CallFunctionObjArgs(getattribute, self, name, NULL); |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 4949 | if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 4950 | PyErr_Clear(); |
Georg Brandl | 684fd0c | 2006-05-25 19:15:31 +0000 | [diff] [blame] | 4951 | res = PyObject_CallFunctionObjArgs(getattr, self, name, NULL); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 4952 | } |
| 4953 | return res; |
| 4954 | } |
| 4955 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4956 | static int |
| 4957 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) |
| 4958 | { |
| 4959 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4960 | static PyObject *delattr_str, *setattr_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4961 | |
| 4962 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4963 | res = call_method(self, "__delattr__", &delattr_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4964 | "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4965 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4966 | res = call_method(self, "__setattr__", &setattr_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4967 | "(OO)", name, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4968 | if (res == NULL) |
| 4969 | return -1; |
| 4970 | Py_DECREF(res); |
| 4971 | return 0; |
| 4972 | } |
| 4973 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4974 | static PyObject * |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4975 | half_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4976 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4977 | PyObject *func, *args, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4978 | static PyObject *op_str[6]; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4979 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4980 | func = lookup_method(self, name_op[op], &op_str[op]); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4981 | if (func == NULL) { |
| 4982 | PyErr_Clear(); |
| 4983 | Py_INCREF(Py_NotImplemented); |
| 4984 | return Py_NotImplemented; |
| 4985 | } |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4986 | args = PyTuple_Pack(1, other); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4987 | if (args == NULL) |
| 4988 | res = NULL; |
| 4989 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4990 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4991 | Py_DECREF(args); |
| 4992 | } |
| 4993 | Py_DECREF(func); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4994 | return res; |
| 4995 | } |
| 4996 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4997 | static PyObject * |
| 4998 | slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
| 4999 | { |
| 5000 | PyObject *res; |
| 5001 | |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5002 | if (Py_Type(self)->tp_richcompare == slot_tp_richcompare) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5003 | res = half_richcompare(self, other, op); |
| 5004 | if (res != Py_NotImplemented) |
| 5005 | return res; |
| 5006 | Py_DECREF(res); |
| 5007 | } |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5008 | if (Py_Type(other)->tp_richcompare == slot_tp_richcompare) { |
Tim Peters | f4aca75 | 2004-09-23 02:39:37 +0000 | [diff] [blame] | 5009 | res = half_richcompare(other, self, _Py_SwappedOp[op]); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5010 | if (res != Py_NotImplemented) { |
| 5011 | return res; |
| 5012 | } |
| 5013 | Py_DECREF(res); |
| 5014 | } |
| 5015 | Py_INCREF(Py_NotImplemented); |
| 5016 | return Py_NotImplemented; |
| 5017 | } |
| 5018 | |
| 5019 | static PyObject * |
| 5020 | slot_tp_iter(PyObject *self) |
| 5021 | { |
| 5022 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5023 | static PyObject *iter_str, *getitem_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5024 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5025 | func = lookup_method(self, "__iter__", &iter_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5026 | if (func != NULL) { |
Guido van Rossum | 84b2bed | 2002-08-16 17:01:09 +0000 | [diff] [blame] | 5027 | PyObject *args; |
| 5028 | args = res = PyTuple_New(0); |
| 5029 | if (args != NULL) { |
| 5030 | res = PyObject_Call(func, args, NULL); |
| 5031 | Py_DECREF(args); |
| 5032 | } |
| 5033 | Py_DECREF(func); |
| 5034 | return res; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5035 | } |
| 5036 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5037 | func = lookup_method(self, "__getitem__", &getitem_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5038 | if (func == NULL) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 5039 | PyErr_Format(PyExc_TypeError, |
| 5040 | "'%.200s' object is not iterable", |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5041 | Py_Type(self)->tp_name); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5042 | return NULL; |
| 5043 | } |
| 5044 | Py_DECREF(func); |
| 5045 | return PySeqIter_New(self); |
| 5046 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5047 | |
| 5048 | static PyObject * |
| 5049 | slot_tp_iternext(PyObject *self) |
| 5050 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5051 | static PyObject *next_str; |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5052 | return call_method(self, "next", &next_str, "()"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5053 | } |
| 5054 | |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5055 | static PyObject * |
| 5056 | slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 5057 | { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5058 | PyTypeObject *tp = Py_Type(self); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5059 | PyObject *get; |
| 5060 | static PyObject *get_str = NULL; |
| 5061 | |
| 5062 | if (get_str == NULL) { |
| 5063 | get_str = PyString_InternFromString("__get__"); |
| 5064 | if (get_str == NULL) |
| 5065 | return NULL; |
| 5066 | } |
| 5067 | get = _PyType_Lookup(tp, get_str); |
| 5068 | if (get == NULL) { |
| 5069 | /* Avoid further slowdowns */ |
| 5070 | if (tp->tp_descr_get == slot_tp_descr_get) |
| 5071 | tp->tp_descr_get = NULL; |
| 5072 | Py_INCREF(self); |
| 5073 | return self; |
| 5074 | } |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5075 | if (obj == NULL) |
| 5076 | obj = Py_None; |
| 5077 | if (type == NULL) |
| 5078 | type = Py_None; |
Georg Brandl | 684fd0c | 2006-05-25 19:15:31 +0000 | [diff] [blame] | 5079 | return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5080 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5081 | |
| 5082 | static int |
| 5083 | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) |
| 5084 | { |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5085 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5086 | static PyObject *del_str, *set_str; |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5087 | |
| 5088 | if (value == NULL) |
Guido van Rossum | 1d5b3f2 | 2001-12-03 00:08:33 +0000 | [diff] [blame] | 5089 | res = call_method(self, "__delete__", &del_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5090 | "(O)", target); |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5091 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5092 | res = call_method(self, "__set__", &set_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5093 | "(OO)", target, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5094 | if (res == NULL) |
| 5095 | return -1; |
| 5096 | Py_DECREF(res); |
| 5097 | return 0; |
| 5098 | } |
| 5099 | |
| 5100 | static int |
| 5101 | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 5102 | { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5103 | static PyObject *init_str; |
| 5104 | PyObject *meth = lookup_method(self, "__init__", &init_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5105 | PyObject *res; |
| 5106 | |
| 5107 | if (meth == NULL) |
| 5108 | return -1; |
| 5109 | res = PyObject_Call(meth, args, kwds); |
| 5110 | Py_DECREF(meth); |
| 5111 | if (res == NULL) |
| 5112 | return -1; |
Raymond Hettinger | b67cc80 | 2005-03-03 16:45:19 +0000 | [diff] [blame] | 5113 | if (res != Py_None) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 5114 | PyErr_Format(PyExc_TypeError, |
| 5115 | "__init__() should return None, not '%.200s'", |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5116 | Py_Type(res)->tp_name); |
Raymond Hettinger | b67cc80 | 2005-03-03 16:45:19 +0000 | [diff] [blame] | 5117 | Py_DECREF(res); |
| 5118 | return -1; |
| 5119 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5120 | Py_DECREF(res); |
| 5121 | return 0; |
| 5122 | } |
| 5123 | |
| 5124 | static PyObject * |
| 5125 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 5126 | { |
Guido van Rossum | 7bed213 | 2002-08-08 21:57:53 +0000 | [diff] [blame] | 5127 | static PyObject *new_str; |
| 5128 | PyObject *func; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5129 | PyObject *newargs, *x; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5130 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5131 | |
Guido van Rossum | 7bed213 | 2002-08-08 21:57:53 +0000 | [diff] [blame] | 5132 | if (new_str == NULL) { |
| 5133 | new_str = PyString_InternFromString("__new__"); |
| 5134 | if (new_str == NULL) |
| 5135 | return NULL; |
| 5136 | } |
| 5137 | func = PyObject_GetAttr((PyObject *)type, new_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5138 | if (func == NULL) |
| 5139 | return NULL; |
| 5140 | assert(PyTuple_Check(args)); |
| 5141 | n = PyTuple_GET_SIZE(args); |
| 5142 | newargs = PyTuple_New(n+1); |
| 5143 | if (newargs == NULL) |
| 5144 | return NULL; |
| 5145 | Py_INCREF(type); |
| 5146 | PyTuple_SET_ITEM(newargs, 0, (PyObject *)type); |
| 5147 | for (i = 0; i < n; i++) { |
| 5148 | x = PyTuple_GET_ITEM(args, i); |
| 5149 | Py_INCREF(x); |
| 5150 | PyTuple_SET_ITEM(newargs, i+1, x); |
| 5151 | } |
| 5152 | x = PyObject_Call(func, newargs, kwds); |
Guido van Rossum | 25d1807 | 2001-10-01 15:55:28 +0000 | [diff] [blame] | 5153 | Py_DECREF(newargs); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5154 | Py_DECREF(func); |
| 5155 | return x; |
| 5156 | } |
| 5157 | |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5158 | static void |
| 5159 | slot_tp_del(PyObject *self) |
| 5160 | { |
| 5161 | static PyObject *del_str = NULL; |
| 5162 | PyObject *del, *res; |
| 5163 | PyObject *error_type, *error_value, *error_traceback; |
| 5164 | |
| 5165 | /* Temporarily resurrect the object. */ |
| 5166 | assert(self->ob_refcnt == 0); |
| 5167 | self->ob_refcnt = 1; |
| 5168 | |
| 5169 | /* Save the current exception, if any. */ |
| 5170 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 5171 | |
| 5172 | /* Execute __del__ method, if any. */ |
| 5173 | del = lookup_maybe(self, "__del__", &del_str); |
| 5174 | if (del != NULL) { |
| 5175 | res = PyEval_CallObject(del, NULL); |
| 5176 | if (res == NULL) |
| 5177 | PyErr_WriteUnraisable(del); |
| 5178 | else |
| 5179 | Py_DECREF(res); |
| 5180 | Py_DECREF(del); |
| 5181 | } |
| 5182 | |
| 5183 | /* Restore the saved exception. */ |
| 5184 | PyErr_Restore(error_type, error_value, error_traceback); |
| 5185 | |
| 5186 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 5187 | * cause a recursive call. |
| 5188 | */ |
| 5189 | assert(self->ob_refcnt > 0); |
| 5190 | if (--self->ob_refcnt == 0) |
| 5191 | return; /* this is the normal path out */ |
| 5192 | |
| 5193 | /* __del__ resurrected it! Make it look like the original Py_DECREF |
| 5194 | * never happened. |
| 5195 | */ |
| 5196 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 5197 | Py_ssize_t refcnt = self->ob_refcnt; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5198 | _Py_NewReference(self); |
| 5199 | self->ob_refcnt = refcnt; |
| 5200 | } |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5201 | assert(!PyType_IS_GC(Py_Type(self)) || |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5202 | _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED); |
Michael W. Hudson | 3f3b668 | 2004-08-03 10:21:03 +0000 | [diff] [blame] | 5203 | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so |
| 5204 | * we need to undo that. */ |
| 5205 | _Py_DEC_REFTOTAL; |
| 5206 | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object |
| 5207 | * chain, so no more to do there. |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5208 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
| 5209 | * _Py_NewReference bumped tp_allocs: both of those need to be |
| 5210 | * undone. |
| 5211 | */ |
| 5212 | #ifdef COUNT_ALLOCS |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5213 | --Py_Type(self)->tp_frees; |
| 5214 | --Py_Type(self)->tp_allocs; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5215 | #endif |
| 5216 | } |
| 5217 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5218 | |
| 5219 | /* 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] | 5220 | functions. The offsets here are relative to the 'PyHeapTypeObject' |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5221 | structure, which incorporates the additional structures used for numbers, |
| 5222 | sequences and mappings. |
| 5223 | 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] | 5224 | __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] | 5225 | slots (e.g. __str__ affects tp_str as well as tp_repr). The table is |
| 5226 | terminated with an all-zero entry. (This table is further initialized and |
| 5227 | sorted in init_slotdefs() below.) */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5228 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5229 | typedef struct wrapperbase slotdef; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5230 | |
| 5231 | #undef TPSLOT |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5232 | #undef FLSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5233 | #undef ETSLOT |
| 5234 | #undef SQSLOT |
| 5235 | #undef MPSLOT |
| 5236 | #undef NBSLOT |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5237 | #undef UNSLOT |
| 5238 | #undef IBSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5239 | #undef BINSLOT |
| 5240 | #undef RBINSLOT |
| 5241 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5242 | #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Neal Norwitz | d47714a | 2002-08-13 19:01:38 +0000 | [diff] [blame] | 5243 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5244 | PyDoc_STR(DOC)} |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5245 | #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ |
| 5246 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
Neal Norwitz | d47714a | 2002-08-13 19:01:38 +0000 | [diff] [blame] | 5247 | PyDoc_STR(DOC), FLAGS} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5248 | #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5249 | {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
Neal Norwitz | d47714a | 2002-08-13 19:01:38 +0000 | [diff] [blame] | 5250 | PyDoc_STR(DOC)} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5251 | #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5252 | ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) |
| 5253 | #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5254 | ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) |
| 5255 | #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5256 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) |
| 5257 | #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5258 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5259 | "x." NAME "() <==> " DOC) |
| 5260 | #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5261 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5262 | "x." NAME "(y) <==> x" DOC "y") |
| 5263 | #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
| 5264 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5265 | "x." NAME "(y) <==> x" DOC "y") |
| 5266 | #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
| 5267 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5268 | "x." NAME "(y) <==> y" DOC "x") |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5269 | #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
| 5270 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5271 | "x." NAME "(y) <==> " DOC) |
| 5272 | #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
| 5273 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5274 | "x." NAME "(y) <==> " DOC) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5275 | |
| 5276 | static slotdef slotdefs[] = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5277 | SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5278 | "x.__len__() <==> len(x)"), |
Armin Rigo | fd163f9 | 2005-12-29 15:59:19 +0000 | [diff] [blame] | 5279 | /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL. |
| 5280 | The logic in abstract.c always falls back to nb_add/nb_multiply in |
| 5281 | this case. Defining both the nb_* and the sq_* slots to call the |
| 5282 | user-defined methods has unexpected side-effects, as shown by |
| 5283 | test_descr.notimplemented() */ |
| 5284 | SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc, |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5285 | "x.__add__(y) <==> x+y"), |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 5286 | SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc, |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5287 | "x.__mul__(n) <==> x*n"), |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 5288 | SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc, |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5289 | "x.__rmul__(n) <==> n*x"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5290 | SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, |
| 5291 | "x.__getitem__(y) <==> x[y]"), |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5292 | SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_ssizessizeargfunc, |
Brett Cannon | 154da9b | 2003-05-20 02:30:04 +0000 | [diff] [blame] | 5293 | "x.__getslice__(i, j) <==> x[i:j]\n\ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5294 | \n\ |
| 5295 | Use of negative indices is not supported."), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5296 | SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, |
Brett Cannon | be67d87 | 2003-05-20 02:40:12 +0000 | [diff] [blame] | 5297 | "x.__setitem__(i, y) <==> x[i]=y"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5298 | SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, |
Brett Cannon | be67d87 | 2003-05-20 02:40:12 +0000 | [diff] [blame] | 5299 | "x.__delitem__(y) <==> del x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5300 | SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5301 | wrap_ssizessizeobjargproc, |
Brett Cannon | be67d87 | 2003-05-20 02:40:12 +0000 | [diff] [blame] | 5302 | "x.__setslice__(i, j, y) <==> x[i:j]=y\n\ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5303 | \n\ |
| 5304 | Use of negative indices is not supported."), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5305 | SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice, |
Brett Cannon | be67d87 | 2003-05-20 02:40:12 +0000 | [diff] [blame] | 5306 | "x.__delslice__(i, j) <==> del x[i:j]\n\ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5307 | \n\ |
| 5308 | Use of negative indices is not supported."), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5309 | SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, |
| 5310 | "x.__contains__(y) <==> y in x"), |
Armin Rigo | fd163f9 | 2005-12-29 15:59:19 +0000 | [diff] [blame] | 5311 | SQSLOT("__iadd__", sq_inplace_concat, NULL, |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5312 | wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"), |
Armin Rigo | fd163f9 | 2005-12-29 15:59:19 +0000 | [diff] [blame] | 5313 | SQSLOT("__imul__", sq_inplace_repeat, NULL, |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5314 | wrap_indexargfunc, "x.__imul__(y) <==> x*=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5315 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5316 | MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5317 | "x.__len__() <==> len(x)"), |
Guido van Rossum | fd38f8e | 2001-10-09 20:17:57 +0000 | [diff] [blame] | 5318 | MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5319 | wrap_binaryfunc, |
| 5320 | "x.__getitem__(y) <==> x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5321 | MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5322 | wrap_objobjargproc, |
| 5323 | "x.__setitem__(i, y) <==> x[i]=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5324 | MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5325 | wrap_delitem, |
| 5326 | "x.__delitem__(y) <==> del x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5327 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5328 | BINSLOT("__add__", nb_add, slot_nb_add, |
| 5329 | "+"), |
| 5330 | RBINSLOT("__radd__", nb_add, slot_nb_add, |
| 5331 | "+"), |
| 5332 | BINSLOT("__sub__", nb_subtract, slot_nb_subtract, |
| 5333 | "-"), |
| 5334 | RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract, |
| 5335 | "-"), |
| 5336 | BINSLOT("__mul__", nb_multiply, slot_nb_multiply, |
| 5337 | "*"), |
| 5338 | RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply, |
| 5339 | "*"), |
| 5340 | BINSLOT("__div__", nb_divide, slot_nb_divide, |
| 5341 | "/"), |
| 5342 | RBINSLOT("__rdiv__", nb_divide, slot_nb_divide, |
| 5343 | "/"), |
| 5344 | BINSLOT("__mod__", nb_remainder, slot_nb_remainder, |
| 5345 | "%"), |
| 5346 | RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder, |
| 5347 | "%"), |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5348 | BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5349 | "divmod(x, y)"), |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5350 | RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5351 | "divmod(y, x)"), |
| 5352 | NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, |
| 5353 | "x.__pow__(y[, z]) <==> pow(x, y[, z])"), |
| 5354 | NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, |
| 5355 | "y.__rpow__(x[, z]) <==> pow(x, y[, z])"), |
| 5356 | UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"), |
| 5357 | UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"), |
| 5358 | UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, |
| 5359 | "abs(x)"), |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 5360 | UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquirypred, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5361 | "x != 0"), |
| 5362 | UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"), |
| 5363 | BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5364 | RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5365 | BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5366 | RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5367 | BINSLOT("__and__", nb_and, slot_nb_and, "&"), |
| 5368 | RBINSLOT("__rand__", nb_and, slot_nb_and, "&"), |
| 5369 | BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"), |
| 5370 | RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), |
| 5371 | BINSLOT("__or__", nb_or, slot_nb_or, "|"), |
| 5372 | RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), |
| 5373 | NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc, |
| 5374 | "x.__coerce__(y) <==> coerce(x, y)"), |
| 5375 | UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, |
| 5376 | "int(x)"), |
| 5377 | UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc, |
| 5378 | "long(x)"), |
| 5379 | UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, |
| 5380 | "float(x)"), |
| 5381 | UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc, |
| 5382 | "oct(x)"), |
| 5383 | UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc, |
| 5384 | "hex(x)"), |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 5385 | NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc, |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5386 | "x[y:z] <==> x[y.__index__():z.__index__()]"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5387 | IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, |
| 5388 | wrap_binaryfunc, "+"), |
| 5389 | IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, |
| 5390 | wrap_binaryfunc, "-"), |
| 5391 | IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply, |
| 5392 | wrap_binaryfunc, "*"), |
| 5393 | IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide, |
| 5394 | wrap_binaryfunc, "/"), |
| 5395 | IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder, |
| 5396 | wrap_binaryfunc, "%"), |
| 5397 | IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power, |
Guido van Rossum | 6e5680f | 2002-10-15 01:01:53 +0000 | [diff] [blame] | 5398 | wrap_binaryfunc, "**"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5399 | IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift, |
| 5400 | wrap_binaryfunc, "<<"), |
| 5401 | IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift, |
| 5402 | wrap_binaryfunc, ">>"), |
| 5403 | IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and, |
| 5404 | wrap_binaryfunc, "&"), |
| 5405 | IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor, |
| 5406 | wrap_binaryfunc, "^"), |
| 5407 | IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or, |
| 5408 | wrap_binaryfunc, "|"), |
| 5409 | BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5410 | RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5411 | BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5412 | RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5413 | IBSLOT("__ifloordiv__", nb_inplace_floor_divide, |
| 5414 | slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"), |
| 5415 | IBSLOT("__itruediv__", nb_inplace_true_divide, |
| 5416 | slot_nb_inplace_true_divide, wrap_binaryfunc, "/"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5417 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5418 | TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, |
| 5419 | "x.__str__() <==> str(x)"), |
Guido van Rossum | afe7a94 | 2001-10-29 14:33:44 +0000 | [diff] [blame] | 5420 | TPSLOT("__str__", tp_print, NULL, NULL, ""), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5421 | TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, |
| 5422 | "x.__repr__() <==> repr(x)"), |
Guido van Rossum | afe7a94 | 2001-10-29 14:33:44 +0000 | [diff] [blame] | 5423 | TPSLOT("__repr__", tp_print, NULL, NULL, ""), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5424 | TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc, |
| 5425 | "x.__cmp__(y) <==> cmp(x,y)"), |
| 5426 | TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, |
| 5427 | "x.__hash__() <==> hash(x)"), |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5428 | FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, |
| 5429 | "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS), |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5430 | TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5431 | wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"), |
| 5432 | TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), |
| 5433 | TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), |
| 5434 | TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), |
| 5435 | TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, |
| 5436 | "x.__setattr__('name', value) <==> x.name = value"), |
| 5437 | TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), |
| 5438 | TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, |
| 5439 | "x.__delattr__('name') <==> del x.name"), |
| 5440 | TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), |
| 5441 | TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, |
| 5442 | "x.__lt__(y) <==> x<y"), |
| 5443 | TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, |
| 5444 | "x.__le__(y) <==> x<=y"), |
| 5445 | TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, |
| 5446 | "x.__eq__(y) <==> x==y"), |
| 5447 | TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, |
| 5448 | "x.__ne__(y) <==> x!=y"), |
| 5449 | TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, |
| 5450 | "x.__gt__(y) <==> x>y"), |
| 5451 | TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, |
| 5452 | "x.__ge__(y) <==> x>=y"), |
| 5453 | TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, |
| 5454 | "x.__iter__() <==> iter(x)"), |
| 5455 | TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next, |
| 5456 | "x.next() -> the next value, or raise StopIteration"), |
| 5457 | TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, |
| 5458 | "descr.__get__(obj[, type]) -> value"), |
| 5459 | TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, |
| 5460 | "descr.__set__(obj, value)"), |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 5461 | TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set, |
| 5462 | wrap_descr_delete, "descr.__delete__(obj)"), |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5463 | FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5464 | "x.__init__(...) initializes x; " |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5465 | "see x.__class__.__doc__ for signature", |
| 5466 | PyWrapperFlag_KEYWORDS), |
| 5467 | TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""), |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5468 | TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5469 | {NULL} |
| 5470 | }; |
| 5471 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5472 | /* Given a type pointer and an offset gotten from a slotdef entry, return a |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5473 | pointer to the actual slot. This is not quite the same as simply adding |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5474 | the offset to the type pointer, since it takes care to indirect through the |
| 5475 | proper indirection pointer (as_buffer, etc.); it returns NULL if the |
| 5476 | indirection pointer is NULL. */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5477 | static void ** |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5478 | slotptr(PyTypeObject *type, int ioffset) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5479 | { |
| 5480 | char *ptr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5481 | long offset = ioffset; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5482 | |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5483 | /* Note: this depends on the order of the members of PyHeapTypeObject! */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5484 | assert(offset >= 0); |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 5485 | assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer)); |
| 5486 | if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) { |
Martin v. Löwis | ee36d65 | 2006-04-11 09:08:02 +0000 | [diff] [blame] | 5487 | ptr = (char *)type->tp_as_sequence; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5488 | offset -= offsetof(PyHeapTypeObject, as_sequence); |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5489 | } |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 5490 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) { |
Martin v. Löwis | ee36d65 | 2006-04-11 09:08:02 +0000 | [diff] [blame] | 5491 | ptr = (char *)type->tp_as_mapping; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5492 | offset -= offsetof(PyHeapTypeObject, as_mapping); |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 5493 | } |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 5494 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) { |
Martin v. Löwis | ee36d65 | 2006-04-11 09:08:02 +0000 | [diff] [blame] | 5495 | ptr = (char *)type->tp_as_number; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5496 | offset -= offsetof(PyHeapTypeObject, as_number); |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5497 | } |
| 5498 | else { |
Martin v. Löwis | ee36d65 | 2006-04-11 09:08:02 +0000 | [diff] [blame] | 5499 | ptr = (char *)type; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5500 | } |
| 5501 | if (ptr != NULL) |
| 5502 | ptr += offset; |
| 5503 | return (void **)ptr; |
| 5504 | } |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 5505 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5506 | /* Length of array of slotdef pointers used to store slots with the |
| 5507 | same __name__. There should be at most MAX_EQUIV-1 slotdef entries with |
| 5508 | the same __name__, for any __name__. Since that's a static property, it is |
| 5509 | appropriate to declare fixed-size arrays for this. */ |
| 5510 | #define MAX_EQUIV 10 |
| 5511 | |
| 5512 | /* Return a slot pointer for a given name, but ONLY if the attribute has |
| 5513 | exactly one slot function. The name must be an interned string. */ |
| 5514 | static void ** |
| 5515 | resolve_slotdups(PyTypeObject *type, PyObject *name) |
| 5516 | { |
| 5517 | /* XXX Maybe this could be optimized more -- but is it worth it? */ |
| 5518 | |
| 5519 | /* pname and ptrs act as a little cache */ |
| 5520 | static PyObject *pname; |
| 5521 | static slotdef *ptrs[MAX_EQUIV]; |
| 5522 | slotdef *p, **pp; |
| 5523 | void **res, **ptr; |
| 5524 | |
| 5525 | if (pname != name) { |
| 5526 | /* Collect all slotdefs that match name into ptrs. */ |
| 5527 | pname = name; |
| 5528 | pp = ptrs; |
| 5529 | for (p = slotdefs; p->name_strobj; p++) { |
| 5530 | if (p->name_strobj == name) |
| 5531 | *pp++ = p; |
| 5532 | } |
| 5533 | *pp = NULL; |
| 5534 | } |
| 5535 | |
| 5536 | /* Look in all matching slots of the type; if exactly one of these has |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5537 | a filled-in slot, return its value. Otherwise return NULL. */ |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5538 | res = NULL; |
| 5539 | for (pp = ptrs; *pp; pp++) { |
| 5540 | ptr = slotptr(type, (*pp)->offset); |
| 5541 | if (ptr == NULL || *ptr == NULL) |
| 5542 | continue; |
| 5543 | if (res != NULL) |
| 5544 | return NULL; |
| 5545 | res = ptr; |
| 5546 | } |
| 5547 | return res; |
| 5548 | } |
| 5549 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5550 | /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5551 | does some incredibly complex thinking and then sticks something into the |
| 5552 | slot. (It sees if the adjacent slotdefs for the same slot have conflicting |
| 5553 | interests, and then stores a generic wrapper or a specific function into |
| 5554 | the slot.) Return a pointer to the next slotdef with a different offset, |
| 5555 | because that's convenient for fixup_slot_dispatchers(). */ |
| 5556 | static slotdef * |
| 5557 | update_one_slot(PyTypeObject *type, slotdef *p) |
| 5558 | { |
| 5559 | PyObject *descr; |
| 5560 | PyWrapperDescrObject *d; |
| 5561 | void *generic = NULL, *specific = NULL; |
| 5562 | int use_generic = 0; |
| 5563 | int offset = p->offset; |
| 5564 | void **ptr = slotptr(type, offset); |
| 5565 | |
| 5566 | if (ptr == NULL) { |
| 5567 | do { |
| 5568 | ++p; |
| 5569 | } while (p->offset == offset); |
| 5570 | return p; |
| 5571 | } |
| 5572 | do { |
| 5573 | descr = _PyType_Lookup(type, p->name_strobj); |
| 5574 | if (descr == NULL) |
| 5575 | continue; |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5576 | if (Py_Type(descr) == &PyWrapperDescr_Type) { |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5577 | void **tptr = resolve_slotdups(type, p->name_strobj); |
| 5578 | if (tptr == NULL || tptr == ptr) |
| 5579 | generic = p->function; |
| 5580 | d = (PyWrapperDescrObject *)descr; |
| 5581 | if (d->d_base->wrapper == p->wrapper && |
| 5582 | PyType_IsSubtype(type, d->d_type)) |
| 5583 | { |
| 5584 | if (specific == NULL || |
| 5585 | specific == d->d_wrapped) |
| 5586 | specific = d->d_wrapped; |
| 5587 | else |
| 5588 | use_generic = 1; |
| 5589 | } |
| 5590 | } |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5591 | else if (Py_Type(descr) == &PyCFunction_Type && |
Guido van Rossum | 721f62e | 2002-08-09 02:14:34 +0000 | [diff] [blame] | 5592 | PyCFunction_GET_FUNCTION(descr) == |
| 5593 | (PyCFunction)tp_new_wrapper && |
| 5594 | strcmp(p->name, "__new__") == 0) |
| 5595 | { |
| 5596 | /* The __new__ wrapper is not a wrapper descriptor, |
| 5597 | so must be special-cased differently. |
| 5598 | If we don't do this, creating an instance will |
| 5599 | always use slot_tp_new which will look up |
| 5600 | __new__ in the MRO which will call tp_new_wrapper |
| 5601 | which will look through the base classes looking |
| 5602 | for a static base and call its tp_new (usually |
| 5603 | PyType_GenericNew), after performing various |
| 5604 | sanity checks and constructing a new argument |
| 5605 | list. Cut all that nonsense short -- this speeds |
| 5606 | up instance creation tremendously. */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5607 | specific = (void *)type->tp_new; |
Guido van Rossum | 721f62e | 2002-08-09 02:14:34 +0000 | [diff] [blame] | 5608 | /* XXX I'm not 100% sure that there isn't a hole |
| 5609 | in this reasoning that requires additional |
| 5610 | sanity checks. I'll buy the first person to |
| 5611 | point out a bug in this reasoning a beer. */ |
| 5612 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5613 | else { |
| 5614 | use_generic = 1; |
| 5615 | generic = p->function; |
| 5616 | } |
| 5617 | } while ((++p)->offset == offset); |
| 5618 | if (specific && !use_generic) |
| 5619 | *ptr = specific; |
| 5620 | else |
| 5621 | *ptr = generic; |
| 5622 | return p; |
| 5623 | } |
| 5624 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5625 | /* In the type, update the slots whose slotdefs are gathered in the pp array. |
| 5626 | This is a callback for update_subclasses(). */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5627 | static int |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5628 | update_slots_callback(PyTypeObject *type, void *data) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5629 | { |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5630 | slotdef **pp = (slotdef **)data; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5631 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5632 | for (; *pp; pp++) |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5633 | update_one_slot(type, *pp); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5634 | return 0; |
| 5635 | } |
| 5636 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5637 | /* Comparison function for qsort() to compare slotdefs by their offset, and |
| 5638 | for equal offset by their address (to force a stable sort). */ |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5639 | static int |
| 5640 | slotdef_cmp(const void *aa, const void *bb) |
| 5641 | { |
| 5642 | const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb; |
| 5643 | int c = a->offset - b->offset; |
| 5644 | if (c != 0) |
| 5645 | return c; |
| 5646 | else |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5647 | /* Cannot use a-b, as this gives off_t, |
| 5648 | which may lose precision when converted to int. */ |
| 5649 | return (a > b) ? 1 : (a < b) ? -1 : 0; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5650 | } |
| 5651 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5652 | /* Initialize the slotdefs table by adding interned string objects for the |
| 5653 | names and sorting the entries. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5654 | static void |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5655 | init_slotdefs(void) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5656 | { |
| 5657 | slotdef *p; |
| 5658 | static int initialized = 0; |
| 5659 | |
| 5660 | if (initialized) |
| 5661 | return; |
| 5662 | for (p = slotdefs; p->name; p++) { |
| 5663 | p->name_strobj = PyString_InternFromString(p->name); |
| 5664 | if (!p->name_strobj) |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5665 | Py_FatalError("Out of memory interning slotdef names"); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5666 | } |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 5667 | qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef), |
| 5668 | slotdef_cmp); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5669 | initialized = 1; |
| 5670 | } |
| 5671 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5672 | /* Update the slots after assignment to a class (type) attribute. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5673 | static int |
| 5674 | update_slot(PyTypeObject *type, PyObject *name) |
| 5675 | { |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5676 | slotdef *ptrs[MAX_EQUIV]; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5677 | slotdef *p; |
| 5678 | slotdef **pp; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 5679 | int offset; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5680 | |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 5681 | init_slotdefs(); |
| 5682 | pp = ptrs; |
| 5683 | for (p = slotdefs; p->name; p++) { |
| 5684 | /* XXX assume name is interned! */ |
| 5685 | if (p->name_strobj == name) |
| 5686 | *pp++ = p; |
| 5687 | } |
| 5688 | *pp = NULL; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5689 | for (pp = ptrs; *pp; pp++) { |
| 5690 | p = *pp; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 5691 | offset = p->offset; |
| 5692 | while (p > slotdefs && (p-1)->offset == offset) |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5693 | --p; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 5694 | *pp = p; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5695 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5696 | if (ptrs[0] == NULL) |
| 5697 | return 0; /* Not an attribute that affects any slots */ |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5698 | return update_subclasses(type, name, |
| 5699 | update_slots_callback, (void *)ptrs); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5700 | } |
| 5701 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5702 | /* Store the proper functions in the slot dispatches at class (type) |
| 5703 | definition time, based upon which operations the class overrides in its |
| 5704 | dict. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5705 | static void |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5706 | fixup_slot_dispatchers(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5707 | { |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5708 | slotdef *p; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5709 | |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5710 | init_slotdefs(); |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5711 | for (p = slotdefs; p->name; ) |
| 5712 | p = update_one_slot(type, p); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5713 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5714 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 5715 | static void |
| 5716 | update_all_slots(PyTypeObject* type) |
| 5717 | { |
| 5718 | slotdef *p; |
| 5719 | |
| 5720 | init_slotdefs(); |
| 5721 | for (p = slotdefs; p->name; p++) { |
| 5722 | /* update_slot returns int but can't actually fail */ |
| 5723 | update_slot(type, p->name_strobj); |
| 5724 | } |
| 5725 | } |
| 5726 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5727 | /* recurse_down_subclasses() and update_subclasses() are mutually |
| 5728 | recursive functions to call a callback for all subclasses, |
| 5729 | but refraining from recursing into subclasses that define 'name'. */ |
| 5730 | |
| 5731 | static int |
| 5732 | update_subclasses(PyTypeObject *type, PyObject *name, |
| 5733 | update_callback callback, void *data) |
| 5734 | { |
| 5735 | if (callback(type, data) < 0) |
| 5736 | return -1; |
| 5737 | return recurse_down_subclasses(type, name, callback, data); |
| 5738 | } |
| 5739 | |
| 5740 | static int |
| 5741 | recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
| 5742 | update_callback callback, void *data) |
| 5743 | { |
| 5744 | PyTypeObject *subclass; |
| 5745 | PyObject *ref, *subclasses, *dict; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5746 | Py_ssize_t i, n; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5747 | |
| 5748 | subclasses = type->tp_subclasses; |
| 5749 | if (subclasses == NULL) |
| 5750 | return 0; |
| 5751 | assert(PyList_Check(subclasses)); |
| 5752 | n = PyList_GET_SIZE(subclasses); |
| 5753 | for (i = 0; i < n; i++) { |
| 5754 | ref = PyList_GET_ITEM(subclasses, i); |
| 5755 | assert(PyWeakref_CheckRef(ref)); |
| 5756 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 5757 | assert(subclass != NULL); |
| 5758 | if ((PyObject *)subclass == Py_None) |
| 5759 | continue; |
| 5760 | assert(PyType_Check(subclass)); |
| 5761 | /* Avoid recursing down into unaffected classes */ |
| 5762 | dict = subclass->tp_dict; |
| 5763 | if (dict != NULL && PyDict_Check(dict) && |
| 5764 | PyDict_GetItem(dict, name) != NULL) |
| 5765 | continue; |
| 5766 | if (update_subclasses(subclass, name, callback, data) < 0) |
| 5767 | return -1; |
| 5768 | } |
| 5769 | return 0; |
| 5770 | } |
| 5771 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5772 | /* This function is called by PyType_Ready() to populate the type's |
| 5773 | dictionary with method descriptors for function slots. For each |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 5774 | function slot (like tp_repr) that's defined in the type, one or more |
| 5775 | corresponding descriptors are added in the type's tp_dict dictionary |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5776 | under the appropriate name (like __repr__). Some function slots |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 5777 | cause more than one descriptor to be added (for example, the nb_add |
| 5778 | slot adds both __add__ and __radd__ descriptors) and some function |
| 5779 | slots compete for the same descriptor (for example both sq_item and |
| 5780 | mp_subscript generate a __getitem__ descriptor). |
| 5781 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5782 | In the latter case, the first slotdef entry encoutered wins. Since |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5783 | 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] | 5784 | PyHeapTypeObject, this gives us some control over disambiguating |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5785 | between competing slots: the members of PyHeapTypeObject are listed |
| 5786 | from most general to least general, so the most general slot is |
| 5787 | preferred. In particular, because as_mapping comes before as_sequence, |
| 5788 | for a type that defines both mp_subscript and sq_item, mp_subscript |
| 5789 | wins. |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 5790 | |
| 5791 | This only adds new descriptors and doesn't overwrite entries in |
| 5792 | tp_dict that were previously defined. The descriptors contain a |
| 5793 | reference to the C function they must call, so that it's safe if they |
| 5794 | are copied into a subtype's __dict__ and the subtype has a different |
| 5795 | C function in its slot -- calling the method defined by the |
| 5796 | descriptor will call the C function that was used to create it, |
| 5797 | rather than the C function present in the slot when it is called. |
| 5798 | (This is important because a subtype may have a C function in the |
| 5799 | slot that calls the method from the dictionary, and we want to avoid |
| 5800 | infinite recursion here.) */ |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5801 | |
| 5802 | static int |
| 5803 | add_operators(PyTypeObject *type) |
| 5804 | { |
| 5805 | PyObject *dict = type->tp_dict; |
| 5806 | slotdef *p; |
| 5807 | PyObject *descr; |
| 5808 | void **ptr; |
| 5809 | |
| 5810 | init_slotdefs(); |
| 5811 | for (p = slotdefs; p->name; p++) { |
| 5812 | if (p->wrapper == NULL) |
| 5813 | continue; |
| 5814 | ptr = slotptr(type, p->offset); |
| 5815 | if (!ptr || !*ptr) |
| 5816 | continue; |
| 5817 | if (PyDict_GetItem(dict, p->name_strobj)) |
| 5818 | continue; |
| 5819 | descr = PyDescr_NewWrapper(type, p, *ptr); |
| 5820 | if (descr == NULL) |
| 5821 | return -1; |
| 5822 | if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) |
| 5823 | return -1; |
| 5824 | Py_DECREF(descr); |
| 5825 | } |
| 5826 | if (type->tp_new != NULL) { |
| 5827 | if (add_tp_new_wrapper(type) < 0) |
| 5828 | return -1; |
| 5829 | } |
| 5830 | return 0; |
| 5831 | } |
| 5832 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5833 | |
| 5834 | /* Cooperative 'super' */ |
| 5835 | |
| 5836 | typedef struct { |
| 5837 | PyObject_HEAD |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 5838 | PyTypeObject *type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5839 | PyObject *obj; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 5840 | PyTypeObject *obj_type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5841 | } superobject; |
| 5842 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 5843 | static PyMemberDef super_members[] = { |
| 5844 | {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY, |
| 5845 | "the class invoking super()"}, |
| 5846 | {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY, |
| 5847 | "the instance invoking super(); may be None"}, |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 5848 | {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY, |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 5849 | "the type of the instance invoking super(); may be None"}, |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 5850 | {0} |
| 5851 | }; |
| 5852 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5853 | static void |
| 5854 | super_dealloc(PyObject *self) |
| 5855 | { |
| 5856 | superobject *su = (superobject *)self; |
| 5857 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 5858 | _PyObject_GC_UNTRACK(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5859 | Py_XDECREF(su->obj); |
| 5860 | Py_XDECREF(su->type); |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 5861 | Py_XDECREF(su->obj_type); |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5862 | Py_Type(self)->tp_free(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5863 | } |
| 5864 | |
| 5865 | static PyObject * |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 5866 | super_repr(PyObject *self) |
| 5867 | { |
| 5868 | superobject *su = (superobject *)self; |
| 5869 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 5870 | if (su->obj_type) |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 5871 | return PyString_FromFormat( |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 5872 | "<super: <class '%s'>, <%s object>>", |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 5873 | su->type ? su->type->tp_name : "NULL", |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 5874 | su->obj_type->tp_name); |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 5875 | else |
| 5876 | return PyString_FromFormat( |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 5877 | "<super: <class '%s'>, NULL>", |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 5878 | su->type ? su->type->tp_name : "NULL"); |
| 5879 | } |
| 5880 | |
| 5881 | static PyObject * |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5882 | super_getattro(PyObject *self, PyObject *name) |
| 5883 | { |
| 5884 | superobject *su = (superobject *)self; |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 5885 | int skip = su->obj_type == NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5886 | |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 5887 | if (!skip) { |
| 5888 | /* We want __class__ to return the class of the super object |
| 5889 | (i.e. super, or a subclass), not the class of su->obj. */ |
| 5890 | skip = (PyString_Check(name) && |
| 5891 | PyString_GET_SIZE(name) == 9 && |
| 5892 | strcmp(PyString_AS_STRING(name), "__class__") == 0); |
| 5893 | } |
| 5894 | |
| 5895 | if (!skip) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 5896 | PyObject *mro, *res, *tmp, *dict; |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 5897 | PyTypeObject *starttype; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5898 | descrgetfunc f; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5899 | Py_ssize_t i, n; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5900 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 5901 | starttype = su->obj_type; |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 5902 | mro = starttype->tp_mro; |
| 5903 | |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 5904 | if (mro == NULL) |
| 5905 | n = 0; |
| 5906 | else { |
| 5907 | assert(PyTuple_Check(mro)); |
| 5908 | n = PyTuple_GET_SIZE(mro); |
| 5909 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5910 | for (i = 0; i < n; i++) { |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 5911 | if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5912 | break; |
| 5913 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5914 | i++; |
| 5915 | res = NULL; |
| 5916 | for (; i < n; i++) { |
| 5917 | tmp = PyTuple_GET_ITEM(mro, i); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 5918 | if (PyType_Check(tmp)) |
| 5919 | dict = ((PyTypeObject *)tmp)->tp_dict; |
| 5920 | else if (PyClass_Check(tmp)) |
| 5921 | dict = ((PyClassObject *)tmp)->cl_dict; |
| 5922 | else |
| 5923 | continue; |
| 5924 | res = PyDict_GetItem(dict, name); |
Guido van Rossum | 6cc5bb6 | 2003-04-16 20:01:36 +0000 | [diff] [blame] | 5925 | if (res != NULL) { |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5926 | Py_INCREF(res); |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5927 | f = Py_Type(res)->tp_descr_get; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5928 | if (f != NULL) { |
Phillip J. Eby | 91a968a | 2004-03-25 02:19:34 +0000 | [diff] [blame] | 5929 | tmp = f(res, |
| 5930 | /* Only pass 'obj' param if |
| 5931 | this is instance-mode super |
| 5932 | (See SF ID #743627) |
| 5933 | */ |
Hye-Shik Chang | ff365c9 | 2004-03-25 16:37:03 +0000 | [diff] [blame] | 5934 | (su->obj == (PyObject *) |
| 5935 | su->obj_type |
Phillip J. Eby | 91a968a | 2004-03-25 02:19:34 +0000 | [diff] [blame] | 5936 | ? (PyObject *)NULL |
| 5937 | : su->obj), |
Guido van Rossum | d464107 | 2002-04-03 02:13:37 +0000 | [diff] [blame] | 5938 | (PyObject *)starttype); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5939 | Py_DECREF(res); |
| 5940 | res = tmp; |
| 5941 | } |
| 5942 | return res; |
| 5943 | } |
| 5944 | } |
| 5945 | } |
| 5946 | return PyObject_GenericGetAttr(self, name); |
| 5947 | } |
| 5948 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 5949 | static PyTypeObject * |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 5950 | supercheck(PyTypeObject *type, PyObject *obj) |
| 5951 | { |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 5952 | /* Check that a super() call makes sense. Return a type object. |
| 5953 | |
| 5954 | obj can be a new-style class, or an instance of one: |
| 5955 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5956 | - If it is a class, it must be a subclass of 'type'. This case is |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 5957 | used for class methods; the return value is obj. |
| 5958 | |
| 5959 | - If it is an instance, it must be an instance of 'type'. This is |
| 5960 | the normal case; the return value is obj.__class__. |
| 5961 | |
| 5962 | But... when obj is an instance, we want to allow for the case where |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5963 | Py_Type(obj) is not a subclass of type, but obj.__class__ is! |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 5964 | This will allow using super() with a proxy for obj. |
| 5965 | */ |
| 5966 | |
Guido van Rossum | 8e80a72 | 2003-02-18 19:22:22 +0000 | [diff] [blame] | 5967 | /* Check for first bullet above (special case) */ |
| 5968 | if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) { |
| 5969 | Py_INCREF(obj); |
| 5970 | return (PyTypeObject *)obj; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 5971 | } |
Guido van Rossum | 8e80a72 | 2003-02-18 19:22:22 +0000 | [diff] [blame] | 5972 | |
| 5973 | /* Normal case */ |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5974 | if (PyType_IsSubtype(Py_Type(obj), type)) { |
| 5975 | Py_INCREF(Py_Type(obj)); |
| 5976 | return Py_Type(obj); |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 5977 | } |
| 5978 | else { |
| 5979 | /* Try the slow way */ |
| 5980 | static PyObject *class_str = NULL; |
| 5981 | PyObject *class_attr; |
| 5982 | |
| 5983 | if (class_str == NULL) { |
| 5984 | class_str = PyString_FromString("__class__"); |
| 5985 | if (class_str == NULL) |
| 5986 | return NULL; |
| 5987 | } |
| 5988 | |
| 5989 | class_attr = PyObject_GetAttr(obj, class_str); |
| 5990 | |
| 5991 | if (class_attr != NULL && |
| 5992 | PyType_Check(class_attr) && |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5993 | (PyTypeObject *)class_attr != Py_Type(obj)) |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 5994 | { |
| 5995 | int ok = PyType_IsSubtype( |
| 5996 | (PyTypeObject *)class_attr, type); |
| 5997 | if (ok) |
| 5998 | return (PyTypeObject *)class_attr; |
| 5999 | } |
| 6000 | |
| 6001 | if (class_attr == NULL) |
| 6002 | PyErr_Clear(); |
| 6003 | else |
| 6004 | Py_DECREF(class_attr); |
| 6005 | } |
| 6006 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6007 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6008 | "super(type, obj): " |
| 6009 | "obj must be an instance or subtype of type"); |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6010 | return NULL; |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6011 | } |
| 6012 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6013 | static PyObject * |
| 6014 | super_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 6015 | { |
| 6016 | superobject *su = (superobject *)self; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 6017 | superobject *newobj; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6018 | |
| 6019 | if (obj == NULL || obj == Py_None || su->obj != NULL) { |
| 6020 | /* Not binding to an object, or already bound */ |
| 6021 | Py_INCREF(self); |
| 6022 | return self; |
| 6023 | } |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 6024 | if (Py_Type(su) != &PySuper_Type) |
Armin Rigo | 7726dc0 | 2005-05-15 15:32:08 +0000 | [diff] [blame] | 6025 | /* If su is an instance of a (strict) subclass of super, |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6026 | call its type */ |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 6027 | return PyObject_CallFunctionObjArgs((PyObject *)Py_Type(su), |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6028 | su->type, obj, NULL); |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6029 | else { |
| 6030 | /* Inline the common case */ |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6031 | PyTypeObject *obj_type = supercheck(su->type, obj); |
| 6032 | if (obj_type == NULL) |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6033 | return NULL; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 6034 | newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type, |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6035 | NULL, NULL); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 6036 | if (newobj == NULL) |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6037 | return NULL; |
| 6038 | Py_INCREF(su->type); |
| 6039 | Py_INCREF(obj); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 6040 | newobj->type = su->type; |
| 6041 | newobj->obj = obj; |
| 6042 | newobj->obj_type = obj_type; |
| 6043 | return (PyObject *)newobj; |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6044 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6045 | } |
| 6046 | |
| 6047 | static int |
| 6048 | super_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 6049 | { |
| 6050 | superobject *su = (superobject *)self; |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6051 | PyTypeObject *type; |
| 6052 | PyObject *obj = NULL; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6053 | PyTypeObject *obj_type = NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6054 | |
Georg Brandl | 5d59c09 | 2006-09-30 08:43:30 +0000 | [diff] [blame] | 6055 | if (!_PyArg_NoKeywords("super", kwds)) |
| 6056 | return -1; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6057 | if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj)) |
| 6058 | return -1; |
| 6059 | if (obj == Py_None) |
| 6060 | obj = NULL; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6061 | if (obj != NULL) { |
| 6062 | obj_type = supercheck(type, obj); |
| 6063 | if (obj_type == NULL) |
| 6064 | return -1; |
| 6065 | Py_INCREF(obj); |
| 6066 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6067 | Py_INCREF(type); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6068 | su->type = type; |
| 6069 | su->obj = obj; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6070 | su->obj_type = obj_type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6071 | return 0; |
| 6072 | } |
| 6073 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6074 | PyDoc_STRVAR(super_doc, |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6075 | "super(type) -> unbound super object\n" |
| 6076 | "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6077 | "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6078 | "Typical use to call a cooperative superclass method:\n" |
| 6079 | "class C(B):\n" |
| 6080 | " def meth(self, arg):\n" |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6081 | " super(C, self).meth(arg)"); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6082 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6083 | static int |
| 6084 | super_traverse(PyObject *self, visitproc visit, void *arg) |
| 6085 | { |
| 6086 | superobject *su = (superobject *)self; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6087 | |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 6088 | Py_VISIT(su->obj); |
| 6089 | Py_VISIT(su->type); |
| 6090 | Py_VISIT(su->obj_type); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6091 | |
| 6092 | return 0; |
| 6093 | } |
| 6094 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6095 | PyTypeObject PySuper_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 6096 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6097 | "super", /* tp_name */ |
| 6098 | sizeof(superobject), /* tp_basicsize */ |
| 6099 | 0, /* tp_itemsize */ |
| 6100 | /* methods */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6101 | super_dealloc, /* tp_dealloc */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6102 | 0, /* tp_print */ |
| 6103 | 0, /* tp_getattr */ |
| 6104 | 0, /* tp_setattr */ |
| 6105 | 0, /* tp_compare */ |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6106 | super_repr, /* tp_repr */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6107 | 0, /* tp_as_number */ |
| 6108 | 0, /* tp_as_sequence */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6109 | 0, /* tp_as_mapping */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6110 | 0, /* tp_hash */ |
| 6111 | 0, /* tp_call */ |
| 6112 | 0, /* tp_str */ |
| 6113 | super_getattro, /* tp_getattro */ |
| 6114 | 0, /* tp_setattro */ |
| 6115 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6116 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 6117 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6118 | super_doc, /* tp_doc */ |
| 6119 | super_traverse, /* tp_traverse */ |
| 6120 | 0, /* tp_clear */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6121 | 0, /* tp_richcompare */ |
| 6122 | 0, /* tp_weaklistoffset */ |
| 6123 | 0, /* tp_iter */ |
| 6124 | 0, /* tp_iternext */ |
| 6125 | 0, /* tp_methods */ |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6126 | super_members, /* tp_members */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6127 | 0, /* tp_getset */ |
| 6128 | 0, /* tp_base */ |
| 6129 | 0, /* tp_dict */ |
| 6130 | super_descr_get, /* tp_descr_get */ |
| 6131 | 0, /* tp_descr_set */ |
| 6132 | 0, /* tp_dictoffset */ |
| 6133 | super_init, /* tp_init */ |
| 6134 | PyType_GenericAlloc, /* tp_alloc */ |
| 6135 | PyType_GenericNew, /* tp_new */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6136 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6137 | }; |