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 | |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 8 | |
| 9 | /* Support type attribute cache */ |
| 10 | |
| 11 | /* The cache can keep references to the names alive for longer than |
| 12 | they normally would. This is why the maximum size is limited to |
| 13 | MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large |
| 14 | strings are used as attribute names. */ |
| 15 | #define MCACHE_MAX_ATTR_SIZE 100 |
| 16 | #define MCACHE_SIZE_EXP 10 |
| 17 | #define MCACHE_HASH(version, name_hash) \ |
| 18 | (((unsigned int)(version) * (unsigned int)(name_hash)) \ |
| 19 | >> (8*sizeof(unsigned int) - MCACHE_SIZE_EXP)) |
| 20 | #define MCACHE_HASH_METHOD(type, name) \ |
| 21 | MCACHE_HASH((type)->tp_version_tag, \ |
| 22 | ((PyStringObject *)(name))->ob_shash) |
| 23 | #define MCACHE_CACHEABLE_NAME(name) \ |
| 24 | PyString_CheckExact(name) && \ |
| 25 | PyString_GET_SIZE(name) <= MCACHE_MAX_ATTR_SIZE |
| 26 | |
| 27 | struct method_cache_entry { |
| 28 | unsigned int version; |
| 29 | PyObject *name; /* reference to exactly a str or None */ |
| 30 | PyObject *value; /* borrowed */ |
| 31 | }; |
| 32 | |
| 33 | static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP]; |
| 34 | static unsigned int next_version_tag = 0; |
Christian Heimes | 908caac | 2008-01-27 23:34:59 +0000 | [diff] [blame] | 35 | static void type_modified(PyTypeObject *); |
| 36 | |
| 37 | unsigned int |
| 38 | PyType_ClearCache(void) |
| 39 | { |
| 40 | Py_ssize_t i; |
| 41 | unsigned int cur_version_tag = next_version_tag - 1; |
| 42 | |
| 43 | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { |
| 44 | method_cache[i].version = 0; |
| 45 | Py_CLEAR(method_cache[i].name); |
| 46 | method_cache[i].value = NULL; |
| 47 | } |
| 48 | next_version_tag = 0; |
| 49 | /* mark all version tags as invalid */ |
| 50 | type_modified(&PyBaseObject_Type); |
| 51 | return cur_version_tag; |
| 52 | } |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 53 | |
| 54 | static void |
| 55 | type_modified(PyTypeObject *type) |
| 56 | { |
| 57 | /* Invalidate any cached data for the specified type and all |
| 58 | subclasses. This function is called after the base |
| 59 | classes, mro, or attributes of the type are altered. |
| 60 | |
| 61 | Invariants: |
| 62 | |
| 63 | - Py_TPFLAGS_VALID_VERSION_TAG is never set if |
| 64 | Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type |
| 65 | objects coming from non-recompiled extension modules) |
| 66 | |
| 67 | - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type, |
| 68 | it must first be set on all super types. |
| 69 | |
| 70 | This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a |
| 71 | type (so it must first clear it on all subclasses). The |
| 72 | tp_version_tag value is meaningless unless this flag is set. |
| 73 | We don't assign new version tags eagerly, but only as |
| 74 | needed. |
| 75 | */ |
| 76 | PyObject *raw, *ref; |
| 77 | Py_ssize_t i, n; |
| 78 | |
Neal Norwitz | e7bb918 | 2008-01-27 17:10:14 +0000 | [diff] [blame] | 79 | if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 80 | return; |
| 81 | |
| 82 | raw = type->tp_subclasses; |
| 83 | if (raw != NULL) { |
| 84 | n = PyList_GET_SIZE(raw); |
| 85 | for (i = 0; i < n; i++) { |
| 86 | ref = PyList_GET_ITEM(raw, i); |
| 87 | ref = PyWeakref_GET_OBJECT(ref); |
| 88 | if (ref != Py_None) { |
| 89 | type_modified((PyTypeObject *)ref); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG; |
| 94 | } |
| 95 | |
| 96 | static void |
| 97 | type_mro_modified(PyTypeObject *type, PyObject *bases) { |
| 98 | /* |
| 99 | Check that all base classes or elements of the mro of type are |
| 100 | able to be cached. This function is called after the base |
| 101 | classes or mro of the type are altered. |
| 102 | |
| 103 | Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type |
| 104 | inherits from an old-style class, either directly or if it |
| 105 | appears in the MRO of a new-style class. No support either for |
| 106 | custom MROs that include types that are not officially super |
| 107 | types. |
| 108 | |
| 109 | Called from mro_internal, which will subsequently be called on |
| 110 | each subclass when their mro is recursively updated. |
| 111 | */ |
| 112 | Py_ssize_t i, n; |
| 113 | int clear = 0; |
| 114 | |
Neal Norwitz | e7bb918 | 2008-01-27 17:10:14 +0000 | [diff] [blame] | 115 | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 116 | return; |
| 117 | |
| 118 | n = PyTuple_GET_SIZE(bases); |
| 119 | for (i = 0; i < n; i++) { |
| 120 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 121 | PyTypeObject *cls; |
| 122 | |
| 123 | if (!PyType_Check(b) ) { |
| 124 | clear = 1; |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | cls = (PyTypeObject *)b; |
| 129 | |
| 130 | if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) || |
| 131 | !PyType_IsSubtype(type, cls)) { |
| 132 | clear = 1; |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (clear) |
| 138 | type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG| |
| 139 | Py_TPFLAGS_VALID_VERSION_TAG); |
| 140 | } |
| 141 | |
| 142 | static int |
| 143 | assign_version_tag(PyTypeObject *type) |
| 144 | { |
| 145 | /* Ensure that the tp_version_tag is valid and set |
| 146 | Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this |
| 147 | must first be done on all super classes. Return 0 if this |
| 148 | cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG. |
| 149 | */ |
| 150 | Py_ssize_t i, n; |
| 151 | PyObject *bases; |
| 152 | |
| 153 | if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
| 154 | return 1; |
| 155 | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) |
| 156 | return 0; |
| 157 | if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) |
| 158 | return 0; |
| 159 | |
| 160 | type->tp_version_tag = next_version_tag++; |
| 161 | /* for stress-testing: next_version_tag &= 0xFF; */ |
| 162 | |
| 163 | if (type->tp_version_tag == 0) { |
| 164 | /* wrap-around or just starting Python - clear the whole |
| 165 | cache by filling names with references to Py_None. |
| 166 | Values are also set to NULL for added protection, as they |
| 167 | are borrowed reference */ |
| 168 | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { |
| 169 | method_cache[i].value = NULL; |
| 170 | Py_XDECREF(method_cache[i].name); |
| 171 | method_cache[i].name = Py_None; |
| 172 | Py_INCREF(Py_None); |
| 173 | } |
| 174 | /* mark all version tags as invalid */ |
| 175 | type_modified(&PyBaseObject_Type); |
| 176 | return 1; |
| 177 | } |
| 178 | bases = type->tp_bases; |
| 179 | n = PyTuple_GET_SIZE(bases); |
| 180 | for (i = 0; i < n; i++) { |
| 181 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 182 | assert(PyType_Check(b)); |
| 183 | if (!assign_version_tag((PyTypeObject *)b)) |
| 184 | return 0; |
| 185 | } |
| 186 | type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG; |
| 187 | return 1; |
| 188 | } |
| 189 | |
| 190 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 191 | static PyMemberDef type_members[] = { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 192 | {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY}, |
| 193 | {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY}, |
| 194 | {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY}, |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 195 | {"__weakrefoffset__", T_LONG, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 196 | offsetof(PyTypeObject, tp_weaklistoffset), READONLY}, |
| 197 | {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY}, |
| 198 | {"__dictoffset__", T_LONG, |
| 199 | offsetof(PyTypeObject, tp_dictoffset), READONLY}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 200 | {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY}, |
| 201 | {0} |
| 202 | }; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 204 | static PyObject * |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 205 | type_name(PyTypeObject *type, void *context) |
| 206 | { |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 207 | const char *s; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 208 | |
Michael W. Hudson | ade8c8b | 2002-11-27 16:29:26 +0000 | [diff] [blame] | 209 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 210 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 211 | |
Georg Brandl | c255c7b | 2006-02-20 22:27:28 +0000 | [diff] [blame] | 212 | Py_INCREF(et->ht_name); |
| 213 | return et->ht_name; |
Michael W. Hudson | ade8c8b | 2002-11-27 16:29:26 +0000 | [diff] [blame] | 214 | } |
| 215 | else { |
| 216 | s = strrchr(type->tp_name, '.'); |
| 217 | if (s == NULL) |
| 218 | s = type->tp_name; |
| 219 | else |
| 220 | s++; |
| 221 | return PyString_FromString(s); |
| 222 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 225 | static int |
| 226 | type_set_name(PyTypeObject *type, PyObject *value, void *context) |
| 227 | { |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 228 | PyHeapTypeObject* et; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 229 | |
| 230 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 231 | PyErr_Format(PyExc_TypeError, |
| 232 | "can't set %s.__name__", type->tp_name); |
| 233 | return -1; |
| 234 | } |
| 235 | if (!value) { |
| 236 | PyErr_Format(PyExc_TypeError, |
| 237 | "can't delete %s.__name__", type->tp_name); |
| 238 | return -1; |
| 239 | } |
| 240 | if (!PyString_Check(value)) { |
| 241 | PyErr_Format(PyExc_TypeError, |
| 242 | "can only assign string to %s.__name__, not '%s'", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 243 | type->tp_name, Py_TYPE(value)->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 244 | return -1; |
| 245 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 246 | if (strlen(PyString_AS_STRING(value)) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 247 | != (size_t)PyString_GET_SIZE(value)) { |
| 248 | PyErr_Format(PyExc_ValueError, |
| 249 | "__name__ must not contain null bytes"); |
| 250 | return -1; |
| 251 | } |
| 252 | |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 253 | et = (PyHeapTypeObject*)type; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 254 | |
| 255 | Py_INCREF(value); |
| 256 | |
Georg Brandl | c255c7b | 2006-02-20 22:27:28 +0000 | [diff] [blame] | 257 | Py_DECREF(et->ht_name); |
| 258 | et->ht_name = value; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 259 | |
| 260 | type->tp_name = PyString_AS_STRING(value); |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 265 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 266 | type_module(PyTypeObject *type, void *context) |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 267 | { |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 268 | PyObject *mod; |
| 269 | char *s; |
| 270 | |
Michael W. Hudson | ade8c8b | 2002-11-27 16:29:26 +0000 | [diff] [blame] | 271 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 272 | mod = PyDict_GetItemString(type->tp_dict, "__module__"); |
Anthony Baxter | 3ecdb25 | 2004-06-11 14:41:18 +0000 | [diff] [blame] | 273 | if (!mod) { |
| 274 | PyErr_Format(PyExc_AttributeError, "__module__"); |
| 275 | return 0; |
| 276 | } |
Michael W. Hudson | ade8c8b | 2002-11-27 16:29:26 +0000 | [diff] [blame] | 277 | Py_XINCREF(mod); |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 278 | return mod; |
| 279 | } |
Michael W. Hudson | ade8c8b | 2002-11-27 16:29:26 +0000 | [diff] [blame] | 280 | else { |
| 281 | s = strrchr(type->tp_name, '.'); |
| 282 | if (s != NULL) |
| 283 | return PyString_FromStringAndSize( |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 284 | type->tp_name, (Py_ssize_t)(s - type->tp_name)); |
Michael W. Hudson | ade8c8b | 2002-11-27 16:29:26 +0000 | [diff] [blame] | 285 | return PyString_FromString("__builtin__"); |
| 286 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 289 | static int |
| 290 | type_set_module(PyTypeObject *type, PyObject *value, void *context) |
| 291 | { |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 292 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 293 | PyErr_Format(PyExc_TypeError, |
| 294 | "can't set %s.__module__", type->tp_name); |
| 295 | return -1; |
| 296 | } |
| 297 | if (!value) { |
| 298 | PyErr_Format(PyExc_TypeError, |
| 299 | "can't delete %s.__module__", type->tp_name); |
| 300 | return -1; |
| 301 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 302 | |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 303 | type_modified(type); |
| 304 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 305 | return PyDict_SetItemString(type->tp_dict, "__module__", value); |
| 306 | } |
| 307 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 308 | static PyObject * |
Jeffrey Yasskin | 960b9b7 | 2008-02-28 04:45:36 +0000 | [diff] [blame^] | 309 | type_abstractmethods(PyTypeObject *type, void *context) |
| 310 | { |
| 311 | PyObject *mod = PyDict_GetItemString(type->tp_dict, |
| 312 | "__abstractmethods__"); |
| 313 | if (!mod) { |
| 314 | PyErr_Format(PyExc_AttributeError, "__abstractmethods__"); |
| 315 | return NULL; |
| 316 | } |
| 317 | Py_XINCREF(mod); |
| 318 | return mod; |
| 319 | } |
| 320 | |
| 321 | static int |
| 322 | type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context) |
| 323 | { |
| 324 | /* __abstractmethods__ should only be set once on a type, in |
| 325 | abc.ABCMeta.__new__, so this function doesn't do anything |
| 326 | special to update subclasses. |
| 327 | */ |
| 328 | int res = PyDict_SetItemString(type->tp_dict, |
| 329 | "__abstractmethods__", value); |
| 330 | if (res == 0) { |
| 331 | type_modified(type); |
| 332 | if (value && PyObject_IsTrue(value)) { |
| 333 | type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT; |
| 334 | } |
| 335 | else { |
| 336 | type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT; |
| 337 | } |
| 338 | } |
| 339 | return res; |
| 340 | } |
| 341 | |
| 342 | static PyObject * |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 343 | type_get_bases(PyTypeObject *type, void *context) |
| 344 | { |
| 345 | Py_INCREF(type->tp_bases); |
| 346 | return type->tp_bases; |
| 347 | } |
| 348 | |
| 349 | static PyTypeObject *best_base(PyObject *); |
| 350 | static int mro_internal(PyTypeObject *); |
| 351 | static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *); |
| 352 | static int add_subclass(PyTypeObject*, PyTypeObject*); |
| 353 | static void remove_subclass(PyTypeObject *, PyTypeObject *); |
| 354 | static void update_all_slots(PyTypeObject *); |
| 355 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 356 | typedef int (*update_callback)(PyTypeObject *, void *); |
| 357 | static int update_subclasses(PyTypeObject *type, PyObject *name, |
| 358 | update_callback callback, void *data); |
| 359 | static int recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
| 360 | update_callback callback, void *data); |
| 361 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 362 | static int |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 363 | mro_subclasses(PyTypeObject *type, PyObject* temp) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 364 | { |
| 365 | PyTypeObject *subclass; |
| 366 | PyObject *ref, *subclasses, *old_mro; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 367 | Py_ssize_t i, n; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 368 | |
| 369 | subclasses = type->tp_subclasses; |
| 370 | if (subclasses == NULL) |
| 371 | return 0; |
| 372 | assert(PyList_Check(subclasses)); |
| 373 | n = PyList_GET_SIZE(subclasses); |
| 374 | for (i = 0; i < n; i++) { |
| 375 | ref = PyList_GET_ITEM(subclasses, i); |
| 376 | assert(PyWeakref_CheckRef(ref)); |
| 377 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 378 | assert(subclass != NULL); |
| 379 | if ((PyObject *)subclass == Py_None) |
| 380 | continue; |
| 381 | assert(PyType_Check(subclass)); |
| 382 | old_mro = subclass->tp_mro; |
| 383 | if (mro_internal(subclass) < 0) { |
| 384 | subclass->tp_mro = old_mro; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 385 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 386 | } |
| 387 | else { |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 388 | PyObject* tuple; |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 389 | tuple = PyTuple_Pack(2, subclass, old_mro); |
Guido van Rossum | 19a02ba | 2003-04-15 22:09:45 +0000 | [diff] [blame] | 390 | Py_DECREF(old_mro); |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 391 | if (!tuple) |
| 392 | return -1; |
| 393 | if (PyList_Append(temp, tuple) < 0) |
| 394 | return -1; |
Guido van Rossum | 19a02ba | 2003-04-15 22:09:45 +0000 | [diff] [blame] | 395 | Py_DECREF(tuple); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 396 | } |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 397 | if (mro_subclasses(subclass, temp) < 0) |
| 398 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 399 | } |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 400 | return 0; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | static int |
| 404 | type_set_bases(PyTypeObject *type, PyObject *value, void *context) |
| 405 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 406 | Py_ssize_t i; |
| 407 | int r = 0; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 408 | PyObject *ob, *temp; |
Armin Rigo | c0ba52d | 2007-04-19 14:44:48 +0000 | [diff] [blame] | 409 | PyTypeObject *new_base, *old_base; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 410 | PyObject *old_bases, *old_mro; |
| 411 | |
| 412 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 413 | PyErr_Format(PyExc_TypeError, |
| 414 | "can't set %s.__bases__", type->tp_name); |
| 415 | return -1; |
| 416 | } |
| 417 | if (!value) { |
| 418 | PyErr_Format(PyExc_TypeError, |
| 419 | "can't delete %s.__bases__", type->tp_name); |
| 420 | return -1; |
| 421 | } |
| 422 | if (!PyTuple_Check(value)) { |
| 423 | PyErr_Format(PyExc_TypeError, |
| 424 | "can only assign tuple to %s.__bases__, not %s", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 425 | type->tp_name, Py_TYPE(value)->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 426 | return -1; |
| 427 | } |
Guido van Rossum | 3bbc0ee | 2002-12-13 17:49:38 +0000 | [diff] [blame] | 428 | if (PyTuple_GET_SIZE(value) == 0) { |
| 429 | PyErr_Format(PyExc_TypeError, |
| 430 | "can only assign non-empty tuple to %s.__bases__, not ()", |
| 431 | type->tp_name); |
| 432 | return -1; |
| 433 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 434 | for (i = 0; i < PyTuple_GET_SIZE(value); i++) { |
| 435 | ob = PyTuple_GET_ITEM(value, i); |
| 436 | if (!PyClass_Check(ob) && !PyType_Check(ob)) { |
| 437 | PyErr_Format( |
| 438 | PyExc_TypeError, |
| 439 | "%s.__bases__ must be tuple of old- or new-style classes, not '%s'", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 440 | type->tp_name, Py_TYPE(ob)->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 441 | return -1; |
| 442 | } |
Michael W. Hudson | caf17be | 2002-11-27 10:24:44 +0000 | [diff] [blame] | 443 | if (PyType_Check(ob)) { |
| 444 | if (PyType_IsSubtype((PyTypeObject*)ob, type)) { |
| 445 | PyErr_SetString(PyExc_TypeError, |
| 446 | "a __bases__ item causes an inheritance cycle"); |
| 447 | return -1; |
| 448 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
| 452 | new_base = best_base(value); |
| 453 | |
| 454 | if (!new_base) { |
| 455 | return -1; |
| 456 | } |
| 457 | |
| 458 | if (!compatible_for_assignment(type->tp_base, new_base, "__bases__")) |
| 459 | return -1; |
| 460 | |
| 461 | Py_INCREF(new_base); |
| 462 | Py_INCREF(value); |
| 463 | |
| 464 | old_bases = type->tp_bases; |
| 465 | old_base = type->tp_base; |
| 466 | old_mro = type->tp_mro; |
| 467 | |
| 468 | type->tp_bases = value; |
| 469 | type->tp_base = new_base; |
| 470 | |
| 471 | if (mro_internal(type) < 0) { |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 472 | goto bail; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 475 | temp = PyList_New(0); |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 476 | if (!temp) |
| 477 | goto bail; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 478 | |
| 479 | r = mro_subclasses(type, temp); |
| 480 | |
| 481 | if (r < 0) { |
| 482 | for (i = 0; i < PyList_Size(temp); i++) { |
| 483 | PyTypeObject* cls; |
| 484 | PyObject* mro; |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 485 | PyArg_UnpackTuple(PyList_GET_ITEM(temp, i), |
| 486 | "", 2, 2, &cls, &mro); |
Armin Rigo | 796fc99 | 2007-04-19 14:56:48 +0000 | [diff] [blame] | 487 | Py_INCREF(mro); |
| 488 | ob = cls->tp_mro; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 489 | cls->tp_mro = mro; |
Armin Rigo | 796fc99 | 2007-04-19 14:56:48 +0000 | [diff] [blame] | 490 | Py_DECREF(ob); |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 491 | } |
| 492 | Py_DECREF(temp); |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 493 | goto bail; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | Py_DECREF(temp); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 497 | |
| 498 | /* any base that was in __bases__ but now isn't, we |
Raymond Hettinger | a828586 | 2002-12-14 17:17:56 +0000 | [diff] [blame] | 499 | need to remove |type| from its tp_subclasses. |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 500 | conversely, any class now in __bases__ that wasn't |
Raymond Hettinger | a828586 | 2002-12-14 17:17:56 +0000 | [diff] [blame] | 501 | needs to have |type| added to its subclasses. */ |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 502 | |
| 503 | /* for now, sod that: just remove from all old_bases, |
| 504 | add to all new_bases */ |
| 505 | |
| 506 | for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) { |
| 507 | ob = PyTuple_GET_ITEM(old_bases, i); |
| 508 | if (PyType_Check(ob)) { |
| 509 | remove_subclass( |
| 510 | (PyTypeObject*)ob, type); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) { |
| 515 | ob = PyTuple_GET_ITEM(value, i); |
| 516 | if (PyType_Check(ob)) { |
| 517 | if (add_subclass((PyTypeObject*)ob, type) < 0) |
| 518 | r = -1; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | update_all_slots(type); |
| 523 | |
| 524 | Py_DECREF(old_bases); |
| 525 | Py_DECREF(old_base); |
| 526 | Py_DECREF(old_mro); |
| 527 | |
| 528 | return r; |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 529 | |
| 530 | bail: |
Michael W. Hudson | e723e45 | 2003-08-07 14:58:10 +0000 | [diff] [blame] | 531 | Py_DECREF(type->tp_bases); |
| 532 | Py_DECREF(type->tp_base); |
| 533 | if (type->tp_mro != old_mro) { |
| 534 | Py_DECREF(type->tp_mro); |
| 535 | } |
| 536 | |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 537 | type->tp_bases = old_bases; |
| 538 | type->tp_base = old_base; |
| 539 | type->tp_mro = old_mro; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 540 | |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 541 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 545 | type_dict(PyTypeObject *type, void *context) |
| 546 | { |
| 547 | if (type->tp_dict == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 548 | Py_INCREF(Py_None); |
| 549 | return Py_None; |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 550 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 551 | return PyDictProxy_New(type->tp_dict); |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 554 | static PyObject * |
| 555 | type_get_doc(PyTypeObject *type, void *context) |
| 556 | { |
| 557 | PyObject *result; |
Guido van Rossum | 6ca7d41 | 2002-04-18 00:22:00 +0000 | [diff] [blame] | 558 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 559 | return PyString_FromString(type->tp_doc); |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 560 | result = PyDict_GetItemString(type->tp_dict, "__doc__"); |
Guido van Rossum | 6ca7d41 | 2002-04-18 00:22:00 +0000 | [diff] [blame] | 561 | if (result == NULL) { |
| 562 | result = Py_None; |
| 563 | Py_INCREF(result); |
| 564 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 565 | else if (Py_TYPE(result)->tp_descr_get) { |
| 566 | result = Py_TYPE(result)->tp_descr_get(result, NULL, |
Tim Peters | 2b85897 | 2002-04-18 04:12:28 +0000 | [diff] [blame] | 567 | (PyObject *)type); |
Guido van Rossum | 6ca7d41 | 2002-04-18 00:22:00 +0000 | [diff] [blame] | 568 | } |
| 569 | else { |
| 570 | Py_INCREF(result); |
| 571 | } |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 572 | return result; |
| 573 | } |
| 574 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 575 | static PyGetSetDef type_getsets[] = { |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 576 | {"__name__", (getter)type_name, (setter)type_set_name, NULL}, |
| 577 | {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL}, |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 578 | {"__module__", (getter)type_module, (setter)type_set_module, NULL}, |
Jeffrey Yasskin | 960b9b7 | 2008-02-28 04:45:36 +0000 | [diff] [blame^] | 579 | {"__abstractmethods__", (getter)type_abstractmethods, |
| 580 | (setter)type_set_abstractmethods, NULL}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 581 | {"__dict__", (getter)type_dict, NULL, NULL}, |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 582 | {"__doc__", (getter)type_get_doc, NULL, NULL}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 583 | {0} |
| 584 | }; |
| 585 | |
Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 586 | static int |
| 587 | type_compare(PyObject *v, PyObject *w) |
| 588 | { |
| 589 | /* This is called with type objects only. So we |
| 590 | can just compare the addresses. */ |
| 591 | Py_uintptr_t vv = (Py_uintptr_t)v; |
| 592 | Py_uintptr_t ww = (Py_uintptr_t)w; |
| 593 | return (vv < ww) ? -1 : (vv > ww) ? 1 : 0; |
| 594 | } |
| 595 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 596 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 597 | type_repr(PyTypeObject *type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 598 | { |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 599 | PyObject *mod, *name, *rtn; |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 600 | char *kind; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 601 | |
| 602 | mod = type_module(type, NULL); |
| 603 | if (mod == NULL) |
| 604 | PyErr_Clear(); |
| 605 | else if (!PyString_Check(mod)) { |
| 606 | Py_DECREF(mod); |
| 607 | mod = NULL; |
| 608 | } |
| 609 | name = type_name(type, NULL); |
| 610 | if (name == NULL) |
| 611 | return NULL; |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 612 | |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 613 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 614 | kind = "class"; |
| 615 | else |
| 616 | kind = "type"; |
| 617 | |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 618 | if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) { |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 619 | rtn = PyString_FromFormat("<%s '%s.%s'>", |
| 620 | kind, |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 621 | PyString_AS_STRING(mod), |
| 622 | PyString_AS_STRING(name)); |
| 623 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 624 | else |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 625 | rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 626 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 627 | Py_XDECREF(mod); |
| 628 | Py_DECREF(name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 629 | return rtn; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 632 | static PyObject * |
| 633 | type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 634 | { |
| 635 | PyObject *obj; |
| 636 | |
| 637 | if (type->tp_new == NULL) { |
| 638 | PyErr_Format(PyExc_TypeError, |
| 639 | "cannot create '%.100s' instances", |
| 640 | type->tp_name); |
| 641 | return NULL; |
| 642 | } |
| 643 | |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 644 | obj = type->tp_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 645 | if (obj != NULL) { |
Guido van Rossum | f76de62 | 2001-10-18 15:49:21 +0000 | [diff] [blame] | 646 | /* Ugly exception: when the call was type(something), |
| 647 | don't call tp_init on the result. */ |
| 648 | if (type == &PyType_Type && |
| 649 | PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && |
| 650 | (kwds == NULL || |
| 651 | (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) |
| 652 | return obj; |
Guido van Rossum | 8ace1ab | 2002-04-06 01:05:01 +0000 | [diff] [blame] | 653 | /* If the returned object is not an instance of type, |
| 654 | it won't be initialized. */ |
| 655 | if (!PyType_IsSubtype(obj->ob_type, type)) |
| 656 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 657 | type = obj->ob_type; |
Jeremy Hylton | 719841e | 2002-07-16 19:39:38 +0000 | [diff] [blame] | 658 | if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) && |
| 659 | type->tp_init != NULL && |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 660 | type->tp_init(obj, args, kwds) < 0) { |
| 661 | Py_DECREF(obj); |
| 662 | obj = NULL; |
| 663 | } |
| 664 | } |
| 665 | return obj; |
| 666 | } |
| 667 | |
| 668 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 669 | PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 670 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 671 | PyObject *obj; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 672 | const size_t size = _PyObject_VAR_SIZE(type, nitems+1); |
| 673 | /* note that we need to add one, for the sentinel */ |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 674 | |
| 675 | if (PyType_IS_GC(type)) |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 676 | obj = _PyObject_GC_Malloc(size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 677 | else |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 678 | obj = (PyObject *)PyObject_MALLOC(size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 679 | |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 680 | if (obj == NULL) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 681 | return PyErr_NoMemory(); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 682 | |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 683 | memset(obj, '\0', size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 684 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 685 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 686 | Py_INCREF(type); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 687 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 688 | if (type->tp_itemsize == 0) |
| 689 | PyObject_INIT(obj, type); |
| 690 | else |
| 691 | (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 692 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 693 | if (PyType_IS_GC(type)) |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 694 | _PyObject_GC_TRACK(obj); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 695 | return obj; |
| 696 | } |
| 697 | |
| 698 | PyObject * |
| 699 | PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 700 | { |
| 701 | return type->tp_alloc(type, 0); |
| 702 | } |
| 703 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 704 | /* Helpers for subtyping */ |
| 705 | |
| 706 | static int |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 707 | traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg) |
| 708 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 709 | Py_ssize_t i, n; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 710 | PyMemberDef *mp; |
| 711 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 712 | n = Py_SIZE(type); |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 713 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 714 | for (i = 0; i < n; i++, mp++) { |
| 715 | if (mp->type == T_OBJECT_EX) { |
| 716 | char *addr = (char *)self + mp->offset; |
| 717 | PyObject *obj = *(PyObject **)addr; |
| 718 | if (obj != NULL) { |
| 719 | int err = visit(obj, arg); |
| 720 | if (err) |
| 721 | return err; |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | static int |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 729 | subtype_traverse(PyObject *self, visitproc visit, void *arg) |
| 730 | { |
| 731 | PyTypeObject *type, *base; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 732 | traverseproc basetraverse; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 733 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 734 | /* Find the nearest base with a different tp_traverse, |
| 735 | and traverse slots while we're at it */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 736 | type = Py_TYPE(self); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 737 | base = type; |
| 738 | while ((basetraverse = base->tp_traverse) == subtype_traverse) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 739 | if (Py_SIZE(base)) { |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 740 | int err = traverse_slots(base, self, visit, arg); |
| 741 | if (err) |
| 742 | return err; |
| 743 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 744 | base = base->tp_base; |
| 745 | assert(base); |
| 746 | } |
| 747 | |
| 748 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 749 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 750 | if (dictptr && *dictptr) |
| 751 | Py_VISIT(*dictptr); |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 754 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 755 | /* For a heaptype, the instances count as references |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 756 | to the type. Traverse the type so the collector |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 757 | can find cycles involving this link. */ |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 758 | Py_VISIT(type); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 759 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 760 | if (basetraverse) |
| 761 | return basetraverse(self, visit, arg); |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | static void |
| 766 | clear_slots(PyTypeObject *type, PyObject *self) |
| 767 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 768 | Py_ssize_t i, n; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 769 | PyMemberDef *mp; |
| 770 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 771 | n = Py_SIZE(type); |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 772 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 773 | for (i = 0; i < n; i++, mp++) { |
| 774 | if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) { |
| 775 | char *addr = (char *)self + mp->offset; |
| 776 | PyObject *obj = *(PyObject **)addr; |
| 777 | if (obj != NULL) { |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 778 | *(PyObject **)addr = NULL; |
Thomas Wouters | edf17d8 | 2006-04-15 17:28:34 +0000 | [diff] [blame] | 779 | Py_DECREF(obj); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | static int |
| 786 | subtype_clear(PyObject *self) |
| 787 | { |
| 788 | PyTypeObject *type, *base; |
| 789 | inquiry baseclear; |
| 790 | |
| 791 | /* Find the nearest base with a different tp_clear |
| 792 | and clear slots while we're at it */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 793 | type = Py_TYPE(self); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 794 | base = type; |
| 795 | while ((baseclear = base->tp_clear) == subtype_clear) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 796 | if (Py_SIZE(base)) |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 797 | clear_slots(base, self); |
| 798 | base = base->tp_base; |
| 799 | assert(base); |
| 800 | } |
| 801 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 802 | /* There's no need to clear the instance dict (if any); |
| 803 | the collector will call its tp_clear handler. */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 804 | |
| 805 | if (baseclear) |
| 806 | return baseclear(self); |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 807 | return 0; |
| 808 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 809 | |
| 810 | static void |
| 811 | subtype_dealloc(PyObject *self) |
| 812 | { |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 813 | PyTypeObject *type, *base; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 814 | destructor basedealloc; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 815 | |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 816 | /* Extract the type; we expect it to be a heap type */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 817 | type = Py_TYPE(self); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 818 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 819 | |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 820 | /* Test whether the type has GC exactly once */ |
| 821 | |
| 822 | if (!PyType_IS_GC(type)) { |
| 823 | /* It's really rare to find a dynamic type that doesn't have |
| 824 | GC; it can only happen when deriving from 'object' and not |
| 825 | adding any slots or instance variables. This allows |
| 826 | certain simplifications: there's no need to call |
| 827 | clear_slots(), or DECREF the dict, or clear weakrefs. */ |
| 828 | |
| 829 | /* Maybe call finalizer; exit early if resurrected */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 830 | if (type->tp_del) { |
| 831 | type->tp_del(self); |
| 832 | if (self->ob_refcnt > 0) |
| 833 | return; |
| 834 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 835 | |
| 836 | /* Find the nearest base with a different tp_dealloc */ |
| 837 | base = type; |
| 838 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 839 | assert(Py_SIZE(base) == 0); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 840 | base = base->tp_base; |
| 841 | assert(base); |
| 842 | } |
| 843 | |
| 844 | /* Call the base tp_dealloc() */ |
| 845 | assert(basedealloc); |
| 846 | basedealloc(self); |
| 847 | |
| 848 | /* Can't reference self beyond this point */ |
| 849 | Py_DECREF(type); |
| 850 | |
| 851 | /* Done */ |
| 852 | return; |
| 853 | } |
| 854 | |
| 855 | /* We get here only if the type has GC */ |
| 856 | |
| 857 | /* UnTrack and re-Track around the trashcan macro, alas */ |
Andrew M. Kuchling | c9172d3 | 2003-02-06 15:22:49 +0000 | [diff] [blame] | 858 | /* See explanation at end of function for full disclosure */ |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 859 | PyObject_GC_UnTrack(self); |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 860 | ++_PyTrash_delete_nesting; |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 861 | Py_TRASHCAN_SAFE_BEGIN(self); |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 862 | --_PyTrash_delete_nesting; |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 863 | /* DO NOT restore GC tracking at this point. weakref callbacks |
| 864 | * (if any, and whether directly here or indirectly in something we |
| 865 | * call) may trigger GC, and if self is tracked at that point, it |
| 866 | * will look like trash to GC and GC will try to delete self again. |
Tim Peters | add09b4 | 2003-11-12 20:43:28 +0000 | [diff] [blame] | 867 | */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 868 | |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 869 | /* Find the nearest base with a different tp_dealloc */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 870 | base = type; |
| 871 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 872 | base = base->tp_base; |
| 873 | assert(base); |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 876 | /* If we added a weaklist, we clear it. Do this *before* calling |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 877 | the finalizer (__del__), clearing slots, or clearing the instance |
| 878 | dict. */ |
| 879 | |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 880 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) |
| 881 | PyObject_ClearWeakRefs(self); |
| 882 | |
| 883 | /* Maybe call finalizer; exit early if resurrected */ |
| 884 | if (type->tp_del) { |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 885 | _PyObject_GC_TRACK(self); |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 886 | type->tp_del(self); |
| 887 | if (self->ob_refcnt > 0) |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 888 | goto endlabel; /* resurrected */ |
| 889 | else |
| 890 | _PyObject_GC_UNTRACK(self); |
Brett Cannon | f5bee30 | 2007-01-23 23:21:22 +0000 | [diff] [blame] | 891 | /* New weakrefs could be created during the finalizer call. |
| 892 | If this occurs, clear them out without calling their |
| 893 | finalizers since they might rely on part of the object |
| 894 | being finalized that has already been destroyed. */ |
| 895 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) { |
| 896 | /* Modeled after GET_WEAKREFS_LISTPTR() */ |
| 897 | PyWeakReference **list = (PyWeakReference **) \ |
| 898 | PyObject_GET_WEAKREFS_LISTPTR(self); |
| 899 | while (*list) |
| 900 | _PyWeakref_ClearRef(*list); |
| 901 | } |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 904 | /* Clear slots up to the nearest base with a different tp_dealloc */ |
| 905 | base = type; |
| 906 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 907 | if (Py_SIZE(base)) |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 908 | clear_slots(base, self); |
| 909 | base = base->tp_base; |
| 910 | assert(base); |
| 911 | } |
| 912 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 913 | /* If we added a dict, DECREF it */ |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 914 | if (type->tp_dictoffset && !base->tp_dictoffset) { |
| 915 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 916 | if (dictptr != NULL) { |
| 917 | PyObject *dict = *dictptr; |
| 918 | if (dict != NULL) { |
| 919 | Py_DECREF(dict); |
| 920 | *dictptr = NULL; |
| 921 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | |
Tim Peters | 0bd743c | 2003-11-13 22:50:00 +0000 | [diff] [blame] | 925 | /* Call the base tp_dealloc(); first retrack self if |
| 926 | * basedealloc knows about gc. |
| 927 | */ |
| 928 | if (PyType_IS_GC(base)) |
| 929 | _PyObject_GC_TRACK(self); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 930 | assert(basedealloc); |
| 931 | basedealloc(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 932 | |
| 933 | /* Can't reference self beyond this point */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 934 | Py_DECREF(type); |
| 935 | |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 936 | endlabel: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 937 | ++_PyTrash_delete_nesting; |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 938 | Py_TRASHCAN_SAFE_END(self); |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 939 | --_PyTrash_delete_nesting; |
| 940 | |
| 941 | /* Explanation of the weirdness around the trashcan macros: |
| 942 | |
| 943 | Q. What do the trashcan macros do? |
| 944 | |
| 945 | A. Read the comment titled "Trashcan mechanism" in object.h. |
| 946 | For one, this explains why there must be a call to GC-untrack |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 947 | before the trashcan begin macro. Without understanding the |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 948 | trashcan code, the answers to the following questions don't make |
| 949 | sense. |
| 950 | |
| 951 | Q. Why do we GC-untrack before the trashcan and then immediately |
| 952 | GC-track again afterward? |
| 953 | |
| 954 | A. In the case that the base class is GC-aware, the base class |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 955 | probably GC-untracks the object. If it does that using the |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 956 | UNTRACK macro, this will crash when the object is already |
| 957 | untracked. Because we don't know what the base class does, the |
| 958 | only safe thing is to make sure the object is tracked when we |
| 959 | call the base class dealloc. But... The trashcan begin macro |
| 960 | requires that the object is *untracked* before it is called. So |
| 961 | the dance becomes: |
| 962 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 963 | GC untrack |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 964 | trashcan begin |
| 965 | GC track |
| 966 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 967 | Q. Why did the last question say "immediately GC-track again"? |
| 968 | It's nowhere near immediately. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 969 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 970 | A. Because the code *used* to re-track immediately. Bad Idea. |
| 971 | self has a refcount of 0, and if gc ever gets its hands on it |
| 972 | (which can happen if any weakref callback gets invoked), it |
| 973 | looks like trash to gc too, and gc also tries to delete self |
| 974 | then. But we're already deleting self. Double dealloction is |
| 975 | a subtle disaster. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 976 | |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 977 | Q. Why the bizarre (net-zero) manipulation of |
| 978 | _PyTrash_delete_nesting around the trashcan macros? |
| 979 | |
| 980 | A. Some base classes (e.g. list) also use the trashcan mechanism. |
| 981 | The following scenario used to be possible: |
| 982 | |
| 983 | - suppose the trashcan level is one below the trashcan limit |
| 984 | |
| 985 | - subtype_dealloc() is called |
| 986 | |
| 987 | - the trashcan limit is not yet reached, so the trashcan level |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 988 | is incremented and the code between trashcan begin and end is |
| 989 | executed |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 990 | |
| 991 | - this destroys much of the object's contents, including its |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 992 | slots and __dict__ |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 993 | |
| 994 | - basedealloc() is called; this is really list_dealloc(), or |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 995 | some other type which also uses the trashcan macros |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 996 | |
| 997 | - the trashcan limit is now reached, so the object is put on the |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 998 | trashcan's to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 999 | |
| 1000 | - basedealloc() returns |
| 1001 | |
| 1002 | - subtype_dealloc() decrefs the object's type |
| 1003 | |
| 1004 | - subtype_dealloc() returns |
| 1005 | |
| 1006 | - later, the trashcan code starts deleting the objects from its |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1007 | to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1008 | |
| 1009 | - subtype_dealloc() is called *AGAIN* for the same object |
| 1010 | |
| 1011 | - at the very least (if the destroyed slots and __dict__ don't |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1012 | cause problems) the object's type gets decref'ed a second |
| 1013 | time, which is *BAD*!!! |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1014 | |
| 1015 | The remedy is to make sure that if the code between trashcan |
| 1016 | begin and end in subtype_dealloc() is called, the code between |
| 1017 | trashcan begin and end in basedealloc() will also be called. |
| 1018 | This is done by decrementing the level after passing into the |
| 1019 | trashcan block, and incrementing it just before leaving the |
| 1020 | block. |
| 1021 | |
| 1022 | But now it's possible that a chain of objects consisting solely |
| 1023 | of objects whose deallocator is subtype_dealloc() will defeat |
| 1024 | the trashcan mechanism completely: the decremented level means |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1025 | that the effective level never reaches the limit. Therefore, we |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1026 | *increment* the level *before* entering the trashcan block, and |
| 1027 | matchingly decrement it after leaving. This means the trashcan |
| 1028 | code will trigger a little early, but that's no big deal. |
| 1029 | |
| 1030 | Q. Are there any live examples of code in need of all this |
| 1031 | complexity? |
| 1032 | |
| 1033 | A. Yes. See SF bug 668433 for code that crashed (when Python was |
| 1034 | compiled in debug mode) before the trashcan level manipulations |
| 1035 | were added. For more discussion, see SF patches 581742, 575073 |
| 1036 | and bug 574207. |
| 1037 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1040 | static PyTypeObject *solid_base(PyTypeObject *type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1041 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1042 | /* type test with subclassing support */ |
| 1043 | |
| 1044 | int |
| 1045 | PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
| 1046 | { |
| 1047 | PyObject *mro; |
| 1048 | |
Guido van Rossum | 9478d07 | 2001-09-07 18:52:13 +0000 | [diff] [blame] | 1049 | if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS)) |
| 1050 | return b == a || b == &PyBaseObject_Type; |
| 1051 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1052 | mro = a->tp_mro; |
| 1053 | if (mro != NULL) { |
| 1054 | /* Deal with multiple inheritance without recursion |
| 1055 | by walking the MRO tuple */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1056 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1057 | assert(PyTuple_Check(mro)); |
| 1058 | n = PyTuple_GET_SIZE(mro); |
| 1059 | for (i = 0; i < n; i++) { |
| 1060 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) |
| 1061 | return 1; |
| 1062 | } |
| 1063 | return 0; |
| 1064 | } |
| 1065 | else { |
| 1066 | /* a is not completely initilized yet; follow tp_base */ |
| 1067 | do { |
| 1068 | if (a == b) |
| 1069 | return 1; |
| 1070 | a = a->tp_base; |
| 1071 | } while (a != NULL); |
| 1072 | return b == &PyBaseObject_Type; |
| 1073 | } |
| 1074 | } |
| 1075 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1076 | /* Internal routines to do a method lookup in the type |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1077 | without looking in the instance dictionary |
| 1078 | (so we can't use PyObject_GetAttr) but still binding |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1079 | it to the instance. The arguments are the object, |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1080 | 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] | 1081 | static variable used to cache the interned Python string. |
| 1082 | |
| 1083 | Two variants: |
| 1084 | |
| 1085 | - lookup_maybe() returns NULL without raising an exception |
| 1086 | when the _PyType_Lookup() call fails; |
| 1087 | |
| 1088 | - lookup_method() always raises an exception upon errors. |
| 1089 | */ |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1090 | |
| 1091 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1092 | lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1093 | { |
| 1094 | PyObject *res; |
| 1095 | |
| 1096 | if (*attrobj == NULL) { |
| 1097 | *attrobj = PyString_InternFromString(attrstr); |
| 1098 | if (*attrobj == NULL) |
| 1099 | return NULL; |
| 1100 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1101 | res = _PyType_Lookup(Py_TYPE(self), *attrobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1102 | if (res != NULL) { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1103 | descrgetfunc f; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1104 | if ((f = Py_TYPE(res)->tp_descr_get) == NULL) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1105 | Py_INCREF(res); |
| 1106 | else |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1107 | res = f(res, self, (PyObject *)(Py_TYPE(self))); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1108 | } |
| 1109 | return res; |
| 1110 | } |
| 1111 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1112 | static PyObject * |
| 1113 | lookup_method(PyObject *self, char *attrstr, PyObject **attrobj) |
| 1114 | { |
| 1115 | PyObject *res = lookup_maybe(self, attrstr, attrobj); |
| 1116 | if (res == NULL && !PyErr_Occurred()) |
| 1117 | PyErr_SetObject(PyExc_AttributeError, *attrobj); |
| 1118 | return res; |
| 1119 | } |
| 1120 | |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1121 | /* A variation of PyObject_CallMethod that uses lookup_method() |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1122 | instead of PyObject_GetAttrString(). This uses the same convention |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1123 | as lookup_method to cache the interned name string object. */ |
| 1124 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1125 | static PyObject * |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1126 | call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 1127 | { |
| 1128 | va_list va; |
| 1129 | PyObject *args, *func = 0, *retval; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1130 | va_start(va, format); |
| 1131 | |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 1132 | func = lookup_maybe(o, name, nameobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1133 | if (func == NULL) { |
| 1134 | va_end(va); |
| 1135 | if (!PyErr_Occurred()) |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 1136 | PyErr_SetObject(PyExc_AttributeError, *nameobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1137 | return NULL; |
| 1138 | } |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1139 | |
| 1140 | if (format && *format) |
| 1141 | args = Py_VaBuildValue(format, va); |
| 1142 | else |
| 1143 | args = PyTuple_New(0); |
| 1144 | |
| 1145 | va_end(va); |
| 1146 | |
| 1147 | if (args == NULL) |
| 1148 | return NULL; |
| 1149 | |
| 1150 | assert(PyTuple_Check(args)); |
| 1151 | retval = PyObject_Call(func, args, NULL); |
| 1152 | |
| 1153 | Py_DECREF(args); |
| 1154 | Py_DECREF(func); |
| 1155 | |
| 1156 | return retval; |
| 1157 | } |
| 1158 | |
| 1159 | /* Clone of call_method() that returns NotImplemented when the lookup fails. */ |
| 1160 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1161 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1162 | call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 1163 | { |
| 1164 | va_list va; |
| 1165 | PyObject *args, *func = 0, *retval; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1166 | va_start(va, format); |
| 1167 | |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 1168 | func = lookup_maybe(o, name, nameobj); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1169 | if (func == NULL) { |
| 1170 | va_end(va); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1171 | if (!PyErr_Occurred()) { |
| 1172 | Py_INCREF(Py_NotImplemented); |
| 1173 | return Py_NotImplemented; |
| 1174 | } |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 1175 | return NULL; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | if (format && *format) |
| 1179 | args = Py_VaBuildValue(format, va); |
| 1180 | else |
| 1181 | args = PyTuple_New(0); |
| 1182 | |
| 1183 | va_end(va); |
| 1184 | |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 1185 | if (args == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1186 | return NULL; |
| 1187 | |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 1188 | assert(PyTuple_Check(args)); |
| 1189 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1190 | |
| 1191 | Py_DECREF(args); |
| 1192 | Py_DECREF(func); |
| 1193 | |
| 1194 | return retval; |
| 1195 | } |
| 1196 | |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1197 | static int |
| 1198 | fill_classic_mro(PyObject *mro, PyObject *cls) |
| 1199 | { |
| 1200 | PyObject *bases, *base; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1201 | Py_ssize_t i, n; |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1202 | |
| 1203 | assert(PyList_Check(mro)); |
| 1204 | assert(PyClass_Check(cls)); |
| 1205 | i = PySequence_Contains(mro, cls); |
| 1206 | if (i < 0) |
| 1207 | return -1; |
| 1208 | if (!i) { |
| 1209 | if (PyList_Append(mro, cls) < 0) |
| 1210 | return -1; |
| 1211 | } |
| 1212 | bases = ((PyClassObject *)cls)->cl_bases; |
| 1213 | assert(bases && PyTuple_Check(bases)); |
| 1214 | n = PyTuple_GET_SIZE(bases); |
| 1215 | for (i = 0; i < n; i++) { |
| 1216 | base = PyTuple_GET_ITEM(bases, i); |
| 1217 | if (fill_classic_mro(mro, base) < 0) |
| 1218 | return -1; |
| 1219 | } |
| 1220 | return 0; |
| 1221 | } |
| 1222 | |
| 1223 | static PyObject * |
| 1224 | classic_mro(PyObject *cls) |
| 1225 | { |
| 1226 | PyObject *mro; |
| 1227 | |
| 1228 | assert(PyClass_Check(cls)); |
| 1229 | mro = PyList_New(0); |
| 1230 | if (mro != NULL) { |
| 1231 | if (fill_classic_mro(mro, cls) == 0) |
| 1232 | return mro; |
| 1233 | Py_DECREF(mro); |
| 1234 | } |
| 1235 | return NULL; |
| 1236 | } |
| 1237 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1238 | /* |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1239 | Method resolution order algorithm C3 described in |
| 1240 | "A Monotonic Superclass Linearization for Dylan", |
| 1241 | by Kim Barrett, Bob Cassel, Paul Haahr, |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1242 | David A. Moon, Keith Playford, and P. Tucker Withington. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1243 | (OOPSLA 1996) |
| 1244 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1245 | Some notes about the rules implied by C3: |
| 1246 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1247 | No duplicate bases. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1248 | It isn't legal to repeat a class in a list of base classes. |
| 1249 | |
| 1250 | The next three properties are the 3 constraints in "C3". |
| 1251 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1252 | Local precendece order. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1253 | If A precedes B in C's MRO, then A will precede B in the MRO of all |
| 1254 | subclasses of C. |
| 1255 | |
| 1256 | Monotonicity. |
| 1257 | The MRO of a class must be an extension without reordering of the |
| 1258 | MRO of each of its superclasses. |
| 1259 | |
| 1260 | Extended Precedence Graph (EPG). |
| 1261 | Linearization is consistent if there is a path in the EPG from |
| 1262 | each class to all its successors in the linearization. See |
| 1263 | the paper for definition of EPG. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1264 | */ |
| 1265 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1266 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1267 | tail_contains(PyObject *list, int whence, PyObject *o) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1268 | Py_ssize_t j, size; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1269 | size = PyList_GET_SIZE(list); |
| 1270 | |
| 1271 | for (j = whence+1; j < size; j++) { |
| 1272 | if (PyList_GET_ITEM(list, j) == o) |
| 1273 | return 1; |
| 1274 | } |
| 1275 | return 0; |
| 1276 | } |
| 1277 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1278 | static PyObject * |
| 1279 | class_name(PyObject *cls) |
| 1280 | { |
| 1281 | PyObject *name = PyObject_GetAttrString(cls, "__name__"); |
| 1282 | if (name == NULL) { |
| 1283 | PyErr_Clear(); |
| 1284 | Py_XDECREF(name); |
| 1285 | name = PyObject_Repr(cls); |
| 1286 | } |
| 1287 | if (name == NULL) |
| 1288 | return NULL; |
| 1289 | if (!PyString_Check(name)) { |
| 1290 | Py_DECREF(name); |
| 1291 | return NULL; |
| 1292 | } |
| 1293 | return name; |
| 1294 | } |
| 1295 | |
| 1296 | static int |
| 1297 | check_duplicates(PyObject *list) |
| 1298 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1299 | Py_ssize_t i, j, n; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1300 | /* Let's use a quadratic time algorithm, |
| 1301 | assuming that the bases lists is short. |
| 1302 | */ |
| 1303 | n = PyList_GET_SIZE(list); |
| 1304 | for (i = 0; i < n; i++) { |
| 1305 | PyObject *o = PyList_GET_ITEM(list, i); |
| 1306 | for (j = i + 1; j < n; j++) { |
| 1307 | if (PyList_GET_ITEM(list, j) == o) { |
| 1308 | o = class_name(o); |
| 1309 | PyErr_Format(PyExc_TypeError, |
| 1310 | "duplicate base class %s", |
| 1311 | o ? PyString_AS_STRING(o) : "?"); |
| 1312 | Py_XDECREF(o); |
| 1313 | return -1; |
| 1314 | } |
| 1315 | } |
| 1316 | } |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
| 1320 | /* Raise a TypeError for an MRO order disagreement. |
| 1321 | |
| 1322 | It's hard to produce a good error message. In the absence of better |
| 1323 | insight into error reporting, report the classes that were candidates |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1324 | to be put next into the MRO. There is some conflict between the |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1325 | order in which they should be put in the MRO, but it's hard to |
| 1326 | diagnose what constraint can't be satisfied. |
| 1327 | */ |
| 1328 | |
| 1329 | static void |
| 1330 | set_mro_error(PyObject *to_merge, int *remain) |
| 1331 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1332 | Py_ssize_t i, n, off, to_merge_size; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1333 | char buf[1000]; |
| 1334 | PyObject *k, *v; |
| 1335 | PyObject *set = PyDict_New(); |
Georg Brandl | 5c170fd | 2006-03-17 19:03:25 +0000 | [diff] [blame] | 1336 | if (!set) return; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1337 | |
| 1338 | to_merge_size = PyList_GET_SIZE(to_merge); |
| 1339 | for (i = 0; i < to_merge_size; i++) { |
| 1340 | PyObject *L = PyList_GET_ITEM(to_merge, i); |
| 1341 | if (remain[i] < PyList_GET_SIZE(L)) { |
| 1342 | PyObject *c = PyList_GET_ITEM(L, remain[i]); |
Georg Brandl | 5c170fd | 2006-03-17 19:03:25 +0000 | [diff] [blame] | 1343 | if (PyDict_SetItem(set, c, Py_None) < 0) { |
| 1344 | Py_DECREF(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1345 | return; |
Georg Brandl | 5c170fd | 2006-03-17 19:03:25 +0000 | [diff] [blame] | 1346 | } |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1347 | } |
| 1348 | } |
| 1349 | n = PyDict_Size(set); |
| 1350 | |
Raymond Hettinger | f394df4 | 2003-04-06 19:13:41 +0000 | [diff] [blame] | 1351 | off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \ |
| 1352 | consistent method resolution\norder (MRO) for bases"); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1353 | i = 0; |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 1354 | while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) { |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1355 | PyObject *name = class_name(k); |
| 1356 | off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", |
| 1357 | name ? PyString_AS_STRING(name) : "?"); |
| 1358 | Py_XDECREF(name); |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 1359 | if (--n && (size_t)(off+1) < sizeof(buf)) { |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1360 | buf[off++] = ','; |
| 1361 | buf[off] = '\0'; |
| 1362 | } |
| 1363 | } |
| 1364 | PyErr_SetString(PyExc_TypeError, buf); |
| 1365 | Py_DECREF(set); |
| 1366 | } |
| 1367 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1368 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1369 | pmerge(PyObject *acc, PyObject* to_merge) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1370 | Py_ssize_t i, j, to_merge_size, empty_cnt; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1371 | int *remain; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1372 | int ok; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1373 | |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1374 | to_merge_size = PyList_GET_SIZE(to_merge); |
| 1375 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1376 | /* remain stores an index into each sublist of to_merge. |
| 1377 | remain[i] is the index of the next base in to_merge[i] |
| 1378 | that is not included in acc. |
| 1379 | */ |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 1380 | remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1381 | if (remain == NULL) |
| 1382 | return -1; |
| 1383 | for (i = 0; i < to_merge_size; i++) |
| 1384 | remain[i] = 0; |
| 1385 | |
| 1386 | again: |
| 1387 | empty_cnt = 0; |
| 1388 | for (i = 0; i < to_merge_size; i++) { |
| 1389 | PyObject *candidate; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1390 | |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1391 | PyObject *cur_list = PyList_GET_ITEM(to_merge, i); |
| 1392 | |
| 1393 | if (remain[i] >= PyList_GET_SIZE(cur_list)) { |
| 1394 | empty_cnt++; |
| 1395 | continue; |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1396 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1397 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1398 | /* Choose next candidate for MRO. |
| 1399 | |
| 1400 | The input sequences alone can determine the choice. |
| 1401 | If not, choose the class which appears in the MRO |
| 1402 | of the earliest direct superclass of the new class. |
| 1403 | */ |
| 1404 | |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1405 | candidate = PyList_GET_ITEM(cur_list, remain[i]); |
| 1406 | for (j = 0; j < to_merge_size; j++) { |
| 1407 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1408 | if (tail_contains(j_lst, remain[j], candidate)) { |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1409 | goto skip; /* continue outer loop */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1410 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1411 | } |
| 1412 | ok = PyList_Append(acc, candidate); |
| 1413 | if (ok < 0) { |
| 1414 | PyMem_Free(remain); |
| 1415 | return -1; |
| 1416 | } |
| 1417 | for (j = 0; j < to_merge_size; j++) { |
| 1418 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
Guido van Rossum | 768158c | 2002-12-31 16:33:01 +0000 | [diff] [blame] | 1419 | if (remain[j] < PyList_GET_SIZE(j_lst) && |
| 1420 | PyList_GET_ITEM(j_lst, remain[j]) == candidate) { |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1421 | remain[j]++; |
| 1422 | } |
| 1423 | } |
| 1424 | goto again; |
Tim Peters | 9a6b8d8 | 2002-11-14 23:22:33 +0000 | [diff] [blame] | 1425 | skip: ; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1428 | if (empty_cnt == to_merge_size) { |
| 1429 | PyMem_FREE(remain); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1430 | return 0; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1431 | } |
| 1432 | set_mro_error(to_merge, remain); |
| 1433 | PyMem_FREE(remain); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1434 | return -1; |
| 1435 | } |
| 1436 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1437 | static PyObject * |
| 1438 | mro_implementation(PyTypeObject *type) |
| 1439 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1440 | Py_ssize_t i, n; |
| 1441 | int ok; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1442 | PyObject *bases, *result; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1443 | PyObject *to_merge, *bases_aslist; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1444 | |
Neal Norwitz | e7bb918 | 2008-01-27 17:10:14 +0000 | [diff] [blame] | 1445 | if (type->tp_dict == NULL) { |
| 1446 | if (PyType_Ready(type) < 0) |
Guido van Rossum | 6351757 | 2002-06-18 16:44:57 +0000 | [diff] [blame] | 1447 | return NULL; |
| 1448 | } |
| 1449 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1450 | /* Find a superclass linearization that honors the constraints |
| 1451 | of the explicit lists of bases and the constraints implied by |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1452 | each base class. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1453 | |
| 1454 | to_merge is a list of lists, where each list is a superclass |
| 1455 | linearization implied by a base class. The last element of |
| 1456 | to_merge is the declared list of bases. |
| 1457 | */ |
| 1458 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1459 | bases = type->tp_bases; |
| 1460 | n = PyTuple_GET_SIZE(bases); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1461 | |
| 1462 | to_merge = PyList_New(n+1); |
| 1463 | if (to_merge == NULL) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1464 | return NULL; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1465 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1466 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1467 | PyObject *base = PyTuple_GET_ITEM(bases, i); |
| 1468 | PyObject *parentMRO; |
| 1469 | if (PyType_Check(base)) |
| 1470 | parentMRO = PySequence_List( |
| 1471 | ((PyTypeObject*)base)->tp_mro); |
| 1472 | else |
| 1473 | parentMRO = classic_mro(base); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1474 | if (parentMRO == NULL) { |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1475 | Py_DECREF(to_merge); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1476 | return NULL; |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1477 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1478 | |
| 1479 | PyList_SET_ITEM(to_merge, i, parentMRO); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1480 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1481 | |
| 1482 | bases_aslist = PySequence_List(bases); |
| 1483 | if (bases_aslist == NULL) { |
| 1484 | Py_DECREF(to_merge); |
| 1485 | return NULL; |
| 1486 | } |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1487 | /* This is just a basic sanity check. */ |
| 1488 | if (check_duplicates(bases_aslist) < 0) { |
| 1489 | Py_DECREF(to_merge); |
| 1490 | Py_DECREF(bases_aslist); |
| 1491 | return NULL; |
| 1492 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1493 | PyList_SET_ITEM(to_merge, n, bases_aslist); |
| 1494 | |
| 1495 | result = Py_BuildValue("[O]", (PyObject *)type); |
| 1496 | if (result == NULL) { |
| 1497 | Py_DECREF(to_merge); |
| 1498 | return NULL; |
| 1499 | } |
| 1500 | |
| 1501 | ok = pmerge(result, to_merge); |
| 1502 | Py_DECREF(to_merge); |
| 1503 | if (ok < 0) { |
| 1504 | Py_DECREF(result); |
| 1505 | return NULL; |
| 1506 | } |
| 1507 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1508 | return result; |
| 1509 | } |
| 1510 | |
| 1511 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1512 | mro_external(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1513 | { |
| 1514 | PyTypeObject *type = (PyTypeObject *)self; |
| 1515 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1516 | return mro_implementation(type); |
| 1517 | } |
| 1518 | |
| 1519 | static int |
| 1520 | mro_internal(PyTypeObject *type) |
| 1521 | { |
| 1522 | PyObject *mro, *result, *tuple; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1523 | int checkit = 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1524 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1525 | if (Py_TYPE(type) == &PyType_Type) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1526 | result = mro_implementation(type); |
| 1527 | } |
| 1528 | else { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1529 | static PyObject *mro_str; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1530 | checkit = 1; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1531 | mro = lookup_method((PyObject *)type, "mro", &mro_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1532 | if (mro == NULL) |
| 1533 | return -1; |
| 1534 | result = PyObject_CallObject(mro, NULL); |
| 1535 | Py_DECREF(mro); |
| 1536 | } |
| 1537 | if (result == NULL) |
| 1538 | return -1; |
| 1539 | tuple = PySequence_Tuple(result); |
| 1540 | Py_DECREF(result); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1541 | if (tuple == NULL) |
| 1542 | return -1; |
| 1543 | if (checkit) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1544 | Py_ssize_t i, len; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1545 | PyObject *cls; |
| 1546 | PyTypeObject *solid; |
| 1547 | |
| 1548 | solid = solid_base(type); |
| 1549 | |
| 1550 | len = PyTuple_GET_SIZE(tuple); |
| 1551 | |
| 1552 | for (i = 0; i < len; i++) { |
| 1553 | PyTypeObject *t; |
| 1554 | cls = PyTuple_GET_ITEM(tuple, i); |
| 1555 | if (PyClass_Check(cls)) |
| 1556 | continue; |
| 1557 | else if (!PyType_Check(cls)) { |
| 1558 | PyErr_Format(PyExc_TypeError, |
| 1559 | "mro() returned a non-class ('%.500s')", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1560 | Py_TYPE(cls)->tp_name); |
Neal Norwitz | 50bf51a | 2006-01-02 02:46:54 +0000 | [diff] [blame] | 1561 | Py_DECREF(tuple); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1562 | return -1; |
| 1563 | } |
| 1564 | t = (PyTypeObject*)cls; |
| 1565 | if (!PyType_IsSubtype(solid, solid_base(t))) { |
| 1566 | PyErr_Format(PyExc_TypeError, |
| 1567 | "mro() returned base with unsuitable layout ('%.500s')", |
| 1568 | t->tp_name); |
Neal Norwitz | 50bf51a | 2006-01-02 02:46:54 +0000 | [diff] [blame] | 1569 | Py_DECREF(tuple); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1570 | return -1; |
| 1571 | } |
| 1572 | } |
| 1573 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1574 | type->tp_mro = tuple; |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 1575 | |
| 1576 | type_mro_modified(type, type->tp_mro); |
| 1577 | /* corner case: the old-style super class might have been hidden |
| 1578 | from the custom MRO */ |
| 1579 | type_mro_modified(type, type->tp_bases); |
| 1580 | |
| 1581 | type_modified(type); |
| 1582 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1583 | return 0; |
| 1584 | } |
| 1585 | |
| 1586 | |
| 1587 | /* Calculate the best base amongst multiple base classes. |
Armin Rigo | c0ba52d | 2007-04-19 14:44:48 +0000 | [diff] [blame] | 1588 | This is the first one that's on the path to the "solid base". */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1589 | |
| 1590 | static PyTypeObject * |
| 1591 | best_base(PyObject *bases) |
| 1592 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1593 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1594 | PyTypeObject *base, *winner, *candidate, *base_i; |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1595 | PyObject *base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1596 | |
| 1597 | assert(PyTuple_Check(bases)); |
| 1598 | n = PyTuple_GET_SIZE(bases); |
| 1599 | assert(n > 0); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1600 | base = NULL; |
| 1601 | winner = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1602 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1603 | base_proto = PyTuple_GET_ITEM(bases, i); |
| 1604 | if (PyClass_Check(base_proto)) |
| 1605 | continue; |
Armin Rigo | c0ba52d | 2007-04-19 14:44:48 +0000 | [diff] [blame] | 1606 | if (!PyType_Check(base_proto)) { |
| 1607 | PyErr_SetString( |
| 1608 | PyExc_TypeError, |
| 1609 | "bases must be types"); |
| 1610 | return NULL; |
| 1611 | } |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1612 | base_i = (PyTypeObject *)base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1613 | if (base_i->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1614 | if (PyType_Ready(base_i) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1615 | return NULL; |
| 1616 | } |
| 1617 | candidate = solid_base(base_i); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1618 | if (winner == NULL) { |
| 1619 | winner = candidate; |
| 1620 | base = base_i; |
| 1621 | } |
| 1622 | else if (PyType_IsSubtype(winner, candidate)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1623 | ; |
| 1624 | else if (PyType_IsSubtype(candidate, winner)) { |
| 1625 | winner = candidate; |
| 1626 | base = base_i; |
| 1627 | } |
| 1628 | else { |
| 1629 | PyErr_SetString( |
| 1630 | PyExc_TypeError, |
| 1631 | "multiple bases have " |
| 1632 | "instance lay-out conflict"); |
| 1633 | return NULL; |
| 1634 | } |
| 1635 | } |
Guido van Rossum | e54616c | 2001-12-14 04:19:56 +0000 | [diff] [blame] | 1636 | if (base == NULL) |
| 1637 | PyErr_SetString(PyExc_TypeError, |
| 1638 | "a new-style class can't have only classic bases"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1639 | return base; |
| 1640 | } |
| 1641 | |
| 1642 | static int |
| 1643 | extra_ivars(PyTypeObject *type, PyTypeObject *base) |
| 1644 | { |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 1645 | size_t t_size = type->tp_basicsize; |
| 1646 | size_t b_size = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1647 | |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1648 | assert(t_size >= b_size); /* Else type smaller than base! */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1649 | if (type->tp_itemsize || base->tp_itemsize) { |
| 1650 | /* If itemsize is involved, stricter rules */ |
| 1651 | return t_size != b_size || |
| 1652 | type->tp_itemsize != base->tp_itemsize; |
| 1653 | } |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1654 | if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 && |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1655 | type->tp_weaklistoffset + sizeof(PyObject *) == t_size && |
| 1656 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1657 | t_size -= sizeof(PyObject *); |
| 1658 | if (type->tp_dictoffset && base->tp_dictoffset == 0 && |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1659 | type->tp_dictoffset + sizeof(PyObject *) == t_size && |
| 1660 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1661 | t_size -= sizeof(PyObject *); |
| 1662 | |
| 1663 | return t_size != b_size; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | static PyTypeObject * |
| 1667 | solid_base(PyTypeObject *type) |
| 1668 | { |
| 1669 | PyTypeObject *base; |
| 1670 | |
| 1671 | if (type->tp_base) |
| 1672 | base = solid_base(type->tp_base); |
| 1673 | else |
| 1674 | base = &PyBaseObject_Type; |
| 1675 | if (extra_ivars(type, base)) |
| 1676 | return type; |
| 1677 | else |
| 1678 | return base; |
| 1679 | } |
| 1680 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1681 | static void object_dealloc(PyObject *); |
| 1682 | static int object_init(PyObject *, PyObject *, PyObject *); |
| 1683 | static int update_slot(PyTypeObject *, PyObject *); |
| 1684 | static void fixup_slot_dispatchers(PyTypeObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1685 | |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1686 | /* |
| 1687 | * Helpers for __dict__ descriptor. We don't want to expose the dicts |
| 1688 | * inherited from various builtin types. The builtin base usually provides |
| 1689 | * its own __dict__ descriptor, so we use that when we can. |
| 1690 | */ |
| 1691 | static PyTypeObject * |
| 1692 | get_builtin_base_with_dict(PyTypeObject *type) |
| 1693 | { |
| 1694 | while (type->tp_base != NULL) { |
| 1695 | if (type->tp_dictoffset != 0 && |
| 1696 | !(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 1697 | return type; |
| 1698 | type = type->tp_base; |
| 1699 | } |
| 1700 | return NULL; |
| 1701 | } |
| 1702 | |
| 1703 | static PyObject * |
| 1704 | get_dict_descriptor(PyTypeObject *type) |
| 1705 | { |
| 1706 | static PyObject *dict_str; |
| 1707 | PyObject *descr; |
| 1708 | |
| 1709 | if (dict_str == NULL) { |
| 1710 | dict_str = PyString_InternFromString("__dict__"); |
| 1711 | if (dict_str == NULL) |
| 1712 | return NULL; |
| 1713 | } |
| 1714 | descr = _PyType_Lookup(type, dict_str); |
| 1715 | if (descr == NULL || !PyDescr_IsData(descr)) |
| 1716 | return NULL; |
| 1717 | |
| 1718 | return descr; |
| 1719 | } |
| 1720 | |
| 1721 | static void |
| 1722 | raise_dict_descr_error(PyObject *obj) |
| 1723 | { |
| 1724 | PyErr_Format(PyExc_TypeError, |
| 1725 | "this __dict__ descriptor does not support " |
| 1726 | "'%.200s' objects", obj->ob_type->tp_name); |
| 1727 | } |
| 1728 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1729 | static PyObject * |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1730 | subtype_dict(PyObject *obj, void *context) |
| 1731 | { |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1732 | PyObject **dictptr; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1733 | PyObject *dict; |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1734 | PyTypeObject *base; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1735 | |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1736 | base = get_builtin_base_with_dict(obj->ob_type); |
| 1737 | if (base != NULL) { |
| 1738 | descrgetfunc func; |
| 1739 | PyObject *descr = get_dict_descriptor(base); |
| 1740 | if (descr == NULL) { |
| 1741 | raise_dict_descr_error(obj); |
| 1742 | return NULL; |
| 1743 | } |
| 1744 | func = descr->ob_type->tp_descr_get; |
| 1745 | if (func == NULL) { |
| 1746 | raise_dict_descr_error(obj); |
| 1747 | return NULL; |
| 1748 | } |
| 1749 | return func(descr, obj, (PyObject *)(obj->ob_type)); |
| 1750 | } |
| 1751 | |
| 1752 | dictptr = _PyObject_GetDictPtr(obj); |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1753 | if (dictptr == NULL) { |
| 1754 | PyErr_SetString(PyExc_AttributeError, |
| 1755 | "This object has no __dict__"); |
| 1756 | return NULL; |
| 1757 | } |
| 1758 | dict = *dictptr; |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 1759 | if (dict == NULL) |
| 1760 | *dictptr = dict = PyDict_New(); |
| 1761 | Py_XINCREF(dict); |
| 1762 | return dict; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1765 | static int |
| 1766 | subtype_setdict(PyObject *obj, PyObject *value, void *context) |
| 1767 | { |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1768 | PyObject **dictptr; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1769 | PyObject *dict; |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1770 | PyTypeObject *base; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1771 | |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1772 | base = get_builtin_base_with_dict(obj->ob_type); |
| 1773 | if (base != NULL) { |
| 1774 | descrsetfunc func; |
| 1775 | PyObject *descr = get_dict_descriptor(base); |
| 1776 | if (descr == NULL) { |
| 1777 | raise_dict_descr_error(obj); |
| 1778 | return -1; |
| 1779 | } |
| 1780 | func = descr->ob_type->tp_descr_set; |
| 1781 | if (func == NULL) { |
| 1782 | raise_dict_descr_error(obj); |
| 1783 | return -1; |
| 1784 | } |
| 1785 | return func(descr, obj, value); |
| 1786 | } |
| 1787 | |
| 1788 | dictptr = _PyObject_GetDictPtr(obj); |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1789 | if (dictptr == NULL) { |
| 1790 | PyErr_SetString(PyExc_AttributeError, |
| 1791 | "This object has no __dict__"); |
| 1792 | return -1; |
| 1793 | } |
Guido van Rossum | d331cb5 | 2001-12-05 19:46:42 +0000 | [diff] [blame] | 1794 | if (value != NULL && !PyDict_Check(value)) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 1795 | PyErr_Format(PyExc_TypeError, |
| 1796 | "__dict__ must be set to a dictionary, " |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1797 | "not a '%.200s'", Py_TYPE(value)->tp_name); |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1798 | return -1; |
| 1799 | } |
| 1800 | dict = *dictptr; |
Guido van Rossum | d331cb5 | 2001-12-05 19:46:42 +0000 | [diff] [blame] | 1801 | Py_XINCREF(value); |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1802 | *dictptr = value; |
| 1803 | Py_XDECREF(dict); |
| 1804 | return 0; |
| 1805 | } |
| 1806 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1807 | static PyObject * |
| 1808 | subtype_getweakref(PyObject *obj, void *context) |
| 1809 | { |
| 1810 | PyObject **weaklistptr; |
| 1811 | PyObject *result; |
| 1812 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1813 | if (Py_TYPE(obj)->tp_weaklistoffset == 0) { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1814 | PyErr_SetString(PyExc_AttributeError, |
Fred Drake | 7a36f5f | 2006-08-04 05:17:21 +0000 | [diff] [blame] | 1815 | "This object has no __weakref__"); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1816 | return NULL; |
| 1817 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1818 | assert(Py_TYPE(obj)->tp_weaklistoffset > 0); |
| 1819 | assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <= |
| 1820 | (size_t)(Py_TYPE(obj)->tp_basicsize)); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1821 | weaklistptr = (PyObject **) |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1822 | ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1823 | if (*weaklistptr == NULL) |
| 1824 | result = Py_None; |
| 1825 | else |
| 1826 | result = *weaklistptr; |
| 1827 | Py_INCREF(result); |
| 1828 | return result; |
| 1829 | } |
| 1830 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1831 | /* Three variants on the subtype_getsets list. */ |
| 1832 | |
| 1833 | static PyGetSetDef subtype_getsets_full[] = { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1834 | {"__dict__", subtype_dict, subtype_setdict, |
Neal Norwitz | 858e34f | 2002-08-13 17:18:45 +0000 | [diff] [blame] | 1835 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1836 | {"__weakref__", subtype_getweakref, NULL, |
Neal Norwitz | 858e34f | 2002-08-13 17:18:45 +0000 | [diff] [blame] | 1837 | PyDoc_STR("list of weak references to the object (if defined)")}, |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1838 | {0} |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1839 | }; |
| 1840 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1841 | static PyGetSetDef subtype_getsets_dict_only[] = { |
| 1842 | {"__dict__", subtype_dict, subtype_setdict, |
| 1843 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 1844 | {0} |
| 1845 | }; |
| 1846 | |
| 1847 | static PyGetSetDef subtype_getsets_weakref_only[] = { |
| 1848 | {"__weakref__", subtype_getweakref, NULL, |
| 1849 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 1850 | {0} |
| 1851 | }; |
| 1852 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1853 | static int |
| 1854 | valid_identifier(PyObject *s) |
| 1855 | { |
Guido van Rossum | 03013a0 | 2002-07-16 14:30:28 +0000 | [diff] [blame] | 1856 | unsigned char *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1857 | Py_ssize_t i, n; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1858 | |
| 1859 | if (!PyString_Check(s)) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 1860 | PyErr_Format(PyExc_TypeError, |
| 1861 | "__slots__ items must be strings, not '%.200s'", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1862 | Py_TYPE(s)->tp_name); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1863 | return 0; |
| 1864 | } |
Guido van Rossum | 03013a0 | 2002-07-16 14:30:28 +0000 | [diff] [blame] | 1865 | p = (unsigned char *) PyString_AS_STRING(s); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1866 | n = PyString_GET_SIZE(s); |
| 1867 | /* We must reject an empty name. As a hack, we bump the |
| 1868 | length to 1 so that the loop will balk on the trailing \0. */ |
| 1869 | if (n == 0) |
| 1870 | n = 1; |
| 1871 | for (i = 0; i < n; i++, p++) { |
| 1872 | if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') { |
| 1873 | PyErr_SetString(PyExc_TypeError, |
| 1874 | "__slots__ must be identifiers"); |
| 1875 | return 0; |
| 1876 | } |
| 1877 | } |
| 1878 | return 1; |
| 1879 | } |
| 1880 | |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1881 | #ifdef Py_USING_UNICODE |
| 1882 | /* Replace Unicode objects in slots. */ |
| 1883 | |
| 1884 | static PyObject * |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 1885 | _unicode_to_string(PyObject *slots, Py_ssize_t nslots) |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1886 | { |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1887 | PyObject *tmp = NULL; |
| 1888 | PyObject *slot_name, *new_name; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 1889 | Py_ssize_t i; |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1890 | |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1891 | for (i = 0; i < nslots; i++) { |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1892 | if (PyUnicode_Check(slot_name = PyTuple_GET_ITEM(slots, i))) { |
| 1893 | if (tmp == NULL) { |
| 1894 | tmp = PySequence_List(slots); |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1895 | if (tmp == NULL) |
| 1896 | return NULL; |
| 1897 | } |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1898 | new_name = _PyUnicode_AsDefaultEncodedString(slot_name, |
| 1899 | NULL); |
| 1900 | if (new_name == NULL) { |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1901 | Py_DECREF(tmp); |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1902 | return NULL; |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1903 | } |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1904 | Py_INCREF(new_name); |
| 1905 | PyList_SET_ITEM(tmp, i, new_name); |
| 1906 | Py_DECREF(slot_name); |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1907 | } |
| 1908 | } |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1909 | if (tmp != NULL) { |
| 1910 | slots = PyList_AsTuple(tmp); |
| 1911 | Py_DECREF(tmp); |
| 1912 | } |
| 1913 | return slots; |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1914 | } |
| 1915 | #endif |
| 1916 | |
Guido van Rossum | f102e24 | 2007-03-23 18:53:03 +0000 | [diff] [blame] | 1917 | /* Forward */ |
| 1918 | static int |
| 1919 | object_init(PyObject *self, PyObject *args, PyObject *kwds); |
| 1920 | |
| 1921 | static int |
| 1922 | type_init(PyObject *cls, PyObject *args, PyObject *kwds) |
| 1923 | { |
| 1924 | int res; |
| 1925 | |
| 1926 | assert(args != NULL && PyTuple_Check(args)); |
| 1927 | assert(kwds == NULL || PyDict_Check(kwds)); |
| 1928 | |
| 1929 | if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) { |
| 1930 | PyErr_SetString(PyExc_TypeError, |
| 1931 | "type.__init__() takes no keyword arguments"); |
| 1932 | return -1; |
| 1933 | } |
| 1934 | |
| 1935 | if (args != NULL && PyTuple_Check(args) && |
| 1936 | (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) { |
| 1937 | PyErr_SetString(PyExc_TypeError, |
| 1938 | "type.__init__() takes 1 or 3 arguments"); |
| 1939 | return -1; |
| 1940 | } |
| 1941 | |
| 1942 | /* Call object.__init__(self) now. */ |
| 1943 | /* XXX Could call super(type, cls).__init__() but what's the point? */ |
| 1944 | args = PyTuple_GetSlice(args, 0, 0); |
| 1945 | res = object_init(cls, args, NULL); |
| 1946 | Py_DECREF(args); |
| 1947 | return res; |
| 1948 | } |
| 1949 | |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1950 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1951 | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) |
| 1952 | { |
| 1953 | PyObject *name, *bases, *dict; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 1954 | static char *kwlist[] = {"name", "bases", "dict", 0}; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1955 | PyObject *slots, *tmp, *newslots; |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1956 | PyTypeObject *type, *base, *tmptype, *winner; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 1957 | PyHeapTypeObject *et; |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 1958 | PyMemberDef *mp; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1959 | Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1960 | int j, may_add_dict, may_add_weak; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1961 | |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1962 | assert(args != NULL && PyTuple_Check(args)); |
| 1963 | assert(kwds == NULL || PyDict_Check(kwds)); |
| 1964 | |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1965 | /* Special case: type(x) should return x->ob_type */ |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1966 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1967 | const Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 1968 | const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1969 | |
| 1970 | if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) { |
| 1971 | PyObject *x = PyTuple_GET_ITEM(args, 0); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1972 | Py_INCREF(Py_TYPE(x)); |
| 1973 | return (PyObject *) Py_TYPE(x); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | /* SF bug 475327 -- if that didn't trigger, we need 3 |
| 1977 | arguments. but PyArg_ParseTupleAndKeywords below may give |
| 1978 | a msg saying type() needs exactly 3. */ |
| 1979 | if (nargs + nkwds != 3) { |
| 1980 | PyErr_SetString(PyExc_TypeError, |
| 1981 | "type() takes 1 or 3 arguments"); |
| 1982 | return NULL; |
| 1983 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1986 | /* Check arguments: (name, bases, dict) */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1987 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist, |
| 1988 | &name, |
| 1989 | &PyTuple_Type, &bases, |
| 1990 | &PyDict_Type, &dict)) |
| 1991 | return NULL; |
| 1992 | |
Armin Rigo | c0ba52d | 2007-04-19 14:44:48 +0000 | [diff] [blame] | 1993 | /* Determine the proper metatype to deal with this, |
| 1994 | and check for metatype conflicts while we're at it. |
| 1995 | Note that if some other metatype wins to contract, |
| 1996 | it's possible that its instances are not types. */ |
| 1997 | nbases = PyTuple_GET_SIZE(bases); |
| 1998 | winner = metatype; |
| 1999 | for (i = 0; i < nbases; i++) { |
| 2000 | tmp = PyTuple_GET_ITEM(bases, i); |
| 2001 | tmptype = tmp->ob_type; |
| 2002 | if (tmptype == &PyClass_Type) |
| 2003 | continue; /* Special case classic classes */ |
| 2004 | if (PyType_IsSubtype(winner, tmptype)) |
| 2005 | continue; |
| 2006 | if (PyType_IsSubtype(tmptype, winner)) { |
| 2007 | winner = tmptype; |
| 2008 | continue; |
Jeremy Hylton | fa95569 | 2007-02-27 18:29:45 +0000 | [diff] [blame] | 2009 | } |
Armin Rigo | c0ba52d | 2007-04-19 14:44:48 +0000 | [diff] [blame] | 2010 | PyErr_SetString(PyExc_TypeError, |
| 2011 | "metaclass conflict: " |
| 2012 | "the metaclass of a derived class " |
| 2013 | "must be a (non-strict) subclass " |
| 2014 | "of the metaclasses of all its bases"); |
| 2015 | return NULL; |
| 2016 | } |
| 2017 | if (winner != metatype) { |
| 2018 | if (winner->tp_new != type_new) /* Pass it to the winner */ |
| 2019 | return winner->tp_new(winner, args, kwds); |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 2020 | metatype = winner; |
| 2021 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2022 | |
| 2023 | /* Adjust for empty tuple bases */ |
| 2024 | if (nbases == 0) { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2025 | bases = PyTuple_Pack(1, &PyBaseObject_Type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2026 | if (bases == NULL) |
| 2027 | return NULL; |
| 2028 | nbases = 1; |
| 2029 | } |
| 2030 | else |
| 2031 | Py_INCREF(bases); |
| 2032 | |
| 2033 | /* XXX From here until type is allocated, "return NULL" leaks bases! */ |
| 2034 | |
| 2035 | /* Calculate best base, and check that all bases are type objects */ |
| 2036 | base = best_base(bases); |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2037 | if (base == NULL) { |
| 2038 | Py_DECREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2039 | return NULL; |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2040 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2041 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 2042 | PyErr_Format(PyExc_TypeError, |
| 2043 | "type '%.100s' is not an acceptable base type", |
| 2044 | base->tp_name); |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2045 | Py_DECREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2046 | return NULL; |
| 2047 | } |
| 2048 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2049 | /* Check for a __slots__ sequence variable in dict, and count it */ |
| 2050 | slots = PyDict_GetItemString(dict, "__slots__"); |
| 2051 | nslots = 0; |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 2052 | add_dict = 0; |
| 2053 | add_weak = 0; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2054 | may_add_dict = base->tp_dictoffset == 0; |
| 2055 | may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0; |
| 2056 | if (slots == NULL) { |
| 2057 | if (may_add_dict) { |
| 2058 | add_dict++; |
| 2059 | } |
| 2060 | if (may_add_weak) { |
| 2061 | add_weak++; |
| 2062 | } |
| 2063 | } |
| 2064 | else { |
| 2065 | /* Have slots */ |
| 2066 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2067 | /* Make it into a tuple */ |
Neal Norwitz | cbd9ee6 | 2007-04-14 05:25:50 +0000 | [diff] [blame] | 2068 | if (PyString_Check(slots) || PyUnicode_Check(slots)) |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2069 | slots = PyTuple_Pack(1, slots); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2070 | else |
| 2071 | slots = PySequence_Tuple(slots); |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2072 | if (slots == NULL) { |
| 2073 | Py_DECREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2074 | return NULL; |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2075 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2076 | assert(PyTuple_Check(slots)); |
| 2077 | |
| 2078 | /* Are slots allowed? */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2079 | nslots = PyTuple_GET_SIZE(slots); |
Jeremy Hylton | 1c7a0ea | 2003-07-16 16:08:23 +0000 | [diff] [blame] | 2080 | if (nslots > 0 && base->tp_itemsize != 0) { |
Guido van Rossum | c414187 | 2001-08-30 04:43:35 +0000 | [diff] [blame] | 2081 | PyErr_Format(PyExc_TypeError, |
| 2082 | "nonempty __slots__ " |
| 2083 | "not supported for subtype of '%s'", |
| 2084 | base->tp_name); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2085 | bad_slots: |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2086 | Py_DECREF(bases); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2087 | Py_DECREF(slots); |
Guido van Rossum | c414187 | 2001-08-30 04:43:35 +0000 | [diff] [blame] | 2088 | return NULL; |
| 2089 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2090 | |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 2091 | #ifdef Py_USING_UNICODE |
| 2092 | tmp = _unicode_to_string(slots, nslots); |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 2093 | if (tmp == NULL) |
| 2094 | goto bad_slots; |
Martin v. Löwis | 13b1a5c | 2002-10-14 21:11:34 +0000 | [diff] [blame] | 2095 | if (tmp != slots) { |
| 2096 | Py_DECREF(slots); |
| 2097 | slots = tmp; |
| 2098 | } |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 2099 | #endif |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2100 | /* Check for valid slot names and two special cases */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2101 | for (i = 0; i < nslots; i++) { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2102 | PyObject *tmp = PyTuple_GET_ITEM(slots, i); |
| 2103 | char *s; |
| 2104 | if (!valid_identifier(tmp)) |
| 2105 | goto bad_slots; |
| 2106 | assert(PyString_Check(tmp)); |
| 2107 | s = PyString_AS_STRING(tmp); |
| 2108 | if (strcmp(s, "__dict__") == 0) { |
| 2109 | if (!may_add_dict || add_dict) { |
| 2110 | PyErr_SetString(PyExc_TypeError, |
| 2111 | "__dict__ slot disallowed: " |
| 2112 | "we already got one"); |
| 2113 | goto bad_slots; |
| 2114 | } |
| 2115 | add_dict++; |
| 2116 | } |
| 2117 | if (strcmp(s, "__weakref__") == 0) { |
| 2118 | if (!may_add_weak || add_weak) { |
| 2119 | PyErr_SetString(PyExc_TypeError, |
| 2120 | "__weakref__ slot disallowed: " |
| 2121 | "either we already got one, " |
| 2122 | "or __itemsize__ != 0"); |
| 2123 | goto bad_slots; |
| 2124 | } |
| 2125 | add_weak++; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2126 | } |
| 2127 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2128 | |
Žiga Seilnacht | 6f2d09c | 2007-03-16 11:59:38 +0000 | [diff] [blame] | 2129 | /* Copy slots into a list, mangle names and sort them. |
| 2130 | Sorted names are needed for __class__ assignment. |
| 2131 | Convert them back to tuple at the end. |
| 2132 | */ |
| 2133 | newslots = PyList_New(nslots - add_dict - add_weak); |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2134 | if (newslots == NULL) |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2135 | goto bad_slots; |
| 2136 | for (i = j = 0; i < nslots; i++) { |
| 2137 | char *s; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2138 | tmp = PyTuple_GET_ITEM(slots, i); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2139 | s = PyString_AS_STRING(tmp); |
| 2140 | if ((add_dict && strcmp(s, "__dict__") == 0) || |
| 2141 | (add_weak && strcmp(s, "__weakref__") == 0)) |
| 2142 | continue; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2143 | tmp =_Py_Mangle(name, tmp); |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2144 | if (!tmp) |
| 2145 | goto bad_slots; |
Žiga Seilnacht | 6f2d09c | 2007-03-16 11:59:38 +0000 | [diff] [blame] | 2146 | PyList_SET_ITEM(newslots, j, tmp); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2147 | j++; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2148 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2149 | assert(j == nslots - add_dict - add_weak); |
| 2150 | nslots = j; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2151 | Py_DECREF(slots); |
Žiga Seilnacht | 6f2d09c | 2007-03-16 11:59:38 +0000 | [diff] [blame] | 2152 | if (PyList_Sort(newslots) == -1) { |
| 2153 | Py_DECREF(bases); |
| 2154 | Py_DECREF(newslots); |
| 2155 | return NULL; |
| 2156 | } |
| 2157 | slots = PyList_AsTuple(newslots); |
| 2158 | Py_DECREF(newslots); |
| 2159 | if (slots == NULL) { |
| 2160 | Py_DECREF(bases); |
| 2161 | return NULL; |
| 2162 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2163 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2164 | /* Secondary bases may provide weakrefs or dict */ |
| 2165 | if (nbases > 1 && |
| 2166 | ((may_add_dict && !add_dict) || |
| 2167 | (may_add_weak && !add_weak))) { |
| 2168 | for (i = 0; i < nbases; i++) { |
| 2169 | tmp = PyTuple_GET_ITEM(bases, i); |
| 2170 | if (tmp == (PyObject *)base) |
| 2171 | continue; /* Skip primary base */ |
| 2172 | if (PyClass_Check(tmp)) { |
| 2173 | /* Classic base class provides both */ |
| 2174 | if (may_add_dict && !add_dict) |
| 2175 | add_dict++; |
| 2176 | if (may_add_weak && !add_weak) |
| 2177 | add_weak++; |
| 2178 | break; |
| 2179 | } |
| 2180 | assert(PyType_Check(tmp)); |
| 2181 | tmptype = (PyTypeObject *)tmp; |
| 2182 | if (may_add_dict && !add_dict && |
| 2183 | tmptype->tp_dictoffset != 0) |
| 2184 | add_dict++; |
| 2185 | if (may_add_weak && !add_weak && |
| 2186 | tmptype->tp_weaklistoffset != 0) |
| 2187 | add_weak++; |
| 2188 | if (may_add_dict && !add_dict) |
| 2189 | continue; |
| 2190 | if (may_add_weak && !add_weak) |
| 2191 | continue; |
| 2192 | /* Nothing more to check */ |
| 2193 | break; |
| 2194 | } |
| 2195 | } |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 2196 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2197 | |
| 2198 | /* XXX From here until type is safely allocated, |
| 2199 | "return NULL" may leak slots! */ |
| 2200 | |
| 2201 | /* Allocate the type object */ |
| 2202 | type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2203 | if (type == NULL) { |
| 2204 | Py_XDECREF(slots); |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2205 | Py_DECREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2206 | return NULL; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2207 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2208 | |
| 2209 | /* Keep name and slots alive in the extended type object */ |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2210 | et = (PyHeapTypeObject *)type; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2211 | Py_INCREF(name); |
Georg Brandl | c255c7b | 2006-02-20 22:27:28 +0000 | [diff] [blame] | 2212 | et->ht_name = name; |
| 2213 | et->ht_slots = slots; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2214 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2215 | /* Initialize tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2216 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | |
| 2217 | Py_TPFLAGS_BASETYPE; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2218 | if (base->tp_flags & Py_TPFLAGS_HAVE_GC) |
| 2219 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2220 | |
| 2221 | /* It's a new-style number unless it specifically inherits any |
| 2222 | old-style numeric behavior */ |
| 2223 | if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) || |
| 2224 | (base->tp_as_number == NULL)) |
| 2225 | type->tp_flags |= Py_TPFLAGS_CHECKTYPES; |
| 2226 | |
| 2227 | /* Initialize essential fields */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2228 | type->tp_as_number = &et->as_number; |
| 2229 | type->tp_as_sequence = &et->as_sequence; |
| 2230 | type->tp_as_mapping = &et->as_mapping; |
| 2231 | type->tp_as_buffer = &et->as_buffer; |
| 2232 | type->tp_name = PyString_AS_STRING(name); |
| 2233 | |
| 2234 | /* Set tp_base and tp_bases */ |
| 2235 | type->tp_bases = bases; |
| 2236 | Py_INCREF(base); |
| 2237 | type->tp_base = base; |
| 2238 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2239 | /* Initialize tp_dict from passed-in dict */ |
| 2240 | type->tp_dict = dict = PyDict_Copy(dict); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2241 | if (dict == NULL) { |
| 2242 | Py_DECREF(type); |
| 2243 | return NULL; |
| 2244 | } |
| 2245 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 2246 | /* Set __module__ in the dict */ |
| 2247 | if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 2248 | tmp = PyEval_GetGlobals(); |
| 2249 | if (tmp != NULL) { |
| 2250 | tmp = PyDict_GetItemString(tmp, "__name__"); |
| 2251 | if (tmp != NULL) { |
| 2252 | if (PyDict_SetItemString(dict, "__module__", |
| 2253 | tmp) < 0) |
| 2254 | return NULL; |
| 2255 | } |
| 2256 | } |
| 2257 | } |
| 2258 | |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2259 | /* 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] | 2260 | and is a string. The __doc__ accessor will first look for tp_doc; |
| 2261 | if that fails, it will still look into __dict__. |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2262 | */ |
| 2263 | { |
| 2264 | PyObject *doc = PyDict_GetItemString(dict, "__doc__"); |
| 2265 | if (doc != NULL && PyString_Check(doc)) { |
| 2266 | const size_t n = (size_t)PyString_GET_SIZE(doc); |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2267 | char *tp_doc = (char *)PyObject_MALLOC(n+1); |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 2268 | if (tp_doc == NULL) { |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2269 | Py_DECREF(type); |
| 2270 | return NULL; |
| 2271 | } |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 2272 | memcpy(tp_doc, PyString_AS_STRING(doc), n+1); |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2273 | type->tp_doc = tp_doc; |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2274 | } |
| 2275 | } |
| 2276 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2277 | /* Special-case __new__: if it's a plain function, |
| 2278 | make it a static function */ |
| 2279 | tmp = PyDict_GetItemString(dict, "__new__"); |
| 2280 | if (tmp != NULL && PyFunction_Check(tmp)) { |
| 2281 | tmp = PyStaticMethod_New(tmp); |
| 2282 | if (tmp == NULL) { |
| 2283 | Py_DECREF(type); |
| 2284 | return NULL; |
| 2285 | } |
| 2286 | PyDict_SetItemString(dict, "__new__", tmp); |
| 2287 | Py_DECREF(tmp); |
| 2288 | } |
| 2289 | |
| 2290 | /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2291 | mp = PyHeapType_GET_MEMBERS(et); |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 2292 | slotoffset = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2293 | if (slots != NULL) { |
| 2294 | for (i = 0; i < nslots; i++, mp++) { |
| 2295 | mp->name = PyString_AS_STRING( |
| 2296 | PyTuple_GET_ITEM(slots, i)); |
Guido van Rossum | 64b206c | 2001-12-04 17:13:22 +0000 | [diff] [blame] | 2297 | mp->type = T_OBJECT_EX; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2298 | mp->offset = slotoffset; |
Žiga Seilnacht | 8903208 | 2007-03-11 15:54:54 +0000 | [diff] [blame] | 2299 | |
| 2300 | /* __dict__ and __weakref__ are already filtered out */ |
| 2301 | assert(strcmp(mp->name, "__dict__") != 0); |
| 2302 | assert(strcmp(mp->name, "__weakref__") != 0); |
| 2303 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2304 | slotoffset += sizeof(PyObject *); |
| 2305 | } |
| 2306 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2307 | if (add_dict) { |
| 2308 | if (base->tp_itemsize) |
| 2309 | type->tp_dictoffset = -(long)sizeof(PyObject *); |
| 2310 | else |
| 2311 | type->tp_dictoffset = slotoffset; |
| 2312 | slotoffset += sizeof(PyObject *); |
| 2313 | } |
| 2314 | if (add_weak) { |
| 2315 | assert(!base->tp_itemsize); |
| 2316 | type->tp_weaklistoffset = slotoffset; |
| 2317 | slotoffset += sizeof(PyObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2318 | } |
| 2319 | type->tp_basicsize = slotoffset; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 2320 | type->tp_itemsize = base->tp_itemsize; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2321 | type->tp_members = PyHeapType_GET_MEMBERS(et); |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2322 | |
| 2323 | if (type->tp_weaklistoffset && type->tp_dictoffset) |
| 2324 | type->tp_getset = subtype_getsets_full; |
| 2325 | else if (type->tp_weaklistoffset && !type->tp_dictoffset) |
| 2326 | type->tp_getset = subtype_getsets_weakref_only; |
| 2327 | else if (!type->tp_weaklistoffset && type->tp_dictoffset) |
| 2328 | type->tp_getset = subtype_getsets_dict_only; |
| 2329 | else |
| 2330 | type->tp_getset = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2331 | |
| 2332 | /* Special case some slots */ |
| 2333 | if (type->tp_dictoffset != 0 || nslots > 0) { |
| 2334 | if (base->tp_getattr == NULL && base->tp_getattro == NULL) |
| 2335 | type->tp_getattro = PyObject_GenericGetAttr; |
| 2336 | if (base->tp_setattr == NULL && base->tp_setattro == NULL) |
| 2337 | type->tp_setattro = PyObject_GenericSetAttr; |
| 2338 | } |
| 2339 | type->tp_dealloc = subtype_dealloc; |
| 2340 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 2341 | /* Enable GC unless there are really no instance variables possible */ |
| 2342 | if (!(type->tp_basicsize == sizeof(PyObject) && |
| 2343 | type->tp_itemsize == 0)) |
| 2344 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
| 2345 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2346 | /* Always override allocation strategy to use regular heap */ |
| 2347 | type->tp_alloc = PyType_GenericAlloc; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2348 | if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 2349 | type->tp_free = PyObject_GC_Del; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 2350 | type->tp_traverse = subtype_traverse; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 2351 | type->tp_clear = subtype_clear; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2352 | } |
| 2353 | else |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 2354 | type->tp_free = PyObject_Del; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2355 | |
| 2356 | /* Initialize the rest */ |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 2357 | if (PyType_Ready(type) < 0) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2358 | Py_DECREF(type); |
| 2359 | return NULL; |
| 2360 | } |
| 2361 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 2362 | /* Put the proper slots in place */ |
| 2363 | fixup_slot_dispatchers(type); |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 2364 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2365 | return (PyObject *)type; |
| 2366 | } |
| 2367 | |
| 2368 | /* Internal API to look for a name through the MRO. |
| 2369 | This returns a borrowed reference, and doesn't set an exception! */ |
| 2370 | PyObject * |
| 2371 | _PyType_Lookup(PyTypeObject *type, PyObject *name) |
| 2372 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2373 | Py_ssize_t i, n; |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 2374 | PyObject *mro, *res, *base, *dict; |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 2375 | unsigned int h; |
| 2376 | |
| 2377 | if (MCACHE_CACHEABLE_NAME(name) && |
Neal Norwitz | e7bb918 | 2008-01-27 17:10:14 +0000 | [diff] [blame] | 2378 | PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) { |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 2379 | /* fast path */ |
| 2380 | h = MCACHE_HASH_METHOD(type, name); |
| 2381 | if (method_cache[h].version == type->tp_version_tag && |
| 2382 | method_cache[h].name == name) |
| 2383 | return method_cache[h].value; |
| 2384 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2385 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2386 | /* Look in tp_dict of types in MRO */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2387 | mro = type->tp_mro; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 2388 | |
| 2389 | /* If mro is NULL, the type is either not yet initialized |
| 2390 | by PyType_Ready(), or already cleared by type_clear(). |
| 2391 | Either way the safest thing to do is to return NULL. */ |
| 2392 | if (mro == NULL) |
| 2393 | return NULL; |
| 2394 | |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 2395 | res = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2396 | assert(PyTuple_Check(mro)); |
| 2397 | n = PyTuple_GET_SIZE(mro); |
| 2398 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 2399 | base = PyTuple_GET_ITEM(mro, i); |
| 2400 | if (PyClass_Check(base)) |
| 2401 | dict = ((PyClassObject *)base)->cl_dict; |
| 2402 | else { |
| 2403 | assert(PyType_Check(base)); |
| 2404 | dict = ((PyTypeObject *)base)->tp_dict; |
| 2405 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2406 | assert(dict && PyDict_Check(dict)); |
| 2407 | res = PyDict_GetItem(dict, name); |
| 2408 | if (res != NULL) |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 2409 | break; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2410 | } |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 2411 | |
| 2412 | if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) { |
| 2413 | h = MCACHE_HASH_METHOD(type, name); |
| 2414 | method_cache[h].version = type->tp_version_tag; |
| 2415 | method_cache[h].value = res; /* borrowed */ |
| 2416 | Py_INCREF(name); |
| 2417 | Py_DECREF(method_cache[h].name); |
| 2418 | method_cache[h].name = name; |
| 2419 | } |
| 2420 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2421 | } |
| 2422 | |
| 2423 | /* This is similar to PyObject_GenericGetAttr(), |
| 2424 | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ |
| 2425 | static PyObject * |
| 2426 | type_getattro(PyTypeObject *type, PyObject *name) |
| 2427 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2428 | PyTypeObject *metatype = Py_TYPE(type); |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2429 | PyObject *meta_attribute, *attribute; |
| 2430 | descrgetfunc meta_get; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2431 | |
| 2432 | /* Initialize this type (we'll assume the metatype is initialized) */ |
| 2433 | if (type->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 2434 | if (PyType_Ready(type) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2435 | return NULL; |
| 2436 | } |
| 2437 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2438 | /* No readable descriptor found yet */ |
| 2439 | meta_get = NULL; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2440 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2441 | /* Look for the attribute in the metatype */ |
| 2442 | meta_attribute = _PyType_Lookup(metatype, name); |
| 2443 | |
| 2444 | if (meta_attribute != NULL) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2445 | meta_get = Py_TYPE(meta_attribute)->tp_descr_get; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2446 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2447 | if (meta_get != NULL && PyDescr_IsData(meta_attribute)) { |
| 2448 | /* Data descriptors implement tp_descr_set to intercept |
| 2449 | * writes. Assume the attribute is not overridden in |
| 2450 | * type's tp_dict (and bases): call the descriptor now. |
| 2451 | */ |
| 2452 | return meta_get(meta_attribute, (PyObject *)type, |
| 2453 | (PyObject *)metatype); |
| 2454 | } |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2455 | Py_INCREF(meta_attribute); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2458 | /* No data descriptor found on metatype. Look in tp_dict of this |
| 2459 | * type and its bases */ |
| 2460 | attribute = _PyType_Lookup(type, name); |
| 2461 | if (attribute != NULL) { |
| 2462 | /* Implement descriptor functionality, if any */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2463 | descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get; |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2464 | |
| 2465 | Py_XDECREF(meta_attribute); |
| 2466 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2467 | if (local_get != NULL) { |
| 2468 | /* NULL 2nd argument indicates the descriptor was |
| 2469 | * found on the target object itself (or a base) */ |
| 2470 | return local_get(attribute, (PyObject *)NULL, |
| 2471 | (PyObject *)type); |
| 2472 | } |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2473 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2474 | Py_INCREF(attribute); |
| 2475 | return attribute; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2478 | /* No attribute found in local __dict__ (or bases): use the |
| 2479 | * descriptor from the metatype, if any */ |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2480 | if (meta_get != NULL) { |
| 2481 | PyObject *res; |
| 2482 | res = meta_get(meta_attribute, (PyObject *)type, |
| 2483 | (PyObject *)metatype); |
| 2484 | Py_DECREF(meta_attribute); |
| 2485 | return res; |
| 2486 | } |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2487 | |
| 2488 | /* If an ordinary attribute was found on the metatype, return it now */ |
| 2489 | if (meta_attribute != NULL) { |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2490 | return meta_attribute; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2491 | } |
| 2492 | |
| 2493 | /* Give up */ |
| 2494 | PyErr_Format(PyExc_AttributeError, |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2495 | "type object '%.50s' has no attribute '%.400s'", |
| 2496 | type->tp_name, PyString_AS_STRING(name)); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2497 | return NULL; |
| 2498 | } |
| 2499 | |
| 2500 | static int |
| 2501 | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) |
| 2502 | { |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 2503 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 2504 | PyErr_Format( |
| 2505 | PyExc_TypeError, |
| 2506 | "can't set attributes of built-in/extension type '%s'", |
| 2507 | type->tp_name); |
| 2508 | return -1; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 2509 | } |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 2510 | if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0) |
| 2511 | return -1; |
| 2512 | return update_slot(type, name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
| 2515 | static void |
| 2516 | type_dealloc(PyTypeObject *type) |
| 2517 | { |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2518 | PyHeapTypeObject *et; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2519 | |
| 2520 | /* Assert this is a heap-allocated type object */ |
| 2521 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2522 | _PyObject_GC_UNTRACK(type); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2523 | PyObject_ClearWeakRefs((PyObject *)type); |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2524 | et = (PyHeapTypeObject *)type; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2525 | Py_XDECREF(type->tp_base); |
| 2526 | Py_XDECREF(type->tp_dict); |
| 2527 | Py_XDECREF(type->tp_bases); |
| 2528 | Py_XDECREF(type->tp_mro); |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2529 | Py_XDECREF(type->tp_cache); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2530 | Py_XDECREF(type->tp_subclasses); |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2531 | /* A type's tp_doc is heap allocated, unlike the tp_doc slots |
| 2532 | * of most other objects. It's okay to cast it to char *. |
| 2533 | */ |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 2534 | PyObject_Free((char *)type->tp_doc); |
Georg Brandl | c255c7b | 2006-02-20 22:27:28 +0000 | [diff] [blame] | 2535 | Py_XDECREF(et->ht_name); |
| 2536 | Py_XDECREF(et->ht_slots); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2537 | Py_TYPE(type)->tp_free((PyObject *)type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2538 | } |
| 2539 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2540 | static PyObject * |
| 2541 | type_subclasses(PyTypeObject *type, PyObject *args_ignored) |
| 2542 | { |
| 2543 | PyObject *list, *raw, *ref; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2544 | Py_ssize_t i, n; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2545 | |
| 2546 | list = PyList_New(0); |
| 2547 | if (list == NULL) |
| 2548 | return NULL; |
| 2549 | raw = type->tp_subclasses; |
| 2550 | if (raw == NULL) |
| 2551 | return list; |
| 2552 | assert(PyList_Check(raw)); |
| 2553 | n = PyList_GET_SIZE(raw); |
| 2554 | for (i = 0; i < n; i++) { |
| 2555 | ref = PyList_GET_ITEM(raw, i); |
Tim Peters | 4438338 | 2001-10-08 16:49:26 +0000 | [diff] [blame] | 2556 | assert(PyWeakref_CheckRef(ref)); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2557 | ref = PyWeakref_GET_OBJECT(ref); |
| 2558 | if (ref != Py_None) { |
| 2559 | if (PyList_Append(list, ref) < 0) { |
| 2560 | Py_DECREF(list); |
| 2561 | return NULL; |
| 2562 | } |
| 2563 | } |
| 2564 | } |
| 2565 | return list; |
| 2566 | } |
| 2567 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2568 | static PyMethodDef type_methods[] = { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2569 | {"mro", (PyCFunction)mro_external, METH_NOARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 2570 | PyDoc_STR("mro() -> list\nreturn a type's method resolution order")}, |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2571 | {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 2572 | PyDoc_STR("__subclasses__() -> list of immediate subclasses")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2573 | {0} |
| 2574 | }; |
| 2575 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2576 | PyDoc_STRVAR(type_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2577 | "type(object) -> the object's type\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2578 | "type(name, bases, dict) -> a new type"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2579 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2580 | static int |
| 2581 | type_traverse(PyTypeObject *type, visitproc visit, void *arg) |
| 2582 | { |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2583 | /* Because of type_is_gc(), the collector only calls this |
| 2584 | for heaptypes. */ |
| 2585 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2586 | |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 2587 | Py_VISIT(type->tp_dict); |
| 2588 | Py_VISIT(type->tp_cache); |
| 2589 | Py_VISIT(type->tp_mro); |
| 2590 | Py_VISIT(type->tp_bases); |
| 2591 | Py_VISIT(type->tp_base); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2592 | |
| 2593 | /* There's no need to visit type->tp_subclasses or |
Georg Brandl | c255c7b | 2006-02-20 22:27:28 +0000 | [diff] [blame] | 2594 | ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2595 | in cycles; tp_subclasses is a list of weak references, |
| 2596 | and slots is a tuple of strings. */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2597 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2598 | return 0; |
| 2599 | } |
| 2600 | |
| 2601 | static int |
| 2602 | type_clear(PyTypeObject *type) |
| 2603 | { |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2604 | /* Because of type_is_gc(), the collector only calls this |
| 2605 | for heaptypes. */ |
| 2606 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2607 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2608 | /* The only field we need to clear is tp_mro, which is part of a |
| 2609 | hard cycle (its first element is the class itself) that won't |
| 2610 | be broken otherwise (it's a tuple and tuples don't have a |
| 2611 | tp_clear handler). None of the other fields need to be |
| 2612 | cleared, and here's why: |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2613 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2614 | tp_dict: |
| 2615 | It is a dict, so the collector will call its tp_clear. |
| 2616 | |
| 2617 | tp_cache: |
| 2618 | Not used; if it were, it would be a dict. |
| 2619 | |
| 2620 | tp_bases, tp_base: |
| 2621 | If these are involved in a cycle, there must be at least |
| 2622 | one other, mutable object in the cycle, e.g. a base |
| 2623 | class's dict; the cycle will be broken that way. |
| 2624 | |
| 2625 | tp_subclasses: |
| 2626 | A list of weak references can't be part of a cycle; and |
| 2627 | lists have their own tp_clear. |
| 2628 | |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2629 | slots (in PyHeapTypeObject): |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2630 | A tuple of strings can't be part of a cycle. |
| 2631 | */ |
| 2632 | |
Thomas Wouters | edf17d8 | 2006-04-15 17:28:34 +0000 | [diff] [blame] | 2633 | Py_CLEAR(type->tp_mro); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2634 | |
| 2635 | return 0; |
| 2636 | } |
| 2637 | |
| 2638 | static int |
| 2639 | type_is_gc(PyTypeObject *type) |
| 2640 | { |
| 2641 | return type->tp_flags & Py_TPFLAGS_HEAPTYPE; |
| 2642 | } |
| 2643 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2644 | PyTypeObject PyType_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2645 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2646 | "type", /* tp_name */ |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2647 | sizeof(PyHeapTypeObject), /* tp_basicsize */ |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 2648 | sizeof(PyMemberDef), /* tp_itemsize */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2649 | (destructor)type_dealloc, /* tp_dealloc */ |
| 2650 | 0, /* tp_print */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2651 | 0, /* tp_getattr */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2652 | 0, /* tp_setattr */ |
| 2653 | type_compare, /* tp_compare */ |
| 2654 | (reprfunc)type_repr, /* tp_repr */ |
| 2655 | 0, /* tp_as_number */ |
| 2656 | 0, /* tp_as_sequence */ |
| 2657 | 0, /* tp_as_mapping */ |
| 2658 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
| 2659 | (ternaryfunc)type_call, /* tp_call */ |
| 2660 | 0, /* tp_str */ |
| 2661 | (getattrofunc)type_getattro, /* tp_getattro */ |
| 2662 | (setattrofunc)type_setattro, /* tp_setattro */ |
| 2663 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2664 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 2665 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2666 | type_doc, /* tp_doc */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2667 | (traverseproc)type_traverse, /* tp_traverse */ |
| 2668 | (inquiry)type_clear, /* tp_clear */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2669 | 0, /* tp_richcompare */ |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2670 | offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2671 | 0, /* tp_iter */ |
| 2672 | 0, /* tp_iternext */ |
| 2673 | type_methods, /* tp_methods */ |
| 2674 | type_members, /* tp_members */ |
| 2675 | type_getsets, /* tp_getset */ |
| 2676 | 0, /* tp_base */ |
| 2677 | 0, /* tp_dict */ |
| 2678 | 0, /* tp_descr_get */ |
| 2679 | 0, /* tp_descr_set */ |
| 2680 | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ |
Guido van Rossum | f102e24 | 2007-03-23 18:53:03 +0000 | [diff] [blame] | 2681 | type_init, /* tp_init */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2682 | 0, /* tp_alloc */ |
| 2683 | type_new, /* tp_new */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2684 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2685 | (inquiry)type_is_gc, /* tp_is_gc */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2686 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2687 | |
| 2688 | |
| 2689 | /* The base type of all types (eventually)... except itself. */ |
| 2690 | |
Guido van Rossum | 143b564 | 2007-03-23 04:58:42 +0000 | [diff] [blame] | 2691 | /* You may wonder why object.__new__() only complains about arguments |
| 2692 | when object.__init__() is not overridden, and vice versa. |
| 2693 | |
| 2694 | Consider the use cases: |
| 2695 | |
| 2696 | 1. When neither is overridden, we want to hear complaints about |
| 2697 | excess (i.e., any) arguments, since their presence could |
| 2698 | indicate there's a bug. |
| 2699 | |
| 2700 | 2. When defining an Immutable type, we are likely to override only |
| 2701 | __new__(), since __init__() is called too late to initialize an |
| 2702 | Immutable object. Since __new__() defines the signature for the |
| 2703 | type, it would be a pain to have to override __init__() just to |
| 2704 | stop it from complaining about excess arguments. |
| 2705 | |
| 2706 | 3. When defining a Mutable type, we are likely to override only |
| 2707 | __init__(). So here the converse reasoning applies: we don't |
| 2708 | want to have to override __new__() just to stop it from |
| 2709 | complaining. |
| 2710 | |
| 2711 | 4. When __init__() is overridden, and the subclass __init__() calls |
| 2712 | object.__init__(), the latter should complain about excess |
| 2713 | arguments; ditto for __new__(). |
| 2714 | |
| 2715 | Use cases 2 and 3 make it unattractive to unconditionally check for |
| 2716 | excess arguments. The best solution that addresses all four use |
| 2717 | cases is as follows: __init__() complains about excess arguments |
| 2718 | unless __new__() is overridden and __init__() is not overridden |
| 2719 | (IOW, if __init__() is overridden or __new__() is not overridden); |
| 2720 | symmetrically, __new__() complains about excess arguments unless |
| 2721 | __init__() is overridden and __new__() is not overridden |
| 2722 | (IOW, if __new__() is overridden or __init__() is not overridden). |
| 2723 | |
| 2724 | However, for backwards compatibility, this breaks too much code. |
| 2725 | Therefore, in 2.6, we'll *warn* about excess arguments when both |
| 2726 | methods are overridden; for all other cases we'll use the above |
| 2727 | rules. |
| 2728 | |
| 2729 | */ |
| 2730 | |
| 2731 | /* Forward */ |
| 2732 | static PyObject * |
| 2733 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 2734 | |
| 2735 | static int |
| 2736 | excess_args(PyObject *args, PyObject *kwds) |
| 2737 | { |
| 2738 | return PyTuple_GET_SIZE(args) || |
| 2739 | (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)); |
| 2740 | } |
| 2741 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2742 | static int |
| 2743 | object_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2744 | { |
Guido van Rossum | 143b564 | 2007-03-23 04:58:42 +0000 | [diff] [blame] | 2745 | int err = 0; |
| 2746 | if (excess_args(args, kwds)) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2747 | PyTypeObject *type = Py_TYPE(self); |
Guido van Rossum | 143b564 | 2007-03-23 04:58:42 +0000 | [diff] [blame] | 2748 | if (type->tp_init != object_init && |
| 2749 | type->tp_new != object_new) |
| 2750 | { |
| 2751 | err = PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2752 | "object.__init__() takes no parameters", |
| 2753 | 1); |
| 2754 | } |
| 2755 | else if (type->tp_init != object_init || |
| 2756 | type->tp_new == object_new) |
| 2757 | { |
| 2758 | PyErr_SetString(PyExc_TypeError, |
| 2759 | "object.__init__() takes no parameters"); |
| 2760 | err = -1; |
| 2761 | } |
| 2762 | } |
| 2763 | return err; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2764 | } |
| 2765 | |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2766 | static PyObject * |
| 2767 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2768 | { |
Guido van Rossum | 143b564 | 2007-03-23 04:58:42 +0000 | [diff] [blame] | 2769 | int err = 0; |
| 2770 | if (excess_args(args, kwds)) { |
| 2771 | if (type->tp_new != object_new && |
| 2772 | type->tp_init != object_init) |
| 2773 | { |
| 2774 | err = PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2775 | "object.__new__() takes no parameters", |
| 2776 | 1); |
| 2777 | } |
| 2778 | else if (type->tp_new != object_new || |
| 2779 | type->tp_init == object_init) |
| 2780 | { |
| 2781 | PyErr_SetString(PyExc_TypeError, |
| 2782 | "object.__new__() takes no parameters"); |
| 2783 | err = -1; |
| 2784 | } |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2785 | } |
Guido van Rossum | 143b564 | 2007-03-23 04:58:42 +0000 | [diff] [blame] | 2786 | if (err < 0) |
| 2787 | return NULL; |
Jeffrey Yasskin | 960b9b7 | 2008-02-28 04:45:36 +0000 | [diff] [blame^] | 2788 | |
| 2789 | if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) { |
| 2790 | static PyObject *comma = NULL; |
| 2791 | PyObject *abstract_methods = NULL; |
| 2792 | PyObject *builtins; |
| 2793 | PyObject *sorted; |
| 2794 | PyObject *sorted_methods = NULL; |
| 2795 | PyObject *joined = NULL; |
| 2796 | const char *joined_str; |
| 2797 | |
| 2798 | /* Compute ", ".join(sorted(type.__abstractmethods__)) |
| 2799 | into joined. */ |
| 2800 | abstract_methods = type_abstractmethods(type, NULL); |
| 2801 | if (abstract_methods == NULL) |
| 2802 | goto error; |
| 2803 | builtins = PyEval_GetBuiltins(); |
| 2804 | if (builtins == NULL) |
| 2805 | goto error; |
| 2806 | sorted = PyDict_GetItemString(builtins, "sorted"); |
| 2807 | if (sorted == NULL) |
| 2808 | goto error; |
| 2809 | sorted_methods = PyObject_CallFunctionObjArgs(sorted, |
| 2810 | abstract_methods, |
| 2811 | NULL); |
| 2812 | if (sorted_methods == NULL) |
| 2813 | goto error; |
| 2814 | if (comma == NULL) { |
| 2815 | comma = PyString_InternFromString(", "); |
| 2816 | if (comma == NULL) |
| 2817 | goto error; |
| 2818 | } |
| 2819 | joined = PyObject_CallMethod(comma, "join", |
| 2820 | "O", sorted_methods); |
| 2821 | if (joined == NULL) |
| 2822 | goto error; |
| 2823 | joined_str = PyString_AsString(joined); |
| 2824 | if (joined_str == NULL) |
| 2825 | goto error; |
| 2826 | |
| 2827 | PyErr_Format(PyExc_TypeError, |
| 2828 | "Can't instantiate abstract class %s " |
| 2829 | "with abstract methods %s", |
| 2830 | type->tp_name, |
| 2831 | joined_str); |
| 2832 | error: |
| 2833 | Py_XDECREF(joined); |
| 2834 | Py_XDECREF(sorted_methods); |
| 2835 | Py_XDECREF(abstract_methods); |
| 2836 | return NULL; |
| 2837 | } |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2838 | return type->tp_alloc(type, 0); |
| 2839 | } |
| 2840 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2841 | static void |
| 2842 | object_dealloc(PyObject *self) |
| 2843 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2844 | Py_TYPE(self)->tp_free(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2845 | } |
| 2846 | |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2847 | static PyObject * |
| 2848 | object_repr(PyObject *self) |
| 2849 | { |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2850 | PyTypeObject *type; |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2851 | PyObject *mod, *name, *rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2852 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2853 | type = Py_TYPE(self); |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2854 | mod = type_module(type, NULL); |
| 2855 | if (mod == NULL) |
| 2856 | PyErr_Clear(); |
| 2857 | else if (!PyString_Check(mod)) { |
| 2858 | Py_DECREF(mod); |
| 2859 | mod = NULL; |
| 2860 | } |
| 2861 | name = type_name(type, NULL); |
| 2862 | if (name == NULL) |
| 2863 | return NULL; |
| 2864 | if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) |
Guido van Rossum | ff0e6d6 | 2001-09-24 16:03:59 +0000 | [diff] [blame] | 2865 | rtn = PyString_FromFormat("<%s.%s object at %p>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2866 | PyString_AS_STRING(mod), |
| 2867 | PyString_AS_STRING(name), |
| 2868 | self); |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2869 | else |
Guido van Rossum | ff0e6d6 | 2001-09-24 16:03:59 +0000 | [diff] [blame] | 2870 | rtn = PyString_FromFormat("<%s object at %p>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2871 | type->tp_name, self); |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2872 | Py_XDECREF(mod); |
| 2873 | Py_DECREF(name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2874 | return rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2877 | static PyObject * |
| 2878 | object_str(PyObject *self) |
| 2879 | { |
| 2880 | unaryfunc f; |
| 2881 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2882 | f = Py_TYPE(self)->tp_repr; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2883 | if (f == NULL) |
| 2884 | f = object_repr; |
| 2885 | return f(self); |
| 2886 | } |
| 2887 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2888 | static PyObject * |
| 2889 | object_get_class(PyObject *self, void *closure) |
| 2890 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2891 | Py_INCREF(Py_TYPE(self)); |
| 2892 | return (PyObject *)(Py_TYPE(self)); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2893 | } |
| 2894 | |
| 2895 | static int |
| 2896 | equiv_structs(PyTypeObject *a, PyTypeObject *b) |
| 2897 | { |
| 2898 | return a == b || |
| 2899 | (a != NULL && |
| 2900 | b != NULL && |
| 2901 | a->tp_basicsize == b->tp_basicsize && |
| 2902 | a->tp_itemsize == b->tp_itemsize && |
| 2903 | a->tp_dictoffset == b->tp_dictoffset && |
| 2904 | a->tp_weaklistoffset == b->tp_weaklistoffset && |
| 2905 | ((a->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 2906 | (b->tp_flags & Py_TPFLAGS_HAVE_GC))); |
| 2907 | } |
| 2908 | |
| 2909 | static int |
| 2910 | same_slots_added(PyTypeObject *a, PyTypeObject *b) |
| 2911 | { |
| 2912 | PyTypeObject *base = a->tp_base; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 2913 | Py_ssize_t size; |
Žiga Seilnacht | 6f2d09c | 2007-03-16 11:59:38 +0000 | [diff] [blame] | 2914 | PyObject *slots_a, *slots_b; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2915 | |
| 2916 | if (base != b->tp_base) |
| 2917 | return 0; |
| 2918 | if (equiv_structs(a, base) && equiv_structs(b, base)) |
| 2919 | return 1; |
| 2920 | size = base->tp_basicsize; |
| 2921 | if (a->tp_dictoffset == size && b->tp_dictoffset == size) |
| 2922 | size += sizeof(PyObject *); |
| 2923 | if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size) |
| 2924 | size += sizeof(PyObject *); |
Žiga Seilnacht | 6f2d09c | 2007-03-16 11:59:38 +0000 | [diff] [blame] | 2925 | |
| 2926 | /* Check slots compliance */ |
| 2927 | slots_a = ((PyHeapTypeObject *)a)->ht_slots; |
| 2928 | slots_b = ((PyHeapTypeObject *)b)->ht_slots; |
| 2929 | if (slots_a && slots_b) { |
| 2930 | if (PyObject_Compare(slots_a, slots_b) != 0) |
| 2931 | return 0; |
| 2932 | size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a); |
| 2933 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2934 | return size == a->tp_basicsize && size == b->tp_basicsize; |
| 2935 | } |
| 2936 | |
| 2937 | static int |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2938 | compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2939 | { |
| 2940 | PyTypeObject *newbase, *oldbase; |
| 2941 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2942 | if (newto->tp_dealloc != oldto->tp_dealloc || |
| 2943 | newto->tp_free != oldto->tp_free) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2944 | { |
| 2945 | PyErr_Format(PyExc_TypeError, |
| 2946 | "%s assignment: " |
| 2947 | "'%s' deallocator differs from '%s'", |
| 2948 | attr, |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2949 | newto->tp_name, |
| 2950 | oldto->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2951 | return 0; |
| 2952 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2953 | newbase = newto; |
| 2954 | oldbase = oldto; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2955 | while (equiv_structs(newbase, newbase->tp_base)) |
| 2956 | newbase = newbase->tp_base; |
| 2957 | while (equiv_structs(oldbase, oldbase->tp_base)) |
| 2958 | oldbase = oldbase->tp_base; |
| 2959 | if (newbase != oldbase && |
| 2960 | (newbase->tp_base != oldbase->tp_base || |
| 2961 | !same_slots_added(newbase, oldbase))) { |
| 2962 | PyErr_Format(PyExc_TypeError, |
| 2963 | "%s assignment: " |
| 2964 | "'%s' object layout differs from '%s'", |
| 2965 | attr, |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2966 | newto->tp_name, |
| 2967 | oldto->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2968 | return 0; |
| 2969 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 2970 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2971 | return 1; |
| 2972 | } |
| 2973 | |
| 2974 | static int |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2975 | object_set_class(PyObject *self, PyObject *value, void *closure) |
| 2976 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2977 | PyTypeObject *oldto = Py_TYPE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2978 | PyTypeObject *newto; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2979 | |
Guido van Rossum | b6b8942 | 2002-04-15 01:03:30 +0000 | [diff] [blame] | 2980 | if (value == NULL) { |
| 2981 | PyErr_SetString(PyExc_TypeError, |
| 2982 | "can't delete __class__ attribute"); |
| 2983 | return -1; |
| 2984 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2985 | if (!PyType_Check(value)) { |
| 2986 | PyErr_Format(PyExc_TypeError, |
| 2987 | "__class__ must be set to new-style class, not '%s' object", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2988 | Py_TYPE(value)->tp_name); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2989 | return -1; |
| 2990 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2991 | newto = (PyTypeObject *)value; |
| 2992 | if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) || |
| 2993 | !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
Guido van Rossum | 40af889 | 2002-08-10 05:42:07 +0000 | [diff] [blame] | 2994 | { |
| 2995 | PyErr_Format(PyExc_TypeError, |
| 2996 | "__class__ assignment: only for heap types"); |
| 2997 | return -1; |
| 2998 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2999 | if (compatible_for_assignment(newto, oldto, "__class__")) { |
| 3000 | Py_INCREF(newto); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 3001 | Py_TYPE(self) = newto; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3002 | Py_DECREF(oldto); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3003 | return 0; |
| 3004 | } |
| 3005 | else { |
Guido van Rossum | 9ee4b94 | 2002-05-24 18:47:47 +0000 | [diff] [blame] | 3006 | return -1; |
| 3007 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
| 3010 | static PyGetSetDef object_getsets[] = { |
| 3011 | {"__class__", object_get_class, object_set_class, |
Neal Norwitz | 858e34f | 2002-08-13 17:18:45 +0000 | [diff] [blame] | 3012 | PyDoc_STR("the object's class")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3013 | {0} |
| 3014 | }; |
| 3015 | |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 3016 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3017 | /* Stuff to implement __reduce_ex__ for pickle protocols >= 2. |
| 3018 | We fall back to helpers in copy_reg for: |
| 3019 | - pickle protocols < 2 |
| 3020 | - calculating the list of slot names (done only once per class) |
| 3021 | - the __newobj__ function (which is used as a token but never called) |
| 3022 | */ |
| 3023 | |
| 3024 | static PyObject * |
| 3025 | import_copy_reg(void) |
| 3026 | { |
| 3027 | static PyObject *copy_reg_str; |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3028 | |
| 3029 | if (!copy_reg_str) { |
| 3030 | copy_reg_str = PyString_InternFromString("copy_reg"); |
| 3031 | if (copy_reg_str == NULL) |
| 3032 | return NULL; |
| 3033 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3034 | |
| 3035 | return PyImport_Import(copy_reg_str); |
| 3036 | } |
| 3037 | |
| 3038 | static PyObject * |
| 3039 | slotnames(PyObject *cls) |
| 3040 | { |
| 3041 | PyObject *clsdict; |
| 3042 | PyObject *copy_reg; |
| 3043 | PyObject *slotnames; |
| 3044 | |
| 3045 | if (!PyType_Check(cls)) { |
| 3046 | Py_INCREF(Py_None); |
| 3047 | return Py_None; |
| 3048 | } |
| 3049 | |
| 3050 | clsdict = ((PyTypeObject *)cls)->tp_dict; |
| 3051 | slotnames = PyDict_GetItemString(clsdict, "__slotnames__"); |
Armin Rigo | ec862b9 | 2005-09-24 22:58:41 +0000 | [diff] [blame] | 3052 | if (slotnames != NULL && PyList_Check(slotnames)) { |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3053 | Py_INCREF(slotnames); |
| 3054 | return slotnames; |
| 3055 | } |
| 3056 | |
| 3057 | copy_reg = import_copy_reg(); |
| 3058 | if (copy_reg == NULL) |
| 3059 | return NULL; |
| 3060 | |
| 3061 | slotnames = PyObject_CallMethod(copy_reg, "_slotnames", "O", cls); |
| 3062 | Py_DECREF(copy_reg); |
| 3063 | if (slotnames != NULL && |
| 3064 | slotnames != Py_None && |
| 3065 | !PyList_Check(slotnames)) |
| 3066 | { |
| 3067 | PyErr_SetString(PyExc_TypeError, |
| 3068 | "copy_reg._slotnames didn't return a list or None"); |
| 3069 | Py_DECREF(slotnames); |
| 3070 | slotnames = NULL; |
| 3071 | } |
| 3072 | |
| 3073 | return slotnames; |
| 3074 | } |
| 3075 | |
| 3076 | static PyObject * |
| 3077 | reduce_2(PyObject *obj) |
| 3078 | { |
| 3079 | PyObject *cls, *getnewargs; |
| 3080 | PyObject *args = NULL, *args2 = NULL; |
| 3081 | PyObject *getstate = NULL, *state = NULL, *names = NULL; |
| 3082 | PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL; |
| 3083 | PyObject *copy_reg = NULL, *newobj = NULL, *res = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3084 | Py_ssize_t i, n; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3085 | |
| 3086 | cls = PyObject_GetAttrString(obj, "__class__"); |
| 3087 | if (cls == NULL) |
| 3088 | return NULL; |
| 3089 | |
| 3090 | getnewargs = PyObject_GetAttrString(obj, "__getnewargs__"); |
| 3091 | if (getnewargs != NULL) { |
| 3092 | args = PyObject_CallObject(getnewargs, NULL); |
| 3093 | Py_DECREF(getnewargs); |
| 3094 | if (args != NULL && !PyTuple_Check(args)) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 3095 | PyErr_Format(PyExc_TypeError, |
| 3096 | "__getnewargs__ should return a tuple, " |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 3097 | "not '%.200s'", Py_TYPE(args)->tp_name); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3098 | goto end; |
| 3099 | } |
| 3100 | } |
| 3101 | else { |
| 3102 | PyErr_Clear(); |
| 3103 | args = PyTuple_New(0); |
| 3104 | } |
| 3105 | if (args == NULL) |
| 3106 | goto end; |
| 3107 | |
| 3108 | getstate = PyObject_GetAttrString(obj, "__getstate__"); |
| 3109 | if (getstate != NULL) { |
| 3110 | state = PyObject_CallObject(getstate, NULL); |
| 3111 | Py_DECREF(getstate); |
Neal Norwitz | e2fdc61 | 2003-06-08 13:19:58 +0000 | [diff] [blame] | 3112 | if (state == NULL) |
| 3113 | goto end; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3114 | } |
| 3115 | else { |
Jim Fulton | 8a1a594 | 2004-02-08 04:21:26 +0000 | [diff] [blame] | 3116 | PyErr_Clear(); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3117 | state = PyObject_GetAttrString(obj, "__dict__"); |
| 3118 | if (state == NULL) { |
| 3119 | PyErr_Clear(); |
| 3120 | state = Py_None; |
| 3121 | Py_INCREF(state); |
| 3122 | } |
| 3123 | names = slotnames(cls); |
| 3124 | if (names == NULL) |
| 3125 | goto end; |
| 3126 | if (names != Py_None) { |
| 3127 | assert(PyList_Check(names)); |
| 3128 | slots = PyDict_New(); |
| 3129 | if (slots == NULL) |
| 3130 | goto end; |
| 3131 | n = 0; |
| 3132 | /* Can't pre-compute the list size; the list |
| 3133 | is stored on the class so accessible to other |
| 3134 | threads, which may be run by DECREF */ |
| 3135 | for (i = 0; i < PyList_GET_SIZE(names); i++) { |
| 3136 | PyObject *name, *value; |
| 3137 | name = PyList_GET_ITEM(names, i); |
| 3138 | value = PyObject_GetAttr(obj, name); |
| 3139 | if (value == NULL) |
| 3140 | PyErr_Clear(); |
| 3141 | else { |
| 3142 | int err = PyDict_SetItem(slots, name, |
| 3143 | value); |
| 3144 | Py_DECREF(value); |
| 3145 | if (err) |
| 3146 | goto end; |
| 3147 | n++; |
| 3148 | } |
| 3149 | } |
| 3150 | if (n) { |
| 3151 | state = Py_BuildValue("(NO)", state, slots); |
| 3152 | if (state == NULL) |
| 3153 | goto end; |
| 3154 | } |
| 3155 | } |
| 3156 | } |
| 3157 | |
| 3158 | if (!PyList_Check(obj)) { |
| 3159 | listitems = Py_None; |
| 3160 | Py_INCREF(listitems); |
| 3161 | } |
| 3162 | else { |
| 3163 | listitems = PyObject_GetIter(obj); |
| 3164 | if (listitems == NULL) |
| 3165 | goto end; |
| 3166 | } |
| 3167 | |
| 3168 | if (!PyDict_Check(obj)) { |
| 3169 | dictitems = Py_None; |
| 3170 | Py_INCREF(dictitems); |
| 3171 | } |
| 3172 | else { |
| 3173 | dictitems = PyObject_CallMethod(obj, "iteritems", ""); |
| 3174 | if (dictitems == NULL) |
| 3175 | goto end; |
| 3176 | } |
| 3177 | |
| 3178 | copy_reg = import_copy_reg(); |
| 3179 | if (copy_reg == NULL) |
| 3180 | goto end; |
| 3181 | newobj = PyObject_GetAttrString(copy_reg, "__newobj__"); |
| 3182 | if (newobj == NULL) |
| 3183 | goto end; |
| 3184 | |
| 3185 | n = PyTuple_GET_SIZE(args); |
| 3186 | args2 = PyTuple_New(n+1); |
| 3187 | if (args2 == NULL) |
| 3188 | goto end; |
| 3189 | PyTuple_SET_ITEM(args2, 0, cls); |
| 3190 | cls = NULL; |
| 3191 | for (i = 0; i < n; i++) { |
| 3192 | PyObject *v = PyTuple_GET_ITEM(args, i); |
| 3193 | Py_INCREF(v); |
| 3194 | PyTuple_SET_ITEM(args2, i+1, v); |
| 3195 | } |
| 3196 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3197 | res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3198 | |
| 3199 | end: |
| 3200 | Py_XDECREF(cls); |
| 3201 | Py_XDECREF(args); |
| 3202 | Py_XDECREF(args2); |
Jeremy Hylton | d06483c | 2003-04-09 21:01:42 +0000 | [diff] [blame] | 3203 | Py_XDECREF(slots); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3204 | Py_XDECREF(state); |
| 3205 | Py_XDECREF(names); |
| 3206 | Py_XDECREF(listitems); |
| 3207 | Py_XDECREF(dictitems); |
| 3208 | Py_XDECREF(copy_reg); |
| 3209 | Py_XDECREF(newobj); |
| 3210 | return res; |
| 3211 | } |
| 3212 | |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 3213 | /* |
| 3214 | * There were two problems when object.__reduce__ and object.__reduce_ex__ |
| 3215 | * were implemented in the same function: |
| 3216 | * - trying to pickle an object with a custom __reduce__ method that |
| 3217 | * fell back to object.__reduce__ in certain circumstances led to |
| 3218 | * infinite recursion at Python level and eventual RuntimeError. |
| 3219 | * - Pickling objects that lied about their type by overwriting the |
| 3220 | * __class__ descriptor could lead to infinite recursion at C level |
| 3221 | * and eventual segfault. |
| 3222 | * |
| 3223 | * Because of backwards compatibility, the two methods still have to |
| 3224 | * behave in the same way, even if this is not required by the pickle |
| 3225 | * protocol. This common functionality was moved to the _common_reduce |
| 3226 | * function. |
| 3227 | */ |
| 3228 | static PyObject * |
| 3229 | _common_reduce(PyObject *self, int proto) |
| 3230 | { |
| 3231 | PyObject *copy_reg, *res; |
| 3232 | |
| 3233 | if (proto >= 2) |
| 3234 | return reduce_2(self); |
| 3235 | |
| 3236 | copy_reg = import_copy_reg(); |
| 3237 | if (!copy_reg) |
| 3238 | return NULL; |
| 3239 | |
| 3240 | res = PyEval_CallMethod(copy_reg, "_reduce_ex", "(Oi)", self, proto); |
| 3241 | Py_DECREF(copy_reg); |
| 3242 | |
| 3243 | return res; |
| 3244 | } |
| 3245 | |
| 3246 | static PyObject * |
| 3247 | object_reduce(PyObject *self, PyObject *args) |
| 3248 | { |
| 3249 | int proto = 0; |
| 3250 | |
| 3251 | if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto)) |
| 3252 | return NULL; |
| 3253 | |
| 3254 | return _common_reduce(self, proto); |
| 3255 | } |
| 3256 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3257 | static PyObject * |
| 3258 | object_reduce_ex(PyObject *self, PyObject *args) |
| 3259 | { |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 3260 | PyObject *reduce, *res; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3261 | int proto = 0; |
| 3262 | |
| 3263 | if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto)) |
| 3264 | return NULL; |
| 3265 | |
| 3266 | reduce = PyObject_GetAttrString(self, "__reduce__"); |
| 3267 | if (reduce == NULL) |
| 3268 | PyErr_Clear(); |
| 3269 | else { |
| 3270 | PyObject *cls, *clsreduce, *objreduce; |
| 3271 | int override; |
| 3272 | cls = PyObject_GetAttrString(self, "__class__"); |
| 3273 | if (cls == NULL) { |
| 3274 | Py_DECREF(reduce); |
| 3275 | return NULL; |
| 3276 | } |
| 3277 | clsreduce = PyObject_GetAttrString(cls, "__reduce__"); |
| 3278 | Py_DECREF(cls); |
| 3279 | if (clsreduce == NULL) { |
| 3280 | Py_DECREF(reduce); |
| 3281 | return NULL; |
| 3282 | } |
| 3283 | objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict, |
| 3284 | "__reduce__"); |
| 3285 | override = (clsreduce != objreduce); |
| 3286 | Py_DECREF(clsreduce); |
| 3287 | if (override) { |
| 3288 | res = PyObject_CallObject(reduce, NULL); |
| 3289 | Py_DECREF(reduce); |
| 3290 | return res; |
| 3291 | } |
| 3292 | else |
| 3293 | Py_DECREF(reduce); |
| 3294 | } |
| 3295 | |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 3296 | return _common_reduce(self, proto); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
Jeffrey Yasskin | 960b9b7 | 2008-02-28 04:45:36 +0000 | [diff] [blame^] | 3299 | static PyObject * |
| 3300 | object_subclasshook(PyObject *cls, PyObject *args) |
| 3301 | { |
| 3302 | Py_INCREF(Py_NotImplemented); |
| 3303 | return Py_NotImplemented; |
| 3304 | } |
| 3305 | |
| 3306 | PyDoc_STRVAR(object_subclasshook_doc, |
| 3307 | "Abstract classes can override this to customize issubclass().\n" |
| 3308 | "\n" |
| 3309 | "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n" |
| 3310 | "It should return True, False or NotImplemented. If it returns\n" |
| 3311 | "NotImplemented, the normal algorithm is used. Otherwise, it\n" |
| 3312 | "overrides the normal algorithm (and the outcome is cached).\n"); |
| 3313 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 3314 | /* |
| 3315 | from PEP 3101, this code implements: |
| 3316 | |
| 3317 | class object: |
| 3318 | def __format__(self, format_spec): |
| 3319 | if isinstance(format_spec, str): |
| 3320 | return format(str(self), format_spec) |
| 3321 | elif isinstance(format_spec, unicode): |
| 3322 | return format(unicode(self), format_spec) |
| 3323 | */ |
| 3324 | static PyObject * |
| 3325 | object_format(PyObject *self, PyObject *args) |
| 3326 | { |
| 3327 | PyObject *format_spec; |
| 3328 | PyObject *self_as_str = NULL; |
| 3329 | PyObject *result = NULL; |
| 3330 | PyObject *format_meth = NULL; |
| 3331 | |
| 3332 | if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) |
| 3333 | return NULL; |
| 3334 | if (PyUnicode_Check(format_spec)) { |
| 3335 | self_as_str = PyObject_Unicode(self); |
| 3336 | } else if (PyString_Check(format_spec)) { |
| 3337 | self_as_str = PyObject_Str(self); |
| 3338 | } else { |
| 3339 | PyErr_SetString(PyExc_TypeError, "argument to __format__ must be unicode or str"); |
| 3340 | return NULL; |
| 3341 | } |
| 3342 | |
| 3343 | if (self_as_str != NULL) { |
| 3344 | /* find the format function */ |
| 3345 | format_meth = PyObject_GetAttrString(self_as_str, "__format__"); |
| 3346 | if (format_meth != NULL) { |
| 3347 | /* and call it */ |
| 3348 | result = PyObject_CallFunctionObjArgs(format_meth, format_spec, NULL); |
| 3349 | } |
| 3350 | } |
| 3351 | |
| 3352 | Py_XDECREF(self_as_str); |
| 3353 | Py_XDECREF(format_meth); |
| 3354 | |
| 3355 | return result; |
| 3356 | } |
| 3357 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3358 | static PyMethodDef object_methods[] = { |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 3359 | {"__reduce_ex__", object_reduce_ex, METH_VARARGS, |
| 3360 | PyDoc_STR("helper for pickle")}, |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 3361 | {"__reduce__", object_reduce, METH_VARARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 3362 | PyDoc_STR("helper for pickle")}, |
Jeffrey Yasskin | 960b9b7 | 2008-02-28 04:45:36 +0000 | [diff] [blame^] | 3363 | {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS, |
| 3364 | object_subclasshook_doc}, |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 3365 | {"__format__", object_format, METH_VARARGS, |
| 3366 | PyDoc_STR("default object formatter")}, |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3367 | {0} |
| 3368 | }; |
| 3369 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3370 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3371 | PyTypeObject PyBaseObject_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 3372 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3373 | "object", /* tp_name */ |
| 3374 | sizeof(PyObject), /* tp_basicsize */ |
| 3375 | 0, /* tp_itemsize */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 3376 | object_dealloc, /* tp_dealloc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3377 | 0, /* tp_print */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3378 | 0, /* tp_getattr */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3379 | 0, /* tp_setattr */ |
| 3380 | 0, /* tp_compare */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3381 | object_repr, /* tp_repr */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3382 | 0, /* tp_as_number */ |
| 3383 | 0, /* tp_as_sequence */ |
| 3384 | 0, /* tp_as_mapping */ |
Guido van Rossum | 64c06e3 | 2007-11-22 00:55:51 +0000 | [diff] [blame] | 3385 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3386 | 0, /* tp_call */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3387 | object_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3388 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3389 | PyObject_GenericSetAttr, /* tp_setattro */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3390 | 0, /* tp_as_buffer */ |
| 3391 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 3392 | PyDoc_STR("The most base type"), /* tp_doc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3393 | 0, /* tp_traverse */ |
| 3394 | 0, /* tp_clear */ |
| 3395 | 0, /* tp_richcompare */ |
| 3396 | 0, /* tp_weaklistoffset */ |
| 3397 | 0, /* tp_iter */ |
| 3398 | 0, /* tp_iternext */ |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3399 | object_methods, /* tp_methods */ |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3400 | 0, /* tp_members */ |
| 3401 | object_getsets, /* tp_getset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3402 | 0, /* tp_base */ |
| 3403 | 0, /* tp_dict */ |
| 3404 | 0, /* tp_descr_get */ |
| 3405 | 0, /* tp_descr_set */ |
| 3406 | 0, /* tp_dictoffset */ |
| 3407 | object_init, /* tp_init */ |
| 3408 | PyType_GenericAlloc, /* tp_alloc */ |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 3409 | object_new, /* tp_new */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3410 | PyObject_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3411 | }; |
| 3412 | |
| 3413 | |
| 3414 | /* Initialize the __dict__ in a type object */ |
| 3415 | |
| 3416 | static int |
| 3417 | add_methods(PyTypeObject *type, PyMethodDef *meth) |
| 3418 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3419 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3420 | |
| 3421 | for (; meth->ml_name != NULL; meth++) { |
| 3422 | PyObject *descr; |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 3423 | if (PyDict_GetItemString(dict, meth->ml_name) && |
| 3424 | !(meth->ml_flags & METH_COEXIST)) |
| 3425 | continue; |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 3426 | if (meth->ml_flags & METH_CLASS) { |
| 3427 | if (meth->ml_flags & METH_STATIC) { |
| 3428 | PyErr_SetString(PyExc_ValueError, |
| 3429 | "method cannot be both class and static"); |
| 3430 | return -1; |
| 3431 | } |
Tim Peters | bca1cbc | 2002-12-09 22:56:13 +0000 | [diff] [blame] | 3432 | descr = PyDescr_NewClassMethod(type, meth); |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 3433 | } |
| 3434 | else if (meth->ml_flags & METH_STATIC) { |
Guido van Rossum | 9af48ff | 2003-02-11 17:12:46 +0000 | [diff] [blame] | 3435 | PyObject *cfunc = PyCFunction_New(meth, NULL); |
| 3436 | if (cfunc == NULL) |
| 3437 | return -1; |
| 3438 | descr = PyStaticMethod_New(cfunc); |
| 3439 | Py_DECREF(cfunc); |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 3440 | } |
| 3441 | else { |
| 3442 | descr = PyDescr_NewMethod(type, meth); |
| 3443 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3444 | if (descr == NULL) |
| 3445 | return -1; |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 3446 | if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3447 | return -1; |
| 3448 | Py_DECREF(descr); |
| 3449 | } |
| 3450 | return 0; |
| 3451 | } |
| 3452 | |
| 3453 | static int |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 3454 | add_members(PyTypeObject *type, PyMemberDef *memb) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3455 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3456 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3457 | |
| 3458 | for (; memb->name != NULL; memb++) { |
| 3459 | PyObject *descr; |
| 3460 | if (PyDict_GetItemString(dict, memb->name)) |
| 3461 | continue; |
| 3462 | descr = PyDescr_NewMember(type, memb); |
| 3463 | if (descr == NULL) |
| 3464 | return -1; |
| 3465 | if (PyDict_SetItemString(dict, memb->name, descr) < 0) |
| 3466 | return -1; |
| 3467 | Py_DECREF(descr); |
| 3468 | } |
| 3469 | return 0; |
| 3470 | } |
| 3471 | |
| 3472 | static int |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 3473 | add_getset(PyTypeObject *type, PyGetSetDef *gsp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3474 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3475 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3476 | |
| 3477 | for (; gsp->name != NULL; gsp++) { |
| 3478 | PyObject *descr; |
| 3479 | if (PyDict_GetItemString(dict, gsp->name)) |
| 3480 | continue; |
| 3481 | descr = PyDescr_NewGetSet(type, gsp); |
| 3482 | |
| 3483 | if (descr == NULL) |
| 3484 | return -1; |
| 3485 | if (PyDict_SetItemString(dict, gsp->name, descr) < 0) |
| 3486 | return -1; |
| 3487 | Py_DECREF(descr); |
| 3488 | } |
| 3489 | return 0; |
| 3490 | } |
| 3491 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3492 | static void |
| 3493 | inherit_special(PyTypeObject *type, PyTypeObject *base) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3494 | { |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 3495 | Py_ssize_t oldsize, newsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3496 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3497 | /* Special flag magic */ |
| 3498 | if (!type->tp_as_buffer && base->tp_as_buffer) { |
| 3499 | type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER; |
| 3500 | type->tp_flags |= |
| 3501 | base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER; |
| 3502 | } |
| 3503 | if (!type->tp_as_sequence && base->tp_as_sequence) { |
| 3504 | type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN; |
| 3505 | type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN; |
| 3506 | } |
| 3507 | if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) != |
| 3508 | (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) { |
| 3509 | if ((!type->tp_as_number && base->tp_as_number) || |
| 3510 | (!type->tp_as_sequence && base->tp_as_sequence)) { |
| 3511 | type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS; |
| 3512 | if (!type->tp_as_number && !type->tp_as_sequence) { |
| 3513 | type->tp_flags |= base->tp_flags & |
| 3514 | Py_TPFLAGS_HAVE_INPLACEOPS; |
| 3515 | } |
| 3516 | } |
| 3517 | /* Wow */ |
| 3518 | } |
| 3519 | if (!type->tp_as_number && base->tp_as_number) { |
| 3520 | type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES; |
| 3521 | type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES; |
| 3522 | } |
| 3523 | |
| 3524 | /* Copying basicsize is connected to the GC flags */ |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 3525 | oldsize = base->tp_basicsize; |
| 3526 | newsize = type->tp_basicsize ? type->tp_basicsize : oldsize; |
| 3527 | if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3528 | (base->tp_flags & Py_TPFLAGS_HAVE_GC) && |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3529 | (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) && |
| 3530 | (!type->tp_traverse && !type->tp_clear)) { |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 3531 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3532 | if (type->tp_traverse == NULL) |
| 3533 | type->tp_traverse = base->tp_traverse; |
| 3534 | if (type->tp_clear == NULL) |
| 3535 | type->tp_clear = base->tp_clear; |
| 3536 | } |
| 3537 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
Guido van Rossum | f884b74 | 2001-12-17 17:14:22 +0000 | [diff] [blame] | 3538 | /* The condition below could use some explanation. |
| 3539 | It appears that tp_new is not inherited for static types |
| 3540 | whose base class is 'object'; this seems to be a precaution |
| 3541 | so that old extension types don't suddenly become |
| 3542 | callable (object.__new__ wouldn't insure the invariants |
| 3543 | that the extension type's own factory function ensures). |
| 3544 | Heap types, of course, are under our control, so they do |
| 3545 | inherit tp_new; static extension types that specify some |
| 3546 | other built-in type as the default are considered |
| 3547 | new-style-aware so they also inherit object.__new__. */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3548 | if (base != &PyBaseObject_Type || |
| 3549 | (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 3550 | if (type->tp_new == NULL) |
| 3551 | type->tp_new = base->tp_new; |
| 3552 | } |
| 3553 | } |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 3554 | type->tp_basicsize = newsize; |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3555 | |
| 3556 | /* Copy other non-function slots */ |
| 3557 | |
| 3558 | #undef COPYVAL |
| 3559 | #define COPYVAL(SLOT) \ |
| 3560 | if (type->SLOT == 0) type->SLOT = base->SLOT |
| 3561 | |
| 3562 | COPYVAL(tp_itemsize); |
| 3563 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) { |
| 3564 | COPYVAL(tp_weaklistoffset); |
| 3565 | } |
| 3566 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
| 3567 | COPYVAL(tp_dictoffset); |
| 3568 | } |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 3569 | |
| 3570 | /* Setup fast subclass flags */ |
| 3571 | if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException)) |
| 3572 | type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS; |
| 3573 | else if (PyType_IsSubtype(base, &PyType_Type)) |
| 3574 | type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS; |
| 3575 | else if (PyType_IsSubtype(base, &PyInt_Type)) |
| 3576 | type->tp_flags |= Py_TPFLAGS_INT_SUBCLASS; |
| 3577 | else if (PyType_IsSubtype(base, &PyLong_Type)) |
| 3578 | type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS; |
| 3579 | else if (PyType_IsSubtype(base, &PyString_Type)) |
| 3580 | type->tp_flags |= Py_TPFLAGS_STRING_SUBCLASS; |
Georg Brandl | dfe5dc8 | 2008-01-07 18:16:36 +0000 | [diff] [blame] | 3581 | #ifdef Py_USING_UNICODE |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 3582 | else if (PyType_IsSubtype(base, &PyUnicode_Type)) |
| 3583 | type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS; |
Georg Brandl | dfe5dc8 | 2008-01-07 18:16:36 +0000 | [diff] [blame] | 3584 | #endif |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 3585 | else if (PyType_IsSubtype(base, &PyTuple_Type)) |
| 3586 | type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS; |
| 3587 | else if (PyType_IsSubtype(base, &PyList_Type)) |
| 3588 | type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS; |
| 3589 | else if (PyType_IsSubtype(base, &PyDict_Type)) |
| 3590 | type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
Guido van Rossum | 0b7b6fd | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 3593 | static char *hash_name_op[] = { |
| 3594 | "__eq__", |
| 3595 | "__cmp__", |
| 3596 | "__hash__", |
| 3597 | NULL |
Guido van Rossum | 64c06e3 | 2007-11-22 00:55:51 +0000 | [diff] [blame] | 3598 | }; |
| 3599 | |
| 3600 | static int |
| 3601 | overrides_hash(PyTypeObject *type) |
| 3602 | { |
Guido van Rossum | 0b7b6fd | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 3603 | char **p; |
Guido van Rossum | 64c06e3 | 2007-11-22 00:55:51 +0000 | [diff] [blame] | 3604 | PyObject *dict = type->tp_dict; |
| 3605 | |
| 3606 | assert(dict != NULL); |
Guido van Rossum | 0b7b6fd | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 3607 | for (p = hash_name_op; *p; p++) { |
| 3608 | if (PyDict_GetItemString(dict, *p) != NULL) |
Guido van Rossum | 64c06e3 | 2007-11-22 00:55:51 +0000 | [diff] [blame] | 3609 | return 1; |
| 3610 | } |
| 3611 | return 0; |
| 3612 | } |
| 3613 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3614 | static void |
| 3615 | inherit_slots(PyTypeObject *type, PyTypeObject *base) |
| 3616 | { |
| 3617 | PyTypeObject *basebase; |
| 3618 | |
| 3619 | #undef SLOTDEFINED |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3620 | #undef COPYSLOT |
| 3621 | #undef COPYNUM |
| 3622 | #undef COPYSEQ |
| 3623 | #undef COPYMAP |
Guido van Rossum | 5af588b | 2001-10-12 14:13:21 +0000 | [diff] [blame] | 3624 | #undef COPYBUF |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3625 | |
| 3626 | #define SLOTDEFINED(SLOT) \ |
| 3627 | (base->SLOT != 0 && \ |
| 3628 | (basebase == NULL || base->SLOT != basebase->SLOT)) |
| 3629 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3630 | #define COPYSLOT(SLOT) \ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3631 | if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3632 | |
| 3633 | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT) |
| 3634 | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT) |
| 3635 | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT) |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3636 | #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3637 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3638 | /* This won't inherit indirect slots (from tp_as_number etc.) |
| 3639 | if type doesn't provide the space. */ |
| 3640 | |
| 3641 | if (type->tp_as_number != NULL && base->tp_as_number != NULL) { |
| 3642 | basebase = base->tp_base; |
| 3643 | if (basebase->tp_as_number == NULL) |
| 3644 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3645 | COPYNUM(nb_add); |
| 3646 | COPYNUM(nb_subtract); |
| 3647 | COPYNUM(nb_multiply); |
| 3648 | COPYNUM(nb_divide); |
| 3649 | COPYNUM(nb_remainder); |
| 3650 | COPYNUM(nb_divmod); |
| 3651 | COPYNUM(nb_power); |
| 3652 | COPYNUM(nb_negative); |
| 3653 | COPYNUM(nb_positive); |
| 3654 | COPYNUM(nb_absolute); |
| 3655 | COPYNUM(nb_nonzero); |
| 3656 | COPYNUM(nb_invert); |
| 3657 | COPYNUM(nb_lshift); |
| 3658 | COPYNUM(nb_rshift); |
| 3659 | COPYNUM(nb_and); |
| 3660 | COPYNUM(nb_xor); |
| 3661 | COPYNUM(nb_or); |
| 3662 | COPYNUM(nb_coerce); |
| 3663 | COPYNUM(nb_int); |
| 3664 | COPYNUM(nb_long); |
| 3665 | COPYNUM(nb_float); |
| 3666 | COPYNUM(nb_oct); |
| 3667 | COPYNUM(nb_hex); |
| 3668 | COPYNUM(nb_inplace_add); |
| 3669 | COPYNUM(nb_inplace_subtract); |
| 3670 | COPYNUM(nb_inplace_multiply); |
| 3671 | COPYNUM(nb_inplace_divide); |
| 3672 | COPYNUM(nb_inplace_remainder); |
| 3673 | COPYNUM(nb_inplace_power); |
| 3674 | COPYNUM(nb_inplace_lshift); |
| 3675 | COPYNUM(nb_inplace_rshift); |
| 3676 | COPYNUM(nb_inplace_and); |
| 3677 | COPYNUM(nb_inplace_xor); |
| 3678 | COPYNUM(nb_inplace_or); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3679 | if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) { |
| 3680 | COPYNUM(nb_true_divide); |
| 3681 | COPYNUM(nb_floor_divide); |
| 3682 | COPYNUM(nb_inplace_true_divide); |
| 3683 | COPYNUM(nb_inplace_floor_divide); |
| 3684 | } |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 3685 | if (base->tp_flags & Py_TPFLAGS_HAVE_INDEX) { |
| 3686 | COPYNUM(nb_index); |
| 3687 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3688 | } |
| 3689 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3690 | if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) { |
| 3691 | basebase = base->tp_base; |
| 3692 | if (basebase->tp_as_sequence == NULL) |
| 3693 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3694 | COPYSEQ(sq_length); |
| 3695 | COPYSEQ(sq_concat); |
| 3696 | COPYSEQ(sq_repeat); |
| 3697 | COPYSEQ(sq_item); |
| 3698 | COPYSEQ(sq_slice); |
| 3699 | COPYSEQ(sq_ass_item); |
| 3700 | COPYSEQ(sq_ass_slice); |
| 3701 | COPYSEQ(sq_contains); |
| 3702 | COPYSEQ(sq_inplace_concat); |
| 3703 | COPYSEQ(sq_inplace_repeat); |
| 3704 | } |
| 3705 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3706 | if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) { |
| 3707 | basebase = base->tp_base; |
| 3708 | if (basebase->tp_as_mapping == NULL) |
| 3709 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3710 | COPYMAP(mp_length); |
| 3711 | COPYMAP(mp_subscript); |
| 3712 | COPYMAP(mp_ass_subscript); |
| 3713 | } |
| 3714 | |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3715 | if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) { |
| 3716 | basebase = base->tp_base; |
| 3717 | if (basebase->tp_as_buffer == NULL) |
| 3718 | basebase = NULL; |
| 3719 | COPYBUF(bf_getreadbuffer); |
| 3720 | COPYBUF(bf_getwritebuffer); |
| 3721 | COPYBUF(bf_getsegcount); |
| 3722 | COPYBUF(bf_getcharbuffer); |
| 3723 | } |
| 3724 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3725 | basebase = base->tp_base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3726 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3727 | COPYSLOT(tp_dealloc); |
| 3728 | COPYSLOT(tp_print); |
| 3729 | if (type->tp_getattr == NULL && type->tp_getattro == NULL) { |
| 3730 | type->tp_getattr = base->tp_getattr; |
| 3731 | type->tp_getattro = base->tp_getattro; |
| 3732 | } |
| 3733 | if (type->tp_setattr == NULL && type->tp_setattro == NULL) { |
| 3734 | type->tp_setattr = base->tp_setattr; |
| 3735 | type->tp_setattro = base->tp_setattro; |
| 3736 | } |
| 3737 | /* tp_compare see tp_richcompare */ |
| 3738 | COPYSLOT(tp_repr); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3739 | /* tp_hash see tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3740 | COPYSLOT(tp_call); |
| 3741 | COPYSLOT(tp_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3742 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3743 | if (type->tp_compare == NULL && |
| 3744 | type->tp_richcompare == NULL && |
Guido van Rossum | 64c06e3 | 2007-11-22 00:55:51 +0000 | [diff] [blame] | 3745 | type->tp_hash == NULL && |
| 3746 | !overrides_hash(type)) |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3747 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3748 | type->tp_compare = base->tp_compare; |
| 3749 | type->tp_richcompare = base->tp_richcompare; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3750 | type->tp_hash = base->tp_hash; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3751 | } |
| 3752 | } |
| 3753 | else { |
| 3754 | COPYSLOT(tp_compare); |
| 3755 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3756 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) { |
| 3757 | COPYSLOT(tp_iter); |
| 3758 | COPYSLOT(tp_iternext); |
| 3759 | } |
| 3760 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
| 3761 | COPYSLOT(tp_descr_get); |
| 3762 | COPYSLOT(tp_descr_set); |
| 3763 | COPYSLOT(tp_dictoffset); |
| 3764 | COPYSLOT(tp_init); |
| 3765 | COPYSLOT(tp_alloc); |
Guido van Rossum | cc8fe04 | 2002-04-05 17:10:16 +0000 | [diff] [blame] | 3766 | COPYSLOT(tp_is_gc); |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 3767 | if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 3768 | (base->tp_flags & Py_TPFLAGS_HAVE_GC)) { |
| 3769 | /* They agree about gc. */ |
| 3770 | COPYSLOT(tp_free); |
| 3771 | } |
| 3772 | else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3773 | type->tp_free == NULL && |
| 3774 | base->tp_free == _PyObject_Del) { |
| 3775 | /* A bit of magic to plug in the correct default |
| 3776 | * tp_free function when a derived class adds gc, |
| 3777 | * didn't define tp_free, and the base uses the |
| 3778 | * default non-gc tp_free. |
| 3779 | */ |
| 3780 | type->tp_free = PyObject_GC_Del; |
| 3781 | } |
| 3782 | /* else they didn't agree about gc, and there isn't something |
| 3783 | * obvious to be done -- the type is on its own. |
| 3784 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3785 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3786 | } |
| 3787 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 3788 | static int add_operators(PyTypeObject *); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3789 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3790 | int |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 3791 | PyType_Ready(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3792 | { |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 3793 | PyObject *dict, *bases; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3794 | PyTypeObject *base; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3795 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3796 | |
Guido van Rossum | cab0580 | 2002-06-10 15:29:03 +0000 | [diff] [blame] | 3797 | if (type->tp_flags & Py_TPFLAGS_READY) { |
| 3798 | assert(type->tp_dict != NULL); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3799 | return 0; |
Guido van Rossum | cab0580 | 2002-06-10 15:29:03 +0000 | [diff] [blame] | 3800 | } |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3801 | assert((type->tp_flags & Py_TPFLAGS_READYING) == 0); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3802 | |
| 3803 | type->tp_flags |= Py_TPFLAGS_READYING; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3804 | |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 3805 | #ifdef Py_TRACE_REFS |
| 3806 | /* PyType_Ready is the closest thing we have to a choke point |
| 3807 | * for type objects, so is the best place I can think of to try |
| 3808 | * to get type objects into the doubly-linked list of all objects. |
| 3809 | * Still, not all type objects go thru PyType_Ready. |
| 3810 | */ |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 3811 | _Py_AddToAllObjects((PyObject *)type, 0); |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 3812 | #endif |
| 3813 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3814 | /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
| 3815 | base = type->tp_base; |
Martin v. Löwis | bf60875 | 2004-08-18 13:16:54 +0000 | [diff] [blame] | 3816 | if (base == NULL && type != &PyBaseObject_Type) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3817 | base = type->tp_base = &PyBaseObject_Type; |
Martin v. Löwis | bf60875 | 2004-08-18 13:16:54 +0000 | [diff] [blame] | 3818 | Py_INCREF(base); |
| 3819 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3820 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3821 | /* Now the only way base can still be NULL is if type is |
| 3822 | * &PyBaseObject_Type. |
| 3823 | */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 3824 | |
Guido van Rossum | 323a9cf | 2002-08-14 17:26:30 +0000 | [diff] [blame] | 3825 | /* Initialize the base class */ |
| 3826 | if (base && base->tp_dict == NULL) { |
| 3827 | if (PyType_Ready(base) < 0) |
| 3828 | goto error; |
| 3829 | } |
| 3830 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3831 | /* Initialize ob_type if NULL. This means extensions that want to be |
Guido van Rossum | 0986d82 | 2002-04-08 01:38:42 +0000 | [diff] [blame] | 3832 | compilable separately on Windows can call PyType_Ready() instead of |
| 3833 | initializing the ob_type field of their type objects. */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3834 | /* The test for base != NULL is really unnecessary, since base is only |
| 3835 | NULL when type is &PyBaseObject_Type, and we know its ob_type is |
| 3836 | not NULL (it's initialized to &PyType_Type). But coverity doesn't |
| 3837 | know that. */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 3838 | if (Py_TYPE(type) == NULL && base != NULL) |
| 3839 | Py_TYPE(type) = Py_TYPE(base); |
Guido van Rossum | 0986d82 | 2002-04-08 01:38:42 +0000 | [diff] [blame] | 3840 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3841 | /* Initialize tp_bases */ |
| 3842 | bases = type->tp_bases; |
| 3843 | if (bases == NULL) { |
| 3844 | if (base == NULL) |
| 3845 | bases = PyTuple_New(0); |
| 3846 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3847 | bases = PyTuple_Pack(1, base); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3848 | if (bases == NULL) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3849 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3850 | type->tp_bases = bases; |
| 3851 | } |
| 3852 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3853 | /* Initialize tp_dict */ |
| 3854 | dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3855 | if (dict == NULL) { |
| 3856 | dict = PyDict_New(); |
| 3857 | if (dict == NULL) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3858 | goto error; |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3859 | type->tp_dict = dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3860 | } |
| 3861 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3862 | /* Add type-specific descriptors to tp_dict */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3863 | if (add_operators(type) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3864 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3865 | if (type->tp_methods != NULL) { |
| 3866 | if (add_methods(type, type->tp_methods) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3867 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3868 | } |
| 3869 | if (type->tp_members != NULL) { |
| 3870 | if (add_members(type, type->tp_members) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3871 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3872 | } |
| 3873 | if (type->tp_getset != NULL) { |
| 3874 | if (add_getset(type, type->tp_getset) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3875 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3876 | } |
| 3877 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3878 | /* Calculate method resolution order */ |
| 3879 | if (mro_internal(type) < 0) { |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3880 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3881 | } |
| 3882 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3883 | /* Inherit special flags from dominant base */ |
| 3884 | if (type->tp_base != NULL) |
| 3885 | inherit_special(type, type->tp_base); |
| 3886 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3887 | /* Initialize tp_dict properly */ |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 3888 | bases = type->tp_mro; |
| 3889 | assert(bases != NULL); |
| 3890 | assert(PyTuple_Check(bases)); |
| 3891 | n = PyTuple_GET_SIZE(bases); |
| 3892 | for (i = 1; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 3893 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 3894 | if (PyType_Check(b)) |
| 3895 | inherit_slots(type, (PyTypeObject *)b); |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3896 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3897 | |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 3898 | /* Sanity check for tp_free. */ |
| 3899 | if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) && |
| 3900 | (type->tp_free == NULL || type->tp_free == PyObject_Del)) { |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3901 | /* This base class needs to call tp_free, but doesn't have |
| 3902 | * one, or its tp_free is for non-gc'ed objects. |
| 3903 | */ |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 3904 | PyErr_Format(PyExc_TypeError, "type '%.100s' participates in " |
| 3905 | "gc and is a base type but has inappropriate " |
| 3906 | "tp_free slot", |
| 3907 | type->tp_name); |
| 3908 | goto error; |
| 3909 | } |
| 3910 | |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 3911 | /* if the type dictionary doesn't contain a __doc__, set it from |
| 3912 | the tp_doc slot. |
| 3913 | */ |
| 3914 | if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) { |
| 3915 | if (type->tp_doc != NULL) { |
| 3916 | PyObject *doc = PyString_FromString(type->tp_doc); |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 3917 | if (doc == NULL) |
| 3918 | goto error; |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 3919 | PyDict_SetItemString(type->tp_dict, "__doc__", doc); |
| 3920 | Py_DECREF(doc); |
| 3921 | } else { |
Guido van Rossum | d464107 | 2002-04-03 02:13:37 +0000 | [diff] [blame] | 3922 | PyDict_SetItemString(type->tp_dict, |
| 3923 | "__doc__", Py_None); |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 3924 | } |
| 3925 | } |
| 3926 | |
Guido van Rossum | 64c06e3 | 2007-11-22 00:55:51 +0000 | [diff] [blame] | 3927 | /* Hack for tp_hash and __hash__. |
| 3928 | If after all that, tp_hash is still NULL, and __hash__ is not in |
| 3929 | tp_dict, set tp_dict['__hash__'] equal to None. |
| 3930 | This signals that __hash__ is not inherited. |
| 3931 | */ |
| 3932 | if (type->tp_hash == NULL && |
| 3933 | PyDict_GetItemString(type->tp_dict, "__hash__") == NULL && |
| 3934 | PyDict_SetItemString(type->tp_dict, "__hash__", Py_None) < 0) |
| 3935 | { |
| 3936 | goto error; |
| 3937 | } |
| 3938 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3939 | /* Some more special stuff */ |
| 3940 | base = type->tp_base; |
| 3941 | if (base != NULL) { |
| 3942 | if (type->tp_as_number == NULL) |
| 3943 | type->tp_as_number = base->tp_as_number; |
| 3944 | if (type->tp_as_sequence == NULL) |
| 3945 | type->tp_as_sequence = base->tp_as_sequence; |
| 3946 | if (type->tp_as_mapping == NULL) |
| 3947 | type->tp_as_mapping = base->tp_as_mapping; |
Guido van Rossum | eea4718 | 2003-02-11 20:39:59 +0000 | [diff] [blame] | 3948 | if (type->tp_as_buffer == NULL) |
| 3949 | type->tp_as_buffer = base->tp_as_buffer; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3950 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3951 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3952 | /* Link into each base class's list of subclasses */ |
| 3953 | bases = type->tp_bases; |
| 3954 | n = PyTuple_GET_SIZE(bases); |
| 3955 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 3956 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 3957 | if (PyType_Check(b) && |
| 3958 | add_subclass((PyTypeObject *)b, type) < 0) |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3959 | goto error; |
| 3960 | } |
| 3961 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3962 | /* All done -- set the ready flag */ |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3963 | assert(type->tp_dict != NULL); |
| 3964 | type->tp_flags = |
| 3965 | (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3966 | return 0; |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3967 | |
| 3968 | error: |
| 3969 | type->tp_flags &= ~Py_TPFLAGS_READYING; |
| 3970 | return -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3971 | } |
| 3972 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3973 | static int |
| 3974 | add_subclass(PyTypeObject *base, PyTypeObject *type) |
| 3975 | { |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 3976 | Py_ssize_t i; |
| 3977 | int result; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3978 | PyObject *list, *ref, *newobj; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3979 | |
| 3980 | list = base->tp_subclasses; |
| 3981 | if (list == NULL) { |
| 3982 | base->tp_subclasses = list = PyList_New(0); |
| 3983 | if (list == NULL) |
| 3984 | return -1; |
| 3985 | } |
| 3986 | assert(PyList_Check(list)); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3987 | newobj = PyWeakref_NewRef((PyObject *)type, NULL); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3988 | i = PyList_GET_SIZE(list); |
| 3989 | while (--i >= 0) { |
| 3990 | ref = PyList_GET_ITEM(list, i); |
| 3991 | assert(PyWeakref_CheckRef(ref)); |
Guido van Rossum | 3930bc3 | 2002-10-18 13:51:49 +0000 | [diff] [blame] | 3992 | if (PyWeakref_GET_OBJECT(ref) == Py_None) |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3993 | return PyList_SetItem(list, i, newobj); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3994 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3995 | result = PyList_Append(list, newobj); |
| 3996 | Py_DECREF(newobj); |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 3997 | return result; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3998 | } |
| 3999 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4000 | static void |
| 4001 | remove_subclass(PyTypeObject *base, PyTypeObject *type) |
| 4002 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4003 | Py_ssize_t i; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4004 | PyObject *list, *ref; |
| 4005 | |
| 4006 | list = base->tp_subclasses; |
| 4007 | if (list == NULL) { |
| 4008 | return; |
| 4009 | } |
| 4010 | assert(PyList_Check(list)); |
| 4011 | i = PyList_GET_SIZE(list); |
| 4012 | while (--i >= 0) { |
| 4013 | ref = PyList_GET_ITEM(list, i); |
| 4014 | assert(PyWeakref_CheckRef(ref)); |
| 4015 | if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) { |
| 4016 | /* this can't fail, right? */ |
| 4017 | PySequence_DelItem(list, i); |
| 4018 | return; |
| 4019 | } |
| 4020 | } |
| 4021 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4022 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4023 | static int |
| 4024 | check_num_args(PyObject *ob, int n) |
| 4025 | { |
| 4026 | if (!PyTuple_CheckExact(ob)) { |
| 4027 | PyErr_SetString(PyExc_SystemError, |
| 4028 | "PyArg_UnpackTuple() argument list is not a tuple"); |
| 4029 | return 0; |
| 4030 | } |
| 4031 | if (n == PyTuple_GET_SIZE(ob)) |
| 4032 | return 1; |
| 4033 | PyErr_Format( |
| 4034 | PyExc_TypeError, |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 4035 | "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob)); |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4036 | return 0; |
| 4037 | } |
| 4038 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4039 | /* Generic wrappers for overloadable 'operators' such as __getitem__ */ |
| 4040 | |
| 4041 | /* There's a wrapper *function* for each distinct function typedef used |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 4042 | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4043 | wrapper *table* for each distinct operation (e.g. __len__, __add__). |
| 4044 | Most tables have only one entry; the tables for binary operators have two |
| 4045 | entries, one regular and one with reversed arguments. */ |
| 4046 | |
| 4047 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4048 | wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4049 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4050 | lenfunc func = (lenfunc)wrapped; |
| 4051 | Py_ssize_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4052 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4053 | if (!check_num_args(args, 0)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4054 | return NULL; |
| 4055 | res = (*func)(self); |
| 4056 | if (res == -1 && PyErr_Occurred()) |
| 4057 | return NULL; |
| 4058 | return PyInt_FromLong((long)res); |
| 4059 | } |
| 4060 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4061 | static PyObject * |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4062 | wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped) |
| 4063 | { |
| 4064 | inquiry func = (inquiry)wrapped; |
| 4065 | int res; |
| 4066 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4067 | if (!check_num_args(args, 0)) |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4068 | return NULL; |
| 4069 | res = (*func)(self); |
| 4070 | if (res == -1 && PyErr_Occurred()) |
| 4071 | return NULL; |
| 4072 | return PyBool_FromLong((long)res); |
| 4073 | } |
| 4074 | |
| 4075 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4076 | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4077 | { |
| 4078 | binaryfunc func = (binaryfunc)wrapped; |
| 4079 | PyObject *other; |
| 4080 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4081 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4082 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4083 | other = PyTuple_GET_ITEM(args, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4084 | return (*func)(self, other); |
| 4085 | } |
| 4086 | |
| 4087 | static PyObject * |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4088 | wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped) |
| 4089 | { |
| 4090 | binaryfunc func = (binaryfunc)wrapped; |
| 4091 | PyObject *other; |
| 4092 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4093 | if (!check_num_args(args, 1)) |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4094 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4095 | other = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4096 | if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) && |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4097 | !PyType_IsSubtype(other->ob_type, self->ob_type)) { |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4098 | Py_INCREF(Py_NotImplemented); |
| 4099 | return Py_NotImplemented; |
| 4100 | } |
| 4101 | return (*func)(self, other); |
| 4102 | } |
| 4103 | |
| 4104 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4105 | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4106 | { |
| 4107 | binaryfunc func = (binaryfunc)wrapped; |
| 4108 | PyObject *other; |
| 4109 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4110 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4111 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4112 | other = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4113 | if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) && |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4114 | !PyType_IsSubtype(other->ob_type, self->ob_type)) { |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4115 | Py_INCREF(Py_NotImplemented); |
| 4116 | return Py_NotImplemented; |
| 4117 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4118 | return (*func)(other, self); |
| 4119 | } |
| 4120 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4121 | static PyObject * |
| 4122 | wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped) |
| 4123 | { |
| 4124 | coercion func = (coercion)wrapped; |
| 4125 | PyObject *other, *res; |
| 4126 | int ok; |
| 4127 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4128 | if (!check_num_args(args, 1)) |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4129 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4130 | other = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4131 | ok = func(&self, &other); |
| 4132 | if (ok < 0) |
| 4133 | return NULL; |
| 4134 | if (ok > 0) { |
| 4135 | Py_INCREF(Py_NotImplemented); |
| 4136 | return Py_NotImplemented; |
| 4137 | } |
| 4138 | res = PyTuple_New(2); |
| 4139 | if (res == NULL) { |
| 4140 | Py_DECREF(self); |
| 4141 | Py_DECREF(other); |
| 4142 | return NULL; |
| 4143 | } |
| 4144 | PyTuple_SET_ITEM(res, 0, self); |
| 4145 | PyTuple_SET_ITEM(res, 1, other); |
| 4146 | return res; |
| 4147 | } |
| 4148 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4149 | static PyObject * |
| 4150 | wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4151 | { |
| 4152 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4153 | PyObject *other; |
| 4154 | PyObject *third = Py_None; |
| 4155 | |
| 4156 | /* Note: This wrapper only works for __pow__() */ |
| 4157 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4158 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4159 | return NULL; |
| 4160 | return (*func)(self, other, third); |
| 4161 | } |
| 4162 | |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4163 | static PyObject * |
| 4164 | wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4165 | { |
| 4166 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4167 | PyObject *other; |
| 4168 | PyObject *third = Py_None; |
| 4169 | |
| 4170 | /* Note: This wrapper only works for __pow__() */ |
| 4171 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4172 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4173 | return NULL; |
| 4174 | return (*func)(other, self, third); |
| 4175 | } |
| 4176 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4177 | static PyObject * |
| 4178 | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4179 | { |
| 4180 | unaryfunc func = (unaryfunc)wrapped; |
| 4181 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4182 | if (!check_num_args(args, 0)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4183 | return NULL; |
| 4184 | return (*func)(self); |
| 4185 | } |
| 4186 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4187 | static PyObject * |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 4188 | wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4189 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4190 | ssizeargfunc func = (ssizeargfunc)wrapped; |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 4191 | PyObject* o; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4192 | Py_ssize_t i; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4193 | |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 4194 | if (!PyArg_UnpackTuple(args, "", 1, 1, &o)) |
| 4195 | return NULL; |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 4196 | i = PyNumber_AsSsize_t(o, PyExc_OverflowError); |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 4197 | if (i == -1 && PyErr_Occurred()) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4198 | return NULL; |
| 4199 | return (*func)(self, i); |
| 4200 | } |
| 4201 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4202 | static Py_ssize_t |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4203 | getindex(PyObject *self, PyObject *arg) |
| 4204 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4205 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4206 | |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 4207 | i = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4208 | if (i == -1 && PyErr_Occurred()) |
| 4209 | return -1; |
| 4210 | if (i < 0) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4211 | PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4212 | if (sq && sq->sq_length) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4213 | Py_ssize_t n = (*sq->sq_length)(self); |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4214 | if (n < 0) |
| 4215 | return -1; |
| 4216 | i += n; |
| 4217 | } |
| 4218 | } |
| 4219 | return i; |
| 4220 | } |
| 4221 | |
| 4222 | static PyObject * |
| 4223 | wrap_sq_item(PyObject *self, PyObject *args, void *wrapped) |
| 4224 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4225 | ssizeargfunc func = (ssizeargfunc)wrapped; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4226 | PyObject *arg; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4227 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4228 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4229 | if (PyTuple_GET_SIZE(args) == 1) { |
| 4230 | arg = PyTuple_GET_ITEM(args, 0); |
| 4231 | i = getindex(self, arg); |
| 4232 | if (i == -1 && PyErr_Occurred()) |
| 4233 | return NULL; |
| 4234 | return (*func)(self, i); |
| 4235 | } |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4236 | check_num_args(args, 1); |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4237 | assert(PyErr_Occurred()); |
| 4238 | return NULL; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4239 | } |
| 4240 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4241 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4242 | wrap_ssizessizeargfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4243 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4244 | ssizessizeargfunc func = (ssizessizeargfunc)wrapped; |
| 4245 | Py_ssize_t i, j; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4246 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4247 | if (!PyArg_ParseTuple(args, "nn", &i, &j)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4248 | return NULL; |
| 4249 | return (*func)(self, i, j); |
| 4250 | } |
| 4251 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4252 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4253 | wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4254 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4255 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4256 | Py_ssize_t i; |
| 4257 | int res; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4258 | PyObject *arg, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4259 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4260 | if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value)) |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4261 | return NULL; |
| 4262 | i = getindex(self, arg); |
| 4263 | if (i == -1 && PyErr_Occurred()) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4264 | return NULL; |
| 4265 | res = (*func)(self, i, value); |
| 4266 | if (res == -1 && PyErr_Occurred()) |
| 4267 | return NULL; |
| 4268 | Py_INCREF(Py_None); |
| 4269 | return Py_None; |
| 4270 | } |
| 4271 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4272 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4273 | wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4274 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4275 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4276 | Py_ssize_t i; |
| 4277 | int res; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4278 | PyObject *arg; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4279 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4280 | if (!check_num_args(args, 1)) |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4281 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4282 | arg = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4283 | i = getindex(self, arg); |
| 4284 | if (i == -1 && PyErr_Occurred()) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4285 | return NULL; |
| 4286 | res = (*func)(self, i, NULL); |
| 4287 | if (res == -1 && PyErr_Occurred()) |
| 4288 | return NULL; |
| 4289 | Py_INCREF(Py_None); |
| 4290 | return Py_None; |
| 4291 | } |
| 4292 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4293 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4294 | wrap_ssizessizeobjargproc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4295 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4296 | ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped; |
| 4297 | Py_ssize_t i, j; |
| 4298 | int res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4299 | PyObject *value; |
| 4300 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4301 | if (!PyArg_ParseTuple(args, "nnO", &i, &j, &value)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4302 | return NULL; |
| 4303 | res = (*func)(self, i, j, value); |
| 4304 | if (res == -1 && PyErr_Occurred()) |
| 4305 | return NULL; |
| 4306 | Py_INCREF(Py_None); |
| 4307 | return Py_None; |
| 4308 | } |
| 4309 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4310 | static PyObject * |
| 4311 | wrap_delslice(PyObject *self, PyObject *args, void *wrapped) |
| 4312 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4313 | ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped; |
| 4314 | Py_ssize_t i, j; |
| 4315 | int res; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4316 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4317 | if (!PyArg_ParseTuple(args, "nn", &i, &j)) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4318 | return NULL; |
| 4319 | res = (*func)(self, i, j, NULL); |
| 4320 | if (res == -1 && PyErr_Occurred()) |
| 4321 | return NULL; |
| 4322 | Py_INCREF(Py_None); |
| 4323 | return Py_None; |
| 4324 | } |
| 4325 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4326 | /* XXX objobjproc is a misnomer; should be objargpred */ |
| 4327 | static PyObject * |
| 4328 | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) |
| 4329 | { |
| 4330 | objobjproc func = (objobjproc)wrapped; |
| 4331 | int res; |
Guido van Rossum | 22c3dda | 2003-10-09 03:46:35 +0000 | [diff] [blame] | 4332 | PyObject *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4333 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4334 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4335 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4336 | value = PyTuple_GET_ITEM(args, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4337 | res = (*func)(self, value); |
| 4338 | if (res == -1 && PyErr_Occurred()) |
| 4339 | return NULL; |
Guido van Rossum | 22c3dda | 2003-10-09 03:46:35 +0000 | [diff] [blame] | 4340 | else |
| 4341 | return PyBool_FromLong(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4342 | } |
| 4343 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4344 | static PyObject * |
| 4345 | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 4346 | { |
| 4347 | objobjargproc func = (objobjargproc)wrapped; |
| 4348 | int res; |
| 4349 | PyObject *key, *value; |
| 4350 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4351 | if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4352 | return NULL; |
| 4353 | res = (*func)(self, key, value); |
| 4354 | if (res == -1 && PyErr_Occurred()) |
| 4355 | return NULL; |
| 4356 | Py_INCREF(Py_None); |
| 4357 | return Py_None; |
| 4358 | } |
| 4359 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4360 | static PyObject * |
| 4361 | wrap_delitem(PyObject *self, PyObject *args, void *wrapped) |
| 4362 | { |
| 4363 | objobjargproc func = (objobjargproc)wrapped; |
| 4364 | int res; |
| 4365 | PyObject *key; |
| 4366 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4367 | if (!check_num_args(args, 1)) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4368 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4369 | key = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4370 | res = (*func)(self, key, NULL); |
| 4371 | if (res == -1 && PyErr_Occurred()) |
| 4372 | return NULL; |
| 4373 | Py_INCREF(Py_None); |
| 4374 | return Py_None; |
| 4375 | } |
| 4376 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4377 | static PyObject * |
| 4378 | wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4379 | { |
| 4380 | cmpfunc func = (cmpfunc)wrapped; |
| 4381 | int res; |
| 4382 | PyObject *other; |
| 4383 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4384 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4385 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4386 | other = PyTuple_GET_ITEM(args, 0); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4387 | if (Py_TYPE(other)->tp_compare != func && |
| 4388 | !PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) { |
Guido van Rossum | ceccae5 | 2001-09-18 20:03:57 +0000 | [diff] [blame] | 4389 | PyErr_Format( |
| 4390 | PyExc_TypeError, |
| 4391 | "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4392 | Py_TYPE(self)->tp_name, |
| 4393 | Py_TYPE(self)->tp_name, |
| 4394 | Py_TYPE(other)->tp_name); |
Guido van Rossum | ceccae5 | 2001-09-18 20:03:57 +0000 | [diff] [blame] | 4395 | return NULL; |
| 4396 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4397 | res = (*func)(self, other); |
| 4398 | if (PyErr_Occurred()) |
| 4399 | return NULL; |
| 4400 | return PyInt_FromLong((long)res); |
| 4401 | } |
| 4402 | |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4403 | /* Helper to check for object.__setattr__ or __delattr__ applied to a type. |
Guido van Rossum | 52b2705 | 2003-04-15 20:05:10 +0000 | [diff] [blame] | 4404 | This is called the Carlo Verre hack after its discoverer. */ |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4405 | static int |
| 4406 | hackcheck(PyObject *self, setattrofunc func, char *what) |
| 4407 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4408 | PyTypeObject *type = Py_TYPE(self); |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4409 | while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 4410 | type = type->tp_base; |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 4411 | /* If type is NULL now, this is a really weird type. |
| 4412 | In the spirit of backwards compatibility (?), just shut up. */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 4413 | if (type && type->tp_setattro != func) { |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4414 | PyErr_Format(PyExc_TypeError, |
| 4415 | "can't apply this %s to %s object", |
| 4416 | what, |
| 4417 | type->tp_name); |
| 4418 | return 0; |
| 4419 | } |
| 4420 | return 1; |
| 4421 | } |
| 4422 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4423 | static PyObject * |
| 4424 | wrap_setattr(PyObject *self, PyObject *args, void *wrapped) |
| 4425 | { |
| 4426 | setattrofunc func = (setattrofunc)wrapped; |
| 4427 | int res; |
| 4428 | PyObject *name, *value; |
| 4429 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4430 | if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4431 | return NULL; |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4432 | if (!hackcheck(self, func, "__setattr__")) |
| 4433 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4434 | res = (*func)(self, name, value); |
| 4435 | if (res < 0) |
| 4436 | return NULL; |
| 4437 | Py_INCREF(Py_None); |
| 4438 | return Py_None; |
| 4439 | } |
| 4440 | |
| 4441 | static PyObject * |
| 4442 | wrap_delattr(PyObject *self, PyObject *args, void *wrapped) |
| 4443 | { |
| 4444 | setattrofunc func = (setattrofunc)wrapped; |
| 4445 | int res; |
| 4446 | PyObject *name; |
| 4447 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4448 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4449 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4450 | name = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4451 | if (!hackcheck(self, func, "__delattr__")) |
| 4452 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4453 | res = (*func)(self, name, NULL); |
| 4454 | if (res < 0) |
| 4455 | return NULL; |
| 4456 | Py_INCREF(Py_None); |
| 4457 | return Py_None; |
| 4458 | } |
| 4459 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4460 | static PyObject * |
| 4461 | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4462 | { |
| 4463 | hashfunc func = (hashfunc)wrapped; |
| 4464 | long res; |
| 4465 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4466 | if (!check_num_args(args, 0)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4467 | return NULL; |
| 4468 | res = (*func)(self); |
| 4469 | if (res == -1 && PyErr_Occurred()) |
| 4470 | return NULL; |
| 4471 | return PyInt_FromLong(res); |
| 4472 | } |
| 4473 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4474 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4475 | wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4476 | { |
| 4477 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4478 | |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4479 | return (*func)(self, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4480 | } |
| 4481 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4482 | static PyObject * |
| 4483 | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) |
| 4484 | { |
| 4485 | richcmpfunc func = (richcmpfunc)wrapped; |
| 4486 | PyObject *other; |
| 4487 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4488 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4489 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4490 | other = PyTuple_GET_ITEM(args, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4491 | return (*func)(self, other, op); |
| 4492 | } |
| 4493 | |
| 4494 | #undef RICHCMP_WRAPPER |
| 4495 | #define RICHCMP_WRAPPER(NAME, OP) \ |
| 4496 | static PyObject * \ |
| 4497 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ |
| 4498 | { \ |
| 4499 | return wrap_richcmpfunc(self, args, wrapped, OP); \ |
| 4500 | } |
| 4501 | |
Jack Jansen | 8e938b4 | 2001-08-08 15:29:49 +0000 | [diff] [blame] | 4502 | RICHCMP_WRAPPER(lt, Py_LT) |
| 4503 | RICHCMP_WRAPPER(le, Py_LE) |
| 4504 | RICHCMP_WRAPPER(eq, Py_EQ) |
| 4505 | RICHCMP_WRAPPER(ne, Py_NE) |
| 4506 | RICHCMP_WRAPPER(gt, Py_GT) |
| 4507 | RICHCMP_WRAPPER(ge, Py_GE) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4508 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4509 | static PyObject * |
| 4510 | wrap_next(PyObject *self, PyObject *args, void *wrapped) |
| 4511 | { |
| 4512 | unaryfunc func = (unaryfunc)wrapped; |
| 4513 | PyObject *res; |
| 4514 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4515 | if (!check_num_args(args, 0)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4516 | return NULL; |
| 4517 | res = (*func)(self); |
| 4518 | if (res == NULL && !PyErr_Occurred()) |
| 4519 | PyErr_SetNone(PyExc_StopIteration); |
| 4520 | return res; |
| 4521 | } |
| 4522 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4523 | static PyObject * |
| 4524 | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped) |
| 4525 | { |
| 4526 | descrgetfunc func = (descrgetfunc)wrapped; |
| 4527 | PyObject *obj; |
| 4528 | PyObject *type = NULL; |
| 4529 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4530 | if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4531 | return NULL; |
Guido van Rossum | 82ed25c | 2003-02-11 16:25:43 +0000 | [diff] [blame] | 4532 | if (obj == Py_None) |
| 4533 | obj = NULL; |
| 4534 | if (type == Py_None) |
| 4535 | type = NULL; |
| 4536 | if (type == NULL &&obj == NULL) { |
| 4537 | PyErr_SetString(PyExc_TypeError, |
| 4538 | "__get__(None, None) is invalid"); |
| 4539 | return NULL; |
| 4540 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4541 | return (*func)(self, obj, type); |
| 4542 | } |
| 4543 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4544 | static PyObject * |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4545 | wrap_descr_set(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4546 | { |
| 4547 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4548 | PyObject *obj, *value; |
| 4549 | int ret; |
| 4550 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4551 | if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4552 | return NULL; |
| 4553 | ret = (*func)(self, obj, value); |
| 4554 | if (ret < 0) |
| 4555 | return NULL; |
| 4556 | Py_INCREF(Py_None); |
| 4557 | return Py_None; |
| 4558 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 4559 | |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4560 | static PyObject * |
| 4561 | wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped) |
| 4562 | { |
| 4563 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4564 | PyObject *obj; |
| 4565 | int ret; |
| 4566 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4567 | if (!check_num_args(args, 1)) |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4568 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4569 | obj = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4570 | ret = (*func)(self, obj, NULL); |
| 4571 | if (ret < 0) |
| 4572 | return NULL; |
| 4573 | Py_INCREF(Py_None); |
| 4574 | return Py_None; |
| 4575 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4576 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4577 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4578 | wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4579 | { |
| 4580 | initproc func = (initproc)wrapped; |
| 4581 | |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4582 | if (func(self, args, kwds) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4583 | return NULL; |
| 4584 | Py_INCREF(Py_None); |
| 4585 | return Py_None; |
| 4586 | } |
| 4587 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4588 | static PyObject * |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4589 | tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4590 | { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4591 | PyTypeObject *type, *subtype, *staticbase; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4592 | PyObject *arg0, *res; |
| 4593 | |
| 4594 | if (self == NULL || !PyType_Check(self)) |
| 4595 | Py_FatalError("__new__() called with non-type 'self'"); |
| 4596 | type = (PyTypeObject *)self; |
| 4597 | if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4598 | PyErr_Format(PyExc_TypeError, |
| 4599 | "%s.__new__(): not enough arguments", |
| 4600 | type->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4601 | return NULL; |
| 4602 | } |
| 4603 | arg0 = PyTuple_GET_ITEM(args, 0); |
| 4604 | if (!PyType_Check(arg0)) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4605 | PyErr_Format(PyExc_TypeError, |
| 4606 | "%s.__new__(X): X is not a type object (%s)", |
| 4607 | type->tp_name, |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4608 | Py_TYPE(arg0)->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4609 | return NULL; |
| 4610 | } |
| 4611 | subtype = (PyTypeObject *)arg0; |
| 4612 | if (!PyType_IsSubtype(subtype, type)) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4613 | PyErr_Format(PyExc_TypeError, |
| 4614 | "%s.__new__(%s): %s is not a subtype of %s", |
| 4615 | type->tp_name, |
| 4616 | subtype->tp_name, |
| 4617 | subtype->tp_name, |
| 4618 | type->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4619 | return NULL; |
| 4620 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4621 | |
| 4622 | /* Check that the use doesn't do something silly and unsafe like |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 4623 | object.__new__(dict). To do this, we check that the |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4624 | most derived base that's not a heap type is this type. */ |
| 4625 | staticbase = subtype; |
| 4626 | while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 4627 | staticbase = staticbase->tp_base; |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 4628 | /* If staticbase is NULL now, it is a really weird type. |
| 4629 | In the spirit of backwards compatibility (?), just shut up. */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 4630 | if (staticbase && staticbase->tp_new != type->tp_new) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4631 | PyErr_Format(PyExc_TypeError, |
| 4632 | "%s.__new__(%s) is not safe, use %s.__new__()", |
| 4633 | type->tp_name, |
| 4634 | subtype->tp_name, |
| 4635 | staticbase == NULL ? "?" : staticbase->tp_name); |
| 4636 | return NULL; |
| 4637 | } |
| 4638 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4639 | args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 4640 | if (args == NULL) |
| 4641 | return NULL; |
| 4642 | res = type->tp_new(subtype, args, kwds); |
| 4643 | Py_DECREF(args); |
| 4644 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4645 | } |
| 4646 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4647 | static struct PyMethodDef tp_new_methoddef[] = { |
Neal Norwitz | a84dcd7 | 2007-05-22 07:16:44 +0000 | [diff] [blame] | 4648 | {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 4649 | PyDoc_STR("T.__new__(S, ...) -> " |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 4650 | "a new object with type S, a subtype of T")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4651 | {0} |
| 4652 | }; |
| 4653 | |
| 4654 | static int |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4655 | add_tp_new_wrapper(PyTypeObject *type) |
| 4656 | { |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4657 | PyObject *func; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4658 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 4659 | if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL) |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4660 | return 0; |
| 4661 | func = PyCFunction_New(tp_new_methoddef, (PyObject *)type); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4662 | if (func == NULL) |
| 4663 | return -1; |
Raymond Hettinger | 8d726ee | 2004-06-25 22:24:35 +0000 | [diff] [blame] | 4664 | if (PyDict_SetItemString(type->tp_dict, "__new__", func)) { |
Raymond Hettinger | d56cbe5 | 2004-06-25 22:17:39 +0000 | [diff] [blame] | 4665 | Py_DECREF(func); |
| 4666 | return -1; |
| 4667 | } |
| 4668 | Py_DECREF(func); |
| 4669 | return 0; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4670 | } |
| 4671 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4672 | /* Slot wrappers that call the corresponding __foo__ slot. See comments |
| 4673 | below at override_slots() for more explanation. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4674 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4675 | #define SLOT0(FUNCNAME, OPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4676 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4677 | FUNCNAME(PyObject *self) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4678 | { \ |
Guido van Rossum | 5592e4d | 2001-08-28 18:28:21 +0000 | [diff] [blame] | 4679 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4680 | return call_method(self, OPSTR, &cache_str, "()"); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4681 | } |
| 4682 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4683 | #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4684 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4685 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4686 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4687 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4688 | return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4689 | } |
| 4690 | |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4691 | /* Boolean helper for SLOT1BINFULL(). |
| 4692 | right.__class__ is a nontrivial subclass of left.__class__. */ |
| 4693 | static int |
| 4694 | method_is_overloaded(PyObject *left, PyObject *right, char *name) |
| 4695 | { |
| 4696 | PyObject *a, *b; |
| 4697 | int ok; |
| 4698 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4699 | b = PyObject_GetAttrString((PyObject *)(Py_TYPE(right)), name); |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4700 | if (b == NULL) { |
| 4701 | PyErr_Clear(); |
| 4702 | /* If right doesn't have it, it's not overloaded */ |
| 4703 | return 0; |
| 4704 | } |
| 4705 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4706 | a = PyObject_GetAttrString((PyObject *)(Py_TYPE(left)), name); |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4707 | if (a == NULL) { |
| 4708 | PyErr_Clear(); |
| 4709 | Py_DECREF(b); |
| 4710 | /* If right has it but left doesn't, it's overloaded */ |
| 4711 | return 1; |
| 4712 | } |
| 4713 | |
| 4714 | ok = PyObject_RichCompareBool(a, b, Py_NE); |
| 4715 | Py_DECREF(a); |
| 4716 | Py_DECREF(b); |
| 4717 | if (ok < 0) { |
| 4718 | PyErr_Clear(); |
| 4719 | return 0; |
| 4720 | } |
| 4721 | |
| 4722 | return ok; |
| 4723 | } |
| 4724 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4725 | |
| 4726 | #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4727 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4728 | FUNCNAME(PyObject *self, PyObject *other) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4729 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4730 | static PyObject *cache_str, *rcache_str; \ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4731 | int do_other = Py_TYPE(self) != Py_TYPE(other) && \ |
| 4732 | Py_TYPE(other)->tp_as_number != NULL && \ |
| 4733 | Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \ |
| 4734 | if (Py_TYPE(self)->tp_as_number != NULL && \ |
| 4735 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4736 | PyObject *r; \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4737 | if (do_other && \ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4738 | PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \ |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4739 | method_is_overloaded(self, other, ROPSTR)) { \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4740 | r = call_maybe( \ |
| 4741 | other, ROPSTR, &rcache_str, "(O)", self); \ |
| 4742 | if (r != Py_NotImplemented) \ |
| 4743 | return r; \ |
| 4744 | Py_DECREF(r); \ |
| 4745 | do_other = 0; \ |
| 4746 | } \ |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 4747 | r = call_maybe( \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4748 | self, OPSTR, &cache_str, "(O)", other); \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4749 | if (r != Py_NotImplemented || \ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4750 | Py_TYPE(other) == Py_TYPE(self)) \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4751 | return r; \ |
| 4752 | Py_DECREF(r); \ |
| 4753 | } \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4754 | if (do_other) { \ |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 4755 | return call_maybe( \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4756 | other, ROPSTR, &rcache_str, "(O)", self); \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4757 | } \ |
| 4758 | Py_INCREF(Py_NotImplemented); \ |
| 4759 | return Py_NotImplemented; \ |
| 4760 | } |
| 4761 | |
| 4762 | #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ |
| 4763 | SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR) |
| 4764 | |
| 4765 | #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \ |
| 4766 | static PyObject * \ |
| 4767 | FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ |
| 4768 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4769 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4770 | return call_method(self, OPSTR, &cache_str, \ |
| 4771 | "(" ARGCODES ")", arg1, arg2); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4774 | static Py_ssize_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4775 | slot_sq_length(PyObject *self) |
| 4776 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4777 | static PyObject *len_str; |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4778 | PyObject *res = call_method(self, "__len__", &len_str, "()"); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4779 | Py_ssize_t len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4780 | |
| 4781 | if (res == NULL) |
| 4782 | return -1; |
Neal Norwitz | 1872b1c | 2006-08-12 18:44:06 +0000 | [diff] [blame] | 4783 | len = PyInt_AsSsize_t(res); |
Guido van Rossum | 2611162 | 2001-10-01 16:42:49 +0000 | [diff] [blame] | 4784 | Py_DECREF(res); |
Jeremy Hylton | f20fcf9 | 2002-07-25 16:06:15 +0000 | [diff] [blame] | 4785 | if (len < 0) { |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 4786 | if (!PyErr_Occurred()) |
| 4787 | PyErr_SetString(PyExc_ValueError, |
| 4788 | "__len__() should return >= 0"); |
Jeremy Hylton | f20fcf9 | 2002-07-25 16:06:15 +0000 | [diff] [blame] | 4789 | return -1; |
| 4790 | } |
Guido van Rossum | 2611162 | 2001-10-01 16:42:49 +0000 | [diff] [blame] | 4791 | return len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4792 | } |
| 4793 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4794 | /* Super-optimized version of slot_sq_item. |
| 4795 | Other slots could do the same... */ |
| 4796 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4797 | slot_sq_item(PyObject *self, Py_ssize_t i) |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4798 | { |
| 4799 | static PyObject *getitem_str; |
| 4800 | PyObject *func, *args = NULL, *ival = NULL, *retval = NULL; |
| 4801 | descrgetfunc f; |
| 4802 | |
| 4803 | if (getitem_str == NULL) { |
| 4804 | getitem_str = PyString_InternFromString("__getitem__"); |
| 4805 | if (getitem_str == NULL) |
| 4806 | return NULL; |
| 4807 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4808 | func = _PyType_Lookup(Py_TYPE(self), getitem_str); |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4809 | if (func != NULL) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4810 | if ((f = Py_TYPE(func)->tp_descr_get) == NULL) |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4811 | Py_INCREF(func); |
Neal Norwitz | 673cd82 | 2002-10-18 16:33:13 +0000 | [diff] [blame] | 4812 | else { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4813 | func = f(func, self, (PyObject *)(Py_TYPE(self))); |
Neal Norwitz | 673cd82 | 2002-10-18 16:33:13 +0000 | [diff] [blame] | 4814 | if (func == NULL) { |
| 4815 | return NULL; |
| 4816 | } |
| 4817 | } |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 4818 | ival = PyInt_FromSsize_t(i); |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4819 | if (ival != NULL) { |
| 4820 | args = PyTuple_New(1); |
| 4821 | if (args != NULL) { |
| 4822 | PyTuple_SET_ITEM(args, 0, ival); |
| 4823 | retval = PyObject_Call(func, args, NULL); |
| 4824 | Py_XDECREF(args); |
| 4825 | Py_XDECREF(func); |
| 4826 | return retval; |
| 4827 | } |
| 4828 | } |
| 4829 | } |
| 4830 | else { |
| 4831 | PyErr_SetObject(PyExc_AttributeError, getitem_str); |
| 4832 | } |
| 4833 | Py_XDECREF(args); |
| 4834 | Py_XDECREF(ival); |
| 4835 | Py_XDECREF(func); |
| 4836 | return NULL; |
| 4837 | } |
| 4838 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4839 | SLOT2(slot_sq_slice, "__getslice__", Py_ssize_t, Py_ssize_t, "nn") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4840 | |
| 4841 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4842 | slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4843 | { |
| 4844 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4845 | static PyObject *delitem_str, *setitem_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4846 | |
| 4847 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4848 | res = call_method(self, "__delitem__", &delitem_str, |
Thomas Wouters | 4e90810 | 2006-04-21 11:26:56 +0000 | [diff] [blame] | 4849 | "(n)", index); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4850 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4851 | res = call_method(self, "__setitem__", &setitem_str, |
Thomas Wouters | 4e90810 | 2006-04-21 11:26:56 +0000 | [diff] [blame] | 4852 | "(nO)", index, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4853 | if (res == NULL) |
| 4854 | return -1; |
| 4855 | Py_DECREF(res); |
| 4856 | return 0; |
| 4857 | } |
| 4858 | |
| 4859 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4860 | slot_sq_ass_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j, PyObject *value) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4861 | { |
| 4862 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4863 | static PyObject *delslice_str, *setslice_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4864 | |
| 4865 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4866 | res = call_method(self, "__delslice__", &delslice_str, |
Thomas Wouters | 4e90810 | 2006-04-21 11:26:56 +0000 | [diff] [blame] | 4867 | "(nn)", i, j); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4868 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4869 | res = call_method(self, "__setslice__", &setslice_str, |
Thomas Wouters | 4e90810 | 2006-04-21 11:26:56 +0000 | [diff] [blame] | 4870 | "(nnO)", i, j, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4871 | if (res == NULL) |
| 4872 | return -1; |
| 4873 | Py_DECREF(res); |
| 4874 | return 0; |
| 4875 | } |
| 4876 | |
| 4877 | static int |
| 4878 | slot_sq_contains(PyObject *self, PyObject *value) |
| 4879 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4880 | PyObject *func, *res, *args; |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 4881 | int result = -1; |
| 4882 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4883 | static PyObject *contains_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4884 | |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4885 | func = lookup_maybe(self, "__contains__", &contains_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4886 | if (func != NULL) { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4887 | args = PyTuple_Pack(1, value); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4888 | if (args == NULL) |
| 4889 | res = NULL; |
| 4890 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4891 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4892 | Py_DECREF(args); |
| 4893 | } |
| 4894 | Py_DECREF(func); |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 4895 | if (res != NULL) { |
| 4896 | result = PyObject_IsTrue(res); |
| 4897 | Py_DECREF(res); |
| 4898 | } |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4899 | } |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 4900 | else if (! PyErr_Occurred()) { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4901 | /* Possible results: -1 and 1 */ |
| 4902 | result = (int)_PySequence_IterSearch(self, value, |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 4903 | PY_ITERSEARCH_CONTAINS); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4904 | } |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 4905 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4906 | } |
| 4907 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4908 | #define slot_mp_length slot_sq_length |
| 4909 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4910 | SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4911 | |
| 4912 | static int |
| 4913 | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) |
| 4914 | { |
| 4915 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4916 | static PyObject *delitem_str, *setitem_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4917 | |
| 4918 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4919 | res = call_method(self, "__delitem__", &delitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4920 | "(O)", key); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4921 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4922 | res = call_method(self, "__setitem__", &setitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4923 | "(OO)", key, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4924 | if (res == NULL) |
| 4925 | return -1; |
| 4926 | Py_DECREF(res); |
| 4927 | return 0; |
| 4928 | } |
| 4929 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4930 | SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__") |
| 4931 | SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__") |
| 4932 | SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__") |
| 4933 | SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__") |
| 4934 | SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__") |
| 4935 | SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__") |
| 4936 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 4937 | static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4938 | |
| 4939 | SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, |
| 4940 | nb_power, "__pow__", "__rpow__") |
| 4941 | |
| 4942 | static PyObject * |
| 4943 | slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) |
| 4944 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4945 | static PyObject *pow_str; |
| 4946 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4947 | if (modulus == Py_None) |
| 4948 | return slot_nb_power_binary(self, other); |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 4949 | /* Three-arg power doesn't use __rpow__. But ternary_op |
| 4950 | can call this when the second argument's type uses |
| 4951 | slot_nb_power, so check before calling self.__pow__. */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4952 | if (Py_TYPE(self)->tp_as_number != NULL && |
| 4953 | Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) { |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 4954 | return call_method(self, "__pow__", &pow_str, |
| 4955 | "(OO)", other, modulus); |
| 4956 | } |
| 4957 | Py_INCREF(Py_NotImplemented); |
| 4958 | return Py_NotImplemented; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4959 | } |
| 4960 | |
| 4961 | SLOT0(slot_nb_negative, "__neg__") |
| 4962 | SLOT0(slot_nb_positive, "__pos__") |
| 4963 | SLOT0(slot_nb_absolute, "__abs__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4964 | |
| 4965 | static int |
| 4966 | slot_nb_nonzero(PyObject *self) |
| 4967 | { |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 4968 | PyObject *func, *args; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4969 | static PyObject *nonzero_str, *len_str; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 4970 | int result = -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4971 | |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4972 | func = lookup_maybe(self, "__nonzero__", &nonzero_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4973 | if (func == NULL) { |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4974 | if (PyErr_Occurred()) |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4975 | return -1; |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4976 | func = lookup_maybe(self, "__len__", &len_str); |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 4977 | if (func == NULL) |
| 4978 | return PyErr_Occurred() ? -1 : 1; |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 4979 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 4980 | args = PyTuple_New(0); |
| 4981 | if (args != NULL) { |
| 4982 | PyObject *temp = PyObject_Call(func, args, NULL); |
| 4983 | Py_DECREF(args); |
| 4984 | if (temp != NULL) { |
Jeremy Hylton | 3e3159c | 2003-06-27 17:38:27 +0000 | [diff] [blame] | 4985 | if (PyInt_CheckExact(temp) || PyBool_Check(temp)) |
Jeremy Hylton | 090a349 | 2003-06-27 16:46:45 +0000 | [diff] [blame] | 4986 | result = PyObject_IsTrue(temp); |
| 4987 | else { |
| 4988 | PyErr_Format(PyExc_TypeError, |
| 4989 | "__nonzero__ should return " |
| 4990 | "bool or int, returned %s", |
| 4991 | temp->ob_type->tp_name); |
Jeremy Hylton | 3e3159c | 2003-06-27 17:38:27 +0000 | [diff] [blame] | 4992 | result = -1; |
Jeremy Hylton | 090a349 | 2003-06-27 16:46:45 +0000 | [diff] [blame] | 4993 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 4994 | Py_DECREF(temp); |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4995 | } |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4996 | } |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4997 | Py_DECREF(func); |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 4998 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4999 | } |
| 5000 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5001 | |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 5002 | static PyObject * |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5003 | slot_nb_index(PyObject *self) |
| 5004 | { |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5005 | static PyObject *index_str; |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 5006 | return call_method(self, "__index__", &index_str, "()"); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5007 | } |
| 5008 | |
| 5009 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5010 | SLOT0(slot_nb_invert, "__invert__") |
| 5011 | SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") |
| 5012 | SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") |
| 5013 | SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") |
| 5014 | SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") |
| 5015 | SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5016 | |
| 5017 | static int |
| 5018 | slot_nb_coerce(PyObject **a, PyObject **b) |
| 5019 | { |
| 5020 | static PyObject *coerce_str; |
| 5021 | PyObject *self = *a, *other = *b; |
| 5022 | |
| 5023 | if (self->ob_type->tp_as_number != NULL && |
| 5024 | self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) { |
| 5025 | PyObject *r; |
| 5026 | r = call_maybe( |
| 5027 | self, "__coerce__", &coerce_str, "(O)", other); |
| 5028 | if (r == NULL) |
| 5029 | return -1; |
| 5030 | if (r == Py_NotImplemented) { |
| 5031 | Py_DECREF(r); |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5032 | } |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 5033 | else { |
| 5034 | if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) { |
| 5035 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5036 | "__coerce__ didn't return a 2-tuple"); |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 5037 | Py_DECREF(r); |
| 5038 | return -1; |
| 5039 | } |
| 5040 | *a = PyTuple_GET_ITEM(r, 0); |
| 5041 | Py_INCREF(*a); |
| 5042 | *b = PyTuple_GET_ITEM(r, 1); |
| 5043 | Py_INCREF(*b); |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5044 | Py_DECREF(r); |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 5045 | return 0; |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5046 | } |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5047 | } |
| 5048 | if (other->ob_type->tp_as_number != NULL && |
| 5049 | other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) { |
| 5050 | PyObject *r; |
| 5051 | r = call_maybe( |
| 5052 | other, "__coerce__", &coerce_str, "(O)", self); |
| 5053 | if (r == NULL) |
| 5054 | return -1; |
| 5055 | if (r == Py_NotImplemented) { |
| 5056 | Py_DECREF(r); |
| 5057 | return 1; |
| 5058 | } |
| 5059 | if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) { |
| 5060 | PyErr_SetString(PyExc_TypeError, |
| 5061 | "__coerce__ didn't return a 2-tuple"); |
| 5062 | Py_DECREF(r); |
| 5063 | return -1; |
| 5064 | } |
| 5065 | *a = PyTuple_GET_ITEM(r, 1); |
| 5066 | Py_INCREF(*a); |
| 5067 | *b = PyTuple_GET_ITEM(r, 0); |
| 5068 | Py_INCREF(*b); |
| 5069 | Py_DECREF(r); |
| 5070 | return 0; |
| 5071 | } |
| 5072 | return 1; |
| 5073 | } |
| 5074 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5075 | SLOT0(slot_nb_int, "__int__") |
| 5076 | SLOT0(slot_nb_long, "__long__") |
| 5077 | SLOT0(slot_nb_float, "__float__") |
| 5078 | SLOT0(slot_nb_oct, "__oct__") |
| 5079 | SLOT0(slot_nb_hex, "__hex__") |
| 5080 | SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O") |
| 5081 | SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O") |
| 5082 | SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O") |
| 5083 | SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O") |
| 5084 | SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O") |
Martin v. Löwis | fd96326 | 2007-02-09 12:19:32 +0000 | [diff] [blame] | 5085 | /* Can't use SLOT1 here, because nb_inplace_power is ternary */ |
| 5086 | static PyObject * |
| 5087 | slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2) |
| 5088 | { |
| 5089 | static PyObject *cache_str; |
| 5090 | return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1); |
| 5091 | } |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5092 | SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O") |
| 5093 | SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O") |
| 5094 | SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O") |
| 5095 | SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O") |
| 5096 | SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O") |
| 5097 | SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, |
| 5098 | "__floordiv__", "__rfloordiv__") |
| 5099 | SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__") |
| 5100 | SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O") |
| 5101 | SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5102 | |
| 5103 | static int |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5104 | half_compare(PyObject *self, PyObject *other) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5105 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5106 | PyObject *func, *args, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5107 | static PyObject *cmp_str; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5108 | Py_ssize_t c; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5109 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5110 | func = lookup_method(self, "__cmp__", &cmp_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5111 | if (func == NULL) { |
| 5112 | PyErr_Clear(); |
| 5113 | } |
| 5114 | else { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 5115 | args = PyTuple_Pack(1, other); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5116 | if (args == NULL) |
| 5117 | res = NULL; |
| 5118 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5119 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5120 | Py_DECREF(args); |
| 5121 | } |
Raymond Hettinger | ab5dae3 | 2002-06-24 13:08:16 +0000 | [diff] [blame] | 5122 | Py_DECREF(func); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5123 | if (res != Py_NotImplemented) { |
| 5124 | if (res == NULL) |
| 5125 | return -2; |
| 5126 | c = PyInt_AsLong(res); |
| 5127 | Py_DECREF(res); |
| 5128 | if (c == -1 && PyErr_Occurred()) |
| 5129 | return -2; |
| 5130 | return (c < 0) ? -1 : (c > 0) ? 1 : 0; |
| 5131 | } |
| 5132 | Py_DECREF(res); |
| 5133 | } |
| 5134 | return 2; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5135 | } |
| 5136 | |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 5137 | /* This slot is published for the benefit of try_3way_compare in object.c */ |
| 5138 | int |
| 5139 | _PyObject_SlotCompare(PyObject *self, PyObject *other) |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5140 | { |
| 5141 | int c; |
| 5142 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5143 | if (Py_TYPE(self)->tp_compare == _PyObject_SlotCompare) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5144 | c = half_compare(self, other); |
| 5145 | if (c <= 1) |
| 5146 | return c; |
| 5147 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5148 | if (Py_TYPE(other)->tp_compare == _PyObject_SlotCompare) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5149 | c = half_compare(other, self); |
| 5150 | if (c < -1) |
| 5151 | return -2; |
| 5152 | if (c <= 1) |
| 5153 | return -c; |
| 5154 | } |
| 5155 | return (void *)self < (void *)other ? -1 : |
| 5156 | (void *)self > (void *)other ? 1 : 0; |
| 5157 | } |
| 5158 | |
| 5159 | static PyObject * |
| 5160 | slot_tp_repr(PyObject *self) |
| 5161 | { |
| 5162 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5163 | static PyObject *repr_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5164 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5165 | func = lookup_method(self, "__repr__", &repr_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5166 | if (func != NULL) { |
| 5167 | res = PyEval_CallObject(func, NULL); |
| 5168 | Py_DECREF(func); |
| 5169 | return res; |
| 5170 | } |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 5171 | PyErr_Clear(); |
| 5172 | return PyString_FromFormat("<%s object at %p>", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5173 | Py_TYPE(self)->tp_name, self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5174 | } |
| 5175 | |
| 5176 | static PyObject * |
| 5177 | slot_tp_str(PyObject *self) |
| 5178 | { |
| 5179 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5180 | static PyObject *str_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5181 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5182 | func = lookup_method(self, "__str__", &str_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5183 | if (func != NULL) { |
| 5184 | res = PyEval_CallObject(func, NULL); |
| 5185 | Py_DECREF(func); |
| 5186 | return res; |
| 5187 | } |
| 5188 | else { |
| 5189 | PyErr_Clear(); |
| 5190 | return slot_tp_repr(self); |
| 5191 | } |
| 5192 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5193 | |
| 5194 | static long |
| 5195 | slot_tp_hash(PyObject *self) |
| 5196 | { |
Tim Peters | 61ce0a9 | 2002-12-06 23:38:02 +0000 | [diff] [blame] | 5197 | PyObject *func; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5198 | static PyObject *hash_str, *eq_str, *cmp_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5199 | long h; |
| 5200 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5201 | func = lookup_method(self, "__hash__", &hash_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5202 | |
Guido van Rossum | 0b7b6fd | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 5203 | if (func != NULL && func != Py_None) { |
Tim Peters | 61ce0a9 | 2002-12-06 23:38:02 +0000 | [diff] [blame] | 5204 | PyObject *res = PyEval_CallObject(func, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5205 | Py_DECREF(func); |
| 5206 | if (res == NULL) |
| 5207 | return -1; |
Martin v. Löwis | ab2f8f7 | 2006-08-09 07:57:39 +0000 | [diff] [blame] | 5208 | if (PyLong_Check(res)) |
Armin Rigo | 51fc8c4 | 2006-08-09 14:55:26 +0000 | [diff] [blame] | 5209 | h = PyLong_Type.tp_hash(res); |
Martin v. Löwis | ab2f8f7 | 2006-08-09 07:57:39 +0000 | [diff] [blame] | 5210 | else |
| 5211 | h = PyInt_AsLong(res); |
Tim Peters | 61ce0a9 | 2002-12-06 23:38:02 +0000 | [diff] [blame] | 5212 | Py_DECREF(res); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5213 | } |
| 5214 | else { |
Georg Brandl | 30b7804 | 2007-12-20 21:03:02 +0000 | [diff] [blame] | 5215 | Py_XDECREF(func); /* may be None */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5216 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5217 | func = lookup_method(self, "__eq__", &eq_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5218 | if (func == NULL) { |
| 5219 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5220 | func = lookup_method(self, "__cmp__", &cmp_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5221 | } |
| 5222 | if (func != NULL) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 5223 | PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'", |
| 5224 | self->ob_type->tp_name); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5225 | Py_DECREF(func); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5226 | return -1; |
| 5227 | } |
| 5228 | PyErr_Clear(); |
| 5229 | h = _Py_HashPointer((void *)self); |
| 5230 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5231 | if (h == -1 && !PyErr_Occurred()) |
| 5232 | h = -2; |
| 5233 | return h; |
| 5234 | } |
| 5235 | |
| 5236 | static PyObject * |
| 5237 | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 5238 | { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5239 | static PyObject *call_str; |
| 5240 | PyObject *meth = lookup_method(self, "__call__", &call_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5241 | PyObject *res; |
| 5242 | |
| 5243 | if (meth == NULL) |
| 5244 | return NULL; |
Armin Rigo | 53c1692f | 2006-06-21 21:58:50 +0000 | [diff] [blame] | 5245 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5246 | res = PyObject_Call(meth, args, kwds); |
Armin Rigo | 53c1692f | 2006-06-21 21:58:50 +0000 | [diff] [blame] | 5247 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5248 | Py_DECREF(meth); |
| 5249 | return res; |
| 5250 | } |
| 5251 | |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5252 | /* There are two slot dispatch functions for tp_getattro. |
| 5253 | |
| 5254 | - slot_tp_getattro() is used when __getattribute__ is overridden |
| 5255 | but no __getattr__ hook is present; |
| 5256 | |
| 5257 | - slot_tp_getattr_hook() is used when a __getattr__ hook is present. |
| 5258 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5259 | The code in update_one_slot() always installs slot_tp_getattr_hook(); this |
| 5260 | detects the absence of __getattr__ and then installs the simpler slot if |
| 5261 | necessary. */ |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5262 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5263 | static PyObject * |
| 5264 | slot_tp_getattro(PyObject *self, PyObject *name) |
| 5265 | { |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5266 | static PyObject *getattribute_str = NULL; |
| 5267 | return call_method(self, "__getattribute__", &getattribute_str, |
| 5268 | "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5269 | } |
| 5270 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5271 | static PyObject * |
| 5272 | slot_tp_getattr_hook(PyObject *self, PyObject *name) |
| 5273 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5274 | PyTypeObject *tp = Py_TYPE(self); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5275 | PyObject *getattr, *getattribute, *res; |
| 5276 | static PyObject *getattribute_str = NULL; |
| 5277 | static PyObject *getattr_str = NULL; |
| 5278 | |
| 5279 | if (getattr_str == NULL) { |
| 5280 | getattr_str = PyString_InternFromString("__getattr__"); |
| 5281 | if (getattr_str == NULL) |
| 5282 | return NULL; |
| 5283 | } |
| 5284 | if (getattribute_str == NULL) { |
| 5285 | getattribute_str = |
| 5286 | PyString_InternFromString("__getattribute__"); |
| 5287 | if (getattribute_str == NULL) |
| 5288 | return NULL; |
| 5289 | } |
| 5290 | getattr = _PyType_Lookup(tp, getattr_str); |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5291 | if (getattr == NULL) { |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5292 | /* No __getattr__ hook: use a simpler dispatcher */ |
| 5293 | tp->tp_getattro = slot_tp_getattro; |
| 5294 | return slot_tp_getattro(self, name); |
| 5295 | } |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5296 | getattribute = _PyType_Lookup(tp, getattribute_str); |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5297 | if (getattribute == NULL || |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5298 | (Py_TYPE(getattribute) == &PyWrapperDescr_Type && |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5299 | ((PyWrapperDescrObject *)getattribute)->d_wrapped == |
| 5300 | (void *)PyObject_GenericGetAttr)) |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5301 | res = PyObject_GenericGetAttr(self, name); |
| 5302 | else |
Georg Brandl | 684fd0c | 2006-05-25 19:15:31 +0000 | [diff] [blame] | 5303 | res = PyObject_CallFunctionObjArgs(getattribute, self, name, NULL); |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5304 | if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5305 | PyErr_Clear(); |
Georg Brandl | 684fd0c | 2006-05-25 19:15:31 +0000 | [diff] [blame] | 5306 | res = PyObject_CallFunctionObjArgs(getattr, self, name, NULL); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5307 | } |
| 5308 | return res; |
| 5309 | } |
| 5310 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5311 | static int |
| 5312 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) |
| 5313 | { |
| 5314 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5315 | static PyObject *delattr_str, *setattr_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5316 | |
| 5317 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5318 | res = call_method(self, "__delattr__", &delattr_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5319 | "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5320 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5321 | res = call_method(self, "__setattr__", &setattr_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5322 | "(OO)", name, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5323 | if (res == NULL) |
| 5324 | return -1; |
| 5325 | Py_DECREF(res); |
| 5326 | return 0; |
| 5327 | } |
| 5328 | |
Guido van Rossum | 0b7b6fd | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 5329 | static char *name_op[] = { |
| 5330 | "__lt__", |
| 5331 | "__le__", |
| 5332 | "__eq__", |
| 5333 | "__ne__", |
| 5334 | "__gt__", |
| 5335 | "__ge__", |
| 5336 | }; |
| 5337 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5338 | static PyObject * |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5339 | half_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5340 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5341 | PyObject *func, *args, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5342 | static PyObject *op_str[6]; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5343 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5344 | func = lookup_method(self, name_op[op], &op_str[op]); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5345 | if (func == NULL) { |
| 5346 | PyErr_Clear(); |
| 5347 | Py_INCREF(Py_NotImplemented); |
| 5348 | return Py_NotImplemented; |
| 5349 | } |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 5350 | args = PyTuple_Pack(1, other); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5351 | if (args == NULL) |
| 5352 | res = NULL; |
| 5353 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5354 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5355 | Py_DECREF(args); |
| 5356 | } |
| 5357 | Py_DECREF(func); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5358 | return res; |
| 5359 | } |
| 5360 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5361 | static PyObject * |
| 5362 | slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
| 5363 | { |
| 5364 | PyObject *res; |
| 5365 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5366 | if (Py_TYPE(self)->tp_richcompare == slot_tp_richcompare) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5367 | res = half_richcompare(self, other, op); |
| 5368 | if (res != Py_NotImplemented) |
| 5369 | return res; |
| 5370 | Py_DECREF(res); |
| 5371 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5372 | if (Py_TYPE(other)->tp_richcompare == slot_tp_richcompare) { |
Tim Peters | f4aca75 | 2004-09-23 02:39:37 +0000 | [diff] [blame] | 5373 | res = half_richcompare(other, self, _Py_SwappedOp[op]); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5374 | if (res != Py_NotImplemented) { |
| 5375 | return res; |
| 5376 | } |
| 5377 | Py_DECREF(res); |
| 5378 | } |
| 5379 | Py_INCREF(Py_NotImplemented); |
| 5380 | return Py_NotImplemented; |
| 5381 | } |
| 5382 | |
| 5383 | static PyObject * |
| 5384 | slot_tp_iter(PyObject *self) |
| 5385 | { |
| 5386 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5387 | static PyObject *iter_str, *getitem_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5388 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5389 | func = lookup_method(self, "__iter__", &iter_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5390 | if (func != NULL) { |
Guido van Rossum | 84b2bed | 2002-08-16 17:01:09 +0000 | [diff] [blame] | 5391 | PyObject *args; |
| 5392 | args = res = PyTuple_New(0); |
| 5393 | if (args != NULL) { |
| 5394 | res = PyObject_Call(func, args, NULL); |
| 5395 | Py_DECREF(args); |
| 5396 | } |
| 5397 | Py_DECREF(func); |
| 5398 | return res; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5399 | } |
| 5400 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5401 | func = lookup_method(self, "__getitem__", &getitem_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5402 | if (func == NULL) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 5403 | PyErr_Format(PyExc_TypeError, |
| 5404 | "'%.200s' object is not iterable", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5405 | Py_TYPE(self)->tp_name); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5406 | return NULL; |
| 5407 | } |
| 5408 | Py_DECREF(func); |
| 5409 | return PySeqIter_New(self); |
| 5410 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5411 | |
| 5412 | static PyObject * |
| 5413 | slot_tp_iternext(PyObject *self) |
| 5414 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5415 | static PyObject *next_str; |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5416 | return call_method(self, "next", &next_str, "()"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5417 | } |
| 5418 | |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5419 | static PyObject * |
| 5420 | slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 5421 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5422 | PyTypeObject *tp = Py_TYPE(self); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5423 | PyObject *get; |
| 5424 | static PyObject *get_str = NULL; |
| 5425 | |
| 5426 | if (get_str == NULL) { |
| 5427 | get_str = PyString_InternFromString("__get__"); |
| 5428 | if (get_str == NULL) |
| 5429 | return NULL; |
| 5430 | } |
| 5431 | get = _PyType_Lookup(tp, get_str); |
| 5432 | if (get == NULL) { |
| 5433 | /* Avoid further slowdowns */ |
| 5434 | if (tp->tp_descr_get == slot_tp_descr_get) |
| 5435 | tp->tp_descr_get = NULL; |
| 5436 | Py_INCREF(self); |
| 5437 | return self; |
| 5438 | } |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5439 | if (obj == NULL) |
| 5440 | obj = Py_None; |
| 5441 | if (type == NULL) |
| 5442 | type = Py_None; |
Georg Brandl | 684fd0c | 2006-05-25 19:15:31 +0000 | [diff] [blame] | 5443 | return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5444 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5445 | |
| 5446 | static int |
| 5447 | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) |
| 5448 | { |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5449 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5450 | static PyObject *del_str, *set_str; |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5451 | |
| 5452 | if (value == NULL) |
Guido van Rossum | 1d5b3f2 | 2001-12-03 00:08:33 +0000 | [diff] [blame] | 5453 | res = call_method(self, "__delete__", &del_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5454 | "(O)", target); |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5455 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5456 | res = call_method(self, "__set__", &set_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5457 | "(OO)", target, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5458 | if (res == NULL) |
| 5459 | return -1; |
| 5460 | Py_DECREF(res); |
| 5461 | return 0; |
| 5462 | } |
| 5463 | |
| 5464 | static int |
| 5465 | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 5466 | { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5467 | static PyObject *init_str; |
| 5468 | PyObject *meth = lookup_method(self, "__init__", &init_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5469 | PyObject *res; |
| 5470 | |
| 5471 | if (meth == NULL) |
| 5472 | return -1; |
| 5473 | res = PyObject_Call(meth, args, kwds); |
| 5474 | Py_DECREF(meth); |
| 5475 | if (res == NULL) |
| 5476 | return -1; |
Raymond Hettinger | b67cc80 | 2005-03-03 16:45:19 +0000 | [diff] [blame] | 5477 | if (res != Py_None) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 5478 | PyErr_Format(PyExc_TypeError, |
| 5479 | "__init__() should return None, not '%.200s'", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5480 | Py_TYPE(res)->tp_name); |
Raymond Hettinger | b67cc80 | 2005-03-03 16:45:19 +0000 | [diff] [blame] | 5481 | Py_DECREF(res); |
| 5482 | return -1; |
| 5483 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5484 | Py_DECREF(res); |
| 5485 | return 0; |
| 5486 | } |
| 5487 | |
| 5488 | static PyObject * |
| 5489 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 5490 | { |
Guido van Rossum | 7bed213 | 2002-08-08 21:57:53 +0000 | [diff] [blame] | 5491 | static PyObject *new_str; |
| 5492 | PyObject *func; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5493 | PyObject *newargs, *x; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5494 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5495 | |
Guido van Rossum | 7bed213 | 2002-08-08 21:57:53 +0000 | [diff] [blame] | 5496 | if (new_str == NULL) { |
| 5497 | new_str = PyString_InternFromString("__new__"); |
| 5498 | if (new_str == NULL) |
| 5499 | return NULL; |
| 5500 | } |
| 5501 | func = PyObject_GetAttr((PyObject *)type, new_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5502 | if (func == NULL) |
| 5503 | return NULL; |
| 5504 | assert(PyTuple_Check(args)); |
| 5505 | n = PyTuple_GET_SIZE(args); |
| 5506 | newargs = PyTuple_New(n+1); |
| 5507 | if (newargs == NULL) |
| 5508 | return NULL; |
| 5509 | Py_INCREF(type); |
| 5510 | PyTuple_SET_ITEM(newargs, 0, (PyObject *)type); |
| 5511 | for (i = 0; i < n; i++) { |
| 5512 | x = PyTuple_GET_ITEM(args, i); |
| 5513 | Py_INCREF(x); |
| 5514 | PyTuple_SET_ITEM(newargs, i+1, x); |
| 5515 | } |
| 5516 | x = PyObject_Call(func, newargs, kwds); |
Guido van Rossum | 25d1807 | 2001-10-01 15:55:28 +0000 | [diff] [blame] | 5517 | Py_DECREF(newargs); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5518 | Py_DECREF(func); |
| 5519 | return x; |
| 5520 | } |
| 5521 | |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5522 | static void |
| 5523 | slot_tp_del(PyObject *self) |
| 5524 | { |
| 5525 | static PyObject *del_str = NULL; |
| 5526 | PyObject *del, *res; |
| 5527 | PyObject *error_type, *error_value, *error_traceback; |
| 5528 | |
| 5529 | /* Temporarily resurrect the object. */ |
| 5530 | assert(self->ob_refcnt == 0); |
| 5531 | self->ob_refcnt = 1; |
| 5532 | |
| 5533 | /* Save the current exception, if any. */ |
| 5534 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 5535 | |
| 5536 | /* Execute __del__ method, if any. */ |
| 5537 | del = lookup_maybe(self, "__del__", &del_str); |
| 5538 | if (del != NULL) { |
| 5539 | res = PyEval_CallObject(del, NULL); |
| 5540 | if (res == NULL) |
| 5541 | PyErr_WriteUnraisable(del); |
| 5542 | else |
| 5543 | Py_DECREF(res); |
| 5544 | Py_DECREF(del); |
| 5545 | } |
| 5546 | |
| 5547 | /* Restore the saved exception. */ |
| 5548 | PyErr_Restore(error_type, error_value, error_traceback); |
| 5549 | |
| 5550 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 5551 | * cause a recursive call. |
| 5552 | */ |
| 5553 | assert(self->ob_refcnt > 0); |
| 5554 | if (--self->ob_refcnt == 0) |
| 5555 | return; /* this is the normal path out */ |
| 5556 | |
| 5557 | /* __del__ resurrected it! Make it look like the original Py_DECREF |
| 5558 | * never happened. |
| 5559 | */ |
| 5560 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 5561 | Py_ssize_t refcnt = self->ob_refcnt; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5562 | _Py_NewReference(self); |
| 5563 | self->ob_refcnt = refcnt; |
| 5564 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5565 | assert(!PyType_IS_GC(Py_TYPE(self)) || |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5566 | _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED); |
Michael W. Hudson | 3f3b668 | 2004-08-03 10:21:03 +0000 | [diff] [blame] | 5567 | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so |
| 5568 | * we need to undo that. */ |
| 5569 | _Py_DEC_REFTOTAL; |
| 5570 | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object |
| 5571 | * chain, so no more to do there. |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5572 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
| 5573 | * _Py_NewReference bumped tp_allocs: both of those need to be |
| 5574 | * undone. |
| 5575 | */ |
| 5576 | #ifdef COUNT_ALLOCS |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5577 | --Py_TYPE(self)->tp_frees; |
| 5578 | --Py_TYPE(self)->tp_allocs; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5579 | #endif |
| 5580 | } |
| 5581 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5582 | |
| 5583 | /* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5584 | functions. The offsets here are relative to the 'PyHeapTypeObject' |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5585 | structure, which incorporates the additional structures used for numbers, |
| 5586 | sequences and mappings. |
| 5587 | Note that multiple names may map to the same slot (e.g. __eq__, |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5588 | __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] | 5589 | slots (e.g. __str__ affects tp_str as well as tp_repr). The table is |
| 5590 | terminated with an all-zero entry. (This table is further initialized and |
| 5591 | sorted in init_slotdefs() below.) */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5592 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5593 | typedef struct wrapperbase slotdef; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5594 | |
| 5595 | #undef TPSLOT |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5596 | #undef FLSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5597 | #undef ETSLOT |
| 5598 | #undef SQSLOT |
| 5599 | #undef MPSLOT |
| 5600 | #undef NBSLOT |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5601 | #undef UNSLOT |
| 5602 | #undef IBSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5603 | #undef BINSLOT |
| 5604 | #undef RBINSLOT |
| 5605 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5606 | #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Neal Norwitz | d47714a | 2002-08-13 19:01:38 +0000 | [diff] [blame] | 5607 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5608 | PyDoc_STR(DOC)} |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5609 | #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ |
| 5610 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
Neal Norwitz | d47714a | 2002-08-13 19:01:38 +0000 | [diff] [blame] | 5611 | PyDoc_STR(DOC), FLAGS} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5612 | #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5613 | {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
Neal Norwitz | d47714a | 2002-08-13 19:01:38 +0000 | [diff] [blame] | 5614 | PyDoc_STR(DOC)} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5615 | #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5616 | ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) |
| 5617 | #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5618 | ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) |
| 5619 | #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5620 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) |
| 5621 | #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5622 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5623 | "x." NAME "() <==> " DOC) |
| 5624 | #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5625 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5626 | "x." NAME "(y) <==> x" DOC "y") |
| 5627 | #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
| 5628 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5629 | "x." NAME "(y) <==> x" DOC "y") |
| 5630 | #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
| 5631 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5632 | "x." NAME "(y) <==> y" DOC "x") |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5633 | #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
| 5634 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5635 | "x." NAME "(y) <==> " DOC) |
| 5636 | #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
| 5637 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5638 | "x." NAME "(y) <==> " DOC) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5639 | |
| 5640 | static slotdef slotdefs[] = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5641 | SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5642 | "x.__len__() <==> len(x)"), |
Armin Rigo | fd163f9 | 2005-12-29 15:59:19 +0000 | [diff] [blame] | 5643 | /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL. |
| 5644 | The logic in abstract.c always falls back to nb_add/nb_multiply in |
| 5645 | this case. Defining both the nb_* and the sq_* slots to call the |
| 5646 | user-defined methods has unexpected side-effects, as shown by |
| 5647 | test_descr.notimplemented() */ |
| 5648 | SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc, |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5649 | "x.__add__(y) <==> x+y"), |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 5650 | SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc, |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5651 | "x.__mul__(n) <==> x*n"), |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 5652 | SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc, |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5653 | "x.__rmul__(n) <==> n*x"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5654 | SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, |
| 5655 | "x.__getitem__(y) <==> x[y]"), |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5656 | SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_ssizessizeargfunc, |
Brett Cannon | 154da9b | 2003-05-20 02:30:04 +0000 | [diff] [blame] | 5657 | "x.__getslice__(i, j) <==> x[i:j]\n\ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5658 | \n\ |
| 5659 | Use of negative indices is not supported."), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5660 | SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, |
Brett Cannon | be67d87 | 2003-05-20 02:40:12 +0000 | [diff] [blame] | 5661 | "x.__setitem__(i, y) <==> x[i]=y"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5662 | SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, |
Brett Cannon | be67d87 | 2003-05-20 02:40:12 +0000 | [diff] [blame] | 5663 | "x.__delitem__(y) <==> del x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5664 | SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5665 | wrap_ssizessizeobjargproc, |
Brett Cannon | be67d87 | 2003-05-20 02:40:12 +0000 | [diff] [blame] | 5666 | "x.__setslice__(i, j, y) <==> x[i:j]=y\n\ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5667 | \n\ |
| 5668 | Use of negative indices is not supported."), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5669 | SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice, |
Brett Cannon | be67d87 | 2003-05-20 02:40:12 +0000 | [diff] [blame] | 5670 | "x.__delslice__(i, j) <==> del x[i:j]\n\ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5671 | \n\ |
| 5672 | Use of negative indices is not supported."), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5673 | SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, |
| 5674 | "x.__contains__(y) <==> y in x"), |
Armin Rigo | fd163f9 | 2005-12-29 15:59:19 +0000 | [diff] [blame] | 5675 | SQSLOT("__iadd__", sq_inplace_concat, NULL, |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5676 | wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"), |
Armin Rigo | fd163f9 | 2005-12-29 15:59:19 +0000 | [diff] [blame] | 5677 | SQSLOT("__imul__", sq_inplace_repeat, NULL, |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5678 | wrap_indexargfunc, "x.__imul__(y) <==> x*=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5679 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5680 | MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5681 | "x.__len__() <==> len(x)"), |
Guido van Rossum | fd38f8e | 2001-10-09 20:17:57 +0000 | [diff] [blame] | 5682 | MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5683 | wrap_binaryfunc, |
| 5684 | "x.__getitem__(y) <==> x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5685 | MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5686 | wrap_objobjargproc, |
| 5687 | "x.__setitem__(i, y) <==> x[i]=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5688 | MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5689 | wrap_delitem, |
| 5690 | "x.__delitem__(y) <==> del x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5691 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5692 | BINSLOT("__add__", nb_add, slot_nb_add, |
| 5693 | "+"), |
| 5694 | RBINSLOT("__radd__", nb_add, slot_nb_add, |
| 5695 | "+"), |
| 5696 | BINSLOT("__sub__", nb_subtract, slot_nb_subtract, |
| 5697 | "-"), |
| 5698 | RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract, |
| 5699 | "-"), |
| 5700 | BINSLOT("__mul__", nb_multiply, slot_nb_multiply, |
| 5701 | "*"), |
| 5702 | RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply, |
| 5703 | "*"), |
| 5704 | BINSLOT("__div__", nb_divide, slot_nb_divide, |
| 5705 | "/"), |
| 5706 | RBINSLOT("__rdiv__", nb_divide, slot_nb_divide, |
| 5707 | "/"), |
| 5708 | BINSLOT("__mod__", nb_remainder, slot_nb_remainder, |
| 5709 | "%"), |
| 5710 | RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder, |
| 5711 | "%"), |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5712 | BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5713 | "divmod(x, y)"), |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5714 | RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5715 | "divmod(y, x)"), |
| 5716 | NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, |
| 5717 | "x.__pow__(y[, z]) <==> pow(x, y[, z])"), |
| 5718 | NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, |
| 5719 | "y.__rpow__(x[, z]) <==> pow(x, y[, z])"), |
| 5720 | UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"), |
| 5721 | UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"), |
| 5722 | UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, |
| 5723 | "abs(x)"), |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 5724 | UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquirypred, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5725 | "x != 0"), |
| 5726 | UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"), |
| 5727 | BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5728 | RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5729 | BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5730 | RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5731 | BINSLOT("__and__", nb_and, slot_nb_and, "&"), |
| 5732 | RBINSLOT("__rand__", nb_and, slot_nb_and, "&"), |
| 5733 | BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"), |
| 5734 | RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), |
| 5735 | BINSLOT("__or__", nb_or, slot_nb_or, "|"), |
| 5736 | RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), |
| 5737 | NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc, |
| 5738 | "x.__coerce__(y) <==> coerce(x, y)"), |
| 5739 | UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, |
| 5740 | "int(x)"), |
| 5741 | UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc, |
| 5742 | "long(x)"), |
| 5743 | UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, |
| 5744 | "float(x)"), |
| 5745 | UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc, |
| 5746 | "oct(x)"), |
| 5747 | UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc, |
| 5748 | "hex(x)"), |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 5749 | NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc, |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5750 | "x[y:z] <==> x[y.__index__():z.__index__()]"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5751 | IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, |
| 5752 | wrap_binaryfunc, "+"), |
| 5753 | IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, |
| 5754 | wrap_binaryfunc, "-"), |
| 5755 | IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply, |
| 5756 | wrap_binaryfunc, "*"), |
| 5757 | IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide, |
| 5758 | wrap_binaryfunc, "/"), |
| 5759 | IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder, |
| 5760 | wrap_binaryfunc, "%"), |
| 5761 | IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power, |
Guido van Rossum | 6e5680f | 2002-10-15 01:01:53 +0000 | [diff] [blame] | 5762 | wrap_binaryfunc, "**"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5763 | IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift, |
| 5764 | wrap_binaryfunc, "<<"), |
| 5765 | IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift, |
| 5766 | wrap_binaryfunc, ">>"), |
| 5767 | IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and, |
| 5768 | wrap_binaryfunc, "&"), |
| 5769 | IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor, |
| 5770 | wrap_binaryfunc, "^"), |
| 5771 | IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or, |
| 5772 | wrap_binaryfunc, "|"), |
| 5773 | BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5774 | RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5775 | BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5776 | RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5777 | IBSLOT("__ifloordiv__", nb_inplace_floor_divide, |
| 5778 | slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"), |
| 5779 | IBSLOT("__itruediv__", nb_inplace_true_divide, |
| 5780 | slot_nb_inplace_true_divide, wrap_binaryfunc, "/"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5781 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5782 | TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, |
| 5783 | "x.__str__() <==> str(x)"), |
Guido van Rossum | afe7a94 | 2001-10-29 14:33:44 +0000 | [diff] [blame] | 5784 | TPSLOT("__str__", tp_print, NULL, NULL, ""), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5785 | TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, |
| 5786 | "x.__repr__() <==> repr(x)"), |
Guido van Rossum | afe7a94 | 2001-10-29 14:33:44 +0000 | [diff] [blame] | 5787 | TPSLOT("__repr__", tp_print, NULL, NULL, ""), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5788 | TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc, |
| 5789 | "x.__cmp__(y) <==> cmp(x,y)"), |
| 5790 | TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, |
| 5791 | "x.__hash__() <==> hash(x)"), |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5792 | FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, |
| 5793 | "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS), |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5794 | TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5795 | wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"), |
| 5796 | TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), |
| 5797 | TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), |
| 5798 | TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), |
| 5799 | TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, |
| 5800 | "x.__setattr__('name', value) <==> x.name = value"), |
| 5801 | TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), |
| 5802 | TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, |
| 5803 | "x.__delattr__('name') <==> del x.name"), |
| 5804 | TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), |
| 5805 | TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, |
| 5806 | "x.__lt__(y) <==> x<y"), |
| 5807 | TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, |
| 5808 | "x.__le__(y) <==> x<=y"), |
| 5809 | TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, |
| 5810 | "x.__eq__(y) <==> x==y"), |
| 5811 | TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, |
| 5812 | "x.__ne__(y) <==> x!=y"), |
| 5813 | TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, |
| 5814 | "x.__gt__(y) <==> x>y"), |
| 5815 | TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, |
| 5816 | "x.__ge__(y) <==> x>=y"), |
| 5817 | TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, |
| 5818 | "x.__iter__() <==> iter(x)"), |
| 5819 | TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next, |
| 5820 | "x.next() -> the next value, or raise StopIteration"), |
| 5821 | TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, |
| 5822 | "descr.__get__(obj[, type]) -> value"), |
| 5823 | TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, |
| 5824 | "descr.__set__(obj, value)"), |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 5825 | TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set, |
| 5826 | wrap_descr_delete, "descr.__delete__(obj)"), |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5827 | FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5828 | "x.__init__(...) initializes x; " |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5829 | "see x.__class__.__doc__ for signature", |
| 5830 | PyWrapperFlag_KEYWORDS), |
| 5831 | TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""), |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5832 | TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5833 | {NULL} |
| 5834 | }; |
| 5835 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5836 | /* Given a type pointer and an offset gotten from a slotdef entry, return a |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5837 | pointer to the actual slot. This is not quite the same as simply adding |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5838 | the offset to the type pointer, since it takes care to indirect through the |
| 5839 | proper indirection pointer (as_buffer, etc.); it returns NULL if the |
| 5840 | indirection pointer is NULL. */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5841 | static void ** |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5842 | slotptr(PyTypeObject *type, int ioffset) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5843 | { |
| 5844 | char *ptr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5845 | long offset = ioffset; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5846 | |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5847 | /* Note: this depends on the order of the members of PyHeapTypeObject! */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5848 | assert(offset >= 0); |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 5849 | assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer)); |
| 5850 | if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) { |
Martin v. Löwis | ee36d65 | 2006-04-11 09:08:02 +0000 | [diff] [blame] | 5851 | ptr = (char *)type->tp_as_sequence; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5852 | offset -= offsetof(PyHeapTypeObject, as_sequence); |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5853 | } |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 5854 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) { |
Martin v. Löwis | ee36d65 | 2006-04-11 09:08:02 +0000 | [diff] [blame] | 5855 | ptr = (char *)type->tp_as_mapping; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5856 | offset -= offsetof(PyHeapTypeObject, as_mapping); |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 5857 | } |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 5858 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) { |
Martin v. Löwis | ee36d65 | 2006-04-11 09:08:02 +0000 | [diff] [blame] | 5859 | ptr = (char *)type->tp_as_number; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5860 | offset -= offsetof(PyHeapTypeObject, as_number); |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5861 | } |
| 5862 | else { |
Martin v. Löwis | ee36d65 | 2006-04-11 09:08:02 +0000 | [diff] [blame] | 5863 | ptr = (char *)type; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5864 | } |
| 5865 | if (ptr != NULL) |
| 5866 | ptr += offset; |
| 5867 | return (void **)ptr; |
| 5868 | } |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 5869 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5870 | /* Length of array of slotdef pointers used to store slots with the |
| 5871 | same __name__. There should be at most MAX_EQUIV-1 slotdef entries with |
| 5872 | the same __name__, for any __name__. Since that's a static property, it is |
| 5873 | appropriate to declare fixed-size arrays for this. */ |
| 5874 | #define MAX_EQUIV 10 |
| 5875 | |
| 5876 | /* Return a slot pointer for a given name, but ONLY if the attribute has |
| 5877 | exactly one slot function. The name must be an interned string. */ |
| 5878 | static void ** |
| 5879 | resolve_slotdups(PyTypeObject *type, PyObject *name) |
| 5880 | { |
| 5881 | /* XXX Maybe this could be optimized more -- but is it worth it? */ |
| 5882 | |
| 5883 | /* pname and ptrs act as a little cache */ |
| 5884 | static PyObject *pname; |
| 5885 | static slotdef *ptrs[MAX_EQUIV]; |
| 5886 | slotdef *p, **pp; |
| 5887 | void **res, **ptr; |
| 5888 | |
| 5889 | if (pname != name) { |
| 5890 | /* Collect all slotdefs that match name into ptrs. */ |
| 5891 | pname = name; |
| 5892 | pp = ptrs; |
| 5893 | for (p = slotdefs; p->name_strobj; p++) { |
| 5894 | if (p->name_strobj == name) |
| 5895 | *pp++ = p; |
| 5896 | } |
| 5897 | *pp = NULL; |
| 5898 | } |
| 5899 | |
| 5900 | /* Look in all matching slots of the type; if exactly one of these has |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5901 | a filled-in slot, return its value. Otherwise return NULL. */ |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5902 | res = NULL; |
| 5903 | for (pp = ptrs; *pp; pp++) { |
| 5904 | ptr = slotptr(type, (*pp)->offset); |
| 5905 | if (ptr == NULL || *ptr == NULL) |
| 5906 | continue; |
| 5907 | if (res != NULL) |
| 5908 | return NULL; |
| 5909 | res = ptr; |
| 5910 | } |
| 5911 | return res; |
| 5912 | } |
| 5913 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5914 | /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5915 | does some incredibly complex thinking and then sticks something into the |
| 5916 | slot. (It sees if the adjacent slotdefs for the same slot have conflicting |
| 5917 | interests, and then stores a generic wrapper or a specific function into |
| 5918 | the slot.) Return a pointer to the next slotdef with a different offset, |
| 5919 | because that's convenient for fixup_slot_dispatchers(). */ |
| 5920 | static slotdef * |
| 5921 | update_one_slot(PyTypeObject *type, slotdef *p) |
| 5922 | { |
| 5923 | PyObject *descr; |
| 5924 | PyWrapperDescrObject *d; |
| 5925 | void *generic = NULL, *specific = NULL; |
| 5926 | int use_generic = 0; |
| 5927 | int offset = p->offset; |
| 5928 | void **ptr = slotptr(type, offset); |
| 5929 | |
| 5930 | if (ptr == NULL) { |
| 5931 | do { |
| 5932 | ++p; |
| 5933 | } while (p->offset == offset); |
| 5934 | return p; |
| 5935 | } |
| 5936 | do { |
| 5937 | descr = _PyType_Lookup(type, p->name_strobj); |
| 5938 | if (descr == NULL) |
| 5939 | continue; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5940 | if (Py_TYPE(descr) == &PyWrapperDescr_Type) { |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5941 | void **tptr = resolve_slotdups(type, p->name_strobj); |
| 5942 | if (tptr == NULL || tptr == ptr) |
| 5943 | generic = p->function; |
| 5944 | d = (PyWrapperDescrObject *)descr; |
| 5945 | if (d->d_base->wrapper == p->wrapper && |
| 5946 | PyType_IsSubtype(type, d->d_type)) |
| 5947 | { |
| 5948 | if (specific == NULL || |
| 5949 | specific == d->d_wrapped) |
| 5950 | specific = d->d_wrapped; |
| 5951 | else |
| 5952 | use_generic = 1; |
| 5953 | } |
| 5954 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5955 | else if (Py_TYPE(descr) == &PyCFunction_Type && |
Guido van Rossum | 721f62e | 2002-08-09 02:14:34 +0000 | [diff] [blame] | 5956 | PyCFunction_GET_FUNCTION(descr) == |
| 5957 | (PyCFunction)tp_new_wrapper && |
| 5958 | strcmp(p->name, "__new__") == 0) |
| 5959 | { |
| 5960 | /* The __new__ wrapper is not a wrapper descriptor, |
| 5961 | so must be special-cased differently. |
| 5962 | If we don't do this, creating an instance will |
| 5963 | always use slot_tp_new which will look up |
| 5964 | __new__ in the MRO which will call tp_new_wrapper |
| 5965 | which will look through the base classes looking |
| 5966 | for a static base and call its tp_new (usually |
| 5967 | PyType_GenericNew), after performing various |
| 5968 | sanity checks and constructing a new argument |
| 5969 | list. Cut all that nonsense short -- this speeds |
| 5970 | up instance creation tremendously. */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5971 | specific = (void *)type->tp_new; |
Guido van Rossum | 721f62e | 2002-08-09 02:14:34 +0000 | [diff] [blame] | 5972 | /* XXX I'm not 100% sure that there isn't a hole |
| 5973 | in this reasoning that requires additional |
| 5974 | sanity checks. I'll buy the first person to |
| 5975 | point out a bug in this reasoning a beer. */ |
| 5976 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5977 | else { |
| 5978 | use_generic = 1; |
| 5979 | generic = p->function; |
| 5980 | } |
| 5981 | } while ((++p)->offset == offset); |
| 5982 | if (specific && !use_generic) |
| 5983 | *ptr = specific; |
| 5984 | else |
| 5985 | *ptr = generic; |
| 5986 | return p; |
| 5987 | } |
| 5988 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5989 | /* In the type, update the slots whose slotdefs are gathered in the pp array. |
| 5990 | This is a callback for update_subclasses(). */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5991 | static int |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5992 | update_slots_callback(PyTypeObject *type, void *data) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5993 | { |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5994 | slotdef **pp = (slotdef **)data; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5995 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5996 | for (; *pp; pp++) |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5997 | update_one_slot(type, *pp); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5998 | return 0; |
| 5999 | } |
| 6000 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6001 | /* Comparison function for qsort() to compare slotdefs by their offset, and |
| 6002 | for equal offset by their address (to force a stable sort). */ |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6003 | static int |
| 6004 | slotdef_cmp(const void *aa, const void *bb) |
| 6005 | { |
| 6006 | const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb; |
| 6007 | int c = a->offset - b->offset; |
| 6008 | if (c != 0) |
| 6009 | return c; |
| 6010 | else |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6011 | /* Cannot use a-b, as this gives off_t, |
| 6012 | which may lose precision when converted to int. */ |
| 6013 | return (a > b) ? 1 : (a < b) ? -1 : 0; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6014 | } |
| 6015 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6016 | /* Initialize the slotdefs table by adding interned string objects for the |
| 6017 | names and sorting the entries. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6018 | static void |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6019 | init_slotdefs(void) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6020 | { |
| 6021 | slotdef *p; |
| 6022 | static int initialized = 0; |
| 6023 | |
| 6024 | if (initialized) |
| 6025 | return; |
| 6026 | for (p = slotdefs; p->name; p++) { |
| 6027 | p->name_strobj = PyString_InternFromString(p->name); |
| 6028 | if (!p->name_strobj) |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6029 | Py_FatalError("Out of memory interning slotdef names"); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6030 | } |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 6031 | qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef), |
| 6032 | slotdef_cmp); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6033 | initialized = 1; |
| 6034 | } |
| 6035 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6036 | /* Update the slots after assignment to a class (type) attribute. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6037 | static int |
| 6038 | update_slot(PyTypeObject *type, PyObject *name) |
| 6039 | { |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6040 | slotdef *ptrs[MAX_EQUIV]; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6041 | slotdef *p; |
| 6042 | slotdef **pp; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 6043 | int offset; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6044 | |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 6045 | /* Clear the VALID_VERSION flag of 'type' and all its |
| 6046 | subclasses. This could possibly be unified with the |
| 6047 | update_subclasses() recursion below, but carefully: |
| 6048 | they each have their own conditions on which to stop |
| 6049 | recursing into subclasses. */ |
| 6050 | type_modified(type); |
| 6051 | |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 6052 | init_slotdefs(); |
| 6053 | pp = ptrs; |
| 6054 | for (p = slotdefs; p->name; p++) { |
| 6055 | /* XXX assume name is interned! */ |
| 6056 | if (p->name_strobj == name) |
| 6057 | *pp++ = p; |
| 6058 | } |
| 6059 | *pp = NULL; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6060 | for (pp = ptrs; *pp; pp++) { |
| 6061 | p = *pp; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 6062 | offset = p->offset; |
| 6063 | while (p > slotdefs && (p-1)->offset == offset) |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6064 | --p; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 6065 | *pp = p; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6066 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6067 | if (ptrs[0] == NULL) |
| 6068 | return 0; /* Not an attribute that affects any slots */ |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6069 | return update_subclasses(type, name, |
| 6070 | update_slots_callback, (void *)ptrs); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6071 | } |
| 6072 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6073 | /* Store the proper functions in the slot dispatches at class (type) |
| 6074 | definition time, based upon which operations the class overrides in its |
| 6075 | dict. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6076 | static void |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6077 | fixup_slot_dispatchers(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6078 | { |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6079 | slotdef *p; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6080 | |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6081 | init_slotdefs(); |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6082 | for (p = slotdefs; p->name; ) |
| 6083 | p = update_one_slot(type, p); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6084 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6085 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 6086 | static void |
| 6087 | update_all_slots(PyTypeObject* type) |
| 6088 | { |
| 6089 | slotdef *p; |
| 6090 | |
| 6091 | init_slotdefs(); |
| 6092 | for (p = slotdefs; p->name; p++) { |
| 6093 | /* update_slot returns int but can't actually fail */ |
| 6094 | update_slot(type, p->name_strobj); |
| 6095 | } |
| 6096 | } |
| 6097 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6098 | /* recurse_down_subclasses() and update_subclasses() are mutually |
| 6099 | recursive functions to call a callback for all subclasses, |
| 6100 | but refraining from recursing into subclasses that define 'name'. */ |
| 6101 | |
| 6102 | static int |
| 6103 | update_subclasses(PyTypeObject *type, PyObject *name, |
| 6104 | update_callback callback, void *data) |
| 6105 | { |
| 6106 | if (callback(type, data) < 0) |
| 6107 | return -1; |
| 6108 | return recurse_down_subclasses(type, name, callback, data); |
| 6109 | } |
| 6110 | |
| 6111 | static int |
| 6112 | recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
| 6113 | update_callback callback, void *data) |
| 6114 | { |
| 6115 | PyTypeObject *subclass; |
| 6116 | PyObject *ref, *subclasses, *dict; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6117 | Py_ssize_t i, n; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6118 | |
| 6119 | subclasses = type->tp_subclasses; |
| 6120 | if (subclasses == NULL) |
| 6121 | return 0; |
| 6122 | assert(PyList_Check(subclasses)); |
| 6123 | n = PyList_GET_SIZE(subclasses); |
| 6124 | for (i = 0; i < n; i++) { |
| 6125 | ref = PyList_GET_ITEM(subclasses, i); |
| 6126 | assert(PyWeakref_CheckRef(ref)); |
| 6127 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 6128 | assert(subclass != NULL); |
| 6129 | if ((PyObject *)subclass == Py_None) |
| 6130 | continue; |
| 6131 | assert(PyType_Check(subclass)); |
| 6132 | /* Avoid recursing down into unaffected classes */ |
| 6133 | dict = subclass->tp_dict; |
| 6134 | if (dict != NULL && PyDict_Check(dict) && |
| 6135 | PyDict_GetItem(dict, name) != NULL) |
| 6136 | continue; |
| 6137 | if (update_subclasses(subclass, name, callback, data) < 0) |
| 6138 | return -1; |
| 6139 | } |
| 6140 | return 0; |
| 6141 | } |
| 6142 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6143 | /* This function is called by PyType_Ready() to populate the type's |
| 6144 | dictionary with method descriptors for function slots. For each |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6145 | function slot (like tp_repr) that's defined in the type, one or more |
| 6146 | corresponding descriptors are added in the type's tp_dict dictionary |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6147 | under the appropriate name (like __repr__). Some function slots |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6148 | cause more than one descriptor to be added (for example, the nb_add |
| 6149 | slot adds both __add__ and __radd__ descriptors) and some function |
| 6150 | slots compete for the same descriptor (for example both sq_item and |
| 6151 | mp_subscript generate a __getitem__ descriptor). |
| 6152 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6153 | In the latter case, the first slotdef entry encoutered wins. Since |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 6154 | slotdef entries are sorted by the offset of the slot in the |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 6155 | PyHeapTypeObject, this gives us some control over disambiguating |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6156 | between competing slots: the members of PyHeapTypeObject are listed |
| 6157 | from most general to least general, so the most general slot is |
| 6158 | preferred. In particular, because as_mapping comes before as_sequence, |
| 6159 | for a type that defines both mp_subscript and sq_item, mp_subscript |
| 6160 | wins. |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6161 | |
| 6162 | This only adds new descriptors and doesn't overwrite entries in |
| 6163 | tp_dict that were previously defined. The descriptors contain a |
| 6164 | reference to the C function they must call, so that it's safe if they |
| 6165 | are copied into a subtype's __dict__ and the subtype has a different |
| 6166 | C function in its slot -- calling the method defined by the |
| 6167 | descriptor will call the C function that was used to create it, |
| 6168 | rather than the C function present in the slot when it is called. |
| 6169 | (This is important because a subtype may have a C function in the |
| 6170 | slot that calls the method from the dictionary, and we want to avoid |
| 6171 | infinite recursion here.) */ |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6172 | |
| 6173 | static int |
| 6174 | add_operators(PyTypeObject *type) |
| 6175 | { |
| 6176 | PyObject *dict = type->tp_dict; |
| 6177 | slotdef *p; |
| 6178 | PyObject *descr; |
| 6179 | void **ptr; |
| 6180 | |
| 6181 | init_slotdefs(); |
| 6182 | for (p = slotdefs; p->name; p++) { |
| 6183 | if (p->wrapper == NULL) |
| 6184 | continue; |
| 6185 | ptr = slotptr(type, p->offset); |
| 6186 | if (!ptr || !*ptr) |
| 6187 | continue; |
| 6188 | if (PyDict_GetItem(dict, p->name_strobj)) |
| 6189 | continue; |
| 6190 | descr = PyDescr_NewWrapper(type, p, *ptr); |
| 6191 | if (descr == NULL) |
| 6192 | return -1; |
| 6193 | if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) |
| 6194 | return -1; |
| 6195 | Py_DECREF(descr); |
| 6196 | } |
| 6197 | if (type->tp_new != NULL) { |
| 6198 | if (add_tp_new_wrapper(type) < 0) |
| 6199 | return -1; |
| 6200 | } |
| 6201 | return 0; |
| 6202 | } |
| 6203 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6204 | |
| 6205 | /* Cooperative 'super' */ |
| 6206 | |
| 6207 | typedef struct { |
| 6208 | PyObject_HEAD |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6209 | PyTypeObject *type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6210 | PyObject *obj; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6211 | PyTypeObject *obj_type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6212 | } superobject; |
| 6213 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 6214 | static PyMemberDef super_members[] = { |
| 6215 | {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY, |
| 6216 | "the class invoking super()"}, |
| 6217 | {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY, |
| 6218 | "the instance invoking super(); may be None"}, |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6219 | {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY, |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 6220 | "the type of the instance invoking super(); may be None"}, |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6221 | {0} |
| 6222 | }; |
| 6223 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6224 | static void |
| 6225 | super_dealloc(PyObject *self) |
| 6226 | { |
| 6227 | superobject *su = (superobject *)self; |
| 6228 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6229 | _PyObject_GC_UNTRACK(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6230 | Py_XDECREF(su->obj); |
| 6231 | Py_XDECREF(su->type); |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6232 | Py_XDECREF(su->obj_type); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6233 | Py_TYPE(self)->tp_free(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6234 | } |
| 6235 | |
| 6236 | static PyObject * |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6237 | super_repr(PyObject *self) |
| 6238 | { |
| 6239 | superobject *su = (superobject *)self; |
| 6240 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6241 | if (su->obj_type) |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6242 | return PyString_FromFormat( |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 6243 | "<super: <class '%s'>, <%s object>>", |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6244 | su->type ? su->type->tp_name : "NULL", |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6245 | su->obj_type->tp_name); |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6246 | else |
| 6247 | return PyString_FromFormat( |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 6248 | "<super: <class '%s'>, NULL>", |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6249 | su->type ? su->type->tp_name : "NULL"); |
| 6250 | } |
| 6251 | |
| 6252 | static PyObject * |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6253 | super_getattro(PyObject *self, PyObject *name) |
| 6254 | { |
| 6255 | superobject *su = (superobject *)self; |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 6256 | int skip = su->obj_type == NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6257 | |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 6258 | if (!skip) { |
| 6259 | /* We want __class__ to return the class of the super object |
| 6260 | (i.e. super, or a subclass), not the class of su->obj. */ |
| 6261 | skip = (PyString_Check(name) && |
| 6262 | PyString_GET_SIZE(name) == 9 && |
| 6263 | strcmp(PyString_AS_STRING(name), "__class__") == 0); |
| 6264 | } |
| 6265 | |
| 6266 | if (!skip) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 6267 | PyObject *mro, *res, *tmp, *dict; |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 6268 | PyTypeObject *starttype; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6269 | descrgetfunc f; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6270 | Py_ssize_t i, n; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6271 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6272 | starttype = su->obj_type; |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 6273 | mro = starttype->tp_mro; |
| 6274 | |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6275 | if (mro == NULL) |
| 6276 | n = 0; |
| 6277 | else { |
| 6278 | assert(PyTuple_Check(mro)); |
| 6279 | n = PyTuple_GET_SIZE(mro); |
| 6280 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6281 | for (i = 0; i < n; i++) { |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6282 | if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6283 | break; |
| 6284 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6285 | i++; |
| 6286 | res = NULL; |
| 6287 | for (; i < n; i++) { |
| 6288 | tmp = PyTuple_GET_ITEM(mro, i); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 6289 | if (PyType_Check(tmp)) |
| 6290 | dict = ((PyTypeObject *)tmp)->tp_dict; |
| 6291 | else if (PyClass_Check(tmp)) |
| 6292 | dict = ((PyClassObject *)tmp)->cl_dict; |
| 6293 | else |
| 6294 | continue; |
| 6295 | res = PyDict_GetItem(dict, name); |
Guido van Rossum | 6cc5bb6 | 2003-04-16 20:01:36 +0000 | [diff] [blame] | 6296 | if (res != NULL) { |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6297 | Py_INCREF(res); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6298 | f = Py_TYPE(res)->tp_descr_get; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6299 | if (f != NULL) { |
Phillip J. Eby | 91a968a | 2004-03-25 02:19:34 +0000 | [diff] [blame] | 6300 | tmp = f(res, |
| 6301 | /* Only pass 'obj' param if |
| 6302 | this is instance-mode super |
| 6303 | (See SF ID #743627) |
| 6304 | */ |
Hye-Shik Chang | ff365c9 | 2004-03-25 16:37:03 +0000 | [diff] [blame] | 6305 | (su->obj == (PyObject *) |
| 6306 | su->obj_type |
Phillip J. Eby | 91a968a | 2004-03-25 02:19:34 +0000 | [diff] [blame] | 6307 | ? (PyObject *)NULL |
| 6308 | : su->obj), |
Guido van Rossum | d464107 | 2002-04-03 02:13:37 +0000 | [diff] [blame] | 6309 | (PyObject *)starttype); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6310 | Py_DECREF(res); |
| 6311 | res = tmp; |
| 6312 | } |
| 6313 | return res; |
| 6314 | } |
| 6315 | } |
| 6316 | } |
| 6317 | return PyObject_GenericGetAttr(self, name); |
| 6318 | } |
| 6319 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6320 | static PyTypeObject * |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6321 | supercheck(PyTypeObject *type, PyObject *obj) |
| 6322 | { |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6323 | /* Check that a super() call makes sense. Return a type object. |
| 6324 | |
| 6325 | obj can be a new-style class, or an instance of one: |
| 6326 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6327 | - If it is a class, it must be a subclass of 'type'. This case is |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6328 | used for class methods; the return value is obj. |
| 6329 | |
| 6330 | - If it is an instance, it must be an instance of 'type'. This is |
| 6331 | the normal case; the return value is obj.__class__. |
| 6332 | |
| 6333 | But... when obj is an instance, we want to allow for the case where |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6334 | Py_TYPE(obj) is not a subclass of type, but obj.__class__ is! |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6335 | This will allow using super() with a proxy for obj. |
| 6336 | */ |
| 6337 | |
Guido van Rossum | 8e80a72 | 2003-02-18 19:22:22 +0000 | [diff] [blame] | 6338 | /* Check for first bullet above (special case) */ |
| 6339 | if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) { |
| 6340 | Py_INCREF(obj); |
| 6341 | return (PyTypeObject *)obj; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6342 | } |
Guido van Rossum | 8e80a72 | 2003-02-18 19:22:22 +0000 | [diff] [blame] | 6343 | |
| 6344 | /* Normal case */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6345 | if (PyType_IsSubtype(Py_TYPE(obj), type)) { |
| 6346 | Py_INCREF(Py_TYPE(obj)); |
| 6347 | return Py_TYPE(obj); |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6348 | } |
| 6349 | else { |
| 6350 | /* Try the slow way */ |
| 6351 | static PyObject *class_str = NULL; |
| 6352 | PyObject *class_attr; |
| 6353 | |
| 6354 | if (class_str == NULL) { |
| 6355 | class_str = PyString_FromString("__class__"); |
| 6356 | if (class_str == NULL) |
| 6357 | return NULL; |
| 6358 | } |
| 6359 | |
| 6360 | class_attr = PyObject_GetAttr(obj, class_str); |
| 6361 | |
| 6362 | if (class_attr != NULL && |
| 6363 | PyType_Check(class_attr) && |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6364 | (PyTypeObject *)class_attr != Py_TYPE(obj)) |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6365 | { |
| 6366 | int ok = PyType_IsSubtype( |
| 6367 | (PyTypeObject *)class_attr, type); |
| 6368 | if (ok) |
| 6369 | return (PyTypeObject *)class_attr; |
| 6370 | } |
| 6371 | |
| 6372 | if (class_attr == NULL) |
| 6373 | PyErr_Clear(); |
| 6374 | else |
| 6375 | Py_DECREF(class_attr); |
| 6376 | } |
| 6377 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6378 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6379 | "super(type, obj): " |
| 6380 | "obj must be an instance or subtype of type"); |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6381 | return NULL; |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6382 | } |
| 6383 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6384 | static PyObject * |
| 6385 | super_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 6386 | { |
| 6387 | superobject *su = (superobject *)self; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 6388 | superobject *newobj; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6389 | |
| 6390 | if (obj == NULL || obj == Py_None || su->obj != NULL) { |
| 6391 | /* Not binding to an object, or already bound */ |
| 6392 | Py_INCREF(self); |
| 6393 | return self; |
| 6394 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6395 | if (Py_TYPE(su) != &PySuper_Type) |
Armin Rigo | 7726dc0 | 2005-05-15 15:32:08 +0000 | [diff] [blame] | 6396 | /* If su is an instance of a (strict) subclass of super, |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6397 | call its type */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6398 | return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su), |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6399 | su->type, obj, NULL); |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6400 | else { |
| 6401 | /* Inline the common case */ |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6402 | PyTypeObject *obj_type = supercheck(su->type, obj); |
| 6403 | if (obj_type == NULL) |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6404 | return NULL; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 6405 | newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type, |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6406 | NULL, NULL); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 6407 | if (newobj == NULL) |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6408 | return NULL; |
| 6409 | Py_INCREF(su->type); |
| 6410 | Py_INCREF(obj); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 6411 | newobj->type = su->type; |
| 6412 | newobj->obj = obj; |
| 6413 | newobj->obj_type = obj_type; |
| 6414 | return (PyObject *)newobj; |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6415 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6416 | } |
| 6417 | |
| 6418 | static int |
| 6419 | super_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 6420 | { |
| 6421 | superobject *su = (superobject *)self; |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6422 | PyTypeObject *type; |
| 6423 | PyObject *obj = NULL; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6424 | PyTypeObject *obj_type = NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6425 | |
Georg Brandl | 5d59c09 | 2006-09-30 08:43:30 +0000 | [diff] [blame] | 6426 | if (!_PyArg_NoKeywords("super", kwds)) |
| 6427 | return -1; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6428 | if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj)) |
| 6429 | return -1; |
| 6430 | if (obj == Py_None) |
| 6431 | obj = NULL; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6432 | if (obj != NULL) { |
| 6433 | obj_type = supercheck(type, obj); |
| 6434 | if (obj_type == NULL) |
| 6435 | return -1; |
| 6436 | Py_INCREF(obj); |
| 6437 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6438 | Py_INCREF(type); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6439 | su->type = type; |
| 6440 | su->obj = obj; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6441 | su->obj_type = obj_type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6442 | return 0; |
| 6443 | } |
| 6444 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6445 | PyDoc_STRVAR(super_doc, |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6446 | "super(type) -> unbound super object\n" |
| 6447 | "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6448 | "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6449 | "Typical use to call a cooperative superclass method:\n" |
| 6450 | "class C(B):\n" |
| 6451 | " def meth(self, arg):\n" |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6452 | " super(C, self).meth(arg)"); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6453 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6454 | static int |
| 6455 | super_traverse(PyObject *self, visitproc visit, void *arg) |
| 6456 | { |
| 6457 | superobject *su = (superobject *)self; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6458 | |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 6459 | Py_VISIT(su->obj); |
| 6460 | Py_VISIT(su->type); |
| 6461 | Py_VISIT(su->obj_type); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6462 | |
| 6463 | return 0; |
| 6464 | } |
| 6465 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6466 | PyTypeObject PySuper_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 6467 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6468 | "super", /* tp_name */ |
| 6469 | sizeof(superobject), /* tp_basicsize */ |
| 6470 | 0, /* tp_itemsize */ |
| 6471 | /* methods */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6472 | super_dealloc, /* tp_dealloc */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6473 | 0, /* tp_print */ |
| 6474 | 0, /* tp_getattr */ |
| 6475 | 0, /* tp_setattr */ |
| 6476 | 0, /* tp_compare */ |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6477 | super_repr, /* tp_repr */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6478 | 0, /* tp_as_number */ |
| 6479 | 0, /* tp_as_sequence */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6480 | 0, /* tp_as_mapping */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6481 | 0, /* tp_hash */ |
| 6482 | 0, /* tp_call */ |
| 6483 | 0, /* tp_str */ |
| 6484 | super_getattro, /* tp_getattro */ |
| 6485 | 0, /* tp_setattro */ |
| 6486 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6487 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 6488 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6489 | super_doc, /* tp_doc */ |
| 6490 | super_traverse, /* tp_traverse */ |
| 6491 | 0, /* tp_clear */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6492 | 0, /* tp_richcompare */ |
| 6493 | 0, /* tp_weaklistoffset */ |
| 6494 | 0, /* tp_iter */ |
| 6495 | 0, /* tp_iternext */ |
| 6496 | 0, /* tp_methods */ |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6497 | super_members, /* tp_members */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6498 | 0, /* tp_getset */ |
| 6499 | 0, /* tp_base */ |
| 6500 | 0, /* tp_dict */ |
| 6501 | super_descr_get, /* tp_descr_get */ |
| 6502 | 0, /* tp_descr_set */ |
| 6503 | 0, /* tp_dictoffset */ |
| 6504 | super_init, /* tp_init */ |
| 6505 | PyType_GenericAlloc, /* tp_alloc */ |
| 6506 | PyType_GenericNew, /* tp_new */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6507 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6508 | }; |