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