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[] = { |
Benjamin Peterson | 0e10206 | 2010-08-25 23:13:17 +0000 | [diff] [blame] | 192 | {"__basicsize__", T_PYSSIZET, offsetof(PyTypeObject,tp_basicsize),READONLY}, |
| 193 | {"__itemsize__", T_PYSSIZET, offsetof(PyTypeObject, tp_itemsize), READONLY}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 | { |
Benjamin Peterson | aec5fd1 | 2010-10-02 00:03:31 +0000 | [diff] [blame] | 323 | PyObject *mod = NULL; |
Benjamin Peterson | 84060b8 | 2010-10-03 02:13:39 +0000 | [diff] [blame] | 324 | /* type itself has an __abstractmethods__ descriptor (this). Don't return |
| 325 | that. */ |
Benjamin Peterson | aec5fd1 | 2010-10-02 00:03:31 +0000 | [diff] [blame] | 326 | if (type != &PyType_Type) |
| 327 | mod = PyDict_GetItemString(type->tp_dict, "__abstractmethods__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 328 | if (!mod) { |
| 329 | PyErr_Format(PyExc_AttributeError, "__abstractmethods__"); |
| 330 | return NULL; |
| 331 | } |
| 332 | Py_XINCREF(mod); |
| 333 | return mod; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | static int |
| 337 | type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context) |
| 338 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 339 | /* __abstractmethods__ should only be set once on a type, in |
| 340 | abc.ABCMeta.__new__, so this function doesn't do anything |
| 341 | special to update subclasses. |
| 342 | */ |
| 343 | int res = PyDict_SetItemString(type->tp_dict, |
| 344 | "__abstractmethods__", value); |
| 345 | if (res == 0) { |
| 346 | PyType_Modified(type); |
| 347 | if (value && PyObject_IsTrue(value)) { |
| 348 | type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT; |
| 349 | } |
| 350 | else { |
| 351 | type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT; |
| 352 | } |
| 353 | } |
| 354 | return res; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | static PyObject * |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 358 | type_get_bases(PyTypeObject *type, void *context) |
| 359 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 360 | Py_INCREF(type->tp_bases); |
| 361 | return type->tp_bases; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | static PyTypeObject *best_base(PyObject *); |
| 365 | static int mro_internal(PyTypeObject *); |
| 366 | static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *); |
| 367 | static int add_subclass(PyTypeObject*, PyTypeObject*); |
| 368 | static void remove_subclass(PyTypeObject *, PyTypeObject *); |
| 369 | static void update_all_slots(PyTypeObject *); |
| 370 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 371 | typedef int (*update_callback)(PyTypeObject *, void *); |
| 372 | static int update_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 373 | update_callback callback, void *data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 374 | static int recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 375 | update_callback callback, void *data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 376 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 377 | static int |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 378 | mro_subclasses(PyTypeObject *type, PyObject* temp) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 379 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | PyTypeObject *subclass; |
| 381 | PyObject *ref, *subclasses, *old_mro; |
| 382 | Py_ssize_t i, n; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 384 | subclasses = type->tp_subclasses; |
| 385 | if (subclasses == NULL) |
| 386 | return 0; |
| 387 | assert(PyList_Check(subclasses)); |
| 388 | n = PyList_GET_SIZE(subclasses); |
| 389 | for (i = 0; i < n; i++) { |
| 390 | ref = PyList_GET_ITEM(subclasses, i); |
| 391 | assert(PyWeakref_CheckRef(ref)); |
| 392 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 393 | assert(subclass != NULL); |
| 394 | if ((PyObject *)subclass == Py_None) |
| 395 | continue; |
| 396 | assert(PyType_Check(subclass)); |
| 397 | old_mro = subclass->tp_mro; |
| 398 | if (mro_internal(subclass) < 0) { |
| 399 | subclass->tp_mro = old_mro; |
| 400 | return -1; |
| 401 | } |
| 402 | else { |
| 403 | PyObject* tuple; |
| 404 | tuple = PyTuple_Pack(2, subclass, old_mro); |
| 405 | Py_DECREF(old_mro); |
| 406 | if (!tuple) |
| 407 | return -1; |
| 408 | if (PyList_Append(temp, tuple) < 0) |
| 409 | return -1; |
| 410 | Py_DECREF(tuple); |
| 411 | } |
| 412 | if (mro_subclasses(subclass, temp) < 0) |
| 413 | return -1; |
| 414 | } |
| 415 | return 0; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static int |
| 419 | type_set_bases(PyTypeObject *type, PyObject *value, void *context) |
| 420 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 421 | Py_ssize_t i; |
| 422 | int r = 0; |
| 423 | PyObject *ob, *temp; |
| 424 | PyTypeObject *new_base, *old_base; |
| 425 | PyObject *old_bases, *old_mro; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 426 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 427 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 428 | PyErr_Format(PyExc_TypeError, |
| 429 | "can't set %s.__bases__", type->tp_name); |
| 430 | return -1; |
| 431 | } |
| 432 | if (!value) { |
| 433 | PyErr_Format(PyExc_TypeError, |
| 434 | "can't delete %s.__bases__", type->tp_name); |
| 435 | return -1; |
| 436 | } |
| 437 | if (!PyTuple_Check(value)) { |
| 438 | PyErr_Format(PyExc_TypeError, |
| 439 | "can only assign tuple to %s.__bases__, not %s", |
| 440 | type->tp_name, Py_TYPE(value)->tp_name); |
| 441 | return -1; |
| 442 | } |
| 443 | if (PyTuple_GET_SIZE(value) == 0) { |
| 444 | PyErr_Format(PyExc_TypeError, |
| 445 | "can only assign non-empty tuple to %s.__bases__, not ()", |
| 446 | type->tp_name); |
| 447 | return -1; |
| 448 | } |
| 449 | for (i = 0; i < PyTuple_GET_SIZE(value); i++) { |
| 450 | ob = PyTuple_GET_ITEM(value, i); |
| 451 | if (!PyType_Check(ob)) { |
| 452 | PyErr_Format( |
| 453 | PyExc_TypeError, |
| 454 | "%s.__bases__ must be tuple of old- or new-style classes, not '%s'", |
| 455 | type->tp_name, Py_TYPE(ob)->tp_name); |
| 456 | return -1; |
| 457 | } |
| 458 | if (PyType_Check(ob)) { |
| 459 | if (PyType_IsSubtype((PyTypeObject*)ob, type)) { |
| 460 | PyErr_SetString(PyExc_TypeError, |
| 461 | "a __bases__ item causes an inheritance cycle"); |
| 462 | return -1; |
| 463 | } |
| 464 | } |
| 465 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 466 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 467 | new_base = best_base(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 469 | if (!new_base) { |
| 470 | return -1; |
| 471 | } |
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 | if (!compatible_for_assignment(type->tp_base, new_base, "__bases__")) |
| 474 | return -1; |
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 | Py_INCREF(new_base); |
| 477 | Py_INCREF(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 478 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 479 | old_bases = type->tp_bases; |
| 480 | old_base = type->tp_base; |
| 481 | old_mro = type->tp_mro; |
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 | type->tp_bases = value; |
| 484 | type->tp_base = new_base; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 485 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 486 | if (mro_internal(type) < 0) { |
| 487 | goto bail; |
| 488 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 489 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | temp = PyList_New(0); |
| 491 | if (!temp) |
| 492 | goto bail; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | r = mro_subclasses(type, temp); |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | if (r < 0) { |
| 497 | for (i = 0; i < PyList_Size(temp); i++) { |
| 498 | PyTypeObject* cls; |
| 499 | PyObject* mro; |
| 500 | PyArg_UnpackTuple(PyList_GET_ITEM(temp, i), |
| 501 | "", 2, 2, &cls, &mro); |
| 502 | Py_INCREF(mro); |
| 503 | ob = cls->tp_mro; |
| 504 | cls->tp_mro = mro; |
| 505 | Py_DECREF(ob); |
| 506 | } |
| 507 | Py_DECREF(temp); |
| 508 | goto bail; |
| 509 | } |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 511 | Py_DECREF(temp); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 512 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 513 | /* any base that was in __bases__ but now isn't, we |
| 514 | need to remove |type| from its tp_subclasses. |
| 515 | conversely, any class now in __bases__ that wasn't |
| 516 | needs to have |type| added to its subclasses. */ |
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 now, sod that: just remove from all old_bases, |
| 519 | add to all new_bases */ |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) { |
| 522 | ob = PyTuple_GET_ITEM(old_bases, i); |
| 523 | if (PyType_Check(ob)) { |
| 524 | remove_subclass( |
| 525 | (PyTypeObject*)ob, type); |
| 526 | } |
| 527 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 528 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 529 | for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) { |
| 530 | ob = PyTuple_GET_ITEM(value, i); |
| 531 | if (PyType_Check(ob)) { |
| 532 | if (add_subclass((PyTypeObject*)ob, type) < 0) |
| 533 | r = -1; |
| 534 | } |
| 535 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 536 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 537 | update_all_slots(type); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 538 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 | Py_DECREF(old_bases); |
| 540 | Py_DECREF(old_base); |
| 541 | Py_DECREF(old_mro); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 542 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 543 | return r; |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 544 | |
| 545 | bail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 546 | Py_DECREF(type->tp_bases); |
| 547 | Py_DECREF(type->tp_base); |
| 548 | if (type->tp_mro != old_mro) { |
| 549 | Py_DECREF(type->tp_mro); |
| 550 | } |
Michael W. Hudson | e723e45 | 2003-08-07 14:58:10 +0000 | [diff] [blame] | 551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 552 | type->tp_bases = old_bases; |
| 553 | type->tp_base = old_base; |
| 554 | type->tp_mro = old_mro; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 555 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 556 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 560 | type_dict(PyTypeObject *type, void *context) |
| 561 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | if (type->tp_dict == NULL) { |
| 563 | Py_INCREF(Py_None); |
| 564 | return Py_None; |
| 565 | } |
| 566 | return PyDictProxy_New(type->tp_dict); |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 569 | static PyObject * |
| 570 | type_get_doc(PyTypeObject *type, void *context) |
| 571 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 572 | PyObject *result; |
| 573 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) |
| 574 | return PyUnicode_FromString(type->tp_doc); |
| 575 | result = PyDict_GetItemString(type->tp_dict, "__doc__"); |
| 576 | if (result == NULL) { |
| 577 | result = Py_None; |
| 578 | Py_INCREF(result); |
| 579 | } |
| 580 | else if (Py_TYPE(result)->tp_descr_get) { |
| 581 | result = Py_TYPE(result)->tp_descr_get(result, NULL, |
| 582 | (PyObject *)type); |
| 583 | } |
| 584 | else { |
| 585 | Py_INCREF(result); |
| 586 | } |
| 587 | return result; |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 590 | static PyObject * |
| 591 | type___instancecheck__(PyObject *type, PyObject *inst) |
| 592 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 593 | switch (_PyObject_RealIsInstance(inst, type)) { |
| 594 | case -1: |
| 595 | return NULL; |
| 596 | case 0: |
| 597 | Py_RETURN_FALSE; |
| 598 | default: |
| 599 | Py_RETURN_TRUE; |
| 600 | } |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | |
| 604 | static PyObject * |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 605 | type___subclasscheck__(PyObject *type, PyObject *inst) |
| 606 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 607 | switch (_PyObject_RealIsSubclass(inst, type)) { |
| 608 | case -1: |
| 609 | return NULL; |
| 610 | case 0: |
| 611 | Py_RETURN_FALSE; |
| 612 | default: |
| 613 | Py_RETURN_TRUE; |
| 614 | } |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 617 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 618 | static PyGetSetDef type_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | {"__name__", (getter)type_name, (setter)type_set_name, NULL}, |
| 620 | {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL}, |
| 621 | {"__module__", (getter)type_module, (setter)type_set_module, NULL}, |
| 622 | {"__abstractmethods__", (getter)type_abstractmethods, |
| 623 | (setter)type_set_abstractmethods, NULL}, |
| 624 | {"__dict__", (getter)type_dict, NULL, NULL}, |
| 625 | {"__doc__", (getter)type_get_doc, NULL, NULL}, |
| 626 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 627 | }; |
| 628 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 629 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 630 | type_repr(PyTypeObject *type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 631 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | PyObject *mod, *name, *rtn; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 633 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 634 | mod = type_module(type, NULL); |
| 635 | if (mod == NULL) |
| 636 | PyErr_Clear(); |
| 637 | else if (!PyUnicode_Check(mod)) { |
| 638 | Py_DECREF(mod); |
| 639 | mod = NULL; |
| 640 | } |
| 641 | name = type_name(type, NULL); |
| 642 | if (name == NULL) |
| 643 | return NULL; |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 645 | if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins")) |
| 646 | rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name); |
| 647 | else |
| 648 | rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 649 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 650 | Py_XDECREF(mod); |
| 651 | Py_DECREF(name); |
| 652 | return rtn; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 655 | static PyObject * |
| 656 | type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 657 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | PyObject *obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 659 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 660 | if (type->tp_new == NULL) { |
| 661 | PyErr_Format(PyExc_TypeError, |
| 662 | "cannot create '%.100s' instances", |
| 663 | type->tp_name); |
| 664 | return NULL; |
| 665 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 666 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 667 | obj = type->tp_new(type, args, kwds); |
| 668 | if (obj != NULL) { |
| 669 | /* Ugly exception: when the call was type(something), |
| 670 | don't call tp_init on the result. */ |
| 671 | if (type == &PyType_Type && |
| 672 | PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && |
| 673 | (kwds == NULL || |
| 674 | (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) |
| 675 | return obj; |
| 676 | /* If the returned object is not an instance of type, |
| 677 | it won't be initialized. */ |
| 678 | if (!PyType_IsSubtype(Py_TYPE(obj), type)) |
| 679 | return obj; |
| 680 | type = Py_TYPE(obj); |
| 681 | if (type->tp_init != NULL && |
| 682 | type->tp_init(obj, args, kwds) < 0) { |
| 683 | Py_DECREF(obj); |
| 684 | obj = NULL; |
| 685 | } |
| 686 | } |
| 687 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 691 | PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 692 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 693 | PyObject *obj; |
| 694 | const size_t size = _PyObject_VAR_SIZE(type, nitems+1); |
| 695 | /* note that we need to add one, for the sentinel */ |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 697 | if (PyType_IS_GC(type)) |
| 698 | obj = _PyObject_GC_Malloc(size); |
| 699 | else |
| 700 | obj = (PyObject *)PyObject_MALLOC(size); |
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 | if (obj == NULL) |
| 703 | return PyErr_NoMemory(); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 704 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | memset(obj, '\0', size); |
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_flags & Py_TPFLAGS_HEAPTYPE) |
| 708 | Py_INCREF(type); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 709 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 710 | if (type->tp_itemsize == 0) |
| 711 | PyObject_INIT(obj, type); |
| 712 | else |
| 713 | (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 714 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 715 | if (PyType_IS_GC(type)) |
| 716 | _PyObject_GC_TRACK(obj); |
| 717 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | PyObject * |
| 721 | PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 722 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 723 | return type->tp_alloc(type, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 726 | /* Helpers for subtyping */ |
| 727 | |
| 728 | static int |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 729 | traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg) |
| 730 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 731 | Py_ssize_t i, n; |
| 732 | PyMemberDef *mp; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 733 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 734 | n = Py_SIZE(type); |
| 735 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
| 736 | for (i = 0; i < n; i++, mp++) { |
| 737 | if (mp->type == T_OBJECT_EX) { |
| 738 | char *addr = (char *)self + mp->offset; |
| 739 | PyObject *obj = *(PyObject **)addr; |
| 740 | if (obj != NULL) { |
| 741 | int err = visit(obj, arg); |
| 742 | if (err) |
| 743 | return err; |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | return 0; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | static int |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 751 | subtype_traverse(PyObject *self, visitproc visit, void *arg) |
| 752 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 753 | PyTypeObject *type, *base; |
| 754 | traverseproc basetraverse; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 756 | /* Find the nearest base with a different tp_traverse, |
| 757 | and traverse slots while we're at it */ |
| 758 | type = Py_TYPE(self); |
| 759 | base = type; |
| 760 | while ((basetraverse = base->tp_traverse) == subtype_traverse) { |
| 761 | if (Py_SIZE(base)) { |
| 762 | int err = traverse_slots(base, self, visit, arg); |
| 763 | if (err) |
| 764 | return err; |
| 765 | } |
| 766 | base = base->tp_base; |
| 767 | assert(base); |
| 768 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 769 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 770 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 771 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 772 | if (dictptr && *dictptr) |
| 773 | Py_VISIT(*dictptr); |
| 774 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 775 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 776 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 777 | /* For a heaptype, the instances count as references |
| 778 | to the type. Traverse the type so the collector |
| 779 | can find cycles involving this link. */ |
| 780 | Py_VISIT(type); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 781 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 782 | if (basetraverse) |
| 783 | return basetraverse(self, visit, arg); |
| 784 | return 0; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | static void |
| 788 | clear_slots(PyTypeObject *type, PyObject *self) |
| 789 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 790 | Py_ssize_t i, n; |
| 791 | PyMemberDef *mp; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 793 | n = Py_SIZE(type); |
| 794 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
| 795 | for (i = 0; i < n; i++, mp++) { |
| 796 | if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) { |
| 797 | char *addr = (char *)self + mp->offset; |
| 798 | PyObject *obj = *(PyObject **)addr; |
| 799 | if (obj != NULL) { |
| 800 | *(PyObject **)addr = NULL; |
| 801 | Py_DECREF(obj); |
| 802 | } |
| 803 | } |
| 804 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | static int |
| 808 | subtype_clear(PyObject *self) |
| 809 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 810 | PyTypeObject *type, *base; |
| 811 | inquiry baseclear; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 812 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 813 | /* Find the nearest base with a different tp_clear |
| 814 | and clear slots while we're at it */ |
| 815 | type = Py_TYPE(self); |
| 816 | base = type; |
| 817 | while ((baseclear = base->tp_clear) == subtype_clear) { |
| 818 | if (Py_SIZE(base)) |
| 819 | clear_slots(base, self); |
| 820 | base = base->tp_base; |
| 821 | assert(base); |
| 822 | } |
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 | /* There's no need to clear the instance dict (if any); |
| 825 | the collector will call its tp_clear handler. */ |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 826 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 827 | if (baseclear) |
| 828 | return baseclear(self); |
| 829 | return 0; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 830 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 831 | |
| 832 | static void |
| 833 | subtype_dealloc(PyObject *self) |
| 834 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 835 | PyTypeObject *type, *base; |
| 836 | destructor basedealloc; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 837 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 838 | /* Extract the type; we expect it to be a heap type */ |
| 839 | type = Py_TYPE(self); |
| 840 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 841 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | /* Test whether the type has GC exactly once */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 843 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 844 | if (!PyType_IS_GC(type)) { |
| 845 | /* It's really rare to find a dynamic type that doesn't have |
| 846 | GC; it can only happen when deriving from 'object' and not |
| 847 | adding any slots or instance variables. This allows |
| 848 | certain simplifications: there's no need to call |
| 849 | clear_slots(), or DECREF the dict, or clear weakrefs. */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 850 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 851 | /* Maybe call finalizer; exit early if resurrected */ |
| 852 | if (type->tp_del) { |
| 853 | type->tp_del(self); |
| 854 | if (self->ob_refcnt > 0) |
| 855 | return; |
| 856 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 857 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 858 | /* Find the nearest base with a different tp_dealloc */ |
| 859 | base = type; |
| 860 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 861 | assert(Py_SIZE(base) == 0); |
| 862 | base = base->tp_base; |
| 863 | assert(base); |
| 864 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 865 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 866 | /* Extract the type again; tp_del may have changed it */ |
| 867 | type = Py_TYPE(self); |
Benjamin Peterson | 193152c | 2009-04-25 01:08:45 +0000 | [diff] [blame] | 868 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 869 | /* Call the base tp_dealloc() */ |
| 870 | assert(basedealloc); |
| 871 | basedealloc(self); |
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 | /* Can't reference self beyond this point */ |
| 874 | Py_DECREF(type); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | /* Done */ |
| 877 | return; |
| 878 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 880 | /* We get here only if the type has GC */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 882 | /* UnTrack and re-Track around the trashcan macro, alas */ |
| 883 | /* See explanation at end of function for full disclosure */ |
| 884 | PyObject_GC_UnTrack(self); |
| 885 | ++_PyTrash_delete_nesting; |
| 886 | Py_TRASHCAN_SAFE_BEGIN(self); |
| 887 | --_PyTrash_delete_nesting; |
| 888 | /* DO NOT restore GC tracking at this point. weakref callbacks |
| 889 | * (if any, and whether directly here or indirectly in something we |
| 890 | * call) may trigger GC, and if self is tracked at that point, it |
| 891 | * will look like trash to GC and GC will try to delete self again. |
| 892 | */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 893 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 894 | /* Find the nearest base with a different tp_dealloc */ |
| 895 | base = type; |
| 896 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 897 | base = base->tp_base; |
| 898 | assert(base); |
| 899 | } |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 900 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 901 | /* If we added a weaklist, we clear it. Do this *before* calling |
| 902 | the finalizer (__del__), clearing slots, or clearing the instance |
| 903 | dict. */ |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 904 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 905 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) |
| 906 | PyObject_ClearWeakRefs(self); |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 907 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 | /* Maybe call finalizer; exit early if resurrected */ |
| 909 | if (type->tp_del) { |
| 910 | _PyObject_GC_TRACK(self); |
| 911 | type->tp_del(self); |
| 912 | if (self->ob_refcnt > 0) |
| 913 | goto endlabel; /* resurrected */ |
| 914 | else |
| 915 | _PyObject_GC_UNTRACK(self); |
| 916 | /* New weakrefs could be created during the finalizer call. |
| 917 | If this occurs, clear them out without calling their |
| 918 | finalizers since they might rely on part of the object |
| 919 | being finalized that has already been destroyed. */ |
| 920 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) { |
| 921 | /* Modeled after GET_WEAKREFS_LISTPTR() */ |
| 922 | PyWeakReference **list = (PyWeakReference **) \ |
| 923 | PyObject_GET_WEAKREFS_LISTPTR(self); |
| 924 | while (*list) |
| 925 | _PyWeakref_ClearRef(*list); |
| 926 | } |
| 927 | } |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 928 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | /* Clear slots up to the nearest base with a different tp_dealloc */ |
| 930 | base = type; |
| 931 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 932 | if (Py_SIZE(base)) |
| 933 | clear_slots(base, self); |
| 934 | base = base->tp_base; |
| 935 | assert(base); |
| 936 | } |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 938 | /* If we added a dict, DECREF it */ |
| 939 | if (type->tp_dictoffset && !base->tp_dictoffset) { |
| 940 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 941 | if (dictptr != NULL) { |
| 942 | PyObject *dict = *dictptr; |
| 943 | if (dict != NULL) { |
| 944 | Py_DECREF(dict); |
| 945 | *dictptr = NULL; |
| 946 | } |
| 947 | } |
| 948 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 949 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 950 | /* Extract the type again; tp_del may have changed it */ |
| 951 | type = Py_TYPE(self); |
Benjamin Peterson | 193152c | 2009-04-25 01:08:45 +0000 | [diff] [blame] | 952 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 953 | /* Call the base tp_dealloc(); first retrack self if |
| 954 | * basedealloc knows about gc. |
| 955 | */ |
| 956 | if (PyType_IS_GC(base)) |
| 957 | _PyObject_GC_TRACK(self); |
| 958 | assert(basedealloc); |
| 959 | basedealloc(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 961 | /* Can't reference self beyond this point */ |
| 962 | Py_DECREF(type); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 963 | |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 964 | endlabel: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 965 | ++_PyTrash_delete_nesting; |
| 966 | Py_TRASHCAN_SAFE_END(self); |
| 967 | --_PyTrash_delete_nesting; |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 968 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 969 | /* Explanation of the weirdness around the trashcan macros: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | Q. What do the trashcan macros do? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 972 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 973 | A. Read the comment titled "Trashcan mechanism" in object.h. |
| 974 | For one, this explains why there must be a call to GC-untrack |
| 975 | before the trashcan begin macro. Without understanding the |
| 976 | trashcan code, the answers to the following questions don't make |
| 977 | sense. |
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 | Q. Why do we GC-untrack before the trashcan and then immediately |
| 980 | GC-track again afterward? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 981 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 982 | A. In the case that the base class is GC-aware, the base class |
| 983 | probably GC-untracks the object. If it does that using the |
| 984 | UNTRACK macro, this will crash when the object is already |
| 985 | untracked. Because we don't know what the base class does, the |
| 986 | only safe thing is to make sure the object is tracked when we |
| 987 | call the base class dealloc. But... The trashcan begin macro |
| 988 | requires that the object is *untracked* before it is called. So |
| 989 | the dance becomes: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 990 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 991 | GC untrack |
| 992 | trashcan begin |
| 993 | GC track |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 994 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 995 | Q. Why did the last question say "immediately GC-track again"? |
| 996 | It's nowhere near immediately. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 997 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 998 | A. Because the code *used* to re-track immediately. Bad Idea. |
| 999 | self has a refcount of 0, and if gc ever gets its hands on it |
| 1000 | (which can happen if any weakref callback gets invoked), it |
| 1001 | looks like trash to gc too, and gc also tries to delete self |
| 1002 | then. But we're already deleting self. Double dealloction is |
| 1003 | a subtle disaster. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 1004 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1005 | Q. Why the bizarre (net-zero) manipulation of |
| 1006 | _PyTrash_delete_nesting around the trashcan macros? |
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 | A. Some base classes (e.g. list) also use the trashcan mechanism. |
| 1009 | The following scenario used to be possible: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1010 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | - suppose the trashcan level is one below the trashcan limit |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1013 | - subtype_dealloc() is called |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1014 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1015 | - the trashcan limit is not yet reached, so the trashcan level |
| 1016 | is incremented and the code between trashcan begin and end is |
| 1017 | executed |
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 | - this destroys much of the object's contents, including its |
| 1020 | slots and __dict__ |
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 | - basedealloc() is called; this is really list_dealloc(), or |
| 1023 | some other type which also uses the trashcan macros |
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 | - the trashcan limit is now reached, so the object is put on the |
| 1026 | trashcan's to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1027 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | - basedealloc() returns |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1029 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1030 | - subtype_dealloc() decrefs the object's type |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1032 | - subtype_dealloc() returns |
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 | - later, the trashcan code starts deleting the objects from its |
| 1035 | to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1036 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1037 | - subtype_dealloc() is called *AGAIN* for the same object |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1038 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1039 | - at the very least (if the destroyed slots and __dict__ don't |
| 1040 | cause problems) the object's type gets decref'ed a second |
| 1041 | time, which is *BAD*!!! |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1042 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1043 | The remedy is to make sure that if the code between trashcan |
| 1044 | begin and end in subtype_dealloc() is called, the code between |
| 1045 | trashcan begin and end in basedealloc() will also be called. |
| 1046 | This is done by decrementing the level after passing into the |
| 1047 | trashcan block, and incrementing it just before leaving the |
| 1048 | block. |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1049 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1050 | But now it's possible that a chain of objects consisting solely |
| 1051 | of objects whose deallocator is subtype_dealloc() will defeat |
| 1052 | the trashcan mechanism completely: the decremented level means |
| 1053 | that the effective level never reaches the limit. Therefore, we |
| 1054 | *increment* the level *before* entering the trashcan block, and |
| 1055 | matchingly decrement it after leaving. This means the trashcan |
| 1056 | 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] | 1057 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1058 | Q. Are there any live examples of code in need of all this |
| 1059 | complexity? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1060 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1061 | A. Yes. See SF bug 668433 for code that crashed (when Python was |
| 1062 | compiled in debug mode) before the trashcan level manipulations |
| 1063 | were added. For more discussion, see SF patches 581742, 575073 |
| 1064 | and bug 574207. |
| 1065 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1068 | static PyTypeObject *solid_base(PyTypeObject *type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1069 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1070 | /* type test with subclassing support */ |
| 1071 | |
| 1072 | int |
| 1073 | PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
| 1074 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | PyObject *mro; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1076 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | mro = a->tp_mro; |
| 1078 | if (mro != NULL) { |
| 1079 | /* Deal with multiple inheritance without recursion |
| 1080 | by walking the MRO tuple */ |
| 1081 | Py_ssize_t i, n; |
| 1082 | assert(PyTuple_Check(mro)); |
| 1083 | n = PyTuple_GET_SIZE(mro); |
| 1084 | for (i = 0; i < n; i++) { |
| 1085 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) |
| 1086 | return 1; |
| 1087 | } |
| 1088 | return 0; |
| 1089 | } |
| 1090 | else { |
| 1091 | /* a is not completely initilized yet; follow tp_base */ |
| 1092 | do { |
| 1093 | if (a == b) |
| 1094 | return 1; |
| 1095 | a = a->tp_base; |
| 1096 | } while (a != NULL); |
| 1097 | return b == &PyBaseObject_Type; |
| 1098 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1101 | /* Internal routines to do a method lookup in the type |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1102 | without looking in the instance dictionary |
| 1103 | (so we can't use PyObject_GetAttr) but still binding |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1104 | it to the instance. The arguments are the object, |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1105 | 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] | 1106 | static variable used to cache the interned Python string. |
| 1107 | |
| 1108 | Two variants: |
| 1109 | |
| 1110 | - lookup_maybe() returns NULL without raising an exception |
| 1111 | when the _PyType_Lookup() call fails; |
| 1112 | |
| 1113 | - lookup_method() always raises an exception upon errors. |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1114 | |
| 1115 | - _PyObject_LookupSpecial() exported for the benefit of other places. |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1116 | */ |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1117 | |
| 1118 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1119 | lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1120 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1121 | PyObject *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1123 | if (*attrobj == NULL) { |
| 1124 | *attrobj = PyUnicode_InternFromString(attrstr); |
| 1125 | if (*attrobj == NULL) |
| 1126 | return NULL; |
| 1127 | } |
| 1128 | res = _PyType_Lookup(Py_TYPE(self), *attrobj); |
| 1129 | if (res != NULL) { |
| 1130 | descrgetfunc f; |
| 1131 | if ((f = Py_TYPE(res)->tp_descr_get) == NULL) |
| 1132 | Py_INCREF(res); |
| 1133 | else |
| 1134 | res = f(res, self, (PyObject *)(Py_TYPE(self))); |
| 1135 | } |
| 1136 | return res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1139 | static PyObject * |
| 1140 | lookup_method(PyObject *self, char *attrstr, PyObject **attrobj) |
| 1141 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1142 | PyObject *res = lookup_maybe(self, attrstr, attrobj); |
| 1143 | if (res == NULL && !PyErr_Occurred()) |
| 1144 | PyErr_SetObject(PyExc_AttributeError, *attrobj); |
| 1145 | return res; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1148 | PyObject * |
| 1149 | _PyObject_LookupSpecial(PyObject *self, char *attrstr, PyObject **attrobj) |
| 1150 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1151 | return lookup_maybe(self, attrstr, attrobj); |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1154 | /* A variation of PyObject_CallMethod that uses lookup_method() |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | instead of PyObject_GetAttrString(). This uses the same convention |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1156 | as lookup_method to cache the interned name string object. */ |
| 1157 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1158 | static PyObject * |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1159 | call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 1160 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | va_list va; |
| 1162 | PyObject *args, *func = 0, *retval; |
| 1163 | va_start(va, format); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1165 | func = lookup_maybe(o, name, nameobj); |
| 1166 | if (func == NULL) { |
| 1167 | va_end(va); |
| 1168 | if (!PyErr_Occurred()) |
| 1169 | PyErr_SetObject(PyExc_AttributeError, *nameobj); |
| 1170 | return NULL; |
| 1171 | } |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1172 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | if (format && *format) |
| 1174 | args = Py_VaBuildValue(format, va); |
| 1175 | else |
| 1176 | args = PyTuple_New(0); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1178 | va_end(va); |
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 | if (args == NULL) |
| 1181 | return 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 | assert(PyTuple_Check(args)); |
| 1184 | retval = PyObject_Call(func, args, NULL); |
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 | Py_DECREF(args); |
| 1187 | Py_DECREF(func); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1188 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1189 | return retval; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | /* Clone of call_method() that returns NotImplemented when the lookup fails. */ |
| 1193 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1194 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1195 | call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 1196 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1197 | va_list va; |
| 1198 | PyObject *args, *func = 0, *retval; |
| 1199 | va_start(va, format); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1201 | func = lookup_maybe(o, name, nameobj); |
| 1202 | if (func == NULL) { |
| 1203 | va_end(va); |
| 1204 | if (!PyErr_Occurred()) { |
| 1205 | Py_INCREF(Py_NotImplemented); |
| 1206 | return Py_NotImplemented; |
| 1207 | } |
| 1208 | return NULL; |
| 1209 | } |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1211 | if (format && *format) |
| 1212 | args = Py_VaBuildValue(format, va); |
| 1213 | else |
| 1214 | args = PyTuple_New(0); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1216 | va_end(va); |
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 | if (args == NULL) |
| 1219 | return 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 | assert(PyTuple_Check(args)); |
| 1222 | retval = PyObject_Call(func, args, NULL); |
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 | Py_DECREF(args); |
| 1225 | Py_DECREF(func); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1227 | return retval; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1230 | /* |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1231 | Method resolution order algorithm C3 described in |
| 1232 | "A Monotonic Superclass Linearization for Dylan", |
| 1233 | by Kim Barrett, Bob Cassel, Paul Haahr, |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1234 | David A. Moon, Keith Playford, and P. Tucker Withington. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1235 | (OOPSLA 1996) |
| 1236 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1237 | Some notes about the rules implied by C3: |
| 1238 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1239 | No duplicate bases. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1240 | It isn't legal to repeat a class in a list of base classes. |
| 1241 | |
| 1242 | The next three properties are the 3 constraints in "C3". |
| 1243 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1244 | Local precendece order. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1245 | If A precedes B in C's MRO, then A will precede B in the MRO of all |
| 1246 | subclasses of C. |
| 1247 | |
| 1248 | Monotonicity. |
| 1249 | The MRO of a class must be an extension without reordering of the |
| 1250 | MRO of each of its superclasses. |
| 1251 | |
| 1252 | Extended Precedence Graph (EPG). |
| 1253 | Linearization is consistent if there is a path in the EPG from |
| 1254 | each class to all its successors in the linearization. See |
| 1255 | the paper for definition of EPG. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1256 | */ |
| 1257 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1258 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1259 | tail_contains(PyObject *list, int whence, PyObject *o) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1260 | Py_ssize_t j, size; |
| 1261 | size = PyList_GET_SIZE(list); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1263 | for (j = whence+1; j < size; j++) { |
| 1264 | if (PyList_GET_ITEM(list, j) == o) |
| 1265 | return 1; |
| 1266 | } |
| 1267 | return 0; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1270 | static PyObject * |
| 1271 | class_name(PyObject *cls) |
| 1272 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1273 | PyObject *name = PyObject_GetAttrString(cls, "__name__"); |
| 1274 | if (name == NULL) { |
| 1275 | PyErr_Clear(); |
| 1276 | Py_XDECREF(name); |
| 1277 | name = PyObject_Repr(cls); |
| 1278 | } |
| 1279 | if (name == NULL) |
| 1280 | return NULL; |
| 1281 | if (!PyUnicode_Check(name)) { |
| 1282 | Py_DECREF(name); |
| 1283 | return NULL; |
| 1284 | } |
| 1285 | return name; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | static int |
| 1289 | check_duplicates(PyObject *list) |
| 1290 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1291 | Py_ssize_t i, j, n; |
| 1292 | /* Let's use a quadratic time algorithm, |
| 1293 | assuming that the bases lists is short. |
| 1294 | */ |
| 1295 | n = PyList_GET_SIZE(list); |
| 1296 | for (i = 0; i < n; i++) { |
| 1297 | PyObject *o = PyList_GET_ITEM(list, i); |
| 1298 | for (j = i + 1; j < n; j++) { |
| 1299 | if (PyList_GET_ITEM(list, j) == o) { |
| 1300 | o = class_name(o); |
| 1301 | if (o != NULL) { |
| 1302 | PyErr_Format(PyExc_TypeError, |
| 1303 | "duplicate base class %U", |
| 1304 | o); |
| 1305 | Py_DECREF(o); |
| 1306 | } else { |
| 1307 | PyErr_SetString(PyExc_TypeError, |
| 1308 | "duplicate base class"); |
| 1309 | } |
| 1310 | return -1; |
| 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | return 0; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | /* Raise a TypeError for an MRO order disagreement. |
| 1318 | |
| 1319 | It's hard to produce a good error message. In the absence of better |
| 1320 | insight into error reporting, report the classes that were candidates |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1321 | 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] | 1322 | order in which they should be put in the MRO, but it's hard to |
| 1323 | diagnose what constraint can't be satisfied. |
| 1324 | */ |
| 1325 | |
| 1326 | static void |
| 1327 | set_mro_error(PyObject *to_merge, int *remain) |
| 1328 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1329 | Py_ssize_t i, n, off, to_merge_size; |
| 1330 | char buf[1000]; |
| 1331 | PyObject *k, *v; |
| 1332 | PyObject *set = PyDict_New(); |
| 1333 | if (!set) return; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1335 | to_merge_size = PyList_GET_SIZE(to_merge); |
| 1336 | for (i = 0; i < to_merge_size; i++) { |
| 1337 | PyObject *L = PyList_GET_ITEM(to_merge, i); |
| 1338 | if (remain[i] < PyList_GET_SIZE(L)) { |
| 1339 | PyObject *c = PyList_GET_ITEM(L, remain[i]); |
| 1340 | if (PyDict_SetItem(set, c, Py_None) < 0) { |
| 1341 | Py_DECREF(set); |
| 1342 | return; |
| 1343 | } |
| 1344 | } |
| 1345 | } |
| 1346 | n = PyDict_Size(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1348 | off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \ |
Raymond Hettinger | f394df4 | 2003-04-06 19:13:41 +0000 | [diff] [blame] | 1349 | consistent method resolution\norder (MRO) for bases"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1350 | i = 0; |
| 1351 | while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) { |
| 1352 | PyObject *name = class_name(k); |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1353 | char *name_str; |
| 1354 | if (name != NULL) { |
| 1355 | name_str = _PyUnicode_AsString(name); |
| 1356 | if (name_str == NULL) |
Victor Stinner | ba644a6 | 2010-05-19 01:50:45 +0000 | [diff] [blame] | 1357 | name_str = "?"; |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1358 | } else |
Victor Stinner | ba644a6 | 2010-05-19 01:50:45 +0000 | [diff] [blame] | 1359 | name_str = "?"; |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1360 | off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", name_str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1361 | Py_XDECREF(name); |
| 1362 | if (--n && (size_t)(off+1) < sizeof(buf)) { |
| 1363 | buf[off++] = ','; |
| 1364 | buf[off] = '\0'; |
| 1365 | } |
| 1366 | } |
| 1367 | PyErr_SetString(PyExc_TypeError, buf); |
| 1368 | Py_DECREF(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1371 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1372 | pmerge(PyObject *acc, PyObject* to_merge) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1373 | Py_ssize_t i, j, to_merge_size, empty_cnt; |
| 1374 | int *remain; |
| 1375 | int ok; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1377 | to_merge_size = PyList_GET_SIZE(to_merge); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1378 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1379 | /* remain stores an index into each sublist of to_merge. |
| 1380 | remain[i] is the index of the next base in to_merge[i] |
| 1381 | that is not included in acc. |
| 1382 | */ |
| 1383 | remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size); |
| 1384 | if (remain == NULL) |
| 1385 | return -1; |
| 1386 | for (i = 0; i < to_merge_size; i++) |
| 1387 | remain[i] = 0; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1388 | |
| 1389 | again: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1390 | empty_cnt = 0; |
| 1391 | for (i = 0; i < to_merge_size; i++) { |
| 1392 | PyObject *candidate; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1393 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1394 | PyObject *cur_list = PyList_GET_ITEM(to_merge, i); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1395 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1396 | if (remain[i] >= PyList_GET_SIZE(cur_list)) { |
| 1397 | empty_cnt++; |
| 1398 | continue; |
| 1399 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1400 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1401 | /* Choose next candidate for MRO. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1403 | The input sequences alone can determine the choice. |
| 1404 | If not, choose the class which appears in the MRO |
| 1405 | of the earliest direct superclass of the new class. |
| 1406 | */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1408 | candidate = PyList_GET_ITEM(cur_list, remain[i]); |
| 1409 | for (j = 0; j < to_merge_size; j++) { |
| 1410 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
| 1411 | if (tail_contains(j_lst, remain[j], candidate)) { |
| 1412 | goto skip; /* continue outer loop */ |
| 1413 | } |
| 1414 | } |
| 1415 | ok = PyList_Append(acc, candidate); |
| 1416 | if (ok < 0) { |
| 1417 | PyMem_Free(remain); |
| 1418 | return -1; |
| 1419 | } |
| 1420 | for (j = 0; j < to_merge_size; j++) { |
| 1421 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
| 1422 | if (remain[j] < PyList_GET_SIZE(j_lst) && |
| 1423 | PyList_GET_ITEM(j_lst, remain[j]) == candidate) { |
| 1424 | remain[j]++; |
| 1425 | } |
| 1426 | } |
| 1427 | goto again; |
| 1428 | skip: ; |
| 1429 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1431 | if (empty_cnt == to_merge_size) { |
| 1432 | PyMem_FREE(remain); |
| 1433 | return 0; |
| 1434 | } |
| 1435 | set_mro_error(to_merge, remain); |
| 1436 | PyMem_FREE(remain); |
| 1437 | return -1; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1440 | static PyObject * |
| 1441 | mro_implementation(PyTypeObject *type) |
| 1442 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1443 | Py_ssize_t i, n; |
| 1444 | int ok; |
| 1445 | PyObject *bases, *result; |
| 1446 | PyObject *to_merge, *bases_aslist; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1448 | if (type->tp_dict == NULL) { |
| 1449 | if (PyType_Ready(type) < 0) |
| 1450 | return NULL; |
| 1451 | } |
Guido van Rossum | 6351757 | 2002-06-18 16:44:57 +0000 | [diff] [blame] | 1452 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1453 | /* Find a superclass linearization that honors the constraints |
| 1454 | of the explicit lists of bases and the constraints implied by |
| 1455 | each base class. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1456 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1457 | to_merge is a list of lists, where each list is a superclass |
| 1458 | linearization implied by a base class. The last element of |
| 1459 | to_merge is the declared list of bases. |
| 1460 | */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1461 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1462 | bases = type->tp_bases; |
| 1463 | n = PyTuple_GET_SIZE(bases); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1464 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1465 | to_merge = PyList_New(n+1); |
| 1466 | if (to_merge == NULL) |
| 1467 | return NULL; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1469 | for (i = 0; i < n; i++) { |
| 1470 | PyObject *base = PyTuple_GET_ITEM(bases, i); |
| 1471 | PyObject *parentMRO; |
| 1472 | parentMRO = PySequence_List(((PyTypeObject*)base)->tp_mro); |
| 1473 | if (parentMRO == NULL) { |
| 1474 | Py_DECREF(to_merge); |
| 1475 | return NULL; |
| 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 | PyList_SET_ITEM(to_merge, i, parentMRO); |
| 1479 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1481 | bases_aslist = PySequence_List(bases); |
| 1482 | if (bases_aslist == NULL) { |
| 1483 | Py_DECREF(to_merge); |
| 1484 | return NULL; |
| 1485 | } |
| 1486 | /* This is just a basic sanity check. */ |
| 1487 | if (check_duplicates(bases_aslist) < 0) { |
| 1488 | Py_DECREF(to_merge); |
| 1489 | Py_DECREF(bases_aslist); |
| 1490 | return NULL; |
| 1491 | } |
| 1492 | PyList_SET_ITEM(to_merge, n, bases_aslist); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1494 | result = Py_BuildValue("[O]", (PyObject *)type); |
| 1495 | if (result == NULL) { |
| 1496 | Py_DECREF(to_merge); |
| 1497 | return NULL; |
| 1498 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1500 | ok = pmerge(result, to_merge); |
| 1501 | Py_DECREF(to_merge); |
| 1502 | if (ok < 0) { |
| 1503 | Py_DECREF(result); |
| 1504 | return NULL; |
| 1505 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1507 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1511 | mro_external(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1512 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1513 | PyTypeObject *type = (PyTypeObject *)self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1515 | return mro_implementation(type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | static int |
| 1519 | mro_internal(PyTypeObject *type) |
| 1520 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1521 | PyObject *mro, *result, *tuple; |
| 1522 | int checkit = 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1523 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1524 | if (Py_TYPE(type) == &PyType_Type) { |
| 1525 | result = mro_implementation(type); |
| 1526 | } |
| 1527 | else { |
| 1528 | static PyObject *mro_str; |
| 1529 | checkit = 1; |
| 1530 | mro = lookup_method((PyObject *)type, "mro", &mro_str); |
| 1531 | if (mro == NULL) |
| 1532 | return -1; |
| 1533 | result = PyObject_CallObject(mro, NULL); |
| 1534 | Py_DECREF(mro); |
| 1535 | } |
| 1536 | if (result == NULL) |
| 1537 | return -1; |
| 1538 | tuple = PySequence_Tuple(result); |
| 1539 | Py_DECREF(result); |
| 1540 | if (tuple == NULL) |
| 1541 | return -1; |
| 1542 | if (checkit) { |
| 1543 | Py_ssize_t i, len; |
| 1544 | PyObject *cls; |
| 1545 | PyTypeObject *solid; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1547 | solid = solid_base(type); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1549 | len = PyTuple_GET_SIZE(tuple); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1551 | for (i = 0; i < len; i++) { |
| 1552 | PyTypeObject *t; |
| 1553 | cls = PyTuple_GET_ITEM(tuple, i); |
| 1554 | if (!PyType_Check(cls)) { |
| 1555 | PyErr_Format(PyExc_TypeError, |
| 1556 | "mro() returned a non-class ('%.500s')", |
| 1557 | Py_TYPE(cls)->tp_name); |
| 1558 | Py_DECREF(tuple); |
| 1559 | return -1; |
| 1560 | } |
| 1561 | t = (PyTypeObject*)cls; |
| 1562 | if (!PyType_IsSubtype(solid, solid_base(t))) { |
| 1563 | PyErr_Format(PyExc_TypeError, |
| 1564 | "mro() returned base with unsuitable layout ('%.500s')", |
| 1565 | t->tp_name); |
| 1566 | Py_DECREF(tuple); |
| 1567 | return -1; |
| 1568 | } |
| 1569 | } |
| 1570 | } |
| 1571 | type->tp_mro = tuple; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1572 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1573 | type_mro_modified(type, type->tp_mro); |
| 1574 | /* corner case: the old-style super class might have been hidden |
| 1575 | from the custom MRO */ |
| 1576 | type_mro_modified(type, type->tp_bases); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1578 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1579 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1580 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | |
| 1584 | /* Calculate the best base amongst multiple base classes. |
| 1585 | This is the first one that's on the path to the "solid base". */ |
| 1586 | |
| 1587 | static PyTypeObject * |
| 1588 | best_base(PyObject *bases) |
| 1589 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1590 | Py_ssize_t i, n; |
| 1591 | PyTypeObject *base, *winner, *candidate, *base_i; |
| 1592 | PyObject *base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1593 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1594 | assert(PyTuple_Check(bases)); |
| 1595 | n = PyTuple_GET_SIZE(bases); |
| 1596 | assert(n > 0); |
| 1597 | base = NULL; |
| 1598 | winner = NULL; |
| 1599 | for (i = 0; i < n; i++) { |
| 1600 | base_proto = PyTuple_GET_ITEM(bases, i); |
| 1601 | if (!PyType_Check(base_proto)) { |
| 1602 | PyErr_SetString( |
| 1603 | PyExc_TypeError, |
| 1604 | "bases must be types"); |
| 1605 | return NULL; |
| 1606 | } |
| 1607 | base_i = (PyTypeObject *)base_proto; |
| 1608 | if (base_i->tp_dict == NULL) { |
| 1609 | if (PyType_Ready(base_i) < 0) |
| 1610 | return NULL; |
| 1611 | } |
| 1612 | candidate = solid_base(base_i); |
| 1613 | if (winner == NULL) { |
| 1614 | winner = candidate; |
| 1615 | base = base_i; |
| 1616 | } |
| 1617 | else if (PyType_IsSubtype(winner, candidate)) |
| 1618 | ; |
| 1619 | else if (PyType_IsSubtype(candidate, winner)) { |
| 1620 | winner = candidate; |
| 1621 | base = base_i; |
| 1622 | } |
| 1623 | else { |
| 1624 | PyErr_SetString( |
| 1625 | PyExc_TypeError, |
| 1626 | "multiple bases have " |
| 1627 | "instance lay-out conflict"); |
| 1628 | return NULL; |
| 1629 | } |
| 1630 | } |
| 1631 | if (base == NULL) |
| 1632 | PyErr_SetString(PyExc_TypeError, |
| 1633 | "a new-style class can't have only classic bases"); |
| 1634 | return base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | static int |
| 1638 | extra_ivars(PyTypeObject *type, PyTypeObject *base) |
| 1639 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1640 | size_t t_size = type->tp_basicsize; |
| 1641 | size_t b_size = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1642 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1643 | assert(t_size >= b_size); /* Else type smaller than base! */ |
| 1644 | if (type->tp_itemsize || base->tp_itemsize) { |
| 1645 | /* If itemsize is involved, stricter rules */ |
| 1646 | return t_size != b_size || |
| 1647 | type->tp_itemsize != base->tp_itemsize; |
| 1648 | } |
| 1649 | if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 && |
| 1650 | type->tp_weaklistoffset + sizeof(PyObject *) == t_size && |
| 1651 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 1652 | t_size -= sizeof(PyObject *); |
| 1653 | if (type->tp_dictoffset && base->tp_dictoffset == 0 && |
| 1654 | type->tp_dictoffset + sizeof(PyObject *) == t_size && |
| 1655 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 1656 | t_size -= sizeof(PyObject *); |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1658 | return t_size != b_size; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | static PyTypeObject * |
| 1662 | solid_base(PyTypeObject *type) |
| 1663 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1664 | PyTypeObject *base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1665 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1666 | if (type->tp_base) |
| 1667 | base = solid_base(type->tp_base); |
| 1668 | else |
| 1669 | base = &PyBaseObject_Type; |
| 1670 | if (extra_ivars(type, base)) |
| 1671 | return type; |
| 1672 | else |
| 1673 | return base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1676 | static void object_dealloc(PyObject *); |
| 1677 | static int object_init(PyObject *, PyObject *, PyObject *); |
| 1678 | static int update_slot(PyTypeObject *, PyObject *); |
| 1679 | static void fixup_slot_dispatchers(PyTypeObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1680 | |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1681 | /* |
| 1682 | * Helpers for __dict__ descriptor. We don't want to expose the dicts |
| 1683 | * inherited from various builtin types. The builtin base usually provides |
| 1684 | * its own __dict__ descriptor, so we use that when we can. |
| 1685 | */ |
| 1686 | static PyTypeObject * |
| 1687 | get_builtin_base_with_dict(PyTypeObject *type) |
| 1688 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1689 | while (type->tp_base != NULL) { |
| 1690 | if (type->tp_dictoffset != 0 && |
| 1691 | !(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 1692 | return type; |
| 1693 | type = type->tp_base; |
| 1694 | } |
| 1695 | return NULL; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | static PyObject * |
| 1699 | get_dict_descriptor(PyTypeObject *type) |
| 1700 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1701 | static PyObject *dict_str; |
| 1702 | PyObject *descr; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1703 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1704 | if (dict_str == NULL) { |
| 1705 | dict_str = PyUnicode_InternFromString("__dict__"); |
| 1706 | if (dict_str == NULL) |
| 1707 | return NULL; |
| 1708 | } |
| 1709 | descr = _PyType_Lookup(type, dict_str); |
| 1710 | if (descr == NULL || !PyDescr_IsData(descr)) |
| 1711 | return NULL; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1712 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1713 | return descr; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | static void |
| 1717 | raise_dict_descr_error(PyObject *obj) |
| 1718 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1719 | PyErr_Format(PyExc_TypeError, |
| 1720 | "this __dict__ descriptor does not support " |
| 1721 | "'%.200s' objects", Py_TYPE(obj)->tp_name); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1724 | static PyObject * |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1725 | subtype_dict(PyObject *obj, void *context) |
| 1726 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1727 | PyObject **dictptr; |
| 1728 | PyObject *dict; |
| 1729 | PyTypeObject *base; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1730 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1731 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
| 1732 | if (base != NULL) { |
| 1733 | descrgetfunc func; |
| 1734 | PyObject *descr = get_dict_descriptor(base); |
| 1735 | if (descr == NULL) { |
| 1736 | raise_dict_descr_error(obj); |
| 1737 | return NULL; |
| 1738 | } |
| 1739 | func = Py_TYPE(descr)->tp_descr_get; |
| 1740 | if (func == NULL) { |
| 1741 | raise_dict_descr_error(obj); |
| 1742 | return NULL; |
| 1743 | } |
| 1744 | return func(descr, obj, (PyObject *)(Py_TYPE(obj))); |
| 1745 | } |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1746 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1747 | dictptr = _PyObject_GetDictPtr(obj); |
| 1748 | if (dictptr == NULL) { |
| 1749 | PyErr_SetString(PyExc_AttributeError, |
| 1750 | "This object has no __dict__"); |
| 1751 | return NULL; |
| 1752 | } |
| 1753 | dict = *dictptr; |
| 1754 | if (dict == NULL) |
| 1755 | *dictptr = dict = PyDict_New(); |
| 1756 | Py_XINCREF(dict); |
| 1757 | return dict; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1760 | static int |
| 1761 | subtype_setdict(PyObject *obj, PyObject *value, void *context) |
| 1762 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1763 | PyObject **dictptr; |
| 1764 | PyObject *dict; |
| 1765 | PyTypeObject *base; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1766 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1767 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
| 1768 | if (base != NULL) { |
| 1769 | descrsetfunc func; |
| 1770 | PyObject *descr = get_dict_descriptor(base); |
| 1771 | if (descr == NULL) { |
| 1772 | raise_dict_descr_error(obj); |
| 1773 | return -1; |
| 1774 | } |
| 1775 | func = Py_TYPE(descr)->tp_descr_set; |
| 1776 | if (func == NULL) { |
| 1777 | raise_dict_descr_error(obj); |
| 1778 | return -1; |
| 1779 | } |
| 1780 | return func(descr, obj, value); |
| 1781 | } |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1782 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1783 | dictptr = _PyObject_GetDictPtr(obj); |
| 1784 | if (dictptr == NULL) { |
| 1785 | PyErr_SetString(PyExc_AttributeError, |
| 1786 | "This object has no __dict__"); |
| 1787 | return -1; |
| 1788 | } |
| 1789 | if (value != NULL && !PyDict_Check(value)) { |
| 1790 | PyErr_Format(PyExc_TypeError, |
| 1791 | "__dict__ must be set to a dictionary, " |
| 1792 | "not a '%.200s'", Py_TYPE(value)->tp_name); |
| 1793 | return -1; |
| 1794 | } |
| 1795 | dict = *dictptr; |
| 1796 | Py_XINCREF(value); |
| 1797 | *dictptr = value; |
| 1798 | Py_XDECREF(dict); |
| 1799 | return 0; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1802 | static PyObject * |
| 1803 | subtype_getweakref(PyObject *obj, void *context) |
| 1804 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1805 | PyObject **weaklistptr; |
| 1806 | PyObject *result; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1808 | if (Py_TYPE(obj)->tp_weaklistoffset == 0) { |
| 1809 | PyErr_SetString(PyExc_AttributeError, |
| 1810 | "This object has no __weakref__"); |
| 1811 | return NULL; |
| 1812 | } |
| 1813 | assert(Py_TYPE(obj)->tp_weaklistoffset > 0); |
| 1814 | assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <= |
| 1815 | (size_t)(Py_TYPE(obj)->tp_basicsize)); |
| 1816 | weaklistptr = (PyObject **) |
| 1817 | ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset); |
| 1818 | if (*weaklistptr == NULL) |
| 1819 | result = Py_None; |
| 1820 | else |
| 1821 | result = *weaklistptr; |
| 1822 | Py_INCREF(result); |
| 1823 | return result; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1826 | /* Three variants on the subtype_getsets list. */ |
| 1827 | |
| 1828 | static PyGetSetDef subtype_getsets_full[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1829 | {"__dict__", subtype_dict, subtype_setdict, |
| 1830 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 1831 | {"__weakref__", subtype_getweakref, NULL, |
| 1832 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 1833 | {0} |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1834 | }; |
| 1835 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1836 | static PyGetSetDef subtype_getsets_dict_only[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1837 | {"__dict__", subtype_dict, subtype_setdict, |
| 1838 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 1839 | {0} |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1840 | }; |
| 1841 | |
| 1842 | static PyGetSetDef subtype_getsets_weakref_only[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1843 | {"__weakref__", subtype_getweakref, NULL, |
| 1844 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 1845 | {0} |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 1846 | }; |
| 1847 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1848 | static int |
| 1849 | valid_identifier(PyObject *s) |
| 1850 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1851 | if (!PyUnicode_Check(s)) { |
| 1852 | PyErr_Format(PyExc_TypeError, |
| 1853 | "__slots__ items must be strings, not '%.200s'", |
| 1854 | Py_TYPE(s)->tp_name); |
| 1855 | return 0; |
| 1856 | } |
| 1857 | if (!PyUnicode_IsIdentifier(s)) { |
| 1858 | PyErr_SetString(PyExc_TypeError, |
| 1859 | "__slots__ must be identifiers"); |
| 1860 | return 0; |
| 1861 | } |
| 1862 | return 1; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1865 | /* Forward */ |
| 1866 | static int |
| 1867 | object_init(PyObject *self, PyObject *args, PyObject *kwds); |
| 1868 | |
| 1869 | static int |
| 1870 | type_init(PyObject *cls, PyObject *args, PyObject *kwds) |
| 1871 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1872 | int res; |
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 | assert(args != NULL && PyTuple_Check(args)); |
| 1875 | assert(kwds == NULL || PyDict_Check(kwds)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1876 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1877 | if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) { |
| 1878 | PyErr_SetString(PyExc_TypeError, |
| 1879 | "type.__init__() takes no keyword arguments"); |
| 1880 | return -1; |
| 1881 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1882 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1883 | if (args != NULL && PyTuple_Check(args) && |
| 1884 | (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) { |
| 1885 | PyErr_SetString(PyExc_TypeError, |
| 1886 | "type.__init__() takes 1 or 3 arguments"); |
| 1887 | return -1; |
| 1888 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1889 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1890 | /* Call object.__init__(self) now. */ |
| 1891 | /* XXX Could call super(type, cls).__init__() but what's the point? */ |
| 1892 | args = PyTuple_GetSlice(args, 0, 0); |
| 1893 | res = object_init(cls, args, NULL); |
| 1894 | Py_DECREF(args); |
| 1895 | return res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1898 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1899 | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) |
| 1900 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1901 | PyObject *name, *bases, *dict; |
| 1902 | static char *kwlist[] = {"name", "bases", "dict", 0}; |
| 1903 | PyObject *slots, *tmp, *newslots; |
| 1904 | PyTypeObject *type, *base, *tmptype, *winner; |
| 1905 | PyHeapTypeObject *et; |
| 1906 | PyMemberDef *mp; |
| 1907 | Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak; |
| 1908 | int j, may_add_dict, may_add_weak; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1910 | assert(args != NULL && PyTuple_Check(args)); |
| 1911 | assert(kwds == NULL || PyDict_Check(kwds)); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1912 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1913 | /* Special case: type(x) should return x->ob_type */ |
| 1914 | { |
| 1915 | const Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 1916 | const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1918 | if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) { |
| 1919 | PyObject *x = PyTuple_GET_ITEM(args, 0); |
| 1920 | Py_INCREF(Py_TYPE(x)); |
| 1921 | return (PyObject *) Py_TYPE(x); |
| 1922 | } |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 1923 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1924 | /* SF bug 475327 -- if that didn't trigger, we need 3 |
| 1925 | arguments. but PyArg_ParseTupleAndKeywords below may give |
| 1926 | a msg saying type() needs exactly 3. */ |
| 1927 | if (nargs + nkwds != 3) { |
| 1928 | PyErr_SetString(PyExc_TypeError, |
| 1929 | "type() takes 1 or 3 arguments"); |
| 1930 | return NULL; |
| 1931 | } |
| 1932 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1933 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1934 | /* Check arguments: (name, bases, dict) */ |
| 1935 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "UO!O!:type", kwlist, |
| 1936 | &name, |
| 1937 | &PyTuple_Type, &bases, |
| 1938 | &PyDict_Type, &dict)) |
| 1939 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1940 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1941 | /* Determine the proper metatype to deal with this, |
| 1942 | and check for metatype conflicts while we're at it. |
| 1943 | Note that if some other metatype wins to contract, |
| 1944 | it's possible that its instances are not types. */ |
| 1945 | nbases = PyTuple_GET_SIZE(bases); |
| 1946 | winner = metatype; |
| 1947 | for (i = 0; i < nbases; i++) { |
| 1948 | tmp = PyTuple_GET_ITEM(bases, i); |
| 1949 | tmptype = Py_TYPE(tmp); |
| 1950 | if (PyType_IsSubtype(winner, tmptype)) |
| 1951 | continue; |
| 1952 | if (PyType_IsSubtype(tmptype, winner)) { |
| 1953 | winner = tmptype; |
| 1954 | continue; |
| 1955 | } |
| 1956 | PyErr_SetString(PyExc_TypeError, |
| 1957 | "metaclass conflict: " |
| 1958 | "the metaclass of a derived class " |
| 1959 | "must be a (non-strict) subclass " |
| 1960 | "of the metaclasses of all its bases"); |
| 1961 | return NULL; |
| 1962 | } |
| 1963 | if (winner != metatype) { |
| 1964 | if (winner->tp_new != type_new) /* Pass it to the winner */ |
| 1965 | return winner->tp_new(winner, args, kwds); |
| 1966 | metatype = winner; |
| 1967 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1968 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1969 | /* Adjust for empty tuple bases */ |
| 1970 | if (nbases == 0) { |
| 1971 | bases = PyTuple_Pack(1, &PyBaseObject_Type); |
| 1972 | if (bases == NULL) |
| 1973 | return NULL; |
| 1974 | nbases = 1; |
| 1975 | } |
| 1976 | else |
| 1977 | Py_INCREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1979 | /* XXX From here until type is allocated, "return NULL" leaks bases! */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1980 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1981 | /* Calculate best base, and check that all bases are type objects */ |
| 1982 | base = best_base(bases); |
| 1983 | if (base == NULL) { |
| 1984 | Py_DECREF(bases); |
| 1985 | return NULL; |
| 1986 | } |
| 1987 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 1988 | PyErr_Format(PyExc_TypeError, |
| 1989 | "type '%.100s' is not an acceptable base type", |
| 1990 | base->tp_name); |
| 1991 | Py_DECREF(bases); |
| 1992 | return NULL; |
| 1993 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1994 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1995 | /* Check for a __slots__ sequence variable in dict, and count it */ |
| 1996 | slots = PyDict_GetItemString(dict, "__slots__"); |
| 1997 | nslots = 0; |
| 1998 | add_dict = 0; |
| 1999 | add_weak = 0; |
| 2000 | may_add_dict = base->tp_dictoffset == 0; |
| 2001 | may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0; |
| 2002 | if (slots == NULL) { |
| 2003 | if (may_add_dict) { |
| 2004 | add_dict++; |
| 2005 | } |
| 2006 | if (may_add_weak) { |
| 2007 | add_weak++; |
| 2008 | } |
| 2009 | } |
| 2010 | else { |
| 2011 | /* Have slots */ |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2013 | /* Make it into a tuple */ |
| 2014 | if (PyUnicode_Check(slots)) |
| 2015 | slots = PyTuple_Pack(1, slots); |
| 2016 | else |
| 2017 | slots = PySequence_Tuple(slots); |
| 2018 | if (slots == NULL) { |
| 2019 | Py_DECREF(bases); |
| 2020 | return NULL; |
| 2021 | } |
| 2022 | assert(PyTuple_Check(slots)); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2023 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2024 | /* Are slots allowed? */ |
| 2025 | nslots = PyTuple_GET_SIZE(slots); |
| 2026 | if (nslots > 0 && base->tp_itemsize != 0) { |
| 2027 | PyErr_Format(PyExc_TypeError, |
| 2028 | "nonempty __slots__ " |
| 2029 | "not supported for subtype of '%s'", |
| 2030 | base->tp_name); |
| 2031 | bad_slots: |
| 2032 | Py_DECREF(bases); |
| 2033 | Py_DECREF(slots); |
| 2034 | return NULL; |
| 2035 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2036 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2037 | /* Check for valid slot names and two special cases */ |
| 2038 | for (i = 0; i < nslots; i++) { |
| 2039 | PyObject *tmp = PyTuple_GET_ITEM(slots, i); |
| 2040 | if (!valid_identifier(tmp)) |
| 2041 | goto bad_slots; |
| 2042 | assert(PyUnicode_Check(tmp)); |
| 2043 | if (PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) { |
| 2044 | if (!may_add_dict || add_dict) { |
| 2045 | PyErr_SetString(PyExc_TypeError, |
| 2046 | "__dict__ slot disallowed: " |
| 2047 | "we already got one"); |
| 2048 | goto bad_slots; |
| 2049 | } |
| 2050 | add_dict++; |
| 2051 | } |
| 2052 | if (PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0) { |
| 2053 | if (!may_add_weak || add_weak) { |
| 2054 | PyErr_SetString(PyExc_TypeError, |
| 2055 | "__weakref__ slot disallowed: " |
| 2056 | "either we already got one, " |
| 2057 | "or __itemsize__ != 0"); |
| 2058 | goto bad_slots; |
| 2059 | } |
| 2060 | add_weak++; |
| 2061 | } |
| 2062 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2063 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2064 | /* Copy slots into a list, mangle names and sort them. |
| 2065 | Sorted names are needed for __class__ assignment. |
| 2066 | Convert them back to tuple at the end. |
| 2067 | */ |
| 2068 | newslots = PyList_New(nslots - add_dict - add_weak); |
| 2069 | if (newslots == NULL) |
| 2070 | goto bad_slots; |
| 2071 | for (i = j = 0; i < nslots; i++) { |
| 2072 | tmp = PyTuple_GET_ITEM(slots, i); |
| 2073 | if ((add_dict && |
| 2074 | PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) || |
| 2075 | (add_weak && |
| 2076 | PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0)) |
| 2077 | continue; |
| 2078 | tmp =_Py_Mangle(name, tmp); |
| 2079 | if (!tmp) |
| 2080 | goto bad_slots; |
| 2081 | PyList_SET_ITEM(newslots, j, tmp); |
| 2082 | j++; |
| 2083 | } |
| 2084 | assert(j == nslots - add_dict - add_weak); |
| 2085 | nslots = j; |
| 2086 | Py_DECREF(slots); |
| 2087 | if (PyList_Sort(newslots) == -1) { |
| 2088 | Py_DECREF(bases); |
| 2089 | Py_DECREF(newslots); |
| 2090 | return NULL; |
| 2091 | } |
| 2092 | slots = PyList_AsTuple(newslots); |
| 2093 | Py_DECREF(newslots); |
| 2094 | if (slots == NULL) { |
| 2095 | Py_DECREF(bases); |
| 2096 | return NULL; |
| 2097 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2098 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2099 | /* Secondary bases may provide weakrefs or dict */ |
| 2100 | if (nbases > 1 && |
| 2101 | ((may_add_dict && !add_dict) || |
| 2102 | (may_add_weak && !add_weak))) { |
| 2103 | for (i = 0; i < nbases; i++) { |
| 2104 | tmp = PyTuple_GET_ITEM(bases, i); |
| 2105 | if (tmp == (PyObject *)base) |
| 2106 | continue; /* Skip primary base */ |
| 2107 | assert(PyType_Check(tmp)); |
| 2108 | tmptype = (PyTypeObject *)tmp; |
| 2109 | if (may_add_dict && !add_dict && |
| 2110 | tmptype->tp_dictoffset != 0) |
| 2111 | add_dict++; |
| 2112 | if (may_add_weak && !add_weak && |
| 2113 | tmptype->tp_weaklistoffset != 0) |
| 2114 | add_weak++; |
| 2115 | if (may_add_dict && !add_dict) |
| 2116 | continue; |
| 2117 | if (may_add_weak && !add_weak) |
| 2118 | continue; |
| 2119 | /* Nothing more to check */ |
| 2120 | break; |
| 2121 | } |
| 2122 | } |
| 2123 | } |
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 | /* XXX From here until type is safely allocated, |
| 2126 | "return NULL" may leak slots! */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2128 | /* Allocate the type object */ |
| 2129 | type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots); |
| 2130 | if (type == NULL) { |
| 2131 | Py_XDECREF(slots); |
| 2132 | Py_DECREF(bases); |
| 2133 | return NULL; |
| 2134 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2136 | /* Keep name and slots alive in the extended type object */ |
| 2137 | et = (PyHeapTypeObject *)type; |
| 2138 | Py_INCREF(name); |
| 2139 | et->ht_name = name; |
| 2140 | et->ht_slots = slots; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2141 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2142 | /* Initialize tp_flags */ |
| 2143 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | |
| 2144 | Py_TPFLAGS_BASETYPE; |
| 2145 | if (base->tp_flags & Py_TPFLAGS_HAVE_GC) |
| 2146 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2148 | /* Initialize essential fields */ |
| 2149 | type->tp_as_number = &et->as_number; |
| 2150 | type->tp_as_sequence = &et->as_sequence; |
| 2151 | type->tp_as_mapping = &et->as_mapping; |
| 2152 | type->tp_as_buffer = &et->as_buffer; |
| 2153 | type->tp_name = _PyUnicode_AsString(name); |
| 2154 | if (!type->tp_name) { |
| 2155 | Py_DECREF(type); |
| 2156 | return NULL; |
| 2157 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2159 | /* Set tp_base and tp_bases */ |
| 2160 | type->tp_bases = bases; |
| 2161 | Py_INCREF(base); |
| 2162 | type->tp_base = base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2164 | /* Initialize tp_dict from passed-in dict */ |
| 2165 | type->tp_dict = dict = PyDict_Copy(dict); |
| 2166 | if (dict == NULL) { |
| 2167 | Py_DECREF(type); |
| 2168 | return NULL; |
| 2169 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2171 | /* Set __module__ in the dict */ |
| 2172 | if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 2173 | tmp = PyEval_GetGlobals(); |
| 2174 | if (tmp != NULL) { |
| 2175 | tmp = PyDict_GetItemString(tmp, "__name__"); |
| 2176 | if (tmp != NULL) { |
| 2177 | if (PyDict_SetItemString(dict, "__module__", |
| 2178 | tmp) < 0) |
| 2179 | return NULL; |
| 2180 | } |
| 2181 | } |
| 2182 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 2183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2184 | /* Set tp_doc to a copy of dict['__doc__'], if the latter is there |
| 2185 | and is a string. The __doc__ accessor will first look for tp_doc; |
| 2186 | if that fails, it will still look into __dict__. |
| 2187 | */ |
| 2188 | { |
| 2189 | PyObject *doc = PyDict_GetItemString(dict, "__doc__"); |
| 2190 | if (doc != NULL && PyUnicode_Check(doc)) { |
| 2191 | Py_ssize_t len; |
| 2192 | char *doc_str; |
| 2193 | char *tp_doc; |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 2194 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2195 | doc_str = _PyUnicode_AsString(doc); |
| 2196 | if (doc_str == NULL) { |
| 2197 | Py_DECREF(type); |
| 2198 | return NULL; |
| 2199 | } |
| 2200 | /* Silently truncate the docstring if it contains null bytes. */ |
| 2201 | len = strlen(doc_str); |
| 2202 | tp_doc = (char *)PyObject_MALLOC(len + 1); |
| 2203 | if (tp_doc == NULL) { |
| 2204 | Py_DECREF(type); |
| 2205 | return NULL; |
| 2206 | } |
| 2207 | memcpy(tp_doc, doc_str, len + 1); |
| 2208 | type->tp_doc = tp_doc; |
| 2209 | } |
| 2210 | } |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2211 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2212 | /* Special-case __new__: if it's a plain function, |
| 2213 | make it a static function */ |
| 2214 | tmp = PyDict_GetItemString(dict, "__new__"); |
| 2215 | if (tmp != NULL && PyFunction_Check(tmp)) { |
| 2216 | tmp = PyStaticMethod_New(tmp); |
| 2217 | if (tmp == NULL) { |
| 2218 | Py_DECREF(type); |
| 2219 | return NULL; |
| 2220 | } |
| 2221 | PyDict_SetItemString(dict, "__new__", tmp); |
| 2222 | Py_DECREF(tmp); |
| 2223 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2225 | /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
| 2226 | mp = PyHeapType_GET_MEMBERS(et); |
| 2227 | slotoffset = base->tp_basicsize; |
| 2228 | if (slots != NULL) { |
| 2229 | for (i = 0; i < nslots; i++, mp++) { |
| 2230 | mp->name = _PyUnicode_AsString( |
| 2231 | PyTuple_GET_ITEM(slots, i)); |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 2232 | if (mp->name == NULL) { |
| 2233 | Py_DECREF(type); |
| 2234 | return NULL; |
| 2235 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2236 | mp->type = T_OBJECT_EX; |
| 2237 | mp->offset = slotoffset; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2239 | /* __dict__ and __weakref__ are already filtered out */ |
| 2240 | assert(strcmp(mp->name, "__dict__") != 0); |
| 2241 | assert(strcmp(mp->name, "__weakref__") != 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2242 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2243 | slotoffset += sizeof(PyObject *); |
| 2244 | } |
| 2245 | } |
| 2246 | if (add_dict) { |
| 2247 | if (base->tp_itemsize) |
| 2248 | type->tp_dictoffset = -(long)sizeof(PyObject *); |
| 2249 | else |
| 2250 | type->tp_dictoffset = slotoffset; |
| 2251 | slotoffset += sizeof(PyObject *); |
| 2252 | } |
| 2253 | if (add_weak) { |
| 2254 | assert(!base->tp_itemsize); |
| 2255 | type->tp_weaklistoffset = slotoffset; |
| 2256 | slotoffset += sizeof(PyObject *); |
| 2257 | } |
| 2258 | type->tp_basicsize = slotoffset; |
| 2259 | type->tp_itemsize = base->tp_itemsize; |
| 2260 | type->tp_members = PyHeapType_GET_MEMBERS(et); |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2261 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2262 | if (type->tp_weaklistoffset && type->tp_dictoffset) |
| 2263 | type->tp_getset = subtype_getsets_full; |
| 2264 | else if (type->tp_weaklistoffset && !type->tp_dictoffset) |
| 2265 | type->tp_getset = subtype_getsets_weakref_only; |
| 2266 | else if (!type->tp_weaklistoffset && type->tp_dictoffset) |
| 2267 | type->tp_getset = subtype_getsets_dict_only; |
| 2268 | else |
| 2269 | type->tp_getset = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2271 | /* Special case some slots */ |
| 2272 | if (type->tp_dictoffset != 0 || nslots > 0) { |
| 2273 | if (base->tp_getattr == NULL && base->tp_getattro == NULL) |
| 2274 | type->tp_getattro = PyObject_GenericGetAttr; |
| 2275 | if (base->tp_setattr == NULL && base->tp_setattro == NULL) |
| 2276 | type->tp_setattro = PyObject_GenericSetAttr; |
| 2277 | } |
| 2278 | type->tp_dealloc = subtype_dealloc; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2280 | /* Enable GC unless there are really no instance variables possible */ |
| 2281 | if (!(type->tp_basicsize == sizeof(PyObject) && |
| 2282 | type->tp_itemsize == 0)) |
| 2283 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 2284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2285 | /* Always override allocation strategy to use regular heap */ |
| 2286 | type->tp_alloc = PyType_GenericAlloc; |
| 2287 | if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { |
| 2288 | type->tp_free = PyObject_GC_Del; |
| 2289 | type->tp_traverse = subtype_traverse; |
| 2290 | type->tp_clear = subtype_clear; |
| 2291 | } |
| 2292 | else |
| 2293 | type->tp_free = PyObject_Del; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2295 | /* Initialize the rest */ |
| 2296 | if (PyType_Ready(type) < 0) { |
| 2297 | Py_DECREF(type); |
| 2298 | return NULL; |
| 2299 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2301 | /* Put the proper slots in place */ |
| 2302 | fixup_slot_dispatchers(type); |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 2303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2304 | return (PyObject *)type; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2307 | static short slotoffsets[] = { |
| 2308 | -1, /* invalid slot */ |
| 2309 | #include "typeslots.inc" |
| 2310 | }; |
| 2311 | |
| 2312 | PyObject* PyType_FromSpec(PyType_Spec *spec) |
| 2313 | { |
| 2314 | PyHeapTypeObject *res = (PyHeapTypeObject*)PyType_GenericAlloc(&PyType_Type, 0); |
| 2315 | char *res_start = (char*)res; |
| 2316 | PyType_Slot *slot; |
| 2317 | |
| 2318 | res->ht_name = PyUnicode_FromString(spec->name); |
| 2319 | if (!res->ht_name) |
| 2320 | goto fail; |
| 2321 | res->ht_type.tp_name = _PyUnicode_AsString(res->ht_name); |
| 2322 | if (!res->ht_type.tp_name) |
| 2323 | goto fail; |
| 2324 | |
| 2325 | res->ht_type.tp_basicsize = spec->basicsize; |
| 2326 | res->ht_type.tp_itemsize = spec->itemsize; |
| 2327 | res->ht_type.tp_flags = spec->flags | Py_TPFLAGS_HEAPTYPE; |
| 2328 | |
| 2329 | for (slot = spec->slots; slot->slot; slot++) { |
| 2330 | if (slot->slot >= sizeof(slotoffsets)/sizeof(slotoffsets[0])) { |
| 2331 | PyErr_SetString(PyExc_RuntimeError, "invalid slot offset"); |
| 2332 | goto fail; |
| 2333 | } |
| 2334 | *(void**)(res_start + slotoffsets[slot->slot]) = slot->pfunc; |
| 2335 | } |
| 2336 | |
| 2337 | return (PyObject*)res; |
| 2338 | |
| 2339 | fail: |
| 2340 | Py_DECREF(res); |
| 2341 | return NULL; |
| 2342 | } |
| 2343 | |
| 2344 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2345 | /* Internal API to look for a name through the MRO. |
| 2346 | This returns a borrowed reference, and doesn't set an exception! */ |
| 2347 | PyObject * |
| 2348 | _PyType_Lookup(PyTypeObject *type, PyObject *name) |
| 2349 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2350 | Py_ssize_t i, n; |
| 2351 | PyObject *mro, *res, *base, *dict; |
| 2352 | unsigned int h; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 2353 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2354 | if (MCACHE_CACHEABLE_NAME(name) && |
| 2355 | PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) { |
| 2356 | /* fast path */ |
| 2357 | h = MCACHE_HASH_METHOD(type, name); |
| 2358 | if (method_cache[h].version == type->tp_version_tag && |
| 2359 | method_cache[h].name == name) |
| 2360 | return method_cache[h].value; |
| 2361 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2362 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2363 | /* Look in tp_dict of types in MRO */ |
| 2364 | mro = type->tp_mro; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 2365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2366 | /* If mro is NULL, the type is either not yet initialized |
| 2367 | by PyType_Ready(), or already cleared by type_clear(). |
| 2368 | Either way the safest thing to do is to return NULL. */ |
| 2369 | if (mro == NULL) |
| 2370 | return NULL; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 2371 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2372 | res = NULL; |
| 2373 | assert(PyTuple_Check(mro)); |
| 2374 | n = PyTuple_GET_SIZE(mro); |
| 2375 | for (i = 0; i < n; i++) { |
| 2376 | base = PyTuple_GET_ITEM(mro, i); |
| 2377 | assert(PyType_Check(base)); |
| 2378 | dict = ((PyTypeObject *)base)->tp_dict; |
| 2379 | assert(dict && PyDict_Check(dict)); |
| 2380 | res = PyDict_GetItem(dict, name); |
| 2381 | if (res != NULL) |
| 2382 | break; |
| 2383 | } |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 2384 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2385 | if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) { |
| 2386 | h = MCACHE_HASH_METHOD(type, name); |
| 2387 | method_cache[h].version = type->tp_version_tag; |
| 2388 | method_cache[h].value = res; /* borrowed */ |
| 2389 | Py_INCREF(name); |
| 2390 | Py_DECREF(method_cache[h].name); |
| 2391 | method_cache[h].name = name; |
| 2392 | } |
| 2393 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | /* This is similar to PyObject_GenericGetAttr(), |
| 2397 | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ |
| 2398 | static PyObject * |
| 2399 | type_getattro(PyTypeObject *type, PyObject *name) |
| 2400 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2401 | PyTypeObject *metatype = Py_TYPE(type); |
| 2402 | PyObject *meta_attribute, *attribute; |
| 2403 | descrgetfunc meta_get; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2405 | /* Initialize this type (we'll assume the metatype is initialized) */ |
| 2406 | if (type->tp_dict == NULL) { |
| 2407 | if (PyType_Ready(type) < 0) |
| 2408 | return NULL; |
| 2409 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2410 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2411 | /* No readable descriptor found yet */ |
| 2412 | meta_get = NULL; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2413 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2414 | /* Look for the attribute in the metatype */ |
| 2415 | meta_attribute = _PyType_Lookup(metatype, name); |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2416 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2417 | if (meta_attribute != NULL) { |
| 2418 | meta_get = Py_TYPE(meta_attribute)->tp_descr_get; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2420 | if (meta_get != NULL && PyDescr_IsData(meta_attribute)) { |
| 2421 | /* Data descriptors implement tp_descr_set to intercept |
| 2422 | * writes. Assume the attribute is not overridden in |
| 2423 | * type's tp_dict (and bases): call the descriptor now. |
| 2424 | */ |
| 2425 | return meta_get(meta_attribute, (PyObject *)type, |
| 2426 | (PyObject *)metatype); |
| 2427 | } |
| 2428 | Py_INCREF(meta_attribute); |
| 2429 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2431 | /* No data descriptor found on metatype. Look in tp_dict of this |
| 2432 | * type and its bases */ |
| 2433 | attribute = _PyType_Lookup(type, name); |
| 2434 | if (attribute != NULL) { |
| 2435 | /* Implement descriptor functionality, if any */ |
| 2436 | descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get; |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2437 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2438 | Py_XDECREF(meta_attribute); |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2439 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2440 | if (local_get != NULL) { |
| 2441 | /* NULL 2nd argument indicates the descriptor was |
| 2442 | * found on the target object itself (or a base) */ |
| 2443 | return local_get(attribute, (PyObject *)NULL, |
| 2444 | (PyObject *)type); |
| 2445 | } |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2447 | Py_INCREF(attribute); |
| 2448 | return attribute; |
| 2449 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2450 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2451 | /* No attribute found in local __dict__ (or bases): use the |
| 2452 | * descriptor from the metatype, if any */ |
| 2453 | if (meta_get != NULL) { |
| 2454 | PyObject *res; |
| 2455 | res = meta_get(meta_attribute, (PyObject *)type, |
| 2456 | (PyObject *)metatype); |
| 2457 | Py_DECREF(meta_attribute); |
| 2458 | return res; |
| 2459 | } |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2460 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2461 | /* If an ordinary attribute was found on the metatype, return it now */ |
| 2462 | if (meta_attribute != NULL) { |
| 2463 | return meta_attribute; |
| 2464 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2466 | /* Give up */ |
| 2467 | PyErr_Format(PyExc_AttributeError, |
| 2468 | "type object '%.50s' has no attribute '%U'", |
| 2469 | type->tp_name, name); |
| 2470 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | static int |
| 2474 | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) |
| 2475 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2476 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 2477 | PyErr_Format( |
| 2478 | PyExc_TypeError, |
| 2479 | "can't set attributes of built-in/extension type '%s'", |
| 2480 | type->tp_name); |
| 2481 | return -1; |
| 2482 | } |
| 2483 | if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0) |
| 2484 | return -1; |
| 2485 | return update_slot(type, name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
| 2488 | static void |
| 2489 | type_dealloc(PyTypeObject *type) |
| 2490 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2491 | PyHeapTypeObject *et; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2493 | /* Assert this is a heap-allocated type object */ |
| 2494 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
| 2495 | _PyObject_GC_UNTRACK(type); |
| 2496 | PyObject_ClearWeakRefs((PyObject *)type); |
| 2497 | et = (PyHeapTypeObject *)type; |
| 2498 | Py_XDECREF(type->tp_base); |
| 2499 | Py_XDECREF(type->tp_dict); |
| 2500 | Py_XDECREF(type->tp_bases); |
| 2501 | Py_XDECREF(type->tp_mro); |
| 2502 | Py_XDECREF(type->tp_cache); |
| 2503 | Py_XDECREF(type->tp_subclasses); |
| 2504 | /* A type's tp_doc is heap allocated, unlike the tp_doc slots |
| 2505 | * of most other objects. It's okay to cast it to char *. |
| 2506 | */ |
| 2507 | PyObject_Free((char *)type->tp_doc); |
| 2508 | Py_XDECREF(et->ht_name); |
| 2509 | Py_XDECREF(et->ht_slots); |
| 2510 | Py_TYPE(type)->tp_free((PyObject *)type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2511 | } |
| 2512 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2513 | static PyObject * |
| 2514 | type_subclasses(PyTypeObject *type, PyObject *args_ignored) |
| 2515 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2516 | PyObject *list, *raw, *ref; |
| 2517 | Py_ssize_t i, n; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2518 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2519 | list = PyList_New(0); |
| 2520 | if (list == NULL) |
| 2521 | return NULL; |
| 2522 | raw = type->tp_subclasses; |
| 2523 | if (raw == NULL) |
| 2524 | return list; |
| 2525 | assert(PyList_Check(raw)); |
| 2526 | n = PyList_GET_SIZE(raw); |
| 2527 | for (i = 0; i < n; i++) { |
| 2528 | ref = PyList_GET_ITEM(raw, i); |
| 2529 | assert(PyWeakref_CheckRef(ref)); |
| 2530 | ref = PyWeakref_GET_OBJECT(ref); |
| 2531 | if (ref != Py_None) { |
| 2532 | if (PyList_Append(list, ref) < 0) { |
| 2533 | Py_DECREF(list); |
| 2534 | return NULL; |
| 2535 | } |
| 2536 | } |
| 2537 | } |
| 2538 | return list; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
Guido van Rossum | 4737482 | 2007-08-02 16:48:17 +0000 | [diff] [blame] | 2541 | static PyObject * |
| 2542 | type_prepare(PyObject *self, PyObject *args, PyObject *kwds) |
| 2543 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2544 | return PyDict_New(); |
Guido van Rossum | 4737482 | 2007-08-02 16:48:17 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2547 | static PyMethodDef type_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2548 | {"mro", (PyCFunction)mro_external, METH_NOARGS, |
| 2549 | PyDoc_STR("mro() -> list\nreturn a type's method resolution order")}, |
| 2550 | {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS, |
| 2551 | PyDoc_STR("__subclasses__() -> list of immediate subclasses")}, |
| 2552 | {"__prepare__", (PyCFunction)type_prepare, |
| 2553 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 2554 | PyDoc_STR("__prepare__() -> dict\n" |
| 2555 | "used to create the namespace for the class statement")}, |
| 2556 | {"__instancecheck__", type___instancecheck__, METH_O, |
| 2557 | PyDoc_STR("__instancecheck__() -> check if an object is an instance")}, |
| 2558 | {"__subclasscheck__", type___subclasscheck__, METH_O, |
Alexander Belopolsky | 977a684 | 2010-08-16 20:17:07 +0000 | [diff] [blame] | 2559 | PyDoc_STR("__subclasscheck__() -> check if a class is a subclass")}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2560 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2561 | }; |
| 2562 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2563 | PyDoc_STRVAR(type_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2564 | "type(object) -> the object's type\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2565 | "type(name, bases, dict) -> a new type"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2566 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2567 | static int |
| 2568 | type_traverse(PyTypeObject *type, visitproc visit, void *arg) |
| 2569 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2570 | /* Because of type_is_gc(), the collector only calls this |
| 2571 | for heaptypes. */ |
| 2572 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2573 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2574 | Py_VISIT(type->tp_dict); |
| 2575 | Py_VISIT(type->tp_cache); |
| 2576 | Py_VISIT(type->tp_mro); |
| 2577 | Py_VISIT(type->tp_bases); |
| 2578 | Py_VISIT(type->tp_base); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2579 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2580 | /* There's no need to visit type->tp_subclasses or |
| 2581 | ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved |
| 2582 | in cycles; tp_subclasses is a list of weak references, |
| 2583 | and slots is a tuple of strings. */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2584 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2585 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2586 | } |
| 2587 | |
| 2588 | static int |
| 2589 | type_clear(PyTypeObject *type) |
| 2590 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2591 | /* Because of type_is_gc(), the collector only calls this |
| 2592 | for heaptypes. */ |
| 2593 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2594 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2595 | /* The only field we need to clear is tp_mro, which is part of a |
| 2596 | hard cycle (its first element is the class itself) that won't |
| 2597 | be broken otherwise (it's a tuple and tuples don't have a |
| 2598 | tp_clear handler). None of the other fields need to be |
| 2599 | cleared, and here's why: |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2600 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2601 | tp_dict: |
| 2602 | 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] | 2603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2604 | tp_cache: |
| 2605 | Not used; if it were, it would be a dict. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2606 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2607 | tp_bases, tp_base: |
| 2608 | If these are involved in a cycle, there must be at least |
| 2609 | one other, mutable object in the cycle, e.g. a base |
| 2610 | class's dict; the cycle will be broken that way. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2611 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2612 | tp_subclasses: |
| 2613 | A list of weak references can't be part of a cycle; and |
| 2614 | lists have their own tp_clear. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2615 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2616 | slots (in PyHeapTypeObject): |
| 2617 | A tuple of strings can't be part of a cycle. |
| 2618 | */ |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 2619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2620 | Py_CLEAR(type->tp_mro); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2622 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2623 | } |
| 2624 | |
| 2625 | static int |
| 2626 | type_is_gc(PyTypeObject *type) |
| 2627 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2628 | return type->tp_flags & Py_TPFLAGS_HEAPTYPE; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 2629 | } |
| 2630 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2631 | PyTypeObject PyType_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2632 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2633 | "type", /* tp_name */ |
| 2634 | sizeof(PyHeapTypeObject), /* tp_basicsize */ |
| 2635 | sizeof(PyMemberDef), /* tp_itemsize */ |
| 2636 | (destructor)type_dealloc, /* tp_dealloc */ |
| 2637 | 0, /* tp_print */ |
| 2638 | 0, /* tp_getattr */ |
| 2639 | 0, /* tp_setattr */ |
| 2640 | 0, /* tp_reserved */ |
| 2641 | (reprfunc)type_repr, /* tp_repr */ |
| 2642 | 0, /* tp_as_number */ |
| 2643 | 0, /* tp_as_sequence */ |
| 2644 | 0, /* tp_as_mapping */ |
| 2645 | 0, /* tp_hash */ |
| 2646 | (ternaryfunc)type_call, /* tp_call */ |
| 2647 | 0, /* tp_str */ |
| 2648 | (getattrofunc)type_getattro, /* tp_getattro */ |
| 2649 | (setattrofunc)type_setattro, /* tp_setattro */ |
| 2650 | 0, /* tp_as_buffer */ |
| 2651 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2652 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */ |
| 2653 | type_doc, /* tp_doc */ |
| 2654 | (traverseproc)type_traverse, /* tp_traverse */ |
| 2655 | (inquiry)type_clear, /* tp_clear */ |
| 2656 | 0, /* tp_richcompare */ |
| 2657 | offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ |
| 2658 | 0, /* tp_iter */ |
| 2659 | 0, /* tp_iternext */ |
| 2660 | type_methods, /* tp_methods */ |
| 2661 | type_members, /* tp_members */ |
| 2662 | type_getsets, /* tp_getset */ |
| 2663 | 0, /* tp_base */ |
| 2664 | 0, /* tp_dict */ |
| 2665 | 0, /* tp_descr_get */ |
| 2666 | 0, /* tp_descr_set */ |
| 2667 | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ |
| 2668 | type_init, /* tp_init */ |
| 2669 | 0, /* tp_alloc */ |
| 2670 | type_new, /* tp_new */ |
| 2671 | PyObject_GC_Del, /* tp_free */ |
| 2672 | (inquiry)type_is_gc, /* tp_is_gc */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2673 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2674 | |
| 2675 | |
| 2676 | /* The base type of all types (eventually)... except itself. */ |
| 2677 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2678 | /* You may wonder why object.__new__() only complains about arguments |
| 2679 | when object.__init__() is not overridden, and vice versa. |
| 2680 | |
| 2681 | Consider the use cases: |
| 2682 | |
| 2683 | 1. When neither is overridden, we want to hear complaints about |
| 2684 | excess (i.e., any) arguments, since their presence could |
| 2685 | indicate there's a bug. |
| 2686 | |
| 2687 | 2. When defining an Immutable type, we are likely to override only |
| 2688 | __new__(), since __init__() is called too late to initialize an |
| 2689 | Immutable object. Since __new__() defines the signature for the |
| 2690 | type, it would be a pain to have to override __init__() just to |
| 2691 | stop it from complaining about excess arguments. |
| 2692 | |
| 2693 | 3. When defining a Mutable type, we are likely to override only |
| 2694 | __init__(). So here the converse reasoning applies: we don't |
| 2695 | want to have to override __new__() just to stop it from |
| 2696 | complaining. |
| 2697 | |
| 2698 | 4. When __init__() is overridden, and the subclass __init__() calls |
| 2699 | object.__init__(), the latter should complain about excess |
| 2700 | arguments; ditto for __new__(). |
| 2701 | |
| 2702 | Use cases 2 and 3 make it unattractive to unconditionally check for |
| 2703 | excess arguments. The best solution that addresses all four use |
| 2704 | cases is as follows: __init__() complains about excess arguments |
| 2705 | unless __new__() is overridden and __init__() is not overridden |
| 2706 | (IOW, if __init__() is overridden or __new__() is not overridden); |
| 2707 | symmetrically, __new__() complains about excess arguments unless |
| 2708 | __init__() is overridden and __new__() is not overridden |
| 2709 | (IOW, if __new__() is overridden or __init__() is not overridden). |
| 2710 | |
| 2711 | However, for backwards compatibility, this breaks too much code. |
| 2712 | Therefore, in 2.6, we'll *warn* about excess arguments when both |
| 2713 | methods are overridden; for all other cases we'll use the above |
| 2714 | rules. |
| 2715 | |
| 2716 | */ |
| 2717 | |
| 2718 | /* Forward */ |
| 2719 | static PyObject * |
| 2720 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 2721 | |
| 2722 | static int |
| 2723 | excess_args(PyObject *args, PyObject *kwds) |
| 2724 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2725 | return PyTuple_GET_SIZE(args) || |
| 2726 | (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2727 | } |
| 2728 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2729 | static int |
| 2730 | object_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2731 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2732 | int err = 0; |
| 2733 | if (excess_args(args, kwds)) { |
| 2734 | PyTypeObject *type = Py_TYPE(self); |
| 2735 | if (type->tp_init != object_init && |
| 2736 | type->tp_new != object_new) |
| 2737 | { |
| 2738 | err = PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2739 | "object.__init__() takes no parameters", |
| 2740 | 1); |
| 2741 | } |
| 2742 | else if (type->tp_init != object_init || |
| 2743 | type->tp_new == object_new) |
| 2744 | { |
| 2745 | PyErr_SetString(PyExc_TypeError, |
| 2746 | "object.__init__() takes no parameters"); |
| 2747 | err = -1; |
| 2748 | } |
| 2749 | } |
| 2750 | return err; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2753 | static PyObject * |
| 2754 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2755 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2756 | int err = 0; |
| 2757 | if (excess_args(args, kwds)) { |
| 2758 | if (type->tp_new != object_new && |
| 2759 | type->tp_init != object_init) |
| 2760 | { |
| 2761 | err = PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2762 | "object.__new__() takes no parameters", |
| 2763 | 1); |
| 2764 | } |
| 2765 | else if (type->tp_new != object_new || |
| 2766 | type->tp_init == object_init) |
| 2767 | { |
| 2768 | PyErr_SetString(PyExc_TypeError, |
| 2769 | "object.__new__() takes no parameters"); |
| 2770 | err = -1; |
| 2771 | } |
| 2772 | } |
| 2773 | if (err < 0) |
| 2774 | return NULL; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 2775 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2776 | if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) { |
| 2777 | static PyObject *comma = NULL; |
| 2778 | PyObject *abstract_methods = NULL; |
| 2779 | PyObject *builtins; |
| 2780 | PyObject *sorted; |
| 2781 | PyObject *sorted_methods = NULL; |
| 2782 | PyObject *joined = NULL; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 2783 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2784 | /* Compute ", ".join(sorted(type.__abstractmethods__)) |
| 2785 | into joined. */ |
| 2786 | abstract_methods = type_abstractmethods(type, NULL); |
| 2787 | if (abstract_methods == NULL) |
| 2788 | goto error; |
| 2789 | builtins = PyEval_GetBuiltins(); |
| 2790 | if (builtins == NULL) |
| 2791 | goto error; |
| 2792 | sorted = PyDict_GetItemString(builtins, "sorted"); |
| 2793 | if (sorted == NULL) |
| 2794 | goto error; |
| 2795 | sorted_methods = PyObject_CallFunctionObjArgs(sorted, |
| 2796 | abstract_methods, |
| 2797 | NULL); |
| 2798 | if (sorted_methods == NULL) |
| 2799 | goto error; |
| 2800 | if (comma == NULL) { |
| 2801 | comma = PyUnicode_InternFromString(", "); |
| 2802 | if (comma == NULL) |
| 2803 | goto error; |
| 2804 | } |
| 2805 | joined = PyObject_CallMethod(comma, "join", |
| 2806 | "O", sorted_methods); |
| 2807 | if (joined == NULL) |
| 2808 | goto error; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 2809 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2810 | PyErr_Format(PyExc_TypeError, |
| 2811 | "Can't instantiate abstract class %s " |
| 2812 | "with abstract methods %U", |
| 2813 | type->tp_name, |
| 2814 | joined); |
| 2815 | error: |
| 2816 | Py_XDECREF(joined); |
| 2817 | Py_XDECREF(sorted_methods); |
| 2818 | Py_XDECREF(abstract_methods); |
| 2819 | return NULL; |
| 2820 | } |
| 2821 | return type->tp_alloc(type, 0); |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 2822 | } |
| 2823 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2824 | static void |
| 2825 | object_dealloc(PyObject *self) |
| 2826 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2827 | Py_TYPE(self)->tp_free(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2830 | static PyObject * |
| 2831 | object_repr(PyObject *self) |
| 2832 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2833 | PyTypeObject *type; |
| 2834 | PyObject *mod, *name, *rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2835 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2836 | type = Py_TYPE(self); |
| 2837 | mod = type_module(type, NULL); |
| 2838 | if (mod == NULL) |
| 2839 | PyErr_Clear(); |
| 2840 | else if (!PyUnicode_Check(mod)) { |
| 2841 | Py_DECREF(mod); |
| 2842 | mod = NULL; |
| 2843 | } |
| 2844 | name = type_name(type, NULL); |
| 2845 | if (name == NULL) |
| 2846 | return NULL; |
| 2847 | if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins")) |
| 2848 | rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self); |
| 2849 | else |
| 2850 | rtn = PyUnicode_FromFormat("<%s object at %p>", |
| 2851 | type->tp_name, self); |
| 2852 | Py_XDECREF(mod); |
| 2853 | Py_DECREF(name); |
| 2854 | return rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 2855 | } |
| 2856 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2857 | static PyObject * |
| 2858 | object_str(PyObject *self) |
| 2859 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2860 | unaryfunc f; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2861 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2862 | f = Py_TYPE(self)->tp_repr; |
| 2863 | if (f == NULL) |
| 2864 | f = object_repr; |
| 2865 | return f(self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2866 | } |
| 2867 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2868 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2869 | object_richcompare(PyObject *self, PyObject *other, int op) |
| 2870 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2871 | PyObject *res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2872 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2873 | switch (op) { |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2874 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2875 | case Py_EQ: |
| 2876 | /* Return NotImplemented instead of False, so if two |
| 2877 | objects are compared, both get a chance at the |
| 2878 | comparison. See issue #1393. */ |
| 2879 | res = (self == other) ? Py_True : Py_NotImplemented; |
| 2880 | Py_INCREF(res); |
| 2881 | break; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2882 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2883 | case Py_NE: |
| 2884 | /* By default, != returns the opposite of ==, |
| 2885 | unless the latter returns NotImplemented. */ |
| 2886 | res = PyObject_RichCompare(self, other, Py_EQ); |
| 2887 | if (res != NULL && res != Py_NotImplemented) { |
| 2888 | int ok = PyObject_IsTrue(res); |
| 2889 | Py_DECREF(res); |
| 2890 | if (ok < 0) |
| 2891 | res = NULL; |
| 2892 | else { |
| 2893 | if (ok) |
| 2894 | res = Py_False; |
| 2895 | else |
| 2896 | res = Py_True; |
| 2897 | Py_INCREF(res); |
| 2898 | } |
| 2899 | } |
| 2900 | break; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2901 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2902 | default: |
| 2903 | res = Py_NotImplemented; |
| 2904 | Py_INCREF(res); |
| 2905 | break; |
| 2906 | } |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2907 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2908 | return res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
| 2911 | static PyObject * |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2912 | object_get_class(PyObject *self, void *closure) |
| 2913 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2914 | Py_INCREF(Py_TYPE(self)); |
| 2915 | return (PyObject *)(Py_TYPE(self)); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
| 2918 | static int |
| 2919 | equiv_structs(PyTypeObject *a, PyTypeObject *b) |
| 2920 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2921 | return a == b || |
| 2922 | (a != NULL && |
| 2923 | b != NULL && |
| 2924 | a->tp_basicsize == b->tp_basicsize && |
| 2925 | a->tp_itemsize == b->tp_itemsize && |
| 2926 | a->tp_dictoffset == b->tp_dictoffset && |
| 2927 | a->tp_weaklistoffset == b->tp_weaklistoffset && |
| 2928 | ((a->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 2929 | (b->tp_flags & Py_TPFLAGS_HAVE_GC))); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | static int |
| 2933 | same_slots_added(PyTypeObject *a, PyTypeObject *b) |
| 2934 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2935 | PyTypeObject *base = a->tp_base; |
| 2936 | Py_ssize_t size; |
| 2937 | PyObject *slots_a, *slots_b; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2938 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2939 | if (base != b->tp_base) |
| 2940 | return 0; |
| 2941 | if (equiv_structs(a, base) && equiv_structs(b, base)) |
| 2942 | return 1; |
| 2943 | size = base->tp_basicsize; |
| 2944 | if (a->tp_dictoffset == size && b->tp_dictoffset == size) |
| 2945 | size += sizeof(PyObject *); |
| 2946 | if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size) |
| 2947 | size += sizeof(PyObject *); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2949 | /* Check slots compliance */ |
| 2950 | slots_a = ((PyHeapTypeObject *)a)->ht_slots; |
| 2951 | slots_b = ((PyHeapTypeObject *)b)->ht_slots; |
| 2952 | if (slots_a && slots_b) { |
| 2953 | if (PyObject_RichCompareBool(slots_a, slots_b, Py_EQ) != 1) |
| 2954 | return 0; |
| 2955 | size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a); |
| 2956 | } |
| 2957 | return size == a->tp_basicsize && size == b->tp_basicsize; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2958 | } |
| 2959 | |
| 2960 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2961 | compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2962 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2963 | PyTypeObject *newbase, *oldbase; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2964 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2965 | if (newto->tp_dealloc != oldto->tp_dealloc || |
| 2966 | newto->tp_free != oldto->tp_free) |
| 2967 | { |
| 2968 | PyErr_Format(PyExc_TypeError, |
| 2969 | "%s assignment: " |
| 2970 | "'%s' deallocator differs from '%s'", |
| 2971 | attr, |
| 2972 | newto->tp_name, |
| 2973 | oldto->tp_name); |
| 2974 | return 0; |
| 2975 | } |
| 2976 | newbase = newto; |
| 2977 | oldbase = oldto; |
| 2978 | while (equiv_structs(newbase, newbase->tp_base)) |
| 2979 | newbase = newbase->tp_base; |
| 2980 | while (equiv_structs(oldbase, oldbase->tp_base)) |
| 2981 | oldbase = oldbase->tp_base; |
| 2982 | if (newbase != oldbase && |
| 2983 | (newbase->tp_base != oldbase->tp_base || |
| 2984 | !same_slots_added(newbase, oldbase))) { |
| 2985 | PyErr_Format(PyExc_TypeError, |
| 2986 | "%s assignment: " |
| 2987 | "'%s' object layout differs from '%s'", |
| 2988 | attr, |
| 2989 | newto->tp_name, |
| 2990 | oldto->tp_name); |
| 2991 | return 0; |
| 2992 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 2993 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2994 | return 1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 2995 | } |
| 2996 | |
| 2997 | static int |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2998 | object_set_class(PyObject *self, PyObject *value, void *closure) |
| 2999 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3000 | PyTypeObject *oldto = Py_TYPE(self); |
| 3001 | PyTypeObject *newto; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3002 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3003 | if (value == NULL) { |
| 3004 | PyErr_SetString(PyExc_TypeError, |
| 3005 | "can't delete __class__ attribute"); |
| 3006 | return -1; |
| 3007 | } |
| 3008 | if (!PyType_Check(value)) { |
| 3009 | PyErr_Format(PyExc_TypeError, |
| 3010 | "__class__ must be set to new-style class, not '%s' object", |
| 3011 | Py_TYPE(value)->tp_name); |
| 3012 | return -1; |
| 3013 | } |
| 3014 | newto = (PyTypeObject *)value; |
| 3015 | if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) || |
| 3016 | !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 3017 | { |
| 3018 | PyErr_Format(PyExc_TypeError, |
| 3019 | "__class__ assignment: only for heap types"); |
| 3020 | return -1; |
| 3021 | } |
| 3022 | if (compatible_for_assignment(newto, oldto, "__class__")) { |
| 3023 | Py_INCREF(newto); |
| 3024 | Py_TYPE(self) = newto; |
| 3025 | Py_DECREF(oldto); |
| 3026 | return 0; |
| 3027 | } |
| 3028 | else { |
| 3029 | return -1; |
| 3030 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
| 3033 | static PyGetSetDef object_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3034 | {"__class__", object_get_class, object_set_class, |
| 3035 | PyDoc_STR("the object's class")}, |
| 3036 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3037 | }; |
| 3038 | |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 3039 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3040 | /* Stuff to implement __reduce_ex__ for pickle protocols >= 2. |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3041 | We fall back to helpers in copyreg for: |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3042 | - pickle protocols < 2 |
| 3043 | - calculating the list of slot names (done only once per class) |
| 3044 | - the __newobj__ function (which is used as a token but never called) |
| 3045 | */ |
| 3046 | |
| 3047 | static PyObject * |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3048 | import_copyreg(void) |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3049 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3050 | static PyObject *copyreg_str; |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3052 | if (!copyreg_str) { |
| 3053 | copyreg_str = PyUnicode_InternFromString("copyreg"); |
| 3054 | if (copyreg_str == NULL) |
| 3055 | return NULL; |
| 3056 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3057 | |
Antoine Pitrou | ff150f2 | 2010-10-22 21:41:05 +0000 | [diff] [blame] | 3058 | return PyImport_Import(copyreg_str); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3059 | } |
| 3060 | |
| 3061 | static PyObject * |
| 3062 | slotnames(PyObject *cls) |
| 3063 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3064 | PyObject *clsdict; |
| 3065 | PyObject *copyreg; |
| 3066 | PyObject *slotnames; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3067 | |
Antoine Pitrou | ff150f2 | 2010-10-22 21:41:05 +0000 | [diff] [blame] | 3068 | if (!PyType_Check(cls)) { |
| 3069 | Py_INCREF(Py_None); |
| 3070 | return Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3071 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3072 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3073 | clsdict = ((PyTypeObject *)cls)->tp_dict; |
Antoine Pitrou | ff150f2 | 2010-10-22 21:41:05 +0000 | [diff] [blame] | 3074 | slotnames = PyDict_GetItemString(clsdict, "__slotnames__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3075 | if (slotnames != NULL && PyList_Check(slotnames)) { |
| 3076 | Py_INCREF(slotnames); |
| 3077 | return slotnames; |
| 3078 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3079 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3080 | copyreg = import_copyreg(); |
| 3081 | if (copyreg == NULL) |
| 3082 | return NULL; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3083 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3084 | slotnames = PyObject_CallMethod(copyreg, "_slotnames", "O", cls); |
| 3085 | Py_DECREF(copyreg); |
| 3086 | if (slotnames != NULL && |
| 3087 | slotnames != Py_None && |
| 3088 | !PyList_Check(slotnames)) |
| 3089 | { |
| 3090 | PyErr_SetString(PyExc_TypeError, |
| 3091 | "copyreg._slotnames didn't return a list or None"); |
| 3092 | Py_DECREF(slotnames); |
| 3093 | slotnames = NULL; |
| 3094 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3095 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3096 | return slotnames; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
| 3099 | static PyObject * |
| 3100 | reduce_2(PyObject *obj) |
| 3101 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3102 | PyObject *cls, *getnewargs; |
| 3103 | PyObject *args = NULL, *args2 = NULL; |
| 3104 | PyObject *getstate = NULL, *state = NULL, *names = NULL; |
| 3105 | PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL; |
| 3106 | PyObject *copyreg = NULL, *newobj = NULL, *res = NULL; |
| 3107 | Py_ssize_t i, n; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3108 | |
Antoine Pitrou | ff150f2 | 2010-10-22 21:41:05 +0000 | [diff] [blame] | 3109 | cls = PyObject_GetAttrString(obj, "__class__"); |
| 3110 | if (cls == NULL) |
| 3111 | return NULL; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3112 | |
Antoine Pitrou | ff150f2 | 2010-10-22 21:41:05 +0000 | [diff] [blame] | 3113 | getnewargs = PyObject_GetAttrString(obj, "__getnewargs__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3114 | if (getnewargs != NULL) { |
| 3115 | args = PyObject_CallObject(getnewargs, NULL); |
| 3116 | Py_DECREF(getnewargs); |
| 3117 | if (args != NULL && !PyTuple_Check(args)) { |
| 3118 | PyErr_Format(PyExc_TypeError, |
| 3119 | "__getnewargs__ should return a tuple, " |
| 3120 | "not '%.200s'", Py_TYPE(args)->tp_name); |
| 3121 | goto end; |
| 3122 | } |
| 3123 | } |
| 3124 | else { |
| 3125 | PyErr_Clear(); |
| 3126 | args = PyTuple_New(0); |
| 3127 | } |
| 3128 | if (args == NULL) |
| 3129 | goto end; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3130 | |
Antoine Pitrou | ff150f2 | 2010-10-22 21:41:05 +0000 | [diff] [blame] | 3131 | getstate = PyObject_GetAttrString(obj, "__getstate__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3132 | if (getstate != NULL) { |
| 3133 | state = PyObject_CallObject(getstate, NULL); |
| 3134 | Py_DECREF(getstate); |
| 3135 | if (state == NULL) |
| 3136 | goto end; |
| 3137 | } |
| 3138 | else { |
| 3139 | PyErr_Clear(); |
Antoine Pitrou | ff150f2 | 2010-10-22 21:41:05 +0000 | [diff] [blame] | 3140 | state = PyObject_GetAttrString(obj, "__dict__"); |
| 3141 | if (state == NULL) { |
| 3142 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3143 | state = Py_None; |
Antoine Pitrou | ff150f2 | 2010-10-22 21:41:05 +0000 | [diff] [blame] | 3144 | Py_INCREF(state); |
| 3145 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3146 | names = slotnames(cls); |
| 3147 | if (names == NULL) |
| 3148 | goto end; |
Antoine Pitrou | ff150f2 | 2010-10-22 21:41:05 +0000 | [diff] [blame] | 3149 | if (names != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3150 | assert(PyList_Check(names)); |
| 3151 | slots = PyDict_New(); |
| 3152 | if (slots == NULL) |
| 3153 | goto end; |
| 3154 | n = 0; |
| 3155 | /* Can't pre-compute the list size; the list |
| 3156 | is stored on the class so accessible to other |
| 3157 | threads, which may be run by DECREF */ |
| 3158 | for (i = 0; i < PyList_GET_SIZE(names); i++) { |
| 3159 | PyObject *name, *value; |
| 3160 | name = PyList_GET_ITEM(names, i); |
| 3161 | value = PyObject_GetAttr(obj, name); |
| 3162 | if (value == NULL) |
| 3163 | PyErr_Clear(); |
| 3164 | else { |
| 3165 | int err = PyDict_SetItem(slots, name, |
| 3166 | value); |
| 3167 | Py_DECREF(value); |
| 3168 | if (err) |
| 3169 | goto end; |
| 3170 | n++; |
| 3171 | } |
| 3172 | } |
| 3173 | if (n) { |
| 3174 | state = Py_BuildValue("(NO)", state, slots); |
| 3175 | if (state == NULL) |
| 3176 | goto end; |
| 3177 | } |
| 3178 | } |
| 3179 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3181 | if (!PyList_Check(obj)) { |
| 3182 | listitems = Py_None; |
| 3183 | Py_INCREF(listitems); |
| 3184 | } |
| 3185 | else { |
| 3186 | listitems = PyObject_GetIter(obj); |
| 3187 | if (listitems == NULL) |
| 3188 | goto end; |
| 3189 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3190 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3191 | if (!PyDict_Check(obj)) { |
| 3192 | dictitems = Py_None; |
| 3193 | Py_INCREF(dictitems); |
| 3194 | } |
| 3195 | else { |
| 3196 | PyObject *items = PyObject_CallMethod(obj, "items", ""); |
| 3197 | if (items == NULL) |
| 3198 | goto end; |
| 3199 | dictitems = PyObject_GetIter(items); |
| 3200 | Py_DECREF(items); |
| 3201 | if (dictitems == NULL) |
| 3202 | goto end; |
| 3203 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3205 | copyreg = import_copyreg(); |
| 3206 | if (copyreg == NULL) |
| 3207 | goto end; |
Antoine Pitrou | ff150f2 | 2010-10-22 21:41:05 +0000 | [diff] [blame] | 3208 | newobj = PyObject_GetAttrString(copyreg, "__newobj__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3209 | if (newobj == NULL) |
| 3210 | goto end; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3211 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3212 | n = PyTuple_GET_SIZE(args); |
| 3213 | args2 = PyTuple_New(n+1); |
| 3214 | if (args2 == NULL) |
| 3215 | goto end; |
| 3216 | PyTuple_SET_ITEM(args2, 0, cls); |
Antoine Pitrou | ff150f2 | 2010-10-22 21:41:05 +0000 | [diff] [blame] | 3217 | cls = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3218 | for (i = 0; i < n; i++) { |
| 3219 | PyObject *v = PyTuple_GET_ITEM(args, i); |
| 3220 | Py_INCREF(v); |
| 3221 | PyTuple_SET_ITEM(args2, i+1, v); |
| 3222 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3223 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3224 | res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3225 | |
| 3226 | end: |
Antoine Pitrou | ff150f2 | 2010-10-22 21:41:05 +0000 | [diff] [blame] | 3227 | Py_XDECREF(cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3228 | Py_XDECREF(args); |
| 3229 | Py_XDECREF(args2); |
| 3230 | Py_XDECREF(slots); |
| 3231 | Py_XDECREF(state); |
| 3232 | Py_XDECREF(names); |
| 3233 | Py_XDECREF(listitems); |
| 3234 | Py_XDECREF(dictitems); |
| 3235 | Py_XDECREF(copyreg); |
| 3236 | Py_XDECREF(newobj); |
| 3237 | return res; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3238 | } |
| 3239 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3240 | /* |
| 3241 | * There were two problems when object.__reduce__ and object.__reduce_ex__ |
| 3242 | * were implemented in the same function: |
| 3243 | * - trying to pickle an object with a custom __reduce__ method that |
| 3244 | * fell back to object.__reduce__ in certain circumstances led to |
| 3245 | * infinite recursion at Python level and eventual RuntimeError. |
| 3246 | * - Pickling objects that lied about their type by overwriting the |
| 3247 | * __class__ descriptor could lead to infinite recursion at C level |
| 3248 | * and eventual segfault. |
| 3249 | * |
| 3250 | * Because of backwards compatibility, the two methods still have to |
| 3251 | * behave in the same way, even if this is not required by the pickle |
| 3252 | * protocol. This common functionality was moved to the _common_reduce |
| 3253 | * function. |
| 3254 | */ |
| 3255 | static PyObject * |
| 3256 | _common_reduce(PyObject *self, int proto) |
| 3257 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3258 | PyObject *copyreg, *res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3260 | if (proto >= 2) |
| 3261 | return reduce_2(self); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3263 | copyreg = import_copyreg(); |
| 3264 | if (!copyreg) |
| 3265 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3267 | res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto); |
| 3268 | Py_DECREF(copyreg); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3270 | return res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3271 | } |
| 3272 | |
| 3273 | static PyObject * |
| 3274 | object_reduce(PyObject *self, PyObject *args) |
| 3275 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3276 | int proto = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3278 | if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto)) |
| 3279 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3281 | return _common_reduce(self, proto); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3282 | } |
| 3283 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3284 | static PyObject * |
| 3285 | object_reduce_ex(PyObject *self, PyObject *args) |
| 3286 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3287 | PyObject *reduce, *res; |
| 3288 | int proto = 0; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3290 | if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto)) |
| 3291 | return NULL; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3292 | |
Antoine Pitrou | ff150f2 | 2010-10-22 21:41:05 +0000 | [diff] [blame] | 3293 | reduce = PyObject_GetAttrString(self, "__reduce__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3294 | if (reduce == NULL) |
| 3295 | PyErr_Clear(); |
| 3296 | else { |
Antoine Pitrou | ff150f2 | 2010-10-22 21:41:05 +0000 | [diff] [blame] | 3297 | PyObject *cls, *clsreduce, *objreduce; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3298 | int override; |
Antoine Pitrou | ff150f2 | 2010-10-22 21:41:05 +0000 | [diff] [blame] | 3299 | cls = PyObject_GetAttrString(self, "__class__"); |
| 3300 | if (cls == NULL) { |
| 3301 | Py_DECREF(reduce); |
| 3302 | return NULL; |
| 3303 | } |
| 3304 | clsreduce = PyObject_GetAttrString(cls, "__reduce__"); |
| 3305 | Py_DECREF(cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3306 | if (clsreduce == NULL) { |
| 3307 | Py_DECREF(reduce); |
| 3308 | return NULL; |
| 3309 | } |
Antoine Pitrou | ff150f2 | 2010-10-22 21:41:05 +0000 | [diff] [blame] | 3310 | objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict, |
| 3311 | "__reduce__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3312 | override = (clsreduce != objreduce); |
| 3313 | Py_DECREF(clsreduce); |
| 3314 | if (override) { |
| 3315 | res = PyObject_CallObject(reduce, NULL); |
| 3316 | Py_DECREF(reduce); |
| 3317 | return res; |
| 3318 | } |
| 3319 | else |
| 3320 | Py_DECREF(reduce); |
| 3321 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3323 | return _common_reduce(self, proto); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3324 | } |
| 3325 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3326 | static PyObject * |
| 3327 | object_subclasshook(PyObject *cls, PyObject *args) |
| 3328 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3329 | Py_INCREF(Py_NotImplemented); |
| 3330 | return Py_NotImplemented; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3331 | } |
| 3332 | |
| 3333 | PyDoc_STRVAR(object_subclasshook_doc, |
| 3334 | "Abstract classes can override this to customize issubclass().\n" |
| 3335 | "\n" |
| 3336 | "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n" |
| 3337 | "It should return True, False or NotImplemented. If it returns\n" |
| 3338 | "NotImplemented, the normal algorithm is used. Otherwise, it\n" |
| 3339 | "overrides the normal algorithm (and the outcome is cached).\n"); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3340 | |
| 3341 | /* |
| 3342 | from PEP 3101, this code implements: |
| 3343 | |
| 3344 | class object: |
| 3345 | def __format__(self, format_spec): |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3346 | return format(str(self), format_spec) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3347 | */ |
| 3348 | static PyObject * |
| 3349 | object_format(PyObject *self, PyObject *args) |
| 3350 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3351 | PyObject *format_spec; |
| 3352 | PyObject *self_as_str = NULL; |
| 3353 | PyObject *result = NULL; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3355 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 3356 | return NULL; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3357 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3358 | self_as_str = PyObject_Str(self); |
Eric Smith | e4d6317 | 2010-09-13 20:48:43 +0000 | [diff] [blame] | 3359 | if (self_as_str != NULL) { |
| 3360 | /* Issue 7994: If we're converting to a string, we |
| 3361 | should reject format specifications */ |
| 3362 | if (PyUnicode_GET_SIZE(format_spec) > 0) { |
| 3363 | if (PyErr_WarnEx(PyExc_PendingDeprecationWarning, |
| 3364 | "object.__format__ with a non-empty format " |
| 3365 | "string is deprecated", 1) < 0) { |
| 3366 | goto done; |
| 3367 | } |
| 3368 | /* Eventually this will become an error: |
| 3369 | PyErr_Format(PyExc_TypeError, |
| 3370 | "non-empty format string passed to object.__format__"); |
| 3371 | goto done; |
| 3372 | */ |
| 3373 | } |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3374 | |
Eric Smith | e4d6317 | 2010-09-13 20:48:43 +0000 | [diff] [blame] | 3375 | result = PyObject_Format(self_as_str, format_spec); |
| 3376 | } |
| 3377 | |
| 3378 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3379 | Py_XDECREF(self_as_str); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3381 | return result; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3384 | static PyObject * |
| 3385 | object_sizeof(PyObject *self, PyObject *args) |
| 3386 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3387 | Py_ssize_t res, isize; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3389 | res = 0; |
| 3390 | isize = self->ob_type->tp_itemsize; |
| 3391 | if (isize > 0) |
| 3392 | res = Py_SIZE(self->ob_type) * isize; |
| 3393 | res += self->ob_type->tp_basicsize; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3395 | return PyLong_FromSsize_t(res); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3398 | static PyMethodDef object_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3399 | {"__reduce_ex__", object_reduce_ex, METH_VARARGS, |
| 3400 | PyDoc_STR("helper for pickle")}, |
| 3401 | {"__reduce__", object_reduce, METH_VARARGS, |
| 3402 | PyDoc_STR("helper for pickle")}, |
| 3403 | {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS, |
| 3404 | object_subclasshook_doc}, |
| 3405 | {"__format__", object_format, METH_VARARGS, |
| 3406 | PyDoc_STR("default object formatter")}, |
| 3407 | {"__sizeof__", object_sizeof, METH_NOARGS, |
| 3408 | PyDoc_STR("__sizeof__() -> size of object in memory, in bytes")}, |
| 3409 | {0} |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3410 | }; |
| 3411 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3412 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3413 | PyTypeObject PyBaseObject_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3414 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3415 | "object", /* tp_name */ |
| 3416 | sizeof(PyObject), /* tp_basicsize */ |
| 3417 | 0, /* tp_itemsize */ |
| 3418 | object_dealloc, /* tp_dealloc */ |
| 3419 | 0, /* tp_print */ |
| 3420 | 0, /* tp_getattr */ |
| 3421 | 0, /* tp_setattr */ |
| 3422 | 0, /* tp_reserved */ |
| 3423 | object_repr, /* tp_repr */ |
| 3424 | 0, /* tp_as_number */ |
| 3425 | 0, /* tp_as_sequence */ |
| 3426 | 0, /* tp_as_mapping */ |
| 3427 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
| 3428 | 0, /* tp_call */ |
| 3429 | object_str, /* tp_str */ |
| 3430 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3431 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 3432 | 0, /* tp_as_buffer */ |
| 3433 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3434 | PyDoc_STR("The most base type"), /* tp_doc */ |
| 3435 | 0, /* tp_traverse */ |
| 3436 | 0, /* tp_clear */ |
| 3437 | object_richcompare, /* tp_richcompare */ |
| 3438 | 0, /* tp_weaklistoffset */ |
| 3439 | 0, /* tp_iter */ |
| 3440 | 0, /* tp_iternext */ |
| 3441 | object_methods, /* tp_methods */ |
| 3442 | 0, /* tp_members */ |
| 3443 | object_getsets, /* tp_getset */ |
| 3444 | 0, /* tp_base */ |
| 3445 | 0, /* tp_dict */ |
| 3446 | 0, /* tp_descr_get */ |
| 3447 | 0, /* tp_descr_set */ |
| 3448 | 0, /* tp_dictoffset */ |
| 3449 | object_init, /* tp_init */ |
| 3450 | PyType_GenericAlloc, /* tp_alloc */ |
| 3451 | object_new, /* tp_new */ |
| 3452 | PyObject_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3453 | }; |
| 3454 | |
| 3455 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 3456 | /* 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] | 3457 | |
| 3458 | static int |
| 3459 | add_methods(PyTypeObject *type, PyMethodDef *meth) |
| 3460 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3461 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3462 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3463 | for (; meth->ml_name != NULL; meth++) { |
| 3464 | PyObject *descr; |
| 3465 | if (PyDict_GetItemString(dict, meth->ml_name) && |
| 3466 | !(meth->ml_flags & METH_COEXIST)) |
| 3467 | continue; |
| 3468 | if (meth->ml_flags & METH_CLASS) { |
| 3469 | if (meth->ml_flags & METH_STATIC) { |
| 3470 | PyErr_SetString(PyExc_ValueError, |
| 3471 | "method cannot be both class and static"); |
| 3472 | return -1; |
| 3473 | } |
| 3474 | descr = PyDescr_NewClassMethod(type, meth); |
| 3475 | } |
| 3476 | else if (meth->ml_flags & METH_STATIC) { |
| 3477 | PyObject *cfunc = PyCFunction_New(meth, NULL); |
| 3478 | if (cfunc == NULL) |
| 3479 | return -1; |
| 3480 | descr = PyStaticMethod_New(cfunc); |
| 3481 | Py_DECREF(cfunc); |
| 3482 | } |
| 3483 | else { |
| 3484 | descr = PyDescr_NewMethod(type, meth); |
| 3485 | } |
| 3486 | if (descr == NULL) |
| 3487 | return -1; |
| 3488 | if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0) |
| 3489 | return -1; |
| 3490 | Py_DECREF(descr); |
| 3491 | } |
| 3492 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3493 | } |
| 3494 | |
| 3495 | static int |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 3496 | add_members(PyTypeObject *type, PyMemberDef *memb) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3497 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3498 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3500 | for (; memb->name != NULL; memb++) { |
| 3501 | PyObject *descr; |
| 3502 | if (PyDict_GetItemString(dict, memb->name)) |
| 3503 | continue; |
| 3504 | descr = PyDescr_NewMember(type, memb); |
| 3505 | if (descr == NULL) |
| 3506 | return -1; |
| 3507 | if (PyDict_SetItemString(dict, memb->name, descr) < 0) |
| 3508 | return -1; |
| 3509 | Py_DECREF(descr); |
| 3510 | } |
| 3511 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3512 | } |
| 3513 | |
| 3514 | static int |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 3515 | add_getset(PyTypeObject *type, PyGetSetDef *gsp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3516 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3517 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3518 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3519 | for (; gsp->name != NULL; gsp++) { |
| 3520 | PyObject *descr; |
| 3521 | if (PyDict_GetItemString(dict, gsp->name)) |
| 3522 | continue; |
| 3523 | descr = PyDescr_NewGetSet(type, gsp); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3524 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3525 | if (descr == NULL) |
| 3526 | return -1; |
| 3527 | if (PyDict_SetItemString(dict, gsp->name, descr) < 0) |
| 3528 | return -1; |
| 3529 | Py_DECREF(descr); |
| 3530 | } |
| 3531 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3532 | } |
| 3533 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3534 | static void |
| 3535 | inherit_special(PyTypeObject *type, PyTypeObject *base) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3536 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3538 | /* Copying basicsize is connected to the GC flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3539 | if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3540 | (base->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3541 | (!type->tp_traverse && !type->tp_clear)) { |
| 3542 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
| 3543 | if (type->tp_traverse == NULL) |
| 3544 | type->tp_traverse = base->tp_traverse; |
| 3545 | if (type->tp_clear == NULL) |
| 3546 | type->tp_clear = base->tp_clear; |
| 3547 | } |
| 3548 | { |
| 3549 | /* The condition below could use some explanation. |
| 3550 | It appears that tp_new is not inherited for static types |
| 3551 | whose base class is 'object'; this seems to be a precaution |
| 3552 | so that old extension types don't suddenly become |
| 3553 | callable (object.__new__ wouldn't insure the invariants |
| 3554 | that the extension type's own factory function ensures). |
| 3555 | Heap types, of course, are under our control, so they do |
| 3556 | inherit tp_new; static extension types that specify some |
| 3557 | other built-in type as the default are considered |
| 3558 | new-style-aware so they also inherit object.__new__. */ |
| 3559 | if (base != &PyBaseObject_Type || |
| 3560 | (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 3561 | if (type->tp_new == NULL) |
| 3562 | type->tp_new = base->tp_new; |
| 3563 | } |
| 3564 | } |
Benjamin Peterson | a7465e2 | 2010-07-05 15:01:22 +0000 | [diff] [blame] | 3565 | if (type->tp_basicsize == 0) |
| 3566 | type->tp_basicsize = base->tp_basicsize; |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3568 | /* Copy other non-function slots */ |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3569 | |
| 3570 | #undef COPYVAL |
| 3571 | #define COPYVAL(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3572 | if (type->SLOT == 0) type->SLOT = base->SLOT |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 3573 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3574 | COPYVAL(tp_itemsize); |
| 3575 | COPYVAL(tp_weaklistoffset); |
| 3576 | COPYVAL(tp_dictoffset); |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 3577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3578 | /* Setup fast subclass flags */ |
| 3579 | if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException)) |
| 3580 | type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS; |
| 3581 | else if (PyType_IsSubtype(base, &PyType_Type)) |
| 3582 | type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS; |
| 3583 | else if (PyType_IsSubtype(base, &PyLong_Type)) |
| 3584 | type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS; |
| 3585 | else if (PyType_IsSubtype(base, &PyBytes_Type)) |
| 3586 | type->tp_flags |= Py_TPFLAGS_BYTES_SUBCLASS; |
| 3587 | else if (PyType_IsSubtype(base, &PyUnicode_Type)) |
| 3588 | type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS; |
| 3589 | else if (PyType_IsSubtype(base, &PyTuple_Type)) |
| 3590 | type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS; |
| 3591 | else if (PyType_IsSubtype(base, &PyList_Type)) |
| 3592 | type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS; |
| 3593 | else if (PyType_IsSubtype(base, &PyDict_Type)) |
| 3594 | type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3595 | } |
| 3596 | |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 3597 | static char *hash_name_op[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3598 | "__eq__", |
| 3599 | "__hash__", |
| 3600 | NULL |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3601 | }; |
| 3602 | |
| 3603 | static int |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 3604 | overrides_hash(PyTypeObject *type) |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3605 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3606 | char **p; |
| 3607 | PyObject *dict = type->tp_dict; |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3608 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3609 | assert(dict != NULL); |
| 3610 | for (p = hash_name_op; *p; p++) { |
| 3611 | if (PyDict_GetItemString(dict, *p) != NULL) |
| 3612 | return 1; |
| 3613 | } |
| 3614 | return 0; |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3615 | } |
| 3616 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3617 | static void |
| 3618 | inherit_slots(PyTypeObject *type, PyTypeObject *base) |
| 3619 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3620 | PyTypeObject *basebase; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3621 | |
| 3622 | #undef SLOTDEFINED |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3623 | #undef COPYSLOT |
| 3624 | #undef COPYNUM |
| 3625 | #undef COPYSEQ |
| 3626 | #undef COPYMAP |
Guido van Rossum | 5af588b | 2001-10-12 14:13:21 +0000 | [diff] [blame] | 3627 | #undef COPYBUF |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3628 | |
| 3629 | #define SLOTDEFINED(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3630 | (base->SLOT != 0 && \ |
| 3631 | (basebase == NULL || base->SLOT != basebase->SLOT)) |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3632 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3633 | #define COPYSLOT(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3634 | if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3635 | |
| 3636 | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT) |
| 3637 | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT) |
| 3638 | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT) |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3639 | #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3640 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3641 | /* This won't inherit indirect slots (from tp_as_number etc.) |
| 3642 | if type doesn't provide the space. */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3643 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3644 | if (type->tp_as_number != NULL && base->tp_as_number != NULL) { |
| 3645 | basebase = base->tp_base; |
| 3646 | if (basebase->tp_as_number == NULL) |
| 3647 | basebase = NULL; |
| 3648 | COPYNUM(nb_add); |
| 3649 | COPYNUM(nb_subtract); |
| 3650 | COPYNUM(nb_multiply); |
| 3651 | COPYNUM(nb_remainder); |
| 3652 | COPYNUM(nb_divmod); |
| 3653 | COPYNUM(nb_power); |
| 3654 | COPYNUM(nb_negative); |
| 3655 | COPYNUM(nb_positive); |
| 3656 | COPYNUM(nb_absolute); |
| 3657 | COPYNUM(nb_bool); |
| 3658 | COPYNUM(nb_invert); |
| 3659 | COPYNUM(nb_lshift); |
| 3660 | COPYNUM(nb_rshift); |
| 3661 | COPYNUM(nb_and); |
| 3662 | COPYNUM(nb_xor); |
| 3663 | COPYNUM(nb_or); |
| 3664 | COPYNUM(nb_int); |
| 3665 | COPYNUM(nb_float); |
| 3666 | COPYNUM(nb_inplace_add); |
| 3667 | COPYNUM(nb_inplace_subtract); |
| 3668 | COPYNUM(nb_inplace_multiply); |
| 3669 | COPYNUM(nb_inplace_remainder); |
| 3670 | COPYNUM(nb_inplace_power); |
| 3671 | COPYNUM(nb_inplace_lshift); |
| 3672 | COPYNUM(nb_inplace_rshift); |
| 3673 | COPYNUM(nb_inplace_and); |
| 3674 | COPYNUM(nb_inplace_xor); |
| 3675 | COPYNUM(nb_inplace_or); |
| 3676 | COPYNUM(nb_true_divide); |
| 3677 | COPYNUM(nb_floor_divide); |
| 3678 | COPYNUM(nb_inplace_true_divide); |
| 3679 | COPYNUM(nb_inplace_floor_divide); |
| 3680 | COPYNUM(nb_index); |
| 3681 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3682 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3683 | if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) { |
| 3684 | basebase = base->tp_base; |
| 3685 | if (basebase->tp_as_sequence == NULL) |
| 3686 | basebase = NULL; |
| 3687 | COPYSEQ(sq_length); |
| 3688 | COPYSEQ(sq_concat); |
| 3689 | COPYSEQ(sq_repeat); |
| 3690 | COPYSEQ(sq_item); |
| 3691 | COPYSEQ(sq_ass_item); |
| 3692 | COPYSEQ(sq_contains); |
| 3693 | COPYSEQ(sq_inplace_concat); |
| 3694 | COPYSEQ(sq_inplace_repeat); |
| 3695 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3697 | if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) { |
| 3698 | basebase = base->tp_base; |
| 3699 | if (basebase->tp_as_mapping == NULL) |
| 3700 | basebase = NULL; |
| 3701 | COPYMAP(mp_length); |
| 3702 | COPYMAP(mp_subscript); |
| 3703 | COPYMAP(mp_ass_subscript); |
| 3704 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3705 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3706 | if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) { |
| 3707 | basebase = base->tp_base; |
| 3708 | if (basebase->tp_as_buffer == NULL) |
| 3709 | basebase = NULL; |
| 3710 | COPYBUF(bf_getbuffer); |
| 3711 | COPYBUF(bf_releasebuffer); |
| 3712 | } |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3713 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3714 | basebase = base->tp_base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3715 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3716 | COPYSLOT(tp_dealloc); |
| 3717 | if (type->tp_getattr == NULL && type->tp_getattro == NULL) { |
| 3718 | type->tp_getattr = base->tp_getattr; |
| 3719 | type->tp_getattro = base->tp_getattro; |
| 3720 | } |
| 3721 | if (type->tp_setattr == NULL && type->tp_setattro == NULL) { |
| 3722 | type->tp_setattr = base->tp_setattr; |
| 3723 | type->tp_setattro = base->tp_setattro; |
| 3724 | } |
| 3725 | /* tp_reserved is ignored */ |
| 3726 | COPYSLOT(tp_repr); |
| 3727 | /* tp_hash see tp_richcompare */ |
| 3728 | COPYSLOT(tp_call); |
| 3729 | COPYSLOT(tp_str); |
| 3730 | { |
| 3731 | /* Copy comparison-related slots only when |
| 3732 | not overriding them anywhere */ |
| 3733 | if (type->tp_richcompare == NULL && |
| 3734 | type->tp_hash == NULL && |
| 3735 | !overrides_hash(type)) |
| 3736 | { |
| 3737 | type->tp_richcompare = base->tp_richcompare; |
| 3738 | type->tp_hash = base->tp_hash; |
| 3739 | } |
| 3740 | } |
| 3741 | { |
| 3742 | COPYSLOT(tp_iter); |
| 3743 | COPYSLOT(tp_iternext); |
| 3744 | } |
| 3745 | { |
| 3746 | COPYSLOT(tp_descr_get); |
| 3747 | COPYSLOT(tp_descr_set); |
| 3748 | COPYSLOT(tp_dictoffset); |
| 3749 | COPYSLOT(tp_init); |
| 3750 | COPYSLOT(tp_alloc); |
| 3751 | COPYSLOT(tp_is_gc); |
| 3752 | if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 3753 | (base->tp_flags & Py_TPFLAGS_HAVE_GC)) { |
| 3754 | /* They agree about gc. */ |
| 3755 | COPYSLOT(tp_free); |
| 3756 | } |
| 3757 | else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 3758 | type->tp_free == NULL && |
| 3759 | base->tp_free == PyObject_Free) { |
| 3760 | /* A bit of magic to plug in the correct default |
| 3761 | * tp_free function when a derived class adds gc, |
| 3762 | * didn't define tp_free, and the base uses the |
| 3763 | * default non-gc tp_free. |
| 3764 | */ |
| 3765 | type->tp_free = PyObject_GC_Del; |
| 3766 | } |
| 3767 | /* else they didn't agree about gc, and there isn't something |
| 3768 | * obvious to be done -- the type is on its own. |
| 3769 | */ |
| 3770 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3771 | } |
| 3772 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 3773 | static int add_operators(PyTypeObject *); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3774 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3775 | int |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 3776 | PyType_Ready(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3777 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3778 | PyObject *dict, *bases; |
| 3779 | PyTypeObject *base; |
| 3780 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3781 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3782 | if (type->tp_flags & Py_TPFLAGS_READY) { |
| 3783 | assert(type->tp_dict != NULL); |
| 3784 | return 0; |
| 3785 | } |
| 3786 | assert((type->tp_flags & Py_TPFLAGS_READYING) == 0); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3787 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3788 | type->tp_flags |= Py_TPFLAGS_READYING; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3789 | |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 3790 | #ifdef Py_TRACE_REFS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3791 | /* PyType_Ready is the closest thing we have to a choke point |
| 3792 | * for type objects, so is the best place I can think of to try |
| 3793 | * to get type objects into the doubly-linked list of all objects. |
| 3794 | * Still, not all type objects go thru PyType_Ready. |
| 3795 | */ |
| 3796 | _Py_AddToAllObjects((PyObject *)type, 0); |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 3797 | #endif |
| 3798 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3799 | /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
| 3800 | base = type->tp_base; |
| 3801 | if (base == NULL && type != &PyBaseObject_Type) { |
| 3802 | base = type->tp_base = &PyBaseObject_Type; |
| 3803 | Py_INCREF(base); |
| 3804 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3805 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3806 | /* Now the only way base can still be NULL is if type is |
| 3807 | * &PyBaseObject_Type. |
| 3808 | */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 3809 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3810 | /* Initialize the base class */ |
| 3811 | if (base != NULL && base->tp_dict == NULL) { |
| 3812 | if (PyType_Ready(base) < 0) |
| 3813 | goto error; |
| 3814 | } |
Guido van Rossum | 323a9cf | 2002-08-14 17:26:30 +0000 | [diff] [blame] | 3815 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3816 | /* Initialize ob_type if NULL. This means extensions that want to be |
| 3817 | compilable separately on Windows can call PyType_Ready() instead of |
| 3818 | initializing the ob_type field of their type objects. */ |
| 3819 | /* The test for base != NULL is really unnecessary, since base is only |
| 3820 | NULL when type is &PyBaseObject_Type, and we know its ob_type is |
| 3821 | not NULL (it's initialized to &PyType_Type). But coverity doesn't |
| 3822 | know that. */ |
| 3823 | if (Py_TYPE(type) == NULL && base != NULL) |
| 3824 | Py_TYPE(type) = Py_TYPE(base); |
Guido van Rossum | 0986d82 | 2002-04-08 01:38:42 +0000 | [diff] [blame] | 3825 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3826 | /* Initialize tp_bases */ |
| 3827 | bases = type->tp_bases; |
| 3828 | if (bases == NULL) { |
| 3829 | if (base == NULL) |
| 3830 | bases = PyTuple_New(0); |
| 3831 | else |
| 3832 | bases = PyTuple_Pack(1, base); |
| 3833 | if (bases == NULL) |
| 3834 | goto error; |
| 3835 | type->tp_bases = bases; |
| 3836 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3837 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3838 | /* Initialize tp_dict */ |
| 3839 | dict = type->tp_dict; |
| 3840 | if (dict == NULL) { |
| 3841 | dict = PyDict_New(); |
| 3842 | if (dict == NULL) |
| 3843 | goto error; |
| 3844 | type->tp_dict = dict; |
| 3845 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3846 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3847 | /* Add type-specific descriptors to tp_dict */ |
| 3848 | if (add_operators(type) < 0) |
| 3849 | goto error; |
| 3850 | if (type->tp_methods != NULL) { |
| 3851 | if (add_methods(type, type->tp_methods) < 0) |
| 3852 | goto error; |
| 3853 | } |
| 3854 | if (type->tp_members != NULL) { |
| 3855 | if (add_members(type, type->tp_members) < 0) |
| 3856 | goto error; |
| 3857 | } |
| 3858 | if (type->tp_getset != NULL) { |
| 3859 | if (add_getset(type, type->tp_getset) < 0) |
| 3860 | goto error; |
| 3861 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3862 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3863 | /* Calculate method resolution order */ |
| 3864 | if (mro_internal(type) < 0) { |
| 3865 | goto error; |
| 3866 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3868 | /* Inherit special flags from dominant base */ |
| 3869 | if (type->tp_base != NULL) |
| 3870 | inherit_special(type, type->tp_base); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 3871 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3872 | /* Initialize tp_dict properly */ |
| 3873 | bases = type->tp_mro; |
| 3874 | assert(bases != NULL); |
| 3875 | assert(PyTuple_Check(bases)); |
| 3876 | n = PyTuple_GET_SIZE(bases); |
| 3877 | for (i = 1; i < n; i++) { |
| 3878 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 3879 | if (PyType_Check(b)) |
| 3880 | inherit_slots(type, (PyTypeObject *)b); |
| 3881 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3882 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3883 | /* Sanity check for tp_free. */ |
| 3884 | if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) && |
| 3885 | (type->tp_free == NULL || type->tp_free == PyObject_Del)) { |
| 3886 | /* This base class needs to call tp_free, but doesn't have |
| 3887 | * one, or its tp_free is for non-gc'ed objects. |
| 3888 | */ |
| 3889 | PyErr_Format(PyExc_TypeError, "type '%.100s' participates in " |
| 3890 | "gc and is a base type but has inappropriate " |
| 3891 | "tp_free slot", |
| 3892 | type->tp_name); |
| 3893 | goto error; |
| 3894 | } |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 3895 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3896 | /* if the type dictionary doesn't contain a __doc__, set it from |
| 3897 | the tp_doc slot. |
| 3898 | */ |
| 3899 | if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) { |
| 3900 | if (type->tp_doc != NULL) { |
| 3901 | PyObject *doc = PyUnicode_FromString(type->tp_doc); |
| 3902 | if (doc == NULL) |
| 3903 | goto error; |
| 3904 | PyDict_SetItemString(type->tp_dict, "__doc__", doc); |
| 3905 | Py_DECREF(doc); |
| 3906 | } else { |
| 3907 | PyDict_SetItemString(type->tp_dict, |
| 3908 | "__doc__", Py_None); |
| 3909 | } |
| 3910 | } |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 3911 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3912 | /* Hack for tp_hash and __hash__. |
| 3913 | If after all that, tp_hash is still NULL, and __hash__ is not in |
| 3914 | tp_dict, set tp_hash to PyObject_HashNotImplemented and |
| 3915 | tp_dict['__hash__'] equal to None. |
| 3916 | This signals that __hash__ is not inherited. |
| 3917 | */ |
| 3918 | if (type->tp_hash == NULL) { |
| 3919 | if (PyDict_GetItemString(type->tp_dict, "__hash__") == NULL) { |
| 3920 | if (PyDict_SetItemString(type->tp_dict, "__hash__", Py_None) < 0) |
| 3921 | goto error; |
| 3922 | type->tp_hash = PyObject_HashNotImplemented; |
| 3923 | } |
| 3924 | } |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 3925 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3926 | /* Some more special stuff */ |
| 3927 | base = type->tp_base; |
| 3928 | if (base != NULL) { |
| 3929 | if (type->tp_as_number == NULL) |
| 3930 | type->tp_as_number = base->tp_as_number; |
| 3931 | if (type->tp_as_sequence == NULL) |
| 3932 | type->tp_as_sequence = base->tp_as_sequence; |
| 3933 | if (type->tp_as_mapping == NULL) |
| 3934 | type->tp_as_mapping = base->tp_as_mapping; |
| 3935 | if (type->tp_as_buffer == NULL) |
| 3936 | type->tp_as_buffer = base->tp_as_buffer; |
| 3937 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3938 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3939 | /* Link into each base class's list of subclasses */ |
| 3940 | bases = type->tp_bases; |
| 3941 | n = PyTuple_GET_SIZE(bases); |
| 3942 | for (i = 0; i < n; i++) { |
| 3943 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 3944 | if (PyType_Check(b) && |
| 3945 | add_subclass((PyTypeObject *)b, type) < 0) |
| 3946 | goto error; |
| 3947 | } |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3949 | /* Warn for a type that implements tp_compare (now known as |
| 3950 | tp_reserved) but not tp_richcompare. */ |
| 3951 | if (type->tp_reserved && !type->tp_richcompare) { |
| 3952 | int error; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3953 | error = PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 3954 | "Type %.100s defines tp_reserved (formerly tp_compare) " |
| 3955 | "but not tp_richcompare. Comparisons may not behave as intended.", |
| 3956 | type->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3957 | if (error == -1) |
| 3958 | goto error; |
| 3959 | } |
Mark Dickinson | 2a7d45b | 2009-02-08 11:02:10 +0000 | [diff] [blame] | 3960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3961 | /* All done -- set the ready flag */ |
| 3962 | assert(type->tp_dict != NULL); |
| 3963 | type->tp_flags = |
| 3964 | (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; |
| 3965 | return 0; |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 3966 | |
| 3967 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3968 | type->tp_flags &= ~Py_TPFLAGS_READYING; |
| 3969 | return -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3970 | } |
| 3971 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3972 | static int |
| 3973 | add_subclass(PyTypeObject *base, PyTypeObject *type) |
| 3974 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3975 | Py_ssize_t i; |
| 3976 | int result; |
| 3977 | PyObject *list, *ref, *newobj; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3979 | list = base->tp_subclasses; |
| 3980 | if (list == NULL) { |
| 3981 | base->tp_subclasses = list = PyList_New(0); |
| 3982 | if (list == NULL) |
| 3983 | return -1; |
| 3984 | } |
| 3985 | assert(PyList_Check(list)); |
| 3986 | newobj = PyWeakref_NewRef((PyObject *)type, NULL); |
| 3987 | i = PyList_GET_SIZE(list); |
| 3988 | while (--i >= 0) { |
| 3989 | ref = PyList_GET_ITEM(list, i); |
| 3990 | assert(PyWeakref_CheckRef(ref)); |
| 3991 | if (PyWeakref_GET_OBJECT(ref) == Py_None) |
| 3992 | return PyList_SetItem(list, i, newobj); |
| 3993 | } |
| 3994 | result = PyList_Append(list, newobj); |
| 3995 | Py_DECREF(newobj); |
| 3996 | return result; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3997 | } |
| 3998 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3999 | static void |
| 4000 | remove_subclass(PyTypeObject *base, PyTypeObject *type) |
| 4001 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4002 | Py_ssize_t i; |
| 4003 | PyObject *list, *ref; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4004 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4005 | list = base->tp_subclasses; |
| 4006 | if (list == NULL) { |
| 4007 | return; |
| 4008 | } |
| 4009 | assert(PyList_Check(list)); |
| 4010 | i = PyList_GET_SIZE(list); |
| 4011 | while (--i >= 0) { |
| 4012 | ref = PyList_GET_ITEM(list, i); |
| 4013 | assert(PyWeakref_CheckRef(ref)); |
| 4014 | if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) { |
| 4015 | /* this can't fail, right? */ |
| 4016 | PySequence_DelItem(list, i); |
| 4017 | return; |
| 4018 | } |
| 4019 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4020 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4021 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4022 | static int |
| 4023 | check_num_args(PyObject *ob, int n) |
| 4024 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4025 | if (!PyTuple_CheckExact(ob)) { |
| 4026 | PyErr_SetString(PyExc_SystemError, |
| 4027 | "PyArg_UnpackTuple() argument list is not a tuple"); |
| 4028 | return 0; |
| 4029 | } |
| 4030 | if (n == PyTuple_GET_SIZE(ob)) |
| 4031 | return 1; |
| 4032 | PyErr_Format( |
| 4033 | PyExc_TypeError, |
| 4034 | "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob)); |
| 4035 | return 0; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4036 | } |
| 4037 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4038 | /* Generic wrappers for overloadable 'operators' such as __getitem__ */ |
| 4039 | |
| 4040 | /* There's a wrapper *function* for each distinct function typedef used |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4041 | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4042 | wrapper *table* for each distinct operation (e.g. __len__, __add__). |
| 4043 | Most tables have only one entry; the tables for binary operators have two |
| 4044 | entries, one regular and one with reversed arguments. */ |
| 4045 | |
| 4046 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4047 | wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4048 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4049 | lenfunc func = (lenfunc)wrapped; |
| 4050 | Py_ssize_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4052 | if (!check_num_args(args, 0)) |
| 4053 | return NULL; |
| 4054 | res = (*func)(self); |
| 4055 | if (res == -1 && PyErr_Occurred()) |
| 4056 | return NULL; |
| 4057 | return PyLong_FromLong((long)res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4058 | } |
| 4059 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4060 | static PyObject * |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4061 | wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped) |
| 4062 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4063 | inquiry func = (inquiry)wrapped; |
| 4064 | int res; |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4065 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4066 | if (!check_num_args(args, 0)) |
| 4067 | return NULL; |
| 4068 | res = (*func)(self); |
| 4069 | if (res == -1 && PyErr_Occurred()) |
| 4070 | return NULL; |
| 4071 | return PyBool_FromLong((long)res); |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4072 | } |
| 4073 | |
| 4074 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4075 | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4076 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4077 | binaryfunc func = (binaryfunc)wrapped; |
| 4078 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4079 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4080 | if (!check_num_args(args, 1)) |
| 4081 | return NULL; |
| 4082 | other = PyTuple_GET_ITEM(args, 0); |
| 4083 | return (*func)(self, other); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | static PyObject * |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4087 | wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped) |
| 4088 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4089 | binaryfunc func = (binaryfunc)wrapped; |
| 4090 | PyObject *other; |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4091 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4092 | if (!check_num_args(args, 1)) |
| 4093 | return NULL; |
| 4094 | other = PyTuple_GET_ITEM(args, 0); |
| 4095 | return (*func)(self, other); |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4096 | } |
| 4097 | |
| 4098 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4099 | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4100 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4101 | binaryfunc func = (binaryfunc)wrapped; |
| 4102 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4104 | if (!check_num_args(args, 1)) |
| 4105 | return NULL; |
| 4106 | other = PyTuple_GET_ITEM(args, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4107 | return (*func)(other, self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4108 | } |
| 4109 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4110 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4111 | wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4112 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4113 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4114 | PyObject *other; |
| 4115 | PyObject *third = Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4117 | /* Note: This wrapper only works for __pow__() */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4119 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
| 4120 | return NULL; |
| 4121 | return (*func)(self, other, third); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4122 | } |
| 4123 | |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4124 | static PyObject * |
| 4125 | wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4126 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4127 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4128 | PyObject *other; |
| 4129 | PyObject *third = Py_None; |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4130 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4131 | /* Note: This wrapper only works for __pow__() */ |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4132 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4133 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
| 4134 | return NULL; |
| 4135 | return (*func)(other, self, third); |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4136 | } |
| 4137 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4138 | static PyObject * |
| 4139 | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4140 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4141 | unaryfunc func = (unaryfunc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4142 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4143 | if (!check_num_args(args, 0)) |
| 4144 | return NULL; |
| 4145 | return (*func)(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4148 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4149 | wrap_indexargfunc(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 | ssizeargfunc func = (ssizeargfunc)wrapped; |
| 4152 | PyObject* o; |
| 4153 | Py_ssize_t i; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4155 | if (!PyArg_UnpackTuple(args, "", 1, 1, &o)) |
| 4156 | return NULL; |
| 4157 | i = PyNumber_AsSsize_t(o, PyExc_OverflowError); |
| 4158 | if (i == -1 && PyErr_Occurred()) |
| 4159 | return NULL; |
| 4160 | return (*func)(self, i); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4163 | static Py_ssize_t |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4164 | getindex(PyObject *self, PyObject *arg) |
| 4165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4166 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4167 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4168 | i = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
| 4169 | if (i == -1 && PyErr_Occurred()) |
| 4170 | return -1; |
| 4171 | if (i < 0) { |
| 4172 | PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence; |
| 4173 | if (sq && sq->sq_length) { |
| 4174 | Py_ssize_t n = (*sq->sq_length)(self); |
| 4175 | if (n < 0) |
| 4176 | return -1; |
| 4177 | i += n; |
| 4178 | } |
| 4179 | } |
| 4180 | return i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4181 | } |
| 4182 | |
| 4183 | static PyObject * |
| 4184 | wrap_sq_item(PyObject *self, PyObject *args, void *wrapped) |
| 4185 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4186 | ssizeargfunc func = (ssizeargfunc)wrapped; |
| 4187 | PyObject *arg; |
| 4188 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4189 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4190 | if (PyTuple_GET_SIZE(args) == 1) { |
| 4191 | arg = PyTuple_GET_ITEM(args, 0); |
| 4192 | i = getindex(self, arg); |
| 4193 | if (i == -1 && PyErr_Occurred()) |
| 4194 | return NULL; |
| 4195 | return (*func)(self, i); |
| 4196 | } |
| 4197 | check_num_args(args, 1); |
| 4198 | assert(PyErr_Occurred()); |
| 4199 | return NULL; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4200 | } |
| 4201 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4202 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4203 | wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4204 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4205 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4206 | Py_ssize_t i; |
| 4207 | int res; |
| 4208 | PyObject *arg, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4210 | if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value)) |
| 4211 | return NULL; |
| 4212 | i = getindex(self, arg); |
| 4213 | if (i == -1 && PyErr_Occurred()) |
| 4214 | return NULL; |
| 4215 | res = (*func)(self, i, value); |
| 4216 | if (res == -1 && PyErr_Occurred()) |
| 4217 | return NULL; |
| 4218 | Py_INCREF(Py_None); |
| 4219 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4220 | } |
| 4221 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4222 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4223 | wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4224 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4225 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 4226 | Py_ssize_t i; |
| 4227 | int res; |
| 4228 | PyObject *arg; |
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 | arg = PyTuple_GET_ITEM(args, 0); |
| 4233 | i = getindex(self, arg); |
| 4234 | if (i == -1 && PyErr_Occurred()) |
| 4235 | return NULL; |
| 4236 | res = (*func)(self, i, NULL); |
| 4237 | if (res == -1 && PyErr_Occurred()) |
| 4238 | return NULL; |
| 4239 | Py_INCREF(Py_None); |
| 4240 | return Py_None; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4241 | } |
| 4242 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4243 | /* XXX objobjproc is a misnomer; should be objargpred */ |
| 4244 | static PyObject * |
| 4245 | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) |
| 4246 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4247 | objobjproc func = (objobjproc)wrapped; |
| 4248 | int res; |
| 4249 | PyObject *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4251 | if (!check_num_args(args, 1)) |
| 4252 | return NULL; |
| 4253 | value = PyTuple_GET_ITEM(args, 0); |
| 4254 | res = (*func)(self, value); |
| 4255 | if (res == -1 && PyErr_Occurred()) |
| 4256 | return NULL; |
| 4257 | else |
| 4258 | return PyBool_FromLong(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4259 | } |
| 4260 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4261 | static PyObject * |
| 4262 | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 4263 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4264 | objobjargproc func = (objobjargproc)wrapped; |
| 4265 | int res; |
| 4266 | PyObject *key, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4267 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4268 | if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value)) |
| 4269 | return NULL; |
| 4270 | res = (*func)(self, key, value); |
| 4271 | if (res == -1 && PyErr_Occurred()) |
| 4272 | return NULL; |
| 4273 | Py_INCREF(Py_None); |
| 4274 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4275 | } |
| 4276 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4277 | static PyObject * |
| 4278 | wrap_delitem(PyObject *self, PyObject *args, void *wrapped) |
| 4279 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4280 | objobjargproc func = (objobjargproc)wrapped; |
| 4281 | int res; |
| 4282 | PyObject *key; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4284 | if (!check_num_args(args, 1)) |
| 4285 | return NULL; |
| 4286 | key = PyTuple_GET_ITEM(args, 0); |
| 4287 | res = (*func)(self, key, NULL); |
| 4288 | if (res == -1 && PyErr_Occurred()) |
| 4289 | return NULL; |
| 4290 | Py_INCREF(Py_None); |
| 4291 | return Py_None; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 4292 | } |
| 4293 | |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4294 | /* 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] | 4295 | This is called the Carlo Verre hack after its discoverer. */ |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4296 | static int |
| 4297 | hackcheck(PyObject *self, setattrofunc func, char *what) |
| 4298 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4299 | PyTypeObject *type = Py_TYPE(self); |
| 4300 | while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 4301 | type = type->tp_base; |
| 4302 | /* If type is NULL now, this is a really weird type. |
| 4303 | In the spirit of backwards compatibility (?), just shut up. */ |
| 4304 | if (type && type->tp_setattro != func) { |
| 4305 | PyErr_Format(PyExc_TypeError, |
| 4306 | "can't apply this %s to %s object", |
| 4307 | what, |
| 4308 | type->tp_name); |
| 4309 | return 0; |
| 4310 | } |
| 4311 | return 1; |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 4312 | } |
| 4313 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4314 | static PyObject * |
| 4315 | wrap_setattr(PyObject *self, PyObject *args, void *wrapped) |
| 4316 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4317 | setattrofunc func = (setattrofunc)wrapped; |
| 4318 | int res; |
| 4319 | PyObject *name, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4321 | if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value)) |
| 4322 | return NULL; |
| 4323 | if (!hackcheck(self, func, "__setattr__")) |
| 4324 | return NULL; |
| 4325 | res = (*func)(self, name, value); |
| 4326 | if (res < 0) |
| 4327 | return NULL; |
| 4328 | Py_INCREF(Py_None); |
| 4329 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4330 | } |
| 4331 | |
| 4332 | static PyObject * |
| 4333 | wrap_delattr(PyObject *self, PyObject *args, void *wrapped) |
| 4334 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4335 | setattrofunc func = (setattrofunc)wrapped; |
| 4336 | int res; |
| 4337 | PyObject *name; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4339 | if (!check_num_args(args, 1)) |
| 4340 | return NULL; |
| 4341 | name = PyTuple_GET_ITEM(args, 0); |
| 4342 | if (!hackcheck(self, func, "__delattr__")) |
| 4343 | return NULL; |
| 4344 | res = (*func)(self, name, NULL); |
| 4345 | if (res < 0) |
| 4346 | return NULL; |
| 4347 | Py_INCREF(Py_None); |
| 4348 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4349 | } |
| 4350 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4351 | static PyObject * |
| 4352 | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4353 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4354 | hashfunc func = (hashfunc)wrapped; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 4355 | Py_hash_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4356 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4357 | if (!check_num_args(args, 0)) |
| 4358 | return NULL; |
| 4359 | res = (*func)(self); |
| 4360 | if (res == -1 && PyErr_Occurred()) |
| 4361 | return NULL; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 4362 | return PyLong_FromSsize_t(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4363 | } |
| 4364 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4365 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4366 | wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4367 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4368 | ternaryfunc func = (ternaryfunc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4370 | return (*func)(self, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4371 | } |
| 4372 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4373 | static PyObject * |
| 4374 | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) |
| 4375 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4376 | richcmpfunc func = (richcmpfunc)wrapped; |
| 4377 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4378 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4379 | if (!check_num_args(args, 1)) |
| 4380 | return NULL; |
| 4381 | other = PyTuple_GET_ITEM(args, 0); |
| 4382 | return (*func)(self, other, op); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4383 | } |
| 4384 | |
| 4385 | #undef RICHCMP_WRAPPER |
| 4386 | #define RICHCMP_WRAPPER(NAME, OP) \ |
| 4387 | static PyObject * \ |
| 4388 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ |
| 4389 | { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4390 | return wrap_richcmpfunc(self, args, wrapped, OP); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4391 | } |
| 4392 | |
Jack Jansen | 8e938b4 | 2001-08-08 15:29:49 +0000 | [diff] [blame] | 4393 | RICHCMP_WRAPPER(lt, Py_LT) |
| 4394 | RICHCMP_WRAPPER(le, Py_LE) |
| 4395 | RICHCMP_WRAPPER(eq, Py_EQ) |
| 4396 | RICHCMP_WRAPPER(ne, Py_NE) |
| 4397 | RICHCMP_WRAPPER(gt, Py_GT) |
| 4398 | RICHCMP_WRAPPER(ge, Py_GE) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4399 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4400 | static PyObject * |
| 4401 | wrap_next(PyObject *self, PyObject *args, void *wrapped) |
| 4402 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4403 | unaryfunc func = (unaryfunc)wrapped; |
| 4404 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4406 | if (!check_num_args(args, 0)) |
| 4407 | return NULL; |
| 4408 | res = (*func)(self); |
| 4409 | if (res == NULL && !PyErr_Occurred()) |
| 4410 | PyErr_SetNone(PyExc_StopIteration); |
| 4411 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4412 | } |
| 4413 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4414 | static PyObject * |
| 4415 | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped) |
| 4416 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4417 | descrgetfunc func = (descrgetfunc)wrapped; |
| 4418 | PyObject *obj; |
| 4419 | PyObject *type = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4420 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4421 | if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type)) |
| 4422 | return NULL; |
| 4423 | if (obj == Py_None) |
| 4424 | obj = NULL; |
| 4425 | if (type == Py_None) |
| 4426 | type = NULL; |
| 4427 | if (type == NULL &&obj == NULL) { |
| 4428 | PyErr_SetString(PyExc_TypeError, |
| 4429 | "__get__(None, None) is invalid"); |
| 4430 | return NULL; |
| 4431 | } |
| 4432 | return (*func)(self, obj, type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4433 | } |
| 4434 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4435 | static PyObject * |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 4436 | wrap_descr_set(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4437 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4438 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4439 | PyObject *obj, *value; |
| 4440 | int ret; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4441 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4442 | if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value)) |
| 4443 | return NULL; |
| 4444 | ret = (*func)(self, obj, value); |
| 4445 | if (ret < 0) |
| 4446 | return NULL; |
| 4447 | Py_INCREF(Py_None); |
| 4448 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4449 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 4450 | |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4451 | static PyObject * |
| 4452 | wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped) |
| 4453 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4454 | descrsetfunc func = (descrsetfunc)wrapped; |
| 4455 | PyObject *obj; |
| 4456 | int ret; |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4457 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4458 | if (!check_num_args(args, 1)) |
| 4459 | return NULL; |
| 4460 | obj = PyTuple_GET_ITEM(args, 0); |
| 4461 | ret = (*func)(self, obj, NULL); |
| 4462 | if (ret < 0) |
| 4463 | return NULL; |
| 4464 | Py_INCREF(Py_None); |
| 4465 | return Py_None; |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 4466 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4467 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4468 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4469 | wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4470 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4471 | initproc func = (initproc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4473 | if (func(self, args, kwds) < 0) |
| 4474 | return NULL; |
| 4475 | Py_INCREF(Py_None); |
| 4476 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4477 | } |
| 4478 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4479 | static PyObject * |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4480 | tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4481 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4482 | PyTypeObject *type, *subtype, *staticbase; |
| 4483 | PyObject *arg0, *res; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4484 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4485 | if (self == NULL || !PyType_Check(self)) |
| 4486 | Py_FatalError("__new__() called with non-type 'self'"); |
| 4487 | type = (PyTypeObject *)self; |
| 4488 | if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) { |
| 4489 | PyErr_Format(PyExc_TypeError, |
| 4490 | "%s.__new__(): not enough arguments", |
| 4491 | type->tp_name); |
| 4492 | return NULL; |
| 4493 | } |
| 4494 | arg0 = PyTuple_GET_ITEM(args, 0); |
| 4495 | if (!PyType_Check(arg0)) { |
| 4496 | PyErr_Format(PyExc_TypeError, |
| 4497 | "%s.__new__(X): X is not a type object (%s)", |
| 4498 | type->tp_name, |
| 4499 | Py_TYPE(arg0)->tp_name); |
| 4500 | return NULL; |
| 4501 | } |
| 4502 | subtype = (PyTypeObject *)arg0; |
| 4503 | if (!PyType_IsSubtype(subtype, type)) { |
| 4504 | PyErr_Format(PyExc_TypeError, |
| 4505 | "%s.__new__(%s): %s is not a subtype of %s", |
| 4506 | type->tp_name, |
| 4507 | subtype->tp_name, |
| 4508 | subtype->tp_name, |
| 4509 | type->tp_name); |
| 4510 | return NULL; |
| 4511 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4512 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4513 | /* Check that the use doesn't do something silly and unsafe like |
| 4514 | object.__new__(dict). To do this, we check that the |
| 4515 | most derived base that's not a heap type is this type. */ |
| 4516 | staticbase = subtype; |
| 4517 | while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 4518 | staticbase = staticbase->tp_base; |
| 4519 | /* If staticbase is NULL now, it is a really weird type. |
| 4520 | In the spirit of backwards compatibility (?), just shut up. */ |
| 4521 | if (staticbase && staticbase->tp_new != type->tp_new) { |
| 4522 | PyErr_Format(PyExc_TypeError, |
| 4523 | "%s.__new__(%s) is not safe, use %s.__new__()", |
| 4524 | type->tp_name, |
| 4525 | subtype->tp_name, |
| 4526 | staticbase == NULL ? "?" : staticbase->tp_name); |
| 4527 | return NULL; |
| 4528 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 4529 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4530 | args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 4531 | if (args == NULL) |
| 4532 | return NULL; |
| 4533 | res = type->tp_new(subtype, args, kwds); |
| 4534 | Py_DECREF(args); |
| 4535 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4538 | static struct PyMethodDef tp_new_methoddef[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4539 | {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS, |
| 4540 | PyDoc_STR("T.__new__(S, ...) -> " |
| 4541 | "a new object with type S, a subtype of T")}, |
| 4542 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4543 | }; |
| 4544 | |
| 4545 | static int |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4546 | add_tp_new_wrapper(PyTypeObject *type) |
| 4547 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4548 | PyObject *func; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4549 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4550 | if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL) |
| 4551 | return 0; |
| 4552 | func = PyCFunction_New(tp_new_methoddef, (PyObject *)type); |
| 4553 | if (func == NULL) |
| 4554 | return -1; |
| 4555 | if (PyDict_SetItemString(type->tp_dict, "__new__", func)) { |
| 4556 | Py_DECREF(func); |
| 4557 | return -1; |
| 4558 | } |
| 4559 | Py_DECREF(func); |
| 4560 | return 0; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 4561 | } |
| 4562 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 4563 | /* Slot wrappers that call the corresponding __foo__ slot. See comments |
| 4564 | below at override_slots() for more explanation. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4565 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4566 | #define SLOT0(FUNCNAME, OPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4567 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4568 | FUNCNAME(PyObject *self) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4569 | { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4570 | static PyObject *cache_str; \ |
| 4571 | return call_method(self, OPSTR, &cache_str, "()"); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4572 | } |
| 4573 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4574 | #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4575 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4576 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4577 | { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4578 | static PyObject *cache_str; \ |
| 4579 | return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4580 | } |
| 4581 | |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4582 | /* Boolean helper for SLOT1BINFULL(). |
| 4583 | right.__class__ is a nontrivial subclass of left.__class__. */ |
| 4584 | static int |
| 4585 | method_is_overloaded(PyObject *left, PyObject *right, char *name) |
| 4586 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4587 | PyObject *a, *b; |
| 4588 | int ok; |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4589 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4590 | b = PyObject_GetAttrString((PyObject *)(Py_TYPE(right)), name); |
| 4591 | if (b == NULL) { |
| 4592 | PyErr_Clear(); |
| 4593 | /* If right doesn't have it, it's not overloaded */ |
| 4594 | return 0; |
| 4595 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4597 | a = PyObject_GetAttrString((PyObject *)(Py_TYPE(left)), name); |
| 4598 | if (a == NULL) { |
| 4599 | PyErr_Clear(); |
| 4600 | Py_DECREF(b); |
| 4601 | /* If right has it but left doesn't, it's overloaded */ |
| 4602 | return 1; |
| 4603 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4604 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4605 | ok = PyObject_RichCompareBool(a, b, Py_NE); |
| 4606 | Py_DECREF(a); |
| 4607 | Py_DECREF(b); |
| 4608 | if (ok < 0) { |
| 4609 | PyErr_Clear(); |
| 4610 | return 0; |
| 4611 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4612 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4613 | return ok; |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 4614 | } |
| 4615 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4616 | |
| 4617 | #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4618 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4619 | FUNCNAME(PyObject *self, PyObject *other) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4620 | { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4621 | static PyObject *cache_str, *rcache_str; \ |
| 4622 | int do_other = Py_TYPE(self) != Py_TYPE(other) && \ |
| 4623 | Py_TYPE(other)->tp_as_number != NULL && \ |
| 4624 | Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \ |
| 4625 | if (Py_TYPE(self)->tp_as_number != NULL && \ |
| 4626 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ |
| 4627 | PyObject *r; \ |
| 4628 | if (do_other && \ |
| 4629 | PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \ |
| 4630 | method_is_overloaded(self, other, ROPSTR)) { \ |
| 4631 | r = call_maybe( \ |
| 4632 | other, ROPSTR, &rcache_str, "(O)", self); \ |
| 4633 | if (r != Py_NotImplemented) \ |
| 4634 | return r; \ |
| 4635 | Py_DECREF(r); \ |
| 4636 | do_other = 0; \ |
| 4637 | } \ |
| 4638 | r = call_maybe( \ |
| 4639 | self, OPSTR, &cache_str, "(O)", other); \ |
| 4640 | if (r != Py_NotImplemented || \ |
| 4641 | Py_TYPE(other) == Py_TYPE(self)) \ |
| 4642 | return r; \ |
| 4643 | Py_DECREF(r); \ |
| 4644 | } \ |
| 4645 | if (do_other) { \ |
| 4646 | return call_maybe( \ |
| 4647 | other, ROPSTR, &rcache_str, "(O)", self); \ |
| 4648 | } \ |
| 4649 | Py_INCREF(Py_NotImplemented); \ |
| 4650 | return Py_NotImplemented; \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4651 | } |
| 4652 | |
| 4653 | #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4654 | SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR) |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4655 | |
| 4656 | #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \ |
| 4657 | static PyObject * \ |
| 4658 | FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ |
| 4659 | { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4660 | static PyObject *cache_str; \ |
| 4661 | return call_method(self, OPSTR, &cache_str, \ |
| 4662 | "(" ARGCODES ")", arg1, arg2); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4663 | } |
| 4664 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4665 | static Py_ssize_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4666 | slot_sq_length(PyObject *self) |
| 4667 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4668 | static PyObject *len_str; |
| 4669 | PyObject *res = call_method(self, "__len__", &len_str, "()"); |
| 4670 | Py_ssize_t len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4671 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4672 | if (res == NULL) |
| 4673 | return -1; |
| 4674 | len = PyNumber_AsSsize_t(res, PyExc_OverflowError); |
| 4675 | Py_DECREF(res); |
| 4676 | if (len < 0) { |
| 4677 | if (!PyErr_Occurred()) |
| 4678 | PyErr_SetString(PyExc_ValueError, |
| 4679 | "__len__() should return >= 0"); |
| 4680 | return -1; |
| 4681 | } |
| 4682 | return len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4685 | /* Super-optimized version of slot_sq_item. |
| 4686 | Other slots could do the same... */ |
| 4687 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4688 | slot_sq_item(PyObject *self, Py_ssize_t i) |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4689 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4690 | static PyObject *getitem_str; |
| 4691 | PyObject *func, *args = NULL, *ival = NULL, *retval = NULL; |
| 4692 | descrgetfunc f; |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4693 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4694 | if (getitem_str == NULL) { |
| 4695 | getitem_str = PyUnicode_InternFromString("__getitem__"); |
| 4696 | if (getitem_str == NULL) |
| 4697 | return NULL; |
| 4698 | } |
| 4699 | func = _PyType_Lookup(Py_TYPE(self), getitem_str); |
| 4700 | if (func != NULL) { |
| 4701 | if ((f = Py_TYPE(func)->tp_descr_get) == NULL) |
| 4702 | Py_INCREF(func); |
| 4703 | else { |
| 4704 | func = f(func, self, (PyObject *)(Py_TYPE(self))); |
| 4705 | if (func == NULL) { |
| 4706 | return NULL; |
| 4707 | } |
| 4708 | } |
| 4709 | ival = PyLong_FromSsize_t(i); |
| 4710 | if (ival != NULL) { |
| 4711 | args = PyTuple_New(1); |
| 4712 | if (args != NULL) { |
| 4713 | PyTuple_SET_ITEM(args, 0, ival); |
| 4714 | retval = PyObject_Call(func, args, NULL); |
| 4715 | Py_XDECREF(args); |
| 4716 | Py_XDECREF(func); |
| 4717 | return retval; |
| 4718 | } |
| 4719 | } |
| 4720 | } |
| 4721 | else { |
| 4722 | PyErr_SetObject(PyExc_AttributeError, getitem_str); |
| 4723 | } |
| 4724 | Py_XDECREF(args); |
| 4725 | Py_XDECREF(ival); |
| 4726 | Py_XDECREF(func); |
| 4727 | return NULL; |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 4728 | } |
| 4729 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4730 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4731 | slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4732 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4733 | PyObject *res; |
| 4734 | static PyObject *delitem_str, *setitem_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4735 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4736 | if (value == NULL) |
| 4737 | res = call_method(self, "__delitem__", &delitem_str, |
| 4738 | "(n)", index); |
| 4739 | else |
| 4740 | res = call_method(self, "__setitem__", &setitem_str, |
| 4741 | "(nO)", index, value); |
| 4742 | if (res == NULL) |
| 4743 | return -1; |
| 4744 | Py_DECREF(res); |
| 4745 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4746 | } |
| 4747 | |
| 4748 | static int |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4749 | slot_sq_contains(PyObject *self, PyObject *value) |
| 4750 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4751 | PyObject *func, *res, *args; |
| 4752 | int result = -1; |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 4753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4754 | static PyObject *contains_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4756 | func = lookup_maybe(self, "__contains__", &contains_str); |
| 4757 | if (func != NULL) { |
| 4758 | args = PyTuple_Pack(1, value); |
| 4759 | if (args == NULL) |
| 4760 | res = NULL; |
| 4761 | else { |
| 4762 | res = PyObject_Call(func, args, NULL); |
| 4763 | Py_DECREF(args); |
| 4764 | } |
| 4765 | Py_DECREF(func); |
| 4766 | if (res != NULL) { |
| 4767 | result = PyObject_IsTrue(res); |
| 4768 | Py_DECREF(res); |
| 4769 | } |
| 4770 | } |
| 4771 | else if (! PyErr_Occurred()) { |
| 4772 | /* Possible results: -1 and 1 */ |
| 4773 | result = (int)_PySequence_IterSearch(self, value, |
| 4774 | PY_ITERSEARCH_CONTAINS); |
| 4775 | } |
| 4776 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4777 | } |
| 4778 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4779 | #define slot_mp_length slot_sq_length |
| 4780 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4781 | SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4782 | |
| 4783 | static int |
| 4784 | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) |
| 4785 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4786 | PyObject *res; |
| 4787 | static PyObject *delitem_str, *setitem_str; |
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 | if (value == NULL) |
| 4790 | res = call_method(self, "__delitem__", &delitem_str, |
| 4791 | "(O)", key); |
| 4792 | else |
| 4793 | res = call_method(self, "__setitem__", &setitem_str, |
| 4794 | "(OO)", key, value); |
| 4795 | if (res == NULL) |
| 4796 | return -1; |
| 4797 | Py_DECREF(res); |
| 4798 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4799 | } |
| 4800 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4801 | SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__") |
| 4802 | SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__") |
| 4803 | SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4804 | SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__") |
| 4805 | SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__") |
| 4806 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 4807 | static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4808 | |
| 4809 | SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4810 | nb_power, "__pow__", "__rpow__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4811 | |
| 4812 | static PyObject * |
| 4813 | slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) |
| 4814 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4815 | static PyObject *pow_str; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 4816 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4817 | if (modulus == Py_None) |
| 4818 | return slot_nb_power_binary(self, other); |
| 4819 | /* Three-arg power doesn't use __rpow__. But ternary_op |
| 4820 | can call this when the second argument's type uses |
| 4821 | slot_nb_power, so check before calling self.__pow__. */ |
| 4822 | if (Py_TYPE(self)->tp_as_number != NULL && |
| 4823 | Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) { |
| 4824 | return call_method(self, "__pow__", &pow_str, |
| 4825 | "(OO)", other, modulus); |
| 4826 | } |
| 4827 | Py_INCREF(Py_NotImplemented); |
| 4828 | return Py_NotImplemented; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4829 | } |
| 4830 | |
| 4831 | SLOT0(slot_nb_negative, "__neg__") |
| 4832 | SLOT0(slot_nb_positive, "__pos__") |
| 4833 | SLOT0(slot_nb_absolute, "__abs__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4834 | |
| 4835 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 4836 | slot_nb_bool(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4837 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4838 | PyObject *func, *args; |
| 4839 | static PyObject *bool_str, *len_str; |
| 4840 | int result = -1; |
| 4841 | int using_len = 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4842 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4843 | func = lookup_maybe(self, "__bool__", &bool_str); |
| 4844 | if (func == NULL) { |
| 4845 | if (PyErr_Occurred()) |
| 4846 | return -1; |
| 4847 | func = lookup_maybe(self, "__len__", &len_str); |
| 4848 | if (func == NULL) |
| 4849 | return PyErr_Occurred() ? -1 : 1; |
| 4850 | using_len = 1; |
| 4851 | } |
| 4852 | args = PyTuple_New(0); |
| 4853 | if (args != NULL) { |
| 4854 | PyObject *temp = PyObject_Call(func, args, NULL); |
| 4855 | Py_DECREF(args); |
| 4856 | if (temp != NULL) { |
| 4857 | if (using_len) { |
| 4858 | /* enforced by slot_nb_len */ |
| 4859 | result = PyObject_IsTrue(temp); |
| 4860 | } |
| 4861 | else if (PyBool_Check(temp)) { |
| 4862 | result = PyObject_IsTrue(temp); |
| 4863 | } |
| 4864 | else { |
| 4865 | PyErr_Format(PyExc_TypeError, |
| 4866 | "__bool__ should return " |
| 4867 | "bool, returned %s", |
| 4868 | Py_TYPE(temp)->tp_name); |
| 4869 | result = -1; |
| 4870 | } |
| 4871 | Py_DECREF(temp); |
| 4872 | } |
| 4873 | } |
| 4874 | Py_DECREF(func); |
| 4875 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4876 | } |
| 4877 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4878 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 4879 | static PyObject * |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4880 | slot_nb_index(PyObject *self) |
| 4881 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4882 | static PyObject *index_str; |
| 4883 | return call_method(self, "__index__", &index_str, "()"); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4884 | } |
| 4885 | |
| 4886 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4887 | SLOT0(slot_nb_invert, "__invert__") |
| 4888 | SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") |
| 4889 | SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") |
| 4890 | SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") |
| 4891 | SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") |
| 4892 | SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4893 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4894 | SLOT0(slot_nb_int, "__int__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4895 | SLOT0(slot_nb_float, "__float__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4896 | SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O") |
| 4897 | SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O") |
| 4898 | SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4899 | SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4900 | /* Can't use SLOT1 here, because nb_inplace_power is ternary */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4901 | static PyObject * |
| 4902 | slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2) |
| 4903 | { |
| 4904 | static PyObject *cache_str; |
| 4905 | return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4906 | } |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4907 | SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O") |
| 4908 | SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O") |
| 4909 | SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O") |
| 4910 | SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O") |
| 4911 | SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O") |
| 4912 | SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4913 | "__floordiv__", "__rfloordiv__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 4914 | SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__") |
| 4915 | SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O") |
| 4916 | SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4917 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4918 | static PyObject * |
| 4919 | slot_tp_repr(PyObject *self) |
| 4920 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4921 | PyObject *func, *res; |
| 4922 | static PyObject *repr_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4923 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4924 | func = lookup_method(self, "__repr__", &repr_str); |
| 4925 | if (func != NULL) { |
| 4926 | res = PyEval_CallObject(func, NULL); |
| 4927 | Py_DECREF(func); |
| 4928 | return res; |
| 4929 | } |
| 4930 | PyErr_Clear(); |
| 4931 | return PyUnicode_FromFormat("<%s object at %p>", |
| 4932 | Py_TYPE(self)->tp_name, self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4933 | } |
| 4934 | |
| 4935 | static PyObject * |
| 4936 | slot_tp_str(PyObject *self) |
| 4937 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4938 | PyObject *func, *res; |
| 4939 | static PyObject *str_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4940 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4941 | func = lookup_method(self, "__str__", &str_str); |
| 4942 | if (func != NULL) { |
| 4943 | res = PyEval_CallObject(func, NULL); |
| 4944 | Py_DECREF(func); |
| 4945 | return res; |
| 4946 | } |
| 4947 | else { |
| 4948 | PyObject *ress; |
| 4949 | PyErr_Clear(); |
| 4950 | res = slot_tp_repr(self); |
| 4951 | if (!res) |
| 4952 | return NULL; |
| 4953 | ress = _PyUnicode_AsDefaultEncodedString(res, NULL); |
| 4954 | Py_DECREF(res); |
| 4955 | return ress; |
| 4956 | } |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4957 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4958 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 4959 | static Py_hash_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4960 | slot_tp_hash(PyObject *self) |
| 4961 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4962 | PyObject *func, *res; |
| 4963 | static PyObject *hash_str; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 4964 | Py_ssize_t h; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4965 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4966 | func = lookup_method(self, "__hash__", &hash_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 4967 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4968 | if (func == Py_None) { |
| 4969 | Py_DECREF(func); |
| 4970 | func = NULL; |
| 4971 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 4972 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4973 | if (func == NULL) { |
| 4974 | return PyObject_HashNotImplemented(self); |
| 4975 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 4976 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4977 | res = PyEval_CallObject(func, NULL); |
| 4978 | Py_DECREF(func); |
| 4979 | if (res == NULL) |
| 4980 | return -1; |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 4981 | |
| 4982 | if (!PyLong_Check(res)) { |
| 4983 | PyErr_SetString(PyExc_TypeError, |
| 4984 | "__hash__ method should return an integer"); |
| 4985 | return -1; |
| 4986 | } |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 4987 | /* Transform the PyLong `res` to a Py_hash_t `h`. For an existing |
| 4988 | hashable Python object x, hash(x) will always lie within the range of |
| 4989 | Py_hash_t. Therefore our transformation must preserve values that |
| 4990 | already lie within this range, to ensure that if x.__hash__() returns |
| 4991 | hash(y) then hash(x) == hash(y). */ |
| 4992 | h = PyLong_AsSsize_t(res); |
| 4993 | if (h == -1 && PyErr_Occurred()) { |
| 4994 | /* res was not within the range of a Py_hash_t, so we're free to |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 4995 | use any sufficiently bit-mixing transformation; |
| 4996 | long.__hash__ will do nicely. */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 4997 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4998 | h = PyLong_Type.tp_hash(res); |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 4999 | } |
Benjamin Peterson | e7dfeeb | 2010-10-17 21:27:01 +0000 | [diff] [blame] | 5000 | /* -1 is reserved for errors. */ |
| 5001 | if (h == -1) |
| 5002 | h = -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5003 | Py_DECREF(res); |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 5004 | return h; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5005 | } |
| 5006 | |
| 5007 | static PyObject * |
| 5008 | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 5009 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5010 | static PyObject *call_str; |
| 5011 | PyObject *meth = lookup_method(self, "__call__", &call_str); |
| 5012 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5013 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5014 | if (meth == NULL) |
| 5015 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5016 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5017 | res = PyObject_Call(meth, args, kwds); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5018 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5019 | Py_DECREF(meth); |
| 5020 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5021 | } |
| 5022 | |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5023 | /* There are two slot dispatch functions for tp_getattro. |
| 5024 | |
| 5025 | - slot_tp_getattro() is used when __getattribute__ is overridden |
| 5026 | but no __getattr__ hook is present; |
| 5027 | |
| 5028 | - slot_tp_getattr_hook() is used when a __getattr__ hook is present. |
| 5029 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5030 | The code in update_one_slot() always installs slot_tp_getattr_hook(); this |
| 5031 | detects the absence of __getattr__ and then installs the simpler slot if |
| 5032 | necessary. */ |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5033 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5034 | static PyObject * |
| 5035 | slot_tp_getattro(PyObject *self, PyObject *name) |
| 5036 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5037 | static PyObject *getattribute_str = NULL; |
| 5038 | return call_method(self, "__getattribute__", &getattribute_str, |
| 5039 | "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5040 | } |
| 5041 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5042 | static PyObject * |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5043 | call_attribute(PyObject *self, PyObject *attr, PyObject *name) |
| 5044 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5045 | PyObject *res, *descr = NULL; |
| 5046 | descrgetfunc f = Py_TYPE(attr)->tp_descr_get; |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5048 | if (f != NULL) { |
| 5049 | descr = f(attr, self, (PyObject *)(Py_TYPE(self))); |
| 5050 | if (descr == NULL) |
| 5051 | return NULL; |
| 5052 | else |
| 5053 | attr = descr; |
| 5054 | } |
| 5055 | res = PyObject_CallFunctionObjArgs(attr, name, NULL); |
| 5056 | Py_XDECREF(descr); |
| 5057 | return res; |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5058 | } |
| 5059 | |
| 5060 | static PyObject * |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5061 | slot_tp_getattr_hook(PyObject *self, PyObject *name) |
| 5062 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5063 | PyTypeObject *tp = Py_TYPE(self); |
| 5064 | PyObject *getattr, *getattribute, *res; |
| 5065 | static PyObject *getattribute_str = NULL; |
| 5066 | static PyObject *getattr_str = NULL; |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5067 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5068 | if (getattr_str == NULL) { |
| 5069 | getattr_str = PyUnicode_InternFromString("__getattr__"); |
| 5070 | if (getattr_str == NULL) |
| 5071 | return NULL; |
| 5072 | } |
| 5073 | if (getattribute_str == NULL) { |
| 5074 | getattribute_str = |
| 5075 | PyUnicode_InternFromString("__getattribute__"); |
| 5076 | if (getattribute_str == NULL) |
| 5077 | return NULL; |
| 5078 | } |
| 5079 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5080 | method fully for each attribute lookup for classes with |
| 5081 | __getattr__, even when the attribute is present. So we use |
| 5082 | _PyType_Lookup and create the method only when needed, with |
| 5083 | call_attribute. */ |
| 5084 | getattr = _PyType_Lookup(tp, getattr_str); |
| 5085 | if (getattr == NULL) { |
| 5086 | /* No __getattr__ hook: use a simpler dispatcher */ |
| 5087 | tp->tp_getattro = slot_tp_getattro; |
| 5088 | return slot_tp_getattro(self, name); |
| 5089 | } |
| 5090 | Py_INCREF(getattr); |
| 5091 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5092 | method fully for each attribute lookup for classes with |
| 5093 | __getattr__, even when self has the default __getattribute__ |
| 5094 | method. So we use _PyType_Lookup and create the method only when |
| 5095 | needed, with call_attribute. */ |
| 5096 | getattribute = _PyType_Lookup(tp, getattribute_str); |
| 5097 | if (getattribute == NULL || |
| 5098 | (Py_TYPE(getattribute) == &PyWrapperDescr_Type && |
| 5099 | ((PyWrapperDescrObject *)getattribute)->d_wrapped == |
| 5100 | (void *)PyObject_GenericGetAttr)) |
| 5101 | res = PyObject_GenericGetAttr(self, name); |
| 5102 | else { |
| 5103 | Py_INCREF(getattribute); |
| 5104 | res = call_attribute(self, getattribute, name); |
| 5105 | Py_DECREF(getattribute); |
| 5106 | } |
| 5107 | if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 5108 | PyErr_Clear(); |
| 5109 | res = call_attribute(self, getattr, name); |
| 5110 | } |
| 5111 | Py_DECREF(getattr); |
| 5112 | return res; |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5113 | } |
| 5114 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5115 | static int |
| 5116 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) |
| 5117 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5118 | PyObject *res; |
| 5119 | static PyObject *delattr_str, *setattr_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5121 | if (value == NULL) |
| 5122 | res = call_method(self, "__delattr__", &delattr_str, |
| 5123 | "(O)", name); |
| 5124 | else |
| 5125 | res = call_method(self, "__setattr__", &setattr_str, |
| 5126 | "(OO)", name, value); |
| 5127 | if (res == NULL) |
| 5128 | return -1; |
| 5129 | Py_DECREF(res); |
| 5130 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5131 | } |
| 5132 | |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 5133 | static char *name_op[] = { |
| 5134 | "__lt__", |
| 5135 | "__le__", |
| 5136 | "__eq__", |
| 5137 | "__ne__", |
| 5138 | "__gt__", |
| 5139 | "__ge__", |
| 5140 | }; |
| 5141 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5142 | static PyObject * |
Mark Dickinson | 6f1d049 | 2009-11-15 13:58:49 +0000 | [diff] [blame] | 5143 | slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5144 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5145 | PyObject *func, *args, *res; |
| 5146 | static PyObject *op_str[6]; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5148 | func = lookup_method(self, name_op[op], &op_str[op]); |
| 5149 | if (func == NULL) { |
| 5150 | PyErr_Clear(); |
| 5151 | Py_INCREF(Py_NotImplemented); |
| 5152 | return Py_NotImplemented; |
| 5153 | } |
| 5154 | args = PyTuple_Pack(1, other); |
| 5155 | if (args == NULL) |
| 5156 | res = NULL; |
| 5157 | else { |
| 5158 | res = PyObject_Call(func, args, NULL); |
| 5159 | Py_DECREF(args); |
| 5160 | } |
| 5161 | Py_DECREF(func); |
| 5162 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5163 | } |
| 5164 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5165 | static PyObject * |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5166 | slot_tp_iter(PyObject *self) |
| 5167 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5168 | PyObject *func, *res; |
| 5169 | static PyObject *iter_str, *getitem_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5171 | func = lookup_method(self, "__iter__", &iter_str); |
| 5172 | if (func != NULL) { |
| 5173 | PyObject *args; |
| 5174 | args = res = PyTuple_New(0); |
| 5175 | if (args != NULL) { |
| 5176 | res = PyObject_Call(func, args, NULL); |
| 5177 | Py_DECREF(args); |
| 5178 | } |
| 5179 | Py_DECREF(func); |
| 5180 | return res; |
| 5181 | } |
| 5182 | PyErr_Clear(); |
| 5183 | func = lookup_method(self, "__getitem__", &getitem_str); |
| 5184 | if (func == NULL) { |
| 5185 | PyErr_Format(PyExc_TypeError, |
| 5186 | "'%.200s' object is not iterable", |
| 5187 | Py_TYPE(self)->tp_name); |
| 5188 | return NULL; |
| 5189 | } |
| 5190 | Py_DECREF(func); |
| 5191 | return PySeqIter_New(self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5192 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5193 | |
| 5194 | static PyObject * |
| 5195 | slot_tp_iternext(PyObject *self) |
| 5196 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5197 | static PyObject *next_str; |
| 5198 | return call_method(self, "__next__", &next_str, "()"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5199 | } |
| 5200 | |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5201 | static PyObject * |
| 5202 | slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 5203 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5204 | PyTypeObject *tp = Py_TYPE(self); |
| 5205 | PyObject *get; |
| 5206 | static PyObject *get_str = NULL; |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5208 | if (get_str == NULL) { |
| 5209 | get_str = PyUnicode_InternFromString("__get__"); |
| 5210 | if (get_str == NULL) |
| 5211 | return NULL; |
| 5212 | } |
| 5213 | get = _PyType_Lookup(tp, get_str); |
| 5214 | if (get == NULL) { |
| 5215 | /* Avoid further slowdowns */ |
| 5216 | if (tp->tp_descr_get == slot_tp_descr_get) |
| 5217 | tp->tp_descr_get = NULL; |
| 5218 | Py_INCREF(self); |
| 5219 | return self; |
| 5220 | } |
| 5221 | if (obj == NULL) |
| 5222 | obj = Py_None; |
| 5223 | if (type == NULL) |
| 5224 | type = Py_None; |
| 5225 | return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5226 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5227 | |
| 5228 | static int |
| 5229 | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) |
| 5230 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5231 | PyObject *res; |
| 5232 | static PyObject *del_str, *set_str; |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 5233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5234 | if (value == NULL) |
| 5235 | res = call_method(self, "__delete__", &del_str, |
| 5236 | "(O)", target); |
| 5237 | else |
| 5238 | res = call_method(self, "__set__", &set_str, |
| 5239 | "(OO)", target, value); |
| 5240 | if (res == NULL) |
| 5241 | return -1; |
| 5242 | Py_DECREF(res); |
| 5243 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5244 | } |
| 5245 | |
| 5246 | static int |
| 5247 | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 5248 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5249 | static PyObject *init_str; |
| 5250 | PyObject *meth = lookup_method(self, "__init__", &init_str); |
| 5251 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5253 | if (meth == NULL) |
| 5254 | return -1; |
| 5255 | res = PyObject_Call(meth, args, kwds); |
| 5256 | Py_DECREF(meth); |
| 5257 | if (res == NULL) |
| 5258 | return -1; |
| 5259 | if (res != Py_None) { |
| 5260 | PyErr_Format(PyExc_TypeError, |
| 5261 | "__init__() should return None, not '%.200s'", |
| 5262 | Py_TYPE(res)->tp_name); |
| 5263 | Py_DECREF(res); |
| 5264 | return -1; |
| 5265 | } |
| 5266 | Py_DECREF(res); |
| 5267 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5268 | } |
| 5269 | |
| 5270 | static PyObject * |
| 5271 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 5272 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5273 | static PyObject *new_str; |
| 5274 | PyObject *func; |
| 5275 | PyObject *newargs, *x; |
| 5276 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5278 | if (new_str == NULL) { |
| 5279 | new_str = PyUnicode_InternFromString("__new__"); |
| 5280 | if (new_str == NULL) |
| 5281 | return NULL; |
| 5282 | } |
| 5283 | func = PyObject_GetAttr((PyObject *)type, new_str); |
| 5284 | if (func == NULL) |
| 5285 | return NULL; |
| 5286 | assert(PyTuple_Check(args)); |
| 5287 | n = PyTuple_GET_SIZE(args); |
| 5288 | newargs = PyTuple_New(n+1); |
| 5289 | if (newargs == NULL) |
| 5290 | return NULL; |
| 5291 | Py_INCREF(type); |
| 5292 | PyTuple_SET_ITEM(newargs, 0, (PyObject *)type); |
| 5293 | for (i = 0; i < n; i++) { |
| 5294 | x = PyTuple_GET_ITEM(args, i); |
| 5295 | Py_INCREF(x); |
| 5296 | PyTuple_SET_ITEM(newargs, i+1, x); |
| 5297 | } |
| 5298 | x = PyObject_Call(func, newargs, kwds); |
| 5299 | Py_DECREF(newargs); |
| 5300 | Py_DECREF(func); |
| 5301 | return x; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5302 | } |
| 5303 | |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5304 | static void |
| 5305 | slot_tp_del(PyObject *self) |
| 5306 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5307 | static PyObject *del_str = NULL; |
| 5308 | PyObject *del, *res; |
| 5309 | PyObject *error_type, *error_value, *error_traceback; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5311 | /* Temporarily resurrect the object. */ |
| 5312 | assert(self->ob_refcnt == 0); |
| 5313 | self->ob_refcnt = 1; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5315 | /* Save the current exception, if any. */ |
| 5316 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5318 | /* Execute __del__ method, if any. */ |
| 5319 | del = lookup_maybe(self, "__del__", &del_str); |
| 5320 | if (del != NULL) { |
| 5321 | res = PyEval_CallObject(del, NULL); |
| 5322 | if (res == NULL) |
| 5323 | PyErr_WriteUnraisable(del); |
| 5324 | else |
| 5325 | Py_DECREF(res); |
| 5326 | Py_DECREF(del); |
| 5327 | } |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5329 | /* Restore the saved exception. */ |
| 5330 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5332 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 5333 | * cause a recursive call. |
| 5334 | */ |
| 5335 | assert(self->ob_refcnt > 0); |
| 5336 | if (--self->ob_refcnt == 0) |
| 5337 | return; /* this is the normal path out */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5339 | /* __del__ resurrected it! Make it look like the original Py_DECREF |
| 5340 | * never happened. |
| 5341 | */ |
| 5342 | { |
| 5343 | Py_ssize_t refcnt = self->ob_refcnt; |
| 5344 | _Py_NewReference(self); |
| 5345 | self->ob_refcnt = refcnt; |
| 5346 | } |
| 5347 | assert(!PyType_IS_GC(Py_TYPE(self)) || |
| 5348 | _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED); |
| 5349 | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so |
| 5350 | * we need to undo that. */ |
| 5351 | _Py_DEC_REFTOTAL; |
| 5352 | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object |
| 5353 | * chain, so no more to do there. |
| 5354 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
| 5355 | * _Py_NewReference bumped tp_allocs: both of those need to be |
| 5356 | * undone. |
| 5357 | */ |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5358 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5359 | --Py_TYPE(self)->tp_frees; |
| 5360 | --Py_TYPE(self)->tp_allocs; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 5361 | #endif |
| 5362 | } |
| 5363 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5364 | |
| 5365 | /* 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] | 5366 | functions. The offsets here are relative to the 'PyHeapTypeObject' |
Guido van Rossum | e5c691a | 2003-03-07 15:13:17 +0000 | [diff] [blame] | 5367 | structure, which incorporates the additional structures used for numbers, |
| 5368 | sequences and mappings. |
| 5369 | 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] | 5370 | __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] | 5371 | slots (e.g. __str__ affects tp_str as well as tp_repr). The table is |
| 5372 | terminated with an all-zero entry. (This table is further initialized and |
| 5373 | sorted in init_slotdefs() below.) */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5374 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5375 | typedef struct wrapperbase slotdef; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5376 | |
| 5377 | #undef TPSLOT |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5378 | #undef FLSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5379 | #undef ETSLOT |
| 5380 | #undef SQSLOT |
| 5381 | #undef MPSLOT |
| 5382 | #undef NBSLOT |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5383 | #undef UNSLOT |
| 5384 | #undef IBSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5385 | #undef BINSLOT |
| 5386 | #undef RBINSLOT |
| 5387 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5388 | #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5389 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5390 | PyDoc_STR(DOC)} |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5391 | #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5392 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5393 | PyDoc_STR(DOC), FLAGS} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5394 | #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5395 | {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 5396 | PyDoc_STR(DOC)} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5397 | #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5398 | ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5399 | #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5400 | ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5401 | #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5402 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5403 | #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5404 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5405 | "x." NAME "() <==> " DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5406 | #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5407 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 5408 | "x." NAME "(y) <==> x" DOC "y") |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5409 | #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5410 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5411 | "x." NAME "(y) <==> x" DOC "y") |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5412 | #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5413 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5414 | "x." NAME "(y) <==> y" DOC "x") |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5415 | #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5416 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 5417 | "x." NAME "(y) <==> " DOC) |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 5418 | #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5419 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 5420 | "x." NAME "(y) <==> " DOC) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5421 | |
| 5422 | static slotdef slotdefs[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5423 | SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc, |
| 5424 | "x.__len__() <==> len(x)"), |
| 5425 | /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL. |
| 5426 | The logic in abstract.c always falls back to nb_add/nb_multiply in |
| 5427 | this case. Defining both the nb_* and the sq_* slots to call the |
| 5428 | user-defined methods has unexpected side-effects, as shown by |
| 5429 | test_descr.notimplemented() */ |
| 5430 | SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc, |
| 5431 | "x.__add__(y) <==> x+y"), |
| 5432 | SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc, |
| 5433 | "x.__mul__(n) <==> x*n"), |
| 5434 | SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc, |
| 5435 | "x.__rmul__(n) <==> n*x"), |
| 5436 | SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, |
| 5437 | "x.__getitem__(y) <==> x[y]"), |
| 5438 | SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, |
| 5439 | "x.__setitem__(i, y) <==> x[i]=y"), |
| 5440 | SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, |
| 5441 | "x.__delitem__(y) <==> del x[y]"), |
| 5442 | SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, |
| 5443 | "x.__contains__(y) <==> y in x"), |
| 5444 | SQSLOT("__iadd__", sq_inplace_concat, NULL, |
| 5445 | wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"), |
| 5446 | SQSLOT("__imul__", sq_inplace_repeat, NULL, |
| 5447 | wrap_indexargfunc, "x.__imul__(y) <==> x*=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5448 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5449 | MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc, |
| 5450 | "x.__len__() <==> len(x)"), |
| 5451 | MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, |
| 5452 | wrap_binaryfunc, |
| 5453 | "x.__getitem__(y) <==> x[y]"), |
| 5454 | MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, |
| 5455 | wrap_objobjargproc, |
| 5456 | "x.__setitem__(i, y) <==> x[i]=y"), |
| 5457 | MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, |
| 5458 | wrap_delitem, |
| 5459 | "x.__delitem__(y) <==> del x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5460 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5461 | BINSLOT("__add__", nb_add, slot_nb_add, |
| 5462 | "+"), |
| 5463 | RBINSLOT("__radd__", nb_add, slot_nb_add, |
| 5464 | "+"), |
| 5465 | BINSLOT("__sub__", nb_subtract, slot_nb_subtract, |
| 5466 | "-"), |
| 5467 | RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract, |
| 5468 | "-"), |
| 5469 | BINSLOT("__mul__", nb_multiply, slot_nb_multiply, |
| 5470 | "*"), |
| 5471 | RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply, |
| 5472 | "*"), |
| 5473 | BINSLOT("__mod__", nb_remainder, slot_nb_remainder, |
| 5474 | "%"), |
| 5475 | RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder, |
| 5476 | "%"), |
| 5477 | BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod, |
| 5478 | "divmod(x, y)"), |
| 5479 | RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod, |
| 5480 | "divmod(y, x)"), |
| 5481 | NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, |
| 5482 | "x.__pow__(y[, z]) <==> pow(x, y[, z])"), |
| 5483 | NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, |
| 5484 | "y.__rpow__(x[, z]) <==> pow(x, y[, z])"), |
| 5485 | UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"), |
| 5486 | UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"), |
| 5487 | UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, |
| 5488 | "abs(x)"), |
| 5489 | UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred, |
| 5490 | "x != 0"), |
| 5491 | UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"), |
| 5492 | BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5493 | RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 5494 | BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5495 | RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 5496 | BINSLOT("__and__", nb_and, slot_nb_and, "&"), |
| 5497 | RBINSLOT("__rand__", nb_and, slot_nb_and, "&"), |
| 5498 | BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"), |
| 5499 | RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), |
| 5500 | BINSLOT("__or__", nb_or, slot_nb_or, "|"), |
| 5501 | RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), |
| 5502 | UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, |
| 5503 | "int(x)"), |
| 5504 | UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, |
| 5505 | "float(x)"), |
| 5506 | NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc, |
| 5507 | "x[y:z] <==> x[y.__index__():z.__index__()]"), |
| 5508 | IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, |
| 5509 | wrap_binaryfunc, "+"), |
| 5510 | IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, |
| 5511 | wrap_binaryfunc, "-"), |
| 5512 | IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply, |
| 5513 | wrap_binaryfunc, "*"), |
| 5514 | IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder, |
| 5515 | wrap_binaryfunc, "%"), |
| 5516 | IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power, |
| 5517 | wrap_binaryfunc, "**"), |
| 5518 | IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift, |
| 5519 | wrap_binaryfunc, "<<"), |
| 5520 | IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift, |
| 5521 | wrap_binaryfunc, ">>"), |
| 5522 | IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and, |
| 5523 | wrap_binaryfunc, "&"), |
| 5524 | IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor, |
| 5525 | wrap_binaryfunc, "^"), |
| 5526 | IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or, |
| 5527 | wrap_binaryfunc, "|"), |
| 5528 | BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5529 | RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 5530 | BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5531 | RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 5532 | IBSLOT("__ifloordiv__", nb_inplace_floor_divide, |
| 5533 | slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"), |
| 5534 | IBSLOT("__itruediv__", nb_inplace_true_divide, |
| 5535 | slot_nb_inplace_true_divide, wrap_binaryfunc, "/"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5536 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5537 | TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, |
| 5538 | "x.__str__() <==> str(x)"), |
| 5539 | TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, |
| 5540 | "x.__repr__() <==> repr(x)"), |
| 5541 | TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, |
| 5542 | "x.__hash__() <==> hash(x)"), |
| 5543 | FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, |
| 5544 | "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS), |
| 5545 | TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, |
| 5546 | wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"), |
| 5547 | TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), |
| 5548 | TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), |
| 5549 | TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), |
| 5550 | TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, |
| 5551 | "x.__setattr__('name', value) <==> x.name = value"), |
| 5552 | TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), |
| 5553 | TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, |
| 5554 | "x.__delattr__('name') <==> del x.name"), |
| 5555 | TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), |
| 5556 | TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, |
| 5557 | "x.__lt__(y) <==> x<y"), |
| 5558 | TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, |
| 5559 | "x.__le__(y) <==> x<=y"), |
| 5560 | TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, |
| 5561 | "x.__eq__(y) <==> x==y"), |
| 5562 | TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, |
| 5563 | "x.__ne__(y) <==> x!=y"), |
| 5564 | TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, |
| 5565 | "x.__gt__(y) <==> x>y"), |
| 5566 | TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, |
| 5567 | "x.__ge__(y) <==> x>=y"), |
| 5568 | TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, |
| 5569 | "x.__iter__() <==> iter(x)"), |
| 5570 | TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next, |
| 5571 | "x.__next__() <==> next(x)"), |
| 5572 | TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, |
| 5573 | "descr.__get__(obj[, type]) -> value"), |
| 5574 | TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, |
| 5575 | "descr.__set__(obj, value)"), |
| 5576 | TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set, |
| 5577 | wrap_descr_delete, "descr.__delete__(obj)"), |
| 5578 | FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init, |
| 5579 | "x.__init__(...) initializes x; " |
Alexander Belopolsky | 977a684 | 2010-08-16 20:17:07 +0000 | [diff] [blame] | 5580 | "see help(type(x)) for signature", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5581 | PyWrapperFlag_KEYWORDS), |
| 5582 | TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""), |
| 5583 | TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""), |
| 5584 | {NULL} |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5585 | }; |
| 5586 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5587 | /* 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] | 5588 | 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] | 5589 | the offset to the type pointer, since it takes care to indirect through the |
| 5590 | proper indirection pointer (as_buffer, etc.); it returns NULL if the |
| 5591 | indirection pointer is NULL. */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5592 | static void ** |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5593 | slotptr(PyTypeObject *type, int ioffset) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5594 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5595 | char *ptr; |
| 5596 | long offset = ioffset; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5597 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5598 | /* Note: this depends on the order of the members of PyHeapTypeObject! */ |
| 5599 | assert(offset >= 0); |
| 5600 | assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer)); |
| 5601 | if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) { |
| 5602 | ptr = (char *)type->tp_as_sequence; |
| 5603 | offset -= offsetof(PyHeapTypeObject, as_sequence); |
| 5604 | } |
| 5605 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) { |
| 5606 | ptr = (char *)type->tp_as_mapping; |
| 5607 | offset -= offsetof(PyHeapTypeObject, as_mapping); |
| 5608 | } |
| 5609 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) { |
| 5610 | ptr = (char *)type->tp_as_number; |
| 5611 | offset -= offsetof(PyHeapTypeObject, as_number); |
| 5612 | } |
| 5613 | else { |
| 5614 | ptr = (char *)type; |
| 5615 | } |
| 5616 | if (ptr != NULL) |
| 5617 | ptr += offset; |
| 5618 | return (void **)ptr; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5619 | } |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 5620 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5621 | /* Length of array of slotdef pointers used to store slots with the |
| 5622 | same __name__. There should be at most MAX_EQUIV-1 slotdef entries with |
| 5623 | the same __name__, for any __name__. Since that's a static property, it is |
| 5624 | appropriate to declare fixed-size arrays for this. */ |
| 5625 | #define MAX_EQUIV 10 |
| 5626 | |
| 5627 | /* Return a slot pointer for a given name, but ONLY if the attribute has |
| 5628 | exactly one slot function. The name must be an interned string. */ |
| 5629 | static void ** |
| 5630 | resolve_slotdups(PyTypeObject *type, PyObject *name) |
| 5631 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5632 | /* 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] | 5633 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5634 | /* pname and ptrs act as a little cache */ |
| 5635 | static PyObject *pname; |
| 5636 | static slotdef *ptrs[MAX_EQUIV]; |
| 5637 | slotdef *p, **pp; |
| 5638 | void **res, **ptr; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5640 | if (pname != name) { |
| 5641 | /* Collect all slotdefs that match name into ptrs. */ |
| 5642 | pname = name; |
| 5643 | pp = ptrs; |
| 5644 | for (p = slotdefs; p->name_strobj; p++) { |
| 5645 | if (p->name_strobj == name) |
| 5646 | *pp++ = p; |
| 5647 | } |
| 5648 | *pp = NULL; |
| 5649 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5650 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5651 | /* Look in all matching slots of the type; if exactly one of these has |
| 5652 | a filled-in slot, return its value. Otherwise return NULL. */ |
| 5653 | res = NULL; |
| 5654 | for (pp = ptrs; *pp; pp++) { |
| 5655 | ptr = slotptr(type, (*pp)->offset); |
| 5656 | if (ptr == NULL || *ptr == NULL) |
| 5657 | continue; |
| 5658 | if (res != NULL) |
| 5659 | return NULL; |
| 5660 | res = ptr; |
| 5661 | } |
| 5662 | return res; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5663 | } |
| 5664 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5665 | /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5666 | does some incredibly complex thinking and then sticks something into the |
| 5667 | slot. (It sees if the adjacent slotdefs for the same slot have conflicting |
| 5668 | interests, and then stores a generic wrapper or a specific function into |
| 5669 | the slot.) Return a pointer to the next slotdef with a different offset, |
| 5670 | because that's convenient for fixup_slot_dispatchers(). */ |
| 5671 | static slotdef * |
| 5672 | update_one_slot(PyTypeObject *type, slotdef *p) |
| 5673 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5674 | PyObject *descr; |
| 5675 | PyWrapperDescrObject *d; |
| 5676 | void *generic = NULL, *specific = NULL; |
| 5677 | int use_generic = 0; |
| 5678 | int offset = p->offset; |
| 5679 | void **ptr = slotptr(type, offset); |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5681 | if (ptr == NULL) { |
| 5682 | do { |
| 5683 | ++p; |
| 5684 | } while (p->offset == offset); |
| 5685 | return p; |
| 5686 | } |
| 5687 | do { |
| 5688 | descr = _PyType_Lookup(type, p->name_strobj); |
| 5689 | if (descr == NULL) { |
| 5690 | if (ptr == (void**)&type->tp_iternext) { |
| 5691 | specific = _PyObject_NextNotImplemented; |
| 5692 | } |
| 5693 | continue; |
| 5694 | } |
| 5695 | if (Py_TYPE(descr) == &PyWrapperDescr_Type) { |
| 5696 | void **tptr = resolve_slotdups(type, p->name_strobj); |
| 5697 | if (tptr == NULL || tptr == ptr) |
| 5698 | generic = p->function; |
| 5699 | d = (PyWrapperDescrObject *)descr; |
| 5700 | if (d->d_base->wrapper == p->wrapper && |
| 5701 | PyType_IsSubtype(type, PyDescr_TYPE(d))) |
| 5702 | { |
| 5703 | if (specific == NULL || |
| 5704 | specific == d->d_wrapped) |
| 5705 | specific = d->d_wrapped; |
| 5706 | else |
| 5707 | use_generic = 1; |
| 5708 | } |
| 5709 | } |
| 5710 | else if (Py_TYPE(descr) == &PyCFunction_Type && |
| 5711 | PyCFunction_GET_FUNCTION(descr) == |
| 5712 | (PyCFunction)tp_new_wrapper && |
| 5713 | ptr == (void**)&type->tp_new) |
| 5714 | { |
| 5715 | /* The __new__ wrapper is not a wrapper descriptor, |
| 5716 | so must be special-cased differently. |
| 5717 | If we don't do this, creating an instance will |
| 5718 | always use slot_tp_new which will look up |
| 5719 | __new__ in the MRO which will call tp_new_wrapper |
| 5720 | which will look through the base classes looking |
| 5721 | for a static base and call its tp_new (usually |
| 5722 | PyType_GenericNew), after performing various |
| 5723 | sanity checks and constructing a new argument |
| 5724 | list. Cut all that nonsense short -- this speeds |
| 5725 | up instance creation tremendously. */ |
| 5726 | specific = (void *)type->tp_new; |
| 5727 | /* XXX I'm not 100% sure that there isn't a hole |
| 5728 | in this reasoning that requires additional |
| 5729 | sanity checks. I'll buy the first person to |
| 5730 | point out a bug in this reasoning a beer. */ |
| 5731 | } |
| 5732 | else if (descr == Py_None && |
| 5733 | ptr == (void**)&type->tp_hash) { |
| 5734 | /* We specifically allow __hash__ to be set to None |
| 5735 | to prevent inheritance of the default |
| 5736 | implementation from object.__hash__ */ |
| 5737 | specific = PyObject_HashNotImplemented; |
| 5738 | } |
| 5739 | else { |
| 5740 | use_generic = 1; |
| 5741 | generic = p->function; |
| 5742 | } |
| 5743 | } while ((++p)->offset == offset); |
| 5744 | if (specific && !use_generic) |
| 5745 | *ptr = specific; |
| 5746 | else |
| 5747 | *ptr = generic; |
| 5748 | return p; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5749 | } |
| 5750 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5751 | /* In the type, update the slots whose slotdefs are gathered in the pp array. |
| 5752 | This is a callback for update_subclasses(). */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5753 | static int |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5754 | update_slots_callback(PyTypeObject *type, void *data) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5755 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5756 | slotdef **pp = (slotdef **)data; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5757 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5758 | for (; *pp; pp++) |
| 5759 | update_one_slot(type, *pp); |
| 5760 | return 0; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5761 | } |
| 5762 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5763 | /* Comparison function for qsort() to compare slotdefs by their offset, and |
| 5764 | for equal offset by their address (to force a stable sort). */ |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5765 | static int |
| 5766 | slotdef_cmp(const void *aa, const void *bb) |
| 5767 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5768 | const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb; |
| 5769 | int c = a->offset - b->offset; |
| 5770 | if (c != 0) |
| 5771 | return c; |
| 5772 | else |
| 5773 | /* Cannot use a-b, as this gives off_t, |
| 5774 | which may lose precision when converted to int. */ |
| 5775 | return (a > b) ? 1 : (a < b) ? -1 : 0; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5776 | } |
| 5777 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5778 | /* Initialize the slotdefs table by adding interned string objects for the |
| 5779 | names and sorting the entries. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5780 | static void |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 5781 | init_slotdefs(void) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5782 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5783 | slotdef *p; |
| 5784 | static int initialized = 0; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5785 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5786 | if (initialized) |
| 5787 | return; |
| 5788 | for (p = slotdefs; p->name; p++) { |
| 5789 | p->name_strobj = PyUnicode_InternFromString(p->name); |
| 5790 | if (!p->name_strobj) |
| 5791 | Py_FatalError("Out of memory interning slotdef names"); |
| 5792 | } |
| 5793 | qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef), |
| 5794 | slotdef_cmp); |
| 5795 | initialized = 1; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5796 | } |
| 5797 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5798 | /* Update the slots after assignment to a class (type) attribute. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5799 | static int |
| 5800 | update_slot(PyTypeObject *type, PyObject *name) |
| 5801 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5802 | slotdef *ptrs[MAX_EQUIV]; |
| 5803 | slotdef *p; |
| 5804 | slotdef **pp; |
| 5805 | int offset; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5806 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5807 | /* Clear the VALID_VERSION flag of 'type' and all its |
| 5808 | subclasses. This could possibly be unified with the |
| 5809 | update_subclasses() recursion below, but carefully: |
| 5810 | they each have their own conditions on which to stop |
| 5811 | recursing into subclasses. */ |
| 5812 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 5813 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5814 | init_slotdefs(); |
| 5815 | pp = ptrs; |
| 5816 | for (p = slotdefs; p->name; p++) { |
| 5817 | /* XXX assume name is interned! */ |
| 5818 | if (p->name_strobj == name) |
| 5819 | *pp++ = p; |
| 5820 | } |
| 5821 | *pp = NULL; |
| 5822 | for (pp = ptrs; *pp; pp++) { |
| 5823 | p = *pp; |
| 5824 | offset = p->offset; |
| 5825 | while (p > slotdefs && (p-1)->offset == offset) |
| 5826 | --p; |
| 5827 | *pp = p; |
| 5828 | } |
| 5829 | if (ptrs[0] == NULL) |
| 5830 | return 0; /* Not an attribute that affects any slots */ |
| 5831 | return update_subclasses(type, name, |
| 5832 | update_slots_callback, (void *)ptrs); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 5833 | } |
| 5834 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5835 | /* Store the proper functions in the slot dispatches at class (type) |
| 5836 | definition time, based upon which operations the class overrides in its |
| 5837 | dict. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5838 | static void |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5839 | fixup_slot_dispatchers(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5840 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5841 | slotdef *p; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5842 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5843 | init_slotdefs(); |
| 5844 | for (p = slotdefs; p->name; ) |
| 5845 | p = update_one_slot(type, p); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5846 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5847 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 5848 | static void |
| 5849 | update_all_slots(PyTypeObject* type) |
| 5850 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5851 | slotdef *p; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 5852 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5853 | init_slotdefs(); |
| 5854 | for (p = slotdefs; p->name; p++) { |
| 5855 | /* update_slot returns int but can't actually fail */ |
| 5856 | update_slot(type, p->name_strobj); |
| 5857 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 5858 | } |
| 5859 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5860 | /* recurse_down_subclasses() and update_subclasses() are mutually |
| 5861 | recursive functions to call a callback for all subclasses, |
| 5862 | but refraining from recursing into subclasses that define 'name'. */ |
| 5863 | |
| 5864 | static int |
| 5865 | update_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5866 | update_callback callback, void *data) |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5867 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5868 | if (callback(type, data) < 0) |
| 5869 | return -1; |
| 5870 | return recurse_down_subclasses(type, name, callback, data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5871 | } |
| 5872 | |
| 5873 | static int |
| 5874 | recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5875 | update_callback callback, void *data) |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5876 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5877 | PyTypeObject *subclass; |
| 5878 | PyObject *ref, *subclasses, *dict; |
| 5879 | Py_ssize_t i, n; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5880 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5881 | subclasses = type->tp_subclasses; |
| 5882 | if (subclasses == NULL) |
| 5883 | return 0; |
| 5884 | assert(PyList_Check(subclasses)); |
| 5885 | n = PyList_GET_SIZE(subclasses); |
| 5886 | for (i = 0; i < n; i++) { |
| 5887 | ref = PyList_GET_ITEM(subclasses, i); |
| 5888 | assert(PyWeakref_CheckRef(ref)); |
| 5889 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 5890 | assert(subclass != NULL); |
| 5891 | if ((PyObject *)subclass == Py_None) |
| 5892 | continue; |
| 5893 | assert(PyType_Check(subclass)); |
| 5894 | /* Avoid recursing down into unaffected classes */ |
| 5895 | dict = subclass->tp_dict; |
| 5896 | if (dict != NULL && PyDict_Check(dict) && |
| 5897 | PyDict_GetItem(dict, name) != NULL) |
| 5898 | continue; |
| 5899 | if (update_subclasses(subclass, name, callback, data) < 0) |
| 5900 | return -1; |
| 5901 | } |
| 5902 | return 0; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5903 | } |
| 5904 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5905 | /* This function is called by PyType_Ready() to populate the type's |
| 5906 | dictionary with method descriptors for function slots. For each |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 5907 | function slot (like tp_repr) that's defined in the type, one or more |
| 5908 | corresponding descriptors are added in the type's tp_dict dictionary |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5909 | under the appropriate name (like __repr__). Some function slots |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 5910 | cause more than one descriptor to be added (for example, the nb_add |
| 5911 | slot adds both __add__ and __radd__ descriptors) and some function |
| 5912 | slots compete for the same descriptor (for example both sq_item and |
| 5913 | mp_subscript generate a __getitem__ descriptor). |
| 5914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5915 | In the latter case, the first slotdef entry encoutered wins. Since |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5916 | 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] | 5917 | PyHeapTypeObject, this gives us some control over disambiguating |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 5918 | between competing slots: the members of PyHeapTypeObject are listed |
| 5919 | from most general to least general, so the most general slot is |
| 5920 | preferred. In particular, because as_mapping comes before as_sequence, |
| 5921 | for a type that defines both mp_subscript and sq_item, mp_subscript |
| 5922 | wins. |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 5923 | |
| 5924 | This only adds new descriptors and doesn't overwrite entries in |
| 5925 | tp_dict that were previously defined. The descriptors contain a |
| 5926 | reference to the C function they must call, so that it's safe if they |
| 5927 | are copied into a subtype's __dict__ and the subtype has a different |
| 5928 | C function in its slot -- calling the method defined by the |
| 5929 | descriptor will call the C function that was used to create it, |
| 5930 | rather than the C function present in the slot when it is called. |
| 5931 | (This is important because a subtype may have a C function in the |
| 5932 | slot that calls the method from the dictionary, and we want to avoid |
| 5933 | infinite recursion here.) */ |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5934 | |
| 5935 | static int |
| 5936 | add_operators(PyTypeObject *type) |
| 5937 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5938 | PyObject *dict = type->tp_dict; |
| 5939 | slotdef *p; |
| 5940 | PyObject *descr; |
| 5941 | void **ptr; |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5942 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5943 | init_slotdefs(); |
| 5944 | for (p = slotdefs; p->name; p++) { |
| 5945 | if (p->wrapper == NULL) |
| 5946 | continue; |
| 5947 | ptr = slotptr(type, p->offset); |
| 5948 | if (!ptr || !*ptr) |
| 5949 | continue; |
| 5950 | if (PyDict_GetItem(dict, p->name_strobj)) |
| 5951 | continue; |
| 5952 | if (*ptr == PyObject_HashNotImplemented) { |
| 5953 | /* Classes may prevent the inheritance of the tp_hash |
| 5954 | slot by storing PyObject_HashNotImplemented in it. Make it |
| 5955 | visible as a None value for the __hash__ attribute. */ |
| 5956 | if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0) |
| 5957 | return -1; |
| 5958 | } |
| 5959 | else { |
| 5960 | descr = PyDescr_NewWrapper(type, p, *ptr); |
| 5961 | if (descr == NULL) |
| 5962 | return -1; |
| 5963 | if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) |
| 5964 | return -1; |
| 5965 | Py_DECREF(descr); |
| 5966 | } |
| 5967 | } |
| 5968 | if (type->tp_new != NULL) { |
| 5969 | if (add_tp_new_wrapper(type) < 0) |
| 5970 | return -1; |
| 5971 | } |
| 5972 | return 0; |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 5973 | } |
| 5974 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5975 | |
| 5976 | /* Cooperative 'super' */ |
| 5977 | |
| 5978 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5979 | PyObject_HEAD |
| 5980 | PyTypeObject *type; |
| 5981 | PyObject *obj; |
| 5982 | PyTypeObject *obj_type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5983 | } superobject; |
| 5984 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 5985 | static PyMemberDef super_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5986 | {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY, |
| 5987 | "the class invoking super()"}, |
| 5988 | {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY, |
| 5989 | "the instance invoking super(); may be None"}, |
| 5990 | {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY, |
| 5991 | "the type of the instance invoking super(); may be None"}, |
| 5992 | {0} |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 5993 | }; |
| 5994 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5995 | static void |
| 5996 | super_dealloc(PyObject *self) |
| 5997 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5998 | superobject *su = (superobject *)self; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 5999 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6000 | _PyObject_GC_UNTRACK(self); |
| 6001 | Py_XDECREF(su->obj); |
| 6002 | Py_XDECREF(su->type); |
| 6003 | Py_XDECREF(su->obj_type); |
| 6004 | Py_TYPE(self)->tp_free(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6005 | } |
| 6006 | |
| 6007 | static PyObject * |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6008 | super_repr(PyObject *self) |
| 6009 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6010 | superobject *su = (superobject *)self; |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6012 | if (su->obj_type) |
| 6013 | return PyUnicode_FromFormat( |
| 6014 | "<super: <class '%s'>, <%s object>>", |
| 6015 | su->type ? su->type->tp_name : "NULL", |
| 6016 | su->obj_type->tp_name); |
| 6017 | else |
| 6018 | return PyUnicode_FromFormat( |
| 6019 | "<super: <class '%s'>, NULL>", |
| 6020 | su->type ? su->type->tp_name : "NULL"); |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6021 | } |
| 6022 | |
| 6023 | static PyObject * |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6024 | super_getattro(PyObject *self, PyObject *name) |
| 6025 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6026 | superobject *su = (superobject *)self; |
| 6027 | int skip = su->obj_type == NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6028 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6029 | if (!skip) { |
| 6030 | /* We want __class__ to return the class of the super object |
| 6031 | (i.e. super, or a subclass), not the class of su->obj. */ |
| 6032 | skip = (PyUnicode_Check(name) && |
| 6033 | PyUnicode_GET_SIZE(name) == 9 && |
| 6034 | PyUnicode_CompareWithASCIIString(name, "__class__") == 0); |
| 6035 | } |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 6036 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6037 | if (!skip) { |
| 6038 | PyObject *mro, *res, *tmp, *dict; |
| 6039 | PyTypeObject *starttype; |
| 6040 | descrgetfunc f; |
| 6041 | Py_ssize_t i, n; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6042 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6043 | starttype = su->obj_type; |
| 6044 | mro = starttype->tp_mro; |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 6045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6046 | if (mro == NULL) |
| 6047 | n = 0; |
| 6048 | else { |
| 6049 | assert(PyTuple_Check(mro)); |
| 6050 | n = PyTuple_GET_SIZE(mro); |
| 6051 | } |
| 6052 | for (i = 0; i < n; i++) { |
| 6053 | if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) |
| 6054 | break; |
| 6055 | } |
| 6056 | i++; |
| 6057 | res = NULL; |
| 6058 | for (; i < n; i++) { |
| 6059 | tmp = PyTuple_GET_ITEM(mro, i); |
| 6060 | if (PyType_Check(tmp)) |
| 6061 | dict = ((PyTypeObject *)tmp)->tp_dict; |
| 6062 | else |
| 6063 | continue; |
| 6064 | res = PyDict_GetItem(dict, name); |
| 6065 | if (res != NULL) { |
| 6066 | Py_INCREF(res); |
| 6067 | f = Py_TYPE(res)->tp_descr_get; |
| 6068 | if (f != NULL) { |
| 6069 | tmp = f(res, |
| 6070 | /* Only pass 'obj' param if |
| 6071 | this is instance-mode super |
| 6072 | (See SF ID #743627) |
| 6073 | */ |
| 6074 | (su->obj == (PyObject *) |
| 6075 | su->obj_type |
| 6076 | ? (PyObject *)NULL |
| 6077 | : su->obj), |
| 6078 | (PyObject *)starttype); |
| 6079 | Py_DECREF(res); |
| 6080 | res = tmp; |
| 6081 | } |
| 6082 | return res; |
| 6083 | } |
| 6084 | } |
| 6085 | } |
| 6086 | return PyObject_GenericGetAttr(self, name); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6087 | } |
| 6088 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6089 | static PyTypeObject * |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6090 | supercheck(PyTypeObject *type, PyObject *obj) |
| 6091 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6092 | /* Check that a super() call makes sense. Return a type object. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6093 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6094 | 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] | 6095 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6096 | - If it is a class, it must be a subclass of 'type'. This case is |
| 6097 | used for class methods; the return value is obj. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6098 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6099 | - If it is an instance, it must be an instance of 'type'. This is |
| 6100 | the normal case; the return value is obj.__class__. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6101 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6102 | But... when obj is an instance, we want to allow for the case where |
| 6103 | Py_TYPE(obj) is not a subclass of type, but obj.__class__ is! |
| 6104 | This will allow using super() with a proxy for obj. |
| 6105 | */ |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6106 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6107 | /* Check for first bullet above (special case) */ |
| 6108 | if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) { |
| 6109 | Py_INCREF(obj); |
| 6110 | return (PyTypeObject *)obj; |
| 6111 | } |
Guido van Rossum | 8e80a72 | 2003-02-18 19:22:22 +0000 | [diff] [blame] | 6112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6113 | /* Normal case */ |
| 6114 | if (PyType_IsSubtype(Py_TYPE(obj), type)) { |
| 6115 | Py_INCREF(Py_TYPE(obj)); |
| 6116 | return Py_TYPE(obj); |
| 6117 | } |
| 6118 | else { |
| 6119 | /* Try the slow way */ |
| 6120 | static PyObject *class_str = NULL; |
| 6121 | PyObject *class_attr; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6123 | if (class_str == NULL) { |
| 6124 | class_str = PyUnicode_FromString("__class__"); |
| 6125 | if (class_str == NULL) |
| 6126 | return NULL; |
| 6127 | } |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6129 | class_attr = PyObject_GetAttr(obj, class_str); |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6130 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6131 | if (class_attr != NULL && |
| 6132 | PyType_Check(class_attr) && |
| 6133 | (PyTypeObject *)class_attr != Py_TYPE(obj)) |
| 6134 | { |
| 6135 | int ok = PyType_IsSubtype( |
| 6136 | (PyTypeObject *)class_attr, type); |
| 6137 | if (ok) |
| 6138 | return (PyTypeObject *)class_attr; |
| 6139 | } |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6141 | if (class_attr == NULL) |
| 6142 | PyErr_Clear(); |
| 6143 | else |
| 6144 | Py_DECREF(class_attr); |
| 6145 | } |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6147 | PyErr_SetString(PyExc_TypeError, |
| 6148 | "super(type, obj): " |
| 6149 | "obj must be an instance or subtype of type"); |
| 6150 | return NULL; |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6151 | } |
| 6152 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6153 | static PyObject * |
| 6154 | super_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 6155 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6156 | superobject *su = (superobject *)self; |
| 6157 | superobject *newobj; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6159 | if (obj == NULL || obj == Py_None || su->obj != NULL) { |
| 6160 | /* Not binding to an object, or already bound */ |
| 6161 | Py_INCREF(self); |
| 6162 | return self; |
| 6163 | } |
| 6164 | if (Py_TYPE(su) != &PySuper_Type) |
| 6165 | /* If su is an instance of a (strict) subclass of super, |
| 6166 | call its type */ |
| 6167 | return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su), |
| 6168 | su->type, obj, NULL); |
| 6169 | else { |
| 6170 | /* Inline the common case */ |
| 6171 | PyTypeObject *obj_type = supercheck(su->type, obj); |
| 6172 | if (obj_type == NULL) |
| 6173 | return NULL; |
| 6174 | newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type, |
| 6175 | NULL, NULL); |
| 6176 | if (newobj == NULL) |
| 6177 | return NULL; |
| 6178 | Py_INCREF(su->type); |
| 6179 | Py_INCREF(obj); |
| 6180 | newobj->type = su->type; |
| 6181 | newobj->obj = obj; |
| 6182 | newobj->obj_type = obj_type; |
| 6183 | return (PyObject *)newobj; |
| 6184 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6185 | } |
| 6186 | |
| 6187 | static int |
| 6188 | super_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 6189 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6190 | superobject *su = (superobject *)self; |
| 6191 | PyTypeObject *type = NULL; |
| 6192 | PyObject *obj = NULL; |
| 6193 | PyTypeObject *obj_type = NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6194 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6195 | if (!_PyArg_NoKeywords("super", kwds)) |
| 6196 | return -1; |
| 6197 | if (!PyArg_ParseTuple(args, "|O!O:super", &PyType_Type, &type, &obj)) |
| 6198 | return -1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6199 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6200 | if (type == NULL) { |
| 6201 | /* Call super(), without args -- fill in from __class__ |
| 6202 | and first local variable on the stack. */ |
| 6203 | PyFrameObject *f = PyThreadState_GET()->frame; |
| 6204 | PyCodeObject *co = f->f_code; |
| 6205 | int i, n; |
| 6206 | if (co == NULL) { |
| 6207 | PyErr_SetString(PyExc_SystemError, |
| 6208 | "super(): no code object"); |
| 6209 | return -1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6210 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6211 | if (co->co_argcount == 0) { |
| 6212 | PyErr_SetString(PyExc_SystemError, |
| 6213 | "super(): no arguments"); |
| 6214 | return -1; |
| 6215 | } |
| 6216 | obj = f->f_localsplus[0]; |
| 6217 | if (obj == NULL) { |
| 6218 | PyErr_SetString(PyExc_SystemError, |
| 6219 | "super(): arg[0] deleted"); |
| 6220 | return -1; |
| 6221 | } |
| 6222 | if (co->co_freevars == NULL) |
| 6223 | n = 0; |
| 6224 | else { |
| 6225 | assert(PyTuple_Check(co->co_freevars)); |
| 6226 | n = PyTuple_GET_SIZE(co->co_freevars); |
| 6227 | } |
| 6228 | for (i = 0; i < n; i++) { |
| 6229 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 6230 | assert(PyUnicode_Check(name)); |
| 6231 | if (!PyUnicode_CompareWithASCIIString(name, |
| 6232 | "__class__")) { |
| 6233 | Py_ssize_t index = co->co_nlocals + |
| 6234 | PyTuple_GET_SIZE(co->co_cellvars) + i; |
| 6235 | PyObject *cell = f->f_localsplus[index]; |
| 6236 | if (cell == NULL || !PyCell_Check(cell)) { |
| 6237 | PyErr_SetString(PyExc_SystemError, |
| 6238 | "super(): bad __class__ cell"); |
| 6239 | return -1; |
| 6240 | } |
| 6241 | type = (PyTypeObject *) PyCell_GET(cell); |
| 6242 | if (type == NULL) { |
| 6243 | PyErr_SetString(PyExc_SystemError, |
| 6244 | "super(): empty __class__ cell"); |
| 6245 | return -1; |
| 6246 | } |
| 6247 | if (!PyType_Check(type)) { |
| 6248 | PyErr_Format(PyExc_SystemError, |
| 6249 | "super(): __class__ is not a type (%s)", |
| 6250 | Py_TYPE(type)->tp_name); |
| 6251 | return -1; |
| 6252 | } |
| 6253 | break; |
| 6254 | } |
| 6255 | } |
| 6256 | if (type == NULL) { |
| 6257 | PyErr_SetString(PyExc_SystemError, |
| 6258 | "super(): __class__ cell not found"); |
| 6259 | return -1; |
| 6260 | } |
| 6261 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6263 | if (obj == Py_None) |
| 6264 | obj = NULL; |
| 6265 | if (obj != NULL) { |
| 6266 | obj_type = supercheck(type, obj); |
| 6267 | if (obj_type == NULL) |
| 6268 | return -1; |
| 6269 | Py_INCREF(obj); |
| 6270 | } |
| 6271 | Py_INCREF(type); |
| 6272 | su->type = type; |
| 6273 | su->obj = obj; |
| 6274 | su->obj_type = obj_type; |
| 6275 | return 0; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6276 | } |
| 6277 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6278 | PyDoc_STRVAR(super_doc, |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6279 | "super() -> same as super(__class__, <first argument>)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6280 | "super(type) -> unbound super object\n" |
| 6281 | "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 6282 | "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6283 | "Typical use to call a cooperative superclass method:\n" |
| 6284 | "class C(B):\n" |
| 6285 | " def meth(self, arg):\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6286 | " super().meth(arg)\n" |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6287 | "This works for class methods too:\n" |
| 6288 | "class C(B):\n" |
| 6289 | " @classmethod\n" |
| 6290 | " def cmeth(cls, arg):\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6291 | " super().cmeth(arg)\n"); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6292 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6293 | static int |
| 6294 | super_traverse(PyObject *self, visitproc visit, void *arg) |
| 6295 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6296 | superobject *su = (superobject *)self; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6298 | Py_VISIT(su->obj); |
| 6299 | Py_VISIT(su->type); |
| 6300 | Py_VISIT(su->obj_type); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6302 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 6303 | } |
| 6304 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6305 | PyTypeObject PySuper_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6306 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 6307 | "super", /* tp_name */ |
| 6308 | sizeof(superobject), /* tp_basicsize */ |
| 6309 | 0, /* tp_itemsize */ |
| 6310 | /* methods */ |
| 6311 | super_dealloc, /* tp_dealloc */ |
| 6312 | 0, /* tp_print */ |
| 6313 | 0, /* tp_getattr */ |
| 6314 | 0, /* tp_setattr */ |
| 6315 | 0, /* tp_reserved */ |
| 6316 | super_repr, /* tp_repr */ |
| 6317 | 0, /* tp_as_number */ |
| 6318 | 0, /* tp_as_sequence */ |
| 6319 | 0, /* tp_as_mapping */ |
| 6320 | 0, /* tp_hash */ |
| 6321 | 0, /* tp_call */ |
| 6322 | 0, /* tp_str */ |
| 6323 | super_getattro, /* tp_getattro */ |
| 6324 | 0, /* tp_setattro */ |
| 6325 | 0, /* tp_as_buffer */ |
| 6326 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 6327 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 6328 | super_doc, /* tp_doc */ |
| 6329 | super_traverse, /* tp_traverse */ |
| 6330 | 0, /* tp_clear */ |
| 6331 | 0, /* tp_richcompare */ |
| 6332 | 0, /* tp_weaklistoffset */ |
| 6333 | 0, /* tp_iter */ |
| 6334 | 0, /* tp_iternext */ |
| 6335 | 0, /* tp_methods */ |
| 6336 | super_members, /* tp_members */ |
| 6337 | 0, /* tp_getset */ |
| 6338 | 0, /* tp_base */ |
| 6339 | 0, /* tp_dict */ |
| 6340 | super_descr_get, /* tp_descr_get */ |
| 6341 | 0, /* tp_descr_set */ |
| 6342 | 0, /* tp_dictoffset */ |
| 6343 | super_init, /* tp_init */ |
| 6344 | PyType_GenericAlloc, /* tp_alloc */ |
| 6345 | PyType_GenericNew, /* tp_new */ |
| 6346 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6347 | }; |