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 |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 17 | #define MCACHE_SIZE_EXP 9 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 18 | #define MCACHE_HASH(version, name_hash) \ |
| 19 | (((unsigned int)(version) * (unsigned int)(name_hash)) \ |
| 20 | >> (8*sizeof(unsigned int) - MCACHE_SIZE_EXP)) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 21 | #define MCACHE_HASH_METHOD(type, name) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | MCACHE_HASH((type)->tp_version_tag, \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 23 | ((PyASCIIObject *)(name))->hash) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 24 | #define MCACHE_CACHEABLE_NAME(name) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 25 | PyUnicode_CheckExact(name) && \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 26 | PyUnicode_READY(name) != -1 && \ |
| 27 | PyUnicode_GET_LENGTH(name) <= MCACHE_MAX_ATTR_SIZE |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 28 | |
| 29 | struct method_cache_entry { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 30 | unsigned int version; |
| 31 | PyObject *name; /* reference to exactly a str or None */ |
| 32 | PyObject *value; /* borrowed */ |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP]; |
| 36 | static unsigned int next_version_tag = 0; |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 37 | |
Martin v. Löwis | e75fc14 | 2013-11-07 18:46:53 +0100 | [diff] [blame] | 38 | /* alphabetical order */ |
| 39 | _Py_IDENTIFIER(__abstractmethods__); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 40 | _Py_IDENTIFIER(__class__); |
Martin v. Löwis | e75fc14 | 2013-11-07 18:46:53 +0100 | [diff] [blame] | 41 | _Py_IDENTIFIER(__delitem__); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 42 | _Py_IDENTIFIER(__dict__); |
| 43 | _Py_IDENTIFIER(__doc__); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 44 | _Py_IDENTIFIER(__getattribute__); |
Martin v. Löwis | e75fc14 | 2013-11-07 18:46:53 +0100 | [diff] [blame] | 45 | _Py_IDENTIFIER(__getitem__); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 46 | _Py_IDENTIFIER(__hash__); |
Martin v. Löwis | e75fc14 | 2013-11-07 18:46:53 +0100 | [diff] [blame] | 47 | _Py_IDENTIFIER(__len__); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 48 | _Py_IDENTIFIER(__module__); |
| 49 | _Py_IDENTIFIER(__name__); |
| 50 | _Py_IDENTIFIER(__new__); |
Martin v. Löwis | e75fc14 | 2013-11-07 18:46:53 +0100 | [diff] [blame] | 51 | _Py_IDENTIFIER(__setitem__); |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 52 | _Py_IDENTIFIER(builtins); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 53 | |
| 54 | static PyObject * |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 55 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 56 | |
Martin v. Löwis | 996b671 | 2014-07-26 16:44:07 +0200 | [diff] [blame] | 57 | static void |
Victor Stinner | 331a726 | 2014-07-27 16:11:30 +0200 | [diff] [blame] | 58 | clear_slotdefs(void); |
Martin v. Löwis | 996b671 | 2014-07-26 16:44:07 +0200 | [diff] [blame] | 59 | |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 60 | /* |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 61 | * finds the beginning of the docstring's introspection signature. |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 62 | * if present, returns a pointer pointing to the first '('. |
| 63 | * otherwise returns NULL. |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 64 | * |
| 65 | * doesn't guarantee that the signature is valid, only that it |
| 66 | * has a valid prefix. (the signature must also pass skip_signature.) |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 67 | */ |
| 68 | static const char * |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 69 | find_signature(const char *name, const char *doc) |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 70 | { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 71 | const char *dot; |
| 72 | size_t length; |
| 73 | |
| 74 | if (!doc) |
| 75 | return NULL; |
| 76 | |
| 77 | assert(name != NULL); |
| 78 | |
| 79 | /* for dotted names like classes, only use the last component */ |
| 80 | dot = strrchr(name, '.'); |
| 81 | if (dot) |
| 82 | name = dot + 1; |
| 83 | |
| 84 | length = strlen(name); |
| 85 | if (strncmp(doc, name, length)) |
| 86 | return NULL; |
| 87 | doc += length; |
| 88 | if (*doc != '(') |
| 89 | return NULL; |
| 90 | return doc; |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 93 | #define SIGNATURE_END_MARKER ")\n--\n\n" |
| 94 | #define SIGNATURE_END_MARKER_LENGTH 6 |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 95 | /* |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 96 | * skips past the end of the docstring's instrospection signature. |
| 97 | * (assumes doc starts with a valid signature prefix.) |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 98 | */ |
| 99 | static const char * |
| 100 | skip_signature(const char *doc) |
| 101 | { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 102 | while (*doc) { |
| 103 | if ((*doc == *SIGNATURE_END_MARKER) && |
| 104 | !strncmp(doc, SIGNATURE_END_MARKER, SIGNATURE_END_MARKER_LENGTH)) |
| 105 | return doc + SIGNATURE_END_MARKER_LENGTH; |
| 106 | if ((*doc == '\n') && (doc[1] == '\n')) |
| 107 | return NULL; |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 108 | doc++; |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 109 | } |
| 110 | return NULL; |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static const char * |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 114 | _PyType_DocWithoutSignature(const char *name, const char *internal_doc) |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 115 | { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 116 | const char *doc = find_signature(name, internal_doc); |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 117 | |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 118 | if (doc) { |
| 119 | doc = skip_signature(doc); |
| 120 | if (doc) |
| 121 | return doc; |
| 122 | } |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 123 | return internal_doc; |
| 124 | } |
| 125 | |
| 126 | PyObject * |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 127 | _PyType_GetDocFromInternalDoc(const char *name, const char *internal_doc) |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 128 | { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 129 | const char *doc = _PyType_DocWithoutSignature(name, internal_doc); |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 130 | |
| 131 | if (!doc) { |
| 132 | Py_INCREF(Py_None); |
| 133 | return Py_None; |
| 134 | } |
| 135 | |
| 136 | return PyUnicode_FromString(doc); |
| 137 | } |
| 138 | |
| 139 | PyObject * |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 140 | _PyType_GetTextSignatureFromInternalDoc(const char *name, const char *internal_doc) |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 141 | { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 142 | const char *start = find_signature(name, internal_doc); |
| 143 | const char *end; |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 144 | |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 145 | if (start) |
| 146 | end = skip_signature(start); |
| 147 | else |
| 148 | end = NULL; |
| 149 | if (!end) { |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 150 | Py_INCREF(Py_None); |
| 151 | return Py_None; |
| 152 | } |
| 153 | |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 154 | /* back "end" up until it points just past the final ')' */ |
| 155 | end -= SIGNATURE_END_MARKER_LENGTH - 1; |
| 156 | assert((end - start) >= 2); /* should be "()" at least */ |
| 157 | assert(end[-1] == ')'); |
| 158 | assert(end[0] == '\n'); |
| 159 | return PyUnicode_FromStringAndSize(start, end - start); |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 162 | unsigned int |
| 163 | PyType_ClearCache(void) |
| 164 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | Py_ssize_t i; |
| 166 | unsigned int cur_version_tag = next_version_tag - 1; |
| 167 | |
| 168 | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { |
| 169 | method_cache[i].version = 0; |
| 170 | Py_CLEAR(method_cache[i].name); |
| 171 | method_cache[i].value = NULL; |
| 172 | } |
| 173 | next_version_tag = 0; |
| 174 | /* mark all version tags as invalid */ |
| 175 | PyType_Modified(&PyBaseObject_Type); |
| 176 | return cur_version_tag; |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 177 | } |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 178 | |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 179 | void |
Antoine Pitrou | 957a23b | 2013-05-04 20:45:02 +0200 | [diff] [blame] | 180 | _PyType_Fini(void) |
| 181 | { |
| 182 | PyType_ClearCache(); |
Martin v. Löwis | 996b671 | 2014-07-26 16:44:07 +0200 | [diff] [blame] | 183 | clear_slotdefs(); |
Antoine Pitrou | 957a23b | 2013-05-04 20:45:02 +0200 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 187 | PyType_Modified(PyTypeObject *type) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 188 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | /* Invalidate any cached data for the specified type and all |
| 190 | subclasses. This function is called after the base |
| 191 | classes, mro, or attributes of the type are altered. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 192 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 193 | Invariants: |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 194 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 195 | - Py_TPFLAGS_VALID_VERSION_TAG is never set if |
| 196 | Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type |
| 197 | objects coming from non-recompiled extension modules) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type, |
| 200 | it must first be set on all super types. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 202 | This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a |
| 203 | type (so it must first clear it on all subclasses). The |
| 204 | tp_version_tag value is meaningless unless this flag is set. |
| 205 | We don't assign new version tags eagerly, but only as |
| 206 | needed. |
| 207 | */ |
| 208 | PyObject *raw, *ref; |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 209 | Py_ssize_t i; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 211 | if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
| 212 | return; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | raw = type->tp_subclasses; |
| 215 | if (raw != NULL) { |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 216 | assert(PyDict_CheckExact(raw)); |
| 217 | i = 0; |
| 218 | while (PyDict_Next(raw, &i, NULL, &ref)) { |
| 219 | assert(PyWeakref_CheckRef(ref)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 220 | ref = PyWeakref_GET_OBJECT(ref); |
| 221 | if (ref != Py_None) { |
| 222 | PyType_Modified((PyTypeObject *)ref); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static void |
| 230 | type_mro_modified(PyTypeObject *type, PyObject *bases) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 231 | /* |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 232 | Check that all base classes or elements of the MRO of type are |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 233 | able to be cached. This function is called after the base |
| 234 | classes or mro of the type are altered. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 237 | has a custom MRO that includes a type which is not officially |
| 238 | super type. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 240 | Called from mro_internal, which will subsequently be called on |
| 241 | each subclass when their mro is recursively updated. |
| 242 | */ |
| 243 | Py_ssize_t i, n; |
| 244 | int clear = 0; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 245 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) |
| 247 | return; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | n = PyTuple_GET_SIZE(bases); |
| 250 | for (i = 0; i < n; i++) { |
| 251 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 252 | PyTypeObject *cls; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 253 | |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 254 | assert(PyType_Check(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 255 | cls = (PyTypeObject *)b; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 256 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 257 | if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) || |
| 258 | !PyType_IsSubtype(type, cls)) { |
| 259 | clear = 1; |
| 260 | break; |
| 261 | } |
| 262 | } |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | if (clear) |
| 265 | type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG| |
| 266 | Py_TPFLAGS_VALID_VERSION_TAG); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static int |
| 270 | assign_version_tag(PyTypeObject *type) |
| 271 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 272 | /* Ensure that the tp_version_tag is valid and set |
| 273 | Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this |
| 274 | must first be done on all super classes. Return 0 if this |
| 275 | cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG. |
| 276 | */ |
| 277 | Py_ssize_t i, n; |
| 278 | PyObject *bases; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 280 | if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
| 281 | return 1; |
| 282 | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) |
| 283 | return 0; |
| 284 | if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) |
| 285 | return 0; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 287 | type->tp_version_tag = next_version_tag++; |
| 288 | /* for stress-testing: next_version_tag &= 0xFF; */ |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 290 | if (type->tp_version_tag == 0) { |
| 291 | /* wrap-around or just starting Python - clear the whole |
| 292 | cache by filling names with references to Py_None. |
| 293 | Values are also set to NULL for added protection, as they |
| 294 | are borrowed reference */ |
| 295 | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { |
| 296 | method_cache[i].value = NULL; |
| 297 | Py_XDECREF(method_cache[i].name); |
| 298 | method_cache[i].name = Py_None; |
| 299 | Py_INCREF(Py_None); |
| 300 | } |
| 301 | /* mark all version tags as invalid */ |
| 302 | PyType_Modified(&PyBaseObject_Type); |
| 303 | return 1; |
| 304 | } |
| 305 | bases = type->tp_bases; |
| 306 | n = PyTuple_GET_SIZE(bases); |
| 307 | for (i = 0; i < n; i++) { |
| 308 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 309 | assert(PyType_Check(b)); |
| 310 | if (!assign_version_tag((PyTypeObject *)b)) |
| 311 | return 0; |
| 312 | } |
| 313 | type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG; |
| 314 | return 1; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 318 | static PyMemberDef type_members[] = { |
Benjamin Peterson | 0e10206 | 2010-08-25 23:13:17 +0000 | [diff] [blame] | 319 | {"__basicsize__", T_PYSSIZET, offsetof(PyTypeObject,tp_basicsize),READONLY}, |
| 320 | {"__itemsize__", T_PYSSIZET, offsetof(PyTypeObject, tp_itemsize), READONLY}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY}, |
| 322 | {"__weakrefoffset__", T_LONG, |
| 323 | offsetof(PyTypeObject, tp_weaklistoffset), READONLY}, |
| 324 | {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY}, |
| 325 | {"__dictoffset__", T_LONG, |
| 326 | offsetof(PyTypeObject, tp_dictoffset), READONLY}, |
| 327 | {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY}, |
| 328 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 329 | }; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 330 | |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 331 | static int |
| 332 | check_set_special_type_attr(PyTypeObject *type, PyObject *value, const char *name) |
| 333 | { |
| 334 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 335 | PyErr_Format(PyExc_TypeError, |
| 336 | "can't set %s.%s", type->tp_name, name); |
| 337 | return 0; |
| 338 | } |
| 339 | if (!value) { |
| 340 | PyErr_Format(PyExc_TypeError, |
| 341 | "can't delete %s.%s", type->tp_name, name); |
| 342 | return 0; |
| 343 | } |
| 344 | return 1; |
| 345 | } |
| 346 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 347 | static PyObject * |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 348 | type_name(PyTypeObject *type, void *context) |
| 349 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | const char *s; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 351 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 352 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 353 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 355 | Py_INCREF(et->ht_name); |
| 356 | return et->ht_name; |
| 357 | } |
| 358 | else { |
| 359 | s = strrchr(type->tp_name, '.'); |
| 360 | if (s == NULL) |
| 361 | s = type->tp_name; |
| 362 | else |
| 363 | s++; |
| 364 | return PyUnicode_FromString(s); |
| 365 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 368 | static PyObject * |
| 369 | type_qualname(PyTypeObject *type, void *context) |
| 370 | { |
| 371 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 372 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
| 373 | Py_INCREF(et->ht_qualname); |
| 374 | return et->ht_qualname; |
| 375 | } |
| 376 | else { |
| 377 | return type_name(type, context); |
| 378 | } |
| 379 | } |
| 380 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 381 | static int |
| 382 | type_set_name(PyTypeObject *type, PyObject *value, void *context) |
| 383 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 384 | PyHeapTypeObject* et; |
| 385 | char *tp_name; |
| 386 | PyObject *tmp; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 387 | |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 388 | if (!check_set_special_type_attr(type, value, "__name__")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 390 | if (!PyUnicode_Check(value)) { |
| 391 | PyErr_Format(PyExc_TypeError, |
| 392 | "can only assign string to %s.__name__, not '%s'", |
| 393 | type->tp_name, Py_TYPE(value)->tp_name); |
| 394 | return -1; |
| 395 | } |
Guido van Rossum | e845c0f | 2007-11-02 23:07:07 +0000 | [diff] [blame] | 396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 397 | /* Check absence of null characters */ |
| 398 | tmp = PyUnicode_FromStringAndSize("\0", 1); |
| 399 | if (tmp == NULL) |
| 400 | return -1; |
| 401 | if (PyUnicode_Contains(value, tmp) != 0) { |
| 402 | Py_DECREF(tmp); |
| 403 | PyErr_Format(PyExc_ValueError, |
| 404 | "__name__ must not contain null bytes"); |
| 405 | return -1; |
| 406 | } |
| 407 | Py_DECREF(tmp); |
Guido van Rossum | e845c0f | 2007-11-02 23:07:07 +0000 | [diff] [blame] | 408 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 409 | tp_name = _PyUnicode_AsString(value); |
| 410 | if (tp_name == NULL) |
| 411 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 | et = (PyHeapTypeObject*)type; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 414 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 415 | Py_INCREF(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 416 | |
Mark Dickinson | 64aafeb | 2013-04-13 15:26:58 +0100 | [diff] [blame] | 417 | /* Wait until et is a sane state before Py_DECREF'ing the old et->ht_name |
| 418 | value. (Bug #16447.) */ |
| 419 | tmp = et->ht_name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | et->ht_name = value; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 421 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | type->tp_name = tp_name; |
Mark Dickinson | 64aafeb | 2013-04-13 15:26:58 +0100 | [diff] [blame] | 423 | Py_DECREF(tmp); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 424 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | return 0; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 428 | static int |
| 429 | type_set_qualname(PyTypeObject *type, PyObject *value, void *context) |
| 430 | { |
| 431 | PyHeapTypeObject* et; |
| 432 | |
Benjamin Peterson | 2c05a2e | 2012-10-31 00:01:15 -0400 | [diff] [blame] | 433 | if (!check_set_special_type_attr(type, value, "__qualname__")) |
| 434 | return -1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 435 | if (!PyUnicode_Check(value)) { |
| 436 | PyErr_Format(PyExc_TypeError, |
| 437 | "can only assign string to %s.__qualname__, not '%s'", |
| 438 | type->tp_name, Py_TYPE(value)->tp_name); |
| 439 | return -1; |
| 440 | } |
| 441 | |
| 442 | et = (PyHeapTypeObject*)type; |
| 443 | Py_INCREF(value); |
| 444 | Py_DECREF(et->ht_qualname); |
| 445 | et->ht_qualname = value; |
| 446 | return 0; |
| 447 | } |
| 448 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 449 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 450 | type_module(PyTypeObject *type, void *context) |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 451 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | char *s; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 454 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
Victor Stinner | ad14ccd | 2013-11-07 00:46:04 +0100 | [diff] [blame] | 455 | PyObject *mod = _PyDict_GetItemId(type->tp_dict, &PyId___module__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 456 | if (!mod) { |
| 457 | PyErr_Format(PyExc_AttributeError, "__module__"); |
| 458 | return 0; |
| 459 | } |
| 460 | Py_XINCREF(mod); |
| 461 | return mod; |
| 462 | } |
| 463 | else { |
Victor Stinner | ad14ccd | 2013-11-07 00:46:04 +0100 | [diff] [blame] | 464 | PyObject *name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 465 | s = strrchr(type->tp_name, '.'); |
| 466 | if (s != NULL) |
| 467 | return PyUnicode_FromStringAndSize( |
| 468 | type->tp_name, (Py_ssize_t)(s - type->tp_name)); |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 469 | name = _PyUnicode_FromId(&PyId_builtins); |
Victor Stinner | ad14ccd | 2013-11-07 00:46:04 +0100 | [diff] [blame] | 470 | Py_XINCREF(name); |
| 471 | return name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 472 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 475 | static int |
| 476 | type_set_module(PyTypeObject *type, PyObject *value, void *context) |
| 477 | { |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 478 | if (!check_set_special_type_attr(type, value, "__module__")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 479 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 481 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 482 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 483 | return _PyDict_SetItemId(type->tp_dict, &PyId___module__, value); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 486 | static PyObject * |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 487 | type_abstractmethods(PyTypeObject *type, void *context) |
| 488 | { |
Benjamin Peterson | aec5fd1 | 2010-10-02 00:03:31 +0000 | [diff] [blame] | 489 | PyObject *mod = NULL; |
Benjamin Peterson | 84060b8 | 2010-10-03 02:13:39 +0000 | [diff] [blame] | 490 | /* type itself has an __abstractmethods__ descriptor (this). Don't return |
| 491 | that. */ |
Benjamin Peterson | aec5fd1 | 2010-10-02 00:03:31 +0000 | [diff] [blame] | 492 | if (type != &PyType_Type) |
Victor Stinner | 3688aa9 | 2013-11-06 18:59:18 +0100 | [diff] [blame] | 493 | mod = _PyDict_GetItemId(type->tp_dict, &PyId___abstractmethods__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | if (!mod) { |
Victor Stinner | 3688aa9 | 2013-11-06 18:59:18 +0100 | [diff] [blame] | 495 | PyObject *message = _PyUnicode_FromId(&PyId___abstractmethods__); |
| 496 | if (message) |
| 497 | PyErr_SetObject(PyExc_AttributeError, message); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 498 | return NULL; |
| 499 | } |
| 500 | Py_XINCREF(mod); |
| 501 | return mod; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | static int |
| 505 | type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context) |
| 506 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 507 | /* __abstractmethods__ should only be set once on a type, in |
| 508 | abc.ABCMeta.__new__, so this function doesn't do anything |
| 509 | special to update subclasses. |
| 510 | */ |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 511 | int abstract, res; |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 512 | if (value != NULL) { |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 513 | abstract = PyObject_IsTrue(value); |
| 514 | if (abstract < 0) |
| 515 | return -1; |
Victor Stinner | 3688aa9 | 2013-11-06 18:59:18 +0100 | [diff] [blame] | 516 | res = _PyDict_SetItemId(type->tp_dict, &PyId___abstractmethods__, value); |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 517 | } |
| 518 | else { |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 519 | abstract = 0; |
Victor Stinner | 3688aa9 | 2013-11-06 18:59:18 +0100 | [diff] [blame] | 520 | res = _PyDict_DelItemId(type->tp_dict, &PyId___abstractmethods__); |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 521 | if (res && PyErr_ExceptionMatches(PyExc_KeyError)) { |
Victor Stinner | 3688aa9 | 2013-11-06 18:59:18 +0100 | [diff] [blame] | 522 | PyObject *message = _PyUnicode_FromId(&PyId___abstractmethods__); |
| 523 | if (message) |
| 524 | PyErr_SetObject(PyExc_AttributeError, message); |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 525 | return -1; |
| 526 | } |
| 527 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 528 | if (res == 0) { |
| 529 | PyType_Modified(type); |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 530 | if (abstract) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT; |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 532 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 533 | type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | } |
| 535 | return res; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | static PyObject * |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 539 | type_get_bases(PyTypeObject *type, void *context) |
| 540 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 541 | Py_INCREF(type->tp_bases); |
| 542 | return type->tp_bases; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | static PyTypeObject *best_base(PyObject *); |
| 546 | static int mro_internal(PyTypeObject *); |
| 547 | static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *); |
| 548 | static int add_subclass(PyTypeObject*, PyTypeObject*); |
| 549 | static void remove_subclass(PyTypeObject *, PyTypeObject *); |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 550 | static void remove_all_subclasses(PyTypeObject *type, PyObject *bases); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 551 | static void update_all_slots(PyTypeObject *); |
| 552 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 553 | typedef int (*update_callback)(PyTypeObject *, void *); |
| 554 | static int update_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | update_callback callback, void *data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 556 | static int recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 | update_callback callback, void *data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 558 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 559 | static int |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 560 | mro_subclasses(PyTypeObject *type, PyObject* temp) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 561 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | PyTypeObject *subclass; |
| 563 | PyObject *ref, *subclasses, *old_mro; |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 564 | Py_ssize_t i; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 566 | subclasses = type->tp_subclasses; |
| 567 | if (subclasses == NULL) |
| 568 | return 0; |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 569 | assert(PyDict_CheckExact(subclasses)); |
| 570 | i = 0; |
| 571 | |
| 572 | while (PyDict_Next(subclasses, &i, NULL, &ref)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 573 | assert(PyWeakref_CheckRef(ref)); |
| 574 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 575 | assert(subclass != NULL); |
| 576 | if ((PyObject *)subclass == Py_None) |
| 577 | continue; |
| 578 | assert(PyType_Check(subclass)); |
| 579 | old_mro = subclass->tp_mro; |
| 580 | if (mro_internal(subclass) < 0) { |
| 581 | subclass->tp_mro = old_mro; |
| 582 | return -1; |
| 583 | } |
| 584 | else { |
| 585 | PyObject* tuple; |
| 586 | tuple = PyTuple_Pack(2, subclass, old_mro); |
| 587 | Py_DECREF(old_mro); |
| 588 | if (!tuple) |
| 589 | return -1; |
| 590 | if (PyList_Append(temp, tuple) < 0) |
| 591 | return -1; |
| 592 | Py_DECREF(tuple); |
| 593 | } |
| 594 | if (mro_subclasses(subclass, temp) < 0) |
| 595 | return -1; |
| 596 | } |
| 597 | return 0; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | static int |
| 601 | type_set_bases(PyTypeObject *type, PyObject *value, void *context) |
| 602 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 603 | Py_ssize_t i; |
| 604 | int r = 0; |
| 605 | PyObject *ob, *temp; |
| 606 | PyTypeObject *new_base, *old_base; |
| 607 | PyObject *old_bases, *old_mro; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 608 | |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 609 | if (!check_set_special_type_attr(type, value, "__bases__")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 610 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 611 | if (!PyTuple_Check(value)) { |
| 612 | PyErr_Format(PyExc_TypeError, |
| 613 | "can only assign tuple to %s.__bases__, not %s", |
| 614 | type->tp_name, Py_TYPE(value)->tp_name); |
| 615 | return -1; |
| 616 | } |
| 617 | if (PyTuple_GET_SIZE(value) == 0) { |
| 618 | PyErr_Format(PyExc_TypeError, |
| 619 | "can only assign non-empty tuple to %s.__bases__, not ()", |
| 620 | type->tp_name); |
| 621 | return -1; |
| 622 | } |
| 623 | for (i = 0; i < PyTuple_GET_SIZE(value); i++) { |
| 624 | ob = PyTuple_GET_ITEM(value, i); |
| 625 | if (!PyType_Check(ob)) { |
Benjamin Peterson | b6af60c | 2012-04-01 18:49:54 -0400 | [diff] [blame] | 626 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 9ee601e | 2012-04-01 18:51:37 -0400 | [diff] [blame] | 627 | "%s.__bases__ must be tuple of classes, not '%s'", |
Benjamin Peterson | b6af60c | 2012-04-01 18:49:54 -0400 | [diff] [blame] | 628 | type->tp_name, Py_TYPE(ob)->tp_name); |
| 629 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 630 | } |
Benjamin Peterson | 3471bb6 | 2012-04-01 18:48:40 -0400 | [diff] [blame] | 631 | if (PyType_IsSubtype((PyTypeObject*)ob, type)) { |
| 632 | PyErr_SetString(PyExc_TypeError, |
| 633 | "a __bases__ item causes an inheritance cycle"); |
| 634 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 635 | } |
| 636 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | new_base = best_base(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 639 | |
Benjamin Peterson | ab3c1c1 | 2012-04-01 18:48:02 -0400 | [diff] [blame] | 640 | if (!new_base) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 641 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 642 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 643 | if (!compatible_for_assignment(type->tp_base, new_base, "__bases__")) |
| 644 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 645 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | Py_INCREF(new_base); |
| 647 | Py_INCREF(value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 649 | old_bases = type->tp_bases; |
| 650 | old_base = type->tp_base; |
| 651 | old_mro = type->tp_mro; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 653 | type->tp_bases = value; |
| 654 | type->tp_base = new_base; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 656 | if (mro_internal(type) < 0) { |
| 657 | goto bail; |
| 658 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 659 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 660 | temp = PyList_New(0); |
| 661 | if (!temp) |
| 662 | goto bail; |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 664 | r = mro_subclasses(type, temp); |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 665 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 666 | if (r < 0) { |
| 667 | for (i = 0; i < PyList_Size(temp); i++) { |
| 668 | PyTypeObject* cls; |
| 669 | PyObject* mro; |
| 670 | PyArg_UnpackTuple(PyList_GET_ITEM(temp, i), |
| 671 | "", 2, 2, &cls, &mro); |
| 672 | Py_INCREF(mro); |
| 673 | ob = cls->tp_mro; |
| 674 | cls->tp_mro = mro; |
| 675 | Py_DECREF(ob); |
| 676 | } |
| 677 | Py_DECREF(temp); |
| 678 | goto bail; |
| 679 | } |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 681 | Py_DECREF(temp); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 682 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | /* any base that was in __bases__ but now isn't, we |
| 684 | need to remove |type| from its tp_subclasses. |
| 685 | conversely, any class now in __bases__ that wasn't |
| 686 | needs to have |type| added to its subclasses. */ |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 687 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 688 | /* for now, sod that: just remove from all old_bases, |
| 689 | add to all new_bases */ |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 690 | |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 691 | remove_all_subclasses(type, old_bases); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 692 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 693 | for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) { |
| 694 | ob = PyTuple_GET_ITEM(value, i); |
| 695 | if (PyType_Check(ob)) { |
| 696 | if (add_subclass((PyTypeObject*)ob, type) < 0) |
| 697 | r = -1; |
| 698 | } |
| 699 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 700 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 701 | update_all_slots(type); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 702 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 703 | Py_DECREF(old_bases); |
| 704 | Py_DECREF(old_base); |
| 705 | Py_DECREF(old_mro); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 707 | return r; |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 708 | |
| 709 | bail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 710 | Py_DECREF(type->tp_bases); |
| 711 | Py_DECREF(type->tp_base); |
| 712 | if (type->tp_mro != old_mro) { |
| 713 | Py_DECREF(type->tp_mro); |
| 714 | } |
Michael W. Hudson | e723e45 | 2003-08-07 14:58:10 +0000 | [diff] [blame] | 715 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 716 | type->tp_bases = old_bases; |
| 717 | type->tp_base = old_base; |
| 718 | type->tp_mro = old_mro; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 720 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 724 | type_dict(PyTypeObject *type, void *context) |
| 725 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 726 | if (type->tp_dict == NULL) { |
| 727 | Py_INCREF(Py_None); |
| 728 | return Py_None; |
| 729 | } |
| 730 | return PyDictProxy_New(type->tp_dict); |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 733 | static PyObject * |
| 734 | type_get_doc(PyTypeObject *type, void *context) |
| 735 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 736 | PyObject *result; |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 737 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 738 | return _PyType_GetDocFromInternalDoc(type->tp_name, type->tp_doc); |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 739 | } |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 740 | result = _PyDict_GetItemId(type->tp_dict, &PyId___doc__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 741 | if (result == NULL) { |
| 742 | result = Py_None; |
| 743 | Py_INCREF(result); |
| 744 | } |
| 745 | else if (Py_TYPE(result)->tp_descr_get) { |
| 746 | result = Py_TYPE(result)->tp_descr_get(result, NULL, |
| 747 | (PyObject *)type); |
| 748 | } |
| 749 | else { |
| 750 | Py_INCREF(result); |
| 751 | } |
| 752 | return result; |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 755 | static PyObject * |
| 756 | type_get_text_signature(PyTypeObject *type, void *context) |
| 757 | { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 758 | return _PyType_GetTextSignatureFromInternalDoc(type->tp_name, type->tp_doc); |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 759 | } |
| 760 | |
Benjamin Peterson | 01fc6cd | 2011-08-17 12:03:47 -0500 | [diff] [blame] | 761 | static int |
| 762 | type_set_doc(PyTypeObject *type, PyObject *value, void *context) |
| 763 | { |
| 764 | if (!check_set_special_type_attr(type, value, "__doc__")) |
| 765 | return -1; |
| 766 | PyType_Modified(type); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 767 | return _PyDict_SetItemId(type->tp_dict, &PyId___doc__, value); |
Benjamin Peterson | 01fc6cd | 2011-08-17 12:03:47 -0500 | [diff] [blame] | 768 | } |
| 769 | |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 770 | static PyObject * |
| 771 | type___instancecheck__(PyObject *type, PyObject *inst) |
| 772 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 773 | switch (_PyObject_RealIsInstance(inst, type)) { |
| 774 | case -1: |
| 775 | return NULL; |
| 776 | case 0: |
| 777 | Py_RETURN_FALSE; |
| 778 | default: |
| 779 | Py_RETURN_TRUE; |
| 780 | } |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | |
| 784 | static PyObject * |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 785 | type___subclasscheck__(PyObject *type, PyObject *inst) |
| 786 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 787 | switch (_PyObject_RealIsSubclass(inst, type)) { |
| 788 | case -1: |
| 789 | return NULL; |
| 790 | case 0: |
| 791 | Py_RETURN_FALSE; |
| 792 | default: |
| 793 | Py_RETURN_TRUE; |
| 794 | } |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 797 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 798 | static PyGetSetDef type_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 799 | {"__name__", (getter)type_name, (setter)type_set_name, NULL}, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 800 | {"__qualname__", (getter)type_qualname, (setter)type_set_qualname, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 801 | {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL}, |
| 802 | {"__module__", (getter)type_module, (setter)type_set_module, NULL}, |
| 803 | {"__abstractmethods__", (getter)type_abstractmethods, |
| 804 | (setter)type_set_abstractmethods, NULL}, |
| 805 | {"__dict__", (getter)type_dict, NULL, NULL}, |
Benjamin Peterson | 01fc6cd | 2011-08-17 12:03:47 -0500 | [diff] [blame] | 806 | {"__doc__", (getter)type_get_doc, (setter)type_set_doc, NULL}, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 807 | {"__text_signature__", (getter)type_get_text_signature, NULL, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 808 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 809 | }; |
| 810 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 811 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 812 | type_repr(PyTypeObject *type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 813 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 814 | PyObject *mod, *name, *rtn; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 815 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 816 | mod = type_module(type, NULL); |
| 817 | if (mod == NULL) |
| 818 | PyErr_Clear(); |
| 819 | else if (!PyUnicode_Check(mod)) { |
| 820 | Py_DECREF(mod); |
| 821 | mod = NULL; |
| 822 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 823 | name = type_qualname(type, NULL); |
Christian Heimes | a0e7e41 | 2012-09-10 03:00:14 +0200 | [diff] [blame] | 824 | if (name == NULL) { |
| 825 | Py_XDECREF(mod); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 826 | return NULL; |
Christian Heimes | a0e7e41 | 2012-09-10 03:00:14 +0200 | [diff] [blame] | 827 | } |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 828 | |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 829 | if (mod != NULL && _PyUnicode_CompareWithId(mod, &PyId_builtins)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name); |
| 831 | else |
| 832 | rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 833 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 834 | Py_XDECREF(mod); |
| 835 | Py_DECREF(name); |
| 836 | return rtn; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 839 | static PyObject * |
| 840 | type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 841 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | PyObject *obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 843 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 844 | if (type->tp_new == NULL) { |
| 845 | PyErr_Format(PyExc_TypeError, |
| 846 | "cannot create '%.100s' instances", |
| 847 | type->tp_name); |
| 848 | return NULL; |
| 849 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 850 | |
Victor Stinner | 33824f6 | 2013-08-26 14:05:19 +0200 | [diff] [blame] | 851 | #ifdef Py_DEBUG |
| 852 | /* type_call() must not be called with an exception set, |
| 853 | because it may clear it (directly or indirectly) and so the |
| 854 | caller looses its exception */ |
| 855 | assert(!PyErr_Occurred()); |
| 856 | #endif |
| 857 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 858 | obj = type->tp_new(type, args, kwds); |
| 859 | if (obj != NULL) { |
| 860 | /* Ugly exception: when the call was type(something), |
| 861 | don't call tp_init on the result. */ |
| 862 | if (type == &PyType_Type && |
| 863 | PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && |
| 864 | (kwds == NULL || |
| 865 | (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) |
| 866 | return obj; |
| 867 | /* If the returned object is not an instance of type, |
| 868 | it won't be initialized. */ |
| 869 | if (!PyType_IsSubtype(Py_TYPE(obj), type)) |
| 870 | return obj; |
| 871 | type = Py_TYPE(obj); |
Victor Stinner | 3997cfd | 2013-07-16 22:51:21 +0200 | [diff] [blame] | 872 | if (type->tp_init != NULL) { |
| 873 | int res = type->tp_init(obj, args, kwds); |
| 874 | if (res < 0) { |
| 875 | Py_DECREF(obj); |
| 876 | obj = NULL; |
| 877 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 884 | PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 885 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | PyObject *obj; |
| 887 | const size_t size = _PyObject_VAR_SIZE(type, nitems+1); |
| 888 | /* note that we need to add one, for the sentinel */ |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 889 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | if (PyType_IS_GC(type)) |
| 891 | obj = _PyObject_GC_Malloc(size); |
| 892 | else |
| 893 | obj = (PyObject *)PyObject_MALLOC(size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 894 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 895 | if (obj == NULL) |
| 896 | return PyErr_NoMemory(); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 897 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 898 | memset(obj, '\0', size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 899 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 900 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 901 | Py_INCREF(type); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 903 | if (type->tp_itemsize == 0) |
Christian Heimes | d3afe78 | 2013-12-04 09:27:47 +0100 | [diff] [blame] | 904 | (void)PyObject_INIT(obj, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 905 | else |
| 906 | (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 907 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 | if (PyType_IS_GC(type)) |
| 909 | _PyObject_GC_TRACK(obj); |
| 910 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | PyObject * |
| 914 | PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 915 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | return type->tp_alloc(type, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 919 | /* Helpers for subtyping */ |
| 920 | |
| 921 | static int |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 922 | traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg) |
| 923 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | Py_ssize_t i, n; |
| 925 | PyMemberDef *mp; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 926 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 927 | n = Py_SIZE(type); |
| 928 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
| 929 | for (i = 0; i < n; i++, mp++) { |
| 930 | if (mp->type == T_OBJECT_EX) { |
| 931 | char *addr = (char *)self + mp->offset; |
| 932 | PyObject *obj = *(PyObject **)addr; |
| 933 | if (obj != NULL) { |
| 934 | int err = visit(obj, arg); |
| 935 | if (err) |
| 936 | return err; |
| 937 | } |
| 938 | } |
| 939 | } |
| 940 | return 0; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | static int |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 944 | subtype_traverse(PyObject *self, visitproc visit, void *arg) |
| 945 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 946 | PyTypeObject *type, *base; |
| 947 | traverseproc basetraverse; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 949 | /* Find the nearest base with a different tp_traverse, |
| 950 | and traverse slots while we're at it */ |
| 951 | type = Py_TYPE(self); |
| 952 | base = type; |
| 953 | while ((basetraverse = base->tp_traverse) == subtype_traverse) { |
| 954 | if (Py_SIZE(base)) { |
| 955 | int err = traverse_slots(base, self, visit, arg); |
| 956 | if (err) |
| 957 | return err; |
| 958 | } |
| 959 | base = base->tp_base; |
| 960 | assert(base); |
| 961 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 964 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 965 | if (dictptr && *dictptr) |
| 966 | Py_VISIT(*dictptr); |
| 967 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 968 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 969 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 970 | /* For a heaptype, the instances count as references |
| 971 | to the type. Traverse the type so the collector |
| 972 | can find cycles involving this link. */ |
| 973 | Py_VISIT(type); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 975 | if (basetraverse) |
| 976 | return basetraverse(self, visit, arg); |
| 977 | return 0; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | static void |
| 981 | clear_slots(PyTypeObject *type, PyObject *self) |
| 982 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 983 | Py_ssize_t i, n; |
| 984 | PyMemberDef *mp; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 985 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 986 | n = Py_SIZE(type); |
| 987 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
| 988 | for (i = 0; i < n; i++, mp++) { |
| 989 | if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) { |
| 990 | char *addr = (char *)self + mp->offset; |
| 991 | PyObject *obj = *(PyObject **)addr; |
| 992 | if (obj != NULL) { |
| 993 | *(PyObject **)addr = NULL; |
| 994 | Py_DECREF(obj); |
| 995 | } |
| 996 | } |
| 997 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | static int |
| 1001 | subtype_clear(PyObject *self) |
| 1002 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1003 | PyTypeObject *type, *base; |
| 1004 | inquiry baseclear; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1005 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1006 | /* Find the nearest base with a different tp_clear |
| 1007 | and clear slots while we're at it */ |
| 1008 | type = Py_TYPE(self); |
| 1009 | base = type; |
| 1010 | while ((baseclear = base->tp_clear) == subtype_clear) { |
| 1011 | if (Py_SIZE(base)) |
| 1012 | clear_slots(base, self); |
| 1013 | base = base->tp_base; |
| 1014 | assert(base); |
| 1015 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1016 | |
Benjamin Peterson | 52c4243 | 2012-03-07 18:41:11 -0600 | [diff] [blame] | 1017 | /* Clear the instance dict (if any), to break cycles involving only |
| 1018 | __dict__ slots (as in the case 'self.__dict__ is self'). */ |
| 1019 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 1020 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 1021 | if (dictptr && *dictptr) |
| 1022 | Py_CLEAR(*dictptr); |
| 1023 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1024 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1025 | if (baseclear) |
| 1026 | return baseclear(self); |
| 1027 | return 0; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 1028 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1029 | |
| 1030 | static void |
| 1031 | subtype_dealloc(PyObject *self) |
| 1032 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | PyTypeObject *type, *base; |
| 1034 | destructor basedealloc; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 1035 | PyThreadState *tstate = PyThreadState_GET(); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1036 | int has_finalizer; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1037 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1038 | /* Extract the type; we expect it to be a heap type */ |
| 1039 | type = Py_TYPE(self); |
| 1040 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | /* Test whether the type has GC exactly once */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1043 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1044 | if (!PyType_IS_GC(type)) { |
| 1045 | /* It's really rare to find a dynamic type that doesn't have |
| 1046 | GC; it can only happen when deriving from 'object' and not |
| 1047 | adding any slots or instance variables. This allows |
| 1048 | certain simplifications: there's no need to call |
| 1049 | clear_slots(), or DECREF the dict, or clear weakrefs. */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1050 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1051 | /* Maybe call finalizer; exit early if resurrected */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1052 | if (type->tp_finalize) { |
| 1053 | if (PyObject_CallFinalizerFromDealloc(self) < 0) |
| 1054 | return; |
| 1055 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1056 | if (type->tp_del) { |
| 1057 | type->tp_del(self); |
| 1058 | if (self->ob_refcnt > 0) |
| 1059 | return; |
| 1060 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1061 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1062 | /* Find the nearest base with a different tp_dealloc */ |
| 1063 | base = type; |
| 1064 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 1065 | assert(Py_SIZE(base) == 0); |
| 1066 | base = base->tp_base; |
| 1067 | assert(base); |
| 1068 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1069 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1070 | /* Extract the type again; tp_del may have changed it */ |
| 1071 | type = Py_TYPE(self); |
Benjamin Peterson | 193152c | 2009-04-25 01:08:45 +0000 | [diff] [blame] | 1072 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | /* Call the base tp_dealloc() */ |
| 1074 | assert(basedealloc); |
| 1075 | basedealloc(self); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1076 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | /* Can't reference self beyond this point */ |
| 1078 | Py_DECREF(type); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1079 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1080 | /* Done */ |
| 1081 | return; |
| 1082 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1083 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1084 | /* We get here only if the type has GC */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1085 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1086 | /* UnTrack and re-Track around the trashcan macro, alas */ |
| 1087 | /* See explanation at end of function for full disclosure */ |
| 1088 | PyObject_GC_UnTrack(self); |
| 1089 | ++_PyTrash_delete_nesting; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 1090 | ++ tstate->trash_delete_nesting; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | Py_TRASHCAN_SAFE_BEGIN(self); |
| 1092 | --_PyTrash_delete_nesting; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 1093 | -- tstate->trash_delete_nesting; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1094 | /* DO NOT restore GC tracking at this point. weakref callbacks |
| 1095 | * (if any, and whether directly here or indirectly in something we |
| 1096 | * call) may trigger GC, and if self is tracked at that point, it |
| 1097 | * will look like trash to GC and GC will try to delete self again. |
| 1098 | */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1099 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1100 | /* Find the nearest base with a different tp_dealloc */ |
| 1101 | base = type; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 1102 | while ((/*basedealloc =*/ base->tp_dealloc) == subtype_dealloc) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1103 | base = base->tp_base; |
| 1104 | assert(base); |
| 1105 | } |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 1106 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1107 | has_finalizer = type->tp_finalize || type->tp_del; |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 1108 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1109 | /* Maybe call finalizer; exit early if resurrected */ |
| 1110 | if (has_finalizer) |
| 1111 | _PyObject_GC_TRACK(self); |
| 1112 | |
| 1113 | if (type->tp_finalize) { |
| 1114 | if (PyObject_CallFinalizerFromDealloc(self) < 0) { |
| 1115 | /* Resurrected */ |
| 1116 | goto endlabel; |
| 1117 | } |
| 1118 | } |
| 1119 | /* If we added a weaklist, we clear it. Do this *before* calling |
| 1120 | tp_del, clearing slots, or clearing the instance dict. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1121 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) |
| 1122 | PyObject_ClearWeakRefs(self); |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 1123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1124 | if (type->tp_del) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1125 | type->tp_del(self); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1126 | if (self->ob_refcnt > 0) { |
| 1127 | /* Resurrected */ |
| 1128 | goto endlabel; |
| 1129 | } |
| 1130 | } |
| 1131 | if (has_finalizer) { |
| 1132 | _PyObject_GC_UNTRACK(self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1133 | /* New weakrefs could be created during the finalizer call. |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1134 | If this occurs, clear them out without calling their |
| 1135 | finalizers since they might rely on part of the object |
| 1136 | being finalized that has already been destroyed. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1137 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) { |
| 1138 | /* Modeled after GET_WEAKREFS_LISTPTR() */ |
| 1139 | PyWeakReference **list = (PyWeakReference **) \ |
| 1140 | PyObject_GET_WEAKREFS_LISTPTR(self); |
| 1141 | while (*list) |
| 1142 | _PyWeakref_ClearRef(*list); |
| 1143 | } |
| 1144 | } |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 1145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1146 | /* Clear slots up to the nearest base with a different tp_dealloc */ |
| 1147 | base = type; |
| 1148 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 1149 | if (Py_SIZE(base)) |
| 1150 | clear_slots(base, self); |
| 1151 | base = base->tp_base; |
| 1152 | assert(base); |
| 1153 | } |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 1154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | /* If we added a dict, DECREF it */ |
| 1156 | if (type->tp_dictoffset && !base->tp_dictoffset) { |
| 1157 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 1158 | if (dictptr != NULL) { |
| 1159 | PyObject *dict = *dictptr; |
| 1160 | if (dict != NULL) { |
| 1161 | Py_DECREF(dict); |
| 1162 | *dictptr = NULL; |
| 1163 | } |
| 1164 | } |
| 1165 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1167 | /* Extract the type again; tp_del may have changed it */ |
| 1168 | type = Py_TYPE(self); |
Benjamin Peterson | 193152c | 2009-04-25 01:08:45 +0000 | [diff] [blame] | 1169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1170 | /* Call the base tp_dealloc(); first retrack self if |
| 1171 | * basedealloc knows about gc. |
| 1172 | */ |
| 1173 | if (PyType_IS_GC(base)) |
| 1174 | _PyObject_GC_TRACK(self); |
| 1175 | assert(basedealloc); |
| 1176 | basedealloc(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1178 | /* Can't reference self beyond this point */ |
| 1179 | Py_DECREF(type); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1180 | |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 1181 | endlabel: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1182 | ++_PyTrash_delete_nesting; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 1183 | ++ tstate->trash_delete_nesting; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1184 | Py_TRASHCAN_SAFE_END(self); |
| 1185 | --_PyTrash_delete_nesting; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 1186 | -- tstate->trash_delete_nesting; |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1188 | /* Explanation of the weirdness around the trashcan macros: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1189 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1190 | Q. What do the trashcan macros do? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1192 | A. Read the comment titled "Trashcan mechanism" in object.h. |
| 1193 | For one, this explains why there must be a call to GC-untrack |
| 1194 | before the trashcan begin macro. Without understanding the |
| 1195 | trashcan code, the answers to the following questions don't make |
| 1196 | sense. |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1197 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1198 | Q. Why do we GC-untrack before the trashcan and then immediately |
| 1199 | GC-track again afterward? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1201 | A. In the case that the base class is GC-aware, the base class |
| 1202 | probably GC-untracks the object. If it does that using the |
| 1203 | UNTRACK macro, this will crash when the object is already |
| 1204 | untracked. Because we don't know what the base class does, the |
| 1205 | only safe thing is to make sure the object is tracked when we |
| 1206 | call the base class dealloc. But... The trashcan begin macro |
| 1207 | requires that the object is *untracked* before it is called. So |
| 1208 | the dance becomes: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1210 | GC untrack |
| 1211 | trashcan begin |
| 1212 | GC track |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1214 | Q. Why did the last question say "immediately GC-track again"? |
| 1215 | It's nowhere near immediately. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 1216 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1217 | A. Because the code *used* to re-track immediately. Bad Idea. |
| 1218 | self has a refcount of 0, and if gc ever gets its hands on it |
| 1219 | (which can happen if any weakref callback gets invoked), it |
| 1220 | looks like trash to gc too, and gc also tries to delete self |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 1221 | then. But we're already deleting self. Double deallocation is |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1222 | a subtle disaster. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 1223 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1224 | Q. Why the bizarre (net-zero) manipulation of |
| 1225 | _PyTrash_delete_nesting around the trashcan macros? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1227 | A. Some base classes (e.g. list) also use the trashcan mechanism. |
| 1228 | The following scenario used to be possible: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | - suppose the trashcan level is one below the trashcan limit |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1232 | - subtype_dealloc() is called |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1234 | - the trashcan limit is not yet reached, so the trashcan level |
| 1235 | is incremented and the code between trashcan begin and end is |
| 1236 | executed |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1238 | - this destroys much of the object's contents, including its |
| 1239 | slots and __dict__ |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | - basedealloc() is called; this is really list_dealloc(), or |
| 1242 | some other type which also uses the trashcan macros |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1243 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1244 | - the trashcan limit is now reached, so the object is put on the |
| 1245 | trashcan's to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1247 | - basedealloc() returns |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1249 | - subtype_dealloc() decrefs the object's type |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1251 | - subtype_dealloc() returns |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1253 | - later, the trashcan code starts deleting the objects from its |
| 1254 | to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1255 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1256 | - subtype_dealloc() is called *AGAIN* for the same object |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1258 | - at the very least (if the destroyed slots and __dict__ don't |
| 1259 | cause problems) the object's type gets decref'ed a second |
| 1260 | time, which is *BAD*!!! |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1261 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1262 | The remedy is to make sure that if the code between trashcan |
| 1263 | begin and end in subtype_dealloc() is called, the code between |
| 1264 | trashcan begin and end in basedealloc() will also be called. |
| 1265 | This is done by decrementing the level after passing into the |
| 1266 | trashcan block, and incrementing it just before leaving the |
| 1267 | block. |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1268 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1269 | But now it's possible that a chain of objects consisting solely |
| 1270 | of objects whose deallocator is subtype_dealloc() will defeat |
| 1271 | the trashcan mechanism completely: the decremented level means |
| 1272 | that the effective level never reaches the limit. Therefore, we |
| 1273 | *increment* the level *before* entering the trashcan block, and |
| 1274 | matchingly decrement it after leaving. This means the trashcan |
| 1275 | 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] | 1276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1277 | Q. Are there any live examples of code in need of all this |
| 1278 | complexity? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1280 | A. Yes. See SF bug 668433 for code that crashed (when Python was |
| 1281 | compiled in debug mode) before the trashcan level manipulations |
| 1282 | were added. For more discussion, see SF patches 581742, 575073 |
| 1283 | and bug 574207. |
| 1284 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1287 | static PyTypeObject *solid_base(PyTypeObject *type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1288 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1289 | /* type test with subclassing support */ |
| 1290 | |
| 1291 | int |
| 1292 | PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
| 1293 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1294 | PyObject *mro; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1296 | mro = a->tp_mro; |
| 1297 | if (mro != NULL) { |
| 1298 | /* Deal with multiple inheritance without recursion |
| 1299 | by walking the MRO tuple */ |
| 1300 | Py_ssize_t i, n; |
| 1301 | assert(PyTuple_Check(mro)); |
| 1302 | n = PyTuple_GET_SIZE(mro); |
| 1303 | for (i = 0; i < n; i++) { |
| 1304 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) |
| 1305 | return 1; |
| 1306 | } |
| 1307 | return 0; |
| 1308 | } |
| 1309 | else { |
| 1310 | /* a is not completely initilized yet; follow tp_base */ |
| 1311 | do { |
| 1312 | if (a == b) |
| 1313 | return 1; |
| 1314 | a = a->tp_base; |
| 1315 | } while (a != NULL); |
| 1316 | return b == &PyBaseObject_Type; |
| 1317 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1320 | /* Internal routines to do a method lookup in the type |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1321 | without looking in the instance dictionary |
| 1322 | (so we can't use PyObject_GetAttr) but still binding |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1323 | it to the instance. The arguments are the object, |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1324 | 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] | 1325 | static variable used to cache the interned Python string. |
| 1326 | |
| 1327 | Two variants: |
| 1328 | |
| 1329 | - lookup_maybe() returns NULL without raising an exception |
| 1330 | when the _PyType_Lookup() call fails; |
| 1331 | |
| 1332 | - lookup_method() always raises an exception upon errors. |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1333 | |
| 1334 | - _PyObject_LookupSpecial() exported for the benefit of other places. |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1335 | */ |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1336 | |
| 1337 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1338 | lookup_maybe(PyObject *self, _Py_Identifier *attrid) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1339 | { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1340 | PyObject *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1341 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1342 | res = _PyType_LookupId(Py_TYPE(self), attrid); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1343 | if (res != NULL) { |
| 1344 | descrgetfunc f; |
| 1345 | if ((f = Py_TYPE(res)->tp_descr_get) == NULL) |
| 1346 | Py_INCREF(res); |
| 1347 | else |
| 1348 | res = f(res, self, (PyObject *)(Py_TYPE(self))); |
| 1349 | } |
| 1350 | return res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1353 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1354 | lookup_method(PyObject *self, _Py_Identifier *attrid) |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1355 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1356 | PyObject *res = lookup_maybe(self, attrid); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1357 | if (res == NULL && !PyErr_Occurred()) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1358 | PyErr_SetObject(PyExc_AttributeError, attrid->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1359 | return res; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1362 | PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1363 | _PyObject_LookupSpecial(PyObject *self, _Py_Identifier *attrid) |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1364 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1365 | return lookup_maybe(self, attrid); |
Benjamin Peterson | 224205f | 2009-05-08 03:25:19 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1368 | /* A variation of PyObject_CallMethod that uses lookup_method() |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1369 | instead of PyObject_GetAttrString(). This uses the same convention |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1370 | as lookup_method to cache the interned name string object. */ |
| 1371 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1372 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1373 | call_method(PyObject *o, _Py_Identifier *nameid, char *format, ...) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1374 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1375 | va_list va; |
| 1376 | PyObject *args, *func = 0, *retval; |
| 1377 | va_start(va, format); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1378 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1379 | func = lookup_maybe(o, nameid); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1380 | if (func == NULL) { |
| 1381 | va_end(va); |
| 1382 | if (!PyErr_Occurred()) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1383 | PyErr_SetObject(PyExc_AttributeError, nameid->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1384 | return NULL; |
| 1385 | } |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1387 | if (format && *format) |
| 1388 | args = Py_VaBuildValue(format, va); |
| 1389 | else |
| 1390 | args = PyTuple_New(0); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1391 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1392 | va_end(va); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1393 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1394 | if (args == NULL) |
| 1395 | return NULL; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1397 | assert(PyTuple_Check(args)); |
| 1398 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1400 | Py_DECREF(args); |
| 1401 | Py_DECREF(func); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1403 | return retval; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | /* Clone of call_method() that returns NotImplemented when the lookup fails. */ |
| 1407 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1408 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1409 | call_maybe(PyObject *o, _Py_Identifier *nameid, char *format, ...) |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1410 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1411 | va_list va; |
| 1412 | PyObject *args, *func = 0, *retval; |
| 1413 | va_start(va, format); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1414 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1415 | func = lookup_maybe(o, nameid); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1416 | if (func == NULL) { |
| 1417 | va_end(va); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1418 | if (!PyErr_Occurred()) |
| 1419 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1420 | return NULL; |
| 1421 | } |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1423 | if (format && *format) |
| 1424 | args = Py_VaBuildValue(format, va); |
| 1425 | else |
| 1426 | args = PyTuple_New(0); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1427 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1428 | va_end(va); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1429 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1430 | if (args == NULL) |
| 1431 | return NULL; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1432 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1433 | assert(PyTuple_Check(args)); |
| 1434 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1435 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1436 | Py_DECREF(args); |
| 1437 | Py_DECREF(func); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1439 | return retval; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1442 | /* |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1443 | Method resolution order algorithm C3 described in |
| 1444 | "A Monotonic Superclass Linearization for Dylan", |
| 1445 | by Kim Barrett, Bob Cassel, Paul Haahr, |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1446 | David A. Moon, Keith Playford, and P. Tucker Withington. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1447 | (OOPSLA 1996) |
| 1448 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1449 | Some notes about the rules implied by C3: |
| 1450 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1451 | No duplicate bases. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1452 | It isn't legal to repeat a class in a list of base classes. |
| 1453 | |
| 1454 | The next three properties are the 3 constraints in "C3". |
| 1455 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1456 | Local precendece order. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1457 | If A precedes B in C's MRO, then A will precede B in the MRO of all |
| 1458 | subclasses of C. |
| 1459 | |
| 1460 | Monotonicity. |
| 1461 | The MRO of a class must be an extension without reordering of the |
| 1462 | MRO of each of its superclasses. |
| 1463 | |
| 1464 | Extended Precedence Graph (EPG). |
| 1465 | Linearization is consistent if there is a path in the EPG from |
| 1466 | each class to all its successors in the linearization. See |
| 1467 | the paper for definition of EPG. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1468 | */ |
| 1469 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1470 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1471 | tail_contains(PyObject *list, int whence, PyObject *o) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1472 | Py_ssize_t j, size; |
| 1473 | size = PyList_GET_SIZE(list); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1475 | for (j = whence+1; j < size; j++) { |
| 1476 | if (PyList_GET_ITEM(list, j) == o) |
| 1477 | return 1; |
| 1478 | } |
| 1479 | return 0; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1482 | static PyObject * |
| 1483 | class_name(PyObject *cls) |
| 1484 | { |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1485 | PyObject *name = _PyObject_GetAttrId(cls, &PyId___name__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1486 | if (name == NULL) { |
| 1487 | PyErr_Clear(); |
| 1488 | Py_XDECREF(name); |
| 1489 | name = PyObject_Repr(cls); |
| 1490 | } |
| 1491 | if (name == NULL) |
| 1492 | return NULL; |
| 1493 | if (!PyUnicode_Check(name)) { |
| 1494 | Py_DECREF(name); |
| 1495 | return NULL; |
| 1496 | } |
| 1497 | return name; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | static int |
| 1501 | check_duplicates(PyObject *list) |
| 1502 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1503 | Py_ssize_t i, j, n; |
| 1504 | /* Let's use a quadratic time algorithm, |
| 1505 | assuming that the bases lists is short. |
| 1506 | */ |
| 1507 | n = PyList_GET_SIZE(list); |
| 1508 | for (i = 0; i < n; i++) { |
| 1509 | PyObject *o = PyList_GET_ITEM(list, i); |
| 1510 | for (j = i + 1; j < n; j++) { |
| 1511 | if (PyList_GET_ITEM(list, j) == o) { |
| 1512 | o = class_name(o); |
| 1513 | if (o != NULL) { |
| 1514 | PyErr_Format(PyExc_TypeError, |
| 1515 | "duplicate base class %U", |
| 1516 | o); |
| 1517 | Py_DECREF(o); |
| 1518 | } else { |
| 1519 | PyErr_SetString(PyExc_TypeError, |
| 1520 | "duplicate base class"); |
| 1521 | } |
| 1522 | return -1; |
| 1523 | } |
| 1524 | } |
| 1525 | } |
| 1526 | return 0; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | /* Raise a TypeError for an MRO order disagreement. |
| 1530 | |
| 1531 | It's hard to produce a good error message. In the absence of better |
| 1532 | insight into error reporting, report the classes that were candidates |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1533 | 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] | 1534 | order in which they should be put in the MRO, but it's hard to |
| 1535 | diagnose what constraint can't be satisfied. |
| 1536 | */ |
| 1537 | |
| 1538 | static void |
| 1539 | set_mro_error(PyObject *to_merge, int *remain) |
| 1540 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1541 | Py_ssize_t i, n, off, to_merge_size; |
| 1542 | char buf[1000]; |
| 1543 | PyObject *k, *v; |
| 1544 | PyObject *set = PyDict_New(); |
| 1545 | if (!set) return; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1547 | to_merge_size = PyList_GET_SIZE(to_merge); |
| 1548 | for (i = 0; i < to_merge_size; i++) { |
| 1549 | PyObject *L = PyList_GET_ITEM(to_merge, i); |
| 1550 | if (remain[i] < PyList_GET_SIZE(L)) { |
| 1551 | PyObject *c = PyList_GET_ITEM(L, remain[i]); |
| 1552 | if (PyDict_SetItem(set, c, Py_None) < 0) { |
| 1553 | Py_DECREF(set); |
| 1554 | return; |
| 1555 | } |
| 1556 | } |
| 1557 | } |
| 1558 | n = PyDict_Size(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1559 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1560 | off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \ |
Raymond Hettinger | f394df4 | 2003-04-06 19:13:41 +0000 | [diff] [blame] | 1561 | consistent method resolution\norder (MRO) for bases"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1562 | i = 0; |
| 1563 | while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) { |
| 1564 | PyObject *name = class_name(k); |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1565 | char *name_str; |
| 1566 | if (name != NULL) { |
| 1567 | name_str = _PyUnicode_AsString(name); |
| 1568 | if (name_str == NULL) |
Victor Stinner | ba644a6 | 2010-05-19 01:50:45 +0000 | [diff] [blame] | 1569 | name_str = "?"; |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1570 | } else |
Victor Stinner | ba644a6 | 2010-05-19 01:50:45 +0000 | [diff] [blame] | 1571 | name_str = "?"; |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1572 | off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", name_str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1573 | Py_XDECREF(name); |
| 1574 | if (--n && (size_t)(off+1) < sizeof(buf)) { |
| 1575 | buf[off++] = ','; |
| 1576 | buf[off] = '\0'; |
| 1577 | } |
| 1578 | } |
| 1579 | PyErr_SetString(PyExc_TypeError, buf); |
| 1580 | Py_DECREF(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1583 | static int |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1584 | pmerge(PyObject *acc, PyObject* to_merge) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1585 | Py_ssize_t i, j, to_merge_size, empty_cnt; |
| 1586 | int *remain; |
| 1587 | int ok; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1588 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1589 | to_merge_size = PyList_GET_SIZE(to_merge); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1591 | /* remain stores an index into each sublist of to_merge. |
| 1592 | remain[i] is the index of the next base in to_merge[i] |
| 1593 | that is not included in acc. |
| 1594 | */ |
| 1595 | remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size); |
Victor Stinner | a41f085 | 2013-07-12 00:42:14 +0200 | [diff] [blame] | 1596 | if (remain == NULL) { |
| 1597 | PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1598 | return -1; |
Victor Stinner | a41f085 | 2013-07-12 00:42:14 +0200 | [diff] [blame] | 1599 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1600 | for (i = 0; i < to_merge_size; i++) |
| 1601 | remain[i] = 0; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1602 | |
| 1603 | again: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1604 | empty_cnt = 0; |
| 1605 | for (i = 0; i < to_merge_size; i++) { |
| 1606 | PyObject *candidate; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1608 | PyObject *cur_list = PyList_GET_ITEM(to_merge, i); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1609 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1610 | if (remain[i] >= PyList_GET_SIZE(cur_list)) { |
| 1611 | empty_cnt++; |
| 1612 | continue; |
| 1613 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1614 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1615 | /* Choose next candidate for MRO. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1616 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1617 | The input sequences alone can determine the choice. |
| 1618 | If not, choose the class which appears in the MRO |
| 1619 | of the earliest direct superclass of the new class. |
| 1620 | */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1622 | candidate = PyList_GET_ITEM(cur_list, remain[i]); |
| 1623 | for (j = 0; j < to_merge_size; j++) { |
| 1624 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
| 1625 | if (tail_contains(j_lst, remain[j], candidate)) { |
| 1626 | goto skip; /* continue outer loop */ |
| 1627 | } |
| 1628 | } |
| 1629 | ok = PyList_Append(acc, candidate); |
| 1630 | if (ok < 0) { |
Victor Stinner | a41f085 | 2013-07-12 00:42:14 +0200 | [diff] [blame] | 1631 | PyMem_FREE(remain); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1632 | return -1; |
| 1633 | } |
| 1634 | for (j = 0; j < to_merge_size; j++) { |
| 1635 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
| 1636 | if (remain[j] < PyList_GET_SIZE(j_lst) && |
| 1637 | PyList_GET_ITEM(j_lst, remain[j]) == candidate) { |
| 1638 | remain[j]++; |
| 1639 | } |
| 1640 | } |
| 1641 | goto again; |
| 1642 | skip: ; |
| 1643 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1645 | if (empty_cnt == to_merge_size) { |
| 1646 | PyMem_FREE(remain); |
| 1647 | return 0; |
| 1648 | } |
| 1649 | set_mro_error(to_merge, remain); |
| 1650 | PyMem_FREE(remain); |
| 1651 | return -1; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1654 | static PyObject * |
| 1655 | mro_implementation(PyTypeObject *type) |
| 1656 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1657 | Py_ssize_t i, n; |
| 1658 | int ok; |
| 1659 | PyObject *bases, *result; |
| 1660 | PyObject *to_merge, *bases_aslist; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1661 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1662 | if (type->tp_dict == NULL) { |
| 1663 | if (PyType_Ready(type) < 0) |
| 1664 | return NULL; |
| 1665 | } |
Guido van Rossum | 6351757 | 2002-06-18 16:44:57 +0000 | [diff] [blame] | 1666 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1667 | /* Find a superclass linearization that honors the constraints |
| 1668 | of the explicit lists of bases and the constraints implied by |
| 1669 | each base class. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1670 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1671 | to_merge is a list of lists, where each list is a superclass |
| 1672 | linearization implied by a base class. The last element of |
| 1673 | to_merge is the declared list of bases. |
| 1674 | */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1676 | bases = type->tp_bases; |
| 1677 | n = PyTuple_GET_SIZE(bases); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1679 | to_merge = PyList_New(n+1); |
| 1680 | if (to_merge == NULL) |
| 1681 | return NULL; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1682 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1683 | for (i = 0; i < n; i++) { |
| 1684 | PyObject *base = PyTuple_GET_ITEM(bases, i); |
| 1685 | PyObject *parentMRO; |
| 1686 | parentMRO = PySequence_List(((PyTypeObject*)base)->tp_mro); |
| 1687 | if (parentMRO == NULL) { |
| 1688 | Py_DECREF(to_merge); |
| 1689 | return NULL; |
| 1690 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1691 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1692 | PyList_SET_ITEM(to_merge, i, parentMRO); |
| 1693 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1694 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1695 | bases_aslist = PySequence_List(bases); |
| 1696 | if (bases_aslist == NULL) { |
| 1697 | Py_DECREF(to_merge); |
| 1698 | return NULL; |
| 1699 | } |
| 1700 | /* This is just a basic sanity check. */ |
| 1701 | if (check_duplicates(bases_aslist) < 0) { |
| 1702 | Py_DECREF(to_merge); |
| 1703 | Py_DECREF(bases_aslist); |
| 1704 | return NULL; |
| 1705 | } |
| 1706 | PyList_SET_ITEM(to_merge, n, bases_aslist); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1707 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1708 | result = Py_BuildValue("[O]", (PyObject *)type); |
| 1709 | if (result == NULL) { |
| 1710 | Py_DECREF(to_merge); |
| 1711 | return NULL; |
| 1712 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1713 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1714 | ok = pmerge(result, to_merge); |
| 1715 | Py_DECREF(to_merge); |
| 1716 | if (ok < 0) { |
| 1717 | Py_DECREF(result); |
| 1718 | return NULL; |
| 1719 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1720 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1721 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1725 | mro_external(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1726 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1727 | PyTypeObject *type = (PyTypeObject *)self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1729 | return mro_implementation(type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | static int |
| 1733 | mro_internal(PyTypeObject *type) |
| 1734 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1735 | PyObject *mro, *result, *tuple; |
| 1736 | int checkit = 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1737 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1738 | if (Py_TYPE(type) == &PyType_Type) { |
| 1739 | result = mro_implementation(type); |
| 1740 | } |
| 1741 | else { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1742 | _Py_IDENTIFIER(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1743 | checkit = 1; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1744 | mro = lookup_method((PyObject *)type, &PyId_mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1745 | if (mro == NULL) |
| 1746 | return -1; |
| 1747 | result = PyObject_CallObject(mro, NULL); |
| 1748 | Py_DECREF(mro); |
| 1749 | } |
| 1750 | if (result == NULL) |
| 1751 | return -1; |
| 1752 | tuple = PySequence_Tuple(result); |
| 1753 | Py_DECREF(result); |
| 1754 | if (tuple == NULL) |
| 1755 | return -1; |
| 1756 | if (checkit) { |
| 1757 | Py_ssize_t i, len; |
| 1758 | PyObject *cls; |
| 1759 | PyTypeObject *solid; |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1760 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1761 | solid = solid_base(type); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1762 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1763 | len = PyTuple_GET_SIZE(tuple); |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1764 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1765 | for (i = 0; i < len; i++) { |
| 1766 | PyTypeObject *t; |
| 1767 | cls = PyTuple_GET_ITEM(tuple, i); |
| 1768 | if (!PyType_Check(cls)) { |
| 1769 | PyErr_Format(PyExc_TypeError, |
| 1770 | "mro() returned a non-class ('%.500s')", |
| 1771 | Py_TYPE(cls)->tp_name); |
| 1772 | Py_DECREF(tuple); |
| 1773 | return -1; |
| 1774 | } |
| 1775 | t = (PyTypeObject*)cls; |
| 1776 | if (!PyType_IsSubtype(solid, solid_base(t))) { |
| 1777 | PyErr_Format(PyExc_TypeError, |
| 1778 | "mro() returned base with unsuitable layout ('%.500s')", |
| 1779 | t->tp_name); |
| 1780 | Py_DECREF(tuple); |
| 1781 | return -1; |
| 1782 | } |
| 1783 | } |
| 1784 | } |
| 1785 | type->tp_mro = tuple; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1786 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1787 | type_mro_modified(type, type->tp_mro); |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 1788 | /* corner case: the super class might have been hidden |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1789 | from the custom MRO */ |
| 1790 | type_mro_modified(type, type->tp_bases); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1791 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1792 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1793 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1794 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | |
| 1798 | /* Calculate the best base amongst multiple base classes. |
| 1799 | This is the first one that's on the path to the "solid base". */ |
| 1800 | |
| 1801 | static PyTypeObject * |
| 1802 | best_base(PyObject *bases) |
| 1803 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1804 | Py_ssize_t i, n; |
| 1805 | PyTypeObject *base, *winner, *candidate, *base_i; |
| 1806 | PyObject *base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1808 | assert(PyTuple_Check(bases)); |
| 1809 | n = PyTuple_GET_SIZE(bases); |
| 1810 | assert(n > 0); |
| 1811 | base = NULL; |
| 1812 | winner = NULL; |
| 1813 | for (i = 0; i < n; i++) { |
| 1814 | base_proto = PyTuple_GET_ITEM(bases, i); |
| 1815 | if (!PyType_Check(base_proto)) { |
| 1816 | PyErr_SetString( |
| 1817 | PyExc_TypeError, |
| 1818 | "bases must be types"); |
| 1819 | return NULL; |
| 1820 | } |
| 1821 | base_i = (PyTypeObject *)base_proto; |
| 1822 | if (base_i->tp_dict == NULL) { |
| 1823 | if (PyType_Ready(base_i) < 0) |
| 1824 | return NULL; |
| 1825 | } |
| 1826 | candidate = solid_base(base_i); |
| 1827 | if (winner == NULL) { |
| 1828 | winner = candidate; |
| 1829 | base = base_i; |
| 1830 | } |
| 1831 | else if (PyType_IsSubtype(winner, candidate)) |
| 1832 | ; |
| 1833 | else if (PyType_IsSubtype(candidate, winner)) { |
| 1834 | winner = candidate; |
| 1835 | base = base_i; |
| 1836 | } |
| 1837 | else { |
| 1838 | PyErr_SetString( |
| 1839 | PyExc_TypeError, |
| 1840 | "multiple bases have " |
| 1841 | "instance lay-out conflict"); |
| 1842 | return NULL; |
| 1843 | } |
| 1844 | } |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 1845 | assert (base != NULL); |
| 1846 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1847 | return base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
| 1850 | static int |
| 1851 | extra_ivars(PyTypeObject *type, PyTypeObject *base) |
| 1852 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1853 | size_t t_size = type->tp_basicsize; |
| 1854 | size_t b_size = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1855 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1856 | assert(t_size >= b_size); /* Else type smaller than base! */ |
| 1857 | if (type->tp_itemsize || base->tp_itemsize) { |
| 1858 | /* If itemsize is involved, stricter rules */ |
| 1859 | return t_size != b_size || |
| 1860 | type->tp_itemsize != base->tp_itemsize; |
| 1861 | } |
| 1862 | if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 && |
| 1863 | type->tp_weaklistoffset + sizeof(PyObject *) == t_size && |
| 1864 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 1865 | t_size -= sizeof(PyObject *); |
| 1866 | if (type->tp_dictoffset && base->tp_dictoffset == 0 && |
| 1867 | type->tp_dictoffset + sizeof(PyObject *) == t_size && |
| 1868 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 1869 | t_size -= sizeof(PyObject *); |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 1870 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1871 | return t_size != b_size; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | static PyTypeObject * |
| 1875 | solid_base(PyTypeObject *type) |
| 1876 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1877 | PyTypeObject *base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1879 | if (type->tp_base) |
| 1880 | base = solid_base(type->tp_base); |
| 1881 | else |
| 1882 | base = &PyBaseObject_Type; |
| 1883 | if (extra_ivars(type, base)) |
| 1884 | return type; |
| 1885 | else |
| 1886 | return base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1889 | static void object_dealloc(PyObject *); |
| 1890 | static int object_init(PyObject *, PyObject *, PyObject *); |
| 1891 | static int update_slot(PyTypeObject *, PyObject *); |
| 1892 | static void fixup_slot_dispatchers(PyTypeObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1893 | |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1894 | /* |
| 1895 | * Helpers for __dict__ descriptor. We don't want to expose the dicts |
| 1896 | * inherited from various builtin types. The builtin base usually provides |
| 1897 | * its own __dict__ descriptor, so we use that when we can. |
| 1898 | */ |
| 1899 | static PyTypeObject * |
| 1900 | get_builtin_base_with_dict(PyTypeObject *type) |
| 1901 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1902 | while (type->tp_base != NULL) { |
| 1903 | if (type->tp_dictoffset != 0 && |
| 1904 | !(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 1905 | return type; |
| 1906 | type = type->tp_base; |
| 1907 | } |
| 1908 | return NULL; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | static PyObject * |
| 1912 | get_dict_descriptor(PyTypeObject *type) |
| 1913 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1914 | PyObject *descr; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1915 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1916 | descr = _PyType_LookupId(type, &PyId___dict__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1917 | if (descr == NULL || !PyDescr_IsData(descr)) |
| 1918 | return NULL; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1919 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1920 | return descr; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | static void |
| 1924 | raise_dict_descr_error(PyObject *obj) |
| 1925 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1926 | PyErr_Format(PyExc_TypeError, |
| 1927 | "this __dict__ descriptor does not support " |
| 1928 | "'%.200s' objects", Py_TYPE(obj)->tp_name); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1931 | static PyObject * |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1932 | subtype_dict(PyObject *obj, void *context) |
| 1933 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1934 | PyTypeObject *base; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1935 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1936 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
| 1937 | if (base != NULL) { |
| 1938 | descrgetfunc func; |
| 1939 | PyObject *descr = get_dict_descriptor(base); |
| 1940 | if (descr == NULL) { |
| 1941 | raise_dict_descr_error(obj); |
| 1942 | return NULL; |
| 1943 | } |
| 1944 | func = Py_TYPE(descr)->tp_descr_get; |
| 1945 | if (func == NULL) { |
| 1946 | raise_dict_descr_error(obj); |
| 1947 | return NULL; |
| 1948 | } |
| 1949 | return func(descr, obj, (PyObject *)(Py_TYPE(obj))); |
| 1950 | } |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 1951 | return PyObject_GenericGetDict(obj, context); |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1954 | static int |
| 1955 | subtype_setdict(PyObject *obj, PyObject *value, void *context) |
| 1956 | { |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 1957 | PyObject *dict, **dictptr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1958 | PyTypeObject *base; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1959 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1960 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
| 1961 | if (base != NULL) { |
| 1962 | descrsetfunc func; |
| 1963 | PyObject *descr = get_dict_descriptor(base); |
| 1964 | if (descr == NULL) { |
| 1965 | raise_dict_descr_error(obj); |
| 1966 | return -1; |
| 1967 | } |
| 1968 | func = Py_TYPE(descr)->tp_descr_set; |
| 1969 | if (func == NULL) { |
| 1970 | raise_dict_descr_error(obj); |
| 1971 | return -1; |
| 1972 | } |
| 1973 | return func(descr, obj, value); |
| 1974 | } |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 1975 | /* Almost like PyObject_GenericSetDict, but allow __dict__ to be deleted. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1976 | dictptr = _PyObject_GetDictPtr(obj); |
| 1977 | if (dictptr == NULL) { |
| 1978 | PyErr_SetString(PyExc_AttributeError, |
| 1979 | "This object has no __dict__"); |
| 1980 | return -1; |
| 1981 | } |
Benjamin Peterson | 006c5a2 | 2012-02-19 20:36:12 -0500 | [diff] [blame] | 1982 | if (value != NULL && !PyDict_Check(value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1983 | PyErr_Format(PyExc_TypeError, |
| 1984 | "__dict__ must be set to a dictionary, " |
| 1985 | "not a '%.200s'", Py_TYPE(value)->tp_name); |
| 1986 | return -1; |
| 1987 | } |
| 1988 | dict = *dictptr; |
| 1989 | Py_XINCREF(value); |
| 1990 | *dictptr = value; |
| 1991 | Py_XDECREF(dict); |
| 1992 | return 0; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 1995 | static PyObject * |
| 1996 | subtype_getweakref(PyObject *obj, void *context) |
| 1997 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1998 | PyObject **weaklistptr; |
| 1999 | PyObject *result; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2000 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2001 | if (Py_TYPE(obj)->tp_weaklistoffset == 0) { |
| 2002 | PyErr_SetString(PyExc_AttributeError, |
| 2003 | "This object has no __weakref__"); |
| 2004 | return NULL; |
| 2005 | } |
| 2006 | assert(Py_TYPE(obj)->tp_weaklistoffset > 0); |
| 2007 | assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <= |
| 2008 | (size_t)(Py_TYPE(obj)->tp_basicsize)); |
| 2009 | weaklistptr = (PyObject **) |
| 2010 | ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset); |
| 2011 | if (*weaklistptr == NULL) |
| 2012 | result = Py_None; |
| 2013 | else |
| 2014 | result = *weaklistptr; |
| 2015 | Py_INCREF(result); |
| 2016 | return result; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2019 | /* Three variants on the subtype_getsets list. */ |
| 2020 | |
| 2021 | static PyGetSetDef subtype_getsets_full[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2022 | {"__dict__", subtype_dict, subtype_setdict, |
| 2023 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 2024 | {"__weakref__", subtype_getweakref, NULL, |
| 2025 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 2026 | {0} |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 2027 | }; |
| 2028 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2029 | static PyGetSetDef subtype_getsets_dict_only[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2030 | {"__dict__", subtype_dict, subtype_setdict, |
| 2031 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 2032 | {0} |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2033 | }; |
| 2034 | |
| 2035 | static PyGetSetDef subtype_getsets_weakref_only[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2036 | {"__weakref__", subtype_getweakref, NULL, |
| 2037 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 2038 | {0} |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2039 | }; |
| 2040 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 2041 | static int |
| 2042 | valid_identifier(PyObject *s) |
| 2043 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2044 | if (!PyUnicode_Check(s)) { |
| 2045 | PyErr_Format(PyExc_TypeError, |
| 2046 | "__slots__ items must be strings, not '%.200s'", |
| 2047 | Py_TYPE(s)->tp_name); |
| 2048 | return 0; |
| 2049 | } |
| 2050 | if (!PyUnicode_IsIdentifier(s)) { |
| 2051 | PyErr_SetString(PyExc_TypeError, |
| 2052 | "__slots__ must be identifiers"); |
| 2053 | return 0; |
| 2054 | } |
| 2055 | return 1; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2058 | /* Forward */ |
| 2059 | static int |
| 2060 | object_init(PyObject *self, PyObject *args, PyObject *kwds); |
| 2061 | |
| 2062 | static int |
| 2063 | type_init(PyObject *cls, PyObject *args, PyObject *kwds) |
| 2064 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2065 | int res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2066 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2067 | assert(args != NULL && PyTuple_Check(args)); |
| 2068 | assert(kwds == NULL || PyDict_Check(kwds)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2069 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2070 | if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) { |
| 2071 | PyErr_SetString(PyExc_TypeError, |
| 2072 | "type.__init__() takes no keyword arguments"); |
| 2073 | return -1; |
| 2074 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2075 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2076 | if (args != NULL && PyTuple_Check(args) && |
| 2077 | (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) { |
| 2078 | PyErr_SetString(PyExc_TypeError, |
| 2079 | "type.__init__() takes 1 or 3 arguments"); |
| 2080 | return -1; |
| 2081 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2082 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2083 | /* Call object.__init__(self) now. */ |
| 2084 | /* XXX Could call super(type, cls).__init__() but what's the point? */ |
| 2085 | args = PyTuple_GetSlice(args, 0, 0); |
| 2086 | res = object_init(cls, args, NULL); |
| 2087 | Py_DECREF(args); |
| 2088 | return res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 2091 | unsigned long |
Martin v. Löwis | 738236d | 2011-02-05 20:35:29 +0000 | [diff] [blame] | 2092 | PyType_GetFlags(PyTypeObject *type) |
| 2093 | { |
| 2094 | return type->tp_flags; |
| 2095 | } |
| 2096 | |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 2097 | /* Determine the most derived metatype. */ |
| 2098 | PyTypeObject * |
| 2099 | _PyType_CalculateMetaclass(PyTypeObject *metatype, PyObject *bases) |
| 2100 | { |
| 2101 | Py_ssize_t i, nbases; |
| 2102 | PyTypeObject *winner; |
| 2103 | PyObject *tmp; |
| 2104 | PyTypeObject *tmptype; |
| 2105 | |
| 2106 | /* Determine the proper metatype to deal with this, |
| 2107 | and check for metatype conflicts while we're at it. |
| 2108 | Note that if some other metatype wins to contract, |
| 2109 | it's possible that its instances are not types. */ |
| 2110 | |
| 2111 | nbases = PyTuple_GET_SIZE(bases); |
| 2112 | winner = metatype; |
| 2113 | for (i = 0; i < nbases; i++) { |
| 2114 | tmp = PyTuple_GET_ITEM(bases, i); |
| 2115 | tmptype = Py_TYPE(tmp); |
| 2116 | if (PyType_IsSubtype(winner, tmptype)) |
| 2117 | continue; |
| 2118 | if (PyType_IsSubtype(tmptype, winner)) { |
| 2119 | winner = tmptype; |
| 2120 | continue; |
| 2121 | } |
| 2122 | /* else: */ |
| 2123 | PyErr_SetString(PyExc_TypeError, |
| 2124 | "metaclass conflict: " |
| 2125 | "the metaclass of a derived class " |
| 2126 | "must be a (non-strict) subclass " |
| 2127 | "of the metaclasses of all its bases"); |
| 2128 | return NULL; |
| 2129 | } |
| 2130 | return winner; |
| 2131 | } |
| 2132 | |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 2133 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2134 | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) |
| 2135 | { |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2136 | PyObject *name, *bases = NULL, *orig_dict, *dict = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2137 | static char *kwlist[] = {"name", "bases", "dict", 0}; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2138 | PyObject *qualname, *slots = NULL, *tmp, *newslots; |
| 2139 | PyTypeObject *type = NULL, *base, *tmptype, *winner; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2140 | PyHeapTypeObject *et; |
| 2141 | PyMemberDef *mp; |
| 2142 | Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak; |
| 2143 | int j, may_add_dict, may_add_weak; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2144 | _Py_IDENTIFIER(__qualname__); |
| 2145 | _Py_IDENTIFIER(__slots__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2147 | assert(args != NULL && PyTuple_Check(args)); |
| 2148 | assert(kwds == NULL || PyDict_Check(kwds)); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2149 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2150 | /* Special case: type(x) should return x->ob_type */ |
| 2151 | { |
| 2152 | const Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 2153 | const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2155 | if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) { |
| 2156 | PyObject *x = PyTuple_GET_ITEM(args, 0); |
| 2157 | Py_INCREF(Py_TYPE(x)); |
| 2158 | return (PyObject *) Py_TYPE(x); |
| 2159 | } |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2161 | /* SF bug 475327 -- if that didn't trigger, we need 3 |
| 2162 | arguments. but PyArg_ParseTupleAndKeywords below may give |
| 2163 | a msg saying type() needs exactly 3. */ |
| 2164 | if (nargs + nkwds != 3) { |
| 2165 | PyErr_SetString(PyExc_TypeError, |
| 2166 | "type() takes 1 or 3 arguments"); |
| 2167 | return NULL; |
| 2168 | } |
| 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 | /* Check arguments: (name, bases, dict) */ |
| 2172 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "UO!O!:type", kwlist, |
| 2173 | &name, |
| 2174 | &PyTuple_Type, &bases, |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2175 | &PyDict_Type, &orig_dict)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2176 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2177 | |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 2178 | /* Determine the proper metatype to deal with this: */ |
| 2179 | winner = _PyType_CalculateMetaclass(metatype, bases); |
| 2180 | if (winner == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2181 | return NULL; |
| 2182 | } |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 2183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2184 | if (winner != metatype) { |
| 2185 | if (winner->tp_new != type_new) /* Pass it to the winner */ |
| 2186 | return winner->tp_new(winner, args, kwds); |
| 2187 | metatype = winner; |
| 2188 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2189 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2190 | /* Adjust for empty tuple bases */ |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 2191 | nbases = PyTuple_GET_SIZE(bases); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2192 | if (nbases == 0) { |
| 2193 | bases = PyTuple_Pack(1, &PyBaseObject_Type); |
| 2194 | if (bases == NULL) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2195 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2196 | nbases = 1; |
| 2197 | } |
| 2198 | else |
| 2199 | Py_INCREF(bases); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2201 | /* Calculate best base, and check that all bases are type objects */ |
| 2202 | base = best_base(bases); |
| 2203 | if (base == NULL) { |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2204 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2205 | } |
| 2206 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 2207 | PyErr_Format(PyExc_TypeError, |
| 2208 | "type '%.100s' is not an acceptable base type", |
| 2209 | base->tp_name); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2210 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2211 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2212 | |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2213 | dict = PyDict_Copy(orig_dict); |
| 2214 | if (dict == NULL) |
| 2215 | goto error; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2216 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2217 | /* Check for a __slots__ sequence variable in dict, and count it */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2218 | slots = _PyDict_GetItemId(dict, &PyId___slots__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2219 | nslots = 0; |
| 2220 | add_dict = 0; |
| 2221 | add_weak = 0; |
| 2222 | may_add_dict = base->tp_dictoffset == 0; |
| 2223 | may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0; |
| 2224 | if (slots == NULL) { |
| 2225 | if (may_add_dict) { |
| 2226 | add_dict++; |
| 2227 | } |
| 2228 | if (may_add_weak) { |
| 2229 | add_weak++; |
| 2230 | } |
| 2231 | } |
| 2232 | else { |
| 2233 | /* Have slots */ |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2235 | /* Make it into a tuple */ |
| 2236 | if (PyUnicode_Check(slots)) |
| 2237 | slots = PyTuple_Pack(1, slots); |
| 2238 | else |
| 2239 | slots = PySequence_Tuple(slots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2240 | if (slots == NULL) |
| 2241 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2242 | assert(PyTuple_Check(slots)); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2243 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2244 | /* Are slots allowed? */ |
| 2245 | nslots = PyTuple_GET_SIZE(slots); |
| 2246 | if (nslots > 0 && base->tp_itemsize != 0) { |
| 2247 | PyErr_Format(PyExc_TypeError, |
| 2248 | "nonempty __slots__ " |
| 2249 | "not supported for subtype of '%s'", |
| 2250 | base->tp_name); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2251 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2252 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2253 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2254 | /* Check for valid slot names and two special cases */ |
| 2255 | for (i = 0; i < nslots; i++) { |
| 2256 | PyObject *tmp = PyTuple_GET_ITEM(slots, i); |
| 2257 | if (!valid_identifier(tmp)) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2258 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2259 | assert(PyUnicode_Check(tmp)); |
Victor Stinner | ad14ccd | 2013-11-07 00:46:04 +0100 | [diff] [blame] | 2260 | if (_PyUnicode_CompareWithId(tmp, &PyId___dict__) == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2261 | if (!may_add_dict || add_dict) { |
| 2262 | PyErr_SetString(PyExc_TypeError, |
| 2263 | "__dict__ slot disallowed: " |
| 2264 | "we already got one"); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2265 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2266 | } |
| 2267 | add_dict++; |
| 2268 | } |
| 2269 | if (PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0) { |
| 2270 | if (!may_add_weak || add_weak) { |
| 2271 | PyErr_SetString(PyExc_TypeError, |
| 2272 | "__weakref__ slot disallowed: " |
| 2273 | "either we already got one, " |
| 2274 | "or __itemsize__ != 0"); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2275 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2276 | } |
| 2277 | add_weak++; |
| 2278 | } |
| 2279 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2281 | /* Copy slots into a list, mangle names and sort them. |
| 2282 | Sorted names are needed for __class__ assignment. |
| 2283 | Convert them back to tuple at the end. |
| 2284 | */ |
| 2285 | newslots = PyList_New(nslots - add_dict - add_weak); |
| 2286 | if (newslots == NULL) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2287 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2288 | for (i = j = 0; i < nslots; i++) { |
| 2289 | tmp = PyTuple_GET_ITEM(slots, i); |
| 2290 | if ((add_dict && |
Victor Stinner | ad14ccd | 2013-11-07 00:46:04 +0100 | [diff] [blame] | 2291 | _PyUnicode_CompareWithId(tmp, &PyId___dict__) == 0) || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2292 | (add_weak && |
| 2293 | PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0)) |
| 2294 | continue; |
| 2295 | tmp =_Py_Mangle(name, tmp); |
Benjamin Peterson | ae13c88 | 2011-08-16 22:26:48 -0500 | [diff] [blame] | 2296 | if (!tmp) { |
| 2297 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2298 | goto error; |
Benjamin Peterson | ae13c88 | 2011-08-16 22:26:48 -0500 | [diff] [blame] | 2299 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2300 | PyList_SET_ITEM(newslots, j, tmp); |
Benjamin Peterson | c4085c8 | 2011-08-16 18:53:26 -0500 | [diff] [blame] | 2301 | if (PyDict_GetItem(dict, tmp)) { |
| 2302 | PyErr_Format(PyExc_ValueError, |
| 2303 | "%R in __slots__ conflicts with class variable", |
| 2304 | tmp); |
Benjamin Peterson | d17cefc | 2011-08-16 22:28:23 -0500 | [diff] [blame] | 2305 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2306 | goto error; |
Benjamin Peterson | c4085c8 | 2011-08-16 18:53:26 -0500 | [diff] [blame] | 2307 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2308 | j++; |
| 2309 | } |
| 2310 | assert(j == nslots - add_dict - add_weak); |
| 2311 | nslots = j; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2312 | Py_CLEAR(slots); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2313 | if (PyList_Sort(newslots) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2314 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2315 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2316 | } |
| 2317 | slots = PyList_AsTuple(newslots); |
| 2318 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2319 | if (slots == NULL) |
| 2320 | goto error; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2322 | /* Secondary bases may provide weakrefs or dict */ |
| 2323 | if (nbases > 1 && |
| 2324 | ((may_add_dict && !add_dict) || |
| 2325 | (may_add_weak && !add_weak))) { |
| 2326 | for (i = 0; i < nbases; i++) { |
| 2327 | tmp = PyTuple_GET_ITEM(bases, i); |
| 2328 | if (tmp == (PyObject *)base) |
| 2329 | continue; /* Skip primary base */ |
| 2330 | assert(PyType_Check(tmp)); |
| 2331 | tmptype = (PyTypeObject *)tmp; |
| 2332 | if (may_add_dict && !add_dict && |
| 2333 | tmptype->tp_dictoffset != 0) |
| 2334 | add_dict++; |
| 2335 | if (may_add_weak && !add_weak && |
| 2336 | tmptype->tp_weaklistoffset != 0) |
| 2337 | add_weak++; |
| 2338 | if (may_add_dict && !add_dict) |
| 2339 | continue; |
| 2340 | if (may_add_weak && !add_weak) |
| 2341 | continue; |
| 2342 | /* Nothing more to check */ |
| 2343 | break; |
| 2344 | } |
| 2345 | } |
| 2346 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2348 | /* Allocate the type object */ |
| 2349 | type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2350 | if (type == NULL) |
| 2351 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2353 | /* Keep name and slots alive in the extended type object */ |
| 2354 | et = (PyHeapTypeObject *)type; |
| 2355 | Py_INCREF(name); |
| 2356 | et->ht_name = name; |
| 2357 | et->ht_slots = slots; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2358 | slots = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2360 | /* Initialize tp_flags */ |
| 2361 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 2362 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_FINALIZE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2363 | if (base->tp_flags & Py_TPFLAGS_HAVE_GC) |
| 2364 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2366 | /* Initialize essential fields */ |
| 2367 | type->tp_as_number = &et->as_number; |
| 2368 | type->tp_as_sequence = &et->as_sequence; |
| 2369 | type->tp_as_mapping = &et->as_mapping; |
| 2370 | type->tp_as_buffer = &et->as_buffer; |
| 2371 | type->tp_name = _PyUnicode_AsString(name); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2372 | if (!type->tp_name) |
| 2373 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2375 | /* Set tp_base and tp_bases */ |
| 2376 | type->tp_bases = bases; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2377 | bases = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2378 | Py_INCREF(base); |
| 2379 | type->tp_base = base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2381 | /* Initialize tp_dict from passed-in dict */ |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2382 | Py_INCREF(dict); |
| 2383 | type->tp_dict = dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2384 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2385 | /* Set __module__ in the dict */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2386 | if (_PyDict_GetItemId(dict, &PyId___module__) == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2387 | tmp = PyEval_GetGlobals(); |
| 2388 | if (tmp != NULL) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2389 | tmp = _PyDict_GetItemId(tmp, &PyId___name__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2390 | if (tmp != NULL) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2391 | if (_PyDict_SetItemId(dict, &PyId___module__, |
| 2392 | tmp) < 0) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2393 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2394 | } |
| 2395 | } |
| 2396 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 2397 | |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2398 | /* Set ht_qualname to dict['__qualname__'] if available, else to |
| 2399 | __name__. The __qualname__ accessor will look for ht_qualname. |
| 2400 | */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2401 | qualname = _PyDict_GetItemId(dict, &PyId___qualname__); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2402 | if (qualname != NULL) { |
| 2403 | if (!PyUnicode_Check(qualname)) { |
| 2404 | PyErr_Format(PyExc_TypeError, |
| 2405 | "type __qualname__ must be a str, not %s", |
| 2406 | Py_TYPE(qualname)->tp_name); |
| 2407 | goto error; |
| 2408 | } |
| 2409 | } |
Benjamin Peterson | 8afa7fa | 2012-10-30 23:51:03 -0400 | [diff] [blame] | 2410 | et->ht_qualname = qualname ? qualname : et->ht_name; |
| 2411 | Py_INCREF(et->ht_qualname); |
| 2412 | if (qualname != NULL && PyDict_DelItem(dict, PyId___qualname__.object) < 0) |
| 2413 | goto error; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2414 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2415 | /* Set tp_doc to a copy of dict['__doc__'], if the latter is there |
| 2416 | and is a string. The __doc__ accessor will first look for tp_doc; |
| 2417 | if that fails, it will still look into __dict__. |
| 2418 | */ |
| 2419 | { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2420 | PyObject *doc = _PyDict_GetItemId(dict, &PyId___doc__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2421 | if (doc != NULL && PyUnicode_Check(doc)) { |
| 2422 | Py_ssize_t len; |
| 2423 | char *doc_str; |
| 2424 | char *tp_doc; |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 2425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2426 | doc_str = _PyUnicode_AsString(doc); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2427 | if (doc_str == NULL) |
| 2428 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2429 | /* Silently truncate the docstring if it contains null bytes. */ |
| 2430 | len = strlen(doc_str); |
| 2431 | tp_doc = (char *)PyObject_MALLOC(len + 1); |
Victor Stinner | 53510cd | 2013-07-15 19:34:20 +0200 | [diff] [blame] | 2432 | if (tp_doc == NULL) { |
| 2433 | PyErr_NoMemory(); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2434 | goto error; |
Victor Stinner | 53510cd | 2013-07-15 19:34:20 +0200 | [diff] [blame] | 2435 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2436 | memcpy(tp_doc, doc_str, len + 1); |
| 2437 | type->tp_doc = tp_doc; |
| 2438 | } |
| 2439 | } |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2441 | /* Special-case __new__: if it's a plain function, |
| 2442 | make it a static function */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2443 | tmp = _PyDict_GetItemId(dict, &PyId___new__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2444 | if (tmp != NULL && PyFunction_Check(tmp)) { |
| 2445 | tmp = PyStaticMethod_New(tmp); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2446 | if (tmp == NULL) |
| 2447 | goto error; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2448 | if (_PyDict_SetItemId(dict, &PyId___new__, tmp) < 0) |
| 2449 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2450 | Py_DECREF(tmp); |
| 2451 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2452 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2453 | /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
| 2454 | mp = PyHeapType_GET_MEMBERS(et); |
| 2455 | slotoffset = base->tp_basicsize; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2456 | if (et->ht_slots != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2457 | for (i = 0; i < nslots; i++, mp++) { |
| 2458 | mp->name = _PyUnicode_AsString( |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2459 | PyTuple_GET_ITEM(et->ht_slots, i)); |
| 2460 | if (mp->name == NULL) |
| 2461 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2462 | mp->type = T_OBJECT_EX; |
| 2463 | mp->offset = slotoffset; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2464 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2465 | /* __dict__ and __weakref__ are already filtered out */ |
| 2466 | assert(strcmp(mp->name, "__dict__") != 0); |
| 2467 | assert(strcmp(mp->name, "__weakref__") != 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2469 | slotoffset += sizeof(PyObject *); |
| 2470 | } |
| 2471 | } |
| 2472 | if (add_dict) { |
| 2473 | if (base->tp_itemsize) |
| 2474 | type->tp_dictoffset = -(long)sizeof(PyObject *); |
| 2475 | else |
| 2476 | type->tp_dictoffset = slotoffset; |
| 2477 | slotoffset += sizeof(PyObject *); |
| 2478 | } |
| 2479 | if (add_weak) { |
| 2480 | assert(!base->tp_itemsize); |
| 2481 | type->tp_weaklistoffset = slotoffset; |
| 2482 | slotoffset += sizeof(PyObject *); |
| 2483 | } |
| 2484 | type->tp_basicsize = slotoffset; |
| 2485 | type->tp_itemsize = base->tp_itemsize; |
| 2486 | type->tp_members = PyHeapType_GET_MEMBERS(et); |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2487 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2488 | if (type->tp_weaklistoffset && type->tp_dictoffset) |
| 2489 | type->tp_getset = subtype_getsets_full; |
| 2490 | else if (type->tp_weaklistoffset && !type->tp_dictoffset) |
| 2491 | type->tp_getset = subtype_getsets_weakref_only; |
| 2492 | else if (!type->tp_weaklistoffset && type->tp_dictoffset) |
| 2493 | type->tp_getset = subtype_getsets_dict_only; |
| 2494 | else |
| 2495 | type->tp_getset = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2497 | /* Special case some slots */ |
| 2498 | if (type->tp_dictoffset != 0 || nslots > 0) { |
| 2499 | if (base->tp_getattr == NULL && base->tp_getattro == NULL) |
| 2500 | type->tp_getattro = PyObject_GenericGetAttr; |
| 2501 | if (base->tp_setattr == NULL && base->tp_setattro == NULL) |
| 2502 | type->tp_setattro = PyObject_GenericSetAttr; |
| 2503 | } |
| 2504 | type->tp_dealloc = subtype_dealloc; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2505 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2506 | /* Enable GC unless there are really no instance variables possible */ |
| 2507 | if (!(type->tp_basicsize == sizeof(PyObject) && |
| 2508 | type->tp_itemsize == 0)) |
| 2509 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 2510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2511 | /* Always override allocation strategy to use regular heap */ |
| 2512 | type->tp_alloc = PyType_GenericAlloc; |
| 2513 | if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { |
| 2514 | type->tp_free = PyObject_GC_Del; |
| 2515 | type->tp_traverse = subtype_traverse; |
| 2516 | type->tp_clear = subtype_clear; |
| 2517 | } |
| 2518 | else |
| 2519 | type->tp_free = PyObject_Del; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2521 | /* Initialize the rest */ |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2522 | if (PyType_Ready(type) < 0) |
| 2523 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2524 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2525 | /* Put the proper slots in place */ |
| 2526 | fixup_slot_dispatchers(type); |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 2527 | |
Benjamin Peterson | df81379 | 2014-03-17 15:57:17 -0500 | [diff] [blame] | 2528 | if (type->tp_dictoffset) { |
| 2529 | et->ht_cached_keys = _PyDict_NewKeysForClass(); |
| 2530 | } |
| 2531 | |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2532 | Py_DECREF(dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2533 | return (PyObject *)type; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2534 | |
| 2535 | error: |
| 2536 | Py_XDECREF(dict); |
| 2537 | Py_XDECREF(bases); |
| 2538 | Py_XDECREF(slots); |
| 2539 | Py_XDECREF(type); |
| 2540 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2541 | } |
| 2542 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2543 | static short slotoffsets[] = { |
| 2544 | -1, /* invalid slot */ |
| 2545 | #include "typeslots.inc" |
| 2546 | }; |
| 2547 | |
Benjamin Peterson | e28108c | 2012-01-29 20:13:18 -0500 | [diff] [blame] | 2548 | PyObject * |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2549 | PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases) |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2550 | { |
| 2551 | PyHeapTypeObject *res = (PyHeapTypeObject*)PyType_GenericAlloc(&PyType_Type, 0); |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2552 | PyTypeObject *type, *base; |
| 2553 | char *s; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2554 | char *res_start = (char*)res; |
| 2555 | PyType_Slot *slot; |
Victor Stinner | 54e4ca7 | 2013-07-11 22:42:25 +0200 | [diff] [blame] | 2556 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2557 | /* Set the type name and qualname */ |
| 2558 | s = strrchr(spec->name, '.'); |
| 2559 | if (s == NULL) |
| 2560 | s = (char*)spec->name; |
| 2561 | else |
| 2562 | s++; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2563 | |
Martin v. Löwis | 5e06a5d | 2011-02-21 16:24:00 +0000 | [diff] [blame] | 2564 | if (res == NULL) |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2565 | return NULL; |
| 2566 | type = &res->ht_type; |
Antoine Pitrou | 66a3a7e | 2012-06-24 00:42:59 +0200 | [diff] [blame] | 2567 | /* The flags must be initialized early, before the GC traverses us */ |
| 2568 | type->tp_flags = spec->flags | Py_TPFLAGS_HEAPTYPE; |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2569 | res->ht_name = PyUnicode_FromString(s); |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2570 | if (!res->ht_name) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2571 | goto fail; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2572 | res->ht_qualname = res->ht_name; |
| 2573 | Py_INCREF(res->ht_qualname); |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2574 | type->tp_name = spec->name; |
| 2575 | if (!type->tp_name) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2576 | goto fail; |
Victor Stinner | 54e4ca7 | 2013-07-11 22:42:25 +0200 | [diff] [blame] | 2577 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2578 | /* Adjust for empty tuple bases */ |
| 2579 | if (!bases) { |
| 2580 | base = &PyBaseObject_Type; |
| 2581 | /* See whether Py_tp_base(s) was specified */ |
| 2582 | for (slot = spec->slots; slot->slot; slot++) { |
| 2583 | if (slot->slot == Py_tp_base) |
| 2584 | base = slot->pfunc; |
| 2585 | else if (slot->slot == Py_tp_bases) { |
| 2586 | bases = slot->pfunc; |
| 2587 | Py_INCREF(bases); |
| 2588 | } |
| 2589 | } |
| 2590 | if (!bases) |
| 2591 | bases = PyTuple_Pack(1, base); |
| 2592 | if (!bases) |
| 2593 | goto fail; |
| 2594 | } |
| 2595 | else |
| 2596 | Py_INCREF(bases); |
| 2597 | |
| 2598 | /* Calculate best base, and check that all bases are type objects */ |
| 2599 | base = best_base(bases); |
| 2600 | if (base == NULL) { |
| 2601 | goto fail; |
| 2602 | } |
| 2603 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 2604 | PyErr_Format(PyExc_TypeError, |
| 2605 | "type '%.100s' is not an acceptable base type", |
| 2606 | base->tp_name); |
| 2607 | goto fail; |
| 2608 | } |
| 2609 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2610 | /* Initialize essential fields */ |
| 2611 | type->tp_as_number = &res->as_number; |
| 2612 | type->tp_as_sequence = &res->as_sequence; |
| 2613 | type->tp_as_mapping = &res->as_mapping; |
| 2614 | type->tp_as_buffer = &res->as_buffer; |
| 2615 | /* Set tp_base and tp_bases */ |
| 2616 | type->tp_bases = bases; |
| 2617 | bases = NULL; |
| 2618 | Py_INCREF(base); |
| 2619 | type->tp_base = base; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2620 | |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2621 | type->tp_basicsize = spec->basicsize; |
| 2622 | type->tp_itemsize = spec->itemsize; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 2623 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2624 | for (slot = spec->slots; slot->slot; slot++) { |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 2625 | if (slot->slot < 0 |
| 2626 | || (size_t)slot->slot >= Py_ARRAY_LENGTH(slotoffsets)) { |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2627 | PyErr_SetString(PyExc_RuntimeError, "invalid slot offset"); |
| 2628 | goto fail; |
| 2629 | } |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2630 | if (slot->slot == Py_tp_base || slot->slot == Py_tp_bases) |
| 2631 | /* Processed above */ |
| 2632 | continue; |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2633 | *(void**)(res_start + slotoffsets[slot->slot]) = slot->pfunc; |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2634 | |
| 2635 | /* need to make a copy of the docstring slot, which usually |
| 2636 | points to a static string literal */ |
| 2637 | if (slot->slot == Py_tp_doc) { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2638 | const char *old_doc = _PyType_DocWithoutSignature(type->tp_name, slot->pfunc); |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 2639 | size_t len = strlen(old_doc)+1; |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2640 | char *tp_doc = PyObject_MALLOC(len); |
Victor Stinner | 53510cd | 2013-07-15 19:34:20 +0200 | [diff] [blame] | 2641 | if (tp_doc == NULL) { |
| 2642 | PyErr_NoMemory(); |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2643 | goto fail; |
Victor Stinner | 53510cd | 2013-07-15 19:34:20 +0200 | [diff] [blame] | 2644 | } |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 2645 | memcpy(tp_doc, old_doc, len); |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2646 | type->tp_doc = tp_doc; |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2647 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2648 | } |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2649 | if (type->tp_dealloc == NULL) { |
Antoine Pitrou | 99cc629 | 2012-06-23 14:42:38 +0200 | [diff] [blame] | 2650 | /* It's a heap type, so needs the heap types' dealloc. |
| 2651 | subtype_dealloc will call the base type's tp_dealloc, if |
| 2652 | necessary. */ |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2653 | type->tp_dealloc = subtype_dealloc; |
Antoine Pitrou | 99cc629 | 2012-06-23 14:42:38 +0200 | [diff] [blame] | 2654 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2655 | |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2656 | if (PyType_Ready(type) < 0) |
Benjamin Peterson | 2652d25 | 2012-01-29 20:16:37 -0500 | [diff] [blame] | 2657 | goto fail; |
| 2658 | |
Benjamin Peterson | df81379 | 2014-03-17 15:57:17 -0500 | [diff] [blame] | 2659 | if (type->tp_dictoffset) { |
| 2660 | res->ht_cached_keys = _PyDict_NewKeysForClass(); |
| 2661 | } |
| 2662 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2663 | /* Set type.__module__ */ |
| 2664 | s = strrchr(spec->name, '.'); |
| 2665 | if (s != NULL) |
Victor Stinner | 54e4ca7 | 2013-07-11 22:42:25 +0200 | [diff] [blame] | 2666 | _PyDict_SetItemId(type->tp_dict, &PyId___module__, |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2667 | PyUnicode_FromStringAndSize( |
| 2668 | spec->name, (Py_ssize_t)(s - spec->name))); |
| 2669 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2670 | return (PyObject*)res; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 2671 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2672 | fail: |
| 2673 | Py_DECREF(res); |
| 2674 | return NULL; |
| 2675 | } |
| 2676 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2677 | PyObject * |
| 2678 | PyType_FromSpec(PyType_Spec *spec) |
| 2679 | { |
| 2680 | return PyType_FromSpecWithBases(spec, NULL); |
| 2681 | } |
| 2682 | |
Martin v. Löwis | ca7b046 | 2014-02-04 09:33:05 +0100 | [diff] [blame] | 2683 | void * |
| 2684 | PyType_GetSlot(PyTypeObject *type, int slot) |
| 2685 | { |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 2686 | if (!PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE) || slot < 0) { |
Martin v. Löwis | ca7b046 | 2014-02-04 09:33:05 +0100 | [diff] [blame] | 2687 | PyErr_BadInternalCall(); |
| 2688 | return NULL; |
| 2689 | } |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 2690 | if ((size_t)slot >= Py_ARRAY_LENGTH(slotoffsets)) { |
Martin v. Löwis | ca7b046 | 2014-02-04 09:33:05 +0100 | [diff] [blame] | 2691 | /* Extension module requesting slot from a future version */ |
| 2692 | return NULL; |
| 2693 | } |
| 2694 | return *(void**)(((char*)type) + slotoffsets[slot]); |
| 2695 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2696 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2697 | /* Internal API to look for a name through the MRO. |
| 2698 | This returns a borrowed reference, and doesn't set an exception! */ |
| 2699 | PyObject * |
| 2700 | _PyType_Lookup(PyTypeObject *type, PyObject *name) |
| 2701 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2702 | Py_ssize_t i, n; |
| 2703 | PyObject *mro, *res, *base, *dict; |
| 2704 | unsigned int h; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 2705 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2706 | if (MCACHE_CACHEABLE_NAME(name) && |
| 2707 | PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) { |
| 2708 | /* fast path */ |
| 2709 | h = MCACHE_HASH_METHOD(type, name); |
| 2710 | if (method_cache[h].version == type->tp_version_tag && |
| 2711 | method_cache[h].name == name) |
| 2712 | return method_cache[h].value; |
| 2713 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2714 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2715 | /* Look in tp_dict of types in MRO */ |
| 2716 | mro = type->tp_mro; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 2717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2718 | /* If mro is NULL, the type is either not yet initialized |
| 2719 | by PyType_Ready(), or already cleared by type_clear(). |
| 2720 | Either way the safest thing to do is to return NULL. */ |
| 2721 | if (mro == NULL) |
| 2722 | return NULL; |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 2723 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2724 | res = NULL; |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 2725 | /* keep a strong reference to mro because type->tp_mro can be replaced |
| 2726 | during PyDict_GetItem(dict, name) */ |
| 2727 | Py_INCREF(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2728 | assert(PyTuple_Check(mro)); |
| 2729 | n = PyTuple_GET_SIZE(mro); |
| 2730 | for (i = 0; i < n; i++) { |
| 2731 | base = PyTuple_GET_ITEM(mro, i); |
| 2732 | assert(PyType_Check(base)); |
| 2733 | dict = ((PyTypeObject *)base)->tp_dict; |
| 2734 | assert(dict && PyDict_Check(dict)); |
| 2735 | res = PyDict_GetItem(dict, name); |
| 2736 | if (res != NULL) |
| 2737 | break; |
| 2738 | } |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 2739 | Py_DECREF(mro); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 2740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2741 | if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) { |
| 2742 | h = MCACHE_HASH_METHOD(type, name); |
| 2743 | method_cache[h].version = type->tp_version_tag; |
| 2744 | method_cache[h].value = res; /* borrowed */ |
| 2745 | Py_INCREF(name); |
| 2746 | Py_DECREF(method_cache[h].name); |
| 2747 | method_cache[h].name = name; |
| 2748 | } |
| 2749 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2750 | } |
| 2751 | |
Raymond Hettinger | 2ff2190 | 2013-10-01 00:55:43 -0700 | [diff] [blame] | 2752 | PyObject * |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2753 | _PyType_LookupId(PyTypeObject *type, struct _Py_Identifier *name) |
| 2754 | { |
| 2755 | PyObject *oname; |
| 2756 | oname = _PyUnicode_FromId(name); /* borrowed */ |
| 2757 | if (oname == NULL) |
| 2758 | return NULL; |
| 2759 | return _PyType_Lookup(type, oname); |
| 2760 | } |
| 2761 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2762 | /* This is similar to PyObject_GenericGetAttr(), |
| 2763 | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ |
| 2764 | static PyObject * |
| 2765 | type_getattro(PyTypeObject *type, PyObject *name) |
| 2766 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2767 | PyTypeObject *metatype = Py_TYPE(type); |
| 2768 | PyObject *meta_attribute, *attribute; |
| 2769 | descrgetfunc meta_get; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2770 | |
Benjamin Peterson | 16d84ac | 2012-03-16 09:32:59 -0500 | [diff] [blame] | 2771 | if (!PyUnicode_Check(name)) { |
| 2772 | PyErr_Format(PyExc_TypeError, |
| 2773 | "attribute name must be string, not '%.200s'", |
| 2774 | name->ob_type->tp_name); |
| 2775 | return NULL; |
| 2776 | } |
| 2777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2778 | /* Initialize this type (we'll assume the metatype is initialized) */ |
| 2779 | if (type->tp_dict == NULL) { |
| 2780 | if (PyType_Ready(type) < 0) |
| 2781 | return NULL; |
| 2782 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2783 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2784 | /* No readable descriptor found yet */ |
| 2785 | meta_get = NULL; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2786 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2787 | /* Look for the attribute in the metatype */ |
| 2788 | meta_attribute = _PyType_Lookup(metatype, name); |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2789 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2790 | if (meta_attribute != NULL) { |
| 2791 | meta_get = Py_TYPE(meta_attribute)->tp_descr_get; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2793 | if (meta_get != NULL && PyDescr_IsData(meta_attribute)) { |
| 2794 | /* Data descriptors implement tp_descr_set to intercept |
| 2795 | * writes. Assume the attribute is not overridden in |
| 2796 | * type's tp_dict (and bases): call the descriptor now. |
| 2797 | */ |
| 2798 | return meta_get(meta_attribute, (PyObject *)type, |
| 2799 | (PyObject *)metatype); |
| 2800 | } |
| 2801 | Py_INCREF(meta_attribute); |
| 2802 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2803 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2804 | /* No data descriptor found on metatype. Look in tp_dict of this |
| 2805 | * type and its bases */ |
| 2806 | attribute = _PyType_Lookup(type, name); |
| 2807 | if (attribute != NULL) { |
| 2808 | /* Implement descriptor functionality, if any */ |
| 2809 | descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get; |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2810 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2811 | Py_XDECREF(meta_attribute); |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 2812 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2813 | if (local_get != NULL) { |
| 2814 | /* NULL 2nd argument indicates the descriptor was |
| 2815 | * found on the target object itself (or a base) */ |
| 2816 | return local_get(attribute, (PyObject *)NULL, |
| 2817 | (PyObject *)type); |
| 2818 | } |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 2819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2820 | Py_INCREF(attribute); |
| 2821 | return attribute; |
| 2822 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2823 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2824 | /* No attribute found in local __dict__ (or bases): use the |
| 2825 | * descriptor from the metatype, if any */ |
| 2826 | if (meta_get != NULL) { |
| 2827 | PyObject *res; |
| 2828 | res = meta_get(meta_attribute, (PyObject *)type, |
| 2829 | (PyObject *)metatype); |
| 2830 | Py_DECREF(meta_attribute); |
| 2831 | return res; |
| 2832 | } |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 2833 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2834 | /* If an ordinary attribute was found on the metatype, return it now */ |
| 2835 | if (meta_attribute != NULL) { |
| 2836 | return meta_attribute; |
| 2837 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2839 | /* Give up */ |
| 2840 | PyErr_Format(PyExc_AttributeError, |
| 2841 | "type object '%.50s' has no attribute '%U'", |
| 2842 | type->tp_name, name); |
| 2843 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
| 2846 | static int |
| 2847 | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) |
| 2848 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2849 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 2850 | PyErr_Format( |
| 2851 | PyExc_TypeError, |
| 2852 | "can't set attributes of built-in/extension type '%s'", |
| 2853 | type->tp_name); |
| 2854 | return -1; |
| 2855 | } |
| 2856 | if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0) |
| 2857 | return -1; |
| 2858 | return update_slot(type, name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
Benjamin Peterson | 64acccf | 2012-04-27 15:07:36 -0400 | [diff] [blame] | 2861 | extern void |
| 2862 | _PyDictKeys_DecRef(PyDictKeysObject *keys); |
| 2863 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2864 | static void |
| 2865 | type_dealloc(PyTypeObject *type) |
| 2866 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2867 | PyHeapTypeObject *et; |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 2868 | PyObject *tp, *val, *tb; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2870 | /* Assert this is a heap-allocated type object */ |
| 2871 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
| 2872 | _PyObject_GC_UNTRACK(type); |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 2873 | PyErr_Fetch(&tp, &val, &tb); |
| 2874 | remove_all_subclasses(type, type->tp_bases); |
| 2875 | PyErr_Restore(tp, val, tb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2876 | PyObject_ClearWeakRefs((PyObject *)type); |
| 2877 | et = (PyHeapTypeObject *)type; |
| 2878 | Py_XDECREF(type->tp_base); |
| 2879 | Py_XDECREF(type->tp_dict); |
| 2880 | Py_XDECREF(type->tp_bases); |
| 2881 | Py_XDECREF(type->tp_mro); |
| 2882 | Py_XDECREF(type->tp_cache); |
| 2883 | Py_XDECREF(type->tp_subclasses); |
| 2884 | /* A type's tp_doc is heap allocated, unlike the tp_doc slots |
| 2885 | * of most other objects. It's okay to cast it to char *. |
| 2886 | */ |
| 2887 | PyObject_Free((char *)type->tp_doc); |
| 2888 | Py_XDECREF(et->ht_name); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2889 | Py_XDECREF(et->ht_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2890 | Py_XDECREF(et->ht_slots); |
Benjamin Peterson | 64acccf | 2012-04-27 15:07:36 -0400 | [diff] [blame] | 2891 | if (et->ht_cached_keys) |
| 2892 | _PyDictKeys_DecRef(et->ht_cached_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2893 | Py_TYPE(type)->tp_free((PyObject *)type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2896 | static PyObject * |
| 2897 | type_subclasses(PyTypeObject *type, PyObject *args_ignored) |
| 2898 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2899 | PyObject *list, *raw, *ref; |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 2900 | Py_ssize_t i; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2901 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2902 | list = PyList_New(0); |
| 2903 | if (list == NULL) |
| 2904 | return NULL; |
| 2905 | raw = type->tp_subclasses; |
| 2906 | if (raw == NULL) |
| 2907 | return list; |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 2908 | assert(PyDict_CheckExact(raw)); |
| 2909 | i = 0; |
| 2910 | while (PyDict_Next(raw, &i, NULL, &ref)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2911 | assert(PyWeakref_CheckRef(ref)); |
| 2912 | ref = PyWeakref_GET_OBJECT(ref); |
| 2913 | if (ref != Py_None) { |
| 2914 | if (PyList_Append(list, ref) < 0) { |
| 2915 | Py_DECREF(list); |
| 2916 | return NULL; |
| 2917 | } |
| 2918 | } |
| 2919 | } |
| 2920 | return list; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
Guido van Rossum | 4737482 | 2007-08-02 16:48:17 +0000 | [diff] [blame] | 2923 | static PyObject * |
| 2924 | type_prepare(PyObject *self, PyObject *args, PyObject *kwds) |
| 2925 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2926 | return PyDict_New(); |
Guido van Rossum | 4737482 | 2007-08-02 16:48:17 +0000 | [diff] [blame] | 2927 | } |
| 2928 | |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2929 | /* |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2930 | Merge the __dict__ of aclass into dict, and recursively also all |
| 2931 | the __dict__s of aclass's base classes. The order of merging isn't |
| 2932 | defined, as it's expected that only the final set of dict keys is |
| 2933 | interesting. |
| 2934 | Return 0 on success, -1 on error. |
| 2935 | */ |
| 2936 | |
| 2937 | static int |
| 2938 | merge_class_dict(PyObject *dict, PyObject *aclass) |
| 2939 | { |
| 2940 | PyObject *classdict; |
| 2941 | PyObject *bases; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2942 | _Py_IDENTIFIER(__bases__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2943 | |
| 2944 | assert(PyDict_Check(dict)); |
| 2945 | assert(aclass); |
| 2946 | |
| 2947 | /* Merge in the type's dict (if any). */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2948 | classdict = _PyObject_GetAttrId(aclass, &PyId___dict__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2949 | if (classdict == NULL) |
| 2950 | PyErr_Clear(); |
| 2951 | else { |
| 2952 | int status = PyDict_Update(dict, classdict); |
| 2953 | Py_DECREF(classdict); |
| 2954 | if (status < 0) |
| 2955 | return -1; |
| 2956 | } |
| 2957 | |
| 2958 | /* Recursively merge in the base types' (if any) dicts. */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2959 | bases = _PyObject_GetAttrId(aclass, &PyId___bases__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 2960 | if (bases == NULL) |
| 2961 | PyErr_Clear(); |
| 2962 | else { |
| 2963 | /* We have no guarantee that bases is a real tuple */ |
| 2964 | Py_ssize_t i, n; |
| 2965 | n = PySequence_Size(bases); /* This better be right */ |
| 2966 | if (n < 0) |
| 2967 | PyErr_Clear(); |
| 2968 | else { |
| 2969 | for (i = 0; i < n; i++) { |
| 2970 | int status; |
| 2971 | PyObject *base = PySequence_GetItem(bases, i); |
| 2972 | if (base == NULL) { |
| 2973 | Py_DECREF(bases); |
| 2974 | return -1; |
| 2975 | } |
| 2976 | status = merge_class_dict(dict, base); |
| 2977 | Py_DECREF(base); |
| 2978 | if (status < 0) { |
| 2979 | Py_DECREF(bases); |
| 2980 | return -1; |
| 2981 | } |
| 2982 | } |
| 2983 | } |
| 2984 | Py_DECREF(bases); |
| 2985 | } |
| 2986 | return 0; |
| 2987 | } |
| 2988 | |
| 2989 | /* __dir__ for type objects: returns __dict__ and __bases__. |
| 2990 | We deliberately don't suck up its __class__, as methods belonging to the |
| 2991 | metaclass would probably be more confusing than helpful. |
| 2992 | */ |
| 2993 | static PyObject * |
| 2994 | type_dir(PyObject *self, PyObject *args) |
| 2995 | { |
| 2996 | PyObject *result = NULL; |
| 2997 | PyObject *dict = PyDict_New(); |
| 2998 | |
| 2999 | if (dict != NULL && merge_class_dict(dict, self) == 0) |
| 3000 | result = PyDict_Keys(dict); |
| 3001 | |
| 3002 | Py_XDECREF(dict); |
| 3003 | return result; |
| 3004 | } |
| 3005 | |
Martin v. Loewis | 4f2f3b6 | 2012-04-24 19:13:57 +0200 | [diff] [blame] | 3006 | static PyObject* |
| 3007 | type_sizeof(PyObject *self, PyObject *args_unused) |
| 3008 | { |
| 3009 | Py_ssize_t size; |
| 3010 | PyTypeObject *type = (PyTypeObject*)self; |
| 3011 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 3012 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
| 3013 | size = sizeof(PyHeapTypeObject); |
| 3014 | if (et->ht_cached_keys) |
| 3015 | size += _PyDict_KeysSize(et->ht_cached_keys); |
| 3016 | } |
| 3017 | else |
| 3018 | size = sizeof(PyTypeObject); |
| 3019 | return PyLong_FromSsize_t(size); |
| 3020 | } |
| 3021 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3022 | static PyMethodDef type_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3023 | {"mro", (PyCFunction)mro_external, METH_NOARGS, |
| 3024 | PyDoc_STR("mro() -> list\nreturn a type's method resolution order")}, |
| 3025 | {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS, |
| 3026 | PyDoc_STR("__subclasses__() -> list of immediate subclasses")}, |
| 3027 | {"__prepare__", (PyCFunction)type_prepare, |
| 3028 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 3029 | PyDoc_STR("__prepare__() -> dict\n" |
| 3030 | "used to create the namespace for the class statement")}, |
| 3031 | {"__instancecheck__", type___instancecheck__, METH_O, |
Benjamin Peterson | fbe56bb | 2011-05-24 12:42:51 -0500 | [diff] [blame] | 3032 | PyDoc_STR("__instancecheck__() -> bool\ncheck if an object is an instance")}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3033 | {"__subclasscheck__", type___subclasscheck__, METH_O, |
Benjamin Peterson | fbe56bb | 2011-05-24 12:42:51 -0500 | [diff] [blame] | 3034 | PyDoc_STR("__subclasscheck__() -> bool\ncheck if a class is a subclass")}, |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3035 | {"__dir__", type_dir, METH_NOARGS, |
Benjamin Peterson | c728412 | 2011-05-24 12:46:15 -0500 | [diff] [blame] | 3036 | PyDoc_STR("__dir__() -> list\nspecialized __dir__ implementation for types")}, |
Martin v. Loewis | 4f2f3b6 | 2012-04-24 19:13:57 +0200 | [diff] [blame] | 3037 | {"__sizeof__", type_sizeof, METH_NOARGS, |
| 3038 | "__sizeof__() -> int\nreturn memory consumption of the type object"}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3039 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3040 | }; |
| 3041 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3042 | PyDoc_STRVAR(type_doc, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 3043 | /* this text signature cannot be accurate yet. will fix. --larry */ |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 3044 | "type(object_or_name, bases, dict)\n" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3045 | "type(object) -> the object's type\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3046 | "type(name, bases, dict) -> a new type"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3047 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3048 | static int |
| 3049 | type_traverse(PyTypeObject *type, visitproc visit, void *arg) |
| 3050 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3051 | /* Because of type_is_gc(), the collector only calls this |
| 3052 | for heaptypes. */ |
Antoine Pitrou | 1351ca6 | 2012-06-24 00:30:12 +0200 | [diff] [blame] | 3053 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 3054 | char msg[200]; |
| 3055 | sprintf(msg, "type_traverse() called for non-heap type '%.100s'", |
| 3056 | type->tp_name); |
| 3057 | Py_FatalError(msg); |
| 3058 | } |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3059 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3060 | Py_VISIT(type->tp_dict); |
| 3061 | Py_VISIT(type->tp_cache); |
| 3062 | Py_VISIT(type->tp_mro); |
| 3063 | Py_VISIT(type->tp_bases); |
| 3064 | Py_VISIT(type->tp_base); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 3065 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3066 | /* There's no need to visit type->tp_subclasses or |
| 3067 | ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved |
| 3068 | in cycles; tp_subclasses is a list of weak references, |
| 3069 | and slots is a tuple of strings. */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3070 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3071 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3072 | } |
| 3073 | |
| 3074 | static int |
| 3075 | type_clear(PyTypeObject *type) |
| 3076 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3077 | PyDictKeysObject *cached_keys; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3078 | /* Because of type_is_gc(), the collector only calls this |
| 3079 | for heaptypes. */ |
| 3080 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3081 | |
Antoine Pitrou | 2e87208 | 2011-12-15 14:15:31 +0100 | [diff] [blame] | 3082 | /* We need to invalidate the method cache carefully before clearing |
| 3083 | the dict, so that other objects caught in a reference cycle |
| 3084 | don't start calling destroyed methods. |
| 3085 | |
| 3086 | Otherwise, the only field we need to clear is tp_mro, which is |
| 3087 | part of a hard cycle (its first element is the class itself) that |
| 3088 | won't be broken otherwise (it's a tuple and tuples don't have a |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3089 | tp_clear handler). None of the other fields need to be |
| 3090 | cleared, and here's why: |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3091 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3092 | tp_cache: |
| 3093 | Not used; if it were, it would be a dict. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 3094 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3095 | tp_bases, tp_base: |
| 3096 | If these are involved in a cycle, there must be at least |
| 3097 | one other, mutable object in the cycle, e.g. a base |
| 3098 | class's dict; the cycle will be broken that way. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 3099 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3100 | tp_subclasses: |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 3101 | A dict of weak references can't be part of a cycle; and |
| 3102 | dicts have their own tp_clear. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 3103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3104 | slots (in PyHeapTypeObject): |
| 3105 | A tuple of strings can't be part of a cycle. |
| 3106 | */ |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 3107 | |
Antoine Pitrou | 2e87208 | 2011-12-15 14:15:31 +0100 | [diff] [blame] | 3108 | PyType_Modified(type); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3109 | cached_keys = ((PyHeapTypeObject *)type)->ht_cached_keys; |
| 3110 | if (cached_keys != NULL) { |
| 3111 | ((PyHeapTypeObject *)type)->ht_cached_keys = NULL; |
| 3112 | _PyDictKeys_DecRef(cached_keys); |
| 3113 | } |
Antoine Pitrou | 2e87208 | 2011-12-15 14:15:31 +0100 | [diff] [blame] | 3114 | if (type->tp_dict) |
| 3115 | PyDict_Clear(type->tp_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3116 | Py_CLEAR(type->tp_mro); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3117 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3118 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3119 | } |
| 3120 | |
| 3121 | static int |
| 3122 | type_is_gc(PyTypeObject *type) |
| 3123 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3124 | return type->tp_flags & Py_TPFLAGS_HEAPTYPE; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3125 | } |
| 3126 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3127 | PyTypeObject PyType_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3128 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3129 | "type", /* tp_name */ |
| 3130 | sizeof(PyHeapTypeObject), /* tp_basicsize */ |
| 3131 | sizeof(PyMemberDef), /* tp_itemsize */ |
| 3132 | (destructor)type_dealloc, /* tp_dealloc */ |
| 3133 | 0, /* tp_print */ |
| 3134 | 0, /* tp_getattr */ |
| 3135 | 0, /* tp_setattr */ |
| 3136 | 0, /* tp_reserved */ |
| 3137 | (reprfunc)type_repr, /* tp_repr */ |
| 3138 | 0, /* tp_as_number */ |
| 3139 | 0, /* tp_as_sequence */ |
| 3140 | 0, /* tp_as_mapping */ |
| 3141 | 0, /* tp_hash */ |
| 3142 | (ternaryfunc)type_call, /* tp_call */ |
| 3143 | 0, /* tp_str */ |
| 3144 | (getattrofunc)type_getattro, /* tp_getattro */ |
| 3145 | (setattrofunc)type_setattro, /* tp_setattro */ |
| 3146 | 0, /* tp_as_buffer */ |
| 3147 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 3148 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */ |
| 3149 | type_doc, /* tp_doc */ |
| 3150 | (traverseproc)type_traverse, /* tp_traverse */ |
| 3151 | (inquiry)type_clear, /* tp_clear */ |
| 3152 | 0, /* tp_richcompare */ |
| 3153 | offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ |
| 3154 | 0, /* tp_iter */ |
| 3155 | 0, /* tp_iternext */ |
| 3156 | type_methods, /* tp_methods */ |
| 3157 | type_members, /* tp_members */ |
| 3158 | type_getsets, /* tp_getset */ |
| 3159 | 0, /* tp_base */ |
| 3160 | 0, /* tp_dict */ |
| 3161 | 0, /* tp_descr_get */ |
| 3162 | 0, /* tp_descr_set */ |
| 3163 | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ |
| 3164 | type_init, /* tp_init */ |
| 3165 | 0, /* tp_alloc */ |
| 3166 | type_new, /* tp_new */ |
| 3167 | PyObject_GC_Del, /* tp_free */ |
| 3168 | (inquiry)type_is_gc, /* tp_is_gc */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3169 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3170 | |
| 3171 | |
| 3172 | /* The base type of all types (eventually)... except itself. */ |
| 3173 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3174 | /* You may wonder why object.__new__() only complains about arguments |
| 3175 | when object.__init__() is not overridden, and vice versa. |
| 3176 | |
| 3177 | Consider the use cases: |
| 3178 | |
| 3179 | 1. When neither is overridden, we want to hear complaints about |
| 3180 | excess (i.e., any) arguments, since their presence could |
| 3181 | indicate there's a bug. |
| 3182 | |
| 3183 | 2. When defining an Immutable type, we are likely to override only |
| 3184 | __new__(), since __init__() is called too late to initialize an |
| 3185 | Immutable object. Since __new__() defines the signature for the |
| 3186 | type, it would be a pain to have to override __init__() just to |
| 3187 | stop it from complaining about excess arguments. |
| 3188 | |
| 3189 | 3. When defining a Mutable type, we are likely to override only |
| 3190 | __init__(). So here the converse reasoning applies: we don't |
| 3191 | want to have to override __new__() just to stop it from |
| 3192 | complaining. |
| 3193 | |
| 3194 | 4. When __init__() is overridden, and the subclass __init__() calls |
| 3195 | object.__init__(), the latter should complain about excess |
| 3196 | arguments; ditto for __new__(). |
| 3197 | |
| 3198 | Use cases 2 and 3 make it unattractive to unconditionally check for |
| 3199 | excess arguments. The best solution that addresses all four use |
| 3200 | cases is as follows: __init__() complains about excess arguments |
| 3201 | unless __new__() is overridden and __init__() is not overridden |
| 3202 | (IOW, if __init__() is overridden or __new__() is not overridden); |
| 3203 | symmetrically, __new__() complains about excess arguments unless |
| 3204 | __init__() is overridden and __new__() is not overridden |
| 3205 | (IOW, if __new__() is overridden or __init__() is not overridden). |
| 3206 | |
| 3207 | However, for backwards compatibility, this breaks too much code. |
| 3208 | Therefore, in 2.6, we'll *warn* about excess arguments when both |
| 3209 | methods are overridden; for all other cases we'll use the above |
| 3210 | rules. |
| 3211 | |
| 3212 | */ |
| 3213 | |
| 3214 | /* Forward */ |
| 3215 | static PyObject * |
| 3216 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 3217 | |
| 3218 | static int |
| 3219 | excess_args(PyObject *args, PyObject *kwds) |
| 3220 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3221 | return PyTuple_GET_SIZE(args) || |
| 3222 | (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3223 | } |
| 3224 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3225 | static int |
| 3226 | object_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 3227 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3228 | int err = 0; |
Benjamin Peterson | 96384b9 | 2012-03-17 00:05:44 -0500 | [diff] [blame] | 3229 | PyTypeObject *type = Py_TYPE(self); |
| 3230 | if (excess_args(args, kwds) && |
| 3231 | (type->tp_new == object_new || type->tp_init != object_init)) { |
| 3232 | PyErr_SetString(PyExc_TypeError, "object.__init__() takes no parameters"); |
| 3233 | err = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3234 | } |
| 3235 | return err; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3236 | } |
| 3237 | |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 3238 | static PyObject * |
| 3239 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3240 | { |
Benjamin Peterson | 96384b9 | 2012-03-17 00:05:44 -0500 | [diff] [blame] | 3241 | if (excess_args(args, kwds) && |
| 3242 | (type->tp_init == object_init || type->tp_new != object_new)) { |
R David Murray | 702a5dc | 2013-02-18 21:39:18 -0500 | [diff] [blame] | 3243 | PyErr_SetString(PyExc_TypeError, "object() takes no parameters"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3244 | return NULL; |
Benjamin Peterson | 96384b9 | 2012-03-17 00:05:44 -0500 | [diff] [blame] | 3245 | } |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3247 | if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3248 | PyObject *abstract_methods = NULL; |
| 3249 | PyObject *builtins; |
| 3250 | PyObject *sorted; |
| 3251 | PyObject *sorted_methods = NULL; |
| 3252 | PyObject *joined = NULL; |
Benjamin Peterson | 9a03ecf | 2012-03-16 20:15:54 -0500 | [diff] [blame] | 3253 | PyObject *comma; |
| 3254 | _Py_static_string(comma_id, ", "); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3255 | _Py_IDENTIFIER(sorted); |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3256 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3257 | /* Compute ", ".join(sorted(type.__abstractmethods__)) |
| 3258 | into joined. */ |
| 3259 | abstract_methods = type_abstractmethods(type, NULL); |
| 3260 | if (abstract_methods == NULL) |
| 3261 | goto error; |
| 3262 | builtins = PyEval_GetBuiltins(); |
| 3263 | if (builtins == NULL) |
| 3264 | goto error; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3265 | sorted = _PyDict_GetItemId(builtins, &PyId_sorted); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3266 | if (sorted == NULL) |
| 3267 | goto error; |
| 3268 | sorted_methods = PyObject_CallFunctionObjArgs(sorted, |
| 3269 | abstract_methods, |
| 3270 | NULL); |
| 3271 | if (sorted_methods == NULL) |
| 3272 | goto error; |
Benjamin Peterson | 9a03ecf | 2012-03-16 20:15:54 -0500 | [diff] [blame] | 3273 | comma = _PyUnicode_FromId(&comma_id); |
| 3274 | if (comma == NULL) |
| 3275 | goto error; |
| 3276 | joined = PyUnicode_Join(comma, sorted_methods); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3277 | if (joined == NULL) |
| 3278 | goto error; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3280 | PyErr_Format(PyExc_TypeError, |
| 3281 | "Can't instantiate abstract class %s " |
| 3282 | "with abstract methods %U", |
| 3283 | type->tp_name, |
| 3284 | joined); |
| 3285 | error: |
| 3286 | Py_XDECREF(joined); |
| 3287 | Py_XDECREF(sorted_methods); |
| 3288 | Py_XDECREF(abstract_methods); |
| 3289 | return NULL; |
| 3290 | } |
| 3291 | return type->tp_alloc(type, 0); |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3294 | static void |
| 3295 | object_dealloc(PyObject *self) |
| 3296 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3297 | Py_TYPE(self)->tp_free(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3298 | } |
| 3299 | |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 3300 | static PyObject * |
| 3301 | object_repr(PyObject *self) |
| 3302 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3303 | PyTypeObject *type; |
| 3304 | PyObject *mod, *name, *rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 3305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3306 | type = Py_TYPE(self); |
| 3307 | mod = type_module(type, NULL); |
| 3308 | if (mod == NULL) |
| 3309 | PyErr_Clear(); |
| 3310 | else if (!PyUnicode_Check(mod)) { |
| 3311 | Py_DECREF(mod); |
| 3312 | mod = NULL; |
| 3313 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 3314 | name = type_qualname(type, NULL); |
Christian Heimes | e81dc29 | 2012-09-10 16:57:36 +0200 | [diff] [blame] | 3315 | if (name == NULL) { |
| 3316 | Py_XDECREF(mod); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3317 | return NULL; |
Christian Heimes | e81dc29 | 2012-09-10 16:57:36 +0200 | [diff] [blame] | 3318 | } |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 3319 | if (mod != NULL && _PyUnicode_CompareWithId(mod, &PyId_builtins)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3320 | rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self); |
| 3321 | else |
| 3322 | rtn = PyUnicode_FromFormat("<%s object at %p>", |
| 3323 | type->tp_name, self); |
| 3324 | Py_XDECREF(mod); |
| 3325 | Py_DECREF(name); |
| 3326 | return rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3329 | static PyObject * |
| 3330 | object_str(PyObject *self) |
| 3331 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3332 | unaryfunc f; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3334 | f = Py_TYPE(self)->tp_repr; |
Benjamin Peterson | 7b16687 | 2012-04-24 11:06:25 -0400 | [diff] [blame] | 3335 | if (f == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3336 | f = object_repr; |
| 3337 | return f(self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3340 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3341 | object_richcompare(PyObject *self, PyObject *other, int op) |
| 3342 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3343 | PyObject *res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3345 | switch (op) { |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3346 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3347 | case Py_EQ: |
| 3348 | /* Return NotImplemented instead of False, so if two |
| 3349 | objects are compared, both get a chance at the |
| 3350 | comparison. See issue #1393. */ |
| 3351 | res = (self == other) ? Py_True : Py_NotImplemented; |
| 3352 | Py_INCREF(res); |
| 3353 | break; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3355 | case Py_NE: |
| 3356 | /* By default, != returns the opposite of ==, |
| 3357 | unless the latter returns NotImplemented. */ |
| 3358 | res = PyObject_RichCompare(self, other, Py_EQ); |
| 3359 | if (res != NULL && res != Py_NotImplemented) { |
| 3360 | int ok = PyObject_IsTrue(res); |
| 3361 | Py_DECREF(res); |
| 3362 | if (ok < 0) |
| 3363 | res = NULL; |
| 3364 | else { |
| 3365 | if (ok) |
| 3366 | res = Py_False; |
| 3367 | else |
| 3368 | res = Py_True; |
| 3369 | Py_INCREF(res); |
| 3370 | } |
| 3371 | } |
| 3372 | break; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3374 | default: |
| 3375 | res = Py_NotImplemented; |
| 3376 | Py_INCREF(res); |
| 3377 | break; |
| 3378 | } |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3380 | return res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3381 | } |
| 3382 | |
| 3383 | static PyObject * |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3384 | object_get_class(PyObject *self, void *closure) |
| 3385 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3386 | Py_INCREF(Py_TYPE(self)); |
| 3387 | return (PyObject *)(Py_TYPE(self)); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
| 3390 | static int |
| 3391 | equiv_structs(PyTypeObject *a, PyTypeObject *b) |
| 3392 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3393 | return a == b || |
| 3394 | (a != NULL && |
| 3395 | b != NULL && |
| 3396 | a->tp_basicsize == b->tp_basicsize && |
| 3397 | a->tp_itemsize == b->tp_itemsize && |
| 3398 | a->tp_dictoffset == b->tp_dictoffset && |
| 3399 | a->tp_weaklistoffset == b->tp_weaklistoffset && |
| 3400 | ((a->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 3401 | (b->tp_flags & Py_TPFLAGS_HAVE_GC))); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3402 | } |
| 3403 | |
| 3404 | static int |
| 3405 | same_slots_added(PyTypeObject *a, PyTypeObject *b) |
| 3406 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3407 | PyTypeObject *base = a->tp_base; |
| 3408 | Py_ssize_t size; |
| 3409 | PyObject *slots_a, *slots_b; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3410 | |
Benjamin Peterson | 67641d2 | 2011-01-17 19:24:34 +0000 | [diff] [blame] | 3411 | assert(base == b->tp_base); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3412 | size = base->tp_basicsize; |
| 3413 | if (a->tp_dictoffset == size && b->tp_dictoffset == size) |
| 3414 | size += sizeof(PyObject *); |
| 3415 | if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size) |
| 3416 | size += sizeof(PyObject *); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3417 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3418 | /* Check slots compliance */ |
| 3419 | slots_a = ((PyHeapTypeObject *)a)->ht_slots; |
| 3420 | slots_b = ((PyHeapTypeObject *)b)->ht_slots; |
| 3421 | if (slots_a && slots_b) { |
| 3422 | if (PyObject_RichCompareBool(slots_a, slots_b, Py_EQ) != 1) |
| 3423 | return 0; |
| 3424 | size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a); |
| 3425 | } |
| 3426 | return size == a->tp_basicsize && size == b->tp_basicsize; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3427 | } |
| 3428 | |
| 3429 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3430 | compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3431 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3432 | PyTypeObject *newbase, *oldbase; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3434 | if (newto->tp_dealloc != oldto->tp_dealloc || |
| 3435 | newto->tp_free != oldto->tp_free) |
| 3436 | { |
| 3437 | PyErr_Format(PyExc_TypeError, |
| 3438 | "%s assignment: " |
| 3439 | "'%s' deallocator differs from '%s'", |
| 3440 | attr, |
| 3441 | newto->tp_name, |
| 3442 | oldto->tp_name); |
| 3443 | return 0; |
| 3444 | } |
| 3445 | newbase = newto; |
| 3446 | oldbase = oldto; |
| 3447 | while (equiv_structs(newbase, newbase->tp_base)) |
| 3448 | newbase = newbase->tp_base; |
| 3449 | while (equiv_structs(oldbase, oldbase->tp_base)) |
| 3450 | oldbase = oldbase->tp_base; |
| 3451 | if (newbase != oldbase && |
| 3452 | (newbase->tp_base != oldbase->tp_base || |
| 3453 | !same_slots_added(newbase, oldbase))) { |
| 3454 | PyErr_Format(PyExc_TypeError, |
| 3455 | "%s assignment: " |
| 3456 | "'%s' object layout differs from '%s'", |
| 3457 | attr, |
| 3458 | newto->tp_name, |
| 3459 | oldto->tp_name); |
| 3460 | return 0; |
| 3461 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 3462 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3463 | return 1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3464 | } |
| 3465 | |
| 3466 | static int |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3467 | object_set_class(PyObject *self, PyObject *value, void *closure) |
| 3468 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3469 | PyTypeObject *oldto = Py_TYPE(self); |
| 3470 | PyTypeObject *newto; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3471 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3472 | if (value == NULL) { |
| 3473 | PyErr_SetString(PyExc_TypeError, |
| 3474 | "can't delete __class__ attribute"); |
| 3475 | return -1; |
| 3476 | } |
| 3477 | if (!PyType_Check(value)) { |
| 3478 | PyErr_Format(PyExc_TypeError, |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 3479 | "__class__ must be set to a class, not '%s' object", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3480 | Py_TYPE(value)->tp_name); |
| 3481 | return -1; |
| 3482 | } |
| 3483 | newto = (PyTypeObject *)value; |
| 3484 | if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) || |
| 3485 | !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 3486 | { |
| 3487 | PyErr_Format(PyExc_TypeError, |
| 3488 | "__class__ assignment: only for heap types"); |
| 3489 | return -1; |
| 3490 | } |
Christian Heimes | de4d183 | 2013-07-20 14:19:46 +0200 | [diff] [blame] | 3491 | if (compatible_for_assignment(oldto, newto, "__class__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3492 | Py_INCREF(newto); |
| 3493 | Py_TYPE(self) = newto; |
| 3494 | Py_DECREF(oldto); |
| 3495 | return 0; |
| 3496 | } |
| 3497 | else { |
| 3498 | return -1; |
| 3499 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3500 | } |
| 3501 | |
| 3502 | static PyGetSetDef object_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3503 | {"__class__", object_get_class, object_set_class, |
| 3504 | PyDoc_STR("the object's class")}, |
| 3505 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3506 | }; |
| 3507 | |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 3508 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3509 | /* Stuff to implement __reduce_ex__ for pickle protocols >= 2. |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3510 | We fall back to helpers in copyreg for: |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3511 | - pickle protocols < 2 |
| 3512 | - calculating the list of slot names (done only once per class) |
| 3513 | - the __newobj__ function (which is used as a token but never called) |
| 3514 | */ |
| 3515 | |
| 3516 | static PyObject * |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 3517 | import_copyreg(void) |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3518 | { |
Alexandre Vassalotti | 1a83070 | 2013-11-30 00:53:09 -0800 | [diff] [blame] | 3519 | PyObject *copyreg_str; |
| 3520 | PyObject *copyreg_module; |
| 3521 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3522 | _Py_IDENTIFIER(copyreg); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 3523 | |
Alexandre Vassalotti | 1a83070 | 2013-11-30 00:53:09 -0800 | [diff] [blame] | 3524 | copyreg_str = _PyUnicode_FromId(&PyId_copyreg); |
| 3525 | if (copyreg_str == NULL) { |
| 3526 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3527 | } |
Alexandre Vassalotti | 1a83070 | 2013-11-30 00:53:09 -0800 | [diff] [blame] | 3528 | /* Try to fetch cached copy of copyreg from sys.modules first in an |
| 3529 | attempt to avoid the import overhead. Previously this was implemented |
| 3530 | by storing a reference to the cached module in a static variable, but |
| 3531 | this broke when multiple embeded interpreters were in use (see issue |
| 3532 | #17408 and #19088). */ |
| 3533 | copyreg_module = PyDict_GetItemWithError(interp->modules, copyreg_str); |
| 3534 | if (copyreg_module != NULL) { |
| 3535 | Py_INCREF(copyreg_module); |
| 3536 | return copyreg_module; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3537 | } |
Alexandre Vassalotti | 1a83070 | 2013-11-30 00:53:09 -0800 | [diff] [blame] | 3538 | if (PyErr_Occurred()) { |
| 3539 | return NULL; |
| 3540 | } |
| 3541 | return PyImport_Import(copyreg_str); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3544 | Py_LOCAL(PyObject *) |
| 3545 | _PyType_GetSlotNames(PyTypeObject *cls) |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3546 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3547 | PyObject *copyreg; |
| 3548 | PyObject *slotnames; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3549 | _Py_IDENTIFIER(__slotnames__); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3550 | _Py_IDENTIFIER(_slotnames); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3551 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3552 | assert(PyType_Check(cls)); |
| 3553 | |
| 3554 | /* Get the slot names from the cache in the class if possible. */ |
| 3555 | slotnames = _PyDict_GetItemIdWithError(cls->tp_dict, &PyId___slotnames__); |
| 3556 | if (slotnames != NULL) { |
| 3557 | if (slotnames != Py_None && !PyList_Check(slotnames)) { |
| 3558 | PyErr_Format(PyExc_TypeError, |
| 3559 | "%.200s.__slotnames__ should be a list or None, " |
| 3560 | "not %.200s", |
| 3561 | cls->tp_name, Py_TYPE(slotnames)->tp_name); |
| 3562 | return NULL; |
| 3563 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3564 | Py_INCREF(slotnames); |
| 3565 | return slotnames; |
| 3566 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3567 | else { |
| 3568 | if (PyErr_Occurred()) { |
| 3569 | return NULL; |
| 3570 | } |
| 3571 | /* The class does not have the slot names cached yet. */ |
| 3572 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3573 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3574 | copyreg = import_copyreg(); |
| 3575 | if (copyreg == NULL) |
| 3576 | return NULL; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3577 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3578 | /* Use _slotnames function from the copyreg module to find the slots |
| 3579 | by this class and its bases. This function will cache the result |
| 3580 | in __slotnames__. */ |
| 3581 | slotnames = _PyObject_CallMethodIdObjArgs(copyreg, &PyId__slotnames, |
| 3582 | cls, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3583 | Py_DECREF(copyreg); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3584 | if (slotnames == NULL) |
| 3585 | return NULL; |
| 3586 | |
| 3587 | if (slotnames != Py_None && !PyList_Check(slotnames)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3588 | PyErr_SetString(PyExc_TypeError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3589 | "copyreg._slotnames didn't return a list or None"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3590 | Py_DECREF(slotnames); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3591 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3592 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3593 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3594 | return slotnames; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3595 | } |
| 3596 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3597 | Py_LOCAL(PyObject *) |
| 3598 | _PyObject_GetState(PyObject *obj) |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3599 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3600 | PyObject *state; |
| 3601 | PyObject *getstate; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3602 | _Py_IDENTIFIER(__getstate__); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3603 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3604 | getstate = _PyObject_GetAttrId(obj, &PyId___getstate__); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3605 | if (getstate == NULL) { |
| 3606 | PyObject *slotnames; |
| 3607 | |
| 3608 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 3609 | return NULL; |
| 3610 | } |
| 3611 | PyErr_Clear(); |
| 3612 | |
| 3613 | { |
| 3614 | PyObject **dict; |
| 3615 | dict = _PyObject_GetDictPtr(obj); |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 3616 | /* It is possible that the object's dict is not initialized |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3617 | yet. In this case, we will return None for the state. |
| 3618 | We also return None if the dict is empty to make the behavior |
| 3619 | consistent regardless whether the dict was initialized or not. |
| 3620 | This make unit testing easier. */ |
| 3621 | if (dict != NULL && *dict != NULL && PyDict_Size(*dict) > 0) { |
| 3622 | state = *dict; |
| 3623 | } |
| 3624 | else { |
| 3625 | state = Py_None; |
| 3626 | } |
| 3627 | Py_INCREF(state); |
| 3628 | } |
| 3629 | |
| 3630 | slotnames = _PyType_GetSlotNames(Py_TYPE(obj)); |
| 3631 | if (slotnames == NULL) { |
| 3632 | Py_DECREF(state); |
| 3633 | return NULL; |
| 3634 | } |
| 3635 | |
| 3636 | assert(slotnames == Py_None || PyList_Check(slotnames)); |
| 3637 | if (slotnames != Py_None && Py_SIZE(slotnames) > 0) { |
| 3638 | PyObject *slots; |
| 3639 | Py_ssize_t slotnames_size, i; |
| 3640 | |
| 3641 | slots = PyDict_New(); |
| 3642 | if (slots == NULL) { |
| 3643 | Py_DECREF(slotnames); |
| 3644 | Py_DECREF(state); |
| 3645 | return NULL; |
| 3646 | } |
| 3647 | |
| 3648 | slotnames_size = Py_SIZE(slotnames); |
| 3649 | for (i = 0; i < slotnames_size; i++) { |
| 3650 | PyObject *name, *value; |
| 3651 | |
| 3652 | name = PyList_GET_ITEM(slotnames, i); |
| 3653 | value = PyObject_GetAttr(obj, name); |
| 3654 | if (value == NULL) { |
| 3655 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 3656 | goto error; |
| 3657 | } |
| 3658 | /* It is not an error if the attribute is not present. */ |
| 3659 | PyErr_Clear(); |
| 3660 | } |
| 3661 | else { |
| 3662 | int err = PyDict_SetItem(slots, name, value); |
| 3663 | Py_DECREF(value); |
| 3664 | if (err) { |
| 3665 | goto error; |
| 3666 | } |
| 3667 | } |
| 3668 | |
| 3669 | /* The list is stored on the class so it may mutates while we |
| 3670 | iterate over it */ |
| 3671 | if (slotnames_size != Py_SIZE(slotnames)) { |
| 3672 | PyErr_Format(PyExc_RuntimeError, |
| 3673 | "__slotsname__ changed size during iteration"); |
| 3674 | goto error; |
| 3675 | } |
| 3676 | |
| 3677 | /* We handle errors within the loop here. */ |
| 3678 | if (0) { |
| 3679 | error: |
| 3680 | Py_DECREF(slotnames); |
| 3681 | Py_DECREF(slots); |
| 3682 | Py_DECREF(state); |
| 3683 | return NULL; |
| 3684 | } |
| 3685 | } |
| 3686 | |
| 3687 | /* If we found some slot attributes, pack them in a tuple along |
| 3688 | the orginal attribute dictionary. */ |
| 3689 | if (PyDict_Size(slots) > 0) { |
| 3690 | PyObject *state2; |
| 3691 | |
| 3692 | state2 = PyTuple_Pack(2, state, slots); |
| 3693 | Py_DECREF(state); |
| 3694 | if (state2 == NULL) { |
| 3695 | Py_DECREF(slotnames); |
| 3696 | Py_DECREF(slots); |
| 3697 | return NULL; |
| 3698 | } |
| 3699 | state = state2; |
| 3700 | } |
| 3701 | Py_DECREF(slots); |
| 3702 | } |
| 3703 | Py_DECREF(slotnames); |
| 3704 | } |
| 3705 | else { /* getstate != NULL */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3706 | state = PyObject_CallObject(getstate, NULL); |
| 3707 | Py_DECREF(getstate); |
| 3708 | if (state == NULL) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3709 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3710 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3711 | |
| 3712 | return state; |
| 3713 | } |
| 3714 | |
| 3715 | Py_LOCAL(int) |
| 3716 | _PyObject_GetNewArguments(PyObject *obj, PyObject **args, PyObject **kwargs) |
| 3717 | { |
| 3718 | PyObject *getnewargs, *getnewargs_ex; |
| 3719 | _Py_IDENTIFIER(__getnewargs_ex__); |
| 3720 | _Py_IDENTIFIER(__getnewargs__); |
| 3721 | |
| 3722 | if (args == NULL || kwargs == NULL) { |
| 3723 | PyErr_BadInternalCall(); |
| 3724 | return -1; |
| 3725 | } |
| 3726 | |
| 3727 | /* We first attempt to fetch the arguments for __new__ by calling |
| 3728 | __getnewargs_ex__ on the object. */ |
Benjamin Peterson | e686c5c | 2014-02-16 13:49:16 -0500 | [diff] [blame] | 3729 | getnewargs_ex = _PyObject_LookupSpecial(obj, &PyId___getnewargs_ex__); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3730 | if (getnewargs_ex != NULL) { |
| 3731 | PyObject *newargs = PyObject_CallObject(getnewargs_ex, NULL); |
| 3732 | Py_DECREF(getnewargs_ex); |
| 3733 | if (newargs == NULL) { |
| 3734 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3735 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3736 | if (!PyTuple_Check(newargs)) { |
| 3737 | PyErr_Format(PyExc_TypeError, |
| 3738 | "__getnewargs_ex__ should return a tuple, " |
| 3739 | "not '%.200s'", Py_TYPE(newargs)->tp_name); |
| 3740 | Py_DECREF(newargs); |
| 3741 | return -1; |
| 3742 | } |
| 3743 | if (Py_SIZE(newargs) != 2) { |
| 3744 | PyErr_Format(PyExc_ValueError, |
| 3745 | "__getnewargs_ex__ should return a tuple of " |
| 3746 | "length 2, not %zd", Py_SIZE(newargs)); |
| 3747 | Py_DECREF(newargs); |
| 3748 | return -1; |
| 3749 | } |
| 3750 | *args = PyTuple_GET_ITEM(newargs, 0); |
| 3751 | Py_INCREF(*args); |
| 3752 | *kwargs = PyTuple_GET_ITEM(newargs, 1); |
| 3753 | Py_INCREF(*kwargs); |
| 3754 | Py_DECREF(newargs); |
| 3755 | |
| 3756 | /* XXX We should perhaps allow None to be passed here. */ |
| 3757 | if (!PyTuple_Check(*args)) { |
| 3758 | PyErr_Format(PyExc_TypeError, |
| 3759 | "first item of the tuple returned by " |
| 3760 | "__getnewargs_ex__ must be a tuple, not '%.200s'", |
| 3761 | Py_TYPE(*args)->tp_name); |
| 3762 | Py_CLEAR(*args); |
| 3763 | Py_CLEAR(*kwargs); |
| 3764 | return -1; |
| 3765 | } |
| 3766 | if (!PyDict_Check(*kwargs)) { |
| 3767 | PyErr_Format(PyExc_TypeError, |
| 3768 | "second item of the tuple returned by " |
| 3769 | "__getnewargs_ex__ must be a dict, not '%.200s'", |
| 3770 | Py_TYPE(*kwargs)->tp_name); |
| 3771 | Py_CLEAR(*args); |
| 3772 | Py_CLEAR(*kwargs); |
| 3773 | return -1; |
| 3774 | } |
| 3775 | return 0; |
Benjamin Peterson | e686c5c | 2014-02-16 13:49:16 -0500 | [diff] [blame] | 3776 | } else if (PyErr_Occurred()) { |
| 3777 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3778 | } |
| 3779 | |
| 3780 | /* The object does not have __getnewargs_ex__ so we fallback on using |
| 3781 | __getnewargs__ instead. */ |
Benjamin Peterson | e686c5c | 2014-02-16 13:49:16 -0500 | [diff] [blame] | 3782 | getnewargs = _PyObject_LookupSpecial(obj, &PyId___getnewargs__); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3783 | if (getnewargs != NULL) { |
| 3784 | *args = PyObject_CallObject(getnewargs, NULL); |
| 3785 | Py_DECREF(getnewargs); |
| 3786 | if (*args == NULL) { |
| 3787 | return -1; |
| 3788 | } |
| 3789 | if (!PyTuple_Check(*args)) { |
| 3790 | PyErr_Format(PyExc_TypeError, |
| 3791 | "__getnewargs__ should return a tuple, " |
| 3792 | "not '%.200s'", Py_TYPE(*args)->tp_name); |
| 3793 | Py_CLEAR(*args); |
| 3794 | return -1; |
| 3795 | } |
| 3796 | *kwargs = NULL; |
| 3797 | return 0; |
Benjamin Peterson | e686c5c | 2014-02-16 13:49:16 -0500 | [diff] [blame] | 3798 | } else if (PyErr_Occurred()) { |
| 3799 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3800 | } |
| 3801 | |
| 3802 | /* The object does not have __getnewargs_ex__ and __getnewargs__. This may |
| 3803 | means __new__ does not takes any arguments on this object, or that the |
| 3804 | object does not implement the reduce protocol for pickling or |
| 3805 | copying. */ |
| 3806 | *args = NULL; |
| 3807 | *kwargs = NULL; |
| 3808 | return 0; |
| 3809 | } |
| 3810 | |
| 3811 | Py_LOCAL(int) |
| 3812 | _PyObject_GetItemsIter(PyObject *obj, PyObject **listitems, |
| 3813 | PyObject **dictitems) |
| 3814 | { |
| 3815 | if (listitems == NULL || dictitems == NULL) { |
| 3816 | PyErr_BadInternalCall(); |
| 3817 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3818 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3820 | if (!PyList_Check(obj)) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3821 | *listitems = Py_None; |
| 3822 | Py_INCREF(*listitems); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3823 | } |
| 3824 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3825 | *listitems = PyObject_GetIter(obj); |
Christian Heimes | 2489bd8 | 2013-11-23 21:19:43 +0100 | [diff] [blame] | 3826 | if (*listitems == NULL) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3827 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3828 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3829 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3830 | if (!PyDict_Check(obj)) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3831 | *dictitems = Py_None; |
| 3832 | Py_INCREF(*dictitems); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3833 | } |
| 3834 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3835 | PyObject *items; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3836 | _Py_IDENTIFIER(items); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3837 | |
| 3838 | items = _PyObject_CallMethodIdObjArgs(obj, &PyId_items, NULL); |
| 3839 | if (items == NULL) { |
| 3840 | Py_CLEAR(*listitems); |
| 3841 | return -1; |
| 3842 | } |
| 3843 | *dictitems = PyObject_GetIter(items); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3844 | Py_DECREF(items); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3845 | if (*dictitems == NULL) { |
| 3846 | Py_CLEAR(*listitems); |
| 3847 | return -1; |
| 3848 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3849 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3850 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3851 | assert(*listitems != NULL && *dictitems != NULL); |
| 3852 | |
| 3853 | return 0; |
| 3854 | } |
| 3855 | |
| 3856 | static PyObject * |
| 3857 | reduce_4(PyObject *obj) |
| 3858 | { |
| 3859 | PyObject *args = NULL, *kwargs = NULL; |
| 3860 | PyObject *copyreg; |
| 3861 | PyObject *newobj, *newargs, *state, *listitems, *dictitems; |
| 3862 | PyObject *result; |
| 3863 | _Py_IDENTIFIER(__newobj_ex__); |
| 3864 | |
| 3865 | if (_PyObject_GetNewArguments(obj, &args, &kwargs) < 0) { |
| 3866 | return NULL; |
| 3867 | } |
| 3868 | if (args == NULL) { |
| 3869 | args = PyTuple_New(0); |
| 3870 | if (args == NULL) |
| 3871 | return NULL; |
| 3872 | } |
| 3873 | if (kwargs == NULL) { |
| 3874 | kwargs = PyDict_New(); |
| 3875 | if (kwargs == NULL) |
| 3876 | return NULL; |
| 3877 | } |
| 3878 | |
| 3879 | copyreg = import_copyreg(); |
| 3880 | if (copyreg == NULL) { |
| 3881 | Py_DECREF(args); |
| 3882 | Py_DECREF(kwargs); |
| 3883 | return NULL; |
| 3884 | } |
| 3885 | newobj = _PyObject_GetAttrId(copyreg, &PyId___newobj_ex__); |
| 3886 | Py_DECREF(copyreg); |
| 3887 | if (newobj == NULL) { |
| 3888 | Py_DECREF(args); |
| 3889 | Py_DECREF(kwargs); |
| 3890 | return NULL; |
| 3891 | } |
| 3892 | newargs = PyTuple_Pack(3, Py_TYPE(obj), args, kwargs); |
| 3893 | Py_DECREF(args); |
| 3894 | Py_DECREF(kwargs); |
| 3895 | if (newargs == NULL) { |
| 3896 | Py_DECREF(newobj); |
| 3897 | return NULL; |
| 3898 | } |
| 3899 | state = _PyObject_GetState(obj); |
| 3900 | if (state == NULL) { |
| 3901 | Py_DECREF(newobj); |
| 3902 | Py_DECREF(newargs); |
| 3903 | return NULL; |
| 3904 | } |
| 3905 | if (_PyObject_GetItemsIter(obj, &listitems, &dictitems) < 0) { |
| 3906 | Py_DECREF(newobj); |
| 3907 | Py_DECREF(newargs); |
| 3908 | Py_DECREF(state); |
| 3909 | return NULL; |
| 3910 | } |
| 3911 | |
| 3912 | result = PyTuple_Pack(5, newobj, newargs, state, listitems, dictitems); |
| 3913 | Py_DECREF(newobj); |
| 3914 | Py_DECREF(newargs); |
| 3915 | Py_DECREF(state); |
| 3916 | Py_DECREF(listitems); |
| 3917 | Py_DECREF(dictitems); |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 3918 | return result; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3919 | } |
| 3920 | |
| 3921 | static PyObject * |
| 3922 | reduce_2(PyObject *obj) |
| 3923 | { |
| 3924 | PyObject *cls; |
| 3925 | PyObject *args = NULL, *args2 = NULL, *kwargs = NULL; |
| 3926 | PyObject *state = NULL, *listitems = NULL, *dictitems = NULL; |
| 3927 | PyObject *copyreg = NULL, *newobj = NULL, *res = NULL; |
| 3928 | Py_ssize_t i, n; |
| 3929 | _Py_IDENTIFIER(__newobj__); |
| 3930 | |
| 3931 | if (_PyObject_GetNewArguments(obj, &args, &kwargs) < 0) { |
| 3932 | return NULL; |
| 3933 | } |
| 3934 | if (args == NULL) { |
| 3935 | assert(kwargs == NULL); |
| 3936 | args = PyTuple_New(0); |
| 3937 | if (args == NULL) { |
| 3938 | return NULL; |
| 3939 | } |
| 3940 | } |
| 3941 | else if (kwargs != NULL) { |
| 3942 | if (PyDict_Size(kwargs) > 0) { |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 3943 | PyErr_SetString(PyExc_ValueError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3944 | "must use protocol 4 or greater to copy this " |
| 3945 | "object; since __getnewargs_ex__ returned " |
| 3946 | "keyword arguments."); |
| 3947 | Py_DECREF(args); |
| 3948 | Py_DECREF(kwargs); |
| 3949 | return NULL; |
| 3950 | } |
| 3951 | Py_CLEAR(kwargs); |
| 3952 | } |
| 3953 | |
| 3954 | state = _PyObject_GetState(obj); |
| 3955 | if (state == NULL) |
| 3956 | goto end; |
| 3957 | |
| 3958 | if (_PyObject_GetItemsIter(obj, &listitems, &dictitems) < 0) |
| 3959 | goto end; |
| 3960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3961 | copyreg = import_copyreg(); |
| 3962 | if (copyreg == NULL) |
| 3963 | goto end; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3964 | newobj = _PyObject_GetAttrId(copyreg, &PyId___newobj__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3965 | if (newobj == NULL) |
| 3966 | goto end; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3967 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3968 | n = PyTuple_GET_SIZE(args); |
| 3969 | args2 = PyTuple_New(n+1); |
| 3970 | if (args2 == NULL) |
| 3971 | goto end; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3972 | cls = (PyObject *) Py_TYPE(obj); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3973 | Py_INCREF(cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3974 | PyTuple_SET_ITEM(args2, 0, cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3975 | for (i = 0; i < n; i++) { |
| 3976 | PyObject *v = PyTuple_GET_ITEM(args, i); |
| 3977 | Py_INCREF(v); |
| 3978 | PyTuple_SET_ITEM(args2, i+1, v); |
| 3979 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3980 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3981 | res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3982 | |
| 3983 | end: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3984 | Py_XDECREF(args); |
| 3985 | Py_XDECREF(args2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3986 | Py_XDECREF(state); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3987 | Py_XDECREF(listitems); |
| 3988 | Py_XDECREF(dictitems); |
| 3989 | Py_XDECREF(copyreg); |
| 3990 | Py_XDECREF(newobj); |
| 3991 | return res; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 3992 | } |
| 3993 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3994 | /* |
| 3995 | * There were two problems when object.__reduce__ and object.__reduce_ex__ |
| 3996 | * were implemented in the same function: |
| 3997 | * - trying to pickle an object with a custom __reduce__ method that |
| 3998 | * fell back to object.__reduce__ in certain circumstances led to |
| 3999 | * infinite recursion at Python level and eventual RuntimeError. |
| 4000 | * - Pickling objects that lied about their type by overwriting the |
| 4001 | * __class__ descriptor could lead to infinite recursion at C level |
| 4002 | * and eventual segfault. |
| 4003 | * |
| 4004 | * Because of backwards compatibility, the two methods still have to |
| 4005 | * behave in the same way, even if this is not required by the pickle |
| 4006 | * protocol. This common functionality was moved to the _common_reduce |
| 4007 | * function. |
| 4008 | */ |
| 4009 | static PyObject * |
| 4010 | _common_reduce(PyObject *self, int proto) |
| 4011 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4012 | PyObject *copyreg, *res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4013 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4014 | if (proto >= 4) |
| 4015 | return reduce_4(self); |
| 4016 | else if (proto >= 2) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4017 | return reduce_2(self); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4018 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4019 | copyreg = import_copyreg(); |
| 4020 | if (!copyreg) |
| 4021 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4022 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4023 | res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto); |
| 4024 | Py_DECREF(copyreg); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4025 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4026 | return res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4027 | } |
| 4028 | |
| 4029 | static PyObject * |
| 4030 | object_reduce(PyObject *self, PyObject *args) |
| 4031 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4032 | int proto = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4033 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4034 | if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto)) |
| 4035 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4036 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4037 | return _common_reduce(self, proto); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4038 | } |
| 4039 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4040 | static PyObject * |
| 4041 | object_reduce_ex(PyObject *self, PyObject *args) |
| 4042 | { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4043 | static PyObject *objreduce; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4044 | PyObject *reduce, *res; |
| 4045 | int proto = 0; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4046 | _Py_IDENTIFIER(__reduce__); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4048 | if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto)) |
| 4049 | return NULL; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4050 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4051 | if (objreduce == NULL) { |
| 4052 | objreduce = _PyDict_GetItemId(PyBaseObject_Type.tp_dict, |
| 4053 | &PyId___reduce__); |
| 4054 | if (objreduce == NULL) |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 4055 | return NULL; |
| 4056 | } |
| 4057 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4058 | reduce = _PyObject_GetAttrId(self, &PyId___reduce__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4059 | if (reduce == NULL) |
| 4060 | PyErr_Clear(); |
| 4061 | else { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 4062 | PyObject *cls, *clsreduce; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4063 | int override; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 4064 | |
| 4065 | cls = (PyObject *) Py_TYPE(self); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4066 | clsreduce = _PyObject_GetAttrId(cls, &PyId___reduce__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4067 | if (clsreduce == NULL) { |
| 4068 | Py_DECREF(reduce); |
| 4069 | return NULL; |
| 4070 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4071 | override = (clsreduce != objreduce); |
| 4072 | Py_DECREF(clsreduce); |
| 4073 | if (override) { |
| 4074 | res = PyObject_CallObject(reduce, NULL); |
| 4075 | Py_DECREF(reduce); |
| 4076 | return res; |
| 4077 | } |
| 4078 | else |
| 4079 | Py_DECREF(reduce); |
| 4080 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4082 | return _common_reduce(self, proto); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 4083 | } |
| 4084 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 4085 | static PyObject * |
| 4086 | object_subclasshook(PyObject *cls, PyObject *args) |
| 4087 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4088 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 4089 | } |
| 4090 | |
| 4091 | PyDoc_STRVAR(object_subclasshook_doc, |
| 4092 | "Abstract classes can override this to customize issubclass().\n" |
| 4093 | "\n" |
| 4094 | "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n" |
| 4095 | "It should return True, False or NotImplemented. If it returns\n" |
| 4096 | "NotImplemented, the normal algorithm is used. Otherwise, it\n" |
| 4097 | "overrides the normal algorithm (and the outcome is cached).\n"); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 4098 | |
| 4099 | /* |
| 4100 | from PEP 3101, this code implements: |
| 4101 | |
| 4102 | class object: |
| 4103 | def __format__(self, format_spec): |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4104 | return format(str(self), format_spec) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 4105 | */ |
| 4106 | static PyObject * |
| 4107 | object_format(PyObject *self, PyObject *args) |
| 4108 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4109 | PyObject *format_spec; |
| 4110 | PyObject *self_as_str = NULL; |
| 4111 | PyObject *result = NULL; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 4112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4113 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 4114 | return NULL; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 4115 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4116 | self_as_str = PyObject_Str(self); |
Eric Smith | e4d6317 | 2010-09-13 20:48:43 +0000 | [diff] [blame] | 4117 | if (self_as_str != NULL) { |
| 4118 | /* Issue 7994: If we're converting to a string, we |
Eric V. Smith | b9cd353 | 2011-03-12 10:08:48 -0500 | [diff] [blame] | 4119 | should reject format specifications */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4120 | if (PyUnicode_GET_LENGTH(format_spec) > 0) { |
Andrew Svetlov | 2cd8ce4 | 2012-12-23 14:27:17 +0200 | [diff] [blame] | 4121 | PyErr_SetString(PyExc_TypeError, |
Eric V. Smith | b9cd353 | 2011-03-12 10:08:48 -0500 | [diff] [blame] | 4122 | "non-empty format string passed to object.__format__"); |
Andrew Svetlov | 2cd8ce4 | 2012-12-23 14:27:17 +0200 | [diff] [blame] | 4123 | goto done; |
Eric V. Smith | b9cd353 | 2011-03-12 10:08:48 -0500 | [diff] [blame] | 4124 | } |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 4125 | |
Eric V. Smith | b9cd353 | 2011-03-12 10:08:48 -0500 | [diff] [blame] | 4126 | result = PyObject_Format(self_as_str, format_spec); |
Eric Smith | e4d6317 | 2010-09-13 20:48:43 +0000 | [diff] [blame] | 4127 | } |
| 4128 | |
| 4129 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4130 | Py_XDECREF(self_as_str); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 4131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4132 | return result; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 4133 | } |
| 4134 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 4135 | static PyObject * |
| 4136 | object_sizeof(PyObject *self, PyObject *args) |
| 4137 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4138 | Py_ssize_t res, isize; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 4139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4140 | res = 0; |
| 4141 | isize = self->ob_type->tp_itemsize; |
| 4142 | if (isize > 0) |
| 4143 | res = Py_SIZE(self->ob_type) * isize; |
| 4144 | res += self->ob_type->tp_basicsize; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 4145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4146 | return PyLong_FromSsize_t(res); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 4149 | /* __dir__ for generic objects: returns __dict__, __class__, |
| 4150 | and recursively up the __class__.__bases__ chain. |
| 4151 | */ |
| 4152 | static PyObject * |
| 4153 | object_dir(PyObject *self, PyObject *args) |
| 4154 | { |
| 4155 | PyObject *result = NULL; |
| 4156 | PyObject *dict = NULL; |
| 4157 | PyObject *itsclass = NULL; |
| 4158 | |
| 4159 | /* Get __dict__ (which may or may not be a real dict...) */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 4160 | dict = _PyObject_GetAttrId(self, &PyId___dict__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 4161 | if (dict == NULL) { |
| 4162 | PyErr_Clear(); |
| 4163 | dict = PyDict_New(); |
| 4164 | } |
| 4165 | else if (!PyDict_Check(dict)) { |
| 4166 | Py_DECREF(dict); |
| 4167 | dict = PyDict_New(); |
| 4168 | } |
| 4169 | else { |
| 4170 | /* Copy __dict__ to avoid mutating it. */ |
| 4171 | PyObject *temp = PyDict_Copy(dict); |
| 4172 | Py_DECREF(dict); |
| 4173 | dict = temp; |
| 4174 | } |
| 4175 | |
| 4176 | if (dict == NULL) |
| 4177 | goto error; |
| 4178 | |
| 4179 | /* Merge in attrs reachable from its class. */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 4180 | itsclass = _PyObject_GetAttrId(self, &PyId___class__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 4181 | if (itsclass == NULL) |
| 4182 | /* XXX(tomer): Perhaps fall back to obj->ob_type if no |
| 4183 | __class__ exists? */ |
| 4184 | PyErr_Clear(); |
| 4185 | else if (merge_class_dict(dict, itsclass) != 0) |
| 4186 | goto error; |
| 4187 | |
| 4188 | result = PyDict_Keys(dict); |
| 4189 | /* fall through */ |
| 4190 | error: |
| 4191 | Py_XDECREF(itsclass); |
| 4192 | Py_XDECREF(dict); |
| 4193 | return result; |
| 4194 | } |
| 4195 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 4196 | static PyMethodDef object_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4197 | {"__reduce_ex__", object_reduce_ex, METH_VARARGS, |
| 4198 | PyDoc_STR("helper for pickle")}, |
| 4199 | {"__reduce__", object_reduce, METH_VARARGS, |
| 4200 | PyDoc_STR("helper for pickle")}, |
| 4201 | {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS, |
| 4202 | object_subclasshook_doc}, |
| 4203 | {"__format__", object_format, METH_VARARGS, |
| 4204 | PyDoc_STR("default object formatter")}, |
| 4205 | {"__sizeof__", object_sizeof, METH_NOARGS, |
Benjamin Peterson | fbe56bb | 2011-05-24 12:42:51 -0500 | [diff] [blame] | 4206 | PyDoc_STR("__sizeof__() -> int\nsize of object in memory, in bytes")}, |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 4207 | {"__dir__", object_dir, METH_NOARGS, |
Benjamin Peterson | c728412 | 2011-05-24 12:46:15 -0500 | [diff] [blame] | 4208 | PyDoc_STR("__dir__() -> list\ndefault dir() implementation")}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4209 | {0} |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 4210 | }; |
| 4211 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4212 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4213 | PyTypeObject PyBaseObject_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4214 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 4215 | "object", /* tp_name */ |
| 4216 | sizeof(PyObject), /* tp_basicsize */ |
| 4217 | 0, /* tp_itemsize */ |
| 4218 | object_dealloc, /* tp_dealloc */ |
| 4219 | 0, /* tp_print */ |
| 4220 | 0, /* tp_getattr */ |
| 4221 | 0, /* tp_setattr */ |
| 4222 | 0, /* tp_reserved */ |
| 4223 | object_repr, /* tp_repr */ |
| 4224 | 0, /* tp_as_number */ |
| 4225 | 0, /* tp_as_sequence */ |
| 4226 | 0, /* tp_as_mapping */ |
| 4227 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
| 4228 | 0, /* tp_call */ |
| 4229 | object_str, /* tp_str */ |
| 4230 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4231 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4232 | 0, /* tp_as_buffer */ |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 4233 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4234 | PyDoc_STR("object()\n--\n\nThe most base type"), /* tp_doc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4235 | 0, /* tp_traverse */ |
| 4236 | 0, /* tp_clear */ |
| 4237 | object_richcompare, /* tp_richcompare */ |
| 4238 | 0, /* tp_weaklistoffset */ |
| 4239 | 0, /* tp_iter */ |
| 4240 | 0, /* tp_iternext */ |
| 4241 | object_methods, /* tp_methods */ |
| 4242 | 0, /* tp_members */ |
| 4243 | object_getsets, /* tp_getset */ |
| 4244 | 0, /* tp_base */ |
| 4245 | 0, /* tp_dict */ |
| 4246 | 0, /* tp_descr_get */ |
| 4247 | 0, /* tp_descr_set */ |
| 4248 | 0, /* tp_dictoffset */ |
| 4249 | object_init, /* tp_init */ |
| 4250 | PyType_GenericAlloc, /* tp_alloc */ |
| 4251 | object_new, /* tp_new */ |
| 4252 | PyObject_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4253 | }; |
| 4254 | |
| 4255 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 4256 | /* 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] | 4257 | |
| 4258 | static int |
| 4259 | add_methods(PyTypeObject *type, PyMethodDef *meth) |
| 4260 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4261 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4263 | for (; meth->ml_name != NULL; meth++) { |
| 4264 | PyObject *descr; |
Benjamin Peterson | 89a6e9a | 2012-05-08 09:22:24 -0400 | [diff] [blame] | 4265 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4266 | if (PyDict_GetItemString(dict, meth->ml_name) && |
| 4267 | !(meth->ml_flags & METH_COEXIST)) |
| 4268 | continue; |
| 4269 | if (meth->ml_flags & METH_CLASS) { |
| 4270 | if (meth->ml_flags & METH_STATIC) { |
| 4271 | PyErr_SetString(PyExc_ValueError, |
| 4272 | "method cannot be both class and static"); |
| 4273 | return -1; |
| 4274 | } |
| 4275 | descr = PyDescr_NewClassMethod(type, meth); |
| 4276 | } |
| 4277 | else if (meth->ml_flags & METH_STATIC) { |
Andrew Svetlov | 3ba3a3e | 2012-12-25 13:32:35 +0200 | [diff] [blame] | 4278 | PyObject *cfunc = PyCFunction_NewEx(meth, (PyObject*)type, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4279 | if (cfunc == NULL) |
| 4280 | return -1; |
| 4281 | descr = PyStaticMethod_New(cfunc); |
| 4282 | Py_DECREF(cfunc); |
| 4283 | } |
| 4284 | else { |
| 4285 | descr = PyDescr_NewMethod(type, meth); |
| 4286 | } |
| 4287 | if (descr == NULL) |
| 4288 | return -1; |
Benjamin Peterson | 89a6e9a | 2012-05-08 09:22:24 -0400 | [diff] [blame] | 4289 | err = PyDict_SetItemString(dict, meth->ml_name, descr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4290 | Py_DECREF(descr); |
Benjamin Peterson | 89a6e9a | 2012-05-08 09:22:24 -0400 | [diff] [blame] | 4291 | if (err < 0) |
| 4292 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4293 | } |
| 4294 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4295 | } |
| 4296 | |
| 4297 | static int |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 4298 | add_members(PyTypeObject *type, PyMemberDef *memb) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4299 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4300 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4302 | for (; memb->name != NULL; memb++) { |
| 4303 | PyObject *descr; |
| 4304 | if (PyDict_GetItemString(dict, memb->name)) |
| 4305 | continue; |
| 4306 | descr = PyDescr_NewMember(type, memb); |
| 4307 | if (descr == NULL) |
| 4308 | return -1; |
| 4309 | if (PyDict_SetItemString(dict, memb->name, descr) < 0) |
| 4310 | return -1; |
| 4311 | Py_DECREF(descr); |
| 4312 | } |
| 4313 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4314 | } |
| 4315 | |
| 4316 | static int |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 4317 | add_getset(PyTypeObject *type, PyGetSetDef *gsp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4318 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4319 | PyObject *dict = type->tp_dict; |
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 | for (; gsp->name != NULL; gsp++) { |
| 4322 | PyObject *descr; |
| 4323 | if (PyDict_GetItemString(dict, gsp->name)) |
| 4324 | continue; |
| 4325 | descr = PyDescr_NewGetSet(type, gsp); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4327 | if (descr == NULL) |
| 4328 | return -1; |
| 4329 | if (PyDict_SetItemString(dict, gsp->name, descr) < 0) |
| 4330 | return -1; |
| 4331 | Py_DECREF(descr); |
| 4332 | } |
| 4333 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4334 | } |
| 4335 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4336 | static void |
| 4337 | inherit_special(PyTypeObject *type, PyTypeObject *base) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4338 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4340 | /* Copying basicsize is connected to the GC flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4341 | if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 4342 | (base->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 4343 | (!type->tp_traverse && !type->tp_clear)) { |
| 4344 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
| 4345 | if (type->tp_traverse == NULL) |
| 4346 | type->tp_traverse = base->tp_traverse; |
| 4347 | if (type->tp_clear == NULL) |
| 4348 | type->tp_clear = base->tp_clear; |
| 4349 | } |
| 4350 | { |
| 4351 | /* The condition below could use some explanation. |
| 4352 | It appears that tp_new is not inherited for static types |
| 4353 | whose base class is 'object'; this seems to be a precaution |
| 4354 | so that old extension types don't suddenly become |
| 4355 | callable (object.__new__ wouldn't insure the invariants |
| 4356 | that the extension type's own factory function ensures). |
| 4357 | Heap types, of course, are under our control, so they do |
| 4358 | inherit tp_new; static extension types that specify some |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 4359 | other built-in type as the default also |
| 4360 | inherit object.__new__. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4361 | if (base != &PyBaseObject_Type || |
| 4362 | (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 4363 | if (type->tp_new == NULL) |
| 4364 | type->tp_new = base->tp_new; |
| 4365 | } |
| 4366 | } |
Benjamin Peterson | a7465e2 | 2010-07-05 15:01:22 +0000 | [diff] [blame] | 4367 | if (type->tp_basicsize == 0) |
| 4368 | type->tp_basicsize = base->tp_basicsize; |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 4369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4370 | /* Copy other non-function slots */ |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 4371 | |
| 4372 | #undef COPYVAL |
| 4373 | #define COPYVAL(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4374 | if (type->SLOT == 0) type->SLOT = base->SLOT |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 4375 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4376 | COPYVAL(tp_itemsize); |
| 4377 | COPYVAL(tp_weaklistoffset); |
| 4378 | COPYVAL(tp_dictoffset); |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 4379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4380 | /* Setup fast subclass flags */ |
| 4381 | if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException)) |
| 4382 | type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS; |
| 4383 | else if (PyType_IsSubtype(base, &PyType_Type)) |
| 4384 | type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS; |
| 4385 | else if (PyType_IsSubtype(base, &PyLong_Type)) |
| 4386 | type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS; |
| 4387 | else if (PyType_IsSubtype(base, &PyBytes_Type)) |
| 4388 | type->tp_flags |= Py_TPFLAGS_BYTES_SUBCLASS; |
| 4389 | else if (PyType_IsSubtype(base, &PyUnicode_Type)) |
| 4390 | type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS; |
| 4391 | else if (PyType_IsSubtype(base, &PyTuple_Type)) |
| 4392 | type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS; |
| 4393 | else if (PyType_IsSubtype(base, &PyList_Type)) |
| 4394 | type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS; |
| 4395 | else if (PyType_IsSubtype(base, &PyDict_Type)) |
| 4396 | type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4397 | } |
| 4398 | |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 4399 | static int |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 4400 | overrides_hash(PyTypeObject *type) |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 4401 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4402 | PyObject *dict = type->tp_dict; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4403 | _Py_IDENTIFIER(__eq__); |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 4404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4405 | assert(dict != NULL); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4406 | if (_PyDict_GetItemId(dict, &PyId___eq__) != NULL) |
| 4407 | return 1; |
| 4408 | if (_PyDict_GetItemId(dict, &PyId___hash__) != NULL) |
| 4409 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4410 | return 0; |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 4411 | } |
| 4412 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4413 | static void |
| 4414 | inherit_slots(PyTypeObject *type, PyTypeObject *base) |
| 4415 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4416 | PyTypeObject *basebase; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4417 | |
| 4418 | #undef SLOTDEFINED |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4419 | #undef COPYSLOT |
| 4420 | #undef COPYNUM |
| 4421 | #undef COPYSEQ |
| 4422 | #undef COPYMAP |
Guido van Rossum | 5af588b | 2001-10-12 14:13:21 +0000 | [diff] [blame] | 4423 | #undef COPYBUF |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4424 | |
| 4425 | #define SLOTDEFINED(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4426 | (base->SLOT != 0 && \ |
| 4427 | (basebase == NULL || base->SLOT != basebase->SLOT)) |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4428 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4429 | #define COPYSLOT(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4430 | if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4431 | |
| 4432 | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT) |
| 4433 | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT) |
| 4434 | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT) |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 4435 | #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4436 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4437 | /* This won't inherit indirect slots (from tp_as_number etc.) |
| 4438 | if type doesn't provide the space. */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4439 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4440 | if (type->tp_as_number != NULL && base->tp_as_number != NULL) { |
| 4441 | basebase = base->tp_base; |
| 4442 | if (basebase->tp_as_number == NULL) |
| 4443 | basebase = NULL; |
| 4444 | COPYNUM(nb_add); |
| 4445 | COPYNUM(nb_subtract); |
| 4446 | COPYNUM(nb_multiply); |
| 4447 | COPYNUM(nb_remainder); |
| 4448 | COPYNUM(nb_divmod); |
| 4449 | COPYNUM(nb_power); |
| 4450 | COPYNUM(nb_negative); |
| 4451 | COPYNUM(nb_positive); |
| 4452 | COPYNUM(nb_absolute); |
| 4453 | COPYNUM(nb_bool); |
| 4454 | COPYNUM(nb_invert); |
| 4455 | COPYNUM(nb_lshift); |
| 4456 | COPYNUM(nb_rshift); |
| 4457 | COPYNUM(nb_and); |
| 4458 | COPYNUM(nb_xor); |
| 4459 | COPYNUM(nb_or); |
| 4460 | COPYNUM(nb_int); |
| 4461 | COPYNUM(nb_float); |
| 4462 | COPYNUM(nb_inplace_add); |
| 4463 | COPYNUM(nb_inplace_subtract); |
| 4464 | COPYNUM(nb_inplace_multiply); |
| 4465 | COPYNUM(nb_inplace_remainder); |
| 4466 | COPYNUM(nb_inplace_power); |
| 4467 | COPYNUM(nb_inplace_lshift); |
| 4468 | COPYNUM(nb_inplace_rshift); |
| 4469 | COPYNUM(nb_inplace_and); |
| 4470 | COPYNUM(nb_inplace_xor); |
| 4471 | COPYNUM(nb_inplace_or); |
| 4472 | COPYNUM(nb_true_divide); |
| 4473 | COPYNUM(nb_floor_divide); |
| 4474 | COPYNUM(nb_inplace_true_divide); |
| 4475 | COPYNUM(nb_inplace_floor_divide); |
| 4476 | COPYNUM(nb_index); |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 4477 | COPYNUM(nb_matrix_multiply); |
| 4478 | COPYNUM(nb_inplace_matrix_multiply); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4479 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4481 | if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) { |
| 4482 | basebase = base->tp_base; |
| 4483 | if (basebase->tp_as_sequence == NULL) |
| 4484 | basebase = NULL; |
| 4485 | COPYSEQ(sq_length); |
| 4486 | COPYSEQ(sq_concat); |
| 4487 | COPYSEQ(sq_repeat); |
| 4488 | COPYSEQ(sq_item); |
| 4489 | COPYSEQ(sq_ass_item); |
| 4490 | COPYSEQ(sq_contains); |
| 4491 | COPYSEQ(sq_inplace_concat); |
| 4492 | COPYSEQ(sq_inplace_repeat); |
| 4493 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4494 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4495 | if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) { |
| 4496 | basebase = base->tp_base; |
| 4497 | if (basebase->tp_as_mapping == NULL) |
| 4498 | basebase = NULL; |
| 4499 | COPYMAP(mp_length); |
| 4500 | COPYMAP(mp_subscript); |
| 4501 | COPYMAP(mp_ass_subscript); |
| 4502 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4504 | if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) { |
| 4505 | basebase = base->tp_base; |
| 4506 | if (basebase->tp_as_buffer == NULL) |
| 4507 | basebase = NULL; |
| 4508 | COPYBUF(bf_getbuffer); |
| 4509 | COPYBUF(bf_releasebuffer); |
| 4510 | } |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 4511 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4512 | basebase = base->tp_base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4514 | COPYSLOT(tp_dealloc); |
| 4515 | if (type->tp_getattr == NULL && type->tp_getattro == NULL) { |
| 4516 | type->tp_getattr = base->tp_getattr; |
| 4517 | type->tp_getattro = base->tp_getattro; |
| 4518 | } |
| 4519 | if (type->tp_setattr == NULL && type->tp_setattro == NULL) { |
| 4520 | type->tp_setattr = base->tp_setattr; |
| 4521 | type->tp_setattro = base->tp_setattro; |
| 4522 | } |
| 4523 | /* tp_reserved is ignored */ |
| 4524 | COPYSLOT(tp_repr); |
| 4525 | /* tp_hash see tp_richcompare */ |
| 4526 | COPYSLOT(tp_call); |
| 4527 | COPYSLOT(tp_str); |
| 4528 | { |
| 4529 | /* Copy comparison-related slots only when |
| 4530 | not overriding them anywhere */ |
| 4531 | if (type->tp_richcompare == NULL && |
| 4532 | type->tp_hash == NULL && |
| 4533 | !overrides_hash(type)) |
| 4534 | { |
| 4535 | type->tp_richcompare = base->tp_richcompare; |
| 4536 | type->tp_hash = base->tp_hash; |
| 4537 | } |
| 4538 | } |
| 4539 | { |
| 4540 | COPYSLOT(tp_iter); |
| 4541 | COPYSLOT(tp_iternext); |
| 4542 | } |
| 4543 | { |
| 4544 | COPYSLOT(tp_descr_get); |
| 4545 | COPYSLOT(tp_descr_set); |
| 4546 | COPYSLOT(tp_dictoffset); |
| 4547 | COPYSLOT(tp_init); |
| 4548 | COPYSLOT(tp_alloc); |
| 4549 | COPYSLOT(tp_is_gc); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 4550 | if ((type->tp_flags & Py_TPFLAGS_HAVE_FINALIZE) && |
| 4551 | (base->tp_flags & Py_TPFLAGS_HAVE_FINALIZE)) { |
| 4552 | COPYSLOT(tp_finalize); |
| 4553 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4554 | if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 4555 | (base->tp_flags & Py_TPFLAGS_HAVE_GC)) { |
| 4556 | /* They agree about gc. */ |
| 4557 | COPYSLOT(tp_free); |
| 4558 | } |
| 4559 | else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 4560 | type->tp_free == NULL && |
| 4561 | base->tp_free == PyObject_Free) { |
| 4562 | /* A bit of magic to plug in the correct default |
| 4563 | * tp_free function when a derived class adds gc, |
| 4564 | * didn't define tp_free, and the base uses the |
| 4565 | * default non-gc tp_free. |
| 4566 | */ |
| 4567 | type->tp_free = PyObject_GC_Del; |
| 4568 | } |
| 4569 | /* else they didn't agree about gc, and there isn't something |
| 4570 | * obvious to be done -- the type is on its own. |
| 4571 | */ |
| 4572 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4573 | } |
| 4574 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 4575 | static int add_operators(PyTypeObject *); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4576 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4577 | int |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 4578 | PyType_Ready(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4579 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4580 | PyObject *dict, *bases; |
| 4581 | PyTypeObject *base; |
| 4582 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4583 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4584 | if (type->tp_flags & Py_TPFLAGS_READY) { |
| 4585 | assert(type->tp_dict != NULL); |
| 4586 | return 0; |
| 4587 | } |
| 4588 | assert((type->tp_flags & Py_TPFLAGS_READYING) == 0); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 4589 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4590 | type->tp_flags |= Py_TPFLAGS_READYING; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4591 | |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 4592 | #ifdef Py_TRACE_REFS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4593 | /* PyType_Ready is the closest thing we have to a choke point |
| 4594 | * for type objects, so is the best place I can think of to try |
| 4595 | * to get type objects into the doubly-linked list of all objects. |
| 4596 | * Still, not all type objects go thru PyType_Ready. |
| 4597 | */ |
| 4598 | _Py_AddToAllObjects((PyObject *)type, 0); |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 4599 | #endif |
| 4600 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4601 | /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
| 4602 | base = type->tp_base; |
| 4603 | if (base == NULL && type != &PyBaseObject_Type) { |
| 4604 | base = type->tp_base = &PyBaseObject_Type; |
| 4605 | Py_INCREF(base); |
| 4606 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4608 | /* Now the only way base can still be NULL is if type is |
| 4609 | * &PyBaseObject_Type. |
| 4610 | */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 4611 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4612 | /* Initialize the base class */ |
| 4613 | if (base != NULL && base->tp_dict == NULL) { |
| 4614 | if (PyType_Ready(base) < 0) |
| 4615 | goto error; |
| 4616 | } |
Guido van Rossum | 323a9cf | 2002-08-14 17:26:30 +0000 | [diff] [blame] | 4617 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4618 | /* Initialize ob_type if NULL. This means extensions that want to be |
| 4619 | compilable separately on Windows can call PyType_Ready() instead of |
| 4620 | initializing the ob_type field of their type objects. */ |
| 4621 | /* The test for base != NULL is really unnecessary, since base is only |
| 4622 | NULL when type is &PyBaseObject_Type, and we know its ob_type is |
| 4623 | not NULL (it's initialized to &PyType_Type). But coverity doesn't |
| 4624 | know that. */ |
| 4625 | if (Py_TYPE(type) == NULL && base != NULL) |
| 4626 | Py_TYPE(type) = Py_TYPE(base); |
Guido van Rossum | 0986d82 | 2002-04-08 01:38:42 +0000 | [diff] [blame] | 4627 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4628 | /* Initialize tp_bases */ |
| 4629 | bases = type->tp_bases; |
| 4630 | if (bases == NULL) { |
| 4631 | if (base == NULL) |
| 4632 | bases = PyTuple_New(0); |
| 4633 | else |
| 4634 | bases = PyTuple_Pack(1, base); |
| 4635 | if (bases == NULL) |
| 4636 | goto error; |
| 4637 | type->tp_bases = bases; |
| 4638 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4640 | /* Initialize tp_dict */ |
| 4641 | dict = type->tp_dict; |
| 4642 | if (dict == NULL) { |
| 4643 | dict = PyDict_New(); |
| 4644 | if (dict == NULL) |
| 4645 | goto error; |
| 4646 | type->tp_dict = dict; |
| 4647 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4649 | /* Add type-specific descriptors to tp_dict */ |
| 4650 | if (add_operators(type) < 0) |
| 4651 | goto error; |
| 4652 | if (type->tp_methods != NULL) { |
| 4653 | if (add_methods(type, type->tp_methods) < 0) |
| 4654 | goto error; |
| 4655 | } |
| 4656 | if (type->tp_members != NULL) { |
| 4657 | if (add_members(type, type->tp_members) < 0) |
| 4658 | goto error; |
| 4659 | } |
| 4660 | if (type->tp_getset != NULL) { |
| 4661 | if (add_getset(type, type->tp_getset) < 0) |
| 4662 | goto error; |
| 4663 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4664 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4665 | /* Calculate method resolution order */ |
| 4666 | if (mro_internal(type) < 0) { |
| 4667 | goto error; |
| 4668 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4669 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4670 | /* Inherit special flags from dominant base */ |
| 4671 | if (type->tp_base != NULL) |
| 4672 | inherit_special(type, type->tp_base); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4674 | /* Initialize tp_dict properly */ |
| 4675 | bases = type->tp_mro; |
| 4676 | assert(bases != NULL); |
| 4677 | assert(PyTuple_Check(bases)); |
| 4678 | n = PyTuple_GET_SIZE(bases); |
| 4679 | for (i = 1; i < n; i++) { |
| 4680 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 4681 | if (PyType_Check(b)) |
| 4682 | inherit_slots(type, (PyTypeObject *)b); |
| 4683 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4685 | /* Sanity check for tp_free. */ |
| 4686 | if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) && |
| 4687 | (type->tp_free == NULL || type->tp_free == PyObject_Del)) { |
| 4688 | /* This base class needs to call tp_free, but doesn't have |
| 4689 | * one, or its tp_free is for non-gc'ed objects. |
| 4690 | */ |
| 4691 | PyErr_Format(PyExc_TypeError, "type '%.100s' participates in " |
| 4692 | "gc and is a base type but has inappropriate " |
| 4693 | "tp_free slot", |
| 4694 | type->tp_name); |
| 4695 | goto error; |
| 4696 | } |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 4697 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4698 | /* if the type dictionary doesn't contain a __doc__, set it from |
| 4699 | the tp_doc slot. |
| 4700 | */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4701 | if (_PyDict_GetItemId(type->tp_dict, &PyId___doc__) == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4702 | if (type->tp_doc != NULL) { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4703 | const char *old_doc = _PyType_DocWithoutSignature(type->tp_name, |
| 4704 | type->tp_doc); |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 4705 | PyObject *doc = PyUnicode_FromString(old_doc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4706 | if (doc == NULL) |
| 4707 | goto error; |
Victor Stinner | 5967bf4 | 2013-07-17 22:01:37 +0200 | [diff] [blame] | 4708 | if (_PyDict_SetItemId(type->tp_dict, &PyId___doc__, doc) < 0) { |
| 4709 | Py_DECREF(doc); |
| 4710 | goto error; |
| 4711 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4712 | Py_DECREF(doc); |
| 4713 | } else { |
Victor Stinner | 5967bf4 | 2013-07-17 22:01:37 +0200 | [diff] [blame] | 4714 | if (_PyDict_SetItemId(type->tp_dict, |
| 4715 | &PyId___doc__, Py_None) < 0) |
| 4716 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4717 | } |
| 4718 | } |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 4719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4720 | /* Hack for tp_hash and __hash__. |
| 4721 | If after all that, tp_hash is still NULL, and __hash__ is not in |
| 4722 | tp_dict, set tp_hash to PyObject_HashNotImplemented and |
| 4723 | tp_dict['__hash__'] equal to None. |
| 4724 | This signals that __hash__ is not inherited. |
| 4725 | */ |
| 4726 | if (type->tp_hash == NULL) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4727 | if (_PyDict_GetItemId(type->tp_dict, &PyId___hash__) == NULL) { |
| 4728 | if (_PyDict_SetItemId(type->tp_dict, &PyId___hash__, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4729 | goto error; |
| 4730 | type->tp_hash = PyObject_HashNotImplemented; |
| 4731 | } |
| 4732 | } |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 4733 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4734 | /* Some more special stuff */ |
| 4735 | base = type->tp_base; |
| 4736 | if (base != NULL) { |
| 4737 | if (type->tp_as_number == NULL) |
| 4738 | type->tp_as_number = base->tp_as_number; |
| 4739 | if (type->tp_as_sequence == NULL) |
| 4740 | type->tp_as_sequence = base->tp_as_sequence; |
| 4741 | if (type->tp_as_mapping == NULL) |
| 4742 | type->tp_as_mapping = base->tp_as_mapping; |
| 4743 | if (type->tp_as_buffer == NULL) |
| 4744 | type->tp_as_buffer = base->tp_as_buffer; |
| 4745 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4746 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4747 | /* Link into each base class's list of subclasses */ |
| 4748 | bases = type->tp_bases; |
| 4749 | n = PyTuple_GET_SIZE(bases); |
| 4750 | for (i = 0; i < n; i++) { |
| 4751 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 4752 | if (PyType_Check(b) && |
| 4753 | add_subclass((PyTypeObject *)b, type) < 0) |
| 4754 | goto error; |
| 4755 | } |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4756 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4757 | /* Warn for a type that implements tp_compare (now known as |
| 4758 | tp_reserved) but not tp_richcompare. */ |
| 4759 | if (type->tp_reserved && !type->tp_richcompare) { |
Andrew Svetlov | 2cd8ce4 | 2012-12-23 14:27:17 +0200 | [diff] [blame] | 4760 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 4761 | "Type %.100s defines tp_reserved (formerly tp_compare) " |
| 4762 | "but not tp_richcompare. Comparisons may not behave as intended.", |
| 4763 | type->tp_name); |
Andrew Svetlov | 2cd8ce4 | 2012-12-23 14:27:17 +0200 | [diff] [blame] | 4764 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4765 | } |
Mark Dickinson | 2a7d45b | 2009-02-08 11:02:10 +0000 | [diff] [blame] | 4766 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4767 | /* All done -- set the ready flag */ |
| 4768 | assert(type->tp_dict != NULL); |
| 4769 | type->tp_flags = |
| 4770 | (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; |
| 4771 | return 0; |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 4772 | |
| 4773 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4774 | type->tp_flags &= ~Py_TPFLAGS_READYING; |
| 4775 | return -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4776 | } |
| 4777 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4778 | static int |
| 4779 | add_subclass(PyTypeObject *base, PyTypeObject *type) |
| 4780 | { |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 4781 | int result = -1; |
| 4782 | PyObject *dict, *key, *newobj; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4783 | |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 4784 | dict = base->tp_subclasses; |
| 4785 | if (dict == NULL) { |
| 4786 | base->tp_subclasses = dict = PyDict_New(); |
| 4787 | if (dict == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4788 | return -1; |
| 4789 | } |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 4790 | assert(PyDict_CheckExact(dict)); |
| 4791 | key = PyLong_FromVoidPtr((void *) type); |
| 4792 | if (key == NULL) |
Victor Stinner | 9812af8 | 2013-07-08 22:25:48 +0200 | [diff] [blame] | 4793 | return -1; |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 4794 | newobj = PyWeakref_NewRef((PyObject *)type, NULL); |
| 4795 | if (newobj != NULL) { |
| 4796 | result = PyDict_SetItem(dict, key, newobj); |
| 4797 | Py_DECREF(newobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4798 | } |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 4799 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4800 | return result; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 4801 | } |
| 4802 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4803 | static void |
| 4804 | remove_subclass(PyTypeObject *base, PyTypeObject *type) |
| 4805 | { |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 4806 | PyObject *dict, *key; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4807 | |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 4808 | dict = base->tp_subclasses; |
| 4809 | if (dict == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4810 | return; |
| 4811 | } |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 4812 | assert(PyDict_CheckExact(dict)); |
| 4813 | key = PyLong_FromVoidPtr((void *) type); |
| 4814 | if (key == NULL || PyDict_DelItem(dict, key)) { |
| 4815 | /* This can happen if the type initialization errored out before |
| 4816 | the base subclasses were updated (e.g. a non-str __qualname__ |
| 4817 | was passed in the type dict). */ |
| 4818 | PyErr_Clear(); |
| 4819 | } |
| 4820 | Py_XDECREF(key); |
| 4821 | } |
| 4822 | |
| 4823 | static void |
| 4824 | remove_all_subclasses(PyTypeObject *type, PyObject *bases) |
| 4825 | { |
| 4826 | if (bases) { |
| 4827 | Py_ssize_t i; |
| 4828 | for (i = 0; i < PyTuple_GET_SIZE(bases); i++) { |
| 4829 | PyObject *base = PyTuple_GET_ITEM(bases, i); |
| 4830 | if (PyType_Check(base)) |
| 4831 | remove_subclass((PyTypeObject*) base, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4832 | } |
| 4833 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4834 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4835 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4836 | static int |
| 4837 | check_num_args(PyObject *ob, int n) |
| 4838 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4839 | if (!PyTuple_CheckExact(ob)) { |
| 4840 | PyErr_SetString(PyExc_SystemError, |
| 4841 | "PyArg_UnpackTuple() argument list is not a tuple"); |
| 4842 | return 0; |
| 4843 | } |
| 4844 | if (n == PyTuple_GET_SIZE(ob)) |
| 4845 | return 1; |
| 4846 | PyErr_Format( |
| 4847 | PyExc_TypeError, |
| 4848 | "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob)); |
| 4849 | return 0; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 4850 | } |
| 4851 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4852 | /* Generic wrappers for overloadable 'operators' such as __getitem__ */ |
| 4853 | |
| 4854 | /* There's a wrapper *function* for each distinct function typedef used |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4855 | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4856 | wrapper *table* for each distinct operation (e.g. __len__, __add__). |
| 4857 | Most tables have only one entry; the tables for binary operators have two |
| 4858 | entries, one regular and one with reversed arguments. */ |
| 4859 | |
| 4860 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4861 | wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4862 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4863 | lenfunc func = (lenfunc)wrapped; |
| 4864 | Py_ssize_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4865 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4866 | if (!check_num_args(args, 0)) |
| 4867 | return NULL; |
| 4868 | res = (*func)(self); |
| 4869 | if (res == -1 && PyErr_Occurred()) |
| 4870 | return NULL; |
| 4871 | return PyLong_FromLong((long)res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4872 | } |
| 4873 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4874 | static PyObject * |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4875 | wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped) |
| 4876 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4877 | inquiry func = (inquiry)wrapped; |
| 4878 | int res; |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4880 | if (!check_num_args(args, 0)) |
| 4881 | return NULL; |
| 4882 | res = (*func)(self); |
| 4883 | if (res == -1 && PyErr_Occurred()) |
| 4884 | return NULL; |
| 4885 | return PyBool_FromLong((long)res); |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 4886 | } |
| 4887 | |
| 4888 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4889 | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4890 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4891 | binaryfunc func = (binaryfunc)wrapped; |
| 4892 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4893 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4894 | if (!check_num_args(args, 1)) |
| 4895 | return NULL; |
| 4896 | other = PyTuple_GET_ITEM(args, 0); |
| 4897 | return (*func)(self, other); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4898 | } |
| 4899 | |
| 4900 | static PyObject * |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4901 | wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped) |
| 4902 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4903 | binaryfunc func = (binaryfunc)wrapped; |
| 4904 | PyObject *other; |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4906 | if (!check_num_args(args, 1)) |
| 4907 | return NULL; |
| 4908 | other = PyTuple_GET_ITEM(args, 0); |
| 4909 | return (*func)(self, other); |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 4910 | } |
| 4911 | |
| 4912 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4913 | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4914 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4915 | binaryfunc func = (binaryfunc)wrapped; |
| 4916 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4918 | if (!check_num_args(args, 1)) |
| 4919 | return NULL; |
| 4920 | other = PyTuple_GET_ITEM(args, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4921 | return (*func)(other, self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4922 | } |
| 4923 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 4924 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4925 | wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4926 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4927 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4928 | PyObject *other; |
| 4929 | PyObject *third = Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4930 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4931 | /* Note: This wrapper only works for __pow__() */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4933 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
| 4934 | return NULL; |
| 4935 | return (*func)(self, other, third); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4936 | } |
| 4937 | |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4938 | static PyObject * |
| 4939 | wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 4940 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4941 | ternaryfunc func = (ternaryfunc)wrapped; |
| 4942 | PyObject *other; |
| 4943 | PyObject *third = Py_None; |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4944 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4945 | /* Note: This wrapper only works for __pow__() */ |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4947 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
| 4948 | return NULL; |
| 4949 | return (*func)(other, self, third); |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 4950 | } |
| 4951 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4952 | static PyObject * |
| 4953 | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 4954 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4955 | unaryfunc func = (unaryfunc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4956 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4957 | if (!check_num_args(args, 0)) |
| 4958 | return NULL; |
| 4959 | return (*func)(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4960 | } |
| 4961 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4962 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4963 | wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4964 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4965 | ssizeargfunc func = (ssizeargfunc)wrapped; |
| 4966 | PyObject* o; |
| 4967 | Py_ssize_t i; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4968 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4969 | if (!PyArg_UnpackTuple(args, "", 1, 1, &o)) |
| 4970 | return NULL; |
| 4971 | i = PyNumber_AsSsize_t(o, PyExc_OverflowError); |
| 4972 | if (i == -1 && PyErr_Occurred()) |
| 4973 | return NULL; |
| 4974 | return (*func)(self, i); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4975 | } |
| 4976 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4977 | static Py_ssize_t |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4978 | getindex(PyObject *self, PyObject *arg) |
| 4979 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4980 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4981 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4982 | i = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
| 4983 | if (i == -1 && PyErr_Occurred()) |
| 4984 | return -1; |
| 4985 | if (i < 0) { |
| 4986 | PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence; |
| 4987 | if (sq && sq->sq_length) { |
| 4988 | Py_ssize_t n = (*sq->sq_length)(self); |
| 4989 | if (n < 0) |
| 4990 | return -1; |
| 4991 | i += n; |
| 4992 | } |
| 4993 | } |
| 4994 | return i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 4995 | } |
| 4996 | |
| 4997 | static PyObject * |
| 4998 | wrap_sq_item(PyObject *self, PyObject *args, void *wrapped) |
| 4999 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5000 | ssizeargfunc func = (ssizeargfunc)wrapped; |
| 5001 | PyObject *arg; |
| 5002 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 5003 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5004 | if (PyTuple_GET_SIZE(args) == 1) { |
| 5005 | arg = PyTuple_GET_ITEM(args, 0); |
| 5006 | i = getindex(self, arg); |
| 5007 | if (i == -1 && PyErr_Occurred()) |
| 5008 | return NULL; |
| 5009 | return (*func)(self, i); |
| 5010 | } |
| 5011 | check_num_args(args, 1); |
| 5012 | assert(PyErr_Occurred()); |
| 5013 | return NULL; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 5014 | } |
| 5015 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5016 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 5017 | wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5018 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5019 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 5020 | Py_ssize_t i; |
| 5021 | int res; |
| 5022 | PyObject *arg, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5023 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5024 | if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value)) |
| 5025 | return NULL; |
| 5026 | i = getindex(self, arg); |
| 5027 | if (i == -1 && PyErr_Occurred()) |
| 5028 | return NULL; |
| 5029 | res = (*func)(self, i, value); |
| 5030 | if (res == -1 && PyErr_Occurred()) |
| 5031 | return NULL; |
| 5032 | Py_INCREF(Py_None); |
| 5033 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5034 | } |
| 5035 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 5036 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 5037 | wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 5038 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5039 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 5040 | Py_ssize_t i; |
| 5041 | int res; |
| 5042 | PyObject *arg; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 5043 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5044 | if (!check_num_args(args, 1)) |
| 5045 | return NULL; |
| 5046 | arg = PyTuple_GET_ITEM(args, 0); |
| 5047 | i = getindex(self, arg); |
| 5048 | if (i == -1 && PyErr_Occurred()) |
| 5049 | return NULL; |
| 5050 | res = (*func)(self, i, NULL); |
| 5051 | if (res == -1 && PyErr_Occurred()) |
| 5052 | return NULL; |
| 5053 | Py_INCREF(Py_None); |
| 5054 | return Py_None; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 5055 | } |
| 5056 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5057 | /* XXX objobjproc is a misnomer; should be objargpred */ |
| 5058 | static PyObject * |
| 5059 | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) |
| 5060 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5061 | objobjproc func = (objobjproc)wrapped; |
| 5062 | int res; |
| 5063 | PyObject *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5065 | if (!check_num_args(args, 1)) |
| 5066 | return NULL; |
| 5067 | value = PyTuple_GET_ITEM(args, 0); |
| 5068 | res = (*func)(self, value); |
| 5069 | if (res == -1 && PyErr_Occurred()) |
| 5070 | return NULL; |
| 5071 | else |
| 5072 | return PyBool_FromLong(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5073 | } |
| 5074 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5075 | static PyObject * |
| 5076 | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 5077 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5078 | objobjargproc func = (objobjargproc)wrapped; |
| 5079 | int res; |
| 5080 | PyObject *key, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5082 | if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value)) |
| 5083 | return NULL; |
| 5084 | res = (*func)(self, key, value); |
| 5085 | if (res == -1 && PyErr_Occurred()) |
| 5086 | return NULL; |
| 5087 | Py_INCREF(Py_None); |
| 5088 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5089 | } |
| 5090 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 5091 | static PyObject * |
| 5092 | wrap_delitem(PyObject *self, PyObject *args, void *wrapped) |
| 5093 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5094 | objobjargproc func = (objobjargproc)wrapped; |
| 5095 | int res; |
| 5096 | PyObject *key; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 5097 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5098 | if (!check_num_args(args, 1)) |
| 5099 | return NULL; |
| 5100 | key = PyTuple_GET_ITEM(args, 0); |
| 5101 | res = (*func)(self, key, NULL); |
| 5102 | if (res == -1 && PyErr_Occurred()) |
| 5103 | return NULL; |
| 5104 | Py_INCREF(Py_None); |
| 5105 | return Py_None; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 5106 | } |
| 5107 | |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 5108 | /* 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] | 5109 | This is called the Carlo Verre hack after its discoverer. */ |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 5110 | static int |
| 5111 | hackcheck(PyObject *self, setattrofunc func, char *what) |
| 5112 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5113 | PyTypeObject *type = Py_TYPE(self); |
| 5114 | while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 5115 | type = type->tp_base; |
| 5116 | /* If type is NULL now, this is a really weird type. |
| 5117 | In the spirit of backwards compatibility (?), just shut up. */ |
| 5118 | if (type && type->tp_setattro != func) { |
| 5119 | PyErr_Format(PyExc_TypeError, |
| 5120 | "can't apply this %s to %s object", |
| 5121 | what, |
| 5122 | type->tp_name); |
| 5123 | return 0; |
| 5124 | } |
| 5125 | return 1; |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 5126 | } |
| 5127 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5128 | static PyObject * |
| 5129 | wrap_setattr(PyObject *self, PyObject *args, void *wrapped) |
| 5130 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5131 | setattrofunc func = (setattrofunc)wrapped; |
| 5132 | int res; |
| 5133 | PyObject *name, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5134 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5135 | if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value)) |
| 5136 | return NULL; |
| 5137 | if (!hackcheck(self, func, "__setattr__")) |
| 5138 | return NULL; |
| 5139 | res = (*func)(self, name, value); |
| 5140 | if (res < 0) |
| 5141 | return NULL; |
| 5142 | Py_INCREF(Py_None); |
| 5143 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5144 | } |
| 5145 | |
| 5146 | static PyObject * |
| 5147 | wrap_delattr(PyObject *self, PyObject *args, void *wrapped) |
| 5148 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5149 | setattrofunc func = (setattrofunc)wrapped; |
| 5150 | int res; |
| 5151 | PyObject *name; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5153 | if (!check_num_args(args, 1)) |
| 5154 | return NULL; |
| 5155 | name = PyTuple_GET_ITEM(args, 0); |
| 5156 | if (!hackcheck(self, func, "__delattr__")) |
| 5157 | return NULL; |
| 5158 | res = (*func)(self, name, NULL); |
| 5159 | if (res < 0) |
| 5160 | return NULL; |
| 5161 | Py_INCREF(Py_None); |
| 5162 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5163 | } |
| 5164 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5165 | static PyObject * |
| 5166 | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) |
| 5167 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5168 | hashfunc func = (hashfunc)wrapped; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 5169 | Py_hash_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5171 | if (!check_num_args(args, 0)) |
| 5172 | return NULL; |
| 5173 | res = (*func)(self); |
| 5174 | if (res == -1 && PyErr_Occurred()) |
| 5175 | return NULL; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 5176 | return PyLong_FromSsize_t(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5177 | } |
| 5178 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5179 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5180 | wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5181 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5182 | ternaryfunc func = (ternaryfunc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5184 | return (*func)(self, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5185 | } |
| 5186 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5187 | static PyObject * |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 5188 | wrap_del(PyObject *self, PyObject *args, void *wrapped) |
| 5189 | { |
| 5190 | destructor func = (destructor)wrapped; |
| 5191 | |
| 5192 | if (!check_num_args(args, 0)) |
| 5193 | return NULL; |
| 5194 | |
| 5195 | (*func)(self); |
| 5196 | Py_RETURN_NONE; |
| 5197 | } |
| 5198 | |
| 5199 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5200 | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) |
| 5201 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5202 | richcmpfunc func = (richcmpfunc)wrapped; |
| 5203 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5205 | if (!check_num_args(args, 1)) |
| 5206 | return NULL; |
| 5207 | other = PyTuple_GET_ITEM(args, 0); |
| 5208 | return (*func)(self, other, op); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5209 | } |
| 5210 | |
| 5211 | #undef RICHCMP_WRAPPER |
| 5212 | #define RICHCMP_WRAPPER(NAME, OP) \ |
| 5213 | static PyObject * \ |
| 5214 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ |
| 5215 | { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5216 | return wrap_richcmpfunc(self, args, wrapped, OP); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5217 | } |
| 5218 | |
Jack Jansen | 8e938b4 | 2001-08-08 15:29:49 +0000 | [diff] [blame] | 5219 | RICHCMP_WRAPPER(lt, Py_LT) |
| 5220 | RICHCMP_WRAPPER(le, Py_LE) |
| 5221 | RICHCMP_WRAPPER(eq, Py_EQ) |
| 5222 | RICHCMP_WRAPPER(ne, Py_NE) |
| 5223 | RICHCMP_WRAPPER(gt, Py_GT) |
| 5224 | RICHCMP_WRAPPER(ge, Py_GE) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5225 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5226 | static PyObject * |
| 5227 | wrap_next(PyObject *self, PyObject *args, void *wrapped) |
| 5228 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5229 | unaryfunc func = (unaryfunc)wrapped; |
| 5230 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5232 | if (!check_num_args(args, 0)) |
| 5233 | return NULL; |
| 5234 | res = (*func)(self); |
| 5235 | if (res == NULL && !PyErr_Occurred()) |
| 5236 | PyErr_SetNone(PyExc_StopIteration); |
| 5237 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5238 | } |
| 5239 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5240 | static PyObject * |
| 5241 | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped) |
| 5242 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5243 | descrgetfunc func = (descrgetfunc)wrapped; |
| 5244 | PyObject *obj; |
| 5245 | PyObject *type = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5247 | if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type)) |
| 5248 | return NULL; |
| 5249 | if (obj == Py_None) |
| 5250 | obj = NULL; |
| 5251 | if (type == Py_None) |
| 5252 | type = NULL; |
| 5253 | if (type == NULL &&obj == NULL) { |
| 5254 | PyErr_SetString(PyExc_TypeError, |
| 5255 | "__get__(None, None) is invalid"); |
| 5256 | return NULL; |
| 5257 | } |
| 5258 | return (*func)(self, obj, type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5259 | } |
| 5260 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5261 | static PyObject * |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5262 | wrap_descr_set(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5263 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5264 | descrsetfunc func = (descrsetfunc)wrapped; |
| 5265 | PyObject *obj, *value; |
| 5266 | int ret; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5267 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5268 | if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value)) |
| 5269 | return NULL; |
| 5270 | ret = (*func)(self, obj, value); |
| 5271 | if (ret < 0) |
| 5272 | return NULL; |
| 5273 | Py_INCREF(Py_None); |
| 5274 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5275 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 5276 | |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 5277 | static PyObject * |
| 5278 | wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped) |
| 5279 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5280 | descrsetfunc func = (descrsetfunc)wrapped; |
| 5281 | PyObject *obj; |
| 5282 | int ret; |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 5283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5284 | if (!check_num_args(args, 1)) |
| 5285 | return NULL; |
| 5286 | obj = PyTuple_GET_ITEM(args, 0); |
| 5287 | ret = (*func)(self, obj, NULL); |
| 5288 | if (ret < 0) |
| 5289 | return NULL; |
| 5290 | Py_INCREF(Py_None); |
| 5291 | return Py_None; |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 5292 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5293 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5294 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5295 | wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5296 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5297 | initproc func = (initproc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5299 | if (func(self, args, kwds) < 0) |
| 5300 | return NULL; |
| 5301 | Py_INCREF(Py_None); |
| 5302 | return Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5303 | } |
| 5304 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5305 | static PyObject * |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 5306 | tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5307 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5308 | PyTypeObject *type, *subtype, *staticbase; |
| 5309 | PyObject *arg0, *res; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 5310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5311 | if (self == NULL || !PyType_Check(self)) |
| 5312 | Py_FatalError("__new__() called with non-type 'self'"); |
| 5313 | type = (PyTypeObject *)self; |
| 5314 | if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) { |
| 5315 | PyErr_Format(PyExc_TypeError, |
| 5316 | "%s.__new__(): not enough arguments", |
| 5317 | type->tp_name); |
| 5318 | return NULL; |
| 5319 | } |
| 5320 | arg0 = PyTuple_GET_ITEM(args, 0); |
| 5321 | if (!PyType_Check(arg0)) { |
| 5322 | PyErr_Format(PyExc_TypeError, |
| 5323 | "%s.__new__(X): X is not a type object (%s)", |
| 5324 | type->tp_name, |
| 5325 | Py_TYPE(arg0)->tp_name); |
| 5326 | return NULL; |
| 5327 | } |
| 5328 | subtype = (PyTypeObject *)arg0; |
| 5329 | if (!PyType_IsSubtype(subtype, type)) { |
| 5330 | PyErr_Format(PyExc_TypeError, |
| 5331 | "%s.__new__(%s): %s is not a subtype of %s", |
| 5332 | type->tp_name, |
| 5333 | subtype->tp_name, |
| 5334 | subtype->tp_name, |
| 5335 | type->tp_name); |
| 5336 | return NULL; |
| 5337 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 5338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5339 | /* Check that the use doesn't do something silly and unsafe like |
| 5340 | object.__new__(dict). To do this, we check that the |
| 5341 | most derived base that's not a heap type is this type. */ |
| 5342 | staticbase = subtype; |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 5343 | while (staticbase && (staticbase->tp_new == slot_tp_new)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5344 | staticbase = staticbase->tp_base; |
| 5345 | /* If staticbase is NULL now, it is a really weird type. |
| 5346 | In the spirit of backwards compatibility (?), just shut up. */ |
| 5347 | if (staticbase && staticbase->tp_new != type->tp_new) { |
| 5348 | PyErr_Format(PyExc_TypeError, |
| 5349 | "%s.__new__(%s) is not safe, use %s.__new__()", |
| 5350 | type->tp_name, |
| 5351 | subtype->tp_name, |
| 5352 | staticbase == NULL ? "?" : staticbase->tp_name); |
| 5353 | return NULL; |
| 5354 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 5355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5356 | args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 5357 | if (args == NULL) |
| 5358 | return NULL; |
| 5359 | res = type->tp_new(subtype, args, kwds); |
| 5360 | Py_DECREF(args); |
| 5361 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5362 | } |
| 5363 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 5364 | static struct PyMethodDef tp_new_methoddef[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5365 | {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 5366 | PyDoc_STR("__new__($type, *args, **kwargs)\n--\n\n" |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 5367 | "Create and return a new object. " |
| 5368 | "See help(type) for accurate signature.")}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5369 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5370 | }; |
| 5371 | |
| 5372 | static int |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 5373 | add_tp_new_wrapper(PyTypeObject *type) |
| 5374 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5375 | PyObject *func; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 5376 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5377 | if (_PyDict_GetItemId(type->tp_dict, &PyId___new__) != NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5378 | return 0; |
Andrew Svetlov | 3ba3a3e | 2012-12-25 13:32:35 +0200 | [diff] [blame] | 5379 | func = PyCFunction_NewEx(tp_new_methoddef, (PyObject *)type, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5380 | if (func == NULL) |
| 5381 | return -1; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5382 | if (_PyDict_SetItemId(type->tp_dict, &PyId___new__, func)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5383 | Py_DECREF(func); |
| 5384 | return -1; |
| 5385 | } |
| 5386 | Py_DECREF(func); |
| 5387 | return 0; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 5388 | } |
| 5389 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 5390 | /* Slot wrappers that call the corresponding __foo__ slot. See comments |
| 5391 | below at override_slots() for more explanation. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5392 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5393 | #define SLOT0(FUNCNAME, OPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5394 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5395 | FUNCNAME(PyObject *self) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5396 | { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5397 | _Py_static_string(id, OPSTR); \ |
| 5398 | return call_method(self, &id, "()"); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5399 | } |
| 5400 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5401 | #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5402 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5403 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5404 | { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5405 | _Py_static_string(id, OPSTR); \ |
| 5406 | return call_method(self, &id, "(" ARGCODES ")", arg1); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5407 | } |
| 5408 | |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 5409 | /* Boolean helper for SLOT1BINFULL(). |
| 5410 | right.__class__ is a nontrivial subclass of left.__class__. */ |
| 5411 | static int |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5412 | method_is_overloaded(PyObject *left, PyObject *right, struct _Py_Identifier *name) |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 5413 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5414 | PyObject *a, *b; |
| 5415 | int ok; |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 5416 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5417 | b = _PyObject_GetAttrId((PyObject *)(Py_TYPE(right)), name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5418 | if (b == NULL) { |
| 5419 | PyErr_Clear(); |
| 5420 | /* If right doesn't have it, it's not overloaded */ |
| 5421 | return 0; |
| 5422 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 5423 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5424 | a = _PyObject_GetAttrId((PyObject *)(Py_TYPE(left)), name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5425 | if (a == NULL) { |
| 5426 | PyErr_Clear(); |
| 5427 | Py_DECREF(b); |
| 5428 | /* If right has it but left doesn't, it's overloaded */ |
| 5429 | return 1; |
| 5430 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 5431 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5432 | ok = PyObject_RichCompareBool(a, b, Py_NE); |
| 5433 | Py_DECREF(a); |
| 5434 | Py_DECREF(b); |
| 5435 | if (ok < 0) { |
| 5436 | PyErr_Clear(); |
| 5437 | return 0; |
| 5438 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 5439 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5440 | return ok; |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 5441 | } |
| 5442 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5443 | |
| 5444 | #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5445 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5446 | FUNCNAME(PyObject *self, PyObject *other) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5447 | { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5448 | _Py_static_string(op_id, OPSTR); \ |
| 5449 | _Py_static_string(rop_id, ROPSTR); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5450 | int do_other = Py_TYPE(self) != Py_TYPE(other) && \ |
| 5451 | Py_TYPE(other)->tp_as_number != NULL && \ |
| 5452 | Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \ |
| 5453 | if (Py_TYPE(self)->tp_as_number != NULL && \ |
| 5454 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ |
| 5455 | PyObject *r; \ |
| 5456 | if (do_other && \ |
| 5457 | PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5458 | method_is_overloaded(self, other, &rop_id)) { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5459 | r = call_maybe(other, &rop_id, "(O)", self); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5460 | if (r != Py_NotImplemented) \ |
| 5461 | return r; \ |
| 5462 | Py_DECREF(r); \ |
| 5463 | do_other = 0; \ |
| 5464 | } \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5465 | r = call_maybe(self, &op_id, "(O)", other); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5466 | if (r != Py_NotImplemented || \ |
| 5467 | Py_TYPE(other) == Py_TYPE(self)) \ |
| 5468 | return r; \ |
| 5469 | Py_DECREF(r); \ |
| 5470 | } \ |
| 5471 | if (do_other) { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5472 | return call_maybe(other, &rop_id, "(O)", self); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5473 | } \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5474 | Py_RETURN_NOTIMPLEMENTED; \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5475 | } |
| 5476 | |
| 5477 | #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5478 | SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR) |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5479 | |
| 5480 | #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \ |
| 5481 | static PyObject * \ |
| 5482 | FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ |
| 5483 | { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5484 | _Py_static_string(id, #OPSTR); \ |
| 5485 | return call_method(self, &id, "(" ARGCODES ")", arg1, arg2); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5486 | } |
| 5487 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5488 | static Py_ssize_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5489 | slot_sq_length(PyObject *self) |
| 5490 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5491 | PyObject *res = call_method(self, &PyId___len__, "()"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5492 | Py_ssize_t len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5494 | if (res == NULL) |
| 5495 | return -1; |
| 5496 | len = PyNumber_AsSsize_t(res, PyExc_OverflowError); |
| 5497 | Py_DECREF(res); |
| 5498 | if (len < 0) { |
| 5499 | if (!PyErr_Occurred()) |
| 5500 | PyErr_SetString(PyExc_ValueError, |
| 5501 | "__len__() should return >= 0"); |
| 5502 | return -1; |
| 5503 | } |
| 5504 | return len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5505 | } |
| 5506 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 5507 | /* Super-optimized version of slot_sq_item. |
| 5508 | Other slots could do the same... */ |
| 5509 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5510 | slot_sq_item(PyObject *self, Py_ssize_t i) |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 5511 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5512 | PyObject *func, *args = NULL, *ival = NULL, *retval = NULL; |
| 5513 | descrgetfunc f; |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 5514 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5515 | func = _PyType_LookupId(Py_TYPE(self), &PyId___getitem__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5516 | if (func != NULL) { |
| 5517 | if ((f = Py_TYPE(func)->tp_descr_get) == NULL) |
| 5518 | Py_INCREF(func); |
| 5519 | else { |
| 5520 | func = f(func, self, (PyObject *)(Py_TYPE(self))); |
| 5521 | if (func == NULL) { |
| 5522 | return NULL; |
| 5523 | } |
| 5524 | } |
| 5525 | ival = PyLong_FromSsize_t(i); |
| 5526 | if (ival != NULL) { |
| 5527 | args = PyTuple_New(1); |
| 5528 | if (args != NULL) { |
| 5529 | PyTuple_SET_ITEM(args, 0, ival); |
| 5530 | retval = PyObject_Call(func, args, NULL); |
| 5531 | Py_XDECREF(args); |
| 5532 | Py_XDECREF(func); |
| 5533 | return retval; |
| 5534 | } |
| 5535 | } |
| 5536 | } |
| 5537 | else { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5538 | PyObject *getitem_str = _PyUnicode_FromId(&PyId___getitem__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5539 | PyErr_SetObject(PyExc_AttributeError, getitem_str); |
| 5540 | } |
| 5541 | Py_XDECREF(args); |
| 5542 | Py_XDECREF(ival); |
| 5543 | Py_XDECREF(func); |
| 5544 | return NULL; |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 5545 | } |
| 5546 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5547 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5548 | slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5549 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5550 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5552 | if (value == NULL) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5553 | res = call_method(self, &PyId___delitem__, "(n)", index); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5554 | else |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5555 | res = call_method(self, &PyId___setitem__, "(nO)", index, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5556 | if (res == NULL) |
| 5557 | return -1; |
| 5558 | Py_DECREF(res); |
| 5559 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5560 | } |
| 5561 | |
| 5562 | static int |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5563 | slot_sq_contains(PyObject *self, PyObject *value) |
| 5564 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5565 | PyObject *func, *res, *args; |
| 5566 | int result = -1; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5567 | _Py_IDENTIFIER(__contains__); |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 5568 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5569 | func = lookup_maybe(self, &PyId___contains__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5570 | if (func != NULL) { |
| 5571 | args = PyTuple_Pack(1, value); |
| 5572 | if (args == NULL) |
| 5573 | res = NULL; |
| 5574 | else { |
| 5575 | res = PyObject_Call(func, args, NULL); |
| 5576 | Py_DECREF(args); |
| 5577 | } |
| 5578 | Py_DECREF(func); |
| 5579 | if (res != NULL) { |
| 5580 | result = PyObject_IsTrue(res); |
| 5581 | Py_DECREF(res); |
| 5582 | } |
| 5583 | } |
| 5584 | else if (! PyErr_Occurred()) { |
| 5585 | /* Possible results: -1 and 1 */ |
| 5586 | result = (int)_PySequence_IterSearch(self, value, |
| 5587 | PY_ITERSEARCH_CONTAINS); |
| 5588 | } |
| 5589 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5590 | } |
| 5591 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5592 | #define slot_mp_length slot_sq_length |
| 5593 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5594 | SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5595 | |
| 5596 | static int |
| 5597 | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) |
| 5598 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5599 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5600 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5601 | if (value == NULL) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5602 | res = call_method(self, &PyId___delitem__, "(O)", key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5603 | else |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5604 | res = call_method(self, &PyId___setitem__, "(OO)", key, value); |
| 5605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5606 | if (res == NULL) |
| 5607 | return -1; |
| 5608 | Py_DECREF(res); |
| 5609 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5610 | } |
| 5611 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5612 | SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__") |
| 5613 | SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__") |
| 5614 | SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__") |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 5615 | SLOT1BIN(slot_nb_matrix_multiply, nb_matrix_multiply, "__matmul__", "__rmatmul__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5616 | SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__") |
| 5617 | SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__") |
| 5618 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 5619 | static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5620 | |
| 5621 | SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5622 | nb_power, "__pow__", "__rpow__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5623 | |
| 5624 | static PyObject * |
| 5625 | slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) |
| 5626 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5627 | _Py_IDENTIFIER(__pow__); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 5628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5629 | if (modulus == Py_None) |
| 5630 | return slot_nb_power_binary(self, other); |
| 5631 | /* Three-arg power doesn't use __rpow__. But ternary_op |
| 5632 | can call this when the second argument's type uses |
| 5633 | slot_nb_power, so check before calling self.__pow__. */ |
| 5634 | if (Py_TYPE(self)->tp_as_number != NULL && |
| 5635 | Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5636 | return call_method(self, &PyId___pow__, "(OO)", other, modulus); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5637 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5638 | Py_RETURN_NOTIMPLEMENTED; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5639 | } |
| 5640 | |
| 5641 | SLOT0(slot_nb_negative, "__neg__") |
| 5642 | SLOT0(slot_nb_positive, "__pos__") |
| 5643 | SLOT0(slot_nb_absolute, "__abs__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5644 | |
| 5645 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 5646 | slot_nb_bool(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5647 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5648 | PyObject *func, *args; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5649 | int result = -1; |
| 5650 | int using_len = 0; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5651 | _Py_IDENTIFIER(__bool__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5652 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5653 | func = lookup_maybe(self, &PyId___bool__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5654 | if (func == NULL) { |
| 5655 | if (PyErr_Occurred()) |
| 5656 | return -1; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5657 | func = lookup_maybe(self, &PyId___len__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5658 | if (func == NULL) |
| 5659 | return PyErr_Occurred() ? -1 : 1; |
| 5660 | using_len = 1; |
| 5661 | } |
| 5662 | args = PyTuple_New(0); |
| 5663 | if (args != NULL) { |
| 5664 | PyObject *temp = PyObject_Call(func, args, NULL); |
| 5665 | Py_DECREF(args); |
| 5666 | if (temp != NULL) { |
| 5667 | if (using_len) { |
| 5668 | /* enforced by slot_nb_len */ |
| 5669 | result = PyObject_IsTrue(temp); |
| 5670 | } |
| 5671 | else if (PyBool_Check(temp)) { |
| 5672 | result = PyObject_IsTrue(temp); |
| 5673 | } |
| 5674 | else { |
| 5675 | PyErr_Format(PyExc_TypeError, |
| 5676 | "__bool__ should return " |
| 5677 | "bool, returned %s", |
| 5678 | Py_TYPE(temp)->tp_name); |
| 5679 | result = -1; |
| 5680 | } |
| 5681 | Py_DECREF(temp); |
| 5682 | } |
| 5683 | } |
| 5684 | Py_DECREF(func); |
| 5685 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5686 | } |
| 5687 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5688 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 5689 | static PyObject * |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5690 | slot_nb_index(PyObject *self) |
| 5691 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5692 | _Py_IDENTIFIER(__index__); |
| 5693 | return call_method(self, &PyId___index__, "()"); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5694 | } |
| 5695 | |
| 5696 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5697 | SLOT0(slot_nb_invert, "__invert__") |
| 5698 | SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") |
| 5699 | SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") |
| 5700 | SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") |
| 5701 | SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") |
| 5702 | SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5703 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5704 | SLOT0(slot_nb_int, "__int__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5705 | SLOT0(slot_nb_float, "__float__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5706 | SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O") |
| 5707 | SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O") |
| 5708 | SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O") |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 5709 | SLOT1(slot_nb_inplace_matrix_multiply, "__imatmul__", PyObject *, "O") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5710 | SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O") |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 5711 | /* Can't use SLOT1 here, because nb_inplace_power is ternary */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5712 | static PyObject * |
| 5713 | slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2) |
| 5714 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5715 | _Py_IDENTIFIER(__ipow__); |
| 5716 | return call_method(self, &PyId___ipow__, "(" "O" ")", arg1); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 5717 | } |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5718 | SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O") |
| 5719 | SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O") |
| 5720 | SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O") |
| 5721 | SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O") |
| 5722 | SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O") |
| 5723 | SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5724 | "__floordiv__", "__rfloordiv__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5725 | SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__") |
| 5726 | SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O") |
| 5727 | SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5728 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5729 | static PyObject * |
| 5730 | slot_tp_repr(PyObject *self) |
| 5731 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5732 | PyObject *func, *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5733 | _Py_IDENTIFIER(__repr__); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5734 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5735 | func = lookup_method(self, &PyId___repr__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5736 | if (func != NULL) { |
| 5737 | res = PyEval_CallObject(func, NULL); |
| 5738 | Py_DECREF(func); |
| 5739 | return res; |
| 5740 | } |
| 5741 | PyErr_Clear(); |
| 5742 | return PyUnicode_FromFormat("<%s object at %p>", |
| 5743 | Py_TYPE(self)->tp_name, self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5744 | } |
| 5745 | |
| 5746 | static PyObject * |
| 5747 | slot_tp_str(PyObject *self) |
| 5748 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5749 | PyObject *func, *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5750 | _Py_IDENTIFIER(__str__); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5751 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5752 | func = lookup_method(self, &PyId___str__); |
Victor Stinner | 2e8474d | 2013-07-11 22:46:11 +0200 | [diff] [blame] | 5753 | if (func == NULL) |
| 5754 | return NULL; |
Victor Stinner | bebba50 | 2013-10-29 10:56:34 +0100 | [diff] [blame] | 5755 | res = PyEval_CallObject(func, NULL); |
| 5756 | Py_DECREF(func); |
| 5757 | return res; |
| 5758 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5759 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5760 | static Py_hash_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5761 | slot_tp_hash(PyObject *self) |
| 5762 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5763 | PyObject *func, *res; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5764 | Py_ssize_t h; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5765 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5766 | func = lookup_method(self, &PyId___hash__); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5767 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5768 | if (func == Py_None) { |
| 5769 | Py_DECREF(func); |
| 5770 | func = NULL; |
| 5771 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 5772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5773 | if (func == NULL) { |
| 5774 | return PyObject_HashNotImplemented(self); |
| 5775 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 5776 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5777 | res = PyEval_CallObject(func, NULL); |
| 5778 | Py_DECREF(func); |
| 5779 | if (res == NULL) |
| 5780 | return -1; |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 5781 | |
| 5782 | if (!PyLong_Check(res)) { |
| 5783 | PyErr_SetString(PyExc_TypeError, |
| 5784 | "__hash__ method should return an integer"); |
| 5785 | return -1; |
| 5786 | } |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5787 | /* Transform the PyLong `res` to a Py_hash_t `h`. For an existing |
| 5788 | hashable Python object x, hash(x) will always lie within the range of |
| 5789 | Py_hash_t. Therefore our transformation must preserve values that |
| 5790 | already lie within this range, to ensure that if x.__hash__() returns |
| 5791 | hash(y) then hash(x) == hash(y). */ |
| 5792 | h = PyLong_AsSsize_t(res); |
| 5793 | if (h == -1 && PyErr_Occurred()) { |
| 5794 | /* 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] | 5795 | use any sufficiently bit-mixing transformation; |
| 5796 | long.__hash__ will do nicely. */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5797 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5798 | h = PyLong_Type.tp_hash(res); |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5799 | } |
Benjamin Peterson | e7dfeeb | 2010-10-17 21:27:01 +0000 | [diff] [blame] | 5800 | /* -1 is reserved for errors. */ |
| 5801 | if (h == -1) |
| 5802 | h = -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5803 | Py_DECREF(res); |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 5804 | return h; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5805 | } |
| 5806 | |
| 5807 | static PyObject * |
| 5808 | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 5809 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5810 | _Py_IDENTIFIER(__call__); |
| 5811 | PyObject *meth = lookup_method(self, &PyId___call__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5812 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5813 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5814 | if (meth == NULL) |
| 5815 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5816 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5817 | res = PyObject_Call(meth, args, kwds); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5818 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5819 | Py_DECREF(meth); |
| 5820 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5821 | } |
| 5822 | |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5823 | /* There are two slot dispatch functions for tp_getattro. |
| 5824 | |
| 5825 | - slot_tp_getattro() is used when __getattribute__ is overridden |
| 5826 | but no __getattr__ hook is present; |
| 5827 | |
| 5828 | - slot_tp_getattr_hook() is used when a __getattr__ hook is present. |
| 5829 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 5830 | The code in update_one_slot() always installs slot_tp_getattr_hook(); this |
| 5831 | detects the absence of __getattr__ and then installs the simpler slot if |
| 5832 | necessary. */ |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 5833 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5834 | static PyObject * |
| 5835 | slot_tp_getattro(PyObject *self, PyObject *name) |
| 5836 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5837 | return call_method(self, &PyId___getattribute__, "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5838 | } |
| 5839 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5840 | static PyObject * |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5841 | call_attribute(PyObject *self, PyObject *attr, PyObject *name) |
| 5842 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5843 | PyObject *res, *descr = NULL; |
| 5844 | descrgetfunc f = Py_TYPE(attr)->tp_descr_get; |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5845 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5846 | if (f != NULL) { |
| 5847 | descr = f(attr, self, (PyObject *)(Py_TYPE(self))); |
| 5848 | if (descr == NULL) |
| 5849 | return NULL; |
| 5850 | else |
| 5851 | attr = descr; |
| 5852 | } |
| 5853 | res = PyObject_CallFunctionObjArgs(attr, name, NULL); |
| 5854 | Py_XDECREF(descr); |
| 5855 | return res; |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 5856 | } |
| 5857 | |
| 5858 | static PyObject * |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5859 | slot_tp_getattr_hook(PyObject *self, PyObject *name) |
| 5860 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5861 | PyTypeObject *tp = Py_TYPE(self); |
| 5862 | PyObject *getattr, *getattribute, *res; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5863 | _Py_IDENTIFIER(__getattr__); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5865 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5866 | method fully for each attribute lookup for classes with |
| 5867 | __getattr__, even when the attribute is present. So we use |
| 5868 | _PyType_Lookup and create the method only when needed, with |
| 5869 | call_attribute. */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5870 | getattr = _PyType_LookupId(tp, &PyId___getattr__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5871 | if (getattr == NULL) { |
| 5872 | /* No __getattr__ hook: use a simpler dispatcher */ |
| 5873 | tp->tp_getattro = slot_tp_getattro; |
| 5874 | return slot_tp_getattro(self, name); |
| 5875 | } |
| 5876 | Py_INCREF(getattr); |
| 5877 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 5878 | method fully for each attribute lookup for classes with |
| 5879 | __getattr__, even when self has the default __getattribute__ |
| 5880 | method. So we use _PyType_Lookup and create the method only when |
| 5881 | needed, with call_attribute. */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5882 | getattribute = _PyType_LookupId(tp, &PyId___getattribute__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5883 | if (getattribute == NULL || |
| 5884 | (Py_TYPE(getattribute) == &PyWrapperDescr_Type && |
| 5885 | ((PyWrapperDescrObject *)getattribute)->d_wrapped == |
| 5886 | (void *)PyObject_GenericGetAttr)) |
| 5887 | res = PyObject_GenericGetAttr(self, name); |
| 5888 | else { |
| 5889 | Py_INCREF(getattribute); |
| 5890 | res = call_attribute(self, getattribute, name); |
| 5891 | Py_DECREF(getattribute); |
| 5892 | } |
| 5893 | if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 5894 | PyErr_Clear(); |
| 5895 | res = call_attribute(self, getattr, name); |
| 5896 | } |
| 5897 | Py_DECREF(getattr); |
| 5898 | return res; |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 5899 | } |
| 5900 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5901 | static int |
| 5902 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) |
| 5903 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5904 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5905 | _Py_IDENTIFIER(__delattr__); |
| 5906 | _Py_IDENTIFIER(__setattr__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5907 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5908 | if (value == NULL) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5909 | res = call_method(self, &PyId___delattr__, "(O)", name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5910 | else |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5911 | res = call_method(self, &PyId___setattr__, "(OO)", name, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5912 | if (res == NULL) |
| 5913 | return -1; |
| 5914 | Py_DECREF(res); |
| 5915 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5916 | } |
| 5917 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5918 | static _Py_Identifier name_op[] = { |
| 5919 | {0, "__lt__", 0}, |
| 5920 | {0, "__le__", 0}, |
| 5921 | {0, "__eq__", 0}, |
| 5922 | {0, "__ne__", 0}, |
| 5923 | {0, "__gt__", 0}, |
| 5924 | {0, "__ge__", 0} |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 5925 | }; |
| 5926 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5927 | static PyObject * |
Mark Dickinson | 6f1d049 | 2009-11-15 13:58:49 +0000 | [diff] [blame] | 5928 | slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5929 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5930 | PyObject *func, *args, *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5931 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5932 | func = lookup_method(self, &name_op[op]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5933 | if (func == NULL) { |
| 5934 | PyErr_Clear(); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5935 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5936 | } |
| 5937 | args = PyTuple_Pack(1, other); |
| 5938 | if (args == NULL) |
| 5939 | res = NULL; |
| 5940 | else { |
| 5941 | res = PyObject_Call(func, args, NULL); |
| 5942 | Py_DECREF(args); |
| 5943 | } |
| 5944 | Py_DECREF(func); |
| 5945 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5946 | } |
| 5947 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5948 | static PyObject * |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5949 | slot_tp_iter(PyObject *self) |
| 5950 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5951 | PyObject *func, *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5952 | _Py_IDENTIFIER(__iter__); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5953 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5954 | func = lookup_method(self, &PyId___iter__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5955 | if (func != NULL) { |
| 5956 | PyObject *args; |
| 5957 | args = res = PyTuple_New(0); |
| 5958 | if (args != NULL) { |
| 5959 | res = PyObject_Call(func, args, NULL); |
| 5960 | Py_DECREF(args); |
| 5961 | } |
| 5962 | Py_DECREF(func); |
| 5963 | return res; |
| 5964 | } |
| 5965 | PyErr_Clear(); |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5966 | func = lookup_method(self, &PyId___getitem__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5967 | if (func == NULL) { |
| 5968 | PyErr_Format(PyExc_TypeError, |
| 5969 | "'%.200s' object is not iterable", |
| 5970 | Py_TYPE(self)->tp_name); |
| 5971 | return NULL; |
| 5972 | } |
| 5973 | Py_DECREF(func); |
| 5974 | return PySeqIter_New(self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 5975 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5976 | |
| 5977 | static PyObject * |
| 5978 | slot_tp_iternext(PyObject *self) |
| 5979 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5980 | _Py_IDENTIFIER(__next__); |
| 5981 | return call_method(self, &PyId___next__, "()"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5982 | } |
| 5983 | |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5984 | static PyObject * |
| 5985 | slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 5986 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5987 | PyTypeObject *tp = Py_TYPE(self); |
| 5988 | PyObject *get; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5989 | _Py_IDENTIFIER(__get__); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 5990 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5991 | get = _PyType_LookupId(tp, &PyId___get__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5992 | if (get == NULL) { |
| 5993 | /* Avoid further slowdowns */ |
| 5994 | if (tp->tp_descr_get == slot_tp_descr_get) |
| 5995 | tp->tp_descr_get = NULL; |
| 5996 | Py_INCREF(self); |
| 5997 | return self; |
| 5998 | } |
| 5999 | if (obj == NULL) |
| 6000 | obj = Py_None; |
| 6001 | if (type == NULL) |
| 6002 | type = Py_None; |
| 6003 | return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 6004 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6005 | |
| 6006 | static int |
| 6007 | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) |
| 6008 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6009 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 6010 | _Py_IDENTIFIER(__delete__); |
| 6011 | _Py_IDENTIFIER(__set__); |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 6012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6013 | if (value == NULL) |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 6014 | res = call_method(self, &PyId___delete__, "(O)", target); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6015 | else |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 6016 | res = call_method(self, &PyId___set__, "(OO)", target, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6017 | if (res == NULL) |
| 6018 | return -1; |
| 6019 | Py_DECREF(res); |
| 6020 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6021 | } |
| 6022 | |
| 6023 | static int |
| 6024 | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 6025 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 6026 | _Py_IDENTIFIER(__init__); |
| 6027 | PyObject *meth = lookup_method(self, &PyId___init__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6028 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6029 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6030 | if (meth == NULL) |
| 6031 | return -1; |
| 6032 | res = PyObject_Call(meth, args, kwds); |
| 6033 | Py_DECREF(meth); |
| 6034 | if (res == NULL) |
| 6035 | return -1; |
| 6036 | if (res != Py_None) { |
| 6037 | PyErr_Format(PyExc_TypeError, |
| 6038 | "__init__() should return None, not '%.200s'", |
| 6039 | Py_TYPE(res)->tp_name); |
| 6040 | Py_DECREF(res); |
| 6041 | return -1; |
| 6042 | } |
| 6043 | Py_DECREF(res); |
| 6044 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6045 | } |
| 6046 | |
| 6047 | static PyObject * |
| 6048 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 6049 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6050 | PyObject *func; |
| 6051 | PyObject *newargs, *x; |
| 6052 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6053 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 6054 | func = _PyObject_GetAttrId((PyObject *)type, &PyId___new__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6055 | if (func == NULL) |
| 6056 | return NULL; |
| 6057 | assert(PyTuple_Check(args)); |
| 6058 | n = PyTuple_GET_SIZE(args); |
| 6059 | newargs = PyTuple_New(n+1); |
| 6060 | if (newargs == NULL) |
| 6061 | return NULL; |
| 6062 | Py_INCREF(type); |
| 6063 | PyTuple_SET_ITEM(newargs, 0, (PyObject *)type); |
| 6064 | for (i = 0; i < n; i++) { |
| 6065 | x = PyTuple_GET_ITEM(args, i); |
| 6066 | Py_INCREF(x); |
| 6067 | PyTuple_SET_ITEM(newargs, i+1, x); |
| 6068 | } |
| 6069 | x = PyObject_Call(func, newargs, kwds); |
| 6070 | Py_DECREF(newargs); |
| 6071 | Py_DECREF(func); |
| 6072 | return x; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6073 | } |
| 6074 | |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 6075 | static void |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 6076 | slot_tp_finalize(PyObject *self) |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 6077 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 6078 | _Py_IDENTIFIER(__del__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6079 | PyObject *del, *res; |
| 6080 | PyObject *error_type, *error_value, *error_traceback; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 6081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6082 | /* Save the current exception, if any. */ |
| 6083 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 6084 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6085 | /* Execute __del__ method, if any. */ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 6086 | del = lookup_maybe(self, &PyId___del__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6087 | if (del != NULL) { |
| 6088 | res = PyEval_CallObject(del, NULL); |
| 6089 | if (res == NULL) |
| 6090 | PyErr_WriteUnraisable(del); |
| 6091 | else |
| 6092 | Py_DECREF(res); |
| 6093 | Py_DECREF(del); |
| 6094 | } |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 6095 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6096 | /* Restore the saved exception. */ |
| 6097 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 6098 | } |
| 6099 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6100 | |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6101 | /* |
| 6102 | Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper functions. |
| 6103 | |
| 6104 | The table is ordered by offsets relative to the 'PyHeapTypeObject' structure, |
| 6105 | which incorporates the additional structures used for numbers, sequences and |
| 6106 | mappings. Note that multiple names may map to the same slot (e.g. __eq__, |
| 6107 | __ne__ etc. all map to tp_richcompare) and one name may map to multiple slots |
| 6108 | (e.g. __str__ affects tp_str as well as tp_repr). The table is terminated with |
| 6109 | an all-zero entry. (This table is further initialized in init_slotdefs().) |
| 6110 | */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6111 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6112 | typedef struct wrapperbase slotdef; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6113 | |
| 6114 | #undef TPSLOT |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 6115 | #undef FLSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6116 | #undef ETSLOT |
| 6117 | #undef SQSLOT |
| 6118 | #undef MPSLOT |
| 6119 | #undef NBSLOT |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6120 | #undef UNSLOT |
| 6121 | #undef IBSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6122 | #undef BINSLOT |
| 6123 | #undef RBINSLOT |
| 6124 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6125 | #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6126 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 6127 | PyDoc_STR(DOC)} |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 6128 | #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6129 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 6130 | PyDoc_STR(DOC), FLAGS} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6131 | #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6132 | {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 6133 | PyDoc_STR(DOC)} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6134 | #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6135 | ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6136 | #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6137 | ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6138 | #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6139 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6140 | #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6141 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6142 | NAME "($self, /)\n--\n\n" DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6143 | #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6144 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6145 | NAME "($self, value, /)\n--\n\nReturn self" DOC "value.") |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6146 | #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6147 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6148 | NAME "($self, value, /)\n--\n\nReturn self" DOC "value.") |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6149 | #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6150 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6151 | NAME "($self, value, /)\n--\n\nReturn value" DOC "self.") |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 6152 | #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6153 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6154 | NAME "($self, value, /)\n--\n\n" DOC) |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 6155 | #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6156 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6157 | NAME "($self, value, /)\n--\n\n" DOC) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6158 | |
| 6159 | static slotdef slotdefs[] = { |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6160 | TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), |
| 6161 | TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), |
| 6162 | TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), |
| 6163 | TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), |
| 6164 | TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6165 | "__repr__($self, /)\n--\n\nReturn repr(self)."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6166 | TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6167 | "__hash__($self, /)\n--\n\nReturn hash(self)."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6168 | FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6169 | "__call__($self, /, *args, **kwargs)\n--\n\nCall self as a function.", |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6170 | PyWrapperFlag_KEYWORDS), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6171 | TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6172 | "__str__($self, /)\n--\n\nReturn str(self)."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6173 | TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6174 | wrap_binaryfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6175 | "__getattribute__($self, name, /)\n--\n\nReturn getattr(self, name)."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6176 | TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), |
| 6177 | TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6178 | "__setattr__($self, name, value, /)\n--\n\nImplement setattr(self, name, value)."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6179 | TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6180 | "__delattr__($self, name, /)\n--\n\nImplement delattr(self, name)."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6181 | TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6182 | "__lt__($self, value, /)\n--\n\nReturn self<value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6183 | TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6184 | "__le__($self, value, /)\n--\n\nReturn self<=value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6185 | TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6186 | "__eq__($self, value, /)\n--\n\nReturn self==value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6187 | TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6188 | "__ne__($self, value, /)\n--\n\nReturn self!=value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6189 | TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6190 | "__gt__($self, value, /)\n--\n\nReturn self>value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6191 | TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, |
Zachary Ware | ea42b4c | 2014-04-18 09:14:31 -0500 | [diff] [blame] | 6192 | "__ge__($self, value, /)\n--\n\nReturn self>=value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6193 | TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6194 | "__iter__($self, /)\n--\n\nImplement iter(self)."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6195 | TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6196 | "__next__($self, /)\n--\n\nImplement next(self)."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6197 | TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6198 | "__get__($self, instance, owner, /)\n--\n\nReturn an attribute of instance, which is of type owner."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6199 | TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6200 | "__set__($self, instance, value, /)\n--\n\nSet an attribute of instance to value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6201 | TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6202 | wrap_descr_delete, |
Yury Selivanov | 056e265 | 2014-03-02 12:25:27 -0500 | [diff] [blame] | 6203 | "__delete__($self, instance, /)\n--\n\nDelete an attribute of instance."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6204 | FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6205 | "__init__($self, /, *args, **kwargs)\n--\n\n" |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6206 | "Initialize self. See help(type(self)) for accurate signature.", |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6207 | PyWrapperFlag_KEYWORDS), |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6208 | TPSLOT("__new__", tp_new, slot_tp_new, NULL, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6209 | "__new__(type, /, *args, **kwargs)\n--\n\n" |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6210 | "Create and return new object. See help(type) for accurate signature."), |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 6211 | TPSLOT("__del__", tp_finalize, slot_tp_finalize, (wrapperfunc)wrap_del, ""), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6213 | BINSLOT("__add__", nb_add, slot_nb_add, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6214 | "+"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6215 | RBINSLOT("__radd__", nb_add, slot_nb_add, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6216 | "+"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6217 | BINSLOT("__sub__", nb_subtract, slot_nb_subtract, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6218 | "-"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6219 | RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6220 | "-"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6221 | BINSLOT("__mul__", nb_multiply, slot_nb_multiply, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6222 | "*"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6223 | RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6224 | "*"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6225 | BINSLOT("__mod__", nb_remainder, slot_nb_remainder, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6226 | "%"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6227 | RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6228 | "%"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6229 | BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod, |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6230 | "Return divmod(self, value)."), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6231 | RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod, |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6232 | "Return divmod(value, self)."), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6233 | NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6234 | "__pow__($self, value, mod=None, /)\n--\n\nReturn pow(self, value, mod)."), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6235 | NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6236 | "__rpow__($self, value, mod=None, /)\n--\n\nReturn pow(value, self, mod)."), |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6237 | UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-self"), |
| 6238 | UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+self"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6239 | UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6240 | "abs(self)"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6241 | UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6242 | "self != 0"), |
| 6243 | UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~self"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6244 | BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 6245 | RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 6246 | BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 6247 | RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 6248 | BINSLOT("__and__", nb_and, slot_nb_and, "&"), |
| 6249 | RBINSLOT("__rand__", nb_and, slot_nb_and, "&"), |
| 6250 | BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"), |
| 6251 | RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), |
| 6252 | BINSLOT("__or__", nb_or, slot_nb_or, "|"), |
| 6253 | RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), |
| 6254 | UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6255 | "int(self)"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6256 | UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6257 | "float(self)"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6258 | IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 6259 | wrap_binaryfunc, "+="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6260 | IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 6261 | wrap_binaryfunc, "-="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6262 | IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 6263 | wrap_binaryfunc, "*="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6264 | IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 6265 | wrap_binaryfunc, "%="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6266 | IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 6267 | wrap_binaryfunc, "**="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6268 | IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 6269 | wrap_binaryfunc, "<<="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6270 | IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 6271 | wrap_binaryfunc, ">>="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6272 | IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 6273 | wrap_binaryfunc, "&="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6274 | IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 6275 | wrap_binaryfunc, "^="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6276 | IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 6277 | wrap_binaryfunc, "|="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6278 | BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 6279 | RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 6280 | BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 6281 | RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 6282 | IBSLOT("__ifloordiv__", nb_inplace_floor_divide, |
| 6283 | slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"), |
| 6284 | IBSLOT("__itruediv__", nb_inplace_true_divide, |
| 6285 | slot_nb_inplace_true_divide, wrap_binaryfunc, "/"), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6286 | NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6287 | "__index__($self, /)\n--\n\n" |
Zachary Ware | 715ef02 | 2014-04-18 09:23:14 -0500 | [diff] [blame] | 6288 | "Return self converted to an integer, if self is suitable " |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6289 | "for use as an index into a list."), |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 6290 | BINSLOT("__matmul__", nb_matrix_multiply, slot_nb_matrix_multiply, |
| 6291 | "@"), |
| 6292 | RBINSLOT("__rmatmul__", nb_matrix_multiply, slot_nb_matrix_multiply, |
| 6293 | "@"), |
| 6294 | IBSLOT("__imatmul__", nb_inplace_matrix_multiply, slot_nb_inplace_matrix_multiply, |
| 6295 | wrap_binaryfunc, "@="), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6296 | MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6297 | "__len__($self, /)\n--\n\nReturn len(self)."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6298 | MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, |
| 6299 | wrap_binaryfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6300 | "__getitem__($self, key, /)\n--\n\nReturn self[key]."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6301 | MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, |
| 6302 | wrap_objobjargproc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6303 | "__setitem__($self, key, value, /)\n--\n\nSet self[key] to value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6304 | MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, |
| 6305 | wrap_delitem, |
Yury Selivanov | 056e265 | 2014-03-02 12:25:27 -0500 | [diff] [blame] | 6306 | "__delitem__($self, key, /)\n--\n\nDelete self[key]."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6307 | |
| 6308 | SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6309 | "__len__($self, /)\n--\n\nReturn len(self)."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6310 | /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL. |
| 6311 | The logic in abstract.c always falls back to nb_add/nb_multiply in |
| 6312 | this case. Defining both the nb_* and the sq_* slots to call the |
| 6313 | user-defined methods has unexpected side-effects, as shown by |
| 6314 | test_descr.notimplemented() */ |
| 6315 | SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6316 | "__add__($self, value, /)\n--\n\nReturn self+value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6317 | SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6318 | "__mul__($self, value, /)\n--\n\nReturn self*value.n"), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6319 | SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6320 | "__rmul__($self, value, /)\n--\n\nReturn self*value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6321 | SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6322 | "__getitem__($self, key, /)\n--\n\nReturn self[key]."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6323 | SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6324 | "__setitem__($self, key, value, /)\n--\n\nSet self[key] to value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6325 | SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6326 | "__delitem__($self, key, /)\n--\n\nDelete self[key]."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6327 | SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6328 | "__contains__($self, key, /)\n--\n\nReturn key in self."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6329 | SQSLOT("__iadd__", sq_inplace_concat, NULL, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6330 | wrap_binaryfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6331 | "__iadd__($self, value, /)\n--\n\nImplement self+=value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6332 | SQSLOT("__imul__", sq_inplace_repeat, NULL, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6333 | wrap_indexargfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6334 | "__imul__($self, value, /)\n--\n\nImplement self*=value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6336 | {NULL} |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6337 | }; |
| 6338 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6339 | /* 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] | 6340 | 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] | 6341 | the offset to the type pointer, since it takes care to indirect through the |
| 6342 | proper indirection pointer (as_buffer, etc.); it returns NULL if the |
| 6343 | indirection pointer is NULL. */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6344 | static void ** |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6345 | slotptr(PyTypeObject *type, int ioffset) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6346 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6347 | char *ptr; |
| 6348 | long offset = ioffset; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6349 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6350 | /* Note: this depends on the order of the members of PyHeapTypeObject! */ |
| 6351 | assert(offset >= 0); |
| 6352 | assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer)); |
| 6353 | if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) { |
| 6354 | ptr = (char *)type->tp_as_sequence; |
| 6355 | offset -= offsetof(PyHeapTypeObject, as_sequence); |
| 6356 | } |
| 6357 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) { |
| 6358 | ptr = (char *)type->tp_as_mapping; |
| 6359 | offset -= offsetof(PyHeapTypeObject, as_mapping); |
| 6360 | } |
| 6361 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) { |
| 6362 | ptr = (char *)type->tp_as_number; |
| 6363 | offset -= offsetof(PyHeapTypeObject, as_number); |
| 6364 | } |
| 6365 | else { |
| 6366 | ptr = (char *)type; |
| 6367 | } |
| 6368 | if (ptr != NULL) |
| 6369 | ptr += offset; |
| 6370 | return (void **)ptr; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6371 | } |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 6372 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6373 | /* Length of array of slotdef pointers used to store slots with the |
| 6374 | same __name__. There should be at most MAX_EQUIV-1 slotdef entries with |
| 6375 | the same __name__, for any __name__. Since that's a static property, it is |
| 6376 | appropriate to declare fixed-size arrays for this. */ |
| 6377 | #define MAX_EQUIV 10 |
| 6378 | |
| 6379 | /* Return a slot pointer for a given name, but ONLY if the attribute has |
| 6380 | exactly one slot function. The name must be an interned string. */ |
| 6381 | static void ** |
| 6382 | resolve_slotdups(PyTypeObject *type, PyObject *name) |
| 6383 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6384 | /* 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] | 6385 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6386 | /* pname and ptrs act as a little cache */ |
| 6387 | static PyObject *pname; |
| 6388 | static slotdef *ptrs[MAX_EQUIV]; |
| 6389 | slotdef *p, **pp; |
| 6390 | void **res, **ptr; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6391 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6392 | if (pname != name) { |
| 6393 | /* Collect all slotdefs that match name into ptrs. */ |
| 6394 | pname = name; |
| 6395 | pp = ptrs; |
| 6396 | for (p = slotdefs; p->name_strobj; p++) { |
| 6397 | if (p->name_strobj == name) |
| 6398 | *pp++ = p; |
| 6399 | } |
| 6400 | *pp = NULL; |
| 6401 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6403 | /* Look in all matching slots of the type; if exactly one of these has |
| 6404 | a filled-in slot, return its value. Otherwise return NULL. */ |
| 6405 | res = NULL; |
| 6406 | for (pp = ptrs; *pp; pp++) { |
| 6407 | ptr = slotptr(type, (*pp)->offset); |
| 6408 | if (ptr == NULL || *ptr == NULL) |
| 6409 | continue; |
| 6410 | if (res != NULL) |
| 6411 | return NULL; |
| 6412 | res = ptr; |
| 6413 | } |
| 6414 | return res; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6415 | } |
| 6416 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6417 | /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6418 | does some incredibly complex thinking and then sticks something into the |
| 6419 | slot. (It sees if the adjacent slotdefs for the same slot have conflicting |
| 6420 | interests, and then stores a generic wrapper or a specific function into |
| 6421 | the slot.) Return a pointer to the next slotdef with a different offset, |
| 6422 | because that's convenient for fixup_slot_dispatchers(). */ |
| 6423 | static slotdef * |
| 6424 | update_one_slot(PyTypeObject *type, slotdef *p) |
| 6425 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6426 | PyObject *descr; |
| 6427 | PyWrapperDescrObject *d; |
| 6428 | void *generic = NULL, *specific = NULL; |
| 6429 | int use_generic = 0; |
| 6430 | int offset = p->offset; |
| 6431 | void **ptr = slotptr(type, offset); |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6432 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6433 | if (ptr == NULL) { |
| 6434 | do { |
| 6435 | ++p; |
| 6436 | } while (p->offset == offset); |
| 6437 | return p; |
| 6438 | } |
| 6439 | do { |
| 6440 | descr = _PyType_Lookup(type, p->name_strobj); |
| 6441 | if (descr == NULL) { |
| 6442 | if (ptr == (void**)&type->tp_iternext) { |
Trent Nelson | ab02db2 | 2012-09-18 21:58:03 -0400 | [diff] [blame] | 6443 | specific = (void *)_PyObject_NextNotImplemented; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6444 | } |
| 6445 | continue; |
| 6446 | } |
Benjamin Peterson | 7b16687 | 2012-04-24 11:06:25 -0400 | [diff] [blame] | 6447 | if (Py_TYPE(descr) == &PyWrapperDescr_Type && |
| 6448 | ((PyWrapperDescrObject *)descr)->d_base->name_strobj == p->name_strobj) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6449 | void **tptr = resolve_slotdups(type, p->name_strobj); |
| 6450 | if (tptr == NULL || tptr == ptr) |
| 6451 | generic = p->function; |
| 6452 | d = (PyWrapperDescrObject *)descr; |
| 6453 | if (d->d_base->wrapper == p->wrapper && |
| 6454 | PyType_IsSubtype(type, PyDescr_TYPE(d))) |
| 6455 | { |
| 6456 | if (specific == NULL || |
| 6457 | specific == d->d_wrapped) |
| 6458 | specific = d->d_wrapped; |
| 6459 | else |
| 6460 | use_generic = 1; |
| 6461 | } |
| 6462 | } |
| 6463 | else if (Py_TYPE(descr) == &PyCFunction_Type && |
| 6464 | PyCFunction_GET_FUNCTION(descr) == |
| 6465 | (PyCFunction)tp_new_wrapper && |
| 6466 | ptr == (void**)&type->tp_new) |
| 6467 | { |
| 6468 | /* The __new__ wrapper is not a wrapper descriptor, |
| 6469 | so must be special-cased differently. |
| 6470 | If we don't do this, creating an instance will |
| 6471 | always use slot_tp_new which will look up |
| 6472 | __new__ in the MRO which will call tp_new_wrapper |
| 6473 | which will look through the base classes looking |
| 6474 | for a static base and call its tp_new (usually |
| 6475 | PyType_GenericNew), after performing various |
| 6476 | sanity checks and constructing a new argument |
| 6477 | list. Cut all that nonsense short -- this speeds |
| 6478 | up instance creation tremendously. */ |
| 6479 | specific = (void *)type->tp_new; |
| 6480 | /* XXX I'm not 100% sure that there isn't a hole |
| 6481 | in this reasoning that requires additional |
| 6482 | sanity checks. I'll buy the first person to |
| 6483 | point out a bug in this reasoning a beer. */ |
| 6484 | } |
| 6485 | else if (descr == Py_None && |
| 6486 | ptr == (void**)&type->tp_hash) { |
| 6487 | /* We specifically allow __hash__ to be set to None |
| 6488 | to prevent inheritance of the default |
| 6489 | implementation from object.__hash__ */ |
Trent Nelson | ab02db2 | 2012-09-18 21:58:03 -0400 | [diff] [blame] | 6490 | specific = (void *)PyObject_HashNotImplemented; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6491 | } |
| 6492 | else { |
| 6493 | use_generic = 1; |
| 6494 | generic = p->function; |
| 6495 | } |
| 6496 | } while ((++p)->offset == offset); |
| 6497 | if (specific && !use_generic) |
| 6498 | *ptr = specific; |
| 6499 | else |
| 6500 | *ptr = generic; |
| 6501 | return p; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6502 | } |
| 6503 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6504 | /* In the type, update the slots whose slotdefs are gathered in the pp array. |
| 6505 | This is a callback for update_subclasses(). */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6506 | static int |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6507 | update_slots_callback(PyTypeObject *type, void *data) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6508 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6509 | slotdef **pp = (slotdef **)data; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6511 | for (; *pp; pp++) |
| 6512 | update_one_slot(type, *pp); |
| 6513 | return 0; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6514 | } |
| 6515 | |
Martin v. Löwis | 996b671 | 2014-07-26 16:44:07 +0200 | [diff] [blame] | 6516 | static int slotdefs_initialized = 0; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6517 | /* Initialize the slotdefs table by adding interned string objects for the |
Martin v. Löwis | 5b56150 | 2014-07-26 15:25:04 +0200 | [diff] [blame] | 6518 | names. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6519 | static void |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 6520 | init_slotdefs(void) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6521 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6522 | slotdef *p; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6523 | |
Martin v. Löwis | 996b671 | 2014-07-26 16:44:07 +0200 | [diff] [blame] | 6524 | if (slotdefs_initialized) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6525 | return; |
| 6526 | for (p = slotdefs; p->name; p++) { |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6527 | /* Slots must be ordered by their offset in the PyHeapTypeObject. */ |
| 6528 | assert(!p[1].name || p->offset <= p[1].offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6529 | p->name_strobj = PyUnicode_InternFromString(p->name); |
| 6530 | if (!p->name_strobj) |
| 6531 | Py_FatalError("Out of memory interning slotdef names"); |
| 6532 | } |
Martin v. Löwis | 996b671 | 2014-07-26 16:44:07 +0200 | [diff] [blame] | 6533 | slotdefs_initialized = 1; |
| 6534 | } |
| 6535 | |
| 6536 | /* Undo init_slotdefs, releasing the interned strings. */ |
Victor Stinner | 331a726 | 2014-07-27 16:11:30 +0200 | [diff] [blame] | 6537 | static void clear_slotdefs(void) |
Martin v. Löwis | 996b671 | 2014-07-26 16:44:07 +0200 | [diff] [blame] | 6538 | { |
| 6539 | slotdef *p; |
| 6540 | for (p = slotdefs; p->name; p++) { |
| 6541 | Py_CLEAR(p->name_strobj); |
| 6542 | } |
| 6543 | slotdefs_initialized = 0; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6544 | } |
| 6545 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6546 | /* Update the slots after assignment to a class (type) attribute. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6547 | static int |
| 6548 | update_slot(PyTypeObject *type, PyObject *name) |
| 6549 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6550 | slotdef *ptrs[MAX_EQUIV]; |
| 6551 | slotdef *p; |
| 6552 | slotdef **pp; |
| 6553 | int offset; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6555 | /* Clear the VALID_VERSION flag of 'type' and all its |
| 6556 | subclasses. This could possibly be unified with the |
| 6557 | update_subclasses() recursion below, but carefully: |
| 6558 | they each have their own conditions on which to stop |
| 6559 | recursing into subclasses. */ |
| 6560 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 6561 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6562 | init_slotdefs(); |
| 6563 | pp = ptrs; |
| 6564 | for (p = slotdefs; p->name; p++) { |
| 6565 | /* XXX assume name is interned! */ |
| 6566 | if (p->name_strobj == name) |
| 6567 | *pp++ = p; |
| 6568 | } |
| 6569 | *pp = NULL; |
| 6570 | for (pp = ptrs; *pp; pp++) { |
| 6571 | p = *pp; |
| 6572 | offset = p->offset; |
| 6573 | while (p > slotdefs && (p-1)->offset == offset) |
| 6574 | --p; |
| 6575 | *pp = p; |
| 6576 | } |
| 6577 | if (ptrs[0] == NULL) |
| 6578 | return 0; /* Not an attribute that affects any slots */ |
| 6579 | return update_subclasses(type, name, |
| 6580 | update_slots_callback, (void *)ptrs); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 6581 | } |
| 6582 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6583 | /* Store the proper functions in the slot dispatches at class (type) |
| 6584 | definition time, based upon which operations the class overrides in its |
| 6585 | dict. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6586 | static void |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6587 | fixup_slot_dispatchers(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6588 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6589 | slotdef *p; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6591 | init_slotdefs(); |
| 6592 | for (p = slotdefs; p->name; ) |
| 6593 | p = update_one_slot(type, p); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6594 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6595 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 6596 | static void |
| 6597 | update_all_slots(PyTypeObject* type) |
| 6598 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6599 | slotdef *p; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 6600 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6601 | init_slotdefs(); |
| 6602 | for (p = slotdefs; p->name; p++) { |
| 6603 | /* update_slot returns int but can't actually fail */ |
| 6604 | update_slot(type, p->name_strobj); |
| 6605 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 6606 | } |
| 6607 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6608 | /* recurse_down_subclasses() and update_subclasses() are mutually |
| 6609 | recursive functions to call a callback for all subclasses, |
| 6610 | but refraining from recursing into subclasses that define 'name'. */ |
| 6611 | |
| 6612 | static int |
| 6613 | update_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6614 | update_callback callback, void *data) |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6615 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6616 | if (callback(type, data) < 0) |
| 6617 | return -1; |
| 6618 | return recurse_down_subclasses(type, name, callback, data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6619 | } |
| 6620 | |
| 6621 | static int |
| 6622 | recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6623 | update_callback callback, void *data) |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6624 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6625 | PyTypeObject *subclass; |
| 6626 | PyObject *ref, *subclasses, *dict; |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 6627 | Py_ssize_t i; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6629 | subclasses = type->tp_subclasses; |
| 6630 | if (subclasses == NULL) |
| 6631 | return 0; |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 6632 | assert(PyDict_CheckExact(subclasses)); |
| 6633 | i = 0; |
| 6634 | while (PyDict_Next(subclasses, &i, NULL, &ref)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6635 | assert(PyWeakref_CheckRef(ref)); |
| 6636 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 6637 | assert(subclass != NULL); |
| 6638 | if ((PyObject *)subclass == Py_None) |
| 6639 | continue; |
| 6640 | assert(PyType_Check(subclass)); |
| 6641 | /* Avoid recursing down into unaffected classes */ |
| 6642 | dict = subclass->tp_dict; |
| 6643 | if (dict != NULL && PyDict_Check(dict) && |
| 6644 | PyDict_GetItem(dict, name) != NULL) |
| 6645 | continue; |
| 6646 | if (update_subclasses(subclass, name, callback, data) < 0) |
| 6647 | return -1; |
| 6648 | } |
| 6649 | return 0; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6650 | } |
| 6651 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6652 | /* This function is called by PyType_Ready() to populate the type's |
| 6653 | dictionary with method descriptors for function slots. For each |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6654 | function slot (like tp_repr) that's defined in the type, one or more |
| 6655 | corresponding descriptors are added in the type's tp_dict dictionary |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6656 | under the appropriate name (like __repr__). Some function slots |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6657 | cause more than one descriptor to be added (for example, the nb_add |
| 6658 | slot adds both __add__ and __radd__ descriptors) and some function |
| 6659 | slots compete for the same descriptor (for example both sq_item and |
| 6660 | mp_subscript generate a __getitem__ descriptor). |
| 6661 | |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 6662 | In the latter case, the first slotdef entry encountered wins. Since |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 6663 | 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] | 6664 | PyHeapTypeObject, this gives us some control over disambiguating |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 6665 | between competing slots: the members of PyHeapTypeObject are listed |
| 6666 | from most general to least general, so the most general slot is |
| 6667 | preferred. In particular, because as_mapping comes before as_sequence, |
| 6668 | for a type that defines both mp_subscript and sq_item, mp_subscript |
| 6669 | wins. |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 6670 | |
| 6671 | This only adds new descriptors and doesn't overwrite entries in |
| 6672 | tp_dict that were previously defined. The descriptors contain a |
| 6673 | reference to the C function they must call, so that it's safe if they |
| 6674 | are copied into a subtype's __dict__ and the subtype has a different |
| 6675 | C function in its slot -- calling the method defined by the |
| 6676 | descriptor will call the C function that was used to create it, |
| 6677 | rather than the C function present in the slot when it is called. |
| 6678 | (This is important because a subtype may have a C function in the |
| 6679 | slot that calls the method from the dictionary, and we want to avoid |
| 6680 | infinite recursion here.) */ |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6681 | |
| 6682 | static int |
| 6683 | add_operators(PyTypeObject *type) |
| 6684 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6685 | PyObject *dict = type->tp_dict; |
| 6686 | slotdef *p; |
| 6687 | PyObject *descr; |
| 6688 | void **ptr; |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6689 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6690 | init_slotdefs(); |
| 6691 | for (p = slotdefs; p->name; p++) { |
| 6692 | if (p->wrapper == NULL) |
| 6693 | continue; |
| 6694 | ptr = slotptr(type, p->offset); |
| 6695 | if (!ptr || !*ptr) |
| 6696 | continue; |
| 6697 | if (PyDict_GetItem(dict, p->name_strobj)) |
| 6698 | continue; |
Trent Nelson | ab02db2 | 2012-09-18 21:58:03 -0400 | [diff] [blame] | 6699 | if (*ptr == (void *)PyObject_HashNotImplemented) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6700 | /* Classes may prevent the inheritance of the tp_hash |
| 6701 | slot by storing PyObject_HashNotImplemented in it. Make it |
| 6702 | visible as a None value for the __hash__ attribute. */ |
| 6703 | if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0) |
| 6704 | return -1; |
| 6705 | } |
| 6706 | else { |
| 6707 | descr = PyDescr_NewWrapper(type, p, *ptr); |
| 6708 | if (descr == NULL) |
| 6709 | return -1; |
| 6710 | if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) |
| 6711 | return -1; |
| 6712 | Py_DECREF(descr); |
| 6713 | } |
| 6714 | } |
| 6715 | if (type->tp_new != NULL) { |
| 6716 | if (add_tp_new_wrapper(type) < 0) |
| 6717 | return -1; |
| 6718 | } |
| 6719 | return 0; |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6720 | } |
| 6721 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6722 | |
| 6723 | /* Cooperative 'super' */ |
| 6724 | |
| 6725 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6726 | PyObject_HEAD |
| 6727 | PyTypeObject *type; |
| 6728 | PyObject *obj; |
| 6729 | PyTypeObject *obj_type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6730 | } superobject; |
| 6731 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 6732 | static PyMemberDef super_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6733 | {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY, |
| 6734 | "the class invoking super()"}, |
| 6735 | {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY, |
| 6736 | "the instance invoking super(); may be None"}, |
| 6737 | {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY, |
| 6738 | "the type of the instance invoking super(); may be None"}, |
| 6739 | {0} |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6740 | }; |
| 6741 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6742 | static void |
| 6743 | super_dealloc(PyObject *self) |
| 6744 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6745 | superobject *su = (superobject *)self; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6746 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6747 | _PyObject_GC_UNTRACK(self); |
| 6748 | Py_XDECREF(su->obj); |
| 6749 | Py_XDECREF(su->type); |
| 6750 | Py_XDECREF(su->obj_type); |
| 6751 | Py_TYPE(self)->tp_free(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6752 | } |
| 6753 | |
| 6754 | static PyObject * |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6755 | super_repr(PyObject *self) |
| 6756 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6757 | superobject *su = (superobject *)self; |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6758 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6759 | if (su->obj_type) |
| 6760 | return PyUnicode_FromFormat( |
| 6761 | "<super: <class '%s'>, <%s object>>", |
| 6762 | su->type ? su->type->tp_name : "NULL", |
| 6763 | su->obj_type->tp_name); |
| 6764 | else |
| 6765 | return PyUnicode_FromFormat( |
| 6766 | "<super: <class '%s'>, NULL>", |
| 6767 | su->type ? su->type->tp_name : "NULL"); |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 6768 | } |
| 6769 | |
| 6770 | static PyObject * |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6771 | super_getattro(PyObject *self, PyObject *name) |
| 6772 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6773 | superobject *su = (superobject *)self; |
| 6774 | int skip = su->obj_type == NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6775 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6776 | if (!skip) { |
| 6777 | /* We want __class__ to return the class of the super object |
| 6778 | (i.e. super, or a subclass), not the class of su->obj. */ |
| 6779 | skip = (PyUnicode_Check(name) && |
Victor Stinner | ad14ccd | 2013-11-07 00:46:04 +0100 | [diff] [blame] | 6780 | PyUnicode_GET_LENGTH(name) == 9 && |
| 6781 | _PyUnicode_CompareWithId(name, &PyId___class__) == 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6782 | } |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 6783 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6784 | if (!skip) { |
| 6785 | PyObject *mro, *res, *tmp, *dict; |
| 6786 | PyTypeObject *starttype; |
| 6787 | descrgetfunc f; |
| 6788 | Py_ssize_t i, n; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6789 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6790 | starttype = su->obj_type; |
| 6791 | mro = starttype->tp_mro; |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 6792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6793 | if (mro == NULL) |
| 6794 | n = 0; |
| 6795 | else { |
| 6796 | assert(PyTuple_Check(mro)); |
| 6797 | n = PyTuple_GET_SIZE(mro); |
| 6798 | } |
| 6799 | for (i = 0; i < n; i++) { |
| 6800 | if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) |
| 6801 | break; |
| 6802 | } |
| 6803 | i++; |
| 6804 | res = NULL; |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 6805 | /* keep a strong reference to mro because starttype->tp_mro can be |
| 6806 | replaced during PyDict_GetItem(dict, name) */ |
| 6807 | Py_INCREF(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6808 | for (; i < n; i++) { |
| 6809 | tmp = PyTuple_GET_ITEM(mro, i); |
| 6810 | if (PyType_Check(tmp)) |
| 6811 | dict = ((PyTypeObject *)tmp)->tp_dict; |
| 6812 | else |
| 6813 | continue; |
| 6814 | res = PyDict_GetItem(dict, name); |
| 6815 | if (res != NULL) { |
| 6816 | Py_INCREF(res); |
| 6817 | f = Py_TYPE(res)->tp_descr_get; |
| 6818 | if (f != NULL) { |
| 6819 | tmp = f(res, |
| 6820 | /* Only pass 'obj' param if |
| 6821 | this is instance-mode super |
| 6822 | (See SF ID #743627) |
| 6823 | */ |
| 6824 | (su->obj == (PyObject *) |
| 6825 | su->obj_type |
| 6826 | ? (PyObject *)NULL |
| 6827 | : su->obj), |
| 6828 | (PyObject *)starttype); |
| 6829 | Py_DECREF(res); |
| 6830 | res = tmp; |
| 6831 | } |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 6832 | Py_DECREF(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6833 | return res; |
| 6834 | } |
| 6835 | } |
Victor Stinner | d74782b | 2012-03-09 00:39:08 +0100 | [diff] [blame] | 6836 | Py_DECREF(mro); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6837 | } |
| 6838 | return PyObject_GenericGetAttr(self, name); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6839 | } |
| 6840 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6841 | static PyTypeObject * |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6842 | supercheck(PyTypeObject *type, PyObject *obj) |
| 6843 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6844 | /* Check that a super() call makes sense. Return a type object. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6845 | |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 6846 | obj can be a class, or an instance of one: |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6847 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6848 | - If it is a class, it must be a subclass of 'type'. This case is |
| 6849 | used for class methods; the return value is obj. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6850 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6851 | - If it is an instance, it must be an instance of 'type'. This is |
| 6852 | the normal case; the return value is obj.__class__. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6853 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6854 | But... when obj is an instance, we want to allow for the case where |
| 6855 | Py_TYPE(obj) is not a subclass of type, but obj.__class__ is! |
| 6856 | This will allow using super() with a proxy for obj. |
| 6857 | */ |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6858 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6859 | /* Check for first bullet above (special case) */ |
| 6860 | if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) { |
| 6861 | Py_INCREF(obj); |
| 6862 | return (PyTypeObject *)obj; |
| 6863 | } |
Guido van Rossum | 8e80a72 | 2003-02-18 19:22:22 +0000 | [diff] [blame] | 6864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6865 | /* Normal case */ |
| 6866 | if (PyType_IsSubtype(Py_TYPE(obj), type)) { |
| 6867 | Py_INCREF(Py_TYPE(obj)); |
| 6868 | return Py_TYPE(obj); |
| 6869 | } |
| 6870 | else { |
| 6871 | /* Try the slow way */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6872 | PyObject *class_attr; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6873 | |
Martin v. Löwis | bfc6d74 | 2011-10-13 20:03:57 +0200 | [diff] [blame] | 6874 | class_attr = _PyObject_GetAttrId(obj, &PyId___class__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6875 | if (class_attr != NULL && |
| 6876 | PyType_Check(class_attr) && |
| 6877 | (PyTypeObject *)class_attr != Py_TYPE(obj)) |
| 6878 | { |
| 6879 | int ok = PyType_IsSubtype( |
| 6880 | (PyTypeObject *)class_attr, type); |
| 6881 | if (ok) |
| 6882 | return (PyTypeObject *)class_attr; |
| 6883 | } |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6884 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6885 | if (class_attr == NULL) |
| 6886 | PyErr_Clear(); |
| 6887 | else |
| 6888 | Py_DECREF(class_attr); |
| 6889 | } |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 6890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6891 | PyErr_SetString(PyExc_TypeError, |
| 6892 | "super(type, obj): " |
| 6893 | "obj must be an instance or subtype of type"); |
| 6894 | return NULL; |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 6895 | } |
| 6896 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6897 | static PyObject * |
| 6898 | super_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 6899 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6900 | superobject *su = (superobject *)self; |
| 6901 | superobject *newobj; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6903 | if (obj == NULL || obj == Py_None || su->obj != NULL) { |
| 6904 | /* Not binding to an object, or already bound */ |
| 6905 | Py_INCREF(self); |
| 6906 | return self; |
| 6907 | } |
| 6908 | if (Py_TYPE(su) != &PySuper_Type) |
| 6909 | /* If su is an instance of a (strict) subclass of super, |
| 6910 | call its type */ |
| 6911 | return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su), |
| 6912 | su->type, obj, NULL); |
| 6913 | else { |
| 6914 | /* Inline the common case */ |
| 6915 | PyTypeObject *obj_type = supercheck(su->type, obj); |
| 6916 | if (obj_type == NULL) |
| 6917 | return NULL; |
| 6918 | newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type, |
| 6919 | NULL, NULL); |
| 6920 | if (newobj == NULL) |
| 6921 | return NULL; |
| 6922 | Py_INCREF(su->type); |
| 6923 | Py_INCREF(obj); |
| 6924 | newobj->type = su->type; |
| 6925 | newobj->obj = obj; |
| 6926 | newobj->obj_type = obj_type; |
| 6927 | return (PyObject *)newobj; |
| 6928 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6929 | } |
| 6930 | |
| 6931 | static int |
| 6932 | super_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 6933 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6934 | superobject *su = (superobject *)self; |
| 6935 | PyTypeObject *type = NULL; |
| 6936 | PyObject *obj = NULL; |
| 6937 | PyTypeObject *obj_type = NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 6938 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6939 | if (!_PyArg_NoKeywords("super", kwds)) |
| 6940 | return -1; |
| 6941 | if (!PyArg_ParseTuple(args, "|O!O:super", &PyType_Type, &type, &obj)) |
| 6942 | return -1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6943 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6944 | if (type == NULL) { |
| 6945 | /* Call super(), without args -- fill in from __class__ |
| 6946 | and first local variable on the stack. */ |
Victor Stinner | 1c6970f | 2014-05-13 01:32:36 +0200 | [diff] [blame] | 6947 | PyFrameObject *f; |
| 6948 | PyCodeObject *co; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 6949 | Py_ssize_t i, n; |
Victor Stinner | 1c6970f | 2014-05-13 01:32:36 +0200 | [diff] [blame] | 6950 | f = PyThreadState_GET()->frame; |
| 6951 | if (f == NULL) { |
| 6952 | PyErr_SetString(PyExc_RuntimeError, |
| 6953 | "super(): no current frame"); |
| 6954 | return -1; |
| 6955 | } |
| 6956 | co = f->f_code; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6957 | if (co == NULL) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6958 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6959 | "super(): no code object"); |
| 6960 | return -1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6961 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6962 | if (co->co_argcount == 0) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6963 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6964 | "super(): no arguments"); |
| 6965 | return -1; |
| 6966 | } |
| 6967 | obj = f->f_localsplus[0]; |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 6968 | if (obj == NULL && co->co_cell2arg) { |
| 6969 | /* The first argument might be a cell. */ |
| 6970 | n = PyTuple_GET_SIZE(co->co_cellvars); |
| 6971 | for (i = 0; i < n; i++) { |
| 6972 | if (co->co_cell2arg[i] == 0) { |
| 6973 | PyObject *cell = f->f_localsplus[co->co_nlocals + i]; |
| 6974 | assert(PyCell_Check(cell)); |
| 6975 | obj = PyCell_GET(cell); |
| 6976 | break; |
| 6977 | } |
| 6978 | } |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 6979 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6980 | if (obj == NULL) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6981 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6982 | "super(): arg[0] deleted"); |
| 6983 | return -1; |
| 6984 | } |
| 6985 | if (co->co_freevars == NULL) |
| 6986 | n = 0; |
| 6987 | else { |
| 6988 | assert(PyTuple_Check(co->co_freevars)); |
| 6989 | n = PyTuple_GET_SIZE(co->co_freevars); |
| 6990 | } |
| 6991 | for (i = 0; i < n; i++) { |
| 6992 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 6993 | assert(PyUnicode_Check(name)); |
Victor Stinner | ad14ccd | 2013-11-07 00:46:04 +0100 | [diff] [blame] | 6994 | if (!_PyUnicode_CompareWithId(name, &PyId___class__)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6995 | Py_ssize_t index = co->co_nlocals + |
| 6996 | PyTuple_GET_SIZE(co->co_cellvars) + i; |
| 6997 | PyObject *cell = f->f_localsplus[index]; |
| 6998 | if (cell == NULL || !PyCell_Check(cell)) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 6999 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7000 | "super(): bad __class__ cell"); |
| 7001 | return -1; |
| 7002 | } |
| 7003 | type = (PyTypeObject *) PyCell_GET(cell); |
| 7004 | if (type == NULL) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 7005 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7006 | "super(): empty __class__ cell"); |
| 7007 | return -1; |
| 7008 | } |
| 7009 | if (!PyType_Check(type)) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 7010 | PyErr_Format(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7011 | "super(): __class__ is not a type (%s)", |
| 7012 | Py_TYPE(type)->tp_name); |
| 7013 | return -1; |
| 7014 | } |
| 7015 | break; |
| 7016 | } |
| 7017 | } |
| 7018 | if (type == NULL) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 7019 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7020 | "super(): __class__ cell not found"); |
| 7021 | return -1; |
| 7022 | } |
| 7023 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7024 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7025 | if (obj == Py_None) |
| 7026 | obj = NULL; |
| 7027 | if (obj != NULL) { |
| 7028 | obj_type = supercheck(type, obj); |
| 7029 | if (obj_type == NULL) |
| 7030 | return -1; |
| 7031 | Py_INCREF(obj); |
| 7032 | } |
| 7033 | Py_INCREF(type); |
| 7034 | su->type = type; |
| 7035 | su->obj = obj; |
| 7036 | su->obj_type = obj_type; |
| 7037 | return 0; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7038 | } |
| 7039 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7040 | PyDoc_STRVAR(super_doc, |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7041 | "super() -> same as super(__class__, <first argument>)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7042 | "super(type) -> unbound super object\n" |
| 7043 | "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 7044 | "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7045 | "Typical use to call a cooperative superclass method:\n" |
| 7046 | "class C(B):\n" |
| 7047 | " def meth(self, arg):\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7048 | " super().meth(arg)\n" |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7049 | "This works for class methods too:\n" |
| 7050 | "class C(B):\n" |
| 7051 | " @classmethod\n" |
| 7052 | " def cmeth(cls, arg):\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7053 | " super().cmeth(arg)\n"); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7054 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 7055 | static int |
| 7056 | super_traverse(PyObject *self, visitproc visit, void *arg) |
| 7057 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7058 | superobject *su = (superobject *)self; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 7059 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7060 | Py_VISIT(su->obj); |
| 7061 | Py_VISIT(su->type); |
| 7062 | Py_VISIT(su->obj_type); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 7063 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7064 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 7065 | } |
| 7066 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7067 | PyTypeObject PySuper_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7068 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 7069 | "super", /* tp_name */ |
| 7070 | sizeof(superobject), /* tp_basicsize */ |
| 7071 | 0, /* tp_itemsize */ |
| 7072 | /* methods */ |
| 7073 | super_dealloc, /* tp_dealloc */ |
| 7074 | 0, /* tp_print */ |
| 7075 | 0, /* tp_getattr */ |
| 7076 | 0, /* tp_setattr */ |
| 7077 | 0, /* tp_reserved */ |
| 7078 | super_repr, /* tp_repr */ |
| 7079 | 0, /* tp_as_number */ |
| 7080 | 0, /* tp_as_sequence */ |
| 7081 | 0, /* tp_as_mapping */ |
| 7082 | 0, /* tp_hash */ |
| 7083 | 0, /* tp_call */ |
| 7084 | 0, /* tp_str */ |
| 7085 | super_getattro, /* tp_getattro */ |
| 7086 | 0, /* tp_setattro */ |
| 7087 | 0, /* tp_as_buffer */ |
| 7088 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 7089 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 7090 | super_doc, /* tp_doc */ |
| 7091 | super_traverse, /* tp_traverse */ |
| 7092 | 0, /* tp_clear */ |
| 7093 | 0, /* tp_richcompare */ |
| 7094 | 0, /* tp_weaklistoffset */ |
| 7095 | 0, /* tp_iter */ |
| 7096 | 0, /* tp_iternext */ |
| 7097 | 0, /* tp_methods */ |
| 7098 | super_members, /* tp_members */ |
| 7099 | 0, /* tp_getset */ |
| 7100 | 0, /* tp_base */ |
| 7101 | 0, /* tp_dict */ |
| 7102 | super_descr_get, /* tp_descr_get */ |
| 7103 | 0, /* tp_descr_set */ |
| 7104 | 0, /* tp_dictoffset */ |
| 7105 | super_init, /* tp_init */ |
| 7106 | PyType_GenericAlloc, /* tp_alloc */ |
| 7107 | PyType_GenericNew, /* tp_new */ |
| 7108 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7109 | }; |