Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* Type object implementation */ |
| 2 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3 | #include "Python.h" |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 4 | #include "frameobject.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 7 | #include <ctype.h> |
| 8 | |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 9 | |
| 10 | /* Support type attribute cache */ |
| 11 | |
| 12 | /* The cache can keep references to the names alive for longer than |
| 13 | they normally would. This is why the maximum size is limited to |
| 14 | MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large |
| 15 | strings are used as attribute names. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 16 | #define MCACHE_MAX_ATTR_SIZE 100 |
| 17 | #define MCACHE_SIZE_EXP 10 |
| 18 | #define MCACHE_HASH(version, name_hash) \ |
| 19 | (((unsigned int)(version) * (unsigned int)(name_hash)) \ |
| 20 | >> (8*sizeof(unsigned int) - MCACHE_SIZE_EXP)) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 21 | #define MCACHE_HASH_METHOD(type, name) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | MCACHE_HASH((type)->tp_version_tag, \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 23 | ((PyASCIIObject *)(name))->hash) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 24 | #define MCACHE_CACHEABLE_NAME(name) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 25 | PyUnicode_CheckExact(name) && \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 26 | PyUnicode_READY(name) != -1 && \ |
| 27 | PyUnicode_GET_LENGTH(name) <= MCACHE_MAX_ATTR_SIZE |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 28 | |
| 29 | struct method_cache_entry { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 30 | unsigned int version; |
| 31 | PyObject *name; /* reference to exactly a str or None */ |
| 32 | PyObject *value; /* borrowed */ |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP]; |
| 36 | static unsigned int next_version_tag = 0; |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 37 | |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 38 | _Py_IDENTIFIER(__dict__); |
| 39 | _Py_IDENTIFIER(__class__); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 40 | |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 41 | unsigned int |
| 42 | PyType_ClearCache(void) |
| 43 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 44 | Py_ssize_t i; |
| 45 | unsigned int cur_version_tag = next_version_tag - 1; |
| 46 | |
| 47 | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { |
| 48 | method_cache[i].version = 0; |
| 49 | Py_CLEAR(method_cache[i].name); |
| 50 | method_cache[i].value = NULL; |
| 51 | } |
| 52 | next_version_tag = 0; |
| 53 | /* mark all version tags as invalid */ |
| 54 | PyType_Modified(&PyBaseObject_Type); |
| 55 | return cur_version_tag; |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 56 | } |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 57 | |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 58 | void |
| 59 | PyType_Modified(PyTypeObject *type) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 60 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | /* Invalidate any cached data for the specified type and all |
| 62 | subclasses. This function is called after the base |
| 63 | classes, mro, or attributes of the type are altered. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 64 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 65 | Invariants: |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 66 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | - Py_TPFLAGS_VALID_VERSION_TAG is never set if |
| 68 | Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type |
| 69 | objects coming from non-recompiled extension modules) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 70 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type, |
| 72 | it must first be set on all super types. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 73 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 74 | This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a |
| 75 | type (so it must first clear it on all subclasses). The |
| 76 | tp_version_tag value is meaningless unless this flag is set. |
| 77 | We don't assign new version tags eagerly, but only as |
| 78 | needed. |
| 79 | */ |
| 80 | PyObject *raw, *ref; |
| 81 | Py_ssize_t i, n; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 82 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 | if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
| 84 | return; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 85 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 86 | raw = type->tp_subclasses; |
| 87 | if (raw != NULL) { |
| 88 | n = PyList_GET_SIZE(raw); |
| 89 | for (i = 0; i < n; i++) { |
| 90 | ref = PyList_GET_ITEM(raw, i); |
| 91 | ref = PyWeakref_GET_OBJECT(ref); |
| 92 | if (ref != Py_None) { |
| 93 | PyType_Modified((PyTypeObject *)ref); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static void |
| 101 | type_mro_modified(PyTypeObject *type, PyObject *bases) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 102 | /* |
| 103 | Check that all base classes or elements of the mro of type are |
| 104 | able to be cached. This function is called after the base |
| 105 | classes or mro of the type are altered. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 106 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 107 | Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type |
| 108 | inherits from an old-style class, either directly or if it |
| 109 | appears in the MRO of a new-style class. No support either for |
| 110 | custom MROs that include types that are not officially super |
| 111 | types. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | Called from mro_internal, which will subsequently be called on |
| 114 | each subclass when their mro is recursively updated. |
| 115 | */ |
| 116 | Py_ssize_t i, n; |
| 117 | int clear = 0; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 119 | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) |
| 120 | return; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 121 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 122 | n = PyTuple_GET_SIZE(bases); |
| 123 | for (i = 0; i < n; i++) { |
| 124 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 125 | PyTypeObject *cls; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 126 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | if (!PyType_Check(b) ) { |
| 128 | clear = 1; |
| 129 | break; |
| 130 | } |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 132 | cls = (PyTypeObject *)b; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 133 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 134 | if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) || |
| 135 | !PyType_IsSubtype(type, cls)) { |
| 136 | clear = 1; |
| 137 | break; |
| 138 | } |
| 139 | } |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | if (clear) |
| 142 | type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG| |
| 143 | Py_TPFLAGS_VALID_VERSION_TAG); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static int |
| 147 | assign_version_tag(PyTypeObject *type) |
| 148 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | /* Ensure that the tp_version_tag is valid and set |
| 150 | Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this |
| 151 | must first be done on all super classes. Return 0 if this |
| 152 | cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG. |
| 153 | */ |
| 154 | Py_ssize_t i, n; |
| 155 | PyObject *bases; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 156 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 157 | if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
| 158 | return 1; |
| 159 | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) |
| 160 | return 0; |
| 161 | if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) |
| 162 | return 0; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | type->tp_version_tag = next_version_tag++; |
| 165 | /* for stress-testing: next_version_tag &= 0xFF; */ |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 167 | if (type->tp_version_tag == 0) { |
| 168 | /* wrap-around or just starting Python - clear the whole |
| 169 | cache by filling names with references to Py_None. |
| 170 | Values are also set to NULL for added protection, as they |
| 171 | are borrowed reference */ |
| 172 | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { |
| 173 | method_cache[i].value = NULL; |
| 174 | Py_XDECREF(method_cache[i].name); |
| 175 | method_cache[i].name = Py_None; |
| 176 | Py_INCREF(Py_None); |
| 177 | } |
| 178 | /* mark all version tags as invalid */ |
| 179 | PyType_Modified(&PyBaseObject_Type); |
| 180 | return 1; |
| 181 | } |
| 182 | bases = type->tp_bases; |
| 183 | n = PyTuple_GET_SIZE(bases); |
| 184 | for (i = 0; i < n; i++) { |
| 185 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 186 | assert(PyType_Check(b)); |
| 187 | if (!assign_version_tag((PyTypeObject *)b)) |
| 188 | return 0; |
| 189 | } |
| 190 | type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG; |
| 191 | return 1; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 195 | static PyMemberDef type_members[] = { |
Benjamin Peterson | 0e10206 | 2010-08-25 23:13:17 +0000 | [diff] [blame] | 196 | {"__basicsize__", T_PYSSIZET, offsetof(PyTypeObject,tp_basicsize),READONLY}, |
| 197 | {"__itemsize__", T_PYSSIZET, offsetof(PyTypeObject, tp_itemsize), READONLY}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 198 | {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY}, |
| 199 | {"__weakrefoffset__", T_LONG, |
| 200 | offsetof(PyTypeObject, tp_weaklistoffset), READONLY}, |
| 201 | {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY}, |
| 202 | {"__dictoffset__", T_LONG, |
| 203 | offsetof(PyTypeObject, tp_dictoffset), READONLY}, |
| 204 | {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY}, |
| 205 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 206 | }; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 207 | |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 208 | static int |
| 209 | check_set_special_type_attr(PyTypeObject *type, PyObject *value, const char *name) |
| 210 | { |
| 211 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 212 | PyErr_Format(PyExc_TypeError, |
| 213 | "can't set %s.%s", type->tp_name, name); |
| 214 | return 0; |
| 215 | } |
| 216 | if (!value) { |
| 217 | PyErr_Format(PyExc_TypeError, |
| 218 | "can't delete %s.%s", type->tp_name, name); |
| 219 | return 0; |
| 220 | } |
| 221 | return 1; |
| 222 | } |
| 223 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 224 | static PyObject * |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 225 | type_name(PyTypeObject *type, void *context) |
| 226 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 227 | const char *s; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 229 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 230 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 232 | Py_INCREF(et->ht_name); |
| 233 | return et->ht_name; |
| 234 | } |
| 235 | else { |
| 236 | s = strrchr(type->tp_name, '.'); |
| 237 | if (s == NULL) |
| 238 | s = type->tp_name; |
| 239 | else |
| 240 | s++; |
| 241 | return PyUnicode_FromString(s); |
| 242 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 245 | static PyObject * |
| 246 | type_qualname(PyTypeObject *type, void *context) |
| 247 | { |
| 248 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 249 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
| 250 | Py_INCREF(et->ht_qualname); |
| 251 | return et->ht_qualname; |
| 252 | } |
| 253 | else { |
| 254 | return type_name(type, context); |
| 255 | } |
| 256 | } |
| 257 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 258 | static int |
| 259 | type_set_name(PyTypeObject *type, PyObject *value, void *context) |
| 260 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 261 | PyHeapTypeObject* et; |
| 262 | char *tp_name; |
| 263 | PyObject *tmp; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 264 | |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 265 | if (!check_set_special_type_attr(type, value, "__name__")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 266 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | if (!PyUnicode_Check(value)) { |
| 268 | PyErr_Format(PyExc_TypeError, |
| 269 | "can only assign string to %s.__name__, not '%s'", |
| 270 | type->tp_name, Py_TYPE(value)->tp_name); |
| 271 | return -1; |
| 272 | } |
Guido van Rossum | e845c0f | 2007-11-02 23:07:07 +0000 | [diff] [blame] | 273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | /* Check absence of null characters */ |
| 275 | tmp = PyUnicode_FromStringAndSize("\0", 1); |
| 276 | if (tmp == NULL) |
| 277 | return -1; |
| 278 | if (PyUnicode_Contains(value, tmp) != 0) { |
| 279 | Py_DECREF(tmp); |
| 280 | PyErr_Format(PyExc_ValueError, |
| 281 | "__name__ must not contain null bytes"); |
| 282 | return -1; |
| 283 | } |
| 284 | Py_DECREF(tmp); |
Guido van Rossum | e845c0f | 2007-11-02 23:07:07 +0000 | [diff] [blame] | 285 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 286 | tp_name = _PyUnicode_AsString(value); |
| 287 | if (tp_name == NULL) |
| 288 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 290 | et = (PyHeapTypeObject*)type; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 291 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | Py_INCREF(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 293 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 294 | Py_DECREF(et->ht_name); |
| 295 | et->ht_name = value; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 296 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 297 | type->tp_name = tp_name; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | return 0; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 302 | static int |
| 303 | type_set_qualname(PyTypeObject *type, PyObject *value, void *context) |
| 304 | { |
| 305 | PyHeapTypeObject* et; |
| 306 | |
| 307 | if (!PyUnicode_Check(value)) { |
| 308 | PyErr_Format(PyExc_TypeError, |
| 309 | "can only assign string to %s.__qualname__, not '%s'", |
| 310 | type->tp_name, Py_TYPE(value)->tp_name); |
| 311 | return -1; |
| 312 | } |
| 313 | |
| 314 | et = (PyHeapTypeObject*)type; |
| 315 | Py_INCREF(value); |
| 316 | Py_DECREF(et->ht_qualname); |
| 317 | et->ht_qualname = value; |
| 318 | return 0; |
| 319 | } |
| 320 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 321 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 322 | type_module(PyTypeObject *type, void *context) |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 323 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 324 | PyObject *mod; |
| 325 | char *s; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 327 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 328 | mod = PyDict_GetItemString(type->tp_dict, "__module__"); |
| 329 | if (!mod) { |
| 330 | PyErr_Format(PyExc_AttributeError, "__module__"); |
| 331 | return 0; |
| 332 | } |
| 333 | Py_XINCREF(mod); |
| 334 | return mod; |
| 335 | } |
| 336 | else { |
| 337 | s = strrchr(type->tp_name, '.'); |
| 338 | if (s != NULL) |
| 339 | return PyUnicode_FromStringAndSize( |
| 340 | type->tp_name, (Py_ssize_t)(s - type->tp_name)); |
| 341 | return PyUnicode_FromString("builtins"); |
| 342 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 345 | static int |
| 346 | type_set_module(PyTypeObject *type, PyObject *value, void *context) |
| 347 | { |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 348 | if (!check_set_special_type_attr(type, value, "__module__")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 351 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | return PyDict_SetItemString(type->tp_dict, "__module__", value); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 356 | static PyObject * |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 357 | type_abstractmethods(PyTypeObject *type, void *context) |
| 358 | { |
Benjamin Peterson | aec5fd1 | 2010-10-02 00:03:31 +0000 | [diff] [blame] | 359 | PyObject *mod = NULL; |
Benjamin Peterson | 84060b8 | 2010-10-03 02:13:39 +0000 | [diff] [blame] | 360 | /* type itself has an __abstractmethods__ descriptor (this). Don't return |
| 361 | that. */ |
Benjamin Peterson | aec5fd1 | 2010-10-02 00:03:31 +0000 | [diff] [blame] | 362 | if (type != &PyType_Type) |
| 363 | mod = PyDict_GetItemString(type->tp_dict, "__abstractmethods__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 364 | if (!mod) { |
Benjamin Peterson | 23b628e | 2011-01-12 18:56:07 +0000 | [diff] [blame] | 365 | PyErr_SetString(PyExc_AttributeError, "__abstractmethods__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | return NULL; |
| 367 | } |
| 368 | Py_XINCREF(mod); |
| 369 | return mod; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | static int |
| 373 | type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context) |
| 374 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 375 | /* __abstractmethods__ should only be set once on a type, in |
| 376 | abc.ABCMeta.__new__, so this function doesn't do anything |
| 377 | special to update subclasses. |
| 378 | */ |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 379 | int res; |
| 380 | if (value != NULL) { |
| 381 | res = PyDict_SetItemString(type->tp_dict, "__abstractmethods__", value); |
| 382 | } |
| 383 | else { |
| 384 | res = PyDict_DelItemString(type->tp_dict, "__abstractmethods__"); |
| 385 | if (res && PyErr_ExceptionMatches(PyExc_KeyError)) { |
Benjamin Peterson | 23b628e | 2011-01-12 18:56:07 +0000 | [diff] [blame] | 386 | PyErr_SetString(PyExc_AttributeError, "__abstractmethods__"); |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 387 | return -1; |
| 388 | } |
| 389 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 390 | if (res == 0) { |
| 391 | PyType_Modified(type); |
| 392 | if (value && PyObject_IsTrue(value)) { |
| 393 | type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT; |
| 394 | } |
| 395 | else { |
| 396 | type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT; |
| 397 | } |
| 398 | } |
| 399 | return res; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | static PyObject * |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 403 | type_get_bases(PyTypeObject *type, void *context) |
| 404 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 405 | Py_INCREF(type->tp_bases); |
| 406 | return type->tp_bases; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | static PyTypeObject *best_base(PyObject *); |
| 410 | static int mro_internal(PyTypeObject *); |
| 411 | static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *); |
| 412 | static int add_subclass(PyTypeObject*, PyTypeObject*); |
| 413 | static void remove_subclass(PyTypeObject *, PyTypeObject *); |
| 414 | static void update_all_slots(PyTypeObject *); |
| 415 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 416 | typedef int (*update_callback)(PyTypeObject *, void *); |
| 417 | static int update_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | update_callback callback, void *data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 419 | static int recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | update_callback callback, void *data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 421 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 422 | static int |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 423 | mro_subclasses(PyTypeObject *type, PyObject* temp) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 424 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | PyTypeObject *subclass; |
| 426 | PyObject *ref, *subclasses, *old_mro; |
| 427 | Py_ssize_t i, n; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 428 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | subclasses = type->tp_subclasses; |
| 430 | if (subclasses == NULL) |
| 431 | return 0; |
| 432 | assert(PyList_Check(subclasses)); |
| 433 | n = PyList_GET_SIZE(subclasses); |
| 434 | for (i = 0; i < n; i++) { |
| 435 | ref = PyList_GET_ITEM(subclasses, i); |
| 436 | assert(PyWeakref_CheckRef(ref)); |
| 437 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 438 | assert(subclass != NULL); |
| 439 | if ((PyObject *)subclass == Py_None) |
| 440 | continue; |
| 441 | assert(PyType_Check(subclass)); |
| 442 | old_mro = subclass->tp_mro; |
| 443 | if (mro_internal(subclass) < 0) { |
| 444 | subclass->tp_mro = old_mro; |
| 445 | return -1; |
| 446 | } |
| 447 | else { |
| 448 | PyObject* tuple; |
| 449 | tuple = PyTuple_Pack(2, subclass, old_mro); |
| 450 | Py_DECREF(old_mro); |
| 451 | if (!tuple) |
| 452 | return -1; |
| 453 | if (PyList_Append(temp, tuple) < 0) |
| 454 | return -1; |
| 455 | Py_DECREF(tuple); |
| 456 | } |
| 457 | if (mro_subclasses(subclass, temp) < 0) |
| 458 | return -1; |
| 459 | } |
| 460 | return 0; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | static int |
| 464 | type_set_bases(PyTypeObject *type, PyObject *value, void *context) |
| 465 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 466 | Py_ssize_t i; |
| 467 | int r = 0; |
| 468 | PyObject *ob, *temp; |
| 469 | PyTypeObject *new_base, *old_base; |
| 470 | PyObject *old_bases, *old_mro; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 471 | |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 472 | if (!check_set_special_type_attr(type, value, "__bases__")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 474 | if (!PyTuple_Check(value)) { |
| 475 | PyErr_Format(PyExc_TypeError, |
| 476 | "can only assign tuple to %s.__bases__, not %s", |
| 477 | type->tp_name, Py_TYPE(value)->tp_name); |
| 478 | return -1; |
| 479 | } |
| 480 | if (PyTuple_GET_SIZE(value) == 0) { |
| 481 | PyErr_Format(PyExc_TypeError, |
| 482 | "can only assign non-empty tuple to %s.__bases__, not ()", |
| 483 | type->tp_name); |
| 484 | return -1; |
| 485 | } |
| 486 | for (i = 0; i < PyTuple_GET_SIZE(value); i++) { |
| 487 | ob = PyTuple_GET_ITEM(value, i); |
| 488 | if (!PyType_Check(ob)) { |
| 489 | PyErr_Format( |
| 490 | PyExc_TypeError, |
| 491 | "%s.__bases__ must be tuple of old- or new-style classes, not '%s'", |
| 492 | type->tp_name, Py_TYPE(ob)->tp_name); |
| 493 | return -1; |
| 494 | } |
| 495 | if (PyType_Check(ob)) { |
| 496 | if (PyType_IsSubtype((PyTypeObject*)ob, type)) { |
| 497 | PyErr_SetString(PyExc_TypeError, |
| 498 | "a __bases__ item causes an inheritance cycle"); |
| 499 | return -1; |
| 500 | } |
| 501 | } |
| 502 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 504 | new_base = best_base(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 505 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 506 | if (!new_base) { |
| 507 | return -1; |
| 508 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | if (!compatible_for_assignment(type->tp_base, new_base, "__bases__")) |
| 511 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 512 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 513 | Py_INCREF(new_base); |
| 514 | Py_INCREF(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | old_bases = type->tp_bases; |
| 517 | old_base = type->tp_base; |
| 518 | old_mro = type->tp_mro; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | type->tp_bases = value; |
| 521 | type->tp_base = new_base; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 523 | if (mro_internal(type) < 0) { |
| 524 | goto bail; |
| 525 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 527 | temp = PyList_New(0); |
| 528 | if (!temp) |
| 529 | goto bail; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 530 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | r = mro_subclasses(type, temp); |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 533 | if (r < 0) { |
| 534 | for (i = 0; i < PyList_Size(temp); i++) { |
| 535 | PyTypeObject* cls; |
| 536 | PyObject* mro; |
| 537 | PyArg_UnpackTuple(PyList_GET_ITEM(temp, i), |
| 538 | "", 2, 2, &cls, &mro); |
| 539 | Py_INCREF(mro); |
| 540 | ob = cls->tp_mro; |
| 541 | cls->tp_mro = mro; |
| 542 | Py_DECREF(ob); |
| 543 | } |
| 544 | Py_DECREF(temp); |
| 545 | goto bail; |
| 546 | } |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 548 | Py_DECREF(temp); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 549 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 550 | /* any base that was in __bases__ but now isn't, we |
| 551 | need to remove |type| from its tp_subclasses. |
| 552 | conversely, any class now in __bases__ that wasn't |
| 553 | needs to have |type| added to its subclasses. */ |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | /* for now, sod that: just remove from all old_bases, |
| 556 | add to all new_bases */ |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 557 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 558 | for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) { |
| 559 | ob = PyTuple_GET_ITEM(old_bases, i); |
| 560 | if (PyType_Check(ob)) { |
| 561 | remove_subclass( |
| 562 | (PyTypeObject*)ob, type); |
| 563 | } |
| 564 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 566 | for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) { |
| 567 | ob = PyTuple_GET_ITEM(value, i); |
| 568 | if (PyType_Check(ob)) { |
| 569 | if (add_subclass((PyTypeObject*)ob, type) < 0) |
| 570 | r = -1; |
| 571 | } |
| 572 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 573 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 574 | update_all_slots(type); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 575 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | Py_DECREF(old_bases); |
| 577 | Py_DECREF(old_base); |
| 578 | Py_DECREF(old_mro); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 579 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 580 | return r; |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 581 | |
| 582 | bail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 583 | Py_DECREF(type->tp_bases); |
| 584 | Py_DECREF(type->tp_base); |
| 585 | if (type->tp_mro != old_mro) { |
| 586 | Py_DECREF(type->tp_mro); |
| 587 | } |
Michael W. Hudson | e723e45 | 2003-08-07 14:58:10 +0000 | [diff] [blame] | 588 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | type->tp_bases = old_bases; |
| 590 | type->tp_base = old_base; |
| 591 | type->tp_mro = old_mro; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 592 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 593 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 597 | type_dict(PyTypeObject *type, void *context) |
| 598 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 599 | if (type->tp_dict == NULL) { |
| 600 | Py_INCREF(Py_None); |
| 601 | return Py_None; |
| 602 | } |
| 603 | return PyDictProxy_New(type->tp_dict); |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 606 | static PyObject * |
| 607 | type_get_doc(PyTypeObject *type, void *context) |
| 608 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 609 | PyObject *result; |
| 610 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) |
| 611 | return PyUnicode_FromString(type->tp_doc); |
| 612 | result = PyDict_GetItemString(type->tp_dict, "__doc__"); |
| 613 | if (result == NULL) { |
| 614 | result = Py_None; |
| 615 | Py_INCREF(result); |
| 616 | } |
| 617 | else if (Py_TYPE(result)->tp_descr_get) { |
| 618 | result = Py_TYPE(result)->tp_descr_get(result, NULL, |
| 619 | (PyObject *)type); |
| 620 | } |
| 621 | else { |
| 622 | Py_INCREF(result); |
| 623 | } |
| 624 | return result; |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Benjamin Peterson | 01fc6cd | 2011-08-17 12:03:47 -0500 | [diff] [blame] | 627 | static int |
| 628 | type_set_doc(PyTypeObject *type, PyObject *value, void *context) |
| 629 | { |
| 630 | if (!check_set_special_type_attr(type, value, "__doc__")) |
| 631 | return -1; |
| 632 | PyType_Modified(type); |
| 633 | return PyDict_SetItemString(type->tp_dict, "__doc__", value); |
| 634 | } |
| 635 | |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 636 | static PyObject * |
| 637 | type___instancecheck__(PyObject *type, PyObject *inst) |
| 638 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | switch (_PyObject_RealIsInstance(inst, type)) { |
| 640 | case -1: |
| 641 | return NULL; |
| 642 | case 0: |
| 643 | Py_RETURN_FALSE; |
| 644 | default: |
| 645 | Py_RETURN_TRUE; |
| 646 | } |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | |
| 650 | static PyObject * |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 651 | type___subclasscheck__(PyObject *type, PyObject *inst) |
| 652 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 653 | switch (_PyObject_RealIsSubclass(inst, type)) { |
| 654 | case -1: |
| 655 | return NULL; |
| 656 | case 0: |
| 657 | Py_RETURN_FALSE; |
| 658 | default: |
| 659 | Py_RETURN_TRUE; |
| 660 | } |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 663 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 664 | static PyGetSetDef type_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 665 | {"__name__", (getter)type_name, (setter)type_set_name, NULL}, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 666 | {"__qualname__", (getter)type_qualname, (setter)type_set_qualname, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 667 | {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL}, |
| 668 | {"__module__", (getter)type_module, (setter)type_set_module, NULL}, |
| 669 | {"__abstractmethods__", (getter)type_abstractmethods, |
| 670 | (setter)type_set_abstractmethods, NULL}, |
| 671 | {"__dict__", (getter)type_dict, NULL, NULL}, |
Benjamin Peterson | 01fc6cd | 2011-08-17 12:03:47 -0500 | [diff] [blame] | 672 | {"__doc__", (getter)type_get_doc, (setter)type_set_doc, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 674 | }; |
| 675 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 676 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 677 | type_repr(PyTypeObject *type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 678 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | PyObject *mod, *name, *rtn; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 681 | mod = type_module(type, NULL); |
| 682 | if (mod == NULL) |
| 683 | PyErr_Clear(); |
| 684 | else if (!PyUnicode_Check(mod)) { |
| 685 | Py_DECREF(mod); |
| 686 | mod = NULL; |
| 687 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 688 | name = type_qualname(type, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | if (name == NULL) |
| 690 | return NULL; |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 691 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 692 | if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins")) |
| 693 | rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name); |
| 694 | else |
| 695 | rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 697 | Py_XDECREF(mod); |
| 698 | Py_DECREF(name); |
| 699 | return rtn; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 702 | static PyObject * |
| 703 | type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 704 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | PyObject *obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 707 | if (type->tp_new == NULL) { |
| 708 | PyErr_Format(PyExc_TypeError, |
| 709 | "cannot create '%.100s' instances", |
| 710 | type->tp_name); |
| 711 | return NULL; |
| 712 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 713 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 714 | obj = type->tp_new(type, args, kwds); |
| 715 | if (obj != NULL) { |
| 716 | /* Ugly exception: when the call was type(something), |
| 717 | don't call tp_init on the result. */ |
| 718 | if (type == &PyType_Type && |
| 719 | PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && |
| 720 | (kwds == NULL || |
| 721 | (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) |
| 722 | return obj; |
| 723 | /* If the returned object is not an instance of type, |
| 724 | it won't be initialized. */ |
| 725 | if (!PyType_IsSubtype(Py_TYPE(obj), type)) |
| 726 | return obj; |
| 727 | type = Py_TYPE(obj); |
| 728 | if (type->tp_init != NULL && |
| 729 | type->tp_init(obj, args, kwds) < 0) { |
| 730 | Py_DECREF(obj); |
| 731 | obj = NULL; |
| 732 | } |
| 733 | } |
| 734 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 738 | PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 739 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 740 | PyObject *obj; |
| 741 | const size_t size = _PyObject_VAR_SIZE(type, nitems+1); |
| 742 | /* note that we need to add one, for the sentinel */ |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 743 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 744 | if (PyType_IS_GC(type)) |
| 745 | obj = _PyObject_GC_Malloc(size); |
| 746 | else |
| 747 | obj = (PyObject *)PyObject_MALLOC(size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 748 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 749 | if (obj == NULL) |
| 750 | return PyErr_NoMemory(); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 751 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 752 | memset(obj, '\0', size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 754 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 755 | Py_INCREF(type); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 756 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 757 | if (type->tp_itemsize == 0) |
| 758 | PyObject_INIT(obj, type); |
| 759 | else |
| 760 | (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 761 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 762 | if (PyType_IS_GC(type)) |
| 763 | _PyObject_GC_TRACK(obj); |
| 764 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | PyObject * |
| 768 | PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 769 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 770 | return type->tp_alloc(type, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 773 | /* Helpers for subtyping */ |
| 774 | |
| 775 | static int |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 776 | traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg) |
| 777 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 778 | Py_ssize_t i, n; |
| 779 | PyMemberDef *mp; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 781 | n = Py_SIZE(type); |
| 782 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
| 783 | for (i = 0; i < n; i++, mp++) { |
| 784 | if (mp->type == T_OBJECT_EX) { |
| 785 | char *addr = (char *)self + mp->offset; |
| 786 | PyObject *obj = *(PyObject **)addr; |
| 787 | if (obj != NULL) { |
| 788 | int err = visit(obj, arg); |
| 789 | if (err) |
| 790 | return err; |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | return 0; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | static int |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 798 | subtype_traverse(PyObject *self, visitproc visit, void *arg) |
| 799 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | PyTypeObject *type, *base; |
| 801 | traverseproc basetraverse; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 803 | /* Find the nearest base with a different tp_traverse, |
| 804 | and traverse slots while we're at it */ |
| 805 | type = Py_TYPE(self); |
| 806 | base = type; |
| 807 | while ((basetraverse = base->tp_traverse) == subtype_traverse) { |
| 808 | if (Py_SIZE(base)) { |
| 809 | int err = traverse_slots(base, self, visit, arg); |
| 810 | if (err) |
| 811 | return err; |
| 812 | } |
| 813 | base = base->tp_base; |
| 814 | assert(base); |
| 815 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 816 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 817 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 818 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 819 | if (dictptr && *dictptr) |
| 820 | Py_VISIT(*dictptr); |
| 821 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 822 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 823 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 824 | /* For a heaptype, the instances count as references |
| 825 | to the type. Traverse the type so the collector |
| 826 | can find cycles involving this link. */ |
| 827 | Py_VISIT(type); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 828 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 829 | if (basetraverse) |
| 830 | return basetraverse(self, visit, arg); |
| 831 | return 0; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | static void |
| 835 | clear_slots(PyTypeObject *type, PyObject *self) |
| 836 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 837 | Py_ssize_t i, n; |
| 838 | PyMemberDef *mp; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 839 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 840 | n = Py_SIZE(type); |
| 841 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
| 842 | for (i = 0; i < n; i++, mp++) { |
| 843 | if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) { |
| 844 | char *addr = (char *)self + mp->offset; |
| 845 | PyObject *obj = *(PyObject **)addr; |
| 846 | if (obj != NULL) { |
| 847 | *(PyObject **)addr = NULL; |
| 848 | Py_DECREF(obj); |
| 849 | } |
| 850 | } |
| 851 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | static int |
| 855 | subtype_clear(PyObject *self) |
| 856 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 857 | PyTypeObject *type, *base; |
| 858 | inquiry baseclear; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 859 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 860 | /* Find the nearest base with a different tp_clear |
| 861 | and clear slots while we're at it */ |
| 862 | type = Py_TYPE(self); |
| 863 | base = type; |
| 864 | while ((baseclear = base->tp_clear) == subtype_clear) { |
| 865 | if (Py_SIZE(base)) |
| 866 | clear_slots(base, self); |
| 867 | base = base->tp_base; |
| 868 | assert(base); |
| 869 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 870 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 871 | /* There's no need to clear the instance dict (if any); |
| 872 | the collector will call its tp_clear handler. */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 874 | if (baseclear) |
| 875 | return baseclear(self); |
| 876 | return 0; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 877 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 878 | |
| 879 | static void |
| 880 | subtype_dealloc(PyObject *self) |
| 881 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 882 | PyTypeObject *type, *base; |
| 883 | destructor basedealloc; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 884 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 885 | /* Extract the type; we expect it to be a heap type */ |
| 886 | type = Py_TYPE(self); |
| 887 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 889 | /* Test whether the type has GC exactly once */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 | if (!PyType_IS_GC(type)) { |
| 892 | /* It's really rare to find a dynamic type that doesn't have |
| 893 | GC; it can only happen when deriving from 'object' and not |
| 894 | adding any slots or instance variables. This allows |
| 895 | certain simplifications: there's no need to call |
| 896 | clear_slots(), or DECREF the dict, or clear weakrefs. */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 897 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 898 | /* Maybe call finalizer; exit early if resurrected */ |
| 899 | if (type->tp_del) { |
| 900 | type->tp_del(self); |
| 901 | if (self->ob_refcnt > 0) |
| 902 | return; |
| 903 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 904 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 905 | /* Find the nearest base with a different tp_dealloc */ |
| 906 | base = type; |
| 907 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 908 | assert(Py_SIZE(base) == 0); |
| 909 | base = base->tp_base; |
| 910 | assert(base); |
| 911 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 912 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 913 | /* Extract the type again; tp_del may have changed it */ |
| 914 | type = Py_TYPE(self); |
Benjamin Peterson | 193152c | 2009-04-25 01:08:45 +0000 | [diff] [blame] | 915 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | /* Call the base tp_dealloc() */ |
| 917 | assert(basedealloc); |
| 918 | basedealloc(self); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 919 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 920 | /* Can't reference self beyond this point */ |
| 921 | Py_DECREF(type); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 922 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 923 | /* Done */ |
| 924 | return; |
| 925 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 926 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 927 | /* We get here only if the type has GC */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 928 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | /* UnTrack and re-Track around the trashcan macro, alas */ |
| 930 | /* See explanation at end of function for full disclosure */ |
| 931 | PyObject_GC_UnTrack(self); |
| 932 | ++_PyTrash_delete_nesting; |
| 933 | Py_TRASHCAN_SAFE_BEGIN(self); |
| 934 | --_PyTrash_delete_nesting; |
| 935 | /* DO NOT restore GC tracking at this point. weakref callbacks |
| 936 | * (if any, and whether directly here or indirectly in something we |
| 937 | * call) may trigger GC, and if self is tracked at that point, it |
| 938 | * will look like trash to GC and GC will try to delete self again. |
| 939 | */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 940 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 941 | /* Find the nearest base with a different tp_dealloc */ |
| 942 | base = type; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 943 | while ((/*basedealloc =*/ base->tp_dealloc) == subtype_dealloc) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 944 | base = base->tp_base; |
| 945 | assert(base); |
| 946 | } |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 947 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 948 | /* If we added a weaklist, we clear it. Do this *before* calling |
| 949 | the finalizer (__del__), clearing slots, or clearing the instance |
| 950 | dict. */ |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 951 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) |
| 953 | PyObject_ClearWeakRefs(self); |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 954 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 955 | /* Maybe call finalizer; exit early if resurrected */ |
| 956 | if (type->tp_del) { |
| 957 | _PyObject_GC_TRACK(self); |
| 958 | type->tp_del(self); |
| 959 | if (self->ob_refcnt > 0) |
| 960 | goto endlabel; /* resurrected */ |
| 961 | else |
| 962 | _PyObject_GC_UNTRACK(self); |
| 963 | /* New weakrefs could be created during the finalizer call. |
| 964 | If this occurs, clear them out without calling their |
| 965 | finalizers since they might rely on part of the object |
| 966 | being finalized that has already been destroyed. */ |
| 967 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) { |
| 968 | /* Modeled after GET_WEAKREFS_LISTPTR() */ |
| 969 | PyWeakReference **list = (PyWeakReference **) \ |
| 970 | PyObject_GET_WEAKREFS_LISTPTR(self); |
| 971 | while (*list) |
| 972 | _PyWeakref_ClearRef(*list); |
| 973 | } |
| 974 | } |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 975 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 976 | /* Clear slots up to the nearest base with a different tp_dealloc */ |
| 977 | base = type; |
| 978 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 979 | if (Py_SIZE(base)) |
| 980 | clear_slots(base, self); |
| 981 | base = base->tp_base; |
| 982 | assert(base); |
| 983 | } |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 984 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 985 | /* If we added a dict, DECREF it */ |
| 986 | if (type->tp_dictoffset && !base->tp_dictoffset) { |
| 987 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 988 | if (dictptr != NULL) { |
| 989 | PyObject *dict = *dictptr; |
| 990 | if (dict != NULL) { |
| 991 | Py_DECREF(dict); |
| 992 | *dictptr = NULL; |
| 993 | } |
| 994 | } |
| 995 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 996 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 997 | /* Extract the type again; tp_del may have changed it */ |
| 998 | type = Py_TYPE(self); |
Benjamin Peterson | 193152c | 2009-04-25 01:08:45 +0000 | [diff] [blame] | 999 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1000 | /* Call the base tp_dealloc(); first retrack self if |
| 1001 | * basedealloc knows about gc. |
| 1002 | */ |
| 1003 | if (PyType_IS_GC(base)) |
| 1004 | _PyObject_GC_TRACK(self); |
| 1005 | assert(basedealloc); |
| 1006 | basedealloc(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1007 | |
Antoine Pitrou | 84f1b17 | 2011-07-12 21:57:15 +0200 | [diff] [blame] | 1008 | PyType_Modified(type); |
| 1009 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1010 | /* Can't reference self beyond this point */ |
| 1011 | Py_DECREF(type); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1012 | |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 1013 | endlabel: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1014 | ++_PyTrash_delete_nesting; |
| 1015 | Py_TRASHCAN_SAFE_END(self); |
| 1016 | --_PyTrash_delete_nesting; |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1017 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1018 | /* Explanation of the weirdness around the trashcan macros: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1019 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | Q. What do the trashcan macros do? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1021 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1022 | A. Read the comment titled "Trashcan mechanism" in object.h. |
| 1023 | For one, this explains why there must be a call to GC-untrack |
| 1024 | before the trashcan begin macro. Without understanding the |
| 1025 | trashcan code, the answers to the following questions don't make |
| 1026 | sense. |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1027 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | Q. Why do we GC-untrack before the trashcan and then immediately |
| 1029 | GC-track again afterward? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1031 | A. In the case that the base class is GC-aware, the base class |
| 1032 | probably GC-untracks the object. If it does that using the |
| 1033 | UNTRACK macro, this will crash when the object is already |
| 1034 | untracked. Because we don't know what the base class does, the |
| 1035 | only safe thing is to make sure the object is tracked when we |
| 1036 | call the base class dealloc. But... The trashcan begin macro |
| 1037 | requires that the object is *untracked* before it is called. So |
| 1038 | the dance becomes: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1039 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | GC untrack |
| 1041 | trashcan begin |
| 1042 | GC track |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1043 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1044 | Q. Why did the last question say "immediately GC-track again"? |
| 1045 | It's nowhere near immediately. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 1046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1047 | A. Because the code *used* to re-track immediately. Bad Idea. |
| 1048 | self has a refcount of 0, and if gc ever gets its hands on it |
| 1049 | (which can happen if any weakref callback gets invoked), it |
| 1050 | looks like trash to gc too, and gc also tries to delete self |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 1051 | then. But we're already deleting self. Double deallocation is |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1052 | a subtle disaster. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 1053 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1054 | Q. Why the bizarre (net-zero) manipulation of |
| 1055 | _PyTrash_delete_nesting around the trashcan macros? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1056 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1057 | A. Some base classes (e.g. list) also use the trashcan mechanism. |
| 1058 | The following scenario used to be possible: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1059 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1060 | - suppose the trashcan level is one below the trashcan limit |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1061 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1062 | - subtype_dealloc() is called |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1063 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1064 | - the trashcan limit is not yet reached, so the trashcan level |
| 1065 | is incremented and the code between trashcan begin and end is |
| 1066 | executed |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1067 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1068 | - this destroys much of the object's contents, including its |
| 1069 | slots and __dict__ |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1070 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1071 | - basedealloc() is called; this is really list_dealloc(), or |
| 1072 | some other type which also uses the trashcan macros |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1073 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1074 | - the trashcan limit is now reached, so the object is put on the |
| 1075 | trashcan's to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1076 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | - basedealloc() returns |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1078 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | - subtype_dealloc() decrefs the object's type |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1080 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1081 | - subtype_dealloc() returns |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1082 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1083 | - later, the trashcan code starts deleting the objects from its |
| 1084 | to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1085 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1086 | - subtype_dealloc() is called *AGAIN* for the same object |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1087 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1088 | - at the very least (if the destroyed slots and __dict__ don't |
| 1089 | cause problems) the object's type gets decref'ed a second |
| 1090 | time, which is *BAD*!!! |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1091 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1092 | The remedy is to make sure that if the code between trashcan |
| 1093 | begin and end in subtype_dealloc() is called, the code between |
| 1094 | trashcan begin and end in basedealloc() will also be called. |
| 1095 | This is done by decrementing the level after passing into the |
| 1096 | trashcan block, and incrementing it just before leaving the |
| 1097 | block. |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1098 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1099 | But now it's possible that a chain of objects consisting solely |
| 1100 | of objects whose deallocator is subtype_dealloc() will defeat |
| 1101 | the trashcan mechanism completely: the decremented level means |
| 1102 | that the effective level never reaches the limit. Therefore, we |
| 1103 | *increment* the level *before* entering the trashcan block, and |
| 1104 | matchingly decrement it after leaving. This means the trashcan |
| 1105 | code will trigger a little early, but that's no big deal. |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1106 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1107 | Q. Are there any live examples of code in need of all this |
| 1108 | complexity? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1109 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1110 | A. Yes. See SF bug 668433 for code that crashed (when Python was |
| 1111 | compiled in debug mode) before the trashcan level manipulations |
| 1112 | were added. For more discussion, see SF patches 581742, 575073 |
| 1113 | and bug 574207. |
| 1114 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1117 | static PyTypeObject *solid_base(PyTypeObject *type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1118 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1119 | /* type test with subclassing support */ |
| 1120 | |
| 1121 | int |
| 1122 | PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
| 1123 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1124 | PyObject *mro; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1125 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1126 | mro = a->tp_mro; |
| 1127 | if (mro != NULL) { |
| 1128 | /* Deal with multiple inheritance without recursion |
| 1129 | by walking the MRO tuple */ |
| 1130 | Py_ssize_t i, n; |
| 1131 | assert(PyTuple_Check(mro)); |
| 1132 | n = PyTuple_GET_SIZE(mro); |
| 1133 | for (i = 0; i < n; i++) { |
| 1134 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) |
| 1135 | return 1; |
| 1136 | } |
| 1137 | return 0; |
| 1138 | } |
| 1139 | else { |
| 1140 | /* a is not completely initilized yet; follow tp_base */ |
| 1141 | do { |
| 1142 | if (a == b) |
| 1143 | return 1; |
| 1144 | a = a->tp_base; |
| 1145 | } while (a != NULL); |
| 1146 | return b == &PyBaseObject_Type; |
| 1147 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1150 | /* Internal routines to do a method lookup in the type |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1151 | without looking in the instance dictionary |
| 1152 | (so we can't use PyObject_GetAttr) but still binding |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1153 | it to the instance. The arguments are the object, |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1154 | 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] | 1155 | static variable used to cache the interned Python string. |
| 1156 | |
| 1157 | Two variants: |
| 1158 | |
| 1159 | - lookup_maybe() returns NULL without raising an exception |
| 1160 | when the _PyType_Lookup() call fails; |
| 1161 | |
| 1162 | - lookup_method() always raises an exception upon errors. |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1163 | |
| 1164 | - _PyObject_LookupSpecial() exported for the benefit of other places. |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1165 | */ |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1166 | |
| 1167 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1168 | lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1169 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1170 | PyObject *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1172 | if (*attrobj == NULL) { |
| 1173 | *attrobj = PyUnicode_InternFromString(attrstr); |
| 1174 | if (*attrobj == NULL) |
| 1175 | return NULL; |
| 1176 | } |
| 1177 | res = _PyType_Lookup(Py_TYPE(self), *attrobj); |
| 1178 | if (res != NULL) { |
| 1179 | descrgetfunc f; |
| 1180 | if ((f = Py_TYPE(res)->tp_descr_get) == NULL) |
| 1181 | Py_INCREF(res); |
| 1182 | else |
| 1183 | res = f(res, self, (PyObject *)(Py_TYPE(self))); |
| 1184 | } |
| 1185 | return res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1188 | static PyObject * |
| 1189 | lookup_method(PyObject *self, char *attrstr, PyObject **attrobj) |
| 1190 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1191 | PyObject *res = lookup_maybe(self, attrstr, attrobj); |
| 1192 | if (res == NULL && !PyErr_Occurred()) |
| 1193 | PyErr_SetObject(PyExc_AttributeError, *attrobj); |
| 1194 | return res; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1197 | PyObject * |
| 1198 | _PyObject_LookupSpecial(PyObject *self, char *attrstr, PyObject **attrobj) |
| 1199 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1200 | return lookup_maybe(self, attrstr, attrobj); |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1203 | /* A variation of PyObject_CallMethod that uses lookup_method() |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1204 | instead of PyObject_GetAttrString(). This uses the same convention |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1205 | as lookup_method to cache the interned name string object. */ |
| 1206 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1207 | static PyObject * |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1208 | call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 1209 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1210 | va_list va; |
| 1211 | PyObject *args, *func = 0, *retval; |
| 1212 | va_start(va, format); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1214 | func = lookup_maybe(o, name, nameobj); |
| 1215 | if (func == NULL) { |
| 1216 | va_end(va); |
| 1217 | if (!PyErr_Occurred()) |
| 1218 | PyErr_SetObject(PyExc_AttributeError, *nameobj); |
| 1219 | return NULL; |
| 1220 | } |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1222 | if (format && *format) |
| 1223 | args = Py_VaBuildValue(format, va); |
| 1224 | else |
| 1225 | args = PyTuple_New(0); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1227 | va_end(va); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1229 | if (args == NULL) |
| 1230 | return NULL; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1232 | assert(PyTuple_Check(args)); |
| 1233 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1235 | Py_DECREF(args); |
| 1236 | Py_DECREF(func); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1238 | return retval; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | /* Clone of call_method() that returns NotImplemented when the lookup fails. */ |
| 1242 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1243 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1244 | call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 1245 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1246 | va_list va; |
| 1247 | PyObject *args, *func = 0, *retval; |
| 1248 | va_start(va, format); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1249 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1250 | func = lookup_maybe(o, name, nameobj); |
| 1251 | if (func == NULL) { |
| 1252 | va_end(va); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1253 | if (!PyErr_Occurred()) |
| 1254 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1255 | return NULL; |
| 1256 | } |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1258 | if (format && *format) |
| 1259 | args = Py_VaBuildValue(format, va); |
| 1260 | else |
| 1261 | args = PyTuple_New(0); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1263 | va_end(va); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1264 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1265 | if (args == NULL) |
| 1266 | return NULL; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1267 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1268 | assert(PyTuple_Check(args)); |
| 1269 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1271 | Py_DECREF(args); |
| 1272 | Py_DECREF(func); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1274 | return retval; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1277 | /* |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1278 | Method resolution order algorithm C3 described in |
| 1279 | "A Monotonic Superclass Linearization for Dylan", |
| 1280 | by Kim Barrett, Bob Cassel, Paul Haahr, |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1281 | David A. Moon, Keith Playford, and P. Tucker Withington. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1282 | (OOPSLA 1996) |
| 1283 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1284 | Some notes about the rules implied by C3: |
| 1285 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1286 | No duplicate bases. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1287 | It isn't legal to repeat a class in a list of base classes. |
| 1288 | |
| 1289 | The next three properties are the 3 constraints in "C3". |
| 1290 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1291 | Local precendece order. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1292 | If A precedes B in C's MRO, then A will precede B in the MRO of all |
| 1293 | subclasses of C. |
| 1294 | |
| 1295 | Monotonicity. |
| 1296 | The MRO of a class must be an extension without reordering of the |
| 1297 | MRO of each of its superclasses. |
| 1298 | |
| 1299 | Extended Precedence Graph (EPG). |
| 1300 | Linearization is consistent if there is a path in the EPG from |
| 1301 | each class to all its successors in the linearization. See |
| 1302 | the paper for definition of EPG. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1303 | */ |
| 1304 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1305 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1306 | tail_contains(PyObject *list, int whence, PyObject *o) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1307 | Py_ssize_t j, size; |
| 1308 | size = PyList_GET_SIZE(list); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1310 | for (j = whence+1; j < size; j++) { |
| 1311 | if (PyList_GET_ITEM(list, j) == o) |
| 1312 | return 1; |
| 1313 | } |
| 1314 | return 0; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1317 | static PyObject * |
| 1318 | class_name(PyObject *cls) |
| 1319 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1320 | _Py_IDENTIFIER(__name__); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1321 | PyObject *name = _PyObject_GetAttrId(cls, &PyId___name__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 | if (name == NULL) { |
| 1323 | PyErr_Clear(); |
| 1324 | Py_XDECREF(name); |
| 1325 | name = PyObject_Repr(cls); |
| 1326 | } |
| 1327 | if (name == NULL) |
| 1328 | return NULL; |
| 1329 | if (!PyUnicode_Check(name)) { |
| 1330 | Py_DECREF(name); |
| 1331 | return NULL; |
| 1332 | } |
| 1333 | return name; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | static int |
| 1337 | check_duplicates(PyObject *list) |
| 1338 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1339 | Py_ssize_t i, j, n; |
| 1340 | /* Let's use a quadratic time algorithm, |
| 1341 | assuming that the bases lists is short. |
| 1342 | */ |
| 1343 | n = PyList_GET_SIZE(list); |
| 1344 | for (i = 0; i < n; i++) { |
| 1345 | PyObject *o = PyList_GET_ITEM(list, i); |
| 1346 | for (j = i + 1; j < n; j++) { |
| 1347 | if (PyList_GET_ITEM(list, j) == o) { |
| 1348 | o = class_name(o); |
| 1349 | if (o != NULL) { |
| 1350 | PyErr_Format(PyExc_TypeError, |
| 1351 | "duplicate base class %U", |
| 1352 | o); |
| 1353 | Py_DECREF(o); |
| 1354 | } else { |
| 1355 | PyErr_SetString(PyExc_TypeError, |
| 1356 | "duplicate base class"); |
| 1357 | } |
| 1358 | return -1; |
| 1359 | } |
| 1360 | } |
| 1361 | } |
| 1362 | return 0; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | /* Raise a TypeError for an MRO order disagreement. |
| 1366 | |
| 1367 | It's hard to produce a good error message. In the absence of better |
| 1368 | insight into error reporting, report the classes that were candidates |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1369 | 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] | 1370 | order in which they should be put in the MRO, but it's hard to |
| 1371 | diagnose what constraint can't be satisfied. |
| 1372 | */ |
| 1373 | |
| 1374 | static void |
| 1375 | set_mro_error(PyObject *to_merge, int *remain) |
| 1376 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1377 | Py_ssize_t i, n, off, to_merge_size; |
| 1378 | char buf[1000]; |
| 1379 | PyObject *k, *v; |
| 1380 | PyObject *set = PyDict_New(); |
| 1381 | if (!set) return; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1382 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1383 | to_merge_size = PyList_GET_SIZE(to_merge); |
| 1384 | for (i = 0; i < to_merge_size; i++) { |
| 1385 | PyObject *L = PyList_GET_ITEM(to_merge, i); |
| 1386 | if (remain[i] < PyList_GET_SIZE(L)) { |
| 1387 | PyObject *c = PyList_GET_ITEM(L, remain[i]); |
| 1388 | if (PyDict_SetItem(set, c, Py_None) < 0) { |
| 1389 | Py_DECREF(set); |
| 1390 | return; |
| 1391 | } |
| 1392 | } |
| 1393 | } |
| 1394 | n = PyDict_Size(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1395 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1396 | off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \ |
Raymond Hettinger | f394df4 | 2003-04-06 19:13:41 +0000 | [diff] [blame] | 1397 | consistent method resolution\norder (MRO) for bases"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1398 | i = 0; |
| 1399 | while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) { |
| 1400 | PyObject *name = class_name(k); |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1401 | char *name_str; |
| 1402 | if (name != NULL) { |
| 1403 | name_str = _PyUnicode_AsString(name); |
| 1404 | if (name_str == NULL) |
Victor Stinner | ba644a6 | 2010-05-19 01:50:45 +0000 | [diff] [blame] | 1405 | name_str = "?"; |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1406 | } else |
Victor Stinner | ba644a6 | 2010-05-19 01:50:45 +0000 | [diff] [blame] | 1407 | name_str = "?"; |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1408 | off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", name_str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1409 | Py_XDECREF(name); |
| 1410 | if (--n && (size_t)(off+1) < sizeof(buf)) { |
| 1411 | buf[off++] = ','; |
| 1412 | buf[off] = '\0'; |
| 1413 | } |
| 1414 | } |
| 1415 | PyErr_SetString(PyExc_TypeError, buf); |
| 1416 | Py_DECREF(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1419 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1420 | pmerge(PyObject *acc, PyObject* to_merge) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1421 | Py_ssize_t i, j, to_merge_size, empty_cnt; |
| 1422 | int *remain; |
| 1423 | int ok; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1424 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1425 | to_merge_size = PyList_GET_SIZE(to_merge); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1426 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1427 | /* remain stores an index into each sublist of to_merge. |
| 1428 | remain[i] is the index of the next base in to_merge[i] |
| 1429 | that is not included in acc. |
| 1430 | */ |
| 1431 | remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size); |
| 1432 | if (remain == NULL) |
| 1433 | return -1; |
| 1434 | for (i = 0; i < to_merge_size; i++) |
| 1435 | remain[i] = 0; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1436 | |
| 1437 | again: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1438 | empty_cnt = 0; |
| 1439 | for (i = 0; i < to_merge_size; i++) { |
| 1440 | PyObject *candidate; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1441 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1442 | PyObject *cur_list = PyList_GET_ITEM(to_merge, i); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1443 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1444 | if (remain[i] >= PyList_GET_SIZE(cur_list)) { |
| 1445 | empty_cnt++; |
| 1446 | continue; |
| 1447 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1448 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1449 | /* Choose next candidate for MRO. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1450 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1451 | The input sequences alone can determine the choice. |
| 1452 | If not, choose the class which appears in the MRO |
| 1453 | of the earliest direct superclass of the new class. |
| 1454 | */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1455 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1456 | candidate = PyList_GET_ITEM(cur_list, remain[i]); |
| 1457 | for (j = 0; j < to_merge_size; j++) { |
| 1458 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
| 1459 | if (tail_contains(j_lst, remain[j], candidate)) { |
| 1460 | goto skip; /* continue outer loop */ |
| 1461 | } |
| 1462 | } |
| 1463 | ok = PyList_Append(acc, candidate); |
| 1464 | if (ok < 0) { |
| 1465 | PyMem_Free(remain); |
| 1466 | return -1; |
| 1467 | } |
| 1468 | for (j = 0; j < to_merge_size; j++) { |
| 1469 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
| 1470 | if (remain[j] < PyList_GET_SIZE(j_lst) && |
| 1471 | PyList_GET_ITEM(j_lst, remain[j]) == candidate) { |
| 1472 | remain[j]++; |
| 1473 | } |
| 1474 | } |
| 1475 | goto again; |
| 1476 | skip: ; |
| 1477 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1478 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1479 | if (empty_cnt == to_merge_size) { |
| 1480 | PyMem_FREE(remain); |
| 1481 | return 0; |
| 1482 | } |
| 1483 | set_mro_error(to_merge, remain); |
| 1484 | PyMem_FREE(remain); |
| 1485 | return -1; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1488 | static PyObject * |
| 1489 | mro_implementation(PyTypeObject *type) |
| 1490 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1491 | Py_ssize_t i, n; |
| 1492 | int ok; |
| 1493 | PyObject *bases, *result; |
| 1494 | PyObject *to_merge, *bases_aslist; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1496 | if (type->tp_dict == NULL) { |
| 1497 | if (PyType_Ready(type) < 0) |
| 1498 | return NULL; |
| 1499 | } |
Guido van Rossum | 6351757 | 2002-06-18 16:44:57 +0000 | [diff] [blame] | 1500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1501 | /* Find a superclass linearization that honors the constraints |
| 1502 | of the explicit lists of bases and the constraints implied by |
| 1503 | each base class. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1505 | to_merge is a list of lists, where each list is a superclass |
| 1506 | linearization implied by a base class. The last element of |
| 1507 | to_merge is the declared list of bases. |
| 1508 | */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1510 | bases = type->tp_bases; |
| 1511 | n = PyTuple_GET_SIZE(bases); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1512 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1513 | to_merge = PyList_New(n+1); |
| 1514 | if (to_merge == NULL) |
| 1515 | return NULL; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1516 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1517 | for (i = 0; i < n; i++) { |
| 1518 | PyObject *base = PyTuple_GET_ITEM(bases, i); |
| 1519 | PyObject *parentMRO; |
| 1520 | parentMRO = PySequence_List(((PyTypeObject*)base)->tp_mro); |
| 1521 | if (parentMRO == NULL) { |
| 1522 | Py_DECREF(to_merge); |
| 1523 | return NULL; |
| 1524 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1525 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1526 | PyList_SET_ITEM(to_merge, i, parentMRO); |
| 1527 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1528 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1529 | bases_aslist = PySequence_List(bases); |
| 1530 | if (bases_aslist == NULL) { |
| 1531 | Py_DECREF(to_merge); |
| 1532 | return NULL; |
| 1533 | } |
| 1534 | /* This is just a basic sanity check. */ |
| 1535 | if (check_duplicates(bases_aslist) < 0) { |
| 1536 | Py_DECREF(to_merge); |
| 1537 | Py_DECREF(bases_aslist); |
| 1538 | return NULL; |
| 1539 | } |
| 1540 | PyList_SET_ITEM(to_merge, n, bases_aslist); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1542 | result = Py_BuildValue("[O]", (PyObject *)type); |
| 1543 | if (result == NULL) { |
| 1544 | Py_DECREF(to_merge); |
| 1545 | return NULL; |
| 1546 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1548 | ok = pmerge(result, to_merge); |
| 1549 | Py_DECREF(to_merge); |
| 1550 | if (ok < 0) { |
| 1551 | Py_DECREF(result); |
| 1552 | return NULL; |
| 1553 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1555 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1559 | mro_external(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1560 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1561 | PyTypeObject *type = (PyTypeObject *)self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1562 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1563 | return mro_implementation(type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | static int |
| 1567 | mro_internal(PyTypeObject *type) |
| 1568 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1569 | PyObject *mro, *result, *tuple; |
| 1570 | int checkit = 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1572 | if (Py_TYPE(type) == &PyType_Type) { |
| 1573 | result = mro_implementation(type); |
| 1574 | } |
| 1575 | else { |
| 1576 | static PyObject *mro_str; |
| 1577 | checkit = 1; |
| 1578 | mro = lookup_method((PyObject *)type, "mro", &mro_str); |
| 1579 | if (mro == NULL) |
| 1580 | return -1; |
| 1581 | result = PyObject_CallObject(mro, NULL); |
| 1582 | Py_DECREF(mro); |
| 1583 | } |
| 1584 | if (result == NULL) |
| 1585 | return -1; |
| 1586 | tuple = PySequence_Tuple(result); |
| 1587 | Py_DECREF(result); |
| 1588 | if (tuple == NULL) |
| 1589 | return -1; |
| 1590 | if (checkit) { |
| 1591 | Py_ssize_t i, len; |
| 1592 | PyObject *cls; |
| 1593 | PyTypeObject *solid; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1594 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1595 | solid = solid_base(type); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1597 | len = PyTuple_GET_SIZE(tuple); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1599 | for (i = 0; i < len; i++) { |
| 1600 | PyTypeObject *t; |
| 1601 | cls = PyTuple_GET_ITEM(tuple, i); |
| 1602 | if (!PyType_Check(cls)) { |
| 1603 | PyErr_Format(PyExc_TypeError, |
| 1604 | "mro() returned a non-class ('%.500s')", |
| 1605 | Py_TYPE(cls)->tp_name); |
| 1606 | Py_DECREF(tuple); |
| 1607 | return -1; |
| 1608 | } |
| 1609 | t = (PyTypeObject*)cls; |
| 1610 | if (!PyType_IsSubtype(solid, solid_base(t))) { |
| 1611 | PyErr_Format(PyExc_TypeError, |
| 1612 | "mro() returned base with unsuitable layout ('%.500s')", |
| 1613 | t->tp_name); |
| 1614 | Py_DECREF(tuple); |
| 1615 | return -1; |
| 1616 | } |
| 1617 | } |
| 1618 | } |
| 1619 | type->tp_mro = tuple; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1621 | type_mro_modified(type, type->tp_mro); |
| 1622 | /* corner case: the old-style super class might have been hidden |
| 1623 | from the custom MRO */ |
| 1624 | type_mro_modified(type, type->tp_bases); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1625 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1626 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1627 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1628 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | |
| 1632 | /* Calculate the best base amongst multiple base classes. |
| 1633 | This is the first one that's on the path to the "solid base". */ |
| 1634 | |
| 1635 | static PyTypeObject * |
| 1636 | best_base(PyObject *bases) |
| 1637 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1638 | Py_ssize_t i, n; |
| 1639 | PyTypeObject *base, *winner, *candidate, *base_i; |
| 1640 | PyObject *base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1642 | assert(PyTuple_Check(bases)); |
| 1643 | n = PyTuple_GET_SIZE(bases); |
| 1644 | assert(n > 0); |
| 1645 | base = NULL; |
| 1646 | winner = NULL; |
| 1647 | for (i = 0; i < n; i++) { |
| 1648 | base_proto = PyTuple_GET_ITEM(bases, i); |
| 1649 | if (!PyType_Check(base_proto)) { |
| 1650 | PyErr_SetString( |
| 1651 | PyExc_TypeError, |
| 1652 | "bases must be types"); |
| 1653 | return NULL; |
| 1654 | } |
| 1655 | base_i = (PyTypeObject *)base_proto; |
| 1656 | if (base_i->tp_dict == NULL) { |
| 1657 | if (PyType_Ready(base_i) < 0) |
| 1658 | return NULL; |
| 1659 | } |
| 1660 | candidate = solid_base(base_i); |
| 1661 | if (winner == NULL) { |
| 1662 | winner = candidate; |
| 1663 | base = base_i; |
| 1664 | } |
| 1665 | else if (PyType_IsSubtype(winner, candidate)) |
| 1666 | ; |
| 1667 | else if (PyType_IsSubtype(candidate, winner)) { |
| 1668 | winner = candidate; |
| 1669 | base = base_i; |
| 1670 | } |
| 1671 | else { |
| 1672 | PyErr_SetString( |
| 1673 | PyExc_TypeError, |
| 1674 | "multiple bases have " |
| 1675 | "instance lay-out conflict"); |
| 1676 | return NULL; |
| 1677 | } |
| 1678 | } |
| 1679 | if (base == NULL) |
| 1680 | PyErr_SetString(PyExc_TypeError, |
| 1681 | "a new-style class can't have only classic bases"); |
| 1682 | return base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | static int |
| 1686 | extra_ivars(PyTypeObject *type, PyTypeObject *base) |
| 1687 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1688 | size_t t_size = type->tp_basicsize; |
| 1689 | size_t b_size = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1690 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1691 | assert(t_size >= b_size); /* Else type smaller than base! */ |
| 1692 | if (type->tp_itemsize || base->tp_itemsize) { |
| 1693 | /* If itemsize is involved, stricter rules */ |
| 1694 | return t_size != b_size || |
| 1695 | type->tp_itemsize != base->tp_itemsize; |
| 1696 | } |
| 1697 | if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 && |
| 1698 | type->tp_weaklistoffset + sizeof(PyObject *) == t_size && |
| 1699 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 1700 | t_size -= sizeof(PyObject *); |
| 1701 | if (type->tp_dictoffset && base->tp_dictoffset == 0 && |
| 1702 | type->tp_dictoffset + sizeof(PyObject *) == t_size && |
| 1703 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 1704 | t_size -= sizeof(PyObject *); |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1705 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1706 | return t_size != b_size; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | static PyTypeObject * |
| 1710 | solid_base(PyTypeObject *type) |
| 1711 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1712 | PyTypeObject *base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1713 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1714 | if (type->tp_base) |
| 1715 | base = solid_base(type->tp_base); |
| 1716 | else |
| 1717 | base = &PyBaseObject_Type; |
| 1718 | if (extra_ivars(type, base)) |
| 1719 | return type; |
| 1720 | else |
| 1721 | return base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1724 | static void object_dealloc(PyObject *); |
| 1725 | static int object_init(PyObject *, PyObject *, PyObject *); |
| 1726 | static int update_slot(PyTypeObject *, PyObject *); |
| 1727 | static void fixup_slot_dispatchers(PyTypeObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1728 | |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1729 | /* |
| 1730 | * Helpers for __dict__ descriptor. We don't want to expose the dicts |
| 1731 | * inherited from various builtin types. The builtin base usually provides |
| 1732 | * its own __dict__ descriptor, so we use that when we can. |
| 1733 | */ |
| 1734 | static PyTypeObject * |
| 1735 | get_builtin_base_with_dict(PyTypeObject *type) |
| 1736 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1737 | while (type->tp_base != NULL) { |
| 1738 | if (type->tp_dictoffset != 0 && |
| 1739 | !(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 1740 | return type; |
| 1741 | type = type->tp_base; |
| 1742 | } |
| 1743 | return NULL; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | static PyObject * |
| 1747 | get_dict_descriptor(PyTypeObject *type) |
| 1748 | { |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1749 | PyObject *dict_str; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1750 | PyObject *descr; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1751 | |
Martin v. Löwis | d10759f | 2011-11-07 13:00:05 +0100 | [diff] [blame] | 1752 | dict_str = _PyUnicode_FromId(&PyId___dict__); /* borrowed */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1753 | if (dict_str == NULL) |
| 1754 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1755 | descr = _PyType_Lookup(type, dict_str); |
| 1756 | if (descr == NULL || !PyDescr_IsData(descr)) |
| 1757 | return NULL; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1758 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1759 | return descr; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | static void |
| 1763 | raise_dict_descr_error(PyObject *obj) |
| 1764 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1765 | PyErr_Format(PyExc_TypeError, |
| 1766 | "this __dict__ descriptor does not support " |
| 1767 | "'%.200s' objects", Py_TYPE(obj)->tp_name); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1770 | static PyObject * |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1771 | subtype_dict(PyObject *obj, void *context) |
| 1772 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1773 | PyObject **dictptr; |
| 1774 | PyObject *dict; |
| 1775 | PyTypeObject *base; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1776 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1777 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
| 1778 | if (base != NULL) { |
| 1779 | descrgetfunc func; |
| 1780 | PyObject *descr = get_dict_descriptor(base); |
| 1781 | if (descr == NULL) { |
| 1782 | raise_dict_descr_error(obj); |
| 1783 | return NULL; |
| 1784 | } |
| 1785 | func = Py_TYPE(descr)->tp_descr_get; |
| 1786 | if (func == NULL) { |
| 1787 | raise_dict_descr_error(obj); |
| 1788 | return NULL; |
| 1789 | } |
| 1790 | return func(descr, obj, (PyObject *)(Py_TYPE(obj))); |
| 1791 | } |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1793 | dictptr = _PyObject_GetDictPtr(obj); |
| 1794 | if (dictptr == NULL) { |
| 1795 | PyErr_SetString(PyExc_AttributeError, |
| 1796 | "This object has no __dict__"); |
| 1797 | return NULL; |
| 1798 | } |
| 1799 | dict = *dictptr; |
| 1800 | if (dict == NULL) |
| 1801 | *dictptr = dict = PyDict_New(); |
| 1802 | Py_XINCREF(dict); |
| 1803 | return dict; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1806 | static int |
| 1807 | subtype_setdict(PyObject *obj, PyObject *value, void *context) |
| 1808 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1809 | PyObject **dictptr; |
| 1810 | PyObject *dict; |
| 1811 | PyTypeObject *base; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1812 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1813 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
| 1814 | if (base != NULL) { |
| 1815 | descrsetfunc func; |
| 1816 | PyObject *descr = get_dict_descriptor(base); |
| 1817 | if (descr == NULL) { |
| 1818 | raise_dict_descr_error(obj); |
| 1819 | return -1; |
| 1820 | } |
| 1821 | func = Py_TYPE(descr)->tp_descr_set; |
| 1822 | if (func == NULL) { |
| 1823 | raise_dict_descr_error(obj); |
| 1824 | return -1; |
| 1825 | } |
| 1826 | return func(descr, obj, value); |
| 1827 | } |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1828 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1829 | dictptr = _PyObject_GetDictPtr(obj); |
| 1830 | if (dictptr == NULL) { |
| 1831 | PyErr_SetString(PyExc_AttributeError, |
| 1832 | "This object has no __dict__"); |
| 1833 | return -1; |
| 1834 | } |
| 1835 | if (value != NULL && !PyDict_Check(value)) { |
| 1836 | PyErr_Format(PyExc_TypeError, |
| 1837 | "__dict__ must be set to a dictionary, " |
| 1838 | "not a '%.200s'", Py_TYPE(value)->tp_name); |
| 1839 | return -1; |
| 1840 | } |
| 1841 | dict = *dictptr; |
| 1842 | Py_XINCREF(value); |
| 1843 | *dictptr = value; |
| 1844 | Py_XDECREF(dict); |
| 1845 | return 0; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1848 | static PyObject * |
| 1849 | subtype_getweakref(PyObject *obj, void *context) |
| 1850 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1851 | PyObject **weaklistptr; |
| 1852 | PyObject *result; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1853 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1854 | if (Py_TYPE(obj)->tp_weaklistoffset == 0) { |
| 1855 | PyErr_SetString(PyExc_AttributeError, |
| 1856 | "This object has no __weakref__"); |
| 1857 | return NULL; |
| 1858 | } |
| 1859 | assert(Py_TYPE(obj)->tp_weaklistoffset > 0); |
| 1860 | assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <= |
| 1861 | (size_t)(Py_TYPE(obj)->tp_basicsize)); |
| 1862 | weaklistptr = (PyObject **) |
| 1863 | ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset); |
| 1864 | if (*weaklistptr == NULL) |
| 1865 | result = Py_None; |
| 1866 | else |
| 1867 | result = *weaklistptr; |
| 1868 | Py_INCREF(result); |
| 1869 | return result; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1872 | /* Three variants on the subtype_getsets list. */ |
| 1873 | |
| 1874 | static PyGetSetDef subtype_getsets_full[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1875 | {"__dict__", subtype_dict, subtype_setdict, |
| 1876 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 1877 | {"__weakref__", subtype_getweakref, NULL, |
| 1878 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 1879 | {0} |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1880 | }; |
| 1881 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1882 | static PyGetSetDef subtype_getsets_dict_only[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1883 | {"__dict__", subtype_dict, subtype_setdict, |
| 1884 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 1885 | {0} |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1886 | }; |
| 1887 | |
| 1888 | static PyGetSetDef subtype_getsets_weakref_only[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1889 | {"__weakref__", subtype_getweakref, NULL, |
| 1890 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 1891 | {0} |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1892 | }; |
| 1893 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1894 | static int |
| 1895 | valid_identifier(PyObject *s) |
| 1896 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1897 | if (!PyUnicode_Check(s)) { |
| 1898 | PyErr_Format(PyExc_TypeError, |
| 1899 | "__slots__ items must be strings, not '%.200s'", |
| 1900 | Py_TYPE(s)->tp_name); |
| 1901 | return 0; |
| 1902 | } |
| 1903 | if (!PyUnicode_IsIdentifier(s)) { |
| 1904 | PyErr_SetString(PyExc_TypeError, |
| 1905 | "__slots__ must be identifiers"); |
| 1906 | return 0; |
| 1907 | } |
| 1908 | return 1; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1911 | /* Forward */ |
| 1912 | static int |
| 1913 | object_init(PyObject *self, PyObject *args, PyObject *kwds); |
| 1914 | |
| 1915 | static int |
| 1916 | type_init(PyObject *cls, PyObject *args, PyObject *kwds) |
| 1917 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1918 | int res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1919 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1920 | assert(args != NULL && PyTuple_Check(args)); |
| 1921 | assert(kwds == NULL || PyDict_Check(kwds)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1922 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1923 | if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) { |
| 1924 | PyErr_SetString(PyExc_TypeError, |
| 1925 | "type.__init__() takes no keyword arguments"); |
| 1926 | return -1; |
| 1927 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1928 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1929 | if (args != NULL && PyTuple_Check(args) && |
| 1930 | (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) { |
| 1931 | PyErr_SetString(PyExc_TypeError, |
| 1932 | "type.__init__() takes 1 or 3 arguments"); |
| 1933 | return -1; |
| 1934 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1935 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1936 | /* Call object.__init__(self) now. */ |
| 1937 | /* XXX Could call super(type, cls).__init__() but what's the point? */ |
| 1938 | args = PyTuple_GetSlice(args, 0, 0); |
| 1939 | res = object_init(cls, args, NULL); |
| 1940 | Py_DECREF(args); |
| 1941 | return res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
Martin v. Löwis | 738236d | 2011-02-05 20:35:29 +0000 | [diff] [blame] | 1944 | long |
| 1945 | PyType_GetFlags(PyTypeObject *type) |
| 1946 | { |
| 1947 | return type->tp_flags; |
| 1948 | } |
| 1949 | |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 1950 | /* Determine the most derived metatype. */ |
| 1951 | PyTypeObject * |
| 1952 | _PyType_CalculateMetaclass(PyTypeObject *metatype, PyObject *bases) |
| 1953 | { |
| 1954 | Py_ssize_t i, nbases; |
| 1955 | PyTypeObject *winner; |
| 1956 | PyObject *tmp; |
| 1957 | PyTypeObject *tmptype; |
| 1958 | |
| 1959 | /* Determine the proper metatype to deal with this, |
| 1960 | and check for metatype conflicts while we're at it. |
| 1961 | Note that if some other metatype wins to contract, |
| 1962 | it's possible that its instances are not types. */ |
| 1963 | |
| 1964 | nbases = PyTuple_GET_SIZE(bases); |
| 1965 | winner = metatype; |
| 1966 | for (i = 0; i < nbases; i++) { |
| 1967 | tmp = PyTuple_GET_ITEM(bases, i); |
| 1968 | tmptype = Py_TYPE(tmp); |
| 1969 | if (PyType_IsSubtype(winner, tmptype)) |
| 1970 | continue; |
| 1971 | if (PyType_IsSubtype(tmptype, winner)) { |
| 1972 | winner = tmptype; |
| 1973 | continue; |
| 1974 | } |
| 1975 | /* else: */ |
| 1976 | PyErr_SetString(PyExc_TypeError, |
| 1977 | "metaclass conflict: " |
| 1978 | "the metaclass of a derived class " |
| 1979 | "must be a (non-strict) subclass " |
| 1980 | "of the metaclasses of all its bases"); |
| 1981 | return NULL; |
| 1982 | } |
| 1983 | return winner; |
| 1984 | } |
| 1985 | |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1986 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1987 | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) |
| 1988 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1989 | PyObject *name, *bases, *dict; |
| 1990 | static char *kwlist[] = {"name", "bases", "dict", 0}; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1991 | PyObject *qualname, *slots, *tmp, *newslots; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1992 | PyTypeObject *type, *base, *tmptype, *winner; |
| 1993 | PyHeapTypeObject *et; |
| 1994 | PyMemberDef *mp; |
| 1995 | Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak; |
| 1996 | int j, may_add_dict, may_add_weak; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1997 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1998 | assert(args != NULL && PyTuple_Check(args)); |
| 1999 | assert(kwds == NULL || PyDict_Check(kwds)); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2000 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2001 | /* Special case: type(x) should return x->ob_type */ |
| 2002 | { |
| 2003 | const Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 2004 | const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2005 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2006 | if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) { |
| 2007 | PyObject *x = PyTuple_GET_ITEM(args, 0); |
| 2008 | Py_INCREF(Py_TYPE(x)); |
| 2009 | return (PyObject *) Py_TYPE(x); |
| 2010 | } |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2012 | /* SF bug 475327 -- if that didn't trigger, we need 3 |
| 2013 | arguments. but PyArg_ParseTupleAndKeywords below may give |
| 2014 | a msg saying type() needs exactly 3. */ |
| 2015 | if (nargs + nkwds != 3) { |
| 2016 | PyErr_SetString(PyExc_TypeError, |
| 2017 | "type() takes 1 or 3 arguments"); |
| 2018 | return NULL; |
| 2019 | } |
| 2020 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2021 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2022 | /* Check arguments: (name, bases, dict) */ |
| 2023 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "UO!O!:type", kwlist, |
| 2024 | &name, |
| 2025 | &PyTuple_Type, &bases, |
| 2026 | &PyDict_Type, &dict)) |
| 2027 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2028 | |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 2029 | /* Determine the proper metatype to deal with this: */ |
| 2030 | winner = _PyType_CalculateMetaclass(metatype, bases); |
| 2031 | if (winner == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2032 | return NULL; |
| 2033 | } |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 2034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2035 | if (winner != metatype) { |
| 2036 | if (winner->tp_new != type_new) /* Pass it to the winner */ |
| 2037 | return winner->tp_new(winner, args, kwds); |
| 2038 | metatype = winner; |
| 2039 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2040 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2041 | /* Adjust for empty tuple bases */ |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 2042 | nbases = PyTuple_GET_SIZE(bases); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2043 | if (nbases == 0) { |
| 2044 | bases = PyTuple_Pack(1, &PyBaseObject_Type); |
| 2045 | if (bases == NULL) |
| 2046 | return NULL; |
| 2047 | nbases = 1; |
| 2048 | } |
| 2049 | else |
| 2050 | Py_INCREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2052 | /* XXX From here until type is allocated, "return NULL" leaks bases! */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2053 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2054 | /* Calculate best base, and check that all bases are type objects */ |
| 2055 | base = best_base(bases); |
| 2056 | if (base == NULL) { |
| 2057 | Py_DECREF(bases); |
| 2058 | return NULL; |
| 2059 | } |
| 2060 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 2061 | PyErr_Format(PyExc_TypeError, |
| 2062 | "type '%.100s' is not an acceptable base type", |
| 2063 | base->tp_name); |
| 2064 | Py_DECREF(bases); |
| 2065 | return NULL; |
| 2066 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2067 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2068 | /* Check for a __qualname__ variable in dict */ |
| 2069 | qualname = PyDict_GetItemString(dict, "__qualname__"); |
| 2070 | if (qualname == NULL) { |
| 2071 | qualname = name; |
| 2072 | } |
| 2073 | else { |
| 2074 | if (PyDict_DelItemString(dict, "__qualname__") < 0) { |
| 2075 | Py_DECREF(bases); |
| 2076 | return NULL; |
| 2077 | } |
| 2078 | } |
| 2079 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2080 | /* Check for a __slots__ sequence variable in dict, and count it */ |
| 2081 | slots = PyDict_GetItemString(dict, "__slots__"); |
| 2082 | nslots = 0; |
| 2083 | add_dict = 0; |
| 2084 | add_weak = 0; |
| 2085 | may_add_dict = base->tp_dictoffset == 0; |
| 2086 | may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0; |
| 2087 | if (slots == NULL) { |
| 2088 | if (may_add_dict) { |
| 2089 | add_dict++; |
| 2090 | } |
| 2091 | if (may_add_weak) { |
| 2092 | add_weak++; |
| 2093 | } |
| 2094 | } |
| 2095 | else { |
| 2096 | /* Have slots */ |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2097 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2098 | /* Make it into a tuple */ |
| 2099 | if (PyUnicode_Check(slots)) |
| 2100 | slots = PyTuple_Pack(1, slots); |
| 2101 | else |
| 2102 | slots = PySequence_Tuple(slots); |
| 2103 | if (slots == NULL) { |
| 2104 | Py_DECREF(bases); |
| 2105 | return NULL; |
| 2106 | } |
| 2107 | assert(PyTuple_Check(slots)); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2109 | /* Are slots allowed? */ |
| 2110 | nslots = PyTuple_GET_SIZE(slots); |
| 2111 | if (nslots > 0 && base->tp_itemsize != 0) { |
| 2112 | PyErr_Format(PyExc_TypeError, |
| 2113 | "nonempty __slots__ " |
| 2114 | "not supported for subtype of '%s'", |
| 2115 | base->tp_name); |
| 2116 | bad_slots: |
| 2117 | Py_DECREF(bases); |
| 2118 | Py_DECREF(slots); |
| 2119 | return NULL; |
| 2120 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2121 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2122 | /* Check for valid slot names and two special cases */ |
| 2123 | for (i = 0; i < nslots; i++) { |
| 2124 | PyObject *tmp = PyTuple_GET_ITEM(slots, i); |
| 2125 | if (!valid_identifier(tmp)) |
| 2126 | goto bad_slots; |
| 2127 | assert(PyUnicode_Check(tmp)); |
| 2128 | if (PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) { |
| 2129 | if (!may_add_dict || add_dict) { |
| 2130 | PyErr_SetString(PyExc_TypeError, |
| 2131 | "__dict__ slot disallowed: " |
| 2132 | "we already got one"); |
| 2133 | goto bad_slots; |
| 2134 | } |
| 2135 | add_dict++; |
| 2136 | } |
| 2137 | if (PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0) { |
| 2138 | if (!may_add_weak || add_weak) { |
| 2139 | PyErr_SetString(PyExc_TypeError, |
| 2140 | "__weakref__ slot disallowed: " |
| 2141 | "either we already got one, " |
| 2142 | "or __itemsize__ != 0"); |
| 2143 | goto bad_slots; |
| 2144 | } |
| 2145 | add_weak++; |
| 2146 | } |
| 2147 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2149 | /* Copy slots into a list, mangle names and sort them. |
| 2150 | Sorted names are needed for __class__ assignment. |
| 2151 | Convert them back to tuple at the end. |
| 2152 | */ |
| 2153 | newslots = PyList_New(nslots - add_dict - add_weak); |
| 2154 | if (newslots == NULL) |
| 2155 | goto bad_slots; |
| 2156 | for (i = j = 0; i < nslots; i++) { |
| 2157 | tmp = PyTuple_GET_ITEM(slots, i); |
| 2158 | if ((add_dict && |
| 2159 | PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) || |
| 2160 | (add_weak && |
| 2161 | PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0)) |
| 2162 | continue; |
| 2163 | tmp =_Py_Mangle(name, tmp); |
Benjamin Peterson | ae13c88 | 2011-08-16 22:26:48 -0500 | [diff] [blame] | 2164 | if (!tmp) { |
| 2165 | Py_DECREF(newslots); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2166 | goto bad_slots; |
Benjamin Peterson | ae13c88 | 2011-08-16 22:26:48 -0500 | [diff] [blame] | 2167 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2168 | PyList_SET_ITEM(newslots, j, tmp); |
Benjamin Peterson | c4085c8 | 2011-08-16 18:53:26 -0500 | [diff] [blame] | 2169 | if (PyDict_GetItem(dict, tmp)) { |
| 2170 | PyErr_Format(PyExc_ValueError, |
| 2171 | "%R in __slots__ conflicts with class variable", |
| 2172 | tmp); |
Benjamin Peterson | d17cefc | 2011-08-16 22:28:23 -0500 | [diff] [blame] | 2173 | Py_DECREF(newslots); |
Benjamin Peterson | c4085c8 | 2011-08-16 18:53:26 -0500 | [diff] [blame] | 2174 | goto bad_slots; |
| 2175 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2176 | j++; |
| 2177 | } |
| 2178 | assert(j == nslots - add_dict - add_weak); |
| 2179 | nslots = j; |
| 2180 | Py_DECREF(slots); |
| 2181 | if (PyList_Sort(newslots) == -1) { |
| 2182 | Py_DECREF(bases); |
| 2183 | Py_DECREF(newslots); |
| 2184 | return NULL; |
| 2185 | } |
| 2186 | slots = PyList_AsTuple(newslots); |
| 2187 | Py_DECREF(newslots); |
| 2188 | if (slots == NULL) { |
| 2189 | Py_DECREF(bases); |
| 2190 | return NULL; |
| 2191 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2192 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2193 | /* Secondary bases may provide weakrefs or dict */ |
| 2194 | if (nbases > 1 && |
| 2195 | ((may_add_dict && !add_dict) || |
| 2196 | (may_add_weak && !add_weak))) { |
| 2197 | for (i = 0; i < nbases; i++) { |
| 2198 | tmp = PyTuple_GET_ITEM(bases, i); |
| 2199 | if (tmp == (PyObject *)base) |
| 2200 | continue; /* Skip primary base */ |
| 2201 | assert(PyType_Check(tmp)); |
| 2202 | tmptype = (PyTypeObject *)tmp; |
| 2203 | if (may_add_dict && !add_dict && |
| 2204 | tmptype->tp_dictoffset != 0) |
| 2205 | add_dict++; |
| 2206 | if (may_add_weak && !add_weak && |
| 2207 | tmptype->tp_weaklistoffset != 0) |
| 2208 | add_weak++; |
| 2209 | if (may_add_dict && !add_dict) |
| 2210 | continue; |
| 2211 | if (may_add_weak && !add_weak) |
| 2212 | continue; |
| 2213 | /* Nothing more to check */ |
| 2214 | break; |
| 2215 | } |
| 2216 | } |
| 2217 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2219 | /* XXX From here until type is safely allocated, |
| 2220 | "return NULL" may leak slots! */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2222 | /* Allocate the type object */ |
| 2223 | type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots); |
| 2224 | if (type == NULL) { |
| 2225 | Py_XDECREF(slots); |
| 2226 | Py_DECREF(bases); |
| 2227 | return NULL; |
| 2228 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2230 | /* Keep name and slots alive in the extended type object */ |
| 2231 | et = (PyHeapTypeObject *)type; |
| 2232 | Py_INCREF(name); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2233 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2234 | et->ht_name = name; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2235 | et->ht_qualname = qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2236 | et->ht_slots = slots; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2238 | /* Initialize tp_flags */ |
| 2239 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | |
| 2240 | Py_TPFLAGS_BASETYPE; |
| 2241 | if (base->tp_flags & Py_TPFLAGS_HAVE_GC) |
| 2242 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2243 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2244 | /* Initialize essential fields */ |
| 2245 | type->tp_as_number = &et->as_number; |
| 2246 | type->tp_as_sequence = &et->as_sequence; |
| 2247 | type->tp_as_mapping = &et->as_mapping; |
| 2248 | type->tp_as_buffer = &et->as_buffer; |
| 2249 | type->tp_name = _PyUnicode_AsString(name); |
| 2250 | if (!type->tp_name) { |
| 2251 | Py_DECREF(type); |
| 2252 | return NULL; |
| 2253 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2255 | /* Set tp_base and tp_bases */ |
| 2256 | type->tp_bases = bases; |
| 2257 | Py_INCREF(base); |
| 2258 | type->tp_base = base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2260 | /* Initialize tp_dict from passed-in dict */ |
| 2261 | type->tp_dict = dict = PyDict_Copy(dict); |
| 2262 | if (dict == NULL) { |
| 2263 | Py_DECREF(type); |
| 2264 | return NULL; |
| 2265 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2267 | /* Set __module__ in the dict */ |
| 2268 | if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 2269 | tmp = PyEval_GetGlobals(); |
| 2270 | if (tmp != NULL) { |
| 2271 | tmp = PyDict_GetItemString(tmp, "__name__"); |
| 2272 | if (tmp != NULL) { |
| 2273 | if (PyDict_SetItemString(dict, "__module__", |
| 2274 | tmp) < 0) |
| 2275 | return NULL; |
| 2276 | } |
| 2277 | } |
| 2278 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 2279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2280 | /* Set tp_doc to a copy of dict['__doc__'], if the latter is there |
| 2281 | and is a string. The __doc__ accessor will first look for tp_doc; |
| 2282 | if that fails, it will still look into __dict__. |
| 2283 | */ |
| 2284 | { |
| 2285 | PyObject *doc = PyDict_GetItemString(dict, "__doc__"); |
| 2286 | if (doc != NULL && PyUnicode_Check(doc)) { |
| 2287 | Py_ssize_t len; |
| 2288 | char *doc_str; |
| 2289 | char *tp_doc; |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 2290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2291 | doc_str = _PyUnicode_AsString(doc); |
| 2292 | if (doc_str == NULL) { |
| 2293 | Py_DECREF(type); |
| 2294 | return NULL; |
| 2295 | } |
| 2296 | /* Silently truncate the docstring if it contains null bytes. */ |
| 2297 | len = strlen(doc_str); |
| 2298 | tp_doc = (char *)PyObject_MALLOC(len + 1); |
| 2299 | if (tp_doc == NULL) { |
| 2300 | Py_DECREF(type); |
| 2301 | return NULL; |
| 2302 | } |
| 2303 | memcpy(tp_doc, doc_str, len + 1); |
| 2304 | type->tp_doc = tp_doc; |
| 2305 | } |
| 2306 | } |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2308 | /* Special-case __new__: if it's a plain function, |
| 2309 | make it a static function */ |
| 2310 | tmp = PyDict_GetItemString(dict, "__new__"); |
| 2311 | if (tmp != NULL && PyFunction_Check(tmp)) { |
| 2312 | tmp = PyStaticMethod_New(tmp); |
| 2313 | if (tmp == NULL) { |
| 2314 | Py_DECREF(type); |
| 2315 | return NULL; |
| 2316 | } |
| 2317 | PyDict_SetItemString(dict, "__new__", tmp); |
| 2318 | Py_DECREF(tmp); |
| 2319 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2321 | /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
| 2322 | mp = PyHeapType_GET_MEMBERS(et); |
| 2323 | slotoffset = base->tp_basicsize; |
| 2324 | if (slots != NULL) { |
| 2325 | for (i = 0; i < nslots; i++, mp++) { |
| 2326 | mp->name = _PyUnicode_AsString( |
| 2327 | PyTuple_GET_ITEM(slots, i)); |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 2328 | if (mp->name == NULL) { |
| 2329 | Py_DECREF(type); |
| 2330 | return NULL; |
| 2331 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2332 | mp->type = T_OBJECT_EX; |
| 2333 | mp->offset = slotoffset; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2335 | /* __dict__ and __weakref__ are already filtered out */ |
| 2336 | assert(strcmp(mp->name, "__dict__") != 0); |
| 2337 | assert(strcmp(mp->name, "__weakref__") != 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2339 | slotoffset += sizeof(PyObject *); |
| 2340 | } |
| 2341 | } |
| 2342 | if (add_dict) { |
| 2343 | if (base->tp_itemsize) |
| 2344 | type->tp_dictoffset = -(long)sizeof(PyObject *); |
| 2345 | else |
| 2346 | type->tp_dictoffset = slotoffset; |
| 2347 | slotoffset += sizeof(PyObject *); |
| 2348 | } |
| 2349 | if (add_weak) { |
| 2350 | assert(!base->tp_itemsize); |
| 2351 | type->tp_weaklistoffset = slotoffset; |
| 2352 | slotoffset += sizeof(PyObject *); |
| 2353 | } |
| 2354 | type->tp_basicsize = slotoffset; |
| 2355 | type->tp_itemsize = base->tp_itemsize; |
| 2356 | type->tp_members = PyHeapType_GET_MEMBERS(et); |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2357 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2358 | if (type->tp_weaklistoffset && type->tp_dictoffset) |
| 2359 | type->tp_getset = subtype_getsets_full; |
| 2360 | else if (type->tp_weaklistoffset && !type->tp_dictoffset) |
| 2361 | type->tp_getset = subtype_getsets_weakref_only; |
| 2362 | else if (!type->tp_weaklistoffset && type->tp_dictoffset) |
| 2363 | type->tp_getset = subtype_getsets_dict_only; |
| 2364 | else |
| 2365 | type->tp_getset = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2366 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2367 | /* Special case some slots */ |
| 2368 | if (type->tp_dictoffset != 0 || nslots > 0) { |
| 2369 | if (base->tp_getattr == NULL && base->tp_getattro == NULL) |
| 2370 | type->tp_getattro = PyObject_GenericGetAttr; |
| 2371 | if (base->tp_setattr == NULL && base->tp_setattro == NULL) |
| 2372 | type->tp_setattro = PyObject_GenericSetAttr; |
| 2373 | } |
| 2374 | type->tp_dealloc = subtype_dealloc; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2375 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2376 | /* Enable GC unless there are really no instance variables possible */ |
| 2377 | if (!(type->tp_basicsize == sizeof(PyObject) && |
| 2378 | type->tp_itemsize == 0)) |
| 2379 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 2380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2381 | /* Always override allocation strategy to use regular heap */ |
| 2382 | type->tp_alloc = PyType_GenericAlloc; |
| 2383 | if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { |
| 2384 | type->tp_free = PyObject_GC_Del; |
| 2385 | type->tp_traverse = subtype_traverse; |
| 2386 | type->tp_clear = subtype_clear; |
| 2387 | } |
| 2388 | else |
| 2389 | type->tp_free = PyObject_Del; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2390 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2391 | /* Initialize the rest */ |
| 2392 | if (PyType_Ready(type) < 0) { |
| 2393 | Py_DECREF(type); |
| 2394 | return NULL; |
| 2395 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2397 | /* Put the proper slots in place */ |
| 2398 | fixup_slot_dispatchers(type); |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 2399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2400 | return (PyObject *)type; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2403 | static short slotoffsets[] = { |
| 2404 | -1, /* invalid slot */ |
| 2405 | #include "typeslots.inc" |
| 2406 | }; |
| 2407 | |
| 2408 | PyObject* PyType_FromSpec(PyType_Spec *spec) |
| 2409 | { |
| 2410 | PyHeapTypeObject *res = (PyHeapTypeObject*)PyType_GenericAlloc(&PyType_Type, 0); |
| 2411 | char *res_start = (char*)res; |
| 2412 | PyType_Slot *slot; |
| 2413 | |
Martin v. Löwis | 5e06a5d | 2011-02-21 16:24:00 +0000 | [diff] [blame] | 2414 | if (res == NULL) |
| 2415 | return NULL; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2416 | res->ht_name = PyUnicode_FromString(spec->name); |
| 2417 | if (!res->ht_name) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2418 | goto fail; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2419 | res->ht_qualname = res->ht_name; |
| 2420 | Py_INCREF(res->ht_qualname); |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2421 | res->ht_type.tp_name = _PyUnicode_AsString(res->ht_name); |
| 2422 | if (!res->ht_type.tp_name) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2423 | goto fail; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2424 | |
| 2425 | res->ht_type.tp_basicsize = spec->basicsize; |
| 2426 | res->ht_type.tp_itemsize = spec->itemsize; |
| 2427 | res->ht_type.tp_flags = spec->flags | Py_TPFLAGS_HEAPTYPE; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 2428 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2429 | for (slot = spec->slots; slot->slot; slot++) { |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2430 | if (slot->slot >= Py_ARRAY_LENGTH(slotoffsets)) { |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2431 | PyErr_SetString(PyExc_RuntimeError, "invalid slot offset"); |
| 2432 | goto fail; |
| 2433 | } |
| 2434 | *(void**)(res_start + slotoffsets[slot->slot]) = slot->pfunc; |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2435 | |
| 2436 | /* need to make a copy of the docstring slot, which usually |
| 2437 | points to a static string literal */ |
| 2438 | if (slot->slot == Py_tp_doc) { |
| 2439 | ssize_t len = strlen(slot->pfunc)+1; |
| 2440 | char *tp_doc = PyObject_MALLOC(len); |
| 2441 | if (tp_doc == NULL) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2442 | goto fail; |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2443 | memcpy(tp_doc, slot->pfunc, len); |
| 2444 | res->ht_type.tp_doc = tp_doc; |
| 2445 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | return (PyObject*)res; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 2449 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2450 | fail: |
| 2451 | Py_DECREF(res); |
| 2452 | return NULL; |
| 2453 | } |
| 2454 | |
| 2455 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2456 | /* Internal API to look for a name through the MRO. |
| 2457 | This returns a borrowed reference, and doesn't set an exception! */ |
| 2458 | PyObject * |
| 2459 | _PyType_Lookup(PyTypeObject *type, PyObject *name) |
| 2460 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2461 | Py_ssize_t i, n; |
| 2462 | PyObject *mro, *res, *base, *dict; |
| 2463 | unsigned int h; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 2464 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2465 | if (MCACHE_CACHEABLE_NAME(name) && |
| 2466 | PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) { |
| 2467 | /* fast path */ |
| 2468 | h = MCACHE_HASH_METHOD(type, name); |
| 2469 | if (method_cache[h].version == type->tp_version_tag && |
| 2470 | method_cache[h].name == name) |
| 2471 | return method_cache[h].value; |
| 2472 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2474 | /* Look in tp_dict of types in MRO */ |
| 2475 | mro = type->tp_mro; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 2476 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2477 | /* If mro is NULL, the type is either not yet initialized |
| 2478 | by PyType_Ready(), or already cleared by type_clear(). |
| 2479 | Either way the safest thing to do is to return NULL. */ |
| 2480 | if (mro == NULL) |
| 2481 | return NULL; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 2482 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2483 | res = NULL; |
| 2484 | assert(PyTuple_Check(mro)); |
| 2485 | n = PyTuple_GET_SIZE(mro); |
| 2486 | for (i = 0; i < n; i++) { |
| 2487 | base = PyTuple_GET_ITEM(mro, i); |
| 2488 | assert(PyType_Check(base)); |
| 2489 | dict = ((PyTypeObject *)base)->tp_dict; |
| 2490 | assert(dict && PyDict_Check(dict)); |
| 2491 | res = PyDict_GetItem(dict, name); |
| 2492 | if (res != NULL) |
| 2493 | break; |
| 2494 | } |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 2495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2496 | if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) { |
| 2497 | h = MCACHE_HASH_METHOD(type, name); |
| 2498 | method_cache[h].version = type->tp_version_tag; |
| 2499 | method_cache[h].value = res; /* borrowed */ |
| 2500 | Py_INCREF(name); |
| 2501 | Py_DECREF(method_cache[h].name); |
| 2502 | method_cache[h].name = name; |
| 2503 | } |
| 2504 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
| 2507 | /* This is similar to PyObject_GenericGetAttr(), |
| 2508 | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ |
| 2509 | static PyObject * |
| 2510 | type_getattro(PyTypeObject *type, PyObject *name) |
| 2511 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2512 | PyTypeObject *metatype = Py_TYPE(type); |
| 2513 | PyObject *meta_attribute, *attribute; |
| 2514 | descrgetfunc meta_get; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2516 | /* Initialize this type (we'll assume the metatype is initialized) */ |
| 2517 | if (type->tp_dict == NULL) { |
| 2518 | if (PyType_Ready(type) < 0) |
| 2519 | return NULL; |
| 2520 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2521 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2522 | /* No readable descriptor found yet */ |
| 2523 | meta_get = NULL; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2524 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2525 | /* Look for the attribute in the metatype */ |
| 2526 | meta_attribute = _PyType_Lookup(metatype, name); |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2527 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2528 | if (meta_attribute != NULL) { |
| 2529 | meta_get = Py_TYPE(meta_attribute)->tp_descr_get; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2530 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2531 | if (meta_get != NULL && PyDescr_IsData(meta_attribute)) { |
| 2532 | /* Data descriptors implement tp_descr_set to intercept |
| 2533 | * writes. Assume the attribute is not overridden in |
| 2534 | * type's tp_dict (and bases): call the descriptor now. |
| 2535 | */ |
| 2536 | return meta_get(meta_attribute, (PyObject *)type, |
| 2537 | (PyObject *)metatype); |
| 2538 | } |
| 2539 | Py_INCREF(meta_attribute); |
| 2540 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2542 | /* No data descriptor found on metatype. Look in tp_dict of this |
| 2543 | * type and its bases */ |
| 2544 | attribute = _PyType_Lookup(type, name); |
| 2545 | if (attribute != NULL) { |
| 2546 | /* Implement descriptor functionality, if any */ |
| 2547 | descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get; |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2549 | Py_XDECREF(meta_attribute); |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2551 | if (local_get != NULL) { |
| 2552 | /* NULL 2nd argument indicates the descriptor was |
| 2553 | * found on the target object itself (or a base) */ |
| 2554 | return local_get(attribute, (PyObject *)NULL, |
| 2555 | (PyObject *)type); |
| 2556 | } |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2557 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2558 | Py_INCREF(attribute); |
| 2559 | return attribute; |
| 2560 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2561 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2562 | /* No attribute found in local __dict__ (or bases): use the |
| 2563 | * descriptor from the metatype, if any */ |
| 2564 | if (meta_get != NULL) { |
| 2565 | PyObject *res; |
| 2566 | res = meta_get(meta_attribute, (PyObject *)type, |
| 2567 | (PyObject *)metatype); |
| 2568 | Py_DECREF(meta_attribute); |
| 2569 | return res; |
| 2570 | } |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2572 | /* If an ordinary attribute was found on the metatype, return it now */ |
| 2573 | if (meta_attribute != NULL) { |
| 2574 | return meta_attribute; |
| 2575 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2576 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2577 | /* Give up */ |
| 2578 | PyErr_Format(PyExc_AttributeError, |
| 2579 | "type object '%.50s' has no attribute '%U'", |
| 2580 | type->tp_name, name); |
| 2581 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
| 2584 | static int |
| 2585 | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) |
| 2586 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2587 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 2588 | PyErr_Format( |
| 2589 | PyExc_TypeError, |
| 2590 | "can't set attributes of built-in/extension type '%s'", |
| 2591 | type->tp_name); |
| 2592 | return -1; |
| 2593 | } |
| 2594 | if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0) |
| 2595 | return -1; |
| 2596 | return update_slot(type, name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2597 | } |
| 2598 | |
| 2599 | static void |
| 2600 | type_dealloc(PyTypeObject *type) |
| 2601 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2602 | PyHeapTypeObject *et; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2604 | /* Assert this is a heap-allocated type object */ |
| 2605 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
| 2606 | _PyObject_GC_UNTRACK(type); |
| 2607 | PyObject_ClearWeakRefs((PyObject *)type); |
| 2608 | et = (PyHeapTypeObject *)type; |
| 2609 | Py_XDECREF(type->tp_base); |
| 2610 | Py_XDECREF(type->tp_dict); |
| 2611 | Py_XDECREF(type->tp_bases); |
| 2612 | Py_XDECREF(type->tp_mro); |
| 2613 | Py_XDECREF(type->tp_cache); |
| 2614 | Py_XDECREF(type->tp_subclasses); |
| 2615 | /* A type's tp_doc is heap allocated, unlike the tp_doc slots |
| 2616 | * of most other objects. It's okay to cast it to char *. |
| 2617 | */ |
| 2618 | PyObject_Free((char *)type->tp_doc); |
| 2619 | Py_XDECREF(et->ht_name); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2620 | Py_XDECREF(et->ht_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2621 | Py_XDECREF(et->ht_slots); |
| 2622 | Py_TYPE(type)->tp_free((PyObject *)type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2623 | } |
| 2624 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2625 | static PyObject * |
| 2626 | type_subclasses(PyTypeObject *type, PyObject *args_ignored) |
| 2627 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2628 | PyObject *list, *raw, *ref; |
| 2629 | Py_ssize_t i, n; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2630 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2631 | list = PyList_New(0); |
| 2632 | if (list == NULL) |
| 2633 | return NULL; |
| 2634 | raw = type->tp_subclasses; |
| 2635 | if (raw == NULL) |
| 2636 | return list; |
| 2637 | assert(PyList_Check(raw)); |
| 2638 | n = PyList_GET_SIZE(raw); |
| 2639 | for (i = 0; i < n; i++) { |
| 2640 | ref = PyList_GET_ITEM(raw, i); |
| 2641 | assert(PyWeakref_CheckRef(ref)); |
| 2642 | ref = PyWeakref_GET_OBJECT(ref); |
| 2643 | if (ref != Py_None) { |
| 2644 | if (PyList_Append(list, ref) < 0) { |
| 2645 | Py_DECREF(list); |
| 2646 | return NULL; |
| 2647 | } |
| 2648 | } |
| 2649 | } |
| 2650 | return list; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
Guido van Rossum | 4737482 | 2007-08-02 16:48:17 +0000 | [diff] [blame] | 2653 | static PyObject * |
| 2654 | type_prepare(PyObject *self, PyObject *args, PyObject *kwds) |
| 2655 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2656 | return PyDict_New(); |
Guido van Rossum | 4737482 | 2007-08-02 16:48:17 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2659 | /* |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2660 | Merge the __dict__ of aclass into dict, and recursively also all |
| 2661 | the __dict__s of aclass's base classes. The order of merging isn't |
| 2662 | defined, as it's expected that only the final set of dict keys is |
| 2663 | interesting. |
| 2664 | Return 0 on success, -1 on error. |
| 2665 | */ |
| 2666 | |
| 2667 | static int |
| 2668 | merge_class_dict(PyObject *dict, PyObject *aclass) |
| 2669 | { |
| 2670 | PyObject *classdict; |
| 2671 | PyObject *bases; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2672 | _Py_IDENTIFIER(__bases__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2673 | |
| 2674 | assert(PyDict_Check(dict)); |
| 2675 | assert(aclass); |
| 2676 | |
| 2677 | /* Merge in the type's dict (if any). */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2678 | classdict = _PyObject_GetAttrId(aclass, &PyId___dict__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2679 | if (classdict == NULL) |
| 2680 | PyErr_Clear(); |
| 2681 | else { |
| 2682 | int status = PyDict_Update(dict, classdict); |
| 2683 | Py_DECREF(classdict); |
| 2684 | if (status < 0) |
| 2685 | return -1; |
| 2686 | } |
| 2687 | |
| 2688 | /* Recursively merge in the base types' (if any) dicts. */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2689 | bases = _PyObject_GetAttrId(aclass, &PyId___bases__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2690 | if (bases == NULL) |
| 2691 | PyErr_Clear(); |
| 2692 | else { |
| 2693 | /* We have no guarantee that bases is a real tuple */ |
| 2694 | Py_ssize_t i, n; |
| 2695 | n = PySequence_Size(bases); /* This better be right */ |
| 2696 | if (n < 0) |
| 2697 | PyErr_Clear(); |
| 2698 | else { |
| 2699 | for (i = 0; i < n; i++) { |
| 2700 | int status; |
| 2701 | PyObject *base = PySequence_GetItem(bases, i); |
| 2702 | if (base == NULL) { |
| 2703 | Py_DECREF(bases); |
| 2704 | return -1; |
| 2705 | } |
| 2706 | status = merge_class_dict(dict, base); |
| 2707 | Py_DECREF(base); |
| 2708 | if (status < 0) { |
| 2709 | Py_DECREF(bases); |
| 2710 | return -1; |
| 2711 | } |
| 2712 | } |
| 2713 | } |
| 2714 | Py_DECREF(bases); |
| 2715 | } |
| 2716 | return 0; |
| 2717 | } |
| 2718 | |
| 2719 | /* __dir__ for type objects: returns __dict__ and __bases__. |
| 2720 | We deliberately don't suck up its __class__, as methods belonging to the |
| 2721 | metaclass would probably be more confusing than helpful. |
| 2722 | */ |
| 2723 | static PyObject * |
| 2724 | type_dir(PyObject *self, PyObject *args) |
| 2725 | { |
| 2726 | PyObject *result = NULL; |
| 2727 | PyObject *dict = PyDict_New(); |
| 2728 | |
| 2729 | if (dict != NULL && merge_class_dict(dict, self) == 0) |
| 2730 | result = PyDict_Keys(dict); |
| 2731 | |
| 2732 | Py_XDECREF(dict); |
| 2733 | return result; |
| 2734 | } |
| 2735 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2736 | static PyMethodDef type_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2737 | {"mro", (PyCFunction)mro_external, METH_NOARGS, |
| 2738 | PyDoc_STR("mro() -> list\nreturn a type's method resolution order")}, |
| 2739 | {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS, |
| 2740 | PyDoc_STR("__subclasses__() -> list of immediate subclasses")}, |
| 2741 | {"__prepare__", (PyCFunction)type_prepare, |
| 2742 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 2743 | PyDoc_STR("__prepare__() -> dict\n" |
| 2744 | "used to create the namespace for the class statement")}, |
| 2745 | {"__instancecheck__", type___instancecheck__, METH_O, |
Benjamin Peterson | fbe56bb | 2011-05-24 12:42:51 -0500 | [diff] [blame] | 2746 | PyDoc_STR("__instancecheck__() -> bool\ncheck if an object is an instance")}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2747 | {"__subclasscheck__", type___subclasscheck__, METH_O, |
Benjamin Peterson | fbe56bb | 2011-05-24 12:42:51 -0500 | [diff] [blame] | 2748 | PyDoc_STR("__subclasscheck__() -> bool\ncheck if a class is a subclass")}, |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2749 | {"__dir__", type_dir, METH_NOARGS, |
Benjamin Peterson | c728412 | 2011-05-24 12:46:15 -0500 | [diff] [blame] | 2750 | PyDoc_STR("__dir__() -> list\nspecialized __dir__ implementation for types")}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2751 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2752 | }; |
| 2753 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2754 | PyDoc_STRVAR(type_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2755 | "type(object) -> the object's type\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2756 | "type(name, bases, dict) -> a new type"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2757 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2758 | static int |
| 2759 | type_traverse(PyTypeObject *type, visitproc visit, void *arg) |
| 2760 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2761 | /* Because of type_is_gc(), the collector only calls this |
| 2762 | for heaptypes. */ |
| 2763 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2764 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2765 | Py_VISIT(type->tp_dict); |
| 2766 | Py_VISIT(type->tp_cache); |
| 2767 | Py_VISIT(type->tp_mro); |
| 2768 | Py_VISIT(type->tp_bases); |
| 2769 | Py_VISIT(type->tp_base); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2771 | /* There's no need to visit type->tp_subclasses or |
| 2772 | ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved |
| 2773 | in cycles; tp_subclasses is a list of weak references, |
| 2774 | and slots is a tuple of strings. */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2775 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2776 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | static int |
| 2780 | type_clear(PyTypeObject *type) |
| 2781 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2782 | /* Because of type_is_gc(), the collector only calls this |
| 2783 | for heaptypes. */ |
| 2784 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2785 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2786 | /* The only field we need to clear is tp_mro, which is part of a |
| 2787 | hard cycle (its first element is the class itself) that won't |
| 2788 | be broken otherwise (it's a tuple and tuples don't have a |
| 2789 | tp_clear handler). None of the other fields need to be |
| 2790 | cleared, and here's why: |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2791 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2792 | tp_dict: |
| 2793 | It is a dict, so the collector will call its tp_clear. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2794 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2795 | tp_cache: |
| 2796 | Not used; if it were, it would be a dict. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2797 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2798 | tp_bases, tp_base: |
| 2799 | If these are involved in a cycle, there must be at least |
| 2800 | one other, mutable object in the cycle, e.g. a base |
| 2801 | class's dict; the cycle will be broken that way. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2803 | tp_subclasses: |
| 2804 | A list of weak references can't be part of a cycle; and |
| 2805 | lists have their own tp_clear. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2806 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2807 | slots (in PyHeapTypeObject): |
| 2808 | A tuple of strings can't be part of a cycle. |
| 2809 | */ |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2810 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2811 | Py_CLEAR(type->tp_mro); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2812 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2813 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2814 | } |
| 2815 | |
| 2816 | static int |
| 2817 | type_is_gc(PyTypeObject *type) |
| 2818 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2819 | return type->tp_flags & Py_TPFLAGS_HEAPTYPE; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2822 | PyTypeObject PyType_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2823 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2824 | "type", /* tp_name */ |
| 2825 | sizeof(PyHeapTypeObject), /* tp_basicsize */ |
| 2826 | sizeof(PyMemberDef), /* tp_itemsize */ |
| 2827 | (destructor)type_dealloc, /* tp_dealloc */ |
| 2828 | 0, /* tp_print */ |
| 2829 | 0, /* tp_getattr */ |
| 2830 | 0, /* tp_setattr */ |
| 2831 | 0, /* tp_reserved */ |
| 2832 | (reprfunc)type_repr, /* tp_repr */ |
| 2833 | 0, /* tp_as_number */ |
| 2834 | 0, /* tp_as_sequence */ |
| 2835 | 0, /* tp_as_mapping */ |
| 2836 | 0, /* tp_hash */ |
| 2837 | (ternaryfunc)type_call, /* tp_call */ |
| 2838 | 0, /* tp_str */ |
| 2839 | (getattrofunc)type_getattro, /* tp_getattro */ |
| 2840 | (setattrofunc)type_setattro, /* tp_setattro */ |
| 2841 | 0, /* tp_as_buffer */ |
| 2842 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2843 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */ |
| 2844 | type_doc, /* tp_doc */ |
| 2845 | (traverseproc)type_traverse, /* tp_traverse */ |
| 2846 | (inquiry)type_clear, /* tp_clear */ |
| 2847 | 0, /* tp_richcompare */ |
| 2848 | offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ |
| 2849 | 0, /* tp_iter */ |
| 2850 | 0, /* tp_iternext */ |
| 2851 | type_methods, /* tp_methods */ |
| 2852 | type_members, /* tp_members */ |
| 2853 | type_getsets, /* tp_getset */ |
| 2854 | 0, /* tp_base */ |
| 2855 | 0, /* tp_dict */ |
| 2856 | 0, /* tp_descr_get */ |
| 2857 | 0, /* tp_descr_set */ |
| 2858 | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ |
| 2859 | type_init, /* tp_init */ |
| 2860 | 0, /* tp_alloc */ |
| 2861 | type_new, /* tp_new */ |
| 2862 | PyObject_GC_Del, /* tp_free */ |
| 2863 | (inquiry)type_is_gc, /* tp_is_gc */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2864 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2865 | |
| 2866 | |
| 2867 | /* The base type of all types (eventually)... except itself. */ |
| 2868 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2869 | /* You may wonder why object.__new__() only complains about arguments |
| 2870 | when object.__init__() is not overridden, and vice versa. |
| 2871 | |
| 2872 | Consider the use cases: |
| 2873 | |
| 2874 | 1. When neither is overridden, we want to hear complaints about |
| 2875 | excess (i.e., any) arguments, since their presence could |
| 2876 | indicate there's a bug. |
| 2877 | |
| 2878 | 2. When defining an Immutable type, we are likely to override only |
| 2879 | __new__(), since __init__() is called too late to initialize an |
| 2880 | Immutable object. Since __new__() defines the signature for the |
| 2881 | type, it would be a pain to have to override __init__() just to |
| 2882 | stop it from complaining about excess arguments. |
| 2883 | |
| 2884 | 3. When defining a Mutable type, we are likely to override only |
| 2885 | __init__(). So here the converse reasoning applies: we don't |
| 2886 | want to have to override __new__() just to stop it from |
| 2887 | complaining. |
| 2888 | |
| 2889 | 4. When __init__() is overridden, and the subclass __init__() calls |
| 2890 | object.__init__(), the latter should complain about excess |
| 2891 | arguments; ditto for __new__(). |
| 2892 | |
| 2893 | Use cases 2 and 3 make it unattractive to unconditionally check for |
| 2894 | excess arguments. The best solution that addresses all four use |
| 2895 | cases is as follows: __init__() complains about excess arguments |
| 2896 | unless __new__() is overridden and __init__() is not overridden |
| 2897 | (IOW, if __init__() is overridden or __new__() is not overridden); |
| 2898 | symmetrically, __new__() complains about excess arguments unless |
| 2899 | __init__() is overridden and __new__() is not overridden |
| 2900 | (IOW, if __new__() is overridden or __init__() is not overridden). |
| 2901 | |
| 2902 | However, for backwards compatibility, this breaks too much code. |
| 2903 | Therefore, in 2.6, we'll *warn* about excess arguments when both |
| 2904 | methods are overridden; for all other cases we'll use the above |
| 2905 | rules. |
| 2906 | |
| 2907 | */ |
| 2908 | |
| 2909 | /* Forward */ |
| 2910 | static PyObject * |
| 2911 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 2912 | |
| 2913 | static int |
| 2914 | excess_args(PyObject *args, PyObject *kwds) |
| 2915 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2916 | return PyTuple_GET_SIZE(args) || |
| 2917 | (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2918 | } |
| 2919 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2920 | static int |
| 2921 | object_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2922 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2923 | int err = 0; |
| 2924 | if (excess_args(args, kwds)) { |
| 2925 | PyTypeObject *type = Py_TYPE(self); |
| 2926 | if (type->tp_init != object_init && |
| 2927 | type->tp_new != object_new) |
| 2928 | { |
| 2929 | err = PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2930 | "object.__init__() takes no parameters", |
| 2931 | 1); |
| 2932 | } |
| 2933 | else if (type->tp_init != object_init || |
| 2934 | type->tp_new == object_new) |
| 2935 | { |
| 2936 | PyErr_SetString(PyExc_TypeError, |
| 2937 | "object.__init__() takes no parameters"); |
| 2938 | err = -1; |
| 2939 | } |
| 2940 | } |
| 2941 | return err; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2942 | } |
| 2943 | |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2944 | static PyObject * |
| 2945 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2946 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2947 | int err = 0; |
| 2948 | if (excess_args(args, kwds)) { |
| 2949 | if (type->tp_new != object_new && |
| 2950 | type->tp_init != object_init) |
| 2951 | { |
| 2952 | err = PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2953 | "object.__new__() takes no parameters", |
| 2954 | 1); |
| 2955 | } |
| 2956 | else if (type->tp_new != object_new || |
| 2957 | type->tp_init == object_init) |
| 2958 | { |
| 2959 | PyErr_SetString(PyExc_TypeError, |
| 2960 | "object.__new__() takes no parameters"); |
| 2961 | err = -1; |
| 2962 | } |
| 2963 | } |
| 2964 | if (err < 0) |
| 2965 | return NULL; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 2966 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2967 | if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) { |
| 2968 | static PyObject *comma = NULL; |
| 2969 | PyObject *abstract_methods = NULL; |
| 2970 | PyObject *builtins; |
| 2971 | PyObject *sorted; |
| 2972 | PyObject *sorted_methods = NULL; |
| 2973 | PyObject *joined = NULL; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2974 | _Py_IDENTIFIER(join); |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 2975 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2976 | /* Compute ", ".join(sorted(type.__abstractmethods__)) |
| 2977 | into joined. */ |
| 2978 | abstract_methods = type_abstractmethods(type, NULL); |
| 2979 | if (abstract_methods == NULL) |
| 2980 | goto error; |
| 2981 | builtins = PyEval_GetBuiltins(); |
| 2982 | if (builtins == NULL) |
| 2983 | goto error; |
| 2984 | sorted = PyDict_GetItemString(builtins, "sorted"); |
| 2985 | if (sorted == NULL) |
| 2986 | goto error; |
| 2987 | sorted_methods = PyObject_CallFunctionObjArgs(sorted, |
| 2988 | abstract_methods, |
| 2989 | NULL); |
| 2990 | if (sorted_methods == NULL) |
| 2991 | goto error; |
| 2992 | if (comma == NULL) { |
| 2993 | comma = PyUnicode_InternFromString(", "); |
| 2994 | if (comma == NULL) |
| 2995 | goto error; |
| 2996 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2997 | joined = _PyObject_CallMethodId(comma, &PyId_join, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2998 | "O", sorted_methods); |
| 2999 | if (joined == NULL) |
| 3000 | goto error; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3001 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3002 | PyErr_Format(PyExc_TypeError, |
| 3003 | "Can't instantiate abstract class %s " |
| 3004 | "with abstract methods %U", |
| 3005 | type->tp_name, |
| 3006 | joined); |
| 3007 | error: |
| 3008 | Py_XDECREF(joined); |
| 3009 | Py_XDECREF(sorted_methods); |
| 3010 | Py_XDECREF(abstract_methods); |
| 3011 | return NULL; |
| 3012 | } |
| 3013 | return type->tp_alloc(type, 0); |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 3014 | } |
| 3015 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3016 | static void |
| 3017 | object_dealloc(PyObject *self) |
| 3018 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3019 | Py_TYPE(self)->tp_free(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 3022 | static PyObject * |
| 3023 | object_repr(PyObject *self) |
| 3024 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3025 | PyTypeObject *type; |
| 3026 | PyObject *mod, *name, *rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 3027 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3028 | type = Py_TYPE(self); |
| 3029 | mod = type_module(type, NULL); |
| 3030 | if (mod == NULL) |
| 3031 | PyErr_Clear(); |
| 3032 | else if (!PyUnicode_Check(mod)) { |
| 3033 | Py_DECREF(mod); |
| 3034 | mod = NULL; |
| 3035 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 3036 | name = type_qualname(type, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3037 | if (name == NULL) |
| 3038 | return NULL; |
| 3039 | if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins")) |
| 3040 | rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self); |
| 3041 | else |
| 3042 | rtn = PyUnicode_FromFormat("<%s object at %p>", |
| 3043 | type->tp_name, self); |
| 3044 | Py_XDECREF(mod); |
| 3045 | Py_DECREF(name); |
| 3046 | return rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 3047 | } |
| 3048 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3049 | static PyObject * |
| 3050 | object_str(PyObject *self) |
| 3051 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3052 | unaryfunc f; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3053 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3054 | f = Py_TYPE(self)->tp_repr; |
Antoine Pitrou | 8cdc40e | 2011-07-15 21:15:07 +0200 | [diff] [blame] | 3055 | if (f == NULL || f == object_str) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3056 | f = object_repr; |
| 3057 | return f(self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3060 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3061 | object_richcompare(PyObject *self, PyObject *other, int op) |
| 3062 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3063 | PyObject *res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3065 | switch (op) { |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3066 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3067 | case Py_EQ: |
| 3068 | /* Return NotImplemented instead of False, so if two |
| 3069 | objects are compared, both get a chance at the |
| 3070 | comparison. See issue #1393. */ |
| 3071 | res = (self == other) ? Py_True : Py_NotImplemented; |
| 3072 | Py_INCREF(res); |
| 3073 | break; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3075 | case Py_NE: |
| 3076 | /* By default, != returns the opposite of ==, |
| 3077 | unless the latter returns NotImplemented. */ |
| 3078 | res = PyObject_RichCompare(self, other, Py_EQ); |
| 3079 | if (res != NULL && res != Py_NotImplemented) { |
| 3080 | int ok = PyObject_IsTrue(res); |
| 3081 | Py_DECREF(res); |
| 3082 | if (ok < 0) |
| 3083 | res = NULL; |
| 3084 | else { |
| 3085 | if (ok) |
| 3086 | res = Py_False; |
| 3087 | else |
| 3088 | res = Py_True; |
| 3089 | Py_INCREF(res); |
| 3090 | } |
| 3091 | } |
| 3092 | break; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3093 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3094 | default: |
| 3095 | res = Py_NotImplemented; |
| 3096 | Py_INCREF(res); |
| 3097 | break; |
| 3098 | } |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3099 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3100 | return res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
| 3103 | static PyObject * |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3104 | object_get_class(PyObject *self, void *closure) |
| 3105 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3106 | Py_INCREF(Py_TYPE(self)); |
| 3107 | return (PyObject *)(Py_TYPE(self)); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3108 | } |
| 3109 | |
| 3110 | static int |
| 3111 | equiv_structs(PyTypeObject *a, PyTypeObject *b) |
| 3112 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3113 | return a == b || |
| 3114 | (a != NULL && |
| 3115 | b != NULL && |
| 3116 | a->tp_basicsize == b->tp_basicsize && |
| 3117 | a->tp_itemsize == b->tp_itemsize && |
| 3118 | a->tp_dictoffset == b->tp_dictoffset && |
| 3119 | a->tp_weaklistoffset == b->tp_weaklistoffset && |
| 3120 | ((a->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 3121 | (b->tp_flags & Py_TPFLAGS_HAVE_GC))); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3122 | } |
| 3123 | |
| 3124 | static int |
| 3125 | same_slots_added(PyTypeObject *a, PyTypeObject *b) |
| 3126 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3127 | PyTypeObject *base = a->tp_base; |
| 3128 | Py_ssize_t size; |
| 3129 | PyObject *slots_a, *slots_b; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3130 | |
Benjamin Peterson | 67641d2 | 2011-01-17 19:24:34 +0000 | [diff] [blame] | 3131 | assert(base == b->tp_base); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3132 | size = base->tp_basicsize; |
| 3133 | if (a->tp_dictoffset == size && b->tp_dictoffset == size) |
| 3134 | size += sizeof(PyObject *); |
| 3135 | if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size) |
| 3136 | size += sizeof(PyObject *); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3137 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3138 | /* Check slots compliance */ |
| 3139 | slots_a = ((PyHeapTypeObject *)a)->ht_slots; |
| 3140 | slots_b = ((PyHeapTypeObject *)b)->ht_slots; |
| 3141 | if (slots_a && slots_b) { |
| 3142 | if (PyObject_RichCompareBool(slots_a, slots_b, Py_EQ) != 1) |
| 3143 | return 0; |
| 3144 | size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a); |
| 3145 | } |
| 3146 | return size == a->tp_basicsize && size == b->tp_basicsize; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
| 3149 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3150 | compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3151 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3152 | PyTypeObject *newbase, *oldbase; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3153 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3154 | if (newto->tp_dealloc != oldto->tp_dealloc || |
| 3155 | newto->tp_free != oldto->tp_free) |
| 3156 | { |
| 3157 | PyErr_Format(PyExc_TypeError, |
| 3158 | "%s assignment: " |
| 3159 | "'%s' deallocator differs from '%s'", |
| 3160 | attr, |
| 3161 | newto->tp_name, |
| 3162 | oldto->tp_name); |
| 3163 | return 0; |
| 3164 | } |
| 3165 | newbase = newto; |
| 3166 | oldbase = oldto; |
| 3167 | while (equiv_structs(newbase, newbase->tp_base)) |
| 3168 | newbase = newbase->tp_base; |
| 3169 | while (equiv_structs(oldbase, oldbase->tp_base)) |
| 3170 | oldbase = oldbase->tp_base; |
| 3171 | if (newbase != oldbase && |
| 3172 | (newbase->tp_base != oldbase->tp_base || |
| 3173 | !same_slots_added(newbase, oldbase))) { |
| 3174 | PyErr_Format(PyExc_TypeError, |
| 3175 | "%s assignment: " |
| 3176 | "'%s' object layout differs from '%s'", |
| 3177 | attr, |
| 3178 | newto->tp_name, |
| 3179 | oldto->tp_name); |
| 3180 | return 0; |
| 3181 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 3182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3183 | return 1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3184 | } |
| 3185 | |
| 3186 | static int |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3187 | object_set_class(PyObject *self, PyObject *value, void *closure) |
| 3188 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3189 | PyTypeObject *oldto = Py_TYPE(self); |
| 3190 | PyTypeObject *newto; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3192 | if (value == NULL) { |
| 3193 | PyErr_SetString(PyExc_TypeError, |
| 3194 | "can't delete __class__ attribute"); |
| 3195 | return -1; |
| 3196 | } |
| 3197 | if (!PyType_Check(value)) { |
| 3198 | PyErr_Format(PyExc_TypeError, |
| 3199 | "__class__ must be set to new-style class, not '%s' object", |
| 3200 | Py_TYPE(value)->tp_name); |
| 3201 | return -1; |
| 3202 | } |
| 3203 | newto = (PyTypeObject *)value; |
| 3204 | if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) || |
| 3205 | !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 3206 | { |
| 3207 | PyErr_Format(PyExc_TypeError, |
| 3208 | "__class__ assignment: only for heap types"); |
| 3209 | return -1; |
| 3210 | } |
| 3211 | if (compatible_for_assignment(newto, oldto, "__class__")) { |
| 3212 | Py_INCREF(newto); |
| 3213 | Py_TYPE(self) = newto; |
| 3214 | Py_DECREF(oldto); |
| 3215 | return 0; |
| 3216 | } |
| 3217 | else { |
| 3218 | return -1; |
| 3219 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
| 3222 | static PyGetSetDef object_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3223 | {"__class__", object_get_class, object_set_class, |
| 3224 | PyDoc_STR("the object's class")}, |
| 3225 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3226 | }; |
| 3227 | |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 3228 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3229 | /* Stuff to implement __reduce_ex__ for pickle protocols >= 2. |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3230 | We fall back to helpers in copyreg for: |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3231 | - pickle protocols < 2 |
| 3232 | - calculating the list of slot names (done only once per class) |
| 3233 | - the __newobj__ function (which is used as a token but never called) |
| 3234 | */ |
| 3235 | |
| 3236 | static PyObject * |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3237 | import_copyreg(void) |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3238 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3239 | static PyObject *copyreg_str; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3240 | static PyObject *mod_copyreg = NULL; |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3242 | if (!copyreg_str) { |
| 3243 | copyreg_str = PyUnicode_InternFromString("copyreg"); |
| 3244 | if (copyreg_str == NULL) |
| 3245 | return NULL; |
| 3246 | } |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3247 | if (!mod_copyreg) { |
| 3248 | mod_copyreg = PyImport_Import(copyreg_str); |
| 3249 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3250 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3251 | Py_XINCREF(mod_copyreg); |
| 3252 | return mod_copyreg; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
| 3255 | static PyObject * |
| 3256 | slotnames(PyObject *cls) |
| 3257 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3258 | PyObject *clsdict; |
| 3259 | PyObject *copyreg; |
| 3260 | PyObject *slotnames; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3261 | static PyObject *str_slotnames; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3262 | _Py_IDENTIFIER(_slotnames); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3263 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3264 | if (str_slotnames == NULL) { |
| 3265 | str_slotnames = PyUnicode_InternFromString("__slotnames__"); |
| 3266 | if (str_slotnames == NULL) |
| 3267 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3268 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3270 | clsdict = ((PyTypeObject *)cls)->tp_dict; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3271 | slotnames = PyDict_GetItem(clsdict, str_slotnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3272 | if (slotnames != NULL && PyList_Check(slotnames)) { |
| 3273 | Py_INCREF(slotnames); |
| 3274 | return slotnames; |
| 3275 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3277 | copyreg = import_copyreg(); |
| 3278 | if (copyreg == NULL) |
| 3279 | return NULL; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3280 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3281 | slotnames = _PyObject_CallMethodId(copyreg, &PyId__slotnames, "O", cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3282 | Py_DECREF(copyreg); |
| 3283 | if (slotnames != NULL && |
| 3284 | slotnames != Py_None && |
| 3285 | !PyList_Check(slotnames)) |
| 3286 | { |
| 3287 | PyErr_SetString(PyExc_TypeError, |
| 3288 | "copyreg._slotnames didn't return a list or None"); |
| 3289 | Py_DECREF(slotnames); |
| 3290 | slotnames = NULL; |
| 3291 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3292 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3293 | return slotnames; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3294 | } |
| 3295 | |
| 3296 | static PyObject * |
| 3297 | reduce_2(PyObject *obj) |
| 3298 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3299 | PyObject *cls, *getnewargs; |
| 3300 | PyObject *args = NULL, *args2 = NULL; |
| 3301 | PyObject *getstate = NULL, *state = NULL, *names = NULL; |
| 3302 | PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL; |
| 3303 | PyObject *copyreg = NULL, *newobj = NULL, *res = NULL; |
| 3304 | Py_ssize_t i, n; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3305 | static PyObject *str_getnewargs = NULL, *str_getstate = NULL, |
| 3306 | *str_newobj = NULL; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3307 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3308 | if (str_getnewargs == NULL) { |
| 3309 | str_getnewargs = PyUnicode_InternFromString("__getnewargs__"); |
| 3310 | str_getstate = PyUnicode_InternFromString("__getstate__"); |
| 3311 | str_newobj = PyUnicode_InternFromString("__newobj__"); |
| 3312 | if (!str_getnewargs || !str_getstate || !str_newobj) |
| 3313 | return NULL; |
| 3314 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3315 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3316 | cls = (PyObject *) Py_TYPE(obj); |
| 3317 | |
| 3318 | getnewargs = PyObject_GetAttr(obj, str_getnewargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3319 | if (getnewargs != NULL) { |
| 3320 | args = PyObject_CallObject(getnewargs, NULL); |
| 3321 | Py_DECREF(getnewargs); |
| 3322 | if (args != NULL && !PyTuple_Check(args)) { |
| 3323 | PyErr_Format(PyExc_TypeError, |
| 3324 | "__getnewargs__ should return a tuple, " |
| 3325 | "not '%.200s'", Py_TYPE(args)->tp_name); |
| 3326 | goto end; |
| 3327 | } |
| 3328 | } |
| 3329 | else { |
| 3330 | PyErr_Clear(); |
| 3331 | args = PyTuple_New(0); |
| 3332 | } |
| 3333 | if (args == NULL) |
| 3334 | goto end; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3335 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3336 | getstate = PyObject_GetAttr(obj, str_getstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3337 | if (getstate != NULL) { |
| 3338 | state = PyObject_CallObject(getstate, NULL); |
| 3339 | Py_DECREF(getstate); |
| 3340 | if (state == NULL) |
| 3341 | goto end; |
| 3342 | } |
| 3343 | else { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3344 | PyObject **dict; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3345 | PyErr_Clear(); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3346 | dict = _PyObject_GetDictPtr(obj); |
| 3347 | if (dict && *dict) |
| 3348 | state = *dict; |
| 3349 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3350 | state = Py_None; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3351 | Py_INCREF(state); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3352 | names = slotnames(cls); |
| 3353 | if (names == NULL) |
| 3354 | goto end; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3355 | if (names != Py_None && PyList_GET_SIZE(names) > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3356 | assert(PyList_Check(names)); |
| 3357 | slots = PyDict_New(); |
| 3358 | if (slots == NULL) |
| 3359 | goto end; |
| 3360 | n = 0; |
| 3361 | /* Can't pre-compute the list size; the list |
| 3362 | is stored on the class so accessible to other |
| 3363 | threads, which may be run by DECREF */ |
| 3364 | for (i = 0; i < PyList_GET_SIZE(names); i++) { |
| 3365 | PyObject *name, *value; |
| 3366 | name = PyList_GET_ITEM(names, i); |
| 3367 | value = PyObject_GetAttr(obj, name); |
| 3368 | if (value == NULL) |
| 3369 | PyErr_Clear(); |
| 3370 | else { |
| 3371 | int err = PyDict_SetItem(slots, name, |
| 3372 | value); |
| 3373 | Py_DECREF(value); |
| 3374 | if (err) |
| 3375 | goto end; |
| 3376 | n++; |
| 3377 | } |
| 3378 | } |
| 3379 | if (n) { |
| 3380 | state = Py_BuildValue("(NO)", state, slots); |
| 3381 | if (state == NULL) |
| 3382 | goto end; |
| 3383 | } |
| 3384 | } |
| 3385 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3387 | if (!PyList_Check(obj)) { |
| 3388 | listitems = Py_None; |
| 3389 | Py_INCREF(listitems); |
| 3390 | } |
| 3391 | else { |
| 3392 | listitems = PyObject_GetIter(obj); |
| 3393 | if (listitems == NULL) |
| 3394 | goto end; |
| 3395 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3397 | if (!PyDict_Check(obj)) { |
| 3398 | dictitems = Py_None; |
| 3399 | Py_INCREF(dictitems); |
| 3400 | } |
| 3401 | else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3402 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3403 | PyObject *items = _PyObject_CallMethodId(obj, &PyId_items, ""); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3404 | if (items == NULL) |
| 3405 | goto end; |
| 3406 | dictitems = PyObject_GetIter(items); |
| 3407 | Py_DECREF(items); |
| 3408 | if (dictitems == NULL) |
| 3409 | goto end; |
| 3410 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3412 | copyreg = import_copyreg(); |
| 3413 | if (copyreg == NULL) |
| 3414 | goto end; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3415 | newobj = PyObject_GetAttr(copyreg, str_newobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3416 | if (newobj == NULL) |
| 3417 | goto end; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3418 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3419 | n = PyTuple_GET_SIZE(args); |
| 3420 | args2 = PyTuple_New(n+1); |
| 3421 | if (args2 == NULL) |
| 3422 | goto end; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3423 | Py_INCREF(cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3424 | PyTuple_SET_ITEM(args2, 0, cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3425 | for (i = 0; i < n; i++) { |
| 3426 | PyObject *v = PyTuple_GET_ITEM(args, i); |
| 3427 | Py_INCREF(v); |
| 3428 | PyTuple_SET_ITEM(args2, i+1, v); |
| 3429 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3431 | res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3432 | |
| 3433 | end: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3434 | Py_XDECREF(args); |
| 3435 | Py_XDECREF(args2); |
| 3436 | Py_XDECREF(slots); |
| 3437 | Py_XDECREF(state); |
| 3438 | Py_XDECREF(names); |
| 3439 | Py_XDECREF(listitems); |
| 3440 | Py_XDECREF(dictitems); |
| 3441 | Py_XDECREF(copyreg); |
| 3442 | Py_XDECREF(newobj); |
| 3443 | return res; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3444 | } |
| 3445 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3446 | /* |
| 3447 | * There were two problems when object.__reduce__ and object.__reduce_ex__ |
| 3448 | * were implemented in the same function: |
| 3449 | * - trying to pickle an object with a custom __reduce__ method that |
| 3450 | * fell back to object.__reduce__ in certain circumstances led to |
| 3451 | * infinite recursion at Python level and eventual RuntimeError. |
| 3452 | * - Pickling objects that lied about their type by overwriting the |
| 3453 | * __class__ descriptor could lead to infinite recursion at C level |
| 3454 | * and eventual segfault. |
| 3455 | * |
| 3456 | * Because of backwards compatibility, the two methods still have to |
| 3457 | * behave in the same way, even if this is not required by the pickle |
| 3458 | * protocol. This common functionality was moved to the _common_reduce |
| 3459 | * function. |
| 3460 | */ |
| 3461 | static PyObject * |
| 3462 | _common_reduce(PyObject *self, int proto) |
| 3463 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3464 | PyObject *copyreg, *res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3466 | if (proto >= 2) |
| 3467 | return reduce_2(self); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3469 | copyreg = import_copyreg(); |
| 3470 | if (!copyreg) |
| 3471 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3473 | res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto); |
| 3474 | Py_DECREF(copyreg); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3476 | return res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3477 | } |
| 3478 | |
| 3479 | static PyObject * |
| 3480 | object_reduce(PyObject *self, PyObject *args) |
| 3481 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3482 | int proto = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3483 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3484 | if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto)) |
| 3485 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3487 | return _common_reduce(self, proto); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3490 | static PyObject * |
| 3491 | object_reduce_ex(PyObject *self, PyObject *args) |
| 3492 | { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3493 | static PyObject *str_reduce = NULL, *objreduce; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3494 | PyObject *reduce, *res; |
| 3495 | int proto = 0; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3497 | if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto)) |
| 3498 | return NULL; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3499 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3500 | if (str_reduce == NULL) { |
| 3501 | str_reduce = PyUnicode_InternFromString("__reduce__"); |
| 3502 | objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict, |
| 3503 | "__reduce__"); |
| 3504 | if (str_reduce == NULL || objreduce == NULL) |
| 3505 | return NULL; |
| 3506 | } |
| 3507 | |
| 3508 | reduce = PyObject_GetAttr(self, str_reduce); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3509 | if (reduce == NULL) |
| 3510 | PyErr_Clear(); |
| 3511 | else { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3512 | PyObject *cls, *clsreduce; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3513 | int override; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3514 | |
| 3515 | cls = (PyObject *) Py_TYPE(self); |
| 3516 | clsreduce = PyObject_GetAttr(cls, str_reduce); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3517 | if (clsreduce == NULL) { |
| 3518 | Py_DECREF(reduce); |
| 3519 | return NULL; |
| 3520 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3521 | override = (clsreduce != objreduce); |
| 3522 | Py_DECREF(clsreduce); |
| 3523 | if (override) { |
| 3524 | res = PyObject_CallObject(reduce, NULL); |
| 3525 | Py_DECREF(reduce); |
| 3526 | return res; |
| 3527 | } |
| 3528 | else |
| 3529 | Py_DECREF(reduce); |
| 3530 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3531 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3532 | return _common_reduce(self, proto); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3533 | } |
| 3534 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3535 | static PyObject * |
| 3536 | object_subclasshook(PyObject *cls, PyObject *args) |
| 3537 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3538 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3539 | } |
| 3540 | |
| 3541 | PyDoc_STRVAR(object_subclasshook_doc, |
| 3542 | "Abstract classes can override this to customize issubclass().\n" |
| 3543 | "\n" |
| 3544 | "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n" |
| 3545 | "It should return True, False or NotImplemented. If it returns\n" |
| 3546 | "NotImplemented, the normal algorithm is used. Otherwise, it\n" |
| 3547 | "overrides the normal algorithm (and the outcome is cached).\n"); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3548 | |
| 3549 | /* |
| 3550 | from PEP 3101, this code implements: |
| 3551 | |
| 3552 | class object: |
| 3553 | def __format__(self, format_spec): |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3554 | return format(str(self), format_spec) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3555 | */ |
| 3556 | static PyObject * |
| 3557 | object_format(PyObject *self, PyObject *args) |
| 3558 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3559 | PyObject *format_spec; |
| 3560 | PyObject *self_as_str = NULL; |
| 3561 | PyObject *result = NULL; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3562 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3563 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 3564 | return NULL; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3566 | self_as_str = PyObject_Str(self); |
Eric Smith | e4d6317 | 2010-09-13 20:48:43 +0000 | [diff] [blame] | 3567 | if (self_as_str != NULL) { |
| 3568 | /* Issue 7994: If we're converting to a string, we |
Eric V. Smith | b9cd353 | 2011-03-12 10:08:48 -0500 | [diff] [blame] | 3569 | should reject format specifications */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3570 | if (PyUnicode_GET_LENGTH(format_spec) > 0) { |
Eric V. Smith | b9cd353 | 2011-03-12 10:08:48 -0500 | [diff] [blame] | 3571 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 3572 | "object.__format__ with a non-empty format " |
| 3573 | "string is deprecated", 1) < 0) { |
| 3574 | goto done; |
| 3575 | } |
| 3576 | /* Eventually this will become an error: |
| 3577 | PyErr_Format(PyExc_TypeError, |
| 3578 | "non-empty format string passed to object.__format__"); |
| 3579 | goto done; |
| 3580 | */ |
| 3581 | } |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3582 | |
Eric V. Smith | b9cd353 | 2011-03-12 10:08:48 -0500 | [diff] [blame] | 3583 | result = PyObject_Format(self_as_str, format_spec); |
Eric Smith | e4d6317 | 2010-09-13 20:48:43 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3587 | Py_XDECREF(self_as_str); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3588 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3589 | return result; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3590 | } |
| 3591 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3592 | static PyObject * |
| 3593 | object_sizeof(PyObject *self, PyObject *args) |
| 3594 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3595 | Py_ssize_t res, isize; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3597 | res = 0; |
| 3598 | isize = self->ob_type->tp_itemsize; |
| 3599 | if (isize > 0) |
| 3600 | res = Py_SIZE(self->ob_type) * isize; |
| 3601 | res += self->ob_type->tp_basicsize; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3603 | return PyLong_FromSsize_t(res); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3606 | /* __dir__ for generic objects: returns __dict__, __class__, |
| 3607 | and recursively up the __class__.__bases__ chain. |
| 3608 | */ |
| 3609 | static PyObject * |
| 3610 | object_dir(PyObject *self, PyObject *args) |
| 3611 | { |
| 3612 | PyObject *result = NULL; |
| 3613 | PyObject *dict = NULL; |
| 3614 | PyObject *itsclass = NULL; |
| 3615 | |
| 3616 | /* Get __dict__ (which may or may not be a real dict...) */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3617 | dict = _PyObject_GetAttrId(self, &PyId___dict__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3618 | if (dict == NULL) { |
| 3619 | PyErr_Clear(); |
| 3620 | dict = PyDict_New(); |
| 3621 | } |
| 3622 | else if (!PyDict_Check(dict)) { |
| 3623 | Py_DECREF(dict); |
| 3624 | dict = PyDict_New(); |
| 3625 | } |
| 3626 | else { |
| 3627 | /* Copy __dict__ to avoid mutating it. */ |
| 3628 | PyObject *temp = PyDict_Copy(dict); |
| 3629 | Py_DECREF(dict); |
| 3630 | dict = temp; |
| 3631 | } |
| 3632 | |
| 3633 | if (dict == NULL) |
| 3634 | goto error; |
| 3635 | |
| 3636 | /* Merge in attrs reachable from its class. */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3637 | itsclass = _PyObject_GetAttrId(self, &PyId___class__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3638 | if (itsclass == NULL) |
| 3639 | /* XXX(tomer): Perhaps fall back to obj->ob_type if no |
| 3640 | __class__ exists? */ |
| 3641 | PyErr_Clear(); |
| 3642 | else if (merge_class_dict(dict, itsclass) != 0) |
| 3643 | goto error; |
| 3644 | |
| 3645 | result = PyDict_Keys(dict); |
| 3646 | /* fall through */ |
| 3647 | error: |
| 3648 | Py_XDECREF(itsclass); |
| 3649 | Py_XDECREF(dict); |
| 3650 | return result; |
| 3651 | } |
| 3652 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3653 | static PyMethodDef object_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3654 | {"__reduce_ex__", object_reduce_ex, METH_VARARGS, |
| 3655 | PyDoc_STR("helper for pickle")}, |
| 3656 | {"__reduce__", object_reduce, METH_VARARGS, |
| 3657 | PyDoc_STR("helper for pickle")}, |
| 3658 | {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS, |
| 3659 | object_subclasshook_doc}, |
| 3660 | {"__format__", object_format, METH_VARARGS, |
| 3661 | PyDoc_STR("default object formatter")}, |
| 3662 | {"__sizeof__", object_sizeof, METH_NOARGS, |
Benjamin Peterson | fbe56bb | 2011-05-24 12:42:51 -0500 | [diff] [blame] | 3663 | PyDoc_STR("__sizeof__() -> int\nsize of object in memory, in bytes")}, |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3664 | {"__dir__", object_dir, METH_NOARGS, |
Benjamin Peterson | c728412 | 2011-05-24 12:46:15 -0500 | [diff] [blame] | 3665 | PyDoc_STR("__dir__() -> list\ndefault dir() implementation")}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3666 | {0} |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3667 | }; |
| 3668 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3669 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3670 | PyTypeObject PyBaseObject_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3671 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3672 | "object", /* tp_name */ |
| 3673 | sizeof(PyObject), /* tp_basicsize */ |
| 3674 | 0, /* tp_itemsize */ |
| 3675 | object_dealloc, /* tp_dealloc */ |
| 3676 | 0, /* tp_print */ |
| 3677 | 0, /* tp_getattr */ |
| 3678 | 0, /* tp_setattr */ |
| 3679 | 0, /* tp_reserved */ |
| 3680 | object_repr, /* tp_repr */ |
| 3681 | 0, /* tp_as_number */ |
| 3682 | 0, /* tp_as_sequence */ |
| 3683 | 0, /* tp_as_mapping */ |
| 3684 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
| 3685 | 0, /* tp_call */ |
| 3686 | object_str, /* tp_str */ |
| 3687 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3688 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 3689 | 0, /* tp_as_buffer */ |
| 3690 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3691 | PyDoc_STR("The most base type"), /* tp_doc */ |
| 3692 | 0, /* tp_traverse */ |
| 3693 | 0, /* tp_clear */ |
| 3694 | object_richcompare, /* tp_richcompare */ |
| 3695 | 0, /* tp_weaklistoffset */ |
| 3696 | 0, /* tp_iter */ |
| 3697 | 0, /* tp_iternext */ |
| 3698 | object_methods, /* tp_methods */ |
| 3699 | 0, /* tp_members */ |
| 3700 | object_getsets, /* tp_getset */ |
| 3701 | 0, /* tp_base */ |
| 3702 | 0, /* tp_dict */ |
| 3703 | 0, /* tp_descr_get */ |
| 3704 | 0, /* tp_descr_set */ |
| 3705 | 0, /* tp_dictoffset */ |
| 3706 | object_init, /* tp_init */ |
| 3707 | PyType_GenericAlloc, /* tp_alloc */ |
| 3708 | object_new, /* tp_new */ |
| 3709 | PyObject_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3710 | }; |
| 3711 | |
| 3712 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 3713 | /* Add the methods from tp_methods to the __dict__ in a type object */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3714 | |
| 3715 | static int |
| 3716 | add_methods(PyTypeObject *type, PyMethodDef *meth) |
| 3717 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3718 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3720 | for (; meth->ml_name != NULL; meth++) { |
| 3721 | PyObject *descr; |
| 3722 | if (PyDict_GetItemString(dict, meth->ml_name) && |
| 3723 | !(meth->ml_flags & METH_COEXIST)) |
| 3724 | continue; |
| 3725 | if (meth->ml_flags & METH_CLASS) { |
| 3726 | if (meth->ml_flags & METH_STATIC) { |
| 3727 | PyErr_SetString(PyExc_ValueError, |
| 3728 | "method cannot be both class and static"); |
| 3729 | return -1; |
| 3730 | } |
| 3731 | descr = PyDescr_NewClassMethod(type, meth); |
| 3732 | } |
| 3733 | else if (meth->ml_flags & METH_STATIC) { |
| 3734 | PyObject *cfunc = PyCFunction_New(meth, NULL); |
| 3735 | if (cfunc == NULL) |
| 3736 | return -1; |
| 3737 | descr = PyStaticMethod_New(cfunc); |
| 3738 | Py_DECREF(cfunc); |
| 3739 | } |
| 3740 | else { |
| 3741 | descr = PyDescr_NewMethod(type, meth); |
| 3742 | } |
| 3743 | if (descr == NULL) |
| 3744 | return -1; |
| 3745 | if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0) |
| 3746 | return -1; |
| 3747 | Py_DECREF(descr); |
| 3748 | } |
| 3749 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3750 | } |
| 3751 | |
| 3752 | static int |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 3753 | add_members(PyTypeObject *type, PyMemberDef *memb) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3754 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3755 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3756 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3757 | for (; memb->name != NULL; memb++) { |
| 3758 | PyObject *descr; |
| 3759 | if (PyDict_GetItemString(dict, memb->name)) |
| 3760 | continue; |
| 3761 | descr = PyDescr_NewMember(type, memb); |
| 3762 | if (descr == NULL) |
| 3763 | return -1; |
| 3764 | if (PyDict_SetItemString(dict, memb->name, descr) < 0) |
| 3765 | return -1; |
| 3766 | Py_DECREF(descr); |
| 3767 | } |
| 3768 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3769 | } |
| 3770 | |
| 3771 | static int |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 3772 | add_getset(PyTypeObject *type, PyGetSetDef *gsp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3773 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3774 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3775 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3776 | for (; gsp->name != NULL; gsp++) { |
| 3777 | PyObject *descr; |
| 3778 | if (PyDict_GetItemString(dict, gsp->name)) |
| 3779 | continue; |
| 3780 | descr = PyDescr_NewGetSet(type, gsp); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3781 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3782 | if (descr == NULL) |
| 3783 | return -1; |
| 3784 | if (PyDict_SetItemString(dict, gsp->name, descr) < 0) |
| 3785 | return -1; |
| 3786 | Py_DECREF(descr); |
| 3787 | } |
| 3788 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3789 | } |
| 3790 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3791 | static void |
| 3792 | inherit_special(PyTypeObject *type, PyTypeObject *base) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3793 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3794 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3795 | /* Copying basicsize is connected to the GC flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3796 | if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3797 | (base->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3798 | (!type->tp_traverse && !type->tp_clear)) { |
| 3799 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
| 3800 | if (type->tp_traverse == NULL) |
| 3801 | type->tp_traverse = base->tp_traverse; |
| 3802 | if (type->tp_clear == NULL) |
| 3803 | type->tp_clear = base->tp_clear; |
| 3804 | } |
| 3805 | { |
| 3806 | /* The condition below could use some explanation. |
| 3807 | It appears that tp_new is not inherited for static types |
| 3808 | whose base class is 'object'; this seems to be a precaution |
| 3809 | so that old extension types don't suddenly become |
| 3810 | callable (object.__new__ wouldn't insure the invariants |
| 3811 | that the extension type's own factory function ensures). |
| 3812 | Heap types, of course, are under our control, so they do |
| 3813 | inherit tp_new; static extension types that specify some |
| 3814 | other built-in type as the default are considered |
| 3815 | new-style-aware so they also inherit object.__new__. */ |
| 3816 | if (base != &PyBaseObject_Type || |
| 3817 | (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 3818 | if (type->tp_new == NULL) |
| 3819 | type->tp_new = base->tp_new; |
| 3820 | } |
| 3821 | } |
Benjamin Peterson | a7465e2 | 2010-07-05 15:01:22 +0000 | [diff] [blame] | 3822 | if (type->tp_basicsize == 0) |
| 3823 | type->tp_basicsize = base->tp_basicsize; |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3824 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3825 | /* Copy other non-function slots */ |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3826 | |
| 3827 | #undef COPYVAL |
| 3828 | #define COPYVAL(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3829 | if (type->SLOT == 0) type->SLOT = base->SLOT |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3830 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3831 | COPYVAL(tp_itemsize); |
| 3832 | COPYVAL(tp_weaklistoffset); |
| 3833 | COPYVAL(tp_dictoffset); |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 3834 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3835 | /* Setup fast subclass flags */ |
| 3836 | if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException)) |
| 3837 | type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS; |
| 3838 | else if (PyType_IsSubtype(base, &PyType_Type)) |
| 3839 | type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS; |
| 3840 | else if (PyType_IsSubtype(base, &PyLong_Type)) |
| 3841 | type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS; |
| 3842 | else if (PyType_IsSubtype(base, &PyBytes_Type)) |
| 3843 | type->tp_flags |= Py_TPFLAGS_BYTES_SUBCLASS; |
| 3844 | else if (PyType_IsSubtype(base, &PyUnicode_Type)) |
| 3845 | type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS; |
| 3846 | else if (PyType_IsSubtype(base, &PyTuple_Type)) |
| 3847 | type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS; |
| 3848 | else if (PyType_IsSubtype(base, &PyList_Type)) |
| 3849 | type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS; |
| 3850 | else if (PyType_IsSubtype(base, &PyDict_Type)) |
| 3851 | type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3852 | } |
| 3853 | |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 3854 | static char *hash_name_op[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3855 | "__eq__", |
| 3856 | "__hash__", |
| 3857 | NULL |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3858 | }; |
| 3859 | |
| 3860 | static int |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 3861 | overrides_hash(PyTypeObject *type) |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3862 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3863 | char **p; |
| 3864 | PyObject *dict = type->tp_dict; |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3865 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3866 | assert(dict != NULL); |
| 3867 | for (p = hash_name_op; *p; p++) { |
| 3868 | if (PyDict_GetItemString(dict, *p) != NULL) |
| 3869 | return 1; |
| 3870 | } |
| 3871 | return 0; |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3872 | } |
| 3873 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3874 | static void |
| 3875 | inherit_slots(PyTypeObject *type, PyTypeObject *base) |
| 3876 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3877 | PyTypeObject *basebase; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3878 | |
| 3879 | #undef SLOTDEFINED |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3880 | #undef COPYSLOT |
| 3881 | #undef COPYNUM |
| 3882 | #undef COPYSEQ |
| 3883 | #undef COPYMAP |
Guido van Rossum | 5af588b | 2001-10-12 14:13:21 +0000 | [diff] [blame] | 3884 | #undef COPYBUF |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3885 | |
| 3886 | #define SLOTDEFINED(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3887 | (base->SLOT != 0 && \ |
| 3888 | (basebase == NULL || base->SLOT != basebase->SLOT)) |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3889 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3890 | #define COPYSLOT(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3891 | if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3892 | |
| 3893 | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT) |
| 3894 | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT) |
| 3895 | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT) |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3896 | #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3897 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3898 | /* This won't inherit indirect slots (from tp_as_number etc.) |
| 3899 | if type doesn't provide the space. */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3900 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3901 | if (type->tp_as_number != NULL && base->tp_as_number != NULL) { |
| 3902 | basebase = base->tp_base; |
| 3903 | if (basebase->tp_as_number == NULL) |
| 3904 | basebase = NULL; |
| 3905 | COPYNUM(nb_add); |
| 3906 | COPYNUM(nb_subtract); |
| 3907 | COPYNUM(nb_multiply); |
| 3908 | COPYNUM(nb_remainder); |
| 3909 | COPYNUM(nb_divmod); |
| 3910 | COPYNUM(nb_power); |
| 3911 | COPYNUM(nb_negative); |
| 3912 | COPYNUM(nb_positive); |
| 3913 | COPYNUM(nb_absolute); |
| 3914 | COPYNUM(nb_bool); |
| 3915 | COPYNUM(nb_invert); |
| 3916 | COPYNUM(nb_lshift); |
| 3917 | COPYNUM(nb_rshift); |
| 3918 | COPYNUM(nb_and); |
| 3919 | COPYNUM(nb_xor); |
| 3920 | COPYNUM(nb_or); |
| 3921 | COPYNUM(nb_int); |
| 3922 | COPYNUM(nb_float); |
| 3923 | COPYNUM(nb_inplace_add); |
| 3924 | COPYNUM(nb_inplace_subtract); |
| 3925 | COPYNUM(nb_inplace_multiply); |
| 3926 | COPYNUM(nb_inplace_remainder); |
| 3927 | COPYNUM(nb_inplace_power); |
| 3928 | COPYNUM(nb_inplace_lshift); |
| 3929 | COPYNUM(nb_inplace_rshift); |
| 3930 | COPYNUM(nb_inplace_and); |
| 3931 | COPYNUM(nb_inplace_xor); |
| 3932 | COPYNUM(nb_inplace_or); |
| 3933 | COPYNUM(nb_true_divide); |
| 3934 | COPYNUM(nb_floor_divide); |
| 3935 | COPYNUM(nb_inplace_true_divide); |
| 3936 | COPYNUM(nb_inplace_floor_divide); |
| 3937 | COPYNUM(nb_index); |
| 3938 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3939 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3940 | if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) { |
| 3941 | basebase = base->tp_base; |
| 3942 | if (basebase->tp_as_sequence == NULL) |
| 3943 | basebase = NULL; |
| 3944 | COPYSEQ(sq_length); |
| 3945 | COPYSEQ(sq_concat); |
| 3946 | COPYSEQ(sq_repeat); |
| 3947 | COPYSEQ(sq_item); |
| 3948 | COPYSEQ(sq_ass_item); |
| 3949 | COPYSEQ(sq_contains); |
| 3950 | COPYSEQ(sq_inplace_concat); |
| 3951 | COPYSEQ(sq_inplace_repeat); |
| 3952 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3953 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3954 | if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) { |
| 3955 | basebase = base->tp_base; |
| 3956 | if (basebase->tp_as_mapping == NULL) |
| 3957 | basebase = NULL; |
| 3958 | COPYMAP(mp_length); |
| 3959 | COPYMAP(mp_subscript); |
| 3960 | COPYMAP(mp_ass_subscript); |
| 3961 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3963 | if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) { |
| 3964 | basebase = base->tp_base; |
| 3965 | if (basebase->tp_as_buffer == NULL) |
| 3966 | basebase = NULL; |
| 3967 | COPYBUF(bf_getbuffer); |
| 3968 | COPYBUF(bf_releasebuffer); |
| 3969 | } |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3971 | basebase = base->tp_base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3972 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3973 | COPYSLOT(tp_dealloc); |
| 3974 | if (type->tp_getattr == NULL && type->tp_getattro == NULL) { |
| 3975 | type->tp_getattr = base->tp_getattr; |
| 3976 | type->tp_getattro = base->tp_getattro; |
| 3977 | } |
| 3978 | if (type->tp_setattr == NULL && type->tp_setattro == NULL) { |
| 3979 | type->tp_setattr = base->tp_setattr; |
| 3980 | type->tp_setattro = base->tp_setattro; |
| 3981 | } |
| 3982 | /* tp_reserved is ignored */ |
| 3983 | COPYSLOT(tp_repr); |
| 3984 | /* tp_hash see tp_richcompare */ |
| 3985 | COPYSLOT(tp_call); |
| 3986 | COPYSLOT(tp_str); |
| 3987 | { |
| 3988 | /* Copy comparison-related slots only when |
| 3989 | not overriding them anywhere */ |
| 3990 | if (type->tp_richcompare == NULL && |
| 3991 | type->tp_hash == NULL && |
| 3992 | !overrides_hash(type)) |
| 3993 | { |
| 3994 | type->tp_richcompare = base->tp_richcompare; |
| 3995 | type->tp_hash = base->tp_hash; |
| 3996 | } |
| 3997 | } |
| 3998 | { |
| 3999 | COPYSLOT(tp_iter); |
| 4000 | COPYSLOT(tp_iternext); |
| 4001 | } |
| 4002 | { |
| 4003 | COPYSLOT(tp_descr_get); |
| 4004 | COPYSLOT(tp_descr_set); |
| 4005 | COPYSLOT(tp_dictoffset); |
| 4006 | COPYSLOT(tp_init); |
| 4007 | COPYSLOT(tp_alloc); |
| 4008 | COPYSLOT(tp_is_gc); |
| 4009 | if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 4010 | (base->tp_flags & Py_TPFLAGS_HAVE_GC)) { |
| 4011 | /* They agree about gc. */ |
| 4012 | COPYSLOT(tp_free); |
| 4013 | } |
| 4014 | else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 4015 | type->tp_free == NULL && |
| 4016 | base->tp_free == PyObject_Free) { |
| 4017 | /* A bit of magic to plug in the correct default |
| 4018 | * tp_free function when a derived class adds gc, |
| 4019 | * didn't define tp_free, and the base uses the |
| 4020 | * default non-gc tp_free. |
| 4021 | */ |
| 4022 | type->tp_free = PyObject_GC_Del; |
| 4023 | } |
| 4024 | /* else they didn't agree about gc, and there isn't something |
| 4025 | * obvious to be done -- the type is on its own. |
| 4026 | */ |
| 4027 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 4030 | static int add_operators(PyTypeObject *); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4031 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4032 | int |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 4033 | PyType_Ready(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4034 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4035 | PyObject *dict, *bases; |
| 4036 | PyTypeObject *base; |
| 4037 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4038 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4039 | if (type->tp_flags & Py_TPFLAGS_READY) { |
| 4040 | assert(type->tp_dict != NULL); |
| 4041 | return 0; |
| 4042 | } |
| 4043 | assert((type->tp_flags & Py_TPFLAGS_READYING) == 0); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 4044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4045 | type->tp_flags |= Py_TPFLAGS_READYING; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4046 | |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 4047 | #ifdef Py_TRACE_REFS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4048 | /* PyType_Ready is the closest thing we have to a choke point |
| 4049 | * for type objects, so is the best place I can think of to try |
| 4050 | * to get type objects into the doubly-linked list of all objects. |
| 4051 | * Still, not all type objects go thru PyType_Ready. |
| 4052 | */ |
| 4053 | _Py_AddToAllObjects((PyObject *)type, 0); |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 4054 | #endif |
| 4055 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4056 | /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
| 4057 | base = type->tp_base; |
| 4058 | if (base == NULL && type != &PyBaseObject_Type) { |
| 4059 | base = type->tp_base = &PyBaseObject_Type; |
| 4060 | Py_INCREF(base); |
| 4061 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4063 | /* Now the only way base can still be NULL is if type is |
| 4064 | * &PyBaseObject_Type. |
| 4065 | */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 4066 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4067 | /* Initialize the base class */ |
| 4068 | if (base != NULL && base->tp_dict == NULL) { |
| 4069 | if (PyType_Ready(base) < 0) |
| 4070 | goto error; |
| 4071 | } |
Guido van Rossum | 323a9cf | 2002-08-14 17:26:30 +0000 | [diff] [blame] | 4072 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4073 | /* Initialize ob_type if NULL. This means extensions that want to be |
| 4074 | compilable separately on Windows can call PyType_Ready() instead of |
| 4075 | initializing the ob_type field of their type objects. */ |
| 4076 | /* The test for base != NULL is really unnecessary, since base is only |
| 4077 | NULL when type is &PyBaseObject_Type, and we know its ob_type is |
| 4078 | not NULL (it's initialized to &PyType_Type). But coverity doesn't |
| 4079 | know that. */ |
| 4080 | if (Py_TYPE(type) == NULL && base != NULL) |
| 4081 | Py_TYPE(type) = Py_TYPE(base); |
Guido van Rossum | 0986d82 | 2002-04-08 01:38:42 +0000 | [diff] [blame] | 4082 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4083 | /* Initialize tp_bases */ |
| 4084 | bases = type->tp_bases; |
| 4085 | if (bases == NULL) { |
| 4086 | if (base == NULL) |
| 4087 | bases = PyTuple_New(0); |
| 4088 | else |
| 4089 | bases = PyTuple_Pack(1, base); |
| 4090 | if (bases == NULL) |
| 4091 | goto error; |
| 4092 | type->tp_bases = bases; |
| 4093 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4094 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4095 | /* Initialize tp_dict */ |
| 4096 | dict = type->tp_dict; |
| 4097 | if (dict == NULL) { |
| 4098 | dict = PyDict_New(); |
| 4099 | if (dict == NULL) |
| 4100 | goto error; |
| 4101 | type->tp_dict = dict; |
| 4102 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4104 | /* Add type-specific descriptors to tp_dict */ |
| 4105 | if (add_operators(type) < 0) |
| 4106 | goto error; |
| 4107 | if (type->tp_methods != NULL) { |
| 4108 | if (add_methods(type, type->tp_methods) < 0) |
| 4109 | goto error; |
| 4110 | } |
| 4111 | if (type->tp_members != NULL) { |
| 4112 | if (add_members(type, type->tp_members) < 0) |
| 4113 | goto error; |
| 4114 | } |
| 4115 | if (type->tp_getset != NULL) { |
| 4116 | if (add_getset(type, type->tp_getset) < 0) |
| 4117 | goto error; |
| 4118 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4119 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4120 | /* Calculate method resolution order */ |
| 4121 | if (mro_internal(type) < 0) { |
| 4122 | goto error; |
| 4123 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4124 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4125 | /* Inherit special flags from dominant base */ |
| 4126 | if (type->tp_base != NULL) |
| 4127 | inherit_special(type, type->tp_base); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4129 | /* Initialize tp_dict properly */ |
| 4130 | bases = type->tp_mro; |
| 4131 | assert(bases != NULL); |
| 4132 | assert(PyTuple_Check(bases)); |
| 4133 | n = PyTuple_GET_SIZE(bases); |
| 4134 | for (i = 1; i < n; i++) { |
| 4135 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 4136 | if (PyType_Check(b)) |
| 4137 | inherit_slots(type, (PyTypeObject *)b); |
| 4138 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4140 | /* Sanity check for tp_free. */ |
| 4141 | if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) && |
| 4142 | (type->tp_free == NULL || type->tp_free == PyObject_Del)) { |
| 4143 | /* This base class needs to call tp_free, but doesn't have |
| 4144 | * one, or its tp_free is for non-gc'ed objects. |
| 4145 | */ |
| 4146 | PyErr_Format(PyExc_TypeError, "type '%.100s' participates in " |
| 4147 | "gc and is a base type but has inappropriate " |
| 4148 | "tp_free slot", |
| 4149 | type->tp_name); |
| 4150 | goto error; |
| 4151 | } |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 4152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4153 | /* if the type dictionary doesn't contain a __doc__, set it from |
| 4154 | the tp_doc slot. |
| 4155 | */ |
| 4156 | if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) { |
| 4157 | if (type->tp_doc != NULL) { |
| 4158 | PyObject *doc = PyUnicode_FromString(type->tp_doc); |
| 4159 | if (doc == NULL) |
| 4160 | goto error; |
| 4161 | PyDict_SetItemString(type->tp_dict, "__doc__", doc); |
| 4162 | Py_DECREF(doc); |
| 4163 | } else { |
| 4164 | PyDict_SetItemString(type->tp_dict, |
| 4165 | "__doc__", Py_None); |
| 4166 | } |
| 4167 | } |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 4168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4169 | /* Hack for tp_hash and __hash__. |
| 4170 | If after all that, tp_hash is still NULL, and __hash__ is not in |
| 4171 | tp_dict, set tp_hash to PyObject_HashNotImplemented and |
| 4172 | tp_dict['__hash__'] equal to None. |
| 4173 | This signals that __hash__ is not inherited. |
| 4174 | */ |
| 4175 | if (type->tp_hash == NULL) { |
| 4176 | if (PyDict_GetItemString(type->tp_dict, "__hash__") == NULL) { |
| 4177 | if (PyDict_SetItemString(type->tp_dict, "__hash__", Py_None) < 0) |
| 4178 | goto error; |
| 4179 | type->tp_hash = PyObject_HashNotImplemented; |
| 4180 | } |
| 4181 | } |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 4182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4183 | /* Some more special stuff */ |
| 4184 | base = type->tp_base; |
| 4185 | if (base != NULL) { |
| 4186 | if (type->tp_as_number == NULL) |
| 4187 | type->tp_as_number = base->tp_as_number; |
| 4188 | if (type->tp_as_sequence == NULL) |
| 4189 | type->tp_as_sequence = base->tp_as_sequence; |
| 4190 | if (type->tp_as_mapping == NULL) |
| 4191 | type->tp_as_mapping = base->tp_as_mapping; |
| 4192 | if (type->tp_as_buffer == NULL) |
| 4193 | type->tp_as_buffer = base->tp_as_buffer; |
| 4194 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4196 | /* Link into each base class's list of subclasses */ |
| 4197 | bases = type->tp_bases; |
| 4198 | n = PyTuple_GET_SIZE(bases); |
| 4199 | for (i = 0; i < n; i++) { |
| 4200 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 4201 | if (PyType_Check(b) && |
| 4202 | add_subclass((PyTypeObject *)b, type) < 0) |
| 4203 | goto error; |
| 4204 | } |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4205 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4206 | /* Warn for a type that implements tp_compare (now known as |
| 4207 | tp_reserved) but not tp_richcompare. */ |
| 4208 | if (type->tp_reserved && !type->tp_richcompare) { |
| 4209 | int error; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 4210 | error = PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 4211 | "Type %.100s defines tp_reserved (formerly tp_compare) " |
| 4212 | "but not tp_richcompare. Comparisons may not behave as intended.", |
| 4213 | type->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4214 | if (error == -1) |
| 4215 | goto error; |
| 4216 | } |
Mark Dickinson | 2a7d45b | 2009-02-08 11:02:10 +0000 | [diff] [blame] | 4217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4218 | /* All done -- set the ready flag */ |
| 4219 | assert(type->tp_dict != NULL); |
| 4220 | type->tp_flags = |
| 4221 | (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; |
| 4222 | return 0; |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 4223 | |
| 4224 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4225 | type->tp_flags &= ~Py_TPFLAGS_READYING; |
| 4226 | return -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4227 | } |
| 4228 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4229 | static int |
| 4230 | add_subclass(PyTypeObject *base, PyTypeObject *type) |
| 4231 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4232 | Py_ssize_t i; |
| 4233 | int result; |
| 4234 | PyObject *list, *ref, *newobj; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4236 | list = base->tp_subclasses; |
| 4237 | if (list == NULL) { |
| 4238 | base->tp_subclasses = list = PyList_New(0); |
| 4239 | if (list == NULL) |
| 4240 | return -1; |
| 4241 | } |
| 4242 | assert(PyList_Check(list)); |
| 4243 | newobj = PyWeakref_NewRef((PyObject *)type, NULL); |
| 4244 | i = PyList_GET_SIZE(list); |
| 4245 | while (--i >= 0) { |
| 4246 | ref = PyList_GET_ITEM(list, i); |
| 4247 | assert(PyWeakref_CheckRef(ref)); |
| 4248 | if (PyWeakref_GET_OBJECT(ref) == Py_None) |
| 4249 | return PyList_SetItem(list, i, newobj); |
| 4250 | } |
| 4251 | result = PyList_Append(list, newobj); |
| 4252 | Py_DECREF(newobj); |
| 4253 | return result; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4254 | } |
| 4255 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4256 | static void |
| 4257 | remove_subclass(PyTypeObject *base, PyTypeObject *type) |
| 4258 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4259 | Py_ssize_t i; |
| 4260 | PyObject *list, *ref; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4261 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4262 | list = base->tp_subclasses; |
| 4263 | if (list == NULL) { |
| 4264 | return; |
| 4265 | } |
| 4266 | assert(PyList_Check(list)); |
| 4267 | i = PyList_GET_SIZE(list); |
| 4268 | while (--i >= 0) { |
| 4269 | ref = PyList_GET_ITEM(list, i); |
| 4270 | assert(PyWeakref_CheckRef(ref)); |
| 4271 | if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) { |
| 4272 | /* this can't fail, right? */ |
| 4273 | PySequence_DelItem(list, i); |
| 4274 | return; |
| 4275 | } |
| 4276 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4277 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4278 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4279 | static int |
| 4280 | check_num_args(PyObject *ob, int n) |
| 4281 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4282 | if (!PyTuple_CheckExact(ob)) { |
| 4283 | PyErr_SetString(PyExc_SystemError, |
| 4284 | "PyArg_UnpackTuple() argument list is not a tuple"); |
| 4285 | return 0; |
| 4286 | } |
| 4287 | if (n == PyTuple_GET_SIZE(ob)) |
| 4288 | return 1; |
| 4289 | PyErr_Format( |
| 4290 | PyExc_TypeError, |
| 4291 | "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob)); |
| 4292 | return 0; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4293 | } |
| 4294 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4295 | /* Generic wrappers for overloadable 'operators' such as __getitem__ */ |
| 4296 | |
| 4297 | /* There's a wrapper *function* for each distinct function typedef used |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4298 | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4299 | wrapper *table* for each distinct operation (e.g. __len__, __add__). |
| 4300 | Most tables have only one entry; the tables for binary operators have two |
| 4301 | entries, one regular and one with reversed arguments. */ |
| 4302 | |
| 4303 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4304 | wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4305 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4306 | lenfunc func = (lenfunc)wrapped; |
| 4307 | Py_ssize_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4309 | if (!check_num_args(args, 0)) |
| 4310 | return NULL; |
| 4311 | res = (*func)(self); |
| 4312 | if (res == -1 && PyErr_Occurred()) |
| 4313 | return NULL; |
| 4314 | return PyLong_FromLong((long)res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4315 | } |
| 4316 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4317 | static PyObject * |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4318 | wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped) |
| 4319 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4320 | inquiry func = (inquiry)wrapped; |
| 4321 | int res; |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4323 | if (!check_num_args(args, 0)) |
| 4324 | return NULL; |
| 4325 | res = (*func)(self); |
| 4326 | if (res == -1 && PyErr_Occurred()) |
| 4327 | return NULL; |
| 4328 | return PyBool_FromLong((long)res); |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4329 | } |
| 4330 | |
| 4331 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4332 | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4333 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4334 | binaryfunc func = (binaryfunc)wrapped; |
| 4335 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4336 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4337 | if (!check_num_args(args, 1)) |
| 4338 | return NULL; |
| 4339 | other = PyTuple_GET_ITEM(args, 0); |
| 4340 | return (*func)(self, other); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4341 | } |
| 4342 | |
| 4343 | static PyObject * |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4344 | wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped) |
| 4345 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4346 | binaryfunc func = (binaryfunc)wrapped; |
| 4347 | PyObject *other; |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4348 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4349 | if (!check_num_args(args, 1)) |
| 4350 | return NULL; |
| 4351 | other = PyTuple_GET_ITEM(args, 0); |
| 4352 | return (*func)(self, other); |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4353 | } |
| 4354 | |
| 4355 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4356 | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4357 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4358 | binaryfunc func = (binaryfunc)wrapped; |
| 4359 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4360 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4361 | if (!check_num_args(args, 1)) |
| 4362 | return NULL; |
| 4363 | other = PyTuple_GET_ITEM(args, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4364 | return (*func)(other, self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4365 | } |
| 4366 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4367 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4368 | wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4369 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4370 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4371 | PyObject *other; |
| 4372 | PyObject *third = Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4374 | /* Note: This wrapper only works for __pow__() */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4375 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4376 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
| 4377 | return NULL; |
| 4378 | return (*func)(self, other, third); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4379 | } |
| 4380 | |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4381 | static PyObject * |
| 4382 | wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4383 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4384 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4385 | PyObject *other; |
| 4386 | PyObject *third = Py_None; |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4388 | /* Note: This wrapper only works for __pow__() */ |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4390 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
| 4391 | return NULL; |
| 4392 | return (*func)(other, self, third); |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4393 | } |
| 4394 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4395 | static PyObject * |
| 4396 | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4397 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4398 | unaryfunc func = (unaryfunc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4400 | if (!check_num_args(args, 0)) |
| 4401 | return NULL; |
| 4402 | return (*func)(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4403 | } |
| 4404 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4405 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4406 | wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4407 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4408 | ssizeargfunc func = (ssizeargfunc)wrapped; |
| 4409 | PyObject* o; |
| 4410 | Py_ssize_t i; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4412 | if (!PyArg_UnpackTuple(args, "", 1, 1, &o)) |
| 4413 | return NULL; |
| 4414 | i = PyNumber_AsSsize_t(o, PyExc_OverflowError); |
| 4415 | if (i == -1 && PyErr_Occurred()) |
| 4416 | return NULL; |
| 4417 | return (*func)(self, i); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4420 | static Py_ssize_t |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4421 | getindex(PyObject *self, PyObject *arg) |
| 4422 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4423 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4424 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4425 | i = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
| 4426 | if (i == -1 && PyErr_Occurred()) |
| 4427 | return -1; |
| 4428 | if (i < 0) { |
| 4429 | PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence; |
| 4430 | if (sq && sq->sq_length) { |
| 4431 | Py_ssize_t n = (*sq->sq_length)(self); |
| 4432 | if (n < 0) |
| 4433 | return -1; |
| 4434 | i += n; |
| 4435 | } |
| 4436 | } |
| 4437 | return i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4438 | } |
| 4439 | |
| 4440 | static PyObject * |
| 4441 | wrap_sq_item(PyObject *self, PyObject *args, void *wrapped) |
| 4442 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4443 | ssizeargfunc func = (ssizeargfunc)wrapped; |
| 4444 | PyObject *arg; |
| 4445 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4447 | if (PyTuple_GET_SIZE(args) == 1) { |
| 4448 | arg = PyTuple_GET_ITEM(args, 0); |
| 4449 | i = getindex(self, arg); |
| 4450 | if (i == -1 && PyErr_Occurred()) |
| 4451 | return NULL; |
| 4452 | return (*func)(self, i); |
| 4453 | } |
| 4454 | check_num_args(args, 1); |
| 4455 | assert(PyErr_Occurred()); |
| 4456 | return NULL; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4457 | } |
| 4458 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4459 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4460 | wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4461 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4462 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4463 | Py_ssize_t i; |
| 4464 | int res; |
| 4465 | PyObject *arg, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4466 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4467 | if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value)) |
| 4468 | return NULL; |
| 4469 | i = getindex(self, arg); |
| 4470 | if (i == -1 && PyErr_Occurred()) |
| 4471 | return NULL; |
| 4472 | res = (*func)(self, i, value); |
| 4473 | if (res == -1 && PyErr_Occurred()) |
| 4474 | return NULL; |
| 4475 | Py_INCREF(Py_None); |
| 4476 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4477 | } |
| 4478 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4479 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4480 | wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4481 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4482 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4483 | Py_ssize_t i; |
| 4484 | int res; |
| 4485 | PyObject *arg; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4487 | if (!check_num_args(args, 1)) |
| 4488 | return NULL; |
| 4489 | arg = PyTuple_GET_ITEM(args, 0); |
| 4490 | i = getindex(self, arg); |
| 4491 | if (i == -1 && PyErr_Occurred()) |
| 4492 | return NULL; |
| 4493 | res = (*func)(self, i, NULL); |
| 4494 | if (res == -1 && PyErr_Occurred()) |
| 4495 | return NULL; |
| 4496 | Py_INCREF(Py_None); |
| 4497 | return Py_None; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4498 | } |
| 4499 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4500 | /* XXX objobjproc is a misnomer; should be objargpred */ |
| 4501 | static PyObject * |
| 4502 | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) |
| 4503 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4504 | objobjproc func = (objobjproc)wrapped; |
| 4505 | int res; |
| 4506 | PyObject *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4508 | if (!check_num_args(args, 1)) |
| 4509 | return NULL; |
| 4510 | value = PyTuple_GET_ITEM(args, 0); |
| 4511 | res = (*func)(self, value); |
| 4512 | if (res == -1 && PyErr_Occurred()) |
| 4513 | return NULL; |
| 4514 | else |
| 4515 | return PyBool_FromLong(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4516 | } |
| 4517 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4518 | static PyObject * |
| 4519 | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 4520 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4521 | objobjargproc func = (objobjargproc)wrapped; |
| 4522 | int res; |
| 4523 | PyObject *key, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4524 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4525 | if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value)) |
| 4526 | return NULL; |
| 4527 | res = (*func)(self, key, value); |
| 4528 | if (res == -1 && PyErr_Occurred()) |
| 4529 | return NULL; |
| 4530 | Py_INCREF(Py_None); |
| 4531 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4532 | } |
| 4533 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4534 | static PyObject * |
| 4535 | wrap_delitem(PyObject *self, PyObject *args, void *wrapped) |
| 4536 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4537 | objobjargproc func = (objobjargproc)wrapped; |
| 4538 | int res; |
| 4539 | PyObject *key; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4540 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4541 | if (!check_num_args(args, 1)) |
| 4542 | return NULL; |
| 4543 | key = PyTuple_GET_ITEM(args, 0); |
| 4544 | res = (*func)(self, key, NULL); |
| 4545 | if (res == -1 && PyErr_Occurred()) |
| 4546 | return NULL; |
| 4547 | Py_INCREF(Py_None); |
| 4548 | return Py_None; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4549 | } |
| 4550 | |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4551 | /* 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] | 4552 | This is called the Carlo Verre hack after its discoverer. */ |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4553 | static int |
| 4554 | hackcheck(PyObject *self, setattrofunc func, char *what) |
| 4555 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4556 | PyTypeObject *type = Py_TYPE(self); |
| 4557 | while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 4558 | type = type->tp_base; |
| 4559 | /* If type is NULL now, this is a really weird type. |
| 4560 | In the spirit of backwards compatibility (?), just shut up. */ |
| 4561 | if (type && type->tp_setattro != func) { |
| 4562 | PyErr_Format(PyExc_TypeError, |
| 4563 | "can't apply this %s to %s object", |
| 4564 | what, |
| 4565 | type->tp_name); |
| 4566 | return 0; |
| 4567 | } |
| 4568 | return 1; |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4569 | } |
| 4570 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4571 | static PyObject * |
| 4572 | wrap_setattr(PyObject *self, PyObject *args, void *wrapped) |
| 4573 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4574 | setattrofunc func = (setattrofunc)wrapped; |
| 4575 | int res; |
| 4576 | PyObject *name, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4578 | if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value)) |
| 4579 | return NULL; |
| 4580 | if (!hackcheck(self, func, "__setattr__")) |
| 4581 | return NULL; |
| 4582 | res = (*func)(self, name, value); |
| 4583 | if (res < 0) |
| 4584 | return NULL; |
| 4585 | Py_INCREF(Py_None); |
| 4586 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4587 | } |
| 4588 | |
| 4589 | static PyObject * |
| 4590 | wrap_delattr(PyObject *self, PyObject *args, void *wrapped) |
| 4591 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4592 | setattrofunc func = (setattrofunc)wrapped; |
| 4593 | int res; |
| 4594 | PyObject *name; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4596 | if (!check_num_args(args, 1)) |
| 4597 | return NULL; |
| 4598 | name = PyTuple_GET_ITEM(args, 0); |
| 4599 | if (!hackcheck(self, func, "__delattr__")) |
| 4600 | return NULL; |
| 4601 | res = (*func)(self, name, NULL); |
| 4602 | if (res < 0) |
| 4603 | return NULL; |
| 4604 | Py_INCREF(Py_None); |
| 4605 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4606 | } |
| 4607 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4608 | static PyObject * |
| 4609 | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4610 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4611 | hashfunc func = (hashfunc)wrapped; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 4612 | Py_hash_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4614 | if (!check_num_args(args, 0)) |
| 4615 | return NULL; |
| 4616 | res = (*func)(self); |
| 4617 | if (res == -1 && PyErr_Occurred()) |
| 4618 | return NULL; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 4619 | return PyLong_FromSsize_t(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4620 | } |
| 4621 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4622 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4623 | wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4624 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4625 | ternaryfunc func = (ternaryfunc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4626 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4627 | return (*func)(self, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4628 | } |
| 4629 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4630 | static PyObject * |
| 4631 | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) |
| 4632 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4633 | richcmpfunc func = (richcmpfunc)wrapped; |
| 4634 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4636 | if (!check_num_args(args, 1)) |
| 4637 | return NULL; |
| 4638 | other = PyTuple_GET_ITEM(args, 0); |
| 4639 | return (*func)(self, other, op); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4640 | } |
| 4641 | |
| 4642 | #undef RICHCMP_WRAPPER |
| 4643 | #define RICHCMP_WRAPPER(NAME, OP) \ |
| 4644 | static PyObject * \ |
| 4645 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ |
| 4646 | { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4647 | return wrap_richcmpfunc(self, args, wrapped, OP); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4648 | } |
| 4649 | |
Jack Jansen | 8e938b4 | 2001-08-08 15:29:49 +0000 | [diff] [blame] | 4650 | RICHCMP_WRAPPER(lt, Py_LT) |
| 4651 | RICHCMP_WRAPPER(le, Py_LE) |
| 4652 | RICHCMP_WRAPPER(eq, Py_EQ) |
| 4653 | RICHCMP_WRAPPER(ne, Py_NE) |
| 4654 | RICHCMP_WRAPPER(gt, Py_GT) |
| 4655 | RICHCMP_WRAPPER(ge, Py_GE) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4656 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4657 | static PyObject * |
| 4658 | wrap_next(PyObject *self, PyObject *args, void *wrapped) |
| 4659 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4660 | unaryfunc func = (unaryfunc)wrapped; |
| 4661 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4663 | if (!check_num_args(args, 0)) |
| 4664 | return NULL; |
| 4665 | res = (*func)(self); |
| 4666 | if (res == NULL && !PyErr_Occurred()) |
| 4667 | PyErr_SetNone(PyExc_StopIteration); |
| 4668 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4669 | } |
| 4670 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4671 | static PyObject * |
| 4672 | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped) |
| 4673 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4674 | descrgetfunc func = (descrgetfunc)wrapped; |
| 4675 | PyObject *obj; |
| 4676 | PyObject *type = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4677 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4678 | if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type)) |
| 4679 | return NULL; |
| 4680 | if (obj == Py_None) |
| 4681 | obj = NULL; |
| 4682 | if (type == Py_None) |
| 4683 | type = NULL; |
| 4684 | if (type == NULL &&obj == NULL) { |
| 4685 | PyErr_SetString(PyExc_TypeError, |
| 4686 | "__get__(None, None) is invalid"); |
| 4687 | return NULL; |
| 4688 | } |
| 4689 | return (*func)(self, obj, type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4690 | } |
| 4691 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4692 | static PyObject * |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4693 | wrap_descr_set(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4694 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4695 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4696 | PyObject *obj, *value; |
| 4697 | int ret; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4698 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4699 | if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value)) |
| 4700 | return NULL; |
| 4701 | ret = (*func)(self, obj, value); |
| 4702 | if (ret < 0) |
| 4703 | return NULL; |
| 4704 | Py_INCREF(Py_None); |
| 4705 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4706 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 4707 | |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4708 | static PyObject * |
| 4709 | wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped) |
| 4710 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4711 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4712 | PyObject *obj; |
| 4713 | int ret; |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4714 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4715 | if (!check_num_args(args, 1)) |
| 4716 | return NULL; |
| 4717 | obj = PyTuple_GET_ITEM(args, 0); |
| 4718 | ret = (*func)(self, obj, NULL); |
| 4719 | if (ret < 0) |
| 4720 | return NULL; |
| 4721 | Py_INCREF(Py_None); |
| 4722 | return Py_None; |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4723 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4724 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4725 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4726 | wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4727 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4728 | initproc func = (initproc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4729 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4730 | if (func(self, args, kwds) < 0) |
| 4731 | return NULL; |
| 4732 | Py_INCREF(Py_None); |
| 4733 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4734 | } |
| 4735 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4736 | static PyObject * |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4737 | tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4738 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4739 | PyTypeObject *type, *subtype, *staticbase; |
| 4740 | PyObject *arg0, *res; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4741 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4742 | if (self == NULL || !PyType_Check(self)) |
| 4743 | Py_FatalError("__new__() called with non-type 'self'"); |
| 4744 | type = (PyTypeObject *)self; |
| 4745 | if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) { |
| 4746 | PyErr_Format(PyExc_TypeError, |
| 4747 | "%s.__new__(): not enough arguments", |
| 4748 | type->tp_name); |
| 4749 | return NULL; |
| 4750 | } |
| 4751 | arg0 = PyTuple_GET_ITEM(args, 0); |
| 4752 | if (!PyType_Check(arg0)) { |
| 4753 | PyErr_Format(PyExc_TypeError, |
| 4754 | "%s.__new__(X): X is not a type object (%s)", |
| 4755 | type->tp_name, |
| 4756 | Py_TYPE(arg0)->tp_name); |
| 4757 | return NULL; |
| 4758 | } |
| 4759 | subtype = (PyTypeObject *)arg0; |
| 4760 | if (!PyType_IsSubtype(subtype, type)) { |
| 4761 | PyErr_Format(PyExc_TypeError, |
| 4762 | "%s.__new__(%s): %s is not a subtype of %s", |
| 4763 | type->tp_name, |
| 4764 | subtype->tp_name, |
| 4765 | subtype->tp_name, |
| 4766 | type->tp_name); |
| 4767 | return NULL; |
| 4768 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4769 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4770 | /* Check that the use doesn't do something silly and unsafe like |
| 4771 | object.__new__(dict). To do this, we check that the |
| 4772 | most derived base that's not a heap type is this type. */ |
| 4773 | staticbase = subtype; |
| 4774 | while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 4775 | staticbase = staticbase->tp_base; |
| 4776 | /* If staticbase is NULL now, it is a really weird type. |
| 4777 | In the spirit of backwards compatibility (?), just shut up. */ |
| 4778 | if (staticbase && staticbase->tp_new != type->tp_new) { |
| 4779 | PyErr_Format(PyExc_TypeError, |
| 4780 | "%s.__new__(%s) is not safe, use %s.__new__()", |
| 4781 | type->tp_name, |
| 4782 | subtype->tp_name, |
| 4783 | staticbase == NULL ? "?" : staticbase->tp_name); |
| 4784 | return NULL; |
| 4785 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4786 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4787 | args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 4788 | if (args == NULL) |
| 4789 | return NULL; |
| 4790 | res = type->tp_new(subtype, args, kwds); |
| 4791 | Py_DECREF(args); |
| 4792 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4793 | } |
| 4794 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4795 | static struct PyMethodDef tp_new_methoddef[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4796 | {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS, |
| 4797 | PyDoc_STR("T.__new__(S, ...) -> " |
| 4798 | "a new object with type S, a subtype of T")}, |
| 4799 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4800 | }; |
| 4801 | |
| 4802 | static int |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4803 | add_tp_new_wrapper(PyTypeObject *type) |
| 4804 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4805 | PyObject *func; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4806 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4807 | if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL) |
| 4808 | return 0; |
| 4809 | func = PyCFunction_New(tp_new_methoddef, (PyObject *)type); |
| 4810 | if (func == NULL) |
| 4811 | return -1; |
| 4812 | if (PyDict_SetItemString(type->tp_dict, "__new__", func)) { |
| 4813 | Py_DECREF(func); |
| 4814 | return -1; |
| 4815 | } |
| 4816 | Py_DECREF(func); |
| 4817 | return 0; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4818 | } |
| 4819 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4820 | /* Slot wrappers that call the corresponding __foo__ slot. See comments |
| 4821 | below at override_slots() for more explanation. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4822 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4823 | #define SLOT0(FUNCNAME, OPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4824 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4825 | FUNCNAME(PyObject *self) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4826 | { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4827 | static PyObject *cache_str; \ |
| 4828 | return call_method(self, OPSTR, &cache_str, "()"); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4829 | } |
| 4830 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4831 | #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4832 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4833 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4834 | { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4835 | static PyObject *cache_str; \ |
| 4836 | return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4837 | } |
| 4838 | |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4839 | /* Boolean helper for SLOT1BINFULL(). |
| 4840 | right.__class__ is a nontrivial subclass of left.__class__. */ |
| 4841 | static int |
| 4842 | method_is_overloaded(PyObject *left, PyObject *right, char *name) |
| 4843 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4844 | PyObject *a, *b; |
| 4845 | int ok; |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4846 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4847 | b = PyObject_GetAttrString((PyObject *)(Py_TYPE(right)), name); |
| 4848 | if (b == NULL) { |
| 4849 | PyErr_Clear(); |
| 4850 | /* If right doesn't have it, it's not overloaded */ |
| 4851 | return 0; |
| 4852 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4853 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4854 | a = PyObject_GetAttrString((PyObject *)(Py_TYPE(left)), name); |
| 4855 | if (a == NULL) { |
| 4856 | PyErr_Clear(); |
| 4857 | Py_DECREF(b); |
| 4858 | /* If right has it but left doesn't, it's overloaded */ |
| 4859 | return 1; |
| 4860 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4861 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4862 | ok = PyObject_RichCompareBool(a, b, Py_NE); |
| 4863 | Py_DECREF(a); |
| 4864 | Py_DECREF(b); |
| 4865 | if (ok < 0) { |
| 4866 | PyErr_Clear(); |
| 4867 | return 0; |
| 4868 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4870 | return ok; |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4871 | } |
| 4872 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4873 | |
| 4874 | #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4875 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4876 | FUNCNAME(PyObject *self, PyObject *other) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4877 | { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4878 | static PyObject *cache_str, *rcache_str; \ |
| 4879 | int do_other = Py_TYPE(self) != Py_TYPE(other) && \ |
| 4880 | Py_TYPE(other)->tp_as_number != NULL && \ |
| 4881 | Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \ |
| 4882 | if (Py_TYPE(self)->tp_as_number != NULL && \ |
| 4883 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ |
| 4884 | PyObject *r; \ |
| 4885 | if (do_other && \ |
| 4886 | PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \ |
| 4887 | method_is_overloaded(self, other, ROPSTR)) { \ |
| 4888 | r = call_maybe( \ |
| 4889 | other, ROPSTR, &rcache_str, "(O)", self); \ |
| 4890 | if (r != Py_NotImplemented) \ |
| 4891 | return r; \ |
| 4892 | Py_DECREF(r); \ |
| 4893 | do_other = 0; \ |
| 4894 | } \ |
| 4895 | r = call_maybe( \ |
| 4896 | self, OPSTR, &cache_str, "(O)", other); \ |
| 4897 | if (r != Py_NotImplemented || \ |
| 4898 | Py_TYPE(other) == Py_TYPE(self)) \ |
| 4899 | return r; \ |
| 4900 | Py_DECREF(r); \ |
| 4901 | } \ |
| 4902 | if (do_other) { \ |
| 4903 | return call_maybe( \ |
| 4904 | other, ROPSTR, &rcache_str, "(O)", self); \ |
| 4905 | } \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4906 | Py_RETURN_NOTIMPLEMENTED; \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4907 | } |
| 4908 | |
| 4909 | #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4910 | SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR) |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4911 | |
| 4912 | #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \ |
| 4913 | static PyObject * \ |
| 4914 | FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ |
| 4915 | { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4916 | static PyObject *cache_str; \ |
| 4917 | return call_method(self, OPSTR, &cache_str, \ |
| 4918 | "(" ARGCODES ")", arg1, arg2); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4919 | } |
| 4920 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4921 | static Py_ssize_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4922 | slot_sq_length(PyObject *self) |
| 4923 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4924 | static PyObject *len_str; |
| 4925 | PyObject *res = call_method(self, "__len__", &len_str, "()"); |
| 4926 | Py_ssize_t len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4927 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4928 | if (res == NULL) |
| 4929 | return -1; |
| 4930 | len = PyNumber_AsSsize_t(res, PyExc_OverflowError); |
| 4931 | Py_DECREF(res); |
| 4932 | if (len < 0) { |
| 4933 | if (!PyErr_Occurred()) |
| 4934 | PyErr_SetString(PyExc_ValueError, |
| 4935 | "__len__() should return >= 0"); |
| 4936 | return -1; |
| 4937 | } |
| 4938 | return len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4939 | } |
| 4940 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4941 | /* Super-optimized version of slot_sq_item. |
| 4942 | Other slots could do the same... */ |
| 4943 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4944 | slot_sq_item(PyObject *self, Py_ssize_t i) |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4945 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4946 | static PyObject *getitem_str; |
| 4947 | PyObject *func, *args = NULL, *ival = NULL, *retval = NULL; |
| 4948 | descrgetfunc f; |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4949 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4950 | if (getitem_str == NULL) { |
| 4951 | getitem_str = PyUnicode_InternFromString("__getitem__"); |
| 4952 | if (getitem_str == NULL) |
| 4953 | return NULL; |
| 4954 | } |
| 4955 | func = _PyType_Lookup(Py_TYPE(self), getitem_str); |
| 4956 | if (func != NULL) { |
| 4957 | if ((f = Py_TYPE(func)->tp_descr_get) == NULL) |
| 4958 | Py_INCREF(func); |
| 4959 | else { |
| 4960 | func = f(func, self, (PyObject *)(Py_TYPE(self))); |
| 4961 | if (func == NULL) { |
| 4962 | return NULL; |
| 4963 | } |
| 4964 | } |
| 4965 | ival = PyLong_FromSsize_t(i); |
| 4966 | if (ival != NULL) { |
| 4967 | args = PyTuple_New(1); |
| 4968 | if (args != NULL) { |
| 4969 | PyTuple_SET_ITEM(args, 0, ival); |
| 4970 | retval = PyObject_Call(func, args, NULL); |
| 4971 | Py_XDECREF(args); |
| 4972 | Py_XDECREF(func); |
| 4973 | return retval; |
| 4974 | } |
| 4975 | } |
| 4976 | } |
| 4977 | else { |
| 4978 | PyErr_SetObject(PyExc_AttributeError, getitem_str); |
| 4979 | } |
| 4980 | Py_XDECREF(args); |
| 4981 | Py_XDECREF(ival); |
| 4982 | Py_XDECREF(func); |
| 4983 | return NULL; |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4984 | } |
| 4985 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4986 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4987 | slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4988 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4989 | PyObject *res; |
| 4990 | static PyObject *delitem_str, *setitem_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4991 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4992 | if (value == NULL) |
| 4993 | res = call_method(self, "__delitem__", &delitem_str, |
| 4994 | "(n)", index); |
| 4995 | else |
| 4996 | res = call_method(self, "__setitem__", &setitem_str, |
| 4997 | "(nO)", index, value); |
| 4998 | if (res == NULL) |
| 4999 | return -1; |
| 5000 | Py_DECREF(res); |
| 5001 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5002 | } |
| 5003 | |
| 5004 | static int |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5005 | slot_sq_contains(PyObject *self, PyObject *value) |
| 5006 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5007 | PyObject *func, *res, *args; |
| 5008 | int result = -1; |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5009 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5010 | static PyObject *contains_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5012 | func = lookup_maybe(self, "__contains__", &contains_str); |
| 5013 | if (func != NULL) { |
| 5014 | args = PyTuple_Pack(1, value); |
| 5015 | if (args == NULL) |
| 5016 | res = NULL; |
| 5017 | else { |
| 5018 | res = PyObject_Call(func, args, NULL); |
| 5019 | Py_DECREF(args); |
| 5020 | } |
| 5021 | Py_DECREF(func); |
| 5022 | if (res != NULL) { |
| 5023 | result = PyObject_IsTrue(res); |
| 5024 | Py_DECREF(res); |
| 5025 | } |
| 5026 | } |
| 5027 | else if (! PyErr_Occurred()) { |
| 5028 | /* Possible results: -1 and 1 */ |
| 5029 | result = (int)_PySequence_IterSearch(self, value, |
| 5030 | PY_ITERSEARCH_CONTAINS); |
| 5031 | } |
| 5032 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5033 | } |
| 5034 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5035 | #define slot_mp_length slot_sq_length |
| 5036 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5037 | SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5038 | |
| 5039 | static int |
| 5040 | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) |
| 5041 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5042 | PyObject *res; |
| 5043 | static PyObject *delitem_str, *setitem_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5045 | if (value == NULL) |
| 5046 | res = call_method(self, "__delitem__", &delitem_str, |
| 5047 | "(O)", key); |
| 5048 | else |
| 5049 | res = call_method(self, "__setitem__", &setitem_str, |
| 5050 | "(OO)", key, value); |
| 5051 | if (res == NULL) |
| 5052 | return -1; |
| 5053 | Py_DECREF(res); |
| 5054 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5055 | } |
| 5056 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5057 | SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__") |
| 5058 | SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__") |
| 5059 | SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5060 | SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__") |
| 5061 | SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__") |
| 5062 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 5063 | static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5064 | |
| 5065 | SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5066 | nb_power, "__pow__", "__rpow__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5067 | |
| 5068 | static PyObject * |
| 5069 | slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) |
| 5070 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5071 | static PyObject *pow_str; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5072 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5073 | if (modulus == Py_None) |
| 5074 | return slot_nb_power_binary(self, other); |
| 5075 | /* Three-arg power doesn't use __rpow__. But ternary_op |
| 5076 | can call this when the second argument's type uses |
| 5077 | slot_nb_power, so check before calling self.__pow__. */ |
| 5078 | if (Py_TYPE(self)->tp_as_number != NULL && |
| 5079 | Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) { |
| 5080 | return call_method(self, "__pow__", &pow_str, |
| 5081 | "(OO)", other, modulus); |
| 5082 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5083 | Py_RETURN_NOTIMPLEMENTED; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5084 | } |
| 5085 | |
| 5086 | SLOT0(slot_nb_negative, "__neg__") |
| 5087 | SLOT0(slot_nb_positive, "__pos__") |
| 5088 | SLOT0(slot_nb_absolute, "__abs__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5089 | |
| 5090 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 5091 | slot_nb_bool(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5092 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5093 | PyObject *func, *args; |
| 5094 | static PyObject *bool_str, *len_str; |
| 5095 | int result = -1; |
| 5096 | int using_len = 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5097 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5098 | func = lookup_maybe(self, "__bool__", &bool_str); |
| 5099 | if (func == NULL) { |
| 5100 | if (PyErr_Occurred()) |
| 5101 | return -1; |
| 5102 | func = lookup_maybe(self, "__len__", &len_str); |
| 5103 | if (func == NULL) |
| 5104 | return PyErr_Occurred() ? -1 : 1; |
| 5105 | using_len = 1; |
| 5106 | } |
| 5107 | args = PyTuple_New(0); |
| 5108 | if (args != NULL) { |
| 5109 | PyObject *temp = PyObject_Call(func, args, NULL); |
| 5110 | Py_DECREF(args); |
| 5111 | if (temp != NULL) { |
| 5112 | if (using_len) { |
| 5113 | /* enforced by slot_nb_len */ |
| 5114 | result = PyObject_IsTrue(temp); |
| 5115 | } |
| 5116 | else if (PyBool_Check(temp)) { |
| 5117 | result = PyObject_IsTrue(temp); |
| 5118 | } |
| 5119 | else { |
| 5120 | PyErr_Format(PyExc_TypeError, |
| 5121 | "__bool__ should return " |
| 5122 | "bool, returned %s", |
| 5123 | Py_TYPE(temp)->tp_name); |
| 5124 | result = -1; |
| 5125 | } |
| 5126 | Py_DECREF(temp); |
| 5127 | } |
| 5128 | } |
| 5129 | Py_DECREF(func); |
| 5130 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5131 | } |
| 5132 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5133 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 5134 | static PyObject * |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5135 | slot_nb_index(PyObject *self) |
| 5136 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5137 | static PyObject *index_str; |
| 5138 | return call_method(self, "__index__", &index_str, "()"); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5139 | } |
| 5140 | |
| 5141 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5142 | SLOT0(slot_nb_invert, "__invert__") |
| 5143 | SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") |
| 5144 | SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") |
| 5145 | SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") |
| 5146 | SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") |
| 5147 | SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5148 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5149 | SLOT0(slot_nb_int, "__int__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5150 | SLOT0(slot_nb_float, "__float__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5151 | SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O") |
| 5152 | SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O") |
| 5153 | SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5154 | SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 5155 | /* Can't use SLOT1 here, because nb_inplace_power is ternary */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5156 | static PyObject * |
| 5157 | slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2) |
| 5158 | { |
| 5159 | static PyObject *cache_str; |
| 5160 | return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 5161 | } |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5162 | SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O") |
| 5163 | SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O") |
| 5164 | SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O") |
| 5165 | SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O") |
| 5166 | SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O") |
| 5167 | SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5168 | "__floordiv__", "__rfloordiv__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5169 | SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__") |
| 5170 | SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O") |
| 5171 | SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5172 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5173 | static PyObject * |
| 5174 | slot_tp_repr(PyObject *self) |
| 5175 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5176 | PyObject *func, *res; |
| 5177 | static PyObject *repr_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5178 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5179 | func = lookup_method(self, "__repr__", &repr_str); |
| 5180 | if (func != NULL) { |
| 5181 | res = PyEval_CallObject(func, NULL); |
| 5182 | Py_DECREF(func); |
| 5183 | return res; |
| 5184 | } |
| 5185 | PyErr_Clear(); |
| 5186 | return PyUnicode_FromFormat("<%s object at %p>", |
| 5187 | Py_TYPE(self)->tp_name, self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5188 | } |
| 5189 | |
| 5190 | static PyObject * |
| 5191 | slot_tp_str(PyObject *self) |
| 5192 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5193 | PyObject *func, *res; |
| 5194 | static PyObject *str_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5196 | func = lookup_method(self, "__str__", &str_str); |
| 5197 | if (func != NULL) { |
| 5198 | res = PyEval_CallObject(func, NULL); |
| 5199 | Py_DECREF(func); |
| 5200 | return res; |
| 5201 | } |
| 5202 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5203 | /* PyObject *ress; */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5204 | PyErr_Clear(); |
| 5205 | res = slot_tp_repr(self); |
| 5206 | if (!res) |
| 5207 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5208 | /* XXX this is non-sensical. Why should we return |
| 5209 | a bytes object from __str__. Is this code even |
| 5210 | used? - mvl */ |
| 5211 | assert(0); |
| 5212 | return res; |
| 5213 | /* |
Victor Stinner | f3fd733 | 2011-03-02 01:03:11 +0000 | [diff] [blame] | 5214 | ress = _PyUnicode_AsDefaultEncodedString(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5215 | Py_DECREF(res); |
| 5216 | return ress; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5217 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5218 | } |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5219 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5220 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5221 | static Py_hash_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5222 | slot_tp_hash(PyObject *self) |
| 5223 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5224 | PyObject *func, *res; |
| 5225 | static PyObject *hash_str; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5226 | Py_ssize_t h; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5227 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5228 | func = lookup_method(self, "__hash__", &hash_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5230 | if (func == Py_None) { |
| 5231 | Py_DECREF(func); |
| 5232 | func = NULL; |
| 5233 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 5234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5235 | if (func == NULL) { |
| 5236 | return PyObject_HashNotImplemented(self); |
| 5237 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 5238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5239 | res = PyEval_CallObject(func, NULL); |
| 5240 | Py_DECREF(func); |
| 5241 | if (res == NULL) |
| 5242 | return -1; |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 5243 | |
| 5244 | if (!PyLong_Check(res)) { |
| 5245 | PyErr_SetString(PyExc_TypeError, |
| 5246 | "__hash__ method should return an integer"); |
| 5247 | return -1; |
| 5248 | } |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5249 | /* Transform the PyLong `res` to a Py_hash_t `h`. For an existing |
| 5250 | hashable Python object x, hash(x) will always lie within the range of |
| 5251 | Py_hash_t. Therefore our transformation must preserve values that |
| 5252 | already lie within this range, to ensure that if x.__hash__() returns |
| 5253 | hash(y) then hash(x) == hash(y). */ |
| 5254 | h = PyLong_AsSsize_t(res); |
| 5255 | if (h == -1 && PyErr_Occurred()) { |
| 5256 | /* res was not within the range of a Py_hash_t, so we're free to |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 5257 | use any sufficiently bit-mixing transformation; |
| 5258 | long.__hash__ will do nicely. */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5259 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5260 | h = PyLong_Type.tp_hash(res); |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5261 | } |
Benjamin Peterson | e7dfeeb | 2010-10-17 21:27:01 +0000 | [diff] [blame] | 5262 | /* -1 is reserved for errors. */ |
| 5263 | if (h == -1) |
| 5264 | h = -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5265 | Py_DECREF(res); |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 5266 | return h; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5267 | } |
| 5268 | |
| 5269 | static PyObject * |
| 5270 | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 5271 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5272 | static PyObject *call_str; |
| 5273 | PyObject *meth = lookup_method(self, "__call__", &call_str); |
| 5274 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5276 | if (meth == NULL) |
| 5277 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5279 | res = PyObject_Call(meth, args, kwds); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5281 | Py_DECREF(meth); |
| 5282 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5283 | } |
| 5284 | |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5285 | /* There are two slot dispatch functions for tp_getattro. |
| 5286 | |
| 5287 | - slot_tp_getattro() is used when __getattribute__ is overridden |
| 5288 | but no __getattr__ hook is present; |
| 5289 | |
| 5290 | - slot_tp_getattr_hook() is used when a __getattr__ hook is present. |
| 5291 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5292 | The code in update_one_slot() always installs slot_tp_getattr_hook(); this |
| 5293 | detects the absence of __getattr__ and then installs the simpler slot if |
| 5294 | necessary. */ |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5295 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5296 | static PyObject * |
| 5297 | slot_tp_getattro(PyObject *self, PyObject *name) |
| 5298 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5299 | static PyObject *getattribute_str = NULL; |
| 5300 | return call_method(self, "__getattribute__", &getattribute_str, |
| 5301 | "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5302 | } |
| 5303 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5304 | static PyObject * |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5305 | call_attribute(PyObject *self, PyObject *attr, PyObject *name) |
| 5306 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5307 | PyObject *res, *descr = NULL; |
| 5308 | descrgetfunc f = Py_TYPE(attr)->tp_descr_get; |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5310 | if (f != NULL) { |
| 5311 | descr = f(attr, self, (PyObject *)(Py_TYPE(self))); |
| 5312 | if (descr == NULL) |
| 5313 | return NULL; |
| 5314 | else |
| 5315 | attr = descr; |
| 5316 | } |
| 5317 | res = PyObject_CallFunctionObjArgs(attr, name, NULL); |
| 5318 | Py_XDECREF(descr); |
| 5319 | return res; |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5320 | } |
| 5321 | |
| 5322 | static PyObject * |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5323 | slot_tp_getattr_hook(PyObject *self, PyObject *name) |
| 5324 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5325 | PyTypeObject *tp = Py_TYPE(self); |
| 5326 | PyObject *getattr, *getattribute, *res; |
| 5327 | static PyObject *getattribute_str = NULL; |
| 5328 | static PyObject *getattr_str = NULL; |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5329 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5330 | if (getattr_str == NULL) { |
| 5331 | getattr_str = PyUnicode_InternFromString("__getattr__"); |
| 5332 | if (getattr_str == NULL) |
| 5333 | return NULL; |
| 5334 | } |
| 5335 | if (getattribute_str == NULL) { |
| 5336 | getattribute_str = |
| 5337 | PyUnicode_InternFromString("__getattribute__"); |
| 5338 | if (getattribute_str == NULL) |
| 5339 | return NULL; |
| 5340 | } |
| 5341 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5342 | method fully for each attribute lookup for classes with |
| 5343 | __getattr__, even when the attribute is present. So we use |
| 5344 | _PyType_Lookup and create the method only when needed, with |
| 5345 | call_attribute. */ |
| 5346 | getattr = _PyType_Lookup(tp, getattr_str); |
| 5347 | if (getattr == NULL) { |
| 5348 | /* No __getattr__ hook: use a simpler dispatcher */ |
| 5349 | tp->tp_getattro = slot_tp_getattro; |
| 5350 | return slot_tp_getattro(self, name); |
| 5351 | } |
| 5352 | Py_INCREF(getattr); |
| 5353 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5354 | method fully for each attribute lookup for classes with |
| 5355 | __getattr__, even when self has the default __getattribute__ |
| 5356 | method. So we use _PyType_Lookup and create the method only when |
| 5357 | needed, with call_attribute. */ |
| 5358 | getattribute = _PyType_Lookup(tp, getattribute_str); |
| 5359 | if (getattribute == NULL || |
| 5360 | (Py_TYPE(getattribute) == &PyWrapperDescr_Type && |
| 5361 | ((PyWrapperDescrObject *)getattribute)->d_wrapped == |
| 5362 | (void *)PyObject_GenericGetAttr)) |
| 5363 | res = PyObject_GenericGetAttr(self, name); |
| 5364 | else { |
| 5365 | Py_INCREF(getattribute); |
| 5366 | res = call_attribute(self, getattribute, name); |
| 5367 | Py_DECREF(getattribute); |
| 5368 | } |
| 5369 | if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 5370 | PyErr_Clear(); |
| 5371 | res = call_attribute(self, getattr, name); |
| 5372 | } |
| 5373 | Py_DECREF(getattr); |
| 5374 | return res; |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5375 | } |
| 5376 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5377 | static int |
| 5378 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) |
| 5379 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5380 | PyObject *res; |
| 5381 | static PyObject *delattr_str, *setattr_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5382 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5383 | if (value == NULL) |
| 5384 | res = call_method(self, "__delattr__", &delattr_str, |
| 5385 | "(O)", name); |
| 5386 | else |
| 5387 | res = call_method(self, "__setattr__", &setattr_str, |
| 5388 | "(OO)", name, value); |
| 5389 | if (res == NULL) |
| 5390 | return -1; |
| 5391 | Py_DECREF(res); |
| 5392 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5393 | } |
| 5394 | |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 5395 | static char *name_op[] = { |
| 5396 | "__lt__", |
| 5397 | "__le__", |
| 5398 | "__eq__", |
| 5399 | "__ne__", |
| 5400 | "__gt__", |
| 5401 | "__ge__", |
| 5402 | }; |
| 5403 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5404 | static PyObject * |
Mark Dickinson | 6f1d049 | 2009-11-15 13:58:49 +0000 | [diff] [blame] | 5405 | slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5406 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5407 | PyObject *func, *args, *res; |
| 5408 | static PyObject *op_str[6]; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5410 | func = lookup_method(self, name_op[op], &op_str[op]); |
| 5411 | if (func == NULL) { |
| 5412 | PyErr_Clear(); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5413 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5414 | } |
| 5415 | args = PyTuple_Pack(1, other); |
| 5416 | if (args == NULL) |
| 5417 | res = NULL; |
| 5418 | else { |
| 5419 | res = PyObject_Call(func, args, NULL); |
| 5420 | Py_DECREF(args); |
| 5421 | } |
| 5422 | Py_DECREF(func); |
| 5423 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5424 | } |
| 5425 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5426 | static PyObject * |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5427 | slot_tp_iter(PyObject *self) |
| 5428 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5429 | PyObject *func, *res; |
| 5430 | static PyObject *iter_str, *getitem_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5431 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5432 | func = lookup_method(self, "__iter__", &iter_str); |
| 5433 | if (func != NULL) { |
| 5434 | PyObject *args; |
| 5435 | args = res = PyTuple_New(0); |
| 5436 | if (args != NULL) { |
| 5437 | res = PyObject_Call(func, args, NULL); |
| 5438 | Py_DECREF(args); |
| 5439 | } |
| 5440 | Py_DECREF(func); |
| 5441 | return res; |
| 5442 | } |
| 5443 | PyErr_Clear(); |
| 5444 | func = lookup_method(self, "__getitem__", &getitem_str); |
| 5445 | if (func == NULL) { |
| 5446 | PyErr_Format(PyExc_TypeError, |
| 5447 | "'%.200s' object is not iterable", |
| 5448 | Py_TYPE(self)->tp_name); |
| 5449 | return NULL; |
| 5450 | } |
| 5451 | Py_DECREF(func); |
| 5452 | return PySeqIter_New(self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5453 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5454 | |
| 5455 | static PyObject * |
| 5456 | slot_tp_iternext(PyObject *self) |
| 5457 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5458 | static PyObject *next_str; |
| 5459 | return call_method(self, "__next__", &next_str, "()"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5460 | } |
| 5461 | |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5462 | static PyObject * |
| 5463 | slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 5464 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5465 | PyTypeObject *tp = Py_TYPE(self); |
| 5466 | PyObject *get; |
| 5467 | static PyObject *get_str = NULL; |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5469 | if (get_str == NULL) { |
| 5470 | get_str = PyUnicode_InternFromString("__get__"); |
| 5471 | if (get_str == NULL) |
| 5472 | return NULL; |
| 5473 | } |
| 5474 | get = _PyType_Lookup(tp, get_str); |
| 5475 | if (get == NULL) { |
| 5476 | /* Avoid further slowdowns */ |
| 5477 | if (tp->tp_descr_get == slot_tp_descr_get) |
| 5478 | tp->tp_descr_get = NULL; |
| 5479 | Py_INCREF(self); |
| 5480 | return self; |
| 5481 | } |
| 5482 | if (obj == NULL) |
| 5483 | obj = Py_None; |
| 5484 | if (type == NULL) |
| 5485 | type = Py_None; |
| 5486 | return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5487 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5488 | |
| 5489 | static int |
| 5490 | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) |
| 5491 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5492 | PyObject *res; |
| 5493 | static PyObject *del_str, *set_str; |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5494 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5495 | if (value == NULL) |
| 5496 | res = call_method(self, "__delete__", &del_str, |
| 5497 | "(O)", target); |
| 5498 | else |
| 5499 | res = call_method(self, "__set__", &set_str, |
| 5500 | "(OO)", target, value); |
| 5501 | if (res == NULL) |
| 5502 | return -1; |
| 5503 | Py_DECREF(res); |
| 5504 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5505 | } |
| 5506 | |
| 5507 | static int |
| 5508 | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 5509 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5510 | static PyObject *init_str; |
| 5511 | PyObject *meth = lookup_method(self, "__init__", &init_str); |
| 5512 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5514 | if (meth == NULL) |
| 5515 | return -1; |
| 5516 | res = PyObject_Call(meth, args, kwds); |
| 5517 | Py_DECREF(meth); |
| 5518 | if (res == NULL) |
| 5519 | return -1; |
| 5520 | if (res != Py_None) { |
| 5521 | PyErr_Format(PyExc_TypeError, |
| 5522 | "__init__() should return None, not '%.200s'", |
| 5523 | Py_TYPE(res)->tp_name); |
| 5524 | Py_DECREF(res); |
| 5525 | return -1; |
| 5526 | } |
| 5527 | Py_DECREF(res); |
| 5528 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5529 | } |
| 5530 | |
| 5531 | static PyObject * |
| 5532 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 5533 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5534 | static PyObject *new_str; |
| 5535 | PyObject *func; |
| 5536 | PyObject *newargs, *x; |
| 5537 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5538 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5539 | if (new_str == NULL) { |
| 5540 | new_str = PyUnicode_InternFromString("__new__"); |
| 5541 | if (new_str == NULL) |
| 5542 | return NULL; |
| 5543 | } |
| 5544 | func = PyObject_GetAttr((PyObject *)type, new_str); |
| 5545 | if (func == NULL) |
| 5546 | return NULL; |
| 5547 | assert(PyTuple_Check(args)); |
| 5548 | n = PyTuple_GET_SIZE(args); |
| 5549 | newargs = PyTuple_New(n+1); |
| 5550 | if (newargs == NULL) |
| 5551 | return NULL; |
| 5552 | Py_INCREF(type); |
| 5553 | PyTuple_SET_ITEM(newargs, 0, (PyObject *)type); |
| 5554 | for (i = 0; i < n; i++) { |
| 5555 | x = PyTuple_GET_ITEM(args, i); |
| 5556 | Py_INCREF(x); |
| 5557 | PyTuple_SET_ITEM(newargs, i+1, x); |
| 5558 | } |
| 5559 | x = PyObject_Call(func, newargs, kwds); |
| 5560 | Py_DECREF(newargs); |
| 5561 | Py_DECREF(func); |
| 5562 | return x; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5563 | } |
| 5564 | |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5565 | static void |
| 5566 | slot_tp_del(PyObject *self) |
| 5567 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5568 | static PyObject *del_str = NULL; |
| 5569 | PyObject *del, *res; |
| 5570 | PyObject *error_type, *error_value, *error_traceback; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5572 | /* Temporarily resurrect the object. */ |
| 5573 | assert(self->ob_refcnt == 0); |
| 5574 | self->ob_refcnt = 1; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5575 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5576 | /* Save the current exception, if any. */ |
| 5577 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5578 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5579 | /* Execute __del__ method, if any. */ |
| 5580 | del = lookup_maybe(self, "__del__", &del_str); |
| 5581 | if (del != NULL) { |
| 5582 | res = PyEval_CallObject(del, NULL); |
| 5583 | if (res == NULL) |
| 5584 | PyErr_WriteUnraisable(del); |
| 5585 | else |
| 5586 | Py_DECREF(res); |
| 5587 | Py_DECREF(del); |
| 5588 | } |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5589 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5590 | /* Restore the saved exception. */ |
| 5591 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5592 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5593 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 5594 | * cause a recursive call. |
| 5595 | */ |
| 5596 | assert(self->ob_refcnt > 0); |
| 5597 | if (--self->ob_refcnt == 0) |
| 5598 | return; /* this is the normal path out */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5599 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5600 | /* __del__ resurrected it! Make it look like the original Py_DECREF |
| 5601 | * never happened. |
| 5602 | */ |
| 5603 | { |
| 5604 | Py_ssize_t refcnt = self->ob_refcnt; |
| 5605 | _Py_NewReference(self); |
| 5606 | self->ob_refcnt = refcnt; |
| 5607 | } |
| 5608 | assert(!PyType_IS_GC(Py_TYPE(self)) || |
| 5609 | _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED); |
| 5610 | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so |
| 5611 | * we need to undo that. */ |
| 5612 | _Py_DEC_REFTOTAL; |
| 5613 | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object |
| 5614 | * chain, so no more to do there. |
| 5615 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
| 5616 | * _Py_NewReference bumped tp_allocs: both of those need to be |
| 5617 | * undone. |
| 5618 | */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5619 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5620 | --Py_TYPE(self)->tp_frees; |
| 5621 | --Py_TYPE(self)->tp_allocs; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5622 | #endif |
| 5623 | } |
| 5624 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5625 | |
| 5626 | /* 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] | 5627 | functions. The offsets here are relative to the 'PyHeapTypeObject' |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5628 | structure, which incorporates the additional structures used for numbers, |
| 5629 | sequences and mappings. |
| 5630 | 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] | 5631 | __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] | 5632 | slots (e.g. __str__ affects tp_str as well as tp_repr). The table is |
| 5633 | terminated with an all-zero entry. (This table is further initialized and |
| 5634 | sorted in init_slotdefs() below.) */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5635 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5636 | typedef struct wrapperbase slotdef; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5637 | |
| 5638 | #undef TPSLOT |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5639 | #undef FLSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5640 | #undef ETSLOT |
| 5641 | #undef SQSLOT |
| 5642 | #undef MPSLOT |
| 5643 | #undef NBSLOT |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5644 | #undef UNSLOT |
| 5645 | #undef IBSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5646 | #undef BINSLOT |
| 5647 | #undef RBINSLOT |
| 5648 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5649 | #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5650 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5651 | PyDoc_STR(DOC)} |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5652 | #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5653 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5654 | PyDoc_STR(DOC), FLAGS} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5655 | #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5656 | {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5657 | PyDoc_STR(DOC)} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5658 | #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5659 | ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5660 | #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5661 | ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5662 | #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5663 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5664 | #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5665 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5666 | "x." NAME "() <==> " DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5667 | #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5668 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5669 | "x." NAME "(y) <==> x" DOC "y") |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5670 | #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5671 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5672 | "x." NAME "(y) <==> x" DOC "y") |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5673 | #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5674 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5675 | "x." NAME "(y) <==> y" DOC "x") |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5676 | #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5677 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5678 | "x." NAME "(y) <==> " DOC) |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5679 | #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5680 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5681 | "x." NAME "(y) <==> " DOC) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5682 | |
| 5683 | static slotdef slotdefs[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5684 | SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc, |
| 5685 | "x.__len__() <==> len(x)"), |
| 5686 | /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL. |
| 5687 | The logic in abstract.c always falls back to nb_add/nb_multiply in |
| 5688 | this case. Defining both the nb_* and the sq_* slots to call the |
| 5689 | user-defined methods has unexpected side-effects, as shown by |
| 5690 | test_descr.notimplemented() */ |
| 5691 | SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc, |
| 5692 | "x.__add__(y) <==> x+y"), |
| 5693 | SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc, |
| 5694 | "x.__mul__(n) <==> x*n"), |
| 5695 | SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc, |
| 5696 | "x.__rmul__(n) <==> n*x"), |
| 5697 | SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, |
| 5698 | "x.__getitem__(y) <==> x[y]"), |
| 5699 | SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, |
| 5700 | "x.__setitem__(i, y) <==> x[i]=y"), |
| 5701 | SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, |
| 5702 | "x.__delitem__(y) <==> del x[y]"), |
| 5703 | SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, |
| 5704 | "x.__contains__(y) <==> y in x"), |
| 5705 | SQSLOT("__iadd__", sq_inplace_concat, NULL, |
| 5706 | wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"), |
| 5707 | SQSLOT("__imul__", sq_inplace_repeat, NULL, |
| 5708 | wrap_indexargfunc, "x.__imul__(y) <==> x*=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5709 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5710 | MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc, |
| 5711 | "x.__len__() <==> len(x)"), |
| 5712 | MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, |
| 5713 | wrap_binaryfunc, |
| 5714 | "x.__getitem__(y) <==> x[y]"), |
| 5715 | MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, |
| 5716 | wrap_objobjargproc, |
| 5717 | "x.__setitem__(i, y) <==> x[i]=y"), |
| 5718 | MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, |
| 5719 | wrap_delitem, |
| 5720 | "x.__delitem__(y) <==> del x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5721 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5722 | BINSLOT("__add__", nb_add, slot_nb_add, |
| 5723 | "+"), |
| 5724 | RBINSLOT("__radd__", nb_add, slot_nb_add, |
| 5725 | "+"), |
| 5726 | BINSLOT("__sub__", nb_subtract, slot_nb_subtract, |
| 5727 | "-"), |
| 5728 | RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract, |
| 5729 | "-"), |
| 5730 | BINSLOT("__mul__", nb_multiply, slot_nb_multiply, |
| 5731 | "*"), |
| 5732 | RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply, |
| 5733 | "*"), |
| 5734 | BINSLOT("__mod__", nb_remainder, slot_nb_remainder, |
| 5735 | "%"), |
| 5736 | RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder, |
| 5737 | "%"), |
| 5738 | BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod, |
| 5739 | "divmod(x, y)"), |
| 5740 | RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod, |
| 5741 | "divmod(y, x)"), |
| 5742 | NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, |
| 5743 | "x.__pow__(y[, z]) <==> pow(x, y[, z])"), |
| 5744 | NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, |
| 5745 | "y.__rpow__(x[, z]) <==> pow(x, y[, z])"), |
| 5746 | UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"), |
| 5747 | UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"), |
| 5748 | UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, |
| 5749 | "abs(x)"), |
| 5750 | UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred, |
| 5751 | "x != 0"), |
| 5752 | UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"), |
| 5753 | BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5754 | RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5755 | BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5756 | RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5757 | BINSLOT("__and__", nb_and, slot_nb_and, "&"), |
| 5758 | RBINSLOT("__rand__", nb_and, slot_nb_and, "&"), |
| 5759 | BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"), |
| 5760 | RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), |
| 5761 | BINSLOT("__or__", nb_or, slot_nb_or, "|"), |
| 5762 | RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), |
| 5763 | UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, |
| 5764 | "int(x)"), |
| 5765 | UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, |
| 5766 | "float(x)"), |
| 5767 | NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc, |
| 5768 | "x[y:z] <==> x[y.__index__():z.__index__()]"), |
| 5769 | IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5770 | wrap_binaryfunc, "+="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5771 | IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5772 | wrap_binaryfunc, "-="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5773 | IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5774 | wrap_binaryfunc, "*="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5775 | IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5776 | wrap_binaryfunc, "%="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5777 | IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5778 | wrap_binaryfunc, "**="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5779 | IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5780 | wrap_binaryfunc, "<<="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5781 | IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5782 | wrap_binaryfunc, ">>="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5783 | IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5784 | wrap_binaryfunc, "&="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5785 | IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5786 | wrap_binaryfunc, "^="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5787 | IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 5788 | wrap_binaryfunc, "|="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5789 | BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5790 | RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5791 | BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5792 | RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5793 | IBSLOT("__ifloordiv__", nb_inplace_floor_divide, |
| 5794 | slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"), |
| 5795 | IBSLOT("__itruediv__", nb_inplace_true_divide, |
| 5796 | slot_nb_inplace_true_divide, wrap_binaryfunc, "/"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5797 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5798 | TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, |
| 5799 | "x.__str__() <==> str(x)"), |
| 5800 | TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, |
| 5801 | "x.__repr__() <==> repr(x)"), |
| 5802 | TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, |
| 5803 | "x.__hash__() <==> hash(x)"), |
| 5804 | FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, |
| 5805 | "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS), |
| 5806 | TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, |
| 5807 | wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"), |
| 5808 | TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), |
| 5809 | TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), |
| 5810 | TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), |
| 5811 | TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, |
| 5812 | "x.__setattr__('name', value) <==> x.name = value"), |
| 5813 | TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), |
| 5814 | TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, |
| 5815 | "x.__delattr__('name') <==> del x.name"), |
| 5816 | TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), |
| 5817 | TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, |
| 5818 | "x.__lt__(y) <==> x<y"), |
| 5819 | TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, |
| 5820 | "x.__le__(y) <==> x<=y"), |
| 5821 | TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, |
| 5822 | "x.__eq__(y) <==> x==y"), |
| 5823 | TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, |
| 5824 | "x.__ne__(y) <==> x!=y"), |
| 5825 | TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, |
| 5826 | "x.__gt__(y) <==> x>y"), |
| 5827 | TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, |
| 5828 | "x.__ge__(y) <==> x>=y"), |
| 5829 | TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, |
| 5830 | "x.__iter__() <==> iter(x)"), |
| 5831 | TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next, |
| 5832 | "x.__next__() <==> next(x)"), |
| 5833 | TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, |
| 5834 | "descr.__get__(obj[, type]) -> value"), |
| 5835 | TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, |
| 5836 | "descr.__set__(obj, value)"), |
| 5837 | TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set, |
| 5838 | wrap_descr_delete, "descr.__delete__(obj)"), |
| 5839 | FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init, |
| 5840 | "x.__init__(...) initializes x; " |
Alexander Belopolsky | 977a684 | 2010-08-16 20:17:07 +0000 | [diff] [blame] | 5841 | "see help(type(x)) for signature", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5842 | PyWrapperFlag_KEYWORDS), |
| 5843 | TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""), |
| 5844 | TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""), |
| 5845 | {NULL} |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5846 | }; |
| 5847 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5848 | /* Given a type pointer and an offset gotten from a slotdef entry, return a |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5849 | 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] | 5850 | the offset to the type pointer, since it takes care to indirect through the |
| 5851 | proper indirection pointer (as_buffer, etc.); it returns NULL if the |
| 5852 | indirection pointer is NULL. */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5853 | static void ** |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5854 | slotptr(PyTypeObject *type, int ioffset) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5855 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5856 | char *ptr; |
| 5857 | long offset = ioffset; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5858 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5859 | /* Note: this depends on the order of the members of PyHeapTypeObject! */ |
| 5860 | assert(offset >= 0); |
| 5861 | assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer)); |
| 5862 | if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) { |
| 5863 | ptr = (char *)type->tp_as_sequence; |
| 5864 | offset -= offsetof(PyHeapTypeObject, as_sequence); |
| 5865 | } |
| 5866 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) { |
| 5867 | ptr = (char *)type->tp_as_mapping; |
| 5868 | offset -= offsetof(PyHeapTypeObject, as_mapping); |
| 5869 | } |
| 5870 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) { |
| 5871 | ptr = (char *)type->tp_as_number; |
| 5872 | offset -= offsetof(PyHeapTypeObject, as_number); |
| 5873 | } |
| 5874 | else { |
| 5875 | ptr = (char *)type; |
| 5876 | } |
| 5877 | if (ptr != NULL) |
| 5878 | ptr += offset; |
| 5879 | return (void **)ptr; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5880 | } |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 5881 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5882 | /* Length of array of slotdef pointers used to store slots with the |
| 5883 | same __name__. There should be at most MAX_EQUIV-1 slotdef entries with |
| 5884 | the same __name__, for any __name__. Since that's a static property, it is |
| 5885 | appropriate to declare fixed-size arrays for this. */ |
| 5886 | #define MAX_EQUIV 10 |
| 5887 | |
| 5888 | /* Return a slot pointer for a given name, but ONLY if the attribute has |
| 5889 | exactly one slot function. The name must be an interned string. */ |
| 5890 | static void ** |
| 5891 | resolve_slotdups(PyTypeObject *type, PyObject *name) |
| 5892 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5893 | /* XXX Maybe this could be optimized more -- but is it worth it? */ |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5894 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5895 | /* pname and ptrs act as a little cache */ |
| 5896 | static PyObject *pname; |
| 5897 | static slotdef *ptrs[MAX_EQUIV]; |
| 5898 | slotdef *p, **pp; |
| 5899 | void **res, **ptr; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5900 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5901 | if (pname != name) { |
| 5902 | /* Collect all slotdefs that match name into ptrs. */ |
| 5903 | pname = name; |
| 5904 | pp = ptrs; |
| 5905 | for (p = slotdefs; p->name_strobj; p++) { |
| 5906 | if (p->name_strobj == name) |
| 5907 | *pp++ = p; |
| 5908 | } |
| 5909 | *pp = NULL; |
| 5910 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5911 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5912 | /* Look in all matching slots of the type; if exactly one of these has |
| 5913 | a filled-in slot, return its value. Otherwise return NULL. */ |
| 5914 | res = NULL; |
| 5915 | for (pp = ptrs; *pp; pp++) { |
| 5916 | ptr = slotptr(type, (*pp)->offset); |
| 5917 | if (ptr == NULL || *ptr == NULL) |
| 5918 | continue; |
| 5919 | if (res != NULL) |
| 5920 | return NULL; |
| 5921 | res = ptr; |
| 5922 | } |
| 5923 | return res; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5924 | } |
| 5925 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5926 | /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5927 | does some incredibly complex thinking and then sticks something into the |
| 5928 | slot. (It sees if the adjacent slotdefs for the same slot have conflicting |
| 5929 | interests, and then stores a generic wrapper or a specific function into |
| 5930 | the slot.) Return a pointer to the next slotdef with a different offset, |
| 5931 | because that's convenient for fixup_slot_dispatchers(). */ |
| 5932 | static slotdef * |
| 5933 | update_one_slot(PyTypeObject *type, slotdef *p) |
| 5934 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5935 | PyObject *descr; |
| 5936 | PyWrapperDescrObject *d; |
| 5937 | void *generic = NULL, *specific = NULL; |
| 5938 | int use_generic = 0; |
| 5939 | int offset = p->offset; |
| 5940 | void **ptr = slotptr(type, offset); |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5941 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5942 | if (ptr == NULL) { |
| 5943 | do { |
| 5944 | ++p; |
| 5945 | } while (p->offset == offset); |
| 5946 | return p; |
| 5947 | } |
| 5948 | do { |
| 5949 | descr = _PyType_Lookup(type, p->name_strobj); |
| 5950 | if (descr == NULL) { |
| 5951 | if (ptr == (void**)&type->tp_iternext) { |
| 5952 | specific = _PyObject_NextNotImplemented; |
| 5953 | } |
| 5954 | continue; |
| 5955 | } |
| 5956 | if (Py_TYPE(descr) == &PyWrapperDescr_Type) { |
| 5957 | void **tptr = resolve_slotdups(type, p->name_strobj); |
| 5958 | if (tptr == NULL || tptr == ptr) |
| 5959 | generic = p->function; |
| 5960 | d = (PyWrapperDescrObject *)descr; |
| 5961 | if (d->d_base->wrapper == p->wrapper && |
| 5962 | PyType_IsSubtype(type, PyDescr_TYPE(d))) |
| 5963 | { |
| 5964 | if (specific == NULL || |
| 5965 | specific == d->d_wrapped) |
| 5966 | specific = d->d_wrapped; |
| 5967 | else |
| 5968 | use_generic = 1; |
| 5969 | } |
| 5970 | } |
| 5971 | else if (Py_TYPE(descr) == &PyCFunction_Type && |
| 5972 | PyCFunction_GET_FUNCTION(descr) == |
| 5973 | (PyCFunction)tp_new_wrapper && |
| 5974 | ptr == (void**)&type->tp_new) |
| 5975 | { |
| 5976 | /* The __new__ wrapper is not a wrapper descriptor, |
| 5977 | so must be special-cased differently. |
| 5978 | If we don't do this, creating an instance will |
| 5979 | always use slot_tp_new which will look up |
| 5980 | __new__ in the MRO which will call tp_new_wrapper |
| 5981 | which will look through the base classes looking |
| 5982 | for a static base and call its tp_new (usually |
| 5983 | PyType_GenericNew), after performing various |
| 5984 | sanity checks and constructing a new argument |
| 5985 | list. Cut all that nonsense short -- this speeds |
| 5986 | up instance creation tremendously. */ |
| 5987 | specific = (void *)type->tp_new; |
| 5988 | /* XXX I'm not 100% sure that there isn't a hole |
| 5989 | in this reasoning that requires additional |
| 5990 | sanity checks. I'll buy the first person to |
| 5991 | point out a bug in this reasoning a beer. */ |
| 5992 | } |
| 5993 | else if (descr == Py_None && |
| 5994 | ptr == (void**)&type->tp_hash) { |
| 5995 | /* We specifically allow __hash__ to be set to None |
| 5996 | to prevent inheritance of the default |
| 5997 | implementation from object.__hash__ */ |
| 5998 | specific = PyObject_HashNotImplemented; |
| 5999 | } |
| 6000 | else { |
| 6001 | use_generic = 1; |
| 6002 | generic = p->function; |
| 6003 | } |
| 6004 | } while ((++p)->offset == offset); |
| 6005 | if (specific && !use_generic) |
| 6006 | *ptr = specific; |
| 6007 | else |
| 6008 | *ptr = generic; |
| 6009 | return p; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6010 | } |
| 6011 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6012 | /* In the type, update the slots whose slotdefs are gathered in the pp array. |
| 6013 | This is a callback for update_subclasses(). */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6014 | static int |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6015 | update_slots_callback(PyTypeObject *type, void *data) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6016 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6017 | slotdef **pp = (slotdef **)data; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6018 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6019 | for (; *pp; pp++) |
| 6020 | update_one_slot(type, *pp); |
| 6021 | return 0; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6022 | } |
| 6023 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6024 | /* Comparison function for qsort() to compare slotdefs by their offset, and |
| 6025 | for equal offset by their address (to force a stable sort). */ |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6026 | static int |
| 6027 | slotdef_cmp(const void *aa, const void *bb) |
| 6028 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6029 | const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb; |
| 6030 | int c = a->offset - b->offset; |
| 6031 | if (c != 0) |
| 6032 | return c; |
| 6033 | else |
| 6034 | /* Cannot use a-b, as this gives off_t, |
| 6035 | which may lose precision when converted to int. */ |
| 6036 | return (a > b) ? 1 : (a < b) ? -1 : 0; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6037 | } |
| 6038 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6039 | /* Initialize the slotdefs table by adding interned string objects for the |
| 6040 | names and sorting the entries. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6041 | static void |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6042 | init_slotdefs(void) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6043 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6044 | slotdef *p; |
| 6045 | static int initialized = 0; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6047 | if (initialized) |
| 6048 | return; |
| 6049 | for (p = slotdefs; p->name; p++) { |
| 6050 | p->name_strobj = PyUnicode_InternFromString(p->name); |
| 6051 | if (!p->name_strobj) |
| 6052 | Py_FatalError("Out of memory interning slotdef names"); |
| 6053 | } |
| 6054 | qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef), |
| 6055 | slotdef_cmp); |
| 6056 | initialized = 1; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6057 | } |
| 6058 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6059 | /* Update the slots after assignment to a class (type) attribute. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6060 | static int |
| 6061 | update_slot(PyTypeObject *type, PyObject *name) |
| 6062 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6063 | slotdef *ptrs[MAX_EQUIV]; |
| 6064 | slotdef *p; |
| 6065 | slotdef **pp; |
| 6066 | int offset; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6067 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6068 | /* Clear the VALID_VERSION flag of 'type' and all its |
| 6069 | subclasses. This could possibly be unified with the |
| 6070 | update_subclasses() recursion below, but carefully: |
| 6071 | they each have their own conditions on which to stop |
| 6072 | recursing into subclasses. */ |
| 6073 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 6074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6075 | init_slotdefs(); |
| 6076 | pp = ptrs; |
| 6077 | for (p = slotdefs; p->name; p++) { |
| 6078 | /* XXX assume name is interned! */ |
| 6079 | if (p->name_strobj == name) |
| 6080 | *pp++ = p; |
| 6081 | } |
| 6082 | *pp = NULL; |
| 6083 | for (pp = ptrs; *pp; pp++) { |
| 6084 | p = *pp; |
| 6085 | offset = p->offset; |
| 6086 | while (p > slotdefs && (p-1)->offset == offset) |
| 6087 | --p; |
| 6088 | *pp = p; |
| 6089 | } |
| 6090 | if (ptrs[0] == NULL) |
| 6091 | return 0; /* Not an attribute that affects any slots */ |
| 6092 | return update_subclasses(type, name, |
| 6093 | update_slots_callback, (void *)ptrs); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6094 | } |
| 6095 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6096 | /* Store the proper functions in the slot dispatches at class (type) |
| 6097 | definition time, based upon which operations the class overrides in its |
| 6098 | dict. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6099 | static void |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6100 | fixup_slot_dispatchers(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6101 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6102 | slotdef *p; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6104 | init_slotdefs(); |
| 6105 | for (p = slotdefs; p->name; ) |
| 6106 | p = update_one_slot(type, p); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6107 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6108 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 6109 | static void |
| 6110 | update_all_slots(PyTypeObject* type) |
| 6111 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6112 | slotdef *p; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 6113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6114 | init_slotdefs(); |
| 6115 | for (p = slotdefs; p->name; p++) { |
| 6116 | /* update_slot returns int but can't actually fail */ |
| 6117 | update_slot(type, p->name_strobj); |
| 6118 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 6119 | } |
| 6120 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6121 | /* recurse_down_subclasses() and update_subclasses() are mutually |
| 6122 | recursive functions to call a callback for all subclasses, |
| 6123 | but refraining from recursing into subclasses that define 'name'. */ |
| 6124 | |
| 6125 | static int |
| 6126 | update_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6127 | update_callback callback, void *data) |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6128 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6129 | if (callback(type, data) < 0) |
| 6130 | return -1; |
| 6131 | return recurse_down_subclasses(type, name, callback, data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6132 | } |
| 6133 | |
| 6134 | static int |
| 6135 | recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6136 | update_callback callback, void *data) |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6137 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6138 | PyTypeObject *subclass; |
| 6139 | PyObject *ref, *subclasses, *dict; |
| 6140 | Py_ssize_t i, n; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6141 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6142 | subclasses = type->tp_subclasses; |
| 6143 | if (subclasses == NULL) |
| 6144 | return 0; |
| 6145 | assert(PyList_Check(subclasses)); |
| 6146 | n = PyList_GET_SIZE(subclasses); |
| 6147 | for (i = 0; i < n; i++) { |
| 6148 | ref = PyList_GET_ITEM(subclasses, i); |
| 6149 | assert(PyWeakref_CheckRef(ref)); |
| 6150 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 6151 | assert(subclass != NULL); |
| 6152 | if ((PyObject *)subclass == Py_None) |
| 6153 | continue; |
| 6154 | assert(PyType_Check(subclass)); |
| 6155 | /* Avoid recursing down into unaffected classes */ |
| 6156 | dict = subclass->tp_dict; |
| 6157 | if (dict != NULL && PyDict_Check(dict) && |
| 6158 | PyDict_GetItem(dict, name) != NULL) |
| 6159 | continue; |
| 6160 | if (update_subclasses(subclass, name, callback, data) < 0) |
| 6161 | return -1; |
| 6162 | } |
| 6163 | return 0; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6164 | } |
| 6165 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6166 | /* This function is called by PyType_Ready() to populate the type's |
| 6167 | dictionary with method descriptors for function slots. For each |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6168 | function slot (like tp_repr) that's defined in the type, one or more |
| 6169 | corresponding descriptors are added in the type's tp_dict dictionary |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6170 | under the appropriate name (like __repr__). Some function slots |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6171 | cause more than one descriptor to be added (for example, the nb_add |
| 6172 | slot adds both __add__ and __radd__ descriptors) and some function |
| 6173 | slots compete for the same descriptor (for example both sq_item and |
| 6174 | mp_subscript generate a __getitem__ descriptor). |
| 6175 | |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 6176 | In the latter case, the first slotdef entry encountered wins. Since |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 6177 | 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] | 6178 | PyHeapTypeObject, this gives us some control over disambiguating |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6179 | between competing slots: the members of PyHeapTypeObject are listed |
| 6180 | from most general to least general, so the most general slot is |
| 6181 | preferred. In particular, because as_mapping comes before as_sequence, |
| 6182 | for a type that defines both mp_subscript and sq_item, mp_subscript |
| 6183 | wins. |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6184 | |
| 6185 | This only adds new descriptors and doesn't overwrite entries in |
| 6186 | tp_dict that were previously defined. The descriptors contain a |
| 6187 | reference to the C function they must call, so that it's safe if they |
| 6188 | are copied into a subtype's __dict__ and the subtype has a different |
| 6189 | C function in its slot -- calling the method defined by the |
| 6190 | descriptor will call the C function that was used to create it, |
| 6191 | rather than the C function present in the slot when it is called. |
| 6192 | (This is important because a subtype may have a C function in the |
| 6193 | slot that calls the method from the dictionary, and we want to avoid |
| 6194 | infinite recursion here.) */ |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6195 | |
| 6196 | static int |
| 6197 | add_operators(PyTypeObject *type) |
| 6198 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6199 | PyObject *dict = type->tp_dict; |
| 6200 | slotdef *p; |
| 6201 | PyObject *descr; |
| 6202 | void **ptr; |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6204 | init_slotdefs(); |
| 6205 | for (p = slotdefs; p->name; p++) { |
| 6206 | if (p->wrapper == NULL) |
| 6207 | continue; |
| 6208 | ptr = slotptr(type, p->offset); |
| 6209 | if (!ptr || !*ptr) |
| 6210 | continue; |
| 6211 | if (PyDict_GetItem(dict, p->name_strobj)) |
| 6212 | continue; |
| 6213 | if (*ptr == PyObject_HashNotImplemented) { |
| 6214 | /* Classes may prevent the inheritance of the tp_hash |
| 6215 | slot by storing PyObject_HashNotImplemented in it. Make it |
| 6216 | visible as a None value for the __hash__ attribute. */ |
| 6217 | if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0) |
| 6218 | return -1; |
| 6219 | } |
| 6220 | else { |
| 6221 | descr = PyDescr_NewWrapper(type, p, *ptr); |
| 6222 | if (descr == NULL) |
| 6223 | return -1; |
| 6224 | if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) |
| 6225 | return -1; |
| 6226 | Py_DECREF(descr); |
| 6227 | } |
| 6228 | } |
| 6229 | if (type->tp_new != NULL) { |
| 6230 | if (add_tp_new_wrapper(type) < 0) |
| 6231 | return -1; |
| 6232 | } |
| 6233 | return 0; |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6234 | } |
| 6235 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6236 | |
| 6237 | /* Cooperative 'super' */ |
| 6238 | |
| 6239 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6240 | PyObject_HEAD |
| 6241 | PyTypeObject *type; |
| 6242 | PyObject *obj; |
| 6243 | PyTypeObject *obj_type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6244 | } superobject; |
| 6245 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 6246 | static PyMemberDef super_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6247 | {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY, |
| 6248 | "the class invoking super()"}, |
| 6249 | {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY, |
| 6250 | "the instance invoking super(); may be None"}, |
| 6251 | {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY, |
| 6252 | "the type of the instance invoking super(); may be None"}, |
| 6253 | {0} |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6254 | }; |
| 6255 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6256 | static void |
| 6257 | super_dealloc(PyObject *self) |
| 6258 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6259 | superobject *su = (superobject *)self; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6260 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6261 | _PyObject_GC_UNTRACK(self); |
| 6262 | Py_XDECREF(su->obj); |
| 6263 | Py_XDECREF(su->type); |
| 6264 | Py_XDECREF(su->obj_type); |
| 6265 | Py_TYPE(self)->tp_free(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6266 | } |
| 6267 | |
| 6268 | static PyObject * |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6269 | super_repr(PyObject *self) |
| 6270 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6271 | superobject *su = (superobject *)self; |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6273 | if (su->obj_type) |
| 6274 | return PyUnicode_FromFormat( |
| 6275 | "<super: <class '%s'>, <%s object>>", |
| 6276 | su->type ? su->type->tp_name : "NULL", |
| 6277 | su->obj_type->tp_name); |
| 6278 | else |
| 6279 | return PyUnicode_FromFormat( |
| 6280 | "<super: <class '%s'>, NULL>", |
| 6281 | su->type ? su->type->tp_name : "NULL"); |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6282 | } |
| 6283 | |
| 6284 | static PyObject * |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6285 | super_getattro(PyObject *self, PyObject *name) |
| 6286 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6287 | superobject *su = (superobject *)self; |
| 6288 | int skip = su->obj_type == NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6290 | if (!skip) { |
| 6291 | /* We want __class__ to return the class of the super object |
| 6292 | (i.e. super, or a subclass), not the class of su->obj. */ |
| 6293 | skip = (PyUnicode_Check(name) && |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6294 | PyUnicode_GET_LENGTH(name) == 9 && |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6295 | PyUnicode_CompareWithASCIIString(name, "__class__") == 0); |
| 6296 | } |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 6297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6298 | if (!skip) { |
| 6299 | PyObject *mro, *res, *tmp, *dict; |
| 6300 | PyTypeObject *starttype; |
| 6301 | descrgetfunc f; |
| 6302 | Py_ssize_t i, n; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6304 | starttype = su->obj_type; |
| 6305 | mro = starttype->tp_mro; |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 6306 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6307 | if (mro == NULL) |
| 6308 | n = 0; |
| 6309 | else { |
| 6310 | assert(PyTuple_Check(mro)); |
| 6311 | n = PyTuple_GET_SIZE(mro); |
| 6312 | } |
| 6313 | for (i = 0; i < n; i++) { |
| 6314 | if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) |
| 6315 | break; |
| 6316 | } |
| 6317 | i++; |
| 6318 | res = NULL; |
| 6319 | for (; i < n; i++) { |
| 6320 | tmp = PyTuple_GET_ITEM(mro, i); |
| 6321 | if (PyType_Check(tmp)) |
| 6322 | dict = ((PyTypeObject *)tmp)->tp_dict; |
| 6323 | else |
| 6324 | continue; |
| 6325 | res = PyDict_GetItem(dict, name); |
| 6326 | if (res != NULL) { |
| 6327 | Py_INCREF(res); |
| 6328 | f = Py_TYPE(res)->tp_descr_get; |
| 6329 | if (f != NULL) { |
| 6330 | tmp = f(res, |
| 6331 | /* Only pass 'obj' param if |
| 6332 | this is instance-mode super |
| 6333 | (See SF ID #743627) |
| 6334 | */ |
| 6335 | (su->obj == (PyObject *) |
| 6336 | su->obj_type |
| 6337 | ? (PyObject *)NULL |
| 6338 | : su->obj), |
| 6339 | (PyObject *)starttype); |
| 6340 | Py_DECREF(res); |
| 6341 | res = tmp; |
| 6342 | } |
| 6343 | return res; |
| 6344 | } |
| 6345 | } |
| 6346 | } |
| 6347 | return PyObject_GenericGetAttr(self, name); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6348 | } |
| 6349 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6350 | static PyTypeObject * |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6351 | supercheck(PyTypeObject *type, PyObject *obj) |
| 6352 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6353 | /* Check that a super() call makes sense. Return a type object. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6355 | obj can be a new-style class, or an instance of one: |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6356 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6357 | - If it is a class, it must be a subclass of 'type'. This case is |
| 6358 | used for class methods; the return value is obj. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6360 | - If it is an instance, it must be an instance of 'type'. This is |
| 6361 | the normal case; the return value is obj.__class__. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6362 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6363 | But... when obj is an instance, we want to allow for the case where |
| 6364 | Py_TYPE(obj) is not a subclass of type, but obj.__class__ is! |
| 6365 | This will allow using super() with a proxy for obj. |
| 6366 | */ |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6367 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6368 | /* Check for first bullet above (special case) */ |
| 6369 | if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) { |
| 6370 | Py_INCREF(obj); |
| 6371 | return (PyTypeObject *)obj; |
| 6372 | } |
Guido van Rossum | 8e80a72 | 2003-02-18 19:22:22 +0000 | [diff] [blame] | 6373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6374 | /* Normal case */ |
| 6375 | if (PyType_IsSubtype(Py_TYPE(obj), type)) { |
| 6376 | Py_INCREF(Py_TYPE(obj)); |
| 6377 | return Py_TYPE(obj); |
| 6378 | } |
| 6379 | else { |
| 6380 | /* Try the slow way */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6381 | PyObject *class_attr; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6382 | |
Martin v. Löwis | bfc6d74 | 2011-10-13 20:03:57 +0200 | [diff] [blame] | 6383 | class_attr = _PyObject_GetAttrId(obj, &PyId___class__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6384 | if (class_attr != NULL && |
| 6385 | PyType_Check(class_attr) && |
| 6386 | (PyTypeObject *)class_attr != Py_TYPE(obj)) |
| 6387 | { |
| 6388 | int ok = PyType_IsSubtype( |
| 6389 | (PyTypeObject *)class_attr, type); |
| 6390 | if (ok) |
| 6391 | return (PyTypeObject *)class_attr; |
| 6392 | } |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6393 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6394 | if (class_attr == NULL) |
| 6395 | PyErr_Clear(); |
| 6396 | else |
| 6397 | Py_DECREF(class_attr); |
| 6398 | } |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6400 | PyErr_SetString(PyExc_TypeError, |
| 6401 | "super(type, obj): " |
| 6402 | "obj must be an instance or subtype of type"); |
| 6403 | return NULL; |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6404 | } |
| 6405 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6406 | static PyObject * |
| 6407 | super_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 6408 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6409 | superobject *su = (superobject *)self; |
| 6410 | superobject *newobj; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6412 | if (obj == NULL || obj == Py_None || su->obj != NULL) { |
| 6413 | /* Not binding to an object, or already bound */ |
| 6414 | Py_INCREF(self); |
| 6415 | return self; |
| 6416 | } |
| 6417 | if (Py_TYPE(su) != &PySuper_Type) |
| 6418 | /* If su is an instance of a (strict) subclass of super, |
| 6419 | call its type */ |
| 6420 | return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su), |
| 6421 | su->type, obj, NULL); |
| 6422 | else { |
| 6423 | /* Inline the common case */ |
| 6424 | PyTypeObject *obj_type = supercheck(su->type, obj); |
| 6425 | if (obj_type == NULL) |
| 6426 | return NULL; |
| 6427 | newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type, |
| 6428 | NULL, NULL); |
| 6429 | if (newobj == NULL) |
| 6430 | return NULL; |
| 6431 | Py_INCREF(su->type); |
| 6432 | Py_INCREF(obj); |
| 6433 | newobj->type = su->type; |
| 6434 | newobj->obj = obj; |
| 6435 | newobj->obj_type = obj_type; |
| 6436 | return (PyObject *)newobj; |
| 6437 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6438 | } |
| 6439 | |
| 6440 | static int |
| 6441 | super_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 6442 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6443 | superobject *su = (superobject *)self; |
| 6444 | PyTypeObject *type = NULL; |
| 6445 | PyObject *obj = NULL; |
| 6446 | PyTypeObject *obj_type = NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6448 | if (!_PyArg_NoKeywords("super", kwds)) |
| 6449 | return -1; |
| 6450 | if (!PyArg_ParseTuple(args, "|O!O:super", &PyType_Type, &type, &obj)) |
| 6451 | return -1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6452 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6453 | if (type == NULL) { |
| 6454 | /* Call super(), without args -- fill in from __class__ |
| 6455 | and first local variable on the stack. */ |
| 6456 | PyFrameObject *f = PyThreadState_GET()->frame; |
| 6457 | PyCodeObject *co = f->f_code; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 6458 | Py_ssize_t i, n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6459 | if (co == NULL) { |
| 6460 | PyErr_SetString(PyExc_SystemError, |
| 6461 | "super(): no code object"); |
| 6462 | return -1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6463 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6464 | if (co->co_argcount == 0) { |
| 6465 | PyErr_SetString(PyExc_SystemError, |
| 6466 | "super(): no arguments"); |
| 6467 | return -1; |
| 6468 | } |
| 6469 | obj = f->f_localsplus[0]; |
| 6470 | if (obj == NULL) { |
| 6471 | PyErr_SetString(PyExc_SystemError, |
| 6472 | "super(): arg[0] deleted"); |
| 6473 | return -1; |
| 6474 | } |
| 6475 | if (co->co_freevars == NULL) |
| 6476 | n = 0; |
| 6477 | else { |
| 6478 | assert(PyTuple_Check(co->co_freevars)); |
| 6479 | n = PyTuple_GET_SIZE(co->co_freevars); |
| 6480 | } |
| 6481 | for (i = 0; i < n; i++) { |
| 6482 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 6483 | assert(PyUnicode_Check(name)); |
| 6484 | if (!PyUnicode_CompareWithASCIIString(name, |
Benjamin Peterson | f5ff223 | 2011-06-19 19:42:22 -0500 | [diff] [blame] | 6485 | "@__class__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6486 | Py_ssize_t index = co->co_nlocals + |
| 6487 | PyTuple_GET_SIZE(co->co_cellvars) + i; |
| 6488 | PyObject *cell = f->f_localsplus[index]; |
| 6489 | if (cell == NULL || !PyCell_Check(cell)) { |
| 6490 | PyErr_SetString(PyExc_SystemError, |
| 6491 | "super(): bad __class__ cell"); |
| 6492 | return -1; |
| 6493 | } |
| 6494 | type = (PyTypeObject *) PyCell_GET(cell); |
| 6495 | if (type == NULL) { |
| 6496 | PyErr_SetString(PyExc_SystemError, |
| 6497 | "super(): empty __class__ cell"); |
| 6498 | return -1; |
| 6499 | } |
| 6500 | if (!PyType_Check(type)) { |
| 6501 | PyErr_Format(PyExc_SystemError, |
| 6502 | "super(): __class__ is not a type (%s)", |
| 6503 | Py_TYPE(type)->tp_name); |
| 6504 | return -1; |
| 6505 | } |
| 6506 | break; |
| 6507 | } |
| 6508 | } |
| 6509 | if (type == NULL) { |
| 6510 | PyErr_SetString(PyExc_SystemError, |
| 6511 | "super(): __class__ cell not found"); |
| 6512 | return -1; |
| 6513 | } |
| 6514 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6516 | if (obj == Py_None) |
| 6517 | obj = NULL; |
| 6518 | if (obj != NULL) { |
| 6519 | obj_type = supercheck(type, obj); |
| 6520 | if (obj_type == NULL) |
| 6521 | return -1; |
| 6522 | Py_INCREF(obj); |
| 6523 | } |
| 6524 | Py_INCREF(type); |
| 6525 | su->type = type; |
| 6526 | su->obj = obj; |
| 6527 | su->obj_type = obj_type; |
| 6528 | return 0; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6529 | } |
| 6530 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6531 | PyDoc_STRVAR(super_doc, |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6532 | "super() -> same as super(__class__, <first argument>)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6533 | "super(type) -> unbound super object\n" |
| 6534 | "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6535 | "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6536 | "Typical use to call a cooperative superclass method:\n" |
| 6537 | "class C(B):\n" |
| 6538 | " def meth(self, arg):\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6539 | " super().meth(arg)\n" |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6540 | "This works for class methods too:\n" |
| 6541 | "class C(B):\n" |
| 6542 | " @classmethod\n" |
| 6543 | " def cmeth(cls, arg):\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6544 | " super().cmeth(arg)\n"); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6545 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6546 | static int |
| 6547 | super_traverse(PyObject *self, visitproc visit, void *arg) |
| 6548 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6549 | superobject *su = (superobject *)self; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6551 | Py_VISIT(su->obj); |
| 6552 | Py_VISIT(su->type); |
| 6553 | Py_VISIT(su->obj_type); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6555 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6556 | } |
| 6557 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6558 | PyTypeObject PySuper_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6559 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 6560 | "super", /* tp_name */ |
| 6561 | sizeof(superobject), /* tp_basicsize */ |
| 6562 | 0, /* tp_itemsize */ |
| 6563 | /* methods */ |
| 6564 | super_dealloc, /* tp_dealloc */ |
| 6565 | 0, /* tp_print */ |
| 6566 | 0, /* tp_getattr */ |
| 6567 | 0, /* tp_setattr */ |
| 6568 | 0, /* tp_reserved */ |
| 6569 | super_repr, /* tp_repr */ |
| 6570 | 0, /* tp_as_number */ |
| 6571 | 0, /* tp_as_sequence */ |
| 6572 | 0, /* tp_as_mapping */ |
| 6573 | 0, /* tp_hash */ |
| 6574 | 0, /* tp_call */ |
| 6575 | 0, /* tp_str */ |
| 6576 | super_getattro, /* tp_getattro */ |
| 6577 | 0, /* tp_setattro */ |
| 6578 | 0, /* tp_as_buffer */ |
| 6579 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 6580 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 6581 | super_doc, /* tp_doc */ |
| 6582 | super_traverse, /* tp_traverse */ |
| 6583 | 0, /* tp_clear */ |
| 6584 | 0, /* tp_richcompare */ |
| 6585 | 0, /* tp_weaklistoffset */ |
| 6586 | 0, /* tp_iter */ |
| 6587 | 0, /* tp_iternext */ |
| 6588 | 0, /* tp_methods */ |
| 6589 | super_members, /* tp_members */ |
| 6590 | 0, /* tp_getset */ |
| 6591 | 0, /* tp_base */ |
| 6592 | 0, /* tp_dict */ |
| 6593 | super_descr_get, /* tp_descr_get */ |
| 6594 | 0, /* tp_descr_set */ |
| 6595 | 0, /* tp_dictoffset */ |
| 6596 | super_init, /* tp_init */ |
| 6597 | PyType_GenericAlloc, /* tp_alloc */ |
| 6598 | PyType_GenericNew, /* tp_new */ |
| 6599 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6600 | }; |