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