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, \ |
| 23 | ((PyUnicodeObject *)(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) && \ |
| 26 | PyUnicode_GET_SIZE(name) <= MCACHE_MAX_ATTR_SIZE |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 27 | |
| 28 | struct method_cache_entry { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 29 | unsigned int version; |
| 30 | PyObject *name; /* reference to exactly a str or None */ |
| 31 | PyObject *value; /* borrowed */ |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP]; |
| 35 | static unsigned int next_version_tag = 0; |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 36 | |
| 37 | unsigned int |
| 38 | PyType_ClearCache(void) |
| 39 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 40 | Py_ssize_t i; |
| 41 | unsigned int cur_version_tag = next_version_tag - 1; |
| 42 | |
| 43 | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { |
| 44 | method_cache[i].version = 0; |
| 45 | Py_CLEAR(method_cache[i].name); |
| 46 | method_cache[i].value = NULL; |
| 47 | } |
| 48 | next_version_tag = 0; |
| 49 | /* mark all version tags as invalid */ |
| 50 | PyType_Modified(&PyBaseObject_Type); |
| 51 | return cur_version_tag; |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 52 | } |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 53 | |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 54 | void |
| 55 | PyType_Modified(PyTypeObject *type) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 56 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 57 | /* Invalidate any cached data for the specified type and all |
| 58 | subclasses. This function is called after the base |
| 59 | classes, mro, or attributes of the type are altered. |
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 | Invariants: |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 62 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | - Py_TPFLAGS_VALID_VERSION_TAG is never set if |
| 64 | Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type |
| 65 | objects coming from non-recompiled extension modules) |
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 | - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type, |
| 68 | it must first be set on all super types. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 69 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 70 | This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a |
| 71 | type (so it must first clear it on all subclasses). The |
| 72 | tp_version_tag value is meaningless unless this flag is set. |
| 73 | We don't assign new version tags eagerly, but only as |
| 74 | needed. |
| 75 | */ |
| 76 | PyObject *raw, *ref; |
| 77 | Py_ssize_t i, n; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 78 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
| 80 | return; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 81 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 82 | raw = type->tp_subclasses; |
| 83 | if (raw != NULL) { |
| 84 | n = PyList_GET_SIZE(raw); |
| 85 | for (i = 0; i < n; i++) { |
| 86 | ref = PyList_GET_ITEM(raw, i); |
| 87 | ref = PyWeakref_GET_OBJECT(ref); |
| 88 | if (ref != Py_None) { |
| 89 | PyType_Modified((PyTypeObject *)ref); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static void |
| 97 | type_mro_modified(PyTypeObject *type, PyObject *bases) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | /* |
| 99 | Check that all base classes or elements of the mro of type are |
| 100 | able to be cached. This function is called after the base |
| 101 | classes or mro of the type are altered. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 103 | Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type |
| 104 | inherits from an old-style class, either directly or if it |
| 105 | appears in the MRO of a new-style class. No support either for |
| 106 | custom MROs that include types that are not officially super |
| 107 | types. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | Called from mro_internal, which will subsequently be called on |
| 110 | each subclass when their mro is recursively updated. |
| 111 | */ |
| 112 | Py_ssize_t i, n; |
| 113 | int clear = 0; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) |
| 116 | return; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 117 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 118 | n = PyTuple_GET_SIZE(bases); |
| 119 | for (i = 0; i < n; i++) { |
| 120 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 121 | PyTypeObject *cls; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | if (!PyType_Check(b) ) { |
| 124 | clear = 1; |
| 125 | break; |
| 126 | } |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | cls = (PyTypeObject *)b; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 129 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 130 | if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) || |
| 131 | !PyType_IsSubtype(type, cls)) { |
| 132 | clear = 1; |
| 133 | break; |
| 134 | } |
| 135 | } |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | if (clear) |
| 138 | type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG| |
| 139 | Py_TPFLAGS_VALID_VERSION_TAG); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static int |
| 143 | assign_version_tag(PyTypeObject *type) |
| 144 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | /* Ensure that the tp_version_tag is valid and set |
| 146 | Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this |
| 147 | must first be done on all super classes. Return 0 if this |
| 148 | cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG. |
| 149 | */ |
| 150 | Py_ssize_t i, n; |
| 151 | PyObject *bases; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 153 | if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
| 154 | return 1; |
| 155 | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) |
| 156 | return 0; |
| 157 | if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) |
| 158 | return 0; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 159 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 160 | type->tp_version_tag = next_version_tag++; |
| 161 | /* for stress-testing: next_version_tag &= 0xFF; */ |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 162 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 163 | if (type->tp_version_tag == 0) { |
| 164 | /* wrap-around or just starting Python - clear the whole |
| 165 | cache by filling names with references to Py_None. |
| 166 | Values are also set to NULL for added protection, as they |
| 167 | are borrowed reference */ |
| 168 | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { |
| 169 | method_cache[i].value = NULL; |
| 170 | Py_XDECREF(method_cache[i].name); |
| 171 | method_cache[i].name = Py_None; |
| 172 | Py_INCREF(Py_None); |
| 173 | } |
| 174 | /* mark all version tags as invalid */ |
| 175 | PyType_Modified(&PyBaseObject_Type); |
| 176 | return 1; |
| 177 | } |
| 178 | bases = type->tp_bases; |
| 179 | n = PyTuple_GET_SIZE(bases); |
| 180 | for (i = 0; i < n; i++) { |
| 181 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 182 | assert(PyType_Check(b)); |
| 183 | if (!assign_version_tag((PyTypeObject *)b)) |
| 184 | return 0; |
| 185 | } |
| 186 | type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG; |
| 187 | return 1; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 191 | static PyMemberDef type_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 192 | {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY}, |
| 193 | {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY}, |
| 194 | {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY}, |
| 195 | {"__weakrefoffset__", T_LONG, |
| 196 | offsetof(PyTypeObject, tp_weaklistoffset), READONLY}, |
| 197 | {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY}, |
| 198 | {"__dictoffset__", T_LONG, |
| 199 | offsetof(PyTypeObject, tp_dictoffset), READONLY}, |
| 200 | {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY}, |
| 201 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 202 | }; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 204 | static PyObject * |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 205 | type_name(PyTypeObject *type, void *context) |
| 206 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | const char *s; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 210 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 211 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 212 | Py_INCREF(et->ht_name); |
| 213 | return et->ht_name; |
| 214 | } |
| 215 | else { |
| 216 | s = strrchr(type->tp_name, '.'); |
| 217 | if (s == NULL) |
| 218 | s = type->tp_name; |
| 219 | else |
| 220 | s++; |
| 221 | return PyUnicode_FromString(s); |
| 222 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 225 | static int |
| 226 | type_set_name(PyTypeObject *type, PyObject *value, void *context) |
| 227 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 228 | PyHeapTypeObject* et; |
| 229 | char *tp_name; |
| 230 | PyObject *tmp; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 232 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 233 | PyErr_Format(PyExc_TypeError, |
| 234 | "can't set %s.__name__", type->tp_name); |
| 235 | return -1; |
| 236 | } |
| 237 | if (!value) { |
| 238 | PyErr_Format(PyExc_TypeError, |
| 239 | "can't delete %s.__name__", type->tp_name); |
| 240 | return -1; |
| 241 | } |
| 242 | if (!PyUnicode_Check(value)) { |
| 243 | PyErr_Format(PyExc_TypeError, |
| 244 | "can only assign string to %s.__name__, not '%s'", |
| 245 | type->tp_name, Py_TYPE(value)->tp_name); |
| 246 | return -1; |
| 247 | } |
Guido van Rossum | e845c0f | 2007-11-02 23:07:07 +0000 | [diff] [blame] | 248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | /* Check absence of null characters */ |
| 250 | tmp = PyUnicode_FromStringAndSize("\0", 1); |
| 251 | if (tmp == NULL) |
| 252 | return -1; |
| 253 | if (PyUnicode_Contains(value, tmp) != 0) { |
| 254 | Py_DECREF(tmp); |
| 255 | PyErr_Format(PyExc_ValueError, |
| 256 | "__name__ must not contain null bytes"); |
| 257 | return -1; |
| 258 | } |
| 259 | Py_DECREF(tmp); |
Guido van Rossum | e845c0f | 2007-11-02 23:07:07 +0000 | [diff] [blame] | 260 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 261 | tp_name = _PyUnicode_AsString(value); |
| 262 | if (tp_name == NULL) |
| 263 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 264 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 265 | et = (PyHeapTypeObject*)type; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | Py_INCREF(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 268 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | Py_DECREF(et->ht_name); |
| 270 | et->ht_name = value; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 272 | type->tp_name = tp_name; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | return 0; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 277 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 278 | type_module(PyTypeObject *type, void *context) |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 279 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 280 | PyObject *mod; |
| 281 | char *s; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 283 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 284 | mod = PyDict_GetItemString(type->tp_dict, "__module__"); |
| 285 | if (!mod) { |
| 286 | PyErr_Format(PyExc_AttributeError, "__module__"); |
| 287 | return 0; |
| 288 | } |
| 289 | Py_XINCREF(mod); |
| 290 | return mod; |
| 291 | } |
| 292 | else { |
| 293 | s = strrchr(type->tp_name, '.'); |
| 294 | if (s != NULL) |
| 295 | return PyUnicode_FromStringAndSize( |
| 296 | type->tp_name, (Py_ssize_t)(s - type->tp_name)); |
| 297 | return PyUnicode_FromString("builtins"); |
| 298 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 301 | static int |
| 302 | type_set_module(PyTypeObject *type, PyObject *value, void *context) |
| 303 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 304 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 305 | PyErr_Format(PyExc_TypeError, |
| 306 | "can't set %s.__module__", type->tp_name); |
| 307 | return -1; |
| 308 | } |
| 309 | if (!value) { |
| 310 | PyErr_Format(PyExc_TypeError, |
| 311 | "can't delete %s.__module__", type->tp_name); |
| 312 | return -1; |
| 313 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 316 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 317 | return PyDict_SetItemString(type->tp_dict, "__module__", value); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 320 | static PyObject * |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 321 | type_abstractmethods(PyTypeObject *type, void *context) |
| 322 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 323 | PyObject *mod = PyDict_GetItemString(type->tp_dict, |
| 324 | "__abstractmethods__"); |
| 325 | if (!mod) { |
| 326 | PyErr_Format(PyExc_AttributeError, "__abstractmethods__"); |
| 327 | return NULL; |
| 328 | } |
| 329 | Py_XINCREF(mod); |
| 330 | return mod; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | static int |
| 334 | type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context) |
| 335 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 | /* __abstractmethods__ should only be set once on a type, in |
| 337 | abc.ABCMeta.__new__, so this function doesn't do anything |
| 338 | special to update subclasses. |
| 339 | */ |
| 340 | int res = PyDict_SetItemString(type->tp_dict, |
| 341 | "__abstractmethods__", value); |
| 342 | if (res == 0) { |
| 343 | PyType_Modified(type); |
| 344 | if (value && PyObject_IsTrue(value)) { |
| 345 | type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT; |
| 346 | } |
| 347 | else { |
| 348 | type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT; |
| 349 | } |
| 350 | } |
| 351 | return res; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | static PyObject * |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 355 | type_get_bases(PyTypeObject *type, void *context) |
| 356 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 357 | Py_INCREF(type->tp_bases); |
| 358 | return type->tp_bases; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | static PyTypeObject *best_base(PyObject *); |
| 362 | static int mro_internal(PyTypeObject *); |
| 363 | static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *); |
| 364 | static int add_subclass(PyTypeObject*, PyTypeObject*); |
| 365 | static void remove_subclass(PyTypeObject *, PyTypeObject *); |
| 366 | static void update_all_slots(PyTypeObject *); |
| 367 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 368 | typedef int (*update_callback)(PyTypeObject *, void *); |
| 369 | static int update_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | update_callback callback, void *data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 371 | static int recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | update_callback callback, void *data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 373 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 374 | static int |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 375 | mro_subclasses(PyTypeObject *type, PyObject* temp) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 376 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 377 | PyTypeObject *subclass; |
| 378 | PyObject *ref, *subclasses, *old_mro; |
| 379 | Py_ssize_t i, n; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 381 | subclasses = type->tp_subclasses; |
| 382 | if (subclasses == NULL) |
| 383 | return 0; |
| 384 | assert(PyList_Check(subclasses)); |
| 385 | n = PyList_GET_SIZE(subclasses); |
| 386 | for (i = 0; i < n; i++) { |
| 387 | ref = PyList_GET_ITEM(subclasses, i); |
| 388 | assert(PyWeakref_CheckRef(ref)); |
| 389 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 390 | assert(subclass != NULL); |
| 391 | if ((PyObject *)subclass == Py_None) |
| 392 | continue; |
| 393 | assert(PyType_Check(subclass)); |
| 394 | old_mro = subclass->tp_mro; |
| 395 | if (mro_internal(subclass) < 0) { |
| 396 | subclass->tp_mro = old_mro; |
| 397 | return -1; |
| 398 | } |
| 399 | else { |
| 400 | PyObject* tuple; |
| 401 | tuple = PyTuple_Pack(2, subclass, old_mro); |
| 402 | Py_DECREF(old_mro); |
| 403 | if (!tuple) |
| 404 | return -1; |
| 405 | if (PyList_Append(temp, tuple) < 0) |
| 406 | return -1; |
| 407 | Py_DECREF(tuple); |
| 408 | } |
| 409 | if (mro_subclasses(subclass, temp) < 0) |
| 410 | return -1; |
| 411 | } |
| 412 | return 0; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | static int |
| 416 | type_set_bases(PyTypeObject *type, PyObject *value, void *context) |
| 417 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | Py_ssize_t i; |
| 419 | int r = 0; |
| 420 | PyObject *ob, *temp; |
| 421 | PyTypeObject *new_base, *old_base; |
| 422 | PyObject *old_bases, *old_mro; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 424 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 425 | PyErr_Format(PyExc_TypeError, |
| 426 | "can't set %s.__bases__", type->tp_name); |
| 427 | return -1; |
| 428 | } |
| 429 | if (!value) { |
| 430 | PyErr_Format(PyExc_TypeError, |
| 431 | "can't delete %s.__bases__", type->tp_name); |
| 432 | return -1; |
| 433 | } |
| 434 | if (!PyTuple_Check(value)) { |
| 435 | PyErr_Format(PyExc_TypeError, |
| 436 | "can only assign tuple to %s.__bases__, not %s", |
| 437 | type->tp_name, Py_TYPE(value)->tp_name); |
| 438 | return -1; |
| 439 | } |
| 440 | if (PyTuple_GET_SIZE(value) == 0) { |
| 441 | PyErr_Format(PyExc_TypeError, |
| 442 | "can only assign non-empty tuple to %s.__bases__, not ()", |
| 443 | type->tp_name); |
| 444 | return -1; |
| 445 | } |
| 446 | for (i = 0; i < PyTuple_GET_SIZE(value); i++) { |
| 447 | ob = PyTuple_GET_ITEM(value, i); |
| 448 | if (!PyType_Check(ob)) { |
| 449 | PyErr_Format( |
| 450 | PyExc_TypeError, |
| 451 | "%s.__bases__ must be tuple of old- or new-style classes, not '%s'", |
| 452 | type->tp_name, Py_TYPE(ob)->tp_name); |
| 453 | return -1; |
| 454 | } |
| 455 | if (PyType_Check(ob)) { |
| 456 | if (PyType_IsSubtype((PyTypeObject*)ob, type)) { |
| 457 | PyErr_SetString(PyExc_TypeError, |
| 458 | "a __bases__ item causes an inheritance cycle"); |
| 459 | return -1; |
| 460 | } |
| 461 | } |
| 462 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 463 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 464 | new_base = best_base(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 466 | if (!new_base) { |
| 467 | return -1; |
| 468 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 469 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | if (!compatible_for_assignment(type->tp_base, new_base, "__bases__")) |
| 471 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | Py_INCREF(new_base); |
| 474 | Py_INCREF(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 476 | old_bases = type->tp_bases; |
| 477 | old_base = type->tp_base; |
| 478 | old_mro = type->tp_mro; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 479 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | type->tp_bases = value; |
| 481 | type->tp_base = new_base; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 482 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 483 | if (mro_internal(type) < 0) { |
| 484 | goto bail; |
| 485 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 487 | temp = PyList_New(0); |
| 488 | if (!temp) |
| 489 | goto bail; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 491 | r = mro_subclasses(type, temp); |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | if (r < 0) { |
| 494 | for (i = 0; i < PyList_Size(temp); i++) { |
| 495 | PyTypeObject* cls; |
| 496 | PyObject* mro; |
| 497 | PyArg_UnpackTuple(PyList_GET_ITEM(temp, i), |
| 498 | "", 2, 2, &cls, &mro); |
| 499 | Py_INCREF(mro); |
| 500 | ob = cls->tp_mro; |
| 501 | cls->tp_mro = mro; |
| 502 | Py_DECREF(ob); |
| 503 | } |
| 504 | Py_DECREF(temp); |
| 505 | goto bail; |
| 506 | } |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | Py_DECREF(temp); |
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 | /* any base that was in __bases__ but now isn't, we |
| 511 | need to remove |type| from its tp_subclasses. |
| 512 | conversely, any class now in __bases__ that wasn't |
| 513 | needs to have |type| added to its subclasses. */ |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 515 | /* for now, sod that: just remove from all old_bases, |
| 516 | add to all new_bases */ |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) { |
| 519 | ob = PyTuple_GET_ITEM(old_bases, i); |
| 520 | if (PyType_Check(ob)) { |
| 521 | remove_subclass( |
| 522 | (PyTypeObject*)ob, type); |
| 523 | } |
| 524 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 525 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 526 | for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) { |
| 527 | ob = PyTuple_GET_ITEM(value, i); |
| 528 | if (PyType_Check(ob)) { |
| 529 | if (add_subclass((PyTypeObject*)ob, type) < 0) |
| 530 | r = -1; |
| 531 | } |
| 532 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 533 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | update_all_slots(type); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 535 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 536 | Py_DECREF(old_bases); |
| 537 | Py_DECREF(old_base); |
| 538 | Py_DECREF(old_mro); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 539 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | return r; |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 541 | |
| 542 | bail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 543 | Py_DECREF(type->tp_bases); |
| 544 | Py_DECREF(type->tp_base); |
| 545 | if (type->tp_mro != old_mro) { |
| 546 | Py_DECREF(type->tp_mro); |
| 547 | } |
Michael W. Hudson | e723e45 | 2003-08-07 14:58:10 +0000 | [diff] [blame] | 548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 549 | type->tp_bases = old_bases; |
| 550 | type->tp_base = old_base; |
| 551 | type->tp_mro = old_mro; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 552 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 553 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 557 | type_dict(PyTypeObject *type, void *context) |
| 558 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 559 | if (type->tp_dict == NULL) { |
| 560 | Py_INCREF(Py_None); |
| 561 | return Py_None; |
| 562 | } |
| 563 | return PyDictProxy_New(type->tp_dict); |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 566 | static PyObject * |
| 567 | type_get_doc(PyTypeObject *type, void *context) |
| 568 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 569 | PyObject *result; |
| 570 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) |
| 571 | return PyUnicode_FromString(type->tp_doc); |
| 572 | result = PyDict_GetItemString(type->tp_dict, "__doc__"); |
| 573 | if (result == NULL) { |
| 574 | result = Py_None; |
| 575 | Py_INCREF(result); |
| 576 | } |
| 577 | else if (Py_TYPE(result)->tp_descr_get) { |
| 578 | result = Py_TYPE(result)->tp_descr_get(result, NULL, |
| 579 | (PyObject *)type); |
| 580 | } |
| 581 | else { |
| 582 | Py_INCREF(result); |
| 583 | } |
| 584 | return result; |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 587 | static PyObject * |
| 588 | type___instancecheck__(PyObject *type, PyObject *inst) |
| 589 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 590 | switch (_PyObject_RealIsInstance(inst, type)) { |
| 591 | case -1: |
| 592 | return NULL; |
| 593 | case 0: |
| 594 | Py_RETURN_FALSE; |
| 595 | default: |
| 596 | Py_RETURN_TRUE; |
| 597 | } |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | |
| 601 | static PyObject * |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 602 | type___subclasscheck__(PyObject *type, PyObject *inst) |
| 603 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 604 | switch (_PyObject_RealIsSubclass(inst, type)) { |
| 605 | case -1: |
| 606 | return NULL; |
| 607 | case 0: |
| 608 | Py_RETURN_FALSE; |
| 609 | default: |
| 610 | Py_RETURN_TRUE; |
| 611 | } |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 614 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 615 | static PyGetSetDef type_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 616 | {"__name__", (getter)type_name, (setter)type_set_name, NULL}, |
| 617 | {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL}, |
| 618 | {"__module__", (getter)type_module, (setter)type_set_module, NULL}, |
| 619 | {"__abstractmethods__", (getter)type_abstractmethods, |
| 620 | (setter)type_set_abstractmethods, NULL}, |
| 621 | {"__dict__", (getter)type_dict, NULL, NULL}, |
| 622 | {"__doc__", (getter)type_get_doc, NULL, NULL}, |
| 623 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 624 | }; |
| 625 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 626 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 627 | type_repr(PyTypeObject *type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 628 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 629 | PyObject *mod, *name, *rtn; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 630 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | mod = type_module(type, NULL); |
| 632 | if (mod == NULL) |
| 633 | PyErr_Clear(); |
| 634 | else if (!PyUnicode_Check(mod)) { |
| 635 | Py_DECREF(mod); |
| 636 | mod = NULL; |
| 637 | } |
| 638 | name = type_name(type, NULL); |
| 639 | if (name == NULL) |
| 640 | return NULL; |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 642 | if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins")) |
| 643 | rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name); |
| 644 | else |
| 645 | rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 646 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | Py_XDECREF(mod); |
| 648 | Py_DECREF(name); |
| 649 | return rtn; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 652 | static PyObject * |
| 653 | type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 654 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 655 | PyObject *obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 656 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 657 | if (type->tp_new == NULL) { |
| 658 | PyErr_Format(PyExc_TypeError, |
| 659 | "cannot create '%.100s' instances", |
| 660 | type->tp_name); |
| 661 | return NULL; |
| 662 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 664 | obj = type->tp_new(type, args, kwds); |
| 665 | if (obj != NULL) { |
| 666 | /* Ugly exception: when the call was type(something), |
| 667 | don't call tp_init on the result. */ |
| 668 | if (type == &PyType_Type && |
| 669 | PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && |
| 670 | (kwds == NULL || |
| 671 | (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) |
| 672 | return obj; |
| 673 | /* If the returned object is not an instance of type, |
| 674 | it won't be initialized. */ |
| 675 | if (!PyType_IsSubtype(Py_TYPE(obj), type)) |
| 676 | return obj; |
| 677 | type = Py_TYPE(obj); |
| 678 | if (type->tp_init != NULL && |
| 679 | type->tp_init(obj, args, kwds) < 0) { |
| 680 | Py_DECREF(obj); |
| 681 | obj = NULL; |
| 682 | } |
| 683 | } |
| 684 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 688 | PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 689 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 690 | PyObject *obj; |
| 691 | const size_t size = _PyObject_VAR_SIZE(type, nitems+1); |
| 692 | /* note that we need to add one, for the sentinel */ |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 693 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 694 | if (PyType_IS_GC(type)) |
| 695 | obj = _PyObject_GC_Malloc(size); |
| 696 | else |
| 697 | obj = (PyObject *)PyObject_MALLOC(size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 698 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 699 | if (obj == NULL) |
| 700 | return PyErr_NoMemory(); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | memset(obj, '\0', size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 703 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 705 | Py_INCREF(type); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 707 | if (type->tp_itemsize == 0) |
| 708 | PyObject_INIT(obj, type); |
| 709 | else |
| 710 | (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 711 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 712 | if (PyType_IS_GC(type)) |
| 713 | _PyObject_GC_TRACK(obj); |
| 714 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | PyObject * |
| 718 | PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 719 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 720 | return type->tp_alloc(type, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 723 | /* Helpers for subtyping */ |
| 724 | |
| 725 | static int |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 726 | traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg) |
| 727 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 728 | Py_ssize_t i, n; |
| 729 | PyMemberDef *mp; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 730 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 731 | n = Py_SIZE(type); |
| 732 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
| 733 | for (i = 0; i < n; i++, mp++) { |
| 734 | if (mp->type == T_OBJECT_EX) { |
| 735 | char *addr = (char *)self + mp->offset; |
| 736 | PyObject *obj = *(PyObject **)addr; |
| 737 | if (obj != NULL) { |
| 738 | int err = visit(obj, arg); |
| 739 | if (err) |
| 740 | return err; |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | return 0; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | static int |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 748 | subtype_traverse(PyObject *self, visitproc visit, void *arg) |
| 749 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 750 | PyTypeObject *type, *base; |
| 751 | traverseproc basetraverse; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 752 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 753 | /* Find the nearest base with a different tp_traverse, |
| 754 | and traverse slots while we're at it */ |
| 755 | type = Py_TYPE(self); |
| 756 | base = type; |
| 757 | while ((basetraverse = base->tp_traverse) == subtype_traverse) { |
| 758 | if (Py_SIZE(base)) { |
| 759 | int err = traverse_slots(base, self, visit, arg); |
| 760 | if (err) |
| 761 | return err; |
| 762 | } |
| 763 | base = base->tp_base; |
| 764 | assert(base); |
| 765 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 766 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 767 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 768 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 769 | if (dictptr && *dictptr) |
| 770 | Py_VISIT(*dictptr); |
| 771 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 773 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 774 | /* For a heaptype, the instances count as references |
| 775 | to the type. Traverse the type so the collector |
| 776 | can find cycles involving this link. */ |
| 777 | Py_VISIT(type); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 778 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 779 | if (basetraverse) |
| 780 | return basetraverse(self, visit, arg); |
| 781 | return 0; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | static void |
| 785 | clear_slots(PyTypeObject *type, PyObject *self) |
| 786 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 787 | Py_ssize_t i, n; |
| 788 | PyMemberDef *mp; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 789 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 790 | n = Py_SIZE(type); |
| 791 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
| 792 | for (i = 0; i < n; i++, mp++) { |
| 793 | if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) { |
| 794 | char *addr = (char *)self + mp->offset; |
| 795 | PyObject *obj = *(PyObject **)addr; |
| 796 | if (obj != NULL) { |
| 797 | *(PyObject **)addr = NULL; |
| 798 | Py_DECREF(obj); |
| 799 | } |
| 800 | } |
| 801 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | static int |
| 805 | subtype_clear(PyObject *self) |
| 806 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 807 | PyTypeObject *type, *base; |
| 808 | inquiry baseclear; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 809 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 810 | /* Find the nearest base with a different tp_clear |
| 811 | and clear slots while we're at it */ |
| 812 | type = Py_TYPE(self); |
| 813 | base = type; |
| 814 | while ((baseclear = base->tp_clear) == subtype_clear) { |
| 815 | if (Py_SIZE(base)) |
| 816 | clear_slots(base, self); |
| 817 | base = base->tp_base; |
| 818 | assert(base); |
| 819 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 820 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 821 | /* There's no need to clear the instance dict (if any); |
| 822 | the collector will call its tp_clear handler. */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 823 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 824 | if (baseclear) |
| 825 | return baseclear(self); |
| 826 | return 0; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 827 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 828 | |
| 829 | static void |
| 830 | subtype_dealloc(PyObject *self) |
| 831 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 832 | PyTypeObject *type, *base; |
| 833 | destructor basedealloc; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 834 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 835 | /* Extract the type; we expect it to be a heap type */ |
| 836 | type = Py_TYPE(self); |
| 837 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 839 | /* Test whether the type has GC exactly once */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 840 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 841 | if (!PyType_IS_GC(type)) { |
| 842 | /* It's really rare to find a dynamic type that doesn't have |
| 843 | GC; it can only happen when deriving from 'object' and not |
| 844 | adding any slots or instance variables. This allows |
| 845 | certain simplifications: there's no need to call |
| 846 | clear_slots(), or DECREF the dict, or clear weakrefs. */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 847 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 848 | /* Maybe call finalizer; exit early if resurrected */ |
| 849 | if (type->tp_del) { |
| 850 | type->tp_del(self); |
| 851 | if (self->ob_refcnt > 0) |
| 852 | return; |
| 853 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 854 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 855 | /* Find the nearest base with a different tp_dealloc */ |
| 856 | base = type; |
| 857 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 858 | assert(Py_SIZE(base) == 0); |
| 859 | base = base->tp_base; |
| 860 | assert(base); |
| 861 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 862 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 863 | /* Extract the type again; tp_del may have changed it */ |
| 864 | type = Py_TYPE(self); |
Benjamin Peterson | 193152c | 2009-04-25 01:08:45 +0000 | [diff] [blame] | 865 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 866 | /* Call the base tp_dealloc() */ |
| 867 | assert(basedealloc); |
| 868 | basedealloc(self); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 870 | /* Can't reference self beyond this point */ |
| 871 | Py_DECREF(type); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 872 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 873 | /* Done */ |
| 874 | return; |
| 875 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 876 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 877 | /* We get here only if the type has GC */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | /* UnTrack and re-Track around the trashcan macro, alas */ |
| 880 | /* See explanation at end of function for full disclosure */ |
| 881 | PyObject_GC_UnTrack(self); |
| 882 | ++_PyTrash_delete_nesting; |
| 883 | Py_TRASHCAN_SAFE_BEGIN(self); |
| 884 | --_PyTrash_delete_nesting; |
| 885 | /* DO NOT restore GC tracking at this point. weakref callbacks |
| 886 | * (if any, and whether directly here or indirectly in something we |
| 887 | * call) may trigger GC, and if self is tracked at that point, it |
| 888 | * will look like trash to GC and GC will try to delete self again. |
| 889 | */ |
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 | /* Find the nearest base with a different tp_dealloc */ |
| 892 | base = type; |
| 893 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 894 | base = base->tp_base; |
| 895 | assert(base); |
| 896 | } |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 897 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 898 | /* If we added a weaklist, we clear it. Do this *before* calling |
| 899 | the finalizer (__del__), clearing slots, or clearing the instance |
| 900 | dict. */ |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 901 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 902 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) |
| 903 | PyObject_ClearWeakRefs(self); |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 904 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 905 | /* Maybe call finalizer; exit early if resurrected */ |
| 906 | if (type->tp_del) { |
| 907 | _PyObject_GC_TRACK(self); |
| 908 | type->tp_del(self); |
| 909 | if (self->ob_refcnt > 0) |
| 910 | goto endlabel; /* resurrected */ |
| 911 | else |
| 912 | _PyObject_GC_UNTRACK(self); |
| 913 | /* New weakrefs could be created during the finalizer call. |
| 914 | If this occurs, clear them out without calling their |
| 915 | finalizers since they might rely on part of the object |
| 916 | being finalized that has already been destroyed. */ |
| 917 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) { |
| 918 | /* Modeled after GET_WEAKREFS_LISTPTR() */ |
| 919 | PyWeakReference **list = (PyWeakReference **) \ |
| 920 | PyObject_GET_WEAKREFS_LISTPTR(self); |
| 921 | while (*list) |
| 922 | _PyWeakref_ClearRef(*list); |
| 923 | } |
| 924 | } |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 925 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 926 | /* Clear slots up to the nearest base with a different tp_dealloc */ |
| 927 | base = type; |
| 928 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 929 | if (Py_SIZE(base)) |
| 930 | clear_slots(base, self); |
| 931 | base = base->tp_base; |
| 932 | assert(base); |
| 933 | } |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 934 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 935 | /* If we added a dict, DECREF it */ |
| 936 | if (type->tp_dictoffset && !base->tp_dictoffset) { |
| 937 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 938 | if (dictptr != NULL) { |
| 939 | PyObject *dict = *dictptr; |
| 940 | if (dict != NULL) { |
| 941 | Py_DECREF(dict); |
| 942 | *dictptr = NULL; |
| 943 | } |
| 944 | } |
| 945 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 947 | /* Extract the type again; tp_del may have changed it */ |
| 948 | type = Py_TYPE(self); |
Benjamin Peterson | 193152c | 2009-04-25 01:08:45 +0000 | [diff] [blame] | 949 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 950 | /* Call the base tp_dealloc(); first retrack self if |
| 951 | * basedealloc knows about gc. |
| 952 | */ |
| 953 | if (PyType_IS_GC(base)) |
| 954 | _PyObject_GC_TRACK(self); |
| 955 | assert(basedealloc); |
| 956 | basedealloc(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 957 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 958 | /* Can't reference self beyond this point */ |
| 959 | Py_DECREF(type); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 960 | |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 961 | endlabel: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 962 | ++_PyTrash_delete_nesting; |
| 963 | Py_TRASHCAN_SAFE_END(self); |
| 964 | --_PyTrash_delete_nesting; |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 965 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 966 | /* Explanation of the weirdness around the trashcan macros: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 967 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 968 | Q. What do the trashcan macros do? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 969 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 970 | A. Read the comment titled "Trashcan mechanism" in object.h. |
| 971 | For one, this explains why there must be a call to GC-untrack |
| 972 | before the trashcan begin macro. Without understanding the |
| 973 | trashcan code, the answers to the following questions don't make |
| 974 | sense. |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 975 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 976 | Q. Why do we GC-untrack before the trashcan and then immediately |
| 977 | GC-track again afterward? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | A. In the case that the base class is GC-aware, the base class |
| 980 | probably GC-untracks the object. If it does that using the |
| 981 | UNTRACK macro, this will crash when the object is already |
| 982 | untracked. Because we don't know what the base class does, the |
| 983 | only safe thing is to make sure the object is tracked when we |
| 984 | call the base class dealloc. But... The trashcan begin macro |
| 985 | requires that the object is *untracked* before it is called. So |
| 986 | the dance becomes: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 987 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | GC untrack |
| 989 | trashcan begin |
| 990 | GC track |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 991 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 992 | Q. Why did the last question say "immediately GC-track again"? |
| 993 | It's nowhere near immediately. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 994 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 995 | A. Because the code *used* to re-track immediately. Bad Idea. |
| 996 | self has a refcount of 0, and if gc ever gets its hands on it |
| 997 | (which can happen if any weakref callback gets invoked), it |
| 998 | looks like trash to gc too, and gc also tries to delete self |
| 999 | then. But we're already deleting self. Double dealloction is |
| 1000 | a subtle disaster. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 1001 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | Q. Why the bizarre (net-zero) manipulation of |
| 1003 | _PyTrash_delete_nesting around the trashcan macros? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1004 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1005 | A. Some base classes (e.g. list) also use the trashcan mechanism. |
| 1006 | The following scenario used to be possible: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1008 | - suppose the trashcan level is one below the trashcan limit |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1009 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1010 | - subtype_dealloc() is called |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1012 | - the trashcan limit is not yet reached, so the trashcan level |
| 1013 | is incremented and the code between trashcan begin and end is |
| 1014 | executed |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | - this destroys much of the object's contents, including its |
| 1017 | slots and __dict__ |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1018 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1019 | - basedealloc() is called; this is really list_dealloc(), or |
| 1020 | some other type which also uses the trashcan macros |
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 | - the trashcan limit is now reached, so the object is put on the |
| 1023 | trashcan's to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1024 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1025 | - basedealloc() returns |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1026 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | - subtype_dealloc() decrefs the object's type |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1028 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1029 | - subtype_dealloc() returns |
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 | - later, the trashcan code starts deleting the objects from its |
| 1032 | to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1033 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1034 | - subtype_dealloc() is called *AGAIN* for the same object |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1035 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1036 | - at the very least (if the destroyed slots and __dict__ don't |
| 1037 | cause problems) the object's type gets decref'ed a second |
| 1038 | time, which is *BAD*!!! |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1039 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | The remedy is to make sure that if the code between trashcan |
| 1041 | begin and end in subtype_dealloc() is called, the code between |
| 1042 | trashcan begin and end in basedealloc() will also be called. |
| 1043 | This is done by decrementing the level after passing into the |
| 1044 | trashcan block, and incrementing it just before leaving the |
| 1045 | block. |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1047 | But now it's possible that a chain of objects consisting solely |
| 1048 | of objects whose deallocator is subtype_dealloc() will defeat |
| 1049 | the trashcan mechanism completely: the decremented level means |
| 1050 | that the effective level never reaches the limit. Therefore, we |
| 1051 | *increment* the level *before* entering the trashcan block, and |
| 1052 | matchingly decrement it after leaving. This means the trashcan |
| 1053 | code will trigger a little early, but that's no big deal. |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1054 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1055 | Q. Are there any live examples of code in need of all this |
| 1056 | complexity? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1057 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1058 | A. Yes. See SF bug 668433 for code that crashed (when Python was |
| 1059 | compiled in debug mode) before the trashcan level manipulations |
| 1060 | were added. For more discussion, see SF patches 581742, 575073 |
| 1061 | and bug 574207. |
| 1062 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1065 | static PyTypeObject *solid_base(PyTypeObject *type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1066 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1067 | /* type test with subclassing support */ |
| 1068 | |
| 1069 | int |
| 1070 | PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
| 1071 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1072 | PyObject *mro; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1073 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1074 | mro = a->tp_mro; |
| 1075 | if (mro != NULL) { |
| 1076 | /* Deal with multiple inheritance without recursion |
| 1077 | by walking the MRO tuple */ |
| 1078 | Py_ssize_t i, n; |
| 1079 | assert(PyTuple_Check(mro)); |
| 1080 | n = PyTuple_GET_SIZE(mro); |
| 1081 | for (i = 0; i < n; i++) { |
| 1082 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) |
| 1083 | return 1; |
| 1084 | } |
| 1085 | return 0; |
| 1086 | } |
| 1087 | else { |
| 1088 | /* a is not completely initilized yet; follow tp_base */ |
| 1089 | do { |
| 1090 | if (a == b) |
| 1091 | return 1; |
| 1092 | a = a->tp_base; |
| 1093 | } while (a != NULL); |
| 1094 | return b == &PyBaseObject_Type; |
| 1095 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1098 | /* Internal routines to do a method lookup in the type |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1099 | without looking in the instance dictionary |
| 1100 | (so we can't use PyObject_GetAttr) but still binding |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1101 | it to the instance. The arguments are the object, |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1102 | the method name as a C string, and the address of a |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1103 | static variable used to cache the interned Python string. |
| 1104 | |
| 1105 | Two variants: |
| 1106 | |
| 1107 | - lookup_maybe() returns NULL without raising an exception |
| 1108 | when the _PyType_Lookup() call fails; |
| 1109 | |
| 1110 | - lookup_method() always raises an exception upon errors. |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1111 | |
| 1112 | - _PyObject_LookupSpecial() exported for the benefit of other places. |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1113 | */ |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1114 | |
| 1115 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1116 | lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1117 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1118 | PyObject *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1119 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1120 | if (*attrobj == NULL) { |
| 1121 | *attrobj = PyUnicode_InternFromString(attrstr); |
| 1122 | if (*attrobj == NULL) |
| 1123 | return NULL; |
| 1124 | } |
| 1125 | res = _PyType_Lookup(Py_TYPE(self), *attrobj); |
| 1126 | if (res != NULL) { |
| 1127 | descrgetfunc f; |
| 1128 | if ((f = Py_TYPE(res)->tp_descr_get) == NULL) |
| 1129 | Py_INCREF(res); |
| 1130 | else |
| 1131 | res = f(res, self, (PyObject *)(Py_TYPE(self))); |
| 1132 | } |
| 1133 | return res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1136 | static PyObject * |
| 1137 | lookup_method(PyObject *self, char *attrstr, PyObject **attrobj) |
| 1138 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1139 | PyObject *res = lookup_maybe(self, attrstr, attrobj); |
| 1140 | if (res == NULL && !PyErr_Occurred()) |
| 1141 | PyErr_SetObject(PyExc_AttributeError, *attrobj); |
| 1142 | return res; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1145 | PyObject * |
| 1146 | _PyObject_LookupSpecial(PyObject *self, char *attrstr, PyObject **attrobj) |
| 1147 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1148 | return lookup_maybe(self, attrstr, attrobj); |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1151 | /* A variation of PyObject_CallMethod that uses lookup_method() |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1152 | instead of PyObject_GetAttrString(). This uses the same convention |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1153 | as lookup_method to cache the interned name string object. */ |
| 1154 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1155 | static PyObject * |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1156 | call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 1157 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1158 | va_list va; |
| 1159 | PyObject *args, *func = 0, *retval; |
| 1160 | va_start(va, format); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1162 | func = lookup_maybe(o, name, nameobj); |
| 1163 | if (func == NULL) { |
| 1164 | va_end(va); |
| 1165 | if (!PyErr_Occurred()) |
| 1166 | PyErr_SetObject(PyExc_AttributeError, *nameobj); |
| 1167 | return NULL; |
| 1168 | } |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1170 | if (format && *format) |
| 1171 | args = Py_VaBuildValue(format, va); |
| 1172 | else |
| 1173 | args = PyTuple_New(0); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1175 | va_end(va); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1177 | if (args == NULL) |
| 1178 | return NULL; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1179 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1180 | assert(PyTuple_Check(args)); |
| 1181 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1183 | Py_DECREF(args); |
| 1184 | Py_DECREF(func); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1186 | return retval; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | /* Clone of call_method() that returns NotImplemented when the lookup fails. */ |
| 1190 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1191 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1192 | call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 1193 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1194 | va_list va; |
| 1195 | PyObject *args, *func = 0, *retval; |
| 1196 | va_start(va, format); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1197 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1198 | func = lookup_maybe(o, name, nameobj); |
| 1199 | if (func == NULL) { |
| 1200 | va_end(va); |
| 1201 | if (!PyErr_Occurred()) { |
| 1202 | Py_INCREF(Py_NotImplemented); |
| 1203 | return Py_NotImplemented; |
| 1204 | } |
| 1205 | return NULL; |
| 1206 | } |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1208 | if (format && *format) |
| 1209 | args = Py_VaBuildValue(format, va); |
| 1210 | else |
| 1211 | args = PyTuple_New(0); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1213 | va_end(va); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1214 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1215 | if (args == NULL) |
| 1216 | return NULL; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1218 | assert(PyTuple_Check(args)); |
| 1219 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1220 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1221 | Py_DECREF(args); |
| 1222 | Py_DECREF(func); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1223 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1224 | return retval; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1227 | /* |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1228 | Method resolution order algorithm C3 described in |
| 1229 | "A Monotonic Superclass Linearization for Dylan", |
| 1230 | by Kim Barrett, Bob Cassel, Paul Haahr, |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1231 | David A. Moon, Keith Playford, and P. Tucker Withington. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1232 | (OOPSLA 1996) |
| 1233 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1234 | Some notes about the rules implied by C3: |
| 1235 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1236 | No duplicate bases. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1237 | It isn't legal to repeat a class in a list of base classes. |
| 1238 | |
| 1239 | The next three properties are the 3 constraints in "C3". |
| 1240 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1241 | Local precendece order. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1242 | If A precedes B in C's MRO, then A will precede B in the MRO of all |
| 1243 | subclasses of C. |
| 1244 | |
| 1245 | Monotonicity. |
| 1246 | The MRO of a class must be an extension without reordering of the |
| 1247 | MRO of each of its superclasses. |
| 1248 | |
| 1249 | Extended Precedence Graph (EPG). |
| 1250 | Linearization is consistent if there is a path in the EPG from |
| 1251 | each class to all its successors in the linearization. See |
| 1252 | the paper for definition of EPG. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1253 | */ |
| 1254 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1255 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1256 | tail_contains(PyObject *list, int whence, PyObject *o) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1257 | Py_ssize_t j, size; |
| 1258 | size = PyList_GET_SIZE(list); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1260 | for (j = whence+1; j < size; j++) { |
| 1261 | if (PyList_GET_ITEM(list, j) == o) |
| 1262 | return 1; |
| 1263 | } |
| 1264 | return 0; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1267 | static PyObject * |
| 1268 | class_name(PyObject *cls) |
| 1269 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1270 | PyObject *name = PyObject_GetAttrString(cls, "__name__"); |
| 1271 | if (name == NULL) { |
| 1272 | PyErr_Clear(); |
| 1273 | Py_XDECREF(name); |
| 1274 | name = PyObject_Repr(cls); |
| 1275 | } |
| 1276 | if (name == NULL) |
| 1277 | return NULL; |
| 1278 | if (!PyUnicode_Check(name)) { |
| 1279 | Py_DECREF(name); |
| 1280 | return NULL; |
| 1281 | } |
| 1282 | return name; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | static int |
| 1286 | check_duplicates(PyObject *list) |
| 1287 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1288 | Py_ssize_t i, j, n; |
| 1289 | /* Let's use a quadratic time algorithm, |
| 1290 | assuming that the bases lists is short. |
| 1291 | */ |
| 1292 | n = PyList_GET_SIZE(list); |
| 1293 | for (i = 0; i < n; i++) { |
| 1294 | PyObject *o = PyList_GET_ITEM(list, i); |
| 1295 | for (j = i + 1; j < n; j++) { |
| 1296 | if (PyList_GET_ITEM(list, j) == o) { |
| 1297 | o = class_name(o); |
| 1298 | if (o != NULL) { |
| 1299 | PyErr_Format(PyExc_TypeError, |
| 1300 | "duplicate base class %U", |
| 1301 | o); |
| 1302 | Py_DECREF(o); |
| 1303 | } else { |
| 1304 | PyErr_SetString(PyExc_TypeError, |
| 1305 | "duplicate base class"); |
| 1306 | } |
| 1307 | return -1; |
| 1308 | } |
| 1309 | } |
| 1310 | } |
| 1311 | return 0; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | /* Raise a TypeError for an MRO order disagreement. |
| 1315 | |
| 1316 | It's hard to produce a good error message. In the absence of better |
| 1317 | insight into error reporting, report the classes that were candidates |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1318 | to be put next into the MRO. There is some conflict between the |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1319 | order in which they should be put in the MRO, but it's hard to |
| 1320 | diagnose what constraint can't be satisfied. |
| 1321 | */ |
| 1322 | |
| 1323 | static void |
| 1324 | set_mro_error(PyObject *to_merge, int *remain) |
| 1325 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1326 | Py_ssize_t i, n, off, to_merge_size; |
| 1327 | char buf[1000]; |
| 1328 | PyObject *k, *v; |
| 1329 | PyObject *set = PyDict_New(); |
| 1330 | if (!set) return; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1332 | to_merge_size = PyList_GET_SIZE(to_merge); |
| 1333 | for (i = 0; i < to_merge_size; i++) { |
| 1334 | PyObject *L = PyList_GET_ITEM(to_merge, i); |
| 1335 | if (remain[i] < PyList_GET_SIZE(L)) { |
| 1336 | PyObject *c = PyList_GET_ITEM(L, remain[i]); |
| 1337 | if (PyDict_SetItem(set, c, Py_None) < 0) { |
| 1338 | Py_DECREF(set); |
| 1339 | return; |
| 1340 | } |
| 1341 | } |
| 1342 | } |
| 1343 | n = PyDict_Size(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1345 | off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \ |
Raymond Hettinger | f394df4 | 2003-04-06 19:13:41 +0000 | [diff] [blame] | 1346 | consistent method resolution\norder (MRO) for bases"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1347 | i = 0; |
| 1348 | while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) { |
| 1349 | PyObject *name = class_name(k); |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1350 | char *name_str; |
| 1351 | if (name != NULL) { |
| 1352 | name_str = _PyUnicode_AsString(name); |
| 1353 | if (name_str == NULL) |
Victor Stinner | ba644a6 | 2010-05-19 01:50:45 +0000 | [diff] [blame] | 1354 | name_str = "?"; |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1355 | } else |
Victor Stinner | ba644a6 | 2010-05-19 01:50:45 +0000 | [diff] [blame] | 1356 | name_str = "?"; |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1357 | off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", name_str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1358 | Py_XDECREF(name); |
| 1359 | if (--n && (size_t)(off+1) < sizeof(buf)) { |
| 1360 | buf[off++] = ','; |
| 1361 | buf[off] = '\0'; |
| 1362 | } |
| 1363 | } |
| 1364 | PyErr_SetString(PyExc_TypeError, buf); |
| 1365 | Py_DECREF(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1368 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1369 | pmerge(PyObject *acc, PyObject* to_merge) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1370 | Py_ssize_t i, j, to_merge_size, empty_cnt; |
| 1371 | int *remain; |
| 1372 | int ok; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1374 | to_merge_size = PyList_GET_SIZE(to_merge); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1375 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1376 | /* remain stores an index into each sublist of to_merge. |
| 1377 | remain[i] is the index of the next base in to_merge[i] |
| 1378 | that is not included in acc. |
| 1379 | */ |
| 1380 | remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size); |
| 1381 | if (remain == NULL) |
| 1382 | return -1; |
| 1383 | for (i = 0; i < to_merge_size; i++) |
| 1384 | remain[i] = 0; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1385 | |
| 1386 | again: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1387 | empty_cnt = 0; |
| 1388 | for (i = 0; i < to_merge_size; i++) { |
| 1389 | PyObject *candidate; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1390 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1391 | PyObject *cur_list = PyList_GET_ITEM(to_merge, i); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1393 | if (remain[i] >= PyList_GET_SIZE(cur_list)) { |
| 1394 | empty_cnt++; |
| 1395 | continue; |
| 1396 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1398 | /* Choose next candidate for MRO. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1400 | The input sequences alone can determine the choice. |
| 1401 | If not, choose the class which appears in the MRO |
| 1402 | of the earliest direct superclass of the new class. |
| 1403 | */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1405 | candidate = PyList_GET_ITEM(cur_list, remain[i]); |
| 1406 | for (j = 0; j < to_merge_size; j++) { |
| 1407 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
| 1408 | if (tail_contains(j_lst, remain[j], candidate)) { |
| 1409 | goto skip; /* continue outer loop */ |
| 1410 | } |
| 1411 | } |
| 1412 | ok = PyList_Append(acc, candidate); |
| 1413 | if (ok < 0) { |
| 1414 | PyMem_Free(remain); |
| 1415 | return -1; |
| 1416 | } |
| 1417 | for (j = 0; j < to_merge_size; j++) { |
| 1418 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
| 1419 | if (remain[j] < PyList_GET_SIZE(j_lst) && |
| 1420 | PyList_GET_ITEM(j_lst, remain[j]) == candidate) { |
| 1421 | remain[j]++; |
| 1422 | } |
| 1423 | } |
| 1424 | goto again; |
| 1425 | skip: ; |
| 1426 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1427 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1428 | if (empty_cnt == to_merge_size) { |
| 1429 | PyMem_FREE(remain); |
| 1430 | return 0; |
| 1431 | } |
| 1432 | set_mro_error(to_merge, remain); |
| 1433 | PyMem_FREE(remain); |
| 1434 | return -1; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1437 | static PyObject * |
| 1438 | mro_implementation(PyTypeObject *type) |
| 1439 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1440 | Py_ssize_t i, n; |
| 1441 | int ok; |
| 1442 | PyObject *bases, *result; |
| 1443 | PyObject *to_merge, *bases_aslist; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1445 | if (type->tp_dict == NULL) { |
| 1446 | if (PyType_Ready(type) < 0) |
| 1447 | return NULL; |
| 1448 | } |
Guido van Rossum | 6351757 | 2002-06-18 16:44:57 +0000 | [diff] [blame] | 1449 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1450 | /* Find a superclass linearization that honors the constraints |
| 1451 | of the explicit lists of bases and the constraints implied by |
| 1452 | each base class. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1454 | to_merge is a list of lists, where each list is a superclass |
| 1455 | linearization implied by a base class. The last element of |
| 1456 | to_merge is the declared list of bases. |
| 1457 | */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1458 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1459 | bases = type->tp_bases; |
| 1460 | n = PyTuple_GET_SIZE(bases); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1461 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1462 | to_merge = PyList_New(n+1); |
| 1463 | if (to_merge == NULL) |
| 1464 | return NULL; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1466 | for (i = 0; i < n; i++) { |
| 1467 | PyObject *base = PyTuple_GET_ITEM(bases, i); |
| 1468 | PyObject *parentMRO; |
| 1469 | parentMRO = PySequence_List(((PyTypeObject*)base)->tp_mro); |
| 1470 | if (parentMRO == NULL) { |
| 1471 | Py_DECREF(to_merge); |
| 1472 | return NULL; |
| 1473 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1475 | PyList_SET_ITEM(to_merge, i, parentMRO); |
| 1476 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1477 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1478 | bases_aslist = PySequence_List(bases); |
| 1479 | if (bases_aslist == NULL) { |
| 1480 | Py_DECREF(to_merge); |
| 1481 | return NULL; |
| 1482 | } |
| 1483 | /* This is just a basic sanity check. */ |
| 1484 | if (check_duplicates(bases_aslist) < 0) { |
| 1485 | Py_DECREF(to_merge); |
| 1486 | Py_DECREF(bases_aslist); |
| 1487 | return NULL; |
| 1488 | } |
| 1489 | PyList_SET_ITEM(to_merge, n, bases_aslist); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1491 | result = Py_BuildValue("[O]", (PyObject *)type); |
| 1492 | if (result == NULL) { |
| 1493 | Py_DECREF(to_merge); |
| 1494 | return NULL; |
| 1495 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1497 | ok = pmerge(result, to_merge); |
| 1498 | Py_DECREF(to_merge); |
| 1499 | if (ok < 0) { |
| 1500 | Py_DECREF(result); |
| 1501 | return NULL; |
| 1502 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1504 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1508 | mro_external(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1509 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1510 | PyTypeObject *type = (PyTypeObject *)self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1511 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1512 | return mro_implementation(type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | static int |
| 1516 | mro_internal(PyTypeObject *type) |
| 1517 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1518 | PyObject *mro, *result, *tuple; |
| 1519 | int checkit = 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1521 | if (Py_TYPE(type) == &PyType_Type) { |
| 1522 | result = mro_implementation(type); |
| 1523 | } |
| 1524 | else { |
| 1525 | static PyObject *mro_str; |
| 1526 | checkit = 1; |
| 1527 | mro = lookup_method((PyObject *)type, "mro", &mro_str); |
| 1528 | if (mro == NULL) |
| 1529 | return -1; |
| 1530 | result = PyObject_CallObject(mro, NULL); |
| 1531 | Py_DECREF(mro); |
| 1532 | } |
| 1533 | if (result == NULL) |
| 1534 | return -1; |
| 1535 | tuple = PySequence_Tuple(result); |
| 1536 | Py_DECREF(result); |
| 1537 | if (tuple == NULL) |
| 1538 | return -1; |
| 1539 | if (checkit) { |
| 1540 | Py_ssize_t i, len; |
| 1541 | PyObject *cls; |
| 1542 | PyTypeObject *solid; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1544 | solid = solid_base(type); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1546 | len = PyTuple_GET_SIZE(tuple); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1548 | for (i = 0; i < len; i++) { |
| 1549 | PyTypeObject *t; |
| 1550 | cls = PyTuple_GET_ITEM(tuple, i); |
| 1551 | if (!PyType_Check(cls)) { |
| 1552 | PyErr_Format(PyExc_TypeError, |
| 1553 | "mro() returned a non-class ('%.500s')", |
| 1554 | Py_TYPE(cls)->tp_name); |
| 1555 | Py_DECREF(tuple); |
| 1556 | return -1; |
| 1557 | } |
| 1558 | t = (PyTypeObject*)cls; |
| 1559 | if (!PyType_IsSubtype(solid, solid_base(t))) { |
| 1560 | PyErr_Format(PyExc_TypeError, |
| 1561 | "mro() returned base with unsuitable layout ('%.500s')", |
| 1562 | t->tp_name); |
| 1563 | Py_DECREF(tuple); |
| 1564 | return -1; |
| 1565 | } |
| 1566 | } |
| 1567 | } |
| 1568 | type->tp_mro = tuple; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1569 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | type_mro_modified(type, type->tp_mro); |
| 1571 | /* corner case: the old-style super class might have been hidden |
| 1572 | from the custom MRO */ |
| 1573 | type_mro_modified(type, type->tp_bases); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1574 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1575 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1576 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1577 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | |
| 1581 | /* Calculate the best base amongst multiple base classes. |
| 1582 | This is the first one that's on the path to the "solid base". */ |
| 1583 | |
| 1584 | static PyTypeObject * |
| 1585 | best_base(PyObject *bases) |
| 1586 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1587 | Py_ssize_t i, n; |
| 1588 | PyTypeObject *base, *winner, *candidate, *base_i; |
| 1589 | PyObject *base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1591 | assert(PyTuple_Check(bases)); |
| 1592 | n = PyTuple_GET_SIZE(bases); |
| 1593 | assert(n > 0); |
| 1594 | base = NULL; |
| 1595 | winner = NULL; |
| 1596 | for (i = 0; i < n; i++) { |
| 1597 | base_proto = PyTuple_GET_ITEM(bases, i); |
| 1598 | if (!PyType_Check(base_proto)) { |
| 1599 | PyErr_SetString( |
| 1600 | PyExc_TypeError, |
| 1601 | "bases must be types"); |
| 1602 | return NULL; |
| 1603 | } |
| 1604 | base_i = (PyTypeObject *)base_proto; |
| 1605 | if (base_i->tp_dict == NULL) { |
| 1606 | if (PyType_Ready(base_i) < 0) |
| 1607 | return NULL; |
| 1608 | } |
| 1609 | candidate = solid_base(base_i); |
| 1610 | if (winner == NULL) { |
| 1611 | winner = candidate; |
| 1612 | base = base_i; |
| 1613 | } |
| 1614 | else if (PyType_IsSubtype(winner, candidate)) |
| 1615 | ; |
| 1616 | else if (PyType_IsSubtype(candidate, winner)) { |
| 1617 | winner = candidate; |
| 1618 | base = base_i; |
| 1619 | } |
| 1620 | else { |
| 1621 | PyErr_SetString( |
| 1622 | PyExc_TypeError, |
| 1623 | "multiple bases have " |
| 1624 | "instance lay-out conflict"); |
| 1625 | return NULL; |
| 1626 | } |
| 1627 | } |
| 1628 | if (base == NULL) |
| 1629 | PyErr_SetString(PyExc_TypeError, |
| 1630 | "a new-style class can't have only classic bases"); |
| 1631 | return base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | static int |
| 1635 | extra_ivars(PyTypeObject *type, PyTypeObject *base) |
| 1636 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1637 | size_t t_size = type->tp_basicsize; |
| 1638 | size_t b_size = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1640 | assert(t_size >= b_size); /* Else type smaller than base! */ |
| 1641 | if (type->tp_itemsize || base->tp_itemsize) { |
| 1642 | /* If itemsize is involved, stricter rules */ |
| 1643 | return t_size != b_size || |
| 1644 | type->tp_itemsize != base->tp_itemsize; |
| 1645 | } |
| 1646 | if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 && |
| 1647 | type->tp_weaklistoffset + sizeof(PyObject *) == t_size && |
| 1648 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 1649 | t_size -= sizeof(PyObject *); |
| 1650 | if (type->tp_dictoffset && base->tp_dictoffset == 0 && |
| 1651 | type->tp_dictoffset + sizeof(PyObject *) == t_size && |
| 1652 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 1653 | t_size -= sizeof(PyObject *); |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1655 | return t_size != b_size; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | static PyTypeObject * |
| 1659 | solid_base(PyTypeObject *type) |
| 1660 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1661 | PyTypeObject *base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1663 | if (type->tp_base) |
| 1664 | base = solid_base(type->tp_base); |
| 1665 | else |
| 1666 | base = &PyBaseObject_Type; |
| 1667 | if (extra_ivars(type, base)) |
| 1668 | return type; |
| 1669 | else |
| 1670 | return base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1673 | static void object_dealloc(PyObject *); |
| 1674 | static int object_init(PyObject *, PyObject *, PyObject *); |
| 1675 | static int update_slot(PyTypeObject *, PyObject *); |
| 1676 | static void fixup_slot_dispatchers(PyTypeObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1677 | |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1678 | /* |
| 1679 | * Helpers for __dict__ descriptor. We don't want to expose the dicts |
| 1680 | * inherited from various builtin types. The builtin base usually provides |
| 1681 | * its own __dict__ descriptor, so we use that when we can. |
| 1682 | */ |
| 1683 | static PyTypeObject * |
| 1684 | get_builtin_base_with_dict(PyTypeObject *type) |
| 1685 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1686 | while (type->tp_base != NULL) { |
| 1687 | if (type->tp_dictoffset != 0 && |
| 1688 | !(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 1689 | return type; |
| 1690 | type = type->tp_base; |
| 1691 | } |
| 1692 | return NULL; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | static PyObject * |
| 1696 | get_dict_descriptor(PyTypeObject *type) |
| 1697 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1698 | static PyObject *dict_str; |
| 1699 | PyObject *descr; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1700 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1701 | if (dict_str == NULL) { |
| 1702 | dict_str = PyUnicode_InternFromString("__dict__"); |
| 1703 | if (dict_str == NULL) |
| 1704 | return NULL; |
| 1705 | } |
| 1706 | descr = _PyType_Lookup(type, dict_str); |
| 1707 | if (descr == NULL || !PyDescr_IsData(descr)) |
| 1708 | return NULL; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1709 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1710 | return descr; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | static void |
| 1714 | raise_dict_descr_error(PyObject *obj) |
| 1715 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1716 | PyErr_Format(PyExc_TypeError, |
| 1717 | "this __dict__ descriptor does not support " |
| 1718 | "'%.200s' objects", Py_TYPE(obj)->tp_name); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1721 | static PyObject * |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1722 | subtype_dict(PyObject *obj, void *context) |
| 1723 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1724 | PyObject **dictptr; |
| 1725 | PyObject *dict; |
| 1726 | PyTypeObject *base; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1727 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1728 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
| 1729 | if (base != NULL) { |
| 1730 | descrgetfunc func; |
| 1731 | PyObject *descr = get_dict_descriptor(base); |
| 1732 | if (descr == NULL) { |
| 1733 | raise_dict_descr_error(obj); |
| 1734 | return NULL; |
| 1735 | } |
| 1736 | func = Py_TYPE(descr)->tp_descr_get; |
| 1737 | if (func == NULL) { |
| 1738 | raise_dict_descr_error(obj); |
| 1739 | return NULL; |
| 1740 | } |
| 1741 | return func(descr, obj, (PyObject *)(Py_TYPE(obj))); |
| 1742 | } |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1743 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1744 | dictptr = _PyObject_GetDictPtr(obj); |
| 1745 | if (dictptr == NULL) { |
| 1746 | PyErr_SetString(PyExc_AttributeError, |
| 1747 | "This object has no __dict__"); |
| 1748 | return NULL; |
| 1749 | } |
| 1750 | dict = *dictptr; |
| 1751 | if (dict == NULL) |
| 1752 | *dictptr = dict = PyDict_New(); |
| 1753 | Py_XINCREF(dict); |
| 1754 | return dict; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1757 | static int |
| 1758 | subtype_setdict(PyObject *obj, PyObject *value, void *context) |
| 1759 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1760 | PyObject **dictptr; |
| 1761 | PyObject *dict; |
| 1762 | PyTypeObject *base; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1764 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
| 1765 | if (base != NULL) { |
| 1766 | descrsetfunc func; |
| 1767 | PyObject *descr = get_dict_descriptor(base); |
| 1768 | if (descr == NULL) { |
| 1769 | raise_dict_descr_error(obj); |
| 1770 | return -1; |
| 1771 | } |
| 1772 | func = Py_TYPE(descr)->tp_descr_set; |
| 1773 | if (func == NULL) { |
| 1774 | raise_dict_descr_error(obj); |
| 1775 | return -1; |
| 1776 | } |
| 1777 | return func(descr, obj, value); |
| 1778 | } |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1779 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1780 | dictptr = _PyObject_GetDictPtr(obj); |
| 1781 | if (dictptr == NULL) { |
| 1782 | PyErr_SetString(PyExc_AttributeError, |
| 1783 | "This object has no __dict__"); |
| 1784 | return -1; |
| 1785 | } |
| 1786 | if (value != NULL && !PyDict_Check(value)) { |
| 1787 | PyErr_Format(PyExc_TypeError, |
| 1788 | "__dict__ must be set to a dictionary, " |
| 1789 | "not a '%.200s'", Py_TYPE(value)->tp_name); |
| 1790 | return -1; |
| 1791 | } |
| 1792 | dict = *dictptr; |
| 1793 | Py_XINCREF(value); |
| 1794 | *dictptr = value; |
| 1795 | Py_XDECREF(dict); |
| 1796 | return 0; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1799 | static PyObject * |
| 1800 | subtype_getweakref(PyObject *obj, void *context) |
| 1801 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1802 | PyObject **weaklistptr; |
| 1803 | PyObject *result; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1804 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1805 | if (Py_TYPE(obj)->tp_weaklistoffset == 0) { |
| 1806 | PyErr_SetString(PyExc_AttributeError, |
| 1807 | "This object has no __weakref__"); |
| 1808 | return NULL; |
| 1809 | } |
| 1810 | assert(Py_TYPE(obj)->tp_weaklistoffset > 0); |
| 1811 | assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <= |
| 1812 | (size_t)(Py_TYPE(obj)->tp_basicsize)); |
| 1813 | weaklistptr = (PyObject **) |
| 1814 | ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset); |
| 1815 | if (*weaklistptr == NULL) |
| 1816 | result = Py_None; |
| 1817 | else |
| 1818 | result = *weaklistptr; |
| 1819 | Py_INCREF(result); |
| 1820 | return result; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1823 | /* Three variants on the subtype_getsets list. */ |
| 1824 | |
| 1825 | static PyGetSetDef subtype_getsets_full[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1826 | {"__dict__", subtype_dict, subtype_setdict, |
| 1827 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 1828 | {"__weakref__", subtype_getweakref, NULL, |
| 1829 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 1830 | {0} |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1831 | }; |
| 1832 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1833 | static PyGetSetDef subtype_getsets_dict_only[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1834 | {"__dict__", subtype_dict, subtype_setdict, |
| 1835 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 1836 | {0} |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1837 | }; |
| 1838 | |
| 1839 | static PyGetSetDef subtype_getsets_weakref_only[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1840 | {"__weakref__", subtype_getweakref, NULL, |
| 1841 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 1842 | {0} |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1843 | }; |
| 1844 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1845 | static int |
| 1846 | valid_identifier(PyObject *s) |
| 1847 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1848 | if (!PyUnicode_Check(s)) { |
| 1849 | PyErr_Format(PyExc_TypeError, |
| 1850 | "__slots__ items must be strings, not '%.200s'", |
| 1851 | Py_TYPE(s)->tp_name); |
| 1852 | return 0; |
| 1853 | } |
| 1854 | if (!PyUnicode_IsIdentifier(s)) { |
| 1855 | PyErr_SetString(PyExc_TypeError, |
| 1856 | "__slots__ must be identifiers"); |
| 1857 | return 0; |
| 1858 | } |
| 1859 | return 1; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1862 | /* Forward */ |
| 1863 | static int |
| 1864 | object_init(PyObject *self, PyObject *args, PyObject *kwds); |
| 1865 | |
| 1866 | static int |
| 1867 | type_init(PyObject *cls, PyObject *args, PyObject *kwds) |
| 1868 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1869 | int res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1870 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1871 | assert(args != NULL && PyTuple_Check(args)); |
| 1872 | assert(kwds == NULL || PyDict_Check(kwds)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1874 | if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) { |
| 1875 | PyErr_SetString(PyExc_TypeError, |
| 1876 | "type.__init__() takes no keyword arguments"); |
| 1877 | return -1; |
| 1878 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1880 | if (args != NULL && PyTuple_Check(args) && |
| 1881 | (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) { |
| 1882 | PyErr_SetString(PyExc_TypeError, |
| 1883 | "type.__init__() takes 1 or 3 arguments"); |
| 1884 | return -1; |
| 1885 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1887 | /* Call object.__init__(self) now. */ |
| 1888 | /* XXX Could call super(type, cls).__init__() but what's the point? */ |
| 1889 | args = PyTuple_GetSlice(args, 0, 0); |
| 1890 | res = object_init(cls, args, NULL); |
| 1891 | Py_DECREF(args); |
| 1892 | return res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1893 | } |
| 1894 | |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1895 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1896 | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) |
| 1897 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1898 | PyObject *name, *bases, *dict; |
| 1899 | static char *kwlist[] = {"name", "bases", "dict", 0}; |
| 1900 | PyObject *slots, *tmp, *newslots; |
| 1901 | PyTypeObject *type, *base, *tmptype, *winner; |
| 1902 | PyHeapTypeObject *et; |
| 1903 | PyMemberDef *mp; |
| 1904 | Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak; |
| 1905 | int j, may_add_dict, may_add_weak; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1907 | assert(args != NULL && PyTuple_Check(args)); |
| 1908 | assert(kwds == NULL || PyDict_Check(kwds)); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1910 | /* Special case: type(x) should return x->ob_type */ |
| 1911 | { |
| 1912 | const Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 1913 | const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1915 | if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) { |
| 1916 | PyObject *x = PyTuple_GET_ITEM(args, 0); |
| 1917 | Py_INCREF(Py_TYPE(x)); |
| 1918 | return (PyObject *) Py_TYPE(x); |
| 1919 | } |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1920 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1921 | /* SF bug 475327 -- if that didn't trigger, we need 3 |
| 1922 | arguments. but PyArg_ParseTupleAndKeywords below may give |
| 1923 | a msg saying type() needs exactly 3. */ |
| 1924 | if (nargs + nkwds != 3) { |
| 1925 | PyErr_SetString(PyExc_TypeError, |
| 1926 | "type() takes 1 or 3 arguments"); |
| 1927 | return NULL; |
| 1928 | } |
| 1929 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1930 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1931 | /* Check arguments: (name, bases, dict) */ |
| 1932 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "UO!O!:type", kwlist, |
| 1933 | &name, |
| 1934 | &PyTuple_Type, &bases, |
| 1935 | &PyDict_Type, &dict)) |
| 1936 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1938 | /* Determine the proper metatype to deal with this, |
| 1939 | and check for metatype conflicts while we're at it. |
| 1940 | Note that if some other metatype wins to contract, |
| 1941 | it's possible that its instances are not types. */ |
| 1942 | nbases = PyTuple_GET_SIZE(bases); |
| 1943 | winner = metatype; |
| 1944 | for (i = 0; i < nbases; i++) { |
| 1945 | tmp = PyTuple_GET_ITEM(bases, i); |
| 1946 | tmptype = Py_TYPE(tmp); |
| 1947 | if (PyType_IsSubtype(winner, tmptype)) |
| 1948 | continue; |
| 1949 | if (PyType_IsSubtype(tmptype, winner)) { |
| 1950 | winner = tmptype; |
| 1951 | continue; |
| 1952 | } |
| 1953 | PyErr_SetString(PyExc_TypeError, |
| 1954 | "metaclass conflict: " |
| 1955 | "the metaclass of a derived class " |
| 1956 | "must be a (non-strict) subclass " |
| 1957 | "of the metaclasses of all its bases"); |
| 1958 | return NULL; |
| 1959 | } |
| 1960 | if (winner != metatype) { |
| 1961 | if (winner->tp_new != type_new) /* Pass it to the winner */ |
| 1962 | return winner->tp_new(winner, args, kwds); |
| 1963 | metatype = winner; |
| 1964 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1965 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1966 | /* Adjust for empty tuple bases */ |
| 1967 | if (nbases == 0) { |
| 1968 | bases = PyTuple_Pack(1, &PyBaseObject_Type); |
| 1969 | if (bases == NULL) |
| 1970 | return NULL; |
| 1971 | nbases = 1; |
| 1972 | } |
| 1973 | else |
| 1974 | Py_INCREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1975 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1976 | /* XXX From here until type is allocated, "return NULL" leaks bases! */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1977 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1978 | /* Calculate best base, and check that all bases are type objects */ |
| 1979 | base = best_base(bases); |
| 1980 | if (base == NULL) { |
| 1981 | Py_DECREF(bases); |
| 1982 | return NULL; |
| 1983 | } |
| 1984 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 1985 | PyErr_Format(PyExc_TypeError, |
| 1986 | "type '%.100s' is not an acceptable base type", |
| 1987 | base->tp_name); |
| 1988 | Py_DECREF(bases); |
| 1989 | return NULL; |
| 1990 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1991 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1992 | /* Check for a __slots__ sequence variable in dict, and count it */ |
| 1993 | slots = PyDict_GetItemString(dict, "__slots__"); |
| 1994 | nslots = 0; |
| 1995 | add_dict = 0; |
| 1996 | add_weak = 0; |
| 1997 | may_add_dict = base->tp_dictoffset == 0; |
| 1998 | may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0; |
| 1999 | if (slots == NULL) { |
| 2000 | if (may_add_dict) { |
| 2001 | add_dict++; |
| 2002 | } |
| 2003 | if (may_add_weak) { |
| 2004 | add_weak++; |
| 2005 | } |
| 2006 | } |
| 2007 | else { |
| 2008 | /* Have slots */ |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2009 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2010 | /* Make it into a tuple */ |
| 2011 | if (PyUnicode_Check(slots)) |
| 2012 | slots = PyTuple_Pack(1, slots); |
| 2013 | else |
| 2014 | slots = PySequence_Tuple(slots); |
| 2015 | if (slots == NULL) { |
| 2016 | Py_DECREF(bases); |
| 2017 | return NULL; |
| 2018 | } |
| 2019 | assert(PyTuple_Check(slots)); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2020 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2021 | /* Are slots allowed? */ |
| 2022 | nslots = PyTuple_GET_SIZE(slots); |
| 2023 | if (nslots > 0 && base->tp_itemsize != 0) { |
| 2024 | PyErr_Format(PyExc_TypeError, |
| 2025 | "nonempty __slots__ " |
| 2026 | "not supported for subtype of '%s'", |
| 2027 | base->tp_name); |
| 2028 | bad_slots: |
| 2029 | Py_DECREF(bases); |
| 2030 | Py_DECREF(slots); |
| 2031 | return NULL; |
| 2032 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2033 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2034 | /* Check for valid slot names and two special cases */ |
| 2035 | for (i = 0; i < nslots; i++) { |
| 2036 | PyObject *tmp = PyTuple_GET_ITEM(slots, i); |
| 2037 | if (!valid_identifier(tmp)) |
| 2038 | goto bad_slots; |
| 2039 | assert(PyUnicode_Check(tmp)); |
| 2040 | if (PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) { |
| 2041 | if (!may_add_dict || add_dict) { |
| 2042 | PyErr_SetString(PyExc_TypeError, |
| 2043 | "__dict__ slot disallowed: " |
| 2044 | "we already got one"); |
| 2045 | goto bad_slots; |
| 2046 | } |
| 2047 | add_dict++; |
| 2048 | } |
| 2049 | if (PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0) { |
| 2050 | if (!may_add_weak || add_weak) { |
| 2051 | PyErr_SetString(PyExc_TypeError, |
| 2052 | "__weakref__ slot disallowed: " |
| 2053 | "either we already got one, " |
| 2054 | "or __itemsize__ != 0"); |
| 2055 | goto bad_slots; |
| 2056 | } |
| 2057 | add_weak++; |
| 2058 | } |
| 2059 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2060 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2061 | /* Copy slots into a list, mangle names and sort them. |
| 2062 | Sorted names are needed for __class__ assignment. |
| 2063 | Convert them back to tuple at the end. |
| 2064 | */ |
| 2065 | newslots = PyList_New(nslots - add_dict - add_weak); |
| 2066 | if (newslots == NULL) |
| 2067 | goto bad_slots; |
| 2068 | for (i = j = 0; i < nslots; i++) { |
| 2069 | tmp = PyTuple_GET_ITEM(slots, i); |
| 2070 | if ((add_dict && |
| 2071 | PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) || |
| 2072 | (add_weak && |
| 2073 | PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0)) |
| 2074 | continue; |
| 2075 | tmp =_Py_Mangle(name, tmp); |
| 2076 | if (!tmp) |
| 2077 | goto bad_slots; |
| 2078 | PyList_SET_ITEM(newslots, j, tmp); |
| 2079 | j++; |
| 2080 | } |
| 2081 | assert(j == nslots - add_dict - add_weak); |
| 2082 | nslots = j; |
| 2083 | Py_DECREF(slots); |
| 2084 | if (PyList_Sort(newslots) == -1) { |
| 2085 | Py_DECREF(bases); |
| 2086 | Py_DECREF(newslots); |
| 2087 | return NULL; |
| 2088 | } |
| 2089 | slots = PyList_AsTuple(newslots); |
| 2090 | Py_DECREF(newslots); |
| 2091 | if (slots == NULL) { |
| 2092 | Py_DECREF(bases); |
| 2093 | return NULL; |
| 2094 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2095 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2096 | /* Secondary bases may provide weakrefs or dict */ |
| 2097 | if (nbases > 1 && |
| 2098 | ((may_add_dict && !add_dict) || |
| 2099 | (may_add_weak && !add_weak))) { |
| 2100 | for (i = 0; i < nbases; i++) { |
| 2101 | tmp = PyTuple_GET_ITEM(bases, i); |
| 2102 | if (tmp == (PyObject *)base) |
| 2103 | continue; /* Skip primary base */ |
| 2104 | assert(PyType_Check(tmp)); |
| 2105 | tmptype = (PyTypeObject *)tmp; |
| 2106 | if (may_add_dict && !add_dict && |
| 2107 | tmptype->tp_dictoffset != 0) |
| 2108 | add_dict++; |
| 2109 | if (may_add_weak && !add_weak && |
| 2110 | tmptype->tp_weaklistoffset != 0) |
| 2111 | add_weak++; |
| 2112 | if (may_add_dict && !add_dict) |
| 2113 | continue; |
| 2114 | if (may_add_weak && !add_weak) |
| 2115 | continue; |
| 2116 | /* Nothing more to check */ |
| 2117 | break; |
| 2118 | } |
| 2119 | } |
| 2120 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2121 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2122 | /* XXX From here until type is safely allocated, |
| 2123 | "return NULL" may leak slots! */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2124 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2125 | /* Allocate the type object */ |
| 2126 | type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots); |
| 2127 | if (type == NULL) { |
| 2128 | Py_XDECREF(slots); |
| 2129 | Py_DECREF(bases); |
| 2130 | return NULL; |
| 2131 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2132 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2133 | /* Keep name and slots alive in the extended type object */ |
| 2134 | et = (PyHeapTypeObject *)type; |
| 2135 | Py_INCREF(name); |
| 2136 | et->ht_name = name; |
| 2137 | et->ht_slots = slots; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2139 | /* Initialize tp_flags */ |
| 2140 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | |
| 2141 | Py_TPFLAGS_BASETYPE; |
| 2142 | if (base->tp_flags & Py_TPFLAGS_HAVE_GC) |
| 2143 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2145 | /* Initialize essential fields */ |
| 2146 | type->tp_as_number = &et->as_number; |
| 2147 | type->tp_as_sequence = &et->as_sequence; |
| 2148 | type->tp_as_mapping = &et->as_mapping; |
| 2149 | type->tp_as_buffer = &et->as_buffer; |
| 2150 | type->tp_name = _PyUnicode_AsString(name); |
| 2151 | if (!type->tp_name) { |
| 2152 | Py_DECREF(type); |
| 2153 | return NULL; |
| 2154 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2155 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2156 | /* Set tp_base and tp_bases */ |
| 2157 | type->tp_bases = bases; |
| 2158 | Py_INCREF(base); |
| 2159 | type->tp_base = base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2161 | /* Initialize tp_dict from passed-in dict */ |
| 2162 | type->tp_dict = dict = PyDict_Copy(dict); |
| 2163 | if (dict == NULL) { |
| 2164 | Py_DECREF(type); |
| 2165 | return NULL; |
| 2166 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2167 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2168 | /* Set __module__ in the dict */ |
| 2169 | if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 2170 | tmp = PyEval_GetGlobals(); |
| 2171 | if (tmp != NULL) { |
| 2172 | tmp = PyDict_GetItemString(tmp, "__name__"); |
| 2173 | if (tmp != NULL) { |
| 2174 | if (PyDict_SetItemString(dict, "__module__", |
| 2175 | tmp) < 0) |
| 2176 | return NULL; |
| 2177 | } |
| 2178 | } |
| 2179 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 2180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2181 | /* Set tp_doc to a copy of dict['__doc__'], if the latter is there |
| 2182 | and is a string. The __doc__ accessor will first look for tp_doc; |
| 2183 | if that fails, it will still look into __dict__. |
| 2184 | */ |
| 2185 | { |
| 2186 | PyObject *doc = PyDict_GetItemString(dict, "__doc__"); |
| 2187 | if (doc != NULL && PyUnicode_Check(doc)) { |
| 2188 | Py_ssize_t len; |
| 2189 | char *doc_str; |
| 2190 | char *tp_doc; |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 2191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2192 | doc_str = _PyUnicode_AsString(doc); |
| 2193 | if (doc_str == NULL) { |
| 2194 | Py_DECREF(type); |
| 2195 | return NULL; |
| 2196 | } |
| 2197 | /* Silently truncate the docstring if it contains null bytes. */ |
| 2198 | len = strlen(doc_str); |
| 2199 | tp_doc = (char *)PyObject_MALLOC(len + 1); |
| 2200 | if (tp_doc == NULL) { |
| 2201 | Py_DECREF(type); |
| 2202 | return NULL; |
| 2203 | } |
| 2204 | memcpy(tp_doc, doc_str, len + 1); |
| 2205 | type->tp_doc = tp_doc; |
| 2206 | } |
| 2207 | } |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2209 | /* Special-case __new__: if it's a plain function, |
| 2210 | make it a static function */ |
| 2211 | tmp = PyDict_GetItemString(dict, "__new__"); |
| 2212 | if (tmp != NULL && PyFunction_Check(tmp)) { |
| 2213 | tmp = PyStaticMethod_New(tmp); |
| 2214 | if (tmp == NULL) { |
| 2215 | Py_DECREF(type); |
| 2216 | return NULL; |
| 2217 | } |
| 2218 | PyDict_SetItemString(dict, "__new__", tmp); |
| 2219 | Py_DECREF(tmp); |
| 2220 | } |
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 | /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
| 2223 | mp = PyHeapType_GET_MEMBERS(et); |
| 2224 | slotoffset = base->tp_basicsize; |
| 2225 | if (slots != NULL) { |
| 2226 | for (i = 0; i < nslots; i++, mp++) { |
| 2227 | mp->name = _PyUnicode_AsString( |
| 2228 | PyTuple_GET_ITEM(slots, i)); |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 2229 | if (mp->name == NULL) { |
| 2230 | Py_DECREF(type); |
| 2231 | return NULL; |
| 2232 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2233 | mp->type = T_OBJECT_EX; |
| 2234 | mp->offset = slotoffset; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2236 | /* __dict__ and __weakref__ are already filtered out */ |
| 2237 | assert(strcmp(mp->name, "__dict__") != 0); |
| 2238 | assert(strcmp(mp->name, "__weakref__") != 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2240 | slotoffset += sizeof(PyObject *); |
| 2241 | } |
| 2242 | } |
| 2243 | if (add_dict) { |
| 2244 | if (base->tp_itemsize) |
| 2245 | type->tp_dictoffset = -(long)sizeof(PyObject *); |
| 2246 | else |
| 2247 | type->tp_dictoffset = slotoffset; |
| 2248 | slotoffset += sizeof(PyObject *); |
| 2249 | } |
| 2250 | if (add_weak) { |
| 2251 | assert(!base->tp_itemsize); |
| 2252 | type->tp_weaklistoffset = slotoffset; |
| 2253 | slotoffset += sizeof(PyObject *); |
| 2254 | } |
| 2255 | type->tp_basicsize = slotoffset; |
| 2256 | type->tp_itemsize = base->tp_itemsize; |
| 2257 | type->tp_members = PyHeapType_GET_MEMBERS(et); |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2259 | if (type->tp_weaklistoffset && type->tp_dictoffset) |
| 2260 | type->tp_getset = subtype_getsets_full; |
| 2261 | else if (type->tp_weaklistoffset && !type->tp_dictoffset) |
| 2262 | type->tp_getset = subtype_getsets_weakref_only; |
| 2263 | else if (!type->tp_weaklistoffset && type->tp_dictoffset) |
| 2264 | type->tp_getset = subtype_getsets_dict_only; |
| 2265 | else |
| 2266 | type->tp_getset = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2267 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2268 | /* Special case some slots */ |
| 2269 | if (type->tp_dictoffset != 0 || nslots > 0) { |
| 2270 | if (base->tp_getattr == NULL && base->tp_getattro == NULL) |
| 2271 | type->tp_getattro = PyObject_GenericGetAttr; |
| 2272 | if (base->tp_setattr == NULL && base->tp_setattro == NULL) |
| 2273 | type->tp_setattro = PyObject_GenericSetAttr; |
| 2274 | } |
| 2275 | type->tp_dealloc = subtype_dealloc; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2277 | /* Enable GC unless there are really no instance variables possible */ |
| 2278 | if (!(type->tp_basicsize == sizeof(PyObject) && |
| 2279 | type->tp_itemsize == 0)) |
| 2280 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 2281 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2282 | /* Always override allocation strategy to use regular heap */ |
| 2283 | type->tp_alloc = PyType_GenericAlloc; |
| 2284 | if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { |
| 2285 | type->tp_free = PyObject_GC_Del; |
| 2286 | type->tp_traverse = subtype_traverse; |
| 2287 | type->tp_clear = subtype_clear; |
| 2288 | } |
| 2289 | else |
| 2290 | type->tp_free = PyObject_Del; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2291 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2292 | /* Initialize the rest */ |
| 2293 | if (PyType_Ready(type) < 0) { |
| 2294 | Py_DECREF(type); |
| 2295 | return NULL; |
| 2296 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2298 | /* Put the proper slots in place */ |
| 2299 | fixup_slot_dispatchers(type); |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 2300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2301 | return (PyObject *)type; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
| 2304 | /* Internal API to look for a name through the MRO. |
| 2305 | This returns a borrowed reference, and doesn't set an exception! */ |
| 2306 | PyObject * |
| 2307 | _PyType_Lookup(PyTypeObject *type, PyObject *name) |
| 2308 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2309 | Py_ssize_t i, n; |
| 2310 | PyObject *mro, *res, *base, *dict; |
| 2311 | unsigned int h; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 2312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2313 | if (MCACHE_CACHEABLE_NAME(name) && |
| 2314 | PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) { |
| 2315 | /* fast path */ |
| 2316 | h = MCACHE_HASH_METHOD(type, name); |
| 2317 | if (method_cache[h].version == type->tp_version_tag && |
| 2318 | method_cache[h].name == name) |
| 2319 | return method_cache[h].value; |
| 2320 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2322 | /* Look in tp_dict of types in MRO */ |
| 2323 | mro = type->tp_mro; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 2324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2325 | /* If mro is NULL, the type is either not yet initialized |
| 2326 | by PyType_Ready(), or already cleared by type_clear(). |
| 2327 | Either way the safest thing to do is to return NULL. */ |
| 2328 | if (mro == NULL) |
| 2329 | return NULL; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 2330 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2331 | res = NULL; |
| 2332 | assert(PyTuple_Check(mro)); |
| 2333 | n = PyTuple_GET_SIZE(mro); |
| 2334 | for (i = 0; i < n; i++) { |
| 2335 | base = PyTuple_GET_ITEM(mro, i); |
| 2336 | assert(PyType_Check(base)); |
| 2337 | dict = ((PyTypeObject *)base)->tp_dict; |
| 2338 | assert(dict && PyDict_Check(dict)); |
| 2339 | res = PyDict_GetItem(dict, name); |
| 2340 | if (res != NULL) |
| 2341 | break; |
| 2342 | } |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 2343 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2344 | if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) { |
| 2345 | h = MCACHE_HASH_METHOD(type, name); |
| 2346 | method_cache[h].version = type->tp_version_tag; |
| 2347 | method_cache[h].value = res; /* borrowed */ |
| 2348 | Py_INCREF(name); |
| 2349 | Py_DECREF(method_cache[h].name); |
| 2350 | method_cache[h].name = name; |
| 2351 | } |
| 2352 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2353 | } |
| 2354 | |
| 2355 | /* This is similar to PyObject_GenericGetAttr(), |
| 2356 | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ |
| 2357 | static PyObject * |
| 2358 | type_getattro(PyTypeObject *type, PyObject *name) |
| 2359 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2360 | PyTypeObject *metatype = Py_TYPE(type); |
| 2361 | PyObject *meta_attribute, *attribute; |
| 2362 | descrgetfunc meta_get; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2363 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2364 | /* Initialize this type (we'll assume the metatype is initialized) */ |
| 2365 | if (type->tp_dict == NULL) { |
| 2366 | if (PyType_Ready(type) < 0) |
| 2367 | return NULL; |
| 2368 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2370 | /* No readable descriptor found yet */ |
| 2371 | meta_get = NULL; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2373 | /* Look for the attribute in the metatype */ |
| 2374 | meta_attribute = _PyType_Lookup(metatype, name); |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2375 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2376 | if (meta_attribute != NULL) { |
| 2377 | meta_get = Py_TYPE(meta_attribute)->tp_descr_get; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2378 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2379 | if (meta_get != NULL && PyDescr_IsData(meta_attribute)) { |
| 2380 | /* Data descriptors implement tp_descr_set to intercept |
| 2381 | * writes. Assume the attribute is not overridden in |
| 2382 | * type's tp_dict (and bases): call the descriptor now. |
| 2383 | */ |
| 2384 | return meta_get(meta_attribute, (PyObject *)type, |
| 2385 | (PyObject *)metatype); |
| 2386 | } |
| 2387 | Py_INCREF(meta_attribute); |
| 2388 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2390 | /* No data descriptor found on metatype. Look in tp_dict of this |
| 2391 | * type and its bases */ |
| 2392 | attribute = _PyType_Lookup(type, name); |
| 2393 | if (attribute != NULL) { |
| 2394 | /* Implement descriptor functionality, if any */ |
| 2395 | descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get; |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2397 | Py_XDECREF(meta_attribute); |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2399 | if (local_get != NULL) { |
| 2400 | /* NULL 2nd argument indicates the descriptor was |
| 2401 | * found on the target object itself (or a base) */ |
| 2402 | return local_get(attribute, (PyObject *)NULL, |
| 2403 | (PyObject *)type); |
| 2404 | } |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2406 | Py_INCREF(attribute); |
| 2407 | return attribute; |
| 2408 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2410 | /* No attribute found in local __dict__ (or bases): use the |
| 2411 | * descriptor from the metatype, if any */ |
| 2412 | if (meta_get != NULL) { |
| 2413 | PyObject *res; |
| 2414 | res = meta_get(meta_attribute, (PyObject *)type, |
| 2415 | (PyObject *)metatype); |
| 2416 | Py_DECREF(meta_attribute); |
| 2417 | return res; |
| 2418 | } |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2420 | /* If an ordinary attribute was found on the metatype, return it now */ |
| 2421 | if (meta_attribute != NULL) { |
| 2422 | return meta_attribute; |
| 2423 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2424 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2425 | /* Give up */ |
| 2426 | PyErr_Format(PyExc_AttributeError, |
| 2427 | "type object '%.50s' has no attribute '%U'", |
| 2428 | type->tp_name, name); |
| 2429 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | static int |
| 2433 | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) |
| 2434 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2435 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 2436 | PyErr_Format( |
| 2437 | PyExc_TypeError, |
| 2438 | "can't set attributes of built-in/extension type '%s'", |
| 2439 | type->tp_name); |
| 2440 | return -1; |
| 2441 | } |
| 2442 | if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0) |
| 2443 | return -1; |
| 2444 | return update_slot(type, name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2445 | } |
| 2446 | |
| 2447 | static void |
| 2448 | type_dealloc(PyTypeObject *type) |
| 2449 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2450 | PyHeapTypeObject *et; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2452 | /* Assert this is a heap-allocated type object */ |
| 2453 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
| 2454 | _PyObject_GC_UNTRACK(type); |
| 2455 | PyObject_ClearWeakRefs((PyObject *)type); |
| 2456 | et = (PyHeapTypeObject *)type; |
| 2457 | Py_XDECREF(type->tp_base); |
| 2458 | Py_XDECREF(type->tp_dict); |
| 2459 | Py_XDECREF(type->tp_bases); |
| 2460 | Py_XDECREF(type->tp_mro); |
| 2461 | Py_XDECREF(type->tp_cache); |
| 2462 | Py_XDECREF(type->tp_subclasses); |
| 2463 | /* A type's tp_doc is heap allocated, unlike the tp_doc slots |
| 2464 | * of most other objects. It's okay to cast it to char *. |
| 2465 | */ |
| 2466 | PyObject_Free((char *)type->tp_doc); |
| 2467 | Py_XDECREF(et->ht_name); |
| 2468 | Py_XDECREF(et->ht_slots); |
| 2469 | Py_TYPE(type)->tp_free((PyObject *)type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2470 | } |
| 2471 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2472 | static PyObject * |
| 2473 | type_subclasses(PyTypeObject *type, PyObject *args_ignored) |
| 2474 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2475 | PyObject *list, *raw, *ref; |
| 2476 | Py_ssize_t i, n; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2477 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2478 | list = PyList_New(0); |
| 2479 | if (list == NULL) |
| 2480 | return NULL; |
| 2481 | raw = type->tp_subclasses; |
| 2482 | if (raw == NULL) |
| 2483 | return list; |
| 2484 | assert(PyList_Check(raw)); |
| 2485 | n = PyList_GET_SIZE(raw); |
| 2486 | for (i = 0; i < n; i++) { |
| 2487 | ref = PyList_GET_ITEM(raw, i); |
| 2488 | assert(PyWeakref_CheckRef(ref)); |
| 2489 | ref = PyWeakref_GET_OBJECT(ref); |
| 2490 | if (ref != Py_None) { |
| 2491 | if (PyList_Append(list, ref) < 0) { |
| 2492 | Py_DECREF(list); |
| 2493 | return NULL; |
| 2494 | } |
| 2495 | } |
| 2496 | } |
| 2497 | return list; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2498 | } |
| 2499 | |
Guido van Rossum | 4737482 | 2007-08-02 16:48:17 +0000 | [diff] [blame] | 2500 | static PyObject * |
| 2501 | type_prepare(PyObject *self, PyObject *args, PyObject *kwds) |
| 2502 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2503 | return PyDict_New(); |
Guido van Rossum | 4737482 | 2007-08-02 16:48:17 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2506 | static PyMethodDef type_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2507 | {"mro", (PyCFunction)mro_external, METH_NOARGS, |
| 2508 | PyDoc_STR("mro() -> list\nreturn a type's method resolution order")}, |
| 2509 | {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS, |
| 2510 | PyDoc_STR("__subclasses__() -> list of immediate subclasses")}, |
| 2511 | {"__prepare__", (PyCFunction)type_prepare, |
| 2512 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 2513 | PyDoc_STR("__prepare__() -> dict\n" |
| 2514 | "used to create the namespace for the class statement")}, |
| 2515 | {"__instancecheck__", type___instancecheck__, METH_O, |
| 2516 | PyDoc_STR("__instancecheck__() -> check if an object is an instance")}, |
| 2517 | {"__subclasscheck__", type___subclasscheck__, METH_O, |
Alexander Belopolsky | 977a684 | 2010-08-16 20:17:07 +0000 | [diff] [blame^] | 2518 | PyDoc_STR("__subclasscheck__() -> check if a class is a subclass")}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2519 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2520 | }; |
| 2521 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2522 | PyDoc_STRVAR(type_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2523 | "type(object) -> the object's type\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2524 | "type(name, bases, dict) -> a new type"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2525 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2526 | static int |
| 2527 | type_traverse(PyTypeObject *type, visitproc visit, void *arg) |
| 2528 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2529 | /* Because of type_is_gc(), the collector only calls this |
| 2530 | for heaptypes. */ |
| 2531 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2533 | Py_VISIT(type->tp_dict); |
| 2534 | Py_VISIT(type->tp_cache); |
| 2535 | Py_VISIT(type->tp_mro); |
| 2536 | Py_VISIT(type->tp_bases); |
| 2537 | Py_VISIT(type->tp_base); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2538 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2539 | /* There's no need to visit type->tp_subclasses or |
| 2540 | ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved |
| 2541 | in cycles; tp_subclasses is a list of weak references, |
| 2542 | and slots is a tuple of strings. */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2544 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
| 2547 | static int |
| 2548 | type_clear(PyTypeObject *type) |
| 2549 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2550 | /* Because of type_is_gc(), the collector only calls this |
| 2551 | for heaptypes. */ |
| 2552 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2553 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2554 | /* The only field we need to clear is tp_mro, which is part of a |
| 2555 | hard cycle (its first element is the class itself) that won't |
| 2556 | be broken otherwise (it's a tuple and tuples don't have a |
| 2557 | tp_clear handler). None of the other fields need to be |
| 2558 | cleared, and here's why: |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2559 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2560 | tp_dict: |
| 2561 | 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] | 2562 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2563 | tp_cache: |
| 2564 | Not used; if it were, it would be a dict. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2566 | tp_bases, tp_base: |
| 2567 | If these are involved in a cycle, there must be at least |
| 2568 | one other, mutable object in the cycle, e.g. a base |
| 2569 | class's dict; the cycle will be broken that way. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2570 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2571 | tp_subclasses: |
| 2572 | A list of weak references can't be part of a cycle; and |
| 2573 | lists have their own tp_clear. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2574 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2575 | slots (in PyHeapTypeObject): |
| 2576 | A tuple of strings can't be part of a cycle. |
| 2577 | */ |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2578 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2579 | Py_CLEAR(type->tp_mro); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2580 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2581 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
| 2584 | static int |
| 2585 | type_is_gc(PyTypeObject *type) |
| 2586 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2587 | return type->tp_flags & Py_TPFLAGS_HEAPTYPE; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2590 | PyTypeObject PyType_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2591 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2592 | "type", /* tp_name */ |
| 2593 | sizeof(PyHeapTypeObject), /* tp_basicsize */ |
| 2594 | sizeof(PyMemberDef), /* tp_itemsize */ |
| 2595 | (destructor)type_dealloc, /* tp_dealloc */ |
| 2596 | 0, /* tp_print */ |
| 2597 | 0, /* tp_getattr */ |
| 2598 | 0, /* tp_setattr */ |
| 2599 | 0, /* tp_reserved */ |
| 2600 | (reprfunc)type_repr, /* tp_repr */ |
| 2601 | 0, /* tp_as_number */ |
| 2602 | 0, /* tp_as_sequence */ |
| 2603 | 0, /* tp_as_mapping */ |
| 2604 | 0, /* tp_hash */ |
| 2605 | (ternaryfunc)type_call, /* tp_call */ |
| 2606 | 0, /* tp_str */ |
| 2607 | (getattrofunc)type_getattro, /* tp_getattro */ |
| 2608 | (setattrofunc)type_setattro, /* tp_setattro */ |
| 2609 | 0, /* tp_as_buffer */ |
| 2610 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2611 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */ |
| 2612 | type_doc, /* tp_doc */ |
| 2613 | (traverseproc)type_traverse, /* tp_traverse */ |
| 2614 | (inquiry)type_clear, /* tp_clear */ |
| 2615 | 0, /* tp_richcompare */ |
| 2616 | offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ |
| 2617 | 0, /* tp_iter */ |
| 2618 | 0, /* tp_iternext */ |
| 2619 | type_methods, /* tp_methods */ |
| 2620 | type_members, /* tp_members */ |
| 2621 | type_getsets, /* tp_getset */ |
| 2622 | 0, /* tp_base */ |
| 2623 | 0, /* tp_dict */ |
| 2624 | 0, /* tp_descr_get */ |
| 2625 | 0, /* tp_descr_set */ |
| 2626 | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ |
| 2627 | type_init, /* tp_init */ |
| 2628 | 0, /* tp_alloc */ |
| 2629 | type_new, /* tp_new */ |
| 2630 | PyObject_GC_Del, /* tp_free */ |
| 2631 | (inquiry)type_is_gc, /* tp_is_gc */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2632 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2633 | |
| 2634 | |
| 2635 | /* The base type of all types (eventually)... except itself. */ |
| 2636 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2637 | /* You may wonder why object.__new__() only complains about arguments |
| 2638 | when object.__init__() is not overridden, and vice versa. |
| 2639 | |
| 2640 | Consider the use cases: |
| 2641 | |
| 2642 | 1. When neither is overridden, we want to hear complaints about |
| 2643 | excess (i.e., any) arguments, since their presence could |
| 2644 | indicate there's a bug. |
| 2645 | |
| 2646 | 2. When defining an Immutable type, we are likely to override only |
| 2647 | __new__(), since __init__() is called too late to initialize an |
| 2648 | Immutable object. Since __new__() defines the signature for the |
| 2649 | type, it would be a pain to have to override __init__() just to |
| 2650 | stop it from complaining about excess arguments. |
| 2651 | |
| 2652 | 3. When defining a Mutable type, we are likely to override only |
| 2653 | __init__(). So here the converse reasoning applies: we don't |
| 2654 | want to have to override __new__() just to stop it from |
| 2655 | complaining. |
| 2656 | |
| 2657 | 4. When __init__() is overridden, and the subclass __init__() calls |
| 2658 | object.__init__(), the latter should complain about excess |
| 2659 | arguments; ditto for __new__(). |
| 2660 | |
| 2661 | Use cases 2 and 3 make it unattractive to unconditionally check for |
| 2662 | excess arguments. The best solution that addresses all four use |
| 2663 | cases is as follows: __init__() complains about excess arguments |
| 2664 | unless __new__() is overridden and __init__() is not overridden |
| 2665 | (IOW, if __init__() is overridden or __new__() is not overridden); |
| 2666 | symmetrically, __new__() complains about excess arguments unless |
| 2667 | __init__() is overridden and __new__() is not overridden |
| 2668 | (IOW, if __new__() is overridden or __init__() is not overridden). |
| 2669 | |
| 2670 | However, for backwards compatibility, this breaks too much code. |
| 2671 | Therefore, in 2.6, we'll *warn* about excess arguments when both |
| 2672 | methods are overridden; for all other cases we'll use the above |
| 2673 | rules. |
| 2674 | |
| 2675 | */ |
| 2676 | |
| 2677 | /* Forward */ |
| 2678 | static PyObject * |
| 2679 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 2680 | |
| 2681 | static int |
| 2682 | excess_args(PyObject *args, PyObject *kwds) |
| 2683 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2684 | return PyTuple_GET_SIZE(args) || |
| 2685 | (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2686 | } |
| 2687 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2688 | static int |
| 2689 | object_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2690 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2691 | int err = 0; |
| 2692 | if (excess_args(args, kwds)) { |
| 2693 | PyTypeObject *type = Py_TYPE(self); |
| 2694 | if (type->tp_init != object_init && |
| 2695 | type->tp_new != object_new) |
| 2696 | { |
| 2697 | err = PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2698 | "object.__init__() takes no parameters", |
| 2699 | 1); |
| 2700 | } |
| 2701 | else if (type->tp_init != object_init || |
| 2702 | type->tp_new == object_new) |
| 2703 | { |
| 2704 | PyErr_SetString(PyExc_TypeError, |
| 2705 | "object.__init__() takes no parameters"); |
| 2706 | err = -1; |
| 2707 | } |
| 2708 | } |
| 2709 | return err; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2712 | static PyObject * |
| 2713 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2714 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2715 | int err = 0; |
| 2716 | if (excess_args(args, kwds)) { |
| 2717 | if (type->tp_new != object_new && |
| 2718 | type->tp_init != object_init) |
| 2719 | { |
| 2720 | err = PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2721 | "object.__new__() takes no parameters", |
| 2722 | 1); |
| 2723 | } |
| 2724 | else if (type->tp_new != object_new || |
| 2725 | type->tp_init == object_init) |
| 2726 | { |
| 2727 | PyErr_SetString(PyExc_TypeError, |
| 2728 | "object.__new__() takes no parameters"); |
| 2729 | err = -1; |
| 2730 | } |
| 2731 | } |
| 2732 | if (err < 0) |
| 2733 | return NULL; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 2734 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2735 | if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) { |
| 2736 | static PyObject *comma = NULL; |
| 2737 | PyObject *abstract_methods = NULL; |
| 2738 | PyObject *builtins; |
| 2739 | PyObject *sorted; |
| 2740 | PyObject *sorted_methods = NULL; |
| 2741 | PyObject *joined = NULL; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 2742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2743 | /* Compute ", ".join(sorted(type.__abstractmethods__)) |
| 2744 | into joined. */ |
| 2745 | abstract_methods = type_abstractmethods(type, NULL); |
| 2746 | if (abstract_methods == NULL) |
| 2747 | goto error; |
| 2748 | builtins = PyEval_GetBuiltins(); |
| 2749 | if (builtins == NULL) |
| 2750 | goto error; |
| 2751 | sorted = PyDict_GetItemString(builtins, "sorted"); |
| 2752 | if (sorted == NULL) |
| 2753 | goto error; |
| 2754 | sorted_methods = PyObject_CallFunctionObjArgs(sorted, |
| 2755 | abstract_methods, |
| 2756 | NULL); |
| 2757 | if (sorted_methods == NULL) |
| 2758 | goto error; |
| 2759 | if (comma == NULL) { |
| 2760 | comma = PyUnicode_InternFromString(", "); |
| 2761 | if (comma == NULL) |
| 2762 | goto error; |
| 2763 | } |
| 2764 | joined = PyObject_CallMethod(comma, "join", |
| 2765 | "O", sorted_methods); |
| 2766 | if (joined == NULL) |
| 2767 | goto error; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 2768 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2769 | PyErr_Format(PyExc_TypeError, |
| 2770 | "Can't instantiate abstract class %s " |
| 2771 | "with abstract methods %U", |
| 2772 | type->tp_name, |
| 2773 | joined); |
| 2774 | error: |
| 2775 | Py_XDECREF(joined); |
| 2776 | Py_XDECREF(sorted_methods); |
| 2777 | Py_XDECREF(abstract_methods); |
| 2778 | return NULL; |
| 2779 | } |
| 2780 | return type->tp_alloc(type, 0); |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2783 | static void |
| 2784 | object_dealloc(PyObject *self) |
| 2785 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2786 | Py_TYPE(self)->tp_free(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2789 | static PyObject * |
| 2790 | object_repr(PyObject *self) |
| 2791 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2792 | PyTypeObject *type; |
| 2793 | PyObject *mod, *name, *rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2794 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2795 | type = Py_TYPE(self); |
| 2796 | mod = type_module(type, NULL); |
| 2797 | if (mod == NULL) |
| 2798 | PyErr_Clear(); |
| 2799 | else if (!PyUnicode_Check(mod)) { |
| 2800 | Py_DECREF(mod); |
| 2801 | mod = NULL; |
| 2802 | } |
| 2803 | name = type_name(type, NULL); |
| 2804 | if (name == NULL) |
| 2805 | return NULL; |
| 2806 | if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins")) |
| 2807 | rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self); |
| 2808 | else |
| 2809 | rtn = PyUnicode_FromFormat("<%s object at %p>", |
| 2810 | type->tp_name, self); |
| 2811 | Py_XDECREF(mod); |
| 2812 | Py_DECREF(name); |
| 2813 | return rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2814 | } |
| 2815 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2816 | static PyObject * |
| 2817 | object_str(PyObject *self) |
| 2818 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2819 | unaryfunc f; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2820 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2821 | f = Py_TYPE(self)->tp_repr; |
| 2822 | if (f == NULL) |
| 2823 | f = object_repr; |
| 2824 | return f(self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2827 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2828 | object_richcompare(PyObject *self, PyObject *other, int op) |
| 2829 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2830 | PyObject *res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2831 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2832 | switch (op) { |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2833 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2834 | case Py_EQ: |
| 2835 | /* Return NotImplemented instead of False, so if two |
| 2836 | objects are compared, both get a chance at the |
| 2837 | comparison. See issue #1393. */ |
| 2838 | res = (self == other) ? Py_True : Py_NotImplemented; |
| 2839 | Py_INCREF(res); |
| 2840 | break; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2841 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2842 | case Py_NE: |
| 2843 | /* By default, != returns the opposite of ==, |
| 2844 | unless the latter returns NotImplemented. */ |
| 2845 | res = PyObject_RichCompare(self, other, Py_EQ); |
| 2846 | if (res != NULL && res != Py_NotImplemented) { |
| 2847 | int ok = PyObject_IsTrue(res); |
| 2848 | Py_DECREF(res); |
| 2849 | if (ok < 0) |
| 2850 | res = NULL; |
| 2851 | else { |
| 2852 | if (ok) |
| 2853 | res = Py_False; |
| 2854 | else |
| 2855 | res = Py_True; |
| 2856 | Py_INCREF(res); |
| 2857 | } |
| 2858 | } |
| 2859 | break; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2860 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2861 | default: |
| 2862 | res = Py_NotImplemented; |
| 2863 | Py_INCREF(res); |
| 2864 | break; |
| 2865 | } |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2866 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2867 | return res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
| 2870 | static PyObject * |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2871 | object_get_class(PyObject *self, void *closure) |
| 2872 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2873 | Py_INCREF(Py_TYPE(self)); |
| 2874 | return (PyObject *)(Py_TYPE(self)); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
| 2877 | static int |
| 2878 | equiv_structs(PyTypeObject *a, PyTypeObject *b) |
| 2879 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2880 | return a == b || |
| 2881 | (a != NULL && |
| 2882 | b != NULL && |
| 2883 | a->tp_basicsize == b->tp_basicsize && |
| 2884 | a->tp_itemsize == b->tp_itemsize && |
| 2885 | a->tp_dictoffset == b->tp_dictoffset && |
| 2886 | a->tp_weaklistoffset == b->tp_weaklistoffset && |
| 2887 | ((a->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 2888 | (b->tp_flags & Py_TPFLAGS_HAVE_GC))); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2889 | } |
| 2890 | |
| 2891 | static int |
| 2892 | same_slots_added(PyTypeObject *a, PyTypeObject *b) |
| 2893 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2894 | PyTypeObject *base = a->tp_base; |
| 2895 | Py_ssize_t size; |
| 2896 | PyObject *slots_a, *slots_b; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2897 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2898 | if (base != b->tp_base) |
| 2899 | return 0; |
| 2900 | if (equiv_structs(a, base) && equiv_structs(b, base)) |
| 2901 | return 1; |
| 2902 | size = base->tp_basicsize; |
| 2903 | if (a->tp_dictoffset == size && b->tp_dictoffset == size) |
| 2904 | size += sizeof(PyObject *); |
| 2905 | if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size) |
| 2906 | size += sizeof(PyObject *); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2907 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2908 | /* Check slots compliance */ |
| 2909 | slots_a = ((PyHeapTypeObject *)a)->ht_slots; |
| 2910 | slots_b = ((PyHeapTypeObject *)b)->ht_slots; |
| 2911 | if (slots_a && slots_b) { |
| 2912 | if (PyObject_RichCompareBool(slots_a, slots_b, Py_EQ) != 1) |
| 2913 | return 0; |
| 2914 | size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a); |
| 2915 | } |
| 2916 | return size == a->tp_basicsize && size == b->tp_basicsize; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2920 | compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2921 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2922 | PyTypeObject *newbase, *oldbase; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2923 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2924 | if (newto->tp_dealloc != oldto->tp_dealloc || |
| 2925 | newto->tp_free != oldto->tp_free) |
| 2926 | { |
| 2927 | PyErr_Format(PyExc_TypeError, |
| 2928 | "%s assignment: " |
| 2929 | "'%s' deallocator differs from '%s'", |
| 2930 | attr, |
| 2931 | newto->tp_name, |
| 2932 | oldto->tp_name); |
| 2933 | return 0; |
| 2934 | } |
| 2935 | newbase = newto; |
| 2936 | oldbase = oldto; |
| 2937 | while (equiv_structs(newbase, newbase->tp_base)) |
| 2938 | newbase = newbase->tp_base; |
| 2939 | while (equiv_structs(oldbase, oldbase->tp_base)) |
| 2940 | oldbase = oldbase->tp_base; |
| 2941 | if (newbase != oldbase && |
| 2942 | (newbase->tp_base != oldbase->tp_base || |
| 2943 | !same_slots_added(newbase, oldbase))) { |
| 2944 | PyErr_Format(PyExc_TypeError, |
| 2945 | "%s assignment: " |
| 2946 | "'%s' object layout differs from '%s'", |
| 2947 | attr, |
| 2948 | newto->tp_name, |
| 2949 | oldto->tp_name); |
| 2950 | return 0; |
| 2951 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 2952 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2953 | return 1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2954 | } |
| 2955 | |
| 2956 | static int |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2957 | object_set_class(PyObject *self, PyObject *value, void *closure) |
| 2958 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2959 | PyTypeObject *oldto = Py_TYPE(self); |
| 2960 | PyTypeObject *newto; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2961 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2962 | if (value == NULL) { |
| 2963 | PyErr_SetString(PyExc_TypeError, |
| 2964 | "can't delete __class__ attribute"); |
| 2965 | return -1; |
| 2966 | } |
| 2967 | if (!PyType_Check(value)) { |
| 2968 | PyErr_Format(PyExc_TypeError, |
| 2969 | "__class__ must be set to new-style class, not '%s' object", |
| 2970 | Py_TYPE(value)->tp_name); |
| 2971 | return -1; |
| 2972 | } |
| 2973 | newto = (PyTypeObject *)value; |
| 2974 | if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) || |
| 2975 | !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 2976 | { |
| 2977 | PyErr_Format(PyExc_TypeError, |
| 2978 | "__class__ assignment: only for heap types"); |
| 2979 | return -1; |
| 2980 | } |
| 2981 | if (compatible_for_assignment(newto, oldto, "__class__")) { |
| 2982 | Py_INCREF(newto); |
| 2983 | Py_TYPE(self) = newto; |
| 2984 | Py_DECREF(oldto); |
| 2985 | return 0; |
| 2986 | } |
| 2987 | else { |
| 2988 | return -1; |
| 2989 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2990 | } |
| 2991 | |
| 2992 | static PyGetSetDef object_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2993 | {"__class__", object_get_class, object_set_class, |
| 2994 | PyDoc_STR("the object's class")}, |
| 2995 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2996 | }; |
| 2997 | |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 2998 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 2999 | /* Stuff to implement __reduce_ex__ for pickle protocols >= 2. |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3000 | We fall back to helpers in copyreg for: |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3001 | - pickle protocols < 2 |
| 3002 | - calculating the list of slot names (done only once per class) |
| 3003 | - the __newobj__ function (which is used as a token but never called) |
| 3004 | */ |
| 3005 | |
| 3006 | static PyObject * |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3007 | import_copyreg(void) |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3008 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3009 | static PyObject *copyreg_str; |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3010 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3011 | if (!copyreg_str) { |
| 3012 | copyreg_str = PyUnicode_InternFromString("copyreg"); |
| 3013 | if (copyreg_str == NULL) |
| 3014 | return NULL; |
| 3015 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3016 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3017 | return PyImport_Import(copyreg_str); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3018 | } |
| 3019 | |
| 3020 | static PyObject * |
| 3021 | slotnames(PyObject *cls) |
| 3022 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3023 | PyObject *clsdict; |
| 3024 | PyObject *copyreg; |
| 3025 | PyObject *slotnames; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3026 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3027 | if (!PyType_Check(cls)) { |
| 3028 | Py_INCREF(Py_None); |
| 3029 | return Py_None; |
| 3030 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3032 | clsdict = ((PyTypeObject *)cls)->tp_dict; |
| 3033 | slotnames = PyDict_GetItemString(clsdict, "__slotnames__"); |
| 3034 | if (slotnames != NULL && PyList_Check(slotnames)) { |
| 3035 | Py_INCREF(slotnames); |
| 3036 | return slotnames; |
| 3037 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3038 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3039 | copyreg = import_copyreg(); |
| 3040 | if (copyreg == NULL) |
| 3041 | return NULL; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3042 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3043 | slotnames = PyObject_CallMethod(copyreg, "_slotnames", "O", cls); |
| 3044 | Py_DECREF(copyreg); |
| 3045 | if (slotnames != NULL && |
| 3046 | slotnames != Py_None && |
| 3047 | !PyList_Check(slotnames)) |
| 3048 | { |
| 3049 | PyErr_SetString(PyExc_TypeError, |
| 3050 | "copyreg._slotnames didn't return a list or None"); |
| 3051 | Py_DECREF(slotnames); |
| 3052 | slotnames = NULL; |
| 3053 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3054 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3055 | return slotnames; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3056 | } |
| 3057 | |
| 3058 | static PyObject * |
| 3059 | reduce_2(PyObject *obj) |
| 3060 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3061 | PyObject *cls, *getnewargs; |
| 3062 | PyObject *args = NULL, *args2 = NULL; |
| 3063 | PyObject *getstate = NULL, *state = NULL, *names = NULL; |
| 3064 | PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL; |
| 3065 | PyObject *copyreg = NULL, *newobj = NULL, *res = NULL; |
| 3066 | Py_ssize_t i, n; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3067 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3068 | cls = PyObject_GetAttrString(obj, "__class__"); |
| 3069 | if (cls == NULL) |
| 3070 | return NULL; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3071 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3072 | getnewargs = PyObject_GetAttrString(obj, "__getnewargs__"); |
| 3073 | if (getnewargs != NULL) { |
| 3074 | args = PyObject_CallObject(getnewargs, NULL); |
| 3075 | Py_DECREF(getnewargs); |
| 3076 | if (args != NULL && !PyTuple_Check(args)) { |
| 3077 | PyErr_Format(PyExc_TypeError, |
| 3078 | "__getnewargs__ should return a tuple, " |
| 3079 | "not '%.200s'", Py_TYPE(args)->tp_name); |
| 3080 | goto end; |
| 3081 | } |
| 3082 | } |
| 3083 | else { |
| 3084 | PyErr_Clear(); |
| 3085 | args = PyTuple_New(0); |
| 3086 | } |
| 3087 | if (args == NULL) |
| 3088 | goto end; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3089 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3090 | getstate = PyObject_GetAttrString(obj, "__getstate__"); |
| 3091 | if (getstate != NULL) { |
| 3092 | state = PyObject_CallObject(getstate, NULL); |
| 3093 | Py_DECREF(getstate); |
| 3094 | if (state == NULL) |
| 3095 | goto end; |
| 3096 | } |
| 3097 | else { |
| 3098 | PyErr_Clear(); |
| 3099 | state = PyObject_GetAttrString(obj, "__dict__"); |
| 3100 | if (state == NULL) { |
| 3101 | PyErr_Clear(); |
| 3102 | state = Py_None; |
| 3103 | Py_INCREF(state); |
| 3104 | } |
| 3105 | names = slotnames(cls); |
| 3106 | if (names == NULL) |
| 3107 | goto end; |
| 3108 | if (names != Py_None) { |
| 3109 | assert(PyList_Check(names)); |
| 3110 | slots = PyDict_New(); |
| 3111 | if (slots == NULL) |
| 3112 | goto end; |
| 3113 | n = 0; |
| 3114 | /* Can't pre-compute the list size; the list |
| 3115 | is stored on the class so accessible to other |
| 3116 | threads, which may be run by DECREF */ |
| 3117 | for (i = 0; i < PyList_GET_SIZE(names); i++) { |
| 3118 | PyObject *name, *value; |
| 3119 | name = PyList_GET_ITEM(names, i); |
| 3120 | value = PyObject_GetAttr(obj, name); |
| 3121 | if (value == NULL) |
| 3122 | PyErr_Clear(); |
| 3123 | else { |
| 3124 | int err = PyDict_SetItem(slots, name, |
| 3125 | value); |
| 3126 | Py_DECREF(value); |
| 3127 | if (err) |
| 3128 | goto end; |
| 3129 | n++; |
| 3130 | } |
| 3131 | } |
| 3132 | if (n) { |
| 3133 | state = Py_BuildValue("(NO)", state, slots); |
| 3134 | if (state == NULL) |
| 3135 | goto end; |
| 3136 | } |
| 3137 | } |
| 3138 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3140 | if (!PyList_Check(obj)) { |
| 3141 | listitems = Py_None; |
| 3142 | Py_INCREF(listitems); |
| 3143 | } |
| 3144 | else { |
| 3145 | listitems = PyObject_GetIter(obj); |
| 3146 | if (listitems == NULL) |
| 3147 | goto end; |
| 3148 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3149 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3150 | if (!PyDict_Check(obj)) { |
| 3151 | dictitems = Py_None; |
| 3152 | Py_INCREF(dictitems); |
| 3153 | } |
| 3154 | else { |
| 3155 | PyObject *items = PyObject_CallMethod(obj, "items", ""); |
| 3156 | if (items == NULL) |
| 3157 | goto end; |
| 3158 | dictitems = PyObject_GetIter(items); |
| 3159 | Py_DECREF(items); |
| 3160 | if (dictitems == NULL) |
| 3161 | goto end; |
| 3162 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3164 | copyreg = import_copyreg(); |
| 3165 | if (copyreg == NULL) |
| 3166 | goto end; |
| 3167 | newobj = PyObject_GetAttrString(copyreg, "__newobj__"); |
| 3168 | if (newobj == NULL) |
| 3169 | goto end; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3171 | n = PyTuple_GET_SIZE(args); |
| 3172 | args2 = PyTuple_New(n+1); |
| 3173 | if (args2 == NULL) |
| 3174 | goto end; |
| 3175 | PyTuple_SET_ITEM(args2, 0, cls); |
| 3176 | cls = NULL; |
| 3177 | for (i = 0; i < n; i++) { |
| 3178 | PyObject *v = PyTuple_GET_ITEM(args, i); |
| 3179 | Py_INCREF(v); |
| 3180 | PyTuple_SET_ITEM(args2, i+1, v); |
| 3181 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3183 | res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3184 | |
| 3185 | end: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3186 | Py_XDECREF(cls); |
| 3187 | Py_XDECREF(args); |
| 3188 | Py_XDECREF(args2); |
| 3189 | Py_XDECREF(slots); |
| 3190 | Py_XDECREF(state); |
| 3191 | Py_XDECREF(names); |
| 3192 | Py_XDECREF(listitems); |
| 3193 | Py_XDECREF(dictitems); |
| 3194 | Py_XDECREF(copyreg); |
| 3195 | Py_XDECREF(newobj); |
| 3196 | return res; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3197 | } |
| 3198 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3199 | /* |
| 3200 | * There were two problems when object.__reduce__ and object.__reduce_ex__ |
| 3201 | * were implemented in the same function: |
| 3202 | * - trying to pickle an object with a custom __reduce__ method that |
| 3203 | * fell back to object.__reduce__ in certain circumstances led to |
| 3204 | * infinite recursion at Python level and eventual RuntimeError. |
| 3205 | * - Pickling objects that lied about their type by overwriting the |
| 3206 | * __class__ descriptor could lead to infinite recursion at C level |
| 3207 | * and eventual segfault. |
| 3208 | * |
| 3209 | * Because of backwards compatibility, the two methods still have to |
| 3210 | * behave in the same way, even if this is not required by the pickle |
| 3211 | * protocol. This common functionality was moved to the _common_reduce |
| 3212 | * function. |
| 3213 | */ |
| 3214 | static PyObject * |
| 3215 | _common_reduce(PyObject *self, int proto) |
| 3216 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3217 | PyObject *copyreg, *res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3219 | if (proto >= 2) |
| 3220 | return reduce_2(self); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3222 | copyreg = import_copyreg(); |
| 3223 | if (!copyreg) |
| 3224 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3226 | res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto); |
| 3227 | Py_DECREF(copyreg); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3229 | return res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
| 3232 | static PyObject * |
| 3233 | object_reduce(PyObject *self, PyObject *args) |
| 3234 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3235 | int proto = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3237 | if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto)) |
| 3238 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3240 | return _common_reduce(self, proto); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3241 | } |
| 3242 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3243 | static PyObject * |
| 3244 | object_reduce_ex(PyObject *self, PyObject *args) |
| 3245 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3246 | PyObject *reduce, *res; |
| 3247 | int proto = 0; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3249 | if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto)) |
| 3250 | return NULL; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3252 | reduce = PyObject_GetAttrString(self, "__reduce__"); |
| 3253 | if (reduce == NULL) |
| 3254 | PyErr_Clear(); |
| 3255 | else { |
| 3256 | PyObject *cls, *clsreduce, *objreduce; |
| 3257 | int override; |
| 3258 | cls = PyObject_GetAttrString(self, "__class__"); |
| 3259 | if (cls == NULL) { |
| 3260 | Py_DECREF(reduce); |
| 3261 | return NULL; |
| 3262 | } |
| 3263 | clsreduce = PyObject_GetAttrString(cls, "__reduce__"); |
| 3264 | Py_DECREF(cls); |
| 3265 | if (clsreduce == NULL) { |
| 3266 | Py_DECREF(reduce); |
| 3267 | return NULL; |
| 3268 | } |
| 3269 | objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict, |
| 3270 | "__reduce__"); |
| 3271 | override = (clsreduce != objreduce); |
| 3272 | Py_DECREF(clsreduce); |
| 3273 | if (override) { |
| 3274 | res = PyObject_CallObject(reduce, NULL); |
| 3275 | Py_DECREF(reduce); |
| 3276 | return res; |
| 3277 | } |
| 3278 | else |
| 3279 | Py_DECREF(reduce); |
| 3280 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3281 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3282 | return _common_reduce(self, proto); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3283 | } |
| 3284 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3285 | static PyObject * |
| 3286 | object_subclasshook(PyObject *cls, PyObject *args) |
| 3287 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3288 | Py_INCREF(Py_NotImplemented); |
| 3289 | return Py_NotImplemented; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | PyDoc_STRVAR(object_subclasshook_doc, |
| 3293 | "Abstract classes can override this to customize issubclass().\n" |
| 3294 | "\n" |
| 3295 | "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n" |
| 3296 | "It should return True, False or NotImplemented. If it returns\n" |
| 3297 | "NotImplemented, the normal algorithm is used. Otherwise, it\n" |
| 3298 | "overrides the normal algorithm (and the outcome is cached).\n"); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3299 | |
| 3300 | /* |
| 3301 | from PEP 3101, this code implements: |
| 3302 | |
| 3303 | class object: |
| 3304 | def __format__(self, format_spec): |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3305 | return format(str(self), format_spec) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3306 | */ |
| 3307 | static PyObject * |
| 3308 | object_format(PyObject *self, PyObject *args) |
| 3309 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3310 | PyObject *format_spec; |
| 3311 | PyObject *self_as_str = NULL; |
| 3312 | PyObject *result = NULL; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3314 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 3315 | return NULL; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3316 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3317 | self_as_str = PyObject_Str(self); |
Benjamin Peterson | c03d757 | 2010-06-05 01:03:24 +0000 | [diff] [blame] | 3318 | if (self_as_str != NULL) |
| 3319 | result = PyObject_Format(self_as_str, format_spec); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3321 | Py_XDECREF(self_as_str); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3323 | return result; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3324 | } |
| 3325 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3326 | static PyObject * |
| 3327 | object_sizeof(PyObject *self, PyObject *args) |
| 3328 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3329 | Py_ssize_t res, isize; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3330 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3331 | res = 0; |
| 3332 | isize = self->ob_type->tp_itemsize; |
| 3333 | if (isize > 0) |
| 3334 | res = Py_SIZE(self->ob_type) * isize; |
| 3335 | res += self->ob_type->tp_basicsize; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3336 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3337 | return PyLong_FromSsize_t(res); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3340 | static PyMethodDef object_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3341 | {"__reduce_ex__", object_reduce_ex, METH_VARARGS, |
| 3342 | PyDoc_STR("helper for pickle")}, |
| 3343 | {"__reduce__", object_reduce, METH_VARARGS, |
| 3344 | PyDoc_STR("helper for pickle")}, |
| 3345 | {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS, |
| 3346 | object_subclasshook_doc}, |
| 3347 | {"__format__", object_format, METH_VARARGS, |
| 3348 | PyDoc_STR("default object formatter")}, |
| 3349 | {"__sizeof__", object_sizeof, METH_NOARGS, |
| 3350 | PyDoc_STR("__sizeof__() -> size of object in memory, in bytes")}, |
| 3351 | {0} |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3352 | }; |
| 3353 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3354 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3355 | PyTypeObject PyBaseObject_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3356 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3357 | "object", /* tp_name */ |
| 3358 | sizeof(PyObject), /* tp_basicsize */ |
| 3359 | 0, /* tp_itemsize */ |
| 3360 | object_dealloc, /* tp_dealloc */ |
| 3361 | 0, /* tp_print */ |
| 3362 | 0, /* tp_getattr */ |
| 3363 | 0, /* tp_setattr */ |
| 3364 | 0, /* tp_reserved */ |
| 3365 | object_repr, /* tp_repr */ |
| 3366 | 0, /* tp_as_number */ |
| 3367 | 0, /* tp_as_sequence */ |
| 3368 | 0, /* tp_as_mapping */ |
| 3369 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
| 3370 | 0, /* tp_call */ |
| 3371 | object_str, /* tp_str */ |
| 3372 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3373 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 3374 | 0, /* tp_as_buffer */ |
| 3375 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3376 | PyDoc_STR("The most base type"), /* tp_doc */ |
| 3377 | 0, /* tp_traverse */ |
| 3378 | 0, /* tp_clear */ |
| 3379 | object_richcompare, /* tp_richcompare */ |
| 3380 | 0, /* tp_weaklistoffset */ |
| 3381 | 0, /* tp_iter */ |
| 3382 | 0, /* tp_iternext */ |
| 3383 | object_methods, /* tp_methods */ |
| 3384 | 0, /* tp_members */ |
| 3385 | object_getsets, /* tp_getset */ |
| 3386 | 0, /* tp_base */ |
| 3387 | 0, /* tp_dict */ |
| 3388 | 0, /* tp_descr_get */ |
| 3389 | 0, /* tp_descr_set */ |
| 3390 | 0, /* tp_dictoffset */ |
| 3391 | object_init, /* tp_init */ |
| 3392 | PyType_GenericAlloc, /* tp_alloc */ |
| 3393 | object_new, /* tp_new */ |
| 3394 | PyObject_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3395 | }; |
| 3396 | |
| 3397 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 3398 | /* 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] | 3399 | |
| 3400 | static int |
| 3401 | add_methods(PyTypeObject *type, PyMethodDef *meth) |
| 3402 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3403 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3405 | for (; meth->ml_name != NULL; meth++) { |
| 3406 | PyObject *descr; |
| 3407 | if (PyDict_GetItemString(dict, meth->ml_name) && |
| 3408 | !(meth->ml_flags & METH_COEXIST)) |
| 3409 | continue; |
| 3410 | if (meth->ml_flags & METH_CLASS) { |
| 3411 | if (meth->ml_flags & METH_STATIC) { |
| 3412 | PyErr_SetString(PyExc_ValueError, |
| 3413 | "method cannot be both class and static"); |
| 3414 | return -1; |
| 3415 | } |
| 3416 | descr = PyDescr_NewClassMethod(type, meth); |
| 3417 | } |
| 3418 | else if (meth->ml_flags & METH_STATIC) { |
| 3419 | PyObject *cfunc = PyCFunction_New(meth, NULL); |
| 3420 | if (cfunc == NULL) |
| 3421 | return -1; |
| 3422 | descr = PyStaticMethod_New(cfunc); |
| 3423 | Py_DECREF(cfunc); |
| 3424 | } |
| 3425 | else { |
| 3426 | descr = PyDescr_NewMethod(type, meth); |
| 3427 | } |
| 3428 | if (descr == NULL) |
| 3429 | return -1; |
| 3430 | if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0) |
| 3431 | return -1; |
| 3432 | Py_DECREF(descr); |
| 3433 | } |
| 3434 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3435 | } |
| 3436 | |
| 3437 | static int |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 3438 | add_members(PyTypeObject *type, PyMemberDef *memb) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3439 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3440 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3441 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3442 | for (; memb->name != NULL; memb++) { |
| 3443 | PyObject *descr; |
| 3444 | if (PyDict_GetItemString(dict, memb->name)) |
| 3445 | continue; |
| 3446 | descr = PyDescr_NewMember(type, memb); |
| 3447 | if (descr == NULL) |
| 3448 | return -1; |
| 3449 | if (PyDict_SetItemString(dict, memb->name, descr) < 0) |
| 3450 | return -1; |
| 3451 | Py_DECREF(descr); |
| 3452 | } |
| 3453 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3454 | } |
| 3455 | |
| 3456 | static int |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 3457 | add_getset(PyTypeObject *type, PyGetSetDef *gsp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3458 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3459 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3460 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3461 | for (; gsp->name != NULL; gsp++) { |
| 3462 | PyObject *descr; |
| 3463 | if (PyDict_GetItemString(dict, gsp->name)) |
| 3464 | continue; |
| 3465 | descr = PyDescr_NewGetSet(type, gsp); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3466 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3467 | if (descr == NULL) |
| 3468 | return -1; |
| 3469 | if (PyDict_SetItemString(dict, gsp->name, descr) < 0) |
| 3470 | return -1; |
| 3471 | Py_DECREF(descr); |
| 3472 | } |
| 3473 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3474 | } |
| 3475 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3476 | static void |
| 3477 | inherit_special(PyTypeObject *type, PyTypeObject *base) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3478 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3479 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3480 | /* Copying basicsize is connected to the GC flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3481 | if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3482 | (base->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3483 | (!type->tp_traverse && !type->tp_clear)) { |
| 3484 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
| 3485 | if (type->tp_traverse == NULL) |
| 3486 | type->tp_traverse = base->tp_traverse; |
| 3487 | if (type->tp_clear == NULL) |
| 3488 | type->tp_clear = base->tp_clear; |
| 3489 | } |
| 3490 | { |
| 3491 | /* The condition below could use some explanation. |
| 3492 | It appears that tp_new is not inherited for static types |
| 3493 | whose base class is 'object'; this seems to be a precaution |
| 3494 | so that old extension types don't suddenly become |
| 3495 | callable (object.__new__ wouldn't insure the invariants |
| 3496 | that the extension type's own factory function ensures). |
| 3497 | Heap types, of course, are under our control, so they do |
| 3498 | inherit tp_new; static extension types that specify some |
| 3499 | other built-in type as the default are considered |
| 3500 | new-style-aware so they also inherit object.__new__. */ |
| 3501 | if (base != &PyBaseObject_Type || |
| 3502 | (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 3503 | if (type->tp_new == NULL) |
| 3504 | type->tp_new = base->tp_new; |
| 3505 | } |
| 3506 | } |
Benjamin Peterson | a7465e2 | 2010-07-05 15:01:22 +0000 | [diff] [blame] | 3507 | if (type->tp_basicsize == 0) |
| 3508 | type->tp_basicsize = base->tp_basicsize; |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3510 | /* Copy other non-function slots */ |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3511 | |
| 3512 | #undef COPYVAL |
| 3513 | #define COPYVAL(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3514 | if (type->SLOT == 0) type->SLOT = base->SLOT |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3516 | COPYVAL(tp_itemsize); |
| 3517 | COPYVAL(tp_weaklistoffset); |
| 3518 | COPYVAL(tp_dictoffset); |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 3519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3520 | /* Setup fast subclass flags */ |
| 3521 | if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException)) |
| 3522 | type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS; |
| 3523 | else if (PyType_IsSubtype(base, &PyType_Type)) |
| 3524 | type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS; |
| 3525 | else if (PyType_IsSubtype(base, &PyLong_Type)) |
| 3526 | type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS; |
| 3527 | else if (PyType_IsSubtype(base, &PyBytes_Type)) |
| 3528 | type->tp_flags |= Py_TPFLAGS_BYTES_SUBCLASS; |
| 3529 | else if (PyType_IsSubtype(base, &PyUnicode_Type)) |
| 3530 | type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS; |
| 3531 | else if (PyType_IsSubtype(base, &PyTuple_Type)) |
| 3532 | type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS; |
| 3533 | else if (PyType_IsSubtype(base, &PyList_Type)) |
| 3534 | type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS; |
| 3535 | else if (PyType_IsSubtype(base, &PyDict_Type)) |
| 3536 | type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3537 | } |
| 3538 | |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 3539 | static char *hash_name_op[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3540 | "__eq__", |
| 3541 | "__hash__", |
| 3542 | NULL |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3543 | }; |
| 3544 | |
| 3545 | static int |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 3546 | overrides_hash(PyTypeObject *type) |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3547 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3548 | char **p; |
| 3549 | PyObject *dict = type->tp_dict; |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3551 | assert(dict != NULL); |
| 3552 | for (p = hash_name_op; *p; p++) { |
| 3553 | if (PyDict_GetItemString(dict, *p) != NULL) |
| 3554 | return 1; |
| 3555 | } |
| 3556 | return 0; |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3557 | } |
| 3558 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3559 | static void |
| 3560 | inherit_slots(PyTypeObject *type, PyTypeObject *base) |
| 3561 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3562 | PyTypeObject *basebase; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3563 | |
| 3564 | #undef SLOTDEFINED |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3565 | #undef COPYSLOT |
| 3566 | #undef COPYNUM |
| 3567 | #undef COPYSEQ |
| 3568 | #undef COPYMAP |
Guido van Rossum | 5af588b | 2001-10-12 14:13:21 +0000 | [diff] [blame] | 3569 | #undef COPYBUF |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3570 | |
| 3571 | #define SLOTDEFINED(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3572 | (base->SLOT != 0 && \ |
| 3573 | (basebase == NULL || base->SLOT != basebase->SLOT)) |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3574 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3575 | #define COPYSLOT(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3576 | if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3577 | |
| 3578 | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT) |
| 3579 | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT) |
| 3580 | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT) |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3581 | #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3582 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3583 | /* This won't inherit indirect slots (from tp_as_number etc.) |
| 3584 | if type doesn't provide the space. */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3586 | if (type->tp_as_number != NULL && base->tp_as_number != NULL) { |
| 3587 | basebase = base->tp_base; |
| 3588 | if (basebase->tp_as_number == NULL) |
| 3589 | basebase = NULL; |
| 3590 | COPYNUM(nb_add); |
| 3591 | COPYNUM(nb_subtract); |
| 3592 | COPYNUM(nb_multiply); |
| 3593 | COPYNUM(nb_remainder); |
| 3594 | COPYNUM(nb_divmod); |
| 3595 | COPYNUM(nb_power); |
| 3596 | COPYNUM(nb_negative); |
| 3597 | COPYNUM(nb_positive); |
| 3598 | COPYNUM(nb_absolute); |
| 3599 | COPYNUM(nb_bool); |
| 3600 | COPYNUM(nb_invert); |
| 3601 | COPYNUM(nb_lshift); |
| 3602 | COPYNUM(nb_rshift); |
| 3603 | COPYNUM(nb_and); |
| 3604 | COPYNUM(nb_xor); |
| 3605 | COPYNUM(nb_or); |
| 3606 | COPYNUM(nb_int); |
| 3607 | COPYNUM(nb_float); |
| 3608 | COPYNUM(nb_inplace_add); |
| 3609 | COPYNUM(nb_inplace_subtract); |
| 3610 | COPYNUM(nb_inplace_multiply); |
| 3611 | COPYNUM(nb_inplace_remainder); |
| 3612 | COPYNUM(nb_inplace_power); |
| 3613 | COPYNUM(nb_inplace_lshift); |
| 3614 | COPYNUM(nb_inplace_rshift); |
| 3615 | COPYNUM(nb_inplace_and); |
| 3616 | COPYNUM(nb_inplace_xor); |
| 3617 | COPYNUM(nb_inplace_or); |
| 3618 | COPYNUM(nb_true_divide); |
| 3619 | COPYNUM(nb_floor_divide); |
| 3620 | COPYNUM(nb_inplace_true_divide); |
| 3621 | COPYNUM(nb_inplace_floor_divide); |
| 3622 | COPYNUM(nb_index); |
| 3623 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3625 | if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) { |
| 3626 | basebase = base->tp_base; |
| 3627 | if (basebase->tp_as_sequence == NULL) |
| 3628 | basebase = NULL; |
| 3629 | COPYSEQ(sq_length); |
| 3630 | COPYSEQ(sq_concat); |
| 3631 | COPYSEQ(sq_repeat); |
| 3632 | COPYSEQ(sq_item); |
| 3633 | COPYSEQ(sq_ass_item); |
| 3634 | COPYSEQ(sq_contains); |
| 3635 | COPYSEQ(sq_inplace_concat); |
| 3636 | COPYSEQ(sq_inplace_repeat); |
| 3637 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3638 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3639 | if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) { |
| 3640 | basebase = base->tp_base; |
| 3641 | if (basebase->tp_as_mapping == NULL) |
| 3642 | basebase = NULL; |
| 3643 | COPYMAP(mp_length); |
| 3644 | COPYMAP(mp_subscript); |
| 3645 | COPYMAP(mp_ass_subscript); |
| 3646 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3647 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3648 | if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) { |
| 3649 | basebase = base->tp_base; |
| 3650 | if (basebase->tp_as_buffer == NULL) |
| 3651 | basebase = NULL; |
| 3652 | COPYBUF(bf_getbuffer); |
| 3653 | COPYBUF(bf_releasebuffer); |
| 3654 | } |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3656 | basebase = base->tp_base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3658 | COPYSLOT(tp_dealloc); |
| 3659 | if (type->tp_getattr == NULL && type->tp_getattro == NULL) { |
| 3660 | type->tp_getattr = base->tp_getattr; |
| 3661 | type->tp_getattro = base->tp_getattro; |
| 3662 | } |
| 3663 | if (type->tp_setattr == NULL && type->tp_setattro == NULL) { |
| 3664 | type->tp_setattr = base->tp_setattr; |
| 3665 | type->tp_setattro = base->tp_setattro; |
| 3666 | } |
| 3667 | /* tp_reserved is ignored */ |
| 3668 | COPYSLOT(tp_repr); |
| 3669 | /* tp_hash see tp_richcompare */ |
| 3670 | COPYSLOT(tp_call); |
| 3671 | COPYSLOT(tp_str); |
| 3672 | { |
| 3673 | /* Copy comparison-related slots only when |
| 3674 | not overriding them anywhere */ |
| 3675 | if (type->tp_richcompare == NULL && |
| 3676 | type->tp_hash == NULL && |
| 3677 | !overrides_hash(type)) |
| 3678 | { |
| 3679 | type->tp_richcompare = base->tp_richcompare; |
| 3680 | type->tp_hash = base->tp_hash; |
| 3681 | } |
| 3682 | } |
| 3683 | { |
| 3684 | COPYSLOT(tp_iter); |
| 3685 | COPYSLOT(tp_iternext); |
| 3686 | } |
| 3687 | { |
| 3688 | COPYSLOT(tp_descr_get); |
| 3689 | COPYSLOT(tp_descr_set); |
| 3690 | COPYSLOT(tp_dictoffset); |
| 3691 | COPYSLOT(tp_init); |
| 3692 | COPYSLOT(tp_alloc); |
| 3693 | COPYSLOT(tp_is_gc); |
| 3694 | if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 3695 | (base->tp_flags & Py_TPFLAGS_HAVE_GC)) { |
| 3696 | /* They agree about gc. */ |
| 3697 | COPYSLOT(tp_free); |
| 3698 | } |
| 3699 | else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3700 | type->tp_free == NULL && |
| 3701 | base->tp_free == PyObject_Free) { |
| 3702 | /* A bit of magic to plug in the correct default |
| 3703 | * tp_free function when a derived class adds gc, |
| 3704 | * didn't define tp_free, and the base uses the |
| 3705 | * default non-gc tp_free. |
| 3706 | */ |
| 3707 | type->tp_free = PyObject_GC_Del; |
| 3708 | } |
| 3709 | /* else they didn't agree about gc, and there isn't something |
| 3710 | * obvious to be done -- the type is on its own. |
| 3711 | */ |
| 3712 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3713 | } |
| 3714 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 3715 | static int add_operators(PyTypeObject *); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3716 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3717 | int |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 3718 | PyType_Ready(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3719 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3720 | PyObject *dict, *bases; |
| 3721 | PyTypeObject *base; |
| 3722 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3723 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3724 | if (type->tp_flags & Py_TPFLAGS_READY) { |
| 3725 | assert(type->tp_dict != NULL); |
| 3726 | return 0; |
| 3727 | } |
| 3728 | assert((type->tp_flags & Py_TPFLAGS_READYING) == 0); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3729 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3730 | type->tp_flags |= Py_TPFLAGS_READYING; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3731 | |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 3732 | #ifdef Py_TRACE_REFS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3733 | /* PyType_Ready is the closest thing we have to a choke point |
| 3734 | * for type objects, so is the best place I can think of to try |
| 3735 | * to get type objects into the doubly-linked list of all objects. |
| 3736 | * Still, not all type objects go thru PyType_Ready. |
| 3737 | */ |
| 3738 | _Py_AddToAllObjects((PyObject *)type, 0); |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 3739 | #endif |
| 3740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3741 | /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
| 3742 | base = type->tp_base; |
| 3743 | if (base == NULL && type != &PyBaseObject_Type) { |
| 3744 | base = type->tp_base = &PyBaseObject_Type; |
| 3745 | Py_INCREF(base); |
| 3746 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3747 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3748 | /* Now the only way base can still be NULL is if type is |
| 3749 | * &PyBaseObject_Type. |
| 3750 | */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 3751 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3752 | /* Initialize the base class */ |
| 3753 | if (base != NULL && base->tp_dict == NULL) { |
| 3754 | if (PyType_Ready(base) < 0) |
| 3755 | goto error; |
| 3756 | } |
Guido van Rossum | 323a9cf | 2002-08-14 17:26:30 +0000 | [diff] [blame] | 3757 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3758 | /* Initialize ob_type if NULL. This means extensions that want to be |
| 3759 | compilable separately on Windows can call PyType_Ready() instead of |
| 3760 | initializing the ob_type field of their type objects. */ |
| 3761 | /* The test for base != NULL is really unnecessary, since base is only |
| 3762 | NULL when type is &PyBaseObject_Type, and we know its ob_type is |
| 3763 | not NULL (it's initialized to &PyType_Type). But coverity doesn't |
| 3764 | know that. */ |
| 3765 | if (Py_TYPE(type) == NULL && base != NULL) |
| 3766 | Py_TYPE(type) = Py_TYPE(base); |
Guido van Rossum | 0986d82 | 2002-04-08 01:38:42 +0000 | [diff] [blame] | 3767 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3768 | /* Initialize tp_bases */ |
| 3769 | bases = type->tp_bases; |
| 3770 | if (bases == NULL) { |
| 3771 | if (base == NULL) |
| 3772 | bases = PyTuple_New(0); |
| 3773 | else |
| 3774 | bases = PyTuple_Pack(1, base); |
| 3775 | if (bases == NULL) |
| 3776 | goto error; |
| 3777 | type->tp_bases = bases; |
| 3778 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3779 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3780 | /* Initialize tp_dict */ |
| 3781 | dict = type->tp_dict; |
| 3782 | if (dict == NULL) { |
| 3783 | dict = PyDict_New(); |
| 3784 | if (dict == NULL) |
| 3785 | goto error; |
| 3786 | type->tp_dict = dict; |
| 3787 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3788 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3789 | /* Add type-specific descriptors to tp_dict */ |
| 3790 | if (add_operators(type) < 0) |
| 3791 | goto error; |
| 3792 | if (type->tp_methods != NULL) { |
| 3793 | if (add_methods(type, type->tp_methods) < 0) |
| 3794 | goto error; |
| 3795 | } |
| 3796 | if (type->tp_members != NULL) { |
| 3797 | if (add_members(type, type->tp_members) < 0) |
| 3798 | goto error; |
| 3799 | } |
| 3800 | if (type->tp_getset != NULL) { |
| 3801 | if (add_getset(type, type->tp_getset) < 0) |
| 3802 | goto error; |
| 3803 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3804 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3805 | /* Calculate method resolution order */ |
| 3806 | if (mro_internal(type) < 0) { |
| 3807 | goto error; |
| 3808 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3809 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3810 | /* Inherit special flags from dominant base */ |
| 3811 | if (type->tp_base != NULL) |
| 3812 | inherit_special(type, type->tp_base); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3813 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3814 | /* Initialize tp_dict properly */ |
| 3815 | bases = type->tp_mro; |
| 3816 | assert(bases != NULL); |
| 3817 | assert(PyTuple_Check(bases)); |
| 3818 | n = PyTuple_GET_SIZE(bases); |
| 3819 | for (i = 1; i < n; i++) { |
| 3820 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 3821 | if (PyType_Check(b)) |
| 3822 | inherit_slots(type, (PyTypeObject *)b); |
| 3823 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3824 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3825 | /* Sanity check for tp_free. */ |
| 3826 | if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) && |
| 3827 | (type->tp_free == NULL || type->tp_free == PyObject_Del)) { |
| 3828 | /* This base class needs to call tp_free, but doesn't have |
| 3829 | * one, or its tp_free is for non-gc'ed objects. |
| 3830 | */ |
| 3831 | PyErr_Format(PyExc_TypeError, "type '%.100s' participates in " |
| 3832 | "gc and is a base type but has inappropriate " |
| 3833 | "tp_free slot", |
| 3834 | type->tp_name); |
| 3835 | goto error; |
| 3836 | } |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 3837 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3838 | /* if the type dictionary doesn't contain a __doc__, set it from |
| 3839 | the tp_doc slot. |
| 3840 | */ |
| 3841 | if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) { |
| 3842 | if (type->tp_doc != NULL) { |
| 3843 | PyObject *doc = PyUnicode_FromString(type->tp_doc); |
| 3844 | if (doc == NULL) |
| 3845 | goto error; |
| 3846 | PyDict_SetItemString(type->tp_dict, "__doc__", doc); |
| 3847 | Py_DECREF(doc); |
| 3848 | } else { |
| 3849 | PyDict_SetItemString(type->tp_dict, |
| 3850 | "__doc__", Py_None); |
| 3851 | } |
| 3852 | } |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 3853 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3854 | /* Hack for tp_hash and __hash__. |
| 3855 | If after all that, tp_hash is still NULL, and __hash__ is not in |
| 3856 | tp_dict, set tp_hash to PyObject_HashNotImplemented and |
| 3857 | tp_dict['__hash__'] equal to None. |
| 3858 | This signals that __hash__ is not inherited. |
| 3859 | */ |
| 3860 | if (type->tp_hash == NULL) { |
| 3861 | if (PyDict_GetItemString(type->tp_dict, "__hash__") == NULL) { |
| 3862 | if (PyDict_SetItemString(type->tp_dict, "__hash__", Py_None) < 0) |
| 3863 | goto error; |
| 3864 | type->tp_hash = PyObject_HashNotImplemented; |
| 3865 | } |
| 3866 | } |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3868 | /* Some more special stuff */ |
| 3869 | base = type->tp_base; |
| 3870 | if (base != NULL) { |
| 3871 | if (type->tp_as_number == NULL) |
| 3872 | type->tp_as_number = base->tp_as_number; |
| 3873 | if (type->tp_as_sequence == NULL) |
| 3874 | type->tp_as_sequence = base->tp_as_sequence; |
| 3875 | if (type->tp_as_mapping == NULL) |
| 3876 | type->tp_as_mapping = base->tp_as_mapping; |
| 3877 | if (type->tp_as_buffer == NULL) |
| 3878 | type->tp_as_buffer = base->tp_as_buffer; |
| 3879 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3880 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3881 | /* Link into each base class's list of subclasses */ |
| 3882 | bases = type->tp_bases; |
| 3883 | n = PyTuple_GET_SIZE(bases); |
| 3884 | for (i = 0; i < n; i++) { |
| 3885 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 3886 | if (PyType_Check(b) && |
| 3887 | add_subclass((PyTypeObject *)b, type) < 0) |
| 3888 | goto error; |
| 3889 | } |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3891 | /* Warn for a type that implements tp_compare (now known as |
| 3892 | tp_reserved) but not tp_richcompare. */ |
| 3893 | if (type->tp_reserved && !type->tp_richcompare) { |
| 3894 | int error; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3895 | error = PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 3896 | "Type %.100s defines tp_reserved (formerly tp_compare) " |
| 3897 | "but not tp_richcompare. Comparisons may not behave as intended.", |
| 3898 | type->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3899 | if (error == -1) |
| 3900 | goto error; |
| 3901 | } |
Mark Dickinson | 2a7d45b | 2009-02-08 11:02:10 +0000 | [diff] [blame] | 3902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3903 | /* All done -- set the ready flag */ |
| 3904 | assert(type->tp_dict != NULL); |
| 3905 | type->tp_flags = |
| 3906 | (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; |
| 3907 | return 0; |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3908 | |
| 3909 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3910 | type->tp_flags &= ~Py_TPFLAGS_READYING; |
| 3911 | return -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3912 | } |
| 3913 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3914 | static int |
| 3915 | add_subclass(PyTypeObject *base, PyTypeObject *type) |
| 3916 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3917 | Py_ssize_t i; |
| 3918 | int result; |
| 3919 | PyObject *list, *ref, *newobj; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3920 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3921 | list = base->tp_subclasses; |
| 3922 | if (list == NULL) { |
| 3923 | base->tp_subclasses = list = PyList_New(0); |
| 3924 | if (list == NULL) |
| 3925 | return -1; |
| 3926 | } |
| 3927 | assert(PyList_Check(list)); |
| 3928 | newobj = PyWeakref_NewRef((PyObject *)type, NULL); |
| 3929 | i = PyList_GET_SIZE(list); |
| 3930 | while (--i >= 0) { |
| 3931 | ref = PyList_GET_ITEM(list, i); |
| 3932 | assert(PyWeakref_CheckRef(ref)); |
| 3933 | if (PyWeakref_GET_OBJECT(ref) == Py_None) |
| 3934 | return PyList_SetItem(list, i, newobj); |
| 3935 | } |
| 3936 | result = PyList_Append(list, newobj); |
| 3937 | Py_DECREF(newobj); |
| 3938 | return result; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3941 | static void |
| 3942 | remove_subclass(PyTypeObject *base, PyTypeObject *type) |
| 3943 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3944 | Py_ssize_t i; |
| 3945 | PyObject *list, *ref; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3947 | list = base->tp_subclasses; |
| 3948 | if (list == NULL) { |
| 3949 | return; |
| 3950 | } |
| 3951 | assert(PyList_Check(list)); |
| 3952 | i = PyList_GET_SIZE(list); |
| 3953 | while (--i >= 0) { |
| 3954 | ref = PyList_GET_ITEM(list, i); |
| 3955 | assert(PyWeakref_CheckRef(ref)); |
| 3956 | if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) { |
| 3957 | /* this can't fail, right? */ |
| 3958 | PySequence_DelItem(list, i); |
| 3959 | return; |
| 3960 | } |
| 3961 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3962 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3963 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3964 | static int |
| 3965 | check_num_args(PyObject *ob, int n) |
| 3966 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3967 | if (!PyTuple_CheckExact(ob)) { |
| 3968 | PyErr_SetString(PyExc_SystemError, |
| 3969 | "PyArg_UnpackTuple() argument list is not a tuple"); |
| 3970 | return 0; |
| 3971 | } |
| 3972 | if (n == PyTuple_GET_SIZE(ob)) |
| 3973 | return 1; |
| 3974 | PyErr_Format( |
| 3975 | PyExc_TypeError, |
| 3976 | "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob)); |
| 3977 | return 0; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 3978 | } |
| 3979 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3980 | /* Generic wrappers for overloadable 'operators' such as __getitem__ */ |
| 3981 | |
| 3982 | /* There's a wrapper *function* for each distinct function typedef used |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3983 | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3984 | wrapper *table* for each distinct operation (e.g. __len__, __add__). |
| 3985 | Most tables have only one entry; the tables for binary operators have two |
| 3986 | entries, one regular and one with reversed arguments. */ |
| 3987 | |
| 3988 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3989 | wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3990 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3991 | lenfunc func = (lenfunc)wrapped; |
| 3992 | Py_ssize_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3993 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3994 | if (!check_num_args(args, 0)) |
| 3995 | return NULL; |
| 3996 | res = (*func)(self); |
| 3997 | if (res == -1 && PyErr_Occurred()) |
| 3998 | return NULL; |
| 3999 | return PyLong_FromLong((long)res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4000 | } |
| 4001 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4002 | static PyObject * |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4003 | wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped) |
| 4004 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4005 | inquiry func = (inquiry)wrapped; |
| 4006 | int res; |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4008 | if (!check_num_args(args, 0)) |
| 4009 | return NULL; |
| 4010 | res = (*func)(self); |
| 4011 | if (res == -1 && PyErr_Occurred()) |
| 4012 | return NULL; |
| 4013 | return PyBool_FromLong((long)res); |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4014 | } |
| 4015 | |
| 4016 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4017 | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4018 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4019 | binaryfunc func = (binaryfunc)wrapped; |
| 4020 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4021 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4022 | if (!check_num_args(args, 1)) |
| 4023 | return NULL; |
| 4024 | other = PyTuple_GET_ITEM(args, 0); |
| 4025 | return (*func)(self, other); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4026 | } |
| 4027 | |
| 4028 | static PyObject * |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4029 | wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped) |
| 4030 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4031 | binaryfunc func = (binaryfunc)wrapped; |
| 4032 | PyObject *other; |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4033 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4034 | if (!check_num_args(args, 1)) |
| 4035 | return NULL; |
| 4036 | other = PyTuple_GET_ITEM(args, 0); |
| 4037 | return (*func)(self, other); |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4038 | } |
| 4039 | |
| 4040 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4041 | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4042 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4043 | binaryfunc func = (binaryfunc)wrapped; |
| 4044 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4046 | if (!check_num_args(args, 1)) |
| 4047 | return NULL; |
| 4048 | other = PyTuple_GET_ITEM(args, 0); |
| 4049 | if (!PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) { |
| 4050 | Py_INCREF(Py_NotImplemented); |
| 4051 | return Py_NotImplemented; |
| 4052 | } |
| 4053 | return (*func)(other, self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4054 | } |
| 4055 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4056 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4057 | wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4058 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4059 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4060 | PyObject *other; |
| 4061 | PyObject *third = Py_None; |
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 | /* Note: This wrapper only works for __pow__() */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4065 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
| 4066 | return NULL; |
| 4067 | return (*func)(self, other, third); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4068 | } |
| 4069 | |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4070 | static PyObject * |
| 4071 | wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4072 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4073 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4074 | PyObject *other; |
| 4075 | PyObject *third = Py_None; |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4076 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4077 | /* Note: This wrapper only works for __pow__() */ |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4078 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4079 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
| 4080 | return NULL; |
| 4081 | return (*func)(other, self, third); |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4082 | } |
| 4083 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4084 | static PyObject * |
| 4085 | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4086 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4087 | unaryfunc func = (unaryfunc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4088 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4089 | if (!check_num_args(args, 0)) |
| 4090 | return NULL; |
| 4091 | return (*func)(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4092 | } |
| 4093 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4094 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4095 | wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4096 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4097 | ssizeargfunc func = (ssizeargfunc)wrapped; |
| 4098 | PyObject* o; |
| 4099 | Py_ssize_t i; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4100 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4101 | if (!PyArg_UnpackTuple(args, "", 1, 1, &o)) |
| 4102 | return NULL; |
| 4103 | i = PyNumber_AsSsize_t(o, PyExc_OverflowError); |
| 4104 | if (i == -1 && PyErr_Occurred()) |
| 4105 | return NULL; |
| 4106 | return (*func)(self, i); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4109 | static Py_ssize_t |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4110 | getindex(PyObject *self, PyObject *arg) |
| 4111 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4112 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4114 | i = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
| 4115 | if (i == -1 && PyErr_Occurred()) |
| 4116 | return -1; |
| 4117 | if (i < 0) { |
| 4118 | PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence; |
| 4119 | if (sq && sq->sq_length) { |
| 4120 | Py_ssize_t n = (*sq->sq_length)(self); |
| 4121 | if (n < 0) |
| 4122 | return -1; |
| 4123 | i += n; |
| 4124 | } |
| 4125 | } |
| 4126 | return i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4127 | } |
| 4128 | |
| 4129 | static PyObject * |
| 4130 | wrap_sq_item(PyObject *self, PyObject *args, void *wrapped) |
| 4131 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4132 | ssizeargfunc func = (ssizeargfunc)wrapped; |
| 4133 | PyObject *arg; |
| 4134 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4136 | if (PyTuple_GET_SIZE(args) == 1) { |
| 4137 | arg = PyTuple_GET_ITEM(args, 0); |
| 4138 | i = getindex(self, arg); |
| 4139 | if (i == -1 && PyErr_Occurred()) |
| 4140 | return NULL; |
| 4141 | return (*func)(self, i); |
| 4142 | } |
| 4143 | check_num_args(args, 1); |
| 4144 | assert(PyErr_Occurred()); |
| 4145 | return NULL; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4148 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4149 | wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4150 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4151 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4152 | Py_ssize_t i; |
| 4153 | int res; |
| 4154 | PyObject *arg, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4155 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4156 | if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value)) |
| 4157 | return NULL; |
| 4158 | i = getindex(self, arg); |
| 4159 | if (i == -1 && PyErr_Occurred()) |
| 4160 | return NULL; |
| 4161 | res = (*func)(self, i, value); |
| 4162 | if (res == -1 && PyErr_Occurred()) |
| 4163 | return NULL; |
| 4164 | Py_INCREF(Py_None); |
| 4165 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4166 | } |
| 4167 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4168 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4169 | wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4170 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4171 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4172 | Py_ssize_t i; |
| 4173 | int res; |
| 4174 | PyObject *arg; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4176 | if (!check_num_args(args, 1)) |
| 4177 | return NULL; |
| 4178 | arg = PyTuple_GET_ITEM(args, 0); |
| 4179 | i = getindex(self, arg); |
| 4180 | if (i == -1 && PyErr_Occurred()) |
| 4181 | return NULL; |
| 4182 | res = (*func)(self, i, NULL); |
| 4183 | if (res == -1 && PyErr_Occurred()) |
| 4184 | return NULL; |
| 4185 | Py_INCREF(Py_None); |
| 4186 | return Py_None; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4187 | } |
| 4188 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4189 | /* XXX objobjproc is a misnomer; should be objargpred */ |
| 4190 | static PyObject * |
| 4191 | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) |
| 4192 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4193 | objobjproc func = (objobjproc)wrapped; |
| 4194 | int res; |
| 4195 | PyObject *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4196 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4197 | if (!check_num_args(args, 1)) |
| 4198 | return NULL; |
| 4199 | value = PyTuple_GET_ITEM(args, 0); |
| 4200 | res = (*func)(self, value); |
| 4201 | if (res == -1 && PyErr_Occurred()) |
| 4202 | return NULL; |
| 4203 | else |
| 4204 | return PyBool_FromLong(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4205 | } |
| 4206 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4207 | static PyObject * |
| 4208 | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 4209 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4210 | objobjargproc func = (objobjargproc)wrapped; |
| 4211 | int res; |
| 4212 | PyObject *key, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4214 | if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value)) |
| 4215 | return NULL; |
| 4216 | res = (*func)(self, key, value); |
| 4217 | if (res == -1 && PyErr_Occurred()) |
| 4218 | return NULL; |
| 4219 | Py_INCREF(Py_None); |
| 4220 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4221 | } |
| 4222 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4223 | static PyObject * |
| 4224 | wrap_delitem(PyObject *self, PyObject *args, void *wrapped) |
| 4225 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4226 | objobjargproc func = (objobjargproc)wrapped; |
| 4227 | int res; |
| 4228 | PyObject *key; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4230 | if (!check_num_args(args, 1)) |
| 4231 | return NULL; |
| 4232 | key = PyTuple_GET_ITEM(args, 0); |
| 4233 | res = (*func)(self, key, NULL); |
| 4234 | if (res == -1 && PyErr_Occurred()) |
| 4235 | return NULL; |
| 4236 | Py_INCREF(Py_None); |
| 4237 | return Py_None; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4238 | } |
| 4239 | |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4240 | /* 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] | 4241 | This is called the Carlo Verre hack after its discoverer. */ |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4242 | static int |
| 4243 | hackcheck(PyObject *self, setattrofunc func, char *what) |
| 4244 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4245 | PyTypeObject *type = Py_TYPE(self); |
| 4246 | while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 4247 | type = type->tp_base; |
| 4248 | /* If type is NULL now, this is a really weird type. |
| 4249 | In the spirit of backwards compatibility (?), just shut up. */ |
| 4250 | if (type && type->tp_setattro != func) { |
| 4251 | PyErr_Format(PyExc_TypeError, |
| 4252 | "can't apply this %s to %s object", |
| 4253 | what, |
| 4254 | type->tp_name); |
| 4255 | return 0; |
| 4256 | } |
| 4257 | return 1; |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4258 | } |
| 4259 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4260 | static PyObject * |
| 4261 | wrap_setattr(PyObject *self, PyObject *args, void *wrapped) |
| 4262 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4263 | setattrofunc func = (setattrofunc)wrapped; |
| 4264 | int res; |
| 4265 | PyObject *name, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4267 | if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value)) |
| 4268 | return NULL; |
| 4269 | if (!hackcheck(self, func, "__setattr__")) |
| 4270 | return NULL; |
| 4271 | res = (*func)(self, name, value); |
| 4272 | if (res < 0) |
| 4273 | return NULL; |
| 4274 | Py_INCREF(Py_None); |
| 4275 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4276 | } |
| 4277 | |
| 4278 | static PyObject * |
| 4279 | wrap_delattr(PyObject *self, PyObject *args, void *wrapped) |
| 4280 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4281 | setattrofunc func = (setattrofunc)wrapped; |
| 4282 | int res; |
| 4283 | PyObject *name; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4285 | if (!check_num_args(args, 1)) |
| 4286 | return NULL; |
| 4287 | name = PyTuple_GET_ITEM(args, 0); |
| 4288 | if (!hackcheck(self, func, "__delattr__")) |
| 4289 | return NULL; |
| 4290 | res = (*func)(self, name, NULL); |
| 4291 | if (res < 0) |
| 4292 | return NULL; |
| 4293 | Py_INCREF(Py_None); |
| 4294 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4295 | } |
| 4296 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4297 | static PyObject * |
| 4298 | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4299 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4300 | hashfunc func = (hashfunc)wrapped; |
| 4301 | long res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4303 | if (!check_num_args(args, 0)) |
| 4304 | return NULL; |
| 4305 | res = (*func)(self); |
| 4306 | if (res == -1 && PyErr_Occurred()) |
| 4307 | return NULL; |
| 4308 | return PyLong_FromLong(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4309 | } |
| 4310 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4311 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4312 | wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4313 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4314 | ternaryfunc func = (ternaryfunc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4315 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4316 | return (*func)(self, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4317 | } |
| 4318 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4319 | static PyObject * |
| 4320 | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) |
| 4321 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4322 | richcmpfunc func = (richcmpfunc)wrapped; |
| 4323 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4325 | if (!check_num_args(args, 1)) |
| 4326 | return NULL; |
| 4327 | other = PyTuple_GET_ITEM(args, 0); |
| 4328 | return (*func)(self, other, op); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4329 | } |
| 4330 | |
| 4331 | #undef RICHCMP_WRAPPER |
| 4332 | #define RICHCMP_WRAPPER(NAME, OP) \ |
| 4333 | static PyObject * \ |
| 4334 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ |
| 4335 | { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4336 | return wrap_richcmpfunc(self, args, wrapped, OP); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4337 | } |
| 4338 | |
Jack Jansen | 8e938b4 | 2001-08-08 15:29:49 +0000 | [diff] [blame] | 4339 | RICHCMP_WRAPPER(lt, Py_LT) |
| 4340 | RICHCMP_WRAPPER(le, Py_LE) |
| 4341 | RICHCMP_WRAPPER(eq, Py_EQ) |
| 4342 | RICHCMP_WRAPPER(ne, Py_NE) |
| 4343 | RICHCMP_WRAPPER(gt, Py_GT) |
| 4344 | RICHCMP_WRAPPER(ge, Py_GE) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4345 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4346 | static PyObject * |
| 4347 | wrap_next(PyObject *self, PyObject *args, void *wrapped) |
| 4348 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4349 | unaryfunc func = (unaryfunc)wrapped; |
| 4350 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4351 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4352 | if (!check_num_args(args, 0)) |
| 4353 | return NULL; |
| 4354 | res = (*func)(self); |
| 4355 | if (res == NULL && !PyErr_Occurred()) |
| 4356 | PyErr_SetNone(PyExc_StopIteration); |
| 4357 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4358 | } |
| 4359 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4360 | static PyObject * |
| 4361 | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped) |
| 4362 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4363 | descrgetfunc func = (descrgetfunc)wrapped; |
| 4364 | PyObject *obj; |
| 4365 | PyObject *type = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4366 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4367 | if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type)) |
| 4368 | return NULL; |
| 4369 | if (obj == Py_None) |
| 4370 | obj = NULL; |
| 4371 | if (type == Py_None) |
| 4372 | type = NULL; |
| 4373 | if (type == NULL &&obj == NULL) { |
| 4374 | PyErr_SetString(PyExc_TypeError, |
| 4375 | "__get__(None, None) is invalid"); |
| 4376 | return NULL; |
| 4377 | } |
| 4378 | return (*func)(self, obj, type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4379 | } |
| 4380 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4381 | static PyObject * |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4382 | wrap_descr_set(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4383 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4384 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4385 | PyObject *obj, *value; |
| 4386 | int ret; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4388 | if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value)) |
| 4389 | return NULL; |
| 4390 | ret = (*func)(self, obj, value); |
| 4391 | if (ret < 0) |
| 4392 | return NULL; |
| 4393 | Py_INCREF(Py_None); |
| 4394 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4395 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 4396 | |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4397 | static PyObject * |
| 4398 | wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped) |
| 4399 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4400 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4401 | PyObject *obj; |
| 4402 | int ret; |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4403 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4404 | if (!check_num_args(args, 1)) |
| 4405 | return NULL; |
| 4406 | obj = PyTuple_GET_ITEM(args, 0); |
| 4407 | ret = (*func)(self, obj, NULL); |
| 4408 | if (ret < 0) |
| 4409 | return NULL; |
| 4410 | Py_INCREF(Py_None); |
| 4411 | return Py_None; |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4412 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4413 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4414 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4415 | wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4416 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4417 | initproc func = (initproc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4418 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4419 | if (func(self, args, kwds) < 0) |
| 4420 | return NULL; |
| 4421 | Py_INCREF(Py_None); |
| 4422 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4423 | } |
| 4424 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4425 | static PyObject * |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4426 | tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4427 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4428 | PyTypeObject *type, *subtype, *staticbase; |
| 4429 | PyObject *arg0, *res; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4431 | if (self == NULL || !PyType_Check(self)) |
| 4432 | Py_FatalError("__new__() called with non-type 'self'"); |
| 4433 | type = (PyTypeObject *)self; |
| 4434 | if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) { |
| 4435 | PyErr_Format(PyExc_TypeError, |
| 4436 | "%s.__new__(): not enough arguments", |
| 4437 | type->tp_name); |
| 4438 | return NULL; |
| 4439 | } |
| 4440 | arg0 = PyTuple_GET_ITEM(args, 0); |
| 4441 | if (!PyType_Check(arg0)) { |
| 4442 | PyErr_Format(PyExc_TypeError, |
| 4443 | "%s.__new__(X): X is not a type object (%s)", |
| 4444 | type->tp_name, |
| 4445 | Py_TYPE(arg0)->tp_name); |
| 4446 | return NULL; |
| 4447 | } |
| 4448 | subtype = (PyTypeObject *)arg0; |
| 4449 | if (!PyType_IsSubtype(subtype, type)) { |
| 4450 | PyErr_Format(PyExc_TypeError, |
| 4451 | "%s.__new__(%s): %s is not a subtype of %s", |
| 4452 | type->tp_name, |
| 4453 | subtype->tp_name, |
| 4454 | subtype->tp_name, |
| 4455 | type->tp_name); |
| 4456 | return NULL; |
| 4457 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4458 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4459 | /* Check that the use doesn't do something silly and unsafe like |
| 4460 | object.__new__(dict). To do this, we check that the |
| 4461 | most derived base that's not a heap type is this type. */ |
| 4462 | staticbase = subtype; |
| 4463 | while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 4464 | staticbase = staticbase->tp_base; |
| 4465 | /* If staticbase is NULL now, it is a really weird type. |
| 4466 | In the spirit of backwards compatibility (?), just shut up. */ |
| 4467 | if (staticbase && staticbase->tp_new != type->tp_new) { |
| 4468 | PyErr_Format(PyExc_TypeError, |
| 4469 | "%s.__new__(%s) is not safe, use %s.__new__()", |
| 4470 | type->tp_name, |
| 4471 | subtype->tp_name, |
| 4472 | staticbase == NULL ? "?" : staticbase->tp_name); |
| 4473 | return NULL; |
| 4474 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4476 | args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 4477 | if (args == NULL) |
| 4478 | return NULL; |
| 4479 | res = type->tp_new(subtype, args, kwds); |
| 4480 | Py_DECREF(args); |
| 4481 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4482 | } |
| 4483 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4484 | static struct PyMethodDef tp_new_methoddef[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4485 | {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS, |
| 4486 | PyDoc_STR("T.__new__(S, ...) -> " |
| 4487 | "a new object with type S, a subtype of T")}, |
| 4488 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4489 | }; |
| 4490 | |
| 4491 | static int |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4492 | add_tp_new_wrapper(PyTypeObject *type) |
| 4493 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4494 | PyObject *func; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4496 | if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL) |
| 4497 | return 0; |
| 4498 | func = PyCFunction_New(tp_new_methoddef, (PyObject *)type); |
| 4499 | if (func == NULL) |
| 4500 | return -1; |
| 4501 | if (PyDict_SetItemString(type->tp_dict, "__new__", func)) { |
| 4502 | Py_DECREF(func); |
| 4503 | return -1; |
| 4504 | } |
| 4505 | Py_DECREF(func); |
| 4506 | return 0; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4507 | } |
| 4508 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4509 | /* Slot wrappers that call the corresponding __foo__ slot. See comments |
| 4510 | below at override_slots() for more explanation. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4511 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4512 | #define SLOT0(FUNCNAME, OPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4513 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4514 | FUNCNAME(PyObject *self) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4515 | { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4516 | static PyObject *cache_str; \ |
| 4517 | return call_method(self, OPSTR, &cache_str, "()"); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4518 | } |
| 4519 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4520 | #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4521 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4522 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4523 | { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4524 | static PyObject *cache_str; \ |
| 4525 | return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4526 | } |
| 4527 | |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4528 | /* Boolean helper for SLOT1BINFULL(). |
| 4529 | right.__class__ is a nontrivial subclass of left.__class__. */ |
| 4530 | static int |
| 4531 | method_is_overloaded(PyObject *left, PyObject *right, char *name) |
| 4532 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4533 | PyObject *a, *b; |
| 4534 | int ok; |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4535 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4536 | b = PyObject_GetAttrString((PyObject *)(Py_TYPE(right)), name); |
| 4537 | if (b == NULL) { |
| 4538 | PyErr_Clear(); |
| 4539 | /* If right doesn't have it, it's not overloaded */ |
| 4540 | return 0; |
| 4541 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4542 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4543 | a = PyObject_GetAttrString((PyObject *)(Py_TYPE(left)), name); |
| 4544 | if (a == NULL) { |
| 4545 | PyErr_Clear(); |
| 4546 | Py_DECREF(b); |
| 4547 | /* If right has it but left doesn't, it's overloaded */ |
| 4548 | return 1; |
| 4549 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4551 | ok = PyObject_RichCompareBool(a, b, Py_NE); |
| 4552 | Py_DECREF(a); |
| 4553 | Py_DECREF(b); |
| 4554 | if (ok < 0) { |
| 4555 | PyErr_Clear(); |
| 4556 | return 0; |
| 4557 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4558 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4559 | return ok; |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4560 | } |
| 4561 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4562 | |
| 4563 | #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4564 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4565 | FUNCNAME(PyObject *self, PyObject *other) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4566 | { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4567 | static PyObject *cache_str, *rcache_str; \ |
| 4568 | int do_other = Py_TYPE(self) != Py_TYPE(other) && \ |
| 4569 | Py_TYPE(other)->tp_as_number != NULL && \ |
| 4570 | Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \ |
| 4571 | if (Py_TYPE(self)->tp_as_number != NULL && \ |
| 4572 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ |
| 4573 | PyObject *r; \ |
| 4574 | if (do_other && \ |
| 4575 | PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \ |
| 4576 | method_is_overloaded(self, other, ROPSTR)) { \ |
| 4577 | r = call_maybe( \ |
| 4578 | other, ROPSTR, &rcache_str, "(O)", self); \ |
| 4579 | if (r != Py_NotImplemented) \ |
| 4580 | return r; \ |
| 4581 | Py_DECREF(r); \ |
| 4582 | do_other = 0; \ |
| 4583 | } \ |
| 4584 | r = call_maybe( \ |
| 4585 | self, OPSTR, &cache_str, "(O)", other); \ |
| 4586 | if (r != Py_NotImplemented || \ |
| 4587 | Py_TYPE(other) == Py_TYPE(self)) \ |
| 4588 | return r; \ |
| 4589 | Py_DECREF(r); \ |
| 4590 | } \ |
| 4591 | if (do_other) { \ |
| 4592 | return call_maybe( \ |
| 4593 | other, ROPSTR, &rcache_str, "(O)", self); \ |
| 4594 | } \ |
| 4595 | Py_INCREF(Py_NotImplemented); \ |
| 4596 | return Py_NotImplemented; \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4597 | } |
| 4598 | |
| 4599 | #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4600 | SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR) |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4601 | |
| 4602 | #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \ |
| 4603 | static PyObject * \ |
| 4604 | FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ |
| 4605 | { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4606 | static PyObject *cache_str; \ |
| 4607 | return call_method(self, OPSTR, &cache_str, \ |
| 4608 | "(" ARGCODES ")", arg1, arg2); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4611 | static Py_ssize_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4612 | slot_sq_length(PyObject *self) |
| 4613 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4614 | static PyObject *len_str; |
| 4615 | PyObject *res = call_method(self, "__len__", &len_str, "()"); |
| 4616 | Py_ssize_t len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4617 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4618 | if (res == NULL) |
| 4619 | return -1; |
| 4620 | len = PyNumber_AsSsize_t(res, PyExc_OverflowError); |
| 4621 | Py_DECREF(res); |
| 4622 | if (len < 0) { |
| 4623 | if (!PyErr_Occurred()) |
| 4624 | PyErr_SetString(PyExc_ValueError, |
| 4625 | "__len__() should return >= 0"); |
| 4626 | return -1; |
| 4627 | } |
| 4628 | return len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4631 | /* Super-optimized version of slot_sq_item. |
| 4632 | Other slots could do the same... */ |
| 4633 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4634 | slot_sq_item(PyObject *self, Py_ssize_t i) |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4635 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4636 | static PyObject *getitem_str; |
| 4637 | PyObject *func, *args = NULL, *ival = NULL, *retval = NULL; |
| 4638 | descrgetfunc f; |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4640 | if (getitem_str == NULL) { |
| 4641 | getitem_str = PyUnicode_InternFromString("__getitem__"); |
| 4642 | if (getitem_str == NULL) |
| 4643 | return NULL; |
| 4644 | } |
| 4645 | func = _PyType_Lookup(Py_TYPE(self), getitem_str); |
| 4646 | if (func != NULL) { |
| 4647 | if ((f = Py_TYPE(func)->tp_descr_get) == NULL) |
| 4648 | Py_INCREF(func); |
| 4649 | else { |
| 4650 | func = f(func, self, (PyObject *)(Py_TYPE(self))); |
| 4651 | if (func == NULL) { |
| 4652 | return NULL; |
| 4653 | } |
| 4654 | } |
| 4655 | ival = PyLong_FromSsize_t(i); |
| 4656 | if (ival != NULL) { |
| 4657 | args = PyTuple_New(1); |
| 4658 | if (args != NULL) { |
| 4659 | PyTuple_SET_ITEM(args, 0, ival); |
| 4660 | retval = PyObject_Call(func, args, NULL); |
| 4661 | Py_XDECREF(args); |
| 4662 | Py_XDECREF(func); |
| 4663 | return retval; |
| 4664 | } |
| 4665 | } |
| 4666 | } |
| 4667 | else { |
| 4668 | PyErr_SetObject(PyExc_AttributeError, getitem_str); |
| 4669 | } |
| 4670 | Py_XDECREF(args); |
| 4671 | Py_XDECREF(ival); |
| 4672 | Py_XDECREF(func); |
| 4673 | return NULL; |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4674 | } |
| 4675 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4676 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4677 | slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4678 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4679 | PyObject *res; |
| 4680 | static PyObject *delitem_str, *setitem_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4682 | if (value == NULL) |
| 4683 | res = call_method(self, "__delitem__", &delitem_str, |
| 4684 | "(n)", index); |
| 4685 | else |
| 4686 | res = call_method(self, "__setitem__", &setitem_str, |
| 4687 | "(nO)", index, value); |
| 4688 | if (res == NULL) |
| 4689 | return -1; |
| 4690 | Py_DECREF(res); |
| 4691 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4692 | } |
| 4693 | |
| 4694 | static int |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4695 | slot_sq_contains(PyObject *self, PyObject *value) |
| 4696 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4697 | PyObject *func, *res, *args; |
| 4698 | int result = -1; |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 4699 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4700 | static PyObject *contains_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4702 | func = lookup_maybe(self, "__contains__", &contains_str); |
| 4703 | if (func != NULL) { |
| 4704 | args = PyTuple_Pack(1, value); |
| 4705 | if (args == NULL) |
| 4706 | res = NULL; |
| 4707 | else { |
| 4708 | res = PyObject_Call(func, args, NULL); |
| 4709 | Py_DECREF(args); |
| 4710 | } |
| 4711 | Py_DECREF(func); |
| 4712 | if (res != NULL) { |
| 4713 | result = PyObject_IsTrue(res); |
| 4714 | Py_DECREF(res); |
| 4715 | } |
| 4716 | } |
| 4717 | else if (! PyErr_Occurred()) { |
| 4718 | /* Possible results: -1 and 1 */ |
| 4719 | result = (int)_PySequence_IterSearch(self, value, |
| 4720 | PY_ITERSEARCH_CONTAINS); |
| 4721 | } |
| 4722 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4723 | } |
| 4724 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4725 | #define slot_mp_length slot_sq_length |
| 4726 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4727 | SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4728 | |
| 4729 | static int |
| 4730 | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) |
| 4731 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4732 | PyObject *res; |
| 4733 | static PyObject *delitem_str, *setitem_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4734 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4735 | if (value == NULL) |
| 4736 | res = call_method(self, "__delitem__", &delitem_str, |
| 4737 | "(O)", key); |
| 4738 | else |
| 4739 | res = call_method(self, "__setitem__", &setitem_str, |
| 4740 | "(OO)", key, value); |
| 4741 | if (res == NULL) |
| 4742 | return -1; |
| 4743 | Py_DECREF(res); |
| 4744 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4745 | } |
| 4746 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4747 | SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__") |
| 4748 | SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__") |
| 4749 | SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4750 | SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__") |
| 4751 | SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__") |
| 4752 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 4753 | static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4754 | |
| 4755 | SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4756 | nb_power, "__pow__", "__rpow__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4757 | |
| 4758 | static PyObject * |
| 4759 | slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) |
| 4760 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4761 | static PyObject *pow_str; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4762 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4763 | if (modulus == Py_None) |
| 4764 | return slot_nb_power_binary(self, other); |
| 4765 | /* Three-arg power doesn't use __rpow__. But ternary_op |
| 4766 | can call this when the second argument's type uses |
| 4767 | slot_nb_power, so check before calling self.__pow__. */ |
| 4768 | if (Py_TYPE(self)->tp_as_number != NULL && |
| 4769 | Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) { |
| 4770 | return call_method(self, "__pow__", &pow_str, |
| 4771 | "(OO)", other, modulus); |
| 4772 | } |
| 4773 | Py_INCREF(Py_NotImplemented); |
| 4774 | return Py_NotImplemented; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4775 | } |
| 4776 | |
| 4777 | SLOT0(slot_nb_negative, "__neg__") |
| 4778 | SLOT0(slot_nb_positive, "__pos__") |
| 4779 | SLOT0(slot_nb_absolute, "__abs__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4780 | |
| 4781 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 4782 | slot_nb_bool(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4783 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4784 | PyObject *func, *args; |
| 4785 | static PyObject *bool_str, *len_str; |
| 4786 | int result = -1; |
| 4787 | int using_len = 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4788 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4789 | func = lookup_maybe(self, "__bool__", &bool_str); |
| 4790 | if (func == NULL) { |
| 4791 | if (PyErr_Occurred()) |
| 4792 | return -1; |
| 4793 | func = lookup_maybe(self, "__len__", &len_str); |
| 4794 | if (func == NULL) |
| 4795 | return PyErr_Occurred() ? -1 : 1; |
| 4796 | using_len = 1; |
| 4797 | } |
| 4798 | args = PyTuple_New(0); |
| 4799 | if (args != NULL) { |
| 4800 | PyObject *temp = PyObject_Call(func, args, NULL); |
| 4801 | Py_DECREF(args); |
| 4802 | if (temp != NULL) { |
| 4803 | if (using_len) { |
| 4804 | /* enforced by slot_nb_len */ |
| 4805 | result = PyObject_IsTrue(temp); |
| 4806 | } |
| 4807 | else if (PyBool_Check(temp)) { |
| 4808 | result = PyObject_IsTrue(temp); |
| 4809 | } |
| 4810 | else { |
| 4811 | PyErr_Format(PyExc_TypeError, |
| 4812 | "__bool__ should return " |
| 4813 | "bool, returned %s", |
| 4814 | Py_TYPE(temp)->tp_name); |
| 4815 | result = -1; |
| 4816 | } |
| 4817 | Py_DECREF(temp); |
| 4818 | } |
| 4819 | } |
| 4820 | Py_DECREF(func); |
| 4821 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4822 | } |
| 4823 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4824 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 4825 | static PyObject * |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4826 | slot_nb_index(PyObject *self) |
| 4827 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4828 | static PyObject *index_str; |
| 4829 | return call_method(self, "__index__", &index_str, "()"); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4830 | } |
| 4831 | |
| 4832 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4833 | SLOT0(slot_nb_invert, "__invert__") |
| 4834 | SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") |
| 4835 | SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") |
| 4836 | SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") |
| 4837 | SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") |
| 4838 | SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4839 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4840 | SLOT0(slot_nb_int, "__int__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4841 | SLOT0(slot_nb_float, "__float__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4842 | SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O") |
| 4843 | SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O") |
| 4844 | SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4845 | SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4846 | /* Can't use SLOT1 here, because nb_inplace_power is ternary */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4847 | static PyObject * |
| 4848 | slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2) |
| 4849 | { |
| 4850 | static PyObject *cache_str; |
| 4851 | return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4852 | } |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4853 | SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O") |
| 4854 | SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O") |
| 4855 | SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O") |
| 4856 | SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O") |
| 4857 | SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O") |
| 4858 | SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4859 | "__floordiv__", "__rfloordiv__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4860 | SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__") |
| 4861 | SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O") |
| 4862 | SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4863 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4864 | static PyObject * |
| 4865 | slot_tp_repr(PyObject *self) |
| 4866 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4867 | PyObject *func, *res; |
| 4868 | static PyObject *repr_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4870 | func = lookup_method(self, "__repr__", &repr_str); |
| 4871 | if (func != NULL) { |
| 4872 | res = PyEval_CallObject(func, NULL); |
| 4873 | Py_DECREF(func); |
| 4874 | return res; |
| 4875 | } |
| 4876 | PyErr_Clear(); |
| 4877 | return PyUnicode_FromFormat("<%s object at %p>", |
| 4878 | Py_TYPE(self)->tp_name, self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4879 | } |
| 4880 | |
| 4881 | static PyObject * |
| 4882 | slot_tp_str(PyObject *self) |
| 4883 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4884 | PyObject *func, *res; |
| 4885 | static PyObject *str_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4887 | func = lookup_method(self, "__str__", &str_str); |
| 4888 | if (func != NULL) { |
| 4889 | res = PyEval_CallObject(func, NULL); |
| 4890 | Py_DECREF(func); |
| 4891 | return res; |
| 4892 | } |
| 4893 | else { |
| 4894 | PyObject *ress; |
| 4895 | PyErr_Clear(); |
| 4896 | res = slot_tp_repr(self); |
| 4897 | if (!res) |
| 4898 | return NULL; |
| 4899 | ress = _PyUnicode_AsDefaultEncodedString(res, NULL); |
| 4900 | Py_DECREF(res); |
| 4901 | return ress; |
| 4902 | } |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4903 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4904 | |
| 4905 | static long |
| 4906 | slot_tp_hash(PyObject *self) |
| 4907 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4908 | PyObject *func, *res; |
| 4909 | static PyObject *hash_str; |
| 4910 | long h; |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 4911 | int overflow; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4912 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4913 | func = lookup_method(self, "__hash__", &hash_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4915 | if (func == Py_None) { |
| 4916 | Py_DECREF(func); |
| 4917 | func = NULL; |
| 4918 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 4919 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4920 | if (func == NULL) { |
| 4921 | return PyObject_HashNotImplemented(self); |
| 4922 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 4923 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4924 | res = PyEval_CallObject(func, NULL); |
| 4925 | Py_DECREF(func); |
| 4926 | if (res == NULL) |
| 4927 | return -1; |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 4928 | |
| 4929 | if (!PyLong_Check(res)) { |
| 4930 | PyErr_SetString(PyExc_TypeError, |
| 4931 | "__hash__ method should return an integer"); |
| 4932 | return -1; |
| 4933 | } |
| 4934 | /* Transform the PyLong `res` to a C long `h`. For an existing |
| 4935 | hashable Python object x, hash(x) will always lie within the range |
| 4936 | of a C long. Therefore our transformation must preserve values |
| 4937 | that already lie within this range, to ensure that if x.__hash__() |
| 4938 | returns hash(y) then hash(x) == hash(y). */ |
| 4939 | h = PyLong_AsLongAndOverflow(res, &overflow); |
| 4940 | if (overflow) |
| 4941 | /* res was not within the range of a C long, so we're free to |
| 4942 | use any sufficiently bit-mixing transformation; |
| 4943 | long.__hash__ will do nicely. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4944 | h = PyLong_Type.tp_hash(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4945 | Py_DECREF(res); |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 4946 | if (h == -1 && !PyErr_Occurred()) |
| 4947 | h = -2; |
| 4948 | return h; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4949 | } |
| 4950 | |
| 4951 | static PyObject * |
| 4952 | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 4953 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4954 | static PyObject *call_str; |
| 4955 | PyObject *meth = lookup_method(self, "__call__", &call_str); |
| 4956 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4957 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4958 | if (meth == NULL) |
| 4959 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4961 | res = PyObject_Call(meth, args, kwds); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4963 | Py_DECREF(meth); |
| 4964 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4965 | } |
| 4966 | |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 4967 | /* There are two slot dispatch functions for tp_getattro. |
| 4968 | |
| 4969 | - slot_tp_getattro() is used when __getattribute__ is overridden |
| 4970 | but no __getattr__ hook is present; |
| 4971 | |
| 4972 | - slot_tp_getattr_hook() is used when a __getattr__ hook is present. |
| 4973 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 4974 | The code in update_one_slot() always installs slot_tp_getattr_hook(); this |
| 4975 | detects the absence of __getattr__ and then installs the simpler slot if |
| 4976 | necessary. */ |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 4977 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4978 | static PyObject * |
| 4979 | slot_tp_getattro(PyObject *self, PyObject *name) |
| 4980 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4981 | static PyObject *getattribute_str = NULL; |
| 4982 | return call_method(self, "__getattribute__", &getattribute_str, |
| 4983 | "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4984 | } |
| 4985 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 4986 | static PyObject * |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 4987 | call_attribute(PyObject *self, PyObject *attr, PyObject *name) |
| 4988 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4989 | PyObject *res, *descr = NULL; |
| 4990 | descrgetfunc f = Py_TYPE(attr)->tp_descr_get; |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 4991 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4992 | if (f != NULL) { |
| 4993 | descr = f(attr, self, (PyObject *)(Py_TYPE(self))); |
| 4994 | if (descr == NULL) |
| 4995 | return NULL; |
| 4996 | else |
| 4997 | attr = descr; |
| 4998 | } |
| 4999 | res = PyObject_CallFunctionObjArgs(attr, name, NULL); |
| 5000 | Py_XDECREF(descr); |
| 5001 | return res; |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5002 | } |
| 5003 | |
| 5004 | static PyObject * |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5005 | slot_tp_getattr_hook(PyObject *self, PyObject *name) |
| 5006 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5007 | PyTypeObject *tp = Py_TYPE(self); |
| 5008 | PyObject *getattr, *getattribute, *res; |
| 5009 | static PyObject *getattribute_str = NULL; |
| 5010 | static PyObject *getattr_str = NULL; |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5012 | if (getattr_str == NULL) { |
| 5013 | getattr_str = PyUnicode_InternFromString("__getattr__"); |
| 5014 | if (getattr_str == NULL) |
| 5015 | return NULL; |
| 5016 | } |
| 5017 | if (getattribute_str == NULL) { |
| 5018 | getattribute_str = |
| 5019 | PyUnicode_InternFromString("__getattribute__"); |
| 5020 | if (getattribute_str == NULL) |
| 5021 | return NULL; |
| 5022 | } |
| 5023 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5024 | method fully for each attribute lookup for classes with |
| 5025 | __getattr__, even when the attribute is present. So we use |
| 5026 | _PyType_Lookup and create the method only when needed, with |
| 5027 | call_attribute. */ |
| 5028 | getattr = _PyType_Lookup(tp, getattr_str); |
| 5029 | if (getattr == NULL) { |
| 5030 | /* No __getattr__ hook: use a simpler dispatcher */ |
| 5031 | tp->tp_getattro = slot_tp_getattro; |
| 5032 | return slot_tp_getattro(self, name); |
| 5033 | } |
| 5034 | Py_INCREF(getattr); |
| 5035 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5036 | method fully for each attribute lookup for classes with |
| 5037 | __getattr__, even when self has the default __getattribute__ |
| 5038 | method. So we use _PyType_Lookup and create the method only when |
| 5039 | needed, with call_attribute. */ |
| 5040 | getattribute = _PyType_Lookup(tp, getattribute_str); |
| 5041 | if (getattribute == NULL || |
| 5042 | (Py_TYPE(getattribute) == &PyWrapperDescr_Type && |
| 5043 | ((PyWrapperDescrObject *)getattribute)->d_wrapped == |
| 5044 | (void *)PyObject_GenericGetAttr)) |
| 5045 | res = PyObject_GenericGetAttr(self, name); |
| 5046 | else { |
| 5047 | Py_INCREF(getattribute); |
| 5048 | res = call_attribute(self, getattribute, name); |
| 5049 | Py_DECREF(getattribute); |
| 5050 | } |
| 5051 | if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 5052 | PyErr_Clear(); |
| 5053 | res = call_attribute(self, getattr, name); |
| 5054 | } |
| 5055 | Py_DECREF(getattr); |
| 5056 | return res; |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5057 | } |
| 5058 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5059 | static int |
| 5060 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) |
| 5061 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5062 | PyObject *res; |
| 5063 | static PyObject *delattr_str, *setattr_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5065 | if (value == NULL) |
| 5066 | res = call_method(self, "__delattr__", &delattr_str, |
| 5067 | "(O)", name); |
| 5068 | else |
| 5069 | res = call_method(self, "__setattr__", &setattr_str, |
| 5070 | "(OO)", name, value); |
| 5071 | if (res == NULL) |
| 5072 | return -1; |
| 5073 | Py_DECREF(res); |
| 5074 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5075 | } |
| 5076 | |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 5077 | static char *name_op[] = { |
| 5078 | "__lt__", |
| 5079 | "__le__", |
| 5080 | "__eq__", |
| 5081 | "__ne__", |
| 5082 | "__gt__", |
| 5083 | "__ge__", |
| 5084 | }; |
| 5085 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5086 | static PyObject * |
Mark Dickinson | 6f1d049 | 2009-11-15 13:58:49 +0000 | [diff] [blame] | 5087 | slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5088 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5089 | PyObject *func, *args, *res; |
| 5090 | static PyObject *op_str[6]; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5091 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5092 | func = lookup_method(self, name_op[op], &op_str[op]); |
| 5093 | if (func == NULL) { |
| 5094 | PyErr_Clear(); |
| 5095 | Py_INCREF(Py_NotImplemented); |
| 5096 | return Py_NotImplemented; |
| 5097 | } |
| 5098 | args = PyTuple_Pack(1, other); |
| 5099 | if (args == NULL) |
| 5100 | res = NULL; |
| 5101 | else { |
| 5102 | res = PyObject_Call(func, args, NULL); |
| 5103 | Py_DECREF(args); |
| 5104 | } |
| 5105 | Py_DECREF(func); |
| 5106 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5107 | } |
| 5108 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5109 | static PyObject * |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5110 | slot_tp_iter(PyObject *self) |
| 5111 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5112 | PyObject *func, *res; |
| 5113 | static PyObject *iter_str, *getitem_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5115 | func = lookup_method(self, "__iter__", &iter_str); |
| 5116 | if (func != NULL) { |
| 5117 | PyObject *args; |
| 5118 | args = res = PyTuple_New(0); |
| 5119 | if (args != NULL) { |
| 5120 | res = PyObject_Call(func, args, NULL); |
| 5121 | Py_DECREF(args); |
| 5122 | } |
| 5123 | Py_DECREF(func); |
| 5124 | return res; |
| 5125 | } |
| 5126 | PyErr_Clear(); |
| 5127 | func = lookup_method(self, "__getitem__", &getitem_str); |
| 5128 | if (func == NULL) { |
| 5129 | PyErr_Format(PyExc_TypeError, |
| 5130 | "'%.200s' object is not iterable", |
| 5131 | Py_TYPE(self)->tp_name); |
| 5132 | return NULL; |
| 5133 | } |
| 5134 | Py_DECREF(func); |
| 5135 | return PySeqIter_New(self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5136 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5137 | |
| 5138 | static PyObject * |
| 5139 | slot_tp_iternext(PyObject *self) |
| 5140 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5141 | static PyObject *next_str; |
| 5142 | return call_method(self, "__next__", &next_str, "()"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5143 | } |
| 5144 | |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5145 | static PyObject * |
| 5146 | slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 5147 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5148 | PyTypeObject *tp = Py_TYPE(self); |
| 5149 | PyObject *get; |
| 5150 | static PyObject *get_str = NULL; |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5152 | if (get_str == NULL) { |
| 5153 | get_str = PyUnicode_InternFromString("__get__"); |
| 5154 | if (get_str == NULL) |
| 5155 | return NULL; |
| 5156 | } |
| 5157 | get = _PyType_Lookup(tp, get_str); |
| 5158 | if (get == NULL) { |
| 5159 | /* Avoid further slowdowns */ |
| 5160 | if (tp->tp_descr_get == slot_tp_descr_get) |
| 5161 | tp->tp_descr_get = NULL; |
| 5162 | Py_INCREF(self); |
| 5163 | return self; |
| 5164 | } |
| 5165 | if (obj == NULL) |
| 5166 | obj = Py_None; |
| 5167 | if (type == NULL) |
| 5168 | type = Py_None; |
| 5169 | return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5170 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5171 | |
| 5172 | static int |
| 5173 | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) |
| 5174 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5175 | PyObject *res; |
| 5176 | static PyObject *del_str, *set_str; |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5178 | if (value == NULL) |
| 5179 | res = call_method(self, "__delete__", &del_str, |
| 5180 | "(O)", target); |
| 5181 | else |
| 5182 | res = call_method(self, "__set__", &set_str, |
| 5183 | "(OO)", target, value); |
| 5184 | if (res == NULL) |
| 5185 | return -1; |
| 5186 | Py_DECREF(res); |
| 5187 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5188 | } |
| 5189 | |
| 5190 | static int |
| 5191 | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 5192 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5193 | static PyObject *init_str; |
| 5194 | PyObject *meth = lookup_method(self, "__init__", &init_str); |
| 5195 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5196 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5197 | if (meth == NULL) |
| 5198 | return -1; |
| 5199 | res = PyObject_Call(meth, args, kwds); |
| 5200 | Py_DECREF(meth); |
| 5201 | if (res == NULL) |
| 5202 | return -1; |
| 5203 | if (res != Py_None) { |
| 5204 | PyErr_Format(PyExc_TypeError, |
| 5205 | "__init__() should return None, not '%.200s'", |
| 5206 | Py_TYPE(res)->tp_name); |
| 5207 | Py_DECREF(res); |
| 5208 | return -1; |
| 5209 | } |
| 5210 | Py_DECREF(res); |
| 5211 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5212 | } |
| 5213 | |
| 5214 | static PyObject * |
| 5215 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 5216 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5217 | static PyObject *new_str; |
| 5218 | PyObject *func; |
| 5219 | PyObject *newargs, *x; |
| 5220 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5222 | if (new_str == NULL) { |
| 5223 | new_str = PyUnicode_InternFromString("__new__"); |
| 5224 | if (new_str == NULL) |
| 5225 | return NULL; |
| 5226 | } |
| 5227 | func = PyObject_GetAttr((PyObject *)type, new_str); |
| 5228 | if (func == NULL) |
| 5229 | return NULL; |
| 5230 | assert(PyTuple_Check(args)); |
| 5231 | n = PyTuple_GET_SIZE(args); |
| 5232 | newargs = PyTuple_New(n+1); |
| 5233 | if (newargs == NULL) |
| 5234 | return NULL; |
| 5235 | Py_INCREF(type); |
| 5236 | PyTuple_SET_ITEM(newargs, 0, (PyObject *)type); |
| 5237 | for (i = 0; i < n; i++) { |
| 5238 | x = PyTuple_GET_ITEM(args, i); |
| 5239 | Py_INCREF(x); |
| 5240 | PyTuple_SET_ITEM(newargs, i+1, x); |
| 5241 | } |
| 5242 | x = PyObject_Call(func, newargs, kwds); |
| 5243 | Py_DECREF(newargs); |
| 5244 | Py_DECREF(func); |
| 5245 | return x; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5246 | } |
| 5247 | |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5248 | static void |
| 5249 | slot_tp_del(PyObject *self) |
| 5250 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5251 | static PyObject *del_str = NULL; |
| 5252 | PyObject *del, *res; |
| 5253 | PyObject *error_type, *error_value, *error_traceback; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5255 | /* Temporarily resurrect the object. */ |
| 5256 | assert(self->ob_refcnt == 0); |
| 5257 | self->ob_refcnt = 1; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5259 | /* Save the current exception, if any. */ |
| 5260 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5261 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5262 | /* Execute __del__ method, if any. */ |
| 5263 | del = lookup_maybe(self, "__del__", &del_str); |
| 5264 | if (del != NULL) { |
| 5265 | res = PyEval_CallObject(del, NULL); |
| 5266 | if (res == NULL) |
| 5267 | PyErr_WriteUnraisable(del); |
| 5268 | else |
| 5269 | Py_DECREF(res); |
| 5270 | Py_DECREF(del); |
| 5271 | } |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5273 | /* Restore the saved exception. */ |
| 5274 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5276 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 5277 | * cause a recursive call. |
| 5278 | */ |
| 5279 | assert(self->ob_refcnt > 0); |
| 5280 | if (--self->ob_refcnt == 0) |
| 5281 | return; /* this is the normal path out */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5283 | /* __del__ resurrected it! Make it look like the original Py_DECREF |
| 5284 | * never happened. |
| 5285 | */ |
| 5286 | { |
| 5287 | Py_ssize_t refcnt = self->ob_refcnt; |
| 5288 | _Py_NewReference(self); |
| 5289 | self->ob_refcnt = refcnt; |
| 5290 | } |
| 5291 | assert(!PyType_IS_GC(Py_TYPE(self)) || |
| 5292 | _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED); |
| 5293 | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so |
| 5294 | * we need to undo that. */ |
| 5295 | _Py_DEC_REFTOTAL; |
| 5296 | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object |
| 5297 | * chain, so no more to do there. |
| 5298 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
| 5299 | * _Py_NewReference bumped tp_allocs: both of those need to be |
| 5300 | * undone. |
| 5301 | */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5302 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5303 | --Py_TYPE(self)->tp_frees; |
| 5304 | --Py_TYPE(self)->tp_allocs; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5305 | #endif |
| 5306 | } |
| 5307 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5308 | |
| 5309 | /* 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] | 5310 | functions. The offsets here are relative to the 'PyHeapTypeObject' |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5311 | structure, which incorporates the additional structures used for numbers, |
| 5312 | sequences and mappings. |
| 5313 | 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] | 5314 | __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] | 5315 | slots (e.g. __str__ affects tp_str as well as tp_repr). The table is |
| 5316 | terminated with an all-zero entry. (This table is further initialized and |
| 5317 | sorted in init_slotdefs() below.) */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5318 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5319 | typedef struct wrapperbase slotdef; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5320 | |
| 5321 | #undef TPSLOT |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5322 | #undef FLSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5323 | #undef ETSLOT |
| 5324 | #undef SQSLOT |
| 5325 | #undef MPSLOT |
| 5326 | #undef NBSLOT |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5327 | #undef UNSLOT |
| 5328 | #undef IBSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5329 | #undef BINSLOT |
| 5330 | #undef RBINSLOT |
| 5331 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5332 | #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5333 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5334 | PyDoc_STR(DOC)} |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5335 | #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5336 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5337 | PyDoc_STR(DOC), FLAGS} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5338 | #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5339 | {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5340 | PyDoc_STR(DOC)} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5341 | #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5342 | ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5343 | #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5344 | ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5345 | #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5346 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5347 | #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5348 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5349 | "x." NAME "() <==> " DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5350 | #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5351 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5352 | "x." NAME "(y) <==> x" DOC "y") |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5353 | #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5354 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5355 | "x." NAME "(y) <==> x" DOC "y") |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5356 | #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5357 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5358 | "x." NAME "(y) <==> y" DOC "x") |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5359 | #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5360 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5361 | "x." NAME "(y) <==> " DOC) |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5362 | #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5363 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5364 | "x." NAME "(y) <==> " DOC) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5365 | |
| 5366 | static slotdef slotdefs[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5367 | SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc, |
| 5368 | "x.__len__() <==> len(x)"), |
| 5369 | /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL. |
| 5370 | The logic in abstract.c always falls back to nb_add/nb_multiply in |
| 5371 | this case. Defining both the nb_* and the sq_* slots to call the |
| 5372 | user-defined methods has unexpected side-effects, as shown by |
| 5373 | test_descr.notimplemented() */ |
| 5374 | SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc, |
| 5375 | "x.__add__(y) <==> x+y"), |
| 5376 | SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc, |
| 5377 | "x.__mul__(n) <==> x*n"), |
| 5378 | SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc, |
| 5379 | "x.__rmul__(n) <==> n*x"), |
| 5380 | SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, |
| 5381 | "x.__getitem__(y) <==> x[y]"), |
| 5382 | SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, |
| 5383 | "x.__setitem__(i, y) <==> x[i]=y"), |
| 5384 | SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, |
| 5385 | "x.__delitem__(y) <==> del x[y]"), |
| 5386 | SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, |
| 5387 | "x.__contains__(y) <==> y in x"), |
| 5388 | SQSLOT("__iadd__", sq_inplace_concat, NULL, |
| 5389 | wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"), |
| 5390 | SQSLOT("__imul__", sq_inplace_repeat, NULL, |
| 5391 | wrap_indexargfunc, "x.__imul__(y) <==> x*=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5393 | MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc, |
| 5394 | "x.__len__() <==> len(x)"), |
| 5395 | MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, |
| 5396 | wrap_binaryfunc, |
| 5397 | "x.__getitem__(y) <==> x[y]"), |
| 5398 | MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, |
| 5399 | wrap_objobjargproc, |
| 5400 | "x.__setitem__(i, y) <==> x[i]=y"), |
| 5401 | MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, |
| 5402 | wrap_delitem, |
| 5403 | "x.__delitem__(y) <==> del x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5405 | BINSLOT("__add__", nb_add, slot_nb_add, |
| 5406 | "+"), |
| 5407 | RBINSLOT("__radd__", nb_add, slot_nb_add, |
| 5408 | "+"), |
| 5409 | BINSLOT("__sub__", nb_subtract, slot_nb_subtract, |
| 5410 | "-"), |
| 5411 | RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract, |
| 5412 | "-"), |
| 5413 | BINSLOT("__mul__", nb_multiply, slot_nb_multiply, |
| 5414 | "*"), |
| 5415 | RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply, |
| 5416 | "*"), |
| 5417 | BINSLOT("__mod__", nb_remainder, slot_nb_remainder, |
| 5418 | "%"), |
| 5419 | RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder, |
| 5420 | "%"), |
| 5421 | BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod, |
| 5422 | "divmod(x, y)"), |
| 5423 | RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod, |
| 5424 | "divmod(y, x)"), |
| 5425 | NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, |
| 5426 | "x.__pow__(y[, z]) <==> pow(x, y[, z])"), |
| 5427 | NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, |
| 5428 | "y.__rpow__(x[, z]) <==> pow(x, y[, z])"), |
| 5429 | UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"), |
| 5430 | UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"), |
| 5431 | UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, |
| 5432 | "abs(x)"), |
| 5433 | UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred, |
| 5434 | "x != 0"), |
| 5435 | UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"), |
| 5436 | BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5437 | RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5438 | BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5439 | RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5440 | BINSLOT("__and__", nb_and, slot_nb_and, "&"), |
| 5441 | RBINSLOT("__rand__", nb_and, slot_nb_and, "&"), |
| 5442 | BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"), |
| 5443 | RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), |
| 5444 | BINSLOT("__or__", nb_or, slot_nb_or, "|"), |
| 5445 | RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), |
| 5446 | UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, |
| 5447 | "int(x)"), |
| 5448 | UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, |
| 5449 | "float(x)"), |
| 5450 | NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc, |
| 5451 | "x[y:z] <==> x[y.__index__():z.__index__()]"), |
| 5452 | IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, |
| 5453 | wrap_binaryfunc, "+"), |
| 5454 | IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, |
| 5455 | wrap_binaryfunc, "-"), |
| 5456 | IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply, |
| 5457 | wrap_binaryfunc, "*"), |
| 5458 | IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder, |
| 5459 | wrap_binaryfunc, "%"), |
| 5460 | IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power, |
| 5461 | wrap_binaryfunc, "**"), |
| 5462 | IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift, |
| 5463 | wrap_binaryfunc, "<<"), |
| 5464 | IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift, |
| 5465 | wrap_binaryfunc, ">>"), |
| 5466 | IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and, |
| 5467 | wrap_binaryfunc, "&"), |
| 5468 | IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor, |
| 5469 | wrap_binaryfunc, "^"), |
| 5470 | IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or, |
| 5471 | wrap_binaryfunc, "|"), |
| 5472 | BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5473 | RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5474 | BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5475 | RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5476 | IBSLOT("__ifloordiv__", nb_inplace_floor_divide, |
| 5477 | slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"), |
| 5478 | IBSLOT("__itruediv__", nb_inplace_true_divide, |
| 5479 | slot_nb_inplace_true_divide, wrap_binaryfunc, "/"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5481 | TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, |
| 5482 | "x.__str__() <==> str(x)"), |
| 5483 | TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, |
| 5484 | "x.__repr__() <==> repr(x)"), |
| 5485 | TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, |
| 5486 | "x.__hash__() <==> hash(x)"), |
| 5487 | FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, |
| 5488 | "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS), |
| 5489 | TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, |
| 5490 | wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"), |
| 5491 | TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), |
| 5492 | TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), |
| 5493 | TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), |
| 5494 | TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, |
| 5495 | "x.__setattr__('name', value) <==> x.name = value"), |
| 5496 | TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), |
| 5497 | TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, |
| 5498 | "x.__delattr__('name') <==> del x.name"), |
| 5499 | TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), |
| 5500 | TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, |
| 5501 | "x.__lt__(y) <==> x<y"), |
| 5502 | TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, |
| 5503 | "x.__le__(y) <==> x<=y"), |
| 5504 | TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, |
| 5505 | "x.__eq__(y) <==> x==y"), |
| 5506 | TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, |
| 5507 | "x.__ne__(y) <==> x!=y"), |
| 5508 | TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, |
| 5509 | "x.__gt__(y) <==> x>y"), |
| 5510 | TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, |
| 5511 | "x.__ge__(y) <==> x>=y"), |
| 5512 | TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, |
| 5513 | "x.__iter__() <==> iter(x)"), |
| 5514 | TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next, |
| 5515 | "x.__next__() <==> next(x)"), |
| 5516 | TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, |
| 5517 | "descr.__get__(obj[, type]) -> value"), |
| 5518 | TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, |
| 5519 | "descr.__set__(obj, value)"), |
| 5520 | TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set, |
| 5521 | wrap_descr_delete, "descr.__delete__(obj)"), |
| 5522 | FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init, |
| 5523 | "x.__init__(...) initializes x; " |
Alexander Belopolsky | 977a684 | 2010-08-16 20:17:07 +0000 | [diff] [blame^] | 5524 | "see help(type(x)) for signature", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5525 | PyWrapperFlag_KEYWORDS), |
| 5526 | TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""), |
| 5527 | TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""), |
| 5528 | {NULL} |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5529 | }; |
| 5530 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5531 | /* 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] | 5532 | 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] | 5533 | the offset to the type pointer, since it takes care to indirect through the |
| 5534 | proper indirection pointer (as_buffer, etc.); it returns NULL if the |
| 5535 | indirection pointer is NULL. */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5536 | static void ** |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5537 | slotptr(PyTypeObject *type, int ioffset) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5538 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5539 | char *ptr; |
| 5540 | long offset = ioffset; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5542 | /* Note: this depends on the order of the members of PyHeapTypeObject! */ |
| 5543 | assert(offset >= 0); |
| 5544 | assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer)); |
| 5545 | if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) { |
| 5546 | ptr = (char *)type->tp_as_sequence; |
| 5547 | offset -= offsetof(PyHeapTypeObject, as_sequence); |
| 5548 | } |
| 5549 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) { |
| 5550 | ptr = (char *)type->tp_as_mapping; |
| 5551 | offset -= offsetof(PyHeapTypeObject, as_mapping); |
| 5552 | } |
| 5553 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) { |
| 5554 | ptr = (char *)type->tp_as_number; |
| 5555 | offset -= offsetof(PyHeapTypeObject, as_number); |
| 5556 | } |
| 5557 | else { |
| 5558 | ptr = (char *)type; |
| 5559 | } |
| 5560 | if (ptr != NULL) |
| 5561 | ptr += offset; |
| 5562 | return (void **)ptr; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5563 | } |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 5564 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5565 | /* Length of array of slotdef pointers used to store slots with the |
| 5566 | same __name__. There should be at most MAX_EQUIV-1 slotdef entries with |
| 5567 | the same __name__, for any __name__. Since that's a static property, it is |
| 5568 | appropriate to declare fixed-size arrays for this. */ |
| 5569 | #define MAX_EQUIV 10 |
| 5570 | |
| 5571 | /* Return a slot pointer for a given name, but ONLY if the attribute has |
| 5572 | exactly one slot function. The name must be an interned string. */ |
| 5573 | static void ** |
| 5574 | resolve_slotdups(PyTypeObject *type, PyObject *name) |
| 5575 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5576 | /* 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] | 5577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5578 | /* pname and ptrs act as a little cache */ |
| 5579 | static PyObject *pname; |
| 5580 | static slotdef *ptrs[MAX_EQUIV]; |
| 5581 | slotdef *p, **pp; |
| 5582 | void **res, **ptr; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5583 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5584 | if (pname != name) { |
| 5585 | /* Collect all slotdefs that match name into ptrs. */ |
| 5586 | pname = name; |
| 5587 | pp = ptrs; |
| 5588 | for (p = slotdefs; p->name_strobj; p++) { |
| 5589 | if (p->name_strobj == name) |
| 5590 | *pp++ = p; |
| 5591 | } |
| 5592 | *pp = NULL; |
| 5593 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5594 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5595 | /* Look in all matching slots of the type; if exactly one of these has |
| 5596 | a filled-in slot, return its value. Otherwise return NULL. */ |
| 5597 | res = NULL; |
| 5598 | for (pp = ptrs; *pp; pp++) { |
| 5599 | ptr = slotptr(type, (*pp)->offset); |
| 5600 | if (ptr == NULL || *ptr == NULL) |
| 5601 | continue; |
| 5602 | if (res != NULL) |
| 5603 | return NULL; |
| 5604 | res = ptr; |
| 5605 | } |
| 5606 | return res; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5607 | } |
| 5608 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5609 | /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5610 | does some incredibly complex thinking and then sticks something into the |
| 5611 | slot. (It sees if the adjacent slotdefs for the same slot have conflicting |
| 5612 | interests, and then stores a generic wrapper or a specific function into |
| 5613 | the slot.) Return a pointer to the next slotdef with a different offset, |
| 5614 | because that's convenient for fixup_slot_dispatchers(). */ |
| 5615 | static slotdef * |
| 5616 | update_one_slot(PyTypeObject *type, slotdef *p) |
| 5617 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5618 | PyObject *descr; |
| 5619 | PyWrapperDescrObject *d; |
| 5620 | void *generic = NULL, *specific = NULL; |
| 5621 | int use_generic = 0; |
| 5622 | int offset = p->offset; |
| 5623 | void **ptr = slotptr(type, offset); |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5625 | if (ptr == NULL) { |
| 5626 | do { |
| 5627 | ++p; |
| 5628 | } while (p->offset == offset); |
| 5629 | return p; |
| 5630 | } |
| 5631 | do { |
| 5632 | descr = _PyType_Lookup(type, p->name_strobj); |
| 5633 | if (descr == NULL) { |
| 5634 | if (ptr == (void**)&type->tp_iternext) { |
| 5635 | specific = _PyObject_NextNotImplemented; |
| 5636 | } |
| 5637 | continue; |
| 5638 | } |
| 5639 | if (Py_TYPE(descr) == &PyWrapperDescr_Type) { |
| 5640 | void **tptr = resolve_slotdups(type, p->name_strobj); |
| 5641 | if (tptr == NULL || tptr == ptr) |
| 5642 | generic = p->function; |
| 5643 | d = (PyWrapperDescrObject *)descr; |
| 5644 | if (d->d_base->wrapper == p->wrapper && |
| 5645 | PyType_IsSubtype(type, PyDescr_TYPE(d))) |
| 5646 | { |
| 5647 | if (specific == NULL || |
| 5648 | specific == d->d_wrapped) |
| 5649 | specific = d->d_wrapped; |
| 5650 | else |
| 5651 | use_generic = 1; |
| 5652 | } |
| 5653 | } |
| 5654 | else if (Py_TYPE(descr) == &PyCFunction_Type && |
| 5655 | PyCFunction_GET_FUNCTION(descr) == |
| 5656 | (PyCFunction)tp_new_wrapper && |
| 5657 | ptr == (void**)&type->tp_new) |
| 5658 | { |
| 5659 | /* The __new__ wrapper is not a wrapper descriptor, |
| 5660 | so must be special-cased differently. |
| 5661 | If we don't do this, creating an instance will |
| 5662 | always use slot_tp_new which will look up |
| 5663 | __new__ in the MRO which will call tp_new_wrapper |
| 5664 | which will look through the base classes looking |
| 5665 | for a static base and call its tp_new (usually |
| 5666 | PyType_GenericNew), after performing various |
| 5667 | sanity checks and constructing a new argument |
| 5668 | list. Cut all that nonsense short -- this speeds |
| 5669 | up instance creation tremendously. */ |
| 5670 | specific = (void *)type->tp_new; |
| 5671 | /* XXX I'm not 100% sure that there isn't a hole |
| 5672 | in this reasoning that requires additional |
| 5673 | sanity checks. I'll buy the first person to |
| 5674 | point out a bug in this reasoning a beer. */ |
| 5675 | } |
| 5676 | else if (descr == Py_None && |
| 5677 | ptr == (void**)&type->tp_hash) { |
| 5678 | /* We specifically allow __hash__ to be set to None |
| 5679 | to prevent inheritance of the default |
| 5680 | implementation from object.__hash__ */ |
| 5681 | specific = PyObject_HashNotImplemented; |
| 5682 | } |
| 5683 | else { |
| 5684 | use_generic = 1; |
| 5685 | generic = p->function; |
| 5686 | } |
| 5687 | } while ((++p)->offset == offset); |
| 5688 | if (specific && !use_generic) |
| 5689 | *ptr = specific; |
| 5690 | else |
| 5691 | *ptr = generic; |
| 5692 | return p; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5693 | } |
| 5694 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5695 | /* In the type, update the slots whose slotdefs are gathered in the pp array. |
| 5696 | This is a callback for update_subclasses(). */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5697 | static int |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5698 | update_slots_callback(PyTypeObject *type, void *data) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5699 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5700 | slotdef **pp = (slotdef **)data; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5702 | for (; *pp; pp++) |
| 5703 | update_one_slot(type, *pp); |
| 5704 | return 0; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5705 | } |
| 5706 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5707 | /* Comparison function for qsort() to compare slotdefs by their offset, and |
| 5708 | for equal offset by their address (to force a stable sort). */ |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5709 | static int |
| 5710 | slotdef_cmp(const void *aa, const void *bb) |
| 5711 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5712 | const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb; |
| 5713 | int c = a->offset - b->offset; |
| 5714 | if (c != 0) |
| 5715 | return c; |
| 5716 | else |
| 5717 | /* Cannot use a-b, as this gives off_t, |
| 5718 | which may lose precision when converted to int. */ |
| 5719 | return (a > b) ? 1 : (a < b) ? -1 : 0; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5720 | } |
| 5721 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5722 | /* Initialize the slotdefs table by adding interned string objects for the |
| 5723 | names and sorting the entries. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5724 | static void |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5725 | init_slotdefs(void) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5726 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5727 | slotdef *p; |
| 5728 | static int initialized = 0; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5729 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5730 | if (initialized) |
| 5731 | return; |
| 5732 | for (p = slotdefs; p->name; p++) { |
| 5733 | p->name_strobj = PyUnicode_InternFromString(p->name); |
| 5734 | if (!p->name_strobj) |
| 5735 | Py_FatalError("Out of memory interning slotdef names"); |
| 5736 | } |
| 5737 | qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef), |
| 5738 | slotdef_cmp); |
| 5739 | initialized = 1; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5740 | } |
| 5741 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5742 | /* Update the slots after assignment to a class (type) attribute. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5743 | static int |
| 5744 | update_slot(PyTypeObject *type, PyObject *name) |
| 5745 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5746 | slotdef *ptrs[MAX_EQUIV]; |
| 5747 | slotdef *p; |
| 5748 | slotdef **pp; |
| 5749 | int offset; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5751 | /* Clear the VALID_VERSION flag of 'type' and all its |
| 5752 | subclasses. This could possibly be unified with the |
| 5753 | update_subclasses() recursion below, but carefully: |
| 5754 | they each have their own conditions on which to stop |
| 5755 | recursing into subclasses. */ |
| 5756 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 5757 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5758 | init_slotdefs(); |
| 5759 | pp = ptrs; |
| 5760 | for (p = slotdefs; p->name; p++) { |
| 5761 | /* XXX assume name is interned! */ |
| 5762 | if (p->name_strobj == name) |
| 5763 | *pp++ = p; |
| 5764 | } |
| 5765 | *pp = NULL; |
| 5766 | for (pp = ptrs; *pp; pp++) { |
| 5767 | p = *pp; |
| 5768 | offset = p->offset; |
| 5769 | while (p > slotdefs && (p-1)->offset == offset) |
| 5770 | --p; |
| 5771 | *pp = p; |
| 5772 | } |
| 5773 | if (ptrs[0] == NULL) |
| 5774 | return 0; /* Not an attribute that affects any slots */ |
| 5775 | return update_subclasses(type, name, |
| 5776 | update_slots_callback, (void *)ptrs); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5777 | } |
| 5778 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5779 | /* Store the proper functions in the slot dispatches at class (type) |
| 5780 | definition time, based upon which operations the class overrides in its |
| 5781 | dict. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5782 | static void |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5783 | fixup_slot_dispatchers(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5784 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5785 | slotdef *p; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5786 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5787 | init_slotdefs(); |
| 5788 | for (p = slotdefs; p->name; ) |
| 5789 | p = update_one_slot(type, p); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5790 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5791 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 5792 | static void |
| 5793 | update_all_slots(PyTypeObject* type) |
| 5794 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5795 | slotdef *p; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 5796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5797 | init_slotdefs(); |
| 5798 | for (p = slotdefs; p->name; p++) { |
| 5799 | /* update_slot returns int but can't actually fail */ |
| 5800 | update_slot(type, p->name_strobj); |
| 5801 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 5802 | } |
| 5803 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5804 | /* recurse_down_subclasses() and update_subclasses() are mutually |
| 5805 | recursive functions to call a callback for all subclasses, |
| 5806 | but refraining from recursing into subclasses that define 'name'. */ |
| 5807 | |
| 5808 | static int |
| 5809 | update_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5810 | update_callback callback, void *data) |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5811 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5812 | if (callback(type, data) < 0) |
| 5813 | return -1; |
| 5814 | return recurse_down_subclasses(type, name, callback, data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5815 | } |
| 5816 | |
| 5817 | static int |
| 5818 | recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5819 | update_callback callback, void *data) |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5820 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5821 | PyTypeObject *subclass; |
| 5822 | PyObject *ref, *subclasses, *dict; |
| 5823 | Py_ssize_t i, n; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5824 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5825 | subclasses = type->tp_subclasses; |
| 5826 | if (subclasses == NULL) |
| 5827 | return 0; |
| 5828 | assert(PyList_Check(subclasses)); |
| 5829 | n = PyList_GET_SIZE(subclasses); |
| 5830 | for (i = 0; i < n; i++) { |
| 5831 | ref = PyList_GET_ITEM(subclasses, i); |
| 5832 | assert(PyWeakref_CheckRef(ref)); |
| 5833 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 5834 | assert(subclass != NULL); |
| 5835 | if ((PyObject *)subclass == Py_None) |
| 5836 | continue; |
| 5837 | assert(PyType_Check(subclass)); |
| 5838 | /* Avoid recursing down into unaffected classes */ |
| 5839 | dict = subclass->tp_dict; |
| 5840 | if (dict != NULL && PyDict_Check(dict) && |
| 5841 | PyDict_GetItem(dict, name) != NULL) |
| 5842 | continue; |
| 5843 | if (update_subclasses(subclass, name, callback, data) < 0) |
| 5844 | return -1; |
| 5845 | } |
| 5846 | return 0; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5847 | } |
| 5848 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5849 | /* This function is called by PyType_Ready() to populate the type's |
| 5850 | dictionary with method descriptors for function slots. For each |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 5851 | function slot (like tp_repr) that's defined in the type, one or more |
| 5852 | corresponding descriptors are added in the type's tp_dict dictionary |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5853 | under the appropriate name (like __repr__). Some function slots |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 5854 | cause more than one descriptor to be added (for example, the nb_add |
| 5855 | slot adds both __add__ and __radd__ descriptors) and some function |
| 5856 | slots compete for the same descriptor (for example both sq_item and |
| 5857 | mp_subscript generate a __getitem__ descriptor). |
| 5858 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5859 | In the latter case, the first slotdef entry encoutered wins. Since |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5860 | 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] | 5861 | PyHeapTypeObject, this gives us some control over disambiguating |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5862 | between competing slots: the members of PyHeapTypeObject are listed |
| 5863 | from most general to least general, so the most general slot is |
| 5864 | preferred. In particular, because as_mapping comes before as_sequence, |
| 5865 | for a type that defines both mp_subscript and sq_item, mp_subscript |
| 5866 | wins. |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 5867 | |
| 5868 | This only adds new descriptors and doesn't overwrite entries in |
| 5869 | tp_dict that were previously defined. The descriptors contain a |
| 5870 | reference to the C function they must call, so that it's safe if they |
| 5871 | are copied into a subtype's __dict__ and the subtype has a different |
| 5872 | C function in its slot -- calling the method defined by the |
| 5873 | descriptor will call the C function that was used to create it, |
| 5874 | rather than the C function present in the slot when it is called. |
| 5875 | (This is important because a subtype may have a C function in the |
| 5876 | slot that calls the method from the dictionary, and we want to avoid |
| 5877 | infinite recursion here.) */ |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5878 | |
| 5879 | static int |
| 5880 | add_operators(PyTypeObject *type) |
| 5881 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5882 | PyObject *dict = type->tp_dict; |
| 5883 | slotdef *p; |
| 5884 | PyObject *descr; |
| 5885 | void **ptr; |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5887 | init_slotdefs(); |
| 5888 | for (p = slotdefs; p->name; p++) { |
| 5889 | if (p->wrapper == NULL) |
| 5890 | continue; |
| 5891 | ptr = slotptr(type, p->offset); |
| 5892 | if (!ptr || !*ptr) |
| 5893 | continue; |
| 5894 | if (PyDict_GetItem(dict, p->name_strobj)) |
| 5895 | continue; |
| 5896 | if (*ptr == PyObject_HashNotImplemented) { |
| 5897 | /* Classes may prevent the inheritance of the tp_hash |
| 5898 | slot by storing PyObject_HashNotImplemented in it. Make it |
| 5899 | visible as a None value for the __hash__ attribute. */ |
| 5900 | if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0) |
| 5901 | return -1; |
| 5902 | } |
| 5903 | else { |
| 5904 | descr = PyDescr_NewWrapper(type, p, *ptr); |
| 5905 | if (descr == NULL) |
| 5906 | return -1; |
| 5907 | if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) |
| 5908 | return -1; |
| 5909 | Py_DECREF(descr); |
| 5910 | } |
| 5911 | } |
| 5912 | if (type->tp_new != NULL) { |
| 5913 | if (add_tp_new_wrapper(type) < 0) |
| 5914 | return -1; |
| 5915 | } |
| 5916 | return 0; |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5917 | } |
| 5918 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5919 | |
| 5920 | /* Cooperative 'super' */ |
| 5921 | |
| 5922 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5923 | PyObject_HEAD |
| 5924 | PyTypeObject *type; |
| 5925 | PyObject *obj; |
| 5926 | PyTypeObject *obj_type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5927 | } superobject; |
| 5928 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 5929 | static PyMemberDef super_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5930 | {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY, |
| 5931 | "the class invoking super()"}, |
| 5932 | {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY, |
| 5933 | "the instance invoking super(); may be None"}, |
| 5934 | {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY, |
| 5935 | "the type of the instance invoking super(); may be None"}, |
| 5936 | {0} |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 5937 | }; |
| 5938 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5939 | static void |
| 5940 | super_dealloc(PyObject *self) |
| 5941 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5942 | superobject *su = (superobject *)self; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5943 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5944 | _PyObject_GC_UNTRACK(self); |
| 5945 | Py_XDECREF(su->obj); |
| 5946 | Py_XDECREF(su->type); |
| 5947 | Py_XDECREF(su->obj_type); |
| 5948 | Py_TYPE(self)->tp_free(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5949 | } |
| 5950 | |
| 5951 | static PyObject * |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 5952 | super_repr(PyObject *self) |
| 5953 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5954 | superobject *su = (superobject *)self; |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 5955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5956 | if (su->obj_type) |
| 5957 | return PyUnicode_FromFormat( |
| 5958 | "<super: <class '%s'>, <%s object>>", |
| 5959 | su->type ? su->type->tp_name : "NULL", |
| 5960 | su->obj_type->tp_name); |
| 5961 | else |
| 5962 | return PyUnicode_FromFormat( |
| 5963 | "<super: <class '%s'>, NULL>", |
| 5964 | su->type ? su->type->tp_name : "NULL"); |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 5965 | } |
| 5966 | |
| 5967 | static PyObject * |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5968 | super_getattro(PyObject *self, PyObject *name) |
| 5969 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5970 | superobject *su = (superobject *)self; |
| 5971 | int skip = su->obj_type == NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5972 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5973 | if (!skip) { |
| 5974 | /* We want __class__ to return the class of the super object |
| 5975 | (i.e. super, or a subclass), not the class of su->obj. */ |
| 5976 | skip = (PyUnicode_Check(name) && |
| 5977 | PyUnicode_GET_SIZE(name) == 9 && |
| 5978 | PyUnicode_CompareWithASCIIString(name, "__class__") == 0); |
| 5979 | } |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 5980 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5981 | if (!skip) { |
| 5982 | PyObject *mro, *res, *tmp, *dict; |
| 5983 | PyTypeObject *starttype; |
| 5984 | descrgetfunc f; |
| 5985 | Py_ssize_t i, n; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5986 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5987 | starttype = su->obj_type; |
| 5988 | mro = starttype->tp_mro; |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 5989 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5990 | if (mro == NULL) |
| 5991 | n = 0; |
| 5992 | else { |
| 5993 | assert(PyTuple_Check(mro)); |
| 5994 | n = PyTuple_GET_SIZE(mro); |
| 5995 | } |
| 5996 | for (i = 0; i < n; i++) { |
| 5997 | if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) |
| 5998 | break; |
| 5999 | } |
| 6000 | i++; |
| 6001 | res = NULL; |
| 6002 | for (; i < n; i++) { |
| 6003 | tmp = PyTuple_GET_ITEM(mro, i); |
| 6004 | if (PyType_Check(tmp)) |
| 6005 | dict = ((PyTypeObject *)tmp)->tp_dict; |
| 6006 | else |
| 6007 | continue; |
| 6008 | res = PyDict_GetItem(dict, name); |
| 6009 | if (res != NULL) { |
| 6010 | Py_INCREF(res); |
| 6011 | f = Py_TYPE(res)->tp_descr_get; |
| 6012 | if (f != NULL) { |
| 6013 | tmp = f(res, |
| 6014 | /* Only pass 'obj' param if |
| 6015 | this is instance-mode super |
| 6016 | (See SF ID #743627) |
| 6017 | */ |
| 6018 | (su->obj == (PyObject *) |
| 6019 | su->obj_type |
| 6020 | ? (PyObject *)NULL |
| 6021 | : su->obj), |
| 6022 | (PyObject *)starttype); |
| 6023 | Py_DECREF(res); |
| 6024 | res = tmp; |
| 6025 | } |
| 6026 | return res; |
| 6027 | } |
| 6028 | } |
| 6029 | } |
| 6030 | return PyObject_GenericGetAttr(self, name); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6031 | } |
| 6032 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6033 | static PyTypeObject * |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6034 | supercheck(PyTypeObject *type, PyObject *obj) |
| 6035 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6036 | /* Check that a super() call makes sense. Return a type object. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6037 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6038 | 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] | 6039 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6040 | - If it is a class, it must be a subclass of 'type'. This case is |
| 6041 | used for class methods; the return value is obj. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6042 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6043 | - If it is an instance, it must be an instance of 'type'. This is |
| 6044 | the normal case; the return value is obj.__class__. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6046 | But... when obj is an instance, we want to allow for the case where |
| 6047 | Py_TYPE(obj) is not a subclass of type, but obj.__class__ is! |
| 6048 | This will allow using super() with a proxy for obj. |
| 6049 | */ |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6050 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6051 | /* Check for first bullet above (special case) */ |
| 6052 | if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) { |
| 6053 | Py_INCREF(obj); |
| 6054 | return (PyTypeObject *)obj; |
| 6055 | } |
Guido van Rossum | 8e80a72 | 2003-02-18 19:22:22 +0000 | [diff] [blame] | 6056 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6057 | /* Normal case */ |
| 6058 | if (PyType_IsSubtype(Py_TYPE(obj), type)) { |
| 6059 | Py_INCREF(Py_TYPE(obj)); |
| 6060 | return Py_TYPE(obj); |
| 6061 | } |
| 6062 | else { |
| 6063 | /* Try the slow way */ |
| 6064 | static PyObject *class_str = NULL; |
| 6065 | PyObject *class_attr; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6066 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6067 | if (class_str == NULL) { |
| 6068 | class_str = PyUnicode_FromString("__class__"); |
| 6069 | if (class_str == NULL) |
| 6070 | return NULL; |
| 6071 | } |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6072 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6073 | class_attr = PyObject_GetAttr(obj, class_str); |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6075 | if (class_attr != NULL && |
| 6076 | PyType_Check(class_attr) && |
| 6077 | (PyTypeObject *)class_attr != Py_TYPE(obj)) |
| 6078 | { |
| 6079 | int ok = PyType_IsSubtype( |
| 6080 | (PyTypeObject *)class_attr, type); |
| 6081 | if (ok) |
| 6082 | return (PyTypeObject *)class_attr; |
| 6083 | } |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6084 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6085 | if (class_attr == NULL) |
| 6086 | PyErr_Clear(); |
| 6087 | else |
| 6088 | Py_DECREF(class_attr); |
| 6089 | } |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6090 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6091 | PyErr_SetString(PyExc_TypeError, |
| 6092 | "super(type, obj): " |
| 6093 | "obj must be an instance or subtype of type"); |
| 6094 | return NULL; |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6095 | } |
| 6096 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6097 | static PyObject * |
| 6098 | super_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 6099 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6100 | superobject *su = (superobject *)self; |
| 6101 | superobject *newobj; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6103 | if (obj == NULL || obj == Py_None || su->obj != NULL) { |
| 6104 | /* Not binding to an object, or already bound */ |
| 6105 | Py_INCREF(self); |
| 6106 | return self; |
| 6107 | } |
| 6108 | if (Py_TYPE(su) != &PySuper_Type) |
| 6109 | /* If su is an instance of a (strict) subclass of super, |
| 6110 | call its type */ |
| 6111 | return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su), |
| 6112 | su->type, obj, NULL); |
| 6113 | else { |
| 6114 | /* Inline the common case */ |
| 6115 | PyTypeObject *obj_type = supercheck(su->type, obj); |
| 6116 | if (obj_type == NULL) |
| 6117 | return NULL; |
| 6118 | newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type, |
| 6119 | NULL, NULL); |
| 6120 | if (newobj == NULL) |
| 6121 | return NULL; |
| 6122 | Py_INCREF(su->type); |
| 6123 | Py_INCREF(obj); |
| 6124 | newobj->type = su->type; |
| 6125 | newobj->obj = obj; |
| 6126 | newobj->obj_type = obj_type; |
| 6127 | return (PyObject *)newobj; |
| 6128 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6129 | } |
| 6130 | |
| 6131 | static int |
| 6132 | super_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 6133 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6134 | superobject *su = (superobject *)self; |
| 6135 | PyTypeObject *type = NULL; |
| 6136 | PyObject *obj = NULL; |
| 6137 | PyTypeObject *obj_type = NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6139 | if (!_PyArg_NoKeywords("super", kwds)) |
| 6140 | return -1; |
| 6141 | if (!PyArg_ParseTuple(args, "|O!O:super", &PyType_Type, &type, &obj)) |
| 6142 | return -1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6143 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6144 | if (type == NULL) { |
| 6145 | /* Call super(), without args -- fill in from __class__ |
| 6146 | and first local variable on the stack. */ |
| 6147 | PyFrameObject *f = PyThreadState_GET()->frame; |
| 6148 | PyCodeObject *co = f->f_code; |
| 6149 | int i, n; |
| 6150 | if (co == NULL) { |
| 6151 | PyErr_SetString(PyExc_SystemError, |
| 6152 | "super(): no code object"); |
| 6153 | return -1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6154 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6155 | if (co->co_argcount == 0) { |
| 6156 | PyErr_SetString(PyExc_SystemError, |
| 6157 | "super(): no arguments"); |
| 6158 | return -1; |
| 6159 | } |
| 6160 | obj = f->f_localsplus[0]; |
| 6161 | if (obj == NULL) { |
| 6162 | PyErr_SetString(PyExc_SystemError, |
| 6163 | "super(): arg[0] deleted"); |
| 6164 | return -1; |
| 6165 | } |
| 6166 | if (co->co_freevars == NULL) |
| 6167 | n = 0; |
| 6168 | else { |
| 6169 | assert(PyTuple_Check(co->co_freevars)); |
| 6170 | n = PyTuple_GET_SIZE(co->co_freevars); |
| 6171 | } |
| 6172 | for (i = 0; i < n; i++) { |
| 6173 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 6174 | assert(PyUnicode_Check(name)); |
| 6175 | if (!PyUnicode_CompareWithASCIIString(name, |
| 6176 | "__class__")) { |
| 6177 | Py_ssize_t index = co->co_nlocals + |
| 6178 | PyTuple_GET_SIZE(co->co_cellvars) + i; |
| 6179 | PyObject *cell = f->f_localsplus[index]; |
| 6180 | if (cell == NULL || !PyCell_Check(cell)) { |
| 6181 | PyErr_SetString(PyExc_SystemError, |
| 6182 | "super(): bad __class__ cell"); |
| 6183 | return -1; |
| 6184 | } |
| 6185 | type = (PyTypeObject *) PyCell_GET(cell); |
| 6186 | if (type == NULL) { |
| 6187 | PyErr_SetString(PyExc_SystemError, |
| 6188 | "super(): empty __class__ cell"); |
| 6189 | return -1; |
| 6190 | } |
| 6191 | if (!PyType_Check(type)) { |
| 6192 | PyErr_Format(PyExc_SystemError, |
| 6193 | "super(): __class__ is not a type (%s)", |
| 6194 | Py_TYPE(type)->tp_name); |
| 6195 | return -1; |
| 6196 | } |
| 6197 | break; |
| 6198 | } |
| 6199 | } |
| 6200 | if (type == NULL) { |
| 6201 | PyErr_SetString(PyExc_SystemError, |
| 6202 | "super(): __class__ cell not found"); |
| 6203 | return -1; |
| 6204 | } |
| 6205 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6207 | if (obj == Py_None) |
| 6208 | obj = NULL; |
| 6209 | if (obj != NULL) { |
| 6210 | obj_type = supercheck(type, obj); |
| 6211 | if (obj_type == NULL) |
| 6212 | return -1; |
| 6213 | Py_INCREF(obj); |
| 6214 | } |
| 6215 | Py_INCREF(type); |
| 6216 | su->type = type; |
| 6217 | su->obj = obj; |
| 6218 | su->obj_type = obj_type; |
| 6219 | return 0; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6220 | } |
| 6221 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6222 | PyDoc_STRVAR(super_doc, |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6223 | "super() -> same as super(__class__, <first argument>)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6224 | "super(type) -> unbound super object\n" |
| 6225 | "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6226 | "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6227 | "Typical use to call a cooperative superclass method:\n" |
| 6228 | "class C(B):\n" |
| 6229 | " def meth(self, arg):\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6230 | " super().meth(arg)\n" |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6231 | "This works for class methods too:\n" |
| 6232 | "class C(B):\n" |
| 6233 | " @classmethod\n" |
| 6234 | " def cmeth(cls, arg):\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6235 | " super().cmeth(arg)\n"); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6236 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6237 | static int |
| 6238 | super_traverse(PyObject *self, visitproc visit, void *arg) |
| 6239 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6240 | superobject *su = (superobject *)self; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6242 | Py_VISIT(su->obj); |
| 6243 | Py_VISIT(su->type); |
| 6244 | Py_VISIT(su->obj_type); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6245 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6246 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6247 | } |
| 6248 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6249 | PyTypeObject PySuper_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6250 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 6251 | "super", /* tp_name */ |
| 6252 | sizeof(superobject), /* tp_basicsize */ |
| 6253 | 0, /* tp_itemsize */ |
| 6254 | /* methods */ |
| 6255 | super_dealloc, /* tp_dealloc */ |
| 6256 | 0, /* tp_print */ |
| 6257 | 0, /* tp_getattr */ |
| 6258 | 0, /* tp_setattr */ |
| 6259 | 0, /* tp_reserved */ |
| 6260 | super_repr, /* tp_repr */ |
| 6261 | 0, /* tp_as_number */ |
| 6262 | 0, /* tp_as_sequence */ |
| 6263 | 0, /* tp_as_mapping */ |
| 6264 | 0, /* tp_hash */ |
| 6265 | 0, /* tp_call */ |
| 6266 | 0, /* tp_str */ |
| 6267 | super_getattro, /* tp_getattro */ |
| 6268 | 0, /* tp_setattro */ |
| 6269 | 0, /* tp_as_buffer */ |
| 6270 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 6271 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 6272 | super_doc, /* tp_doc */ |
| 6273 | super_traverse, /* tp_traverse */ |
| 6274 | 0, /* tp_clear */ |
| 6275 | 0, /* tp_richcompare */ |
| 6276 | 0, /* tp_weaklistoffset */ |
| 6277 | 0, /* tp_iter */ |
| 6278 | 0, /* tp_iternext */ |
| 6279 | 0, /* tp_methods */ |
| 6280 | super_members, /* tp_members */ |
| 6281 | 0, /* tp_getset */ |
| 6282 | 0, /* tp_base */ |
| 6283 | 0, /* tp_dict */ |
| 6284 | super_descr_get, /* tp_descr_get */ |
| 6285 | 0, /* tp_descr_set */ |
| 6286 | 0, /* tp_dictoffset */ |
| 6287 | super_init, /* tp_init */ |
| 6288 | PyType_GenericAlloc, /* tp_alloc */ |
| 6289 | PyType_GenericNew, /* tp_new */ |
| 6290 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6291 | }; |