Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* Type object implementation */ |
| 2 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3 | #include "Python.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 6 | #include <ctype.h> |
| 7 | |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 8 | |
| 9 | /* Support type attribute cache */ |
| 10 | |
| 11 | /* The cache can keep references to the names alive for longer than |
| 12 | they normally would. This is why the maximum size is limited to |
| 13 | MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large |
| 14 | strings are used as attribute names. */ |
| 15 | #define MCACHE_MAX_ATTR_SIZE 100 |
| 16 | #define MCACHE_SIZE_EXP 10 |
| 17 | #define MCACHE_HASH(version, name_hash) \ |
| 18 | (((unsigned int)(version) * (unsigned int)(name_hash)) \ |
| 19 | >> (8*sizeof(unsigned int) - MCACHE_SIZE_EXP)) |
| 20 | #define MCACHE_HASH_METHOD(type, name) \ |
| 21 | MCACHE_HASH((type)->tp_version_tag, \ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 22 | ((PyStringObject *)(name))->ob_shash) |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 23 | #define MCACHE_CACHEABLE_NAME(name) \ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 24 | PyString_CheckExact(name) && \ |
| 25 | PyString_GET_SIZE(name) <= MCACHE_MAX_ATTR_SIZE |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 26 | |
| 27 | struct method_cache_entry { |
| 28 | unsigned int version; |
| 29 | PyObject *name; /* reference to exactly a str or None */ |
| 30 | PyObject *value; /* borrowed */ |
| 31 | }; |
| 32 | |
| 33 | static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP]; |
| 34 | static unsigned int next_version_tag = 0; |
Christian Heimes | 908caac | 2008-01-27 23:34:59 +0000 | [diff] [blame] | 35 | |
| 36 | unsigned int |
| 37 | PyType_ClearCache(void) |
| 38 | { |
| 39 | Py_ssize_t i; |
| 40 | unsigned int cur_version_tag = next_version_tag - 1; |
| 41 | |
| 42 | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { |
| 43 | method_cache[i].version = 0; |
| 44 | Py_CLEAR(method_cache[i].name); |
| 45 | method_cache[i].value = NULL; |
| 46 | } |
| 47 | next_version_tag = 0; |
| 48 | /* mark all version tags as invalid */ |
Georg Brandl | 74a1dea | 2008-05-28 11:21:39 +0000 | [diff] [blame] | 49 | PyType_Modified(&PyBaseObject_Type); |
Christian Heimes | 908caac | 2008-01-27 23:34:59 +0000 | [diff] [blame] | 50 | return cur_version_tag; |
| 51 | } |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 52 | |
Georg Brandl | 74a1dea | 2008-05-28 11:21:39 +0000 | [diff] [blame] | 53 | void |
| 54 | PyType_Modified(PyTypeObject *type) |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 55 | { |
| 56 | /* Invalidate any cached data for the specified type and all |
| 57 | subclasses. This function is called after the base |
| 58 | classes, mro, or attributes of the type are altered. |
| 59 | |
| 60 | Invariants: |
| 61 | |
| 62 | - Py_TPFLAGS_VALID_VERSION_TAG is never set if |
| 63 | Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type |
| 64 | objects coming from non-recompiled extension modules) |
| 65 | |
| 66 | - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type, |
| 67 | it must first be set on all super types. |
| 68 | |
| 69 | This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a |
| 70 | type (so it must first clear it on all subclasses). The |
| 71 | tp_version_tag value is meaningless unless this flag is set. |
| 72 | We don't assign new version tags eagerly, but only as |
| 73 | needed. |
| 74 | */ |
| 75 | PyObject *raw, *ref; |
| 76 | Py_ssize_t i, n; |
| 77 | |
Neal Norwitz | e7bb918 | 2008-01-27 17:10:14 +0000 | [diff] [blame] | 78 | if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 79 | return; |
| 80 | |
| 81 | raw = type->tp_subclasses; |
| 82 | if (raw != NULL) { |
| 83 | n = PyList_GET_SIZE(raw); |
| 84 | for (i = 0; i < n; i++) { |
| 85 | ref = PyList_GET_ITEM(raw, i); |
| 86 | ref = PyWeakref_GET_OBJECT(ref); |
| 87 | if (ref != Py_None) { |
Georg Brandl | 74a1dea | 2008-05-28 11:21:39 +0000 | [diff] [blame] | 88 | PyType_Modified((PyTypeObject *)ref); |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
| 92 | type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG; |
| 93 | } |
| 94 | |
| 95 | static void |
| 96 | type_mro_modified(PyTypeObject *type, PyObject *bases) { |
| 97 | /* |
| 98 | Check that all base classes or elements of the mro of type are |
| 99 | able to be cached. This function is called after the base |
| 100 | classes or mro of the type are altered. |
| 101 | |
| 102 | Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type |
| 103 | inherits from an old-style class, either directly or if it |
| 104 | appears in the MRO of a new-style class. No support either for |
| 105 | custom MROs that include types that are not officially super |
| 106 | types. |
| 107 | |
| 108 | Called from mro_internal, which will subsequently be called on |
| 109 | each subclass when their mro is recursively updated. |
| 110 | */ |
| 111 | Py_ssize_t i, n; |
| 112 | int clear = 0; |
| 113 | |
Neal Norwitz | e7bb918 | 2008-01-27 17:10:14 +0000 | [diff] [blame] | 114 | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 115 | return; |
| 116 | |
| 117 | n = PyTuple_GET_SIZE(bases); |
| 118 | for (i = 0; i < n; i++) { |
| 119 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 120 | PyTypeObject *cls; |
| 121 | |
| 122 | if (!PyType_Check(b) ) { |
| 123 | clear = 1; |
| 124 | break; |
| 125 | } |
| 126 | |
| 127 | cls = (PyTypeObject *)b; |
| 128 | |
| 129 | if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) || |
| 130 | !PyType_IsSubtype(type, cls)) { |
| 131 | clear = 1; |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (clear) |
| 137 | type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG| |
| 138 | Py_TPFLAGS_VALID_VERSION_TAG); |
| 139 | } |
| 140 | |
| 141 | static int |
| 142 | assign_version_tag(PyTypeObject *type) |
| 143 | { |
| 144 | /* Ensure that the tp_version_tag is valid and set |
| 145 | Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this |
| 146 | must first be done on all super classes. Return 0 if this |
| 147 | cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG. |
| 148 | */ |
| 149 | Py_ssize_t i, n; |
Georg Brandl | f18a707 | 2008-05-29 14:35:39 +0000 | [diff] [blame] | 150 | PyObject *bases; |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 151 | |
| 152 | if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
| 153 | return 1; |
| 154 | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) |
| 155 | return 0; |
| 156 | if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) |
| 157 | return 0; |
| 158 | |
| 159 | type->tp_version_tag = next_version_tag++; |
| 160 | /* for stress-testing: next_version_tag &= 0xFF; */ |
| 161 | |
| 162 | if (type->tp_version_tag == 0) { |
| 163 | /* wrap-around or just starting Python - clear the whole |
| 164 | cache by filling names with references to Py_None. |
| 165 | Values are also set to NULL for added protection, as they |
| 166 | are borrowed reference */ |
| 167 | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { |
| 168 | method_cache[i].value = NULL; |
Georg Brandl | f18a707 | 2008-05-29 14:35:39 +0000 | [diff] [blame] | 169 | Py_XDECREF(method_cache[i].name); |
Georg Brandl | 5ec330c | 2008-05-28 15:41:36 +0000 | [diff] [blame] | 170 | method_cache[i].name = Py_None; |
Georg Brandl | f18a707 | 2008-05-29 14:35:39 +0000 | [diff] [blame] | 171 | Py_INCREF(Py_None); |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 172 | } |
| 173 | /* mark all version tags as invalid */ |
Georg Brandl | 74a1dea | 2008-05-28 11:21:39 +0000 | [diff] [blame] | 174 | PyType_Modified(&PyBaseObject_Type); |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 175 | return 1; |
| 176 | } |
| 177 | bases = type->tp_bases; |
| 178 | n = PyTuple_GET_SIZE(bases); |
| 179 | for (i = 0; i < n; i++) { |
| 180 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 181 | assert(PyType_Check(b)); |
| 182 | if (!assign_version_tag((PyTypeObject *)b)) |
| 183 | return 0; |
| 184 | } |
| 185 | type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG; |
| 186 | return 1; |
| 187 | } |
| 188 | |
| 189 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 190 | static PyMemberDef type_members[] = { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 191 | {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY}, |
| 192 | {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY}, |
| 193 | {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY}, |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 194 | {"__weakrefoffset__", T_LONG, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 195 | offsetof(PyTypeObject, tp_weaklistoffset), READONLY}, |
| 196 | {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY}, |
| 197 | {"__dictoffset__", T_LONG, |
| 198 | offsetof(PyTypeObject, tp_dictoffset), READONLY}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 199 | {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY}, |
| 200 | {0} |
| 201 | }; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 202 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 203 | static PyObject * |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 204 | type_name(PyTypeObject *type, void *context) |
| 205 | { |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 206 | const char *s; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 207 | |
Michael W. Hudson | ade8c8b | 2002-11-27 16:29:26 +0000 | [diff] [blame] | 208 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 209 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 210 | |
Georg Brandl | c255c7b | 2006-02-20 22:27:28 +0000 | [diff] [blame] | 211 | Py_INCREF(et->ht_name); |
| 212 | return et->ht_name; |
Michael W. Hudson | ade8c8b | 2002-11-27 16:29:26 +0000 | [diff] [blame] | 213 | } |
| 214 | else { |
| 215 | s = strrchr(type->tp_name, '.'); |
| 216 | if (s == NULL) |
| 217 | s = type->tp_name; |
| 218 | else |
| 219 | s++; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 220 | return PyString_FromString(s); |
Michael W. Hudson | ade8c8b | 2002-11-27 16:29:26 +0000 | [diff] [blame] | 221 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 224 | static int |
| 225 | type_set_name(PyTypeObject *type, PyObject *value, void *context) |
| 226 | { |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 227 | PyHeapTypeObject* et; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 228 | |
| 229 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 230 | PyErr_Format(PyExc_TypeError, |
| 231 | "can't set %s.__name__", type->tp_name); |
| 232 | return -1; |
| 233 | } |
| 234 | if (!value) { |
| 235 | PyErr_Format(PyExc_TypeError, |
| 236 | "can't delete %s.__name__", type->tp_name); |
| 237 | return -1; |
| 238 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 239 | if (!PyString_Check(value)) { |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 240 | PyErr_Format(PyExc_TypeError, |
| 241 | "can only assign string to %s.__name__, not '%s'", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 242 | type->tp_name, Py_TYPE(value)->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 243 | return -1; |
| 244 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 245 | if (strlen(PyString_AS_STRING(value)) |
| 246 | != (size_t)PyString_GET_SIZE(value)) { |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 247 | PyErr_Format(PyExc_ValueError, |
| 248 | "__name__ must not contain null bytes"); |
| 249 | return -1; |
| 250 | } |
| 251 | |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 252 | et = (PyHeapTypeObject*)type; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 253 | |
| 254 | Py_INCREF(value); |
| 255 | |
Georg Brandl | c255c7b | 2006-02-20 22:27:28 +0000 | [diff] [blame] | 256 | Py_DECREF(et->ht_name); |
| 257 | et->ht_name = value; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 258 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 259 | type->tp_name = PyString_AS_STRING(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 264 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 265 | type_module(PyTypeObject *type, void *context) |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 266 | { |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 267 | PyObject *mod; |
| 268 | char *s; |
| 269 | |
Michael W. Hudson | ade8c8b | 2002-11-27 16:29:26 +0000 | [diff] [blame] | 270 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 271 | mod = PyDict_GetItemString(type->tp_dict, "__module__"); |
Anthony Baxter | 3ecdb25 | 2004-06-11 14:41:18 +0000 | [diff] [blame] | 272 | if (!mod) { |
| 273 | PyErr_Format(PyExc_AttributeError, "__module__"); |
| 274 | return 0; |
| 275 | } |
Michael W. Hudson | ade8c8b | 2002-11-27 16:29:26 +0000 | [diff] [blame] | 276 | Py_XINCREF(mod); |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 277 | return mod; |
| 278 | } |
Michael W. Hudson | ade8c8b | 2002-11-27 16:29:26 +0000 | [diff] [blame] | 279 | else { |
| 280 | s = strrchr(type->tp_name, '.'); |
| 281 | if (s != NULL) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 282 | return PyString_FromStringAndSize( |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 283 | type->tp_name, (Py_ssize_t)(s - type->tp_name)); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 284 | return PyString_FromString("__builtin__"); |
Michael W. Hudson | ade8c8b | 2002-11-27 16:29:26 +0000 | [diff] [blame] | 285 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 288 | static int |
| 289 | type_set_module(PyTypeObject *type, PyObject *value, void *context) |
| 290 | { |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 291 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 292 | PyErr_Format(PyExc_TypeError, |
| 293 | "can't set %s.__module__", type->tp_name); |
| 294 | return -1; |
| 295 | } |
| 296 | if (!value) { |
| 297 | PyErr_Format(PyExc_TypeError, |
| 298 | "can't delete %s.__module__", type->tp_name); |
| 299 | return -1; |
| 300 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 301 | |
Georg Brandl | 74a1dea | 2008-05-28 11:21:39 +0000 | [diff] [blame] | 302 | PyType_Modified(type); |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 303 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 304 | return PyDict_SetItemString(type->tp_dict, "__module__", value); |
| 305 | } |
| 306 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 307 | static PyObject * |
Jeffrey Yasskin | 960b9b7 | 2008-02-28 04:45:36 +0000 | [diff] [blame] | 308 | type_abstractmethods(PyTypeObject *type, void *context) |
| 309 | { |
| 310 | PyObject *mod = PyDict_GetItemString(type->tp_dict, |
| 311 | "__abstractmethods__"); |
| 312 | if (!mod) { |
| 313 | PyErr_Format(PyExc_AttributeError, "__abstractmethods__"); |
| 314 | return NULL; |
| 315 | } |
| 316 | Py_XINCREF(mod); |
| 317 | return mod; |
| 318 | } |
| 319 | |
| 320 | static int |
| 321 | type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context) |
| 322 | { |
| 323 | /* __abstractmethods__ should only be set once on a type, in |
| 324 | abc.ABCMeta.__new__, so this function doesn't do anything |
| 325 | special to update subclasses. |
| 326 | */ |
| 327 | int res = PyDict_SetItemString(type->tp_dict, |
| 328 | "__abstractmethods__", value); |
| 329 | if (res == 0) { |
Georg Brandl | 74a1dea | 2008-05-28 11:21:39 +0000 | [diff] [blame] | 330 | PyType_Modified(type); |
Jeffrey Yasskin | 960b9b7 | 2008-02-28 04:45:36 +0000 | [diff] [blame] | 331 | if (value && PyObject_IsTrue(value)) { |
| 332 | type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT; |
| 333 | } |
| 334 | else { |
| 335 | type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT; |
| 336 | } |
| 337 | } |
| 338 | return res; |
| 339 | } |
| 340 | |
| 341 | static PyObject * |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 342 | type_get_bases(PyTypeObject *type, void *context) |
| 343 | { |
| 344 | Py_INCREF(type->tp_bases); |
| 345 | return type->tp_bases; |
| 346 | } |
| 347 | |
| 348 | static PyTypeObject *best_base(PyObject *); |
| 349 | static int mro_internal(PyTypeObject *); |
| 350 | static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *); |
| 351 | static int add_subclass(PyTypeObject*, PyTypeObject*); |
| 352 | static void remove_subclass(PyTypeObject *, PyTypeObject *); |
| 353 | static void update_all_slots(PyTypeObject *); |
| 354 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 355 | typedef int (*update_callback)(PyTypeObject *, void *); |
| 356 | static int update_subclasses(PyTypeObject *type, PyObject *name, |
| 357 | update_callback callback, void *data); |
| 358 | static int recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
| 359 | update_callback callback, void *data); |
| 360 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 361 | static int |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 362 | mro_subclasses(PyTypeObject *type, PyObject* temp) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 363 | { |
| 364 | PyTypeObject *subclass; |
| 365 | PyObject *ref, *subclasses, *old_mro; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 366 | Py_ssize_t i, n; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 367 | |
| 368 | subclasses = type->tp_subclasses; |
| 369 | if (subclasses == NULL) |
| 370 | return 0; |
| 371 | assert(PyList_Check(subclasses)); |
| 372 | n = PyList_GET_SIZE(subclasses); |
| 373 | for (i = 0; i < n; i++) { |
| 374 | ref = PyList_GET_ITEM(subclasses, i); |
| 375 | assert(PyWeakref_CheckRef(ref)); |
| 376 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 377 | assert(subclass != NULL); |
| 378 | if ((PyObject *)subclass == Py_None) |
| 379 | continue; |
| 380 | assert(PyType_Check(subclass)); |
| 381 | old_mro = subclass->tp_mro; |
| 382 | if (mro_internal(subclass) < 0) { |
| 383 | subclass->tp_mro = old_mro; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 384 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 385 | } |
| 386 | else { |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 387 | PyObject* tuple; |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 388 | tuple = PyTuple_Pack(2, subclass, old_mro); |
Guido van Rossum | 19a02ba | 2003-04-15 22:09:45 +0000 | [diff] [blame] | 389 | Py_DECREF(old_mro); |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 390 | if (!tuple) |
| 391 | return -1; |
| 392 | if (PyList_Append(temp, tuple) < 0) |
| 393 | return -1; |
Guido van Rossum | 19a02ba | 2003-04-15 22:09:45 +0000 | [diff] [blame] | 394 | Py_DECREF(tuple); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 395 | } |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 396 | if (mro_subclasses(subclass, temp) < 0) |
| 397 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 398 | } |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 399 | return 0; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | static int |
| 403 | type_set_bases(PyTypeObject *type, PyObject *value, void *context) |
| 404 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 405 | Py_ssize_t i; |
| 406 | int r = 0; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 407 | PyObject *ob, *temp; |
Armin Rigo | c0ba52d | 2007-04-19 14:44:48 +0000 | [diff] [blame] | 408 | PyTypeObject *new_base, *old_base; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 409 | PyObject *old_bases, *old_mro; |
| 410 | |
| 411 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 412 | PyErr_Format(PyExc_TypeError, |
| 413 | "can't set %s.__bases__", type->tp_name); |
| 414 | return -1; |
| 415 | } |
| 416 | if (!value) { |
| 417 | PyErr_Format(PyExc_TypeError, |
| 418 | "can't delete %s.__bases__", type->tp_name); |
| 419 | return -1; |
| 420 | } |
| 421 | if (!PyTuple_Check(value)) { |
| 422 | PyErr_Format(PyExc_TypeError, |
| 423 | "can only assign tuple to %s.__bases__, not %s", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 424 | type->tp_name, Py_TYPE(value)->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 425 | return -1; |
| 426 | } |
Guido van Rossum | 3bbc0ee | 2002-12-13 17:49:38 +0000 | [diff] [blame] | 427 | if (PyTuple_GET_SIZE(value) == 0) { |
| 428 | PyErr_Format(PyExc_TypeError, |
| 429 | "can only assign non-empty tuple to %s.__bases__, not ()", |
| 430 | type->tp_name); |
| 431 | return -1; |
| 432 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 433 | for (i = 0; i < PyTuple_GET_SIZE(value); i++) { |
| 434 | ob = PyTuple_GET_ITEM(value, i); |
| 435 | if (!PyClass_Check(ob) && !PyType_Check(ob)) { |
| 436 | PyErr_Format( |
| 437 | PyExc_TypeError, |
| 438 | "%s.__bases__ must be tuple of old- or new-style classes, not '%s'", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 439 | type->tp_name, Py_TYPE(ob)->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 440 | return -1; |
| 441 | } |
Michael W. Hudson | caf17be | 2002-11-27 10:24:44 +0000 | [diff] [blame] | 442 | if (PyType_Check(ob)) { |
| 443 | if (PyType_IsSubtype((PyTypeObject*)ob, type)) { |
| 444 | PyErr_SetString(PyExc_TypeError, |
| 445 | "a __bases__ item causes an inheritance cycle"); |
| 446 | return -1; |
| 447 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
| 451 | new_base = best_base(value); |
| 452 | |
| 453 | if (!new_base) { |
| 454 | return -1; |
| 455 | } |
| 456 | |
| 457 | if (!compatible_for_assignment(type->tp_base, new_base, "__bases__")) |
| 458 | return -1; |
| 459 | |
| 460 | Py_INCREF(new_base); |
| 461 | Py_INCREF(value); |
| 462 | |
| 463 | old_bases = type->tp_bases; |
| 464 | old_base = type->tp_base; |
| 465 | old_mro = type->tp_mro; |
| 466 | |
| 467 | type->tp_bases = value; |
| 468 | type->tp_base = new_base; |
| 469 | |
| 470 | if (mro_internal(type) < 0) { |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 471 | goto bail; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 474 | temp = PyList_New(0); |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 475 | if (!temp) |
| 476 | goto bail; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 477 | |
| 478 | r = mro_subclasses(type, temp); |
| 479 | |
| 480 | if (r < 0) { |
| 481 | for (i = 0; i < PyList_Size(temp); i++) { |
| 482 | PyTypeObject* cls; |
| 483 | PyObject* mro; |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 484 | PyArg_UnpackTuple(PyList_GET_ITEM(temp, i), |
| 485 | "", 2, 2, &cls, &mro); |
Armin Rigo | 796fc99 | 2007-04-19 14:56:48 +0000 | [diff] [blame] | 486 | Py_INCREF(mro); |
| 487 | ob = cls->tp_mro; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 488 | cls->tp_mro = mro; |
Armin Rigo | 796fc99 | 2007-04-19 14:56:48 +0000 | [diff] [blame] | 489 | Py_DECREF(ob); |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 490 | } |
| 491 | Py_DECREF(temp); |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 492 | goto bail; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | Py_DECREF(temp); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 496 | |
| 497 | /* any base that was in __bases__ but now isn't, we |
Raymond Hettinger | a828586 | 2002-12-14 17:17:56 +0000 | [diff] [blame] | 498 | need to remove |type| from its tp_subclasses. |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 499 | conversely, any class now in __bases__ that wasn't |
Raymond Hettinger | a828586 | 2002-12-14 17:17:56 +0000 | [diff] [blame] | 500 | needs to have |type| added to its subclasses. */ |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 501 | |
| 502 | /* for now, sod that: just remove from all old_bases, |
| 503 | add to all new_bases */ |
| 504 | |
| 505 | for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) { |
| 506 | ob = PyTuple_GET_ITEM(old_bases, i); |
| 507 | if (PyType_Check(ob)) { |
| 508 | remove_subclass( |
| 509 | (PyTypeObject*)ob, type); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) { |
| 514 | ob = PyTuple_GET_ITEM(value, i); |
| 515 | if (PyType_Check(ob)) { |
| 516 | if (add_subclass((PyTypeObject*)ob, type) < 0) |
| 517 | r = -1; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | update_all_slots(type); |
| 522 | |
| 523 | Py_DECREF(old_bases); |
| 524 | Py_DECREF(old_base); |
| 525 | Py_DECREF(old_mro); |
| 526 | |
| 527 | return r; |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 528 | |
| 529 | bail: |
Michael W. Hudson | e723e45 | 2003-08-07 14:58:10 +0000 | [diff] [blame] | 530 | Py_DECREF(type->tp_bases); |
| 531 | Py_DECREF(type->tp_base); |
| 532 | if (type->tp_mro != old_mro) { |
| 533 | Py_DECREF(type->tp_mro); |
| 534 | } |
| 535 | |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 536 | type->tp_bases = old_bases; |
| 537 | type->tp_base = old_base; |
| 538 | type->tp_mro = old_mro; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 539 | |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 540 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 544 | type_dict(PyTypeObject *type, void *context) |
| 545 | { |
| 546 | if (type->tp_dict == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 547 | Py_INCREF(Py_None); |
| 548 | return Py_None; |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 549 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 550 | return PyDictProxy_New(type->tp_dict); |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 553 | static PyObject * |
| 554 | type_get_doc(PyTypeObject *type, void *context) |
| 555 | { |
| 556 | PyObject *result; |
Guido van Rossum | 6ca7d41 | 2002-04-18 00:22:00 +0000 | [diff] [blame] | 557 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 558 | return PyString_FromString(type->tp_doc); |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 559 | result = PyDict_GetItemString(type->tp_dict, "__doc__"); |
Guido van Rossum | 6ca7d41 | 2002-04-18 00:22:00 +0000 | [diff] [blame] | 560 | if (result == NULL) { |
| 561 | result = Py_None; |
| 562 | Py_INCREF(result); |
| 563 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 564 | else if (Py_TYPE(result)->tp_descr_get) { |
| 565 | result = Py_TYPE(result)->tp_descr_get(result, NULL, |
Tim Peters | 2b85897 | 2002-04-18 04:12:28 +0000 | [diff] [blame] | 566 | (PyObject *)type); |
Guido van Rossum | 6ca7d41 | 2002-04-18 00:22:00 +0000 | [diff] [blame] | 567 | } |
| 568 | else { |
| 569 | Py_INCREF(result); |
| 570 | } |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 571 | return result; |
| 572 | } |
| 573 | |
Antoine Pitrou | 0668c62 | 2008-08-26 22:42:08 +0000 | [diff] [blame] | 574 | static PyObject * |
| 575 | type___instancecheck__(PyObject *type, PyObject *inst) |
| 576 | { |
| 577 | switch (_PyObject_RealIsInstance(inst, type)) { |
| 578 | case -1: |
| 579 | return NULL; |
| 580 | case 0: |
| 581 | Py_RETURN_FALSE; |
| 582 | default: |
| 583 | Py_RETURN_TRUE; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | |
| 588 | static PyObject * |
| 589 | type_get_instancecheck(PyObject *type, void *context) |
| 590 | { |
| 591 | static PyMethodDef ml = {"__instancecheck__", |
| 592 | type___instancecheck__, METH_O }; |
| 593 | return PyCFunction_New(&ml, type); |
| 594 | } |
| 595 | |
| 596 | static PyObject * |
| 597 | type___subclasscheck__(PyObject *type, PyObject *inst) |
| 598 | { |
| 599 | switch (_PyObject_RealIsSubclass(inst, type)) { |
| 600 | case -1: |
| 601 | return NULL; |
| 602 | case 0: |
| 603 | Py_RETURN_FALSE; |
| 604 | default: |
| 605 | Py_RETURN_TRUE; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | static PyObject * |
| 610 | type_get_subclasscheck(PyObject *type, void *context) |
| 611 | { |
| 612 | static PyMethodDef ml = {"__subclasscheck__", |
| 613 | type___subclasscheck__, METH_O }; |
| 614 | return PyCFunction_New(&ml, type); |
| 615 | } |
| 616 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 617 | static PyGetSetDef type_getsets[] = { |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 618 | {"__name__", (getter)type_name, (setter)type_set_name, NULL}, |
| 619 | {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL}, |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 620 | {"__module__", (getter)type_module, (setter)type_set_module, NULL}, |
Jeffrey Yasskin | 960b9b7 | 2008-02-28 04:45:36 +0000 | [diff] [blame] | 621 | {"__abstractmethods__", (getter)type_abstractmethods, |
| 622 | (setter)type_set_abstractmethods, NULL}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 623 | {"__dict__", (getter)type_dict, NULL, NULL}, |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 624 | {"__doc__", (getter)type_get_doc, NULL, NULL}, |
Antoine Pitrou | 0668c62 | 2008-08-26 22:42:08 +0000 | [diff] [blame] | 625 | {"__instancecheck__", (getter)type_get_instancecheck, NULL, NULL}, |
| 626 | {"__subclasscheck__", (getter)type_get_subclasscheck, NULL, NULL}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 627 | {0} |
| 628 | }; |
| 629 | |
Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 630 | static int |
| 631 | type_compare(PyObject *v, PyObject *w) |
| 632 | { |
| 633 | /* This is called with type objects only. So we |
| 634 | can just compare the addresses. */ |
| 635 | Py_uintptr_t vv = (Py_uintptr_t)v; |
| 636 | Py_uintptr_t ww = (Py_uintptr_t)w; |
| 637 | return (vv < ww) ? -1 : (vv > ww) ? 1 : 0; |
| 638 | } |
| 639 | |
Steven Bethard | ae42f33 | 2008-03-18 17:26:10 +0000 | [diff] [blame] | 640 | static PyObject* |
| 641 | type_richcompare(PyObject *v, PyObject *w, int op) |
| 642 | { |
| 643 | PyObject *result; |
| 644 | Py_uintptr_t vv, ww; |
| 645 | int c; |
| 646 | |
| 647 | /* Make sure both arguments are types. */ |
| 648 | if (!PyType_Check(v) || !PyType_Check(w)) { |
| 649 | result = Py_NotImplemented; |
| 650 | goto out; |
| 651 | } |
| 652 | |
| 653 | /* Py3K warning if comparison isn't == or != */ |
| 654 | if (Py_Py3kWarningFlag && op != Py_EQ && op != Py_NE && |
Benjamin Peterson | 9f4f481 | 2008-04-27 03:01:45 +0000 | [diff] [blame] | 655 | PyErr_WarnEx(PyExc_DeprecationWarning, |
Georg Brandl | d5b635f | 2008-03-25 08:29:14 +0000 | [diff] [blame] | 656 | "type inequality comparisons not supported " |
Benjamin Peterson | 9f4f481 | 2008-04-27 03:01:45 +0000 | [diff] [blame] | 657 | "in 3.x", 1) < 0) { |
Steven Bethard | ae42f33 | 2008-03-18 17:26:10 +0000 | [diff] [blame] | 658 | return NULL; |
| 659 | } |
| 660 | |
| 661 | /* Compare addresses */ |
| 662 | vv = (Py_uintptr_t)v; |
| 663 | ww = (Py_uintptr_t)w; |
| 664 | switch (op) { |
| 665 | case Py_LT: c = vv < ww; break; |
| 666 | case Py_LE: c = vv <= ww; break; |
| 667 | case Py_EQ: c = vv == ww; break; |
| 668 | case Py_NE: c = vv != ww; break; |
| 669 | case Py_GT: c = vv > ww; break; |
| 670 | case Py_GE: c = vv >= ww; break; |
| 671 | default: |
| 672 | result = Py_NotImplemented; |
| 673 | goto out; |
| 674 | } |
| 675 | result = c ? Py_True : Py_False; |
| 676 | |
| 677 | /* incref and return */ |
| 678 | out: |
| 679 | Py_INCREF(result); |
| 680 | return result; |
| 681 | } |
| 682 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 683 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 684 | type_repr(PyTypeObject *type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 685 | { |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 686 | PyObject *mod, *name, *rtn; |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 687 | char *kind; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 688 | |
| 689 | mod = type_module(type, NULL); |
| 690 | if (mod == NULL) |
| 691 | PyErr_Clear(); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 692 | else if (!PyString_Check(mod)) { |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 693 | Py_DECREF(mod); |
| 694 | mod = NULL; |
| 695 | } |
| 696 | name = type_name(type, NULL); |
| 697 | if (name == NULL) |
| 698 | return NULL; |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 699 | |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 700 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 701 | kind = "class"; |
| 702 | else |
| 703 | kind = "type"; |
| 704 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 705 | if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) { |
| 706 | rtn = PyString_FromFormat("<%s '%s.%s'>", |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 707 | kind, |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 708 | PyString_AS_STRING(mod), |
| 709 | PyString_AS_STRING(name)); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 710 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 711 | else |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 712 | rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 713 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 714 | Py_XDECREF(mod); |
| 715 | Py_DECREF(name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 716 | return rtn; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 719 | static PyObject * |
| 720 | type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 721 | { |
| 722 | PyObject *obj; |
| 723 | |
| 724 | if (type->tp_new == NULL) { |
| 725 | PyErr_Format(PyExc_TypeError, |
| 726 | "cannot create '%.100s' instances", |
| 727 | type->tp_name); |
| 728 | return NULL; |
| 729 | } |
| 730 | |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 731 | obj = type->tp_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 732 | if (obj != NULL) { |
Guido van Rossum | f76de62 | 2001-10-18 15:49:21 +0000 | [diff] [blame] | 733 | /* Ugly exception: when the call was type(something), |
| 734 | don't call tp_init on the result. */ |
| 735 | if (type == &PyType_Type && |
| 736 | PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && |
| 737 | (kwds == NULL || |
| 738 | (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) |
| 739 | return obj; |
Guido van Rossum | 8ace1ab | 2002-04-06 01:05:01 +0000 | [diff] [blame] | 740 | /* If the returned object is not an instance of type, |
| 741 | it won't be initialized. */ |
| 742 | if (!PyType_IsSubtype(obj->ob_type, type)) |
| 743 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 744 | type = obj->ob_type; |
Jeremy Hylton | 719841e | 2002-07-16 19:39:38 +0000 | [diff] [blame] | 745 | if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) && |
| 746 | type->tp_init != NULL && |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 747 | type->tp_init(obj, args, kwds) < 0) { |
| 748 | Py_DECREF(obj); |
| 749 | obj = NULL; |
| 750 | } |
| 751 | } |
| 752 | return obj; |
| 753 | } |
| 754 | |
| 755 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 756 | PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 757 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 758 | PyObject *obj; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 759 | const size_t size = _PyObject_VAR_SIZE(type, nitems+1); |
| 760 | /* note that we need to add one, for the sentinel */ |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 761 | |
| 762 | if (PyType_IS_GC(type)) |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 763 | obj = _PyObject_GC_Malloc(size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 764 | else |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 765 | obj = (PyObject *)PyObject_MALLOC(size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 766 | |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 767 | if (obj == NULL) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 768 | return PyErr_NoMemory(); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 769 | |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 770 | memset(obj, '\0', size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 771 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 772 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 773 | Py_INCREF(type); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 774 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 775 | if (type->tp_itemsize == 0) |
| 776 | PyObject_INIT(obj, type); |
| 777 | else |
| 778 | (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 779 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 780 | if (PyType_IS_GC(type)) |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 781 | _PyObject_GC_TRACK(obj); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 782 | return obj; |
| 783 | } |
| 784 | |
| 785 | PyObject * |
| 786 | PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 787 | { |
| 788 | return type->tp_alloc(type, 0); |
| 789 | } |
| 790 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 791 | /* Helpers for subtyping */ |
| 792 | |
| 793 | static int |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 794 | traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg) |
| 795 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 796 | Py_ssize_t i, n; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 797 | PyMemberDef *mp; |
| 798 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 799 | n = Py_SIZE(type); |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 800 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 801 | for (i = 0; i < n; i++, mp++) { |
| 802 | if (mp->type == T_OBJECT_EX) { |
| 803 | char *addr = (char *)self + mp->offset; |
| 804 | PyObject *obj = *(PyObject **)addr; |
| 805 | if (obj != NULL) { |
| 806 | int err = visit(obj, arg); |
| 807 | if (err) |
| 808 | return err; |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | static int |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 816 | subtype_traverse(PyObject *self, visitproc visit, void *arg) |
| 817 | { |
| 818 | PyTypeObject *type, *base; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 819 | traverseproc basetraverse; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 820 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 821 | /* Find the nearest base with a different tp_traverse, |
| 822 | and traverse slots while we're at it */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 823 | type = Py_TYPE(self); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 824 | base = type; |
| 825 | while ((basetraverse = base->tp_traverse) == subtype_traverse) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 826 | if (Py_SIZE(base)) { |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 827 | int err = traverse_slots(base, self, visit, arg); |
| 828 | if (err) |
| 829 | return err; |
| 830 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 831 | base = base->tp_base; |
| 832 | assert(base); |
| 833 | } |
| 834 | |
| 835 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 836 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 837 | if (dictptr && *dictptr) |
| 838 | Py_VISIT(*dictptr); |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 841 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 842 | /* For a heaptype, the instances count as references |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 843 | to the type. Traverse the type so the collector |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 844 | can find cycles involving this link. */ |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 845 | Py_VISIT(type); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 846 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 847 | if (basetraverse) |
| 848 | return basetraverse(self, visit, arg); |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | static void |
| 853 | clear_slots(PyTypeObject *type, PyObject *self) |
| 854 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 855 | Py_ssize_t i, n; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 856 | PyMemberDef *mp; |
| 857 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 858 | n = Py_SIZE(type); |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 859 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 860 | for (i = 0; i < n; i++, mp++) { |
| 861 | if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) { |
| 862 | char *addr = (char *)self + mp->offset; |
| 863 | PyObject *obj = *(PyObject **)addr; |
| 864 | if (obj != NULL) { |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 865 | *(PyObject **)addr = NULL; |
Thomas Wouters | edf17d8 | 2006-04-15 17:28:34 +0000 | [diff] [blame] | 866 | Py_DECREF(obj); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | static int |
| 873 | subtype_clear(PyObject *self) |
| 874 | { |
| 875 | PyTypeObject *type, *base; |
| 876 | inquiry baseclear; |
| 877 | |
| 878 | /* Find the nearest base with a different tp_clear |
| 879 | and clear slots while we're at it */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 880 | type = Py_TYPE(self); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 881 | base = type; |
| 882 | while ((baseclear = base->tp_clear) == subtype_clear) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 883 | if (Py_SIZE(base)) |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 884 | clear_slots(base, self); |
| 885 | base = base->tp_base; |
| 886 | assert(base); |
| 887 | } |
| 888 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 889 | /* There's no need to clear the instance dict (if any); |
| 890 | the collector will call its tp_clear handler. */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 891 | |
| 892 | if (baseclear) |
| 893 | return baseclear(self); |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 894 | return 0; |
| 895 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 896 | |
| 897 | static void |
| 898 | subtype_dealloc(PyObject *self) |
| 899 | { |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 900 | PyTypeObject *type, *base; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 901 | destructor basedealloc; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 902 | |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 903 | /* Extract the type; we expect it to be a heap type */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 904 | type = Py_TYPE(self); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 905 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 906 | |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 907 | /* Test whether the type has GC exactly once */ |
| 908 | |
| 909 | if (!PyType_IS_GC(type)) { |
| 910 | /* It's really rare to find a dynamic type that doesn't have |
| 911 | GC; it can only happen when deriving from 'object' and not |
| 912 | adding any slots or instance variables. This allows |
| 913 | certain simplifications: there's no need to call |
| 914 | clear_slots(), or DECREF the dict, or clear weakrefs. */ |
| 915 | |
| 916 | /* Maybe call finalizer; exit early if resurrected */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 917 | if (type->tp_del) { |
| 918 | type->tp_del(self); |
| 919 | if (self->ob_refcnt > 0) |
| 920 | return; |
| 921 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 922 | |
| 923 | /* Find the nearest base with a different tp_dealloc */ |
| 924 | base = type; |
| 925 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 926 | assert(Py_SIZE(base) == 0); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 927 | base = base->tp_base; |
| 928 | assert(base); |
| 929 | } |
| 930 | |
| 931 | /* Call the base tp_dealloc() */ |
| 932 | assert(basedealloc); |
| 933 | basedealloc(self); |
| 934 | |
| 935 | /* Can't reference self beyond this point */ |
| 936 | Py_DECREF(type); |
| 937 | |
| 938 | /* Done */ |
| 939 | return; |
| 940 | } |
| 941 | |
| 942 | /* We get here only if the type has GC */ |
| 943 | |
| 944 | /* UnTrack and re-Track around the trashcan macro, alas */ |
Andrew M. Kuchling | c9172d3 | 2003-02-06 15:22:49 +0000 | [diff] [blame] | 945 | /* See explanation at end of function for full disclosure */ |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 946 | PyObject_GC_UnTrack(self); |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 947 | ++_PyTrash_delete_nesting; |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 948 | Py_TRASHCAN_SAFE_BEGIN(self); |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 949 | --_PyTrash_delete_nesting; |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 950 | /* DO NOT restore GC tracking at this point. weakref callbacks |
| 951 | * (if any, and whether directly here or indirectly in something we |
| 952 | * call) may trigger GC, and if self is tracked at that point, it |
| 953 | * 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] | 954 | */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 955 | |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 956 | /* Find the nearest base with a different tp_dealloc */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 957 | base = type; |
| 958 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 959 | base = base->tp_base; |
| 960 | assert(base); |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 963 | /* 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] | 964 | the finalizer (__del__), clearing slots, or clearing the instance |
| 965 | dict. */ |
| 966 | |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 967 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) |
| 968 | PyObject_ClearWeakRefs(self); |
| 969 | |
| 970 | /* Maybe call finalizer; exit early if resurrected */ |
| 971 | if (type->tp_del) { |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 972 | _PyObject_GC_TRACK(self); |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 973 | type->tp_del(self); |
| 974 | if (self->ob_refcnt > 0) |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 975 | goto endlabel; /* resurrected */ |
| 976 | else |
| 977 | _PyObject_GC_UNTRACK(self); |
Brett Cannon | f5bee30 | 2007-01-23 23:21:22 +0000 | [diff] [blame] | 978 | /* New weakrefs could be created during the finalizer call. |
| 979 | If this occurs, clear them out without calling their |
| 980 | finalizers since they might rely on part of the object |
| 981 | being finalized that has already been destroyed. */ |
| 982 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) { |
| 983 | /* Modeled after GET_WEAKREFS_LISTPTR() */ |
| 984 | PyWeakReference **list = (PyWeakReference **) \ |
| 985 | PyObject_GET_WEAKREFS_LISTPTR(self); |
| 986 | while (*list) |
| 987 | _PyWeakref_ClearRef(*list); |
| 988 | } |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 991 | /* Clear slots up to the nearest base with a different tp_dealloc */ |
| 992 | base = type; |
| 993 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 994 | if (Py_SIZE(base)) |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 995 | clear_slots(base, self); |
| 996 | base = base->tp_base; |
| 997 | assert(base); |
| 998 | } |
| 999 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1000 | /* If we added a dict, DECREF it */ |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1001 | if (type->tp_dictoffset && !base->tp_dictoffset) { |
| 1002 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 1003 | if (dictptr != NULL) { |
| 1004 | PyObject *dict = *dictptr; |
| 1005 | if (dict != NULL) { |
| 1006 | Py_DECREF(dict); |
| 1007 | *dictptr = NULL; |
| 1008 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | |
Tim Peters | 0bd743c | 2003-11-13 22:50:00 +0000 | [diff] [blame] | 1012 | /* Call the base tp_dealloc(); first retrack self if |
| 1013 | * basedealloc knows about gc. |
| 1014 | */ |
| 1015 | if (PyType_IS_GC(base)) |
| 1016 | _PyObject_GC_TRACK(self); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1017 | assert(basedealloc); |
| 1018 | basedealloc(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1019 | |
| 1020 | /* Can't reference self beyond this point */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1021 | Py_DECREF(type); |
| 1022 | |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 1023 | endlabel: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1024 | ++_PyTrash_delete_nesting; |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1025 | Py_TRASHCAN_SAFE_END(self); |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1026 | --_PyTrash_delete_nesting; |
| 1027 | |
| 1028 | /* Explanation of the weirdness around the trashcan macros: |
| 1029 | |
| 1030 | Q. What do the trashcan macros do? |
| 1031 | |
| 1032 | A. Read the comment titled "Trashcan mechanism" in object.h. |
| 1033 | For one, this explains why there must be a call to GC-untrack |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1034 | before the trashcan begin macro. Without understanding the |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1035 | trashcan code, the answers to the following questions don't make |
| 1036 | sense. |
| 1037 | |
| 1038 | Q. Why do we GC-untrack before the trashcan and then immediately |
| 1039 | GC-track again afterward? |
| 1040 | |
| 1041 | A. In the case that the base class is GC-aware, the base class |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1042 | probably GC-untracks the object. If it does that using the |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1043 | UNTRACK macro, this will crash when the object is already |
| 1044 | untracked. Because we don't know what the base class does, the |
| 1045 | only safe thing is to make sure the object is tracked when we |
| 1046 | call the base class dealloc. But... The trashcan begin macro |
| 1047 | requires that the object is *untracked* before it is called. So |
| 1048 | the dance becomes: |
| 1049 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1050 | GC untrack |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1051 | trashcan begin |
| 1052 | GC track |
| 1053 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1054 | Q. Why did the last question say "immediately GC-track again"? |
| 1055 | It's nowhere near immediately. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 1056 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1057 | A. Because the code *used* to re-track immediately. Bad Idea. |
| 1058 | self has a refcount of 0, and if gc ever gets its hands on it |
| 1059 | (which can happen if any weakref callback gets invoked), it |
| 1060 | looks like trash to gc too, and gc also tries to delete self |
| 1061 | then. But we're already deleting self. Double dealloction is |
| 1062 | a subtle disaster. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 1063 | |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1064 | Q. Why the bizarre (net-zero) manipulation of |
| 1065 | _PyTrash_delete_nesting around the trashcan macros? |
| 1066 | |
| 1067 | A. Some base classes (e.g. list) also use the trashcan mechanism. |
| 1068 | The following scenario used to be possible: |
| 1069 | |
| 1070 | - suppose the trashcan level is one below the trashcan limit |
| 1071 | |
| 1072 | - subtype_dealloc() is called |
| 1073 | |
| 1074 | - the trashcan limit is not yet reached, so the trashcan level |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1075 | is incremented and the code between trashcan begin and end is |
| 1076 | executed |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1077 | |
| 1078 | - this destroys much of the object's contents, including its |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1079 | slots and __dict__ |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1080 | |
| 1081 | - basedealloc() is called; this is really list_dealloc(), or |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1082 | some other type which also uses the trashcan macros |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1083 | |
| 1084 | - the trashcan limit is now reached, so the object is put on the |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1085 | trashcan's to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1086 | |
| 1087 | - basedealloc() returns |
| 1088 | |
| 1089 | - subtype_dealloc() decrefs the object's type |
| 1090 | |
| 1091 | - subtype_dealloc() returns |
| 1092 | |
| 1093 | - later, the trashcan code starts deleting the objects from its |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1094 | to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1095 | |
| 1096 | - subtype_dealloc() is called *AGAIN* for the same object |
| 1097 | |
| 1098 | - at the very least (if the destroyed slots and __dict__ don't |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1099 | cause problems) the object's type gets decref'ed a second |
| 1100 | time, which is *BAD*!!! |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1101 | |
| 1102 | The remedy is to make sure that if the code between trashcan |
| 1103 | begin and end in subtype_dealloc() is called, the code between |
| 1104 | trashcan begin and end in basedealloc() will also be called. |
| 1105 | This is done by decrementing the level after passing into the |
| 1106 | trashcan block, and incrementing it just before leaving the |
| 1107 | block. |
| 1108 | |
| 1109 | But now it's possible that a chain of objects consisting solely |
| 1110 | of objects whose deallocator is subtype_dealloc() will defeat |
| 1111 | the trashcan mechanism completely: the decremented level means |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1112 | that the effective level never reaches the limit. Therefore, we |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1113 | *increment* the level *before* entering the trashcan block, and |
| 1114 | matchingly decrement it after leaving. This means the trashcan |
| 1115 | code will trigger a little early, but that's no big deal. |
| 1116 | |
| 1117 | Q. Are there any live examples of code in need of all this |
| 1118 | complexity? |
| 1119 | |
| 1120 | A. Yes. See SF bug 668433 for code that crashed (when Python was |
| 1121 | compiled in debug mode) before the trashcan level manipulations |
| 1122 | were added. For more discussion, see SF patches 581742, 575073 |
| 1123 | and bug 574207. |
| 1124 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1127 | static PyTypeObject *solid_base(PyTypeObject *type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1128 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1129 | /* type test with subclassing support */ |
| 1130 | |
| 1131 | int |
| 1132 | PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
| 1133 | { |
| 1134 | PyObject *mro; |
| 1135 | |
Guido van Rossum | 9478d07 | 2001-09-07 18:52:13 +0000 | [diff] [blame] | 1136 | if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS)) |
| 1137 | return b == a || b == &PyBaseObject_Type; |
| 1138 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1139 | mro = a->tp_mro; |
| 1140 | if (mro != NULL) { |
| 1141 | /* Deal with multiple inheritance without recursion |
| 1142 | by walking the MRO tuple */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1143 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1144 | assert(PyTuple_Check(mro)); |
| 1145 | n = PyTuple_GET_SIZE(mro); |
| 1146 | for (i = 0; i < n; i++) { |
| 1147 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) |
| 1148 | return 1; |
| 1149 | } |
| 1150 | return 0; |
| 1151 | } |
| 1152 | else { |
| 1153 | /* a is not completely initilized yet; follow tp_base */ |
| 1154 | do { |
| 1155 | if (a == b) |
| 1156 | return 1; |
| 1157 | a = a->tp_base; |
| 1158 | } while (a != NULL); |
| 1159 | return b == &PyBaseObject_Type; |
| 1160 | } |
| 1161 | } |
| 1162 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1163 | /* Internal routines to do a method lookup in the type |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1164 | without looking in the instance dictionary |
| 1165 | (so we can't use PyObject_GetAttr) but still binding |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1166 | it to the instance. The arguments are the object, |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1167 | 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] | 1168 | static variable used to cache the interned Python string. |
| 1169 | |
| 1170 | Two variants: |
| 1171 | |
| 1172 | - lookup_maybe() returns NULL without raising an exception |
| 1173 | when the _PyType_Lookup() call fails; |
| 1174 | |
| 1175 | - lookup_method() always raises an exception upon errors. |
| 1176 | */ |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1177 | |
| 1178 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1179 | lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1180 | { |
| 1181 | PyObject *res; |
| 1182 | |
| 1183 | if (*attrobj == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1184 | *attrobj = PyString_InternFromString(attrstr); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1185 | if (*attrobj == NULL) |
| 1186 | return NULL; |
| 1187 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1188 | res = _PyType_Lookup(Py_TYPE(self), *attrobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1189 | if (res != NULL) { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1190 | descrgetfunc f; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1191 | if ((f = Py_TYPE(res)->tp_descr_get) == NULL) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1192 | Py_INCREF(res); |
| 1193 | else |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1194 | res = f(res, self, (PyObject *)(Py_TYPE(self))); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1195 | } |
| 1196 | return res; |
| 1197 | } |
| 1198 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1199 | static PyObject * |
| 1200 | lookup_method(PyObject *self, char *attrstr, PyObject **attrobj) |
| 1201 | { |
| 1202 | PyObject *res = lookup_maybe(self, attrstr, attrobj); |
| 1203 | if (res == NULL && !PyErr_Occurred()) |
| 1204 | PyErr_SetObject(PyExc_AttributeError, *attrobj); |
| 1205 | return res; |
| 1206 | } |
| 1207 | |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1208 | /* A variation of PyObject_CallMethod that uses lookup_method() |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1209 | instead of PyObject_GetAttrString(). This uses the same convention |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1210 | as lookup_method to cache the interned name string object. */ |
| 1211 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1212 | static PyObject * |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1213 | call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 1214 | { |
| 1215 | va_list va; |
| 1216 | PyObject *args, *func = 0, *retval; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1217 | va_start(va, format); |
| 1218 | |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 1219 | func = lookup_maybe(o, name, nameobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1220 | if (func == NULL) { |
| 1221 | va_end(va); |
| 1222 | if (!PyErr_Occurred()) |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 1223 | PyErr_SetObject(PyExc_AttributeError, *nameobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1224 | return NULL; |
| 1225 | } |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1226 | |
| 1227 | if (format && *format) |
| 1228 | args = Py_VaBuildValue(format, va); |
| 1229 | else |
| 1230 | args = PyTuple_New(0); |
| 1231 | |
| 1232 | va_end(va); |
| 1233 | |
| 1234 | if (args == NULL) |
| 1235 | return NULL; |
| 1236 | |
| 1237 | assert(PyTuple_Check(args)); |
| 1238 | retval = PyObject_Call(func, args, NULL); |
| 1239 | |
| 1240 | Py_DECREF(args); |
| 1241 | Py_DECREF(func); |
| 1242 | |
| 1243 | return retval; |
| 1244 | } |
| 1245 | |
| 1246 | /* Clone of call_method() that returns NotImplemented when the lookup fails. */ |
| 1247 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1248 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1249 | call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 1250 | { |
| 1251 | va_list va; |
| 1252 | PyObject *args, *func = 0, *retval; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1253 | va_start(va, format); |
| 1254 | |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 1255 | func = lookup_maybe(o, name, nameobj); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1256 | if (func == NULL) { |
| 1257 | va_end(va); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1258 | if (!PyErr_Occurred()) { |
| 1259 | Py_INCREF(Py_NotImplemented); |
| 1260 | return Py_NotImplemented; |
| 1261 | } |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 1262 | return NULL; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | if (format && *format) |
| 1266 | args = Py_VaBuildValue(format, va); |
| 1267 | else |
| 1268 | args = PyTuple_New(0); |
| 1269 | |
| 1270 | va_end(va); |
| 1271 | |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 1272 | if (args == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1273 | return NULL; |
| 1274 | |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 1275 | assert(PyTuple_Check(args)); |
| 1276 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1277 | |
| 1278 | Py_DECREF(args); |
| 1279 | Py_DECREF(func); |
| 1280 | |
| 1281 | return retval; |
| 1282 | } |
| 1283 | |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1284 | static int |
| 1285 | fill_classic_mro(PyObject *mro, PyObject *cls) |
| 1286 | { |
| 1287 | PyObject *bases, *base; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1288 | Py_ssize_t i, n; |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1289 | |
| 1290 | assert(PyList_Check(mro)); |
| 1291 | assert(PyClass_Check(cls)); |
| 1292 | i = PySequence_Contains(mro, cls); |
| 1293 | if (i < 0) |
| 1294 | return -1; |
| 1295 | if (!i) { |
| 1296 | if (PyList_Append(mro, cls) < 0) |
| 1297 | return -1; |
| 1298 | } |
| 1299 | bases = ((PyClassObject *)cls)->cl_bases; |
| 1300 | assert(bases && PyTuple_Check(bases)); |
| 1301 | n = PyTuple_GET_SIZE(bases); |
| 1302 | for (i = 0; i < n; i++) { |
| 1303 | base = PyTuple_GET_ITEM(bases, i); |
| 1304 | if (fill_classic_mro(mro, base) < 0) |
| 1305 | return -1; |
| 1306 | } |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
| 1310 | static PyObject * |
| 1311 | classic_mro(PyObject *cls) |
| 1312 | { |
| 1313 | PyObject *mro; |
| 1314 | |
| 1315 | assert(PyClass_Check(cls)); |
| 1316 | mro = PyList_New(0); |
| 1317 | if (mro != NULL) { |
| 1318 | if (fill_classic_mro(mro, cls) == 0) |
| 1319 | return mro; |
| 1320 | Py_DECREF(mro); |
| 1321 | } |
| 1322 | return NULL; |
| 1323 | } |
| 1324 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1325 | /* |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1326 | Method resolution order algorithm C3 described in |
| 1327 | "A Monotonic Superclass Linearization for Dylan", |
| 1328 | by Kim Barrett, Bob Cassel, Paul Haahr, |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1329 | David A. Moon, Keith Playford, and P. Tucker Withington. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1330 | (OOPSLA 1996) |
| 1331 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1332 | Some notes about the rules implied by C3: |
| 1333 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1334 | No duplicate bases. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1335 | It isn't legal to repeat a class in a list of base classes. |
| 1336 | |
| 1337 | The next three properties are the 3 constraints in "C3". |
| 1338 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1339 | Local precendece order. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1340 | If A precedes B in C's MRO, then A will precede B in the MRO of all |
| 1341 | subclasses of C. |
| 1342 | |
| 1343 | Monotonicity. |
| 1344 | The MRO of a class must be an extension without reordering of the |
| 1345 | MRO of each of its superclasses. |
| 1346 | |
| 1347 | Extended Precedence Graph (EPG). |
| 1348 | Linearization is consistent if there is a path in the EPG from |
| 1349 | each class to all its successors in the linearization. See |
| 1350 | the paper for definition of EPG. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1351 | */ |
| 1352 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1353 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1354 | tail_contains(PyObject *list, int whence, PyObject *o) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1355 | Py_ssize_t j, size; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1356 | size = PyList_GET_SIZE(list); |
| 1357 | |
| 1358 | for (j = whence+1; j < size; j++) { |
| 1359 | if (PyList_GET_ITEM(list, j) == o) |
| 1360 | return 1; |
| 1361 | } |
| 1362 | return 0; |
| 1363 | } |
| 1364 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1365 | static PyObject * |
| 1366 | class_name(PyObject *cls) |
| 1367 | { |
| 1368 | PyObject *name = PyObject_GetAttrString(cls, "__name__"); |
| 1369 | if (name == NULL) { |
| 1370 | PyErr_Clear(); |
| 1371 | Py_XDECREF(name); |
| 1372 | name = PyObject_Repr(cls); |
| 1373 | } |
| 1374 | if (name == NULL) |
| 1375 | return NULL; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1376 | if (!PyString_Check(name)) { |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1377 | Py_DECREF(name); |
| 1378 | return NULL; |
| 1379 | } |
| 1380 | return name; |
| 1381 | } |
| 1382 | |
| 1383 | static int |
| 1384 | check_duplicates(PyObject *list) |
| 1385 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1386 | Py_ssize_t i, j, n; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1387 | /* Let's use a quadratic time algorithm, |
| 1388 | assuming that the bases lists is short. |
| 1389 | */ |
| 1390 | n = PyList_GET_SIZE(list); |
| 1391 | for (i = 0; i < n; i++) { |
| 1392 | PyObject *o = PyList_GET_ITEM(list, i); |
| 1393 | for (j = i + 1; j < n; j++) { |
| 1394 | if (PyList_GET_ITEM(list, j) == o) { |
| 1395 | o = class_name(o); |
| 1396 | PyErr_Format(PyExc_TypeError, |
| 1397 | "duplicate base class %s", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1398 | o ? PyString_AS_STRING(o) : "?"); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1399 | Py_XDECREF(o); |
| 1400 | return -1; |
| 1401 | } |
| 1402 | } |
| 1403 | } |
| 1404 | return 0; |
| 1405 | } |
| 1406 | |
| 1407 | /* Raise a TypeError for an MRO order disagreement. |
| 1408 | |
| 1409 | It's hard to produce a good error message. In the absence of better |
| 1410 | insight into error reporting, report the classes that were candidates |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1411 | 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] | 1412 | order in which they should be put in the MRO, but it's hard to |
| 1413 | diagnose what constraint can't be satisfied. |
| 1414 | */ |
| 1415 | |
| 1416 | static void |
| 1417 | set_mro_error(PyObject *to_merge, int *remain) |
| 1418 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1419 | Py_ssize_t i, n, off, to_merge_size; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1420 | char buf[1000]; |
| 1421 | PyObject *k, *v; |
| 1422 | PyObject *set = PyDict_New(); |
Georg Brandl | 5c170fd | 2006-03-17 19:03:25 +0000 | [diff] [blame] | 1423 | if (!set) return; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1424 | |
| 1425 | to_merge_size = PyList_GET_SIZE(to_merge); |
| 1426 | for (i = 0; i < to_merge_size; i++) { |
| 1427 | PyObject *L = PyList_GET_ITEM(to_merge, i); |
| 1428 | if (remain[i] < PyList_GET_SIZE(L)) { |
| 1429 | PyObject *c = PyList_GET_ITEM(L, remain[i]); |
Georg Brandl | 5c170fd | 2006-03-17 19:03:25 +0000 | [diff] [blame] | 1430 | if (PyDict_SetItem(set, c, Py_None) < 0) { |
| 1431 | Py_DECREF(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1432 | return; |
Georg Brandl | 5c170fd | 2006-03-17 19:03:25 +0000 | [diff] [blame] | 1433 | } |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1434 | } |
| 1435 | } |
| 1436 | n = PyDict_Size(set); |
| 1437 | |
Raymond Hettinger | f394df4 | 2003-04-06 19:13:41 +0000 | [diff] [blame] | 1438 | off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \ |
| 1439 | consistent method resolution\norder (MRO) for bases"); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1440 | i = 0; |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 1441 | 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] | 1442 | PyObject *name = class_name(k); |
| 1443 | off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1444 | name ? PyString_AS_STRING(name) : "?"); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1445 | Py_XDECREF(name); |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 1446 | if (--n && (size_t)(off+1) < sizeof(buf)) { |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1447 | buf[off++] = ','; |
| 1448 | buf[off] = '\0'; |
| 1449 | } |
| 1450 | } |
| 1451 | PyErr_SetString(PyExc_TypeError, buf); |
| 1452 | Py_DECREF(set); |
| 1453 | } |
| 1454 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1455 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1456 | pmerge(PyObject *acc, PyObject* to_merge) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1457 | Py_ssize_t i, j, to_merge_size, empty_cnt; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1458 | int *remain; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1459 | int ok; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1460 | |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1461 | to_merge_size = PyList_GET_SIZE(to_merge); |
| 1462 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1463 | /* remain stores an index into each sublist of to_merge. |
| 1464 | remain[i] is the index of the next base in to_merge[i] |
| 1465 | that is not included in acc. |
| 1466 | */ |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 1467 | remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1468 | if (remain == NULL) |
| 1469 | return -1; |
| 1470 | for (i = 0; i < to_merge_size; i++) |
| 1471 | remain[i] = 0; |
| 1472 | |
| 1473 | again: |
| 1474 | empty_cnt = 0; |
| 1475 | for (i = 0; i < to_merge_size; i++) { |
| 1476 | PyObject *candidate; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1477 | |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1478 | PyObject *cur_list = PyList_GET_ITEM(to_merge, i); |
| 1479 | |
| 1480 | if (remain[i] >= PyList_GET_SIZE(cur_list)) { |
| 1481 | empty_cnt++; |
| 1482 | continue; |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1483 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1484 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1485 | /* Choose next candidate for MRO. |
| 1486 | |
| 1487 | The input sequences alone can determine the choice. |
| 1488 | If not, choose the class which appears in the MRO |
| 1489 | of the earliest direct superclass of the new class. |
| 1490 | */ |
| 1491 | |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1492 | candidate = PyList_GET_ITEM(cur_list, remain[i]); |
| 1493 | for (j = 0; j < to_merge_size; j++) { |
| 1494 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1495 | if (tail_contains(j_lst, remain[j], candidate)) { |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1496 | goto skip; /* continue outer loop */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1497 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1498 | } |
| 1499 | ok = PyList_Append(acc, candidate); |
| 1500 | if (ok < 0) { |
| 1501 | PyMem_Free(remain); |
| 1502 | return -1; |
| 1503 | } |
| 1504 | for (j = 0; j < to_merge_size; j++) { |
| 1505 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
Guido van Rossum | 768158c | 2002-12-31 16:33:01 +0000 | [diff] [blame] | 1506 | if (remain[j] < PyList_GET_SIZE(j_lst) && |
| 1507 | PyList_GET_ITEM(j_lst, remain[j]) == candidate) { |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1508 | remain[j]++; |
| 1509 | } |
| 1510 | } |
| 1511 | goto again; |
Tim Peters | 9a6b8d8 | 2002-11-14 23:22:33 +0000 | [diff] [blame] | 1512 | skip: ; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1515 | if (empty_cnt == to_merge_size) { |
| 1516 | PyMem_FREE(remain); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1517 | return 0; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1518 | } |
| 1519 | set_mro_error(to_merge, remain); |
| 1520 | PyMem_FREE(remain); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1521 | return -1; |
| 1522 | } |
| 1523 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1524 | static PyObject * |
| 1525 | mro_implementation(PyTypeObject *type) |
| 1526 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1527 | Py_ssize_t i, n; |
| 1528 | int ok; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1529 | PyObject *bases, *result; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1530 | PyObject *to_merge, *bases_aslist; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1531 | |
Neal Norwitz | e7bb918 | 2008-01-27 17:10:14 +0000 | [diff] [blame] | 1532 | if (type->tp_dict == NULL) { |
| 1533 | if (PyType_Ready(type) < 0) |
Guido van Rossum | 6351757 | 2002-06-18 16:44:57 +0000 | [diff] [blame] | 1534 | return NULL; |
| 1535 | } |
| 1536 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1537 | /* Find a superclass linearization that honors the constraints |
| 1538 | of the explicit lists of bases and the constraints implied by |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1539 | each base class. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1540 | |
| 1541 | to_merge is a list of lists, where each list is a superclass |
| 1542 | linearization implied by a base class. The last element of |
| 1543 | to_merge is the declared list of bases. |
| 1544 | */ |
| 1545 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1546 | bases = type->tp_bases; |
| 1547 | n = PyTuple_GET_SIZE(bases); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1548 | |
| 1549 | to_merge = PyList_New(n+1); |
| 1550 | if (to_merge == NULL) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1551 | return NULL; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1552 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1553 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1554 | PyObject *base = PyTuple_GET_ITEM(bases, i); |
| 1555 | PyObject *parentMRO; |
| 1556 | if (PyType_Check(base)) |
| 1557 | parentMRO = PySequence_List( |
| 1558 | ((PyTypeObject*)base)->tp_mro); |
| 1559 | else |
| 1560 | parentMRO = classic_mro(base); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1561 | if (parentMRO == NULL) { |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1562 | Py_DECREF(to_merge); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1563 | return NULL; |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1564 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1565 | |
| 1566 | PyList_SET_ITEM(to_merge, i, parentMRO); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1567 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1568 | |
| 1569 | bases_aslist = PySequence_List(bases); |
| 1570 | if (bases_aslist == NULL) { |
| 1571 | Py_DECREF(to_merge); |
| 1572 | return NULL; |
| 1573 | } |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1574 | /* This is just a basic sanity check. */ |
| 1575 | if (check_duplicates(bases_aslist) < 0) { |
| 1576 | Py_DECREF(to_merge); |
| 1577 | Py_DECREF(bases_aslist); |
| 1578 | return NULL; |
| 1579 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1580 | PyList_SET_ITEM(to_merge, n, bases_aslist); |
| 1581 | |
| 1582 | result = Py_BuildValue("[O]", (PyObject *)type); |
| 1583 | if (result == NULL) { |
| 1584 | Py_DECREF(to_merge); |
| 1585 | return NULL; |
| 1586 | } |
| 1587 | |
| 1588 | ok = pmerge(result, to_merge); |
| 1589 | Py_DECREF(to_merge); |
| 1590 | if (ok < 0) { |
| 1591 | Py_DECREF(result); |
| 1592 | return NULL; |
| 1593 | } |
| 1594 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1595 | return result; |
| 1596 | } |
| 1597 | |
| 1598 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1599 | mro_external(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1600 | { |
| 1601 | PyTypeObject *type = (PyTypeObject *)self; |
| 1602 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1603 | return mro_implementation(type); |
| 1604 | } |
| 1605 | |
| 1606 | static int |
| 1607 | mro_internal(PyTypeObject *type) |
| 1608 | { |
| 1609 | PyObject *mro, *result, *tuple; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1610 | int checkit = 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1611 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1612 | if (Py_TYPE(type) == &PyType_Type) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1613 | result = mro_implementation(type); |
| 1614 | } |
| 1615 | else { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1616 | static PyObject *mro_str; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1617 | checkit = 1; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1618 | mro = lookup_method((PyObject *)type, "mro", &mro_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1619 | if (mro == NULL) |
| 1620 | return -1; |
| 1621 | result = PyObject_CallObject(mro, NULL); |
| 1622 | Py_DECREF(mro); |
| 1623 | } |
| 1624 | if (result == NULL) |
| 1625 | return -1; |
| 1626 | tuple = PySequence_Tuple(result); |
| 1627 | Py_DECREF(result); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1628 | if (tuple == NULL) |
| 1629 | return -1; |
| 1630 | if (checkit) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1631 | Py_ssize_t i, len; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1632 | PyObject *cls; |
| 1633 | PyTypeObject *solid; |
| 1634 | |
| 1635 | solid = solid_base(type); |
| 1636 | |
| 1637 | len = PyTuple_GET_SIZE(tuple); |
| 1638 | |
| 1639 | for (i = 0; i < len; i++) { |
| 1640 | PyTypeObject *t; |
| 1641 | cls = PyTuple_GET_ITEM(tuple, i); |
| 1642 | if (PyClass_Check(cls)) |
| 1643 | continue; |
| 1644 | else if (!PyType_Check(cls)) { |
| 1645 | PyErr_Format(PyExc_TypeError, |
| 1646 | "mro() returned a non-class ('%.500s')", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1647 | Py_TYPE(cls)->tp_name); |
Neal Norwitz | 50bf51a | 2006-01-02 02:46:54 +0000 | [diff] [blame] | 1648 | Py_DECREF(tuple); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1649 | return -1; |
| 1650 | } |
| 1651 | t = (PyTypeObject*)cls; |
| 1652 | if (!PyType_IsSubtype(solid, solid_base(t))) { |
| 1653 | PyErr_Format(PyExc_TypeError, |
| 1654 | "mro() returned base with unsuitable layout ('%.500s')", |
| 1655 | t->tp_name); |
Neal Norwitz | 50bf51a | 2006-01-02 02:46:54 +0000 | [diff] [blame] | 1656 | Py_DECREF(tuple); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1657 | return -1; |
| 1658 | } |
| 1659 | } |
| 1660 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1661 | type->tp_mro = tuple; |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 1662 | |
| 1663 | type_mro_modified(type, type->tp_mro); |
| 1664 | /* corner case: the old-style super class might have been hidden |
| 1665 | from the custom MRO */ |
| 1666 | type_mro_modified(type, type->tp_bases); |
| 1667 | |
Georg Brandl | 74a1dea | 2008-05-28 11:21:39 +0000 | [diff] [blame] | 1668 | PyType_Modified(type); |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 1669 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1670 | return 0; |
| 1671 | } |
| 1672 | |
| 1673 | |
| 1674 | /* Calculate the best base amongst multiple base classes. |
Armin Rigo | c0ba52d | 2007-04-19 14:44:48 +0000 | [diff] [blame] | 1675 | This is the first one that's on the path to the "solid base". */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1676 | |
| 1677 | static PyTypeObject * |
| 1678 | best_base(PyObject *bases) |
| 1679 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1680 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1681 | PyTypeObject *base, *winner, *candidate, *base_i; |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1682 | PyObject *base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1683 | |
| 1684 | assert(PyTuple_Check(bases)); |
| 1685 | n = PyTuple_GET_SIZE(bases); |
| 1686 | assert(n > 0); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1687 | base = NULL; |
| 1688 | winner = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1689 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1690 | base_proto = PyTuple_GET_ITEM(bases, i); |
| 1691 | if (PyClass_Check(base_proto)) |
| 1692 | continue; |
Armin Rigo | c0ba52d | 2007-04-19 14:44:48 +0000 | [diff] [blame] | 1693 | if (!PyType_Check(base_proto)) { |
| 1694 | PyErr_SetString( |
| 1695 | PyExc_TypeError, |
| 1696 | "bases must be types"); |
| 1697 | return NULL; |
| 1698 | } |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1699 | base_i = (PyTypeObject *)base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1700 | if (base_i->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1701 | if (PyType_Ready(base_i) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1702 | return NULL; |
| 1703 | } |
| 1704 | candidate = solid_base(base_i); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1705 | if (winner == NULL) { |
| 1706 | winner = candidate; |
| 1707 | base = base_i; |
| 1708 | } |
| 1709 | else if (PyType_IsSubtype(winner, candidate)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1710 | ; |
| 1711 | else if (PyType_IsSubtype(candidate, winner)) { |
| 1712 | winner = candidate; |
| 1713 | base = base_i; |
| 1714 | } |
| 1715 | else { |
| 1716 | PyErr_SetString( |
| 1717 | PyExc_TypeError, |
| 1718 | "multiple bases have " |
| 1719 | "instance lay-out conflict"); |
| 1720 | return NULL; |
| 1721 | } |
| 1722 | } |
Guido van Rossum | e54616c | 2001-12-14 04:19:56 +0000 | [diff] [blame] | 1723 | if (base == NULL) |
| 1724 | PyErr_SetString(PyExc_TypeError, |
| 1725 | "a new-style class can't have only classic bases"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1726 | return base; |
| 1727 | } |
| 1728 | |
| 1729 | static int |
| 1730 | extra_ivars(PyTypeObject *type, PyTypeObject *base) |
| 1731 | { |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 1732 | size_t t_size = type->tp_basicsize; |
| 1733 | size_t b_size = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1734 | |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1735 | assert(t_size >= b_size); /* Else type smaller than base! */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1736 | if (type->tp_itemsize || base->tp_itemsize) { |
| 1737 | /* If itemsize is involved, stricter rules */ |
| 1738 | return t_size != b_size || |
| 1739 | type->tp_itemsize != base->tp_itemsize; |
| 1740 | } |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1741 | if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 && |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1742 | type->tp_weaklistoffset + sizeof(PyObject *) == t_size && |
| 1743 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1744 | t_size -= sizeof(PyObject *); |
| 1745 | if (type->tp_dictoffset && base->tp_dictoffset == 0 && |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1746 | type->tp_dictoffset + sizeof(PyObject *) == t_size && |
| 1747 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1748 | t_size -= sizeof(PyObject *); |
| 1749 | |
| 1750 | return t_size != b_size; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | static PyTypeObject * |
| 1754 | solid_base(PyTypeObject *type) |
| 1755 | { |
| 1756 | PyTypeObject *base; |
| 1757 | |
| 1758 | if (type->tp_base) |
| 1759 | base = solid_base(type->tp_base); |
| 1760 | else |
| 1761 | base = &PyBaseObject_Type; |
| 1762 | if (extra_ivars(type, base)) |
| 1763 | return type; |
| 1764 | else |
| 1765 | return base; |
| 1766 | } |
| 1767 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1768 | static void object_dealloc(PyObject *); |
| 1769 | static int object_init(PyObject *, PyObject *, PyObject *); |
| 1770 | static int update_slot(PyTypeObject *, PyObject *); |
| 1771 | static void fixup_slot_dispatchers(PyTypeObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1772 | |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1773 | /* |
| 1774 | * Helpers for __dict__ descriptor. We don't want to expose the dicts |
| 1775 | * inherited from various builtin types. The builtin base usually provides |
| 1776 | * its own __dict__ descriptor, so we use that when we can. |
| 1777 | */ |
| 1778 | static PyTypeObject * |
| 1779 | get_builtin_base_with_dict(PyTypeObject *type) |
| 1780 | { |
| 1781 | while (type->tp_base != NULL) { |
| 1782 | if (type->tp_dictoffset != 0 && |
| 1783 | !(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 1784 | return type; |
| 1785 | type = type->tp_base; |
| 1786 | } |
| 1787 | return NULL; |
| 1788 | } |
| 1789 | |
| 1790 | static PyObject * |
| 1791 | get_dict_descriptor(PyTypeObject *type) |
| 1792 | { |
| 1793 | static PyObject *dict_str; |
| 1794 | PyObject *descr; |
| 1795 | |
| 1796 | if (dict_str == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1797 | dict_str = PyString_InternFromString("__dict__"); |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1798 | if (dict_str == NULL) |
| 1799 | return NULL; |
| 1800 | } |
| 1801 | descr = _PyType_Lookup(type, dict_str); |
| 1802 | if (descr == NULL || !PyDescr_IsData(descr)) |
| 1803 | return NULL; |
| 1804 | |
| 1805 | return descr; |
| 1806 | } |
| 1807 | |
| 1808 | static void |
| 1809 | raise_dict_descr_error(PyObject *obj) |
| 1810 | { |
| 1811 | PyErr_Format(PyExc_TypeError, |
| 1812 | "this __dict__ descriptor does not support " |
| 1813 | "'%.200s' objects", obj->ob_type->tp_name); |
| 1814 | } |
| 1815 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1816 | static PyObject * |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1817 | subtype_dict(PyObject *obj, void *context) |
| 1818 | { |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1819 | PyObject **dictptr; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1820 | PyObject *dict; |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1821 | PyTypeObject *base; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1822 | |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1823 | base = get_builtin_base_with_dict(obj->ob_type); |
| 1824 | if (base != NULL) { |
| 1825 | descrgetfunc func; |
| 1826 | PyObject *descr = get_dict_descriptor(base); |
| 1827 | if (descr == NULL) { |
| 1828 | raise_dict_descr_error(obj); |
| 1829 | return NULL; |
| 1830 | } |
| 1831 | func = descr->ob_type->tp_descr_get; |
| 1832 | if (func == NULL) { |
| 1833 | raise_dict_descr_error(obj); |
| 1834 | return NULL; |
| 1835 | } |
| 1836 | return func(descr, obj, (PyObject *)(obj->ob_type)); |
| 1837 | } |
| 1838 | |
| 1839 | dictptr = _PyObject_GetDictPtr(obj); |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1840 | if (dictptr == NULL) { |
| 1841 | PyErr_SetString(PyExc_AttributeError, |
| 1842 | "This object has no __dict__"); |
| 1843 | return NULL; |
| 1844 | } |
| 1845 | dict = *dictptr; |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 1846 | if (dict == NULL) |
| 1847 | *dictptr = dict = PyDict_New(); |
| 1848 | Py_XINCREF(dict); |
| 1849 | return dict; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1852 | static int |
| 1853 | subtype_setdict(PyObject *obj, PyObject *value, void *context) |
| 1854 | { |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1855 | PyObject **dictptr; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1856 | PyObject *dict; |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1857 | PyTypeObject *base; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1858 | |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1859 | base = get_builtin_base_with_dict(obj->ob_type); |
| 1860 | if (base != NULL) { |
| 1861 | descrsetfunc func; |
| 1862 | PyObject *descr = get_dict_descriptor(base); |
| 1863 | if (descr == NULL) { |
| 1864 | raise_dict_descr_error(obj); |
| 1865 | return -1; |
| 1866 | } |
| 1867 | func = descr->ob_type->tp_descr_set; |
| 1868 | if (func == NULL) { |
| 1869 | raise_dict_descr_error(obj); |
| 1870 | return -1; |
| 1871 | } |
| 1872 | return func(descr, obj, value); |
| 1873 | } |
| 1874 | |
| 1875 | dictptr = _PyObject_GetDictPtr(obj); |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1876 | if (dictptr == NULL) { |
| 1877 | PyErr_SetString(PyExc_AttributeError, |
| 1878 | "This object has no __dict__"); |
| 1879 | return -1; |
| 1880 | } |
Guido van Rossum | d331cb5 | 2001-12-05 19:46:42 +0000 | [diff] [blame] | 1881 | if (value != NULL && !PyDict_Check(value)) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 1882 | PyErr_Format(PyExc_TypeError, |
| 1883 | "__dict__ must be set to a dictionary, " |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1884 | "not a '%.200s'", Py_TYPE(value)->tp_name); |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1885 | return -1; |
| 1886 | } |
| 1887 | dict = *dictptr; |
Guido van Rossum | d331cb5 | 2001-12-05 19:46:42 +0000 | [diff] [blame] | 1888 | Py_XINCREF(value); |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1889 | *dictptr = value; |
| 1890 | Py_XDECREF(dict); |
| 1891 | return 0; |
| 1892 | } |
| 1893 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1894 | static PyObject * |
| 1895 | subtype_getweakref(PyObject *obj, void *context) |
| 1896 | { |
| 1897 | PyObject **weaklistptr; |
| 1898 | PyObject *result; |
| 1899 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1900 | if (Py_TYPE(obj)->tp_weaklistoffset == 0) { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1901 | PyErr_SetString(PyExc_AttributeError, |
Fred Drake | 7a36f5f | 2006-08-04 05:17:21 +0000 | [diff] [blame] | 1902 | "This object has no __weakref__"); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1903 | return NULL; |
| 1904 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1905 | assert(Py_TYPE(obj)->tp_weaklistoffset > 0); |
| 1906 | assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <= |
| 1907 | (size_t)(Py_TYPE(obj)->tp_basicsize)); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1908 | weaklistptr = (PyObject **) |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1909 | ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1910 | if (*weaklistptr == NULL) |
| 1911 | result = Py_None; |
| 1912 | else |
| 1913 | result = *weaklistptr; |
| 1914 | Py_INCREF(result); |
| 1915 | return result; |
| 1916 | } |
| 1917 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1918 | /* Three variants on the subtype_getsets list. */ |
| 1919 | |
| 1920 | static PyGetSetDef subtype_getsets_full[] = { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1921 | {"__dict__", subtype_dict, subtype_setdict, |
Neal Norwitz | 858e34f | 2002-08-13 17:18:45 +0000 | [diff] [blame] | 1922 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1923 | {"__weakref__", subtype_getweakref, NULL, |
Neal Norwitz | 858e34f | 2002-08-13 17:18:45 +0000 | [diff] [blame] | 1924 | PyDoc_STR("list of weak references to the object (if defined)")}, |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1925 | {0} |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1926 | }; |
| 1927 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1928 | static PyGetSetDef subtype_getsets_dict_only[] = { |
| 1929 | {"__dict__", subtype_dict, subtype_setdict, |
| 1930 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 1931 | {0} |
| 1932 | }; |
| 1933 | |
| 1934 | static PyGetSetDef subtype_getsets_weakref_only[] = { |
| 1935 | {"__weakref__", subtype_getweakref, NULL, |
| 1936 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 1937 | {0} |
| 1938 | }; |
| 1939 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1940 | static int |
| 1941 | valid_identifier(PyObject *s) |
| 1942 | { |
Guido van Rossum | 03013a0 | 2002-07-16 14:30:28 +0000 | [diff] [blame] | 1943 | unsigned char *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1944 | Py_ssize_t i, n; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1945 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1946 | if (!PyString_Check(s)) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 1947 | PyErr_Format(PyExc_TypeError, |
| 1948 | "__slots__ items must be strings, not '%.200s'", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1949 | Py_TYPE(s)->tp_name); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1950 | return 0; |
| 1951 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1952 | p = (unsigned char *) PyString_AS_STRING(s); |
| 1953 | n = PyString_GET_SIZE(s); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1954 | /* We must reject an empty name. As a hack, we bump the |
| 1955 | length to 1 so that the loop will balk on the trailing \0. */ |
| 1956 | if (n == 0) |
| 1957 | n = 1; |
| 1958 | for (i = 0; i < n; i++, p++) { |
| 1959 | if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') { |
| 1960 | PyErr_SetString(PyExc_TypeError, |
| 1961 | "__slots__ must be identifiers"); |
| 1962 | return 0; |
| 1963 | } |
| 1964 | } |
| 1965 | return 1; |
| 1966 | } |
| 1967 | |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1968 | #ifdef Py_USING_UNICODE |
| 1969 | /* Replace Unicode objects in slots. */ |
| 1970 | |
| 1971 | static PyObject * |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 1972 | _unicode_to_string(PyObject *slots, Py_ssize_t nslots) |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1973 | { |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1974 | PyObject *tmp = NULL; |
| 1975 | PyObject *slot_name, *new_name; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 1976 | Py_ssize_t i; |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1977 | |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1978 | for (i = 0; i < nslots; i++) { |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1979 | if (PyUnicode_Check(slot_name = PyTuple_GET_ITEM(slots, i))) { |
| 1980 | if (tmp == NULL) { |
| 1981 | tmp = PySequence_List(slots); |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1982 | if (tmp == NULL) |
| 1983 | return NULL; |
| 1984 | } |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1985 | new_name = _PyUnicode_AsDefaultEncodedString(slot_name, |
| 1986 | NULL); |
| 1987 | if (new_name == NULL) { |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1988 | Py_DECREF(tmp); |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1989 | return NULL; |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1990 | } |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1991 | Py_INCREF(new_name); |
| 1992 | PyList_SET_ITEM(tmp, i, new_name); |
| 1993 | Py_DECREF(slot_name); |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1994 | } |
| 1995 | } |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1996 | if (tmp != NULL) { |
| 1997 | slots = PyList_AsTuple(tmp); |
| 1998 | Py_DECREF(tmp); |
| 1999 | } |
| 2000 | return slots; |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 2001 | } |
| 2002 | #endif |
| 2003 | |
Guido van Rossum | f102e24 | 2007-03-23 18:53:03 +0000 | [diff] [blame] | 2004 | /* Forward */ |
| 2005 | static int |
| 2006 | object_init(PyObject *self, PyObject *args, PyObject *kwds); |
| 2007 | |
| 2008 | static int |
| 2009 | type_init(PyObject *cls, PyObject *args, PyObject *kwds) |
| 2010 | { |
| 2011 | int res; |
| 2012 | |
| 2013 | assert(args != NULL && PyTuple_Check(args)); |
| 2014 | assert(kwds == NULL || PyDict_Check(kwds)); |
| 2015 | |
| 2016 | if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) { |
| 2017 | PyErr_SetString(PyExc_TypeError, |
| 2018 | "type.__init__() takes no keyword arguments"); |
| 2019 | return -1; |
| 2020 | } |
| 2021 | |
| 2022 | if (args != NULL && PyTuple_Check(args) && |
| 2023 | (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) { |
| 2024 | PyErr_SetString(PyExc_TypeError, |
| 2025 | "type.__init__() takes 1 or 3 arguments"); |
| 2026 | return -1; |
| 2027 | } |
| 2028 | |
| 2029 | /* Call object.__init__(self) now. */ |
| 2030 | /* XXX Could call super(type, cls).__init__() but what's the point? */ |
| 2031 | args = PyTuple_GetSlice(args, 0, 0); |
| 2032 | res = object_init(cls, args, NULL); |
| 2033 | Py_DECREF(args); |
| 2034 | return res; |
| 2035 | } |
| 2036 | |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 2037 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2038 | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) |
| 2039 | { |
| 2040 | PyObject *name, *bases, *dict; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 2041 | static char *kwlist[] = {"name", "bases", "dict", 0}; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2042 | PyObject *slots, *tmp, *newslots; |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 2043 | PyTypeObject *type, *base, *tmptype, *winner; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2044 | PyHeapTypeObject *et; |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 2045 | PyMemberDef *mp; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2046 | Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2047 | int j, may_add_dict, may_add_weak; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2048 | |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2049 | assert(args != NULL && PyTuple_Check(args)); |
| 2050 | assert(kwds == NULL || PyDict_Check(kwds)); |
| 2051 | |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 2052 | /* Special case: type(x) should return x->ob_type */ |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2053 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2054 | const Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 2055 | const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2056 | |
| 2057 | if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) { |
| 2058 | PyObject *x = PyTuple_GET_ITEM(args, 0); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2059 | Py_INCREF(Py_TYPE(x)); |
| 2060 | return (PyObject *) Py_TYPE(x); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | /* SF bug 475327 -- if that didn't trigger, we need 3 |
| 2064 | arguments. but PyArg_ParseTupleAndKeywords below may give |
| 2065 | a msg saying type() needs exactly 3. */ |
| 2066 | if (nargs + nkwds != 3) { |
| 2067 | PyErr_SetString(PyExc_TypeError, |
| 2068 | "type() takes 1 or 3 arguments"); |
| 2069 | return NULL; |
| 2070 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 2073 | /* Check arguments: (name, bases, dict) */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2074 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist, |
| 2075 | &name, |
| 2076 | &PyTuple_Type, &bases, |
| 2077 | &PyDict_Type, &dict)) |
| 2078 | return NULL; |
| 2079 | |
Armin Rigo | c0ba52d | 2007-04-19 14:44:48 +0000 | [diff] [blame] | 2080 | /* Determine the proper metatype to deal with this, |
| 2081 | and check for metatype conflicts while we're at it. |
| 2082 | Note that if some other metatype wins to contract, |
| 2083 | it's possible that its instances are not types. */ |
| 2084 | nbases = PyTuple_GET_SIZE(bases); |
| 2085 | winner = metatype; |
| 2086 | for (i = 0; i < nbases; i++) { |
| 2087 | tmp = PyTuple_GET_ITEM(bases, i); |
| 2088 | tmptype = tmp->ob_type; |
| 2089 | if (tmptype == &PyClass_Type) |
| 2090 | continue; /* Special case classic classes */ |
| 2091 | if (PyType_IsSubtype(winner, tmptype)) |
| 2092 | continue; |
| 2093 | if (PyType_IsSubtype(tmptype, winner)) { |
| 2094 | winner = tmptype; |
| 2095 | continue; |
Jeremy Hylton | fa95569 | 2007-02-27 18:29:45 +0000 | [diff] [blame] | 2096 | } |
Armin Rigo | c0ba52d | 2007-04-19 14:44:48 +0000 | [diff] [blame] | 2097 | PyErr_SetString(PyExc_TypeError, |
| 2098 | "metaclass conflict: " |
| 2099 | "the metaclass of a derived class " |
| 2100 | "must be a (non-strict) subclass " |
| 2101 | "of the metaclasses of all its bases"); |
| 2102 | return NULL; |
| 2103 | } |
| 2104 | if (winner != metatype) { |
| 2105 | if (winner->tp_new != type_new) /* Pass it to the winner */ |
| 2106 | return winner->tp_new(winner, args, kwds); |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 2107 | metatype = winner; |
| 2108 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2109 | |
| 2110 | /* Adjust for empty tuple bases */ |
| 2111 | if (nbases == 0) { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2112 | bases = PyTuple_Pack(1, &PyBaseObject_Type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2113 | if (bases == NULL) |
| 2114 | return NULL; |
| 2115 | nbases = 1; |
| 2116 | } |
| 2117 | else |
| 2118 | Py_INCREF(bases); |
| 2119 | |
| 2120 | /* XXX From here until type is allocated, "return NULL" leaks bases! */ |
| 2121 | |
| 2122 | /* Calculate best base, and check that all bases are type objects */ |
| 2123 | base = best_base(bases); |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2124 | if (base == NULL) { |
| 2125 | Py_DECREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2126 | return NULL; |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2127 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2128 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 2129 | PyErr_Format(PyExc_TypeError, |
| 2130 | "type '%.100s' is not an acceptable base type", |
| 2131 | base->tp_name); |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2132 | Py_DECREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2133 | return NULL; |
| 2134 | } |
| 2135 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2136 | /* Check for a __slots__ sequence variable in dict, and count it */ |
| 2137 | slots = PyDict_GetItemString(dict, "__slots__"); |
| 2138 | nslots = 0; |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 2139 | add_dict = 0; |
| 2140 | add_weak = 0; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2141 | may_add_dict = base->tp_dictoffset == 0; |
| 2142 | may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0; |
| 2143 | if (slots == NULL) { |
| 2144 | if (may_add_dict) { |
| 2145 | add_dict++; |
| 2146 | } |
| 2147 | if (may_add_weak) { |
| 2148 | add_weak++; |
| 2149 | } |
| 2150 | } |
| 2151 | else { |
| 2152 | /* Have slots */ |
| 2153 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2154 | /* Make it into a tuple */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2155 | if (PyString_Check(slots) || PyUnicode_Check(slots)) |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2156 | slots = PyTuple_Pack(1, slots); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2157 | else |
| 2158 | slots = PySequence_Tuple(slots); |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2159 | if (slots == NULL) { |
| 2160 | Py_DECREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2161 | return NULL; |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2162 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2163 | assert(PyTuple_Check(slots)); |
| 2164 | |
| 2165 | /* Are slots allowed? */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2166 | nslots = PyTuple_GET_SIZE(slots); |
Jeremy Hylton | 1c7a0ea | 2003-07-16 16:08:23 +0000 | [diff] [blame] | 2167 | if (nslots > 0 && base->tp_itemsize != 0) { |
Guido van Rossum | c414187 | 2001-08-30 04:43:35 +0000 | [diff] [blame] | 2168 | PyErr_Format(PyExc_TypeError, |
| 2169 | "nonempty __slots__ " |
| 2170 | "not supported for subtype of '%s'", |
| 2171 | base->tp_name); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2172 | bad_slots: |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2173 | Py_DECREF(bases); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2174 | Py_DECREF(slots); |
Guido van Rossum | c414187 | 2001-08-30 04:43:35 +0000 | [diff] [blame] | 2175 | return NULL; |
| 2176 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2177 | |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 2178 | #ifdef Py_USING_UNICODE |
| 2179 | tmp = _unicode_to_string(slots, nslots); |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 2180 | if (tmp == NULL) |
| 2181 | goto bad_slots; |
Martin v. Löwis | 13b1a5c | 2002-10-14 21:11:34 +0000 | [diff] [blame] | 2182 | if (tmp != slots) { |
| 2183 | Py_DECREF(slots); |
| 2184 | slots = tmp; |
| 2185 | } |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 2186 | #endif |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2187 | /* Check for valid slot names and two special cases */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2188 | for (i = 0; i < nslots; i++) { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2189 | PyObject *tmp = PyTuple_GET_ITEM(slots, i); |
| 2190 | char *s; |
| 2191 | if (!valid_identifier(tmp)) |
| 2192 | goto bad_slots; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2193 | assert(PyString_Check(tmp)); |
| 2194 | s = PyString_AS_STRING(tmp); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2195 | if (strcmp(s, "__dict__") == 0) { |
| 2196 | if (!may_add_dict || add_dict) { |
| 2197 | PyErr_SetString(PyExc_TypeError, |
| 2198 | "__dict__ slot disallowed: " |
| 2199 | "we already got one"); |
| 2200 | goto bad_slots; |
| 2201 | } |
| 2202 | add_dict++; |
| 2203 | } |
| 2204 | if (strcmp(s, "__weakref__") == 0) { |
| 2205 | if (!may_add_weak || add_weak) { |
| 2206 | PyErr_SetString(PyExc_TypeError, |
| 2207 | "__weakref__ slot disallowed: " |
| 2208 | "either we already got one, " |
| 2209 | "or __itemsize__ != 0"); |
| 2210 | goto bad_slots; |
| 2211 | } |
| 2212 | add_weak++; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2213 | } |
| 2214 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2215 | |
Žiga Seilnacht | 6f2d09c | 2007-03-16 11:59:38 +0000 | [diff] [blame] | 2216 | /* Copy slots into a list, mangle names and sort them. |
| 2217 | Sorted names are needed for __class__ assignment. |
| 2218 | Convert them back to tuple at the end. |
| 2219 | */ |
| 2220 | newslots = PyList_New(nslots - add_dict - add_weak); |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2221 | if (newslots == NULL) |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2222 | goto bad_slots; |
| 2223 | for (i = j = 0; i < nslots; i++) { |
| 2224 | char *s; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2225 | tmp = PyTuple_GET_ITEM(slots, i); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2226 | s = PyString_AS_STRING(tmp); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2227 | if ((add_dict && strcmp(s, "__dict__") == 0) || |
| 2228 | (add_weak && strcmp(s, "__weakref__") == 0)) |
| 2229 | continue; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2230 | tmp =_Py_Mangle(name, tmp); |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2231 | if (!tmp) |
| 2232 | goto bad_slots; |
Žiga Seilnacht | 6f2d09c | 2007-03-16 11:59:38 +0000 | [diff] [blame] | 2233 | PyList_SET_ITEM(newslots, j, tmp); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2234 | j++; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2235 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2236 | assert(j == nslots - add_dict - add_weak); |
| 2237 | nslots = j; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2238 | Py_DECREF(slots); |
Žiga Seilnacht | 6f2d09c | 2007-03-16 11:59:38 +0000 | [diff] [blame] | 2239 | if (PyList_Sort(newslots) == -1) { |
| 2240 | Py_DECREF(bases); |
| 2241 | Py_DECREF(newslots); |
| 2242 | return NULL; |
| 2243 | } |
| 2244 | slots = PyList_AsTuple(newslots); |
| 2245 | Py_DECREF(newslots); |
| 2246 | if (slots == NULL) { |
| 2247 | Py_DECREF(bases); |
| 2248 | return NULL; |
| 2249 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2250 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2251 | /* Secondary bases may provide weakrefs or dict */ |
| 2252 | if (nbases > 1 && |
| 2253 | ((may_add_dict && !add_dict) || |
| 2254 | (may_add_weak && !add_weak))) { |
| 2255 | for (i = 0; i < nbases; i++) { |
| 2256 | tmp = PyTuple_GET_ITEM(bases, i); |
| 2257 | if (tmp == (PyObject *)base) |
| 2258 | continue; /* Skip primary base */ |
| 2259 | if (PyClass_Check(tmp)) { |
| 2260 | /* Classic base class provides both */ |
| 2261 | if (may_add_dict && !add_dict) |
| 2262 | add_dict++; |
| 2263 | if (may_add_weak && !add_weak) |
| 2264 | add_weak++; |
| 2265 | break; |
| 2266 | } |
| 2267 | assert(PyType_Check(tmp)); |
| 2268 | tmptype = (PyTypeObject *)tmp; |
| 2269 | if (may_add_dict && !add_dict && |
| 2270 | tmptype->tp_dictoffset != 0) |
| 2271 | add_dict++; |
| 2272 | if (may_add_weak && !add_weak && |
| 2273 | tmptype->tp_weaklistoffset != 0) |
| 2274 | add_weak++; |
| 2275 | if (may_add_dict && !add_dict) |
| 2276 | continue; |
| 2277 | if (may_add_weak && !add_weak) |
| 2278 | continue; |
| 2279 | /* Nothing more to check */ |
| 2280 | break; |
| 2281 | } |
| 2282 | } |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 2283 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2284 | |
| 2285 | /* XXX From here until type is safely allocated, |
| 2286 | "return NULL" may leak slots! */ |
| 2287 | |
| 2288 | /* Allocate the type object */ |
| 2289 | type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2290 | if (type == NULL) { |
| 2291 | Py_XDECREF(slots); |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2292 | Py_DECREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2293 | return NULL; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2294 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2295 | |
| 2296 | /* Keep name and slots alive in the extended type object */ |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2297 | et = (PyHeapTypeObject *)type; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2298 | Py_INCREF(name); |
Georg Brandl | c255c7b | 2006-02-20 22:27:28 +0000 | [diff] [blame] | 2299 | et->ht_name = name; |
| 2300 | et->ht_slots = slots; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2301 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2302 | /* Initialize tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2303 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | |
| 2304 | Py_TPFLAGS_BASETYPE; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2305 | if (base->tp_flags & Py_TPFLAGS_HAVE_GC) |
| 2306 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2307 | |
| 2308 | /* It's a new-style number unless it specifically inherits any |
| 2309 | old-style numeric behavior */ |
| 2310 | if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) || |
| 2311 | (base->tp_as_number == NULL)) |
| 2312 | type->tp_flags |= Py_TPFLAGS_CHECKTYPES; |
| 2313 | |
| 2314 | /* Initialize essential fields */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2315 | type->tp_as_number = &et->as_number; |
| 2316 | type->tp_as_sequence = &et->as_sequence; |
| 2317 | type->tp_as_mapping = &et->as_mapping; |
| 2318 | type->tp_as_buffer = &et->as_buffer; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2319 | type->tp_name = PyString_AS_STRING(name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2320 | |
| 2321 | /* Set tp_base and tp_bases */ |
| 2322 | type->tp_bases = bases; |
| 2323 | Py_INCREF(base); |
| 2324 | type->tp_base = base; |
| 2325 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2326 | /* Initialize tp_dict from passed-in dict */ |
| 2327 | type->tp_dict = dict = PyDict_Copy(dict); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2328 | if (dict == NULL) { |
| 2329 | Py_DECREF(type); |
| 2330 | return NULL; |
| 2331 | } |
| 2332 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 2333 | /* Set __module__ in the dict */ |
| 2334 | if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 2335 | tmp = PyEval_GetGlobals(); |
| 2336 | if (tmp != NULL) { |
| 2337 | tmp = PyDict_GetItemString(tmp, "__name__"); |
| 2338 | if (tmp != NULL) { |
| 2339 | if (PyDict_SetItemString(dict, "__module__", |
| 2340 | tmp) < 0) |
| 2341 | return NULL; |
| 2342 | } |
| 2343 | } |
| 2344 | } |
| 2345 | |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2346 | /* 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] | 2347 | and is a string. The __doc__ accessor will first look for tp_doc; |
| 2348 | if that fails, it will still look into __dict__. |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2349 | */ |
| 2350 | { |
| 2351 | PyObject *doc = PyDict_GetItemString(dict, "__doc__"); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2352 | if (doc != NULL && PyString_Check(doc)) { |
| 2353 | const size_t n = (size_t)PyString_GET_SIZE(doc); |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2354 | char *tp_doc = (char *)PyObject_MALLOC(n+1); |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 2355 | if (tp_doc == NULL) { |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2356 | Py_DECREF(type); |
| 2357 | return NULL; |
| 2358 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2359 | memcpy(tp_doc, PyString_AS_STRING(doc), n+1); |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2360 | type->tp_doc = tp_doc; |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2361 | } |
| 2362 | } |
| 2363 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2364 | /* Special-case __new__: if it's a plain function, |
| 2365 | make it a static function */ |
| 2366 | tmp = PyDict_GetItemString(dict, "__new__"); |
| 2367 | if (tmp != NULL && PyFunction_Check(tmp)) { |
| 2368 | tmp = PyStaticMethod_New(tmp); |
| 2369 | if (tmp == NULL) { |
| 2370 | Py_DECREF(type); |
| 2371 | return NULL; |
| 2372 | } |
| 2373 | PyDict_SetItemString(dict, "__new__", tmp); |
| 2374 | Py_DECREF(tmp); |
| 2375 | } |
| 2376 | |
| 2377 | /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2378 | mp = PyHeapType_GET_MEMBERS(et); |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 2379 | slotoffset = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2380 | if (slots != NULL) { |
| 2381 | for (i = 0; i < nslots; i++, mp++) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2382 | mp->name = PyString_AS_STRING( |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2383 | PyTuple_GET_ITEM(slots, i)); |
Guido van Rossum | 64b206c | 2001-12-04 17:13:22 +0000 | [diff] [blame] | 2384 | mp->type = T_OBJECT_EX; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2385 | mp->offset = slotoffset; |
Žiga Seilnacht | 8903208 | 2007-03-11 15:54:54 +0000 | [diff] [blame] | 2386 | |
| 2387 | /* __dict__ and __weakref__ are already filtered out */ |
| 2388 | assert(strcmp(mp->name, "__dict__") != 0); |
| 2389 | assert(strcmp(mp->name, "__weakref__") != 0); |
| 2390 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2391 | slotoffset += sizeof(PyObject *); |
| 2392 | } |
| 2393 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2394 | if (add_dict) { |
| 2395 | if (base->tp_itemsize) |
| 2396 | type->tp_dictoffset = -(long)sizeof(PyObject *); |
| 2397 | else |
| 2398 | type->tp_dictoffset = slotoffset; |
| 2399 | slotoffset += sizeof(PyObject *); |
| 2400 | } |
| 2401 | if (add_weak) { |
| 2402 | assert(!base->tp_itemsize); |
| 2403 | type->tp_weaklistoffset = slotoffset; |
| 2404 | slotoffset += sizeof(PyObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2405 | } |
| 2406 | type->tp_basicsize = slotoffset; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 2407 | type->tp_itemsize = base->tp_itemsize; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2408 | type->tp_members = PyHeapType_GET_MEMBERS(et); |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2409 | |
| 2410 | if (type->tp_weaklistoffset && type->tp_dictoffset) |
| 2411 | type->tp_getset = subtype_getsets_full; |
| 2412 | else if (type->tp_weaklistoffset && !type->tp_dictoffset) |
| 2413 | type->tp_getset = subtype_getsets_weakref_only; |
| 2414 | else if (!type->tp_weaklistoffset && type->tp_dictoffset) |
| 2415 | type->tp_getset = subtype_getsets_dict_only; |
| 2416 | else |
| 2417 | type->tp_getset = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2418 | |
| 2419 | /* Special case some slots */ |
| 2420 | if (type->tp_dictoffset != 0 || nslots > 0) { |
| 2421 | if (base->tp_getattr == NULL && base->tp_getattro == NULL) |
| 2422 | type->tp_getattro = PyObject_GenericGetAttr; |
| 2423 | if (base->tp_setattr == NULL && base->tp_setattro == NULL) |
| 2424 | type->tp_setattro = PyObject_GenericSetAttr; |
| 2425 | } |
| 2426 | type->tp_dealloc = subtype_dealloc; |
| 2427 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 2428 | /* Enable GC unless there are really no instance variables possible */ |
| 2429 | if (!(type->tp_basicsize == sizeof(PyObject) && |
| 2430 | type->tp_itemsize == 0)) |
| 2431 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
| 2432 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2433 | /* Always override allocation strategy to use regular heap */ |
| 2434 | type->tp_alloc = PyType_GenericAlloc; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2435 | if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 2436 | type->tp_free = PyObject_GC_Del; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 2437 | type->tp_traverse = subtype_traverse; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 2438 | type->tp_clear = subtype_clear; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2439 | } |
| 2440 | else |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 2441 | type->tp_free = PyObject_Del; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2442 | |
| 2443 | /* Initialize the rest */ |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 2444 | if (PyType_Ready(type) < 0) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2445 | Py_DECREF(type); |
| 2446 | return NULL; |
| 2447 | } |
| 2448 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 2449 | /* Put the proper slots in place */ |
| 2450 | fixup_slot_dispatchers(type); |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 2451 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2452 | return (PyObject *)type; |
| 2453 | } |
| 2454 | |
| 2455 | /* Internal API to look for a name through the MRO. |
| 2456 | This returns a borrowed reference, and doesn't set an exception! */ |
| 2457 | PyObject * |
| 2458 | _PyType_Lookup(PyTypeObject *type, PyObject *name) |
| 2459 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2460 | Py_ssize_t i, n; |
Georg Brandl | f18a707 | 2008-05-29 14:35:39 +0000 | [diff] [blame] | 2461 | PyObject *mro, *res, *base, *dict; |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 2462 | unsigned int h; |
| 2463 | |
| 2464 | if (MCACHE_CACHEABLE_NAME(name) && |
Neal Norwitz | e7bb918 | 2008-01-27 17:10:14 +0000 | [diff] [blame] | 2465 | PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) { |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 2466 | /* fast path */ |
| 2467 | h = MCACHE_HASH_METHOD(type, name); |
| 2468 | if (method_cache[h].version == type->tp_version_tag && |
| 2469 | method_cache[h].name == name) |
| 2470 | return method_cache[h].value; |
| 2471 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2472 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2473 | /* Look in tp_dict of types in MRO */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2474 | mro = type->tp_mro; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 2475 | |
| 2476 | /* If mro is NULL, the type is either not yet initialized |
| 2477 | by PyType_Ready(), or already cleared by type_clear(). |
| 2478 | Either way the safest thing to do is to return NULL. */ |
| 2479 | if (mro == NULL) |
| 2480 | return NULL; |
| 2481 | |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 2482 | res = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2483 | assert(PyTuple_Check(mro)); |
| 2484 | n = PyTuple_GET_SIZE(mro); |
| 2485 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 2486 | base = PyTuple_GET_ITEM(mro, i); |
| 2487 | if (PyClass_Check(base)) |
| 2488 | dict = ((PyClassObject *)base)->cl_dict; |
| 2489 | else { |
| 2490 | assert(PyType_Check(base)); |
| 2491 | dict = ((PyTypeObject *)base)->tp_dict; |
| 2492 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2493 | assert(dict && PyDict_Check(dict)); |
| 2494 | res = PyDict_GetItem(dict, name); |
| 2495 | if (res != NULL) |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 2496 | break; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2497 | } |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 2498 | |
| 2499 | if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) { |
| 2500 | h = MCACHE_HASH_METHOD(type, name); |
| 2501 | method_cache[h].version = type->tp_version_tag; |
| 2502 | method_cache[h].value = res; /* borrowed */ |
| 2503 | Py_INCREF(name); |
Georg Brandl | f18a707 | 2008-05-29 14:35:39 +0000 | [diff] [blame] | 2504 | Py_DECREF(method_cache[h].name); |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 2505 | method_cache[h].name = name; |
| 2506 | } |
| 2507 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2508 | } |
| 2509 | |
| 2510 | /* This is similar to PyObject_GenericGetAttr(), |
| 2511 | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ |
| 2512 | static PyObject * |
| 2513 | type_getattro(PyTypeObject *type, PyObject *name) |
| 2514 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2515 | PyTypeObject *metatype = Py_TYPE(type); |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2516 | PyObject *meta_attribute, *attribute; |
| 2517 | descrgetfunc meta_get; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2518 | |
| 2519 | /* Initialize this type (we'll assume the metatype is initialized) */ |
| 2520 | if (type->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 2521 | if (PyType_Ready(type) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2522 | return NULL; |
| 2523 | } |
| 2524 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2525 | /* No readable descriptor found yet */ |
| 2526 | meta_get = NULL; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2527 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2528 | /* Look for the attribute in the metatype */ |
| 2529 | meta_attribute = _PyType_Lookup(metatype, name); |
| 2530 | |
| 2531 | if (meta_attribute != NULL) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2532 | meta_get = Py_TYPE(meta_attribute)->tp_descr_get; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2533 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2534 | if (meta_get != NULL && PyDescr_IsData(meta_attribute)) { |
| 2535 | /* Data descriptors implement tp_descr_set to intercept |
| 2536 | * writes. Assume the attribute is not overridden in |
| 2537 | * type's tp_dict (and bases): call the descriptor now. |
| 2538 | */ |
| 2539 | return meta_get(meta_attribute, (PyObject *)type, |
| 2540 | (PyObject *)metatype); |
| 2541 | } |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2542 | Py_INCREF(meta_attribute); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2543 | } |
| 2544 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2545 | /* No data descriptor found on metatype. Look in tp_dict of this |
| 2546 | * type and its bases */ |
| 2547 | attribute = _PyType_Lookup(type, name); |
| 2548 | if (attribute != NULL) { |
| 2549 | /* Implement descriptor functionality, if any */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2550 | descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get; |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2551 | |
| 2552 | Py_XDECREF(meta_attribute); |
| 2553 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2554 | if (local_get != NULL) { |
| 2555 | /* NULL 2nd argument indicates the descriptor was |
| 2556 | * found on the target object itself (or a base) */ |
| 2557 | return local_get(attribute, (PyObject *)NULL, |
| 2558 | (PyObject *)type); |
| 2559 | } |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2560 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2561 | Py_INCREF(attribute); |
| 2562 | return attribute; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2565 | /* No attribute found in local __dict__ (or bases): use the |
| 2566 | * descriptor from the metatype, if any */ |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2567 | if (meta_get != NULL) { |
| 2568 | PyObject *res; |
| 2569 | res = meta_get(meta_attribute, (PyObject *)type, |
| 2570 | (PyObject *)metatype); |
| 2571 | Py_DECREF(meta_attribute); |
| 2572 | return res; |
| 2573 | } |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2574 | |
| 2575 | /* If an ordinary attribute was found on the metatype, return it now */ |
| 2576 | if (meta_attribute != NULL) { |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2577 | return meta_attribute; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2578 | } |
| 2579 | |
| 2580 | /* Give up */ |
| 2581 | PyErr_Format(PyExc_AttributeError, |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2582 | "type object '%.50s' has no attribute '%.400s'", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2583 | type->tp_name, PyString_AS_STRING(name)); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2584 | return NULL; |
| 2585 | } |
| 2586 | |
| 2587 | static int |
| 2588 | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) |
| 2589 | { |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 2590 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 2591 | PyErr_Format( |
| 2592 | PyExc_TypeError, |
| 2593 | "can't set attributes of built-in/extension type '%s'", |
| 2594 | type->tp_name); |
| 2595 | return -1; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 2596 | } |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 2597 | if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0) |
| 2598 | return -1; |
| 2599 | return update_slot(type, name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
| 2602 | static void |
| 2603 | type_dealloc(PyTypeObject *type) |
| 2604 | { |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2605 | PyHeapTypeObject *et; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2606 | |
| 2607 | /* Assert this is a heap-allocated type object */ |
| 2608 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2609 | _PyObject_GC_UNTRACK(type); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2610 | PyObject_ClearWeakRefs((PyObject *)type); |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2611 | et = (PyHeapTypeObject *)type; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2612 | Py_XDECREF(type->tp_base); |
| 2613 | Py_XDECREF(type->tp_dict); |
| 2614 | Py_XDECREF(type->tp_bases); |
| 2615 | Py_XDECREF(type->tp_mro); |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2616 | Py_XDECREF(type->tp_cache); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2617 | Py_XDECREF(type->tp_subclasses); |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2618 | /* A type's tp_doc is heap allocated, unlike the tp_doc slots |
| 2619 | * of most other objects. It's okay to cast it to char *. |
| 2620 | */ |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 2621 | PyObject_Free((char *)type->tp_doc); |
Georg Brandl | c255c7b | 2006-02-20 22:27:28 +0000 | [diff] [blame] | 2622 | Py_XDECREF(et->ht_name); |
| 2623 | Py_XDECREF(et->ht_slots); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2624 | Py_TYPE(type)->tp_free((PyObject *)type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2625 | } |
| 2626 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2627 | static PyObject * |
| 2628 | type_subclasses(PyTypeObject *type, PyObject *args_ignored) |
| 2629 | { |
| 2630 | PyObject *list, *raw, *ref; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2631 | Py_ssize_t i, n; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2632 | |
| 2633 | list = PyList_New(0); |
| 2634 | if (list == NULL) |
| 2635 | return NULL; |
| 2636 | raw = type->tp_subclasses; |
| 2637 | if (raw == NULL) |
| 2638 | return list; |
| 2639 | assert(PyList_Check(raw)); |
| 2640 | n = PyList_GET_SIZE(raw); |
| 2641 | for (i = 0; i < n; i++) { |
| 2642 | ref = PyList_GET_ITEM(raw, i); |
Tim Peters | 4438338 | 2001-10-08 16:49:26 +0000 | [diff] [blame] | 2643 | assert(PyWeakref_CheckRef(ref)); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2644 | ref = PyWeakref_GET_OBJECT(ref); |
| 2645 | if (ref != Py_None) { |
| 2646 | if (PyList_Append(list, ref) < 0) { |
| 2647 | Py_DECREF(list); |
| 2648 | return NULL; |
| 2649 | } |
| 2650 | } |
| 2651 | } |
| 2652 | return list; |
| 2653 | } |
| 2654 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2655 | static PyMethodDef type_methods[] = { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2656 | {"mro", (PyCFunction)mro_external, METH_NOARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 2657 | PyDoc_STR("mro() -> list\nreturn a type's method resolution order")}, |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2658 | {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 2659 | PyDoc_STR("__subclasses__() -> list of immediate subclasses")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2660 | {0} |
| 2661 | }; |
| 2662 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2663 | PyDoc_STRVAR(type_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2664 | "type(object) -> the object's type\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2665 | "type(name, bases, dict) -> a new type"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2666 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2667 | static int |
| 2668 | type_traverse(PyTypeObject *type, visitproc visit, void *arg) |
| 2669 | { |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2670 | /* Because of type_is_gc(), the collector only calls this |
| 2671 | for heaptypes. */ |
| 2672 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2673 | |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 2674 | Py_VISIT(type->tp_dict); |
| 2675 | Py_VISIT(type->tp_cache); |
| 2676 | Py_VISIT(type->tp_mro); |
| 2677 | Py_VISIT(type->tp_bases); |
| 2678 | Py_VISIT(type->tp_base); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2679 | |
| 2680 | /* There's no need to visit type->tp_subclasses or |
Georg Brandl | c255c7b | 2006-02-20 22:27:28 +0000 | [diff] [blame] | 2681 | ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2682 | in cycles; tp_subclasses is a list of weak references, |
| 2683 | and slots is a tuple of strings. */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2684 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2685 | return 0; |
| 2686 | } |
| 2687 | |
| 2688 | static int |
| 2689 | type_clear(PyTypeObject *type) |
| 2690 | { |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2691 | /* Because of type_is_gc(), the collector only calls this |
| 2692 | for heaptypes. */ |
| 2693 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2694 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2695 | /* The only field we need to clear is tp_mro, which is part of a |
| 2696 | hard cycle (its first element is the class itself) that won't |
| 2697 | be broken otherwise (it's a tuple and tuples don't have a |
| 2698 | tp_clear handler). None of the other fields need to be |
| 2699 | cleared, and here's why: |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2700 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2701 | tp_dict: |
| 2702 | It is a dict, so the collector will call its tp_clear. |
| 2703 | |
| 2704 | tp_cache: |
| 2705 | Not used; if it were, it would be a dict. |
| 2706 | |
| 2707 | tp_bases, tp_base: |
| 2708 | If these are involved in a cycle, there must be at least |
| 2709 | one other, mutable object in the cycle, e.g. a base |
| 2710 | class's dict; the cycle will be broken that way. |
| 2711 | |
| 2712 | tp_subclasses: |
| 2713 | A list of weak references can't be part of a cycle; and |
| 2714 | lists have their own tp_clear. |
| 2715 | |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2716 | slots (in PyHeapTypeObject): |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2717 | A tuple of strings can't be part of a cycle. |
| 2718 | */ |
| 2719 | |
Thomas Wouters | edf17d8 | 2006-04-15 17:28:34 +0000 | [diff] [blame] | 2720 | Py_CLEAR(type->tp_mro); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2721 | |
| 2722 | return 0; |
| 2723 | } |
| 2724 | |
| 2725 | static int |
| 2726 | type_is_gc(PyTypeObject *type) |
| 2727 | { |
| 2728 | return type->tp_flags & Py_TPFLAGS_HEAPTYPE; |
| 2729 | } |
| 2730 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2731 | PyTypeObject PyType_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2732 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2733 | "type", /* tp_name */ |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2734 | sizeof(PyHeapTypeObject), /* tp_basicsize */ |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 2735 | sizeof(PyMemberDef), /* tp_itemsize */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2736 | (destructor)type_dealloc, /* tp_dealloc */ |
| 2737 | 0, /* tp_print */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2738 | 0, /* tp_getattr */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2739 | 0, /* tp_setattr */ |
| 2740 | type_compare, /* tp_compare */ |
| 2741 | (reprfunc)type_repr, /* tp_repr */ |
| 2742 | 0, /* tp_as_number */ |
| 2743 | 0, /* tp_as_sequence */ |
| 2744 | 0, /* tp_as_mapping */ |
| 2745 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
| 2746 | (ternaryfunc)type_call, /* tp_call */ |
| 2747 | 0, /* tp_str */ |
| 2748 | (getattrofunc)type_getattro, /* tp_getattro */ |
| 2749 | (setattrofunc)type_setattro, /* tp_setattro */ |
| 2750 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2751 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 2752 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2753 | type_doc, /* tp_doc */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2754 | (traverseproc)type_traverse, /* tp_traverse */ |
| 2755 | (inquiry)type_clear, /* tp_clear */ |
Steven Bethard | ae42f33 | 2008-03-18 17:26:10 +0000 | [diff] [blame] | 2756 | type_richcompare, /* tp_richcompare */ |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2757 | offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2758 | 0, /* tp_iter */ |
| 2759 | 0, /* tp_iternext */ |
| 2760 | type_methods, /* tp_methods */ |
| 2761 | type_members, /* tp_members */ |
| 2762 | type_getsets, /* tp_getset */ |
| 2763 | 0, /* tp_base */ |
| 2764 | 0, /* tp_dict */ |
| 2765 | 0, /* tp_descr_get */ |
| 2766 | 0, /* tp_descr_set */ |
| 2767 | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ |
Guido van Rossum | f102e24 | 2007-03-23 18:53:03 +0000 | [diff] [blame] | 2768 | type_init, /* tp_init */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2769 | 0, /* tp_alloc */ |
| 2770 | type_new, /* tp_new */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2771 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2772 | (inquiry)type_is_gc, /* tp_is_gc */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2773 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2774 | |
| 2775 | |
| 2776 | /* The base type of all types (eventually)... except itself. */ |
| 2777 | |
Guido van Rossum | 143b564 | 2007-03-23 04:58:42 +0000 | [diff] [blame] | 2778 | /* You may wonder why object.__new__() only complains about arguments |
| 2779 | when object.__init__() is not overridden, and vice versa. |
| 2780 | |
| 2781 | Consider the use cases: |
| 2782 | |
| 2783 | 1. When neither is overridden, we want to hear complaints about |
| 2784 | excess (i.e., any) arguments, since their presence could |
| 2785 | indicate there's a bug. |
| 2786 | |
| 2787 | 2. When defining an Immutable type, we are likely to override only |
| 2788 | __new__(), since __init__() is called too late to initialize an |
| 2789 | Immutable object. Since __new__() defines the signature for the |
| 2790 | type, it would be a pain to have to override __init__() just to |
| 2791 | stop it from complaining about excess arguments. |
| 2792 | |
| 2793 | 3. When defining a Mutable type, we are likely to override only |
| 2794 | __init__(). So here the converse reasoning applies: we don't |
| 2795 | want to have to override __new__() just to stop it from |
| 2796 | complaining. |
| 2797 | |
| 2798 | 4. When __init__() is overridden, and the subclass __init__() calls |
| 2799 | object.__init__(), the latter should complain about excess |
| 2800 | arguments; ditto for __new__(). |
| 2801 | |
| 2802 | Use cases 2 and 3 make it unattractive to unconditionally check for |
| 2803 | excess arguments. The best solution that addresses all four use |
| 2804 | cases is as follows: __init__() complains about excess arguments |
| 2805 | unless __new__() is overridden and __init__() is not overridden |
| 2806 | (IOW, if __init__() is overridden or __new__() is not overridden); |
| 2807 | symmetrically, __new__() complains about excess arguments unless |
| 2808 | __init__() is overridden and __new__() is not overridden |
| 2809 | (IOW, if __new__() is overridden or __init__() is not overridden). |
| 2810 | |
| 2811 | However, for backwards compatibility, this breaks too much code. |
| 2812 | Therefore, in 2.6, we'll *warn* about excess arguments when both |
| 2813 | methods are overridden; for all other cases we'll use the above |
| 2814 | rules. |
| 2815 | |
| 2816 | */ |
| 2817 | |
| 2818 | /* Forward */ |
| 2819 | static PyObject * |
| 2820 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 2821 | |
| 2822 | static int |
| 2823 | excess_args(PyObject *args, PyObject *kwds) |
| 2824 | { |
| 2825 | return PyTuple_GET_SIZE(args) || |
| 2826 | (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)); |
| 2827 | } |
| 2828 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2829 | static int |
| 2830 | object_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2831 | { |
Guido van Rossum | 143b564 | 2007-03-23 04:58:42 +0000 | [diff] [blame] | 2832 | int err = 0; |
| 2833 | if (excess_args(args, kwds)) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2834 | PyTypeObject *type = Py_TYPE(self); |
Guido van Rossum | 143b564 | 2007-03-23 04:58:42 +0000 | [diff] [blame] | 2835 | if (type->tp_init != object_init && |
| 2836 | type->tp_new != object_new) |
| 2837 | { |
| 2838 | err = PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2839 | "object.__init__() takes no parameters", |
| 2840 | 1); |
| 2841 | } |
| 2842 | else if (type->tp_init != object_init || |
| 2843 | type->tp_new == object_new) |
| 2844 | { |
| 2845 | PyErr_SetString(PyExc_TypeError, |
| 2846 | "object.__init__() takes no parameters"); |
| 2847 | err = -1; |
| 2848 | } |
| 2849 | } |
| 2850 | return err; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2853 | static PyObject * |
| 2854 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2855 | { |
Guido van Rossum | 143b564 | 2007-03-23 04:58:42 +0000 | [diff] [blame] | 2856 | int err = 0; |
| 2857 | if (excess_args(args, kwds)) { |
| 2858 | if (type->tp_new != object_new && |
| 2859 | type->tp_init != object_init) |
| 2860 | { |
| 2861 | err = PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2862 | "object.__new__() takes no parameters", |
| 2863 | 1); |
| 2864 | } |
| 2865 | else if (type->tp_new != object_new || |
| 2866 | type->tp_init == object_init) |
| 2867 | { |
| 2868 | PyErr_SetString(PyExc_TypeError, |
| 2869 | "object.__new__() takes no parameters"); |
| 2870 | err = -1; |
| 2871 | } |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2872 | } |
Guido van Rossum | 143b564 | 2007-03-23 04:58:42 +0000 | [diff] [blame] | 2873 | if (err < 0) |
| 2874 | return NULL; |
Jeffrey Yasskin | 960b9b7 | 2008-02-28 04:45:36 +0000 | [diff] [blame] | 2875 | |
| 2876 | if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) { |
| 2877 | static PyObject *comma = NULL; |
| 2878 | PyObject *abstract_methods = NULL; |
| 2879 | PyObject *builtins; |
| 2880 | PyObject *sorted; |
| 2881 | PyObject *sorted_methods = NULL; |
| 2882 | PyObject *joined = NULL; |
| 2883 | const char *joined_str; |
| 2884 | |
| 2885 | /* Compute ", ".join(sorted(type.__abstractmethods__)) |
| 2886 | into joined. */ |
| 2887 | abstract_methods = type_abstractmethods(type, NULL); |
| 2888 | if (abstract_methods == NULL) |
| 2889 | goto error; |
| 2890 | builtins = PyEval_GetBuiltins(); |
| 2891 | if (builtins == NULL) |
| 2892 | goto error; |
| 2893 | sorted = PyDict_GetItemString(builtins, "sorted"); |
| 2894 | if (sorted == NULL) |
| 2895 | goto error; |
| 2896 | sorted_methods = PyObject_CallFunctionObjArgs(sorted, |
| 2897 | abstract_methods, |
| 2898 | NULL); |
| 2899 | if (sorted_methods == NULL) |
| 2900 | goto error; |
| 2901 | if (comma == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2902 | comma = PyString_InternFromString(", "); |
Jeffrey Yasskin | 960b9b7 | 2008-02-28 04:45:36 +0000 | [diff] [blame] | 2903 | if (comma == NULL) |
| 2904 | goto error; |
| 2905 | } |
| 2906 | joined = PyObject_CallMethod(comma, "join", |
| 2907 | "O", sorted_methods); |
| 2908 | if (joined == NULL) |
| 2909 | goto error; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2910 | joined_str = PyString_AsString(joined); |
Jeffrey Yasskin | 960b9b7 | 2008-02-28 04:45:36 +0000 | [diff] [blame] | 2911 | if (joined_str == NULL) |
| 2912 | goto error; |
| 2913 | |
| 2914 | PyErr_Format(PyExc_TypeError, |
| 2915 | "Can't instantiate abstract class %s " |
| 2916 | "with abstract methods %s", |
| 2917 | type->tp_name, |
| 2918 | joined_str); |
| 2919 | error: |
| 2920 | Py_XDECREF(joined); |
| 2921 | Py_XDECREF(sorted_methods); |
| 2922 | Py_XDECREF(abstract_methods); |
| 2923 | return NULL; |
| 2924 | } |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2925 | return type->tp_alloc(type, 0); |
| 2926 | } |
| 2927 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2928 | static void |
| 2929 | object_dealloc(PyObject *self) |
| 2930 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2931 | Py_TYPE(self)->tp_free(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2932 | } |
| 2933 | |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2934 | static PyObject * |
| 2935 | object_repr(PyObject *self) |
| 2936 | { |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2937 | PyTypeObject *type; |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2938 | PyObject *mod, *name, *rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2939 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2940 | type = Py_TYPE(self); |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2941 | mod = type_module(type, NULL); |
| 2942 | if (mod == NULL) |
| 2943 | PyErr_Clear(); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2944 | else if (!PyString_Check(mod)) { |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2945 | Py_DECREF(mod); |
| 2946 | mod = NULL; |
| 2947 | } |
| 2948 | name = type_name(type, NULL); |
| 2949 | if (name == NULL) |
| 2950 | return NULL; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2951 | if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) |
| 2952 | rtn = PyString_FromFormat("<%s.%s object at %p>", |
| 2953 | PyString_AS_STRING(mod), |
| 2954 | PyString_AS_STRING(name), |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2955 | self); |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2956 | else |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2957 | rtn = PyString_FromFormat("<%s object at %p>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2958 | type->tp_name, self); |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2959 | Py_XDECREF(mod); |
| 2960 | Py_DECREF(name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2961 | return rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2962 | } |
| 2963 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2964 | static PyObject * |
| 2965 | object_str(PyObject *self) |
| 2966 | { |
| 2967 | unaryfunc f; |
| 2968 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2969 | f = Py_TYPE(self)->tp_repr; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2970 | if (f == NULL) |
| 2971 | f = object_repr; |
| 2972 | return f(self); |
| 2973 | } |
| 2974 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2975 | static PyObject * |
| 2976 | object_get_class(PyObject *self, void *closure) |
| 2977 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2978 | Py_INCREF(Py_TYPE(self)); |
| 2979 | return (PyObject *)(Py_TYPE(self)); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | static int |
| 2983 | equiv_structs(PyTypeObject *a, PyTypeObject *b) |
| 2984 | { |
| 2985 | return a == b || |
| 2986 | (a != NULL && |
| 2987 | b != NULL && |
| 2988 | a->tp_basicsize == b->tp_basicsize && |
| 2989 | a->tp_itemsize == b->tp_itemsize && |
| 2990 | a->tp_dictoffset == b->tp_dictoffset && |
| 2991 | a->tp_weaklistoffset == b->tp_weaklistoffset && |
| 2992 | ((a->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 2993 | (b->tp_flags & Py_TPFLAGS_HAVE_GC))); |
| 2994 | } |
| 2995 | |
| 2996 | static int |
| 2997 | same_slots_added(PyTypeObject *a, PyTypeObject *b) |
| 2998 | { |
| 2999 | PyTypeObject *base = a->tp_base; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 3000 | Py_ssize_t size; |
Žiga Seilnacht | 6f2d09c | 2007-03-16 11:59:38 +0000 | [diff] [blame] | 3001 | PyObject *slots_a, *slots_b; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3002 | |
| 3003 | if (base != b->tp_base) |
| 3004 | return 0; |
| 3005 | if (equiv_structs(a, base) && equiv_structs(b, base)) |
| 3006 | return 1; |
| 3007 | size = base->tp_basicsize; |
| 3008 | if (a->tp_dictoffset == size && b->tp_dictoffset == size) |
| 3009 | size += sizeof(PyObject *); |
| 3010 | if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size) |
| 3011 | size += sizeof(PyObject *); |
Žiga Seilnacht | 6f2d09c | 2007-03-16 11:59:38 +0000 | [diff] [blame] | 3012 | |
| 3013 | /* Check slots compliance */ |
| 3014 | slots_a = ((PyHeapTypeObject *)a)->ht_slots; |
| 3015 | slots_b = ((PyHeapTypeObject *)b)->ht_slots; |
| 3016 | if (slots_a && slots_b) { |
| 3017 | if (PyObject_Compare(slots_a, slots_b) != 0) |
| 3018 | return 0; |
| 3019 | size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a); |
| 3020 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3021 | return size == a->tp_basicsize && size == b->tp_basicsize; |
| 3022 | } |
| 3023 | |
| 3024 | static int |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3025 | compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3026 | { |
| 3027 | PyTypeObject *newbase, *oldbase; |
| 3028 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3029 | if (newto->tp_dealloc != oldto->tp_dealloc || |
| 3030 | newto->tp_free != oldto->tp_free) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3031 | { |
| 3032 | PyErr_Format(PyExc_TypeError, |
| 3033 | "%s assignment: " |
| 3034 | "'%s' deallocator differs from '%s'", |
| 3035 | attr, |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3036 | newto->tp_name, |
| 3037 | oldto->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3038 | return 0; |
| 3039 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3040 | newbase = newto; |
| 3041 | oldbase = oldto; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3042 | while (equiv_structs(newbase, newbase->tp_base)) |
| 3043 | newbase = newbase->tp_base; |
| 3044 | while (equiv_structs(oldbase, oldbase->tp_base)) |
| 3045 | oldbase = oldbase->tp_base; |
| 3046 | if (newbase != oldbase && |
| 3047 | (newbase->tp_base != oldbase->tp_base || |
| 3048 | !same_slots_added(newbase, oldbase))) { |
| 3049 | PyErr_Format(PyExc_TypeError, |
| 3050 | "%s assignment: " |
| 3051 | "'%s' object layout differs from '%s'", |
| 3052 | attr, |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3053 | newto->tp_name, |
| 3054 | oldto->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3055 | return 0; |
| 3056 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 3057 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3058 | return 1; |
| 3059 | } |
| 3060 | |
| 3061 | static int |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3062 | object_set_class(PyObject *self, PyObject *value, void *closure) |
| 3063 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 3064 | PyTypeObject *oldto = Py_TYPE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3065 | PyTypeObject *newto; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3066 | |
Guido van Rossum | b6b8942 | 2002-04-15 01:03:30 +0000 | [diff] [blame] | 3067 | if (value == NULL) { |
| 3068 | PyErr_SetString(PyExc_TypeError, |
| 3069 | "can't delete __class__ attribute"); |
| 3070 | return -1; |
| 3071 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3072 | if (!PyType_Check(value)) { |
| 3073 | PyErr_Format(PyExc_TypeError, |
| 3074 | "__class__ must be set to new-style class, not '%s' object", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 3075 | Py_TYPE(value)->tp_name); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3076 | return -1; |
| 3077 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3078 | newto = (PyTypeObject *)value; |
| 3079 | if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) || |
| 3080 | !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
Guido van Rossum | 40af889 | 2002-08-10 05:42:07 +0000 | [diff] [blame] | 3081 | { |
| 3082 | PyErr_Format(PyExc_TypeError, |
| 3083 | "__class__ assignment: only for heap types"); |
| 3084 | return -1; |
| 3085 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3086 | if (compatible_for_assignment(newto, oldto, "__class__")) { |
| 3087 | Py_INCREF(newto); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 3088 | Py_TYPE(self) = newto; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3089 | Py_DECREF(oldto); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3090 | return 0; |
| 3091 | } |
| 3092 | else { |
Guido van Rossum | 9ee4b94 | 2002-05-24 18:47:47 +0000 | [diff] [blame] | 3093 | return -1; |
| 3094 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
| 3097 | static PyGetSetDef object_getsets[] = { |
| 3098 | {"__class__", object_get_class, object_set_class, |
Neal Norwitz | 858e34f | 2002-08-13 17:18:45 +0000 | [diff] [blame] | 3099 | PyDoc_STR("the object's class")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3100 | {0} |
| 3101 | }; |
| 3102 | |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 3103 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3104 | /* Stuff to implement __reduce_ex__ for pickle protocols >= 2. |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 3105 | We fall back to helpers in copy_reg for: |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3106 | - pickle protocols < 2 |
| 3107 | - calculating the list of slot names (done only once per class) |
| 3108 | - the __newobj__ function (which is used as a token but never called) |
| 3109 | */ |
| 3110 | |
| 3111 | static PyObject * |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3112 | import_copyreg(void) |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3113 | { |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3114 | static PyObject *copyreg_str; |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3115 | |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3116 | if (!copyreg_str) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3117 | copyreg_str = PyString_InternFromString("copy_reg"); |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3118 | if (copyreg_str == NULL) |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3119 | return NULL; |
| 3120 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3121 | |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3122 | return PyImport_Import(copyreg_str); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3123 | } |
| 3124 | |
| 3125 | static PyObject * |
| 3126 | slotnames(PyObject *cls) |
| 3127 | { |
| 3128 | PyObject *clsdict; |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3129 | PyObject *copyreg; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3130 | PyObject *slotnames; |
| 3131 | |
| 3132 | if (!PyType_Check(cls)) { |
| 3133 | Py_INCREF(Py_None); |
| 3134 | return Py_None; |
| 3135 | } |
| 3136 | |
| 3137 | clsdict = ((PyTypeObject *)cls)->tp_dict; |
| 3138 | slotnames = PyDict_GetItemString(clsdict, "__slotnames__"); |
Armin Rigo | ec862b9 | 2005-09-24 22:58:41 +0000 | [diff] [blame] | 3139 | if (slotnames != NULL && PyList_Check(slotnames)) { |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3140 | Py_INCREF(slotnames); |
| 3141 | return slotnames; |
| 3142 | } |
| 3143 | |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3144 | copyreg = import_copyreg(); |
| 3145 | if (copyreg == NULL) |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3146 | return NULL; |
| 3147 | |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3148 | slotnames = PyObject_CallMethod(copyreg, "_slotnames", "O", cls); |
| 3149 | Py_DECREF(copyreg); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3150 | if (slotnames != NULL && |
| 3151 | slotnames != Py_None && |
| 3152 | !PyList_Check(slotnames)) |
| 3153 | { |
| 3154 | PyErr_SetString(PyExc_TypeError, |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 3155 | "copy_reg._slotnames didn't return a list or None"); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3156 | Py_DECREF(slotnames); |
| 3157 | slotnames = NULL; |
| 3158 | } |
| 3159 | |
| 3160 | return slotnames; |
| 3161 | } |
| 3162 | |
| 3163 | static PyObject * |
| 3164 | reduce_2(PyObject *obj) |
| 3165 | { |
| 3166 | PyObject *cls, *getnewargs; |
| 3167 | PyObject *args = NULL, *args2 = NULL; |
| 3168 | PyObject *getstate = NULL, *state = NULL, *names = NULL; |
| 3169 | PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL; |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3170 | PyObject *copyreg = NULL, *newobj = NULL, *res = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3171 | Py_ssize_t i, n; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3172 | |
| 3173 | cls = PyObject_GetAttrString(obj, "__class__"); |
| 3174 | if (cls == NULL) |
| 3175 | return NULL; |
| 3176 | |
| 3177 | getnewargs = PyObject_GetAttrString(obj, "__getnewargs__"); |
| 3178 | if (getnewargs != NULL) { |
| 3179 | args = PyObject_CallObject(getnewargs, NULL); |
| 3180 | Py_DECREF(getnewargs); |
| 3181 | if (args != NULL && !PyTuple_Check(args)) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 3182 | PyErr_Format(PyExc_TypeError, |
| 3183 | "__getnewargs__ should return a tuple, " |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 3184 | "not '%.200s'", Py_TYPE(args)->tp_name); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3185 | goto end; |
| 3186 | } |
| 3187 | } |
| 3188 | else { |
| 3189 | PyErr_Clear(); |
| 3190 | args = PyTuple_New(0); |
| 3191 | } |
| 3192 | if (args == NULL) |
| 3193 | goto end; |
| 3194 | |
| 3195 | getstate = PyObject_GetAttrString(obj, "__getstate__"); |
| 3196 | if (getstate != NULL) { |
| 3197 | state = PyObject_CallObject(getstate, NULL); |
| 3198 | Py_DECREF(getstate); |
Neal Norwitz | e2fdc61 | 2003-06-08 13:19:58 +0000 | [diff] [blame] | 3199 | if (state == NULL) |
| 3200 | goto end; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3201 | } |
| 3202 | else { |
Jim Fulton | 8a1a594 | 2004-02-08 04:21:26 +0000 | [diff] [blame] | 3203 | PyErr_Clear(); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3204 | state = PyObject_GetAttrString(obj, "__dict__"); |
| 3205 | if (state == NULL) { |
| 3206 | PyErr_Clear(); |
| 3207 | state = Py_None; |
| 3208 | Py_INCREF(state); |
| 3209 | } |
| 3210 | names = slotnames(cls); |
| 3211 | if (names == NULL) |
| 3212 | goto end; |
| 3213 | if (names != Py_None) { |
| 3214 | assert(PyList_Check(names)); |
| 3215 | slots = PyDict_New(); |
| 3216 | if (slots == NULL) |
| 3217 | goto end; |
| 3218 | n = 0; |
| 3219 | /* Can't pre-compute the list size; the list |
| 3220 | is stored on the class so accessible to other |
| 3221 | threads, which may be run by DECREF */ |
| 3222 | for (i = 0; i < PyList_GET_SIZE(names); i++) { |
| 3223 | PyObject *name, *value; |
| 3224 | name = PyList_GET_ITEM(names, i); |
| 3225 | value = PyObject_GetAttr(obj, name); |
| 3226 | if (value == NULL) |
| 3227 | PyErr_Clear(); |
| 3228 | else { |
| 3229 | int err = PyDict_SetItem(slots, name, |
| 3230 | value); |
| 3231 | Py_DECREF(value); |
| 3232 | if (err) |
| 3233 | goto end; |
| 3234 | n++; |
| 3235 | } |
| 3236 | } |
| 3237 | if (n) { |
| 3238 | state = Py_BuildValue("(NO)", state, slots); |
| 3239 | if (state == NULL) |
| 3240 | goto end; |
| 3241 | } |
| 3242 | } |
| 3243 | } |
| 3244 | |
| 3245 | if (!PyList_Check(obj)) { |
| 3246 | listitems = Py_None; |
| 3247 | Py_INCREF(listitems); |
| 3248 | } |
| 3249 | else { |
| 3250 | listitems = PyObject_GetIter(obj); |
| 3251 | if (listitems == NULL) |
| 3252 | goto end; |
| 3253 | } |
| 3254 | |
| 3255 | if (!PyDict_Check(obj)) { |
| 3256 | dictitems = Py_None; |
| 3257 | Py_INCREF(dictitems); |
| 3258 | } |
| 3259 | else { |
| 3260 | dictitems = PyObject_CallMethod(obj, "iteritems", ""); |
| 3261 | if (dictitems == NULL) |
| 3262 | goto end; |
| 3263 | } |
| 3264 | |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3265 | copyreg = import_copyreg(); |
| 3266 | if (copyreg == NULL) |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3267 | goto end; |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3268 | newobj = PyObject_GetAttrString(copyreg, "__newobj__"); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3269 | if (newobj == NULL) |
| 3270 | goto end; |
| 3271 | |
| 3272 | n = PyTuple_GET_SIZE(args); |
| 3273 | args2 = PyTuple_New(n+1); |
| 3274 | if (args2 == NULL) |
| 3275 | goto end; |
| 3276 | PyTuple_SET_ITEM(args2, 0, cls); |
| 3277 | cls = NULL; |
| 3278 | for (i = 0; i < n; i++) { |
| 3279 | PyObject *v = PyTuple_GET_ITEM(args, i); |
| 3280 | Py_INCREF(v); |
| 3281 | PyTuple_SET_ITEM(args2, i+1, v); |
| 3282 | } |
| 3283 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3284 | res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3285 | |
| 3286 | end: |
| 3287 | Py_XDECREF(cls); |
| 3288 | Py_XDECREF(args); |
| 3289 | Py_XDECREF(args2); |
Jeremy Hylton | d06483c | 2003-04-09 21:01:42 +0000 | [diff] [blame] | 3290 | Py_XDECREF(slots); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3291 | Py_XDECREF(state); |
| 3292 | Py_XDECREF(names); |
| 3293 | Py_XDECREF(listitems); |
| 3294 | Py_XDECREF(dictitems); |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3295 | Py_XDECREF(copyreg); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3296 | Py_XDECREF(newobj); |
| 3297 | return res; |
| 3298 | } |
| 3299 | |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 3300 | /* |
| 3301 | * There were two problems when object.__reduce__ and object.__reduce_ex__ |
| 3302 | * were implemented in the same function: |
| 3303 | * - trying to pickle an object with a custom __reduce__ method that |
| 3304 | * fell back to object.__reduce__ in certain circumstances led to |
| 3305 | * infinite recursion at Python level and eventual RuntimeError. |
| 3306 | * - Pickling objects that lied about their type by overwriting the |
| 3307 | * __class__ descriptor could lead to infinite recursion at C level |
| 3308 | * and eventual segfault. |
| 3309 | * |
| 3310 | * Because of backwards compatibility, the two methods still have to |
| 3311 | * behave in the same way, even if this is not required by the pickle |
| 3312 | * protocol. This common functionality was moved to the _common_reduce |
| 3313 | * function. |
| 3314 | */ |
| 3315 | static PyObject * |
| 3316 | _common_reduce(PyObject *self, int proto) |
| 3317 | { |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3318 | PyObject *copyreg, *res; |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 3319 | |
| 3320 | if (proto >= 2) |
| 3321 | return reduce_2(self); |
| 3322 | |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3323 | copyreg = import_copyreg(); |
| 3324 | if (!copyreg) |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 3325 | return NULL; |
| 3326 | |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3327 | res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto); |
| 3328 | Py_DECREF(copyreg); |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 3329 | |
| 3330 | return res; |
| 3331 | } |
| 3332 | |
| 3333 | static PyObject * |
| 3334 | object_reduce(PyObject *self, PyObject *args) |
| 3335 | { |
| 3336 | int proto = 0; |
| 3337 | |
| 3338 | if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto)) |
| 3339 | return NULL; |
| 3340 | |
| 3341 | return _common_reduce(self, proto); |
| 3342 | } |
| 3343 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3344 | static PyObject * |
| 3345 | object_reduce_ex(PyObject *self, PyObject *args) |
| 3346 | { |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 3347 | PyObject *reduce, *res; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3348 | int proto = 0; |
| 3349 | |
| 3350 | if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto)) |
| 3351 | return NULL; |
| 3352 | |
| 3353 | reduce = PyObject_GetAttrString(self, "__reduce__"); |
| 3354 | if (reduce == NULL) |
| 3355 | PyErr_Clear(); |
| 3356 | else { |
| 3357 | PyObject *cls, *clsreduce, *objreduce; |
| 3358 | int override; |
| 3359 | cls = PyObject_GetAttrString(self, "__class__"); |
| 3360 | if (cls == NULL) { |
| 3361 | Py_DECREF(reduce); |
| 3362 | return NULL; |
| 3363 | } |
| 3364 | clsreduce = PyObject_GetAttrString(cls, "__reduce__"); |
| 3365 | Py_DECREF(cls); |
| 3366 | if (clsreduce == NULL) { |
| 3367 | Py_DECREF(reduce); |
| 3368 | return NULL; |
| 3369 | } |
| 3370 | objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict, |
| 3371 | "__reduce__"); |
| 3372 | override = (clsreduce != objreduce); |
| 3373 | Py_DECREF(clsreduce); |
| 3374 | if (override) { |
| 3375 | res = PyObject_CallObject(reduce, NULL); |
| 3376 | Py_DECREF(reduce); |
| 3377 | return res; |
| 3378 | } |
| 3379 | else |
| 3380 | Py_DECREF(reduce); |
| 3381 | } |
| 3382 | |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 3383 | return _common_reduce(self, proto); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3384 | } |
| 3385 | |
Jeffrey Yasskin | 960b9b7 | 2008-02-28 04:45:36 +0000 | [diff] [blame] | 3386 | static PyObject * |
| 3387 | object_subclasshook(PyObject *cls, PyObject *args) |
| 3388 | { |
| 3389 | Py_INCREF(Py_NotImplemented); |
| 3390 | return Py_NotImplemented; |
| 3391 | } |
| 3392 | |
| 3393 | PyDoc_STRVAR(object_subclasshook_doc, |
| 3394 | "Abstract classes can override this to customize issubclass().\n" |
| 3395 | "\n" |
| 3396 | "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n" |
| 3397 | "It should return True, False or NotImplemented. If it returns\n" |
| 3398 | "NotImplemented, the normal algorithm is used. Otherwise, it\n" |
| 3399 | "overrides the normal algorithm (and the outcome is cached).\n"); |
| 3400 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 3401 | /* |
| 3402 | from PEP 3101, this code implements: |
| 3403 | |
| 3404 | class object: |
| 3405 | def __format__(self, format_spec): |
| 3406 | if isinstance(format_spec, str): |
| 3407 | return format(str(self), format_spec) |
| 3408 | elif isinstance(format_spec, unicode): |
| 3409 | return format(unicode(self), format_spec) |
| 3410 | */ |
| 3411 | static PyObject * |
| 3412 | object_format(PyObject *self, PyObject *args) |
| 3413 | { |
| 3414 | PyObject *format_spec; |
| 3415 | PyObject *self_as_str = NULL; |
| 3416 | PyObject *result = NULL; |
| 3417 | PyObject *format_meth = NULL; |
| 3418 | |
| 3419 | if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) |
| 3420 | return NULL; |
| 3421 | if (PyUnicode_Check(format_spec)) { |
| 3422 | self_as_str = PyObject_Unicode(self); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3423 | } else if (PyString_Check(format_spec)) { |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 3424 | self_as_str = PyObject_Str(self); |
| 3425 | } else { |
| 3426 | PyErr_SetString(PyExc_TypeError, "argument to __format__ must be unicode or str"); |
| 3427 | return NULL; |
| 3428 | } |
| 3429 | |
| 3430 | if (self_as_str != NULL) { |
| 3431 | /* find the format function */ |
| 3432 | format_meth = PyObject_GetAttrString(self_as_str, "__format__"); |
| 3433 | if (format_meth != NULL) { |
| 3434 | /* and call it */ |
| 3435 | result = PyObject_CallFunctionObjArgs(format_meth, format_spec, NULL); |
| 3436 | } |
| 3437 | } |
| 3438 | |
| 3439 | Py_XDECREF(self_as_str); |
| 3440 | Py_XDECREF(format_meth); |
| 3441 | |
| 3442 | return result; |
| 3443 | } |
| 3444 | |
Robert Schuppenies | 51df064 | 2008-06-01 16:16:17 +0000 | [diff] [blame] | 3445 | static PyObject * |
| 3446 | object_sizeof(PyObject *self, PyObject *args) |
| 3447 | { |
| 3448 | Py_ssize_t res, isize; |
| 3449 | |
| 3450 | res = 0; |
| 3451 | isize = self->ob_type->tp_itemsize; |
| 3452 | if (isize > 0) |
| 3453 | res = self->ob_type->ob_size * isize; |
| 3454 | res += self->ob_type->tp_basicsize; |
| 3455 | |
| 3456 | return PyInt_FromSsize_t(res); |
| 3457 | } |
| 3458 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3459 | static PyMethodDef object_methods[] = { |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 3460 | {"__reduce_ex__", object_reduce_ex, METH_VARARGS, |
| 3461 | PyDoc_STR("helper for pickle")}, |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 3462 | {"__reduce__", object_reduce, METH_VARARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 3463 | PyDoc_STR("helper for pickle")}, |
Jeffrey Yasskin | 960b9b7 | 2008-02-28 04:45:36 +0000 | [diff] [blame] | 3464 | {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS, |
| 3465 | object_subclasshook_doc}, |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 3466 | {"__format__", object_format, METH_VARARGS, |
| 3467 | PyDoc_STR("default object formatter")}, |
Robert Schuppenies | 51df064 | 2008-06-01 16:16:17 +0000 | [diff] [blame] | 3468 | {"__sizeof__", object_sizeof, METH_NOARGS, |
Georg Brandl | 7a6de8b | 2008-06-01 16:42:16 +0000 | [diff] [blame] | 3469 | PyDoc_STR("__sizeof__() -> size of object in memory, in bytes")}, |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3470 | {0} |
| 3471 | }; |
| 3472 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3473 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3474 | PyTypeObject PyBaseObject_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 3475 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3476 | "object", /* tp_name */ |
| 3477 | sizeof(PyObject), /* tp_basicsize */ |
| 3478 | 0, /* tp_itemsize */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 3479 | object_dealloc, /* tp_dealloc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3480 | 0, /* tp_print */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3481 | 0, /* tp_getattr */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3482 | 0, /* tp_setattr */ |
| 3483 | 0, /* tp_compare */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3484 | object_repr, /* tp_repr */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3485 | 0, /* tp_as_number */ |
| 3486 | 0, /* tp_as_sequence */ |
| 3487 | 0, /* tp_as_mapping */ |
Guido van Rossum | 64c06e3 | 2007-11-22 00:55:51 +0000 | [diff] [blame] | 3488 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3489 | 0, /* tp_call */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3490 | object_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3491 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3492 | PyObject_GenericSetAttr, /* tp_setattro */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3493 | 0, /* tp_as_buffer */ |
| 3494 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 3495 | PyDoc_STR("The most base type"), /* tp_doc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3496 | 0, /* tp_traverse */ |
| 3497 | 0, /* tp_clear */ |
| 3498 | 0, /* tp_richcompare */ |
| 3499 | 0, /* tp_weaklistoffset */ |
| 3500 | 0, /* tp_iter */ |
| 3501 | 0, /* tp_iternext */ |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3502 | object_methods, /* tp_methods */ |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3503 | 0, /* tp_members */ |
| 3504 | object_getsets, /* tp_getset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3505 | 0, /* tp_base */ |
| 3506 | 0, /* tp_dict */ |
| 3507 | 0, /* tp_descr_get */ |
| 3508 | 0, /* tp_descr_set */ |
| 3509 | 0, /* tp_dictoffset */ |
| 3510 | object_init, /* tp_init */ |
| 3511 | PyType_GenericAlloc, /* tp_alloc */ |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 3512 | object_new, /* tp_new */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3513 | PyObject_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3514 | }; |
| 3515 | |
| 3516 | |
| 3517 | /* Initialize the __dict__ in a type object */ |
| 3518 | |
| 3519 | static int |
| 3520 | add_methods(PyTypeObject *type, PyMethodDef *meth) |
| 3521 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3522 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3523 | |
| 3524 | for (; meth->ml_name != NULL; meth++) { |
| 3525 | PyObject *descr; |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 3526 | if (PyDict_GetItemString(dict, meth->ml_name) && |
| 3527 | !(meth->ml_flags & METH_COEXIST)) |
| 3528 | continue; |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 3529 | if (meth->ml_flags & METH_CLASS) { |
| 3530 | if (meth->ml_flags & METH_STATIC) { |
| 3531 | PyErr_SetString(PyExc_ValueError, |
| 3532 | "method cannot be both class and static"); |
| 3533 | return -1; |
| 3534 | } |
Tim Peters | bca1cbc | 2002-12-09 22:56:13 +0000 | [diff] [blame] | 3535 | descr = PyDescr_NewClassMethod(type, meth); |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 3536 | } |
| 3537 | else if (meth->ml_flags & METH_STATIC) { |
Guido van Rossum | 9af48ff | 2003-02-11 17:12:46 +0000 | [diff] [blame] | 3538 | PyObject *cfunc = PyCFunction_New(meth, NULL); |
| 3539 | if (cfunc == NULL) |
| 3540 | return -1; |
| 3541 | descr = PyStaticMethod_New(cfunc); |
| 3542 | Py_DECREF(cfunc); |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 3543 | } |
| 3544 | else { |
| 3545 | descr = PyDescr_NewMethod(type, meth); |
| 3546 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3547 | if (descr == NULL) |
| 3548 | return -1; |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 3549 | if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3550 | return -1; |
| 3551 | Py_DECREF(descr); |
| 3552 | } |
| 3553 | return 0; |
| 3554 | } |
| 3555 | |
| 3556 | static int |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 3557 | add_members(PyTypeObject *type, PyMemberDef *memb) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3558 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3559 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3560 | |
| 3561 | for (; memb->name != NULL; memb++) { |
| 3562 | PyObject *descr; |
| 3563 | if (PyDict_GetItemString(dict, memb->name)) |
| 3564 | continue; |
| 3565 | descr = PyDescr_NewMember(type, memb); |
| 3566 | if (descr == NULL) |
| 3567 | return -1; |
| 3568 | if (PyDict_SetItemString(dict, memb->name, descr) < 0) |
| 3569 | return -1; |
| 3570 | Py_DECREF(descr); |
| 3571 | } |
| 3572 | return 0; |
| 3573 | } |
| 3574 | |
| 3575 | static int |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 3576 | add_getset(PyTypeObject *type, PyGetSetDef *gsp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3577 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3578 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3579 | |
| 3580 | for (; gsp->name != NULL; gsp++) { |
| 3581 | PyObject *descr; |
| 3582 | if (PyDict_GetItemString(dict, gsp->name)) |
| 3583 | continue; |
| 3584 | descr = PyDescr_NewGetSet(type, gsp); |
| 3585 | |
| 3586 | if (descr == NULL) |
| 3587 | return -1; |
| 3588 | if (PyDict_SetItemString(dict, gsp->name, descr) < 0) |
| 3589 | return -1; |
| 3590 | Py_DECREF(descr); |
| 3591 | } |
| 3592 | return 0; |
| 3593 | } |
| 3594 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3595 | static void |
| 3596 | inherit_special(PyTypeObject *type, PyTypeObject *base) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3597 | { |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 3598 | Py_ssize_t oldsize, newsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3599 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3600 | /* Special flag magic */ |
| 3601 | if (!type->tp_as_buffer && base->tp_as_buffer) { |
| 3602 | type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER; |
| 3603 | type->tp_flags |= |
| 3604 | base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER; |
| 3605 | } |
| 3606 | if (!type->tp_as_sequence && base->tp_as_sequence) { |
| 3607 | type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN; |
| 3608 | type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN; |
| 3609 | } |
| 3610 | if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) != |
| 3611 | (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) { |
| 3612 | if ((!type->tp_as_number && base->tp_as_number) || |
| 3613 | (!type->tp_as_sequence && base->tp_as_sequence)) { |
| 3614 | type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS; |
| 3615 | if (!type->tp_as_number && !type->tp_as_sequence) { |
| 3616 | type->tp_flags |= base->tp_flags & |
| 3617 | Py_TPFLAGS_HAVE_INPLACEOPS; |
| 3618 | } |
| 3619 | } |
| 3620 | /* Wow */ |
| 3621 | } |
| 3622 | if (!type->tp_as_number && base->tp_as_number) { |
| 3623 | type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES; |
| 3624 | type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES; |
| 3625 | } |
| 3626 | |
| 3627 | /* Copying basicsize is connected to the GC flags */ |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 3628 | oldsize = base->tp_basicsize; |
| 3629 | newsize = type->tp_basicsize ? type->tp_basicsize : oldsize; |
| 3630 | if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3631 | (base->tp_flags & Py_TPFLAGS_HAVE_GC) && |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3632 | (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) && |
| 3633 | (!type->tp_traverse && !type->tp_clear)) { |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 3634 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3635 | if (type->tp_traverse == NULL) |
| 3636 | type->tp_traverse = base->tp_traverse; |
| 3637 | if (type->tp_clear == NULL) |
| 3638 | type->tp_clear = base->tp_clear; |
| 3639 | } |
| 3640 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
Guido van Rossum | f884b74 | 2001-12-17 17:14:22 +0000 | [diff] [blame] | 3641 | /* The condition below could use some explanation. |
| 3642 | It appears that tp_new is not inherited for static types |
| 3643 | whose base class is 'object'; this seems to be a precaution |
| 3644 | so that old extension types don't suddenly become |
| 3645 | callable (object.__new__ wouldn't insure the invariants |
| 3646 | that the extension type's own factory function ensures). |
| 3647 | Heap types, of course, are under our control, so they do |
| 3648 | inherit tp_new; static extension types that specify some |
| 3649 | other built-in type as the default are considered |
| 3650 | new-style-aware so they also inherit object.__new__. */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3651 | if (base != &PyBaseObject_Type || |
| 3652 | (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 3653 | if (type->tp_new == NULL) |
| 3654 | type->tp_new = base->tp_new; |
| 3655 | } |
| 3656 | } |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 3657 | type->tp_basicsize = newsize; |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3658 | |
| 3659 | /* Copy other non-function slots */ |
| 3660 | |
| 3661 | #undef COPYVAL |
| 3662 | #define COPYVAL(SLOT) \ |
| 3663 | if (type->SLOT == 0) type->SLOT = base->SLOT |
| 3664 | |
| 3665 | COPYVAL(tp_itemsize); |
| 3666 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) { |
| 3667 | COPYVAL(tp_weaklistoffset); |
| 3668 | } |
| 3669 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
| 3670 | COPYVAL(tp_dictoffset); |
| 3671 | } |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 3672 | |
| 3673 | /* Setup fast subclass flags */ |
| 3674 | if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException)) |
| 3675 | type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS; |
| 3676 | else if (PyType_IsSubtype(base, &PyType_Type)) |
| 3677 | type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS; |
| 3678 | else if (PyType_IsSubtype(base, &PyInt_Type)) |
| 3679 | type->tp_flags |= Py_TPFLAGS_INT_SUBCLASS; |
| 3680 | else if (PyType_IsSubtype(base, &PyLong_Type)) |
| 3681 | type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3682 | else if (PyType_IsSubtype(base, &PyString_Type)) |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 3683 | type->tp_flags |= Py_TPFLAGS_STRING_SUBCLASS; |
Georg Brandl | dfe5dc8 | 2008-01-07 18:16:36 +0000 | [diff] [blame] | 3684 | #ifdef Py_USING_UNICODE |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 3685 | else if (PyType_IsSubtype(base, &PyUnicode_Type)) |
| 3686 | type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS; |
Georg Brandl | dfe5dc8 | 2008-01-07 18:16:36 +0000 | [diff] [blame] | 3687 | #endif |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 3688 | else if (PyType_IsSubtype(base, &PyTuple_Type)) |
| 3689 | type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS; |
| 3690 | else if (PyType_IsSubtype(base, &PyList_Type)) |
| 3691 | type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS; |
| 3692 | else if (PyType_IsSubtype(base, &PyDict_Type)) |
| 3693 | type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3694 | } |
| 3695 | |
Nick Coghlan | 48361f5 | 2008-08-11 15:45:58 +0000 | [diff] [blame] | 3696 | static int |
| 3697 | overrides_name(PyTypeObject *type, char *name) |
| 3698 | { |
| 3699 | PyObject *dict = type->tp_dict; |
| 3700 | |
| 3701 | assert(dict != NULL); |
| 3702 | if (PyDict_GetItemString(dict, name) != NULL) { |
| 3703 | return 1; |
| 3704 | } |
| 3705 | return 0; |
| 3706 | } |
| 3707 | |
| 3708 | #define OVERRIDES_HASH(x) overrides_name(x, "__hash__") |
| 3709 | #define OVERRIDES_CMP(x) overrides_name(x, "__cmp__") |
| 3710 | #define OVERRIDES_EQ(x) overrides_name(x, "__eq__") |
| 3711 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3712 | static void |
| 3713 | inherit_slots(PyTypeObject *type, PyTypeObject *base) |
| 3714 | { |
| 3715 | PyTypeObject *basebase; |
| 3716 | |
| 3717 | #undef SLOTDEFINED |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3718 | #undef COPYSLOT |
| 3719 | #undef COPYNUM |
| 3720 | #undef COPYSEQ |
| 3721 | #undef COPYMAP |
Guido van Rossum | 5af588b | 2001-10-12 14:13:21 +0000 | [diff] [blame] | 3722 | #undef COPYBUF |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3723 | |
| 3724 | #define SLOTDEFINED(SLOT) \ |
| 3725 | (base->SLOT != 0 && \ |
| 3726 | (basebase == NULL || base->SLOT != basebase->SLOT)) |
| 3727 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3728 | #define COPYSLOT(SLOT) \ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3729 | if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3730 | |
| 3731 | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT) |
| 3732 | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT) |
| 3733 | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT) |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3734 | #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3735 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3736 | /* This won't inherit indirect slots (from tp_as_number etc.) |
| 3737 | if type doesn't provide the space. */ |
| 3738 | |
| 3739 | if (type->tp_as_number != NULL && base->tp_as_number != NULL) { |
| 3740 | basebase = base->tp_base; |
| 3741 | if (basebase->tp_as_number == NULL) |
| 3742 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3743 | COPYNUM(nb_add); |
| 3744 | COPYNUM(nb_subtract); |
| 3745 | COPYNUM(nb_multiply); |
| 3746 | COPYNUM(nb_divide); |
| 3747 | COPYNUM(nb_remainder); |
| 3748 | COPYNUM(nb_divmod); |
| 3749 | COPYNUM(nb_power); |
| 3750 | COPYNUM(nb_negative); |
| 3751 | COPYNUM(nb_positive); |
| 3752 | COPYNUM(nb_absolute); |
| 3753 | COPYNUM(nb_nonzero); |
| 3754 | COPYNUM(nb_invert); |
| 3755 | COPYNUM(nb_lshift); |
| 3756 | COPYNUM(nb_rshift); |
| 3757 | COPYNUM(nb_and); |
| 3758 | COPYNUM(nb_xor); |
| 3759 | COPYNUM(nb_or); |
| 3760 | COPYNUM(nb_coerce); |
| 3761 | COPYNUM(nb_int); |
| 3762 | COPYNUM(nb_long); |
| 3763 | COPYNUM(nb_float); |
| 3764 | COPYNUM(nb_oct); |
| 3765 | COPYNUM(nb_hex); |
| 3766 | COPYNUM(nb_inplace_add); |
| 3767 | COPYNUM(nb_inplace_subtract); |
| 3768 | COPYNUM(nb_inplace_multiply); |
| 3769 | COPYNUM(nb_inplace_divide); |
| 3770 | COPYNUM(nb_inplace_remainder); |
| 3771 | COPYNUM(nb_inplace_power); |
| 3772 | COPYNUM(nb_inplace_lshift); |
| 3773 | COPYNUM(nb_inplace_rshift); |
| 3774 | COPYNUM(nb_inplace_and); |
| 3775 | COPYNUM(nb_inplace_xor); |
| 3776 | COPYNUM(nb_inplace_or); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3777 | if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) { |
| 3778 | COPYNUM(nb_true_divide); |
| 3779 | COPYNUM(nb_floor_divide); |
| 3780 | COPYNUM(nb_inplace_true_divide); |
| 3781 | COPYNUM(nb_inplace_floor_divide); |
| 3782 | } |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 3783 | if (base->tp_flags & Py_TPFLAGS_HAVE_INDEX) { |
| 3784 | COPYNUM(nb_index); |
| 3785 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3786 | } |
| 3787 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3788 | if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) { |
| 3789 | basebase = base->tp_base; |
| 3790 | if (basebase->tp_as_sequence == NULL) |
| 3791 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3792 | COPYSEQ(sq_length); |
| 3793 | COPYSEQ(sq_concat); |
| 3794 | COPYSEQ(sq_repeat); |
| 3795 | COPYSEQ(sq_item); |
| 3796 | COPYSEQ(sq_slice); |
| 3797 | COPYSEQ(sq_ass_item); |
| 3798 | COPYSEQ(sq_ass_slice); |
| 3799 | COPYSEQ(sq_contains); |
| 3800 | COPYSEQ(sq_inplace_concat); |
| 3801 | COPYSEQ(sq_inplace_repeat); |
| 3802 | } |
| 3803 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3804 | if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) { |
| 3805 | basebase = base->tp_base; |
| 3806 | if (basebase->tp_as_mapping == NULL) |
| 3807 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3808 | COPYMAP(mp_length); |
| 3809 | COPYMAP(mp_subscript); |
| 3810 | COPYMAP(mp_ass_subscript); |
| 3811 | } |
| 3812 | |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3813 | if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) { |
| 3814 | basebase = base->tp_base; |
| 3815 | if (basebase->tp_as_buffer == NULL) |
| 3816 | basebase = NULL; |
| 3817 | COPYBUF(bf_getreadbuffer); |
| 3818 | COPYBUF(bf_getwritebuffer); |
| 3819 | COPYBUF(bf_getsegcount); |
| 3820 | COPYBUF(bf_getcharbuffer); |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 3821 | COPYBUF(bf_getbuffer); |
| 3822 | COPYBUF(bf_releasebuffer); |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3823 | } |
| 3824 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3825 | basebase = base->tp_base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3826 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3827 | COPYSLOT(tp_dealloc); |
| 3828 | COPYSLOT(tp_print); |
| 3829 | if (type->tp_getattr == NULL && type->tp_getattro == NULL) { |
| 3830 | type->tp_getattr = base->tp_getattr; |
| 3831 | type->tp_getattro = base->tp_getattro; |
| 3832 | } |
| 3833 | if (type->tp_setattr == NULL && type->tp_setattro == NULL) { |
| 3834 | type->tp_setattr = base->tp_setattr; |
| 3835 | type->tp_setattro = base->tp_setattro; |
| 3836 | } |
| 3837 | /* tp_compare see tp_richcompare */ |
| 3838 | COPYSLOT(tp_repr); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3839 | /* tp_hash see tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3840 | COPYSLOT(tp_call); |
| 3841 | COPYSLOT(tp_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3842 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3843 | if (type->tp_compare == NULL && |
| 3844 | type->tp_richcompare == NULL && |
Nick Coghlan | 53663a6 | 2008-07-15 14:27:37 +0000 | [diff] [blame] | 3845 | type->tp_hash == NULL) |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3846 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3847 | type->tp_compare = base->tp_compare; |
| 3848 | type->tp_richcompare = base->tp_richcompare; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3849 | type->tp_hash = base->tp_hash; |
Nick Coghlan | 48361f5 | 2008-08-11 15:45:58 +0000 | [diff] [blame] | 3850 | /* Check for changes to inherited methods in Py3k*/ |
| 3851 | if (Py_Py3kWarningFlag) { |
| 3852 | if (base->tp_hash && |
| 3853 | (base->tp_hash != PyObject_HashNotImplemented) && |
| 3854 | !OVERRIDES_HASH(type)) { |
| 3855 | if (OVERRIDES_CMP(type)) { |
| 3856 | PyErr_WarnPy3k("Overriding " |
| 3857 | "__cmp__ blocks inheritance " |
| 3858 | "of __hash__ in 3.x", |
| 3859 | 1); |
| 3860 | } |
| 3861 | if (OVERRIDES_EQ(type)) { |
| 3862 | PyErr_WarnPy3k("Overriding " |
| 3863 | "__eq__ blocks inheritance " |
| 3864 | "of __hash__ in 3.x", |
| 3865 | 1); |
| 3866 | } |
| 3867 | } |
| 3868 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3869 | } |
| 3870 | } |
| 3871 | else { |
| 3872 | COPYSLOT(tp_compare); |
| 3873 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3874 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) { |
| 3875 | COPYSLOT(tp_iter); |
| 3876 | COPYSLOT(tp_iternext); |
| 3877 | } |
| 3878 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
| 3879 | COPYSLOT(tp_descr_get); |
| 3880 | COPYSLOT(tp_descr_set); |
| 3881 | COPYSLOT(tp_dictoffset); |
| 3882 | COPYSLOT(tp_init); |
| 3883 | COPYSLOT(tp_alloc); |
Guido van Rossum | cc8fe04 | 2002-04-05 17:10:16 +0000 | [diff] [blame] | 3884 | COPYSLOT(tp_is_gc); |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 3885 | if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 3886 | (base->tp_flags & Py_TPFLAGS_HAVE_GC)) { |
| 3887 | /* They agree about gc. */ |
| 3888 | COPYSLOT(tp_free); |
| 3889 | } |
| 3890 | else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3891 | type->tp_free == NULL && |
| 3892 | base->tp_free == _PyObject_Del) { |
| 3893 | /* A bit of magic to plug in the correct default |
| 3894 | * tp_free function when a derived class adds gc, |
| 3895 | * didn't define tp_free, and the base uses the |
| 3896 | * default non-gc tp_free. |
| 3897 | */ |
| 3898 | type->tp_free = PyObject_GC_Del; |
| 3899 | } |
| 3900 | /* else they didn't agree about gc, and there isn't something |
| 3901 | * obvious to be done -- the type is on its own. |
| 3902 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3903 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3904 | } |
| 3905 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 3906 | static int add_operators(PyTypeObject *); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3907 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3908 | int |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 3909 | PyType_Ready(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3910 | { |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 3911 | PyObject *dict, *bases; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3912 | PyTypeObject *base; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3913 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3914 | |
Guido van Rossum | cab0580 | 2002-06-10 15:29:03 +0000 | [diff] [blame] | 3915 | if (type->tp_flags & Py_TPFLAGS_READY) { |
| 3916 | assert(type->tp_dict != NULL); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3917 | return 0; |
Guido van Rossum | cab0580 | 2002-06-10 15:29:03 +0000 | [diff] [blame] | 3918 | } |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3919 | assert((type->tp_flags & Py_TPFLAGS_READYING) == 0); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3920 | |
| 3921 | type->tp_flags |= Py_TPFLAGS_READYING; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3922 | |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 3923 | #ifdef Py_TRACE_REFS |
| 3924 | /* PyType_Ready is the closest thing we have to a choke point |
| 3925 | * for type objects, so is the best place I can think of to try |
| 3926 | * to get type objects into the doubly-linked list of all objects. |
| 3927 | * Still, not all type objects go thru PyType_Ready. |
| 3928 | */ |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 3929 | _Py_AddToAllObjects((PyObject *)type, 0); |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 3930 | #endif |
| 3931 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3932 | /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
| 3933 | base = type->tp_base; |
Martin v. Löwis | bf60875 | 2004-08-18 13:16:54 +0000 | [diff] [blame] | 3934 | if (base == NULL && type != &PyBaseObject_Type) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3935 | base = type->tp_base = &PyBaseObject_Type; |
Martin v. Löwis | bf60875 | 2004-08-18 13:16:54 +0000 | [diff] [blame] | 3936 | Py_INCREF(base); |
| 3937 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3938 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3939 | /* Now the only way base can still be NULL is if type is |
| 3940 | * &PyBaseObject_Type. |
| 3941 | */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 3942 | |
Guido van Rossum | 323a9cf | 2002-08-14 17:26:30 +0000 | [diff] [blame] | 3943 | /* Initialize the base class */ |
| 3944 | if (base && base->tp_dict == NULL) { |
| 3945 | if (PyType_Ready(base) < 0) |
| 3946 | goto error; |
| 3947 | } |
| 3948 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3949 | /* 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] | 3950 | compilable separately on Windows can call PyType_Ready() instead of |
| 3951 | initializing the ob_type field of their type objects. */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3952 | /* The test for base != NULL is really unnecessary, since base is only |
| 3953 | NULL when type is &PyBaseObject_Type, and we know its ob_type is |
| 3954 | not NULL (it's initialized to &PyType_Type). But coverity doesn't |
| 3955 | know that. */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 3956 | if (Py_TYPE(type) == NULL && base != NULL) |
| 3957 | Py_TYPE(type) = Py_TYPE(base); |
Guido van Rossum | 0986d82 | 2002-04-08 01:38:42 +0000 | [diff] [blame] | 3958 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3959 | /* Initialize tp_bases */ |
| 3960 | bases = type->tp_bases; |
| 3961 | if (bases == NULL) { |
| 3962 | if (base == NULL) |
| 3963 | bases = PyTuple_New(0); |
| 3964 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3965 | bases = PyTuple_Pack(1, base); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3966 | if (bases == NULL) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3967 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3968 | type->tp_bases = bases; |
| 3969 | } |
| 3970 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3971 | /* Initialize tp_dict */ |
| 3972 | dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3973 | if (dict == NULL) { |
| 3974 | dict = PyDict_New(); |
| 3975 | if (dict == NULL) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3976 | goto error; |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3977 | type->tp_dict = dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3978 | } |
| 3979 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3980 | /* Add type-specific descriptors to tp_dict */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3981 | if (add_operators(type) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3982 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3983 | if (type->tp_methods != NULL) { |
| 3984 | if (add_methods(type, type->tp_methods) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3985 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3986 | } |
| 3987 | if (type->tp_members != NULL) { |
| 3988 | if (add_members(type, type->tp_members) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3989 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3990 | } |
| 3991 | if (type->tp_getset != NULL) { |
| 3992 | if (add_getset(type, type->tp_getset) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3993 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3994 | } |
| 3995 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3996 | /* Calculate method resolution order */ |
| 3997 | if (mro_internal(type) < 0) { |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3998 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3999 | } |
| 4000 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4001 | /* Inherit special flags from dominant base */ |
| 4002 | if (type->tp_base != NULL) |
| 4003 | inherit_special(type, type->tp_base); |
| 4004 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4005 | /* Initialize tp_dict properly */ |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 4006 | bases = type->tp_mro; |
| 4007 | assert(bases != NULL); |
| 4008 | assert(PyTuple_Check(bases)); |
| 4009 | n = PyTuple_GET_SIZE(bases); |
| 4010 | for (i = 1; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 4011 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 4012 | if (PyType_Check(b)) |
| 4013 | inherit_slots(type, (PyTypeObject *)b); |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4014 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4015 | |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 4016 | /* Sanity check for tp_free. */ |
| 4017 | if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) && |
| 4018 | (type->tp_free == NULL || type->tp_free == PyObject_Del)) { |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 4019 | /* This base class needs to call tp_free, but doesn't have |
| 4020 | * one, or its tp_free is for non-gc'ed objects. |
| 4021 | */ |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 4022 | PyErr_Format(PyExc_TypeError, "type '%.100s' participates in " |
| 4023 | "gc and is a base type but has inappropriate " |
| 4024 | "tp_free slot", |
| 4025 | type->tp_name); |
| 4026 | goto error; |
| 4027 | } |
| 4028 | |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 4029 | /* if the type dictionary doesn't contain a __doc__, set it from |
| 4030 | the tp_doc slot. |
| 4031 | */ |
| 4032 | if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) { |
| 4033 | if (type->tp_doc != NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4034 | PyObject *doc = PyString_FromString(type->tp_doc); |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 4035 | if (doc == NULL) |
| 4036 | goto error; |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 4037 | PyDict_SetItemString(type->tp_dict, "__doc__", doc); |
| 4038 | Py_DECREF(doc); |
| 4039 | } else { |
Guido van Rossum | d464107 | 2002-04-03 02:13:37 +0000 | [diff] [blame] | 4040 | PyDict_SetItemString(type->tp_dict, |
| 4041 | "__doc__", Py_None); |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 4042 | } |
| 4043 | } |
| 4044 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4045 | /* Some more special stuff */ |
| 4046 | base = type->tp_base; |
| 4047 | if (base != NULL) { |
| 4048 | if (type->tp_as_number == NULL) |
| 4049 | type->tp_as_number = base->tp_as_number; |
| 4050 | if (type->tp_as_sequence == NULL) |
| 4051 | type->tp_as_sequence = base->tp_as_sequence; |
| 4052 | if (type->tp_as_mapping == NULL) |
| 4053 | type->tp_as_mapping = base->tp_as_mapping; |
Guido van Rossum | eea4718 | 2003-02-11 20:39:59 +0000 | [diff] [blame] | 4054 | if (type->tp_as_buffer == NULL) |
| 4055 | type->tp_as_buffer = base->tp_as_buffer; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4056 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4057 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4058 | /* Link into each base class's list of subclasses */ |
| 4059 | bases = type->tp_bases; |
| 4060 | n = PyTuple_GET_SIZE(bases); |
| 4061 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 4062 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 4063 | if (PyType_Check(b) && |
| 4064 | add_subclass((PyTypeObject *)b, type) < 0) |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4065 | goto error; |
| 4066 | } |
| 4067 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4068 | /* All done -- set the ready flag */ |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 4069 | assert(type->tp_dict != NULL); |
| 4070 | type->tp_flags = |
| 4071 | (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4072 | return 0; |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 4073 | |
| 4074 | error: |
| 4075 | type->tp_flags &= ~Py_TPFLAGS_READYING; |
| 4076 | return -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4077 | } |
| 4078 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4079 | static int |
| 4080 | add_subclass(PyTypeObject *base, PyTypeObject *type) |
| 4081 | { |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 4082 | Py_ssize_t i; |
| 4083 | int result; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 4084 | PyObject *list, *ref, *newobj; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4085 | |
| 4086 | list = base->tp_subclasses; |
| 4087 | if (list == NULL) { |
| 4088 | base->tp_subclasses = list = PyList_New(0); |
| 4089 | if (list == NULL) |
| 4090 | return -1; |
| 4091 | } |
| 4092 | assert(PyList_Check(list)); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 4093 | newobj = PyWeakref_NewRef((PyObject *)type, NULL); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4094 | i = PyList_GET_SIZE(list); |
| 4095 | while (--i >= 0) { |
| 4096 | ref = PyList_GET_ITEM(list, i); |
| 4097 | assert(PyWeakref_CheckRef(ref)); |
Guido van Rossum | 3930bc3 | 2002-10-18 13:51:49 +0000 | [diff] [blame] | 4098 | if (PyWeakref_GET_OBJECT(ref) == Py_None) |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 4099 | return PyList_SetItem(list, i, newobj); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4100 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 4101 | result = PyList_Append(list, newobj); |
| 4102 | Py_DECREF(newobj); |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 4103 | return result; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4104 | } |
| 4105 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4106 | static void |
| 4107 | remove_subclass(PyTypeObject *base, PyTypeObject *type) |
| 4108 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4109 | Py_ssize_t i; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4110 | PyObject *list, *ref; |
| 4111 | |
| 4112 | list = base->tp_subclasses; |
| 4113 | if (list == NULL) { |
| 4114 | return; |
| 4115 | } |
| 4116 | assert(PyList_Check(list)); |
| 4117 | i = PyList_GET_SIZE(list); |
| 4118 | while (--i >= 0) { |
| 4119 | ref = PyList_GET_ITEM(list, i); |
| 4120 | assert(PyWeakref_CheckRef(ref)); |
| 4121 | if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) { |
| 4122 | /* this can't fail, right? */ |
| 4123 | PySequence_DelItem(list, i); |
| 4124 | return; |
| 4125 | } |
| 4126 | } |
| 4127 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4128 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4129 | static int |
| 4130 | check_num_args(PyObject *ob, int n) |
| 4131 | { |
| 4132 | if (!PyTuple_CheckExact(ob)) { |
| 4133 | PyErr_SetString(PyExc_SystemError, |
| 4134 | "PyArg_UnpackTuple() argument list is not a tuple"); |
| 4135 | return 0; |
| 4136 | } |
| 4137 | if (n == PyTuple_GET_SIZE(ob)) |
| 4138 | return 1; |
| 4139 | PyErr_Format( |
| 4140 | PyExc_TypeError, |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 4141 | "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob)); |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4142 | return 0; |
| 4143 | } |
| 4144 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4145 | /* Generic wrappers for overloadable 'operators' such as __getitem__ */ |
| 4146 | |
| 4147 | /* There's a wrapper *function* for each distinct function typedef used |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 4148 | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4149 | wrapper *table* for each distinct operation (e.g. __len__, __add__). |
| 4150 | Most tables have only one entry; the tables for binary operators have two |
| 4151 | entries, one regular and one with reversed arguments. */ |
| 4152 | |
| 4153 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4154 | wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4155 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4156 | lenfunc func = (lenfunc)wrapped; |
| 4157 | Py_ssize_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4158 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4159 | if (!check_num_args(args, 0)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4160 | return NULL; |
| 4161 | res = (*func)(self); |
| 4162 | if (res == -1 && PyErr_Occurred()) |
| 4163 | return NULL; |
| 4164 | return PyInt_FromLong((long)res); |
| 4165 | } |
| 4166 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4167 | static PyObject * |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4168 | wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped) |
| 4169 | { |
| 4170 | inquiry func = (inquiry)wrapped; |
| 4171 | int res; |
| 4172 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4173 | if (!check_num_args(args, 0)) |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4174 | return NULL; |
| 4175 | res = (*func)(self); |
| 4176 | if (res == -1 && PyErr_Occurred()) |
| 4177 | return NULL; |
| 4178 | return PyBool_FromLong((long)res); |
| 4179 | } |
| 4180 | |
| 4181 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4182 | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4183 | { |
| 4184 | binaryfunc func = (binaryfunc)wrapped; |
| 4185 | PyObject *other; |
| 4186 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4187 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4188 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4189 | other = PyTuple_GET_ITEM(args, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4190 | return (*func)(self, other); |
| 4191 | } |
| 4192 | |
| 4193 | static PyObject * |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4194 | wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped) |
| 4195 | { |
| 4196 | binaryfunc func = (binaryfunc)wrapped; |
| 4197 | PyObject *other; |
| 4198 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4199 | if (!check_num_args(args, 1)) |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4200 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4201 | other = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4202 | if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) && |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4203 | !PyType_IsSubtype(other->ob_type, self->ob_type)) { |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4204 | Py_INCREF(Py_NotImplemented); |
| 4205 | return Py_NotImplemented; |
| 4206 | } |
| 4207 | return (*func)(self, other); |
| 4208 | } |
| 4209 | |
| 4210 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4211 | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4212 | { |
| 4213 | binaryfunc func = (binaryfunc)wrapped; |
| 4214 | PyObject *other; |
| 4215 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4216 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4217 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4218 | other = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4219 | if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) && |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4220 | !PyType_IsSubtype(other->ob_type, self->ob_type)) { |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4221 | Py_INCREF(Py_NotImplemented); |
| 4222 | return Py_NotImplemented; |
| 4223 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4224 | return (*func)(other, self); |
| 4225 | } |
| 4226 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4227 | static PyObject * |
| 4228 | wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped) |
| 4229 | { |
| 4230 | coercion func = (coercion)wrapped; |
| 4231 | PyObject *other, *res; |
| 4232 | int ok; |
| 4233 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4234 | if (!check_num_args(args, 1)) |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4235 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4236 | other = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4237 | ok = func(&self, &other); |
| 4238 | if (ok < 0) |
| 4239 | return NULL; |
| 4240 | if (ok > 0) { |
| 4241 | Py_INCREF(Py_NotImplemented); |
| 4242 | return Py_NotImplemented; |
| 4243 | } |
| 4244 | res = PyTuple_New(2); |
| 4245 | if (res == NULL) { |
| 4246 | Py_DECREF(self); |
| 4247 | Py_DECREF(other); |
| 4248 | return NULL; |
| 4249 | } |
| 4250 | PyTuple_SET_ITEM(res, 0, self); |
| 4251 | PyTuple_SET_ITEM(res, 1, other); |
| 4252 | return res; |
| 4253 | } |
| 4254 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4255 | static PyObject * |
| 4256 | wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4257 | { |
| 4258 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4259 | PyObject *other; |
| 4260 | PyObject *third = Py_None; |
| 4261 | |
| 4262 | /* Note: This wrapper only works for __pow__() */ |
| 4263 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4264 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4265 | return NULL; |
| 4266 | return (*func)(self, other, third); |
| 4267 | } |
| 4268 | |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4269 | static PyObject * |
| 4270 | wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4271 | { |
| 4272 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4273 | PyObject *other; |
| 4274 | PyObject *third = Py_None; |
| 4275 | |
| 4276 | /* Note: This wrapper only works for __pow__() */ |
| 4277 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4278 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4279 | return NULL; |
| 4280 | return (*func)(other, self, third); |
| 4281 | } |
| 4282 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4283 | static PyObject * |
| 4284 | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4285 | { |
| 4286 | unaryfunc func = (unaryfunc)wrapped; |
| 4287 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4288 | if (!check_num_args(args, 0)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4289 | return NULL; |
| 4290 | return (*func)(self); |
| 4291 | } |
| 4292 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4293 | static PyObject * |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 4294 | wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4295 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4296 | ssizeargfunc func = (ssizeargfunc)wrapped; |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 4297 | PyObject* o; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4298 | Py_ssize_t i; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4299 | |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 4300 | if (!PyArg_UnpackTuple(args, "", 1, 1, &o)) |
| 4301 | return NULL; |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 4302 | i = PyNumber_AsSsize_t(o, PyExc_OverflowError); |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 4303 | if (i == -1 && PyErr_Occurred()) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4304 | return NULL; |
| 4305 | return (*func)(self, i); |
| 4306 | } |
| 4307 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4308 | static Py_ssize_t |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4309 | getindex(PyObject *self, PyObject *arg) |
| 4310 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4311 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4312 | |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 4313 | i = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4314 | if (i == -1 && PyErr_Occurred()) |
| 4315 | return -1; |
| 4316 | if (i < 0) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4317 | PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4318 | if (sq && sq->sq_length) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4319 | Py_ssize_t n = (*sq->sq_length)(self); |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4320 | if (n < 0) |
| 4321 | return -1; |
| 4322 | i += n; |
| 4323 | } |
| 4324 | } |
| 4325 | return i; |
| 4326 | } |
| 4327 | |
| 4328 | static PyObject * |
| 4329 | wrap_sq_item(PyObject *self, PyObject *args, void *wrapped) |
| 4330 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4331 | ssizeargfunc func = (ssizeargfunc)wrapped; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4332 | PyObject *arg; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4333 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4334 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4335 | if (PyTuple_GET_SIZE(args) == 1) { |
| 4336 | arg = PyTuple_GET_ITEM(args, 0); |
| 4337 | i = getindex(self, arg); |
| 4338 | if (i == -1 && PyErr_Occurred()) |
| 4339 | return NULL; |
| 4340 | return (*func)(self, i); |
| 4341 | } |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4342 | check_num_args(args, 1); |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4343 | assert(PyErr_Occurred()); |
| 4344 | return NULL; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4345 | } |
| 4346 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4347 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4348 | wrap_ssizessizeargfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4349 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4350 | ssizessizeargfunc func = (ssizessizeargfunc)wrapped; |
| 4351 | Py_ssize_t i, j; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4352 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4353 | if (!PyArg_ParseTuple(args, "nn", &i, &j)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4354 | return NULL; |
| 4355 | return (*func)(self, i, j); |
| 4356 | } |
| 4357 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4358 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4359 | wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4360 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4361 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4362 | Py_ssize_t i; |
| 4363 | int res; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4364 | PyObject *arg, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4365 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4366 | if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value)) |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4367 | return NULL; |
| 4368 | i = getindex(self, arg); |
| 4369 | if (i == -1 && PyErr_Occurred()) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4370 | return NULL; |
| 4371 | res = (*func)(self, i, value); |
| 4372 | if (res == -1 && PyErr_Occurred()) |
| 4373 | return NULL; |
| 4374 | Py_INCREF(Py_None); |
| 4375 | return Py_None; |
| 4376 | } |
| 4377 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4378 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4379 | wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4380 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4381 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4382 | Py_ssize_t i; |
| 4383 | int res; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4384 | PyObject *arg; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4385 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4386 | if (!check_num_args(args, 1)) |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4387 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4388 | arg = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4389 | i = getindex(self, arg); |
| 4390 | if (i == -1 && PyErr_Occurred()) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4391 | return NULL; |
| 4392 | res = (*func)(self, i, NULL); |
| 4393 | if (res == -1 && PyErr_Occurred()) |
| 4394 | return NULL; |
| 4395 | Py_INCREF(Py_None); |
| 4396 | return Py_None; |
| 4397 | } |
| 4398 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4399 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4400 | wrap_ssizessizeobjargproc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4401 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4402 | ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped; |
| 4403 | Py_ssize_t i, j; |
| 4404 | int res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4405 | PyObject *value; |
| 4406 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4407 | if (!PyArg_ParseTuple(args, "nnO", &i, &j, &value)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4408 | return NULL; |
| 4409 | res = (*func)(self, i, j, value); |
| 4410 | if (res == -1 && PyErr_Occurred()) |
| 4411 | return NULL; |
| 4412 | Py_INCREF(Py_None); |
| 4413 | return Py_None; |
| 4414 | } |
| 4415 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4416 | static PyObject * |
| 4417 | wrap_delslice(PyObject *self, PyObject *args, void *wrapped) |
| 4418 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4419 | ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped; |
| 4420 | Py_ssize_t i, j; |
| 4421 | int res; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4422 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4423 | if (!PyArg_ParseTuple(args, "nn", &i, &j)) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4424 | return NULL; |
| 4425 | res = (*func)(self, i, j, NULL); |
| 4426 | if (res == -1 && PyErr_Occurred()) |
| 4427 | return NULL; |
| 4428 | Py_INCREF(Py_None); |
| 4429 | return Py_None; |
| 4430 | } |
| 4431 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4432 | /* XXX objobjproc is a misnomer; should be objargpred */ |
| 4433 | static PyObject * |
| 4434 | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) |
| 4435 | { |
| 4436 | objobjproc func = (objobjproc)wrapped; |
| 4437 | int res; |
Guido van Rossum | 22c3dda | 2003-10-09 03:46:35 +0000 | [diff] [blame] | 4438 | PyObject *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4439 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4440 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4441 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4442 | value = PyTuple_GET_ITEM(args, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4443 | res = (*func)(self, value); |
| 4444 | if (res == -1 && PyErr_Occurred()) |
| 4445 | return NULL; |
Guido van Rossum | 22c3dda | 2003-10-09 03:46:35 +0000 | [diff] [blame] | 4446 | else |
| 4447 | return PyBool_FromLong(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4448 | } |
| 4449 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4450 | static PyObject * |
| 4451 | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 4452 | { |
| 4453 | objobjargproc func = (objobjargproc)wrapped; |
| 4454 | int res; |
| 4455 | PyObject *key, *value; |
| 4456 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4457 | if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4458 | return NULL; |
| 4459 | res = (*func)(self, key, value); |
| 4460 | if (res == -1 && PyErr_Occurred()) |
| 4461 | return NULL; |
| 4462 | Py_INCREF(Py_None); |
| 4463 | return Py_None; |
| 4464 | } |
| 4465 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4466 | static PyObject * |
| 4467 | wrap_delitem(PyObject *self, PyObject *args, void *wrapped) |
| 4468 | { |
| 4469 | objobjargproc func = (objobjargproc)wrapped; |
| 4470 | int res; |
| 4471 | PyObject *key; |
| 4472 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4473 | if (!check_num_args(args, 1)) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4474 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4475 | key = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4476 | res = (*func)(self, key, NULL); |
| 4477 | if (res == -1 && PyErr_Occurred()) |
| 4478 | return NULL; |
| 4479 | Py_INCREF(Py_None); |
| 4480 | return Py_None; |
| 4481 | } |
| 4482 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4483 | static PyObject * |
| 4484 | wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4485 | { |
| 4486 | cmpfunc func = (cmpfunc)wrapped; |
| 4487 | int res; |
| 4488 | PyObject *other; |
| 4489 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4490 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4491 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4492 | other = PyTuple_GET_ITEM(args, 0); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4493 | if (Py_TYPE(other)->tp_compare != func && |
| 4494 | !PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) { |
Guido van Rossum | ceccae5 | 2001-09-18 20:03:57 +0000 | [diff] [blame] | 4495 | PyErr_Format( |
| 4496 | PyExc_TypeError, |
| 4497 | "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4498 | Py_TYPE(self)->tp_name, |
| 4499 | Py_TYPE(self)->tp_name, |
| 4500 | Py_TYPE(other)->tp_name); |
Guido van Rossum | ceccae5 | 2001-09-18 20:03:57 +0000 | [diff] [blame] | 4501 | return NULL; |
| 4502 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4503 | res = (*func)(self, other); |
| 4504 | if (PyErr_Occurred()) |
| 4505 | return NULL; |
| 4506 | return PyInt_FromLong((long)res); |
| 4507 | } |
| 4508 | |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4509 | /* 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] | 4510 | This is called the Carlo Verre hack after its discoverer. */ |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4511 | static int |
| 4512 | hackcheck(PyObject *self, setattrofunc func, char *what) |
| 4513 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4514 | PyTypeObject *type = Py_TYPE(self); |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4515 | while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 4516 | type = type->tp_base; |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 4517 | /* If type is NULL now, this is a really weird type. |
| 4518 | In the spirit of backwards compatibility (?), just shut up. */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 4519 | if (type && type->tp_setattro != func) { |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4520 | PyErr_Format(PyExc_TypeError, |
| 4521 | "can't apply this %s to %s object", |
| 4522 | what, |
| 4523 | type->tp_name); |
| 4524 | return 0; |
| 4525 | } |
| 4526 | return 1; |
| 4527 | } |
| 4528 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4529 | static PyObject * |
| 4530 | wrap_setattr(PyObject *self, PyObject *args, void *wrapped) |
| 4531 | { |
| 4532 | setattrofunc func = (setattrofunc)wrapped; |
| 4533 | int res; |
| 4534 | PyObject *name, *value; |
| 4535 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4536 | if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4537 | return NULL; |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4538 | if (!hackcheck(self, func, "__setattr__")) |
| 4539 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4540 | res = (*func)(self, name, value); |
| 4541 | if (res < 0) |
| 4542 | return NULL; |
| 4543 | Py_INCREF(Py_None); |
| 4544 | return Py_None; |
| 4545 | } |
| 4546 | |
| 4547 | static PyObject * |
| 4548 | wrap_delattr(PyObject *self, PyObject *args, void *wrapped) |
| 4549 | { |
| 4550 | setattrofunc func = (setattrofunc)wrapped; |
| 4551 | int res; |
| 4552 | PyObject *name; |
| 4553 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4554 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4555 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4556 | name = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4557 | if (!hackcheck(self, func, "__delattr__")) |
| 4558 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4559 | res = (*func)(self, name, NULL); |
| 4560 | if (res < 0) |
| 4561 | return NULL; |
| 4562 | Py_INCREF(Py_None); |
| 4563 | return Py_None; |
| 4564 | } |
| 4565 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4566 | static PyObject * |
| 4567 | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4568 | { |
| 4569 | hashfunc func = (hashfunc)wrapped; |
| 4570 | long res; |
| 4571 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4572 | if (!check_num_args(args, 0)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4573 | return NULL; |
| 4574 | res = (*func)(self); |
| 4575 | if (res == -1 && PyErr_Occurred()) |
| 4576 | return NULL; |
| 4577 | return PyInt_FromLong(res); |
| 4578 | } |
| 4579 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4580 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4581 | wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4582 | { |
| 4583 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4584 | |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4585 | return (*func)(self, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4586 | } |
| 4587 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4588 | static PyObject * |
| 4589 | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) |
| 4590 | { |
| 4591 | richcmpfunc func = (richcmpfunc)wrapped; |
| 4592 | PyObject *other; |
| 4593 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4594 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4595 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4596 | other = PyTuple_GET_ITEM(args, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4597 | return (*func)(self, other, op); |
| 4598 | } |
| 4599 | |
| 4600 | #undef RICHCMP_WRAPPER |
| 4601 | #define RICHCMP_WRAPPER(NAME, OP) \ |
| 4602 | static PyObject * \ |
| 4603 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ |
| 4604 | { \ |
| 4605 | return wrap_richcmpfunc(self, args, wrapped, OP); \ |
| 4606 | } |
| 4607 | |
Jack Jansen | 8e938b4 | 2001-08-08 15:29:49 +0000 | [diff] [blame] | 4608 | RICHCMP_WRAPPER(lt, Py_LT) |
| 4609 | RICHCMP_WRAPPER(le, Py_LE) |
| 4610 | RICHCMP_WRAPPER(eq, Py_EQ) |
| 4611 | RICHCMP_WRAPPER(ne, Py_NE) |
| 4612 | RICHCMP_WRAPPER(gt, Py_GT) |
| 4613 | RICHCMP_WRAPPER(ge, Py_GE) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4614 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4615 | static PyObject * |
| 4616 | wrap_next(PyObject *self, PyObject *args, void *wrapped) |
| 4617 | { |
| 4618 | unaryfunc func = (unaryfunc)wrapped; |
| 4619 | PyObject *res; |
| 4620 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4621 | if (!check_num_args(args, 0)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4622 | return NULL; |
| 4623 | res = (*func)(self); |
| 4624 | if (res == NULL && !PyErr_Occurred()) |
| 4625 | PyErr_SetNone(PyExc_StopIteration); |
| 4626 | return res; |
| 4627 | } |
| 4628 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4629 | static PyObject * |
| 4630 | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped) |
| 4631 | { |
| 4632 | descrgetfunc func = (descrgetfunc)wrapped; |
| 4633 | PyObject *obj; |
| 4634 | PyObject *type = NULL; |
| 4635 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4636 | if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4637 | return NULL; |
Guido van Rossum | 82ed25c | 2003-02-11 16:25:43 +0000 | [diff] [blame] | 4638 | if (obj == Py_None) |
| 4639 | obj = NULL; |
| 4640 | if (type == Py_None) |
| 4641 | type = NULL; |
| 4642 | if (type == NULL &&obj == NULL) { |
| 4643 | PyErr_SetString(PyExc_TypeError, |
| 4644 | "__get__(None, None) is invalid"); |
| 4645 | return NULL; |
| 4646 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4647 | return (*func)(self, obj, type); |
| 4648 | } |
| 4649 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4650 | static PyObject * |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4651 | wrap_descr_set(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4652 | { |
| 4653 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4654 | PyObject *obj, *value; |
| 4655 | int ret; |
| 4656 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4657 | if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4658 | return NULL; |
| 4659 | ret = (*func)(self, obj, value); |
| 4660 | if (ret < 0) |
| 4661 | return NULL; |
| 4662 | Py_INCREF(Py_None); |
| 4663 | return Py_None; |
| 4664 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 4665 | |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4666 | static PyObject * |
| 4667 | wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped) |
| 4668 | { |
| 4669 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4670 | PyObject *obj; |
| 4671 | int ret; |
| 4672 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4673 | if (!check_num_args(args, 1)) |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4674 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4675 | obj = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4676 | ret = (*func)(self, obj, NULL); |
| 4677 | if (ret < 0) |
| 4678 | return NULL; |
| 4679 | Py_INCREF(Py_None); |
| 4680 | return Py_None; |
| 4681 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4682 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4683 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4684 | wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4685 | { |
| 4686 | initproc func = (initproc)wrapped; |
| 4687 | |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4688 | if (func(self, args, kwds) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4689 | return NULL; |
| 4690 | Py_INCREF(Py_None); |
| 4691 | return Py_None; |
| 4692 | } |
| 4693 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4694 | static PyObject * |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4695 | tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4696 | { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4697 | PyTypeObject *type, *subtype, *staticbase; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4698 | PyObject *arg0, *res; |
| 4699 | |
| 4700 | if (self == NULL || !PyType_Check(self)) |
| 4701 | Py_FatalError("__new__() called with non-type 'self'"); |
| 4702 | type = (PyTypeObject *)self; |
| 4703 | if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4704 | PyErr_Format(PyExc_TypeError, |
| 4705 | "%s.__new__(): not enough arguments", |
| 4706 | type->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4707 | return NULL; |
| 4708 | } |
| 4709 | arg0 = PyTuple_GET_ITEM(args, 0); |
| 4710 | if (!PyType_Check(arg0)) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4711 | PyErr_Format(PyExc_TypeError, |
| 4712 | "%s.__new__(X): X is not a type object (%s)", |
| 4713 | type->tp_name, |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4714 | Py_TYPE(arg0)->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4715 | return NULL; |
| 4716 | } |
| 4717 | subtype = (PyTypeObject *)arg0; |
| 4718 | if (!PyType_IsSubtype(subtype, type)) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4719 | PyErr_Format(PyExc_TypeError, |
| 4720 | "%s.__new__(%s): %s is not a subtype of %s", |
| 4721 | type->tp_name, |
| 4722 | subtype->tp_name, |
| 4723 | subtype->tp_name, |
| 4724 | type->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4725 | return NULL; |
| 4726 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4727 | |
| 4728 | /* Check that the use doesn't do something silly and unsafe like |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 4729 | object.__new__(dict). To do this, we check that the |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4730 | most derived base that's not a heap type is this type. */ |
| 4731 | staticbase = subtype; |
| 4732 | while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 4733 | staticbase = staticbase->tp_base; |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 4734 | /* If staticbase is NULL now, it is a really weird type. |
| 4735 | In the spirit of backwards compatibility (?), just shut up. */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 4736 | if (staticbase && staticbase->tp_new != type->tp_new) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4737 | PyErr_Format(PyExc_TypeError, |
| 4738 | "%s.__new__(%s) is not safe, use %s.__new__()", |
| 4739 | type->tp_name, |
| 4740 | subtype->tp_name, |
| 4741 | staticbase == NULL ? "?" : staticbase->tp_name); |
| 4742 | return NULL; |
| 4743 | } |
| 4744 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4745 | args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 4746 | if (args == NULL) |
| 4747 | return NULL; |
| 4748 | res = type->tp_new(subtype, args, kwds); |
| 4749 | Py_DECREF(args); |
| 4750 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4751 | } |
| 4752 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4753 | static struct PyMethodDef tp_new_methoddef[] = { |
Neal Norwitz | a84dcd7 | 2007-05-22 07:16:44 +0000 | [diff] [blame] | 4754 | {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 4755 | PyDoc_STR("T.__new__(S, ...) -> " |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 4756 | "a new object with type S, a subtype of T")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4757 | {0} |
| 4758 | }; |
| 4759 | |
| 4760 | static int |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4761 | add_tp_new_wrapper(PyTypeObject *type) |
| 4762 | { |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4763 | PyObject *func; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4764 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 4765 | if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL) |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4766 | return 0; |
| 4767 | func = PyCFunction_New(tp_new_methoddef, (PyObject *)type); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4768 | if (func == NULL) |
| 4769 | return -1; |
Raymond Hettinger | 8d726ee | 2004-06-25 22:24:35 +0000 | [diff] [blame] | 4770 | if (PyDict_SetItemString(type->tp_dict, "__new__", func)) { |
Raymond Hettinger | d56cbe5 | 2004-06-25 22:17:39 +0000 | [diff] [blame] | 4771 | Py_DECREF(func); |
| 4772 | return -1; |
| 4773 | } |
| 4774 | Py_DECREF(func); |
| 4775 | return 0; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4776 | } |
| 4777 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4778 | /* Slot wrappers that call the corresponding __foo__ slot. See comments |
| 4779 | below at override_slots() for more explanation. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4780 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4781 | #define SLOT0(FUNCNAME, OPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4782 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4783 | FUNCNAME(PyObject *self) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4784 | { \ |
Guido van Rossum | 5592e4d | 2001-08-28 18:28:21 +0000 | [diff] [blame] | 4785 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4786 | return call_method(self, OPSTR, &cache_str, "()"); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4787 | } |
| 4788 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4789 | #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4790 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4791 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4792 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4793 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4794 | return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4795 | } |
| 4796 | |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4797 | /* Boolean helper for SLOT1BINFULL(). |
| 4798 | right.__class__ is a nontrivial subclass of left.__class__. */ |
| 4799 | static int |
| 4800 | method_is_overloaded(PyObject *left, PyObject *right, char *name) |
| 4801 | { |
| 4802 | PyObject *a, *b; |
| 4803 | int ok; |
| 4804 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4805 | b = PyObject_GetAttrString((PyObject *)(Py_TYPE(right)), name); |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4806 | if (b == NULL) { |
| 4807 | PyErr_Clear(); |
| 4808 | /* If right doesn't have it, it's not overloaded */ |
| 4809 | return 0; |
| 4810 | } |
| 4811 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4812 | a = PyObject_GetAttrString((PyObject *)(Py_TYPE(left)), name); |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4813 | if (a == NULL) { |
| 4814 | PyErr_Clear(); |
| 4815 | Py_DECREF(b); |
| 4816 | /* If right has it but left doesn't, it's overloaded */ |
| 4817 | return 1; |
| 4818 | } |
| 4819 | |
| 4820 | ok = PyObject_RichCompareBool(a, b, Py_NE); |
| 4821 | Py_DECREF(a); |
| 4822 | Py_DECREF(b); |
| 4823 | if (ok < 0) { |
| 4824 | PyErr_Clear(); |
| 4825 | return 0; |
| 4826 | } |
| 4827 | |
| 4828 | return ok; |
| 4829 | } |
| 4830 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4831 | |
| 4832 | #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4833 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4834 | FUNCNAME(PyObject *self, PyObject *other) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4835 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4836 | static PyObject *cache_str, *rcache_str; \ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4837 | int do_other = Py_TYPE(self) != Py_TYPE(other) && \ |
| 4838 | Py_TYPE(other)->tp_as_number != NULL && \ |
| 4839 | Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \ |
| 4840 | if (Py_TYPE(self)->tp_as_number != NULL && \ |
| 4841 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4842 | PyObject *r; \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4843 | if (do_other && \ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4844 | PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \ |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4845 | method_is_overloaded(self, other, ROPSTR)) { \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4846 | r = call_maybe( \ |
| 4847 | other, ROPSTR, &rcache_str, "(O)", self); \ |
| 4848 | if (r != Py_NotImplemented) \ |
| 4849 | return r; \ |
| 4850 | Py_DECREF(r); \ |
| 4851 | do_other = 0; \ |
| 4852 | } \ |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 4853 | r = call_maybe( \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4854 | self, OPSTR, &cache_str, "(O)", other); \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4855 | if (r != Py_NotImplemented || \ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4856 | Py_TYPE(other) == Py_TYPE(self)) \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4857 | return r; \ |
| 4858 | Py_DECREF(r); \ |
| 4859 | } \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4860 | if (do_other) { \ |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 4861 | return call_maybe( \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4862 | other, ROPSTR, &rcache_str, "(O)", self); \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4863 | } \ |
| 4864 | Py_INCREF(Py_NotImplemented); \ |
| 4865 | return Py_NotImplemented; \ |
| 4866 | } |
| 4867 | |
| 4868 | #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ |
| 4869 | SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR) |
| 4870 | |
| 4871 | #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \ |
| 4872 | static PyObject * \ |
| 4873 | FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ |
| 4874 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4875 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4876 | return call_method(self, OPSTR, &cache_str, \ |
| 4877 | "(" ARGCODES ")", arg1, arg2); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4878 | } |
| 4879 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4880 | static Py_ssize_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4881 | slot_sq_length(PyObject *self) |
| 4882 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4883 | static PyObject *len_str; |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4884 | PyObject *res = call_method(self, "__len__", &len_str, "()"); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4885 | Py_ssize_t len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4886 | |
| 4887 | if (res == NULL) |
| 4888 | return -1; |
Neal Norwitz | 1872b1c | 2006-08-12 18:44:06 +0000 | [diff] [blame] | 4889 | len = PyInt_AsSsize_t(res); |
Guido van Rossum | 2611162 | 2001-10-01 16:42:49 +0000 | [diff] [blame] | 4890 | Py_DECREF(res); |
Jeremy Hylton | f20fcf9 | 2002-07-25 16:06:15 +0000 | [diff] [blame] | 4891 | if (len < 0) { |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 4892 | if (!PyErr_Occurred()) |
| 4893 | PyErr_SetString(PyExc_ValueError, |
| 4894 | "__len__() should return >= 0"); |
Jeremy Hylton | f20fcf9 | 2002-07-25 16:06:15 +0000 | [diff] [blame] | 4895 | return -1; |
| 4896 | } |
Guido van Rossum | 2611162 | 2001-10-01 16:42:49 +0000 | [diff] [blame] | 4897 | return len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4898 | } |
| 4899 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4900 | /* Super-optimized version of slot_sq_item. |
| 4901 | Other slots could do the same... */ |
| 4902 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4903 | slot_sq_item(PyObject *self, Py_ssize_t i) |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4904 | { |
| 4905 | static PyObject *getitem_str; |
| 4906 | PyObject *func, *args = NULL, *ival = NULL, *retval = NULL; |
| 4907 | descrgetfunc f; |
| 4908 | |
| 4909 | if (getitem_str == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4910 | getitem_str = PyString_InternFromString("__getitem__"); |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4911 | if (getitem_str == NULL) |
| 4912 | return NULL; |
| 4913 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4914 | func = _PyType_Lookup(Py_TYPE(self), getitem_str); |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4915 | if (func != NULL) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4916 | if ((f = Py_TYPE(func)->tp_descr_get) == NULL) |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4917 | Py_INCREF(func); |
Neal Norwitz | 673cd82 | 2002-10-18 16:33:13 +0000 | [diff] [blame] | 4918 | else { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4919 | func = f(func, self, (PyObject *)(Py_TYPE(self))); |
Neal Norwitz | 673cd82 | 2002-10-18 16:33:13 +0000 | [diff] [blame] | 4920 | if (func == NULL) { |
| 4921 | return NULL; |
| 4922 | } |
| 4923 | } |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 4924 | ival = PyInt_FromSsize_t(i); |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4925 | if (ival != NULL) { |
| 4926 | args = PyTuple_New(1); |
| 4927 | if (args != NULL) { |
| 4928 | PyTuple_SET_ITEM(args, 0, ival); |
| 4929 | retval = PyObject_Call(func, args, NULL); |
| 4930 | Py_XDECREF(args); |
| 4931 | Py_XDECREF(func); |
| 4932 | return retval; |
| 4933 | } |
| 4934 | } |
| 4935 | } |
| 4936 | else { |
| 4937 | PyErr_SetObject(PyExc_AttributeError, getitem_str); |
| 4938 | } |
| 4939 | Py_XDECREF(args); |
| 4940 | Py_XDECREF(ival); |
| 4941 | Py_XDECREF(func); |
| 4942 | return NULL; |
| 4943 | } |
| 4944 | |
Benjamin Peterson | 712ee92 | 2008-08-24 18:10:20 +0000 | [diff] [blame] | 4945 | static PyObject* |
| 4946 | slot_sq_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j) |
| 4947 | { |
| 4948 | static PyObject *getslice_str; |
| 4949 | |
| 4950 | if (PyErr_WarnPy3k("in 3.x, __getslice__ has been removed; " |
| 4951 | "use __getitem__", 1) < 0) |
| 4952 | return NULL; |
| 4953 | return call_method(self, "__getslice__", &getslice_str, |
| 4954 | "nn", i, j); |
| 4955 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4956 | |
| 4957 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4958 | slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4959 | { |
| 4960 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4961 | static PyObject *delitem_str, *setitem_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4962 | |
| 4963 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4964 | res = call_method(self, "__delitem__", &delitem_str, |
Thomas Wouters | 4e90810 | 2006-04-21 11:26:56 +0000 | [diff] [blame] | 4965 | "(n)", index); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4966 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4967 | res = call_method(self, "__setitem__", &setitem_str, |
Thomas Wouters | 4e90810 | 2006-04-21 11:26:56 +0000 | [diff] [blame] | 4968 | "(nO)", index, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4969 | if (res == NULL) |
| 4970 | return -1; |
| 4971 | Py_DECREF(res); |
| 4972 | return 0; |
| 4973 | } |
| 4974 | |
| 4975 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4976 | slot_sq_ass_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j, PyObject *value) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4977 | { |
| 4978 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4979 | static PyObject *delslice_str, *setslice_str; |
Benjamin Peterson | 712ee92 | 2008-08-24 18:10:20 +0000 | [diff] [blame] | 4980 | |
| 4981 | if (value == NULL) { |
| 4982 | if (PyErr_WarnPy3k("in 3.x, __delslice__ has been removed; " |
| 4983 | "use __delitem__", 1) < 0) |
| 4984 | return -1; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4985 | res = call_method(self, "__delslice__", &delslice_str, |
Thomas Wouters | 4e90810 | 2006-04-21 11:26:56 +0000 | [diff] [blame] | 4986 | "(nn)", i, j); |
Benjamin Peterson | 712ee92 | 2008-08-24 18:10:20 +0000 | [diff] [blame] | 4987 | } |
| 4988 | else { |
| 4989 | if (PyErr_WarnPy3k("in 3.x, __setslice__ has been removed; " |
| 4990 | "use __setitem__", 1) < 0) |
| 4991 | return -1; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4992 | res = call_method(self, "__setslice__", &setslice_str, |
Benjamin Peterson | 712ee92 | 2008-08-24 18:10:20 +0000 | [diff] [blame] | 4993 | "(nnO)", i, j, value); |
| 4994 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4995 | if (res == NULL) |
| 4996 | return -1; |
| 4997 | Py_DECREF(res); |
| 4998 | return 0; |
| 4999 | } |
| 5000 | |
| 5001 | static int |
| 5002 | slot_sq_contains(PyObject *self, PyObject *value) |
| 5003 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5004 | PyObject *func, *res, *args; |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5005 | int result = -1; |
| 5006 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5007 | static PyObject *contains_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5008 | |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 5009 | func = lookup_maybe(self, "__contains__", &contains_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5010 | if (func != NULL) { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 5011 | args = PyTuple_Pack(1, value); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5012 | if (args == NULL) |
| 5013 | res = NULL; |
| 5014 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5015 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5016 | Py_DECREF(args); |
| 5017 | } |
| 5018 | Py_DECREF(func); |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5019 | if (res != NULL) { |
| 5020 | result = PyObject_IsTrue(res); |
| 5021 | Py_DECREF(res); |
| 5022 | } |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5023 | } |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5024 | else if (! PyErr_Occurred()) { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 5025 | /* Possible results: -1 and 1 */ |
| 5026 | result = (int)_PySequence_IterSearch(self, value, |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5027 | PY_ITERSEARCH_CONTAINS); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5028 | } |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5029 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5030 | } |
| 5031 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5032 | #define slot_mp_length slot_sq_length |
| 5033 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5034 | SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5035 | |
| 5036 | static int |
| 5037 | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) |
| 5038 | { |
| 5039 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5040 | static PyObject *delitem_str, *setitem_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5041 | |
| 5042 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5043 | res = call_method(self, "__delitem__", &delitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5044 | "(O)", key); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5045 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5046 | res = call_method(self, "__setitem__", &setitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5047 | "(OO)", key, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5048 | if (res == NULL) |
| 5049 | return -1; |
| 5050 | Py_DECREF(res); |
| 5051 | return 0; |
| 5052 | } |
| 5053 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5054 | SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__") |
| 5055 | SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__") |
| 5056 | SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__") |
| 5057 | SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__") |
| 5058 | SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__") |
| 5059 | SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__") |
| 5060 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 5061 | static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5062 | |
| 5063 | SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, |
| 5064 | nb_power, "__pow__", "__rpow__") |
| 5065 | |
| 5066 | static PyObject * |
| 5067 | slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) |
| 5068 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5069 | static PyObject *pow_str; |
| 5070 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5071 | if (modulus == Py_None) |
| 5072 | return slot_nb_power_binary(self, other); |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 5073 | /* Three-arg power doesn't use __rpow__. But ternary_op |
| 5074 | can call this when the second argument's type uses |
| 5075 | slot_nb_power, so check before calling self.__pow__. */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5076 | if (Py_TYPE(self)->tp_as_number != NULL && |
| 5077 | Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) { |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 5078 | return call_method(self, "__pow__", &pow_str, |
| 5079 | "(OO)", other, modulus); |
| 5080 | } |
| 5081 | Py_INCREF(Py_NotImplemented); |
| 5082 | return Py_NotImplemented; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5083 | } |
| 5084 | |
| 5085 | SLOT0(slot_nb_negative, "__neg__") |
| 5086 | SLOT0(slot_nb_positive, "__pos__") |
| 5087 | SLOT0(slot_nb_absolute, "__abs__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5088 | |
| 5089 | static int |
| 5090 | slot_nb_nonzero(PyObject *self) |
| 5091 | { |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 5092 | PyObject *func, *args; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5093 | static PyObject *nonzero_str, *len_str; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 5094 | int result = -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5095 | |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 5096 | func = lookup_maybe(self, "__nonzero__", &nonzero_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5097 | if (func == NULL) { |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 5098 | if (PyErr_Occurred()) |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5099 | return -1; |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 5100 | func = lookup_maybe(self, "__len__", &len_str); |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 5101 | if (func == NULL) |
| 5102 | return PyErr_Occurred() ? -1 : 1; |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5103 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 5104 | args = PyTuple_New(0); |
| 5105 | if (args != NULL) { |
| 5106 | PyObject *temp = PyObject_Call(func, args, NULL); |
| 5107 | Py_DECREF(args); |
| 5108 | if (temp != NULL) { |
Jeremy Hylton | 3e3159c | 2003-06-27 17:38:27 +0000 | [diff] [blame] | 5109 | if (PyInt_CheckExact(temp) || PyBool_Check(temp)) |
Jeremy Hylton | 090a349 | 2003-06-27 16:46:45 +0000 | [diff] [blame] | 5110 | result = PyObject_IsTrue(temp); |
| 5111 | else { |
| 5112 | PyErr_Format(PyExc_TypeError, |
| 5113 | "__nonzero__ should return " |
| 5114 | "bool or int, returned %s", |
| 5115 | temp->ob_type->tp_name); |
Jeremy Hylton | 3e3159c | 2003-06-27 17:38:27 +0000 | [diff] [blame] | 5116 | result = -1; |
Jeremy Hylton | 090a349 | 2003-06-27 16:46:45 +0000 | [diff] [blame] | 5117 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 5118 | Py_DECREF(temp); |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 5119 | } |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5120 | } |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 5121 | Py_DECREF(func); |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 5122 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5123 | } |
| 5124 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5125 | |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 5126 | static PyObject * |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5127 | slot_nb_index(PyObject *self) |
| 5128 | { |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5129 | static PyObject *index_str; |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 5130 | return call_method(self, "__index__", &index_str, "()"); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5131 | } |
| 5132 | |
| 5133 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5134 | SLOT0(slot_nb_invert, "__invert__") |
| 5135 | SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") |
| 5136 | SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") |
| 5137 | SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") |
| 5138 | SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") |
| 5139 | SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5140 | |
| 5141 | static int |
| 5142 | slot_nb_coerce(PyObject **a, PyObject **b) |
| 5143 | { |
| 5144 | static PyObject *coerce_str; |
| 5145 | PyObject *self = *a, *other = *b; |
| 5146 | |
| 5147 | if (self->ob_type->tp_as_number != NULL && |
| 5148 | self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) { |
| 5149 | PyObject *r; |
| 5150 | r = call_maybe( |
| 5151 | self, "__coerce__", &coerce_str, "(O)", other); |
| 5152 | if (r == NULL) |
| 5153 | return -1; |
| 5154 | if (r == Py_NotImplemented) { |
| 5155 | Py_DECREF(r); |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5156 | } |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 5157 | else { |
| 5158 | if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) { |
| 5159 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5160 | "__coerce__ didn't return a 2-tuple"); |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 5161 | Py_DECREF(r); |
| 5162 | return -1; |
| 5163 | } |
| 5164 | *a = PyTuple_GET_ITEM(r, 0); |
| 5165 | Py_INCREF(*a); |
| 5166 | *b = PyTuple_GET_ITEM(r, 1); |
| 5167 | Py_INCREF(*b); |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5168 | Py_DECREF(r); |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 5169 | return 0; |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5170 | } |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5171 | } |
| 5172 | if (other->ob_type->tp_as_number != NULL && |
| 5173 | other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) { |
| 5174 | PyObject *r; |
| 5175 | r = call_maybe( |
| 5176 | other, "__coerce__", &coerce_str, "(O)", self); |
| 5177 | if (r == NULL) |
| 5178 | return -1; |
| 5179 | if (r == Py_NotImplemented) { |
| 5180 | Py_DECREF(r); |
| 5181 | return 1; |
| 5182 | } |
| 5183 | if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) { |
| 5184 | PyErr_SetString(PyExc_TypeError, |
| 5185 | "__coerce__ didn't return a 2-tuple"); |
| 5186 | Py_DECREF(r); |
| 5187 | return -1; |
| 5188 | } |
| 5189 | *a = PyTuple_GET_ITEM(r, 1); |
| 5190 | Py_INCREF(*a); |
| 5191 | *b = PyTuple_GET_ITEM(r, 0); |
| 5192 | Py_INCREF(*b); |
| 5193 | Py_DECREF(r); |
| 5194 | return 0; |
| 5195 | } |
| 5196 | return 1; |
| 5197 | } |
| 5198 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5199 | SLOT0(slot_nb_int, "__int__") |
| 5200 | SLOT0(slot_nb_long, "__long__") |
| 5201 | SLOT0(slot_nb_float, "__float__") |
| 5202 | SLOT0(slot_nb_oct, "__oct__") |
| 5203 | SLOT0(slot_nb_hex, "__hex__") |
| 5204 | SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O") |
| 5205 | SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O") |
| 5206 | SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O") |
| 5207 | SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O") |
| 5208 | SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O") |
Martin v. Löwis | fd96326 | 2007-02-09 12:19:32 +0000 | [diff] [blame] | 5209 | /* Can't use SLOT1 here, because nb_inplace_power is ternary */ |
| 5210 | static PyObject * |
| 5211 | slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2) |
| 5212 | { |
| 5213 | static PyObject *cache_str; |
| 5214 | return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1); |
| 5215 | } |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5216 | SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O") |
| 5217 | SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O") |
| 5218 | SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O") |
| 5219 | SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O") |
| 5220 | SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O") |
| 5221 | SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, |
| 5222 | "__floordiv__", "__rfloordiv__") |
| 5223 | SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__") |
| 5224 | SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O") |
| 5225 | SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5226 | |
| 5227 | static int |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5228 | half_compare(PyObject *self, PyObject *other) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5229 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5230 | PyObject *func, *args, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5231 | static PyObject *cmp_str; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5232 | Py_ssize_t c; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5233 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5234 | func = lookup_method(self, "__cmp__", &cmp_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5235 | if (func == NULL) { |
| 5236 | PyErr_Clear(); |
| 5237 | } |
| 5238 | else { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 5239 | args = PyTuple_Pack(1, other); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5240 | if (args == NULL) |
| 5241 | res = NULL; |
| 5242 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5243 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5244 | Py_DECREF(args); |
| 5245 | } |
Raymond Hettinger | ab5dae3 | 2002-06-24 13:08:16 +0000 | [diff] [blame] | 5246 | Py_DECREF(func); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5247 | if (res != Py_NotImplemented) { |
| 5248 | if (res == NULL) |
| 5249 | return -2; |
| 5250 | c = PyInt_AsLong(res); |
| 5251 | Py_DECREF(res); |
| 5252 | if (c == -1 && PyErr_Occurred()) |
| 5253 | return -2; |
| 5254 | return (c < 0) ? -1 : (c > 0) ? 1 : 0; |
| 5255 | } |
| 5256 | Py_DECREF(res); |
| 5257 | } |
| 5258 | return 2; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5259 | } |
| 5260 | |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 5261 | /* This slot is published for the benefit of try_3way_compare in object.c */ |
| 5262 | int |
| 5263 | _PyObject_SlotCompare(PyObject *self, PyObject *other) |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5264 | { |
| 5265 | int c; |
| 5266 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5267 | if (Py_TYPE(self)->tp_compare == _PyObject_SlotCompare) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5268 | c = half_compare(self, other); |
| 5269 | if (c <= 1) |
| 5270 | return c; |
| 5271 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5272 | if (Py_TYPE(other)->tp_compare == _PyObject_SlotCompare) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5273 | c = half_compare(other, self); |
| 5274 | if (c < -1) |
| 5275 | return -2; |
| 5276 | if (c <= 1) |
| 5277 | return -c; |
| 5278 | } |
| 5279 | return (void *)self < (void *)other ? -1 : |
| 5280 | (void *)self > (void *)other ? 1 : 0; |
| 5281 | } |
| 5282 | |
| 5283 | static PyObject * |
| 5284 | slot_tp_repr(PyObject *self) |
| 5285 | { |
| 5286 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5287 | static PyObject *repr_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5288 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5289 | func = lookup_method(self, "__repr__", &repr_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5290 | if (func != NULL) { |
| 5291 | res = PyEval_CallObject(func, NULL); |
| 5292 | Py_DECREF(func); |
| 5293 | return res; |
| 5294 | } |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 5295 | PyErr_Clear(); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 5296 | return PyString_FromFormat("<%s object at %p>", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5297 | Py_TYPE(self)->tp_name, self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5298 | } |
| 5299 | |
| 5300 | static PyObject * |
| 5301 | slot_tp_str(PyObject *self) |
| 5302 | { |
| 5303 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5304 | static PyObject *str_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5305 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5306 | func = lookup_method(self, "__str__", &str_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5307 | if (func != NULL) { |
| 5308 | res = PyEval_CallObject(func, NULL); |
| 5309 | Py_DECREF(func); |
| 5310 | return res; |
| 5311 | } |
| 5312 | else { |
| 5313 | PyErr_Clear(); |
| 5314 | return slot_tp_repr(self); |
| 5315 | } |
| 5316 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5317 | |
| 5318 | static long |
| 5319 | slot_tp_hash(PyObject *self) |
| 5320 | { |
Tim Peters | 61ce0a9 | 2002-12-06 23:38:02 +0000 | [diff] [blame] | 5321 | PyObject *func; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5322 | static PyObject *hash_str, *eq_str, *cmp_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5323 | long h; |
| 5324 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5325 | func = lookup_method(self, "__hash__", &hash_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5326 | |
Guido van Rossum | 0b7b6fd | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 5327 | if (func != NULL && func != Py_None) { |
Tim Peters | 61ce0a9 | 2002-12-06 23:38:02 +0000 | [diff] [blame] | 5328 | PyObject *res = PyEval_CallObject(func, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5329 | Py_DECREF(func); |
| 5330 | if (res == NULL) |
| 5331 | return -1; |
Martin v. Löwis | ab2f8f7 | 2006-08-09 07:57:39 +0000 | [diff] [blame] | 5332 | if (PyLong_Check(res)) |
Armin Rigo | 51fc8c4 | 2006-08-09 14:55:26 +0000 | [diff] [blame] | 5333 | h = PyLong_Type.tp_hash(res); |
Martin v. Löwis | ab2f8f7 | 2006-08-09 07:57:39 +0000 | [diff] [blame] | 5334 | else |
| 5335 | h = PyInt_AsLong(res); |
Tim Peters | 61ce0a9 | 2002-12-06 23:38:02 +0000 | [diff] [blame] | 5336 | Py_DECREF(res); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5337 | } |
| 5338 | else { |
Georg Brandl | 30b7804 | 2007-12-20 21:03:02 +0000 | [diff] [blame] | 5339 | Py_XDECREF(func); /* may be None */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5340 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5341 | func = lookup_method(self, "__eq__", &eq_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5342 | if (func == NULL) { |
| 5343 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5344 | func = lookup_method(self, "__cmp__", &cmp_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5345 | } |
| 5346 | if (func != NULL) { |
| 5347 | Py_DECREF(func); |
Nick Coghlan | 53663a6 | 2008-07-15 14:27:37 +0000 | [diff] [blame] | 5348 | return PyObject_HashNotImplemented(self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5349 | } |
| 5350 | PyErr_Clear(); |
| 5351 | h = _Py_HashPointer((void *)self); |
| 5352 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5353 | if (h == -1 && !PyErr_Occurred()) |
| 5354 | h = -2; |
| 5355 | return h; |
| 5356 | } |
| 5357 | |
| 5358 | static PyObject * |
| 5359 | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 5360 | { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5361 | static PyObject *call_str; |
| 5362 | PyObject *meth = lookup_method(self, "__call__", &call_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5363 | PyObject *res; |
| 5364 | |
| 5365 | if (meth == NULL) |
| 5366 | return NULL; |
Armin Rigo | 53c1692f | 2006-06-21 21:58:50 +0000 | [diff] [blame] | 5367 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5368 | res = PyObject_Call(meth, args, kwds); |
Armin Rigo | 53c1692f | 2006-06-21 21:58:50 +0000 | [diff] [blame] | 5369 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5370 | Py_DECREF(meth); |
| 5371 | return res; |
| 5372 | } |
| 5373 | |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5374 | /* There are two slot dispatch functions for tp_getattro. |
| 5375 | |
| 5376 | - slot_tp_getattro() is used when __getattribute__ is overridden |
| 5377 | but no __getattr__ hook is present; |
| 5378 | |
| 5379 | - slot_tp_getattr_hook() is used when a __getattr__ hook is present. |
| 5380 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5381 | The code in update_one_slot() always installs slot_tp_getattr_hook(); this |
| 5382 | detects the absence of __getattr__ and then installs the simpler slot if |
| 5383 | necessary. */ |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5384 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5385 | static PyObject * |
| 5386 | slot_tp_getattro(PyObject *self, PyObject *name) |
| 5387 | { |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5388 | static PyObject *getattribute_str = NULL; |
| 5389 | return call_method(self, "__getattribute__", &getattribute_str, |
| 5390 | "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5391 | } |
| 5392 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5393 | static PyObject * |
Benjamin Peterson | 273c233 | 2008-11-17 22:39:09 +0000 | [diff] [blame] | 5394 | call_attribute(PyObject *self, PyObject *attr, PyObject *name) |
| 5395 | { |
| 5396 | PyObject *res, *descr = NULL; |
| 5397 | descrgetfunc f = Py_TYPE(attr)->tp_descr_get; |
| 5398 | |
| 5399 | if (f != NULL) { |
| 5400 | descr = f(attr, self, (PyObject *)(Py_TYPE(self))); |
| 5401 | if (descr == NULL) |
| 5402 | return NULL; |
| 5403 | else |
| 5404 | attr = descr; |
| 5405 | } |
| 5406 | res = PyObject_CallFunctionObjArgs(attr, name, NULL); |
| 5407 | Py_XDECREF(descr); |
| 5408 | return res; |
| 5409 | } |
| 5410 | |
| 5411 | static PyObject * |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5412 | slot_tp_getattr_hook(PyObject *self, PyObject *name) |
| 5413 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5414 | PyTypeObject *tp = Py_TYPE(self); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5415 | PyObject *getattr, *getattribute, *res; |
| 5416 | static PyObject *getattribute_str = NULL; |
| 5417 | static PyObject *getattr_str = NULL; |
| 5418 | |
| 5419 | if (getattr_str == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 5420 | getattr_str = PyString_InternFromString("__getattr__"); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5421 | if (getattr_str == NULL) |
| 5422 | return NULL; |
| 5423 | } |
| 5424 | if (getattribute_str == NULL) { |
| 5425 | getattribute_str = |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 5426 | PyString_InternFromString("__getattribute__"); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5427 | if (getattribute_str == NULL) |
| 5428 | return NULL; |
| 5429 | } |
Benjamin Peterson | 273c233 | 2008-11-17 22:39:09 +0000 | [diff] [blame] | 5430 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5431 | method fully for each attribute lookup for classes with |
| 5432 | __getattr__, even when the attribute is present. So we use |
| 5433 | _PyType_Lookup and create the method only when needed, with |
| 5434 | call_attribute. */ |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5435 | getattr = _PyType_Lookup(tp, getattr_str); |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5436 | if (getattr == NULL) { |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5437 | /* No __getattr__ hook: use a simpler dispatcher */ |
| 5438 | tp->tp_getattro = slot_tp_getattro; |
| 5439 | return slot_tp_getattro(self, name); |
| 5440 | } |
Benjamin Peterson | 273c233 | 2008-11-17 22:39:09 +0000 | [diff] [blame] | 5441 | Py_INCREF(getattr); |
| 5442 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5443 | method fully for each attribute lookup for classes with |
| 5444 | __getattr__, even when self has the default __getattribute__ |
| 5445 | method. So we use _PyType_Lookup and create the method only when |
| 5446 | needed, with call_attribute. */ |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5447 | getattribute = _PyType_Lookup(tp, getattribute_str); |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5448 | if (getattribute == NULL || |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5449 | (Py_TYPE(getattribute) == &PyWrapperDescr_Type && |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5450 | ((PyWrapperDescrObject *)getattribute)->d_wrapped == |
| 5451 | (void *)PyObject_GenericGetAttr)) |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5452 | res = PyObject_GenericGetAttr(self, name); |
Benjamin Peterson | 273c233 | 2008-11-17 22:39:09 +0000 | [diff] [blame] | 5453 | else { |
| 5454 | Py_INCREF(getattribute); |
| 5455 | res = call_attribute(self, getattribute, name); |
| 5456 | Py_DECREF(getattribute); |
| 5457 | } |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5458 | if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5459 | PyErr_Clear(); |
Benjamin Peterson | 273c233 | 2008-11-17 22:39:09 +0000 | [diff] [blame] | 5460 | res = call_attribute(self, getattr, name); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5461 | } |
Benjamin Peterson | 273c233 | 2008-11-17 22:39:09 +0000 | [diff] [blame] | 5462 | Py_DECREF(getattr); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5463 | return res; |
| 5464 | } |
| 5465 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5466 | static int |
| 5467 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) |
| 5468 | { |
| 5469 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5470 | static PyObject *delattr_str, *setattr_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5471 | |
| 5472 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5473 | res = call_method(self, "__delattr__", &delattr_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5474 | "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5475 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5476 | res = call_method(self, "__setattr__", &setattr_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5477 | "(OO)", name, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5478 | if (res == NULL) |
| 5479 | return -1; |
| 5480 | Py_DECREF(res); |
| 5481 | return 0; |
| 5482 | } |
| 5483 | |
Guido van Rossum | 0b7b6fd | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 5484 | static char *name_op[] = { |
| 5485 | "__lt__", |
| 5486 | "__le__", |
| 5487 | "__eq__", |
| 5488 | "__ne__", |
| 5489 | "__gt__", |
| 5490 | "__ge__", |
| 5491 | }; |
| 5492 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5493 | static PyObject * |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5494 | half_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5495 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5496 | PyObject *func, *args, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5497 | static PyObject *op_str[6]; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5498 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5499 | func = lookup_method(self, name_op[op], &op_str[op]); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5500 | if (func == NULL) { |
| 5501 | PyErr_Clear(); |
| 5502 | Py_INCREF(Py_NotImplemented); |
| 5503 | return Py_NotImplemented; |
| 5504 | } |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 5505 | args = PyTuple_Pack(1, other); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5506 | if (args == NULL) |
| 5507 | res = NULL; |
| 5508 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5509 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5510 | Py_DECREF(args); |
| 5511 | } |
| 5512 | Py_DECREF(func); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5513 | return res; |
| 5514 | } |
| 5515 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5516 | static PyObject * |
| 5517 | slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
| 5518 | { |
| 5519 | PyObject *res; |
| 5520 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5521 | if (Py_TYPE(self)->tp_richcompare == slot_tp_richcompare) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5522 | res = half_richcompare(self, other, op); |
| 5523 | if (res != Py_NotImplemented) |
| 5524 | return res; |
| 5525 | Py_DECREF(res); |
| 5526 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5527 | if (Py_TYPE(other)->tp_richcompare == slot_tp_richcompare) { |
Tim Peters | f4aca75 | 2004-09-23 02:39:37 +0000 | [diff] [blame] | 5528 | res = half_richcompare(other, self, _Py_SwappedOp[op]); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5529 | if (res != Py_NotImplemented) { |
| 5530 | return res; |
| 5531 | } |
| 5532 | Py_DECREF(res); |
| 5533 | } |
| 5534 | Py_INCREF(Py_NotImplemented); |
| 5535 | return Py_NotImplemented; |
| 5536 | } |
| 5537 | |
| 5538 | static PyObject * |
| 5539 | slot_tp_iter(PyObject *self) |
| 5540 | { |
| 5541 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5542 | static PyObject *iter_str, *getitem_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5543 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5544 | func = lookup_method(self, "__iter__", &iter_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5545 | if (func != NULL) { |
Guido van Rossum | 84b2bed | 2002-08-16 17:01:09 +0000 | [diff] [blame] | 5546 | PyObject *args; |
| 5547 | args = res = PyTuple_New(0); |
| 5548 | if (args != NULL) { |
| 5549 | res = PyObject_Call(func, args, NULL); |
| 5550 | Py_DECREF(args); |
| 5551 | } |
| 5552 | Py_DECREF(func); |
| 5553 | return res; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5554 | } |
| 5555 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5556 | func = lookup_method(self, "__getitem__", &getitem_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5557 | if (func == NULL) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 5558 | PyErr_Format(PyExc_TypeError, |
| 5559 | "'%.200s' object is not iterable", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5560 | Py_TYPE(self)->tp_name); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5561 | return NULL; |
| 5562 | } |
| 5563 | Py_DECREF(func); |
| 5564 | return PySeqIter_New(self); |
| 5565 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5566 | |
| 5567 | static PyObject * |
| 5568 | slot_tp_iternext(PyObject *self) |
| 5569 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5570 | static PyObject *next_str; |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5571 | return call_method(self, "next", &next_str, "()"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5572 | } |
| 5573 | |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5574 | static PyObject * |
| 5575 | slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 5576 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5577 | PyTypeObject *tp = Py_TYPE(self); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5578 | PyObject *get; |
| 5579 | static PyObject *get_str = NULL; |
| 5580 | |
| 5581 | if (get_str == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 5582 | get_str = PyString_InternFromString("__get__"); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5583 | if (get_str == NULL) |
| 5584 | return NULL; |
| 5585 | } |
| 5586 | get = _PyType_Lookup(tp, get_str); |
| 5587 | if (get == NULL) { |
| 5588 | /* Avoid further slowdowns */ |
| 5589 | if (tp->tp_descr_get == slot_tp_descr_get) |
| 5590 | tp->tp_descr_get = NULL; |
| 5591 | Py_INCREF(self); |
| 5592 | return self; |
| 5593 | } |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5594 | if (obj == NULL) |
| 5595 | obj = Py_None; |
| 5596 | if (type == NULL) |
| 5597 | type = Py_None; |
Georg Brandl | 684fd0c | 2006-05-25 19:15:31 +0000 | [diff] [blame] | 5598 | return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5599 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5600 | |
| 5601 | static int |
| 5602 | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) |
| 5603 | { |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5604 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5605 | static PyObject *del_str, *set_str; |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5606 | |
| 5607 | if (value == NULL) |
Guido van Rossum | 1d5b3f2 | 2001-12-03 00:08:33 +0000 | [diff] [blame] | 5608 | res = call_method(self, "__delete__", &del_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5609 | "(O)", target); |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5610 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5611 | res = call_method(self, "__set__", &set_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5612 | "(OO)", target, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5613 | if (res == NULL) |
| 5614 | return -1; |
| 5615 | Py_DECREF(res); |
| 5616 | return 0; |
| 5617 | } |
| 5618 | |
| 5619 | static int |
| 5620 | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 5621 | { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5622 | static PyObject *init_str; |
| 5623 | PyObject *meth = lookup_method(self, "__init__", &init_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5624 | PyObject *res; |
| 5625 | |
| 5626 | if (meth == NULL) |
| 5627 | return -1; |
| 5628 | res = PyObject_Call(meth, args, kwds); |
| 5629 | Py_DECREF(meth); |
| 5630 | if (res == NULL) |
| 5631 | return -1; |
Raymond Hettinger | b67cc80 | 2005-03-03 16:45:19 +0000 | [diff] [blame] | 5632 | if (res != Py_None) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 5633 | PyErr_Format(PyExc_TypeError, |
| 5634 | "__init__() should return None, not '%.200s'", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5635 | Py_TYPE(res)->tp_name); |
Raymond Hettinger | b67cc80 | 2005-03-03 16:45:19 +0000 | [diff] [blame] | 5636 | Py_DECREF(res); |
| 5637 | return -1; |
| 5638 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5639 | Py_DECREF(res); |
| 5640 | return 0; |
| 5641 | } |
| 5642 | |
| 5643 | static PyObject * |
| 5644 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 5645 | { |
Guido van Rossum | 7bed213 | 2002-08-08 21:57:53 +0000 | [diff] [blame] | 5646 | static PyObject *new_str; |
| 5647 | PyObject *func; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5648 | PyObject *newargs, *x; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5649 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5650 | |
Guido van Rossum | 7bed213 | 2002-08-08 21:57:53 +0000 | [diff] [blame] | 5651 | if (new_str == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 5652 | new_str = PyString_InternFromString("__new__"); |
Guido van Rossum | 7bed213 | 2002-08-08 21:57:53 +0000 | [diff] [blame] | 5653 | if (new_str == NULL) |
| 5654 | return NULL; |
| 5655 | } |
| 5656 | func = PyObject_GetAttr((PyObject *)type, new_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5657 | if (func == NULL) |
| 5658 | return NULL; |
| 5659 | assert(PyTuple_Check(args)); |
| 5660 | n = PyTuple_GET_SIZE(args); |
| 5661 | newargs = PyTuple_New(n+1); |
| 5662 | if (newargs == NULL) |
| 5663 | return NULL; |
| 5664 | Py_INCREF(type); |
| 5665 | PyTuple_SET_ITEM(newargs, 0, (PyObject *)type); |
| 5666 | for (i = 0; i < n; i++) { |
| 5667 | x = PyTuple_GET_ITEM(args, i); |
| 5668 | Py_INCREF(x); |
| 5669 | PyTuple_SET_ITEM(newargs, i+1, x); |
| 5670 | } |
| 5671 | x = PyObject_Call(func, newargs, kwds); |
Guido van Rossum | 25d1807 | 2001-10-01 15:55:28 +0000 | [diff] [blame] | 5672 | Py_DECREF(newargs); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5673 | Py_DECREF(func); |
| 5674 | return x; |
| 5675 | } |
| 5676 | |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5677 | static void |
| 5678 | slot_tp_del(PyObject *self) |
| 5679 | { |
| 5680 | static PyObject *del_str = NULL; |
| 5681 | PyObject *del, *res; |
| 5682 | PyObject *error_type, *error_value, *error_traceback; |
| 5683 | |
| 5684 | /* Temporarily resurrect the object. */ |
| 5685 | assert(self->ob_refcnt == 0); |
| 5686 | self->ob_refcnt = 1; |
| 5687 | |
| 5688 | /* Save the current exception, if any. */ |
| 5689 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 5690 | |
| 5691 | /* Execute __del__ method, if any. */ |
| 5692 | del = lookup_maybe(self, "__del__", &del_str); |
| 5693 | if (del != NULL) { |
| 5694 | res = PyEval_CallObject(del, NULL); |
| 5695 | if (res == NULL) |
| 5696 | PyErr_WriteUnraisable(del); |
| 5697 | else |
| 5698 | Py_DECREF(res); |
| 5699 | Py_DECREF(del); |
| 5700 | } |
| 5701 | |
| 5702 | /* Restore the saved exception. */ |
| 5703 | PyErr_Restore(error_type, error_value, error_traceback); |
| 5704 | |
| 5705 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 5706 | * cause a recursive call. |
| 5707 | */ |
| 5708 | assert(self->ob_refcnt > 0); |
| 5709 | if (--self->ob_refcnt == 0) |
| 5710 | return; /* this is the normal path out */ |
| 5711 | |
| 5712 | /* __del__ resurrected it! Make it look like the original Py_DECREF |
| 5713 | * never happened. |
| 5714 | */ |
| 5715 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 5716 | Py_ssize_t refcnt = self->ob_refcnt; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5717 | _Py_NewReference(self); |
| 5718 | self->ob_refcnt = refcnt; |
| 5719 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5720 | assert(!PyType_IS_GC(Py_TYPE(self)) || |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5721 | _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED); |
Michael W. Hudson | 3f3b668 | 2004-08-03 10:21:03 +0000 | [diff] [blame] | 5722 | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so |
| 5723 | * we need to undo that. */ |
| 5724 | _Py_DEC_REFTOTAL; |
| 5725 | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object |
| 5726 | * chain, so no more to do there. |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5727 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
| 5728 | * _Py_NewReference bumped tp_allocs: both of those need to be |
| 5729 | * undone. |
| 5730 | */ |
| 5731 | #ifdef COUNT_ALLOCS |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5732 | --Py_TYPE(self)->tp_frees; |
| 5733 | --Py_TYPE(self)->tp_allocs; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5734 | #endif |
| 5735 | } |
| 5736 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5737 | |
| 5738 | /* 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] | 5739 | functions. The offsets here are relative to the 'PyHeapTypeObject' |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5740 | structure, which incorporates the additional structures used for numbers, |
| 5741 | sequences and mappings. |
| 5742 | 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] | 5743 | __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] | 5744 | slots (e.g. __str__ affects tp_str as well as tp_repr). The table is |
| 5745 | terminated with an all-zero entry. (This table is further initialized and |
| 5746 | sorted in init_slotdefs() below.) */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5747 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5748 | typedef struct wrapperbase slotdef; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5749 | |
| 5750 | #undef TPSLOT |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5751 | #undef FLSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5752 | #undef ETSLOT |
| 5753 | #undef SQSLOT |
| 5754 | #undef MPSLOT |
| 5755 | #undef NBSLOT |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5756 | #undef UNSLOT |
| 5757 | #undef IBSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5758 | #undef BINSLOT |
| 5759 | #undef RBINSLOT |
| 5760 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5761 | #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Neal Norwitz | d47714a | 2002-08-13 19:01:38 +0000 | [diff] [blame] | 5762 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5763 | PyDoc_STR(DOC)} |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5764 | #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ |
| 5765 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
Neal Norwitz | d47714a | 2002-08-13 19:01:38 +0000 | [diff] [blame] | 5766 | PyDoc_STR(DOC), FLAGS} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5767 | #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5768 | {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
Neal Norwitz | d47714a | 2002-08-13 19:01:38 +0000 | [diff] [blame] | 5769 | PyDoc_STR(DOC)} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5770 | #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5771 | ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) |
| 5772 | #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5773 | ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) |
| 5774 | #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5775 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) |
| 5776 | #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5777 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5778 | "x." NAME "() <==> " DOC) |
| 5779 | #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5780 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5781 | "x." NAME "(y) <==> x" DOC "y") |
| 5782 | #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
| 5783 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5784 | "x." NAME "(y) <==> x" DOC "y") |
| 5785 | #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
| 5786 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5787 | "x." NAME "(y) <==> y" DOC "x") |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5788 | #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
| 5789 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5790 | "x." NAME "(y) <==> " DOC) |
| 5791 | #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
| 5792 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5793 | "x." NAME "(y) <==> " DOC) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5794 | |
| 5795 | static slotdef slotdefs[] = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5796 | SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5797 | "x.__len__() <==> len(x)"), |
Armin Rigo | fd163f9 | 2005-12-29 15:59:19 +0000 | [diff] [blame] | 5798 | /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL. |
| 5799 | The logic in abstract.c always falls back to nb_add/nb_multiply in |
| 5800 | this case. Defining both the nb_* and the sq_* slots to call the |
| 5801 | user-defined methods has unexpected side-effects, as shown by |
| 5802 | test_descr.notimplemented() */ |
| 5803 | SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc, |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5804 | "x.__add__(y) <==> x+y"), |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 5805 | SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc, |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5806 | "x.__mul__(n) <==> x*n"), |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 5807 | SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc, |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5808 | "x.__rmul__(n) <==> n*x"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5809 | SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, |
| 5810 | "x.__getitem__(y) <==> x[y]"), |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5811 | SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_ssizessizeargfunc, |
Brett Cannon | 154da9b | 2003-05-20 02:30:04 +0000 | [diff] [blame] | 5812 | "x.__getslice__(i, j) <==> x[i:j]\n\ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5813 | \n\ |
| 5814 | Use of negative indices is not supported."), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5815 | SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, |
Brett Cannon | be67d87 | 2003-05-20 02:40:12 +0000 | [diff] [blame] | 5816 | "x.__setitem__(i, y) <==> x[i]=y"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5817 | SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, |
Brett Cannon | be67d87 | 2003-05-20 02:40:12 +0000 | [diff] [blame] | 5818 | "x.__delitem__(y) <==> del x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5819 | SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5820 | wrap_ssizessizeobjargproc, |
Brett Cannon | be67d87 | 2003-05-20 02:40:12 +0000 | [diff] [blame] | 5821 | "x.__setslice__(i, j, y) <==> x[i:j]=y\n\ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5822 | \n\ |
| 5823 | Use of negative indices is not supported."), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5824 | SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice, |
Brett Cannon | be67d87 | 2003-05-20 02:40:12 +0000 | [diff] [blame] | 5825 | "x.__delslice__(i, j) <==> del x[i:j]\n\ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5826 | \n\ |
| 5827 | Use of negative indices is not supported."), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5828 | SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, |
| 5829 | "x.__contains__(y) <==> y in x"), |
Armin Rigo | fd163f9 | 2005-12-29 15:59:19 +0000 | [diff] [blame] | 5830 | SQSLOT("__iadd__", sq_inplace_concat, NULL, |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5831 | wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"), |
Armin Rigo | fd163f9 | 2005-12-29 15:59:19 +0000 | [diff] [blame] | 5832 | SQSLOT("__imul__", sq_inplace_repeat, NULL, |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5833 | wrap_indexargfunc, "x.__imul__(y) <==> x*=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5834 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5835 | MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5836 | "x.__len__() <==> len(x)"), |
Guido van Rossum | fd38f8e | 2001-10-09 20:17:57 +0000 | [diff] [blame] | 5837 | MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5838 | wrap_binaryfunc, |
| 5839 | "x.__getitem__(y) <==> x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5840 | MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5841 | wrap_objobjargproc, |
| 5842 | "x.__setitem__(i, y) <==> x[i]=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5843 | MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5844 | wrap_delitem, |
| 5845 | "x.__delitem__(y) <==> del x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5846 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5847 | BINSLOT("__add__", nb_add, slot_nb_add, |
| 5848 | "+"), |
| 5849 | RBINSLOT("__radd__", nb_add, slot_nb_add, |
| 5850 | "+"), |
| 5851 | BINSLOT("__sub__", nb_subtract, slot_nb_subtract, |
| 5852 | "-"), |
| 5853 | RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract, |
| 5854 | "-"), |
| 5855 | BINSLOT("__mul__", nb_multiply, slot_nb_multiply, |
| 5856 | "*"), |
| 5857 | RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply, |
| 5858 | "*"), |
| 5859 | BINSLOT("__div__", nb_divide, slot_nb_divide, |
| 5860 | "/"), |
| 5861 | RBINSLOT("__rdiv__", nb_divide, slot_nb_divide, |
| 5862 | "/"), |
| 5863 | BINSLOT("__mod__", nb_remainder, slot_nb_remainder, |
| 5864 | "%"), |
| 5865 | RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder, |
| 5866 | "%"), |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5867 | BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5868 | "divmod(x, y)"), |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5869 | RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5870 | "divmod(y, x)"), |
| 5871 | NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, |
| 5872 | "x.__pow__(y[, z]) <==> pow(x, y[, z])"), |
| 5873 | NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, |
| 5874 | "y.__rpow__(x[, z]) <==> pow(x, y[, z])"), |
| 5875 | UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"), |
| 5876 | UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"), |
| 5877 | UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, |
| 5878 | "abs(x)"), |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 5879 | UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquirypred, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5880 | "x != 0"), |
| 5881 | UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"), |
| 5882 | BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5883 | RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5884 | BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5885 | RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5886 | BINSLOT("__and__", nb_and, slot_nb_and, "&"), |
| 5887 | RBINSLOT("__rand__", nb_and, slot_nb_and, "&"), |
| 5888 | BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"), |
| 5889 | RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), |
| 5890 | BINSLOT("__or__", nb_or, slot_nb_or, "|"), |
| 5891 | RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), |
| 5892 | NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc, |
| 5893 | "x.__coerce__(y) <==> coerce(x, y)"), |
| 5894 | UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, |
| 5895 | "int(x)"), |
| 5896 | UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc, |
| 5897 | "long(x)"), |
| 5898 | UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, |
| 5899 | "float(x)"), |
| 5900 | UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc, |
| 5901 | "oct(x)"), |
| 5902 | UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc, |
| 5903 | "hex(x)"), |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 5904 | NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc, |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5905 | "x[y:z] <==> x[y.__index__():z.__index__()]"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5906 | IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, |
| 5907 | wrap_binaryfunc, "+"), |
| 5908 | IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, |
| 5909 | wrap_binaryfunc, "-"), |
| 5910 | IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply, |
| 5911 | wrap_binaryfunc, "*"), |
| 5912 | IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide, |
| 5913 | wrap_binaryfunc, "/"), |
| 5914 | IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder, |
| 5915 | wrap_binaryfunc, "%"), |
| 5916 | IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power, |
Guido van Rossum | 6e5680f | 2002-10-15 01:01:53 +0000 | [diff] [blame] | 5917 | wrap_binaryfunc, "**"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5918 | IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift, |
| 5919 | wrap_binaryfunc, "<<"), |
| 5920 | IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift, |
| 5921 | wrap_binaryfunc, ">>"), |
| 5922 | IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and, |
| 5923 | wrap_binaryfunc, "&"), |
| 5924 | IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor, |
| 5925 | wrap_binaryfunc, "^"), |
| 5926 | IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or, |
| 5927 | wrap_binaryfunc, "|"), |
| 5928 | BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5929 | RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5930 | BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5931 | RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5932 | IBSLOT("__ifloordiv__", nb_inplace_floor_divide, |
| 5933 | slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"), |
| 5934 | IBSLOT("__itruediv__", nb_inplace_true_divide, |
| 5935 | slot_nb_inplace_true_divide, wrap_binaryfunc, "/"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5936 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5937 | TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, |
| 5938 | "x.__str__() <==> str(x)"), |
Guido van Rossum | afe7a94 | 2001-10-29 14:33:44 +0000 | [diff] [blame] | 5939 | TPSLOT("__str__", tp_print, NULL, NULL, ""), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5940 | TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, |
| 5941 | "x.__repr__() <==> repr(x)"), |
Guido van Rossum | afe7a94 | 2001-10-29 14:33:44 +0000 | [diff] [blame] | 5942 | TPSLOT("__repr__", tp_print, NULL, NULL, ""), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5943 | TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc, |
| 5944 | "x.__cmp__(y) <==> cmp(x,y)"), |
| 5945 | TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, |
| 5946 | "x.__hash__() <==> hash(x)"), |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5947 | FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, |
| 5948 | "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS), |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5949 | TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5950 | wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"), |
| 5951 | TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), |
| 5952 | TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), |
| 5953 | TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), |
| 5954 | TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, |
| 5955 | "x.__setattr__('name', value) <==> x.name = value"), |
| 5956 | TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), |
| 5957 | TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, |
| 5958 | "x.__delattr__('name') <==> del x.name"), |
| 5959 | TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), |
| 5960 | TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, |
| 5961 | "x.__lt__(y) <==> x<y"), |
| 5962 | TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, |
| 5963 | "x.__le__(y) <==> x<=y"), |
| 5964 | TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, |
| 5965 | "x.__eq__(y) <==> x==y"), |
| 5966 | TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, |
| 5967 | "x.__ne__(y) <==> x!=y"), |
| 5968 | TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, |
| 5969 | "x.__gt__(y) <==> x>y"), |
| 5970 | TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, |
| 5971 | "x.__ge__(y) <==> x>=y"), |
| 5972 | TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, |
| 5973 | "x.__iter__() <==> iter(x)"), |
| 5974 | TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next, |
| 5975 | "x.next() -> the next value, or raise StopIteration"), |
| 5976 | TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, |
| 5977 | "descr.__get__(obj[, type]) -> value"), |
| 5978 | TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, |
| 5979 | "descr.__set__(obj, value)"), |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 5980 | TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set, |
| 5981 | wrap_descr_delete, "descr.__delete__(obj)"), |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5982 | FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5983 | "x.__init__(...) initializes x; " |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5984 | "see x.__class__.__doc__ for signature", |
| 5985 | PyWrapperFlag_KEYWORDS), |
| 5986 | TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""), |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5987 | TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5988 | {NULL} |
| 5989 | }; |
| 5990 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5991 | /* Given a type pointer and an offset gotten from a slotdef entry, return a |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5992 | 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] | 5993 | the offset to the type pointer, since it takes care to indirect through the |
| 5994 | proper indirection pointer (as_buffer, etc.); it returns NULL if the |
| 5995 | indirection pointer is NULL. */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5996 | static void ** |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5997 | slotptr(PyTypeObject *type, int ioffset) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5998 | { |
| 5999 | char *ptr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6000 | long offset = ioffset; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6001 | |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 6002 | /* Note: this depends on the order of the members of PyHeapTypeObject! */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6003 | assert(offset >= 0); |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 6004 | assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer)); |
| 6005 | if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) { |
Martin v. Löwis | ee36d65 | 2006-04-11 09:08:02 +0000 | [diff] [blame] | 6006 | ptr = (char *)type->tp_as_sequence; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 6007 | offset -= offsetof(PyHeapTypeObject, as_sequence); |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6008 | } |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 6009 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) { |
Martin v. Löwis | ee36d65 | 2006-04-11 09:08:02 +0000 | [diff] [blame] | 6010 | ptr = (char *)type->tp_as_mapping; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 6011 | offset -= offsetof(PyHeapTypeObject, as_mapping); |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6012 | } |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 6013 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) { |
Martin v. Löwis | ee36d65 | 2006-04-11 09:08:02 +0000 | [diff] [blame] | 6014 | ptr = (char *)type->tp_as_number; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 6015 | offset -= offsetof(PyHeapTypeObject, as_number); |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6016 | } |
| 6017 | else { |
Martin v. Löwis | ee36d65 | 2006-04-11 09:08:02 +0000 | [diff] [blame] | 6018 | ptr = (char *)type; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6019 | } |
| 6020 | if (ptr != NULL) |
| 6021 | ptr += offset; |
| 6022 | return (void **)ptr; |
| 6023 | } |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 6024 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6025 | /* Length of array of slotdef pointers used to store slots with the |
| 6026 | same __name__. There should be at most MAX_EQUIV-1 slotdef entries with |
| 6027 | the same __name__, for any __name__. Since that's a static property, it is |
| 6028 | appropriate to declare fixed-size arrays for this. */ |
| 6029 | #define MAX_EQUIV 10 |
| 6030 | |
| 6031 | /* Return a slot pointer for a given name, but ONLY if the attribute has |
| 6032 | exactly one slot function. The name must be an interned string. */ |
| 6033 | static void ** |
| 6034 | resolve_slotdups(PyTypeObject *type, PyObject *name) |
| 6035 | { |
| 6036 | /* XXX Maybe this could be optimized more -- but is it worth it? */ |
| 6037 | |
| 6038 | /* pname and ptrs act as a little cache */ |
| 6039 | static PyObject *pname; |
| 6040 | static slotdef *ptrs[MAX_EQUIV]; |
| 6041 | slotdef *p, **pp; |
| 6042 | void **res, **ptr; |
| 6043 | |
| 6044 | if (pname != name) { |
| 6045 | /* Collect all slotdefs that match name into ptrs. */ |
| 6046 | pname = name; |
| 6047 | pp = ptrs; |
| 6048 | for (p = slotdefs; p->name_strobj; p++) { |
| 6049 | if (p->name_strobj == name) |
| 6050 | *pp++ = p; |
| 6051 | } |
| 6052 | *pp = NULL; |
| 6053 | } |
| 6054 | |
| 6055 | /* Look in all matching slots of the type; if exactly one of these has |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6056 | a filled-in slot, return its value. Otherwise return NULL. */ |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6057 | res = NULL; |
| 6058 | for (pp = ptrs; *pp; pp++) { |
| 6059 | ptr = slotptr(type, (*pp)->offset); |
| 6060 | if (ptr == NULL || *ptr == NULL) |
| 6061 | continue; |
| 6062 | if (res != NULL) |
| 6063 | return NULL; |
| 6064 | res = ptr; |
| 6065 | } |
| 6066 | return res; |
| 6067 | } |
| 6068 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6069 | /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6070 | does some incredibly complex thinking and then sticks something into the |
| 6071 | slot. (It sees if the adjacent slotdefs for the same slot have conflicting |
| 6072 | interests, and then stores a generic wrapper or a specific function into |
| 6073 | the slot.) Return a pointer to the next slotdef with a different offset, |
| 6074 | because that's convenient for fixup_slot_dispatchers(). */ |
| 6075 | static slotdef * |
| 6076 | update_one_slot(PyTypeObject *type, slotdef *p) |
| 6077 | { |
| 6078 | PyObject *descr; |
| 6079 | PyWrapperDescrObject *d; |
| 6080 | void *generic = NULL, *specific = NULL; |
| 6081 | int use_generic = 0; |
| 6082 | int offset = p->offset; |
| 6083 | void **ptr = slotptr(type, offset); |
| 6084 | |
| 6085 | if (ptr == NULL) { |
| 6086 | do { |
| 6087 | ++p; |
| 6088 | } while (p->offset == offset); |
| 6089 | return p; |
| 6090 | } |
| 6091 | do { |
| 6092 | descr = _PyType_Lookup(type, p->name_strobj); |
Amaury Forgeot d'Arc | a40d573 | 2009-01-12 23:36:55 +0000 | [diff] [blame] | 6093 | if (descr == NULL) { |
| 6094 | if (ptr == (void**)&type->tp_iternext) { |
| 6095 | specific = _PyObject_NextNotImplemented; |
| 6096 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6097 | continue; |
Amaury Forgeot d'Arc | a40d573 | 2009-01-12 23:36:55 +0000 | [diff] [blame] | 6098 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6099 | if (Py_TYPE(descr) == &PyWrapperDescr_Type) { |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6100 | void **tptr = resolve_slotdups(type, p->name_strobj); |
| 6101 | if (tptr == NULL || tptr == ptr) |
| 6102 | generic = p->function; |
| 6103 | d = (PyWrapperDescrObject *)descr; |
| 6104 | if (d->d_base->wrapper == p->wrapper && |
| 6105 | PyType_IsSubtype(type, d->d_type)) |
| 6106 | { |
| 6107 | if (specific == NULL || |
| 6108 | specific == d->d_wrapped) |
| 6109 | specific = d->d_wrapped; |
| 6110 | else |
| 6111 | use_generic = 1; |
| 6112 | } |
| 6113 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6114 | else if (Py_TYPE(descr) == &PyCFunction_Type && |
Guido van Rossum | 721f62e | 2002-08-09 02:14:34 +0000 | [diff] [blame] | 6115 | PyCFunction_GET_FUNCTION(descr) == |
| 6116 | (PyCFunction)tp_new_wrapper && |
| 6117 | strcmp(p->name, "__new__") == 0) |
| 6118 | { |
| 6119 | /* The __new__ wrapper is not a wrapper descriptor, |
| 6120 | so must be special-cased differently. |
| 6121 | If we don't do this, creating an instance will |
| 6122 | always use slot_tp_new which will look up |
| 6123 | __new__ in the MRO which will call tp_new_wrapper |
| 6124 | which will look through the base classes looking |
| 6125 | for a static base and call its tp_new (usually |
| 6126 | PyType_GenericNew), after performing various |
| 6127 | sanity checks and constructing a new argument |
| 6128 | list. Cut all that nonsense short -- this speeds |
| 6129 | up instance creation tremendously. */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 6130 | specific = (void *)type->tp_new; |
Guido van Rossum | 721f62e | 2002-08-09 02:14:34 +0000 | [diff] [blame] | 6131 | /* XXX I'm not 100% sure that there isn't a hole |
| 6132 | in this reasoning that requires additional |
| 6133 | sanity checks. I'll buy the first person to |
| 6134 | point out a bug in this reasoning a beer. */ |
| 6135 | } |
Nick Coghlan | 53663a6 | 2008-07-15 14:27:37 +0000 | [diff] [blame] | 6136 | else if (descr == Py_None && |
| 6137 | strcmp(p->name, "__hash__") == 0) { |
| 6138 | /* We specifically allow __hash__ to be set to None |
| 6139 | to prevent inheritance of the default |
| 6140 | implementation from object.__hash__ */ |
| 6141 | specific = PyObject_HashNotImplemented; |
| 6142 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6143 | else { |
| 6144 | use_generic = 1; |
| 6145 | generic = p->function; |
| 6146 | } |
| 6147 | } while ((++p)->offset == offset); |
| 6148 | if (specific && !use_generic) |
| 6149 | *ptr = specific; |
| 6150 | else |
| 6151 | *ptr = generic; |
| 6152 | return p; |
| 6153 | } |
| 6154 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6155 | /* In the type, update the slots whose slotdefs are gathered in the pp array. |
| 6156 | This is a callback for update_subclasses(). */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6157 | static int |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6158 | update_slots_callback(PyTypeObject *type, void *data) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6159 | { |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6160 | slotdef **pp = (slotdef **)data; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6161 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6162 | for (; *pp; pp++) |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6163 | update_one_slot(type, *pp); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6164 | return 0; |
| 6165 | } |
| 6166 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6167 | /* Comparison function for qsort() to compare slotdefs by their offset, and |
| 6168 | for equal offset by their address (to force a stable sort). */ |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6169 | static int |
| 6170 | slotdef_cmp(const void *aa, const void *bb) |
| 6171 | { |
| 6172 | const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb; |
| 6173 | int c = a->offset - b->offset; |
| 6174 | if (c != 0) |
| 6175 | return c; |
| 6176 | else |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6177 | /* Cannot use a-b, as this gives off_t, |
| 6178 | which may lose precision when converted to int. */ |
| 6179 | return (a > b) ? 1 : (a < b) ? -1 : 0; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6180 | } |
| 6181 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6182 | /* Initialize the slotdefs table by adding interned string objects for the |
| 6183 | names and sorting the entries. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6184 | static void |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6185 | init_slotdefs(void) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6186 | { |
| 6187 | slotdef *p; |
| 6188 | static int initialized = 0; |
| 6189 | |
| 6190 | if (initialized) |
| 6191 | return; |
| 6192 | for (p = slotdefs; p->name; p++) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 6193 | p->name_strobj = PyString_InternFromString(p->name); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6194 | if (!p->name_strobj) |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6195 | Py_FatalError("Out of memory interning slotdef names"); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6196 | } |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 6197 | qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef), |
| 6198 | slotdef_cmp); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6199 | initialized = 1; |
| 6200 | } |
| 6201 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6202 | /* Update the slots after assignment to a class (type) attribute. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6203 | static int |
| 6204 | update_slot(PyTypeObject *type, PyObject *name) |
| 6205 | { |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6206 | slotdef *ptrs[MAX_EQUIV]; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6207 | slotdef *p; |
| 6208 | slotdef **pp; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 6209 | int offset; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6210 | |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 6211 | /* Clear the VALID_VERSION flag of 'type' and all its |
| 6212 | subclasses. This could possibly be unified with the |
| 6213 | update_subclasses() recursion below, but carefully: |
| 6214 | they each have their own conditions on which to stop |
| 6215 | recursing into subclasses. */ |
Georg Brandl | 74a1dea | 2008-05-28 11:21:39 +0000 | [diff] [blame] | 6216 | PyType_Modified(type); |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 6217 | |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 6218 | init_slotdefs(); |
| 6219 | pp = ptrs; |
| 6220 | for (p = slotdefs; p->name; p++) { |
| 6221 | /* XXX assume name is interned! */ |
| 6222 | if (p->name_strobj == name) |
| 6223 | *pp++ = p; |
| 6224 | } |
| 6225 | *pp = NULL; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6226 | for (pp = ptrs; *pp; pp++) { |
| 6227 | p = *pp; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 6228 | offset = p->offset; |
| 6229 | while (p > slotdefs && (p-1)->offset == offset) |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6230 | --p; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 6231 | *pp = p; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6232 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6233 | if (ptrs[0] == NULL) |
| 6234 | return 0; /* Not an attribute that affects any slots */ |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6235 | return update_subclasses(type, name, |
| 6236 | update_slots_callback, (void *)ptrs); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6237 | } |
| 6238 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6239 | /* Store the proper functions in the slot dispatches at class (type) |
| 6240 | definition time, based upon which operations the class overrides in its |
| 6241 | dict. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6242 | static void |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6243 | fixup_slot_dispatchers(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6244 | { |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6245 | slotdef *p; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6246 | |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6247 | init_slotdefs(); |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6248 | for (p = slotdefs; p->name; ) |
| 6249 | p = update_one_slot(type, p); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6250 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6251 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 6252 | static void |
| 6253 | update_all_slots(PyTypeObject* type) |
| 6254 | { |
| 6255 | slotdef *p; |
| 6256 | |
| 6257 | init_slotdefs(); |
| 6258 | for (p = slotdefs; p->name; p++) { |
| 6259 | /* update_slot returns int but can't actually fail */ |
| 6260 | update_slot(type, p->name_strobj); |
| 6261 | } |
| 6262 | } |
| 6263 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6264 | /* recurse_down_subclasses() and update_subclasses() are mutually |
| 6265 | recursive functions to call a callback for all subclasses, |
| 6266 | but refraining from recursing into subclasses that define 'name'. */ |
| 6267 | |
| 6268 | static int |
| 6269 | update_subclasses(PyTypeObject *type, PyObject *name, |
| 6270 | update_callback callback, void *data) |
| 6271 | { |
| 6272 | if (callback(type, data) < 0) |
| 6273 | return -1; |
| 6274 | return recurse_down_subclasses(type, name, callback, data); |
| 6275 | } |
| 6276 | |
| 6277 | static int |
| 6278 | recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
| 6279 | update_callback callback, void *data) |
| 6280 | { |
| 6281 | PyTypeObject *subclass; |
| 6282 | PyObject *ref, *subclasses, *dict; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6283 | Py_ssize_t i, n; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6284 | |
| 6285 | subclasses = type->tp_subclasses; |
| 6286 | if (subclasses == NULL) |
| 6287 | return 0; |
| 6288 | assert(PyList_Check(subclasses)); |
| 6289 | n = PyList_GET_SIZE(subclasses); |
| 6290 | for (i = 0; i < n; i++) { |
| 6291 | ref = PyList_GET_ITEM(subclasses, i); |
| 6292 | assert(PyWeakref_CheckRef(ref)); |
| 6293 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 6294 | assert(subclass != NULL); |
| 6295 | if ((PyObject *)subclass == Py_None) |
| 6296 | continue; |
| 6297 | assert(PyType_Check(subclass)); |
| 6298 | /* Avoid recursing down into unaffected classes */ |
| 6299 | dict = subclass->tp_dict; |
| 6300 | if (dict != NULL && PyDict_Check(dict) && |
| 6301 | PyDict_GetItem(dict, name) != NULL) |
| 6302 | continue; |
| 6303 | if (update_subclasses(subclass, name, callback, data) < 0) |
| 6304 | return -1; |
| 6305 | } |
| 6306 | return 0; |
| 6307 | } |
| 6308 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6309 | /* This function is called by PyType_Ready() to populate the type's |
| 6310 | dictionary with method descriptors for function slots. For each |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6311 | function slot (like tp_repr) that's defined in the type, one or more |
| 6312 | corresponding descriptors are added in the type's tp_dict dictionary |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6313 | under the appropriate name (like __repr__). Some function slots |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6314 | cause more than one descriptor to be added (for example, the nb_add |
| 6315 | slot adds both __add__ and __radd__ descriptors) and some function |
| 6316 | slots compete for the same descriptor (for example both sq_item and |
| 6317 | mp_subscript generate a __getitem__ descriptor). |
| 6318 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6319 | In the latter case, the first slotdef entry encoutered wins. Since |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 6320 | 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] | 6321 | PyHeapTypeObject, this gives us some control over disambiguating |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6322 | between competing slots: the members of PyHeapTypeObject are listed |
| 6323 | from most general to least general, so the most general slot is |
| 6324 | preferred. In particular, because as_mapping comes before as_sequence, |
| 6325 | for a type that defines both mp_subscript and sq_item, mp_subscript |
| 6326 | wins. |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6327 | |
| 6328 | This only adds new descriptors and doesn't overwrite entries in |
| 6329 | tp_dict that were previously defined. The descriptors contain a |
| 6330 | reference to the C function they must call, so that it's safe if they |
| 6331 | are copied into a subtype's __dict__ and the subtype has a different |
| 6332 | C function in its slot -- calling the method defined by the |
| 6333 | descriptor will call the C function that was used to create it, |
| 6334 | rather than the C function present in the slot when it is called. |
| 6335 | (This is important because a subtype may have a C function in the |
| 6336 | slot that calls the method from the dictionary, and we want to avoid |
| 6337 | infinite recursion here.) */ |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6338 | |
| 6339 | static int |
| 6340 | add_operators(PyTypeObject *type) |
| 6341 | { |
| 6342 | PyObject *dict = type->tp_dict; |
| 6343 | slotdef *p; |
| 6344 | PyObject *descr; |
| 6345 | void **ptr; |
| 6346 | |
| 6347 | init_slotdefs(); |
| 6348 | for (p = slotdefs; p->name; p++) { |
| 6349 | if (p->wrapper == NULL) |
| 6350 | continue; |
| 6351 | ptr = slotptr(type, p->offset); |
| 6352 | if (!ptr || !*ptr) |
| 6353 | continue; |
| 6354 | if (PyDict_GetItem(dict, p->name_strobj)) |
| 6355 | continue; |
Nick Coghlan | 53663a6 | 2008-07-15 14:27:37 +0000 | [diff] [blame] | 6356 | if (*ptr == PyObject_HashNotImplemented) { |
| 6357 | /* Classes may prevent the inheritance of the tp_hash |
| 6358 | slot by storing PyObject_HashNotImplemented in it. Make it |
| 6359 | visible as a None value for the __hash__ attribute. */ |
| 6360 | if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0) |
| 6361 | return -1; |
| 6362 | } |
| 6363 | else { |
| 6364 | descr = PyDescr_NewWrapper(type, p, *ptr); |
| 6365 | if (descr == NULL) |
| 6366 | return -1; |
| 6367 | if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) |
| 6368 | return -1; |
| 6369 | Py_DECREF(descr); |
| 6370 | } |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6371 | } |
| 6372 | if (type->tp_new != NULL) { |
| 6373 | if (add_tp_new_wrapper(type) < 0) |
| 6374 | return -1; |
| 6375 | } |
| 6376 | return 0; |
| 6377 | } |
| 6378 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6379 | |
| 6380 | /* Cooperative 'super' */ |
| 6381 | |
| 6382 | typedef struct { |
| 6383 | PyObject_HEAD |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6384 | PyTypeObject *type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6385 | PyObject *obj; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6386 | PyTypeObject *obj_type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6387 | } superobject; |
| 6388 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 6389 | static PyMemberDef super_members[] = { |
| 6390 | {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY, |
| 6391 | "the class invoking super()"}, |
| 6392 | {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY, |
| 6393 | "the instance invoking super(); may be None"}, |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6394 | {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY, |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 6395 | "the type of the instance invoking super(); may be None"}, |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6396 | {0} |
| 6397 | }; |
| 6398 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6399 | static void |
| 6400 | super_dealloc(PyObject *self) |
| 6401 | { |
| 6402 | superobject *su = (superobject *)self; |
| 6403 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6404 | _PyObject_GC_UNTRACK(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6405 | Py_XDECREF(su->obj); |
| 6406 | Py_XDECREF(su->type); |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6407 | Py_XDECREF(su->obj_type); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6408 | Py_TYPE(self)->tp_free(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6409 | } |
| 6410 | |
| 6411 | static PyObject * |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6412 | super_repr(PyObject *self) |
| 6413 | { |
| 6414 | superobject *su = (superobject *)self; |
| 6415 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6416 | if (su->obj_type) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 6417 | return PyString_FromFormat( |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 6418 | "<super: <class '%s'>, <%s object>>", |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6419 | su->type ? su->type->tp_name : "NULL", |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6420 | su->obj_type->tp_name); |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6421 | else |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 6422 | return PyString_FromFormat( |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 6423 | "<super: <class '%s'>, NULL>", |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6424 | su->type ? su->type->tp_name : "NULL"); |
| 6425 | } |
| 6426 | |
| 6427 | static PyObject * |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6428 | super_getattro(PyObject *self, PyObject *name) |
| 6429 | { |
| 6430 | superobject *su = (superobject *)self; |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 6431 | int skip = su->obj_type == NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6432 | |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 6433 | if (!skip) { |
| 6434 | /* We want __class__ to return the class of the super object |
| 6435 | (i.e. super, or a subclass), not the class of su->obj. */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 6436 | skip = (PyString_Check(name) && |
| 6437 | PyString_GET_SIZE(name) == 9 && |
| 6438 | strcmp(PyString_AS_STRING(name), "__class__") == 0); |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 6439 | } |
| 6440 | |
| 6441 | if (!skip) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 6442 | PyObject *mro, *res, *tmp, *dict; |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 6443 | PyTypeObject *starttype; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6444 | descrgetfunc f; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6445 | Py_ssize_t i, n; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6446 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6447 | starttype = su->obj_type; |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 6448 | mro = starttype->tp_mro; |
| 6449 | |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6450 | if (mro == NULL) |
| 6451 | n = 0; |
| 6452 | else { |
| 6453 | assert(PyTuple_Check(mro)); |
| 6454 | n = PyTuple_GET_SIZE(mro); |
| 6455 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6456 | for (i = 0; i < n; i++) { |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6457 | if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6458 | break; |
| 6459 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6460 | i++; |
| 6461 | res = NULL; |
| 6462 | for (; i < n; i++) { |
| 6463 | tmp = PyTuple_GET_ITEM(mro, i); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 6464 | if (PyType_Check(tmp)) |
| 6465 | dict = ((PyTypeObject *)tmp)->tp_dict; |
| 6466 | else if (PyClass_Check(tmp)) |
| 6467 | dict = ((PyClassObject *)tmp)->cl_dict; |
| 6468 | else |
| 6469 | continue; |
| 6470 | res = PyDict_GetItem(dict, name); |
Guido van Rossum | 6cc5bb6 | 2003-04-16 20:01:36 +0000 | [diff] [blame] | 6471 | if (res != NULL) { |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6472 | Py_INCREF(res); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6473 | f = Py_TYPE(res)->tp_descr_get; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6474 | if (f != NULL) { |
Phillip J. Eby | 91a968a | 2004-03-25 02:19:34 +0000 | [diff] [blame] | 6475 | tmp = f(res, |
| 6476 | /* Only pass 'obj' param if |
| 6477 | this is instance-mode super |
| 6478 | (See SF ID #743627) |
| 6479 | */ |
Hye-Shik Chang | ff365c9 | 2004-03-25 16:37:03 +0000 | [diff] [blame] | 6480 | (su->obj == (PyObject *) |
| 6481 | su->obj_type |
Phillip J. Eby | 91a968a | 2004-03-25 02:19:34 +0000 | [diff] [blame] | 6482 | ? (PyObject *)NULL |
| 6483 | : su->obj), |
Guido van Rossum | d464107 | 2002-04-03 02:13:37 +0000 | [diff] [blame] | 6484 | (PyObject *)starttype); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6485 | Py_DECREF(res); |
| 6486 | res = tmp; |
| 6487 | } |
| 6488 | return res; |
| 6489 | } |
| 6490 | } |
| 6491 | } |
| 6492 | return PyObject_GenericGetAttr(self, name); |
| 6493 | } |
| 6494 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6495 | static PyTypeObject * |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6496 | supercheck(PyTypeObject *type, PyObject *obj) |
| 6497 | { |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6498 | /* Check that a super() call makes sense. Return a type object. |
| 6499 | |
| 6500 | obj can be a new-style class, or an instance of one: |
| 6501 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6502 | - 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] | 6503 | used for class methods; the return value is obj. |
| 6504 | |
| 6505 | - If it is an instance, it must be an instance of 'type'. This is |
| 6506 | the normal case; the return value is obj.__class__. |
| 6507 | |
| 6508 | But... when obj is an instance, we want to allow for the case where |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6509 | 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] | 6510 | This will allow using super() with a proxy for obj. |
| 6511 | */ |
| 6512 | |
Guido van Rossum | 8e80a72 | 2003-02-18 19:22:22 +0000 | [diff] [blame] | 6513 | /* Check for first bullet above (special case) */ |
| 6514 | if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) { |
| 6515 | Py_INCREF(obj); |
| 6516 | return (PyTypeObject *)obj; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6517 | } |
Guido van Rossum | 8e80a72 | 2003-02-18 19:22:22 +0000 | [diff] [blame] | 6518 | |
| 6519 | /* Normal case */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6520 | if (PyType_IsSubtype(Py_TYPE(obj), type)) { |
| 6521 | Py_INCREF(Py_TYPE(obj)); |
| 6522 | return Py_TYPE(obj); |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6523 | } |
| 6524 | else { |
| 6525 | /* Try the slow way */ |
| 6526 | static PyObject *class_str = NULL; |
| 6527 | PyObject *class_attr; |
| 6528 | |
| 6529 | if (class_str == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 6530 | class_str = PyString_FromString("__class__"); |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6531 | if (class_str == NULL) |
| 6532 | return NULL; |
| 6533 | } |
| 6534 | |
| 6535 | class_attr = PyObject_GetAttr(obj, class_str); |
| 6536 | |
| 6537 | if (class_attr != NULL && |
| 6538 | PyType_Check(class_attr) && |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6539 | (PyTypeObject *)class_attr != Py_TYPE(obj)) |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6540 | { |
| 6541 | int ok = PyType_IsSubtype( |
| 6542 | (PyTypeObject *)class_attr, type); |
| 6543 | if (ok) |
| 6544 | return (PyTypeObject *)class_attr; |
| 6545 | } |
| 6546 | |
| 6547 | if (class_attr == NULL) |
| 6548 | PyErr_Clear(); |
| 6549 | else |
| 6550 | Py_DECREF(class_attr); |
| 6551 | } |
| 6552 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6553 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6554 | "super(type, obj): " |
| 6555 | "obj must be an instance or subtype of type"); |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6556 | return NULL; |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6557 | } |
| 6558 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6559 | static PyObject * |
| 6560 | super_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 6561 | { |
| 6562 | superobject *su = (superobject *)self; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 6563 | superobject *newobj; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6564 | |
| 6565 | if (obj == NULL || obj == Py_None || su->obj != NULL) { |
| 6566 | /* Not binding to an object, or already bound */ |
| 6567 | Py_INCREF(self); |
| 6568 | return self; |
| 6569 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6570 | if (Py_TYPE(su) != &PySuper_Type) |
Armin Rigo | 7726dc0 | 2005-05-15 15:32:08 +0000 | [diff] [blame] | 6571 | /* If su is an instance of a (strict) subclass of super, |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6572 | call its type */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6573 | return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su), |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6574 | su->type, obj, NULL); |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6575 | else { |
| 6576 | /* Inline the common case */ |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6577 | PyTypeObject *obj_type = supercheck(su->type, obj); |
| 6578 | if (obj_type == NULL) |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6579 | return NULL; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 6580 | newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type, |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6581 | NULL, NULL); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 6582 | if (newobj == NULL) |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6583 | return NULL; |
| 6584 | Py_INCREF(su->type); |
| 6585 | Py_INCREF(obj); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 6586 | newobj->type = su->type; |
| 6587 | newobj->obj = obj; |
| 6588 | newobj->obj_type = obj_type; |
| 6589 | return (PyObject *)newobj; |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6590 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6591 | } |
| 6592 | |
| 6593 | static int |
| 6594 | super_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 6595 | { |
| 6596 | superobject *su = (superobject *)self; |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6597 | PyTypeObject *type; |
| 6598 | PyObject *obj = NULL; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6599 | PyTypeObject *obj_type = NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6600 | |
Georg Brandl | 5d59c09 | 2006-09-30 08:43:30 +0000 | [diff] [blame] | 6601 | if (!_PyArg_NoKeywords("super", kwds)) |
| 6602 | return -1; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6603 | if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj)) |
| 6604 | return -1; |
| 6605 | if (obj == Py_None) |
| 6606 | obj = NULL; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6607 | if (obj != NULL) { |
| 6608 | obj_type = supercheck(type, obj); |
| 6609 | if (obj_type == NULL) |
| 6610 | return -1; |
| 6611 | Py_INCREF(obj); |
| 6612 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6613 | Py_INCREF(type); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6614 | su->type = type; |
| 6615 | su->obj = obj; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6616 | su->obj_type = obj_type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6617 | return 0; |
| 6618 | } |
| 6619 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6620 | PyDoc_STRVAR(super_doc, |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6621 | "super(type) -> unbound super object\n" |
| 6622 | "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6623 | "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6624 | "Typical use to call a cooperative superclass method:\n" |
| 6625 | "class C(B):\n" |
| 6626 | " def meth(self, arg):\n" |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6627 | " super(C, self).meth(arg)"); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6628 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6629 | static int |
| 6630 | super_traverse(PyObject *self, visitproc visit, void *arg) |
| 6631 | { |
| 6632 | superobject *su = (superobject *)self; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6633 | |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 6634 | Py_VISIT(su->obj); |
| 6635 | Py_VISIT(su->type); |
| 6636 | Py_VISIT(su->obj_type); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6637 | |
| 6638 | return 0; |
| 6639 | } |
| 6640 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6641 | PyTypeObject PySuper_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 6642 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6643 | "super", /* tp_name */ |
| 6644 | sizeof(superobject), /* tp_basicsize */ |
| 6645 | 0, /* tp_itemsize */ |
| 6646 | /* methods */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6647 | super_dealloc, /* tp_dealloc */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6648 | 0, /* tp_print */ |
| 6649 | 0, /* tp_getattr */ |
| 6650 | 0, /* tp_setattr */ |
| 6651 | 0, /* tp_compare */ |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6652 | super_repr, /* tp_repr */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6653 | 0, /* tp_as_number */ |
| 6654 | 0, /* tp_as_sequence */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6655 | 0, /* tp_as_mapping */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6656 | 0, /* tp_hash */ |
| 6657 | 0, /* tp_call */ |
| 6658 | 0, /* tp_str */ |
| 6659 | super_getattro, /* tp_getattro */ |
| 6660 | 0, /* tp_setattro */ |
| 6661 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6662 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 6663 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6664 | super_doc, /* tp_doc */ |
| 6665 | super_traverse, /* tp_traverse */ |
| 6666 | 0, /* tp_clear */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6667 | 0, /* tp_richcompare */ |
| 6668 | 0, /* tp_weaklistoffset */ |
| 6669 | 0, /* tp_iter */ |
| 6670 | 0, /* tp_iternext */ |
| 6671 | 0, /* tp_methods */ |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6672 | super_members, /* tp_members */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6673 | 0, /* tp_getset */ |
| 6674 | 0, /* tp_base */ |
| 6675 | 0, /* tp_dict */ |
| 6676 | super_descr_get, /* tp_descr_get */ |
| 6677 | 0, /* tp_descr_set */ |
| 6678 | 0, /* tp_dictoffset */ |
| 6679 | super_init, /* tp_init */ |
| 6680 | PyType_GenericAlloc, /* tp_alloc */ |
| 6681 | PyType_GenericNew, /* tp_new */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6682 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6683 | }; |