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