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