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 | |
Benjamin Peterson | 5083dc5 | 2009-04-25 00:41:22 +0000 | [diff] [blame^] | 931 | /* Extract the type again; tp_del may have changed it */ |
| 932 | type = Py_TYPE(self); |
| 933 | |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 934 | /* Call the base tp_dealloc() */ |
| 935 | assert(basedealloc); |
| 936 | basedealloc(self); |
| 937 | |
| 938 | /* Can't reference self beyond this point */ |
| 939 | Py_DECREF(type); |
| 940 | |
| 941 | /* Done */ |
| 942 | return; |
| 943 | } |
| 944 | |
| 945 | /* We get here only if the type has GC */ |
| 946 | |
| 947 | /* UnTrack and re-Track around the trashcan macro, alas */ |
Andrew M. Kuchling | c9172d3 | 2003-02-06 15:22:49 +0000 | [diff] [blame] | 948 | /* See explanation at end of function for full disclosure */ |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 949 | PyObject_GC_UnTrack(self); |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 950 | ++_PyTrash_delete_nesting; |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 951 | Py_TRASHCAN_SAFE_BEGIN(self); |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 952 | --_PyTrash_delete_nesting; |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 953 | /* DO NOT restore GC tracking at this point. weakref callbacks |
| 954 | * (if any, and whether directly here or indirectly in something we |
| 955 | * call) may trigger GC, and if self is tracked at that point, it |
| 956 | * 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] | 957 | */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 958 | |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 959 | /* Find the nearest base with a different tp_dealloc */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 960 | base = type; |
| 961 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 962 | base = base->tp_base; |
| 963 | assert(base); |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 966 | /* 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] | 967 | the finalizer (__del__), clearing slots, or clearing the instance |
| 968 | dict. */ |
| 969 | |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 970 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) |
| 971 | PyObject_ClearWeakRefs(self); |
| 972 | |
| 973 | /* Maybe call finalizer; exit early if resurrected */ |
| 974 | if (type->tp_del) { |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 975 | _PyObject_GC_TRACK(self); |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 976 | type->tp_del(self); |
| 977 | if (self->ob_refcnt > 0) |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 978 | goto endlabel; /* resurrected */ |
| 979 | else |
| 980 | _PyObject_GC_UNTRACK(self); |
Brett Cannon | f5bee30 | 2007-01-23 23:21:22 +0000 | [diff] [blame] | 981 | /* New weakrefs could be created during the finalizer call. |
| 982 | If this occurs, clear them out without calling their |
| 983 | finalizers since they might rely on part of the object |
| 984 | being finalized that has already been destroyed. */ |
| 985 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) { |
| 986 | /* Modeled after GET_WEAKREFS_LISTPTR() */ |
| 987 | PyWeakReference **list = (PyWeakReference **) \ |
| 988 | PyObject_GET_WEAKREFS_LISTPTR(self); |
| 989 | while (*list) |
| 990 | _PyWeakref_ClearRef(*list); |
| 991 | } |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 994 | /* Clear slots up to the nearest base with a different tp_dealloc */ |
| 995 | base = type; |
| 996 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 997 | if (Py_SIZE(base)) |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 998 | clear_slots(base, self); |
| 999 | base = base->tp_base; |
| 1000 | assert(base); |
| 1001 | } |
| 1002 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1003 | /* If we added a dict, DECREF it */ |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1004 | if (type->tp_dictoffset && !base->tp_dictoffset) { |
| 1005 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 1006 | if (dictptr != NULL) { |
| 1007 | PyObject *dict = *dictptr; |
| 1008 | if (dict != NULL) { |
| 1009 | Py_DECREF(dict); |
| 1010 | *dictptr = NULL; |
| 1011 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1012 | } |
| 1013 | } |
| 1014 | |
Benjamin Peterson | 5083dc5 | 2009-04-25 00:41:22 +0000 | [diff] [blame^] | 1015 | /* Extract the type again; tp_del may have changed it */ |
| 1016 | type = Py_TYPE(self); |
| 1017 | |
Tim Peters | 0bd743c | 2003-11-13 22:50:00 +0000 | [diff] [blame] | 1018 | /* Call the base tp_dealloc(); first retrack self if |
| 1019 | * basedealloc knows about gc. |
| 1020 | */ |
| 1021 | if (PyType_IS_GC(base)) |
| 1022 | _PyObject_GC_TRACK(self); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1023 | assert(basedealloc); |
| 1024 | basedealloc(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1025 | |
| 1026 | /* Can't reference self beyond this point */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1027 | Py_DECREF(type); |
| 1028 | |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 1029 | endlabel: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1030 | ++_PyTrash_delete_nesting; |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1031 | Py_TRASHCAN_SAFE_END(self); |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1032 | --_PyTrash_delete_nesting; |
| 1033 | |
| 1034 | /* Explanation of the weirdness around the trashcan macros: |
| 1035 | |
| 1036 | Q. What do the trashcan macros do? |
| 1037 | |
| 1038 | A. Read the comment titled "Trashcan mechanism" in object.h. |
| 1039 | 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] | 1040 | before the trashcan begin macro. Without understanding the |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1041 | trashcan code, the answers to the following questions don't make |
| 1042 | sense. |
| 1043 | |
| 1044 | Q. Why do we GC-untrack before the trashcan and then immediately |
| 1045 | GC-track again afterward? |
| 1046 | |
| 1047 | 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] | 1048 | probably GC-untracks the object. If it does that using the |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1049 | UNTRACK macro, this will crash when the object is already |
| 1050 | untracked. Because we don't know what the base class does, the |
| 1051 | only safe thing is to make sure the object is tracked when we |
| 1052 | call the base class dealloc. But... The trashcan begin macro |
| 1053 | requires that the object is *untracked* before it is called. So |
| 1054 | the dance becomes: |
| 1055 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1056 | GC untrack |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1057 | trashcan begin |
| 1058 | GC track |
| 1059 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1060 | Q. Why did the last question say "immediately GC-track again"? |
| 1061 | It's nowhere near immediately. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 1062 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1063 | A. Because the code *used* to re-track immediately. Bad Idea. |
| 1064 | self has a refcount of 0, and if gc ever gets its hands on it |
| 1065 | (which can happen if any weakref callback gets invoked), it |
| 1066 | looks like trash to gc too, and gc also tries to delete self |
| 1067 | then. But we're already deleting self. Double dealloction is |
| 1068 | a subtle disaster. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 1069 | |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1070 | Q. Why the bizarre (net-zero) manipulation of |
| 1071 | _PyTrash_delete_nesting around the trashcan macros? |
| 1072 | |
| 1073 | A. Some base classes (e.g. list) also use the trashcan mechanism. |
| 1074 | The following scenario used to be possible: |
| 1075 | |
| 1076 | - suppose the trashcan level is one below the trashcan limit |
| 1077 | |
| 1078 | - subtype_dealloc() is called |
| 1079 | |
| 1080 | - the trashcan limit is not yet reached, so the trashcan level |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1081 | is incremented and the code between trashcan begin and end is |
| 1082 | executed |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1083 | |
| 1084 | - this destroys much of the object's contents, including its |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1085 | slots and __dict__ |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1086 | |
| 1087 | - basedealloc() is called; this is really list_dealloc(), or |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1088 | some other type which also uses the trashcan macros |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1089 | |
| 1090 | - 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] | 1091 | trashcan's to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1092 | |
| 1093 | - basedealloc() returns |
| 1094 | |
| 1095 | - subtype_dealloc() decrefs the object's type |
| 1096 | |
| 1097 | - subtype_dealloc() returns |
| 1098 | |
| 1099 | - later, the trashcan code starts deleting the objects from its |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1100 | to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1101 | |
| 1102 | - subtype_dealloc() is called *AGAIN* for the same object |
| 1103 | |
| 1104 | - at the very least (if the destroyed slots and __dict__ don't |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1105 | cause problems) the object's type gets decref'ed a second |
| 1106 | time, which is *BAD*!!! |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1107 | |
| 1108 | The remedy is to make sure that if the code between trashcan |
| 1109 | begin and end in subtype_dealloc() is called, the code between |
| 1110 | trashcan begin and end in basedealloc() will also be called. |
| 1111 | This is done by decrementing the level after passing into the |
| 1112 | trashcan block, and incrementing it just before leaving the |
| 1113 | block. |
| 1114 | |
| 1115 | But now it's possible that a chain of objects consisting solely |
| 1116 | of objects whose deallocator is subtype_dealloc() will defeat |
| 1117 | the trashcan mechanism completely: the decremented level means |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1118 | that the effective level never reaches the limit. Therefore, we |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1119 | *increment* the level *before* entering the trashcan block, and |
| 1120 | matchingly decrement it after leaving. This means the trashcan |
| 1121 | code will trigger a little early, but that's no big deal. |
| 1122 | |
| 1123 | Q. Are there any live examples of code in need of all this |
| 1124 | complexity? |
| 1125 | |
| 1126 | A. Yes. See SF bug 668433 for code that crashed (when Python was |
| 1127 | compiled in debug mode) before the trashcan level manipulations |
| 1128 | were added. For more discussion, see SF patches 581742, 575073 |
| 1129 | and bug 574207. |
| 1130 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1133 | static PyTypeObject *solid_base(PyTypeObject *type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1134 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1135 | /* type test with subclassing support */ |
| 1136 | |
| 1137 | int |
| 1138 | PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
| 1139 | { |
| 1140 | PyObject *mro; |
| 1141 | |
Guido van Rossum | 9478d07 | 2001-09-07 18:52:13 +0000 | [diff] [blame] | 1142 | if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS)) |
| 1143 | return b == a || b == &PyBaseObject_Type; |
| 1144 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1145 | mro = a->tp_mro; |
| 1146 | if (mro != NULL) { |
| 1147 | /* Deal with multiple inheritance without recursion |
| 1148 | by walking the MRO tuple */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1149 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1150 | assert(PyTuple_Check(mro)); |
| 1151 | n = PyTuple_GET_SIZE(mro); |
| 1152 | for (i = 0; i < n; i++) { |
| 1153 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) |
| 1154 | return 1; |
| 1155 | } |
| 1156 | return 0; |
| 1157 | } |
| 1158 | else { |
| 1159 | /* a is not completely initilized yet; follow tp_base */ |
| 1160 | do { |
| 1161 | if (a == b) |
| 1162 | return 1; |
| 1163 | a = a->tp_base; |
| 1164 | } while (a != NULL); |
| 1165 | return b == &PyBaseObject_Type; |
| 1166 | } |
| 1167 | } |
| 1168 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1169 | /* Internal routines to do a method lookup in the type |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1170 | without looking in the instance dictionary |
| 1171 | (so we can't use PyObject_GetAttr) but still binding |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1172 | it to the instance. The arguments are the object, |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1173 | 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] | 1174 | static variable used to cache the interned Python string. |
| 1175 | |
| 1176 | Two variants: |
| 1177 | |
| 1178 | - lookup_maybe() returns NULL without raising an exception |
| 1179 | when the _PyType_Lookup() call fails; |
| 1180 | |
| 1181 | - lookup_method() always raises an exception upon errors. |
| 1182 | */ |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1183 | |
| 1184 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1185 | lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1186 | { |
| 1187 | PyObject *res; |
| 1188 | |
| 1189 | if (*attrobj == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1190 | *attrobj = PyString_InternFromString(attrstr); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1191 | if (*attrobj == NULL) |
| 1192 | return NULL; |
| 1193 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1194 | res = _PyType_Lookup(Py_TYPE(self), *attrobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1195 | if (res != NULL) { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1196 | descrgetfunc f; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1197 | if ((f = Py_TYPE(res)->tp_descr_get) == NULL) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1198 | Py_INCREF(res); |
| 1199 | else |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1200 | res = f(res, self, (PyObject *)(Py_TYPE(self))); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1201 | } |
| 1202 | return res; |
| 1203 | } |
| 1204 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1205 | static PyObject * |
| 1206 | lookup_method(PyObject *self, char *attrstr, PyObject **attrobj) |
| 1207 | { |
| 1208 | PyObject *res = lookup_maybe(self, attrstr, attrobj); |
| 1209 | if (res == NULL && !PyErr_Occurred()) |
| 1210 | PyErr_SetObject(PyExc_AttributeError, *attrobj); |
| 1211 | return res; |
| 1212 | } |
| 1213 | |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1214 | /* A variation of PyObject_CallMethod that uses lookup_method() |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1215 | instead of PyObject_GetAttrString(). This uses the same convention |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1216 | as lookup_method to cache the interned name string object. */ |
| 1217 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1218 | static PyObject * |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1219 | call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 1220 | { |
| 1221 | va_list va; |
| 1222 | PyObject *args, *func = 0, *retval; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1223 | va_start(va, format); |
| 1224 | |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 1225 | func = lookup_maybe(o, name, nameobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1226 | if (func == NULL) { |
| 1227 | va_end(va); |
| 1228 | if (!PyErr_Occurred()) |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 1229 | PyErr_SetObject(PyExc_AttributeError, *nameobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1230 | return NULL; |
| 1231 | } |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1232 | |
| 1233 | if (format && *format) |
| 1234 | args = Py_VaBuildValue(format, va); |
| 1235 | else |
| 1236 | args = PyTuple_New(0); |
| 1237 | |
| 1238 | va_end(va); |
| 1239 | |
| 1240 | if (args == NULL) |
| 1241 | return NULL; |
| 1242 | |
| 1243 | assert(PyTuple_Check(args)); |
| 1244 | retval = PyObject_Call(func, args, NULL); |
| 1245 | |
| 1246 | Py_DECREF(args); |
| 1247 | Py_DECREF(func); |
| 1248 | |
| 1249 | return retval; |
| 1250 | } |
| 1251 | |
| 1252 | /* Clone of call_method() that returns NotImplemented when the lookup fails. */ |
| 1253 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1254 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1255 | call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 1256 | { |
| 1257 | va_list va; |
| 1258 | PyObject *args, *func = 0, *retval; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1259 | va_start(va, format); |
| 1260 | |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 1261 | func = lookup_maybe(o, name, nameobj); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1262 | if (func == NULL) { |
| 1263 | va_end(va); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1264 | if (!PyErr_Occurred()) { |
| 1265 | Py_INCREF(Py_NotImplemented); |
| 1266 | return Py_NotImplemented; |
| 1267 | } |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 1268 | return NULL; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | if (format && *format) |
| 1272 | args = Py_VaBuildValue(format, va); |
| 1273 | else |
| 1274 | args = PyTuple_New(0); |
| 1275 | |
| 1276 | va_end(va); |
| 1277 | |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 1278 | if (args == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1279 | return NULL; |
| 1280 | |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 1281 | assert(PyTuple_Check(args)); |
| 1282 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1283 | |
| 1284 | Py_DECREF(args); |
| 1285 | Py_DECREF(func); |
| 1286 | |
| 1287 | return retval; |
| 1288 | } |
| 1289 | |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1290 | static int |
| 1291 | fill_classic_mro(PyObject *mro, PyObject *cls) |
| 1292 | { |
| 1293 | PyObject *bases, *base; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1294 | Py_ssize_t i, n; |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1295 | |
| 1296 | assert(PyList_Check(mro)); |
| 1297 | assert(PyClass_Check(cls)); |
| 1298 | i = PySequence_Contains(mro, cls); |
| 1299 | if (i < 0) |
| 1300 | return -1; |
| 1301 | if (!i) { |
| 1302 | if (PyList_Append(mro, cls) < 0) |
| 1303 | return -1; |
| 1304 | } |
| 1305 | bases = ((PyClassObject *)cls)->cl_bases; |
| 1306 | assert(bases && PyTuple_Check(bases)); |
| 1307 | n = PyTuple_GET_SIZE(bases); |
| 1308 | for (i = 0; i < n; i++) { |
| 1309 | base = PyTuple_GET_ITEM(bases, i); |
| 1310 | if (fill_classic_mro(mro, base) < 0) |
| 1311 | return -1; |
| 1312 | } |
| 1313 | return 0; |
| 1314 | } |
| 1315 | |
| 1316 | static PyObject * |
| 1317 | classic_mro(PyObject *cls) |
| 1318 | { |
| 1319 | PyObject *mro; |
| 1320 | |
| 1321 | assert(PyClass_Check(cls)); |
| 1322 | mro = PyList_New(0); |
| 1323 | if (mro != NULL) { |
| 1324 | if (fill_classic_mro(mro, cls) == 0) |
| 1325 | return mro; |
| 1326 | Py_DECREF(mro); |
| 1327 | } |
| 1328 | return NULL; |
| 1329 | } |
| 1330 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1331 | /* |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1332 | Method resolution order algorithm C3 described in |
| 1333 | "A Monotonic Superclass Linearization for Dylan", |
| 1334 | by Kim Barrett, Bob Cassel, Paul Haahr, |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1335 | David A. Moon, Keith Playford, and P. Tucker Withington. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1336 | (OOPSLA 1996) |
| 1337 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1338 | Some notes about the rules implied by C3: |
| 1339 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1340 | No duplicate bases. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1341 | It isn't legal to repeat a class in a list of base classes. |
| 1342 | |
| 1343 | The next three properties are the 3 constraints in "C3". |
| 1344 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1345 | Local precendece order. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1346 | If A precedes B in C's MRO, then A will precede B in the MRO of all |
| 1347 | subclasses of C. |
| 1348 | |
| 1349 | Monotonicity. |
| 1350 | The MRO of a class must be an extension without reordering of the |
| 1351 | MRO of each of its superclasses. |
| 1352 | |
| 1353 | Extended Precedence Graph (EPG). |
| 1354 | Linearization is consistent if there is a path in the EPG from |
| 1355 | each class to all its successors in the linearization. See |
| 1356 | the paper for definition of EPG. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1357 | */ |
| 1358 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1359 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1360 | tail_contains(PyObject *list, int whence, PyObject *o) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1361 | Py_ssize_t j, size; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1362 | size = PyList_GET_SIZE(list); |
| 1363 | |
| 1364 | for (j = whence+1; j < size; j++) { |
| 1365 | if (PyList_GET_ITEM(list, j) == o) |
| 1366 | return 1; |
| 1367 | } |
| 1368 | return 0; |
| 1369 | } |
| 1370 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1371 | static PyObject * |
| 1372 | class_name(PyObject *cls) |
| 1373 | { |
| 1374 | PyObject *name = PyObject_GetAttrString(cls, "__name__"); |
| 1375 | if (name == NULL) { |
| 1376 | PyErr_Clear(); |
| 1377 | Py_XDECREF(name); |
| 1378 | name = PyObject_Repr(cls); |
| 1379 | } |
| 1380 | if (name == NULL) |
| 1381 | return NULL; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1382 | if (!PyString_Check(name)) { |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1383 | Py_DECREF(name); |
| 1384 | return NULL; |
| 1385 | } |
| 1386 | return name; |
| 1387 | } |
| 1388 | |
| 1389 | static int |
| 1390 | check_duplicates(PyObject *list) |
| 1391 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1392 | Py_ssize_t i, j, n; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1393 | /* Let's use a quadratic time algorithm, |
| 1394 | assuming that the bases lists is short. |
| 1395 | */ |
| 1396 | n = PyList_GET_SIZE(list); |
| 1397 | for (i = 0; i < n; i++) { |
| 1398 | PyObject *o = PyList_GET_ITEM(list, i); |
| 1399 | for (j = i + 1; j < n; j++) { |
| 1400 | if (PyList_GET_ITEM(list, j) == o) { |
| 1401 | o = class_name(o); |
| 1402 | PyErr_Format(PyExc_TypeError, |
| 1403 | "duplicate base class %s", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1404 | o ? PyString_AS_STRING(o) : "?"); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1405 | Py_XDECREF(o); |
| 1406 | return -1; |
| 1407 | } |
| 1408 | } |
| 1409 | } |
| 1410 | return 0; |
| 1411 | } |
| 1412 | |
| 1413 | /* Raise a TypeError for an MRO order disagreement. |
| 1414 | |
| 1415 | It's hard to produce a good error message. In the absence of better |
| 1416 | insight into error reporting, report the classes that were candidates |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1417 | 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] | 1418 | order in which they should be put in the MRO, but it's hard to |
| 1419 | diagnose what constraint can't be satisfied. |
| 1420 | */ |
| 1421 | |
| 1422 | static void |
| 1423 | set_mro_error(PyObject *to_merge, int *remain) |
| 1424 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1425 | Py_ssize_t i, n, off, to_merge_size; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1426 | char buf[1000]; |
| 1427 | PyObject *k, *v; |
| 1428 | PyObject *set = PyDict_New(); |
Georg Brandl | 5c170fd | 2006-03-17 19:03:25 +0000 | [diff] [blame] | 1429 | if (!set) return; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1430 | |
| 1431 | to_merge_size = PyList_GET_SIZE(to_merge); |
| 1432 | for (i = 0; i < to_merge_size; i++) { |
| 1433 | PyObject *L = PyList_GET_ITEM(to_merge, i); |
| 1434 | if (remain[i] < PyList_GET_SIZE(L)) { |
| 1435 | PyObject *c = PyList_GET_ITEM(L, remain[i]); |
Georg Brandl | 5c170fd | 2006-03-17 19:03:25 +0000 | [diff] [blame] | 1436 | if (PyDict_SetItem(set, c, Py_None) < 0) { |
| 1437 | Py_DECREF(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1438 | return; |
Georg Brandl | 5c170fd | 2006-03-17 19:03:25 +0000 | [diff] [blame] | 1439 | } |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1440 | } |
| 1441 | } |
| 1442 | n = PyDict_Size(set); |
| 1443 | |
Raymond Hettinger | f394df4 | 2003-04-06 19:13:41 +0000 | [diff] [blame] | 1444 | off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \ |
| 1445 | consistent method resolution\norder (MRO) for bases"); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1446 | i = 0; |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 1447 | 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] | 1448 | PyObject *name = class_name(k); |
| 1449 | off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1450 | name ? PyString_AS_STRING(name) : "?"); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1451 | Py_XDECREF(name); |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 1452 | if (--n && (size_t)(off+1) < sizeof(buf)) { |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1453 | buf[off++] = ','; |
| 1454 | buf[off] = '\0'; |
| 1455 | } |
| 1456 | } |
| 1457 | PyErr_SetString(PyExc_TypeError, buf); |
| 1458 | Py_DECREF(set); |
| 1459 | } |
| 1460 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1461 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1462 | pmerge(PyObject *acc, PyObject* to_merge) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1463 | Py_ssize_t i, j, to_merge_size, empty_cnt; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1464 | int *remain; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1465 | int ok; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1466 | |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1467 | to_merge_size = PyList_GET_SIZE(to_merge); |
| 1468 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1469 | /* remain stores an index into each sublist of to_merge. |
| 1470 | remain[i] is the index of the next base in to_merge[i] |
| 1471 | that is not included in acc. |
| 1472 | */ |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 1473 | remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1474 | if (remain == NULL) |
| 1475 | return -1; |
| 1476 | for (i = 0; i < to_merge_size; i++) |
| 1477 | remain[i] = 0; |
| 1478 | |
| 1479 | again: |
| 1480 | empty_cnt = 0; |
| 1481 | for (i = 0; i < to_merge_size; i++) { |
| 1482 | PyObject *candidate; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1483 | |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1484 | PyObject *cur_list = PyList_GET_ITEM(to_merge, i); |
| 1485 | |
| 1486 | if (remain[i] >= PyList_GET_SIZE(cur_list)) { |
| 1487 | empty_cnt++; |
| 1488 | continue; |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1489 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1490 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1491 | /* Choose next candidate for MRO. |
| 1492 | |
| 1493 | The input sequences alone can determine the choice. |
| 1494 | If not, choose the class which appears in the MRO |
| 1495 | of the earliest direct superclass of the new class. |
| 1496 | */ |
| 1497 | |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1498 | candidate = PyList_GET_ITEM(cur_list, remain[i]); |
| 1499 | for (j = 0; j < to_merge_size; j++) { |
| 1500 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1501 | if (tail_contains(j_lst, remain[j], candidate)) { |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1502 | goto skip; /* continue outer loop */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1503 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1504 | } |
| 1505 | ok = PyList_Append(acc, candidate); |
| 1506 | if (ok < 0) { |
| 1507 | PyMem_Free(remain); |
| 1508 | return -1; |
| 1509 | } |
| 1510 | for (j = 0; j < to_merge_size; j++) { |
| 1511 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
Guido van Rossum | 768158c | 2002-12-31 16:33:01 +0000 | [diff] [blame] | 1512 | if (remain[j] < PyList_GET_SIZE(j_lst) && |
| 1513 | PyList_GET_ITEM(j_lst, remain[j]) == candidate) { |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1514 | remain[j]++; |
| 1515 | } |
| 1516 | } |
| 1517 | goto again; |
Tim Peters | 9a6b8d8 | 2002-11-14 23:22:33 +0000 | [diff] [blame] | 1518 | skip: ; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1521 | if (empty_cnt == to_merge_size) { |
| 1522 | PyMem_FREE(remain); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1523 | return 0; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1524 | } |
| 1525 | set_mro_error(to_merge, remain); |
| 1526 | PyMem_FREE(remain); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1527 | return -1; |
| 1528 | } |
| 1529 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1530 | static PyObject * |
| 1531 | mro_implementation(PyTypeObject *type) |
| 1532 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1533 | Py_ssize_t i, n; |
| 1534 | int ok; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1535 | PyObject *bases, *result; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1536 | PyObject *to_merge, *bases_aslist; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1537 | |
Neal Norwitz | e7bb918 | 2008-01-27 17:10:14 +0000 | [diff] [blame] | 1538 | if (type->tp_dict == NULL) { |
| 1539 | if (PyType_Ready(type) < 0) |
Guido van Rossum | 6351757 | 2002-06-18 16:44:57 +0000 | [diff] [blame] | 1540 | return NULL; |
| 1541 | } |
| 1542 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1543 | /* Find a superclass linearization that honors the constraints |
| 1544 | of the explicit lists of bases and the constraints implied by |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1545 | each base class. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1546 | |
| 1547 | to_merge is a list of lists, where each list is a superclass |
| 1548 | linearization implied by a base class. The last element of |
| 1549 | to_merge is the declared list of bases. |
| 1550 | */ |
| 1551 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1552 | bases = type->tp_bases; |
| 1553 | n = PyTuple_GET_SIZE(bases); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1554 | |
| 1555 | to_merge = PyList_New(n+1); |
| 1556 | if (to_merge == NULL) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1557 | return NULL; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1558 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1559 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1560 | PyObject *base = PyTuple_GET_ITEM(bases, i); |
| 1561 | PyObject *parentMRO; |
| 1562 | if (PyType_Check(base)) |
| 1563 | parentMRO = PySequence_List( |
| 1564 | ((PyTypeObject*)base)->tp_mro); |
| 1565 | else |
| 1566 | parentMRO = classic_mro(base); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1567 | if (parentMRO == NULL) { |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1568 | Py_DECREF(to_merge); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1569 | return NULL; |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 1570 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1571 | |
| 1572 | PyList_SET_ITEM(to_merge, i, parentMRO); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1573 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1574 | |
| 1575 | bases_aslist = PySequence_List(bases); |
| 1576 | if (bases_aslist == NULL) { |
| 1577 | Py_DECREF(to_merge); |
| 1578 | return NULL; |
| 1579 | } |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1580 | /* This is just a basic sanity check. */ |
| 1581 | if (check_duplicates(bases_aslist) < 0) { |
| 1582 | Py_DECREF(to_merge); |
| 1583 | Py_DECREF(bases_aslist); |
| 1584 | return NULL; |
| 1585 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1586 | PyList_SET_ITEM(to_merge, n, bases_aslist); |
| 1587 | |
| 1588 | result = Py_BuildValue("[O]", (PyObject *)type); |
| 1589 | if (result == NULL) { |
| 1590 | Py_DECREF(to_merge); |
| 1591 | return NULL; |
| 1592 | } |
| 1593 | |
| 1594 | ok = pmerge(result, to_merge); |
| 1595 | Py_DECREF(to_merge); |
| 1596 | if (ok < 0) { |
| 1597 | Py_DECREF(result); |
| 1598 | return NULL; |
| 1599 | } |
| 1600 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1601 | return result; |
| 1602 | } |
| 1603 | |
| 1604 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1605 | mro_external(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1606 | { |
| 1607 | PyTypeObject *type = (PyTypeObject *)self; |
| 1608 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1609 | return mro_implementation(type); |
| 1610 | } |
| 1611 | |
| 1612 | static int |
| 1613 | mro_internal(PyTypeObject *type) |
| 1614 | { |
| 1615 | PyObject *mro, *result, *tuple; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1616 | int checkit = 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1617 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1618 | if (Py_TYPE(type) == &PyType_Type) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1619 | result = mro_implementation(type); |
| 1620 | } |
| 1621 | else { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1622 | static PyObject *mro_str; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1623 | checkit = 1; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1624 | mro = lookup_method((PyObject *)type, "mro", &mro_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1625 | if (mro == NULL) |
| 1626 | return -1; |
| 1627 | result = PyObject_CallObject(mro, NULL); |
| 1628 | Py_DECREF(mro); |
| 1629 | } |
| 1630 | if (result == NULL) |
| 1631 | return -1; |
| 1632 | tuple = PySequence_Tuple(result); |
| 1633 | Py_DECREF(result); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1634 | if (tuple == NULL) |
| 1635 | return -1; |
| 1636 | if (checkit) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1637 | Py_ssize_t i, len; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1638 | PyObject *cls; |
| 1639 | PyTypeObject *solid; |
| 1640 | |
| 1641 | solid = solid_base(type); |
| 1642 | |
| 1643 | len = PyTuple_GET_SIZE(tuple); |
| 1644 | |
| 1645 | for (i = 0; i < len; i++) { |
| 1646 | PyTypeObject *t; |
| 1647 | cls = PyTuple_GET_ITEM(tuple, i); |
| 1648 | if (PyClass_Check(cls)) |
| 1649 | continue; |
| 1650 | else if (!PyType_Check(cls)) { |
| 1651 | PyErr_Format(PyExc_TypeError, |
| 1652 | "mro() returned a non-class ('%.500s')", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1653 | Py_TYPE(cls)->tp_name); |
Neal Norwitz | 50bf51a | 2006-01-02 02:46:54 +0000 | [diff] [blame] | 1654 | Py_DECREF(tuple); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1655 | return -1; |
| 1656 | } |
| 1657 | t = (PyTypeObject*)cls; |
| 1658 | if (!PyType_IsSubtype(solid, solid_base(t))) { |
| 1659 | PyErr_Format(PyExc_TypeError, |
| 1660 | "mro() returned base with unsuitable layout ('%.500s')", |
| 1661 | t->tp_name); |
Neal Norwitz | 50bf51a | 2006-01-02 02:46:54 +0000 | [diff] [blame] | 1662 | Py_DECREF(tuple); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1663 | return -1; |
| 1664 | } |
| 1665 | } |
| 1666 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1667 | type->tp_mro = tuple; |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 1668 | |
| 1669 | type_mro_modified(type, type->tp_mro); |
| 1670 | /* corner case: the old-style super class might have been hidden |
| 1671 | from the custom MRO */ |
| 1672 | type_mro_modified(type, type->tp_bases); |
| 1673 | |
Georg Brandl | 74a1dea | 2008-05-28 11:21:39 +0000 | [diff] [blame] | 1674 | PyType_Modified(type); |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 1675 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1676 | return 0; |
| 1677 | } |
| 1678 | |
| 1679 | |
| 1680 | /* Calculate the best base amongst multiple base classes. |
Armin Rigo | c0ba52d | 2007-04-19 14:44:48 +0000 | [diff] [blame] | 1681 | 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] | 1682 | |
| 1683 | static PyTypeObject * |
| 1684 | best_base(PyObject *bases) |
| 1685 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1686 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1687 | PyTypeObject *base, *winner, *candidate, *base_i; |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1688 | PyObject *base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1689 | |
| 1690 | assert(PyTuple_Check(bases)); |
| 1691 | n = PyTuple_GET_SIZE(bases); |
| 1692 | assert(n > 0); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1693 | base = NULL; |
| 1694 | winner = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1695 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1696 | base_proto = PyTuple_GET_ITEM(bases, i); |
| 1697 | if (PyClass_Check(base_proto)) |
| 1698 | continue; |
Armin Rigo | c0ba52d | 2007-04-19 14:44:48 +0000 | [diff] [blame] | 1699 | if (!PyType_Check(base_proto)) { |
| 1700 | PyErr_SetString( |
| 1701 | PyExc_TypeError, |
| 1702 | "bases must be types"); |
| 1703 | return NULL; |
| 1704 | } |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1705 | base_i = (PyTypeObject *)base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1706 | if (base_i->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1707 | if (PyType_Ready(base_i) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1708 | return NULL; |
| 1709 | } |
| 1710 | candidate = solid_base(base_i); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 1711 | if (winner == NULL) { |
| 1712 | winner = candidate; |
| 1713 | base = base_i; |
| 1714 | } |
| 1715 | else if (PyType_IsSubtype(winner, candidate)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1716 | ; |
| 1717 | else if (PyType_IsSubtype(candidate, winner)) { |
| 1718 | winner = candidate; |
| 1719 | base = base_i; |
| 1720 | } |
| 1721 | else { |
| 1722 | PyErr_SetString( |
| 1723 | PyExc_TypeError, |
| 1724 | "multiple bases have " |
| 1725 | "instance lay-out conflict"); |
| 1726 | return NULL; |
| 1727 | } |
| 1728 | } |
Guido van Rossum | e54616c | 2001-12-14 04:19:56 +0000 | [diff] [blame] | 1729 | if (base == NULL) |
| 1730 | PyErr_SetString(PyExc_TypeError, |
| 1731 | "a new-style class can't have only classic bases"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1732 | return base; |
| 1733 | } |
| 1734 | |
| 1735 | static int |
| 1736 | extra_ivars(PyTypeObject *type, PyTypeObject *base) |
| 1737 | { |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 1738 | size_t t_size = type->tp_basicsize; |
| 1739 | size_t b_size = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1740 | |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1741 | assert(t_size >= b_size); /* Else type smaller than base! */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1742 | if (type->tp_itemsize || base->tp_itemsize) { |
| 1743 | /* If itemsize is involved, stricter rules */ |
| 1744 | return t_size != b_size || |
| 1745 | type->tp_itemsize != base->tp_itemsize; |
| 1746 | } |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1747 | if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 && |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1748 | type->tp_weaklistoffset + sizeof(PyObject *) == t_size && |
| 1749 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1750 | t_size -= sizeof(PyObject *); |
| 1751 | if (type->tp_dictoffset && base->tp_dictoffset == 0 && |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1752 | type->tp_dictoffset + sizeof(PyObject *) == t_size && |
| 1753 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1754 | t_size -= sizeof(PyObject *); |
| 1755 | |
| 1756 | return t_size != b_size; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | static PyTypeObject * |
| 1760 | solid_base(PyTypeObject *type) |
| 1761 | { |
| 1762 | PyTypeObject *base; |
| 1763 | |
| 1764 | if (type->tp_base) |
| 1765 | base = solid_base(type->tp_base); |
| 1766 | else |
| 1767 | base = &PyBaseObject_Type; |
| 1768 | if (extra_ivars(type, base)) |
| 1769 | return type; |
| 1770 | else |
| 1771 | return base; |
| 1772 | } |
| 1773 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1774 | static void object_dealloc(PyObject *); |
| 1775 | static int object_init(PyObject *, PyObject *, PyObject *); |
| 1776 | static int update_slot(PyTypeObject *, PyObject *); |
| 1777 | static void fixup_slot_dispatchers(PyTypeObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1778 | |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1779 | /* |
| 1780 | * Helpers for __dict__ descriptor. We don't want to expose the dicts |
| 1781 | * inherited from various builtin types. The builtin base usually provides |
| 1782 | * its own __dict__ descriptor, so we use that when we can. |
| 1783 | */ |
| 1784 | static PyTypeObject * |
| 1785 | get_builtin_base_with_dict(PyTypeObject *type) |
| 1786 | { |
| 1787 | while (type->tp_base != NULL) { |
| 1788 | if (type->tp_dictoffset != 0 && |
| 1789 | !(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 1790 | return type; |
| 1791 | type = type->tp_base; |
| 1792 | } |
| 1793 | return NULL; |
| 1794 | } |
| 1795 | |
| 1796 | static PyObject * |
| 1797 | get_dict_descriptor(PyTypeObject *type) |
| 1798 | { |
| 1799 | static PyObject *dict_str; |
| 1800 | PyObject *descr; |
| 1801 | |
| 1802 | if (dict_str == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1803 | dict_str = PyString_InternFromString("__dict__"); |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1804 | if (dict_str == NULL) |
| 1805 | return NULL; |
| 1806 | } |
| 1807 | descr = _PyType_Lookup(type, dict_str); |
| 1808 | if (descr == NULL || !PyDescr_IsData(descr)) |
| 1809 | return NULL; |
| 1810 | |
| 1811 | return descr; |
| 1812 | } |
| 1813 | |
| 1814 | static void |
| 1815 | raise_dict_descr_error(PyObject *obj) |
| 1816 | { |
| 1817 | PyErr_Format(PyExc_TypeError, |
| 1818 | "this __dict__ descriptor does not support " |
| 1819 | "'%.200s' objects", obj->ob_type->tp_name); |
| 1820 | } |
| 1821 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1822 | static PyObject * |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1823 | subtype_dict(PyObject *obj, void *context) |
| 1824 | { |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1825 | PyObject **dictptr; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1826 | PyObject *dict; |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1827 | PyTypeObject *base; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1828 | |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1829 | base = get_builtin_base_with_dict(obj->ob_type); |
| 1830 | if (base != NULL) { |
| 1831 | descrgetfunc func; |
| 1832 | PyObject *descr = get_dict_descriptor(base); |
| 1833 | if (descr == NULL) { |
| 1834 | raise_dict_descr_error(obj); |
| 1835 | return NULL; |
| 1836 | } |
| 1837 | func = descr->ob_type->tp_descr_get; |
| 1838 | if (func == NULL) { |
| 1839 | raise_dict_descr_error(obj); |
| 1840 | return NULL; |
| 1841 | } |
| 1842 | return func(descr, obj, (PyObject *)(obj->ob_type)); |
| 1843 | } |
| 1844 | |
| 1845 | dictptr = _PyObject_GetDictPtr(obj); |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1846 | if (dictptr == NULL) { |
| 1847 | PyErr_SetString(PyExc_AttributeError, |
| 1848 | "This object has no __dict__"); |
| 1849 | return NULL; |
| 1850 | } |
| 1851 | dict = *dictptr; |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 1852 | if (dict == NULL) |
| 1853 | *dictptr = dict = PyDict_New(); |
| 1854 | Py_XINCREF(dict); |
| 1855 | return dict; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1858 | static int |
| 1859 | subtype_setdict(PyObject *obj, PyObject *value, void *context) |
| 1860 | { |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1861 | PyObject **dictptr; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1862 | PyObject *dict; |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1863 | PyTypeObject *base; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1864 | |
Armin Rigo | 9790a27 | 2007-05-02 19:23:31 +0000 | [diff] [blame] | 1865 | base = get_builtin_base_with_dict(obj->ob_type); |
| 1866 | if (base != NULL) { |
| 1867 | descrsetfunc func; |
| 1868 | PyObject *descr = get_dict_descriptor(base); |
| 1869 | if (descr == NULL) { |
| 1870 | raise_dict_descr_error(obj); |
| 1871 | return -1; |
| 1872 | } |
| 1873 | func = descr->ob_type->tp_descr_set; |
| 1874 | if (func == NULL) { |
| 1875 | raise_dict_descr_error(obj); |
| 1876 | return -1; |
| 1877 | } |
| 1878 | return func(descr, obj, value); |
| 1879 | } |
| 1880 | |
| 1881 | dictptr = _PyObject_GetDictPtr(obj); |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1882 | if (dictptr == NULL) { |
| 1883 | PyErr_SetString(PyExc_AttributeError, |
| 1884 | "This object has no __dict__"); |
| 1885 | return -1; |
| 1886 | } |
Guido van Rossum | d331cb5 | 2001-12-05 19:46:42 +0000 | [diff] [blame] | 1887 | if (value != NULL && !PyDict_Check(value)) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 1888 | PyErr_Format(PyExc_TypeError, |
| 1889 | "__dict__ must be set to a dictionary, " |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1890 | "not a '%.200s'", Py_TYPE(value)->tp_name); |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1891 | return -1; |
| 1892 | } |
| 1893 | dict = *dictptr; |
Guido van Rossum | d331cb5 | 2001-12-05 19:46:42 +0000 | [diff] [blame] | 1894 | Py_XINCREF(value); |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1895 | *dictptr = value; |
| 1896 | Py_XDECREF(dict); |
| 1897 | return 0; |
| 1898 | } |
| 1899 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1900 | static PyObject * |
| 1901 | subtype_getweakref(PyObject *obj, void *context) |
| 1902 | { |
| 1903 | PyObject **weaklistptr; |
| 1904 | PyObject *result; |
| 1905 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1906 | if (Py_TYPE(obj)->tp_weaklistoffset == 0) { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1907 | PyErr_SetString(PyExc_AttributeError, |
Fred Drake | 7a36f5f | 2006-08-04 05:17:21 +0000 | [diff] [blame] | 1908 | "This object has no __weakref__"); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1909 | return NULL; |
| 1910 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1911 | assert(Py_TYPE(obj)->tp_weaklistoffset > 0); |
| 1912 | assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <= |
| 1913 | (size_t)(Py_TYPE(obj)->tp_basicsize)); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1914 | weaklistptr = (PyObject **) |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1915 | ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1916 | if (*weaklistptr == NULL) |
| 1917 | result = Py_None; |
| 1918 | else |
| 1919 | result = *weaklistptr; |
| 1920 | Py_INCREF(result); |
| 1921 | return result; |
| 1922 | } |
| 1923 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1924 | /* Three variants on the subtype_getsets list. */ |
| 1925 | |
| 1926 | static PyGetSetDef subtype_getsets_full[] = { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1927 | {"__dict__", subtype_dict, subtype_setdict, |
Neal Norwitz | 858e34f | 2002-08-13 17:18:45 +0000 | [diff] [blame] | 1928 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1929 | {"__weakref__", subtype_getweakref, NULL, |
Neal Norwitz | 858e34f | 2002-08-13 17:18:45 +0000 | [diff] [blame] | 1930 | PyDoc_STR("list of weak references to the object (if defined)")}, |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1931 | {0} |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1932 | }; |
| 1933 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1934 | static PyGetSetDef subtype_getsets_dict_only[] = { |
| 1935 | {"__dict__", subtype_dict, subtype_setdict, |
| 1936 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 1937 | {0} |
| 1938 | }; |
| 1939 | |
| 1940 | static PyGetSetDef subtype_getsets_weakref_only[] = { |
| 1941 | {"__weakref__", subtype_getweakref, NULL, |
| 1942 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 1943 | {0} |
| 1944 | }; |
| 1945 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1946 | static int |
| 1947 | valid_identifier(PyObject *s) |
| 1948 | { |
Guido van Rossum | 03013a0 | 2002-07-16 14:30:28 +0000 | [diff] [blame] | 1949 | unsigned char *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1950 | Py_ssize_t i, n; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1951 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1952 | if (!PyString_Check(s)) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 1953 | PyErr_Format(PyExc_TypeError, |
| 1954 | "__slots__ items must be strings, not '%.200s'", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1955 | Py_TYPE(s)->tp_name); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1956 | return 0; |
| 1957 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1958 | p = (unsigned char *) PyString_AS_STRING(s); |
| 1959 | n = PyString_GET_SIZE(s); |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1960 | /* We must reject an empty name. As a hack, we bump the |
| 1961 | length to 1 so that the loop will balk on the trailing \0. */ |
| 1962 | if (n == 0) |
| 1963 | n = 1; |
| 1964 | for (i = 0; i < n; i++, p++) { |
| 1965 | if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') { |
| 1966 | PyErr_SetString(PyExc_TypeError, |
| 1967 | "__slots__ must be identifiers"); |
| 1968 | return 0; |
| 1969 | } |
| 1970 | } |
| 1971 | return 1; |
| 1972 | } |
| 1973 | |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1974 | #ifdef Py_USING_UNICODE |
| 1975 | /* Replace Unicode objects in slots. */ |
| 1976 | |
| 1977 | static PyObject * |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 1978 | _unicode_to_string(PyObject *slots, Py_ssize_t nslots) |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1979 | { |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1980 | PyObject *tmp = NULL; |
| 1981 | PyObject *slot_name, *new_name; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 1982 | Py_ssize_t i; |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1983 | |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1984 | for (i = 0; i < nslots; i++) { |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1985 | if (PyUnicode_Check(slot_name = PyTuple_GET_ITEM(slots, i))) { |
| 1986 | if (tmp == NULL) { |
| 1987 | tmp = PySequence_List(slots); |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1988 | if (tmp == NULL) |
| 1989 | return NULL; |
| 1990 | } |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1991 | new_name = _PyUnicode_AsDefaultEncodedString(slot_name, |
| 1992 | NULL); |
| 1993 | if (new_name == NULL) { |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1994 | Py_DECREF(tmp); |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1995 | return NULL; |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 1996 | } |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 1997 | Py_INCREF(new_name); |
| 1998 | PyList_SET_ITEM(tmp, i, new_name); |
| 1999 | Py_DECREF(slot_name); |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 2000 | } |
| 2001 | } |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 2002 | if (tmp != NULL) { |
| 2003 | slots = PyList_AsTuple(tmp); |
| 2004 | Py_DECREF(tmp); |
| 2005 | } |
| 2006 | return slots; |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 2007 | } |
| 2008 | #endif |
| 2009 | |
Guido van Rossum | f102e24 | 2007-03-23 18:53:03 +0000 | [diff] [blame] | 2010 | /* Forward */ |
| 2011 | static int |
| 2012 | object_init(PyObject *self, PyObject *args, PyObject *kwds); |
| 2013 | |
| 2014 | static int |
| 2015 | type_init(PyObject *cls, PyObject *args, PyObject *kwds) |
| 2016 | { |
| 2017 | int res; |
| 2018 | |
| 2019 | assert(args != NULL && PyTuple_Check(args)); |
| 2020 | assert(kwds == NULL || PyDict_Check(kwds)); |
| 2021 | |
| 2022 | if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) { |
| 2023 | PyErr_SetString(PyExc_TypeError, |
| 2024 | "type.__init__() takes no keyword arguments"); |
| 2025 | return -1; |
| 2026 | } |
| 2027 | |
| 2028 | if (args != NULL && PyTuple_Check(args) && |
| 2029 | (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) { |
| 2030 | PyErr_SetString(PyExc_TypeError, |
| 2031 | "type.__init__() takes 1 or 3 arguments"); |
| 2032 | return -1; |
| 2033 | } |
| 2034 | |
| 2035 | /* Call object.__init__(self) now. */ |
| 2036 | /* XXX Could call super(type, cls).__init__() but what's the point? */ |
| 2037 | args = PyTuple_GetSlice(args, 0, 0); |
| 2038 | res = object_init(cls, args, NULL); |
| 2039 | Py_DECREF(args); |
| 2040 | return res; |
| 2041 | } |
| 2042 | |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 2043 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2044 | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) |
| 2045 | { |
| 2046 | PyObject *name, *bases, *dict; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 2047 | static char *kwlist[] = {"name", "bases", "dict", 0}; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2048 | PyObject *slots, *tmp, *newslots; |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 2049 | PyTypeObject *type, *base, *tmptype, *winner; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2050 | PyHeapTypeObject *et; |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 2051 | PyMemberDef *mp; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2052 | Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2053 | int j, may_add_dict, may_add_weak; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2054 | |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2055 | assert(args != NULL && PyTuple_Check(args)); |
| 2056 | assert(kwds == NULL || PyDict_Check(kwds)); |
| 2057 | |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 2058 | /* Special case: type(x) should return x->ob_type */ |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2059 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2060 | const Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 2061 | const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2062 | |
| 2063 | if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) { |
| 2064 | PyObject *x = PyTuple_GET_ITEM(args, 0); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2065 | Py_INCREF(Py_TYPE(x)); |
| 2066 | return (PyObject *) Py_TYPE(x); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | /* SF bug 475327 -- if that didn't trigger, we need 3 |
| 2070 | arguments. but PyArg_ParseTupleAndKeywords below may give |
| 2071 | a msg saying type() needs exactly 3. */ |
| 2072 | if (nargs + nkwds != 3) { |
| 2073 | PyErr_SetString(PyExc_TypeError, |
| 2074 | "type() takes 1 or 3 arguments"); |
| 2075 | return NULL; |
| 2076 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 2079 | /* Check arguments: (name, bases, dict) */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2080 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist, |
| 2081 | &name, |
| 2082 | &PyTuple_Type, &bases, |
| 2083 | &PyDict_Type, &dict)) |
| 2084 | return NULL; |
| 2085 | |
Armin Rigo | c0ba52d | 2007-04-19 14:44:48 +0000 | [diff] [blame] | 2086 | /* Determine the proper metatype to deal with this, |
| 2087 | and check for metatype conflicts while we're at it. |
| 2088 | Note that if some other metatype wins to contract, |
| 2089 | it's possible that its instances are not types. */ |
| 2090 | nbases = PyTuple_GET_SIZE(bases); |
| 2091 | winner = metatype; |
| 2092 | for (i = 0; i < nbases; i++) { |
| 2093 | tmp = PyTuple_GET_ITEM(bases, i); |
| 2094 | tmptype = tmp->ob_type; |
| 2095 | if (tmptype == &PyClass_Type) |
| 2096 | continue; /* Special case classic classes */ |
| 2097 | if (PyType_IsSubtype(winner, tmptype)) |
| 2098 | continue; |
| 2099 | if (PyType_IsSubtype(tmptype, winner)) { |
| 2100 | winner = tmptype; |
| 2101 | continue; |
Jeremy Hylton | fa95569 | 2007-02-27 18:29:45 +0000 | [diff] [blame] | 2102 | } |
Armin Rigo | c0ba52d | 2007-04-19 14:44:48 +0000 | [diff] [blame] | 2103 | PyErr_SetString(PyExc_TypeError, |
| 2104 | "metaclass conflict: " |
| 2105 | "the metaclass of a derived class " |
| 2106 | "must be a (non-strict) subclass " |
| 2107 | "of the metaclasses of all its bases"); |
| 2108 | return NULL; |
| 2109 | } |
| 2110 | if (winner != metatype) { |
| 2111 | if (winner->tp_new != type_new) /* Pass it to the winner */ |
| 2112 | return winner->tp_new(winner, args, kwds); |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 2113 | metatype = winner; |
| 2114 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2115 | |
| 2116 | /* Adjust for empty tuple bases */ |
| 2117 | if (nbases == 0) { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2118 | bases = PyTuple_Pack(1, &PyBaseObject_Type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2119 | if (bases == NULL) |
| 2120 | return NULL; |
| 2121 | nbases = 1; |
| 2122 | } |
| 2123 | else |
| 2124 | Py_INCREF(bases); |
| 2125 | |
| 2126 | /* XXX From here until type is allocated, "return NULL" leaks bases! */ |
| 2127 | |
| 2128 | /* Calculate best base, and check that all bases are type objects */ |
| 2129 | base = best_base(bases); |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2130 | if (base == NULL) { |
| 2131 | Py_DECREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2132 | return NULL; |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2133 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2134 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 2135 | PyErr_Format(PyExc_TypeError, |
| 2136 | "type '%.100s' is not an acceptable base type", |
| 2137 | base->tp_name); |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2138 | Py_DECREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2139 | return NULL; |
| 2140 | } |
| 2141 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2142 | /* Check for a __slots__ sequence variable in dict, and count it */ |
| 2143 | slots = PyDict_GetItemString(dict, "__slots__"); |
| 2144 | nslots = 0; |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 2145 | add_dict = 0; |
| 2146 | add_weak = 0; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2147 | may_add_dict = base->tp_dictoffset == 0; |
| 2148 | may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0; |
| 2149 | if (slots == NULL) { |
| 2150 | if (may_add_dict) { |
| 2151 | add_dict++; |
| 2152 | } |
| 2153 | if (may_add_weak) { |
| 2154 | add_weak++; |
| 2155 | } |
| 2156 | } |
| 2157 | else { |
| 2158 | /* Have slots */ |
| 2159 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2160 | /* Make it into a tuple */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2161 | if (PyString_Check(slots) || PyUnicode_Check(slots)) |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2162 | slots = PyTuple_Pack(1, slots); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2163 | else |
| 2164 | slots = PySequence_Tuple(slots); |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2165 | if (slots == NULL) { |
| 2166 | Py_DECREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2167 | return NULL; |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2168 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2169 | assert(PyTuple_Check(slots)); |
| 2170 | |
| 2171 | /* Are slots allowed? */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2172 | nslots = PyTuple_GET_SIZE(slots); |
Jeremy Hylton | 1c7a0ea | 2003-07-16 16:08:23 +0000 | [diff] [blame] | 2173 | if (nslots > 0 && base->tp_itemsize != 0) { |
Guido van Rossum | c414187 | 2001-08-30 04:43:35 +0000 | [diff] [blame] | 2174 | PyErr_Format(PyExc_TypeError, |
| 2175 | "nonempty __slots__ " |
| 2176 | "not supported for subtype of '%s'", |
| 2177 | base->tp_name); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2178 | bad_slots: |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2179 | Py_DECREF(bases); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2180 | Py_DECREF(slots); |
Guido van Rossum | c414187 | 2001-08-30 04:43:35 +0000 | [diff] [blame] | 2181 | return NULL; |
| 2182 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2183 | |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 2184 | #ifdef Py_USING_UNICODE |
| 2185 | tmp = _unicode_to_string(slots, nslots); |
Žiga Seilnacht | 71436f0 | 2007-03-14 12:24:09 +0000 | [diff] [blame] | 2186 | if (tmp == NULL) |
| 2187 | goto bad_slots; |
Martin v. Löwis | 13b1a5c | 2002-10-14 21:11:34 +0000 | [diff] [blame] | 2188 | if (tmp != slots) { |
| 2189 | Py_DECREF(slots); |
| 2190 | slots = tmp; |
| 2191 | } |
Martin v. Löwis | d919a59 | 2002-10-14 21:07:28 +0000 | [diff] [blame] | 2192 | #endif |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2193 | /* Check for valid slot names and two special cases */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2194 | for (i = 0; i < nslots; i++) { |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2195 | PyObject *tmp = PyTuple_GET_ITEM(slots, i); |
| 2196 | char *s; |
| 2197 | if (!valid_identifier(tmp)) |
| 2198 | goto bad_slots; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2199 | assert(PyString_Check(tmp)); |
| 2200 | s = PyString_AS_STRING(tmp); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2201 | if (strcmp(s, "__dict__") == 0) { |
| 2202 | if (!may_add_dict || add_dict) { |
| 2203 | PyErr_SetString(PyExc_TypeError, |
| 2204 | "__dict__ slot disallowed: " |
| 2205 | "we already got one"); |
| 2206 | goto bad_slots; |
| 2207 | } |
| 2208 | add_dict++; |
| 2209 | } |
| 2210 | if (strcmp(s, "__weakref__") == 0) { |
| 2211 | if (!may_add_weak || add_weak) { |
| 2212 | PyErr_SetString(PyExc_TypeError, |
| 2213 | "__weakref__ slot disallowed: " |
| 2214 | "either we already got one, " |
| 2215 | "or __itemsize__ != 0"); |
| 2216 | goto bad_slots; |
| 2217 | } |
| 2218 | add_weak++; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2219 | } |
| 2220 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2221 | |
Žiga Seilnacht | 6f2d09c | 2007-03-16 11:59:38 +0000 | [diff] [blame] | 2222 | /* Copy slots into a list, mangle names and sort them. |
| 2223 | Sorted names are needed for __class__ assignment. |
| 2224 | Convert them back to tuple at the end. |
| 2225 | */ |
| 2226 | newslots = PyList_New(nslots - add_dict - add_weak); |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2227 | if (newslots == NULL) |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2228 | goto bad_slots; |
| 2229 | for (i = j = 0; i < nslots; i++) { |
| 2230 | char *s; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2231 | tmp = PyTuple_GET_ITEM(slots, i); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2232 | s = PyString_AS_STRING(tmp); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2233 | if ((add_dict && strcmp(s, "__dict__") == 0) || |
| 2234 | (add_weak && strcmp(s, "__weakref__") == 0)) |
| 2235 | continue; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2236 | tmp =_Py_Mangle(name, tmp); |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2237 | if (!tmp) |
| 2238 | goto bad_slots; |
Žiga Seilnacht | 6f2d09c | 2007-03-16 11:59:38 +0000 | [diff] [blame] | 2239 | PyList_SET_ITEM(newslots, j, tmp); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2240 | j++; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2241 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2242 | assert(j == nslots - add_dict - add_weak); |
| 2243 | nslots = j; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2244 | Py_DECREF(slots); |
Žiga Seilnacht | 6f2d09c | 2007-03-16 11:59:38 +0000 | [diff] [blame] | 2245 | if (PyList_Sort(newslots) == -1) { |
| 2246 | Py_DECREF(bases); |
| 2247 | Py_DECREF(newslots); |
| 2248 | return NULL; |
| 2249 | } |
| 2250 | slots = PyList_AsTuple(newslots); |
| 2251 | Py_DECREF(newslots); |
| 2252 | if (slots == NULL) { |
| 2253 | Py_DECREF(bases); |
| 2254 | return NULL; |
| 2255 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2256 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2257 | /* Secondary bases may provide weakrefs or dict */ |
| 2258 | if (nbases > 1 && |
| 2259 | ((may_add_dict && !add_dict) || |
| 2260 | (may_add_weak && !add_weak))) { |
| 2261 | for (i = 0; i < nbases; i++) { |
| 2262 | tmp = PyTuple_GET_ITEM(bases, i); |
| 2263 | if (tmp == (PyObject *)base) |
| 2264 | continue; /* Skip primary base */ |
| 2265 | if (PyClass_Check(tmp)) { |
| 2266 | /* Classic base class provides both */ |
| 2267 | if (may_add_dict && !add_dict) |
| 2268 | add_dict++; |
| 2269 | if (may_add_weak && !add_weak) |
| 2270 | add_weak++; |
| 2271 | break; |
| 2272 | } |
| 2273 | assert(PyType_Check(tmp)); |
| 2274 | tmptype = (PyTypeObject *)tmp; |
| 2275 | if (may_add_dict && !add_dict && |
| 2276 | tmptype->tp_dictoffset != 0) |
| 2277 | add_dict++; |
| 2278 | if (may_add_weak && !add_weak && |
| 2279 | tmptype->tp_weaklistoffset != 0) |
| 2280 | add_weak++; |
| 2281 | if (may_add_dict && !add_dict) |
| 2282 | continue; |
| 2283 | if (may_add_weak && !add_weak) |
| 2284 | continue; |
| 2285 | /* Nothing more to check */ |
| 2286 | break; |
| 2287 | } |
| 2288 | } |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 2289 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2290 | |
| 2291 | /* XXX From here until type is safely allocated, |
| 2292 | "return NULL" may leak slots! */ |
| 2293 | |
| 2294 | /* Allocate the type object */ |
| 2295 | type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2296 | if (type == NULL) { |
| 2297 | Py_XDECREF(slots); |
Michael W. Hudson | a6a277d | 2003-08-08 13:57:22 +0000 | [diff] [blame] | 2298 | Py_DECREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2299 | return NULL; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2300 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2301 | |
| 2302 | /* Keep name and slots alive in the extended type object */ |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2303 | et = (PyHeapTypeObject *)type; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2304 | Py_INCREF(name); |
Georg Brandl | c255c7b | 2006-02-20 22:27:28 +0000 | [diff] [blame] | 2305 | et->ht_name = name; |
| 2306 | et->ht_slots = slots; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2307 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2308 | /* Initialize tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2309 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | |
| 2310 | Py_TPFLAGS_BASETYPE; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2311 | if (base->tp_flags & Py_TPFLAGS_HAVE_GC) |
| 2312 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 2313 | if (base->tp_flags & Py_TPFLAGS_HAVE_NEWBUFFER) |
| 2314 | type->tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2315 | |
| 2316 | /* It's a new-style number unless it specifically inherits any |
| 2317 | old-style numeric behavior */ |
| 2318 | if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) || |
| 2319 | (base->tp_as_number == NULL)) |
| 2320 | type->tp_flags |= Py_TPFLAGS_CHECKTYPES; |
| 2321 | |
| 2322 | /* Initialize essential fields */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2323 | type->tp_as_number = &et->as_number; |
| 2324 | type->tp_as_sequence = &et->as_sequence; |
| 2325 | type->tp_as_mapping = &et->as_mapping; |
| 2326 | type->tp_as_buffer = &et->as_buffer; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2327 | type->tp_name = PyString_AS_STRING(name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2328 | |
| 2329 | /* Set tp_base and tp_bases */ |
| 2330 | type->tp_bases = bases; |
| 2331 | Py_INCREF(base); |
| 2332 | type->tp_base = base; |
| 2333 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2334 | /* Initialize tp_dict from passed-in dict */ |
| 2335 | type->tp_dict = dict = PyDict_Copy(dict); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2336 | if (dict == NULL) { |
| 2337 | Py_DECREF(type); |
| 2338 | return NULL; |
| 2339 | } |
| 2340 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 2341 | /* Set __module__ in the dict */ |
| 2342 | if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 2343 | tmp = PyEval_GetGlobals(); |
| 2344 | if (tmp != NULL) { |
| 2345 | tmp = PyDict_GetItemString(tmp, "__name__"); |
| 2346 | if (tmp != NULL) { |
| 2347 | if (PyDict_SetItemString(dict, "__module__", |
| 2348 | tmp) < 0) |
| 2349 | return NULL; |
| 2350 | } |
| 2351 | } |
| 2352 | } |
| 2353 | |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2354 | /* 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] | 2355 | and is a string. The __doc__ accessor will first look for tp_doc; |
| 2356 | if that fails, it will still look into __dict__. |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2357 | */ |
| 2358 | { |
| 2359 | PyObject *doc = PyDict_GetItemString(dict, "__doc__"); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2360 | if (doc != NULL && PyString_Check(doc)) { |
| 2361 | const size_t n = (size_t)PyString_GET_SIZE(doc); |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2362 | char *tp_doc = (char *)PyObject_MALLOC(n+1); |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 2363 | if (tp_doc == NULL) { |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2364 | Py_DECREF(type); |
| 2365 | return NULL; |
| 2366 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2367 | memcpy(tp_doc, PyString_AS_STRING(doc), n+1); |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2368 | type->tp_doc = tp_doc; |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2369 | } |
| 2370 | } |
| 2371 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2372 | /* Special-case __new__: if it's a plain function, |
| 2373 | make it a static function */ |
| 2374 | tmp = PyDict_GetItemString(dict, "__new__"); |
| 2375 | if (tmp != NULL && PyFunction_Check(tmp)) { |
| 2376 | tmp = PyStaticMethod_New(tmp); |
| 2377 | if (tmp == NULL) { |
| 2378 | Py_DECREF(type); |
| 2379 | return NULL; |
| 2380 | } |
| 2381 | PyDict_SetItemString(dict, "__new__", tmp); |
| 2382 | Py_DECREF(tmp); |
| 2383 | } |
| 2384 | |
| 2385 | /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2386 | mp = PyHeapType_GET_MEMBERS(et); |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 2387 | slotoffset = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2388 | if (slots != NULL) { |
| 2389 | for (i = 0; i < nslots; i++, mp++) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2390 | mp->name = PyString_AS_STRING( |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2391 | PyTuple_GET_ITEM(slots, i)); |
Guido van Rossum | 64b206c | 2001-12-04 17:13:22 +0000 | [diff] [blame] | 2392 | mp->type = T_OBJECT_EX; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2393 | mp->offset = slotoffset; |
Žiga Seilnacht | 8903208 | 2007-03-11 15:54:54 +0000 | [diff] [blame] | 2394 | |
| 2395 | /* __dict__ and __weakref__ are already filtered out */ |
| 2396 | assert(strcmp(mp->name, "__dict__") != 0); |
| 2397 | assert(strcmp(mp->name, "__weakref__") != 0); |
| 2398 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2399 | slotoffset += sizeof(PyObject *); |
| 2400 | } |
| 2401 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2402 | if (add_dict) { |
| 2403 | if (base->tp_itemsize) |
| 2404 | type->tp_dictoffset = -(long)sizeof(PyObject *); |
| 2405 | else |
| 2406 | type->tp_dictoffset = slotoffset; |
| 2407 | slotoffset += sizeof(PyObject *); |
| 2408 | } |
| 2409 | if (add_weak) { |
| 2410 | assert(!base->tp_itemsize); |
| 2411 | type->tp_weaklistoffset = slotoffset; |
| 2412 | slotoffset += sizeof(PyObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2413 | } |
| 2414 | type->tp_basicsize = slotoffset; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 2415 | type->tp_itemsize = base->tp_itemsize; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2416 | type->tp_members = PyHeapType_GET_MEMBERS(et); |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2417 | |
| 2418 | if (type->tp_weaklistoffset && type->tp_dictoffset) |
| 2419 | type->tp_getset = subtype_getsets_full; |
| 2420 | else if (type->tp_weaklistoffset && !type->tp_dictoffset) |
| 2421 | type->tp_getset = subtype_getsets_weakref_only; |
| 2422 | else if (!type->tp_weaklistoffset && type->tp_dictoffset) |
| 2423 | type->tp_getset = subtype_getsets_dict_only; |
| 2424 | else |
| 2425 | type->tp_getset = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2426 | |
| 2427 | /* Special case some slots */ |
| 2428 | if (type->tp_dictoffset != 0 || nslots > 0) { |
| 2429 | if (base->tp_getattr == NULL && base->tp_getattro == NULL) |
| 2430 | type->tp_getattro = PyObject_GenericGetAttr; |
| 2431 | if (base->tp_setattr == NULL && base->tp_setattro == NULL) |
| 2432 | type->tp_setattro = PyObject_GenericSetAttr; |
| 2433 | } |
| 2434 | type->tp_dealloc = subtype_dealloc; |
| 2435 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 2436 | /* Enable GC unless there are really no instance variables possible */ |
| 2437 | if (!(type->tp_basicsize == sizeof(PyObject) && |
| 2438 | type->tp_itemsize == 0)) |
| 2439 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
| 2440 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2441 | /* Always override allocation strategy to use regular heap */ |
| 2442 | type->tp_alloc = PyType_GenericAlloc; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2443 | if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 2444 | type->tp_free = PyObject_GC_Del; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 2445 | type->tp_traverse = subtype_traverse; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 2446 | type->tp_clear = subtype_clear; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2447 | } |
| 2448 | else |
Neil Schemenauer | 09a2ae5 | 2002-04-12 03:06:53 +0000 | [diff] [blame] | 2449 | type->tp_free = PyObject_Del; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2450 | |
| 2451 | /* Initialize the rest */ |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 2452 | if (PyType_Ready(type) < 0) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2453 | Py_DECREF(type); |
| 2454 | return NULL; |
| 2455 | } |
| 2456 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 2457 | /* Put the proper slots in place */ |
| 2458 | fixup_slot_dispatchers(type); |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 2459 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2460 | return (PyObject *)type; |
| 2461 | } |
| 2462 | |
| 2463 | /* Internal API to look for a name through the MRO. |
| 2464 | This returns a borrowed reference, and doesn't set an exception! */ |
| 2465 | PyObject * |
| 2466 | _PyType_Lookup(PyTypeObject *type, PyObject *name) |
| 2467 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2468 | Py_ssize_t i, n; |
Georg Brandl | f18a707 | 2008-05-29 14:35:39 +0000 | [diff] [blame] | 2469 | PyObject *mro, *res, *base, *dict; |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 2470 | unsigned int h; |
| 2471 | |
| 2472 | if (MCACHE_CACHEABLE_NAME(name) && |
Neal Norwitz | e7bb918 | 2008-01-27 17:10:14 +0000 | [diff] [blame] | 2473 | PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) { |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 2474 | /* fast path */ |
| 2475 | h = MCACHE_HASH_METHOD(type, name); |
| 2476 | if (method_cache[h].version == type->tp_version_tag && |
| 2477 | method_cache[h].name == name) |
| 2478 | return method_cache[h].value; |
| 2479 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2480 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2481 | /* Look in tp_dict of types in MRO */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2482 | mro = type->tp_mro; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 2483 | |
| 2484 | /* If mro is NULL, the type is either not yet initialized |
| 2485 | by PyType_Ready(), or already cleared by type_clear(). |
| 2486 | Either way the safest thing to do is to return NULL. */ |
| 2487 | if (mro == NULL) |
| 2488 | return NULL; |
| 2489 | |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 2490 | res = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2491 | assert(PyTuple_Check(mro)); |
| 2492 | n = PyTuple_GET_SIZE(mro); |
| 2493 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 2494 | base = PyTuple_GET_ITEM(mro, i); |
| 2495 | if (PyClass_Check(base)) |
| 2496 | dict = ((PyClassObject *)base)->cl_dict; |
| 2497 | else { |
| 2498 | assert(PyType_Check(base)); |
| 2499 | dict = ((PyTypeObject *)base)->tp_dict; |
| 2500 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2501 | assert(dict && PyDict_Check(dict)); |
| 2502 | res = PyDict_GetItem(dict, name); |
| 2503 | if (res != NULL) |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 2504 | break; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2505 | } |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 2506 | |
| 2507 | if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) { |
| 2508 | h = MCACHE_HASH_METHOD(type, name); |
| 2509 | method_cache[h].version = type->tp_version_tag; |
| 2510 | method_cache[h].value = res; /* borrowed */ |
| 2511 | Py_INCREF(name); |
Georg Brandl | f18a707 | 2008-05-29 14:35:39 +0000 | [diff] [blame] | 2512 | Py_DECREF(method_cache[h].name); |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 2513 | method_cache[h].name = name; |
| 2514 | } |
| 2515 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | /* This is similar to PyObject_GenericGetAttr(), |
| 2519 | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ |
| 2520 | static PyObject * |
| 2521 | type_getattro(PyTypeObject *type, PyObject *name) |
| 2522 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2523 | PyTypeObject *metatype = Py_TYPE(type); |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2524 | PyObject *meta_attribute, *attribute; |
| 2525 | descrgetfunc meta_get; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2526 | |
| 2527 | /* Initialize this type (we'll assume the metatype is initialized) */ |
| 2528 | if (type->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 2529 | if (PyType_Ready(type) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2530 | return NULL; |
| 2531 | } |
| 2532 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2533 | /* No readable descriptor found yet */ |
| 2534 | meta_get = NULL; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2535 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2536 | /* Look for the attribute in the metatype */ |
| 2537 | meta_attribute = _PyType_Lookup(metatype, name); |
| 2538 | |
| 2539 | if (meta_attribute != NULL) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2540 | meta_get = Py_TYPE(meta_attribute)->tp_descr_get; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2541 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2542 | if (meta_get != NULL && PyDescr_IsData(meta_attribute)) { |
| 2543 | /* Data descriptors implement tp_descr_set to intercept |
| 2544 | * writes. Assume the attribute is not overridden in |
| 2545 | * type's tp_dict (and bases): call the descriptor now. |
| 2546 | */ |
| 2547 | return meta_get(meta_attribute, (PyObject *)type, |
| 2548 | (PyObject *)metatype); |
| 2549 | } |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2550 | Py_INCREF(meta_attribute); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2553 | /* No data descriptor found on metatype. Look in tp_dict of this |
| 2554 | * type and its bases */ |
| 2555 | attribute = _PyType_Lookup(type, name); |
| 2556 | if (attribute != NULL) { |
| 2557 | /* Implement descriptor functionality, if any */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2558 | descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get; |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2559 | |
| 2560 | Py_XDECREF(meta_attribute); |
| 2561 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2562 | if (local_get != NULL) { |
| 2563 | /* NULL 2nd argument indicates the descriptor was |
| 2564 | * found on the target object itself (or a base) */ |
| 2565 | return local_get(attribute, (PyObject *)NULL, |
| 2566 | (PyObject *)type); |
| 2567 | } |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2568 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2569 | Py_INCREF(attribute); |
| 2570 | return attribute; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2573 | /* No attribute found in local __dict__ (or bases): use the |
| 2574 | * descriptor from the metatype, if any */ |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2575 | if (meta_get != NULL) { |
| 2576 | PyObject *res; |
| 2577 | res = meta_get(meta_attribute, (PyObject *)type, |
| 2578 | (PyObject *)metatype); |
| 2579 | Py_DECREF(meta_attribute); |
| 2580 | return res; |
| 2581 | } |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2582 | |
| 2583 | /* If an ordinary attribute was found on the metatype, return it now */ |
| 2584 | if (meta_attribute != NULL) { |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2585 | return meta_attribute; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2586 | } |
| 2587 | |
| 2588 | /* Give up */ |
| 2589 | PyErr_Format(PyExc_AttributeError, |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2590 | "type object '%.50s' has no attribute '%.400s'", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2591 | type->tp_name, PyString_AS_STRING(name)); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2592 | return NULL; |
| 2593 | } |
| 2594 | |
| 2595 | static int |
| 2596 | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) |
| 2597 | { |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 2598 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 2599 | PyErr_Format( |
| 2600 | PyExc_TypeError, |
| 2601 | "can't set attributes of built-in/extension type '%s'", |
| 2602 | type->tp_name); |
| 2603 | return -1; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 2604 | } |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 2605 | if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0) |
| 2606 | return -1; |
| 2607 | return update_slot(type, name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | static void |
| 2611 | type_dealloc(PyTypeObject *type) |
| 2612 | { |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2613 | PyHeapTypeObject *et; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2614 | |
| 2615 | /* Assert this is a heap-allocated type object */ |
| 2616 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2617 | _PyObject_GC_UNTRACK(type); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2618 | PyObject_ClearWeakRefs((PyObject *)type); |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2619 | et = (PyHeapTypeObject *)type; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2620 | Py_XDECREF(type->tp_base); |
| 2621 | Py_XDECREF(type->tp_dict); |
| 2622 | Py_XDECREF(type->tp_bases); |
| 2623 | Py_XDECREF(type->tp_mro); |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2624 | Py_XDECREF(type->tp_cache); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2625 | Py_XDECREF(type->tp_subclasses); |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2626 | /* A type's tp_doc is heap allocated, unlike the tp_doc slots |
| 2627 | * of most other objects. It's okay to cast it to char *. |
| 2628 | */ |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 2629 | PyObject_Free((char *)type->tp_doc); |
Georg Brandl | c255c7b | 2006-02-20 22:27:28 +0000 | [diff] [blame] | 2630 | Py_XDECREF(et->ht_name); |
| 2631 | Py_XDECREF(et->ht_slots); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2632 | Py_TYPE(type)->tp_free((PyObject *)type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2635 | static PyObject * |
| 2636 | type_subclasses(PyTypeObject *type, PyObject *args_ignored) |
| 2637 | { |
| 2638 | PyObject *list, *raw, *ref; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2639 | Py_ssize_t i, n; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2640 | |
| 2641 | list = PyList_New(0); |
| 2642 | if (list == NULL) |
| 2643 | return NULL; |
| 2644 | raw = type->tp_subclasses; |
| 2645 | if (raw == NULL) |
| 2646 | return list; |
| 2647 | assert(PyList_Check(raw)); |
| 2648 | n = PyList_GET_SIZE(raw); |
| 2649 | for (i = 0; i < n; i++) { |
| 2650 | ref = PyList_GET_ITEM(raw, i); |
Tim Peters | 4438338 | 2001-10-08 16:49:26 +0000 | [diff] [blame] | 2651 | assert(PyWeakref_CheckRef(ref)); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2652 | ref = PyWeakref_GET_OBJECT(ref); |
| 2653 | if (ref != Py_None) { |
| 2654 | if (PyList_Append(list, ref) < 0) { |
| 2655 | Py_DECREF(list); |
| 2656 | return NULL; |
| 2657 | } |
| 2658 | } |
| 2659 | } |
| 2660 | return list; |
| 2661 | } |
| 2662 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2663 | static PyMethodDef type_methods[] = { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2664 | {"mro", (PyCFunction)mro_external, METH_NOARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 2665 | PyDoc_STR("mro() -> list\nreturn a type's method resolution order")}, |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2666 | {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 2667 | PyDoc_STR("__subclasses__() -> list of immediate subclasses")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2668 | {0} |
| 2669 | }; |
| 2670 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2671 | PyDoc_STRVAR(type_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2672 | "type(object) -> the object's type\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2673 | "type(name, bases, dict) -> a new type"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2674 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2675 | static int |
| 2676 | type_traverse(PyTypeObject *type, visitproc visit, void *arg) |
| 2677 | { |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2678 | /* Because of type_is_gc(), the collector only calls this |
| 2679 | for heaptypes. */ |
| 2680 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2681 | |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 2682 | Py_VISIT(type->tp_dict); |
| 2683 | Py_VISIT(type->tp_cache); |
| 2684 | Py_VISIT(type->tp_mro); |
| 2685 | Py_VISIT(type->tp_bases); |
| 2686 | Py_VISIT(type->tp_base); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2687 | |
| 2688 | /* There's no need to visit type->tp_subclasses or |
Georg Brandl | c255c7b | 2006-02-20 22:27:28 +0000 | [diff] [blame] | 2689 | ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2690 | in cycles; tp_subclasses is a list of weak references, |
| 2691 | and slots is a tuple of strings. */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2692 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2693 | return 0; |
| 2694 | } |
| 2695 | |
| 2696 | static int |
| 2697 | type_clear(PyTypeObject *type) |
| 2698 | { |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2699 | /* Because of type_is_gc(), the collector only calls this |
| 2700 | for heaptypes. */ |
| 2701 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2702 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2703 | /* The only field we need to clear is tp_mro, which is part of a |
| 2704 | hard cycle (its first element is the class itself) that won't |
| 2705 | be broken otherwise (it's a tuple and tuples don't have a |
| 2706 | tp_clear handler). None of the other fields need to be |
| 2707 | cleared, and here's why: |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2708 | |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2709 | tp_dict: |
| 2710 | It is a dict, so the collector will call its tp_clear. |
| 2711 | |
| 2712 | tp_cache: |
| 2713 | Not used; if it were, it would be a dict. |
| 2714 | |
| 2715 | tp_bases, tp_base: |
| 2716 | If these are involved in a cycle, there must be at least |
| 2717 | one other, mutable object in the cycle, e.g. a base |
| 2718 | class's dict; the cycle will be broken that way. |
| 2719 | |
| 2720 | tp_subclasses: |
| 2721 | A list of weak references can't be part of a cycle; and |
| 2722 | lists have their own tp_clear. |
| 2723 | |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2724 | slots (in PyHeapTypeObject): |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2725 | A tuple of strings can't be part of a cycle. |
| 2726 | */ |
| 2727 | |
Thomas Wouters | edf17d8 | 2006-04-15 17:28:34 +0000 | [diff] [blame] | 2728 | Py_CLEAR(type->tp_mro); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2729 | |
| 2730 | return 0; |
| 2731 | } |
| 2732 | |
| 2733 | static int |
| 2734 | type_is_gc(PyTypeObject *type) |
| 2735 | { |
| 2736 | return type->tp_flags & Py_TPFLAGS_HEAPTYPE; |
| 2737 | } |
| 2738 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2739 | PyTypeObject PyType_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2740 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2741 | "type", /* tp_name */ |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 2742 | sizeof(PyHeapTypeObject), /* tp_basicsize */ |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 2743 | sizeof(PyMemberDef), /* tp_itemsize */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2744 | (destructor)type_dealloc, /* tp_dealloc */ |
| 2745 | 0, /* tp_print */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2746 | 0, /* tp_getattr */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2747 | 0, /* tp_setattr */ |
| 2748 | type_compare, /* tp_compare */ |
| 2749 | (reprfunc)type_repr, /* tp_repr */ |
| 2750 | 0, /* tp_as_number */ |
| 2751 | 0, /* tp_as_sequence */ |
| 2752 | 0, /* tp_as_mapping */ |
| 2753 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
| 2754 | (ternaryfunc)type_call, /* tp_call */ |
| 2755 | 0, /* tp_str */ |
| 2756 | (getattrofunc)type_getattro, /* tp_getattro */ |
| 2757 | (setattrofunc)type_setattro, /* tp_setattro */ |
| 2758 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2759 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 2760 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2761 | type_doc, /* tp_doc */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2762 | (traverseproc)type_traverse, /* tp_traverse */ |
| 2763 | (inquiry)type_clear, /* tp_clear */ |
Steven Bethard | ae42f33 | 2008-03-18 17:26:10 +0000 | [diff] [blame] | 2764 | type_richcompare, /* tp_richcompare */ |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2765 | offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2766 | 0, /* tp_iter */ |
| 2767 | 0, /* tp_iternext */ |
| 2768 | type_methods, /* tp_methods */ |
| 2769 | type_members, /* tp_members */ |
| 2770 | type_getsets, /* tp_getset */ |
| 2771 | 0, /* tp_base */ |
| 2772 | 0, /* tp_dict */ |
| 2773 | 0, /* tp_descr_get */ |
| 2774 | 0, /* tp_descr_set */ |
| 2775 | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ |
Guido van Rossum | f102e24 | 2007-03-23 18:53:03 +0000 | [diff] [blame] | 2776 | type_init, /* tp_init */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2777 | 0, /* tp_alloc */ |
| 2778 | type_new, /* tp_new */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 2779 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2780 | (inquiry)type_is_gc, /* tp_is_gc */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2781 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2782 | |
| 2783 | |
| 2784 | /* The base type of all types (eventually)... except itself. */ |
| 2785 | |
Guido van Rossum | 143b564 | 2007-03-23 04:58:42 +0000 | [diff] [blame] | 2786 | /* You may wonder why object.__new__() only complains about arguments |
| 2787 | when object.__init__() is not overridden, and vice versa. |
| 2788 | |
| 2789 | Consider the use cases: |
| 2790 | |
| 2791 | 1. When neither is overridden, we want to hear complaints about |
| 2792 | excess (i.e., any) arguments, since their presence could |
| 2793 | indicate there's a bug. |
| 2794 | |
| 2795 | 2. When defining an Immutable type, we are likely to override only |
| 2796 | __new__(), since __init__() is called too late to initialize an |
| 2797 | Immutable object. Since __new__() defines the signature for the |
| 2798 | type, it would be a pain to have to override __init__() just to |
| 2799 | stop it from complaining about excess arguments. |
| 2800 | |
| 2801 | 3. When defining a Mutable type, we are likely to override only |
| 2802 | __init__(). So here the converse reasoning applies: we don't |
| 2803 | want to have to override __new__() just to stop it from |
| 2804 | complaining. |
| 2805 | |
| 2806 | 4. When __init__() is overridden, and the subclass __init__() calls |
| 2807 | object.__init__(), the latter should complain about excess |
| 2808 | arguments; ditto for __new__(). |
| 2809 | |
| 2810 | Use cases 2 and 3 make it unattractive to unconditionally check for |
| 2811 | excess arguments. The best solution that addresses all four use |
| 2812 | cases is as follows: __init__() complains about excess arguments |
| 2813 | unless __new__() is overridden and __init__() is not overridden |
| 2814 | (IOW, if __init__() is overridden or __new__() is not overridden); |
| 2815 | symmetrically, __new__() complains about excess arguments unless |
| 2816 | __init__() is overridden and __new__() is not overridden |
| 2817 | (IOW, if __new__() is overridden or __init__() is not overridden). |
| 2818 | |
| 2819 | However, for backwards compatibility, this breaks too much code. |
| 2820 | Therefore, in 2.6, we'll *warn* about excess arguments when both |
| 2821 | methods are overridden; for all other cases we'll use the above |
| 2822 | rules. |
| 2823 | |
| 2824 | */ |
| 2825 | |
| 2826 | /* Forward */ |
| 2827 | static PyObject * |
| 2828 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 2829 | |
| 2830 | static int |
| 2831 | excess_args(PyObject *args, PyObject *kwds) |
| 2832 | { |
| 2833 | return PyTuple_GET_SIZE(args) || |
| 2834 | (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)); |
| 2835 | } |
| 2836 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2837 | static int |
| 2838 | object_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2839 | { |
Guido van Rossum | 143b564 | 2007-03-23 04:58:42 +0000 | [diff] [blame] | 2840 | int err = 0; |
| 2841 | if (excess_args(args, kwds)) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2842 | PyTypeObject *type = Py_TYPE(self); |
Guido van Rossum | 143b564 | 2007-03-23 04:58:42 +0000 | [diff] [blame] | 2843 | if (type->tp_init != object_init && |
| 2844 | type->tp_new != object_new) |
| 2845 | { |
| 2846 | err = PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2847 | "object.__init__() takes no parameters", |
| 2848 | 1); |
| 2849 | } |
| 2850 | else if (type->tp_init != object_init || |
| 2851 | type->tp_new == object_new) |
| 2852 | { |
| 2853 | PyErr_SetString(PyExc_TypeError, |
| 2854 | "object.__init__() takes no parameters"); |
| 2855 | err = -1; |
| 2856 | } |
| 2857 | } |
| 2858 | return err; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2861 | static PyObject * |
| 2862 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2863 | { |
Guido van Rossum | 143b564 | 2007-03-23 04:58:42 +0000 | [diff] [blame] | 2864 | int err = 0; |
| 2865 | if (excess_args(args, kwds)) { |
| 2866 | if (type->tp_new != object_new && |
| 2867 | type->tp_init != object_init) |
| 2868 | { |
| 2869 | err = PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2870 | "object.__new__() takes no parameters", |
| 2871 | 1); |
| 2872 | } |
| 2873 | else if (type->tp_new != object_new || |
| 2874 | type->tp_init == object_init) |
| 2875 | { |
| 2876 | PyErr_SetString(PyExc_TypeError, |
| 2877 | "object.__new__() takes no parameters"); |
| 2878 | err = -1; |
| 2879 | } |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2880 | } |
Guido van Rossum | 143b564 | 2007-03-23 04:58:42 +0000 | [diff] [blame] | 2881 | if (err < 0) |
| 2882 | return NULL; |
Jeffrey Yasskin | 960b9b7 | 2008-02-28 04:45:36 +0000 | [diff] [blame] | 2883 | |
| 2884 | if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) { |
| 2885 | static PyObject *comma = NULL; |
| 2886 | PyObject *abstract_methods = NULL; |
| 2887 | PyObject *builtins; |
| 2888 | PyObject *sorted; |
| 2889 | PyObject *sorted_methods = NULL; |
| 2890 | PyObject *joined = NULL; |
| 2891 | const char *joined_str; |
| 2892 | |
| 2893 | /* Compute ", ".join(sorted(type.__abstractmethods__)) |
| 2894 | into joined. */ |
| 2895 | abstract_methods = type_abstractmethods(type, NULL); |
| 2896 | if (abstract_methods == NULL) |
| 2897 | goto error; |
| 2898 | builtins = PyEval_GetBuiltins(); |
| 2899 | if (builtins == NULL) |
| 2900 | goto error; |
| 2901 | sorted = PyDict_GetItemString(builtins, "sorted"); |
| 2902 | if (sorted == NULL) |
| 2903 | goto error; |
| 2904 | sorted_methods = PyObject_CallFunctionObjArgs(sorted, |
| 2905 | abstract_methods, |
| 2906 | NULL); |
| 2907 | if (sorted_methods == NULL) |
| 2908 | goto error; |
| 2909 | if (comma == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2910 | comma = PyString_InternFromString(", "); |
Jeffrey Yasskin | 960b9b7 | 2008-02-28 04:45:36 +0000 | [diff] [blame] | 2911 | if (comma == NULL) |
| 2912 | goto error; |
| 2913 | } |
| 2914 | joined = PyObject_CallMethod(comma, "join", |
| 2915 | "O", sorted_methods); |
| 2916 | if (joined == NULL) |
| 2917 | goto error; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2918 | joined_str = PyString_AsString(joined); |
Jeffrey Yasskin | 960b9b7 | 2008-02-28 04:45:36 +0000 | [diff] [blame] | 2919 | if (joined_str == NULL) |
| 2920 | goto error; |
| 2921 | |
| 2922 | PyErr_Format(PyExc_TypeError, |
| 2923 | "Can't instantiate abstract class %s " |
| 2924 | "with abstract methods %s", |
| 2925 | type->tp_name, |
| 2926 | joined_str); |
| 2927 | error: |
| 2928 | Py_XDECREF(joined); |
| 2929 | Py_XDECREF(sorted_methods); |
| 2930 | Py_XDECREF(abstract_methods); |
| 2931 | return NULL; |
| 2932 | } |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2933 | return type->tp_alloc(type, 0); |
| 2934 | } |
| 2935 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2936 | static void |
| 2937 | object_dealloc(PyObject *self) |
| 2938 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2939 | Py_TYPE(self)->tp_free(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2942 | static PyObject * |
| 2943 | object_repr(PyObject *self) |
| 2944 | { |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2945 | PyTypeObject *type; |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2946 | PyObject *mod, *name, *rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2947 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2948 | type = Py_TYPE(self); |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2949 | mod = type_module(type, NULL); |
| 2950 | if (mod == NULL) |
| 2951 | PyErr_Clear(); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2952 | else if (!PyString_Check(mod)) { |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2953 | Py_DECREF(mod); |
| 2954 | mod = NULL; |
| 2955 | } |
| 2956 | name = type_name(type, NULL); |
| 2957 | if (name == NULL) |
| 2958 | return NULL; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2959 | if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) |
| 2960 | rtn = PyString_FromFormat("<%s.%s object at %p>", |
| 2961 | PyString_AS_STRING(mod), |
| 2962 | PyString_AS_STRING(name), |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2963 | self); |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2964 | else |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2965 | rtn = PyString_FromFormat("<%s object at %p>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2966 | type->tp_name, self); |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 2967 | Py_XDECREF(mod); |
| 2968 | Py_DECREF(name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2969 | return rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2972 | static PyObject * |
| 2973 | object_str(PyObject *self) |
| 2974 | { |
| 2975 | unaryfunc f; |
| 2976 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2977 | f = Py_TYPE(self)->tp_repr; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2978 | if (f == NULL) |
| 2979 | f = object_repr; |
| 2980 | return f(self); |
| 2981 | } |
| 2982 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2983 | static PyObject * |
| 2984 | object_get_class(PyObject *self, void *closure) |
| 2985 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2986 | Py_INCREF(Py_TYPE(self)); |
| 2987 | return (PyObject *)(Py_TYPE(self)); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2988 | } |
| 2989 | |
| 2990 | static int |
| 2991 | equiv_structs(PyTypeObject *a, PyTypeObject *b) |
| 2992 | { |
| 2993 | return a == b || |
| 2994 | (a != NULL && |
| 2995 | b != NULL && |
| 2996 | a->tp_basicsize == b->tp_basicsize && |
| 2997 | a->tp_itemsize == b->tp_itemsize && |
| 2998 | a->tp_dictoffset == b->tp_dictoffset && |
| 2999 | a->tp_weaklistoffset == b->tp_weaklistoffset && |
| 3000 | ((a->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 3001 | (b->tp_flags & Py_TPFLAGS_HAVE_GC))); |
| 3002 | } |
| 3003 | |
| 3004 | static int |
| 3005 | same_slots_added(PyTypeObject *a, PyTypeObject *b) |
| 3006 | { |
| 3007 | PyTypeObject *base = a->tp_base; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 3008 | Py_ssize_t size; |
Žiga Seilnacht | 6f2d09c | 2007-03-16 11:59:38 +0000 | [diff] [blame] | 3009 | PyObject *slots_a, *slots_b; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3010 | |
| 3011 | if (base != b->tp_base) |
| 3012 | return 0; |
| 3013 | if (equiv_structs(a, base) && equiv_structs(b, base)) |
| 3014 | return 1; |
| 3015 | size = base->tp_basicsize; |
| 3016 | if (a->tp_dictoffset == size && b->tp_dictoffset == size) |
| 3017 | size += sizeof(PyObject *); |
| 3018 | if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size) |
| 3019 | size += sizeof(PyObject *); |
Žiga Seilnacht | 6f2d09c | 2007-03-16 11:59:38 +0000 | [diff] [blame] | 3020 | |
| 3021 | /* Check slots compliance */ |
| 3022 | slots_a = ((PyHeapTypeObject *)a)->ht_slots; |
| 3023 | slots_b = ((PyHeapTypeObject *)b)->ht_slots; |
| 3024 | if (slots_a && slots_b) { |
| 3025 | if (PyObject_Compare(slots_a, slots_b) != 0) |
| 3026 | return 0; |
| 3027 | size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a); |
| 3028 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3029 | return size == a->tp_basicsize && size == b->tp_basicsize; |
| 3030 | } |
| 3031 | |
| 3032 | static int |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3033 | compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3034 | { |
| 3035 | PyTypeObject *newbase, *oldbase; |
| 3036 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3037 | if (newto->tp_dealloc != oldto->tp_dealloc || |
| 3038 | newto->tp_free != oldto->tp_free) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3039 | { |
| 3040 | PyErr_Format(PyExc_TypeError, |
| 3041 | "%s assignment: " |
| 3042 | "'%s' deallocator differs from '%s'", |
| 3043 | attr, |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3044 | newto->tp_name, |
| 3045 | oldto->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3046 | return 0; |
| 3047 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3048 | newbase = newto; |
| 3049 | oldbase = oldto; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3050 | while (equiv_structs(newbase, newbase->tp_base)) |
| 3051 | newbase = newbase->tp_base; |
| 3052 | while (equiv_structs(oldbase, oldbase->tp_base)) |
| 3053 | oldbase = oldbase->tp_base; |
| 3054 | if (newbase != oldbase && |
| 3055 | (newbase->tp_base != oldbase->tp_base || |
| 3056 | !same_slots_added(newbase, oldbase))) { |
| 3057 | PyErr_Format(PyExc_TypeError, |
| 3058 | "%s assignment: " |
| 3059 | "'%s' object layout differs from '%s'", |
| 3060 | attr, |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3061 | newto->tp_name, |
| 3062 | oldto->tp_name); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3063 | return 0; |
| 3064 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 3065 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3066 | return 1; |
| 3067 | } |
| 3068 | |
| 3069 | static int |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3070 | object_set_class(PyObject *self, PyObject *value, void *closure) |
| 3071 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 3072 | PyTypeObject *oldto = Py_TYPE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3073 | PyTypeObject *newto; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3074 | |
Guido van Rossum | b6b8942 | 2002-04-15 01:03:30 +0000 | [diff] [blame] | 3075 | if (value == NULL) { |
| 3076 | PyErr_SetString(PyExc_TypeError, |
| 3077 | "can't delete __class__ attribute"); |
| 3078 | return -1; |
| 3079 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3080 | if (!PyType_Check(value)) { |
| 3081 | PyErr_Format(PyExc_TypeError, |
| 3082 | "__class__ must be set to new-style class, not '%s' object", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 3083 | Py_TYPE(value)->tp_name); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3084 | return -1; |
| 3085 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3086 | newto = (PyTypeObject *)value; |
| 3087 | if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) || |
| 3088 | !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
Guido van Rossum | 40af889 | 2002-08-10 05:42:07 +0000 | [diff] [blame] | 3089 | { |
| 3090 | PyErr_Format(PyExc_TypeError, |
| 3091 | "__class__ assignment: only for heap types"); |
| 3092 | return -1; |
| 3093 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3094 | if (compatible_for_assignment(newto, oldto, "__class__")) { |
| 3095 | Py_INCREF(newto); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 3096 | Py_TYPE(self) = newto; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3097 | Py_DECREF(oldto); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3098 | return 0; |
| 3099 | } |
| 3100 | else { |
Guido van Rossum | 9ee4b94 | 2002-05-24 18:47:47 +0000 | [diff] [blame] | 3101 | return -1; |
| 3102 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3103 | } |
| 3104 | |
| 3105 | static PyGetSetDef object_getsets[] = { |
| 3106 | {"__class__", object_get_class, object_set_class, |
Neal Norwitz | 858e34f | 2002-08-13 17:18:45 +0000 | [diff] [blame] | 3107 | PyDoc_STR("the object's class")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3108 | {0} |
| 3109 | }; |
| 3110 | |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 3111 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3112 | /* Stuff to implement __reduce_ex__ for pickle protocols >= 2. |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 3113 | We fall back to helpers in copy_reg for: |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3114 | - pickle protocols < 2 |
| 3115 | - calculating the list of slot names (done only once per class) |
| 3116 | - the __newobj__ function (which is used as a token but never called) |
| 3117 | */ |
| 3118 | |
| 3119 | static PyObject * |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3120 | import_copyreg(void) |
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 | static PyObject *copyreg_str; |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3123 | |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3124 | if (!copyreg_str) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3125 | copyreg_str = PyString_InternFromString("copy_reg"); |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3126 | if (copyreg_str == NULL) |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3127 | return NULL; |
| 3128 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3129 | |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3130 | return PyImport_Import(copyreg_str); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
| 3133 | static PyObject * |
| 3134 | slotnames(PyObject *cls) |
| 3135 | { |
| 3136 | PyObject *clsdict; |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3137 | PyObject *copyreg; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3138 | PyObject *slotnames; |
| 3139 | |
| 3140 | if (!PyType_Check(cls)) { |
| 3141 | Py_INCREF(Py_None); |
| 3142 | return Py_None; |
| 3143 | } |
| 3144 | |
| 3145 | clsdict = ((PyTypeObject *)cls)->tp_dict; |
| 3146 | slotnames = PyDict_GetItemString(clsdict, "__slotnames__"); |
Armin Rigo | ec862b9 | 2005-09-24 22:58:41 +0000 | [diff] [blame] | 3147 | if (slotnames != NULL && PyList_Check(slotnames)) { |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3148 | Py_INCREF(slotnames); |
| 3149 | return slotnames; |
| 3150 | } |
| 3151 | |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3152 | copyreg = import_copyreg(); |
| 3153 | if (copyreg == NULL) |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3154 | return NULL; |
| 3155 | |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3156 | slotnames = PyObject_CallMethod(copyreg, "_slotnames", "O", cls); |
| 3157 | Py_DECREF(copyreg); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3158 | if (slotnames != NULL && |
| 3159 | slotnames != Py_None && |
| 3160 | !PyList_Check(slotnames)) |
| 3161 | { |
| 3162 | PyErr_SetString(PyExc_TypeError, |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 3163 | "copy_reg._slotnames didn't return a list or None"); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3164 | Py_DECREF(slotnames); |
| 3165 | slotnames = NULL; |
| 3166 | } |
| 3167 | |
| 3168 | return slotnames; |
| 3169 | } |
| 3170 | |
| 3171 | static PyObject * |
| 3172 | reduce_2(PyObject *obj) |
| 3173 | { |
| 3174 | PyObject *cls, *getnewargs; |
| 3175 | PyObject *args = NULL, *args2 = NULL; |
| 3176 | PyObject *getstate = NULL, *state = NULL, *names = NULL; |
| 3177 | PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL; |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3178 | PyObject *copyreg = NULL, *newobj = NULL, *res = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3179 | Py_ssize_t i, n; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3180 | |
| 3181 | cls = PyObject_GetAttrString(obj, "__class__"); |
| 3182 | if (cls == NULL) |
| 3183 | return NULL; |
| 3184 | |
| 3185 | getnewargs = PyObject_GetAttrString(obj, "__getnewargs__"); |
| 3186 | if (getnewargs != NULL) { |
| 3187 | args = PyObject_CallObject(getnewargs, NULL); |
| 3188 | Py_DECREF(getnewargs); |
| 3189 | if (args != NULL && !PyTuple_Check(args)) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 3190 | PyErr_Format(PyExc_TypeError, |
| 3191 | "__getnewargs__ should return a tuple, " |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 3192 | "not '%.200s'", Py_TYPE(args)->tp_name); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3193 | goto end; |
| 3194 | } |
| 3195 | } |
| 3196 | else { |
| 3197 | PyErr_Clear(); |
| 3198 | args = PyTuple_New(0); |
| 3199 | } |
| 3200 | if (args == NULL) |
| 3201 | goto end; |
| 3202 | |
| 3203 | getstate = PyObject_GetAttrString(obj, "__getstate__"); |
| 3204 | if (getstate != NULL) { |
| 3205 | state = PyObject_CallObject(getstate, NULL); |
| 3206 | Py_DECREF(getstate); |
Neal Norwitz | e2fdc61 | 2003-06-08 13:19:58 +0000 | [diff] [blame] | 3207 | if (state == NULL) |
| 3208 | goto end; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3209 | } |
| 3210 | else { |
Jim Fulton | 8a1a594 | 2004-02-08 04:21:26 +0000 | [diff] [blame] | 3211 | PyErr_Clear(); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3212 | state = PyObject_GetAttrString(obj, "__dict__"); |
| 3213 | if (state == NULL) { |
| 3214 | PyErr_Clear(); |
| 3215 | state = Py_None; |
| 3216 | Py_INCREF(state); |
| 3217 | } |
| 3218 | names = slotnames(cls); |
| 3219 | if (names == NULL) |
| 3220 | goto end; |
| 3221 | if (names != Py_None) { |
| 3222 | assert(PyList_Check(names)); |
| 3223 | slots = PyDict_New(); |
| 3224 | if (slots == NULL) |
| 3225 | goto end; |
| 3226 | n = 0; |
| 3227 | /* Can't pre-compute the list size; the list |
| 3228 | is stored on the class so accessible to other |
| 3229 | threads, which may be run by DECREF */ |
| 3230 | for (i = 0; i < PyList_GET_SIZE(names); i++) { |
| 3231 | PyObject *name, *value; |
| 3232 | name = PyList_GET_ITEM(names, i); |
| 3233 | value = PyObject_GetAttr(obj, name); |
| 3234 | if (value == NULL) |
| 3235 | PyErr_Clear(); |
| 3236 | else { |
| 3237 | int err = PyDict_SetItem(slots, name, |
| 3238 | value); |
| 3239 | Py_DECREF(value); |
| 3240 | if (err) |
| 3241 | goto end; |
| 3242 | n++; |
| 3243 | } |
| 3244 | } |
| 3245 | if (n) { |
| 3246 | state = Py_BuildValue("(NO)", state, slots); |
| 3247 | if (state == NULL) |
| 3248 | goto end; |
| 3249 | } |
| 3250 | } |
| 3251 | } |
| 3252 | |
| 3253 | if (!PyList_Check(obj)) { |
| 3254 | listitems = Py_None; |
| 3255 | Py_INCREF(listitems); |
| 3256 | } |
| 3257 | else { |
| 3258 | listitems = PyObject_GetIter(obj); |
| 3259 | if (listitems == NULL) |
| 3260 | goto end; |
| 3261 | } |
| 3262 | |
| 3263 | if (!PyDict_Check(obj)) { |
| 3264 | dictitems = Py_None; |
| 3265 | Py_INCREF(dictitems); |
| 3266 | } |
| 3267 | else { |
| 3268 | dictitems = PyObject_CallMethod(obj, "iteritems", ""); |
| 3269 | if (dictitems == NULL) |
| 3270 | goto end; |
| 3271 | } |
| 3272 | |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3273 | copyreg = import_copyreg(); |
| 3274 | if (copyreg == NULL) |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3275 | goto end; |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3276 | newobj = PyObject_GetAttrString(copyreg, "__newobj__"); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3277 | if (newobj == NULL) |
| 3278 | goto end; |
| 3279 | |
| 3280 | n = PyTuple_GET_SIZE(args); |
| 3281 | args2 = PyTuple_New(n+1); |
| 3282 | if (args2 == NULL) |
| 3283 | goto end; |
| 3284 | PyTuple_SET_ITEM(args2, 0, cls); |
| 3285 | cls = NULL; |
| 3286 | for (i = 0; i < n; i++) { |
| 3287 | PyObject *v = PyTuple_GET_ITEM(args, i); |
| 3288 | Py_INCREF(v); |
| 3289 | PyTuple_SET_ITEM(args2, i+1, v); |
| 3290 | } |
| 3291 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3292 | res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3293 | |
| 3294 | end: |
| 3295 | Py_XDECREF(cls); |
| 3296 | Py_XDECREF(args); |
| 3297 | Py_XDECREF(args2); |
Jeremy Hylton | d06483c | 2003-04-09 21:01:42 +0000 | [diff] [blame] | 3298 | Py_XDECREF(slots); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3299 | Py_XDECREF(state); |
| 3300 | Py_XDECREF(names); |
| 3301 | Py_XDECREF(listitems); |
| 3302 | Py_XDECREF(dictitems); |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3303 | Py_XDECREF(copyreg); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3304 | Py_XDECREF(newobj); |
| 3305 | return res; |
| 3306 | } |
| 3307 | |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 3308 | /* |
| 3309 | * There were two problems when object.__reduce__ and object.__reduce_ex__ |
| 3310 | * were implemented in the same function: |
| 3311 | * - trying to pickle an object with a custom __reduce__ method that |
| 3312 | * fell back to object.__reduce__ in certain circumstances led to |
| 3313 | * infinite recursion at Python level and eventual RuntimeError. |
| 3314 | * - Pickling objects that lied about their type by overwriting the |
| 3315 | * __class__ descriptor could lead to infinite recursion at C level |
| 3316 | * and eventual segfault. |
| 3317 | * |
| 3318 | * Because of backwards compatibility, the two methods still have to |
| 3319 | * behave in the same way, even if this is not required by the pickle |
| 3320 | * protocol. This common functionality was moved to the _common_reduce |
| 3321 | * function. |
| 3322 | */ |
| 3323 | static PyObject * |
| 3324 | _common_reduce(PyObject *self, int proto) |
| 3325 | { |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3326 | PyObject *copyreg, *res; |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 3327 | |
| 3328 | if (proto >= 2) |
| 3329 | return reduce_2(self); |
| 3330 | |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3331 | copyreg = import_copyreg(); |
| 3332 | if (!copyreg) |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 3333 | return NULL; |
| 3334 | |
Alexandre Vassalotti | 9510e4a | 2008-05-11 08:25:28 +0000 | [diff] [blame] | 3335 | res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto); |
| 3336 | Py_DECREF(copyreg); |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 3337 | |
| 3338 | return res; |
| 3339 | } |
| 3340 | |
| 3341 | static PyObject * |
| 3342 | object_reduce(PyObject *self, PyObject *args) |
| 3343 | { |
| 3344 | int proto = 0; |
| 3345 | |
| 3346 | if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto)) |
| 3347 | return NULL; |
| 3348 | |
| 3349 | return _common_reduce(self, proto); |
| 3350 | } |
| 3351 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3352 | static PyObject * |
| 3353 | object_reduce_ex(PyObject *self, PyObject *args) |
| 3354 | { |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 3355 | PyObject *reduce, *res; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3356 | int proto = 0; |
| 3357 | |
| 3358 | if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto)) |
| 3359 | return NULL; |
| 3360 | |
| 3361 | reduce = PyObject_GetAttrString(self, "__reduce__"); |
| 3362 | if (reduce == NULL) |
| 3363 | PyErr_Clear(); |
| 3364 | else { |
| 3365 | PyObject *cls, *clsreduce, *objreduce; |
| 3366 | int override; |
| 3367 | cls = PyObject_GetAttrString(self, "__class__"); |
| 3368 | if (cls == NULL) { |
| 3369 | Py_DECREF(reduce); |
| 3370 | return NULL; |
| 3371 | } |
| 3372 | clsreduce = PyObject_GetAttrString(cls, "__reduce__"); |
| 3373 | Py_DECREF(cls); |
| 3374 | if (clsreduce == NULL) { |
| 3375 | Py_DECREF(reduce); |
| 3376 | return NULL; |
| 3377 | } |
| 3378 | objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict, |
| 3379 | "__reduce__"); |
| 3380 | override = (clsreduce != objreduce); |
| 3381 | Py_DECREF(clsreduce); |
| 3382 | if (override) { |
| 3383 | res = PyObject_CallObject(reduce, NULL); |
| 3384 | Py_DECREF(reduce); |
| 3385 | return res; |
| 3386 | } |
| 3387 | else |
| 3388 | Py_DECREF(reduce); |
| 3389 | } |
| 3390 | |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 3391 | return _common_reduce(self, proto); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3392 | } |
| 3393 | |
Jeffrey Yasskin | 960b9b7 | 2008-02-28 04:45:36 +0000 | [diff] [blame] | 3394 | static PyObject * |
| 3395 | object_subclasshook(PyObject *cls, PyObject *args) |
| 3396 | { |
| 3397 | Py_INCREF(Py_NotImplemented); |
| 3398 | return Py_NotImplemented; |
| 3399 | } |
| 3400 | |
| 3401 | PyDoc_STRVAR(object_subclasshook_doc, |
| 3402 | "Abstract classes can override this to customize issubclass().\n" |
| 3403 | "\n" |
| 3404 | "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n" |
| 3405 | "It should return True, False or NotImplemented. If it returns\n" |
| 3406 | "NotImplemented, the normal algorithm is used. Otherwise, it\n" |
| 3407 | "overrides the normal algorithm (and the outcome is cached).\n"); |
| 3408 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 3409 | /* |
| 3410 | from PEP 3101, this code implements: |
| 3411 | |
| 3412 | class object: |
| 3413 | def __format__(self, format_spec): |
| 3414 | if isinstance(format_spec, str): |
| 3415 | return format(str(self), format_spec) |
| 3416 | elif isinstance(format_spec, unicode): |
| 3417 | return format(unicode(self), format_spec) |
| 3418 | */ |
| 3419 | static PyObject * |
| 3420 | object_format(PyObject *self, PyObject *args) |
| 3421 | { |
| 3422 | PyObject *format_spec; |
| 3423 | PyObject *self_as_str = NULL; |
| 3424 | PyObject *result = NULL; |
| 3425 | PyObject *format_meth = NULL; |
| 3426 | |
| 3427 | if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) |
| 3428 | return NULL; |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3429 | #ifdef Py_USING_UNICODE |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 3430 | if (PyUnicode_Check(format_spec)) { |
| 3431 | self_as_str = PyObject_Unicode(self); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3432 | } else if (PyString_Check(format_spec)) { |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3433 | #else |
| 3434 | if (PyString_Check(format_spec)) { |
| 3435 | #endif |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 3436 | self_as_str = PyObject_Str(self); |
| 3437 | } else { |
| 3438 | PyErr_SetString(PyExc_TypeError, "argument to __format__ must be unicode or str"); |
| 3439 | return NULL; |
| 3440 | } |
| 3441 | |
| 3442 | if (self_as_str != NULL) { |
| 3443 | /* find the format function */ |
| 3444 | format_meth = PyObject_GetAttrString(self_as_str, "__format__"); |
| 3445 | if (format_meth != NULL) { |
| 3446 | /* and call it */ |
| 3447 | result = PyObject_CallFunctionObjArgs(format_meth, format_spec, NULL); |
| 3448 | } |
| 3449 | } |
| 3450 | |
| 3451 | Py_XDECREF(self_as_str); |
| 3452 | Py_XDECREF(format_meth); |
| 3453 | |
| 3454 | return result; |
| 3455 | } |
| 3456 | |
Robert Schuppenies | 51df064 | 2008-06-01 16:16:17 +0000 | [diff] [blame] | 3457 | static PyObject * |
| 3458 | object_sizeof(PyObject *self, PyObject *args) |
| 3459 | { |
| 3460 | Py_ssize_t res, isize; |
| 3461 | |
| 3462 | res = 0; |
| 3463 | isize = self->ob_type->tp_itemsize; |
| 3464 | if (isize > 0) |
| 3465 | res = self->ob_type->ob_size * isize; |
| 3466 | res += self->ob_type->tp_basicsize; |
| 3467 | |
| 3468 | return PyInt_FromSsize_t(res); |
| 3469 | } |
| 3470 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3471 | static PyMethodDef object_methods[] = { |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 3472 | {"__reduce_ex__", object_reduce_ex, METH_VARARGS, |
| 3473 | PyDoc_STR("helper for pickle")}, |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 3474 | {"__reduce__", object_reduce, METH_VARARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 3475 | PyDoc_STR("helper for pickle")}, |
Jeffrey Yasskin | 960b9b7 | 2008-02-28 04:45:36 +0000 | [diff] [blame] | 3476 | {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS, |
| 3477 | object_subclasshook_doc}, |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 3478 | {"__format__", object_format, METH_VARARGS, |
| 3479 | PyDoc_STR("default object formatter")}, |
Robert Schuppenies | 51df064 | 2008-06-01 16:16:17 +0000 | [diff] [blame] | 3480 | {"__sizeof__", object_sizeof, METH_NOARGS, |
Georg Brandl | 7a6de8b | 2008-06-01 16:42:16 +0000 | [diff] [blame] | 3481 | PyDoc_STR("__sizeof__() -> size of object in memory, in bytes")}, |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3482 | {0} |
| 3483 | }; |
| 3484 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3485 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3486 | PyTypeObject PyBaseObject_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 3487 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3488 | "object", /* tp_name */ |
| 3489 | sizeof(PyObject), /* tp_basicsize */ |
| 3490 | 0, /* tp_itemsize */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 3491 | object_dealloc, /* tp_dealloc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3492 | 0, /* tp_print */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3493 | 0, /* tp_getattr */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3494 | 0, /* tp_setattr */ |
| 3495 | 0, /* tp_compare */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3496 | object_repr, /* tp_repr */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3497 | 0, /* tp_as_number */ |
| 3498 | 0, /* tp_as_sequence */ |
| 3499 | 0, /* tp_as_mapping */ |
Guido van Rossum | 64c06e3 | 2007-11-22 00:55:51 +0000 | [diff] [blame] | 3500 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3501 | 0, /* tp_call */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3502 | object_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3503 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3504 | PyObject_GenericSetAttr, /* tp_setattro */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3505 | 0, /* tp_as_buffer */ |
| 3506 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 3507 | PyDoc_STR("The most base type"), /* tp_doc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3508 | 0, /* tp_traverse */ |
| 3509 | 0, /* tp_clear */ |
| 3510 | 0, /* tp_richcompare */ |
| 3511 | 0, /* tp_weaklistoffset */ |
| 3512 | 0, /* tp_iter */ |
| 3513 | 0, /* tp_iternext */ |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3514 | object_methods, /* tp_methods */ |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3515 | 0, /* tp_members */ |
| 3516 | object_getsets, /* tp_getset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3517 | 0, /* tp_base */ |
| 3518 | 0, /* tp_dict */ |
| 3519 | 0, /* tp_descr_get */ |
| 3520 | 0, /* tp_descr_set */ |
| 3521 | 0, /* tp_dictoffset */ |
| 3522 | object_init, /* tp_init */ |
| 3523 | PyType_GenericAlloc, /* tp_alloc */ |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 3524 | object_new, /* tp_new */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3525 | PyObject_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3526 | }; |
| 3527 | |
| 3528 | |
| 3529 | /* Initialize the __dict__ in a type object */ |
| 3530 | |
| 3531 | static int |
| 3532 | add_methods(PyTypeObject *type, PyMethodDef *meth) |
| 3533 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3534 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3535 | |
| 3536 | for (; meth->ml_name != NULL; meth++) { |
| 3537 | PyObject *descr; |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 3538 | if (PyDict_GetItemString(dict, meth->ml_name) && |
| 3539 | !(meth->ml_flags & METH_COEXIST)) |
| 3540 | continue; |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 3541 | if (meth->ml_flags & METH_CLASS) { |
| 3542 | if (meth->ml_flags & METH_STATIC) { |
| 3543 | PyErr_SetString(PyExc_ValueError, |
| 3544 | "method cannot be both class and static"); |
| 3545 | return -1; |
| 3546 | } |
Tim Peters | bca1cbc | 2002-12-09 22:56:13 +0000 | [diff] [blame] | 3547 | descr = PyDescr_NewClassMethod(type, meth); |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 3548 | } |
| 3549 | else if (meth->ml_flags & METH_STATIC) { |
Guido van Rossum | 9af48ff | 2003-02-11 17:12:46 +0000 | [diff] [blame] | 3550 | PyObject *cfunc = PyCFunction_New(meth, NULL); |
| 3551 | if (cfunc == NULL) |
| 3552 | return -1; |
| 3553 | descr = PyStaticMethod_New(cfunc); |
| 3554 | Py_DECREF(cfunc); |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 3555 | } |
| 3556 | else { |
| 3557 | descr = PyDescr_NewMethod(type, meth); |
| 3558 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3559 | if (descr == NULL) |
| 3560 | return -1; |
Fred Drake | 7bf9715 | 2002-03-28 05:33:33 +0000 | [diff] [blame] | 3561 | if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3562 | return -1; |
| 3563 | Py_DECREF(descr); |
| 3564 | } |
| 3565 | return 0; |
| 3566 | } |
| 3567 | |
| 3568 | static int |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 3569 | add_members(PyTypeObject *type, PyMemberDef *memb) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3570 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3571 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3572 | |
| 3573 | for (; memb->name != NULL; memb++) { |
| 3574 | PyObject *descr; |
| 3575 | if (PyDict_GetItemString(dict, memb->name)) |
| 3576 | continue; |
| 3577 | descr = PyDescr_NewMember(type, memb); |
| 3578 | if (descr == NULL) |
| 3579 | return -1; |
| 3580 | if (PyDict_SetItemString(dict, memb->name, descr) < 0) |
| 3581 | return -1; |
| 3582 | Py_DECREF(descr); |
| 3583 | } |
| 3584 | return 0; |
| 3585 | } |
| 3586 | |
| 3587 | static int |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 3588 | add_getset(PyTypeObject *type, PyGetSetDef *gsp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3589 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3590 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3591 | |
| 3592 | for (; gsp->name != NULL; gsp++) { |
| 3593 | PyObject *descr; |
| 3594 | if (PyDict_GetItemString(dict, gsp->name)) |
| 3595 | continue; |
| 3596 | descr = PyDescr_NewGetSet(type, gsp); |
| 3597 | |
| 3598 | if (descr == NULL) |
| 3599 | return -1; |
| 3600 | if (PyDict_SetItemString(dict, gsp->name, descr) < 0) |
| 3601 | return -1; |
| 3602 | Py_DECREF(descr); |
| 3603 | } |
| 3604 | return 0; |
| 3605 | } |
| 3606 | |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 3607 | #define BUFFER_FLAGS (Py_TPFLAGS_HAVE_GETCHARBUFFER | Py_TPFLAGS_HAVE_NEWBUFFER) |
| 3608 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3609 | static void |
| 3610 | inherit_special(PyTypeObject *type, PyTypeObject *base) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3611 | { |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 3612 | Py_ssize_t oldsize, newsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3613 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3614 | /* Special flag magic */ |
| 3615 | if (!type->tp_as_buffer && base->tp_as_buffer) { |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 3616 | type->tp_flags &= ~BUFFER_FLAGS; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3617 | type->tp_flags |= |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 3618 | base->tp_flags & BUFFER_FLAGS; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3619 | } |
| 3620 | if (!type->tp_as_sequence && base->tp_as_sequence) { |
| 3621 | type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN; |
| 3622 | type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN; |
| 3623 | } |
| 3624 | if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) != |
| 3625 | (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) { |
| 3626 | if ((!type->tp_as_number && base->tp_as_number) || |
| 3627 | (!type->tp_as_sequence && base->tp_as_sequence)) { |
| 3628 | type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS; |
| 3629 | if (!type->tp_as_number && !type->tp_as_sequence) { |
| 3630 | type->tp_flags |= base->tp_flags & |
| 3631 | Py_TPFLAGS_HAVE_INPLACEOPS; |
| 3632 | } |
| 3633 | } |
| 3634 | /* Wow */ |
| 3635 | } |
| 3636 | if (!type->tp_as_number && base->tp_as_number) { |
| 3637 | type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES; |
| 3638 | type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES; |
| 3639 | } |
| 3640 | |
| 3641 | /* Copying basicsize is connected to the GC flags */ |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 3642 | oldsize = base->tp_basicsize; |
| 3643 | newsize = type->tp_basicsize ? type->tp_basicsize : oldsize; |
| 3644 | if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3645 | (base->tp_flags & Py_TPFLAGS_HAVE_GC) && |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3646 | (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) && |
| 3647 | (!type->tp_traverse && !type->tp_clear)) { |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 3648 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3649 | if (type->tp_traverse == NULL) |
| 3650 | type->tp_traverse = base->tp_traverse; |
| 3651 | if (type->tp_clear == NULL) |
| 3652 | type->tp_clear = base->tp_clear; |
| 3653 | } |
| 3654 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
Guido van Rossum | f884b74 | 2001-12-17 17:14:22 +0000 | [diff] [blame] | 3655 | /* The condition below could use some explanation. |
| 3656 | It appears that tp_new is not inherited for static types |
| 3657 | whose base class is 'object'; this seems to be a precaution |
| 3658 | so that old extension types don't suddenly become |
| 3659 | callable (object.__new__ wouldn't insure the invariants |
| 3660 | that the extension type's own factory function ensures). |
| 3661 | Heap types, of course, are under our control, so they do |
| 3662 | inherit tp_new; static extension types that specify some |
| 3663 | other built-in type as the default are considered |
| 3664 | new-style-aware so they also inherit object.__new__. */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3665 | if (base != &PyBaseObject_Type || |
| 3666 | (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 3667 | if (type->tp_new == NULL) |
| 3668 | type->tp_new = base->tp_new; |
| 3669 | } |
| 3670 | } |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 3671 | type->tp_basicsize = newsize; |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3672 | |
| 3673 | /* Copy other non-function slots */ |
| 3674 | |
| 3675 | #undef COPYVAL |
| 3676 | #define COPYVAL(SLOT) \ |
| 3677 | if (type->SLOT == 0) type->SLOT = base->SLOT |
| 3678 | |
| 3679 | COPYVAL(tp_itemsize); |
| 3680 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) { |
| 3681 | COPYVAL(tp_weaklistoffset); |
| 3682 | } |
| 3683 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
| 3684 | COPYVAL(tp_dictoffset); |
| 3685 | } |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 3686 | |
| 3687 | /* Setup fast subclass flags */ |
| 3688 | if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException)) |
| 3689 | type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS; |
| 3690 | else if (PyType_IsSubtype(base, &PyType_Type)) |
| 3691 | type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS; |
| 3692 | else if (PyType_IsSubtype(base, &PyInt_Type)) |
| 3693 | type->tp_flags |= Py_TPFLAGS_INT_SUBCLASS; |
| 3694 | else if (PyType_IsSubtype(base, &PyLong_Type)) |
| 3695 | type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3696 | else if (PyType_IsSubtype(base, &PyString_Type)) |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 3697 | type->tp_flags |= Py_TPFLAGS_STRING_SUBCLASS; |
Georg Brandl | dfe5dc8 | 2008-01-07 18:16:36 +0000 | [diff] [blame] | 3698 | #ifdef Py_USING_UNICODE |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 3699 | else if (PyType_IsSubtype(base, &PyUnicode_Type)) |
| 3700 | type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS; |
Georg Brandl | dfe5dc8 | 2008-01-07 18:16:36 +0000 | [diff] [blame] | 3701 | #endif |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 3702 | else if (PyType_IsSubtype(base, &PyTuple_Type)) |
| 3703 | type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS; |
| 3704 | else if (PyType_IsSubtype(base, &PyList_Type)) |
| 3705 | type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS; |
| 3706 | else if (PyType_IsSubtype(base, &PyDict_Type)) |
| 3707 | type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3708 | } |
| 3709 | |
Nick Coghlan | 48361f5 | 2008-08-11 15:45:58 +0000 | [diff] [blame] | 3710 | static int |
| 3711 | overrides_name(PyTypeObject *type, char *name) |
| 3712 | { |
| 3713 | PyObject *dict = type->tp_dict; |
| 3714 | |
| 3715 | assert(dict != NULL); |
| 3716 | if (PyDict_GetItemString(dict, name) != NULL) { |
| 3717 | return 1; |
| 3718 | } |
| 3719 | return 0; |
| 3720 | } |
| 3721 | |
| 3722 | #define OVERRIDES_HASH(x) overrides_name(x, "__hash__") |
| 3723 | #define OVERRIDES_CMP(x) overrides_name(x, "__cmp__") |
| 3724 | #define OVERRIDES_EQ(x) overrides_name(x, "__eq__") |
| 3725 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3726 | static void |
| 3727 | inherit_slots(PyTypeObject *type, PyTypeObject *base) |
| 3728 | { |
| 3729 | PyTypeObject *basebase; |
| 3730 | |
| 3731 | #undef SLOTDEFINED |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3732 | #undef COPYSLOT |
| 3733 | #undef COPYNUM |
| 3734 | #undef COPYSEQ |
| 3735 | #undef COPYMAP |
Guido van Rossum | 5af588b | 2001-10-12 14:13:21 +0000 | [diff] [blame] | 3736 | #undef COPYBUF |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3737 | |
| 3738 | #define SLOTDEFINED(SLOT) \ |
| 3739 | (base->SLOT != 0 && \ |
| 3740 | (basebase == NULL || base->SLOT != basebase->SLOT)) |
| 3741 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3742 | #define COPYSLOT(SLOT) \ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3743 | if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3744 | |
| 3745 | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT) |
| 3746 | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT) |
| 3747 | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT) |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3748 | #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3749 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3750 | /* This won't inherit indirect slots (from tp_as_number etc.) |
| 3751 | if type doesn't provide the space. */ |
| 3752 | |
| 3753 | if (type->tp_as_number != NULL && base->tp_as_number != NULL) { |
| 3754 | basebase = base->tp_base; |
| 3755 | if (basebase->tp_as_number == NULL) |
| 3756 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3757 | COPYNUM(nb_add); |
| 3758 | COPYNUM(nb_subtract); |
| 3759 | COPYNUM(nb_multiply); |
| 3760 | COPYNUM(nb_divide); |
| 3761 | COPYNUM(nb_remainder); |
| 3762 | COPYNUM(nb_divmod); |
| 3763 | COPYNUM(nb_power); |
| 3764 | COPYNUM(nb_negative); |
| 3765 | COPYNUM(nb_positive); |
| 3766 | COPYNUM(nb_absolute); |
| 3767 | COPYNUM(nb_nonzero); |
| 3768 | COPYNUM(nb_invert); |
| 3769 | COPYNUM(nb_lshift); |
| 3770 | COPYNUM(nb_rshift); |
| 3771 | COPYNUM(nb_and); |
| 3772 | COPYNUM(nb_xor); |
| 3773 | COPYNUM(nb_or); |
| 3774 | COPYNUM(nb_coerce); |
| 3775 | COPYNUM(nb_int); |
| 3776 | COPYNUM(nb_long); |
| 3777 | COPYNUM(nb_float); |
| 3778 | COPYNUM(nb_oct); |
| 3779 | COPYNUM(nb_hex); |
| 3780 | COPYNUM(nb_inplace_add); |
| 3781 | COPYNUM(nb_inplace_subtract); |
| 3782 | COPYNUM(nb_inplace_multiply); |
| 3783 | COPYNUM(nb_inplace_divide); |
| 3784 | COPYNUM(nb_inplace_remainder); |
| 3785 | COPYNUM(nb_inplace_power); |
| 3786 | COPYNUM(nb_inplace_lshift); |
| 3787 | COPYNUM(nb_inplace_rshift); |
| 3788 | COPYNUM(nb_inplace_and); |
| 3789 | COPYNUM(nb_inplace_xor); |
| 3790 | COPYNUM(nb_inplace_or); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 3791 | if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) { |
| 3792 | COPYNUM(nb_true_divide); |
| 3793 | COPYNUM(nb_floor_divide); |
| 3794 | COPYNUM(nb_inplace_true_divide); |
| 3795 | COPYNUM(nb_inplace_floor_divide); |
| 3796 | } |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 3797 | if (base->tp_flags & Py_TPFLAGS_HAVE_INDEX) { |
| 3798 | COPYNUM(nb_index); |
| 3799 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3800 | } |
| 3801 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3802 | if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) { |
| 3803 | basebase = base->tp_base; |
| 3804 | if (basebase->tp_as_sequence == NULL) |
| 3805 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3806 | COPYSEQ(sq_length); |
| 3807 | COPYSEQ(sq_concat); |
| 3808 | COPYSEQ(sq_repeat); |
| 3809 | COPYSEQ(sq_item); |
| 3810 | COPYSEQ(sq_slice); |
| 3811 | COPYSEQ(sq_ass_item); |
| 3812 | COPYSEQ(sq_ass_slice); |
| 3813 | COPYSEQ(sq_contains); |
| 3814 | COPYSEQ(sq_inplace_concat); |
| 3815 | COPYSEQ(sq_inplace_repeat); |
| 3816 | } |
| 3817 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3818 | if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) { |
| 3819 | basebase = base->tp_base; |
| 3820 | if (basebase->tp_as_mapping == NULL) |
| 3821 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3822 | COPYMAP(mp_length); |
| 3823 | COPYMAP(mp_subscript); |
| 3824 | COPYMAP(mp_ass_subscript); |
| 3825 | } |
| 3826 | |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3827 | if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) { |
| 3828 | basebase = base->tp_base; |
| 3829 | if (basebase->tp_as_buffer == NULL) |
| 3830 | basebase = NULL; |
| 3831 | COPYBUF(bf_getreadbuffer); |
| 3832 | COPYBUF(bf_getwritebuffer); |
| 3833 | COPYBUF(bf_getsegcount); |
| 3834 | COPYBUF(bf_getcharbuffer); |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 3835 | COPYBUF(bf_getbuffer); |
| 3836 | COPYBUF(bf_releasebuffer); |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3837 | } |
| 3838 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3839 | basebase = base->tp_base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3840 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3841 | COPYSLOT(tp_dealloc); |
| 3842 | COPYSLOT(tp_print); |
| 3843 | if (type->tp_getattr == NULL && type->tp_getattro == NULL) { |
| 3844 | type->tp_getattr = base->tp_getattr; |
| 3845 | type->tp_getattro = base->tp_getattro; |
| 3846 | } |
| 3847 | if (type->tp_setattr == NULL && type->tp_setattro == NULL) { |
| 3848 | type->tp_setattr = base->tp_setattr; |
| 3849 | type->tp_setattro = base->tp_setattro; |
| 3850 | } |
| 3851 | /* tp_compare see tp_richcompare */ |
| 3852 | COPYSLOT(tp_repr); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3853 | /* tp_hash see tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3854 | COPYSLOT(tp_call); |
| 3855 | COPYSLOT(tp_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3856 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3857 | if (type->tp_compare == NULL && |
| 3858 | type->tp_richcompare == NULL && |
Nick Coghlan | 53663a6 | 2008-07-15 14:27:37 +0000 | [diff] [blame] | 3859 | type->tp_hash == NULL) |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3860 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3861 | type->tp_compare = base->tp_compare; |
| 3862 | type->tp_richcompare = base->tp_richcompare; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3863 | type->tp_hash = base->tp_hash; |
Nick Coghlan | 48361f5 | 2008-08-11 15:45:58 +0000 | [diff] [blame] | 3864 | /* Check for changes to inherited methods in Py3k*/ |
| 3865 | if (Py_Py3kWarningFlag) { |
| 3866 | if (base->tp_hash && |
| 3867 | (base->tp_hash != PyObject_HashNotImplemented) && |
| 3868 | !OVERRIDES_HASH(type)) { |
| 3869 | if (OVERRIDES_CMP(type)) { |
| 3870 | PyErr_WarnPy3k("Overriding " |
| 3871 | "__cmp__ blocks inheritance " |
| 3872 | "of __hash__ in 3.x", |
| 3873 | 1); |
| 3874 | } |
| 3875 | if (OVERRIDES_EQ(type)) { |
| 3876 | PyErr_WarnPy3k("Overriding " |
| 3877 | "__eq__ blocks inheritance " |
| 3878 | "of __hash__ in 3.x", |
| 3879 | 1); |
| 3880 | } |
| 3881 | } |
| 3882 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3883 | } |
| 3884 | } |
| 3885 | else { |
| 3886 | COPYSLOT(tp_compare); |
| 3887 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3888 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) { |
| 3889 | COPYSLOT(tp_iter); |
| 3890 | COPYSLOT(tp_iternext); |
| 3891 | } |
| 3892 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
| 3893 | COPYSLOT(tp_descr_get); |
| 3894 | COPYSLOT(tp_descr_set); |
| 3895 | COPYSLOT(tp_dictoffset); |
| 3896 | COPYSLOT(tp_init); |
| 3897 | COPYSLOT(tp_alloc); |
Guido van Rossum | cc8fe04 | 2002-04-05 17:10:16 +0000 | [diff] [blame] | 3898 | COPYSLOT(tp_is_gc); |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 3899 | if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 3900 | (base->tp_flags & Py_TPFLAGS_HAVE_GC)) { |
| 3901 | /* They agree about gc. */ |
| 3902 | COPYSLOT(tp_free); |
| 3903 | } |
| 3904 | else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3905 | type->tp_free == NULL && |
| 3906 | base->tp_free == _PyObject_Del) { |
| 3907 | /* A bit of magic to plug in the correct default |
| 3908 | * tp_free function when a derived class adds gc, |
| 3909 | * didn't define tp_free, and the base uses the |
| 3910 | * default non-gc tp_free. |
| 3911 | */ |
| 3912 | type->tp_free = PyObject_GC_Del; |
| 3913 | } |
| 3914 | /* else they didn't agree about gc, and there isn't something |
| 3915 | * obvious to be done -- the type is on its own. |
| 3916 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3917 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3918 | } |
| 3919 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 3920 | static int add_operators(PyTypeObject *); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3921 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3922 | int |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 3923 | PyType_Ready(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3924 | { |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 3925 | PyObject *dict, *bases; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3926 | PyTypeObject *base; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3927 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3928 | |
Guido van Rossum | cab0580 | 2002-06-10 15:29:03 +0000 | [diff] [blame] | 3929 | if (type->tp_flags & Py_TPFLAGS_READY) { |
| 3930 | assert(type->tp_dict != NULL); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3931 | return 0; |
Guido van Rossum | cab0580 | 2002-06-10 15:29:03 +0000 | [diff] [blame] | 3932 | } |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3933 | assert((type->tp_flags & Py_TPFLAGS_READYING) == 0); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3934 | |
| 3935 | type->tp_flags |= Py_TPFLAGS_READYING; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3936 | |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 3937 | #ifdef Py_TRACE_REFS |
| 3938 | /* PyType_Ready is the closest thing we have to a choke point |
| 3939 | * for type objects, so is the best place I can think of to try |
| 3940 | * to get type objects into the doubly-linked list of all objects. |
| 3941 | * Still, not all type objects go thru PyType_Ready. |
| 3942 | */ |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 3943 | _Py_AddToAllObjects((PyObject *)type, 0); |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 3944 | #endif |
| 3945 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3946 | /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
| 3947 | base = type->tp_base; |
Martin v. Löwis | bf60875 | 2004-08-18 13:16:54 +0000 | [diff] [blame] | 3948 | if (base == NULL && type != &PyBaseObject_Type) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3949 | base = type->tp_base = &PyBaseObject_Type; |
Martin v. Löwis | bf60875 | 2004-08-18 13:16:54 +0000 | [diff] [blame] | 3950 | Py_INCREF(base); |
| 3951 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3952 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3953 | /* Now the only way base can still be NULL is if type is |
| 3954 | * &PyBaseObject_Type. |
| 3955 | */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 3956 | |
Guido van Rossum | 323a9cf | 2002-08-14 17:26:30 +0000 | [diff] [blame] | 3957 | /* Initialize the base class */ |
| 3958 | if (base && base->tp_dict == NULL) { |
| 3959 | if (PyType_Ready(base) < 0) |
| 3960 | goto error; |
| 3961 | } |
| 3962 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3963 | /* 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] | 3964 | compilable separately on Windows can call PyType_Ready() instead of |
| 3965 | initializing the ob_type field of their type objects. */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 3966 | /* The test for base != NULL is really unnecessary, since base is only |
| 3967 | NULL when type is &PyBaseObject_Type, and we know its ob_type is |
| 3968 | not NULL (it's initialized to &PyType_Type). But coverity doesn't |
| 3969 | know that. */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 3970 | if (Py_TYPE(type) == NULL && base != NULL) |
| 3971 | Py_TYPE(type) = Py_TYPE(base); |
Guido van Rossum | 0986d82 | 2002-04-08 01:38:42 +0000 | [diff] [blame] | 3972 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3973 | /* Initialize tp_bases */ |
| 3974 | bases = type->tp_bases; |
| 3975 | if (bases == NULL) { |
| 3976 | if (base == NULL) |
| 3977 | bases = PyTuple_New(0); |
| 3978 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3979 | bases = PyTuple_Pack(1, base); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3980 | if (bases == NULL) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3981 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3982 | type->tp_bases = bases; |
| 3983 | } |
| 3984 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3985 | /* Initialize tp_dict */ |
| 3986 | dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3987 | if (dict == NULL) { |
| 3988 | dict = PyDict_New(); |
| 3989 | if (dict == NULL) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3990 | goto error; |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3991 | type->tp_dict = dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3992 | } |
| 3993 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3994 | /* Add type-specific descriptors to tp_dict */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3995 | if (add_operators(type) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3996 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3997 | if (type->tp_methods != NULL) { |
| 3998 | if (add_methods(type, type->tp_methods) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3999 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4000 | } |
| 4001 | if (type->tp_members != NULL) { |
| 4002 | if (add_members(type, type->tp_members) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 4003 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4004 | } |
| 4005 | if (type->tp_getset != NULL) { |
| 4006 | if (add_getset(type, type->tp_getset) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 4007 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4008 | } |
| 4009 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4010 | /* Calculate method resolution order */ |
| 4011 | if (mro_internal(type) < 0) { |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 4012 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4015 | /* Inherit special flags from dominant base */ |
| 4016 | if (type->tp_base != NULL) |
| 4017 | inherit_special(type, type->tp_base); |
| 4018 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4019 | /* Initialize tp_dict properly */ |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 4020 | bases = type->tp_mro; |
| 4021 | assert(bases != NULL); |
| 4022 | assert(PyTuple_Check(bases)); |
| 4023 | n = PyTuple_GET_SIZE(bases); |
| 4024 | for (i = 1; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 4025 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 4026 | if (PyType_Check(b)) |
| 4027 | inherit_slots(type, (PyTypeObject *)b); |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4028 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4029 | |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 4030 | /* Sanity check for tp_free. */ |
| 4031 | if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) && |
| 4032 | (type->tp_free == NULL || type->tp_free == PyObject_Del)) { |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 4033 | /* This base class needs to call tp_free, but doesn't have |
| 4034 | * one, or its tp_free is for non-gc'ed objects. |
| 4035 | */ |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 4036 | PyErr_Format(PyExc_TypeError, "type '%.100s' participates in " |
| 4037 | "gc and is a base type but has inappropriate " |
| 4038 | "tp_free slot", |
| 4039 | type->tp_name); |
| 4040 | goto error; |
| 4041 | } |
| 4042 | |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 4043 | /* if the type dictionary doesn't contain a __doc__, set it from |
| 4044 | the tp_doc slot. |
| 4045 | */ |
| 4046 | if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) { |
| 4047 | if (type->tp_doc != NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4048 | PyObject *doc = PyString_FromString(type->tp_doc); |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 4049 | if (doc == NULL) |
| 4050 | goto error; |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 4051 | PyDict_SetItemString(type->tp_dict, "__doc__", doc); |
| 4052 | Py_DECREF(doc); |
| 4053 | } else { |
Guido van Rossum | d464107 | 2002-04-03 02:13:37 +0000 | [diff] [blame] | 4054 | PyDict_SetItemString(type->tp_dict, |
| 4055 | "__doc__", Py_None); |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 4056 | } |
| 4057 | } |
| 4058 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4059 | /* Some more special stuff */ |
| 4060 | base = type->tp_base; |
| 4061 | if (base != NULL) { |
| 4062 | if (type->tp_as_number == NULL) |
| 4063 | type->tp_as_number = base->tp_as_number; |
| 4064 | if (type->tp_as_sequence == NULL) |
| 4065 | type->tp_as_sequence = base->tp_as_sequence; |
| 4066 | if (type->tp_as_mapping == NULL) |
| 4067 | type->tp_as_mapping = base->tp_as_mapping; |
Guido van Rossum | eea4718 | 2003-02-11 20:39:59 +0000 | [diff] [blame] | 4068 | if (type->tp_as_buffer == NULL) |
| 4069 | type->tp_as_buffer = base->tp_as_buffer; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4070 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4071 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4072 | /* Link into each base class's list of subclasses */ |
| 4073 | bases = type->tp_bases; |
| 4074 | n = PyTuple_GET_SIZE(bases); |
| 4075 | for (i = 0; i < n; i++) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 4076 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 4077 | if (PyType_Check(b) && |
| 4078 | add_subclass((PyTypeObject *)b, type) < 0) |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4079 | goto error; |
| 4080 | } |
| 4081 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4082 | /* All done -- set the ready flag */ |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 4083 | assert(type->tp_dict != NULL); |
| 4084 | type->tp_flags = |
| 4085 | (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4086 | return 0; |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 4087 | |
| 4088 | error: |
| 4089 | type->tp_flags &= ~Py_TPFLAGS_READYING; |
| 4090 | return -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4091 | } |
| 4092 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4093 | static int |
| 4094 | add_subclass(PyTypeObject *base, PyTypeObject *type) |
| 4095 | { |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 4096 | Py_ssize_t i; |
| 4097 | int result; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 4098 | PyObject *list, *ref, *newobj; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4099 | |
| 4100 | list = base->tp_subclasses; |
| 4101 | if (list == NULL) { |
| 4102 | base->tp_subclasses = list = PyList_New(0); |
| 4103 | if (list == NULL) |
| 4104 | return -1; |
| 4105 | } |
| 4106 | assert(PyList_Check(list)); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 4107 | newobj = PyWeakref_NewRef((PyObject *)type, NULL); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4108 | i = PyList_GET_SIZE(list); |
| 4109 | while (--i >= 0) { |
| 4110 | ref = PyList_GET_ITEM(list, i); |
| 4111 | assert(PyWeakref_CheckRef(ref)); |
Guido van Rossum | 3930bc3 | 2002-10-18 13:51:49 +0000 | [diff] [blame] | 4112 | if (PyWeakref_GET_OBJECT(ref) == Py_None) |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 4113 | return PyList_SetItem(list, i, newobj); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4114 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 4115 | result = PyList_Append(list, newobj); |
| 4116 | Py_DECREF(newobj); |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 4117 | return result; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4118 | } |
| 4119 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4120 | static void |
| 4121 | remove_subclass(PyTypeObject *base, PyTypeObject *type) |
| 4122 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4123 | Py_ssize_t i; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4124 | PyObject *list, *ref; |
| 4125 | |
| 4126 | list = base->tp_subclasses; |
| 4127 | if (list == NULL) { |
| 4128 | return; |
| 4129 | } |
| 4130 | assert(PyList_Check(list)); |
| 4131 | i = PyList_GET_SIZE(list); |
| 4132 | while (--i >= 0) { |
| 4133 | ref = PyList_GET_ITEM(list, i); |
| 4134 | assert(PyWeakref_CheckRef(ref)); |
| 4135 | if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) { |
| 4136 | /* this can't fail, right? */ |
| 4137 | PySequence_DelItem(list, i); |
| 4138 | return; |
| 4139 | } |
| 4140 | } |
| 4141 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4142 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4143 | static int |
| 4144 | check_num_args(PyObject *ob, int n) |
| 4145 | { |
| 4146 | if (!PyTuple_CheckExact(ob)) { |
| 4147 | PyErr_SetString(PyExc_SystemError, |
| 4148 | "PyArg_UnpackTuple() argument list is not a tuple"); |
| 4149 | return 0; |
| 4150 | } |
| 4151 | if (n == PyTuple_GET_SIZE(ob)) |
| 4152 | return 1; |
| 4153 | PyErr_Format( |
| 4154 | PyExc_TypeError, |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 4155 | "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob)); |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4156 | return 0; |
| 4157 | } |
| 4158 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4159 | /* Generic wrappers for overloadable 'operators' such as __getitem__ */ |
| 4160 | |
| 4161 | /* There's a wrapper *function* for each distinct function typedef used |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 4162 | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4163 | wrapper *table* for each distinct operation (e.g. __len__, __add__). |
| 4164 | Most tables have only one entry; the tables for binary operators have two |
| 4165 | entries, one regular and one with reversed arguments. */ |
| 4166 | |
| 4167 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4168 | wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4169 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4170 | lenfunc func = (lenfunc)wrapped; |
| 4171 | Py_ssize_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4172 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4173 | if (!check_num_args(args, 0)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4174 | return NULL; |
| 4175 | res = (*func)(self); |
| 4176 | if (res == -1 && PyErr_Occurred()) |
| 4177 | return NULL; |
| 4178 | return PyInt_FromLong((long)res); |
| 4179 | } |
| 4180 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4181 | static PyObject * |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4182 | wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped) |
| 4183 | { |
| 4184 | inquiry func = (inquiry)wrapped; |
| 4185 | int res; |
| 4186 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4187 | if (!check_num_args(args, 0)) |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4188 | return NULL; |
| 4189 | res = (*func)(self); |
| 4190 | if (res == -1 && PyErr_Occurred()) |
| 4191 | return NULL; |
| 4192 | return PyBool_FromLong((long)res); |
| 4193 | } |
| 4194 | |
| 4195 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4196 | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4197 | { |
| 4198 | binaryfunc func = (binaryfunc)wrapped; |
| 4199 | PyObject *other; |
| 4200 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4201 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4202 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4203 | other = PyTuple_GET_ITEM(args, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4204 | return (*func)(self, other); |
| 4205 | } |
| 4206 | |
| 4207 | static PyObject * |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4208 | wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped) |
| 4209 | { |
| 4210 | binaryfunc func = (binaryfunc)wrapped; |
| 4211 | PyObject *other; |
| 4212 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4213 | if (!check_num_args(args, 1)) |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4214 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4215 | other = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4216 | if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) && |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4217 | !PyType_IsSubtype(other->ob_type, self->ob_type)) { |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4218 | Py_INCREF(Py_NotImplemented); |
| 4219 | return Py_NotImplemented; |
| 4220 | } |
| 4221 | return (*func)(self, other); |
| 4222 | } |
| 4223 | |
| 4224 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4225 | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4226 | { |
| 4227 | binaryfunc func = (binaryfunc)wrapped; |
| 4228 | PyObject *other; |
| 4229 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4230 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4231 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4232 | other = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4233 | if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) && |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4234 | !PyType_IsSubtype(other->ob_type, self->ob_type)) { |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4235 | Py_INCREF(Py_NotImplemented); |
| 4236 | return Py_NotImplemented; |
| 4237 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4238 | return (*func)(other, self); |
| 4239 | } |
| 4240 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4241 | static PyObject * |
| 4242 | wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped) |
| 4243 | { |
| 4244 | coercion func = (coercion)wrapped; |
| 4245 | PyObject *other, *res; |
| 4246 | int ok; |
| 4247 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4248 | if (!check_num_args(args, 1)) |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4249 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4250 | other = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4251 | ok = func(&self, &other); |
| 4252 | if (ok < 0) |
| 4253 | return NULL; |
| 4254 | if (ok > 0) { |
| 4255 | Py_INCREF(Py_NotImplemented); |
| 4256 | return Py_NotImplemented; |
| 4257 | } |
| 4258 | res = PyTuple_New(2); |
| 4259 | if (res == NULL) { |
| 4260 | Py_DECREF(self); |
| 4261 | Py_DECREF(other); |
| 4262 | return NULL; |
| 4263 | } |
| 4264 | PyTuple_SET_ITEM(res, 0, self); |
| 4265 | PyTuple_SET_ITEM(res, 1, other); |
| 4266 | return res; |
| 4267 | } |
| 4268 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4269 | static PyObject * |
| 4270 | wrap_ternaryfunc(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)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4279 | return NULL; |
| 4280 | return (*func)(self, other, third); |
| 4281 | } |
| 4282 | |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4283 | static PyObject * |
| 4284 | wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4285 | { |
| 4286 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4287 | PyObject *other; |
| 4288 | PyObject *third = Py_None; |
| 4289 | |
| 4290 | /* Note: This wrapper only works for __pow__() */ |
| 4291 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4292 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4293 | return NULL; |
| 4294 | return (*func)(other, self, third); |
| 4295 | } |
| 4296 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4297 | static PyObject * |
| 4298 | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4299 | { |
| 4300 | unaryfunc func = (unaryfunc)wrapped; |
| 4301 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4302 | if (!check_num_args(args, 0)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4303 | return NULL; |
| 4304 | return (*func)(self); |
| 4305 | } |
| 4306 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4307 | static PyObject * |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 4308 | wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4309 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4310 | ssizeargfunc func = (ssizeargfunc)wrapped; |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 4311 | PyObject* o; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4312 | Py_ssize_t i; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4313 | |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 4314 | if (!PyArg_UnpackTuple(args, "", 1, 1, &o)) |
| 4315 | return NULL; |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 4316 | i = PyNumber_AsSsize_t(o, PyExc_OverflowError); |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 4317 | if (i == -1 && PyErr_Occurred()) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4318 | return NULL; |
| 4319 | return (*func)(self, i); |
| 4320 | } |
| 4321 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4322 | static Py_ssize_t |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4323 | getindex(PyObject *self, PyObject *arg) |
| 4324 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4325 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4326 | |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 4327 | i = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4328 | if (i == -1 && PyErr_Occurred()) |
| 4329 | return -1; |
| 4330 | if (i < 0) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4331 | PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4332 | if (sq && sq->sq_length) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4333 | Py_ssize_t n = (*sq->sq_length)(self); |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4334 | if (n < 0) |
| 4335 | return -1; |
| 4336 | i += n; |
| 4337 | } |
| 4338 | } |
| 4339 | return i; |
| 4340 | } |
| 4341 | |
| 4342 | static PyObject * |
| 4343 | wrap_sq_item(PyObject *self, PyObject *args, void *wrapped) |
| 4344 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4345 | ssizeargfunc func = (ssizeargfunc)wrapped; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4346 | PyObject *arg; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4347 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4348 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4349 | if (PyTuple_GET_SIZE(args) == 1) { |
| 4350 | arg = PyTuple_GET_ITEM(args, 0); |
| 4351 | i = getindex(self, arg); |
| 4352 | if (i == -1 && PyErr_Occurred()) |
| 4353 | return NULL; |
| 4354 | return (*func)(self, i); |
| 4355 | } |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4356 | check_num_args(args, 1); |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4357 | assert(PyErr_Occurred()); |
| 4358 | return NULL; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4361 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4362 | wrap_ssizessizeargfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4363 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4364 | ssizessizeargfunc func = (ssizessizeargfunc)wrapped; |
| 4365 | Py_ssize_t i, j; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4366 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4367 | if (!PyArg_ParseTuple(args, "nn", &i, &j)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4368 | return NULL; |
| 4369 | return (*func)(self, i, j); |
| 4370 | } |
| 4371 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4372 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4373 | wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4374 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4375 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4376 | Py_ssize_t i; |
| 4377 | int res; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4378 | PyObject *arg, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4379 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4380 | if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value)) |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4381 | return NULL; |
| 4382 | i = getindex(self, arg); |
| 4383 | if (i == -1 && PyErr_Occurred()) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4384 | return NULL; |
| 4385 | res = (*func)(self, i, value); |
| 4386 | if (res == -1 && PyErr_Occurred()) |
| 4387 | return NULL; |
| 4388 | Py_INCREF(Py_None); |
| 4389 | return Py_None; |
| 4390 | } |
| 4391 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4392 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4393 | wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4394 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4395 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4396 | Py_ssize_t i; |
| 4397 | int res; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4398 | PyObject *arg; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4399 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4400 | if (!check_num_args(args, 1)) |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4401 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4402 | arg = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4403 | i = getindex(self, arg); |
| 4404 | if (i == -1 && PyErr_Occurred()) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4405 | return NULL; |
| 4406 | res = (*func)(self, i, NULL); |
| 4407 | if (res == -1 && PyErr_Occurred()) |
| 4408 | return NULL; |
| 4409 | Py_INCREF(Py_None); |
| 4410 | return Py_None; |
| 4411 | } |
| 4412 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4413 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4414 | wrap_ssizessizeobjargproc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4415 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4416 | ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped; |
| 4417 | Py_ssize_t i, j; |
| 4418 | int res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4419 | PyObject *value; |
| 4420 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4421 | if (!PyArg_ParseTuple(args, "nnO", &i, &j, &value)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4422 | return NULL; |
| 4423 | res = (*func)(self, i, j, value); |
| 4424 | if (res == -1 && PyErr_Occurred()) |
| 4425 | return NULL; |
| 4426 | Py_INCREF(Py_None); |
| 4427 | return Py_None; |
| 4428 | } |
| 4429 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4430 | static PyObject * |
| 4431 | wrap_delslice(PyObject *self, PyObject *args, void *wrapped) |
| 4432 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4433 | ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped; |
| 4434 | Py_ssize_t i, j; |
| 4435 | int res; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4436 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4437 | if (!PyArg_ParseTuple(args, "nn", &i, &j)) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4438 | return NULL; |
| 4439 | res = (*func)(self, i, j, NULL); |
| 4440 | if (res == -1 && PyErr_Occurred()) |
| 4441 | return NULL; |
| 4442 | Py_INCREF(Py_None); |
| 4443 | return Py_None; |
| 4444 | } |
| 4445 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4446 | /* XXX objobjproc is a misnomer; should be objargpred */ |
| 4447 | static PyObject * |
| 4448 | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) |
| 4449 | { |
| 4450 | objobjproc func = (objobjproc)wrapped; |
| 4451 | int res; |
Guido van Rossum | 22c3dda | 2003-10-09 03:46:35 +0000 | [diff] [blame] | 4452 | PyObject *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4453 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4454 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4455 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4456 | value = PyTuple_GET_ITEM(args, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4457 | res = (*func)(self, value); |
| 4458 | if (res == -1 && PyErr_Occurred()) |
| 4459 | return NULL; |
Guido van Rossum | 22c3dda | 2003-10-09 03:46:35 +0000 | [diff] [blame] | 4460 | else |
| 4461 | return PyBool_FromLong(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4462 | } |
| 4463 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4464 | static PyObject * |
| 4465 | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 4466 | { |
| 4467 | objobjargproc func = (objobjargproc)wrapped; |
| 4468 | int res; |
| 4469 | PyObject *key, *value; |
| 4470 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4471 | if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4472 | return NULL; |
| 4473 | res = (*func)(self, key, value); |
| 4474 | if (res == -1 && PyErr_Occurred()) |
| 4475 | return NULL; |
| 4476 | Py_INCREF(Py_None); |
| 4477 | return Py_None; |
| 4478 | } |
| 4479 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4480 | static PyObject * |
| 4481 | wrap_delitem(PyObject *self, PyObject *args, void *wrapped) |
| 4482 | { |
| 4483 | objobjargproc func = (objobjargproc)wrapped; |
| 4484 | int res; |
| 4485 | PyObject *key; |
| 4486 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4487 | if (!check_num_args(args, 1)) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4488 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4489 | key = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4490 | res = (*func)(self, key, NULL); |
| 4491 | if (res == -1 && PyErr_Occurred()) |
| 4492 | return NULL; |
| 4493 | Py_INCREF(Py_None); |
| 4494 | return Py_None; |
| 4495 | } |
| 4496 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4497 | static PyObject * |
| 4498 | wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4499 | { |
| 4500 | cmpfunc func = (cmpfunc)wrapped; |
| 4501 | int res; |
| 4502 | PyObject *other; |
| 4503 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4504 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4505 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4506 | other = PyTuple_GET_ITEM(args, 0); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4507 | if (Py_TYPE(other)->tp_compare != func && |
| 4508 | !PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) { |
Guido van Rossum | ceccae5 | 2001-09-18 20:03:57 +0000 | [diff] [blame] | 4509 | PyErr_Format( |
| 4510 | PyExc_TypeError, |
| 4511 | "%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] | 4512 | Py_TYPE(self)->tp_name, |
| 4513 | Py_TYPE(self)->tp_name, |
| 4514 | Py_TYPE(other)->tp_name); |
Guido van Rossum | ceccae5 | 2001-09-18 20:03:57 +0000 | [diff] [blame] | 4515 | return NULL; |
| 4516 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4517 | res = (*func)(self, other); |
| 4518 | if (PyErr_Occurred()) |
| 4519 | return NULL; |
| 4520 | return PyInt_FromLong((long)res); |
| 4521 | } |
| 4522 | |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4523 | /* 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] | 4524 | This is called the Carlo Verre hack after its discoverer. */ |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4525 | static int |
| 4526 | hackcheck(PyObject *self, setattrofunc func, char *what) |
| 4527 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4528 | PyTypeObject *type = Py_TYPE(self); |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4529 | while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 4530 | type = type->tp_base; |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 4531 | /* If type is NULL now, this is a really weird type. |
| 4532 | In the spirit of backwards compatibility (?), just shut up. */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 4533 | if (type && type->tp_setattro != func) { |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4534 | PyErr_Format(PyExc_TypeError, |
| 4535 | "can't apply this %s to %s object", |
| 4536 | what, |
| 4537 | type->tp_name); |
| 4538 | return 0; |
| 4539 | } |
| 4540 | return 1; |
| 4541 | } |
| 4542 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4543 | static PyObject * |
| 4544 | wrap_setattr(PyObject *self, PyObject *args, void *wrapped) |
| 4545 | { |
| 4546 | setattrofunc func = (setattrofunc)wrapped; |
| 4547 | int res; |
| 4548 | PyObject *name, *value; |
| 4549 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4550 | if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4551 | return NULL; |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4552 | if (!hackcheck(self, func, "__setattr__")) |
| 4553 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4554 | res = (*func)(self, name, value); |
| 4555 | if (res < 0) |
| 4556 | return NULL; |
| 4557 | Py_INCREF(Py_None); |
| 4558 | return Py_None; |
| 4559 | } |
| 4560 | |
| 4561 | static PyObject * |
| 4562 | wrap_delattr(PyObject *self, PyObject *args, void *wrapped) |
| 4563 | { |
| 4564 | setattrofunc func = (setattrofunc)wrapped; |
| 4565 | int res; |
| 4566 | PyObject *name; |
| 4567 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4568 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4569 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4570 | name = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4571 | if (!hackcheck(self, func, "__delattr__")) |
| 4572 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4573 | res = (*func)(self, name, NULL); |
| 4574 | if (res < 0) |
| 4575 | return NULL; |
| 4576 | Py_INCREF(Py_None); |
| 4577 | return Py_None; |
| 4578 | } |
| 4579 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4580 | static PyObject * |
| 4581 | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4582 | { |
| 4583 | hashfunc func = (hashfunc)wrapped; |
| 4584 | long res; |
| 4585 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4586 | if (!check_num_args(args, 0)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4587 | return NULL; |
| 4588 | res = (*func)(self); |
| 4589 | if (res == -1 && PyErr_Occurred()) |
| 4590 | return NULL; |
| 4591 | return PyInt_FromLong(res); |
| 4592 | } |
| 4593 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4594 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4595 | wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4596 | { |
| 4597 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4598 | |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4599 | return (*func)(self, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4600 | } |
| 4601 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4602 | static PyObject * |
| 4603 | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) |
| 4604 | { |
| 4605 | richcmpfunc func = (richcmpfunc)wrapped; |
| 4606 | PyObject *other; |
| 4607 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4608 | if (!check_num_args(args, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4609 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4610 | other = PyTuple_GET_ITEM(args, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4611 | return (*func)(self, other, op); |
| 4612 | } |
| 4613 | |
| 4614 | #undef RICHCMP_WRAPPER |
| 4615 | #define RICHCMP_WRAPPER(NAME, OP) \ |
| 4616 | static PyObject * \ |
| 4617 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ |
| 4618 | { \ |
| 4619 | return wrap_richcmpfunc(self, args, wrapped, OP); \ |
| 4620 | } |
| 4621 | |
Jack Jansen | 8e938b4 | 2001-08-08 15:29:49 +0000 | [diff] [blame] | 4622 | RICHCMP_WRAPPER(lt, Py_LT) |
| 4623 | RICHCMP_WRAPPER(le, Py_LE) |
| 4624 | RICHCMP_WRAPPER(eq, Py_EQ) |
| 4625 | RICHCMP_WRAPPER(ne, Py_NE) |
| 4626 | RICHCMP_WRAPPER(gt, Py_GT) |
| 4627 | RICHCMP_WRAPPER(ge, Py_GE) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4628 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4629 | static PyObject * |
| 4630 | wrap_next(PyObject *self, PyObject *args, void *wrapped) |
| 4631 | { |
| 4632 | unaryfunc func = (unaryfunc)wrapped; |
| 4633 | PyObject *res; |
| 4634 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4635 | if (!check_num_args(args, 0)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4636 | return NULL; |
| 4637 | res = (*func)(self); |
| 4638 | if (res == NULL && !PyErr_Occurred()) |
| 4639 | PyErr_SetNone(PyExc_StopIteration); |
| 4640 | return res; |
| 4641 | } |
| 4642 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4643 | static PyObject * |
| 4644 | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped) |
| 4645 | { |
| 4646 | descrgetfunc func = (descrgetfunc)wrapped; |
| 4647 | PyObject *obj; |
| 4648 | PyObject *type = NULL; |
| 4649 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4650 | if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4651 | return NULL; |
Guido van Rossum | 82ed25c | 2003-02-11 16:25:43 +0000 | [diff] [blame] | 4652 | if (obj == Py_None) |
| 4653 | obj = NULL; |
| 4654 | if (type == Py_None) |
| 4655 | type = NULL; |
| 4656 | if (type == NULL &&obj == NULL) { |
| 4657 | PyErr_SetString(PyExc_TypeError, |
| 4658 | "__get__(None, None) is invalid"); |
| 4659 | return NULL; |
| 4660 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4661 | return (*func)(self, obj, type); |
| 4662 | } |
| 4663 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4664 | static PyObject * |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4665 | wrap_descr_set(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4666 | { |
| 4667 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4668 | PyObject *obj, *value; |
| 4669 | int ret; |
| 4670 | |
Raymond Hettinger | 56bb16f | 2003-10-11 19:32:18 +0000 | [diff] [blame] | 4671 | if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4672 | return NULL; |
| 4673 | ret = (*func)(self, obj, value); |
| 4674 | if (ret < 0) |
| 4675 | return NULL; |
| 4676 | Py_INCREF(Py_None); |
| 4677 | return Py_None; |
| 4678 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 4679 | |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4680 | static PyObject * |
| 4681 | wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped) |
| 4682 | { |
| 4683 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4684 | PyObject *obj; |
| 4685 | int ret; |
| 4686 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4687 | if (!check_num_args(args, 1)) |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4688 | return NULL; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4689 | obj = PyTuple_GET_ITEM(args, 0); |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4690 | ret = (*func)(self, obj, NULL); |
| 4691 | if (ret < 0) |
| 4692 | return NULL; |
| 4693 | Py_INCREF(Py_None); |
| 4694 | return Py_None; |
| 4695 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4696 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4697 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4698 | wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4699 | { |
| 4700 | initproc func = (initproc)wrapped; |
| 4701 | |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4702 | if (func(self, args, kwds) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4703 | return NULL; |
| 4704 | Py_INCREF(Py_None); |
| 4705 | return Py_None; |
| 4706 | } |
| 4707 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4708 | static PyObject * |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4709 | tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4710 | { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4711 | PyTypeObject *type, *subtype, *staticbase; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4712 | PyObject *arg0, *res; |
| 4713 | |
| 4714 | if (self == NULL || !PyType_Check(self)) |
| 4715 | Py_FatalError("__new__() called with non-type 'self'"); |
| 4716 | type = (PyTypeObject *)self; |
| 4717 | if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4718 | PyErr_Format(PyExc_TypeError, |
| 4719 | "%s.__new__(): not enough arguments", |
| 4720 | type->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4721 | return NULL; |
| 4722 | } |
| 4723 | arg0 = PyTuple_GET_ITEM(args, 0); |
| 4724 | if (!PyType_Check(arg0)) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4725 | PyErr_Format(PyExc_TypeError, |
| 4726 | "%s.__new__(X): X is not a type object (%s)", |
| 4727 | type->tp_name, |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4728 | Py_TYPE(arg0)->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4729 | return NULL; |
| 4730 | } |
| 4731 | subtype = (PyTypeObject *)arg0; |
| 4732 | if (!PyType_IsSubtype(subtype, type)) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4733 | PyErr_Format(PyExc_TypeError, |
| 4734 | "%s.__new__(%s): %s is not a subtype of %s", |
| 4735 | type->tp_name, |
| 4736 | subtype->tp_name, |
| 4737 | subtype->tp_name, |
| 4738 | type->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4739 | return NULL; |
| 4740 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4741 | |
| 4742 | /* Check that the use doesn't do something silly and unsafe like |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 4743 | object.__new__(dict). To do this, we check that the |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4744 | most derived base that's not a heap type is this type. */ |
| 4745 | staticbase = subtype; |
| 4746 | while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 4747 | staticbase = staticbase->tp_base; |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 4748 | /* If staticbase is NULL now, it is a really weird type. |
| 4749 | In the spirit of backwards compatibility (?), just shut up. */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 4750 | if (staticbase && staticbase->tp_new != type->tp_new) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4751 | PyErr_Format(PyExc_TypeError, |
| 4752 | "%s.__new__(%s) is not safe, use %s.__new__()", |
| 4753 | type->tp_name, |
| 4754 | subtype->tp_name, |
| 4755 | staticbase == NULL ? "?" : staticbase->tp_name); |
| 4756 | return NULL; |
| 4757 | } |
| 4758 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4759 | args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 4760 | if (args == NULL) |
| 4761 | return NULL; |
| 4762 | res = type->tp_new(subtype, args, kwds); |
| 4763 | Py_DECREF(args); |
| 4764 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4765 | } |
| 4766 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4767 | static struct PyMethodDef tp_new_methoddef[] = { |
Neal Norwitz | a84dcd7 | 2007-05-22 07:16:44 +0000 | [diff] [blame] | 4768 | {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 4769 | PyDoc_STR("T.__new__(S, ...) -> " |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 4770 | "a new object with type S, a subtype of T")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4771 | {0} |
| 4772 | }; |
| 4773 | |
| 4774 | static int |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4775 | add_tp_new_wrapper(PyTypeObject *type) |
| 4776 | { |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4777 | PyObject *func; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4778 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 4779 | if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL) |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4780 | return 0; |
| 4781 | func = PyCFunction_New(tp_new_methoddef, (PyObject *)type); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4782 | if (func == NULL) |
| 4783 | return -1; |
Raymond Hettinger | 8d726ee | 2004-06-25 22:24:35 +0000 | [diff] [blame] | 4784 | if (PyDict_SetItemString(type->tp_dict, "__new__", func)) { |
Raymond Hettinger | d56cbe5 | 2004-06-25 22:17:39 +0000 | [diff] [blame] | 4785 | Py_DECREF(func); |
| 4786 | return -1; |
| 4787 | } |
| 4788 | Py_DECREF(func); |
| 4789 | return 0; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4790 | } |
| 4791 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4792 | /* Slot wrappers that call the corresponding __foo__ slot. See comments |
| 4793 | below at override_slots() for more explanation. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4794 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4795 | #define SLOT0(FUNCNAME, OPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4796 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4797 | FUNCNAME(PyObject *self) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4798 | { \ |
Guido van Rossum | 5592e4d | 2001-08-28 18:28:21 +0000 | [diff] [blame] | 4799 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4800 | return call_method(self, OPSTR, &cache_str, "()"); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4801 | } |
| 4802 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4803 | #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4804 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4805 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4806 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4807 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4808 | return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4809 | } |
| 4810 | |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4811 | /* Boolean helper for SLOT1BINFULL(). |
| 4812 | right.__class__ is a nontrivial subclass of left.__class__. */ |
| 4813 | static int |
| 4814 | method_is_overloaded(PyObject *left, PyObject *right, char *name) |
| 4815 | { |
| 4816 | PyObject *a, *b; |
| 4817 | int ok; |
| 4818 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4819 | b = PyObject_GetAttrString((PyObject *)(Py_TYPE(right)), name); |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4820 | if (b == NULL) { |
| 4821 | PyErr_Clear(); |
| 4822 | /* If right doesn't have it, it's not overloaded */ |
| 4823 | return 0; |
| 4824 | } |
| 4825 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4826 | a = PyObject_GetAttrString((PyObject *)(Py_TYPE(left)), name); |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4827 | if (a == NULL) { |
| 4828 | PyErr_Clear(); |
| 4829 | Py_DECREF(b); |
| 4830 | /* If right has it but left doesn't, it's overloaded */ |
| 4831 | return 1; |
| 4832 | } |
| 4833 | |
| 4834 | ok = PyObject_RichCompareBool(a, b, Py_NE); |
| 4835 | Py_DECREF(a); |
| 4836 | Py_DECREF(b); |
| 4837 | if (ok < 0) { |
| 4838 | PyErr_Clear(); |
| 4839 | return 0; |
| 4840 | } |
| 4841 | |
| 4842 | return ok; |
| 4843 | } |
| 4844 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4845 | |
| 4846 | #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4847 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4848 | FUNCNAME(PyObject *self, PyObject *other) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4849 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4850 | static PyObject *cache_str, *rcache_str; \ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4851 | int do_other = Py_TYPE(self) != Py_TYPE(other) && \ |
| 4852 | Py_TYPE(other)->tp_as_number != NULL && \ |
| 4853 | Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \ |
| 4854 | if (Py_TYPE(self)->tp_as_number != NULL && \ |
| 4855 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4856 | PyObject *r; \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4857 | if (do_other && \ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4858 | PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \ |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4859 | method_is_overloaded(self, other, ROPSTR)) { \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4860 | r = call_maybe( \ |
| 4861 | other, ROPSTR, &rcache_str, "(O)", self); \ |
| 4862 | if (r != Py_NotImplemented) \ |
| 4863 | return r; \ |
| 4864 | Py_DECREF(r); \ |
| 4865 | do_other = 0; \ |
| 4866 | } \ |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 4867 | r = call_maybe( \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4868 | self, OPSTR, &cache_str, "(O)", other); \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4869 | if (r != Py_NotImplemented || \ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4870 | Py_TYPE(other) == Py_TYPE(self)) \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4871 | return r; \ |
| 4872 | Py_DECREF(r); \ |
| 4873 | } \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 4874 | if (do_other) { \ |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 4875 | return call_maybe( \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4876 | other, ROPSTR, &rcache_str, "(O)", self); \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4877 | } \ |
| 4878 | Py_INCREF(Py_NotImplemented); \ |
| 4879 | return Py_NotImplemented; \ |
| 4880 | } |
| 4881 | |
| 4882 | #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ |
| 4883 | SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR) |
| 4884 | |
| 4885 | #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \ |
| 4886 | static PyObject * \ |
| 4887 | FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ |
| 4888 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4889 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4890 | return call_method(self, OPSTR, &cache_str, \ |
| 4891 | "(" ARGCODES ")", arg1, arg2); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4892 | } |
| 4893 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4894 | static Py_ssize_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4895 | slot_sq_length(PyObject *self) |
| 4896 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4897 | static PyObject *len_str; |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 4898 | PyObject *res = call_method(self, "__len__", &len_str, "()"); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4899 | Py_ssize_t len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4900 | |
| 4901 | if (res == NULL) |
| 4902 | return -1; |
Neal Norwitz | 1872b1c | 2006-08-12 18:44:06 +0000 | [diff] [blame] | 4903 | len = PyInt_AsSsize_t(res); |
Guido van Rossum | 2611162 | 2001-10-01 16:42:49 +0000 | [diff] [blame] | 4904 | Py_DECREF(res); |
Jeremy Hylton | f20fcf9 | 2002-07-25 16:06:15 +0000 | [diff] [blame] | 4905 | if (len < 0) { |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 4906 | if (!PyErr_Occurred()) |
| 4907 | PyErr_SetString(PyExc_ValueError, |
| 4908 | "__len__() should return >= 0"); |
Jeremy Hylton | f20fcf9 | 2002-07-25 16:06:15 +0000 | [diff] [blame] | 4909 | return -1; |
| 4910 | } |
Guido van Rossum | 2611162 | 2001-10-01 16:42:49 +0000 | [diff] [blame] | 4911 | return len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4912 | } |
| 4913 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4914 | /* Super-optimized version of slot_sq_item. |
| 4915 | Other slots could do the same... */ |
| 4916 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4917 | slot_sq_item(PyObject *self, Py_ssize_t i) |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4918 | { |
| 4919 | static PyObject *getitem_str; |
| 4920 | PyObject *func, *args = NULL, *ival = NULL, *retval = NULL; |
| 4921 | descrgetfunc f; |
| 4922 | |
| 4923 | if (getitem_str == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4924 | getitem_str = PyString_InternFromString("__getitem__"); |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4925 | if (getitem_str == NULL) |
| 4926 | return NULL; |
| 4927 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4928 | func = _PyType_Lookup(Py_TYPE(self), getitem_str); |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4929 | if (func != NULL) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4930 | if ((f = Py_TYPE(func)->tp_descr_get) == NULL) |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4931 | Py_INCREF(func); |
Neal Norwitz | 673cd82 | 2002-10-18 16:33:13 +0000 | [diff] [blame] | 4932 | else { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4933 | func = f(func, self, (PyObject *)(Py_TYPE(self))); |
Neal Norwitz | 673cd82 | 2002-10-18 16:33:13 +0000 | [diff] [blame] | 4934 | if (func == NULL) { |
| 4935 | return NULL; |
| 4936 | } |
| 4937 | } |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 4938 | ival = PyInt_FromSsize_t(i); |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4939 | if (ival != NULL) { |
| 4940 | args = PyTuple_New(1); |
| 4941 | if (args != NULL) { |
| 4942 | PyTuple_SET_ITEM(args, 0, ival); |
| 4943 | retval = PyObject_Call(func, args, NULL); |
| 4944 | Py_XDECREF(args); |
| 4945 | Py_XDECREF(func); |
| 4946 | return retval; |
| 4947 | } |
| 4948 | } |
| 4949 | } |
| 4950 | else { |
| 4951 | PyErr_SetObject(PyExc_AttributeError, getitem_str); |
| 4952 | } |
| 4953 | Py_XDECREF(args); |
| 4954 | Py_XDECREF(ival); |
| 4955 | Py_XDECREF(func); |
| 4956 | return NULL; |
| 4957 | } |
| 4958 | |
Benjamin Peterson | 712ee92 | 2008-08-24 18:10:20 +0000 | [diff] [blame] | 4959 | static PyObject* |
| 4960 | slot_sq_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j) |
| 4961 | { |
| 4962 | static PyObject *getslice_str; |
| 4963 | |
| 4964 | if (PyErr_WarnPy3k("in 3.x, __getslice__ has been removed; " |
| 4965 | "use __getitem__", 1) < 0) |
| 4966 | return NULL; |
| 4967 | return call_method(self, "__getslice__", &getslice_str, |
| 4968 | "nn", i, j); |
| 4969 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4970 | |
| 4971 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4972 | slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4973 | { |
| 4974 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4975 | static PyObject *delitem_str, *setitem_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4976 | |
| 4977 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4978 | res = call_method(self, "__delitem__", &delitem_str, |
Thomas Wouters | 4e90810 | 2006-04-21 11:26:56 +0000 | [diff] [blame] | 4979 | "(n)", index); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4980 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4981 | res = call_method(self, "__setitem__", &setitem_str, |
Thomas Wouters | 4e90810 | 2006-04-21 11:26:56 +0000 | [diff] [blame] | 4982 | "(nO)", index, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4983 | if (res == NULL) |
| 4984 | return -1; |
| 4985 | Py_DECREF(res); |
| 4986 | return 0; |
| 4987 | } |
| 4988 | |
| 4989 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4990 | 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] | 4991 | { |
| 4992 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4993 | static PyObject *delslice_str, *setslice_str; |
Benjamin Peterson | 712ee92 | 2008-08-24 18:10:20 +0000 | [diff] [blame] | 4994 | |
| 4995 | if (value == NULL) { |
| 4996 | if (PyErr_WarnPy3k("in 3.x, __delslice__ has been removed; " |
| 4997 | "use __delitem__", 1) < 0) |
| 4998 | return -1; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4999 | res = call_method(self, "__delslice__", &delslice_str, |
Thomas Wouters | 4e90810 | 2006-04-21 11:26:56 +0000 | [diff] [blame] | 5000 | "(nn)", i, j); |
Benjamin Peterson | 712ee92 | 2008-08-24 18:10:20 +0000 | [diff] [blame] | 5001 | } |
| 5002 | else { |
| 5003 | if (PyErr_WarnPy3k("in 3.x, __setslice__ has been removed; " |
| 5004 | "use __setitem__", 1) < 0) |
| 5005 | return -1; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5006 | res = call_method(self, "__setslice__", &setslice_str, |
Benjamin Peterson | 712ee92 | 2008-08-24 18:10:20 +0000 | [diff] [blame] | 5007 | "(nnO)", i, j, value); |
| 5008 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5009 | if (res == NULL) |
| 5010 | return -1; |
| 5011 | Py_DECREF(res); |
| 5012 | return 0; |
| 5013 | } |
| 5014 | |
| 5015 | static int |
| 5016 | slot_sq_contains(PyObject *self, PyObject *value) |
| 5017 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5018 | PyObject *func, *res, *args; |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5019 | int result = -1; |
| 5020 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5021 | static PyObject *contains_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5022 | |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 5023 | func = lookup_maybe(self, "__contains__", &contains_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5024 | if (func != NULL) { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 5025 | args = PyTuple_Pack(1, value); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5026 | if (args == NULL) |
| 5027 | res = NULL; |
| 5028 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5029 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5030 | Py_DECREF(args); |
| 5031 | } |
| 5032 | Py_DECREF(func); |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5033 | if (res != NULL) { |
| 5034 | result = PyObject_IsTrue(res); |
| 5035 | Py_DECREF(res); |
| 5036 | } |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5037 | } |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5038 | else if (! PyErr_Occurred()) { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 5039 | /* Possible results: -1 and 1 */ |
| 5040 | result = (int)_PySequence_IterSearch(self, value, |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5041 | PY_ITERSEARCH_CONTAINS); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5042 | } |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5043 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5044 | } |
| 5045 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5046 | #define slot_mp_length slot_sq_length |
| 5047 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5048 | SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5049 | |
| 5050 | static int |
| 5051 | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) |
| 5052 | { |
| 5053 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5054 | static PyObject *delitem_str, *setitem_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5055 | |
| 5056 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5057 | res = call_method(self, "__delitem__", &delitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5058 | "(O)", key); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5059 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5060 | res = call_method(self, "__setitem__", &setitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5061 | "(OO)", key, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5062 | if (res == NULL) |
| 5063 | return -1; |
| 5064 | Py_DECREF(res); |
| 5065 | return 0; |
| 5066 | } |
| 5067 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5068 | SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__") |
| 5069 | SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__") |
| 5070 | SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__") |
| 5071 | SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__") |
| 5072 | SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__") |
| 5073 | SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__") |
| 5074 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 5075 | static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5076 | |
| 5077 | SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, |
| 5078 | nb_power, "__pow__", "__rpow__") |
| 5079 | |
| 5080 | static PyObject * |
| 5081 | slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) |
| 5082 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5083 | static PyObject *pow_str; |
| 5084 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5085 | if (modulus == Py_None) |
| 5086 | return slot_nb_power_binary(self, other); |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 5087 | /* Three-arg power doesn't use __rpow__. But ternary_op |
| 5088 | can call this when the second argument's type uses |
| 5089 | slot_nb_power, so check before calling self.__pow__. */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5090 | if (Py_TYPE(self)->tp_as_number != NULL && |
| 5091 | Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) { |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 5092 | return call_method(self, "__pow__", &pow_str, |
| 5093 | "(OO)", other, modulus); |
| 5094 | } |
| 5095 | Py_INCREF(Py_NotImplemented); |
| 5096 | return Py_NotImplemented; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5097 | } |
| 5098 | |
| 5099 | SLOT0(slot_nb_negative, "__neg__") |
| 5100 | SLOT0(slot_nb_positive, "__pos__") |
| 5101 | SLOT0(slot_nb_absolute, "__abs__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5102 | |
| 5103 | static int |
| 5104 | slot_nb_nonzero(PyObject *self) |
| 5105 | { |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 5106 | PyObject *func, *args; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5107 | static PyObject *nonzero_str, *len_str; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 5108 | int result = -1; |
Georg Brandl | ed4cefb | 2009-03-15 21:43:38 +0000 | [diff] [blame] | 5109 | int using_len = 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5110 | |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 5111 | func = lookup_maybe(self, "__nonzero__", &nonzero_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5112 | if (func == NULL) { |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 5113 | if (PyErr_Occurred()) |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5114 | return -1; |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 5115 | func = lookup_maybe(self, "__len__", &len_str); |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 5116 | if (func == NULL) |
| 5117 | return PyErr_Occurred() ? -1 : 1; |
Georg Brandl | ed4cefb | 2009-03-15 21:43:38 +0000 | [diff] [blame] | 5118 | using_len = 1; |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5119 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 5120 | args = PyTuple_New(0); |
| 5121 | if (args != NULL) { |
| 5122 | PyObject *temp = PyObject_Call(func, args, NULL); |
| 5123 | Py_DECREF(args); |
| 5124 | if (temp != NULL) { |
Jeremy Hylton | 3e3159c | 2003-06-27 17:38:27 +0000 | [diff] [blame] | 5125 | if (PyInt_CheckExact(temp) || PyBool_Check(temp)) |
Jeremy Hylton | 090a349 | 2003-06-27 16:46:45 +0000 | [diff] [blame] | 5126 | result = PyObject_IsTrue(temp); |
| 5127 | else { |
| 5128 | PyErr_Format(PyExc_TypeError, |
Georg Brandl | ed4cefb | 2009-03-15 21:43:38 +0000 | [diff] [blame] | 5129 | "%s should return " |
Jeremy Hylton | 090a349 | 2003-06-27 16:46:45 +0000 | [diff] [blame] | 5130 | "bool or int, returned %s", |
Georg Brandl | ed4cefb | 2009-03-15 21:43:38 +0000 | [diff] [blame] | 5131 | (using_len ? "__len__" |
| 5132 | : "__nonzero__"), |
Jeremy Hylton | 090a349 | 2003-06-27 16:46:45 +0000 | [diff] [blame] | 5133 | temp->ob_type->tp_name); |
Jeremy Hylton | 3e3159c | 2003-06-27 17:38:27 +0000 | [diff] [blame] | 5134 | result = -1; |
Jeremy Hylton | 090a349 | 2003-06-27 16:46:45 +0000 | [diff] [blame] | 5135 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 5136 | Py_DECREF(temp); |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 5137 | } |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5138 | } |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 5139 | Py_DECREF(func); |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 5140 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5141 | } |
| 5142 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5143 | |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 5144 | static PyObject * |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5145 | slot_nb_index(PyObject *self) |
| 5146 | { |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5147 | static PyObject *index_str; |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 5148 | return call_method(self, "__index__", &index_str, "()"); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5149 | } |
| 5150 | |
| 5151 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5152 | SLOT0(slot_nb_invert, "__invert__") |
| 5153 | SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") |
| 5154 | SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") |
| 5155 | SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") |
| 5156 | SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") |
| 5157 | SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5158 | |
| 5159 | static int |
| 5160 | slot_nb_coerce(PyObject **a, PyObject **b) |
| 5161 | { |
| 5162 | static PyObject *coerce_str; |
| 5163 | PyObject *self = *a, *other = *b; |
| 5164 | |
| 5165 | if (self->ob_type->tp_as_number != NULL && |
| 5166 | self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) { |
| 5167 | PyObject *r; |
| 5168 | r = call_maybe( |
| 5169 | self, "__coerce__", &coerce_str, "(O)", other); |
| 5170 | if (r == NULL) |
| 5171 | return -1; |
| 5172 | if (r == Py_NotImplemented) { |
| 5173 | Py_DECREF(r); |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5174 | } |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 5175 | else { |
| 5176 | if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) { |
| 5177 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5178 | "__coerce__ didn't return a 2-tuple"); |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 5179 | Py_DECREF(r); |
| 5180 | return -1; |
| 5181 | } |
| 5182 | *a = PyTuple_GET_ITEM(r, 0); |
| 5183 | Py_INCREF(*a); |
| 5184 | *b = PyTuple_GET_ITEM(r, 1); |
| 5185 | Py_INCREF(*b); |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5186 | Py_DECREF(r); |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 5187 | return 0; |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5188 | } |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5189 | } |
| 5190 | if (other->ob_type->tp_as_number != NULL && |
| 5191 | other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) { |
| 5192 | PyObject *r; |
| 5193 | r = call_maybe( |
| 5194 | other, "__coerce__", &coerce_str, "(O)", self); |
| 5195 | if (r == NULL) |
| 5196 | return -1; |
| 5197 | if (r == Py_NotImplemented) { |
| 5198 | Py_DECREF(r); |
| 5199 | return 1; |
| 5200 | } |
| 5201 | if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) { |
| 5202 | PyErr_SetString(PyExc_TypeError, |
| 5203 | "__coerce__ didn't return a 2-tuple"); |
| 5204 | Py_DECREF(r); |
| 5205 | return -1; |
| 5206 | } |
| 5207 | *a = PyTuple_GET_ITEM(r, 1); |
| 5208 | Py_INCREF(*a); |
| 5209 | *b = PyTuple_GET_ITEM(r, 0); |
| 5210 | Py_INCREF(*b); |
| 5211 | Py_DECREF(r); |
| 5212 | return 0; |
| 5213 | } |
| 5214 | return 1; |
| 5215 | } |
| 5216 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5217 | SLOT0(slot_nb_int, "__int__") |
| 5218 | SLOT0(slot_nb_long, "__long__") |
| 5219 | SLOT0(slot_nb_float, "__float__") |
| 5220 | SLOT0(slot_nb_oct, "__oct__") |
| 5221 | SLOT0(slot_nb_hex, "__hex__") |
| 5222 | SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O") |
| 5223 | SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O") |
| 5224 | SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O") |
| 5225 | SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O") |
| 5226 | SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O") |
Martin v. Löwis | fd96326 | 2007-02-09 12:19:32 +0000 | [diff] [blame] | 5227 | /* Can't use SLOT1 here, because nb_inplace_power is ternary */ |
| 5228 | static PyObject * |
| 5229 | slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2) |
| 5230 | { |
| 5231 | static PyObject *cache_str; |
| 5232 | return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1); |
| 5233 | } |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5234 | SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O") |
| 5235 | SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O") |
| 5236 | SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O") |
| 5237 | SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O") |
| 5238 | SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O") |
| 5239 | SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, |
| 5240 | "__floordiv__", "__rfloordiv__") |
| 5241 | SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__") |
| 5242 | SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O") |
| 5243 | SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5244 | |
| 5245 | static int |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5246 | half_compare(PyObject *self, PyObject *other) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5247 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5248 | PyObject *func, *args, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5249 | static PyObject *cmp_str; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5250 | Py_ssize_t c; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5251 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5252 | func = lookup_method(self, "__cmp__", &cmp_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5253 | if (func == NULL) { |
| 5254 | PyErr_Clear(); |
| 5255 | } |
| 5256 | else { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 5257 | args = PyTuple_Pack(1, other); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5258 | if (args == NULL) |
| 5259 | res = NULL; |
| 5260 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5261 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5262 | Py_DECREF(args); |
| 5263 | } |
Raymond Hettinger | ab5dae3 | 2002-06-24 13:08:16 +0000 | [diff] [blame] | 5264 | Py_DECREF(func); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5265 | if (res != Py_NotImplemented) { |
| 5266 | if (res == NULL) |
| 5267 | return -2; |
| 5268 | c = PyInt_AsLong(res); |
| 5269 | Py_DECREF(res); |
| 5270 | if (c == -1 && PyErr_Occurred()) |
| 5271 | return -2; |
| 5272 | return (c < 0) ? -1 : (c > 0) ? 1 : 0; |
| 5273 | } |
| 5274 | Py_DECREF(res); |
| 5275 | } |
| 5276 | return 2; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5277 | } |
| 5278 | |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 5279 | /* This slot is published for the benefit of try_3way_compare in object.c */ |
| 5280 | int |
| 5281 | _PyObject_SlotCompare(PyObject *self, PyObject *other) |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5282 | { |
| 5283 | int c; |
| 5284 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5285 | if (Py_TYPE(self)->tp_compare == _PyObject_SlotCompare) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5286 | c = half_compare(self, other); |
| 5287 | if (c <= 1) |
| 5288 | return c; |
| 5289 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5290 | if (Py_TYPE(other)->tp_compare == _PyObject_SlotCompare) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5291 | c = half_compare(other, self); |
| 5292 | if (c < -1) |
| 5293 | return -2; |
| 5294 | if (c <= 1) |
| 5295 | return -c; |
| 5296 | } |
| 5297 | return (void *)self < (void *)other ? -1 : |
| 5298 | (void *)self > (void *)other ? 1 : 0; |
| 5299 | } |
| 5300 | |
| 5301 | static PyObject * |
| 5302 | slot_tp_repr(PyObject *self) |
| 5303 | { |
| 5304 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5305 | static PyObject *repr_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5306 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5307 | func = lookup_method(self, "__repr__", &repr_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5308 | if (func != NULL) { |
| 5309 | res = PyEval_CallObject(func, NULL); |
| 5310 | Py_DECREF(func); |
| 5311 | return res; |
| 5312 | } |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 5313 | PyErr_Clear(); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 5314 | return PyString_FromFormat("<%s object at %p>", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5315 | Py_TYPE(self)->tp_name, self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5316 | } |
| 5317 | |
| 5318 | static PyObject * |
| 5319 | slot_tp_str(PyObject *self) |
| 5320 | { |
| 5321 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5322 | static PyObject *str_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5323 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5324 | func = lookup_method(self, "__str__", &str_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5325 | if (func != NULL) { |
| 5326 | res = PyEval_CallObject(func, NULL); |
| 5327 | Py_DECREF(func); |
| 5328 | return res; |
| 5329 | } |
| 5330 | else { |
| 5331 | PyErr_Clear(); |
| 5332 | return slot_tp_repr(self); |
| 5333 | } |
| 5334 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5335 | |
| 5336 | static long |
| 5337 | slot_tp_hash(PyObject *self) |
| 5338 | { |
Tim Peters | 61ce0a9 | 2002-12-06 23:38:02 +0000 | [diff] [blame] | 5339 | PyObject *func; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5340 | static PyObject *hash_str, *eq_str, *cmp_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5341 | long h; |
| 5342 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5343 | func = lookup_method(self, "__hash__", &hash_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5344 | |
Guido van Rossum | 0b7b6fd | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 5345 | if (func != NULL && func != Py_None) { |
Tim Peters | 61ce0a9 | 2002-12-06 23:38:02 +0000 | [diff] [blame] | 5346 | PyObject *res = PyEval_CallObject(func, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5347 | Py_DECREF(func); |
| 5348 | if (res == NULL) |
| 5349 | return -1; |
Martin v. Löwis | ab2f8f7 | 2006-08-09 07:57:39 +0000 | [diff] [blame] | 5350 | if (PyLong_Check(res)) |
Armin Rigo | 51fc8c4 | 2006-08-09 14:55:26 +0000 | [diff] [blame] | 5351 | h = PyLong_Type.tp_hash(res); |
Martin v. Löwis | ab2f8f7 | 2006-08-09 07:57:39 +0000 | [diff] [blame] | 5352 | else |
| 5353 | h = PyInt_AsLong(res); |
Tim Peters | 61ce0a9 | 2002-12-06 23:38:02 +0000 | [diff] [blame] | 5354 | Py_DECREF(res); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5355 | } |
| 5356 | else { |
Georg Brandl | 30b7804 | 2007-12-20 21:03:02 +0000 | [diff] [blame] | 5357 | Py_XDECREF(func); /* may be None */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5358 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5359 | func = lookup_method(self, "__eq__", &eq_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5360 | if (func == NULL) { |
| 5361 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5362 | func = lookup_method(self, "__cmp__", &cmp_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5363 | } |
| 5364 | if (func != NULL) { |
| 5365 | Py_DECREF(func); |
Nick Coghlan | 53663a6 | 2008-07-15 14:27:37 +0000 | [diff] [blame] | 5366 | return PyObject_HashNotImplemented(self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5367 | } |
| 5368 | PyErr_Clear(); |
| 5369 | h = _Py_HashPointer((void *)self); |
| 5370 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5371 | if (h == -1 && !PyErr_Occurred()) |
| 5372 | h = -2; |
| 5373 | return h; |
| 5374 | } |
| 5375 | |
| 5376 | static PyObject * |
| 5377 | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 5378 | { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5379 | static PyObject *call_str; |
| 5380 | PyObject *meth = lookup_method(self, "__call__", &call_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5381 | PyObject *res; |
| 5382 | |
| 5383 | if (meth == NULL) |
| 5384 | return NULL; |
Armin Rigo | 53c1692f | 2006-06-21 21:58:50 +0000 | [diff] [blame] | 5385 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5386 | res = PyObject_Call(meth, args, kwds); |
Armin Rigo | 53c1692f | 2006-06-21 21:58:50 +0000 | [diff] [blame] | 5387 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5388 | Py_DECREF(meth); |
| 5389 | return res; |
| 5390 | } |
| 5391 | |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5392 | /* There are two slot dispatch functions for tp_getattro. |
| 5393 | |
| 5394 | - slot_tp_getattro() is used when __getattribute__ is overridden |
| 5395 | but no __getattr__ hook is present; |
| 5396 | |
| 5397 | - slot_tp_getattr_hook() is used when a __getattr__ hook is present. |
| 5398 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5399 | The code in update_one_slot() always installs slot_tp_getattr_hook(); this |
| 5400 | detects the absence of __getattr__ and then installs the simpler slot if |
| 5401 | necessary. */ |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5402 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5403 | static PyObject * |
| 5404 | slot_tp_getattro(PyObject *self, PyObject *name) |
| 5405 | { |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5406 | static PyObject *getattribute_str = NULL; |
| 5407 | return call_method(self, "__getattribute__", &getattribute_str, |
| 5408 | "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5409 | } |
| 5410 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5411 | static PyObject * |
Benjamin Peterson | 273c233 | 2008-11-17 22:39:09 +0000 | [diff] [blame] | 5412 | call_attribute(PyObject *self, PyObject *attr, PyObject *name) |
| 5413 | { |
| 5414 | PyObject *res, *descr = NULL; |
| 5415 | descrgetfunc f = Py_TYPE(attr)->tp_descr_get; |
| 5416 | |
| 5417 | if (f != NULL) { |
| 5418 | descr = f(attr, self, (PyObject *)(Py_TYPE(self))); |
| 5419 | if (descr == NULL) |
| 5420 | return NULL; |
| 5421 | else |
| 5422 | attr = descr; |
| 5423 | } |
| 5424 | res = PyObject_CallFunctionObjArgs(attr, name, NULL); |
| 5425 | Py_XDECREF(descr); |
| 5426 | return res; |
| 5427 | } |
| 5428 | |
| 5429 | static PyObject * |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5430 | slot_tp_getattr_hook(PyObject *self, PyObject *name) |
| 5431 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5432 | PyTypeObject *tp = Py_TYPE(self); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5433 | PyObject *getattr, *getattribute, *res; |
| 5434 | static PyObject *getattribute_str = NULL; |
| 5435 | static PyObject *getattr_str = NULL; |
| 5436 | |
| 5437 | if (getattr_str == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 5438 | getattr_str = PyString_InternFromString("__getattr__"); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5439 | if (getattr_str == NULL) |
| 5440 | return NULL; |
| 5441 | } |
| 5442 | if (getattribute_str == NULL) { |
| 5443 | getattribute_str = |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 5444 | PyString_InternFromString("__getattribute__"); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5445 | if (getattribute_str == NULL) |
| 5446 | return NULL; |
| 5447 | } |
Benjamin Peterson | 273c233 | 2008-11-17 22:39:09 +0000 | [diff] [blame] | 5448 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5449 | method fully for each attribute lookup for classes with |
| 5450 | __getattr__, even when the attribute is present. So we use |
| 5451 | _PyType_Lookup and create the method only when needed, with |
| 5452 | call_attribute. */ |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5453 | getattr = _PyType_Lookup(tp, getattr_str); |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5454 | if (getattr == NULL) { |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5455 | /* No __getattr__ hook: use a simpler dispatcher */ |
| 5456 | tp->tp_getattro = slot_tp_getattro; |
| 5457 | return slot_tp_getattro(self, name); |
| 5458 | } |
Benjamin Peterson | 273c233 | 2008-11-17 22:39:09 +0000 | [diff] [blame] | 5459 | Py_INCREF(getattr); |
| 5460 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5461 | method fully for each attribute lookup for classes with |
| 5462 | __getattr__, even when self has the default __getattribute__ |
| 5463 | method. So we use _PyType_Lookup and create the method only when |
| 5464 | needed, with call_attribute. */ |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5465 | getattribute = _PyType_Lookup(tp, getattribute_str); |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5466 | if (getattribute == NULL || |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5467 | (Py_TYPE(getattribute) == &PyWrapperDescr_Type && |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5468 | ((PyWrapperDescrObject *)getattribute)->d_wrapped == |
| 5469 | (void *)PyObject_GenericGetAttr)) |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5470 | res = PyObject_GenericGetAttr(self, name); |
Benjamin Peterson | 273c233 | 2008-11-17 22:39:09 +0000 | [diff] [blame] | 5471 | else { |
| 5472 | Py_INCREF(getattribute); |
| 5473 | res = call_attribute(self, getattribute, name); |
| 5474 | Py_DECREF(getattribute); |
| 5475 | } |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5476 | if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5477 | PyErr_Clear(); |
Benjamin Peterson | 273c233 | 2008-11-17 22:39:09 +0000 | [diff] [blame] | 5478 | res = call_attribute(self, getattr, name); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5479 | } |
Benjamin Peterson | 273c233 | 2008-11-17 22:39:09 +0000 | [diff] [blame] | 5480 | Py_DECREF(getattr); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5481 | return res; |
| 5482 | } |
| 5483 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5484 | static int |
| 5485 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) |
| 5486 | { |
| 5487 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5488 | static PyObject *delattr_str, *setattr_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5489 | |
| 5490 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5491 | res = call_method(self, "__delattr__", &delattr_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5492 | "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5493 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5494 | res = call_method(self, "__setattr__", &setattr_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5495 | "(OO)", name, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5496 | if (res == NULL) |
| 5497 | return -1; |
| 5498 | Py_DECREF(res); |
| 5499 | return 0; |
| 5500 | } |
| 5501 | |
Guido van Rossum | 0b7b6fd | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 5502 | static char *name_op[] = { |
| 5503 | "__lt__", |
| 5504 | "__le__", |
| 5505 | "__eq__", |
| 5506 | "__ne__", |
| 5507 | "__gt__", |
| 5508 | "__ge__", |
| 5509 | }; |
| 5510 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5511 | static PyObject * |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5512 | half_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5513 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5514 | PyObject *func, *args, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5515 | static PyObject *op_str[6]; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5516 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5517 | func = lookup_method(self, name_op[op], &op_str[op]); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5518 | if (func == NULL) { |
| 5519 | PyErr_Clear(); |
| 5520 | Py_INCREF(Py_NotImplemented); |
| 5521 | return Py_NotImplemented; |
| 5522 | } |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 5523 | args = PyTuple_Pack(1, other); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5524 | if (args == NULL) |
| 5525 | res = NULL; |
| 5526 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5527 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5528 | Py_DECREF(args); |
| 5529 | } |
| 5530 | Py_DECREF(func); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5531 | return res; |
| 5532 | } |
| 5533 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5534 | static PyObject * |
| 5535 | slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
| 5536 | { |
| 5537 | PyObject *res; |
| 5538 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5539 | if (Py_TYPE(self)->tp_richcompare == slot_tp_richcompare) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5540 | res = half_richcompare(self, other, op); |
| 5541 | if (res != Py_NotImplemented) |
| 5542 | return res; |
| 5543 | Py_DECREF(res); |
| 5544 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5545 | if (Py_TYPE(other)->tp_richcompare == slot_tp_richcompare) { |
Tim Peters | f4aca75 | 2004-09-23 02:39:37 +0000 | [diff] [blame] | 5546 | res = half_richcompare(other, self, _Py_SwappedOp[op]); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5547 | if (res != Py_NotImplemented) { |
| 5548 | return res; |
| 5549 | } |
| 5550 | Py_DECREF(res); |
| 5551 | } |
| 5552 | Py_INCREF(Py_NotImplemented); |
| 5553 | return Py_NotImplemented; |
| 5554 | } |
| 5555 | |
| 5556 | static PyObject * |
| 5557 | slot_tp_iter(PyObject *self) |
| 5558 | { |
| 5559 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5560 | static PyObject *iter_str, *getitem_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5561 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5562 | func = lookup_method(self, "__iter__", &iter_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5563 | if (func != NULL) { |
Guido van Rossum | 84b2bed | 2002-08-16 17:01:09 +0000 | [diff] [blame] | 5564 | PyObject *args; |
| 5565 | args = res = PyTuple_New(0); |
| 5566 | if (args != NULL) { |
| 5567 | res = PyObject_Call(func, args, NULL); |
| 5568 | Py_DECREF(args); |
| 5569 | } |
| 5570 | Py_DECREF(func); |
| 5571 | return res; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5572 | } |
| 5573 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5574 | func = lookup_method(self, "__getitem__", &getitem_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5575 | if (func == NULL) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 5576 | PyErr_Format(PyExc_TypeError, |
| 5577 | "'%.200s' object is not iterable", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5578 | Py_TYPE(self)->tp_name); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5579 | return NULL; |
| 5580 | } |
| 5581 | Py_DECREF(func); |
| 5582 | return PySeqIter_New(self); |
| 5583 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5584 | |
| 5585 | static PyObject * |
| 5586 | slot_tp_iternext(PyObject *self) |
| 5587 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5588 | static PyObject *next_str; |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5589 | return call_method(self, "next", &next_str, "()"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5590 | } |
| 5591 | |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5592 | static PyObject * |
| 5593 | slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 5594 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5595 | PyTypeObject *tp = Py_TYPE(self); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5596 | PyObject *get; |
| 5597 | static PyObject *get_str = NULL; |
| 5598 | |
| 5599 | if (get_str == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 5600 | get_str = PyString_InternFromString("__get__"); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5601 | if (get_str == NULL) |
| 5602 | return NULL; |
| 5603 | } |
| 5604 | get = _PyType_Lookup(tp, get_str); |
| 5605 | if (get == NULL) { |
| 5606 | /* Avoid further slowdowns */ |
| 5607 | if (tp->tp_descr_get == slot_tp_descr_get) |
| 5608 | tp->tp_descr_get = NULL; |
| 5609 | Py_INCREF(self); |
| 5610 | return self; |
| 5611 | } |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5612 | if (obj == NULL) |
| 5613 | obj = Py_None; |
| 5614 | if (type == NULL) |
| 5615 | type = Py_None; |
Georg Brandl | 684fd0c | 2006-05-25 19:15:31 +0000 | [diff] [blame] | 5616 | return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5617 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5618 | |
| 5619 | static int |
| 5620 | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) |
| 5621 | { |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5622 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5623 | static PyObject *del_str, *set_str; |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5624 | |
| 5625 | if (value == NULL) |
Guido van Rossum | 1d5b3f2 | 2001-12-03 00:08:33 +0000 | [diff] [blame] | 5626 | res = call_method(self, "__delete__", &del_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5627 | "(O)", target); |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5628 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5629 | res = call_method(self, "__set__", &set_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 5630 | "(OO)", target, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5631 | if (res == NULL) |
| 5632 | return -1; |
| 5633 | Py_DECREF(res); |
| 5634 | return 0; |
| 5635 | } |
| 5636 | |
| 5637 | static int |
| 5638 | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 5639 | { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 5640 | static PyObject *init_str; |
| 5641 | PyObject *meth = lookup_method(self, "__init__", &init_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5642 | PyObject *res; |
| 5643 | |
| 5644 | if (meth == NULL) |
| 5645 | return -1; |
| 5646 | res = PyObject_Call(meth, args, kwds); |
| 5647 | Py_DECREF(meth); |
| 5648 | if (res == NULL) |
| 5649 | return -1; |
Raymond Hettinger | b67cc80 | 2005-03-03 16:45:19 +0000 | [diff] [blame] | 5650 | if (res != Py_None) { |
Georg Brandl | ccff785 | 2006-06-18 22:17:29 +0000 | [diff] [blame] | 5651 | PyErr_Format(PyExc_TypeError, |
| 5652 | "__init__() should return None, not '%.200s'", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5653 | Py_TYPE(res)->tp_name); |
Raymond Hettinger | b67cc80 | 2005-03-03 16:45:19 +0000 | [diff] [blame] | 5654 | Py_DECREF(res); |
| 5655 | return -1; |
| 5656 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5657 | Py_DECREF(res); |
| 5658 | return 0; |
| 5659 | } |
| 5660 | |
| 5661 | static PyObject * |
| 5662 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 5663 | { |
Guido van Rossum | 7bed213 | 2002-08-08 21:57:53 +0000 | [diff] [blame] | 5664 | static PyObject *new_str; |
| 5665 | PyObject *func; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5666 | PyObject *newargs, *x; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5667 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5668 | |
Guido van Rossum | 7bed213 | 2002-08-08 21:57:53 +0000 | [diff] [blame] | 5669 | if (new_str == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 5670 | new_str = PyString_InternFromString("__new__"); |
Guido van Rossum | 7bed213 | 2002-08-08 21:57:53 +0000 | [diff] [blame] | 5671 | if (new_str == NULL) |
| 5672 | return NULL; |
| 5673 | } |
| 5674 | func = PyObject_GetAttr((PyObject *)type, new_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5675 | if (func == NULL) |
| 5676 | return NULL; |
| 5677 | assert(PyTuple_Check(args)); |
| 5678 | n = PyTuple_GET_SIZE(args); |
| 5679 | newargs = PyTuple_New(n+1); |
| 5680 | if (newargs == NULL) |
| 5681 | return NULL; |
| 5682 | Py_INCREF(type); |
| 5683 | PyTuple_SET_ITEM(newargs, 0, (PyObject *)type); |
| 5684 | for (i = 0; i < n; i++) { |
| 5685 | x = PyTuple_GET_ITEM(args, i); |
| 5686 | Py_INCREF(x); |
| 5687 | PyTuple_SET_ITEM(newargs, i+1, x); |
| 5688 | } |
| 5689 | x = PyObject_Call(func, newargs, kwds); |
Guido van Rossum | 25d1807 | 2001-10-01 15:55:28 +0000 | [diff] [blame] | 5690 | Py_DECREF(newargs); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5691 | Py_DECREF(func); |
| 5692 | return x; |
| 5693 | } |
| 5694 | |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5695 | static void |
| 5696 | slot_tp_del(PyObject *self) |
| 5697 | { |
| 5698 | static PyObject *del_str = NULL; |
| 5699 | PyObject *del, *res; |
| 5700 | PyObject *error_type, *error_value, *error_traceback; |
| 5701 | |
| 5702 | /* Temporarily resurrect the object. */ |
| 5703 | assert(self->ob_refcnt == 0); |
| 5704 | self->ob_refcnt = 1; |
| 5705 | |
| 5706 | /* Save the current exception, if any. */ |
| 5707 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 5708 | |
| 5709 | /* Execute __del__ method, if any. */ |
| 5710 | del = lookup_maybe(self, "__del__", &del_str); |
| 5711 | if (del != NULL) { |
| 5712 | res = PyEval_CallObject(del, NULL); |
| 5713 | if (res == NULL) |
| 5714 | PyErr_WriteUnraisable(del); |
| 5715 | else |
| 5716 | Py_DECREF(res); |
| 5717 | Py_DECREF(del); |
| 5718 | } |
| 5719 | |
| 5720 | /* Restore the saved exception. */ |
| 5721 | PyErr_Restore(error_type, error_value, error_traceback); |
| 5722 | |
| 5723 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 5724 | * cause a recursive call. |
| 5725 | */ |
| 5726 | assert(self->ob_refcnt > 0); |
| 5727 | if (--self->ob_refcnt == 0) |
| 5728 | return; /* this is the normal path out */ |
| 5729 | |
| 5730 | /* __del__ resurrected it! Make it look like the original Py_DECREF |
| 5731 | * never happened. |
| 5732 | */ |
| 5733 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 5734 | Py_ssize_t refcnt = self->ob_refcnt; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5735 | _Py_NewReference(self); |
| 5736 | self->ob_refcnt = refcnt; |
| 5737 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5738 | assert(!PyType_IS_GC(Py_TYPE(self)) || |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5739 | _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED); |
Michael W. Hudson | 3f3b668 | 2004-08-03 10:21:03 +0000 | [diff] [blame] | 5740 | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so |
| 5741 | * we need to undo that. */ |
| 5742 | _Py_DEC_REFTOTAL; |
| 5743 | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object |
| 5744 | * chain, so no more to do there. |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5745 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
| 5746 | * _Py_NewReference bumped tp_allocs: both of those need to be |
| 5747 | * undone. |
| 5748 | */ |
| 5749 | #ifdef COUNT_ALLOCS |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5750 | --Py_TYPE(self)->tp_frees; |
| 5751 | --Py_TYPE(self)->tp_allocs; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5752 | #endif |
| 5753 | } |
| 5754 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5755 | |
| 5756 | /* 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] | 5757 | functions. The offsets here are relative to the 'PyHeapTypeObject' |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5758 | structure, which incorporates the additional structures used for numbers, |
| 5759 | sequences and mappings. |
| 5760 | 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] | 5761 | __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] | 5762 | slots (e.g. __str__ affects tp_str as well as tp_repr). The table is |
| 5763 | terminated with an all-zero entry. (This table is further initialized and |
| 5764 | sorted in init_slotdefs() below.) */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5765 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5766 | typedef struct wrapperbase slotdef; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5767 | |
| 5768 | #undef TPSLOT |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5769 | #undef FLSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5770 | #undef ETSLOT |
| 5771 | #undef SQSLOT |
| 5772 | #undef MPSLOT |
| 5773 | #undef NBSLOT |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5774 | #undef UNSLOT |
| 5775 | #undef IBSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5776 | #undef BINSLOT |
| 5777 | #undef RBINSLOT |
| 5778 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5779 | #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Neal Norwitz | d47714a | 2002-08-13 19:01:38 +0000 | [diff] [blame] | 5780 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5781 | PyDoc_STR(DOC)} |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5782 | #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ |
| 5783 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
Neal Norwitz | d47714a | 2002-08-13 19:01:38 +0000 | [diff] [blame] | 5784 | PyDoc_STR(DOC), FLAGS} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5785 | #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5786 | {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
Neal Norwitz | d47714a | 2002-08-13 19:01:38 +0000 | [diff] [blame] | 5787 | PyDoc_STR(DOC)} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5788 | #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5789 | ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) |
| 5790 | #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5791 | ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) |
| 5792 | #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5793 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) |
| 5794 | #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5795 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5796 | "x." NAME "() <==> " DOC) |
| 5797 | #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 5798 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5799 | "x." NAME "(y) <==> x" DOC "y") |
| 5800 | #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
| 5801 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5802 | "x." NAME "(y) <==> x" DOC "y") |
| 5803 | #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
| 5804 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5805 | "x." NAME "(y) <==> y" DOC "x") |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5806 | #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
| 5807 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5808 | "x." NAME "(y) <==> " DOC) |
| 5809 | #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
| 5810 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5811 | "x." NAME "(y) <==> " DOC) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5812 | |
| 5813 | static slotdef slotdefs[] = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5814 | SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5815 | "x.__len__() <==> len(x)"), |
Armin Rigo | fd163f9 | 2005-12-29 15:59:19 +0000 | [diff] [blame] | 5816 | /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL. |
| 5817 | The logic in abstract.c always falls back to nb_add/nb_multiply in |
| 5818 | this case. Defining both the nb_* and the sq_* slots to call the |
| 5819 | user-defined methods has unexpected side-effects, as shown by |
| 5820 | test_descr.notimplemented() */ |
| 5821 | SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc, |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5822 | "x.__add__(y) <==> x+y"), |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 5823 | SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc, |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5824 | "x.__mul__(n) <==> x*n"), |
Armin Rigo | 314861c | 2006-03-30 14:04:02 +0000 | [diff] [blame] | 5825 | SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc, |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5826 | "x.__rmul__(n) <==> n*x"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5827 | SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, |
| 5828 | "x.__getitem__(y) <==> x[y]"), |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5829 | SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_ssizessizeargfunc, |
Brett Cannon | 154da9b | 2003-05-20 02:30:04 +0000 | [diff] [blame] | 5830 | "x.__getslice__(i, j) <==> x[i:j]\n\ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5831 | \n\ |
| 5832 | Use of negative indices is not supported."), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5833 | SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, |
Brett Cannon | be67d87 | 2003-05-20 02:40:12 +0000 | [diff] [blame] | 5834 | "x.__setitem__(i, y) <==> x[i]=y"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5835 | SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, |
Brett Cannon | be67d87 | 2003-05-20 02:40:12 +0000 | [diff] [blame] | 5836 | "x.__delitem__(y) <==> del x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5837 | SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5838 | wrap_ssizessizeobjargproc, |
Brett Cannon | be67d87 | 2003-05-20 02:40:12 +0000 | [diff] [blame] | 5839 | "x.__setslice__(i, j, y) <==> x[i:j]=y\n\ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5840 | \n\ |
| 5841 | Use of negative indices is not supported."), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5842 | SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice, |
Brett Cannon | be67d87 | 2003-05-20 02:40:12 +0000 | [diff] [blame] | 5843 | "x.__delslice__(i, j) <==> del x[i:j]\n\ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5844 | \n\ |
| 5845 | Use of negative indices is not supported."), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5846 | SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, |
| 5847 | "x.__contains__(y) <==> y in x"), |
Armin Rigo | fd163f9 | 2005-12-29 15:59:19 +0000 | [diff] [blame] | 5848 | SQSLOT("__iadd__", sq_inplace_concat, NULL, |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5849 | wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"), |
Armin Rigo | fd163f9 | 2005-12-29 15:59:19 +0000 | [diff] [blame] | 5850 | SQSLOT("__imul__", sq_inplace_repeat, NULL, |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 5851 | wrap_indexargfunc, "x.__imul__(y) <==> x*=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5852 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5853 | MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5854 | "x.__len__() <==> len(x)"), |
Guido van Rossum | fd38f8e | 2001-10-09 20:17:57 +0000 | [diff] [blame] | 5855 | MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5856 | wrap_binaryfunc, |
| 5857 | "x.__getitem__(y) <==> x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5858 | MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5859 | wrap_objobjargproc, |
| 5860 | "x.__setitem__(i, y) <==> x[i]=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5861 | MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5862 | wrap_delitem, |
| 5863 | "x.__delitem__(y) <==> del x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5864 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5865 | BINSLOT("__add__", nb_add, slot_nb_add, |
| 5866 | "+"), |
| 5867 | RBINSLOT("__radd__", nb_add, slot_nb_add, |
| 5868 | "+"), |
| 5869 | BINSLOT("__sub__", nb_subtract, slot_nb_subtract, |
| 5870 | "-"), |
| 5871 | RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract, |
| 5872 | "-"), |
| 5873 | BINSLOT("__mul__", nb_multiply, slot_nb_multiply, |
| 5874 | "*"), |
| 5875 | RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply, |
| 5876 | "*"), |
| 5877 | BINSLOT("__div__", nb_divide, slot_nb_divide, |
| 5878 | "/"), |
| 5879 | RBINSLOT("__rdiv__", nb_divide, slot_nb_divide, |
| 5880 | "/"), |
| 5881 | BINSLOT("__mod__", nb_remainder, slot_nb_remainder, |
| 5882 | "%"), |
| 5883 | RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder, |
| 5884 | "%"), |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5885 | BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5886 | "divmod(x, y)"), |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5887 | RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5888 | "divmod(y, x)"), |
| 5889 | NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, |
| 5890 | "x.__pow__(y[, z]) <==> pow(x, y[, z])"), |
| 5891 | NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, |
| 5892 | "y.__rpow__(x[, z]) <==> pow(x, y[, z])"), |
| 5893 | UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"), |
| 5894 | UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"), |
| 5895 | UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, |
| 5896 | "abs(x)"), |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 5897 | UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquirypred, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5898 | "x != 0"), |
| 5899 | UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"), |
| 5900 | BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5901 | RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5902 | BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5903 | RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5904 | BINSLOT("__and__", nb_and, slot_nb_and, "&"), |
| 5905 | RBINSLOT("__rand__", nb_and, slot_nb_and, "&"), |
| 5906 | BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"), |
| 5907 | RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), |
| 5908 | BINSLOT("__or__", nb_or, slot_nb_or, "|"), |
| 5909 | RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), |
| 5910 | NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc, |
| 5911 | "x.__coerce__(y) <==> coerce(x, y)"), |
| 5912 | UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, |
| 5913 | "int(x)"), |
| 5914 | UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc, |
| 5915 | "long(x)"), |
| 5916 | UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, |
| 5917 | "float(x)"), |
| 5918 | UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc, |
| 5919 | "oct(x)"), |
| 5920 | UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc, |
| 5921 | "hex(x)"), |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 5922 | NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc, |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5923 | "x[y:z] <==> x[y.__index__():z.__index__()]"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5924 | IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, |
| 5925 | wrap_binaryfunc, "+"), |
| 5926 | IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, |
| 5927 | wrap_binaryfunc, "-"), |
| 5928 | IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply, |
| 5929 | wrap_binaryfunc, "*"), |
| 5930 | IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide, |
| 5931 | wrap_binaryfunc, "/"), |
| 5932 | IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder, |
| 5933 | wrap_binaryfunc, "%"), |
| 5934 | IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power, |
Guido van Rossum | 6e5680f | 2002-10-15 01:01:53 +0000 | [diff] [blame] | 5935 | wrap_binaryfunc, "**"), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5936 | IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift, |
| 5937 | wrap_binaryfunc, "<<"), |
| 5938 | IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift, |
| 5939 | wrap_binaryfunc, ">>"), |
| 5940 | IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and, |
| 5941 | wrap_binaryfunc, "&"), |
| 5942 | IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor, |
| 5943 | wrap_binaryfunc, "^"), |
| 5944 | IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or, |
| 5945 | wrap_binaryfunc, "|"), |
| 5946 | BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5947 | RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5948 | BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5949 | RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5950 | IBSLOT("__ifloordiv__", nb_inplace_floor_divide, |
| 5951 | slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"), |
| 5952 | IBSLOT("__itruediv__", nb_inplace_true_divide, |
| 5953 | slot_nb_inplace_true_divide, wrap_binaryfunc, "/"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5954 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5955 | TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, |
| 5956 | "x.__str__() <==> str(x)"), |
Guido van Rossum | afe7a94 | 2001-10-29 14:33:44 +0000 | [diff] [blame] | 5957 | TPSLOT("__str__", tp_print, NULL, NULL, ""), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5958 | TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, |
| 5959 | "x.__repr__() <==> repr(x)"), |
Guido van Rossum | afe7a94 | 2001-10-29 14:33:44 +0000 | [diff] [blame] | 5960 | TPSLOT("__repr__", tp_print, NULL, NULL, ""), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5961 | TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc, |
| 5962 | "x.__cmp__(y) <==> cmp(x,y)"), |
| 5963 | TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, |
| 5964 | "x.__hash__() <==> hash(x)"), |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5965 | FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, |
| 5966 | "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS), |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5967 | TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5968 | wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"), |
| 5969 | TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), |
| 5970 | TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), |
| 5971 | TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), |
| 5972 | TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, |
| 5973 | "x.__setattr__('name', value) <==> x.name = value"), |
| 5974 | TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), |
| 5975 | TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, |
| 5976 | "x.__delattr__('name') <==> del x.name"), |
| 5977 | TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), |
| 5978 | TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, |
| 5979 | "x.__lt__(y) <==> x<y"), |
| 5980 | TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, |
| 5981 | "x.__le__(y) <==> x<=y"), |
| 5982 | TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, |
| 5983 | "x.__eq__(y) <==> x==y"), |
| 5984 | TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, |
| 5985 | "x.__ne__(y) <==> x!=y"), |
| 5986 | TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, |
| 5987 | "x.__gt__(y) <==> x>y"), |
| 5988 | TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, |
| 5989 | "x.__ge__(y) <==> x>=y"), |
| 5990 | TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, |
| 5991 | "x.__iter__() <==> iter(x)"), |
| 5992 | TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next, |
| 5993 | "x.next() -> the next value, or raise StopIteration"), |
| 5994 | TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, |
| 5995 | "descr.__get__(obj[, type]) -> value"), |
| 5996 | TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, |
| 5997 | "descr.__set__(obj, value)"), |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 5998 | TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set, |
| 5999 | wrap_descr_delete, "descr.__delete__(obj)"), |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 6000 | FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6001 | "x.__init__(...) initializes x; " |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 6002 | "see x.__class__.__doc__ for signature", |
| 6003 | PyWrapperFlag_KEYWORDS), |
| 6004 | TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""), |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 6005 | TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6006 | {NULL} |
| 6007 | }; |
| 6008 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6009 | /* 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] | 6010 | 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] | 6011 | the offset to the type pointer, since it takes care to indirect through the |
| 6012 | proper indirection pointer (as_buffer, etc.); it returns NULL if the |
| 6013 | indirection pointer is NULL. */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6014 | static void ** |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6015 | slotptr(PyTypeObject *type, int ioffset) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6016 | { |
| 6017 | char *ptr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6018 | long offset = ioffset; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6019 | |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 6020 | /* Note: this depends on the order of the members of PyHeapTypeObject! */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6021 | assert(offset >= 0); |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 6022 | assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer)); |
| 6023 | if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) { |
Martin v. Löwis | ee36d65 | 2006-04-11 09:08:02 +0000 | [diff] [blame] | 6024 | ptr = (char *)type->tp_as_sequence; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 6025 | offset -= offsetof(PyHeapTypeObject, as_sequence); |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6026 | } |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 6027 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) { |
Martin v. Löwis | ee36d65 | 2006-04-11 09:08:02 +0000 | [diff] [blame] | 6028 | ptr = (char *)type->tp_as_mapping; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 6029 | offset -= offsetof(PyHeapTypeObject, as_mapping); |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6030 | } |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 6031 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) { |
Martin v. Löwis | ee36d65 | 2006-04-11 09:08:02 +0000 | [diff] [blame] | 6032 | ptr = (char *)type->tp_as_number; |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 6033 | offset -= offsetof(PyHeapTypeObject, as_number); |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6034 | } |
| 6035 | else { |
Martin v. Löwis | ee36d65 | 2006-04-11 09:08:02 +0000 | [diff] [blame] | 6036 | ptr = (char *)type; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6037 | } |
| 6038 | if (ptr != NULL) |
| 6039 | ptr += offset; |
| 6040 | return (void **)ptr; |
| 6041 | } |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 6042 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6043 | /* Length of array of slotdef pointers used to store slots with the |
| 6044 | same __name__. There should be at most MAX_EQUIV-1 slotdef entries with |
| 6045 | the same __name__, for any __name__. Since that's a static property, it is |
| 6046 | appropriate to declare fixed-size arrays for this. */ |
| 6047 | #define MAX_EQUIV 10 |
| 6048 | |
| 6049 | /* Return a slot pointer for a given name, but ONLY if the attribute has |
| 6050 | exactly one slot function. The name must be an interned string. */ |
| 6051 | static void ** |
| 6052 | resolve_slotdups(PyTypeObject *type, PyObject *name) |
| 6053 | { |
| 6054 | /* XXX Maybe this could be optimized more -- but is it worth it? */ |
| 6055 | |
| 6056 | /* pname and ptrs act as a little cache */ |
| 6057 | static PyObject *pname; |
| 6058 | static slotdef *ptrs[MAX_EQUIV]; |
| 6059 | slotdef *p, **pp; |
| 6060 | void **res, **ptr; |
| 6061 | |
| 6062 | if (pname != name) { |
| 6063 | /* Collect all slotdefs that match name into ptrs. */ |
| 6064 | pname = name; |
| 6065 | pp = ptrs; |
| 6066 | for (p = slotdefs; p->name_strobj; p++) { |
| 6067 | if (p->name_strobj == name) |
| 6068 | *pp++ = p; |
| 6069 | } |
| 6070 | *pp = NULL; |
| 6071 | } |
| 6072 | |
| 6073 | /* 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] | 6074 | a filled-in slot, return its value. Otherwise return NULL. */ |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6075 | res = NULL; |
| 6076 | for (pp = ptrs; *pp; pp++) { |
| 6077 | ptr = slotptr(type, (*pp)->offset); |
| 6078 | if (ptr == NULL || *ptr == NULL) |
| 6079 | continue; |
| 6080 | if (res != NULL) |
| 6081 | return NULL; |
| 6082 | res = ptr; |
| 6083 | } |
| 6084 | return res; |
| 6085 | } |
| 6086 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6087 | /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6088 | does some incredibly complex thinking and then sticks something into the |
| 6089 | slot. (It sees if the adjacent slotdefs for the same slot have conflicting |
| 6090 | interests, and then stores a generic wrapper or a specific function into |
| 6091 | the slot.) Return a pointer to the next slotdef with a different offset, |
| 6092 | because that's convenient for fixup_slot_dispatchers(). */ |
| 6093 | static slotdef * |
| 6094 | update_one_slot(PyTypeObject *type, slotdef *p) |
| 6095 | { |
| 6096 | PyObject *descr; |
| 6097 | PyWrapperDescrObject *d; |
| 6098 | void *generic = NULL, *specific = NULL; |
| 6099 | int use_generic = 0; |
| 6100 | int offset = p->offset; |
| 6101 | void **ptr = slotptr(type, offset); |
| 6102 | |
| 6103 | if (ptr == NULL) { |
| 6104 | do { |
| 6105 | ++p; |
| 6106 | } while (p->offset == offset); |
| 6107 | return p; |
| 6108 | } |
| 6109 | do { |
| 6110 | descr = _PyType_Lookup(type, p->name_strobj); |
Amaury Forgeot d'Arc | a40d573 | 2009-01-12 23:36:55 +0000 | [diff] [blame] | 6111 | if (descr == NULL) { |
| 6112 | if (ptr == (void**)&type->tp_iternext) { |
| 6113 | specific = _PyObject_NextNotImplemented; |
| 6114 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6115 | continue; |
Amaury Forgeot d'Arc | a40d573 | 2009-01-12 23:36:55 +0000 | [diff] [blame] | 6116 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6117 | if (Py_TYPE(descr) == &PyWrapperDescr_Type) { |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6118 | void **tptr = resolve_slotdups(type, p->name_strobj); |
| 6119 | if (tptr == NULL || tptr == ptr) |
| 6120 | generic = p->function; |
| 6121 | d = (PyWrapperDescrObject *)descr; |
| 6122 | if (d->d_base->wrapper == p->wrapper && |
| 6123 | PyType_IsSubtype(type, d->d_type)) |
| 6124 | { |
| 6125 | if (specific == NULL || |
| 6126 | specific == d->d_wrapped) |
| 6127 | specific = d->d_wrapped; |
| 6128 | else |
| 6129 | use_generic = 1; |
| 6130 | } |
| 6131 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6132 | else if (Py_TYPE(descr) == &PyCFunction_Type && |
Guido van Rossum | 721f62e | 2002-08-09 02:14:34 +0000 | [diff] [blame] | 6133 | PyCFunction_GET_FUNCTION(descr) == |
| 6134 | (PyCFunction)tp_new_wrapper && |
Amaury Forgeot d'Arc | bd55c52 | 2009-01-17 17:11:50 +0000 | [diff] [blame] | 6135 | ptr == (void**)&type->tp_new) |
Guido van Rossum | 721f62e | 2002-08-09 02:14:34 +0000 | [diff] [blame] | 6136 | { |
| 6137 | /* The __new__ wrapper is not a wrapper descriptor, |
| 6138 | so must be special-cased differently. |
| 6139 | If we don't do this, creating an instance will |
| 6140 | always use slot_tp_new which will look up |
| 6141 | __new__ in the MRO which will call tp_new_wrapper |
| 6142 | which will look through the base classes looking |
| 6143 | for a static base and call its tp_new (usually |
| 6144 | PyType_GenericNew), after performing various |
| 6145 | sanity checks and constructing a new argument |
| 6146 | list. Cut all that nonsense short -- this speeds |
| 6147 | up instance creation tremendously. */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 6148 | specific = (void *)type->tp_new; |
Guido van Rossum | 721f62e | 2002-08-09 02:14:34 +0000 | [diff] [blame] | 6149 | /* XXX I'm not 100% sure that there isn't a hole |
| 6150 | in this reasoning that requires additional |
| 6151 | sanity checks. I'll buy the first person to |
| 6152 | point out a bug in this reasoning a beer. */ |
| 6153 | } |
Nick Coghlan | 53663a6 | 2008-07-15 14:27:37 +0000 | [diff] [blame] | 6154 | else if (descr == Py_None && |
Amaury Forgeot d'Arc | bd55c52 | 2009-01-17 17:11:50 +0000 | [diff] [blame] | 6155 | ptr == (void**)&type->tp_hash) { |
Nick Coghlan | 53663a6 | 2008-07-15 14:27:37 +0000 | [diff] [blame] | 6156 | /* We specifically allow __hash__ to be set to None |
| 6157 | to prevent inheritance of the default |
| 6158 | implementation from object.__hash__ */ |
| 6159 | specific = PyObject_HashNotImplemented; |
| 6160 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6161 | else { |
| 6162 | use_generic = 1; |
| 6163 | generic = p->function; |
| 6164 | } |
| 6165 | } while ((++p)->offset == offset); |
| 6166 | if (specific && !use_generic) |
| 6167 | *ptr = specific; |
| 6168 | else |
| 6169 | *ptr = generic; |
| 6170 | return p; |
| 6171 | } |
| 6172 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6173 | /* In the type, update the slots whose slotdefs are gathered in the pp array. |
| 6174 | This is a callback for update_subclasses(). */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6175 | static int |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6176 | update_slots_callback(PyTypeObject *type, void *data) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6177 | { |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6178 | slotdef **pp = (slotdef **)data; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6179 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6180 | for (; *pp; pp++) |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6181 | update_one_slot(type, *pp); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6182 | return 0; |
| 6183 | } |
| 6184 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6185 | /* Comparison function for qsort() to compare slotdefs by their offset, and |
| 6186 | for equal offset by their address (to force a stable sort). */ |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6187 | static int |
| 6188 | slotdef_cmp(const void *aa, const void *bb) |
| 6189 | { |
| 6190 | const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb; |
| 6191 | int c = a->offset - b->offset; |
| 6192 | if (c != 0) |
| 6193 | return c; |
| 6194 | else |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6195 | /* Cannot use a-b, as this gives off_t, |
| 6196 | which may lose precision when converted to int. */ |
| 6197 | return (a > b) ? 1 : (a < b) ? -1 : 0; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6198 | } |
| 6199 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6200 | /* Initialize the slotdefs table by adding interned string objects for the |
| 6201 | names and sorting the entries. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6202 | static void |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6203 | init_slotdefs(void) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6204 | { |
| 6205 | slotdef *p; |
| 6206 | static int initialized = 0; |
| 6207 | |
| 6208 | if (initialized) |
| 6209 | return; |
| 6210 | for (p = slotdefs; p->name; p++) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 6211 | p->name_strobj = PyString_InternFromString(p->name); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6212 | if (!p->name_strobj) |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6213 | Py_FatalError("Out of memory interning slotdef names"); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6214 | } |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 6215 | qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef), |
| 6216 | slotdef_cmp); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6217 | initialized = 1; |
| 6218 | } |
| 6219 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6220 | /* Update the slots after assignment to a class (type) attribute. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6221 | static int |
| 6222 | update_slot(PyTypeObject *type, PyObject *name) |
| 6223 | { |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6224 | slotdef *ptrs[MAX_EQUIV]; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6225 | slotdef *p; |
| 6226 | slotdef **pp; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 6227 | int offset; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6228 | |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 6229 | /* Clear the VALID_VERSION flag of 'type' and all its |
| 6230 | subclasses. This could possibly be unified with the |
| 6231 | update_subclasses() recursion below, but carefully: |
| 6232 | they each have their own conditions on which to stop |
| 6233 | recursing into subclasses. */ |
Georg Brandl | 74a1dea | 2008-05-28 11:21:39 +0000 | [diff] [blame] | 6234 | PyType_Modified(type); |
Amaury Forgeot d'Arc | e4c270c | 2008-01-14 00:29:41 +0000 | [diff] [blame] | 6235 | |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 6236 | init_slotdefs(); |
| 6237 | pp = ptrs; |
| 6238 | for (p = slotdefs; p->name; p++) { |
| 6239 | /* XXX assume name is interned! */ |
| 6240 | if (p->name_strobj == name) |
| 6241 | *pp++ = p; |
| 6242 | } |
| 6243 | *pp = NULL; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6244 | for (pp = ptrs; *pp; pp++) { |
| 6245 | p = *pp; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 6246 | offset = p->offset; |
| 6247 | while (p > slotdefs && (p-1)->offset == offset) |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6248 | --p; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 6249 | *pp = p; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6250 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6251 | if (ptrs[0] == NULL) |
| 6252 | return 0; /* Not an attribute that affects any slots */ |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6253 | return update_subclasses(type, name, |
| 6254 | update_slots_callback, (void *)ptrs); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6255 | } |
| 6256 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6257 | /* Store the proper functions in the slot dispatches at class (type) |
| 6258 | definition time, based upon which operations the class overrides in its |
| 6259 | dict. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6260 | static void |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6261 | fixup_slot_dispatchers(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6262 | { |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6263 | slotdef *p; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6264 | |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6265 | init_slotdefs(); |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6266 | for (p = slotdefs; p->name; ) |
| 6267 | p = update_one_slot(type, p); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6268 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6269 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 6270 | static void |
| 6271 | update_all_slots(PyTypeObject* type) |
| 6272 | { |
| 6273 | slotdef *p; |
| 6274 | |
| 6275 | init_slotdefs(); |
| 6276 | for (p = slotdefs; p->name; p++) { |
| 6277 | /* update_slot returns int but can't actually fail */ |
| 6278 | update_slot(type, p->name_strobj); |
| 6279 | } |
| 6280 | } |
| 6281 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6282 | /* recurse_down_subclasses() and update_subclasses() are mutually |
| 6283 | recursive functions to call a callback for all subclasses, |
| 6284 | but refraining from recursing into subclasses that define 'name'. */ |
| 6285 | |
| 6286 | static int |
| 6287 | update_subclasses(PyTypeObject *type, PyObject *name, |
| 6288 | update_callback callback, void *data) |
| 6289 | { |
| 6290 | if (callback(type, data) < 0) |
| 6291 | return -1; |
| 6292 | return recurse_down_subclasses(type, name, callback, data); |
| 6293 | } |
| 6294 | |
| 6295 | static int |
| 6296 | recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
| 6297 | update_callback callback, void *data) |
| 6298 | { |
| 6299 | PyTypeObject *subclass; |
| 6300 | PyObject *ref, *subclasses, *dict; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6301 | Py_ssize_t i, n; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6302 | |
| 6303 | subclasses = type->tp_subclasses; |
| 6304 | if (subclasses == NULL) |
| 6305 | return 0; |
| 6306 | assert(PyList_Check(subclasses)); |
| 6307 | n = PyList_GET_SIZE(subclasses); |
| 6308 | for (i = 0; i < n; i++) { |
| 6309 | ref = PyList_GET_ITEM(subclasses, i); |
| 6310 | assert(PyWeakref_CheckRef(ref)); |
| 6311 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 6312 | assert(subclass != NULL); |
| 6313 | if ((PyObject *)subclass == Py_None) |
| 6314 | continue; |
| 6315 | assert(PyType_Check(subclass)); |
| 6316 | /* Avoid recursing down into unaffected classes */ |
| 6317 | dict = subclass->tp_dict; |
| 6318 | if (dict != NULL && PyDict_Check(dict) && |
| 6319 | PyDict_GetItem(dict, name) != NULL) |
| 6320 | continue; |
| 6321 | if (update_subclasses(subclass, name, callback, data) < 0) |
| 6322 | return -1; |
| 6323 | } |
| 6324 | return 0; |
| 6325 | } |
| 6326 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6327 | /* This function is called by PyType_Ready() to populate the type's |
| 6328 | dictionary with method descriptors for function slots. For each |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6329 | function slot (like tp_repr) that's defined in the type, one or more |
| 6330 | corresponding descriptors are added in the type's tp_dict dictionary |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6331 | under the appropriate name (like __repr__). Some function slots |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6332 | cause more than one descriptor to be added (for example, the nb_add |
| 6333 | slot adds both __add__ and __radd__ descriptors) and some function |
| 6334 | slots compete for the same descriptor (for example both sq_item and |
| 6335 | mp_subscript generate a __getitem__ descriptor). |
| 6336 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6337 | In the latter case, the first slotdef entry encoutered wins. Since |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 6338 | 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] | 6339 | PyHeapTypeObject, this gives us some control over disambiguating |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6340 | between competing slots: the members of PyHeapTypeObject are listed |
| 6341 | from most general to least general, so the most general slot is |
| 6342 | preferred. In particular, because as_mapping comes before as_sequence, |
| 6343 | for a type that defines both mp_subscript and sq_item, mp_subscript |
| 6344 | wins. |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6345 | |
| 6346 | This only adds new descriptors and doesn't overwrite entries in |
| 6347 | tp_dict that were previously defined. The descriptors contain a |
| 6348 | reference to the C function they must call, so that it's safe if they |
| 6349 | are copied into a subtype's __dict__ and the subtype has a different |
| 6350 | C function in its slot -- calling the method defined by the |
| 6351 | descriptor will call the C function that was used to create it, |
| 6352 | rather than the C function present in the slot when it is called. |
| 6353 | (This is important because a subtype may have a C function in the |
| 6354 | slot that calls the method from the dictionary, and we want to avoid |
| 6355 | infinite recursion here.) */ |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6356 | |
| 6357 | static int |
| 6358 | add_operators(PyTypeObject *type) |
| 6359 | { |
| 6360 | PyObject *dict = type->tp_dict; |
| 6361 | slotdef *p; |
| 6362 | PyObject *descr; |
| 6363 | void **ptr; |
| 6364 | |
| 6365 | init_slotdefs(); |
| 6366 | for (p = slotdefs; p->name; p++) { |
| 6367 | if (p->wrapper == NULL) |
| 6368 | continue; |
| 6369 | ptr = slotptr(type, p->offset); |
| 6370 | if (!ptr || !*ptr) |
| 6371 | continue; |
| 6372 | if (PyDict_GetItem(dict, p->name_strobj)) |
| 6373 | continue; |
Nick Coghlan | 53663a6 | 2008-07-15 14:27:37 +0000 | [diff] [blame] | 6374 | if (*ptr == PyObject_HashNotImplemented) { |
| 6375 | /* Classes may prevent the inheritance of the tp_hash |
| 6376 | slot by storing PyObject_HashNotImplemented in it. Make it |
| 6377 | visible as a None value for the __hash__ attribute. */ |
| 6378 | if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0) |
| 6379 | return -1; |
| 6380 | } |
| 6381 | else { |
| 6382 | descr = PyDescr_NewWrapper(type, p, *ptr); |
| 6383 | if (descr == NULL) |
| 6384 | return -1; |
| 6385 | if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) |
| 6386 | return -1; |
| 6387 | Py_DECREF(descr); |
| 6388 | } |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6389 | } |
| 6390 | if (type->tp_new != NULL) { |
| 6391 | if (add_tp_new_wrapper(type) < 0) |
| 6392 | return -1; |
| 6393 | } |
| 6394 | return 0; |
| 6395 | } |
| 6396 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6397 | |
| 6398 | /* Cooperative 'super' */ |
| 6399 | |
| 6400 | typedef struct { |
| 6401 | PyObject_HEAD |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6402 | PyTypeObject *type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6403 | PyObject *obj; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6404 | PyTypeObject *obj_type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6405 | } superobject; |
| 6406 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 6407 | static PyMemberDef super_members[] = { |
| 6408 | {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY, |
| 6409 | "the class invoking super()"}, |
| 6410 | {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY, |
| 6411 | "the instance invoking super(); may be None"}, |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6412 | {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY, |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 6413 | "the type of the instance invoking super(); may be None"}, |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6414 | {0} |
| 6415 | }; |
| 6416 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6417 | static void |
| 6418 | super_dealloc(PyObject *self) |
| 6419 | { |
| 6420 | superobject *su = (superobject *)self; |
| 6421 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6422 | _PyObject_GC_UNTRACK(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6423 | Py_XDECREF(su->obj); |
| 6424 | Py_XDECREF(su->type); |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6425 | Py_XDECREF(su->obj_type); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6426 | Py_TYPE(self)->tp_free(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6427 | } |
| 6428 | |
| 6429 | static PyObject * |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6430 | super_repr(PyObject *self) |
| 6431 | { |
| 6432 | superobject *su = (superobject *)self; |
| 6433 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6434 | if (su->obj_type) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 6435 | return PyString_FromFormat( |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 6436 | "<super: <class '%s'>, <%s object>>", |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6437 | su->type ? su->type->tp_name : "NULL", |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6438 | su->obj_type->tp_name); |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6439 | else |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 6440 | return PyString_FromFormat( |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 6441 | "<super: <class '%s'>, NULL>", |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6442 | su->type ? su->type->tp_name : "NULL"); |
| 6443 | } |
| 6444 | |
| 6445 | static PyObject * |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6446 | super_getattro(PyObject *self, PyObject *name) |
| 6447 | { |
| 6448 | superobject *su = (superobject *)self; |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 6449 | int skip = su->obj_type == NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6450 | |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 6451 | if (!skip) { |
| 6452 | /* We want __class__ to return the class of the super object |
| 6453 | (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] | 6454 | skip = (PyString_Check(name) && |
| 6455 | PyString_GET_SIZE(name) == 9 && |
| 6456 | strcmp(PyString_AS_STRING(name), "__class__") == 0); |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 6457 | } |
| 6458 | |
| 6459 | if (!skip) { |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 6460 | PyObject *mro, *res, *tmp, *dict; |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 6461 | PyTypeObject *starttype; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6462 | descrgetfunc f; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6463 | Py_ssize_t i, n; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6464 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6465 | starttype = su->obj_type; |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 6466 | mro = starttype->tp_mro; |
| 6467 | |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6468 | if (mro == NULL) |
| 6469 | n = 0; |
| 6470 | else { |
| 6471 | assert(PyTuple_Check(mro)); |
| 6472 | n = PyTuple_GET_SIZE(mro); |
| 6473 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6474 | for (i = 0; i < n; i++) { |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6475 | if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6476 | break; |
| 6477 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6478 | i++; |
| 6479 | res = NULL; |
| 6480 | for (; i < n; i++) { |
| 6481 | tmp = PyTuple_GET_ITEM(mro, i); |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 6482 | if (PyType_Check(tmp)) |
| 6483 | dict = ((PyTypeObject *)tmp)->tp_dict; |
| 6484 | else if (PyClass_Check(tmp)) |
| 6485 | dict = ((PyClassObject *)tmp)->cl_dict; |
| 6486 | else |
| 6487 | continue; |
| 6488 | res = PyDict_GetItem(dict, name); |
Guido van Rossum | 6cc5bb6 | 2003-04-16 20:01:36 +0000 | [diff] [blame] | 6489 | if (res != NULL) { |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6490 | Py_INCREF(res); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6491 | f = Py_TYPE(res)->tp_descr_get; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6492 | if (f != NULL) { |
Phillip J. Eby | 91a968a | 2004-03-25 02:19:34 +0000 | [diff] [blame] | 6493 | tmp = f(res, |
| 6494 | /* Only pass 'obj' param if |
| 6495 | this is instance-mode super |
| 6496 | (See SF ID #743627) |
| 6497 | */ |
Hye-Shik Chang | ff365c9 | 2004-03-25 16:37:03 +0000 | [diff] [blame] | 6498 | (su->obj == (PyObject *) |
| 6499 | su->obj_type |
Phillip J. Eby | 91a968a | 2004-03-25 02:19:34 +0000 | [diff] [blame] | 6500 | ? (PyObject *)NULL |
| 6501 | : su->obj), |
Guido van Rossum | d464107 | 2002-04-03 02:13:37 +0000 | [diff] [blame] | 6502 | (PyObject *)starttype); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6503 | Py_DECREF(res); |
| 6504 | res = tmp; |
| 6505 | } |
| 6506 | return res; |
| 6507 | } |
| 6508 | } |
| 6509 | } |
| 6510 | return PyObject_GenericGetAttr(self, name); |
| 6511 | } |
| 6512 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6513 | static PyTypeObject * |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6514 | supercheck(PyTypeObject *type, PyObject *obj) |
| 6515 | { |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6516 | /* Check that a super() call makes sense. Return a type object. |
| 6517 | |
| 6518 | obj can be a new-style class, or an instance of one: |
| 6519 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6520 | - 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] | 6521 | used for class methods; the return value is obj. |
| 6522 | |
| 6523 | - If it is an instance, it must be an instance of 'type'. This is |
| 6524 | the normal case; the return value is obj.__class__. |
| 6525 | |
| 6526 | 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] | 6527 | 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] | 6528 | This will allow using super() with a proxy for obj. |
| 6529 | */ |
| 6530 | |
Guido van Rossum | 8e80a72 | 2003-02-18 19:22:22 +0000 | [diff] [blame] | 6531 | /* Check for first bullet above (special case) */ |
| 6532 | if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) { |
| 6533 | Py_INCREF(obj); |
| 6534 | return (PyTypeObject *)obj; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6535 | } |
Guido van Rossum | 8e80a72 | 2003-02-18 19:22:22 +0000 | [diff] [blame] | 6536 | |
| 6537 | /* Normal case */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6538 | if (PyType_IsSubtype(Py_TYPE(obj), type)) { |
| 6539 | Py_INCREF(Py_TYPE(obj)); |
| 6540 | return Py_TYPE(obj); |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6541 | } |
| 6542 | else { |
| 6543 | /* Try the slow way */ |
| 6544 | static PyObject *class_str = NULL; |
| 6545 | PyObject *class_attr; |
| 6546 | |
| 6547 | if (class_str == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 6548 | class_str = PyString_FromString("__class__"); |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6549 | if (class_str == NULL) |
| 6550 | return NULL; |
| 6551 | } |
| 6552 | |
| 6553 | class_attr = PyObject_GetAttr(obj, class_str); |
| 6554 | |
| 6555 | if (class_attr != NULL && |
| 6556 | PyType_Check(class_attr) && |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6557 | (PyTypeObject *)class_attr != Py_TYPE(obj)) |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6558 | { |
| 6559 | int ok = PyType_IsSubtype( |
| 6560 | (PyTypeObject *)class_attr, type); |
| 6561 | if (ok) |
| 6562 | return (PyTypeObject *)class_attr; |
| 6563 | } |
| 6564 | |
| 6565 | if (class_attr == NULL) |
| 6566 | PyErr_Clear(); |
| 6567 | else |
| 6568 | Py_DECREF(class_attr); |
| 6569 | } |
| 6570 | |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6571 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6572 | "super(type, obj): " |
| 6573 | "obj must be an instance or subtype of type"); |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6574 | return NULL; |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6575 | } |
| 6576 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6577 | static PyObject * |
| 6578 | super_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 6579 | { |
| 6580 | superobject *su = (superobject *)self; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 6581 | superobject *newobj; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6582 | |
| 6583 | if (obj == NULL || obj == Py_None || su->obj != NULL) { |
| 6584 | /* Not binding to an object, or already bound */ |
| 6585 | Py_INCREF(self); |
| 6586 | return self; |
| 6587 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6588 | if (Py_TYPE(su) != &PySuper_Type) |
Armin Rigo | 7726dc0 | 2005-05-15 15:32:08 +0000 | [diff] [blame] | 6589 | /* If su is an instance of a (strict) subclass of super, |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6590 | call its type */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6591 | return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su), |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6592 | su->type, obj, NULL); |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6593 | else { |
| 6594 | /* Inline the common case */ |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6595 | PyTypeObject *obj_type = supercheck(su->type, obj); |
| 6596 | if (obj_type == NULL) |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6597 | return NULL; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 6598 | newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type, |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6599 | NULL, NULL); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 6600 | if (newobj == NULL) |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6601 | return NULL; |
| 6602 | Py_INCREF(su->type); |
| 6603 | Py_INCREF(obj); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 6604 | newobj->type = su->type; |
| 6605 | newobj->obj = obj; |
| 6606 | newobj->obj_type = obj_type; |
| 6607 | return (PyObject *)newobj; |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6608 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6609 | } |
| 6610 | |
| 6611 | static int |
| 6612 | super_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 6613 | { |
| 6614 | superobject *su = (superobject *)self; |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6615 | PyTypeObject *type; |
| 6616 | PyObject *obj = NULL; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6617 | PyTypeObject *obj_type = NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6618 | |
Georg Brandl | 5d59c09 | 2006-09-30 08:43:30 +0000 | [diff] [blame] | 6619 | if (!_PyArg_NoKeywords("super", kwds)) |
| 6620 | return -1; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6621 | if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj)) |
| 6622 | return -1; |
| 6623 | if (obj == Py_None) |
| 6624 | obj = NULL; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6625 | if (obj != NULL) { |
| 6626 | obj_type = supercheck(type, obj); |
| 6627 | if (obj_type == NULL) |
| 6628 | return -1; |
| 6629 | Py_INCREF(obj); |
| 6630 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6631 | Py_INCREF(type); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6632 | su->type = type; |
| 6633 | su->obj = obj; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6634 | su->obj_type = obj_type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6635 | return 0; |
| 6636 | } |
| 6637 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6638 | PyDoc_STRVAR(super_doc, |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6639 | "super(type) -> unbound super object\n" |
| 6640 | "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6641 | "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6642 | "Typical use to call a cooperative superclass method:\n" |
| 6643 | "class C(B):\n" |
| 6644 | " def meth(self, arg):\n" |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6645 | " super(C, self).meth(arg)"); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6646 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6647 | static int |
| 6648 | super_traverse(PyObject *self, visitproc visit, void *arg) |
| 6649 | { |
| 6650 | superobject *su = (superobject *)self; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6651 | |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 6652 | Py_VISIT(su->obj); |
| 6653 | Py_VISIT(su->type); |
| 6654 | Py_VISIT(su->obj_type); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6655 | |
| 6656 | return 0; |
| 6657 | } |
| 6658 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6659 | PyTypeObject PySuper_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 6660 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6661 | "super", /* tp_name */ |
| 6662 | sizeof(superobject), /* tp_basicsize */ |
| 6663 | 0, /* tp_itemsize */ |
| 6664 | /* methods */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6665 | super_dealloc, /* tp_dealloc */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6666 | 0, /* tp_print */ |
| 6667 | 0, /* tp_getattr */ |
| 6668 | 0, /* tp_setattr */ |
| 6669 | 0, /* tp_compare */ |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6670 | super_repr, /* tp_repr */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6671 | 0, /* tp_as_number */ |
| 6672 | 0, /* tp_as_sequence */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6673 | 0, /* tp_as_mapping */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6674 | 0, /* tp_hash */ |
| 6675 | 0, /* tp_call */ |
| 6676 | 0, /* tp_str */ |
| 6677 | super_getattro, /* tp_getattro */ |
| 6678 | 0, /* tp_setattro */ |
| 6679 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6680 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 6681 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6682 | super_doc, /* tp_doc */ |
| 6683 | super_traverse, /* tp_traverse */ |
| 6684 | 0, /* tp_clear */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6685 | 0, /* tp_richcompare */ |
| 6686 | 0, /* tp_weaklistoffset */ |
| 6687 | 0, /* tp_iter */ |
| 6688 | 0, /* tp_iternext */ |
| 6689 | 0, /* tp_methods */ |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6690 | super_members, /* tp_members */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6691 | 0, /* tp_getset */ |
| 6692 | 0, /* tp_base */ |
| 6693 | 0, /* tp_dict */ |
| 6694 | super_descr_get, /* tp_descr_get */ |
| 6695 | 0, /* tp_descr_set */ |
| 6696 | 0, /* tp_dictoffset */ |
| 6697 | super_init, /* tp_init */ |
| 6698 | PyType_GenericAlloc, /* tp_alloc */ |
| 6699 | PyType_GenericNew, /* tp_new */ |
Jeremy Hylton | 2d1f5c9 | 2007-02-27 17:24:48 +0000 | [diff] [blame] | 6700 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6701 | }; |