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" |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 4 | #include "frameobject.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 7 | #include <ctype.h> |
| 8 | |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 9 | |
| 10 | /* Support type attribute cache */ |
| 11 | |
| 12 | /* The cache can keep references to the names alive for longer than |
| 13 | they normally would. This is why the maximum size is limited to |
| 14 | MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large |
| 15 | strings are used as attribute names. */ |
| 16 | #define MCACHE_MAX_ATTR_SIZE 100 |
| 17 | #define MCACHE_SIZE_EXP 10 |
| 18 | #define MCACHE_HASH(version, name_hash) \ |
| 19 | (((unsigned int)(version) * (unsigned int)(name_hash)) \ |
| 20 | >> (8*sizeof(unsigned int) - MCACHE_SIZE_EXP)) |
| 21 | #define MCACHE_HASH_METHOD(type, name) \ |
| 22 | MCACHE_HASH((type)->tp_version_tag, \ |
Georg Brandl | 1bcf35a | 2008-05-25 09:32:09 +0000 | [diff] [blame] | 23 | ((PyUnicodeObject *)(name))->hash) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 24 | #define MCACHE_CACHEABLE_NAME(name) \ |
Georg Brandl | 1bcf35a | 2008-05-25 09:32:09 +0000 | [diff] [blame] | 25 | PyUnicode_CheckExact(name) && \ |
| 26 | PyUnicode_GET_SIZE(name) <= MCACHE_MAX_ATTR_SIZE |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 27 | |
| 28 | struct method_cache_entry { |
| 29 | unsigned int version; |
| 30 | PyObject *name; /* reference to exactly a str or None */ |
| 31 | PyObject *value; /* borrowed */ |
| 32 | }; |
| 33 | |
| 34 | static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP]; |
| 35 | static unsigned int next_version_tag = 0; |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 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 */ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 50 | PyType_Modified(&PyBaseObject_Type); |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 51 | return cur_version_tag; |
| 52 | } |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 53 | |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 54 | void |
| 55 | PyType_Modified(PyTypeObject *type) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 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 | |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 79 | if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +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) { |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 89 | PyType_Modified((PyTypeObject *)ref); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 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 | |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 115 | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +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 */ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 175 | PyType_Modified(&PyBaseObject_Type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 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 | ade8c8b2 | 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 | ade8c8b2 | 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++; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 221 | return PyUnicode_FromString(s); |
Michael W. Hudson | ade8c8b2 | 2002-11-27 16:29:26 +0000 | [diff] [blame] | 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; |
Neal Norwitz | 80e7f27 | 2007-08-26 06:45:23 +0000 | [diff] [blame] | 229 | char *tp_name; |
Guido van Rossum | e845c0f | 2007-11-02 23:07:07 +0000 | [diff] [blame] | 230 | PyObject *tmp; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 231 | |
| 232 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 233 | PyErr_Format(PyExc_TypeError, |
| 234 | "can't set %s.__name__", type->tp_name); |
| 235 | return -1; |
| 236 | } |
| 237 | if (!value) { |
| 238 | PyErr_Format(PyExc_TypeError, |
| 239 | "can't delete %s.__name__", type->tp_name); |
| 240 | return -1; |
| 241 | } |
Neal Norwitz | 6ea45d3 | 2007-08-26 04:19:43 +0000 | [diff] [blame] | 242 | if (!PyUnicode_Check(value)) { |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 243 | PyErr_Format(PyExc_TypeError, |
| 244 | "can only assign string to %s.__name__, not '%s'", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 245 | type->tp_name, Py_TYPE(value)->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 246 | return -1; |
| 247 | } |
Guido van Rossum | e845c0f | 2007-11-02 23:07:07 +0000 | [diff] [blame] | 248 | |
| 249 | /* Check absence of null characters */ |
| 250 | tmp = PyUnicode_FromStringAndSize("\0", 1); |
| 251 | if (tmp == NULL) |
Neal Norwitz | 6ea45d3 | 2007-08-26 04:19:43 +0000 | [diff] [blame] | 252 | return -1; |
Guido van Rossum | e845c0f | 2007-11-02 23:07:07 +0000 | [diff] [blame] | 253 | if (PyUnicode_Contains(value, tmp) != 0) { |
| 254 | Py_DECREF(tmp); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 255 | PyErr_Format(PyExc_ValueError, |
| 256 | "__name__ must not contain null bytes"); |
| 257 | return -1; |
| 258 | } |
Guido van Rossum | e845c0f | 2007-11-02 23:07:07 +0000 | [diff] [blame] | 259 | Py_DECREF(tmp); |
| 260 | |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 261 | tp_name = _PyUnicode_AsString(value); |
Guido van Rossum | e845c0f | 2007-11-02 23:07:07 +0000 | [diff] [blame] | 262 | if (tp_name == NULL) |
| 263 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 264 | |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 265 | et = (PyHeapTypeObject*)type; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 266 | |
| 267 | Py_INCREF(value); |
| 268 | |
Georg Brandl | c255c7b | 2006-02-20 22:27:28 +0000 | [diff] [blame] | 269 | Py_DECREF(et->ht_name); |
| 270 | et->ht_name = value; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 271 | |
Neal Norwitz | 80e7f27 | 2007-08-26 06:45:23 +0000 | [diff] [blame] | 272 | type->tp_name = tp_name; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 277 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 278 | type_module(PyTypeObject *type, void *context) |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 279 | { |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 280 | PyObject *mod; |
| 281 | char *s; |
| 282 | |
Michael W. Hudson | ade8c8b2 | 2002-11-27 16:29:26 +0000 | [diff] [blame] | 283 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 284 | mod = PyDict_GetItemString(type->tp_dict, "__module__"); |
Anthony Baxter | 3ecdb25 | 2004-06-11 14:41:18 +0000 | [diff] [blame] | 285 | if (!mod) { |
| 286 | PyErr_Format(PyExc_AttributeError, "__module__"); |
| 287 | return 0; |
| 288 | } |
Michael W. Hudson | ade8c8b2 | 2002-11-27 16:29:26 +0000 | [diff] [blame] | 289 | Py_XINCREF(mod); |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 290 | return mod; |
| 291 | } |
Michael W. Hudson | ade8c8b2 | 2002-11-27 16:29:26 +0000 | [diff] [blame] | 292 | else { |
| 293 | s = strrchr(type->tp_name, '.'); |
| 294 | if (s != NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 295 | return PyUnicode_FromStringAndSize( |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 296 | type->tp_name, (Py_ssize_t)(s - type->tp_name)); |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 297 | return PyUnicode_FromString("builtins"); |
Michael W. Hudson | ade8c8b2 | 2002-11-27 16:29:26 +0000 | [diff] [blame] | 298 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 301 | static int |
| 302 | type_set_module(PyTypeObject *type, PyObject *value, void *context) |
| 303 | { |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 304 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 305 | PyErr_Format(PyExc_TypeError, |
| 306 | "can't set %s.__module__", type->tp_name); |
| 307 | return -1; |
| 308 | } |
| 309 | if (!value) { |
| 310 | PyErr_Format(PyExc_TypeError, |
| 311 | "can't delete %s.__module__", type->tp_name); |
| 312 | return -1; |
| 313 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 314 | |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 315 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 316 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 317 | return PyDict_SetItemString(type->tp_dict, "__module__", value); |
| 318 | } |
| 319 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 320 | static PyObject * |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 321 | type_abstractmethods(PyTypeObject *type, void *context) |
| 322 | { |
| 323 | PyObject *mod = PyDict_GetItemString(type->tp_dict, |
| 324 | "__abstractmethods__"); |
| 325 | if (!mod) { |
| 326 | PyErr_Format(PyExc_AttributeError, "__abstractmethods__"); |
| 327 | return NULL; |
| 328 | } |
| 329 | Py_XINCREF(mod); |
| 330 | return mod; |
| 331 | } |
| 332 | |
| 333 | static int |
| 334 | type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context) |
| 335 | { |
| 336 | /* __abstractmethods__ should only be set once on a type, in |
| 337 | abc.ABCMeta.__new__, so this function doesn't do anything |
| 338 | special to update subclasses. |
| 339 | */ |
| 340 | int res = PyDict_SetItemString(type->tp_dict, |
| 341 | "__abstractmethods__", value); |
| 342 | if (res == 0) { |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 343 | PyType_Modified(type); |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 344 | if (value && PyObject_IsTrue(value)) { |
| 345 | type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT; |
| 346 | } |
| 347 | else { |
| 348 | type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT; |
| 349 | } |
| 350 | } |
| 351 | return res; |
| 352 | } |
| 353 | |
| 354 | static PyObject * |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 355 | type_get_bases(PyTypeObject *type, void *context) |
| 356 | { |
| 357 | Py_INCREF(type->tp_bases); |
| 358 | return type->tp_bases; |
| 359 | } |
| 360 | |
| 361 | static PyTypeObject *best_base(PyObject *); |
| 362 | static int mro_internal(PyTypeObject *); |
| 363 | static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *); |
| 364 | static int add_subclass(PyTypeObject*, PyTypeObject*); |
| 365 | static void remove_subclass(PyTypeObject *, PyTypeObject *); |
| 366 | static void update_all_slots(PyTypeObject *); |
| 367 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 368 | typedef int (*update_callback)(PyTypeObject *, void *); |
| 369 | static int update_subclasses(PyTypeObject *type, PyObject *name, |
| 370 | update_callback callback, void *data); |
| 371 | static int recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
| 372 | update_callback callback, void *data); |
| 373 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 374 | static int |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 375 | mro_subclasses(PyTypeObject *type, PyObject* temp) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 376 | { |
| 377 | PyTypeObject *subclass; |
| 378 | PyObject *ref, *subclasses, *old_mro; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 379 | Py_ssize_t i, n; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 380 | |
| 381 | subclasses = type->tp_subclasses; |
| 382 | if (subclasses == NULL) |
| 383 | return 0; |
| 384 | assert(PyList_Check(subclasses)); |
| 385 | n = PyList_GET_SIZE(subclasses); |
| 386 | for (i = 0; i < n; i++) { |
| 387 | ref = PyList_GET_ITEM(subclasses, i); |
| 388 | assert(PyWeakref_CheckRef(ref)); |
| 389 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 390 | assert(subclass != NULL); |
| 391 | if ((PyObject *)subclass == Py_None) |
| 392 | continue; |
| 393 | assert(PyType_Check(subclass)); |
| 394 | old_mro = subclass->tp_mro; |
| 395 | if (mro_internal(subclass) < 0) { |
| 396 | subclass->tp_mro = old_mro; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 397 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 398 | } |
| 399 | else { |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 400 | PyObject* tuple; |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 401 | tuple = PyTuple_Pack(2, subclass, old_mro); |
Guido van Rossum | 19a02ba | 2003-04-15 22:09:45 +0000 | [diff] [blame] | 402 | Py_DECREF(old_mro); |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 403 | if (!tuple) |
| 404 | return -1; |
| 405 | if (PyList_Append(temp, tuple) < 0) |
| 406 | return -1; |
Guido van Rossum | 19a02ba | 2003-04-15 22:09:45 +0000 | [diff] [blame] | 407 | Py_DECREF(tuple); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 408 | } |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 409 | if (mro_subclasses(subclass, temp) < 0) |
| 410 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 411 | } |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 412 | return 0; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | static int |
| 416 | type_set_bases(PyTypeObject *type, PyObject *value, void *context) |
| 417 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 418 | Py_ssize_t i; |
| 419 | int r = 0; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 420 | PyObject *ob, *temp; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 421 | PyTypeObject *new_base, *old_base; |
| 422 | PyObject *old_bases, *old_mro; |
| 423 | |
| 424 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 425 | PyErr_Format(PyExc_TypeError, |
| 426 | "can't set %s.__bases__", type->tp_name); |
| 427 | return -1; |
| 428 | } |
| 429 | if (!value) { |
| 430 | PyErr_Format(PyExc_TypeError, |
| 431 | "can't delete %s.__bases__", type->tp_name); |
| 432 | return -1; |
| 433 | } |
| 434 | if (!PyTuple_Check(value)) { |
| 435 | PyErr_Format(PyExc_TypeError, |
| 436 | "can only assign tuple to %s.__bases__, not %s", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 437 | type->tp_name, Py_TYPE(value)->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 438 | return -1; |
| 439 | } |
Guido van Rossum | 3bbc0ee | 2002-12-13 17:49:38 +0000 | [diff] [blame] | 440 | if (PyTuple_GET_SIZE(value) == 0) { |
| 441 | PyErr_Format(PyExc_TypeError, |
| 442 | "can only assign non-empty tuple to %s.__bases__, not ()", |
| 443 | type->tp_name); |
| 444 | return -1; |
| 445 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 446 | for (i = 0; i < PyTuple_GET_SIZE(value); i++) { |
| 447 | ob = PyTuple_GET_ITEM(value, i); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 448 | if (!PyType_Check(ob)) { |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 449 | PyErr_Format( |
| 450 | PyExc_TypeError, |
| 451 | "%s.__bases__ must be tuple of old- or new-style classes, not '%s'", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 452 | type->tp_name, Py_TYPE(ob)->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 453 | return -1; |
| 454 | } |
Michael W. Hudson | caf17be | 2002-11-27 10:24:44 +0000 | [diff] [blame] | 455 | if (PyType_Check(ob)) { |
| 456 | if (PyType_IsSubtype((PyTypeObject*)ob, type)) { |
| 457 | PyErr_SetString(PyExc_TypeError, |
| 458 | "a __bases__ item causes an inheritance cycle"); |
| 459 | return -1; |
| 460 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
| 464 | new_base = best_base(value); |
| 465 | |
| 466 | if (!new_base) { |
| 467 | return -1; |
| 468 | } |
| 469 | |
| 470 | if (!compatible_for_assignment(type->tp_base, new_base, "__bases__")) |
| 471 | return -1; |
| 472 | |
| 473 | Py_INCREF(new_base); |
| 474 | Py_INCREF(value); |
| 475 | |
| 476 | old_bases = type->tp_bases; |
| 477 | old_base = type->tp_base; |
| 478 | old_mro = type->tp_mro; |
| 479 | |
| 480 | type->tp_bases = value; |
| 481 | type->tp_base = new_base; |
| 482 | |
| 483 | if (mro_internal(type) < 0) { |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 484 | goto bail; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 487 | temp = PyList_New(0); |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 488 | if (!temp) |
| 489 | goto bail; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 490 | |
| 491 | r = mro_subclasses(type, temp); |
| 492 | |
| 493 | if (r < 0) { |
| 494 | for (i = 0; i < PyList_Size(temp); i++) { |
| 495 | PyTypeObject* cls; |
| 496 | PyObject* mro; |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 497 | PyArg_UnpackTuple(PyList_GET_ITEM(temp, i), |
| 498 | "", 2, 2, &cls, &mro); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 499 | Py_INCREF(mro); |
| 500 | ob = cls->tp_mro; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 501 | cls->tp_mro = mro; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 502 | Py_DECREF(ob); |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 503 | } |
| 504 | Py_DECREF(temp); |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 505 | goto bail; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | Py_DECREF(temp); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 509 | |
| 510 | /* any base that was in __bases__ but now isn't, we |
Raymond Hettinger | a828586 | 2002-12-14 17:17:56 +0000 | [diff] [blame] | 511 | need to remove |type| from its tp_subclasses. |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 512 | conversely, any class now in __bases__ that wasn't |
Raymond Hettinger | a828586 | 2002-12-14 17:17:56 +0000 | [diff] [blame] | 513 | needs to have |type| added to its subclasses. */ |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 514 | |
| 515 | /* for now, sod that: just remove from all old_bases, |
| 516 | add to all new_bases */ |
| 517 | |
| 518 | for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) { |
| 519 | ob = PyTuple_GET_ITEM(old_bases, i); |
| 520 | if (PyType_Check(ob)) { |
| 521 | remove_subclass( |
| 522 | (PyTypeObject*)ob, type); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) { |
| 527 | ob = PyTuple_GET_ITEM(value, i); |
| 528 | if (PyType_Check(ob)) { |
| 529 | if (add_subclass((PyTypeObject*)ob, type) < 0) |
| 530 | r = -1; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | update_all_slots(type); |
| 535 | |
| 536 | Py_DECREF(old_bases); |
| 537 | Py_DECREF(old_base); |
| 538 | Py_DECREF(old_mro); |
| 539 | |
| 540 | return r; |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 541 | |
| 542 | bail: |
Michael W. Hudson | e723e45 | 2003-08-07 14:58:10 +0000 | [diff] [blame] | 543 | Py_DECREF(type->tp_bases); |
| 544 | Py_DECREF(type->tp_base); |
| 545 | if (type->tp_mro != old_mro) { |
| 546 | Py_DECREF(type->tp_mro); |
| 547 | } |
| 548 | |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 549 | type->tp_bases = old_bases; |
| 550 | type->tp_base = old_base; |
| 551 | type->tp_mro = old_mro; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 552 | |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 553 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 557 | type_dict(PyTypeObject *type, void *context) |
| 558 | { |
| 559 | if (type->tp_dict == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 560 | Py_INCREF(Py_None); |
| 561 | return Py_None; |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 562 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 563 | return PyDictProxy_New(type->tp_dict); |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 566 | static PyObject * |
| 567 | type_get_doc(PyTypeObject *type, void *context) |
| 568 | { |
| 569 | PyObject *result; |
Guido van Rossum | 6ca7d41 | 2002-04-18 00:22:00 +0000 | [diff] [blame] | 570 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) |
Neal Norwitz | a369c5a | 2007-08-25 07:41:59 +0000 | [diff] [blame] | 571 | return PyUnicode_FromString(type->tp_doc); |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 572 | result = PyDict_GetItemString(type->tp_dict, "__doc__"); |
Guido van Rossum | 6ca7d41 | 2002-04-18 00:22:00 +0000 | [diff] [blame] | 573 | if (result == NULL) { |
| 574 | result = Py_None; |
| 575 | Py_INCREF(result); |
| 576 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 577 | else if (Py_TYPE(result)->tp_descr_get) { |
| 578 | result = Py_TYPE(result)->tp_descr_get(result, NULL, |
Tim Peters | 2b85897 | 2002-04-18 04:12:28 +0000 | [diff] [blame] | 579 | (PyObject *)type); |
Guido van Rossum | 6ca7d41 | 2002-04-18 00:22:00 +0000 | [diff] [blame] | 580 | } |
| 581 | else { |
| 582 | Py_INCREF(result); |
| 583 | } |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 584 | return result; |
| 585 | } |
| 586 | |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 587 | static PyObject * |
| 588 | type___instancecheck__(PyObject *type, PyObject *inst) |
| 589 | { |
| 590 | switch (_PyObject_RealIsInstance(inst, type)) { |
| 591 | case -1: |
| 592 | return NULL; |
| 593 | case 0: |
| 594 | Py_RETURN_FALSE; |
| 595 | default: |
| 596 | Py_RETURN_TRUE; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | |
| 601 | static PyObject * |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 602 | type___subclasscheck__(PyObject *type, PyObject *inst) |
| 603 | { |
| 604 | switch (_PyObject_RealIsSubclass(inst, type)) { |
| 605 | case -1: |
| 606 | return NULL; |
| 607 | case 0: |
| 608 | Py_RETURN_FALSE; |
| 609 | default: |
| 610 | Py_RETURN_TRUE; |
| 611 | } |
| 612 | } |
| 613 | |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 614 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 615 | static PyGetSetDef type_getsets[] = { |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 616 | {"__name__", (getter)type_name, (setter)type_set_name, NULL}, |
| 617 | {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL}, |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 618 | {"__module__", (getter)type_module, (setter)type_set_module, NULL}, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 619 | {"__abstractmethods__", (getter)type_abstractmethods, |
| 620 | (setter)type_set_abstractmethods, NULL}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 621 | {"__dict__", (getter)type_dict, NULL, NULL}, |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 622 | {"__doc__", (getter)type_get_doc, NULL, NULL}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 623 | {0} |
| 624 | }; |
| 625 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 626 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 627 | type_repr(PyTypeObject *type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 628 | { |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 629 | PyObject *mod, *name, *rtn; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 630 | |
| 631 | mod = type_module(type, NULL); |
| 632 | if (mod == NULL) |
| 633 | PyErr_Clear(); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 634 | else if (!PyUnicode_Check(mod)) { |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 635 | Py_DECREF(mod); |
| 636 | mod = NULL; |
| 637 | } |
| 638 | name = type_name(type, NULL); |
| 639 | if (name == NULL) |
| 640 | return NULL; |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 641 | |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 642 | if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins")) |
Martin v. Löwis | 250ad61 | 2008-04-07 05:43:42 +0000 | [diff] [blame] | 643 | rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name); |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 644 | else |
Martin v. Löwis | 250ad61 | 2008-04-07 05:43:42 +0000 | [diff] [blame] | 645 | rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 646 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 647 | Py_XDECREF(mod); |
| 648 | Py_DECREF(name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 649 | return rtn; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 652 | static PyObject * |
| 653 | type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 654 | { |
| 655 | PyObject *obj; |
| 656 | |
| 657 | if (type->tp_new == NULL) { |
| 658 | PyErr_Format(PyExc_TypeError, |
| 659 | "cannot create '%.100s' instances", |
| 660 | type->tp_name); |
| 661 | return NULL; |
| 662 | } |
| 663 | |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 664 | obj = type->tp_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 665 | if (obj != NULL) { |
Guido van Rossum | f76de62 | 2001-10-18 15:49:21 +0000 | [diff] [blame] | 666 | /* Ugly exception: when the call was type(something), |
| 667 | don't call tp_init on the result. */ |
| 668 | if (type == &PyType_Type && |
| 669 | PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && |
| 670 | (kwds == NULL || |
| 671 | (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) |
| 672 | return obj; |
Guido van Rossum | 8ace1ab | 2002-04-06 01:05:01 +0000 | [diff] [blame] | 673 | /* If the returned object is not an instance of type, |
| 674 | it won't be initialized. */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 675 | if (!PyType_IsSubtype(Py_TYPE(obj), type)) |
Guido van Rossum | 8ace1ab | 2002-04-06 01:05:01 +0000 | [diff] [blame] | 676 | return obj; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 677 | type = Py_TYPE(obj); |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 678 | if (type->tp_init != NULL && |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 679 | type->tp_init(obj, args, kwds) < 0) { |
| 680 | Py_DECREF(obj); |
| 681 | obj = NULL; |
| 682 | } |
| 683 | } |
| 684 | return obj; |
| 685 | } |
| 686 | |
| 687 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 688 | PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 689 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 690 | PyObject *obj; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 691 | const size_t size = _PyObject_VAR_SIZE(type, nitems+1); |
| 692 | /* note that we need to add one, for the sentinel */ |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 693 | |
| 694 | if (PyType_IS_GC(type)) |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 695 | obj = _PyObject_GC_Malloc(size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 696 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 697 | obj = (PyObject *)PyObject_MALLOC(size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 698 | |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 699 | if (obj == NULL) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 700 | return PyErr_NoMemory(); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 701 | |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 702 | memset(obj, '\0', size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 703 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 704 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 705 | Py_INCREF(type); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 706 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 707 | if (type->tp_itemsize == 0) |
| 708 | PyObject_INIT(obj, type); |
| 709 | else |
| 710 | (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 711 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 712 | if (PyType_IS_GC(type)) |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 713 | _PyObject_GC_TRACK(obj); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 714 | return obj; |
| 715 | } |
| 716 | |
| 717 | PyObject * |
| 718 | PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 719 | { |
| 720 | return type->tp_alloc(type, 0); |
| 721 | } |
| 722 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 723 | /* Helpers for subtyping */ |
| 724 | |
| 725 | static int |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 726 | traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg) |
| 727 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 728 | Py_ssize_t i, n; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 729 | PyMemberDef *mp; |
| 730 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 731 | n = Py_SIZE(type); |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 732 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 733 | for (i = 0; i < n; i++, mp++) { |
| 734 | if (mp->type == T_OBJECT_EX) { |
| 735 | char *addr = (char *)self + mp->offset; |
| 736 | PyObject *obj = *(PyObject **)addr; |
| 737 | if (obj != NULL) { |
| 738 | int err = visit(obj, arg); |
| 739 | if (err) |
| 740 | return err; |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | return 0; |
| 745 | } |
| 746 | |
| 747 | static int |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 748 | subtype_traverse(PyObject *self, visitproc visit, void *arg) |
| 749 | { |
| 750 | PyTypeObject *type, *base; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 751 | traverseproc basetraverse; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 752 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 753 | /* Find the nearest base with a different tp_traverse, |
| 754 | and traverse slots while we're at it */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 755 | type = Py_TYPE(self); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 756 | base = type; |
| 757 | while ((basetraverse = base->tp_traverse) == subtype_traverse) { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 758 | if (Py_SIZE(base)) { |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 759 | int err = traverse_slots(base, self, visit, arg); |
| 760 | if (err) |
| 761 | return err; |
| 762 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 763 | base = base->tp_base; |
| 764 | assert(base); |
| 765 | } |
| 766 | |
| 767 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 768 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 769 | if (dictptr && *dictptr) |
| 770 | Py_VISIT(*dictptr); |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 773 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 774 | /* For a heaptype, the instances count as references |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 775 | to the type. Traverse the type so the collector |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 776 | can find cycles involving this link. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 777 | Py_VISIT(type); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 778 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 779 | if (basetraverse) |
| 780 | return basetraverse(self, visit, arg); |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | static void |
| 785 | clear_slots(PyTypeObject *type, PyObject *self) |
| 786 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 787 | Py_ssize_t i, n; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 788 | PyMemberDef *mp; |
| 789 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 790 | n = Py_SIZE(type); |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 791 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 792 | for (i = 0; i < n; i++, mp++) { |
| 793 | if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) { |
| 794 | char *addr = (char *)self + mp->offset; |
| 795 | PyObject *obj = *(PyObject **)addr; |
| 796 | if (obj != NULL) { |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 797 | *(PyObject **)addr = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 798 | Py_DECREF(obj); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | static int |
| 805 | subtype_clear(PyObject *self) |
| 806 | { |
| 807 | PyTypeObject *type, *base; |
| 808 | inquiry baseclear; |
| 809 | |
| 810 | /* Find the nearest base with a different tp_clear |
| 811 | and clear slots while we're at it */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 812 | type = Py_TYPE(self); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 813 | base = type; |
| 814 | while ((baseclear = base->tp_clear) == subtype_clear) { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 815 | if (Py_SIZE(base)) |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 816 | clear_slots(base, self); |
| 817 | base = base->tp_base; |
| 818 | assert(base); |
| 819 | } |
| 820 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 821 | /* There's no need to clear the instance dict (if any); |
| 822 | the collector will call its tp_clear handler. */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 823 | |
| 824 | if (baseclear) |
| 825 | return baseclear(self); |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 826 | return 0; |
| 827 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 828 | |
| 829 | static void |
| 830 | subtype_dealloc(PyObject *self) |
| 831 | { |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 832 | PyTypeObject *type, *base; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 833 | destructor basedealloc; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 834 | |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 835 | /* Extract the type; we expect it to be a heap type */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 836 | type = Py_TYPE(self); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 837 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 838 | |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 839 | /* Test whether the type has GC exactly once */ |
| 840 | |
| 841 | if (!PyType_IS_GC(type)) { |
| 842 | /* It's really rare to find a dynamic type that doesn't have |
| 843 | GC; it can only happen when deriving from 'object' and not |
| 844 | adding any slots or instance variables. This allows |
| 845 | certain simplifications: there's no need to call |
| 846 | clear_slots(), or DECREF the dict, or clear weakrefs. */ |
| 847 | |
| 848 | /* Maybe call finalizer; exit early if resurrected */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 849 | if (type->tp_del) { |
| 850 | type->tp_del(self); |
| 851 | if (self->ob_refcnt > 0) |
| 852 | return; |
| 853 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 854 | |
| 855 | /* Find the nearest base with a different tp_dealloc */ |
| 856 | base = type; |
| 857 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 858 | assert(Py_SIZE(base) == 0); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 859 | base = base->tp_base; |
| 860 | assert(base); |
| 861 | } |
| 862 | |
Benjamin Peterson | 193152c | 2009-04-25 01:08:45 +0000 | [diff] [blame] | 863 | /* Extract the type again; tp_del may have changed it */ |
| 864 | type = Py_TYPE(self); |
| 865 | |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 866 | /* Call the base tp_dealloc() */ |
| 867 | assert(basedealloc); |
| 868 | basedealloc(self); |
| 869 | |
| 870 | /* Can't reference self beyond this point */ |
| 871 | Py_DECREF(type); |
| 872 | |
| 873 | /* Done */ |
| 874 | return; |
| 875 | } |
| 876 | |
| 877 | /* We get here only if the type has GC */ |
| 878 | |
| 879 | /* UnTrack and re-Track around the trashcan macro, alas */ |
Andrew M. Kuchling | c9172d3 | 2003-02-06 15:22:49 +0000 | [diff] [blame] | 880 | /* See explanation at end of function for full disclosure */ |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 881 | PyObject_GC_UnTrack(self); |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 882 | ++_PyTrash_delete_nesting; |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 883 | Py_TRASHCAN_SAFE_BEGIN(self); |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 884 | --_PyTrash_delete_nesting; |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 885 | /* DO NOT restore GC tracking at this point. weakref callbacks |
| 886 | * (if any, and whether directly here or indirectly in something we |
| 887 | * call) may trigger GC, and if self is tracked at that point, it |
| 888 | * 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] | 889 | */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 890 | |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 891 | /* Find the nearest base with a different tp_dealloc */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 892 | base = type; |
| 893 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 894 | base = base->tp_base; |
| 895 | assert(base); |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 898 | /* 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] | 899 | the finalizer (__del__), clearing slots, or clearing the instance |
| 900 | dict. */ |
| 901 | |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 902 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) |
| 903 | PyObject_ClearWeakRefs(self); |
| 904 | |
| 905 | /* Maybe call finalizer; exit early if resurrected */ |
| 906 | if (type->tp_del) { |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 907 | _PyObject_GC_TRACK(self); |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 908 | type->tp_del(self); |
| 909 | if (self->ob_refcnt > 0) |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 910 | goto endlabel; /* resurrected */ |
| 911 | else |
| 912 | _PyObject_GC_UNTRACK(self); |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 913 | /* New weakrefs could be created during the finalizer call. |
| 914 | If this occurs, clear them out without calling their |
| 915 | finalizers since they might rely on part of the object |
| 916 | being finalized that has already been destroyed. */ |
| 917 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) { |
| 918 | /* Modeled after GET_WEAKREFS_LISTPTR() */ |
| 919 | PyWeakReference **list = (PyWeakReference **) \ |
| 920 | PyObject_GET_WEAKREFS_LISTPTR(self); |
| 921 | while (*list) |
| 922 | _PyWeakref_ClearRef(*list); |
| 923 | } |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 926 | /* Clear slots up to the nearest base with a different tp_dealloc */ |
| 927 | base = type; |
| 928 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 929 | if (Py_SIZE(base)) |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 930 | clear_slots(base, self); |
| 931 | base = base->tp_base; |
| 932 | assert(base); |
| 933 | } |
| 934 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 935 | /* If we added a dict, DECREF it */ |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 936 | if (type->tp_dictoffset && !base->tp_dictoffset) { |
| 937 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 938 | if (dictptr != NULL) { |
| 939 | PyObject *dict = *dictptr; |
| 940 | if (dict != NULL) { |
| 941 | Py_DECREF(dict); |
| 942 | *dictptr = NULL; |
| 943 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | |
Benjamin Peterson | 193152c | 2009-04-25 01:08:45 +0000 | [diff] [blame] | 947 | /* Extract the type again; tp_del may have changed it */ |
| 948 | type = Py_TYPE(self); |
| 949 | |
Tim Peters | 0bd743c | 2003-11-13 22:50:00 +0000 | [diff] [blame] | 950 | /* Call the base tp_dealloc(); first retrack self if |
| 951 | * basedealloc knows about gc. |
| 952 | */ |
| 953 | if (PyType_IS_GC(base)) |
| 954 | _PyObject_GC_TRACK(self); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 955 | assert(basedealloc); |
| 956 | basedealloc(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 957 | |
| 958 | /* Can't reference self beyond this point */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 959 | Py_DECREF(type); |
| 960 | |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 961 | endlabel: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 962 | ++_PyTrash_delete_nesting; |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 963 | Py_TRASHCAN_SAFE_END(self); |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 964 | --_PyTrash_delete_nesting; |
| 965 | |
| 966 | /* Explanation of the weirdness around the trashcan macros: |
| 967 | |
| 968 | Q. What do the trashcan macros do? |
| 969 | |
| 970 | A. Read the comment titled "Trashcan mechanism" in object.h. |
| 971 | For one, this explains why there must be a call to GC-untrack |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 972 | before the trashcan begin macro. Without understanding the |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 973 | trashcan code, the answers to the following questions don't make |
| 974 | sense. |
| 975 | |
| 976 | Q. Why do we GC-untrack before the trashcan and then immediately |
| 977 | GC-track again afterward? |
| 978 | |
| 979 | A. In the case that the base class is GC-aware, the base class |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 980 | probably GC-untracks the object. If it does that using the |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 981 | UNTRACK macro, this will crash when the object is already |
| 982 | untracked. Because we don't know what the base class does, the |
| 983 | only safe thing is to make sure the object is tracked when we |
| 984 | call the base class dealloc. But... The trashcan begin macro |
| 985 | requires that the object is *untracked* before it is called. So |
| 986 | the dance becomes: |
| 987 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 988 | GC untrack |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 989 | trashcan begin |
| 990 | GC track |
| 991 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 992 | Q. Why did the last question say "immediately GC-track again"? |
| 993 | It's nowhere near immediately. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 994 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 995 | A. Because the code *used* to re-track immediately. Bad Idea. |
| 996 | self has a refcount of 0, and if gc ever gets its hands on it |
| 997 | (which can happen if any weakref callback gets invoked), it |
| 998 | looks like trash to gc too, and gc also tries to delete self |
| 999 | then. But we're already deleting self. Double dealloction is |
| 1000 | a subtle disaster. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 1001 | |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1002 | Q. Why the bizarre (net-zero) manipulation of |
| 1003 | _PyTrash_delete_nesting around the trashcan macros? |
| 1004 | |
| 1005 | A. Some base classes (e.g. list) also use the trashcan mechanism. |
| 1006 | The following scenario used to be possible: |
| 1007 | |
| 1008 | - suppose the trashcan level is one below the trashcan limit |
| 1009 | |
| 1010 | - subtype_dealloc() is called |
| 1011 | |
| 1012 | - the trashcan limit is not yet reached, so the trashcan level |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1013 | is incremented and the code between trashcan begin and end is |
| 1014 | executed |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1015 | |
| 1016 | - this destroys much of the object's contents, including its |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1017 | slots and __dict__ |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1018 | |
| 1019 | - basedealloc() is called; this is really list_dealloc(), or |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1020 | some other type which also uses the trashcan macros |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1021 | |
| 1022 | - the trashcan limit is now reached, so the object is put on the |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1023 | trashcan's to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1024 | |
| 1025 | - basedealloc() returns |
| 1026 | |
| 1027 | - subtype_dealloc() decrefs the object's type |
| 1028 | |
| 1029 | - subtype_dealloc() returns |
| 1030 | |
| 1031 | - later, the trashcan code starts deleting the objects from its |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1032 | to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1033 | |
| 1034 | - subtype_dealloc() is called *AGAIN* for the same object |
| 1035 | |
| 1036 | - at the very least (if the destroyed slots and __dict__ don't |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1037 | cause problems) the object's type gets decref'ed a second |
| 1038 | time, which is *BAD*!!! |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1039 | |
| 1040 | The remedy is to make sure that if the code between trashcan |
| 1041 | begin and end in subtype_dealloc() is called, the code between |
| 1042 | trashcan begin and end in basedealloc() will also be called. |
| 1043 | This is done by decrementing the level after passing into the |
| 1044 | trashcan block, and incrementing it just before leaving the |
| 1045 | block. |
| 1046 | |
| 1047 | But now it's possible that a chain of objects consisting solely |
| 1048 | of objects whose deallocator is subtype_dealloc() will defeat |
| 1049 | the trashcan mechanism completely: the decremented level means |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1050 | that the effective level never reaches the limit. Therefore, we |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1051 | *increment* the level *before* entering the trashcan block, and |
| 1052 | matchingly decrement it after leaving. This means the trashcan |
| 1053 | code will trigger a little early, but that's no big deal. |
| 1054 | |
| 1055 | Q. Are there any live examples of code in need of all this |
| 1056 | complexity? |
| 1057 | |
| 1058 | A. Yes. See SF bug 668433 for code that crashed (when Python was |
| 1059 | compiled in debug mode) before the trashcan level manipulations |
| 1060 | were added. For more discussion, see SF patches 581742, 575073 |
| 1061 | and bug 574207. |
| 1062 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1065 | static PyTypeObject *solid_base(PyTypeObject *type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1066 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1067 | /* type test with subclassing support */ |
| 1068 | |
| 1069 | int |
| 1070 | PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
| 1071 | { |
| 1072 | PyObject *mro; |
| 1073 | |
| 1074 | mro = a->tp_mro; |
| 1075 | if (mro != NULL) { |
| 1076 | /* Deal with multiple inheritance without recursion |
| 1077 | by walking the MRO tuple */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1078 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1079 | assert(PyTuple_Check(mro)); |
| 1080 | n = PyTuple_GET_SIZE(mro); |
| 1081 | for (i = 0; i < n; i++) { |
| 1082 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) |
| 1083 | return 1; |
| 1084 | } |
| 1085 | return 0; |
| 1086 | } |
| 1087 | else { |
| 1088 | /* a is not completely initilized yet; follow tp_base */ |
| 1089 | do { |
| 1090 | if (a == b) |
| 1091 | return 1; |
| 1092 | a = a->tp_base; |
| 1093 | } while (a != NULL); |
| 1094 | return b == &PyBaseObject_Type; |
| 1095 | } |
| 1096 | } |
| 1097 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1098 | /* Internal routines to do a method lookup in the type |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1099 | without looking in the instance dictionary |
| 1100 | (so we can't use PyObject_GetAttr) but still binding |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1101 | it to the instance. The arguments are the object, |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1102 | 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] | 1103 | static variable used to cache the interned Python string. |
| 1104 | |
| 1105 | Two variants: |
| 1106 | |
| 1107 | - lookup_maybe() returns NULL without raising an exception |
| 1108 | when the _PyType_Lookup() call fails; |
| 1109 | |
| 1110 | - lookup_method() always raises an exception upon errors. |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1111 | |
| 1112 | - _PyObject_LookupSpecial() exported for the benefit of other places. |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1113 | */ |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1114 | |
| 1115 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1116 | lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1117 | { |
| 1118 | PyObject *res; |
| 1119 | |
| 1120 | if (*attrobj == NULL) { |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1121 | *attrobj = PyUnicode_InternFromString(attrstr); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1122 | if (*attrobj == NULL) |
| 1123 | return NULL; |
| 1124 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1125 | res = _PyType_Lookup(Py_TYPE(self), *attrobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1126 | if (res != NULL) { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1127 | descrgetfunc f; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1128 | if ((f = Py_TYPE(res)->tp_descr_get) == NULL) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1129 | Py_INCREF(res); |
| 1130 | else |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1131 | res = f(res, self, (PyObject *)(Py_TYPE(self))); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1132 | } |
| 1133 | return res; |
| 1134 | } |
| 1135 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1136 | static PyObject * |
| 1137 | lookup_method(PyObject *self, char *attrstr, PyObject **attrobj) |
| 1138 | { |
| 1139 | PyObject *res = lookup_maybe(self, attrstr, attrobj); |
| 1140 | if (res == NULL && !PyErr_Occurred()) |
| 1141 | PyErr_SetObject(PyExc_AttributeError, *attrobj); |
| 1142 | return res; |
| 1143 | } |
| 1144 | |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1145 | PyObject * |
| 1146 | _PyObject_LookupSpecial(PyObject *self, char *attrstr, PyObject **attrobj) |
| 1147 | { |
| 1148 | return lookup_maybe(self, attrstr, attrobj); |
| 1149 | } |
| 1150 | |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1151 | /* A variation of PyObject_CallMethod that uses lookup_method() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1152 | instead of PyObject_GetAttrString(). This uses the same convention |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1153 | as lookup_method to cache the interned name string object. */ |
| 1154 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1155 | static PyObject * |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1156 | call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 1157 | { |
| 1158 | va_list va; |
| 1159 | PyObject *args, *func = 0, *retval; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1160 | va_start(va, format); |
| 1161 | |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 1162 | func = lookup_maybe(o, name, nameobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1163 | if (func == NULL) { |
| 1164 | va_end(va); |
| 1165 | if (!PyErr_Occurred()) |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 1166 | PyErr_SetObject(PyExc_AttributeError, *nameobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1167 | return NULL; |
| 1168 | } |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1169 | |
| 1170 | if (format && *format) |
| 1171 | args = Py_VaBuildValue(format, va); |
| 1172 | else |
| 1173 | args = PyTuple_New(0); |
| 1174 | |
| 1175 | va_end(va); |
| 1176 | |
| 1177 | if (args == NULL) |
| 1178 | return NULL; |
| 1179 | |
| 1180 | assert(PyTuple_Check(args)); |
| 1181 | retval = PyObject_Call(func, args, NULL); |
| 1182 | |
| 1183 | Py_DECREF(args); |
| 1184 | Py_DECREF(func); |
| 1185 | |
| 1186 | return retval; |
| 1187 | } |
| 1188 | |
| 1189 | /* Clone of call_method() that returns NotImplemented when the lookup fails. */ |
| 1190 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1191 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1192 | call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 1193 | { |
| 1194 | va_list va; |
| 1195 | PyObject *args, *func = 0, *retval; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1196 | va_start(va, format); |
| 1197 | |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 1198 | func = lookup_maybe(o, name, nameobj); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1199 | if (func == NULL) { |
| 1200 | va_end(va); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1201 | if (!PyErr_Occurred()) { |
| 1202 | Py_INCREF(Py_NotImplemented); |
| 1203 | return Py_NotImplemented; |
| 1204 | } |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 1205 | return NULL; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | if (format && *format) |
| 1209 | args = Py_VaBuildValue(format, va); |
| 1210 | else |
| 1211 | args = PyTuple_New(0); |
| 1212 | |
| 1213 | va_end(va); |
| 1214 | |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 1215 | if (args == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1216 | return NULL; |
| 1217 | |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 1218 | assert(PyTuple_Check(args)); |
| 1219 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1220 | |
| 1221 | Py_DECREF(args); |
| 1222 | Py_DECREF(func); |
| 1223 | |
| 1224 | return retval; |
| 1225 | } |
| 1226 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1227 | /* |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1228 | Method resolution order algorithm C3 described in |
| 1229 | "A Monotonic Superclass Linearization for Dylan", |
| 1230 | by Kim Barrett, Bob Cassel, Paul Haahr, |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1231 | David A. Moon, Keith Playford, and P. Tucker Withington. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1232 | (OOPSLA 1996) |
| 1233 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1234 | Some notes about the rules implied by C3: |
| 1235 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1236 | No duplicate bases. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1237 | It isn't legal to repeat a class in a list of base classes. |
| 1238 | |
| 1239 | The next three properties are the 3 constraints in "C3". |
| 1240 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1241 | Local precendece order. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1242 | If A precedes B in C's MRO, then A will precede B in the MRO of all |
| 1243 | subclasses of C. |
| 1244 | |
| 1245 | Monotonicity. |
| 1246 | The MRO of a class must be an extension without reordering of the |
| 1247 | MRO of each of its superclasses. |
| 1248 | |
| 1249 | Extended Precedence Graph (EPG). |
| 1250 | Linearization is consistent if there is a path in the EPG from |
| 1251 | each class to all its successors in the linearization. See |
| 1252 | the paper for definition of EPG. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1253 | */ |
| 1254 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1255 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1256 | tail_contains(PyObject *list, int whence, PyObject *o) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1257 | Py_ssize_t j, size; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1258 | size = PyList_GET_SIZE(list); |
| 1259 | |
| 1260 | for (j = whence+1; j < size; j++) { |
| 1261 | if (PyList_GET_ITEM(list, j) == o) |
| 1262 | return 1; |
| 1263 | } |
| 1264 | return 0; |
| 1265 | } |
| 1266 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1267 | static PyObject * |
| 1268 | class_name(PyObject *cls) |
| 1269 | { |
| 1270 | PyObject *name = PyObject_GetAttrString(cls, "__name__"); |
| 1271 | if (name == NULL) { |
| 1272 | PyErr_Clear(); |
| 1273 | Py_XDECREF(name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1274 | name = PyObject_Repr(cls); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1275 | } |
| 1276 | if (name == NULL) |
| 1277 | return NULL; |
Martin v. Löwis | 9b9905b | 2007-06-10 21:13:34 +0000 | [diff] [blame] | 1278 | if (!PyUnicode_Check(name)) { |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1279 | Py_DECREF(name); |
| 1280 | return NULL; |
| 1281 | } |
| 1282 | return name; |
| 1283 | } |
| 1284 | |
| 1285 | static int |
| 1286 | check_duplicates(PyObject *list) |
| 1287 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1288 | Py_ssize_t i, j, n; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1289 | /* Let's use a quadratic time algorithm, |
| 1290 | assuming that the bases lists is short. |
| 1291 | */ |
| 1292 | n = PyList_GET_SIZE(list); |
| 1293 | for (i = 0; i < n; i++) { |
| 1294 | PyObject *o = PyList_GET_ITEM(list, i); |
| 1295 | for (j = i + 1; j < n; j++) { |
| 1296 | if (PyList_GET_ITEM(list, j) == o) { |
| 1297 | o = class_name(o); |
Victor Stinner | 3f1af5c | 2010-03-12 17:00:41 +0000 | [diff] [blame] | 1298 | if (o != NULL) { |
| 1299 | PyErr_Format(PyExc_TypeError, |
| 1300 | "duplicate base class %U", |
| 1301 | o); |
| 1302 | Py_DECREF(o); |
| 1303 | } else { |
| 1304 | PyErr_SetString(PyExc_TypeError, |
| 1305 | "duplicate base class"); |
| 1306 | } |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1307 | return -1; |
| 1308 | } |
| 1309 | } |
| 1310 | } |
| 1311 | return 0; |
| 1312 | } |
| 1313 | |
| 1314 | /* Raise a TypeError for an MRO order disagreement. |
| 1315 | |
| 1316 | It's hard to produce a good error message. In the absence of better |
| 1317 | insight into error reporting, report the classes that were candidates |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1318 | 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] | 1319 | order in which they should be put in the MRO, but it's hard to |
| 1320 | diagnose what constraint can't be satisfied. |
| 1321 | */ |
| 1322 | |
| 1323 | static void |
| 1324 | set_mro_error(PyObject *to_merge, int *remain) |
| 1325 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1326 | Py_ssize_t i, n, off, to_merge_size; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1327 | char buf[1000]; |
| 1328 | PyObject *k, *v; |
| 1329 | PyObject *set = PyDict_New(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1330 | if (!set) return; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1331 | |
| 1332 | to_merge_size = PyList_GET_SIZE(to_merge); |
| 1333 | for (i = 0; i < to_merge_size; i++) { |
| 1334 | PyObject *L = PyList_GET_ITEM(to_merge, i); |
| 1335 | if (remain[i] < PyList_GET_SIZE(L)) { |
| 1336 | PyObject *c = PyList_GET_ITEM(L, remain[i]); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1337 | if (PyDict_SetItem(set, c, Py_None) < 0) { |
| 1338 | Py_DECREF(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1339 | return; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1340 | } |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1341 | } |
| 1342 | } |
| 1343 | n = PyDict_Size(set); |
| 1344 | |
Raymond Hettinger | f394df4 | 2003-04-06 19:13:41 +0000 | [diff] [blame] | 1345 | off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \ |
| 1346 | consistent method resolution\norder (MRO) for bases"); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1347 | i = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1348 | 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] | 1349 | PyObject *name = class_name(k); |
| 1350 | off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1351 | name ? _PyUnicode_AsString(name) : "?"); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1352 | Py_XDECREF(name); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1353 | if (--n && (size_t)(off+1) < sizeof(buf)) { |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1354 | buf[off++] = ','; |
| 1355 | buf[off] = '\0'; |
| 1356 | } |
| 1357 | } |
| 1358 | PyErr_SetString(PyExc_TypeError, buf); |
| 1359 | Py_DECREF(set); |
| 1360 | } |
| 1361 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1362 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1363 | pmerge(PyObject *acc, PyObject* to_merge) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1364 | Py_ssize_t i, j, to_merge_size, empty_cnt; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1365 | int *remain; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1366 | int ok; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1367 | |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1368 | to_merge_size = PyList_GET_SIZE(to_merge); |
| 1369 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1370 | /* remain stores an index into each sublist of to_merge. |
| 1371 | remain[i] is the index of the next base in to_merge[i] |
| 1372 | that is not included in acc. |
| 1373 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1374 | remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1375 | if (remain == NULL) |
| 1376 | return -1; |
| 1377 | for (i = 0; i < to_merge_size; i++) |
| 1378 | remain[i] = 0; |
| 1379 | |
| 1380 | again: |
| 1381 | empty_cnt = 0; |
| 1382 | for (i = 0; i < to_merge_size; i++) { |
| 1383 | PyObject *candidate; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1384 | |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1385 | PyObject *cur_list = PyList_GET_ITEM(to_merge, i); |
| 1386 | |
| 1387 | if (remain[i] >= PyList_GET_SIZE(cur_list)) { |
| 1388 | empty_cnt++; |
| 1389 | continue; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1390 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1391 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1392 | /* Choose next candidate for MRO. |
| 1393 | |
| 1394 | The input sequences alone can determine the choice. |
| 1395 | If not, choose the class which appears in the MRO |
| 1396 | of the earliest direct superclass of the new class. |
| 1397 | */ |
| 1398 | |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1399 | candidate = PyList_GET_ITEM(cur_list, remain[i]); |
| 1400 | for (j = 0; j < to_merge_size; j++) { |
| 1401 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1402 | if (tail_contains(j_lst, remain[j], candidate)) { |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1403 | goto skip; /* continue outer loop */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1404 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1405 | } |
| 1406 | ok = PyList_Append(acc, candidate); |
| 1407 | if (ok < 0) { |
| 1408 | PyMem_Free(remain); |
| 1409 | return -1; |
| 1410 | } |
| 1411 | for (j = 0; j < to_merge_size; j++) { |
| 1412 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
Guido van Rossum | 768158c | 2002-12-31 16:33:01 +0000 | [diff] [blame] | 1413 | if (remain[j] < PyList_GET_SIZE(j_lst) && |
| 1414 | PyList_GET_ITEM(j_lst, remain[j]) == candidate) { |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1415 | remain[j]++; |
| 1416 | } |
| 1417 | } |
| 1418 | goto again; |
Tim Peters | 9a6b8d8 | 2002-11-14 23:22:33 +0000 | [diff] [blame] | 1419 | skip: ; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1422 | if (empty_cnt == to_merge_size) { |
| 1423 | PyMem_FREE(remain); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1424 | return 0; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1425 | } |
| 1426 | set_mro_error(to_merge, remain); |
| 1427 | PyMem_FREE(remain); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1428 | return -1; |
| 1429 | } |
| 1430 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1431 | static PyObject * |
| 1432 | mro_implementation(PyTypeObject *type) |
| 1433 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1434 | Py_ssize_t i, n; |
| 1435 | int ok; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1436 | PyObject *bases, *result; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1437 | PyObject *to_merge, *bases_aslist; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1438 | |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 1439 | if (type->tp_dict == NULL) { |
| 1440 | if (PyType_Ready(type) < 0) |
Guido van Rossum | 6351757 | 2002-06-18 16:44:57 +0000 | [diff] [blame] | 1441 | return NULL; |
| 1442 | } |
| 1443 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1444 | /* Find a superclass linearization that honors the constraints |
| 1445 | of the explicit lists of bases and the constraints implied by |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1446 | each base class. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1447 | |
| 1448 | to_merge is a list of lists, where each list is a superclass |
| 1449 | linearization implied by a base class. The last element of |
| 1450 | to_merge is the declared list of bases. |
| 1451 | */ |
| 1452 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1453 | bases = type->tp_bases; |
| 1454 | n = PyTuple_GET_SIZE(bases); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1455 | |
| 1456 | to_merge = PyList_New(n+1); |
| 1457 | if (to_merge == NULL) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1458 | return NULL; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1459 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1460 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1461 | PyObject *base = PyTuple_GET_ITEM(bases, i); |
| 1462 | PyObject *parentMRO; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 1463 | parentMRO = PySequence_List(((PyTypeObject*)base)->tp_mro); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1464 | if (parentMRO == NULL) { |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1465 | Py_DECREF(to_merge); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1466 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1467 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1468 | |
| 1469 | PyList_SET_ITEM(to_merge, i, parentMRO); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1470 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1471 | |
| 1472 | bases_aslist = PySequence_List(bases); |
| 1473 | if (bases_aslist == NULL) { |
| 1474 | Py_DECREF(to_merge); |
| 1475 | return NULL; |
| 1476 | } |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1477 | /* This is just a basic sanity check. */ |
| 1478 | if (check_duplicates(bases_aslist) < 0) { |
| 1479 | Py_DECREF(to_merge); |
| 1480 | Py_DECREF(bases_aslist); |
| 1481 | return NULL; |
| 1482 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1483 | PyList_SET_ITEM(to_merge, n, bases_aslist); |
| 1484 | |
| 1485 | result = Py_BuildValue("[O]", (PyObject *)type); |
| 1486 | if (result == NULL) { |
| 1487 | Py_DECREF(to_merge); |
| 1488 | return NULL; |
| 1489 | } |
| 1490 | |
| 1491 | ok = pmerge(result, to_merge); |
| 1492 | Py_DECREF(to_merge); |
| 1493 | if (ok < 0) { |
| 1494 | Py_DECREF(result); |
| 1495 | return NULL; |
| 1496 | } |
| 1497 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1498 | return result; |
| 1499 | } |
| 1500 | |
| 1501 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1502 | mro_external(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1503 | { |
| 1504 | PyTypeObject *type = (PyTypeObject *)self; |
| 1505 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1506 | return mro_implementation(type); |
| 1507 | } |
| 1508 | |
| 1509 | static int |
| 1510 | mro_internal(PyTypeObject *type) |
| 1511 | { |
| 1512 | PyObject *mro, *result, *tuple; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1513 | int checkit = 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1514 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1515 | if (Py_TYPE(type) == &PyType_Type) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1516 | result = mro_implementation(type); |
| 1517 | } |
| 1518 | else { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1519 | static PyObject *mro_str; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1520 | checkit = 1; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1521 | mro = lookup_method((PyObject *)type, "mro", &mro_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1522 | if (mro == NULL) |
| 1523 | return -1; |
| 1524 | result = PyObject_CallObject(mro, NULL); |
| 1525 | Py_DECREF(mro); |
| 1526 | } |
| 1527 | if (result == NULL) |
| 1528 | return -1; |
| 1529 | tuple = PySequence_Tuple(result); |
| 1530 | Py_DECREF(result); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1531 | if (tuple == NULL) |
| 1532 | return -1; |
| 1533 | if (checkit) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1534 | Py_ssize_t i, len; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1535 | PyObject *cls; |
| 1536 | PyTypeObject *solid; |
| 1537 | |
| 1538 | solid = solid_base(type); |
| 1539 | |
| 1540 | len = PyTuple_GET_SIZE(tuple); |
| 1541 | |
| 1542 | for (i = 0; i < len; i++) { |
| 1543 | PyTypeObject *t; |
| 1544 | cls = PyTuple_GET_ITEM(tuple, i); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 1545 | if (!PyType_Check(cls)) { |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1546 | PyErr_Format(PyExc_TypeError, |
| 1547 | "mro() returned a non-class ('%.500s')", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1548 | Py_TYPE(cls)->tp_name); |
Neal Norwitz | 50bf51a | 2006-01-02 02:46:54 +0000 | [diff] [blame] | 1549 | Py_DECREF(tuple); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1550 | return -1; |
| 1551 | } |
| 1552 | t = (PyTypeObject*)cls; |
| 1553 | if (!PyType_IsSubtype(solid, solid_base(t))) { |
| 1554 | PyErr_Format(PyExc_TypeError, |
| 1555 | "mro() returned base with unsuitable layout ('%.500s')", |
| 1556 | t->tp_name); |
Neal Norwitz | 50bf51a | 2006-01-02 02:46:54 +0000 | [diff] [blame] | 1557 | Py_DECREF(tuple); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1558 | return -1; |
| 1559 | } |
| 1560 | } |
| 1561 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1562 | type->tp_mro = tuple; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1563 | |
| 1564 | type_mro_modified(type, type->tp_mro); |
| 1565 | /* corner case: the old-style super class might have been hidden |
| 1566 | from the custom MRO */ |
| 1567 | type_mro_modified(type, type->tp_bases); |
| 1568 | |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 1569 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1570 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1571 | return 0; |
| 1572 | } |
| 1573 | |
| 1574 | |
| 1575 | /* Calculate the best base amongst multiple base classes. |
| 1576 | This is the first one that's on the path to the "solid base". */ |
| 1577 | |
| 1578 | static PyTypeObject * |
| 1579 | best_base(PyObject *bases) |
| 1580 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1581 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1582 | PyTypeObject *base, *winner, *candidate, *base_i; |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1583 | PyObject *base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1584 | |
| 1585 | assert(PyTuple_Check(bases)); |
| 1586 | n = PyTuple_GET_SIZE(bases); |
| 1587 | assert(n > 0); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1588 | base = NULL; |
| 1589 | winner = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1590 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1591 | base_proto = PyTuple_GET_ITEM(bases, i); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1592 | if (!PyType_Check(base_proto)) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1593 | PyErr_SetString( |
| 1594 | PyExc_TypeError, |
| 1595 | "bases must be types"); |
| 1596 | return NULL; |
| 1597 | } |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1598 | base_i = (PyTypeObject *)base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1599 | if (base_i->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1600 | if (PyType_Ready(base_i) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1601 | return NULL; |
| 1602 | } |
| 1603 | candidate = solid_base(base_i); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1604 | if (winner == NULL) { |
| 1605 | winner = candidate; |
| 1606 | base = base_i; |
| 1607 | } |
| 1608 | else if (PyType_IsSubtype(winner, candidate)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1609 | ; |
| 1610 | else if (PyType_IsSubtype(candidate, winner)) { |
| 1611 | winner = candidate; |
| 1612 | base = base_i; |
| 1613 | } |
| 1614 | else { |
| 1615 | PyErr_SetString( |
| 1616 | PyExc_TypeError, |
| 1617 | "multiple bases have " |
| 1618 | "instance lay-out conflict"); |
| 1619 | return NULL; |
| 1620 | } |
| 1621 | } |
Guido van Rossum | e54616c | 2001-12-14 04:19:56 +0000 | [diff] [blame] | 1622 | if (base == NULL) |
| 1623 | PyErr_SetString(PyExc_TypeError, |
| 1624 | "a new-style class can't have only classic bases"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1625 | return base; |
| 1626 | } |
| 1627 | |
| 1628 | static int |
| 1629 | extra_ivars(PyTypeObject *type, PyTypeObject *base) |
| 1630 | { |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 1631 | size_t t_size = type->tp_basicsize; |
| 1632 | size_t b_size = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1633 | |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1634 | assert(t_size >= b_size); /* Else type smaller than base! */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1635 | if (type->tp_itemsize || base->tp_itemsize) { |
| 1636 | /* If itemsize is involved, stricter rules */ |
| 1637 | return t_size != b_size || |
| 1638 | type->tp_itemsize != base->tp_itemsize; |
| 1639 | } |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1640 | if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 && |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1641 | type->tp_weaklistoffset + sizeof(PyObject *) == t_size && |
| 1642 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1643 | t_size -= sizeof(PyObject *); |
| 1644 | if (type->tp_dictoffset && base->tp_dictoffset == 0 && |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1645 | type->tp_dictoffset + sizeof(PyObject *) == t_size && |
| 1646 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1647 | t_size -= sizeof(PyObject *); |
| 1648 | |
| 1649 | return t_size != b_size; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
| 1652 | static PyTypeObject * |
| 1653 | solid_base(PyTypeObject *type) |
| 1654 | { |
| 1655 | PyTypeObject *base; |
| 1656 | |
| 1657 | if (type->tp_base) |
| 1658 | base = solid_base(type->tp_base); |
| 1659 | else |
| 1660 | base = &PyBaseObject_Type; |
| 1661 | if (extra_ivars(type, base)) |
| 1662 | return type; |
| 1663 | else |
| 1664 | return base; |
| 1665 | } |
| 1666 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1667 | static void object_dealloc(PyObject *); |
| 1668 | static int object_init(PyObject *, PyObject *, PyObject *); |
| 1669 | static int update_slot(PyTypeObject *, PyObject *); |
| 1670 | static void fixup_slot_dispatchers(PyTypeObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1671 | |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1672 | /* |
| 1673 | * Helpers for __dict__ descriptor. We don't want to expose the dicts |
| 1674 | * inherited from various builtin types. The builtin base usually provides |
| 1675 | * its own __dict__ descriptor, so we use that when we can. |
| 1676 | */ |
| 1677 | static PyTypeObject * |
| 1678 | get_builtin_base_with_dict(PyTypeObject *type) |
| 1679 | { |
| 1680 | while (type->tp_base != NULL) { |
| 1681 | if (type->tp_dictoffset != 0 && |
| 1682 | !(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 1683 | return type; |
| 1684 | type = type->tp_base; |
| 1685 | } |
| 1686 | return NULL; |
| 1687 | } |
| 1688 | |
| 1689 | static PyObject * |
| 1690 | get_dict_descriptor(PyTypeObject *type) |
| 1691 | { |
| 1692 | static PyObject *dict_str; |
| 1693 | PyObject *descr; |
| 1694 | |
| 1695 | if (dict_str == NULL) { |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1696 | dict_str = PyUnicode_InternFromString("__dict__"); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1697 | if (dict_str == NULL) |
| 1698 | return NULL; |
| 1699 | } |
| 1700 | descr = _PyType_Lookup(type, dict_str); |
| 1701 | if (descr == NULL || !PyDescr_IsData(descr)) |
| 1702 | return NULL; |
| 1703 | |
| 1704 | return descr; |
| 1705 | } |
| 1706 | |
| 1707 | static void |
| 1708 | raise_dict_descr_error(PyObject *obj) |
| 1709 | { |
| 1710 | PyErr_Format(PyExc_TypeError, |
| 1711 | "this __dict__ descriptor does not support " |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1712 | "'%.200s' objects", Py_TYPE(obj)->tp_name); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1715 | static PyObject * |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1716 | subtype_dict(PyObject *obj, void *context) |
| 1717 | { |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1718 | PyObject **dictptr; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1719 | PyObject *dict; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1720 | PyTypeObject *base; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1721 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1722 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1723 | if (base != NULL) { |
| 1724 | descrgetfunc func; |
| 1725 | PyObject *descr = get_dict_descriptor(base); |
| 1726 | if (descr == NULL) { |
| 1727 | raise_dict_descr_error(obj); |
| 1728 | return NULL; |
| 1729 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1730 | func = Py_TYPE(descr)->tp_descr_get; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1731 | if (func == NULL) { |
| 1732 | raise_dict_descr_error(obj); |
| 1733 | return NULL; |
| 1734 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1735 | return func(descr, obj, (PyObject *)(Py_TYPE(obj))); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | dictptr = _PyObject_GetDictPtr(obj); |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1739 | if (dictptr == NULL) { |
| 1740 | PyErr_SetString(PyExc_AttributeError, |
| 1741 | "This object has no __dict__"); |
| 1742 | return NULL; |
| 1743 | } |
| 1744 | dict = *dictptr; |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 1745 | if (dict == NULL) |
| 1746 | *dictptr = dict = PyDict_New(); |
| 1747 | Py_XINCREF(dict); |
| 1748 | return dict; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1751 | static int |
| 1752 | subtype_setdict(PyObject *obj, PyObject *value, void *context) |
| 1753 | { |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1754 | PyObject **dictptr; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1755 | PyObject *dict; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1756 | PyTypeObject *base; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1757 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1758 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1759 | if (base != NULL) { |
| 1760 | descrsetfunc func; |
| 1761 | PyObject *descr = get_dict_descriptor(base); |
| 1762 | if (descr == NULL) { |
| 1763 | raise_dict_descr_error(obj); |
| 1764 | return -1; |
| 1765 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1766 | func = Py_TYPE(descr)->tp_descr_set; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1767 | if (func == NULL) { |
| 1768 | raise_dict_descr_error(obj); |
| 1769 | return -1; |
| 1770 | } |
| 1771 | return func(descr, obj, value); |
| 1772 | } |
| 1773 | |
| 1774 | dictptr = _PyObject_GetDictPtr(obj); |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1775 | if (dictptr == NULL) { |
| 1776 | PyErr_SetString(PyExc_AttributeError, |
| 1777 | "This object has no __dict__"); |
| 1778 | return -1; |
| 1779 | } |
Guido van Rossum | d331cb5 | 2001-12-05 19:46:42 +0000 | [diff] [blame] | 1780 | if (value != NULL && !PyDict_Check(value)) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1781 | PyErr_Format(PyExc_TypeError, |
| 1782 | "__dict__ must be set to a dictionary, " |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1783 | "not a '%.200s'", Py_TYPE(value)->tp_name); |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1784 | return -1; |
| 1785 | } |
| 1786 | dict = *dictptr; |
Guido van Rossum | d331cb5 | 2001-12-05 19:46:42 +0000 | [diff] [blame] | 1787 | Py_XINCREF(value); |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1788 | *dictptr = value; |
| 1789 | Py_XDECREF(dict); |
| 1790 | return 0; |
| 1791 | } |
| 1792 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1793 | static PyObject * |
| 1794 | subtype_getweakref(PyObject *obj, void *context) |
| 1795 | { |
| 1796 | PyObject **weaklistptr; |
| 1797 | PyObject *result; |
| 1798 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1799 | if (Py_TYPE(obj)->tp_weaklistoffset == 0) { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1800 | PyErr_SetString(PyExc_AttributeError, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1801 | "This object has no __weakref__"); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1802 | return NULL; |
| 1803 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1804 | assert(Py_TYPE(obj)->tp_weaklistoffset > 0); |
| 1805 | assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <= |
| 1806 | (size_t)(Py_TYPE(obj)->tp_basicsize)); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1807 | weaklistptr = (PyObject **) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1808 | ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1809 | if (*weaklistptr == NULL) |
| 1810 | result = Py_None; |
| 1811 | else |
| 1812 | result = *weaklistptr; |
| 1813 | Py_INCREF(result); |
| 1814 | return result; |
| 1815 | } |
| 1816 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1817 | /* Three variants on the subtype_getsets list. */ |
| 1818 | |
| 1819 | static PyGetSetDef subtype_getsets_full[] = { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1820 | {"__dict__", subtype_dict, subtype_setdict, |
Neal Norwitz | 858e34f | 2002-08-13 17:18:45 +0000 | [diff] [blame] | 1821 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1822 | {"__weakref__", subtype_getweakref, NULL, |
Neal Norwitz | 858e34f | 2002-08-13 17:18:45 +0000 | [diff] [blame] | 1823 | PyDoc_STR("list of weak references to the object (if defined)")}, |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1824 | {0} |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1825 | }; |
| 1826 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1827 | static PyGetSetDef subtype_getsets_dict_only[] = { |
| 1828 | {"__dict__", subtype_dict, subtype_setdict, |
| 1829 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 1830 | {0} |
| 1831 | }; |
| 1832 | |
| 1833 | static PyGetSetDef subtype_getsets_weakref_only[] = { |
| 1834 | {"__weakref__", subtype_getweakref, NULL, |
| 1835 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 1836 | {0} |
| 1837 | }; |
| 1838 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1839 | static int |
| 1840 | valid_identifier(PyObject *s) |
| 1841 | { |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1842 | if (!PyUnicode_Check(s)) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1843 | PyErr_Format(PyExc_TypeError, |
| 1844 | "__slots__ items must be strings, not '%.200s'", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1845 | Py_TYPE(s)->tp_name); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1846 | return 0; |
| 1847 | } |
Georg Brandl | f4780d0 | 2007-08-30 18:29:48 +0000 | [diff] [blame] | 1848 | if (!PyUnicode_IsIdentifier(s)) { |
| 1849 | PyErr_SetString(PyExc_TypeError, |
| 1850 | "__slots__ must be identifiers"); |
| 1851 | return 0; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1852 | } |
| 1853 | return 1; |
| 1854 | } |
| 1855 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1856 | /* Forward */ |
| 1857 | static int |
| 1858 | object_init(PyObject *self, PyObject *args, PyObject *kwds); |
| 1859 | |
| 1860 | static int |
| 1861 | type_init(PyObject *cls, PyObject *args, PyObject *kwds) |
| 1862 | { |
| 1863 | int res; |
| 1864 | |
| 1865 | assert(args != NULL && PyTuple_Check(args)); |
| 1866 | assert(kwds == NULL || PyDict_Check(kwds)); |
| 1867 | |
| 1868 | if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) { |
| 1869 | PyErr_SetString(PyExc_TypeError, |
| 1870 | "type.__init__() takes no keyword arguments"); |
| 1871 | return -1; |
| 1872 | } |
| 1873 | |
| 1874 | if (args != NULL && PyTuple_Check(args) && |
| 1875 | (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) { |
| 1876 | PyErr_SetString(PyExc_TypeError, |
| 1877 | "type.__init__() takes 1 or 3 arguments"); |
| 1878 | return -1; |
| 1879 | } |
| 1880 | |
| 1881 | /* Call object.__init__(self) now. */ |
| 1882 | /* XXX Could call super(type, cls).__init__() but what's the point? */ |
| 1883 | args = PyTuple_GetSlice(args, 0, 0); |
| 1884 | res = object_init(cls, args, NULL); |
| 1885 | Py_DECREF(args); |
| 1886 | return res; |
| 1887 | } |
| 1888 | |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1889 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1890 | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) |
| 1891 | { |
| 1892 | PyObject *name, *bases, *dict; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 1893 | static char *kwlist[] = {"name", "bases", "dict", 0}; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1894 | PyObject *slots, *tmp, *newslots; |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1895 | PyTypeObject *type, *base, *tmptype, *winner; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 1896 | PyHeapTypeObject *et; |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 1897 | PyMemberDef *mp; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1898 | Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1899 | int j, may_add_dict, may_add_weak; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1900 | |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1901 | assert(args != NULL && PyTuple_Check(args)); |
| 1902 | assert(kwds == NULL || PyDict_Check(kwds)); |
| 1903 | |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1904 | /* Special case: type(x) should return x->ob_type */ |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1905 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1906 | const Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 1907 | const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1908 | |
| 1909 | if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) { |
| 1910 | PyObject *x = PyTuple_GET_ITEM(args, 0); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1911 | Py_INCREF(Py_TYPE(x)); |
| 1912 | return (PyObject *) Py_TYPE(x); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | /* SF bug 475327 -- if that didn't trigger, we need 3 |
| 1916 | arguments. but PyArg_ParseTupleAndKeywords below may give |
| 1917 | a msg saying type() needs exactly 3. */ |
| 1918 | if (nargs + nkwds != 3) { |
| 1919 | PyErr_SetString(PyExc_TypeError, |
| 1920 | "type() takes 1 or 3 arguments"); |
| 1921 | return NULL; |
| 1922 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1925 | /* Check arguments: (name, bases, dict) */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1926 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "UO!O!:type", kwlist, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1927 | &name, |
| 1928 | &PyTuple_Type, &bases, |
| 1929 | &PyDict_Type, &dict)) |
| 1930 | return NULL; |
| 1931 | |
| 1932 | /* Determine the proper metatype to deal with this, |
| 1933 | and check for metatype conflicts while we're at it. |
| 1934 | Note that if some other metatype wins to contract, |
| 1935 | it's possible that its instances are not types. */ |
| 1936 | nbases = PyTuple_GET_SIZE(bases); |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1937 | winner = metatype; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1938 | for (i = 0; i < nbases; i++) { |
| 1939 | tmp = PyTuple_GET_ITEM(bases, i); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1940 | tmptype = Py_TYPE(tmp); |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1941 | if (PyType_IsSubtype(winner, tmptype)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1942 | continue; |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1943 | if (PyType_IsSubtype(tmptype, winner)) { |
| 1944 | winner = tmptype; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1945 | continue; |
| 1946 | } |
| 1947 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 636688d | 2003-04-23 12:07:22 +0000 | [diff] [blame] | 1948 | "metaclass conflict: " |
| 1949 | "the metaclass of a derived class " |
| 1950 | "must be a (non-strict) subclass " |
| 1951 | "of the metaclasses of all its bases"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1952 | return NULL; |
| 1953 | } |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 1954 | if (winner != metatype) { |
| 1955 | if (winner->tp_new != type_new) /* Pass it to the winner */ |
| 1956 | return winner->tp_new(winner, args, kwds); |
| 1957 | metatype = winner; |
| 1958 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1959 | |
| 1960 | /* Adjust for empty tuple bases */ |
| 1961 | if (nbases == 0) { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1962 | bases = PyTuple_Pack(1, &PyBaseObject_Type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1963 | if (bases == NULL) |
| 1964 | return NULL; |
| 1965 | nbases = 1; |
| 1966 | } |
| 1967 | else |
| 1968 | Py_INCREF(bases); |
| 1969 | |
| 1970 | /* XXX From here until type is allocated, "return NULL" leaks bases! */ |
| 1971 | |
| 1972 | /* Calculate best base, and check that all bases are type objects */ |
| 1973 | base = best_base(bases); |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 1974 | if (base == NULL) { |
| 1975 | Py_DECREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1976 | return NULL; |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 1977 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1978 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 1979 | PyErr_Format(PyExc_TypeError, |
| 1980 | "type '%.100s' is not an acceptable base type", |
| 1981 | base->tp_name); |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 1982 | Py_DECREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1983 | return NULL; |
| 1984 | } |
| 1985 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1986 | /* Check for a __slots__ sequence variable in dict, and count it */ |
| 1987 | slots = PyDict_GetItemString(dict, "__slots__"); |
| 1988 | nslots = 0; |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1989 | add_dict = 0; |
| 1990 | add_weak = 0; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1991 | may_add_dict = base->tp_dictoffset == 0; |
| 1992 | may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0; |
| 1993 | if (slots == NULL) { |
| 1994 | if (may_add_dict) { |
| 1995 | add_dict++; |
| 1996 | } |
| 1997 | if (may_add_weak) { |
| 1998 | add_weak++; |
| 1999 | } |
| 2000 | } |
| 2001 | else { |
| 2002 | /* Have slots */ |
| 2003 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2004 | /* Make it into a tuple */ |
Neal Norwitz | 80e7f27 | 2007-08-26 06:45:23 +0000 | [diff] [blame] | 2005 | if (PyUnicode_Check(slots)) |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2006 | slots = PyTuple_Pack(1, slots); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2007 | else |
| 2008 | slots = PySequence_Tuple(slots); |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2009 | if (slots == NULL) { |
| 2010 | Py_DECREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2011 | return NULL; |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2012 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2013 | assert(PyTuple_Check(slots)); |
| 2014 | |
| 2015 | /* Are slots allowed? */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2016 | nslots = PyTuple_GET_SIZE(slots); |
Jeremy Hylton | 1c7a0ea | 2003-07-16 16:08:23 +0000 | [diff] [blame] | 2017 | if (nslots > 0 && base->tp_itemsize != 0) { |
Guido van Rossum | c414187 | 2001-08-30 04:43:35 +0000 | [diff] [blame] | 2018 | PyErr_Format(PyExc_TypeError, |
| 2019 | "nonempty __slots__ " |
| 2020 | "not supported for subtype of '%s'", |
| 2021 | base->tp_name); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2022 | bad_slots: |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2023 | Py_DECREF(bases); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2024 | Py_DECREF(slots); |
Guido van Rossum | c414187 | 2001-08-30 04:43:35 +0000 | [diff] [blame] | 2025 | return NULL; |
| 2026 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2027 | |
| 2028 | /* Check for valid slot names and two special cases */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2029 | for (i = 0; i < nslots; i++) { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2030 | PyObject *tmp = PyTuple_GET_ITEM(slots, i); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2031 | if (!valid_identifier(tmp)) |
| 2032 | goto bad_slots; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2033 | assert(PyUnicode_Check(tmp)); |
| 2034 | if (PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2035 | if (!may_add_dict || add_dict) { |
| 2036 | PyErr_SetString(PyExc_TypeError, |
| 2037 | "__dict__ slot disallowed: " |
| 2038 | "we already got one"); |
| 2039 | goto bad_slots; |
| 2040 | } |
| 2041 | add_dict++; |
| 2042 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2043 | if (PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0) { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2044 | if (!may_add_weak || add_weak) { |
| 2045 | PyErr_SetString(PyExc_TypeError, |
| 2046 | "__weakref__ slot disallowed: " |
| 2047 | "either we already got one, " |
| 2048 | "or __itemsize__ != 0"); |
| 2049 | goto bad_slots; |
| 2050 | } |
| 2051 | add_weak++; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2052 | } |
| 2053 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2054 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2055 | /* Copy slots into a list, mangle names and sort them. |
| 2056 | Sorted names are needed for __class__ assignment. |
| 2057 | Convert them back to tuple at the end. |
| 2058 | */ |
| 2059 | newslots = PyList_New(nslots - add_dict - add_weak); |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2060 | if (newslots == NULL) |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2061 | goto bad_slots; |
| 2062 | for (i = j = 0; i < nslots; i++) { |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2063 | tmp = PyTuple_GET_ITEM(slots, i); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2064 | if ((add_dict && |
| 2065 | PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) || |
| 2066 | (add_weak && |
| 2067 | PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0)) |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2068 | continue; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2069 | tmp =_Py_Mangle(name, tmp); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2070 | if (!tmp) |
| 2071 | goto bad_slots; |
| 2072 | PyList_SET_ITEM(newslots, j, tmp); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2073 | j++; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2074 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2075 | assert(j == nslots - add_dict - add_weak); |
| 2076 | nslots = j; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2077 | Py_DECREF(slots); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2078 | if (PyList_Sort(newslots) == -1) { |
| 2079 | Py_DECREF(bases); |
| 2080 | Py_DECREF(newslots); |
| 2081 | return NULL; |
| 2082 | } |
| 2083 | slots = PyList_AsTuple(newslots); |
| 2084 | Py_DECREF(newslots); |
| 2085 | if (slots == NULL) { |
| 2086 | Py_DECREF(bases); |
| 2087 | return NULL; |
| 2088 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2089 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2090 | /* Secondary bases may provide weakrefs or dict */ |
| 2091 | if (nbases > 1 && |
| 2092 | ((may_add_dict && !add_dict) || |
| 2093 | (may_add_weak && !add_weak))) { |
| 2094 | for (i = 0; i < nbases; i++) { |
| 2095 | tmp = PyTuple_GET_ITEM(bases, i); |
| 2096 | if (tmp == (PyObject *)base) |
| 2097 | continue; /* Skip primary base */ |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2098 | assert(PyType_Check(tmp)); |
| 2099 | tmptype = (PyTypeObject *)tmp; |
| 2100 | if (may_add_dict && !add_dict && |
| 2101 | tmptype->tp_dictoffset != 0) |
| 2102 | add_dict++; |
| 2103 | if (may_add_weak && !add_weak && |
| 2104 | tmptype->tp_weaklistoffset != 0) |
| 2105 | add_weak++; |
| 2106 | if (may_add_dict && !add_dict) |
| 2107 | continue; |
| 2108 | if (may_add_weak && !add_weak) |
| 2109 | continue; |
| 2110 | /* Nothing more to check */ |
| 2111 | break; |
| 2112 | } |
| 2113 | } |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 2114 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2115 | |
| 2116 | /* XXX From here until type is safely allocated, |
| 2117 | "return NULL" may leak slots! */ |
| 2118 | |
| 2119 | /* Allocate the type object */ |
| 2120 | type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2121 | if (type == NULL) { |
| 2122 | Py_XDECREF(slots); |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2123 | Py_DECREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2124 | return NULL; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2125 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2126 | |
| 2127 | /* Keep name and slots alive in the extended type object */ |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2128 | et = (PyHeapTypeObject *)type; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2129 | Py_INCREF(name); |
Georg Brandl | c255c7b | 2006-02-20 22:27:28 +0000 | [diff] [blame] | 2130 | et->ht_name = name; |
| 2131 | et->ht_slots = slots; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2132 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2133 | /* Initialize tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2134 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | |
| 2135 | Py_TPFLAGS_BASETYPE; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2136 | if (base->tp_flags & Py_TPFLAGS_HAVE_GC) |
| 2137 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2138 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2139 | /* Initialize essential fields */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2140 | type->tp_as_number = &et->as_number; |
| 2141 | type->tp_as_sequence = &et->as_sequence; |
| 2142 | type->tp_as_mapping = &et->as_mapping; |
| 2143 | type->tp_as_buffer = &et->as_buffer; |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 2144 | type->tp_name = _PyUnicode_AsString(name); |
Neal Norwitz | 80e7f27 | 2007-08-26 06:45:23 +0000 | [diff] [blame] | 2145 | if (!type->tp_name) { |
| 2146 | Py_DECREF(type); |
| 2147 | return NULL; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2148 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2149 | |
| 2150 | /* Set tp_base and tp_bases */ |
| 2151 | type->tp_bases = bases; |
| 2152 | Py_INCREF(base); |
| 2153 | type->tp_base = base; |
| 2154 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2155 | /* Initialize tp_dict from passed-in dict */ |
| 2156 | type->tp_dict = dict = PyDict_Copy(dict); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2157 | if (dict == NULL) { |
| 2158 | Py_DECREF(type); |
| 2159 | return NULL; |
| 2160 | } |
| 2161 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 2162 | /* Set __module__ in the dict */ |
| 2163 | if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 2164 | tmp = PyEval_GetGlobals(); |
| 2165 | if (tmp != NULL) { |
| 2166 | tmp = PyDict_GetItemString(tmp, "__name__"); |
| 2167 | if (tmp != NULL) { |
| 2168 | if (PyDict_SetItemString(dict, "__module__", |
| 2169 | tmp) < 0) |
| 2170 | return NULL; |
| 2171 | } |
| 2172 | } |
| 2173 | } |
| 2174 | |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2175 | /* 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] | 2176 | and is a string. The __doc__ accessor will first look for tp_doc; |
| 2177 | if that fails, it will still look into __dict__. |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2178 | */ |
| 2179 | { |
| 2180 | PyObject *doc = PyDict_GetItemString(dict, "__doc__"); |
Neal Norwitz | 6ea45d3 | 2007-08-26 04:19:43 +0000 | [diff] [blame] | 2181 | if (doc != NULL && PyUnicode_Check(doc)) { |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 2182 | Py_ssize_t len; |
| 2183 | char *doc_str; |
Neal Norwitz | 6ea45d3 | 2007-08-26 04:19:43 +0000 | [diff] [blame] | 2184 | char *tp_doc; |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 2185 | |
Alexandre Vassalotti | 394996b | 2009-06-04 00:43:04 +0000 | [diff] [blame] | 2186 | doc_str = _PyUnicode_AsString(doc); |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 2187 | if (doc_str == NULL) { |
Neal Norwitz | 6ea45d3 | 2007-08-26 04:19:43 +0000 | [diff] [blame] | 2188 | Py_DECREF(type); |
| 2189 | return NULL; |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2190 | } |
Alexandre Vassalotti | 394996b | 2009-06-04 00:43:04 +0000 | [diff] [blame] | 2191 | /* Silently truncate the docstring if it contains null bytes. */ |
| 2192 | len = strlen(doc_str); |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 2193 | tp_doc = (char *)PyObject_MALLOC(len + 1); |
Neal Norwitz | 6ea45d3 | 2007-08-26 04:19:43 +0000 | [diff] [blame] | 2194 | if (tp_doc == NULL) { |
| 2195 | Py_DECREF(type); |
| 2196 | return NULL; |
Neal Norwitz | a369c5a | 2007-08-25 07:41:59 +0000 | [diff] [blame] | 2197 | } |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 2198 | memcpy(tp_doc, doc_str, len + 1); |
Neal Norwitz | 6ea45d3 | 2007-08-26 04:19:43 +0000 | [diff] [blame] | 2199 | type->tp_doc = tp_doc; |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2200 | } |
| 2201 | } |
| 2202 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2203 | /* Special-case __new__: if it's a plain function, |
| 2204 | make it a static function */ |
| 2205 | tmp = PyDict_GetItemString(dict, "__new__"); |
| 2206 | if (tmp != NULL && PyFunction_Check(tmp)) { |
| 2207 | tmp = PyStaticMethod_New(tmp); |
| 2208 | if (tmp == NULL) { |
| 2209 | Py_DECREF(type); |
| 2210 | return NULL; |
| 2211 | } |
| 2212 | PyDict_SetItemString(dict, "__new__", tmp); |
| 2213 | Py_DECREF(tmp); |
| 2214 | } |
| 2215 | |
| 2216 | /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2217 | mp = PyHeapType_GET_MEMBERS(et); |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 2218 | slotoffset = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2219 | if (slots != NULL) { |
| 2220 | for (i = 0; i < nslots; i++, mp++) { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 2221 | mp->name = _PyUnicode_AsString( |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2222 | PyTuple_GET_ITEM(slots, i)); |
Guido van Rossum | 64b206c | 2001-12-04 17:13:22 +0000 | [diff] [blame] | 2223 | mp->type = T_OBJECT_EX; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2224 | mp->offset = slotoffset; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2225 | |
| 2226 | /* __dict__ and __weakref__ are already filtered out */ |
| 2227 | assert(strcmp(mp->name, "__dict__") != 0); |
| 2228 | assert(strcmp(mp->name, "__weakref__") != 0); |
| 2229 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2230 | slotoffset += sizeof(PyObject *); |
| 2231 | } |
| 2232 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2233 | if (add_dict) { |
| 2234 | if (base->tp_itemsize) |
| 2235 | type->tp_dictoffset = -(long)sizeof(PyObject *); |
| 2236 | else |
| 2237 | type->tp_dictoffset = slotoffset; |
| 2238 | slotoffset += sizeof(PyObject *); |
| 2239 | } |
| 2240 | if (add_weak) { |
| 2241 | assert(!base->tp_itemsize); |
| 2242 | type->tp_weaklistoffset = slotoffset; |
| 2243 | slotoffset += sizeof(PyObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2244 | } |
| 2245 | type->tp_basicsize = slotoffset; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 2246 | type->tp_itemsize = base->tp_itemsize; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2247 | type->tp_members = PyHeapType_GET_MEMBERS(et); |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2248 | |
| 2249 | if (type->tp_weaklistoffset && type->tp_dictoffset) |
| 2250 | type->tp_getset = subtype_getsets_full; |
| 2251 | else if (type->tp_weaklistoffset && !type->tp_dictoffset) |
| 2252 | type->tp_getset = subtype_getsets_weakref_only; |
| 2253 | else if (!type->tp_weaklistoffset && type->tp_dictoffset) |
| 2254 | type->tp_getset = subtype_getsets_dict_only; |
| 2255 | else |
| 2256 | type->tp_getset = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2257 | |
| 2258 | /* Special case some slots */ |
| 2259 | if (type->tp_dictoffset != 0 || nslots > 0) { |
| 2260 | if (base->tp_getattr == NULL && base->tp_getattro == NULL) |
| 2261 | type->tp_getattro = PyObject_GenericGetAttr; |
| 2262 | if (base->tp_setattr == NULL && base->tp_setattro == NULL) |
| 2263 | type->tp_setattro = PyObject_GenericSetAttr; |
| 2264 | } |
| 2265 | type->tp_dealloc = subtype_dealloc; |
| 2266 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 2267 | /* Enable GC unless there are really no instance variables possible */ |
| 2268 | if (!(type->tp_basicsize == sizeof(PyObject) && |
| 2269 | type->tp_itemsize == 0)) |
| 2270 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
| 2271 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2272 | /* Always override allocation strategy to use regular heap */ |
| 2273 | type->tp_alloc = PyType_GenericAlloc; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2274 | if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 2275 | type->tp_free = PyObject_GC_Del; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 2276 | type->tp_traverse = subtype_traverse; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 2277 | type->tp_clear = subtype_clear; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2278 | } |
| 2279 | else |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 2280 | type->tp_free = PyObject_Del; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2281 | |
| 2282 | /* Initialize the rest */ |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 2283 | if (PyType_Ready(type) < 0) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2284 | Py_DECREF(type); |
| 2285 | return NULL; |
| 2286 | } |
| 2287 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 2288 | /* Put the proper slots in place */ |
| 2289 | fixup_slot_dispatchers(type); |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 2290 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2291 | return (PyObject *)type; |
| 2292 | } |
| 2293 | |
| 2294 | /* Internal API to look for a name through the MRO. |
| 2295 | This returns a borrowed reference, and doesn't set an exception! */ |
| 2296 | PyObject * |
| 2297 | _PyType_Lookup(PyTypeObject *type, PyObject *name) |
| 2298 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2299 | Py_ssize_t i, n; |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 2300 | PyObject *mro, *res, *base, *dict; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 2301 | unsigned int h; |
| 2302 | |
| 2303 | if (MCACHE_CACHEABLE_NAME(name) && |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 2304 | PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) { |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 2305 | /* fast path */ |
| 2306 | h = MCACHE_HASH_METHOD(type, name); |
| 2307 | if (method_cache[h].version == type->tp_version_tag && |
| 2308 | method_cache[h].name == name) |
| 2309 | return method_cache[h].value; |
| 2310 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2311 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2312 | /* Look in tp_dict of types in MRO */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2313 | mro = type->tp_mro; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 2314 | |
| 2315 | /* If mro is NULL, the type is either not yet initialized |
| 2316 | by PyType_Ready(), or already cleared by type_clear(). |
| 2317 | Either way the safest thing to do is to return NULL. */ |
| 2318 | if (mro == NULL) |
| 2319 | return NULL; |
| 2320 | |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 2321 | res = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2322 | assert(PyTuple_Check(mro)); |
| 2323 | n = PyTuple_GET_SIZE(mro); |
| 2324 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 2325 | base = PyTuple_GET_ITEM(mro, i); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 2326 | assert(PyType_Check(base)); |
| 2327 | dict = ((PyTypeObject *)base)->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2328 | assert(dict && PyDict_Check(dict)); |
| 2329 | res = PyDict_GetItem(dict, name); |
| 2330 | if (res != NULL) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 2331 | break; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2332 | } |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 2333 | |
| 2334 | if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) { |
| 2335 | h = MCACHE_HASH_METHOD(type, name); |
| 2336 | method_cache[h].version = type->tp_version_tag; |
| 2337 | method_cache[h].value = res; /* borrowed */ |
| 2338 | Py_INCREF(name); |
| 2339 | Py_DECREF(method_cache[h].name); |
| 2340 | method_cache[h].name = name; |
| 2341 | } |
| 2342 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
| 2345 | /* This is similar to PyObject_GenericGetAttr(), |
| 2346 | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ |
| 2347 | static PyObject * |
| 2348 | type_getattro(PyTypeObject *type, PyObject *name) |
| 2349 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2350 | PyTypeObject *metatype = Py_TYPE(type); |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2351 | PyObject *meta_attribute, *attribute; |
| 2352 | descrgetfunc meta_get; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2353 | |
| 2354 | /* Initialize this type (we'll assume the metatype is initialized) */ |
| 2355 | if (type->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 2356 | if (PyType_Ready(type) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2357 | return NULL; |
| 2358 | } |
| 2359 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2360 | /* No readable descriptor found yet */ |
| 2361 | meta_get = NULL; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2362 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2363 | /* Look for the attribute in the metatype */ |
| 2364 | meta_attribute = _PyType_Lookup(metatype, name); |
| 2365 | |
| 2366 | if (meta_attribute != NULL) { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2367 | meta_get = Py_TYPE(meta_attribute)->tp_descr_get; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2368 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2369 | if (meta_get != NULL && PyDescr_IsData(meta_attribute)) { |
| 2370 | /* Data descriptors implement tp_descr_set to intercept |
| 2371 | * writes. Assume the attribute is not overridden in |
| 2372 | * type's tp_dict (and bases): call the descriptor now. |
| 2373 | */ |
| 2374 | return meta_get(meta_attribute, (PyObject *)type, |
| 2375 | (PyObject *)metatype); |
| 2376 | } |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2377 | Py_INCREF(meta_attribute); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2378 | } |
| 2379 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2380 | /* No data descriptor found on metatype. Look in tp_dict of this |
| 2381 | * type and its bases */ |
| 2382 | attribute = _PyType_Lookup(type, name); |
| 2383 | if (attribute != NULL) { |
| 2384 | /* Implement descriptor functionality, if any */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2385 | descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get; |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2386 | |
| 2387 | Py_XDECREF(meta_attribute); |
| 2388 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2389 | if (local_get != NULL) { |
| 2390 | /* NULL 2nd argument indicates the descriptor was |
| 2391 | * found on the target object itself (or a base) */ |
| 2392 | return local_get(attribute, (PyObject *)NULL, |
| 2393 | (PyObject *)type); |
| 2394 | } |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2395 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2396 | Py_INCREF(attribute); |
| 2397 | return attribute; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2400 | /* No attribute found in local __dict__ (or bases): use the |
| 2401 | * descriptor from the metatype, if any */ |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2402 | if (meta_get != NULL) { |
| 2403 | PyObject *res; |
| 2404 | res = meta_get(meta_attribute, (PyObject *)type, |
| 2405 | (PyObject *)metatype); |
| 2406 | Py_DECREF(meta_attribute); |
| 2407 | return res; |
| 2408 | } |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2409 | |
| 2410 | /* If an ordinary attribute was found on the metatype, return it now */ |
| 2411 | if (meta_attribute != NULL) { |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2412 | return meta_attribute; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
| 2415 | /* Give up */ |
| 2416 | PyErr_Format(PyExc_AttributeError, |
Walter Dörwald | 7516360 | 2007-06-11 15:47:13 +0000 | [diff] [blame] | 2417 | "type object '%.50s' has no attribute '%U'", |
| 2418 | type->tp_name, name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2419 | return NULL; |
| 2420 | } |
| 2421 | |
| 2422 | static int |
| 2423 | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) |
| 2424 | { |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 2425 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 2426 | PyErr_Format( |
| 2427 | PyExc_TypeError, |
| 2428 | "can't set attributes of built-in/extension type '%s'", |
| 2429 | type->tp_name); |
| 2430 | return -1; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 2431 | } |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 2432 | if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0) |
| 2433 | return -1; |
| 2434 | return update_slot(type, name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
| 2437 | static void |
| 2438 | type_dealloc(PyTypeObject *type) |
| 2439 | { |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2440 | PyHeapTypeObject *et; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2441 | |
| 2442 | /* Assert this is a heap-allocated type object */ |
| 2443 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2444 | _PyObject_GC_UNTRACK(type); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2445 | PyObject_ClearWeakRefs((PyObject *)type); |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2446 | et = (PyHeapTypeObject *)type; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2447 | Py_XDECREF(type->tp_base); |
| 2448 | Py_XDECREF(type->tp_dict); |
| 2449 | Py_XDECREF(type->tp_bases); |
| 2450 | Py_XDECREF(type->tp_mro); |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2451 | Py_XDECREF(type->tp_cache); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2452 | Py_XDECREF(type->tp_subclasses); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2453 | /* A type's tp_doc is heap allocated, unlike the tp_doc slots |
| 2454 | * of most other objects. It's okay to cast it to char *. |
| 2455 | */ |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 2456 | PyObject_Free((char *)type->tp_doc); |
Georg Brandl | c255c7b | 2006-02-20 22:27:28 +0000 | [diff] [blame] | 2457 | Py_XDECREF(et->ht_name); |
| 2458 | Py_XDECREF(et->ht_slots); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2459 | Py_TYPE(type)->tp_free((PyObject *)type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2462 | static PyObject * |
| 2463 | type_subclasses(PyTypeObject *type, PyObject *args_ignored) |
| 2464 | { |
| 2465 | PyObject *list, *raw, *ref; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2466 | Py_ssize_t i, n; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2467 | |
| 2468 | list = PyList_New(0); |
| 2469 | if (list == NULL) |
| 2470 | return NULL; |
| 2471 | raw = type->tp_subclasses; |
| 2472 | if (raw == NULL) |
| 2473 | return list; |
| 2474 | assert(PyList_Check(raw)); |
| 2475 | n = PyList_GET_SIZE(raw); |
| 2476 | for (i = 0; i < n; i++) { |
| 2477 | ref = PyList_GET_ITEM(raw, i); |
Tim Peters | 4438338 | 2001-10-08 16:49:26 +0000 | [diff] [blame] | 2478 | assert(PyWeakref_CheckRef(ref)); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2479 | ref = PyWeakref_GET_OBJECT(ref); |
| 2480 | if (ref != Py_None) { |
| 2481 | if (PyList_Append(list, ref) < 0) { |
| 2482 | Py_DECREF(list); |
| 2483 | return NULL; |
| 2484 | } |
| 2485 | } |
| 2486 | } |
| 2487 | return list; |
| 2488 | } |
| 2489 | |
Guido van Rossum | 4737482 | 2007-08-02 16:48:17 +0000 | [diff] [blame] | 2490 | static PyObject * |
| 2491 | type_prepare(PyObject *self, PyObject *args, PyObject *kwds) |
| 2492 | { |
| 2493 | return PyDict_New(); |
| 2494 | } |
| 2495 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2496 | static PyMethodDef type_methods[] = { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2497 | {"mro", (PyCFunction)mro_external, METH_NOARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 2498 | PyDoc_STR("mro() -> list\nreturn a type's method resolution order")}, |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2499 | {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 2500 | PyDoc_STR("__subclasses__() -> list of immediate subclasses")}, |
Guido van Rossum | 4737482 | 2007-08-02 16:48:17 +0000 | [diff] [blame] | 2501 | {"__prepare__", (PyCFunction)type_prepare, |
| 2502 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 2503 | PyDoc_STR("__prepare__() -> dict\n" |
| 2504 | "used to create the namespace for the class statement")}, |
Benjamin Peterson | 88fe5f9 | 2009-05-16 21:55:24 +0000 | [diff] [blame] | 2505 | {"__instancecheck__", type___instancecheck__, METH_O, |
| 2506 | PyDoc_STR("__instancecheck__() -> check if an object is an instance")}, |
| 2507 | {"__subclasscheck__", type___subclasscheck__, METH_O, |
| 2508 | PyDoc_STR("__subclasschck__ -> check if an class is a subclass")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2509 | {0} |
| 2510 | }; |
| 2511 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2512 | PyDoc_STRVAR(type_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2513 | "type(object) -> the object's type\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2514 | "type(name, bases, dict) -> a new type"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2515 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2516 | static int |
| 2517 | type_traverse(PyTypeObject *type, visitproc visit, void *arg) |
| 2518 | { |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2519 | /* Because of type_is_gc(), the collector only calls this |
| 2520 | for heaptypes. */ |
| 2521 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2522 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2523 | Py_VISIT(type->tp_dict); |
| 2524 | Py_VISIT(type->tp_cache); |
| 2525 | Py_VISIT(type->tp_mro); |
| 2526 | Py_VISIT(type->tp_bases); |
| 2527 | Py_VISIT(type->tp_base); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2528 | |
| 2529 | /* There's no need to visit type->tp_subclasses or |
Georg Brandl | c255c7b | 2006-02-20 22:27:28 +0000 | [diff] [blame] | 2530 | ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2531 | in cycles; tp_subclasses is a list of weak references, |
| 2532 | and slots is a tuple of strings. */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2533 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2534 | return 0; |
| 2535 | } |
| 2536 | |
| 2537 | static int |
| 2538 | type_clear(PyTypeObject *type) |
| 2539 | { |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2540 | /* Because of type_is_gc(), the collector only calls this |
| 2541 | for heaptypes. */ |
| 2542 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2543 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2544 | /* The only field we need to clear is tp_mro, which is part of a |
| 2545 | hard cycle (its first element is the class itself) that won't |
| 2546 | be broken otherwise (it's a tuple and tuples don't have a |
| 2547 | tp_clear handler). None of the other fields need to be |
| 2548 | cleared, and here's why: |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2549 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2550 | tp_dict: |
| 2551 | It is a dict, so the collector will call its tp_clear. |
| 2552 | |
| 2553 | tp_cache: |
| 2554 | Not used; if it were, it would be a dict. |
| 2555 | |
| 2556 | tp_bases, tp_base: |
| 2557 | If these are involved in a cycle, there must be at least |
| 2558 | one other, mutable object in the cycle, e.g. a base |
| 2559 | class's dict; the cycle will be broken that way. |
| 2560 | |
| 2561 | tp_subclasses: |
| 2562 | A list of weak references can't be part of a cycle; and |
| 2563 | lists have their own tp_clear. |
| 2564 | |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2565 | slots (in PyHeapTypeObject): |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2566 | A tuple of strings can't be part of a cycle. |
| 2567 | */ |
| 2568 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2569 | Py_CLEAR(type->tp_mro); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2570 | |
| 2571 | return 0; |
| 2572 | } |
| 2573 | |
| 2574 | static int |
| 2575 | type_is_gc(PyTypeObject *type) |
| 2576 | { |
| 2577 | return type->tp_flags & Py_TPFLAGS_HEAPTYPE; |
| 2578 | } |
| 2579 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2580 | PyTypeObject PyType_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2581 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2582 | "type", /* tp_name */ |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2583 | sizeof(PyHeapTypeObject), /* tp_basicsize */ |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 2584 | sizeof(PyMemberDef), /* tp_itemsize */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2585 | (destructor)type_dealloc, /* tp_dealloc */ |
| 2586 | 0, /* tp_print */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2587 | 0, /* tp_getattr */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2588 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 2589 | 0, /* tp_reserved */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2590 | (reprfunc)type_repr, /* tp_repr */ |
| 2591 | 0, /* tp_as_number */ |
| 2592 | 0, /* tp_as_sequence */ |
| 2593 | 0, /* tp_as_mapping */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2594 | 0, /* tp_hash */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2595 | (ternaryfunc)type_call, /* tp_call */ |
| 2596 | 0, /* tp_str */ |
| 2597 | (getattrofunc)type_getattro, /* tp_getattro */ |
| 2598 | (setattrofunc)type_setattro, /* tp_setattro */ |
| 2599 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2600 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 2601 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2602 | type_doc, /* tp_doc */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2603 | (traverseproc)type_traverse, /* tp_traverse */ |
| 2604 | (inquiry)type_clear, /* tp_clear */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2605 | 0, /* tp_richcompare */ |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2606 | offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2607 | 0, /* tp_iter */ |
| 2608 | 0, /* tp_iternext */ |
| 2609 | type_methods, /* tp_methods */ |
| 2610 | type_members, /* tp_members */ |
| 2611 | type_getsets, /* tp_getset */ |
| 2612 | 0, /* tp_base */ |
| 2613 | 0, /* tp_dict */ |
| 2614 | 0, /* tp_descr_get */ |
| 2615 | 0, /* tp_descr_set */ |
| 2616 | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2617 | type_init, /* tp_init */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2618 | 0, /* tp_alloc */ |
| 2619 | type_new, /* tp_new */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2620 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2621 | (inquiry)type_is_gc, /* tp_is_gc */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2622 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2623 | |
| 2624 | |
| 2625 | /* The base type of all types (eventually)... except itself. */ |
| 2626 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2627 | /* You may wonder why object.__new__() only complains about arguments |
| 2628 | when object.__init__() is not overridden, and vice versa. |
| 2629 | |
| 2630 | Consider the use cases: |
| 2631 | |
| 2632 | 1. When neither is overridden, we want to hear complaints about |
| 2633 | excess (i.e., any) arguments, since their presence could |
| 2634 | indicate there's a bug. |
| 2635 | |
| 2636 | 2. When defining an Immutable type, we are likely to override only |
| 2637 | __new__(), since __init__() is called too late to initialize an |
| 2638 | Immutable object. Since __new__() defines the signature for the |
| 2639 | type, it would be a pain to have to override __init__() just to |
| 2640 | stop it from complaining about excess arguments. |
| 2641 | |
| 2642 | 3. When defining a Mutable type, we are likely to override only |
| 2643 | __init__(). So here the converse reasoning applies: we don't |
| 2644 | want to have to override __new__() just to stop it from |
| 2645 | complaining. |
| 2646 | |
| 2647 | 4. When __init__() is overridden, and the subclass __init__() calls |
| 2648 | object.__init__(), the latter should complain about excess |
| 2649 | arguments; ditto for __new__(). |
| 2650 | |
| 2651 | Use cases 2 and 3 make it unattractive to unconditionally check for |
| 2652 | excess arguments. The best solution that addresses all four use |
| 2653 | cases is as follows: __init__() complains about excess arguments |
| 2654 | unless __new__() is overridden and __init__() is not overridden |
| 2655 | (IOW, if __init__() is overridden or __new__() is not overridden); |
| 2656 | symmetrically, __new__() complains about excess arguments unless |
| 2657 | __init__() is overridden and __new__() is not overridden |
| 2658 | (IOW, if __new__() is overridden or __init__() is not overridden). |
| 2659 | |
| 2660 | However, for backwards compatibility, this breaks too much code. |
| 2661 | Therefore, in 2.6, we'll *warn* about excess arguments when both |
| 2662 | methods are overridden; for all other cases we'll use the above |
| 2663 | rules. |
| 2664 | |
| 2665 | */ |
| 2666 | |
| 2667 | /* Forward */ |
| 2668 | static PyObject * |
| 2669 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 2670 | |
| 2671 | static int |
| 2672 | excess_args(PyObject *args, PyObject *kwds) |
| 2673 | { |
| 2674 | return PyTuple_GET_SIZE(args) || |
| 2675 | (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)); |
| 2676 | } |
| 2677 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2678 | static int |
| 2679 | object_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2680 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2681 | int err = 0; |
| 2682 | if (excess_args(args, kwds)) { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2683 | PyTypeObject *type = Py_TYPE(self); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2684 | if (type->tp_init != object_init && |
| 2685 | type->tp_new != object_new) |
| 2686 | { |
| 2687 | err = PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2688 | "object.__init__() takes no parameters", |
| 2689 | 1); |
| 2690 | } |
| 2691 | else if (type->tp_init != object_init || |
| 2692 | type->tp_new == object_new) |
| 2693 | { |
| 2694 | PyErr_SetString(PyExc_TypeError, |
| 2695 | "object.__init__() takes no parameters"); |
| 2696 | err = -1; |
| 2697 | } |
| 2698 | } |
| 2699 | return err; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2702 | static PyObject * |
| 2703 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2704 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2705 | int err = 0; |
| 2706 | if (excess_args(args, kwds)) { |
| 2707 | if (type->tp_new != object_new && |
| 2708 | type->tp_init != object_init) |
| 2709 | { |
| 2710 | err = PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2711 | "object.__new__() takes no parameters", |
| 2712 | 1); |
| 2713 | } |
| 2714 | else if (type->tp_new != object_new || |
| 2715 | type->tp_init == object_init) |
| 2716 | { |
| 2717 | PyErr_SetString(PyExc_TypeError, |
| 2718 | "object.__new__() takes no parameters"); |
| 2719 | err = -1; |
| 2720 | } |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2721 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2722 | if (err < 0) |
| 2723 | return NULL; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 2724 | |
| 2725 | if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) { |
| 2726 | static PyObject *comma = NULL; |
| 2727 | PyObject *abstract_methods = NULL; |
| 2728 | PyObject *builtins; |
| 2729 | PyObject *sorted; |
| 2730 | PyObject *sorted_methods = NULL; |
| 2731 | PyObject *joined = NULL; |
| 2732 | |
| 2733 | /* Compute ", ".join(sorted(type.__abstractmethods__)) |
| 2734 | into joined. */ |
| 2735 | abstract_methods = type_abstractmethods(type, NULL); |
| 2736 | if (abstract_methods == NULL) |
| 2737 | goto error; |
| 2738 | builtins = PyEval_GetBuiltins(); |
| 2739 | if (builtins == NULL) |
| 2740 | goto error; |
| 2741 | sorted = PyDict_GetItemString(builtins, "sorted"); |
| 2742 | if (sorted == NULL) |
| 2743 | goto error; |
| 2744 | sorted_methods = PyObject_CallFunctionObjArgs(sorted, |
| 2745 | abstract_methods, |
| 2746 | NULL); |
| 2747 | if (sorted_methods == NULL) |
| 2748 | goto error; |
| 2749 | if (comma == NULL) { |
| 2750 | comma = PyUnicode_InternFromString(", "); |
| 2751 | if (comma == NULL) |
| 2752 | goto error; |
| 2753 | } |
| 2754 | joined = PyObject_CallMethod(comma, "join", |
| 2755 | "O", sorted_methods); |
| 2756 | if (joined == NULL) |
| 2757 | goto error; |
| 2758 | |
| 2759 | PyErr_Format(PyExc_TypeError, |
| 2760 | "Can't instantiate abstract class %s " |
| 2761 | "with abstract methods %U", |
| 2762 | type->tp_name, |
| 2763 | joined); |
| 2764 | error: |
| 2765 | Py_XDECREF(joined); |
| 2766 | Py_XDECREF(sorted_methods); |
| 2767 | Py_XDECREF(abstract_methods); |
| 2768 | return NULL; |
| 2769 | } |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2770 | return type->tp_alloc(type, 0); |
| 2771 | } |
| 2772 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2773 | static void |
| 2774 | object_dealloc(PyObject *self) |
| 2775 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2776 | Py_TYPE(self)->tp_free(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2779 | static PyObject * |
| 2780 | object_repr(PyObject *self) |
| 2781 | { |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2782 | PyTypeObject *type; |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2783 | PyObject *mod, *name, *rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2784 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2785 | type = Py_TYPE(self); |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2786 | mod = type_module(type, NULL); |
| 2787 | if (mod == NULL) |
| 2788 | PyErr_Clear(); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2789 | else if (!PyUnicode_Check(mod)) { |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2790 | Py_DECREF(mod); |
| 2791 | mod = NULL; |
| 2792 | } |
| 2793 | name = type_name(type, NULL); |
| 2794 | if (name == NULL) |
| 2795 | return NULL; |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 2796 | if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins")) |
Walter Dörwald | 4dbd01b | 2007-06-11 14:03:45 +0000 | [diff] [blame] | 2797 | rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self); |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2798 | else |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 2799 | rtn = PyUnicode_FromFormat("<%s object at %p>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2800 | type->tp_name, self); |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2801 | Py_XDECREF(mod); |
| 2802 | Py_DECREF(name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2803 | return rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2806 | static PyObject * |
| 2807 | object_str(PyObject *self) |
| 2808 | { |
| 2809 | unaryfunc f; |
| 2810 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2811 | f = Py_TYPE(self)->tp_repr; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2812 | if (f == NULL) |
| 2813 | f = object_repr; |
| 2814 | return f(self); |
| 2815 | } |
| 2816 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2817 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2818 | object_richcompare(PyObject *self, PyObject *other, int op) |
| 2819 | { |
| 2820 | PyObject *res; |
| 2821 | |
| 2822 | switch (op) { |
| 2823 | |
| 2824 | case Py_EQ: |
Guido van Rossum | ab078dd | 2008-01-06 00:09:11 +0000 | [diff] [blame] | 2825 | /* Return NotImplemented instead of False, so if two |
| 2826 | objects are compared, both get a chance at the |
| 2827 | comparison. See issue #1393. */ |
| 2828 | res = (self == other) ? Py_True : Py_NotImplemented; |
Guido van Rossum | 6b18a5b | 2007-03-29 20:49:57 +0000 | [diff] [blame] | 2829 | Py_INCREF(res); |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2830 | break; |
| 2831 | |
| 2832 | case Py_NE: |
Guido van Rossum | e27dc72 | 2007-03-27 22:37:34 +0000 | [diff] [blame] | 2833 | /* By default, != returns the opposite of ==, |
| 2834 | unless the latter returns NotImplemented. */ |
| 2835 | res = PyObject_RichCompare(self, other, Py_EQ); |
| 2836 | if (res != NULL && res != Py_NotImplemented) { |
| 2837 | int ok = PyObject_IsTrue(res); |
| 2838 | Py_DECREF(res); |
| 2839 | if (ok < 0) |
| 2840 | res = NULL; |
| 2841 | else { |
| 2842 | if (ok) |
| 2843 | res = Py_False; |
| 2844 | else |
| 2845 | res = Py_True; |
| 2846 | Py_INCREF(res); |
| 2847 | } |
| 2848 | } |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2849 | break; |
| 2850 | |
| 2851 | default: |
| 2852 | res = Py_NotImplemented; |
Guido van Rossum | 6b18a5b | 2007-03-29 20:49:57 +0000 | [diff] [blame] | 2853 | Py_INCREF(res); |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2854 | break; |
| 2855 | } |
| 2856 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2857 | return res; |
| 2858 | } |
| 2859 | |
| 2860 | static PyObject * |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2861 | object_get_class(PyObject *self, void *closure) |
| 2862 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2863 | Py_INCREF(Py_TYPE(self)); |
| 2864 | return (PyObject *)(Py_TYPE(self)); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2865 | } |
| 2866 | |
| 2867 | static int |
| 2868 | equiv_structs(PyTypeObject *a, PyTypeObject *b) |
| 2869 | { |
| 2870 | return a == b || |
| 2871 | (a != NULL && |
| 2872 | b != NULL && |
| 2873 | a->tp_basicsize == b->tp_basicsize && |
| 2874 | a->tp_itemsize == b->tp_itemsize && |
| 2875 | a->tp_dictoffset == b->tp_dictoffset && |
| 2876 | a->tp_weaklistoffset == b->tp_weaklistoffset && |
| 2877 | ((a->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 2878 | (b->tp_flags & Py_TPFLAGS_HAVE_GC))); |
| 2879 | } |
| 2880 | |
| 2881 | static int |
| 2882 | same_slots_added(PyTypeObject *a, PyTypeObject *b) |
| 2883 | { |
| 2884 | PyTypeObject *base = a->tp_base; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 2885 | Py_ssize_t size; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2886 | PyObject *slots_a, *slots_b; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2887 | |
| 2888 | if (base != b->tp_base) |
| 2889 | return 0; |
| 2890 | if (equiv_structs(a, base) && equiv_structs(b, base)) |
| 2891 | return 1; |
| 2892 | size = base->tp_basicsize; |
| 2893 | if (a->tp_dictoffset == size && b->tp_dictoffset == size) |
| 2894 | size += sizeof(PyObject *); |
| 2895 | if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size) |
| 2896 | size += sizeof(PyObject *); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2897 | |
| 2898 | /* Check slots compliance */ |
| 2899 | slots_a = ((PyHeapTypeObject *)a)->ht_slots; |
| 2900 | slots_b = ((PyHeapTypeObject *)b)->ht_slots; |
| 2901 | if (slots_a && slots_b) { |
Mark Dickinson | f02e0aa | 2009-02-01 12:13:56 +0000 | [diff] [blame] | 2902 | if (PyObject_RichCompareBool(slots_a, slots_b, Py_EQ) != 1) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2903 | return 0; |
| 2904 | size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a); |
| 2905 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2906 | return size == a->tp_basicsize && size == b->tp_basicsize; |
| 2907 | } |
| 2908 | |
| 2909 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2910 | compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2911 | { |
| 2912 | PyTypeObject *newbase, *oldbase; |
| 2913 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2914 | if (newto->tp_dealloc != oldto->tp_dealloc || |
| 2915 | newto->tp_free != oldto->tp_free) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2916 | { |
| 2917 | PyErr_Format(PyExc_TypeError, |
| 2918 | "%s assignment: " |
| 2919 | "'%s' deallocator differs from '%s'", |
| 2920 | attr, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2921 | newto->tp_name, |
| 2922 | oldto->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2923 | return 0; |
| 2924 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2925 | newbase = newto; |
| 2926 | oldbase = oldto; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2927 | while (equiv_structs(newbase, newbase->tp_base)) |
| 2928 | newbase = newbase->tp_base; |
| 2929 | while (equiv_structs(oldbase, oldbase->tp_base)) |
| 2930 | oldbase = oldbase->tp_base; |
| 2931 | if (newbase != oldbase && |
| 2932 | (newbase->tp_base != oldbase->tp_base || |
| 2933 | !same_slots_added(newbase, oldbase))) { |
| 2934 | PyErr_Format(PyExc_TypeError, |
| 2935 | "%s assignment: " |
| 2936 | "'%s' object layout differs from '%s'", |
| 2937 | attr, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2938 | newto->tp_name, |
| 2939 | oldto->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2940 | return 0; |
| 2941 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 2942 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2943 | return 1; |
| 2944 | } |
| 2945 | |
| 2946 | static int |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2947 | object_set_class(PyObject *self, PyObject *value, void *closure) |
| 2948 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2949 | PyTypeObject *oldto = Py_TYPE(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2950 | PyTypeObject *newto; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2951 | |
Guido van Rossum | b6b8942 | 2002-04-15 01:03:30 +0000 | [diff] [blame] | 2952 | if (value == NULL) { |
| 2953 | PyErr_SetString(PyExc_TypeError, |
| 2954 | "can't delete __class__ attribute"); |
| 2955 | return -1; |
| 2956 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2957 | if (!PyType_Check(value)) { |
| 2958 | PyErr_Format(PyExc_TypeError, |
| 2959 | "__class__ must be set to new-style class, not '%s' object", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2960 | Py_TYPE(value)->tp_name); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2961 | return -1; |
| 2962 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2963 | newto = (PyTypeObject *)value; |
| 2964 | if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) || |
| 2965 | !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
Guido van Rossum | 40af889 | 2002-08-10 05:42:07 +0000 | [diff] [blame] | 2966 | { |
| 2967 | PyErr_Format(PyExc_TypeError, |
| 2968 | "__class__ assignment: only for heap types"); |
| 2969 | return -1; |
| 2970 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2971 | if (compatible_for_assignment(newto, oldto, "__class__")) { |
| 2972 | Py_INCREF(newto); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2973 | Py_TYPE(self) = newto; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2974 | Py_DECREF(oldto); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2975 | return 0; |
| 2976 | } |
| 2977 | else { |
Guido van Rossum | 9ee4b94 | 2002-05-24 18:47:47 +0000 | [diff] [blame] | 2978 | return -1; |
| 2979 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | static PyGetSetDef object_getsets[] = { |
| 2983 | {"__class__", object_get_class, object_set_class, |
Neal Norwitz | 858e34f | 2002-08-13 17:18:45 +0000 | [diff] [blame] | 2984 | PyDoc_STR("the object's class")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2985 | {0} |
| 2986 | }; |
| 2987 | |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 2988 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 2989 | /* Stuff to implement __reduce_ex__ for pickle protocols >= 2. |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 2990 | We fall back to helpers in copyreg for: |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 2991 | - pickle protocols < 2 |
| 2992 | - calculating the list of slot names (done only once per class) |
| 2993 | - the __newobj__ function (which is used as a token but never called) |
| 2994 | */ |
| 2995 | |
| 2996 | static PyObject * |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 2997 | import_copyreg(void) |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 2998 | { |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 2999 | static PyObject *copyreg_str; |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3000 | |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3001 | if (!copyreg_str) { |
| 3002 | copyreg_str = PyUnicode_InternFromString("copyreg"); |
| 3003 | if (copyreg_str == NULL) |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3004 | return NULL; |
| 3005 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3006 | |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3007 | return PyImport_Import(copyreg_str); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
| 3010 | static PyObject * |
| 3011 | slotnames(PyObject *cls) |
| 3012 | { |
| 3013 | PyObject *clsdict; |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3014 | PyObject *copyreg; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3015 | PyObject *slotnames; |
| 3016 | |
| 3017 | if (!PyType_Check(cls)) { |
| 3018 | Py_INCREF(Py_None); |
| 3019 | return Py_None; |
| 3020 | } |
| 3021 | |
| 3022 | clsdict = ((PyTypeObject *)cls)->tp_dict; |
| 3023 | slotnames = PyDict_GetItemString(clsdict, "__slotnames__"); |
Armin Rigo | ec862b9 | 2005-09-24 22:58:41 +0000 | [diff] [blame] | 3024 | if (slotnames != NULL && PyList_Check(slotnames)) { |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3025 | Py_INCREF(slotnames); |
| 3026 | return slotnames; |
| 3027 | } |
| 3028 | |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3029 | copyreg = import_copyreg(); |
| 3030 | if (copyreg == NULL) |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3031 | return NULL; |
| 3032 | |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3033 | slotnames = PyObject_CallMethod(copyreg, "_slotnames", "O", cls); |
| 3034 | Py_DECREF(copyreg); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3035 | if (slotnames != NULL && |
| 3036 | slotnames != Py_None && |
| 3037 | !PyList_Check(slotnames)) |
| 3038 | { |
| 3039 | PyErr_SetString(PyExc_TypeError, |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3040 | "copyreg._slotnames didn't return a list or None"); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3041 | Py_DECREF(slotnames); |
| 3042 | slotnames = NULL; |
| 3043 | } |
| 3044 | |
| 3045 | return slotnames; |
| 3046 | } |
| 3047 | |
| 3048 | static PyObject * |
| 3049 | reduce_2(PyObject *obj) |
| 3050 | { |
| 3051 | PyObject *cls, *getnewargs; |
| 3052 | PyObject *args = NULL, *args2 = NULL; |
| 3053 | PyObject *getstate = NULL, *state = NULL, *names = NULL; |
| 3054 | PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL; |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3055 | PyObject *copyreg = NULL, *newobj = NULL, *res = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3056 | Py_ssize_t i, n; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3057 | |
| 3058 | cls = PyObject_GetAttrString(obj, "__class__"); |
| 3059 | if (cls == NULL) |
| 3060 | return NULL; |
| 3061 | |
| 3062 | getnewargs = PyObject_GetAttrString(obj, "__getnewargs__"); |
| 3063 | if (getnewargs != NULL) { |
| 3064 | args = PyObject_CallObject(getnewargs, NULL); |
| 3065 | Py_DECREF(getnewargs); |
| 3066 | if (args != NULL && !PyTuple_Check(args)) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3067 | PyErr_Format(PyExc_TypeError, |
| 3068 | "__getnewargs__ should return a tuple, " |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3069 | "not '%.200s'", Py_TYPE(args)->tp_name); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3070 | goto end; |
| 3071 | } |
| 3072 | } |
| 3073 | else { |
| 3074 | PyErr_Clear(); |
| 3075 | args = PyTuple_New(0); |
| 3076 | } |
| 3077 | if (args == NULL) |
| 3078 | goto end; |
| 3079 | |
| 3080 | getstate = PyObject_GetAttrString(obj, "__getstate__"); |
| 3081 | if (getstate != NULL) { |
| 3082 | state = PyObject_CallObject(getstate, NULL); |
| 3083 | Py_DECREF(getstate); |
Neal Norwitz | e2fdc61 | 2003-06-08 13:19:58 +0000 | [diff] [blame] | 3084 | if (state == NULL) |
| 3085 | goto end; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3086 | } |
| 3087 | else { |
Jim Fulton | 8a1a594 | 2004-02-08 04:21:26 +0000 | [diff] [blame] | 3088 | PyErr_Clear(); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3089 | state = PyObject_GetAttrString(obj, "__dict__"); |
| 3090 | if (state == NULL) { |
| 3091 | PyErr_Clear(); |
| 3092 | state = Py_None; |
| 3093 | Py_INCREF(state); |
| 3094 | } |
| 3095 | names = slotnames(cls); |
| 3096 | if (names == NULL) |
| 3097 | goto end; |
| 3098 | if (names != Py_None) { |
| 3099 | assert(PyList_Check(names)); |
| 3100 | slots = PyDict_New(); |
| 3101 | if (slots == NULL) |
| 3102 | goto end; |
| 3103 | n = 0; |
| 3104 | /* Can't pre-compute the list size; the list |
| 3105 | is stored on the class so accessible to other |
| 3106 | threads, which may be run by DECREF */ |
| 3107 | for (i = 0; i < PyList_GET_SIZE(names); i++) { |
| 3108 | PyObject *name, *value; |
| 3109 | name = PyList_GET_ITEM(names, i); |
| 3110 | value = PyObject_GetAttr(obj, name); |
| 3111 | if (value == NULL) |
| 3112 | PyErr_Clear(); |
| 3113 | else { |
| 3114 | int err = PyDict_SetItem(slots, name, |
| 3115 | value); |
| 3116 | Py_DECREF(value); |
| 3117 | if (err) |
| 3118 | goto end; |
| 3119 | n++; |
| 3120 | } |
| 3121 | } |
| 3122 | if (n) { |
| 3123 | state = Py_BuildValue("(NO)", state, slots); |
| 3124 | if (state == NULL) |
| 3125 | goto end; |
| 3126 | } |
| 3127 | } |
| 3128 | } |
| 3129 | |
| 3130 | if (!PyList_Check(obj)) { |
| 3131 | listitems = Py_None; |
| 3132 | Py_INCREF(listitems); |
| 3133 | } |
| 3134 | else { |
| 3135 | listitems = PyObject_GetIter(obj); |
| 3136 | if (listitems == NULL) |
| 3137 | goto end; |
| 3138 | } |
| 3139 | |
| 3140 | if (!PyDict_Check(obj)) { |
| 3141 | dictitems = Py_None; |
| 3142 | Py_INCREF(dictitems); |
| 3143 | } |
| 3144 | else { |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 3145 | PyObject *items = PyObject_CallMethod(obj, "items", ""); |
| 3146 | if (items == NULL) |
| 3147 | goto end; |
| 3148 | dictitems = PyObject_GetIter(items); |
| 3149 | Py_DECREF(items); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3150 | if (dictitems == NULL) |
| 3151 | goto end; |
| 3152 | } |
| 3153 | |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3154 | copyreg = import_copyreg(); |
| 3155 | if (copyreg == NULL) |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3156 | goto end; |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3157 | newobj = PyObject_GetAttrString(copyreg, "__newobj__"); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3158 | if (newobj == NULL) |
| 3159 | goto end; |
| 3160 | |
| 3161 | n = PyTuple_GET_SIZE(args); |
| 3162 | args2 = PyTuple_New(n+1); |
| 3163 | if (args2 == NULL) |
| 3164 | goto end; |
| 3165 | PyTuple_SET_ITEM(args2, 0, cls); |
| 3166 | cls = NULL; |
| 3167 | for (i = 0; i < n; i++) { |
| 3168 | PyObject *v = PyTuple_GET_ITEM(args, i); |
| 3169 | Py_INCREF(v); |
| 3170 | PyTuple_SET_ITEM(args2, i+1, v); |
| 3171 | } |
| 3172 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3173 | res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3174 | |
| 3175 | end: |
| 3176 | Py_XDECREF(cls); |
| 3177 | Py_XDECREF(args); |
| 3178 | Py_XDECREF(args2); |
Jeremy Hylton | d06483c | 2003-04-09 21:01:42 +0000 | [diff] [blame] | 3179 | Py_XDECREF(slots); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3180 | Py_XDECREF(state); |
| 3181 | Py_XDECREF(names); |
| 3182 | Py_XDECREF(listitems); |
| 3183 | Py_XDECREF(dictitems); |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3184 | Py_XDECREF(copyreg); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3185 | Py_XDECREF(newobj); |
| 3186 | return res; |
| 3187 | } |
| 3188 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3189 | /* |
| 3190 | * There were two problems when object.__reduce__ and object.__reduce_ex__ |
| 3191 | * were implemented in the same function: |
| 3192 | * - trying to pickle an object with a custom __reduce__ method that |
| 3193 | * fell back to object.__reduce__ in certain circumstances led to |
| 3194 | * infinite recursion at Python level and eventual RuntimeError. |
| 3195 | * - Pickling objects that lied about their type by overwriting the |
| 3196 | * __class__ descriptor could lead to infinite recursion at C level |
| 3197 | * and eventual segfault. |
| 3198 | * |
| 3199 | * Because of backwards compatibility, the two methods still have to |
| 3200 | * behave in the same way, even if this is not required by the pickle |
| 3201 | * protocol. This common functionality was moved to the _common_reduce |
| 3202 | * function. |
| 3203 | */ |
| 3204 | static PyObject * |
| 3205 | _common_reduce(PyObject *self, int proto) |
| 3206 | { |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3207 | PyObject *copyreg, *res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3208 | |
| 3209 | if (proto >= 2) |
| 3210 | return reduce_2(self); |
| 3211 | |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3212 | copyreg = import_copyreg(); |
| 3213 | if (!copyreg) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3214 | return NULL; |
| 3215 | |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3216 | res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto); |
| 3217 | Py_DECREF(copyreg); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3218 | |
| 3219 | return res; |
| 3220 | } |
| 3221 | |
| 3222 | static PyObject * |
| 3223 | object_reduce(PyObject *self, PyObject *args) |
| 3224 | { |
| 3225 | int proto = 0; |
| 3226 | |
| 3227 | if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto)) |
| 3228 | return NULL; |
| 3229 | |
| 3230 | return _common_reduce(self, proto); |
| 3231 | } |
| 3232 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3233 | static PyObject * |
| 3234 | object_reduce_ex(PyObject *self, PyObject *args) |
| 3235 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3236 | PyObject *reduce, *res; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3237 | int proto = 0; |
| 3238 | |
| 3239 | if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto)) |
| 3240 | return NULL; |
| 3241 | |
| 3242 | reduce = PyObject_GetAttrString(self, "__reduce__"); |
| 3243 | if (reduce == NULL) |
| 3244 | PyErr_Clear(); |
| 3245 | else { |
| 3246 | PyObject *cls, *clsreduce, *objreduce; |
| 3247 | int override; |
| 3248 | cls = PyObject_GetAttrString(self, "__class__"); |
| 3249 | if (cls == NULL) { |
| 3250 | Py_DECREF(reduce); |
| 3251 | return NULL; |
| 3252 | } |
| 3253 | clsreduce = PyObject_GetAttrString(cls, "__reduce__"); |
| 3254 | Py_DECREF(cls); |
| 3255 | if (clsreduce == NULL) { |
| 3256 | Py_DECREF(reduce); |
| 3257 | return NULL; |
| 3258 | } |
| 3259 | objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict, |
| 3260 | "__reduce__"); |
| 3261 | override = (clsreduce != objreduce); |
| 3262 | Py_DECREF(clsreduce); |
| 3263 | if (override) { |
| 3264 | res = PyObject_CallObject(reduce, NULL); |
| 3265 | Py_DECREF(reduce); |
| 3266 | return res; |
| 3267 | } |
| 3268 | else |
| 3269 | Py_DECREF(reduce); |
| 3270 | } |
| 3271 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3272 | return _common_reduce(self, proto); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3275 | static PyObject * |
| 3276 | object_subclasshook(PyObject *cls, PyObject *args) |
| 3277 | { |
| 3278 | Py_INCREF(Py_NotImplemented); |
| 3279 | return Py_NotImplemented; |
| 3280 | } |
| 3281 | |
| 3282 | PyDoc_STRVAR(object_subclasshook_doc, |
| 3283 | "Abstract classes can override this to customize issubclass().\n" |
| 3284 | "\n" |
| 3285 | "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n" |
| 3286 | "It should return True, False or NotImplemented. If it returns\n" |
| 3287 | "NotImplemented, the normal algorithm is used. Otherwise, it\n" |
| 3288 | "overrides the normal algorithm (and the outcome is cached).\n"); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3289 | |
| 3290 | /* |
| 3291 | from PEP 3101, this code implements: |
| 3292 | |
| 3293 | class object: |
| 3294 | def __format__(self, format_spec): |
| 3295 | return format(str(self), format_spec) |
| 3296 | */ |
| 3297 | static PyObject * |
| 3298 | object_format(PyObject *self, PyObject *args) |
| 3299 | { |
| 3300 | PyObject *format_spec; |
| 3301 | PyObject *self_as_str = NULL; |
| 3302 | PyObject *result = NULL; |
| 3303 | PyObject *format_meth = NULL; |
| 3304 | |
Eric Smith | fc6e8fe | 2008-01-11 00:17:22 +0000 | [diff] [blame] | 3305 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3306 | return NULL; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3307 | |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 3308 | self_as_str = PyObject_Str(self); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3309 | if (self_as_str != NULL) { |
| 3310 | /* find the format function */ |
| 3311 | format_meth = PyObject_GetAttrString(self_as_str, "__format__"); |
| 3312 | if (format_meth != NULL) { |
| 3313 | /* and call it */ |
| 3314 | result = PyObject_CallFunctionObjArgs(format_meth, format_spec, NULL); |
| 3315 | } |
| 3316 | } |
| 3317 | |
| 3318 | Py_XDECREF(self_as_str); |
| 3319 | Py_XDECREF(format_meth); |
| 3320 | |
| 3321 | return result; |
| 3322 | } |
| 3323 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3324 | static PyObject * |
| 3325 | object_sizeof(PyObject *self, PyObject *args) |
| 3326 | { |
| 3327 | Py_ssize_t res, isize; |
| 3328 | |
| 3329 | res = 0; |
| 3330 | isize = self->ob_type->tp_itemsize; |
| 3331 | if (isize > 0) |
| 3332 | res = Py_SIZE(self->ob_type) * isize; |
| 3333 | res += self->ob_type->tp_basicsize; |
| 3334 | |
| 3335 | return PyLong_FromSsize_t(res); |
| 3336 | } |
| 3337 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3338 | static PyMethodDef object_methods[] = { |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 3339 | {"__reduce_ex__", object_reduce_ex, METH_VARARGS, |
| 3340 | PyDoc_STR("helper for pickle")}, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3341 | {"__reduce__", object_reduce, METH_VARARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 3342 | PyDoc_STR("helper for pickle")}, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3343 | {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS, |
| 3344 | object_subclasshook_doc}, |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3345 | {"__format__", object_format, METH_VARARGS, |
| 3346 | PyDoc_STR("default object formatter")}, |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3347 | {"__sizeof__", object_sizeof, METH_NOARGS, |
| 3348 | PyDoc_STR("__sizeof__() -> size of object in memory, in bytes")}, |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3349 | {0} |
| 3350 | }; |
| 3351 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3352 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3353 | PyTypeObject PyBaseObject_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3354 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3355 | "object", /* tp_name */ |
| 3356 | sizeof(PyObject), /* tp_basicsize */ |
| 3357 | 0, /* tp_itemsize */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3358 | object_dealloc, /* tp_dealloc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3359 | 0, /* tp_print */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3360 | 0, /* tp_getattr */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3361 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 3362 | 0, /* tp_reserved */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3363 | object_repr, /* tp_repr */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3364 | 0, /* tp_as_number */ |
| 3365 | 0, /* tp_as_sequence */ |
| 3366 | 0, /* tp_as_mapping */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 3367 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3368 | 0, /* tp_call */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3369 | object_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3370 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3371 | PyObject_GenericSetAttr, /* tp_setattro */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3372 | 0, /* tp_as_buffer */ |
| 3373 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 3374 | PyDoc_STR("The most base type"), /* tp_doc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3375 | 0, /* tp_traverse */ |
| 3376 | 0, /* tp_clear */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3377 | object_richcompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3378 | 0, /* tp_weaklistoffset */ |
| 3379 | 0, /* tp_iter */ |
| 3380 | 0, /* tp_iternext */ |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3381 | object_methods, /* tp_methods */ |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3382 | 0, /* tp_members */ |
| 3383 | object_getsets, /* tp_getset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3384 | 0, /* tp_base */ |
| 3385 | 0, /* tp_dict */ |
| 3386 | 0, /* tp_descr_get */ |
| 3387 | 0, /* tp_descr_set */ |
| 3388 | 0, /* tp_dictoffset */ |
| 3389 | object_init, /* tp_init */ |
| 3390 | PyType_GenericAlloc, /* tp_alloc */ |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 3391 | object_new, /* tp_new */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3392 | PyObject_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3393 | }; |
| 3394 | |
| 3395 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 3396 | /* Add the methods from tp_methods to the __dict__ in a type object */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3397 | |
| 3398 | static int |
| 3399 | add_methods(PyTypeObject *type, PyMethodDef *meth) |
| 3400 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3401 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3402 | |
| 3403 | for (; meth->ml_name != NULL; meth++) { |
| 3404 | PyObject *descr; |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 3405 | if (PyDict_GetItemString(dict, meth->ml_name) && |
| 3406 | !(meth->ml_flags & METH_COEXIST)) |
| 3407 | continue; |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 3408 | if (meth->ml_flags & METH_CLASS) { |
| 3409 | if (meth->ml_flags & METH_STATIC) { |
| 3410 | PyErr_SetString(PyExc_ValueError, |
| 3411 | "method cannot be both class and static"); |
| 3412 | return -1; |
| 3413 | } |
Tim Peters | bca1cbc | 2002-12-09 22:56:13 +0000 | [diff] [blame] | 3414 | descr = PyDescr_NewClassMethod(type, meth); |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 3415 | } |
| 3416 | else if (meth->ml_flags & METH_STATIC) { |
Guido van Rossum | 9af48ff | 2003-02-11 17:12:46 +0000 | [diff] [blame] | 3417 | PyObject *cfunc = PyCFunction_New(meth, NULL); |
| 3418 | if (cfunc == NULL) |
| 3419 | return -1; |
| 3420 | descr = PyStaticMethod_New(cfunc); |
| 3421 | Py_DECREF(cfunc); |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 3422 | } |
| 3423 | else { |
| 3424 | descr = PyDescr_NewMethod(type, meth); |
| 3425 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3426 | if (descr == NULL) |
| 3427 | return -1; |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 3428 | if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3429 | return -1; |
| 3430 | Py_DECREF(descr); |
| 3431 | } |
| 3432 | return 0; |
| 3433 | } |
| 3434 | |
| 3435 | static int |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 3436 | add_members(PyTypeObject *type, PyMemberDef *memb) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3437 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3438 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3439 | |
| 3440 | for (; memb->name != NULL; memb++) { |
| 3441 | PyObject *descr; |
| 3442 | if (PyDict_GetItemString(dict, memb->name)) |
| 3443 | continue; |
| 3444 | descr = PyDescr_NewMember(type, memb); |
| 3445 | if (descr == NULL) |
| 3446 | return -1; |
| 3447 | if (PyDict_SetItemString(dict, memb->name, descr) < 0) |
| 3448 | return -1; |
| 3449 | Py_DECREF(descr); |
| 3450 | } |
| 3451 | return 0; |
| 3452 | } |
| 3453 | |
| 3454 | static int |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 3455 | add_getset(PyTypeObject *type, PyGetSetDef *gsp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3456 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3457 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3458 | |
| 3459 | for (; gsp->name != NULL; gsp++) { |
| 3460 | PyObject *descr; |
| 3461 | if (PyDict_GetItemString(dict, gsp->name)) |
| 3462 | continue; |
| 3463 | descr = PyDescr_NewGetSet(type, gsp); |
| 3464 | |
| 3465 | if (descr == NULL) |
| 3466 | return -1; |
| 3467 | if (PyDict_SetItemString(dict, gsp->name, descr) < 0) |
| 3468 | return -1; |
| 3469 | Py_DECREF(descr); |
| 3470 | } |
| 3471 | return 0; |
| 3472 | } |
| 3473 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3474 | static void |
| 3475 | inherit_special(PyTypeObject *type, PyTypeObject *base) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3476 | { |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 3477 | Py_ssize_t oldsize, newsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3478 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3479 | /* Copying basicsize is connected to the GC flags */ |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 3480 | oldsize = base->tp_basicsize; |
| 3481 | newsize = type->tp_basicsize ? type->tp_basicsize : oldsize; |
| 3482 | if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3483 | (base->tp_flags & Py_TPFLAGS_HAVE_GC) && |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3484 | (!type->tp_traverse && !type->tp_clear)) { |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 3485 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3486 | if (type->tp_traverse == NULL) |
| 3487 | type->tp_traverse = base->tp_traverse; |
| 3488 | if (type->tp_clear == NULL) |
| 3489 | type->tp_clear = base->tp_clear; |
| 3490 | } |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 3491 | { |
Guido van Rossum | f884b74 | 2001-12-17 17:14:22 +0000 | [diff] [blame] | 3492 | /* The condition below could use some explanation. |
| 3493 | It appears that tp_new is not inherited for static types |
| 3494 | whose base class is 'object'; this seems to be a precaution |
| 3495 | so that old extension types don't suddenly become |
| 3496 | callable (object.__new__ wouldn't insure the invariants |
| 3497 | that the extension type's own factory function ensures). |
| 3498 | Heap types, of course, are under our control, so they do |
| 3499 | inherit tp_new; static extension types that specify some |
| 3500 | other built-in type as the default are considered |
| 3501 | new-style-aware so they also inherit object.__new__. */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3502 | if (base != &PyBaseObject_Type || |
| 3503 | (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 3504 | if (type->tp_new == NULL) |
| 3505 | type->tp_new = base->tp_new; |
| 3506 | } |
| 3507 | } |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 3508 | type->tp_basicsize = newsize; |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3509 | |
| 3510 | /* Copy other non-function slots */ |
| 3511 | |
| 3512 | #undef COPYVAL |
| 3513 | #define COPYVAL(SLOT) \ |
| 3514 | if (type->SLOT == 0) type->SLOT = base->SLOT |
| 3515 | |
| 3516 | COPYVAL(tp_itemsize); |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 3517 | COPYVAL(tp_weaklistoffset); |
| 3518 | COPYVAL(tp_dictoffset); |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 3519 | |
| 3520 | /* Setup fast subclass flags */ |
| 3521 | if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException)) |
| 3522 | type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS; |
| 3523 | else if (PyType_IsSubtype(base, &PyType_Type)) |
| 3524 | type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS; |
| 3525 | else if (PyType_IsSubtype(base, &PyLong_Type)) |
| 3526 | type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3527 | else if (PyType_IsSubtype(base, &PyBytes_Type)) |
| 3528 | type->tp_flags |= Py_TPFLAGS_BYTES_SUBCLASS; |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 3529 | else if (PyType_IsSubtype(base, &PyUnicode_Type)) |
| 3530 | type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS; |
| 3531 | else if (PyType_IsSubtype(base, &PyTuple_Type)) |
| 3532 | type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS; |
| 3533 | else if (PyType_IsSubtype(base, &PyList_Type)) |
| 3534 | type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS; |
| 3535 | else if (PyType_IsSubtype(base, &PyDict_Type)) |
| 3536 | type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3537 | } |
| 3538 | |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 3539 | static char *hash_name_op[] = { |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3540 | "__eq__", |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3541 | "__hash__", |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 3542 | NULL |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3543 | }; |
| 3544 | |
| 3545 | static int |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 3546 | overrides_hash(PyTypeObject *type) |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3547 | { |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 3548 | char **p; |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3549 | PyObject *dict = type->tp_dict; |
| 3550 | |
| 3551 | assert(dict != NULL); |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 3552 | for (p = hash_name_op; *p; p++) { |
| 3553 | if (PyDict_GetItemString(dict, *p) != NULL) |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3554 | return 1; |
| 3555 | } |
| 3556 | return 0; |
| 3557 | } |
| 3558 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3559 | static void |
| 3560 | inherit_slots(PyTypeObject *type, PyTypeObject *base) |
| 3561 | { |
| 3562 | PyTypeObject *basebase; |
| 3563 | |
| 3564 | #undef SLOTDEFINED |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3565 | #undef COPYSLOT |
| 3566 | #undef COPYNUM |
| 3567 | #undef COPYSEQ |
| 3568 | #undef COPYMAP |
Guido van Rossum | 5af588b | 2001-10-12 14:13:21 +0000 | [diff] [blame] | 3569 | #undef COPYBUF |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3570 | |
| 3571 | #define SLOTDEFINED(SLOT) \ |
| 3572 | (base->SLOT != 0 && \ |
| 3573 | (basebase == NULL || base->SLOT != basebase->SLOT)) |
| 3574 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3575 | #define COPYSLOT(SLOT) \ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3576 | if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3577 | |
| 3578 | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT) |
| 3579 | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT) |
| 3580 | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT) |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3581 | #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3582 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3583 | /* This won't inherit indirect slots (from tp_as_number etc.) |
| 3584 | if type doesn't provide the space. */ |
| 3585 | |
| 3586 | if (type->tp_as_number != NULL && base->tp_as_number != NULL) { |
| 3587 | basebase = base->tp_base; |
| 3588 | if (basebase->tp_as_number == NULL) |
| 3589 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3590 | COPYNUM(nb_add); |
| 3591 | COPYNUM(nb_subtract); |
| 3592 | COPYNUM(nb_multiply); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3593 | COPYNUM(nb_remainder); |
| 3594 | COPYNUM(nb_divmod); |
| 3595 | COPYNUM(nb_power); |
| 3596 | COPYNUM(nb_negative); |
| 3597 | COPYNUM(nb_positive); |
| 3598 | COPYNUM(nb_absolute); |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 3599 | COPYNUM(nb_bool); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3600 | COPYNUM(nb_invert); |
| 3601 | COPYNUM(nb_lshift); |
| 3602 | COPYNUM(nb_rshift); |
| 3603 | COPYNUM(nb_and); |
| 3604 | COPYNUM(nb_xor); |
| 3605 | COPYNUM(nb_or); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3606 | COPYNUM(nb_int); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3607 | COPYNUM(nb_float); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3608 | COPYNUM(nb_inplace_add); |
| 3609 | COPYNUM(nb_inplace_subtract); |
| 3610 | COPYNUM(nb_inplace_multiply); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3611 | COPYNUM(nb_inplace_remainder); |
| 3612 | COPYNUM(nb_inplace_power); |
| 3613 | COPYNUM(nb_inplace_lshift); |
| 3614 | COPYNUM(nb_inplace_rshift); |
| 3615 | COPYNUM(nb_inplace_and); |
| 3616 | COPYNUM(nb_inplace_xor); |
| 3617 | COPYNUM(nb_inplace_or); |
Neal Norwitz | bcc0db8 | 2006-03-24 08:14:36 +0000 | [diff] [blame] | 3618 | COPYNUM(nb_true_divide); |
| 3619 | COPYNUM(nb_floor_divide); |
| 3620 | COPYNUM(nb_inplace_true_divide); |
| 3621 | COPYNUM(nb_inplace_floor_divide); |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 3622 | COPYNUM(nb_index); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3623 | } |
| 3624 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3625 | if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) { |
| 3626 | basebase = base->tp_base; |
| 3627 | if (basebase->tp_as_sequence == NULL) |
| 3628 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3629 | COPYSEQ(sq_length); |
| 3630 | COPYSEQ(sq_concat); |
| 3631 | COPYSEQ(sq_repeat); |
| 3632 | COPYSEQ(sq_item); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3633 | COPYSEQ(sq_ass_item); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3634 | COPYSEQ(sq_contains); |
| 3635 | COPYSEQ(sq_inplace_concat); |
| 3636 | COPYSEQ(sq_inplace_repeat); |
| 3637 | } |
| 3638 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3639 | if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) { |
| 3640 | basebase = base->tp_base; |
| 3641 | if (basebase->tp_as_mapping == NULL) |
| 3642 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3643 | COPYMAP(mp_length); |
| 3644 | COPYMAP(mp_subscript); |
| 3645 | COPYMAP(mp_ass_subscript); |
| 3646 | } |
| 3647 | |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3648 | if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) { |
| 3649 | basebase = base->tp_base; |
| 3650 | if (basebase->tp_as_buffer == NULL) |
| 3651 | basebase = NULL; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 3652 | COPYBUF(bf_getbuffer); |
| 3653 | COPYBUF(bf_releasebuffer); |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3654 | } |
| 3655 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3656 | basebase = base->tp_base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3657 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3658 | COPYSLOT(tp_dealloc); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3659 | if (type->tp_getattr == NULL && type->tp_getattro == NULL) { |
| 3660 | type->tp_getattr = base->tp_getattr; |
| 3661 | type->tp_getattro = base->tp_getattro; |
| 3662 | } |
| 3663 | if (type->tp_setattr == NULL && type->tp_setattro == NULL) { |
| 3664 | type->tp_setattr = base->tp_setattr; |
| 3665 | type->tp_setattro = base->tp_setattro; |
| 3666 | } |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 3667 | /* tp_reserved is ignored */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3668 | COPYSLOT(tp_repr); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3669 | /* tp_hash see tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3670 | COPYSLOT(tp_call); |
| 3671 | COPYSLOT(tp_str); |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 3672 | { |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3673 | /* Copy comparison-related slots only when |
| 3674 | not overriding them anywhere */ |
Mark Dickinson | c008a17 | 2009-02-01 13:59:22 +0000 | [diff] [blame] | 3675 | if (type->tp_richcompare == NULL && |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3676 | type->tp_hash == NULL && |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 3677 | !overrides_hash(type)) |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3678 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3679 | type->tp_richcompare = base->tp_richcompare; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3680 | type->tp_hash = base->tp_hash; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3681 | } |
| 3682 | } |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 3683 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3684 | COPYSLOT(tp_iter); |
| 3685 | COPYSLOT(tp_iternext); |
| 3686 | } |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 3687 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3688 | COPYSLOT(tp_descr_get); |
| 3689 | COPYSLOT(tp_descr_set); |
| 3690 | COPYSLOT(tp_dictoffset); |
| 3691 | COPYSLOT(tp_init); |
| 3692 | COPYSLOT(tp_alloc); |
Guido van Rossum | cc8fe04 | 2002-04-05 17:10:16 +0000 | [diff] [blame] | 3693 | COPYSLOT(tp_is_gc); |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 3694 | if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 3695 | (base->tp_flags & Py_TPFLAGS_HAVE_GC)) { |
| 3696 | /* They agree about gc. */ |
| 3697 | COPYSLOT(tp_free); |
| 3698 | } |
| 3699 | else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3700 | type->tp_free == NULL && |
Neal Norwitz | 30d1c51 | 2007-08-19 22:48:23 +0000 | [diff] [blame] | 3701 | base->tp_free == PyObject_Free) { |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 3702 | /* A bit of magic to plug in the correct default |
| 3703 | * tp_free function when a derived class adds gc, |
| 3704 | * didn't define tp_free, and the base uses the |
| 3705 | * default non-gc tp_free. |
| 3706 | */ |
| 3707 | type->tp_free = PyObject_GC_Del; |
| 3708 | } |
| 3709 | /* else they didn't agree about gc, and there isn't something |
| 3710 | * obvious to be done -- the type is on its own. |
| 3711 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3712 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3713 | } |
| 3714 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 3715 | static int add_operators(PyTypeObject *); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3716 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3717 | int |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 3718 | PyType_Ready(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3719 | { |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 3720 | PyObject *dict, *bases; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3721 | PyTypeObject *base; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3722 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3723 | |
Guido van Rossum | cab0580 | 2002-06-10 15:29:03 +0000 | [diff] [blame] | 3724 | if (type->tp_flags & Py_TPFLAGS_READY) { |
| 3725 | assert(type->tp_dict != NULL); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3726 | return 0; |
Guido van Rossum | cab0580 | 2002-06-10 15:29:03 +0000 | [diff] [blame] | 3727 | } |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3728 | assert((type->tp_flags & Py_TPFLAGS_READYING) == 0); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3729 | |
| 3730 | type->tp_flags |= Py_TPFLAGS_READYING; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3731 | |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 3732 | #ifdef Py_TRACE_REFS |
| 3733 | /* PyType_Ready is the closest thing we have to a choke point |
| 3734 | * for type objects, so is the best place I can think of to try |
| 3735 | * to get type objects into the doubly-linked list of all objects. |
| 3736 | * Still, not all type objects go thru PyType_Ready. |
| 3737 | */ |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 3738 | _Py_AddToAllObjects((PyObject *)type, 0); |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 3739 | #endif |
| 3740 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3741 | /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
| 3742 | base = type->tp_base; |
Martin v. Löwis | bf60875 | 2004-08-18 13:16:54 +0000 | [diff] [blame] | 3743 | if (base == NULL && type != &PyBaseObject_Type) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3744 | base = type->tp_base = &PyBaseObject_Type; |
Martin v. Löwis | bf60875 | 2004-08-18 13:16:54 +0000 | [diff] [blame] | 3745 | Py_INCREF(base); |
| 3746 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3747 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3748 | /* Now the only way base can still be NULL is if type is |
| 3749 | * &PyBaseObject_Type. |
| 3750 | */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 3751 | |
Guido van Rossum | 323a9cf | 2002-08-14 17:26:30 +0000 | [diff] [blame] | 3752 | /* Initialize the base class */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 3753 | if (base != NULL && base->tp_dict == NULL) { |
Guido van Rossum | 323a9cf | 2002-08-14 17:26:30 +0000 | [diff] [blame] | 3754 | if (PyType_Ready(base) < 0) |
| 3755 | goto error; |
| 3756 | } |
| 3757 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3758 | /* 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] | 3759 | compilable separately on Windows can call PyType_Ready() instead of |
| 3760 | initializing the ob_type field of their type objects. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3761 | /* The test for base != NULL is really unnecessary, since base is only |
| 3762 | NULL when type is &PyBaseObject_Type, and we know its ob_type is |
| 3763 | not NULL (it's initialized to &PyType_Type). But coverity doesn't |
| 3764 | know that. */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3765 | if (Py_TYPE(type) == NULL && base != NULL) |
| 3766 | Py_TYPE(type) = Py_TYPE(base); |
Guido van Rossum | 0986d82 | 2002-04-08 01:38:42 +0000 | [diff] [blame] | 3767 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3768 | /* Initialize tp_bases */ |
| 3769 | bases = type->tp_bases; |
| 3770 | if (bases == NULL) { |
| 3771 | if (base == NULL) |
| 3772 | bases = PyTuple_New(0); |
| 3773 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3774 | bases = PyTuple_Pack(1, base); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3775 | if (bases == NULL) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3776 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3777 | type->tp_bases = bases; |
| 3778 | } |
| 3779 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3780 | /* Initialize tp_dict */ |
| 3781 | dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3782 | if (dict == NULL) { |
| 3783 | dict = PyDict_New(); |
| 3784 | if (dict == NULL) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3785 | goto error; |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3786 | type->tp_dict = dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3787 | } |
| 3788 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3789 | /* Add type-specific descriptors to tp_dict */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3790 | if (add_operators(type) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3791 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3792 | if (type->tp_methods != NULL) { |
| 3793 | if (add_methods(type, type->tp_methods) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3794 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3795 | } |
| 3796 | if (type->tp_members != NULL) { |
| 3797 | if (add_members(type, type->tp_members) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3798 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3799 | } |
| 3800 | if (type->tp_getset != NULL) { |
| 3801 | if (add_getset(type, type->tp_getset) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3802 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3803 | } |
| 3804 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3805 | /* Calculate method resolution order */ |
| 3806 | if (mro_internal(type) < 0) { |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3807 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3808 | } |
| 3809 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3810 | /* Inherit special flags from dominant base */ |
| 3811 | if (type->tp_base != NULL) |
| 3812 | inherit_special(type, type->tp_base); |
| 3813 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3814 | /* Initialize tp_dict properly */ |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 3815 | bases = type->tp_mro; |
| 3816 | assert(bases != NULL); |
| 3817 | assert(PyTuple_Check(bases)); |
| 3818 | n = PyTuple_GET_SIZE(bases); |
| 3819 | for (i = 1; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 3820 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 3821 | if (PyType_Check(b)) |
| 3822 | inherit_slots(type, (PyTypeObject *)b); |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3823 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3824 | |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 3825 | /* Sanity check for tp_free. */ |
| 3826 | if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) && |
| 3827 | (type->tp_free == NULL || type->tp_free == PyObject_Del)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3828 | /* This base class needs to call tp_free, but doesn't have |
| 3829 | * one, or its tp_free is for non-gc'ed objects. |
| 3830 | */ |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 3831 | PyErr_Format(PyExc_TypeError, "type '%.100s' participates in " |
| 3832 | "gc and is a base type but has inappropriate " |
| 3833 | "tp_free slot", |
| 3834 | type->tp_name); |
| 3835 | goto error; |
| 3836 | } |
| 3837 | |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 3838 | /* if the type dictionary doesn't contain a __doc__, set it from |
| 3839 | the tp_doc slot. |
| 3840 | */ |
| 3841 | if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) { |
| 3842 | if (type->tp_doc != NULL) { |
Neal Norwitz | a369c5a | 2007-08-25 07:41:59 +0000 | [diff] [blame] | 3843 | PyObject *doc = PyUnicode_FromString(type->tp_doc); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3844 | if (doc == NULL) |
| 3845 | goto error; |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 3846 | PyDict_SetItemString(type->tp_dict, "__doc__", doc); |
| 3847 | Py_DECREF(doc); |
| 3848 | } else { |
Guido van Rossum | d464107 | 2002-04-03 02:13:37 +0000 | [diff] [blame] | 3849 | PyDict_SetItemString(type->tp_dict, |
| 3850 | "__doc__", Py_None); |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 3851 | } |
| 3852 | } |
| 3853 | |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3854 | /* Hack for tp_hash and __hash__. |
| 3855 | If after all that, tp_hash is still NULL, and __hash__ is not in |
Nick Coghlan | d1abd25 | 2008-07-15 15:46:38 +0000 | [diff] [blame] | 3856 | tp_dict, set tp_hash to PyObject_HashNotImplemented and |
| 3857 | tp_dict['__hash__'] equal to None. |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3858 | This signals that __hash__ is not inherited. |
| 3859 | */ |
| 3860 | if (type->tp_hash == NULL) { |
| 3861 | if (PyDict_GetItemString(type->tp_dict, "__hash__") == NULL) { |
| 3862 | if (PyDict_SetItemString(type->tp_dict, "__hash__", Py_None) < 0) |
| 3863 | goto error; |
Nick Coghlan | d1abd25 | 2008-07-15 15:46:38 +0000 | [diff] [blame] | 3864 | type->tp_hash = PyObject_HashNotImplemented; |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3865 | } |
| 3866 | } |
| 3867 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3868 | /* Some more special stuff */ |
| 3869 | base = type->tp_base; |
| 3870 | if (base != NULL) { |
| 3871 | if (type->tp_as_number == NULL) |
| 3872 | type->tp_as_number = base->tp_as_number; |
| 3873 | if (type->tp_as_sequence == NULL) |
| 3874 | type->tp_as_sequence = base->tp_as_sequence; |
| 3875 | if (type->tp_as_mapping == NULL) |
| 3876 | type->tp_as_mapping = base->tp_as_mapping; |
Guido van Rossum | eea4718 | 2003-02-11 20:39:59 +0000 | [diff] [blame] | 3877 | if (type->tp_as_buffer == NULL) |
| 3878 | type->tp_as_buffer = base->tp_as_buffer; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3879 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3880 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3881 | /* Link into each base class's list of subclasses */ |
| 3882 | bases = type->tp_bases; |
| 3883 | n = PyTuple_GET_SIZE(bases); |
| 3884 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 3885 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 3886 | if (PyType_Check(b) && |
| 3887 | add_subclass((PyTypeObject *)b, type) < 0) |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3888 | goto error; |
| 3889 | } |
| 3890 | |
Mark Dickinson | 2a7d45b | 2009-02-08 11:02:10 +0000 | [diff] [blame] | 3891 | /* Warn for a type that implements tp_compare (now known as |
| 3892 | tp_reserved) but not tp_richcompare. */ |
| 3893 | if (type->tp_reserved && !type->tp_richcompare) { |
| 3894 | int error; |
| 3895 | char msg[240]; |
| 3896 | PyOS_snprintf(msg, sizeof(msg), |
| 3897 | "Type %.100s defines tp_reserved (formerly " |
| 3898 | "tp_compare) but not tp_richcompare. " |
| 3899 | "Comparisons may not behave as intended.", |
| 3900 | type->tp_name); |
| 3901 | error = PyErr_WarnEx(PyExc_DeprecationWarning, msg, 1); |
| 3902 | if (error == -1) |
| 3903 | goto error; |
| 3904 | } |
| 3905 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3906 | /* All done -- set the ready flag */ |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3907 | assert(type->tp_dict != NULL); |
| 3908 | type->tp_flags = |
| 3909 | (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3910 | return 0; |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3911 | |
| 3912 | error: |
| 3913 | type->tp_flags &= ~Py_TPFLAGS_READYING; |
| 3914 | return -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3915 | } |
| 3916 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3917 | static int |
| 3918 | add_subclass(PyTypeObject *base, PyTypeObject *type) |
| 3919 | { |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 3920 | Py_ssize_t i; |
| 3921 | int result; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3922 | PyObject *list, *ref, *newobj; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3923 | |
| 3924 | list = base->tp_subclasses; |
| 3925 | if (list == NULL) { |
| 3926 | base->tp_subclasses = list = PyList_New(0); |
| 3927 | if (list == NULL) |
| 3928 | return -1; |
| 3929 | } |
| 3930 | assert(PyList_Check(list)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3931 | newobj = PyWeakref_NewRef((PyObject *)type, NULL); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3932 | i = PyList_GET_SIZE(list); |
| 3933 | while (--i >= 0) { |
| 3934 | ref = PyList_GET_ITEM(list, i); |
| 3935 | assert(PyWeakref_CheckRef(ref)); |
Guido van Rossum | 3930bc3 | 2002-10-18 13:51:49 +0000 | [diff] [blame] | 3936 | if (PyWeakref_GET_OBJECT(ref) == Py_None) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3937 | return PyList_SetItem(list, i, newobj); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3938 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3939 | result = PyList_Append(list, newobj); |
| 3940 | Py_DECREF(newobj); |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 3941 | return result; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3942 | } |
| 3943 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3944 | static void |
| 3945 | remove_subclass(PyTypeObject *base, PyTypeObject *type) |
| 3946 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3947 | Py_ssize_t i; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3948 | PyObject *list, *ref; |
| 3949 | |
| 3950 | list = base->tp_subclasses; |
| 3951 | if (list == NULL) { |
| 3952 | return; |
| 3953 | } |
| 3954 | assert(PyList_Check(list)); |
| 3955 | i = PyList_GET_SIZE(list); |
| 3956 | while (--i >= 0) { |
| 3957 | ref = PyList_GET_ITEM(list, i); |
| 3958 | assert(PyWeakref_CheckRef(ref)); |
| 3959 | if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) { |
| 3960 | /* this can't fail, right? */ |
| 3961 | PySequence_DelItem(list, i); |
| 3962 | return; |
| 3963 | } |
| 3964 | } |
| 3965 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3966 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3967 | static int |
| 3968 | check_num_args(PyObject *ob, int n) |
| 3969 | { |
| 3970 | if (!PyTuple_CheckExact(ob)) { |
| 3971 | PyErr_SetString(PyExc_SystemError, |
| 3972 | "PyArg_UnpackTuple() argument list is not a tuple"); |
| 3973 | return 0; |
| 3974 | } |
| 3975 | if (n == PyTuple_GET_SIZE(ob)) |
| 3976 | return 1; |
| 3977 | PyErr_Format( |
| 3978 | PyExc_TypeError, |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3979 | "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob)); |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3980 | return 0; |
| 3981 | } |
| 3982 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3983 | /* Generic wrappers for overloadable 'operators' such as __getitem__ */ |
| 3984 | |
| 3985 | /* There's a wrapper *function* for each distinct function typedef used |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3986 | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3987 | wrapper *table* for each distinct operation (e.g. __len__, __add__). |
| 3988 | Most tables have only one entry; the tables for binary operators have two |
| 3989 | entries, one regular and one with reversed arguments. */ |
| 3990 | |
| 3991 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3992 | wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3993 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3994 | lenfunc func = (lenfunc)wrapped; |
| 3995 | Py_ssize_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3996 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3997 | if (!check_num_args(args, 0)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3998 | return NULL; |
| 3999 | res = (*func)(self); |
| 4000 | if (res == -1 && PyErr_Occurred()) |
| 4001 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4002 | return PyLong_FromLong((long)res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4003 | } |
| 4004 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4005 | static PyObject * |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4006 | wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped) |
| 4007 | { |
| 4008 | inquiry func = (inquiry)wrapped; |
| 4009 | int res; |
| 4010 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4011 | if (!check_num_args(args, 0)) |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4012 | return NULL; |
| 4013 | res = (*func)(self); |
| 4014 | if (res == -1 && PyErr_Occurred()) |
| 4015 | return NULL; |
| 4016 | return PyBool_FromLong((long)res); |
| 4017 | } |
| 4018 | |
| 4019 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4020 | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4021 | { |
| 4022 | binaryfunc func = (binaryfunc)wrapped; |
| 4023 | PyObject *other; |
| 4024 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4025 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4026 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4027 | other = PyTuple_GET_ITEM(args, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4028 | return (*func)(self, other); |
| 4029 | } |
| 4030 | |
| 4031 | static PyObject * |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4032 | wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped) |
| 4033 | { |
| 4034 | binaryfunc func = (binaryfunc)wrapped; |
| 4035 | PyObject *other; |
| 4036 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4037 | if (!check_num_args(args, 1)) |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4038 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4039 | other = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4040 | return (*func)(self, other); |
| 4041 | } |
| 4042 | |
| 4043 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4044 | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4045 | { |
| 4046 | binaryfunc func = (binaryfunc)wrapped; |
| 4047 | PyObject *other; |
| 4048 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4049 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4050 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4051 | other = PyTuple_GET_ITEM(args, 0); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4052 | if (!PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) { |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4053 | Py_INCREF(Py_NotImplemented); |
| 4054 | return Py_NotImplemented; |
| 4055 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4056 | return (*func)(other, self); |
| 4057 | } |
| 4058 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4059 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4060 | wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4061 | { |
| 4062 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4063 | PyObject *other; |
| 4064 | PyObject *third = Py_None; |
| 4065 | |
| 4066 | /* Note: This wrapper only works for __pow__() */ |
| 4067 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4068 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4069 | return NULL; |
| 4070 | return (*func)(self, other, third); |
| 4071 | } |
| 4072 | |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4073 | static PyObject * |
| 4074 | wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4075 | { |
| 4076 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4077 | PyObject *other; |
| 4078 | PyObject *third = Py_None; |
| 4079 | |
| 4080 | /* Note: This wrapper only works for __pow__() */ |
| 4081 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4082 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4083 | return NULL; |
| 4084 | return (*func)(other, self, third); |
| 4085 | } |
| 4086 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4087 | static PyObject * |
| 4088 | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4089 | { |
| 4090 | unaryfunc func = (unaryfunc)wrapped; |
| 4091 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4092 | if (!check_num_args(args, 0)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4093 | return NULL; |
| 4094 | return (*func)(self); |
| 4095 | } |
| 4096 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4097 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4098 | wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4099 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4100 | ssizeargfunc func = (ssizeargfunc)wrapped; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4101 | PyObject* o; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4102 | Py_ssize_t i; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4103 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4104 | if (!PyArg_UnpackTuple(args, "", 1, 1, &o)) |
| 4105 | return NULL; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 4106 | i = PyNumber_AsSsize_t(o, PyExc_OverflowError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4107 | if (i == -1 && PyErr_Occurred()) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4108 | return NULL; |
| 4109 | return (*func)(self, i); |
| 4110 | } |
| 4111 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4112 | static Py_ssize_t |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4113 | getindex(PyObject *self, PyObject *arg) |
| 4114 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4115 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4116 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 4117 | i = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4118 | if (i == -1 && PyErr_Occurred()) |
| 4119 | return -1; |
| 4120 | if (i < 0) { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4121 | PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4122 | if (sq && sq->sq_length) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4123 | Py_ssize_t n = (*sq->sq_length)(self); |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4124 | if (n < 0) |
| 4125 | return -1; |
| 4126 | i += n; |
| 4127 | } |
| 4128 | } |
| 4129 | return i; |
| 4130 | } |
| 4131 | |
| 4132 | static PyObject * |
| 4133 | wrap_sq_item(PyObject *self, PyObject *args, void *wrapped) |
| 4134 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4135 | ssizeargfunc func = (ssizeargfunc)wrapped; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4136 | PyObject *arg; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4137 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4138 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4139 | if (PyTuple_GET_SIZE(args) == 1) { |
| 4140 | arg = PyTuple_GET_ITEM(args, 0); |
| 4141 | i = getindex(self, arg); |
| 4142 | if (i == -1 && PyErr_Occurred()) |
| 4143 | return NULL; |
| 4144 | return (*func)(self, i); |
| 4145 | } |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4146 | check_num_args(args, 1); |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4147 | assert(PyErr_Occurred()); |
| 4148 | return NULL; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4151 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4152 | wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4153 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4154 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4155 | Py_ssize_t i; |
| 4156 | int res; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4157 | PyObject *arg, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4158 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4159 | if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value)) |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4160 | return NULL; |
| 4161 | i = getindex(self, arg); |
| 4162 | if (i == -1 && PyErr_Occurred()) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4163 | return NULL; |
| 4164 | res = (*func)(self, i, value); |
| 4165 | if (res == -1 && PyErr_Occurred()) |
| 4166 | return NULL; |
| 4167 | Py_INCREF(Py_None); |
| 4168 | return Py_None; |
| 4169 | } |
| 4170 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4171 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4172 | wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4173 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4174 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4175 | Py_ssize_t i; |
| 4176 | int res; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4177 | PyObject *arg; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4178 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4179 | if (!check_num_args(args, 1)) |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4180 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4181 | arg = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4182 | i = getindex(self, arg); |
| 4183 | if (i == -1 && PyErr_Occurred()) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4184 | return NULL; |
| 4185 | res = (*func)(self, i, NULL); |
| 4186 | if (res == -1 && PyErr_Occurred()) |
| 4187 | return NULL; |
| 4188 | Py_INCREF(Py_None); |
| 4189 | return Py_None; |
| 4190 | } |
| 4191 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4192 | /* XXX objobjproc is a misnomer; should be objargpred */ |
| 4193 | static PyObject * |
| 4194 | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) |
| 4195 | { |
| 4196 | objobjproc func = (objobjproc)wrapped; |
| 4197 | int res; |
Guido van Rossum | 22c3dda | 2003-10-09 03:46:35 +0000 | [diff] [blame] | 4198 | PyObject *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4199 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4200 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4201 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4202 | value = PyTuple_GET_ITEM(args, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4203 | res = (*func)(self, value); |
| 4204 | if (res == -1 && PyErr_Occurred()) |
| 4205 | return NULL; |
Guido van Rossum | 22c3dda | 2003-10-09 03:46:35 +0000 | [diff] [blame] | 4206 | else |
| 4207 | return PyBool_FromLong(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4208 | } |
| 4209 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4210 | static PyObject * |
| 4211 | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 4212 | { |
| 4213 | objobjargproc func = (objobjargproc)wrapped; |
| 4214 | int res; |
| 4215 | PyObject *key, *value; |
| 4216 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4217 | if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4218 | return NULL; |
| 4219 | res = (*func)(self, key, value); |
| 4220 | if (res == -1 && PyErr_Occurred()) |
| 4221 | return NULL; |
| 4222 | Py_INCREF(Py_None); |
| 4223 | return Py_None; |
| 4224 | } |
| 4225 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4226 | static PyObject * |
| 4227 | wrap_delitem(PyObject *self, PyObject *args, void *wrapped) |
| 4228 | { |
| 4229 | objobjargproc func = (objobjargproc)wrapped; |
| 4230 | int res; |
| 4231 | PyObject *key; |
| 4232 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4233 | if (!check_num_args(args, 1)) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4234 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4235 | key = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4236 | res = (*func)(self, key, NULL); |
| 4237 | if (res == -1 && PyErr_Occurred()) |
| 4238 | return NULL; |
| 4239 | Py_INCREF(Py_None); |
| 4240 | return Py_None; |
| 4241 | } |
| 4242 | |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4243 | /* 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] | 4244 | This is called the Carlo Verre hack after its discoverer. */ |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4245 | static int |
| 4246 | hackcheck(PyObject *self, setattrofunc func, char *what) |
| 4247 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4248 | PyTypeObject *type = Py_TYPE(self); |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4249 | while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 4250 | type = type->tp_base; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4251 | /* If type is NULL now, this is a really weird type. |
| 4252 | In the spirit of backwards compatibility (?), just shut up. */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 4253 | if (type && type->tp_setattro != func) { |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4254 | PyErr_Format(PyExc_TypeError, |
| 4255 | "can't apply this %s to %s object", |
| 4256 | what, |
| 4257 | type->tp_name); |
| 4258 | return 0; |
| 4259 | } |
| 4260 | return 1; |
| 4261 | } |
| 4262 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4263 | static PyObject * |
| 4264 | wrap_setattr(PyObject *self, PyObject *args, void *wrapped) |
| 4265 | { |
| 4266 | setattrofunc func = (setattrofunc)wrapped; |
| 4267 | int res; |
| 4268 | PyObject *name, *value; |
| 4269 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4270 | if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4271 | return NULL; |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4272 | if (!hackcheck(self, func, "__setattr__")) |
| 4273 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4274 | res = (*func)(self, name, value); |
| 4275 | if (res < 0) |
| 4276 | return NULL; |
| 4277 | Py_INCREF(Py_None); |
| 4278 | return Py_None; |
| 4279 | } |
| 4280 | |
| 4281 | static PyObject * |
| 4282 | wrap_delattr(PyObject *self, PyObject *args, void *wrapped) |
| 4283 | { |
| 4284 | setattrofunc func = (setattrofunc)wrapped; |
| 4285 | int res; |
| 4286 | PyObject *name; |
| 4287 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4288 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4289 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4290 | name = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4291 | if (!hackcheck(self, func, "__delattr__")) |
| 4292 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4293 | res = (*func)(self, name, NULL); |
| 4294 | if (res < 0) |
| 4295 | return NULL; |
| 4296 | Py_INCREF(Py_None); |
| 4297 | return Py_None; |
| 4298 | } |
| 4299 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4300 | static PyObject * |
| 4301 | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4302 | { |
| 4303 | hashfunc func = (hashfunc)wrapped; |
| 4304 | long res; |
| 4305 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4306 | if (!check_num_args(args, 0)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4307 | return NULL; |
| 4308 | res = (*func)(self); |
| 4309 | if (res == -1 && PyErr_Occurred()) |
| 4310 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4311 | return PyLong_FromLong(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4312 | } |
| 4313 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4314 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4315 | wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4316 | { |
| 4317 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4318 | |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4319 | return (*func)(self, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4320 | } |
| 4321 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4322 | static PyObject * |
| 4323 | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) |
| 4324 | { |
| 4325 | richcmpfunc func = (richcmpfunc)wrapped; |
| 4326 | PyObject *other; |
| 4327 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4328 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4329 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4330 | other = PyTuple_GET_ITEM(args, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4331 | return (*func)(self, other, op); |
| 4332 | } |
| 4333 | |
| 4334 | #undef RICHCMP_WRAPPER |
| 4335 | #define RICHCMP_WRAPPER(NAME, OP) \ |
| 4336 | static PyObject * \ |
| 4337 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ |
| 4338 | { \ |
| 4339 | return wrap_richcmpfunc(self, args, wrapped, OP); \ |
| 4340 | } |
| 4341 | |
Jack Jansen | 8e938b4 | 2001-08-08 15:29:49 +0000 | [diff] [blame] | 4342 | RICHCMP_WRAPPER(lt, Py_LT) |
| 4343 | RICHCMP_WRAPPER(le, Py_LE) |
| 4344 | RICHCMP_WRAPPER(eq, Py_EQ) |
| 4345 | RICHCMP_WRAPPER(ne, Py_NE) |
| 4346 | RICHCMP_WRAPPER(gt, Py_GT) |
| 4347 | RICHCMP_WRAPPER(ge, Py_GE) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4348 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4349 | static PyObject * |
| 4350 | wrap_next(PyObject *self, PyObject *args, void *wrapped) |
| 4351 | { |
| 4352 | unaryfunc func = (unaryfunc)wrapped; |
| 4353 | PyObject *res; |
| 4354 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4355 | if (!check_num_args(args, 0)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4356 | return NULL; |
| 4357 | res = (*func)(self); |
| 4358 | if (res == NULL && !PyErr_Occurred()) |
| 4359 | PyErr_SetNone(PyExc_StopIteration); |
| 4360 | return res; |
| 4361 | } |
| 4362 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4363 | static PyObject * |
| 4364 | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped) |
| 4365 | { |
| 4366 | descrgetfunc func = (descrgetfunc)wrapped; |
| 4367 | PyObject *obj; |
| 4368 | PyObject *type = NULL; |
| 4369 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4370 | if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4371 | return NULL; |
Guido van Rossum | 82ed25c | 2003-02-11 16:25:43 +0000 | [diff] [blame] | 4372 | if (obj == Py_None) |
| 4373 | obj = NULL; |
| 4374 | if (type == Py_None) |
| 4375 | type = NULL; |
| 4376 | if (type == NULL &&obj == NULL) { |
| 4377 | PyErr_SetString(PyExc_TypeError, |
| 4378 | "__get__(None, None) is invalid"); |
| 4379 | return NULL; |
| 4380 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4381 | return (*func)(self, obj, type); |
| 4382 | } |
| 4383 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4384 | static PyObject * |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4385 | wrap_descr_set(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4386 | { |
| 4387 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4388 | PyObject *obj, *value; |
| 4389 | int ret; |
| 4390 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4391 | if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4392 | return NULL; |
| 4393 | ret = (*func)(self, obj, value); |
| 4394 | if (ret < 0) |
| 4395 | return NULL; |
| 4396 | Py_INCREF(Py_None); |
| 4397 | return Py_None; |
| 4398 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 4399 | |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4400 | static PyObject * |
| 4401 | wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped) |
| 4402 | { |
| 4403 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4404 | PyObject *obj; |
| 4405 | int ret; |
| 4406 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4407 | if (!check_num_args(args, 1)) |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4408 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4409 | obj = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4410 | ret = (*func)(self, obj, NULL); |
| 4411 | if (ret < 0) |
| 4412 | return NULL; |
| 4413 | Py_INCREF(Py_None); |
| 4414 | return Py_None; |
| 4415 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4416 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4417 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4418 | wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4419 | { |
| 4420 | initproc func = (initproc)wrapped; |
| 4421 | |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4422 | if (func(self, args, kwds) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4423 | return NULL; |
| 4424 | Py_INCREF(Py_None); |
| 4425 | return Py_None; |
| 4426 | } |
| 4427 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4428 | static PyObject * |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4429 | tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4430 | { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4431 | PyTypeObject *type, *subtype, *staticbase; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4432 | PyObject *arg0, *res; |
| 4433 | |
| 4434 | if (self == NULL || !PyType_Check(self)) |
| 4435 | Py_FatalError("__new__() called with non-type 'self'"); |
| 4436 | type = (PyTypeObject *)self; |
| 4437 | if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4438 | PyErr_Format(PyExc_TypeError, |
| 4439 | "%s.__new__(): not enough arguments", |
| 4440 | type->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4441 | return NULL; |
| 4442 | } |
| 4443 | arg0 = PyTuple_GET_ITEM(args, 0); |
| 4444 | if (!PyType_Check(arg0)) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4445 | PyErr_Format(PyExc_TypeError, |
| 4446 | "%s.__new__(X): X is not a type object (%s)", |
| 4447 | type->tp_name, |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4448 | Py_TYPE(arg0)->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4449 | return NULL; |
| 4450 | } |
| 4451 | subtype = (PyTypeObject *)arg0; |
| 4452 | if (!PyType_IsSubtype(subtype, type)) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4453 | PyErr_Format(PyExc_TypeError, |
| 4454 | "%s.__new__(%s): %s is not a subtype of %s", |
| 4455 | type->tp_name, |
| 4456 | subtype->tp_name, |
| 4457 | subtype->tp_name, |
| 4458 | type->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4459 | return NULL; |
| 4460 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4461 | |
| 4462 | /* Check that the use doesn't do something silly and unsafe like |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 4463 | object.__new__(dict). To do this, we check that the |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4464 | most derived base that's not a heap type is this type. */ |
| 4465 | staticbase = subtype; |
| 4466 | while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 4467 | staticbase = staticbase->tp_base; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4468 | /* If staticbase is NULL now, it is a really weird type. |
| 4469 | In the spirit of backwards compatibility (?), just shut up. */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 4470 | if (staticbase && staticbase->tp_new != type->tp_new) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4471 | PyErr_Format(PyExc_TypeError, |
| 4472 | "%s.__new__(%s) is not safe, use %s.__new__()", |
| 4473 | type->tp_name, |
| 4474 | subtype->tp_name, |
| 4475 | staticbase == NULL ? "?" : staticbase->tp_name); |
| 4476 | return NULL; |
| 4477 | } |
| 4478 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4479 | args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 4480 | if (args == NULL) |
| 4481 | return NULL; |
| 4482 | res = type->tp_new(subtype, args, kwds); |
| 4483 | Py_DECREF(args); |
| 4484 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4485 | } |
| 4486 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4487 | static struct PyMethodDef tp_new_methoddef[] = { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4488 | {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 4489 | PyDoc_STR("T.__new__(S, ...) -> " |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4490 | "a new object with type S, a subtype of T")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4491 | {0} |
| 4492 | }; |
| 4493 | |
| 4494 | static int |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4495 | add_tp_new_wrapper(PyTypeObject *type) |
| 4496 | { |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4497 | PyObject *func; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4498 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 4499 | if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL) |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4500 | return 0; |
| 4501 | func = PyCFunction_New(tp_new_methoddef, (PyObject *)type); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4502 | if (func == NULL) |
| 4503 | return -1; |
Raymond Hettinger | 8d726ee | 2004-06-25 22:24:35 +0000 | [diff] [blame] | 4504 | if (PyDict_SetItemString(type->tp_dict, "__new__", func)) { |
Raymond Hettinger | d56cbe5 | 2004-06-25 22:17:39 +0000 | [diff] [blame] | 4505 | Py_DECREF(func); |
| 4506 | return -1; |
| 4507 | } |
| 4508 | Py_DECREF(func); |
| 4509 | return 0; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4510 | } |
| 4511 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4512 | /* Slot wrappers that call the corresponding __foo__ slot. See comments |
| 4513 | below at override_slots() for more explanation. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4514 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4515 | #define SLOT0(FUNCNAME, OPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4516 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4517 | FUNCNAME(PyObject *self) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4518 | { \ |
Guido van Rossum | 5592e4d | 2001-08-28 18:28:21 +0000 | [diff] [blame] | 4519 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4520 | return call_method(self, OPSTR, &cache_str, "()"); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4521 | } |
| 4522 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4523 | #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4524 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4525 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4526 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4527 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4528 | return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4529 | } |
| 4530 | |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4531 | /* Boolean helper for SLOT1BINFULL(). |
| 4532 | right.__class__ is a nontrivial subclass of left.__class__. */ |
| 4533 | static int |
| 4534 | method_is_overloaded(PyObject *left, PyObject *right, char *name) |
| 4535 | { |
| 4536 | PyObject *a, *b; |
| 4537 | int ok; |
| 4538 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4539 | b = PyObject_GetAttrString((PyObject *)(Py_TYPE(right)), name); |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4540 | if (b == NULL) { |
| 4541 | PyErr_Clear(); |
| 4542 | /* If right doesn't have it, it's not overloaded */ |
| 4543 | return 0; |
| 4544 | } |
| 4545 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4546 | a = PyObject_GetAttrString((PyObject *)(Py_TYPE(left)), name); |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4547 | if (a == NULL) { |
| 4548 | PyErr_Clear(); |
| 4549 | Py_DECREF(b); |
| 4550 | /* If right has it but left doesn't, it's overloaded */ |
| 4551 | return 1; |
| 4552 | } |
| 4553 | |
| 4554 | ok = PyObject_RichCompareBool(a, b, Py_NE); |
| 4555 | Py_DECREF(a); |
| 4556 | Py_DECREF(b); |
| 4557 | if (ok < 0) { |
| 4558 | PyErr_Clear(); |
| 4559 | return 0; |
| 4560 | } |
| 4561 | |
| 4562 | return ok; |
| 4563 | } |
| 4564 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4565 | |
| 4566 | #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4567 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4568 | FUNCNAME(PyObject *self, PyObject *other) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4569 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4570 | static PyObject *cache_str, *rcache_str; \ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4571 | int do_other = Py_TYPE(self) != Py_TYPE(other) && \ |
| 4572 | Py_TYPE(other)->tp_as_number != NULL && \ |
| 4573 | Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \ |
| 4574 | if (Py_TYPE(self)->tp_as_number != NULL && \ |
| 4575 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4576 | PyObject *r; \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4577 | if (do_other && \ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4578 | PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \ |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4579 | method_is_overloaded(self, other, ROPSTR)) { \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4580 | r = call_maybe( \ |
| 4581 | other, ROPSTR, &rcache_str, "(O)", self); \ |
| 4582 | if (r != Py_NotImplemented) \ |
| 4583 | return r; \ |
| 4584 | Py_DECREF(r); \ |
| 4585 | do_other = 0; \ |
| 4586 | } \ |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 4587 | r = call_maybe( \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4588 | self, OPSTR, &cache_str, "(O)", other); \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4589 | if (r != Py_NotImplemented || \ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4590 | Py_TYPE(other) == Py_TYPE(self)) \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4591 | return r; \ |
| 4592 | Py_DECREF(r); \ |
| 4593 | } \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4594 | if (do_other) { \ |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 4595 | return call_maybe( \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4596 | other, ROPSTR, &rcache_str, "(O)", self); \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4597 | } \ |
| 4598 | Py_INCREF(Py_NotImplemented); \ |
| 4599 | return Py_NotImplemented; \ |
| 4600 | } |
| 4601 | |
| 4602 | #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ |
| 4603 | SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR) |
| 4604 | |
| 4605 | #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \ |
| 4606 | static PyObject * \ |
| 4607 | FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ |
| 4608 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4609 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4610 | return call_method(self, OPSTR, &cache_str, \ |
| 4611 | "(" ARGCODES ")", arg1, arg2); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4612 | } |
| 4613 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4614 | static Py_ssize_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4615 | slot_sq_length(PyObject *self) |
| 4616 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4617 | static PyObject *len_str; |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4618 | PyObject *res = call_method(self, "__len__", &len_str, "()"); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4619 | Py_ssize_t len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4620 | |
| 4621 | if (res == NULL) |
| 4622 | return -1; |
Benjamin Peterson | ee1ae7c | 2009-02-08 21:07:20 +0000 | [diff] [blame] | 4623 | len = PyNumber_AsSsize_t(res, PyExc_OverflowError); |
Guido van Rossum | 2611162 | 2001-10-01 16:42:49 +0000 | [diff] [blame] | 4624 | Py_DECREF(res); |
Jeremy Hylton | f20fcf9 | 2002-07-25 16:06:15 +0000 | [diff] [blame] | 4625 | if (len < 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4626 | if (!PyErr_Occurred()) |
| 4627 | PyErr_SetString(PyExc_ValueError, |
| 4628 | "__len__() should return >= 0"); |
Jeremy Hylton | f20fcf9 | 2002-07-25 16:06:15 +0000 | [diff] [blame] | 4629 | return -1; |
| 4630 | } |
Guido van Rossum | 2611162 | 2001-10-01 16:42:49 +0000 | [diff] [blame] | 4631 | return len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4632 | } |
| 4633 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4634 | /* Super-optimized version of slot_sq_item. |
| 4635 | Other slots could do the same... */ |
| 4636 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4637 | slot_sq_item(PyObject *self, Py_ssize_t i) |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4638 | { |
| 4639 | static PyObject *getitem_str; |
| 4640 | PyObject *func, *args = NULL, *ival = NULL, *retval = NULL; |
| 4641 | descrgetfunc f; |
| 4642 | |
| 4643 | if (getitem_str == NULL) { |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 4644 | getitem_str = PyUnicode_InternFromString("__getitem__"); |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4645 | if (getitem_str == NULL) |
| 4646 | return NULL; |
| 4647 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4648 | func = _PyType_Lookup(Py_TYPE(self), getitem_str); |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4649 | if (func != NULL) { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4650 | if ((f = Py_TYPE(func)->tp_descr_get) == NULL) |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4651 | Py_INCREF(func); |
Neal Norwitz | 673cd82 | 2002-10-18 16:33:13 +0000 | [diff] [blame] | 4652 | else { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4653 | func = f(func, self, (PyObject *)(Py_TYPE(self))); |
Neal Norwitz | 673cd82 | 2002-10-18 16:33:13 +0000 | [diff] [blame] | 4654 | if (func == NULL) { |
| 4655 | return NULL; |
| 4656 | } |
| 4657 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4658 | ival = PyLong_FromSsize_t(i); |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4659 | if (ival != NULL) { |
| 4660 | args = PyTuple_New(1); |
| 4661 | if (args != NULL) { |
| 4662 | PyTuple_SET_ITEM(args, 0, ival); |
| 4663 | retval = PyObject_Call(func, args, NULL); |
| 4664 | Py_XDECREF(args); |
| 4665 | Py_XDECREF(func); |
| 4666 | return retval; |
| 4667 | } |
| 4668 | } |
| 4669 | } |
| 4670 | else { |
| 4671 | PyErr_SetObject(PyExc_AttributeError, getitem_str); |
| 4672 | } |
| 4673 | Py_XDECREF(args); |
| 4674 | Py_XDECREF(ival); |
| 4675 | Py_XDECREF(func); |
| 4676 | return NULL; |
| 4677 | } |
| 4678 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4679 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4680 | slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4681 | { |
| 4682 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4683 | static PyObject *delitem_str, *setitem_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4684 | |
| 4685 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4686 | res = call_method(self, "__delitem__", &delitem_str, |
Thomas Wouters | 3fc2ca3 | 2006-04-21 11:28:17 +0000 | [diff] [blame] | 4687 | "(n)", index); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4688 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4689 | res = call_method(self, "__setitem__", &setitem_str, |
Thomas Wouters | 3fc2ca3 | 2006-04-21 11:28:17 +0000 | [diff] [blame] | 4690 | "(nO)", index, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4691 | if (res == NULL) |
| 4692 | return -1; |
| 4693 | Py_DECREF(res); |
| 4694 | return 0; |
| 4695 | } |
| 4696 | |
| 4697 | static int |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4698 | slot_sq_contains(PyObject *self, PyObject *value) |
| 4699 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4700 | PyObject *func, *res, *args; |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 4701 | int result = -1; |
| 4702 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4703 | static PyObject *contains_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4704 | |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4705 | func = lookup_maybe(self, "__contains__", &contains_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4706 | if (func != NULL) { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4707 | args = PyTuple_Pack(1, value); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4708 | if (args == NULL) |
| 4709 | res = NULL; |
| 4710 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4711 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4712 | Py_DECREF(args); |
| 4713 | } |
| 4714 | Py_DECREF(func); |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 4715 | if (res != NULL) { |
| 4716 | result = PyObject_IsTrue(res); |
| 4717 | Py_DECREF(res); |
| 4718 | } |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4719 | } |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 4720 | else if (! PyErr_Occurred()) { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4721 | /* Possible results: -1 and 1 */ |
| 4722 | result = (int)_PySequence_IterSearch(self, value, |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 4723 | PY_ITERSEARCH_CONTAINS); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4724 | } |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 4725 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4726 | } |
| 4727 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4728 | #define slot_mp_length slot_sq_length |
| 4729 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4730 | SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4731 | |
| 4732 | static int |
| 4733 | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) |
| 4734 | { |
| 4735 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4736 | static PyObject *delitem_str, *setitem_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4737 | |
| 4738 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4739 | res = call_method(self, "__delitem__", &delitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4740 | "(O)", key); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4741 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4742 | res = call_method(self, "__setitem__", &setitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4743 | "(OO)", key, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4744 | if (res == NULL) |
| 4745 | return -1; |
| 4746 | Py_DECREF(res); |
| 4747 | return 0; |
| 4748 | } |
| 4749 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4750 | SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__") |
| 4751 | SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__") |
| 4752 | SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4753 | SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__") |
| 4754 | SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__") |
| 4755 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 4756 | static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4757 | |
| 4758 | SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, |
| 4759 | nb_power, "__pow__", "__rpow__") |
| 4760 | |
| 4761 | static PyObject * |
| 4762 | slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) |
| 4763 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4764 | static PyObject *pow_str; |
| 4765 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4766 | if (modulus == Py_None) |
| 4767 | return slot_nb_power_binary(self, other); |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 4768 | /* Three-arg power doesn't use __rpow__. But ternary_op |
| 4769 | can call this when the second argument's type uses |
| 4770 | slot_nb_power, so check before calling self.__pow__. */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4771 | if (Py_TYPE(self)->tp_as_number != NULL && |
| 4772 | Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) { |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 4773 | return call_method(self, "__pow__", &pow_str, |
| 4774 | "(OO)", other, modulus); |
| 4775 | } |
| 4776 | Py_INCREF(Py_NotImplemented); |
| 4777 | return Py_NotImplemented; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4778 | } |
| 4779 | |
| 4780 | SLOT0(slot_nb_negative, "__neg__") |
| 4781 | SLOT0(slot_nb_positive, "__pos__") |
| 4782 | SLOT0(slot_nb_absolute, "__abs__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4783 | |
| 4784 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 4785 | slot_nb_bool(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4786 | { |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 4787 | PyObject *func, *args; |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 4788 | static PyObject *bool_str, *len_str; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 4789 | int result = -1; |
Benjamin Peterson | f07d002 | 2009-03-21 17:31:58 +0000 | [diff] [blame] | 4790 | int using_len = 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4791 | |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 4792 | func = lookup_maybe(self, "__bool__", &bool_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4793 | if (func == NULL) { |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4794 | if (PyErr_Occurred()) |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4795 | return -1; |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4796 | func = lookup_maybe(self, "__len__", &len_str); |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 4797 | if (func == NULL) |
| 4798 | return PyErr_Occurred() ? -1 : 1; |
Benjamin Peterson | f07d002 | 2009-03-21 17:31:58 +0000 | [diff] [blame] | 4799 | using_len = 1; |
| 4800 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 4801 | args = PyTuple_New(0); |
| 4802 | if (args != NULL) { |
| 4803 | PyObject *temp = PyObject_Call(func, args, NULL); |
| 4804 | Py_DECREF(args); |
| 4805 | if (temp != NULL) { |
Benjamin Peterson | f07d002 | 2009-03-21 17:31:58 +0000 | [diff] [blame] | 4806 | if (using_len) { |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 4807 | /* enforced by slot_nb_len */ |
Jeremy Hylton | 090a349 | 2003-06-27 16:46:45 +0000 | [diff] [blame] | 4808 | result = PyObject_IsTrue(temp); |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 4809 | } |
| 4810 | else if (PyBool_Check(temp)) { |
| 4811 | result = PyObject_IsTrue(temp); |
| 4812 | } |
Jeremy Hylton | 090a349 | 2003-06-27 16:46:45 +0000 | [diff] [blame] | 4813 | else { |
| 4814 | PyErr_Format(PyExc_TypeError, |
Amaury Forgeot d'Arc | 097cd07 | 2009-07-07 00:43:08 +0000 | [diff] [blame] | 4815 | "__bool__ should return " |
| 4816 | "bool, returned %s", |
Benjamin Peterson | f07d002 | 2009-03-21 17:31:58 +0000 | [diff] [blame] | 4817 | Py_TYPE(temp)->tp_name); |
Jeremy Hylton | 3e3159c | 2003-06-27 17:38:27 +0000 | [diff] [blame] | 4818 | result = -1; |
Jeremy Hylton | 090a349 | 2003-06-27 16:46:45 +0000 | [diff] [blame] | 4819 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 4820 | Py_DECREF(temp); |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4821 | } |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4822 | } |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4823 | Py_DECREF(func); |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 4824 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4825 | } |
| 4826 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4827 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 4828 | static PyObject * |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4829 | slot_nb_index(PyObject *self) |
| 4830 | { |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4831 | static PyObject *index_str; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 4832 | return call_method(self, "__index__", &index_str, "()"); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4833 | } |
| 4834 | |
| 4835 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4836 | SLOT0(slot_nb_invert, "__invert__") |
| 4837 | SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") |
| 4838 | SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") |
| 4839 | SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") |
| 4840 | SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") |
| 4841 | SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4842 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4843 | SLOT0(slot_nb_int, "__int__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4844 | SLOT0(slot_nb_float, "__float__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4845 | SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O") |
| 4846 | SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O") |
| 4847 | SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4848 | SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4849 | /* Can't use SLOT1 here, because nb_inplace_power is ternary */ |
| 4850 | static PyObject * |
| 4851 | slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2) |
| 4852 | { |
| 4853 | static PyObject *cache_str; |
| 4854 | return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1); |
| 4855 | } |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4856 | SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O") |
| 4857 | SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O") |
| 4858 | SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O") |
| 4859 | SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O") |
| 4860 | SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O") |
| 4861 | SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, |
| 4862 | "__floordiv__", "__rfloordiv__") |
| 4863 | SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__") |
| 4864 | SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O") |
| 4865 | SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4866 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4867 | static PyObject * |
| 4868 | slot_tp_repr(PyObject *self) |
| 4869 | { |
| 4870 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4871 | static PyObject *repr_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4872 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4873 | func = lookup_method(self, "__repr__", &repr_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4874 | if (func != NULL) { |
| 4875 | res = PyEval_CallObject(func, NULL); |
| 4876 | Py_DECREF(func); |
| 4877 | return res; |
| 4878 | } |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 4879 | PyErr_Clear(); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 4880 | return PyUnicode_FromFormat("<%s object at %p>", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4881 | Py_TYPE(self)->tp_name, self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4882 | } |
| 4883 | |
| 4884 | static PyObject * |
| 4885 | slot_tp_str(PyObject *self) |
| 4886 | { |
| 4887 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4888 | static PyObject *str_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4889 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4890 | func = lookup_method(self, "__str__", &str_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4891 | if (func != NULL) { |
| 4892 | res = PyEval_CallObject(func, NULL); |
| 4893 | Py_DECREF(func); |
| 4894 | return res; |
| 4895 | } |
| 4896 | else { |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 4897 | PyObject *ress; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4898 | PyErr_Clear(); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 4899 | res = slot_tp_repr(self); |
| 4900 | if (!res) |
| 4901 | return NULL; |
| 4902 | ress = _PyUnicode_AsDefaultEncodedString(res, NULL); |
| 4903 | Py_DECREF(res); |
| 4904 | return ress; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4905 | } |
| 4906 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4907 | |
| 4908 | static long |
| 4909 | slot_tp_hash(PyObject *self) |
| 4910 | { |
Guido van Rossum | 4011a24 | 2006-08-17 23:09:57 +0000 | [diff] [blame] | 4911 | PyObject *func, *res; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 4912 | static PyObject *hash_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4913 | long h; |
| 4914 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4915 | func = lookup_method(self, "__hash__", &hash_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4916 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 4917 | if (func == Py_None) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4918 | Py_DECREF(func); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 4919 | func = NULL; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4920 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 4921 | |
| 4922 | if (func == NULL) { |
Nick Coghlan | d1abd25 | 2008-07-15 15:46:38 +0000 | [diff] [blame] | 4923 | return PyObject_HashNotImplemented(self); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
Guido van Rossum | 4011a24 | 2006-08-17 23:09:57 +0000 | [diff] [blame] | 4926 | res = PyEval_CallObject(func, NULL); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 4927 | Py_DECREF(func); |
| 4928 | if (res == NULL) |
| 4929 | return -1; |
| 4930 | if (PyLong_Check(res)) |
| 4931 | h = PyLong_Type.tp_hash(res); |
| 4932 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4933 | h = PyLong_AsLong(res); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 4934 | Py_DECREF(res); |
| 4935 | if (h == -1 && !PyErr_Occurred()) |
| 4936 | h = -2; |
| 4937 | return h; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4938 | } |
| 4939 | |
| 4940 | static PyObject * |
| 4941 | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 4942 | { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 4943 | static PyObject *call_str; |
| 4944 | PyObject *meth = lookup_method(self, "__call__", &call_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4945 | PyObject *res; |
| 4946 | |
| 4947 | if (meth == NULL) |
| 4948 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4949 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4950 | res = PyObject_Call(meth, args, kwds); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4951 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4952 | Py_DECREF(meth); |
| 4953 | return res; |
| 4954 | } |
| 4955 | |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 4956 | /* There are two slot dispatch functions for tp_getattro. |
| 4957 | |
| 4958 | - slot_tp_getattro() is used when __getattribute__ is overridden |
| 4959 | but no __getattr__ hook is present; |
| 4960 | |
| 4961 | - slot_tp_getattr_hook() is used when a __getattr__ hook is present. |
| 4962 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4963 | The code in update_one_slot() always installs slot_tp_getattr_hook(); this |
| 4964 | detects the absence of __getattr__ and then installs the simpler slot if |
| 4965 | necessary. */ |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 4966 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4967 | static PyObject * |
| 4968 | slot_tp_getattro(PyObject *self, PyObject *name) |
| 4969 | { |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 4970 | static PyObject *getattribute_str = NULL; |
| 4971 | return call_method(self, "__getattribute__", &getattribute_str, |
| 4972 | "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4973 | } |
| 4974 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 4975 | static PyObject * |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 4976 | call_attribute(PyObject *self, PyObject *attr, PyObject *name) |
| 4977 | { |
| 4978 | PyObject *res, *descr = NULL; |
| 4979 | descrgetfunc f = Py_TYPE(attr)->tp_descr_get; |
| 4980 | |
| 4981 | if (f != NULL) { |
| 4982 | descr = f(attr, self, (PyObject *)(Py_TYPE(self))); |
| 4983 | if (descr == NULL) |
| 4984 | return NULL; |
| 4985 | else |
| 4986 | attr = descr; |
| 4987 | } |
| 4988 | res = PyObject_CallFunctionObjArgs(attr, name, NULL); |
| 4989 | Py_XDECREF(descr); |
| 4990 | return res; |
| 4991 | } |
| 4992 | |
| 4993 | static PyObject * |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 4994 | slot_tp_getattr_hook(PyObject *self, PyObject *name) |
| 4995 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4996 | PyTypeObject *tp = Py_TYPE(self); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 4997 | PyObject *getattr, *getattribute, *res; |
| 4998 | static PyObject *getattribute_str = NULL; |
| 4999 | static PyObject *getattr_str = NULL; |
| 5000 | |
| 5001 | if (getattr_str == NULL) { |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 5002 | getattr_str = PyUnicode_InternFromString("__getattr__"); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5003 | if (getattr_str == NULL) |
| 5004 | return NULL; |
| 5005 | } |
| 5006 | if (getattribute_str == NULL) { |
| 5007 | getattribute_str = |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 5008 | PyUnicode_InternFromString("__getattribute__"); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5009 | if (getattribute_str == NULL) |
| 5010 | return NULL; |
| 5011 | } |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5012 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5013 | method fully for each attribute lookup for classes with |
| 5014 | __getattr__, even when the attribute is present. So we use |
| 5015 | _PyType_Lookup and create the method only when needed, with |
| 5016 | call_attribute. */ |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5017 | getattr = _PyType_Lookup(tp, getattr_str); |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5018 | if (getattr == NULL) { |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5019 | /* No __getattr__ hook: use a simpler dispatcher */ |
| 5020 | tp->tp_getattro = slot_tp_getattro; |
| 5021 | return slot_tp_getattro(self, name); |
| 5022 | } |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5023 | Py_INCREF(getattr); |
| 5024 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5025 | method fully for each attribute lookup for classes with |
| 5026 | __getattr__, even when self has the default __getattribute__ |
| 5027 | method. So we use _PyType_Lookup and create the method only when |
| 5028 | needed, with call_attribute. */ |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5029 | getattribute = _PyType_Lookup(tp, getattribute_str); |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5030 | if (getattribute == NULL || |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5031 | (Py_TYPE(getattribute) == &PyWrapperDescr_Type && |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5032 | ((PyWrapperDescrObject *)getattribute)->d_wrapped == |
| 5033 | (void *)PyObject_GenericGetAttr)) |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5034 | res = PyObject_GenericGetAttr(self, name); |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5035 | else { |
| 5036 | Py_INCREF(getattribute); |
| 5037 | res = call_attribute(self, getattribute, name); |
| 5038 | Py_DECREF(getattribute); |
| 5039 | } |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5040 | if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5041 | PyErr_Clear(); |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5042 | res = call_attribute(self, getattr, name); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5043 | } |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5044 | Py_DECREF(getattr); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5045 | return res; |
| 5046 | } |
| 5047 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5048 | static int |
| 5049 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) |
| 5050 | { |
| 5051 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5052 | static PyObject *delattr_str, *setattr_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5053 | |
| 5054 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5055 | res = call_method(self, "__delattr__", &delattr_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5056 | "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5057 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5058 | res = call_method(self, "__setattr__", &setattr_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5059 | "(OO)", name, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5060 | if (res == NULL) |
| 5061 | return -1; |
| 5062 | Py_DECREF(res); |
| 5063 | return 0; |
| 5064 | } |
| 5065 | |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 5066 | static char *name_op[] = { |
| 5067 | "__lt__", |
| 5068 | "__le__", |
| 5069 | "__eq__", |
| 5070 | "__ne__", |
| 5071 | "__gt__", |
| 5072 | "__ge__", |
| 5073 | }; |
| 5074 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5075 | static PyObject * |
Mark Dickinson | 6f1d049 | 2009-11-15 13:58:49 +0000 | [diff] [blame] | 5076 | slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5077 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5078 | PyObject *func, *args, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5079 | static PyObject *op_str[6]; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5080 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5081 | func = lookup_method(self, name_op[op], &op_str[op]); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5082 | if (func == NULL) { |
| 5083 | PyErr_Clear(); |
| 5084 | Py_INCREF(Py_NotImplemented); |
| 5085 | return Py_NotImplemented; |
| 5086 | } |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 5087 | args = PyTuple_Pack(1, other); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5088 | if (args == NULL) |
| 5089 | res = NULL; |
| 5090 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5091 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5092 | Py_DECREF(args); |
| 5093 | } |
| 5094 | Py_DECREF(func); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5095 | return res; |
| 5096 | } |
| 5097 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5098 | static PyObject * |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5099 | slot_tp_iter(PyObject *self) |
| 5100 | { |
| 5101 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5102 | static PyObject *iter_str, *getitem_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5103 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5104 | func = lookup_method(self, "__iter__", &iter_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5105 | if (func != NULL) { |
Guido van Rossum | 84b2bed | 2002-08-16 17:01:09 +0000 | [diff] [blame] | 5106 | PyObject *args; |
| 5107 | args = res = PyTuple_New(0); |
| 5108 | if (args != NULL) { |
| 5109 | res = PyObject_Call(func, args, NULL); |
| 5110 | Py_DECREF(args); |
| 5111 | } |
| 5112 | Py_DECREF(func); |
| 5113 | return res; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5114 | } |
| 5115 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5116 | func = lookup_method(self, "__getitem__", &getitem_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5117 | if (func == NULL) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5118 | PyErr_Format(PyExc_TypeError, |
| 5119 | "'%.200s' object is not iterable", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5120 | Py_TYPE(self)->tp_name); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5121 | return NULL; |
| 5122 | } |
| 5123 | Py_DECREF(func); |
| 5124 | return PySeqIter_New(self); |
| 5125 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5126 | |
| 5127 | static PyObject * |
| 5128 | slot_tp_iternext(PyObject *self) |
| 5129 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5130 | static PyObject *next_str; |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 5131 | return call_method(self, "__next__", &next_str, "()"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5132 | } |
| 5133 | |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5134 | static PyObject * |
| 5135 | slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 5136 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5137 | PyTypeObject *tp = Py_TYPE(self); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5138 | PyObject *get; |
| 5139 | static PyObject *get_str = NULL; |
| 5140 | |
| 5141 | if (get_str == NULL) { |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 5142 | get_str = PyUnicode_InternFromString("__get__"); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5143 | if (get_str == NULL) |
| 5144 | return NULL; |
| 5145 | } |
| 5146 | get = _PyType_Lookup(tp, get_str); |
| 5147 | if (get == NULL) { |
| 5148 | /* Avoid further slowdowns */ |
| 5149 | if (tp->tp_descr_get == slot_tp_descr_get) |
| 5150 | tp->tp_descr_get = NULL; |
| 5151 | Py_INCREF(self); |
| 5152 | return self; |
| 5153 | } |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5154 | if (obj == NULL) |
| 5155 | obj = Py_None; |
| 5156 | if (type == NULL) |
| 5157 | type = Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5158 | return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5159 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5160 | |
| 5161 | static int |
| 5162 | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) |
| 5163 | { |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5164 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5165 | static PyObject *del_str, *set_str; |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5166 | |
| 5167 | if (value == NULL) |
Guido van Rossum | 1d5b3f2 | 2001-12-03 00:08:33 +0000 | [diff] [blame] | 5168 | res = call_method(self, "__delete__", &del_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5169 | "(O)", target); |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5170 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5171 | res = call_method(self, "__set__", &set_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5172 | "(OO)", target, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5173 | if (res == NULL) |
| 5174 | return -1; |
| 5175 | Py_DECREF(res); |
| 5176 | return 0; |
| 5177 | } |
| 5178 | |
| 5179 | static int |
| 5180 | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 5181 | { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5182 | static PyObject *init_str; |
| 5183 | PyObject *meth = lookup_method(self, "__init__", &init_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5184 | PyObject *res; |
| 5185 | |
| 5186 | if (meth == NULL) |
| 5187 | return -1; |
| 5188 | res = PyObject_Call(meth, args, kwds); |
| 5189 | Py_DECREF(meth); |
| 5190 | if (res == NULL) |
| 5191 | return -1; |
Raymond Hettinger | b67cc80 | 2005-03-03 16:45:19 +0000 | [diff] [blame] | 5192 | if (res != Py_None) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5193 | PyErr_Format(PyExc_TypeError, |
| 5194 | "__init__() should return None, not '%.200s'", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5195 | Py_TYPE(res)->tp_name); |
Raymond Hettinger | b67cc80 | 2005-03-03 16:45:19 +0000 | [diff] [blame] | 5196 | Py_DECREF(res); |
| 5197 | return -1; |
| 5198 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5199 | Py_DECREF(res); |
| 5200 | return 0; |
| 5201 | } |
| 5202 | |
| 5203 | static PyObject * |
| 5204 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 5205 | { |
Guido van Rossum | 7bed213 | 2002-08-08 21:57:53 +0000 | [diff] [blame] | 5206 | static PyObject *new_str; |
| 5207 | PyObject *func; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5208 | PyObject *newargs, *x; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5209 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5210 | |
Guido van Rossum | 7bed213 | 2002-08-08 21:57:53 +0000 | [diff] [blame] | 5211 | if (new_str == NULL) { |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 5212 | new_str = PyUnicode_InternFromString("__new__"); |
Guido van Rossum | 7bed213 | 2002-08-08 21:57:53 +0000 | [diff] [blame] | 5213 | if (new_str == NULL) |
| 5214 | return NULL; |
| 5215 | } |
| 5216 | func = PyObject_GetAttr((PyObject *)type, new_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5217 | if (func == NULL) |
| 5218 | return NULL; |
| 5219 | assert(PyTuple_Check(args)); |
| 5220 | n = PyTuple_GET_SIZE(args); |
| 5221 | newargs = PyTuple_New(n+1); |
| 5222 | if (newargs == NULL) |
| 5223 | return NULL; |
| 5224 | Py_INCREF(type); |
| 5225 | PyTuple_SET_ITEM(newargs, 0, (PyObject *)type); |
| 5226 | for (i = 0; i < n; i++) { |
| 5227 | x = PyTuple_GET_ITEM(args, i); |
| 5228 | Py_INCREF(x); |
| 5229 | PyTuple_SET_ITEM(newargs, i+1, x); |
| 5230 | } |
| 5231 | x = PyObject_Call(func, newargs, kwds); |
Guido van Rossum | 25d1807 | 2001-10-01 15:55:28 +0000 | [diff] [blame] | 5232 | Py_DECREF(newargs); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5233 | Py_DECREF(func); |
| 5234 | return x; |
| 5235 | } |
| 5236 | |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5237 | static void |
| 5238 | slot_tp_del(PyObject *self) |
| 5239 | { |
| 5240 | static PyObject *del_str = NULL; |
| 5241 | PyObject *del, *res; |
| 5242 | PyObject *error_type, *error_value, *error_traceback; |
| 5243 | |
| 5244 | /* Temporarily resurrect the object. */ |
| 5245 | assert(self->ob_refcnt == 0); |
| 5246 | self->ob_refcnt = 1; |
| 5247 | |
| 5248 | /* Save the current exception, if any. */ |
| 5249 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 5250 | |
| 5251 | /* Execute __del__ method, if any. */ |
| 5252 | del = lookup_maybe(self, "__del__", &del_str); |
| 5253 | if (del != NULL) { |
| 5254 | res = PyEval_CallObject(del, NULL); |
| 5255 | if (res == NULL) |
| 5256 | PyErr_WriteUnraisable(del); |
| 5257 | else |
| 5258 | Py_DECREF(res); |
| 5259 | Py_DECREF(del); |
| 5260 | } |
| 5261 | |
| 5262 | /* Restore the saved exception. */ |
| 5263 | PyErr_Restore(error_type, error_value, error_traceback); |
| 5264 | |
| 5265 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 5266 | * cause a recursive call. |
| 5267 | */ |
| 5268 | assert(self->ob_refcnt > 0); |
| 5269 | if (--self->ob_refcnt == 0) |
| 5270 | return; /* this is the normal path out */ |
| 5271 | |
| 5272 | /* __del__ resurrected it! Make it look like the original Py_DECREF |
| 5273 | * never happened. |
| 5274 | */ |
| 5275 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 5276 | Py_ssize_t refcnt = self->ob_refcnt; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5277 | _Py_NewReference(self); |
| 5278 | self->ob_refcnt = refcnt; |
| 5279 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5280 | assert(!PyType_IS_GC(Py_TYPE(self)) || |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5281 | _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED); |
Michael W. Hudson | 3f3b668 | 2004-08-03 10:21:03 +0000 | [diff] [blame] | 5282 | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so |
| 5283 | * we need to undo that. */ |
| 5284 | _Py_DEC_REFTOTAL; |
| 5285 | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object |
| 5286 | * chain, so no more to do there. |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5287 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
| 5288 | * _Py_NewReference bumped tp_allocs: both of those need to be |
| 5289 | * undone. |
| 5290 | */ |
| 5291 | #ifdef COUNT_ALLOCS |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5292 | --Py_TYPE(self)->tp_frees; |
| 5293 | --Py_TYPE(self)->tp_allocs; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5294 | #endif |
| 5295 | } |
| 5296 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5297 | |
| 5298 | /* 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] | 5299 | functions. The offsets here are relative to the 'PyHeapTypeObject' |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5300 | structure, which incorporates the additional structures used for numbers, |
| 5301 | sequences and mappings. |
| 5302 | 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] | 5303 | __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] | 5304 | slots (e.g. __str__ affects tp_str as well as tp_repr). The table is |
| 5305 | terminated with an all-zero entry. (This table is further initialized and |
| 5306 | sorted in init_slotdefs() below.) */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5307 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5308 | typedef struct wrapperbase slotdef; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5309 | |
| 5310 | #undef TPSLOT |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5311 | #undef FLSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5312 | #undef ETSLOT |
| 5313 | #undef SQSLOT |
| 5314 | #undef MPSLOT |
| 5315 | #undef NBSLOT |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5316 | #undef UNSLOT |
| 5317 | #undef IBSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5318 | #undef BINSLOT |
| 5319 | #undef RBINSLOT |
| 5320 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5321 | #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Neal Norwitz | d47714a | 2002-08-13 19:01:38 +0000 | [diff] [blame] | 5322 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5323 | PyDoc_STR(DOC)} |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5324 | #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ |
| 5325 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
Neal Norwitz | d47714a | 2002-08-13 19:01:38 +0000 | [diff] [blame] | 5326 | PyDoc_STR(DOC), FLAGS} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5327 | #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5328 | {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
Neal Norwitz | d47714a | 2002-08-13 19:01:38 +0000 | [diff] [blame] | 5329 | PyDoc_STR(DOC)} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5330 | #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5331 | ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) |
| 5332 | #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5333 | ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) |
| 5334 | #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5335 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) |
| 5336 | #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5337 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5338 | "x." NAME "() <==> " DOC) |
| 5339 | #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5340 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5341 | "x." NAME "(y) <==> x" DOC "y") |
| 5342 | #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
| 5343 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5344 | "x." NAME "(y) <==> x" DOC "y") |
| 5345 | #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
| 5346 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5347 | "x." NAME "(y) <==> y" DOC "x") |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5348 | #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
| 5349 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5350 | "x." NAME "(y) <==> " DOC) |
| 5351 | #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
| 5352 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5353 | "x." NAME "(y) <==> " DOC) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5354 | |
| 5355 | static slotdef slotdefs[] = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5356 | SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5357 | "x.__len__() <==> len(x)"), |
Armin Rigo | fd163f9 | 2005-12-29 15:59:19 +0000 | [diff] [blame] | 5358 | /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL. |
| 5359 | The logic in abstract.c always falls back to nb_add/nb_multiply in |
| 5360 | this case. Defining both the nb_* and the sq_* slots to call the |
| 5361 | user-defined methods has unexpected side-effects, as shown by |
| 5362 | test_descr.notimplemented() */ |
| 5363 | SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5364 | "x.__add__(y) <==> x+y"), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5365 | SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5366 | "x.__mul__(n) <==> x*n"), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5367 | SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5368 | "x.__rmul__(n) <==> n*x"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5369 | SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, |
| 5370 | "x.__getitem__(y) <==> x[y]"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5371 | SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, |
Brett Cannon | be67d87 | 2003-05-20 02:40:12 +0000 | [diff] [blame] | 5372 | "x.__setitem__(i, y) <==> x[i]=y"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5373 | SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, |
Brett Cannon | be67d87 | 2003-05-20 02:40:12 +0000 | [diff] [blame] | 5374 | "x.__delitem__(y) <==> del x[y]"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5375 | SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, |
| 5376 | "x.__contains__(y) <==> y in x"), |
Armin Rigo | fd163f9 | 2005-12-29 15:59:19 +0000 | [diff] [blame] | 5377 | SQSLOT("__iadd__", sq_inplace_concat, NULL, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5378 | wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"), |
Armin Rigo | fd163f9 | 2005-12-29 15:59:19 +0000 | [diff] [blame] | 5379 | SQSLOT("__imul__", sq_inplace_repeat, NULL, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5380 | wrap_indexargfunc, "x.__imul__(y) <==> x*=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5381 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5382 | MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5383 | "x.__len__() <==> len(x)"), |
Guido van Rossum | fd38f8e | 2001-10-09 20:17:57 +0000 | [diff] [blame] | 5384 | MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5385 | wrap_binaryfunc, |
| 5386 | "x.__getitem__(y) <==> x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5387 | MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5388 | wrap_objobjargproc, |
| 5389 | "x.__setitem__(i, y) <==> x[i]=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5390 | MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5391 | wrap_delitem, |
| 5392 | "x.__delitem__(y) <==> del x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5393 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5394 | BINSLOT("__add__", nb_add, slot_nb_add, |
| 5395 | "+"), |
| 5396 | RBINSLOT("__radd__", nb_add, slot_nb_add, |
| 5397 | "+"), |
| 5398 | BINSLOT("__sub__", nb_subtract, slot_nb_subtract, |
| 5399 | "-"), |
| 5400 | RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract, |
| 5401 | "-"), |
| 5402 | BINSLOT("__mul__", nb_multiply, slot_nb_multiply, |
| 5403 | "*"), |
| 5404 | RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply, |
| 5405 | "*"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5406 | BINSLOT("__mod__", nb_remainder, slot_nb_remainder, |
| 5407 | "%"), |
| 5408 | RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder, |
| 5409 | "%"), |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5410 | BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5411 | "divmod(x, y)"), |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5412 | RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5413 | "divmod(y, x)"), |
| 5414 | NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, |
| 5415 | "x.__pow__(y[, z]) <==> pow(x, y[, z])"), |
| 5416 | NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, |
| 5417 | "y.__rpow__(x[, z]) <==> pow(x, y[, z])"), |
| 5418 | UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"), |
| 5419 | UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"), |
| 5420 | UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, |
| 5421 | "abs(x)"), |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 5422 | UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5423 | "x != 0"), |
| 5424 | UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"), |
| 5425 | BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5426 | RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5427 | BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5428 | RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5429 | BINSLOT("__and__", nb_and, slot_nb_and, "&"), |
| 5430 | RBINSLOT("__rand__", nb_and, slot_nb_and, "&"), |
| 5431 | BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"), |
| 5432 | RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), |
| 5433 | BINSLOT("__or__", nb_or, slot_nb_or, "|"), |
| 5434 | RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5435 | UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, |
| 5436 | "int(x)"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5437 | UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, |
| 5438 | "float(x)"), |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 5439 | NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc, |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5440 | "x[y:z] <==> x[y.__index__():z.__index__()]"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5441 | IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, |
| 5442 | wrap_binaryfunc, "+"), |
| 5443 | IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, |
| 5444 | wrap_binaryfunc, "-"), |
| 5445 | IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply, |
| 5446 | wrap_binaryfunc, "*"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5447 | IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder, |
| 5448 | wrap_binaryfunc, "%"), |
| 5449 | IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power, |
Guido van Rossum | 6e5680f | 2002-10-15 01:01:53 +0000 | [diff] [blame] | 5450 | wrap_binaryfunc, "**"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5451 | IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift, |
| 5452 | wrap_binaryfunc, "<<"), |
| 5453 | IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift, |
| 5454 | wrap_binaryfunc, ">>"), |
| 5455 | IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and, |
| 5456 | wrap_binaryfunc, "&"), |
| 5457 | IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor, |
| 5458 | wrap_binaryfunc, "^"), |
| 5459 | IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or, |
| 5460 | wrap_binaryfunc, "|"), |
| 5461 | BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5462 | RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5463 | BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5464 | RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5465 | IBSLOT("__ifloordiv__", nb_inplace_floor_divide, |
| 5466 | slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"), |
| 5467 | IBSLOT("__itruediv__", nb_inplace_true_divide, |
| 5468 | slot_nb_inplace_true_divide, wrap_binaryfunc, "/"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5469 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5470 | TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, |
| 5471 | "x.__str__() <==> str(x)"), |
| 5472 | TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, |
| 5473 | "x.__repr__() <==> repr(x)"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5474 | TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, |
| 5475 | "x.__hash__() <==> hash(x)"), |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5476 | FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, |
| 5477 | "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS), |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5478 | TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5479 | wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"), |
| 5480 | TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), |
| 5481 | TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), |
| 5482 | TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), |
| 5483 | TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, |
| 5484 | "x.__setattr__('name', value) <==> x.name = value"), |
| 5485 | TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), |
| 5486 | TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, |
| 5487 | "x.__delattr__('name') <==> del x.name"), |
| 5488 | TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), |
| 5489 | TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, |
| 5490 | "x.__lt__(y) <==> x<y"), |
| 5491 | TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, |
| 5492 | "x.__le__(y) <==> x<=y"), |
| 5493 | TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, |
| 5494 | "x.__eq__(y) <==> x==y"), |
| 5495 | TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, |
| 5496 | "x.__ne__(y) <==> x!=y"), |
| 5497 | TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, |
| 5498 | "x.__gt__(y) <==> x>y"), |
| 5499 | TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, |
| 5500 | "x.__ge__(y) <==> x>=y"), |
| 5501 | TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, |
| 5502 | "x.__iter__() <==> iter(x)"), |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 5503 | TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next, |
| 5504 | "x.__next__() <==> next(x)"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5505 | TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, |
| 5506 | "descr.__get__(obj[, type]) -> value"), |
| 5507 | TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, |
| 5508 | "descr.__set__(obj, value)"), |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 5509 | TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set, |
| 5510 | wrap_descr_delete, "descr.__delete__(obj)"), |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5511 | FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5512 | "x.__init__(...) initializes x; " |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5513 | "see x.__class__.__doc__ for signature", |
| 5514 | PyWrapperFlag_KEYWORDS), |
| 5515 | TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""), |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5516 | TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5517 | {NULL} |
| 5518 | }; |
| 5519 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5520 | /* Given a type pointer and an offset gotten from a slotdef entry, return a |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5521 | 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] | 5522 | the offset to the type pointer, since it takes care to indirect through the |
| 5523 | proper indirection pointer (as_buffer, etc.); it returns NULL if the |
| 5524 | indirection pointer is NULL. */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5525 | static void ** |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5526 | slotptr(PyTypeObject *type, int ioffset) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5527 | { |
| 5528 | char *ptr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5529 | long offset = ioffset; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5530 | |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5531 | /* Note: this depends on the order of the members of PyHeapTypeObject! */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5532 | assert(offset >= 0); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5533 | assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer)); |
| 5534 | if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) { |
| 5535 | ptr = (char *)type->tp_as_sequence; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5536 | offset -= offsetof(PyHeapTypeObject, as_sequence); |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5537 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5538 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) { |
| 5539 | ptr = (char *)type->tp_as_mapping; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5540 | offset -= offsetof(PyHeapTypeObject, as_mapping); |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 5541 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5542 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) { |
| 5543 | ptr = (char *)type->tp_as_number; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5544 | offset -= offsetof(PyHeapTypeObject, as_number); |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5545 | } |
| 5546 | else { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5547 | ptr = (char *)type; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5548 | } |
| 5549 | if (ptr != NULL) |
| 5550 | ptr += offset; |
| 5551 | return (void **)ptr; |
| 5552 | } |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 5553 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5554 | /* Length of array of slotdef pointers used to store slots with the |
| 5555 | same __name__. There should be at most MAX_EQUIV-1 slotdef entries with |
| 5556 | the same __name__, for any __name__. Since that's a static property, it is |
| 5557 | appropriate to declare fixed-size arrays for this. */ |
| 5558 | #define MAX_EQUIV 10 |
| 5559 | |
| 5560 | /* Return a slot pointer for a given name, but ONLY if the attribute has |
| 5561 | exactly one slot function. The name must be an interned string. */ |
| 5562 | static void ** |
| 5563 | resolve_slotdups(PyTypeObject *type, PyObject *name) |
| 5564 | { |
| 5565 | /* XXX Maybe this could be optimized more -- but is it worth it? */ |
| 5566 | |
| 5567 | /* pname and ptrs act as a little cache */ |
| 5568 | static PyObject *pname; |
| 5569 | static slotdef *ptrs[MAX_EQUIV]; |
| 5570 | slotdef *p, **pp; |
| 5571 | void **res, **ptr; |
| 5572 | |
| 5573 | if (pname != name) { |
| 5574 | /* Collect all slotdefs that match name into ptrs. */ |
| 5575 | pname = name; |
| 5576 | pp = ptrs; |
| 5577 | for (p = slotdefs; p->name_strobj; p++) { |
| 5578 | if (p->name_strobj == name) |
| 5579 | *pp++ = p; |
| 5580 | } |
| 5581 | *pp = NULL; |
| 5582 | } |
| 5583 | |
| 5584 | /* Look in all matching slots of the type; if exactly one of these has |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5585 | a filled-in slot, return its value. Otherwise return NULL. */ |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5586 | res = NULL; |
| 5587 | for (pp = ptrs; *pp; pp++) { |
| 5588 | ptr = slotptr(type, (*pp)->offset); |
| 5589 | if (ptr == NULL || *ptr == NULL) |
| 5590 | continue; |
| 5591 | if (res != NULL) |
| 5592 | return NULL; |
| 5593 | res = ptr; |
| 5594 | } |
| 5595 | return res; |
| 5596 | } |
| 5597 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5598 | /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5599 | does some incredibly complex thinking and then sticks something into the |
| 5600 | slot. (It sees if the adjacent slotdefs for the same slot have conflicting |
| 5601 | interests, and then stores a generic wrapper or a specific function into |
| 5602 | the slot.) Return a pointer to the next slotdef with a different offset, |
| 5603 | because that's convenient for fixup_slot_dispatchers(). */ |
| 5604 | static slotdef * |
| 5605 | update_one_slot(PyTypeObject *type, slotdef *p) |
| 5606 | { |
| 5607 | PyObject *descr; |
| 5608 | PyWrapperDescrObject *d; |
| 5609 | void *generic = NULL, *specific = NULL; |
| 5610 | int use_generic = 0; |
| 5611 | int offset = p->offset; |
| 5612 | void **ptr = slotptr(type, offset); |
| 5613 | |
| 5614 | if (ptr == NULL) { |
| 5615 | do { |
| 5616 | ++p; |
| 5617 | } while (p->offset == offset); |
| 5618 | return p; |
| 5619 | } |
| 5620 | do { |
| 5621 | descr = _PyType_Lookup(type, p->name_strobj); |
Amaury Forgeot d'Arc | f343e01 | 2009-01-12 23:58:21 +0000 | [diff] [blame] | 5622 | if (descr == NULL) { |
| 5623 | if (ptr == (void**)&type->tp_iternext) { |
| 5624 | specific = _PyObject_NextNotImplemented; |
| 5625 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5626 | continue; |
Amaury Forgeot d'Arc | f343e01 | 2009-01-12 23:58:21 +0000 | [diff] [blame] | 5627 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5628 | if (Py_TYPE(descr) == &PyWrapperDescr_Type) { |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5629 | void **tptr = resolve_slotdups(type, p->name_strobj); |
| 5630 | if (tptr == NULL || tptr == ptr) |
| 5631 | generic = p->function; |
| 5632 | d = (PyWrapperDescrObject *)descr; |
| 5633 | if (d->d_base->wrapper == p->wrapper && |
Alexandre Vassalotti | 2db046d | 2009-07-22 03:56:36 +0000 | [diff] [blame] | 5634 | PyType_IsSubtype(type, PyDescr_TYPE(d))) |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5635 | { |
| 5636 | if (specific == NULL || |
| 5637 | specific == d->d_wrapped) |
| 5638 | specific = d->d_wrapped; |
| 5639 | else |
| 5640 | use_generic = 1; |
| 5641 | } |
| 5642 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5643 | else if (Py_TYPE(descr) == &PyCFunction_Type && |
Guido van Rossum | 721f62e | 2002-08-09 02:14:34 +0000 | [diff] [blame] | 5644 | PyCFunction_GET_FUNCTION(descr) == |
| 5645 | (PyCFunction)tp_new_wrapper && |
Benjamin Peterson | b547979 | 2009-01-18 22:10:38 +0000 | [diff] [blame] | 5646 | ptr == (void**)&type->tp_new) |
Guido van Rossum | 721f62e | 2002-08-09 02:14:34 +0000 | [diff] [blame] | 5647 | { |
| 5648 | /* The __new__ wrapper is not a wrapper descriptor, |
| 5649 | so must be special-cased differently. |
| 5650 | If we don't do this, creating an instance will |
| 5651 | always use slot_tp_new which will look up |
| 5652 | __new__ in the MRO which will call tp_new_wrapper |
| 5653 | which will look through the base classes looking |
| 5654 | for a static base and call its tp_new (usually |
| 5655 | PyType_GenericNew), after performing various |
| 5656 | sanity checks and constructing a new argument |
| 5657 | list. Cut all that nonsense short -- this speeds |
| 5658 | up instance creation tremendously. */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5659 | specific = (void *)type->tp_new; |
Guido van Rossum | 721f62e | 2002-08-09 02:14:34 +0000 | [diff] [blame] | 5660 | /* XXX I'm not 100% sure that there isn't a hole |
| 5661 | in this reasoning that requires additional |
| 5662 | sanity checks. I'll buy the first person to |
| 5663 | point out a bug in this reasoning a beer. */ |
| 5664 | } |
Nick Coghlan | d1abd25 | 2008-07-15 15:46:38 +0000 | [diff] [blame] | 5665 | else if (descr == Py_None && |
Benjamin Peterson | b547979 | 2009-01-18 22:10:38 +0000 | [diff] [blame] | 5666 | ptr == (void**)&type->tp_hash) { |
Nick Coghlan | d1abd25 | 2008-07-15 15:46:38 +0000 | [diff] [blame] | 5667 | /* We specifically allow __hash__ to be set to None |
| 5668 | to prevent inheritance of the default |
| 5669 | implementation from object.__hash__ */ |
| 5670 | specific = PyObject_HashNotImplemented; |
| 5671 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5672 | else { |
| 5673 | use_generic = 1; |
| 5674 | generic = p->function; |
| 5675 | } |
| 5676 | } while ((++p)->offset == offset); |
| 5677 | if (specific && !use_generic) |
| 5678 | *ptr = specific; |
| 5679 | else |
| 5680 | *ptr = generic; |
| 5681 | return p; |
| 5682 | } |
| 5683 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5684 | /* In the type, update the slots whose slotdefs are gathered in the pp array. |
| 5685 | This is a callback for update_subclasses(). */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5686 | static int |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5687 | update_slots_callback(PyTypeObject *type, void *data) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5688 | { |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5689 | slotdef **pp = (slotdef **)data; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5690 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5691 | for (; *pp; pp++) |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5692 | update_one_slot(type, *pp); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5693 | return 0; |
| 5694 | } |
| 5695 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5696 | /* Comparison function for qsort() to compare slotdefs by their offset, and |
| 5697 | for equal offset by their address (to force a stable sort). */ |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5698 | static int |
| 5699 | slotdef_cmp(const void *aa, const void *bb) |
| 5700 | { |
| 5701 | const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb; |
| 5702 | int c = a->offset - b->offset; |
| 5703 | if (c != 0) |
| 5704 | return c; |
| 5705 | else |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5706 | /* Cannot use a-b, as this gives off_t, |
| 5707 | which may lose precision when converted to int. */ |
| 5708 | return (a > b) ? 1 : (a < b) ? -1 : 0; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5709 | } |
| 5710 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5711 | /* Initialize the slotdefs table by adding interned string objects for the |
| 5712 | names and sorting the entries. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5713 | static void |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5714 | init_slotdefs(void) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5715 | { |
| 5716 | slotdef *p; |
| 5717 | static int initialized = 0; |
| 5718 | |
| 5719 | if (initialized) |
| 5720 | return; |
| 5721 | for (p = slotdefs; p->name; p++) { |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 5722 | p->name_strobj = PyUnicode_InternFromString(p->name); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5723 | if (!p->name_strobj) |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5724 | Py_FatalError("Out of memory interning slotdef names"); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5725 | } |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 5726 | qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef), |
| 5727 | slotdef_cmp); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5728 | initialized = 1; |
| 5729 | } |
| 5730 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5731 | /* Update the slots after assignment to a class (type) attribute. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5732 | static int |
| 5733 | update_slot(PyTypeObject *type, PyObject *name) |
| 5734 | { |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5735 | slotdef *ptrs[MAX_EQUIV]; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5736 | slotdef *p; |
| 5737 | slotdef **pp; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 5738 | int offset; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5739 | |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 5740 | /* Clear the VALID_VERSION flag of 'type' and all its |
| 5741 | subclasses. This could possibly be unified with the |
| 5742 | update_subclasses() recursion below, but carefully: |
| 5743 | they each have their own conditions on which to stop |
| 5744 | recursing into subclasses. */ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 5745 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 5746 | |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 5747 | init_slotdefs(); |
| 5748 | pp = ptrs; |
| 5749 | for (p = slotdefs; p->name; p++) { |
| 5750 | /* XXX assume name is interned! */ |
| 5751 | if (p->name_strobj == name) |
| 5752 | *pp++ = p; |
| 5753 | } |
| 5754 | *pp = NULL; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5755 | for (pp = ptrs; *pp; pp++) { |
| 5756 | p = *pp; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 5757 | offset = p->offset; |
| 5758 | while (p > slotdefs && (p-1)->offset == offset) |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5759 | --p; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 5760 | *pp = p; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5761 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5762 | if (ptrs[0] == NULL) |
| 5763 | return 0; /* Not an attribute that affects any slots */ |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5764 | return update_subclasses(type, name, |
| 5765 | update_slots_callback, (void *)ptrs); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5766 | } |
| 5767 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5768 | /* Store the proper functions in the slot dispatches at class (type) |
| 5769 | definition time, based upon which operations the class overrides in its |
| 5770 | dict. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5771 | static void |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5772 | fixup_slot_dispatchers(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5773 | { |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5774 | slotdef *p; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5775 | |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5776 | init_slotdefs(); |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5777 | for (p = slotdefs; p->name; ) |
| 5778 | p = update_one_slot(type, p); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5779 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5780 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 5781 | static void |
| 5782 | update_all_slots(PyTypeObject* type) |
| 5783 | { |
| 5784 | slotdef *p; |
| 5785 | |
| 5786 | init_slotdefs(); |
| 5787 | for (p = slotdefs; p->name; p++) { |
| 5788 | /* update_slot returns int but can't actually fail */ |
| 5789 | update_slot(type, p->name_strobj); |
| 5790 | } |
| 5791 | } |
| 5792 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5793 | /* recurse_down_subclasses() and update_subclasses() are mutually |
| 5794 | recursive functions to call a callback for all subclasses, |
| 5795 | but refraining from recursing into subclasses that define 'name'. */ |
| 5796 | |
| 5797 | static int |
| 5798 | update_subclasses(PyTypeObject *type, PyObject *name, |
| 5799 | update_callback callback, void *data) |
| 5800 | { |
| 5801 | if (callback(type, data) < 0) |
| 5802 | return -1; |
| 5803 | return recurse_down_subclasses(type, name, callback, data); |
| 5804 | } |
| 5805 | |
| 5806 | static int |
| 5807 | recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
| 5808 | update_callback callback, void *data) |
| 5809 | { |
| 5810 | PyTypeObject *subclass; |
| 5811 | PyObject *ref, *subclasses, *dict; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5812 | Py_ssize_t i, n; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5813 | |
| 5814 | subclasses = type->tp_subclasses; |
| 5815 | if (subclasses == NULL) |
| 5816 | return 0; |
| 5817 | assert(PyList_Check(subclasses)); |
| 5818 | n = PyList_GET_SIZE(subclasses); |
| 5819 | for (i = 0; i < n; i++) { |
| 5820 | ref = PyList_GET_ITEM(subclasses, i); |
| 5821 | assert(PyWeakref_CheckRef(ref)); |
| 5822 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 5823 | assert(subclass != NULL); |
| 5824 | if ((PyObject *)subclass == Py_None) |
| 5825 | continue; |
| 5826 | assert(PyType_Check(subclass)); |
| 5827 | /* Avoid recursing down into unaffected classes */ |
| 5828 | dict = subclass->tp_dict; |
| 5829 | if (dict != NULL && PyDict_Check(dict) && |
| 5830 | PyDict_GetItem(dict, name) != NULL) |
| 5831 | continue; |
| 5832 | if (update_subclasses(subclass, name, callback, data) < 0) |
| 5833 | return -1; |
| 5834 | } |
| 5835 | return 0; |
| 5836 | } |
| 5837 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5838 | /* This function is called by PyType_Ready() to populate the type's |
| 5839 | dictionary with method descriptors for function slots. For each |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 5840 | function slot (like tp_repr) that's defined in the type, one or more |
| 5841 | corresponding descriptors are added in the type's tp_dict dictionary |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5842 | under the appropriate name (like __repr__). Some function slots |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 5843 | cause more than one descriptor to be added (for example, the nb_add |
| 5844 | slot adds both __add__ and __radd__ descriptors) and some function |
| 5845 | slots compete for the same descriptor (for example both sq_item and |
| 5846 | mp_subscript generate a __getitem__ descriptor). |
| 5847 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5848 | In the latter case, the first slotdef entry encoutered wins. Since |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5849 | 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] | 5850 | PyHeapTypeObject, this gives us some control over disambiguating |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5851 | between competing slots: the members of PyHeapTypeObject are listed |
| 5852 | from most general to least general, so the most general slot is |
| 5853 | preferred. In particular, because as_mapping comes before as_sequence, |
| 5854 | for a type that defines both mp_subscript and sq_item, mp_subscript |
| 5855 | wins. |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 5856 | |
| 5857 | This only adds new descriptors and doesn't overwrite entries in |
| 5858 | tp_dict that were previously defined. The descriptors contain a |
| 5859 | reference to the C function they must call, so that it's safe if they |
| 5860 | are copied into a subtype's __dict__ and the subtype has a different |
| 5861 | C function in its slot -- calling the method defined by the |
| 5862 | descriptor will call the C function that was used to create it, |
| 5863 | rather than the C function present in the slot when it is called. |
| 5864 | (This is important because a subtype may have a C function in the |
| 5865 | slot that calls the method from the dictionary, and we want to avoid |
| 5866 | infinite recursion here.) */ |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5867 | |
| 5868 | static int |
| 5869 | add_operators(PyTypeObject *type) |
| 5870 | { |
| 5871 | PyObject *dict = type->tp_dict; |
| 5872 | slotdef *p; |
| 5873 | PyObject *descr; |
| 5874 | void **ptr; |
| 5875 | |
| 5876 | init_slotdefs(); |
| 5877 | for (p = slotdefs; p->name; p++) { |
| 5878 | if (p->wrapper == NULL) |
| 5879 | continue; |
| 5880 | ptr = slotptr(type, p->offset); |
| 5881 | if (!ptr || !*ptr) |
| 5882 | continue; |
| 5883 | if (PyDict_GetItem(dict, p->name_strobj)) |
| 5884 | continue; |
Nick Coghlan | d1abd25 | 2008-07-15 15:46:38 +0000 | [diff] [blame] | 5885 | if (*ptr == PyObject_HashNotImplemented) { |
| 5886 | /* Classes may prevent the inheritance of the tp_hash |
| 5887 | slot by storing PyObject_HashNotImplemented in it. Make it |
| 5888 | visible as a None value for the __hash__ attribute. */ |
| 5889 | if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0) |
| 5890 | return -1; |
| 5891 | } |
| 5892 | else { |
| 5893 | descr = PyDescr_NewWrapper(type, p, *ptr); |
| 5894 | if (descr == NULL) |
| 5895 | return -1; |
| 5896 | if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) |
| 5897 | return -1; |
| 5898 | Py_DECREF(descr); |
| 5899 | } |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5900 | } |
| 5901 | if (type->tp_new != NULL) { |
| 5902 | if (add_tp_new_wrapper(type) < 0) |
| 5903 | return -1; |
| 5904 | } |
| 5905 | return 0; |
| 5906 | } |
| 5907 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5908 | |
| 5909 | /* Cooperative 'super' */ |
| 5910 | |
| 5911 | typedef struct { |
| 5912 | PyObject_HEAD |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 5913 | PyTypeObject *type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5914 | PyObject *obj; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 5915 | PyTypeObject *obj_type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5916 | } superobject; |
| 5917 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 5918 | static PyMemberDef super_members[] = { |
| 5919 | {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY, |
| 5920 | "the class invoking super()"}, |
| 5921 | {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY, |
| 5922 | "the instance invoking super(); may be None"}, |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 5923 | {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY, |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 5924 | "the type of the instance invoking super(); may be None"}, |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 5925 | {0} |
| 5926 | }; |
| 5927 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5928 | static void |
| 5929 | super_dealloc(PyObject *self) |
| 5930 | { |
| 5931 | superobject *su = (superobject *)self; |
| 5932 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 5933 | _PyObject_GC_UNTRACK(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5934 | Py_XDECREF(su->obj); |
| 5935 | Py_XDECREF(su->type); |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 5936 | Py_XDECREF(su->obj_type); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5937 | Py_TYPE(self)->tp_free(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5938 | } |
| 5939 | |
| 5940 | static PyObject * |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 5941 | super_repr(PyObject *self) |
| 5942 | { |
| 5943 | superobject *su = (superobject *)self; |
| 5944 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 5945 | if (su->obj_type) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 5946 | return PyUnicode_FromFormat( |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 5947 | "<super: <class '%s'>, <%s object>>", |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 5948 | su->type ? su->type->tp_name : "NULL", |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 5949 | su->obj_type->tp_name); |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 5950 | else |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 5951 | return PyUnicode_FromFormat( |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 5952 | "<super: <class '%s'>, NULL>", |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 5953 | su->type ? su->type->tp_name : "NULL"); |
| 5954 | } |
| 5955 | |
| 5956 | static PyObject * |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5957 | super_getattro(PyObject *self, PyObject *name) |
| 5958 | { |
| 5959 | superobject *su = (superobject *)self; |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 5960 | int skip = su->obj_type == NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5961 | |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 5962 | if (!skip) { |
| 5963 | /* We want __class__ to return the class of the super object |
| 5964 | (i.e. super, or a subclass), not the class of su->obj. */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 5965 | skip = (PyUnicode_Check(name) && |
| 5966 | PyUnicode_GET_SIZE(name) == 9 && |
| 5967 | PyUnicode_CompareWithASCIIString(name, "__class__") == 0); |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 5968 | } |
| 5969 | |
| 5970 | if (!skip) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 5971 | PyObject *mro, *res, *tmp, *dict; |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 5972 | PyTypeObject *starttype; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5973 | descrgetfunc f; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5974 | Py_ssize_t i, n; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5975 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 5976 | starttype = su->obj_type; |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 5977 | mro = starttype->tp_mro; |
| 5978 | |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 5979 | if (mro == NULL) |
| 5980 | n = 0; |
| 5981 | else { |
| 5982 | assert(PyTuple_Check(mro)); |
| 5983 | n = PyTuple_GET_SIZE(mro); |
| 5984 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5985 | for (i = 0; i < n; i++) { |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 5986 | if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5987 | break; |
| 5988 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5989 | i++; |
| 5990 | res = NULL; |
| 5991 | for (; i < n; i++) { |
| 5992 | tmp = PyTuple_GET_ITEM(mro, i); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 5993 | if (PyType_Check(tmp)) |
| 5994 | dict = ((PyTypeObject *)tmp)->tp_dict; |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 5995 | else |
| 5996 | continue; |
| 5997 | res = PyDict_GetItem(dict, name); |
Guido van Rossum | 6cc5bb6 | 2003-04-16 20:01:36 +0000 | [diff] [blame] | 5998 | if (res != NULL) { |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5999 | Py_INCREF(res); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6000 | f = Py_TYPE(res)->tp_descr_get; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6001 | if (f != NULL) { |
Phillip J. Eby | 91a968a | 2004-03-25 02:19:34 +0000 | [diff] [blame] | 6002 | tmp = f(res, |
| 6003 | /* Only pass 'obj' param if |
| 6004 | this is instance-mode super |
| 6005 | (See SF ID #743627) |
| 6006 | */ |
Hye-Shik Chang | ff365c9 | 2004-03-25 16:37:03 +0000 | [diff] [blame] | 6007 | (su->obj == (PyObject *) |
| 6008 | su->obj_type |
Phillip J. Eby | 91a968a | 2004-03-25 02:19:34 +0000 | [diff] [blame] | 6009 | ? (PyObject *)NULL |
| 6010 | : su->obj), |
Guido van Rossum | d464107 | 2002-04-03 02:13:37 +0000 | [diff] [blame] | 6011 | (PyObject *)starttype); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6012 | Py_DECREF(res); |
| 6013 | res = tmp; |
| 6014 | } |
| 6015 | return res; |
| 6016 | } |
| 6017 | } |
| 6018 | } |
| 6019 | return PyObject_GenericGetAttr(self, name); |
| 6020 | } |
| 6021 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6022 | static PyTypeObject * |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6023 | supercheck(PyTypeObject *type, PyObject *obj) |
| 6024 | { |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6025 | /* Check that a super() call makes sense. Return a type object. |
| 6026 | |
| 6027 | obj can be a new-style class, or an instance of one: |
| 6028 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 6029 | - 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] | 6030 | used for class methods; the return value is obj. |
| 6031 | |
| 6032 | - If it is an instance, it must be an instance of 'type'. This is |
| 6033 | the normal case; the return value is obj.__class__. |
| 6034 | |
| 6035 | But... when obj is an instance, we want to allow for the case where |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6036 | 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] | 6037 | This will allow using super() with a proxy for obj. |
| 6038 | */ |
| 6039 | |
Guido van Rossum | 8e80a72 | 2003-02-18 19:22:22 +0000 | [diff] [blame] | 6040 | /* Check for first bullet above (special case) */ |
| 6041 | if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) { |
| 6042 | Py_INCREF(obj); |
| 6043 | return (PyTypeObject *)obj; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6044 | } |
Guido van Rossum | 8e80a72 | 2003-02-18 19:22:22 +0000 | [diff] [blame] | 6045 | |
| 6046 | /* Normal case */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6047 | if (PyType_IsSubtype(Py_TYPE(obj), type)) { |
| 6048 | Py_INCREF(Py_TYPE(obj)); |
| 6049 | return Py_TYPE(obj); |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6050 | } |
| 6051 | else { |
| 6052 | /* Try the slow way */ |
| 6053 | static PyObject *class_str = NULL; |
| 6054 | PyObject *class_attr; |
| 6055 | |
| 6056 | if (class_str == NULL) { |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6057 | class_str = PyUnicode_FromString("__class__"); |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6058 | if (class_str == NULL) |
| 6059 | return NULL; |
| 6060 | } |
| 6061 | |
| 6062 | class_attr = PyObject_GetAttr(obj, class_str); |
| 6063 | |
| 6064 | if (class_attr != NULL && |
| 6065 | PyType_Check(class_attr) && |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6066 | (PyTypeObject *)class_attr != Py_TYPE(obj)) |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6067 | { |
| 6068 | int ok = PyType_IsSubtype( |
| 6069 | (PyTypeObject *)class_attr, type); |
| 6070 | if (ok) |
| 6071 | return (PyTypeObject *)class_attr; |
| 6072 | } |
| 6073 | |
| 6074 | if (class_attr == NULL) |
| 6075 | PyErr_Clear(); |
| 6076 | else |
| 6077 | Py_DECREF(class_attr); |
| 6078 | } |
| 6079 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 6080 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6081 | "super(type, obj): " |
| 6082 | "obj must be an instance or subtype of type"); |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6083 | return NULL; |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6084 | } |
| 6085 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6086 | static PyObject * |
| 6087 | super_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 6088 | { |
| 6089 | superobject *su = (superobject *)self; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6090 | superobject *newobj; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6091 | |
| 6092 | if (obj == NULL || obj == Py_None || su->obj != NULL) { |
| 6093 | /* Not binding to an object, or already bound */ |
| 6094 | Py_INCREF(self); |
| 6095 | return self; |
| 6096 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6097 | if (Py_TYPE(su) != &PySuper_Type) |
Armin Rigo | 7726dc0 | 2005-05-15 15:32:08 +0000 | [diff] [blame] | 6098 | /* If su is an instance of a (strict) subclass of super, |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6099 | call its type */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6100 | return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su), |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 6101 | su->type, obj, NULL); |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6102 | else { |
| 6103 | /* Inline the common case */ |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6104 | PyTypeObject *obj_type = supercheck(su->type, obj); |
| 6105 | if (obj_type == NULL) |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6106 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6107 | newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type, |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6108 | NULL, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6109 | if (newobj == NULL) |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6110 | return NULL; |
| 6111 | Py_INCREF(su->type); |
| 6112 | Py_INCREF(obj); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6113 | newobj->type = su->type; |
| 6114 | newobj->obj = obj; |
| 6115 | newobj->obj_type = obj_type; |
| 6116 | return (PyObject *)newobj; |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6117 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6118 | } |
| 6119 | |
| 6120 | static int |
| 6121 | super_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 6122 | { |
| 6123 | superobject *su = (superobject *)self; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6124 | PyTypeObject *type = NULL; |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6125 | PyObject *obj = NULL; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6126 | PyTypeObject *obj_type = NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6127 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6128 | if (!_PyArg_NoKeywords("super", kwds)) |
| 6129 | return -1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6130 | if (!PyArg_ParseTuple(args, "|O!O:super", &PyType_Type, &type, &obj)) |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6131 | return -1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6132 | |
| 6133 | if (type == NULL) { |
| 6134 | /* Call super(), without args -- fill in from __class__ |
| 6135 | and first local variable on the stack. */ |
| 6136 | PyFrameObject *f = PyThreadState_GET()->frame; |
| 6137 | PyCodeObject *co = f->f_code; |
| 6138 | int i, n; |
| 6139 | if (co == NULL) { |
| 6140 | PyErr_SetString(PyExc_SystemError, |
| 6141 | "super(): no code object"); |
| 6142 | return -1; |
| 6143 | } |
| 6144 | if (co->co_argcount == 0) { |
| 6145 | PyErr_SetString(PyExc_SystemError, |
| 6146 | "super(): no arguments"); |
| 6147 | return -1; |
| 6148 | } |
| 6149 | obj = f->f_localsplus[0]; |
| 6150 | if (obj == NULL) { |
| 6151 | PyErr_SetString(PyExc_SystemError, |
| 6152 | "super(): arg[0] deleted"); |
| 6153 | return -1; |
| 6154 | } |
| 6155 | if (co->co_freevars == NULL) |
| 6156 | n = 0; |
| 6157 | else { |
| 6158 | assert(PyTuple_Check(co->co_freevars)); |
| 6159 | n = PyTuple_GET_SIZE(co->co_freevars); |
| 6160 | } |
| 6161 | for (i = 0; i < n; i++) { |
| 6162 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 6163 | assert(PyUnicode_Check(name)); |
| 6164 | if (!PyUnicode_CompareWithASCIIString(name, |
| 6165 | "__class__")) { |
Barry Warsaw | 91cc8fb | 2008-11-20 20:01:57 +0000 | [diff] [blame] | 6166 | Py_ssize_t index = co->co_nlocals + |
| 6167 | PyTuple_GET_SIZE(co->co_cellvars) + i; |
| 6168 | PyObject *cell = f->f_localsplus[index]; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6169 | if (cell == NULL || !PyCell_Check(cell)) { |
| 6170 | PyErr_SetString(PyExc_SystemError, |
| 6171 | "super(): bad __class__ cell"); |
| 6172 | return -1; |
| 6173 | } |
| 6174 | type = (PyTypeObject *) PyCell_GET(cell); |
| 6175 | if (type == NULL) { |
| 6176 | PyErr_SetString(PyExc_SystemError, |
| 6177 | "super(): empty __class__ cell"); |
| 6178 | return -1; |
| 6179 | } |
| 6180 | if (!PyType_Check(type)) { |
| 6181 | PyErr_Format(PyExc_SystemError, |
| 6182 | "super(): __class__ is not a type (%s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6183 | Py_TYPE(type)->tp_name); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6184 | return -1; |
| 6185 | } |
| 6186 | break; |
| 6187 | } |
| 6188 | } |
| 6189 | if (type == NULL) { |
| 6190 | PyErr_SetString(PyExc_SystemError, |
| 6191 | "super(): __class__ cell not found"); |
| 6192 | return -1; |
| 6193 | } |
| 6194 | } |
| 6195 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6196 | if (obj == Py_None) |
| 6197 | obj = NULL; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6198 | if (obj != NULL) { |
| 6199 | obj_type = supercheck(type, obj); |
| 6200 | if (obj_type == NULL) |
| 6201 | return -1; |
| 6202 | Py_INCREF(obj); |
| 6203 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6204 | Py_INCREF(type); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6205 | su->type = type; |
| 6206 | su->obj = obj; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6207 | su->obj_type = obj_type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6208 | return 0; |
| 6209 | } |
| 6210 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6211 | PyDoc_STRVAR(super_doc, |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6212 | "super() -> same as super(__class__, <first argument>)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6213 | "super(type) -> unbound super object\n" |
| 6214 | "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6215 | "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6216 | "Typical use to call a cooperative superclass method:\n" |
| 6217 | "class C(B):\n" |
| 6218 | " def meth(self, arg):\n" |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6219 | " super().meth(arg)\n" |
| 6220 | "This works for class methods too:\n" |
| 6221 | "class C(B):\n" |
| 6222 | " @classmethod\n" |
| 6223 | " def cmeth(cls, arg):\n" |
| 6224 | " super().cmeth(arg)\n"); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6225 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6226 | static int |
| 6227 | super_traverse(PyObject *self, visitproc visit, void *arg) |
| 6228 | { |
| 6229 | superobject *su = (superobject *)self; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6230 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6231 | Py_VISIT(su->obj); |
| 6232 | Py_VISIT(su->type); |
| 6233 | Py_VISIT(su->obj_type); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6234 | |
| 6235 | return 0; |
| 6236 | } |
| 6237 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6238 | PyTypeObject PySuper_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 6239 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6240 | "super", /* tp_name */ |
| 6241 | sizeof(superobject), /* tp_basicsize */ |
| 6242 | 0, /* tp_itemsize */ |
| 6243 | /* methods */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 6244 | super_dealloc, /* tp_dealloc */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6245 | 0, /* tp_print */ |
| 6246 | 0, /* tp_getattr */ |
| 6247 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 6248 | 0, /* tp_reserved */ |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6249 | super_repr, /* tp_repr */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6250 | 0, /* tp_as_number */ |
| 6251 | 0, /* tp_as_sequence */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 6252 | 0, /* tp_as_mapping */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6253 | 0, /* tp_hash */ |
| 6254 | 0, /* tp_call */ |
| 6255 | 0, /* tp_str */ |
| 6256 | super_getattro, /* tp_getattro */ |
| 6257 | 0, /* tp_setattro */ |
| 6258 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6259 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 6260 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 6261 | super_doc, /* tp_doc */ |
| 6262 | super_traverse, /* tp_traverse */ |
| 6263 | 0, /* tp_clear */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6264 | 0, /* tp_richcompare */ |
| 6265 | 0, /* tp_weaklistoffset */ |
| 6266 | 0, /* tp_iter */ |
| 6267 | 0, /* tp_iternext */ |
| 6268 | 0, /* tp_methods */ |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6269 | super_members, /* tp_members */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6270 | 0, /* tp_getset */ |
| 6271 | 0, /* tp_base */ |
| 6272 | 0, /* tp_dict */ |
| 6273 | super_descr_get, /* tp_descr_get */ |
| 6274 | 0, /* tp_descr_set */ |
| 6275 | 0, /* tp_dictoffset */ |
| 6276 | super_init, /* tp_init */ |
| 6277 | PyType_GenericAlloc, /* tp_alloc */ |
| 6278 | PyType_GenericNew, /* tp_new */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 6279 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6280 | }; |