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" |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 4 | #include "internal/pystate.h" |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 5 | #include "frameobject.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 8 | #include <ctype.h> |
| 9 | |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 10 | /*[clinic input] |
| 11 | class type "PyTypeObject *" "&PyType_Type" |
| 12 | class object "PyObject *" "&PyBaseObject_Type" |
| 13 | [clinic start generated code]*/ |
| 14 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b94608d231c434b]*/ |
| 15 | |
| 16 | #include "clinic/typeobject.c.h" |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 17 | |
| 18 | /* Support type attribute cache */ |
| 19 | |
| 20 | /* The cache can keep references to the names alive for longer than |
| 21 | they normally would. This is why the maximum size is limited to |
| 22 | MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large |
| 23 | strings are used as attribute names. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 24 | #define MCACHE_MAX_ATTR_SIZE 100 |
Antoine Pitrou | 2a40e36 | 2014-11-15 00:56:27 +0100 | [diff] [blame] | 25 | #define MCACHE_SIZE_EXP 12 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 26 | #define MCACHE_HASH(version, name_hash) \ |
Antoine Pitrou | 2a40e36 | 2014-11-15 00:56:27 +0100 | [diff] [blame] | 27 | (((unsigned int)(version) ^ (unsigned int)(name_hash)) \ |
| 28 | & ((1 << MCACHE_SIZE_EXP) - 1)) |
| 29 | |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 30 | #define MCACHE_HASH_METHOD(type, name) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 31 | MCACHE_HASH((type)->tp_version_tag, \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 32 | ((PyASCIIObject *)(name))->hash) |
Serhiy Storchaka | e3b2b4b | 2017-09-08 09:58:51 +0300 | [diff] [blame] | 33 | #define MCACHE_CACHEABLE_NAME(name) \ |
| 34 | PyUnicode_CheckExact(name) && \ |
| 35 | PyUnicode_IS_READY(name) && \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 36 | PyUnicode_GET_LENGTH(name) <= MCACHE_MAX_ATTR_SIZE |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 37 | |
| 38 | struct method_cache_entry { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 39 | unsigned int version; |
| 40 | PyObject *name; /* reference to exactly a str or None */ |
| 41 | PyObject *value; /* borrowed */ |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP]; |
| 45 | static unsigned int next_version_tag = 0; |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 46 | |
Antoine Pitrou | 2a40e36 | 2014-11-15 00:56:27 +0100 | [diff] [blame] | 47 | #define MCACHE_STATS 0 |
| 48 | |
| 49 | #if MCACHE_STATS |
| 50 | static size_t method_cache_hits = 0; |
| 51 | static size_t method_cache_misses = 0; |
| 52 | static size_t method_cache_collisions = 0; |
| 53 | #endif |
| 54 | |
Martin v. Löwis | e75fc14 | 2013-11-07 18:46:53 +0100 | [diff] [blame] | 55 | /* alphabetical order */ |
| 56 | _Py_IDENTIFIER(__abstractmethods__); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 57 | _Py_IDENTIFIER(__class__); |
Serhiy Storchaka | ce5b0e9 | 2018-01-05 00:21:41 +0200 | [diff] [blame] | 58 | _Py_IDENTIFIER(__class_getitem__); |
Martin v. Löwis | e75fc14 | 2013-11-07 18:46:53 +0100 | [diff] [blame] | 59 | _Py_IDENTIFIER(__delitem__); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 60 | _Py_IDENTIFIER(__dict__); |
| 61 | _Py_IDENTIFIER(__doc__); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 62 | _Py_IDENTIFIER(__getattribute__); |
Martin v. Löwis | e75fc14 | 2013-11-07 18:46:53 +0100 | [diff] [blame] | 63 | _Py_IDENTIFIER(__getitem__); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 64 | _Py_IDENTIFIER(__hash__); |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 65 | _Py_IDENTIFIER(__init_subclass__); |
Martin v. Löwis | e75fc14 | 2013-11-07 18:46:53 +0100 | [diff] [blame] | 66 | _Py_IDENTIFIER(__len__); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 67 | _Py_IDENTIFIER(__module__); |
| 68 | _Py_IDENTIFIER(__name__); |
| 69 | _Py_IDENTIFIER(__new__); |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 70 | _Py_IDENTIFIER(__set_name__); |
Martin v. Löwis | e75fc14 | 2013-11-07 18:46:53 +0100 | [diff] [blame] | 71 | _Py_IDENTIFIER(__setitem__); |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 72 | _Py_IDENTIFIER(builtins); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 73 | |
| 74 | static PyObject * |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 75 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 76 | |
Martin v. Löwis | 996b671 | 2014-07-26 16:44:07 +0200 | [diff] [blame] | 77 | static void |
Victor Stinner | 331a726 | 2014-07-27 16:11:30 +0200 | [diff] [blame] | 78 | clear_slotdefs(void); |
Martin v. Löwis | 996b671 | 2014-07-26 16:44:07 +0200 | [diff] [blame] | 79 | |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 80 | /* |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 81 | * finds the beginning of the docstring's introspection signature. |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 82 | * if present, returns a pointer pointing to the first '('. |
| 83 | * otherwise returns NULL. |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 84 | * |
| 85 | * doesn't guarantee that the signature is valid, only that it |
| 86 | * has a valid prefix. (the signature must also pass skip_signature.) |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 87 | */ |
| 88 | static const char * |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 89 | find_signature(const char *name, const char *doc) |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 90 | { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 91 | const char *dot; |
| 92 | size_t length; |
| 93 | |
| 94 | if (!doc) |
| 95 | return NULL; |
| 96 | |
| 97 | assert(name != NULL); |
| 98 | |
| 99 | /* for dotted names like classes, only use the last component */ |
| 100 | dot = strrchr(name, '.'); |
| 101 | if (dot) |
| 102 | name = dot + 1; |
| 103 | |
| 104 | length = strlen(name); |
| 105 | if (strncmp(doc, name, length)) |
| 106 | return NULL; |
| 107 | doc += length; |
| 108 | if (*doc != '(') |
| 109 | return NULL; |
| 110 | return doc; |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 113 | #define SIGNATURE_END_MARKER ")\n--\n\n" |
| 114 | #define SIGNATURE_END_MARKER_LENGTH 6 |
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 | * skips past the end of the docstring's instrospection signature. |
| 117 | * (assumes doc starts with a valid signature prefix.) |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 118 | */ |
| 119 | static const char * |
| 120 | skip_signature(const char *doc) |
| 121 | { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 122 | while (*doc) { |
| 123 | if ((*doc == *SIGNATURE_END_MARKER) && |
| 124 | !strncmp(doc, SIGNATURE_END_MARKER, SIGNATURE_END_MARKER_LENGTH)) |
| 125 | return doc + SIGNATURE_END_MARKER_LENGTH; |
| 126 | if ((*doc == '\n') && (doc[1] == '\n')) |
| 127 | return NULL; |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 128 | doc++; |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 129 | } |
| 130 | return NULL; |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 131 | } |
| 132 | |
T. Wouters | a00c3fd | 2017-03-31 09:14:41 -0700 | [diff] [blame] | 133 | #ifndef NDEBUG |
Victor Stinner | bda5a2b | 2017-01-25 23:33:27 +0100 | [diff] [blame] | 134 | static int |
| 135 | _PyType_CheckConsistency(PyTypeObject *type) |
| 136 | { |
| 137 | if (!(type->tp_flags & Py_TPFLAGS_READY)) { |
| 138 | /* don't check types before PyType_Ready() */ |
| 139 | return 1; |
| 140 | } |
| 141 | |
| 142 | assert(!(type->tp_flags & Py_TPFLAGS_READYING)); |
| 143 | assert(type->tp_mro != NULL && PyTuple_Check(type->tp_mro)); |
| 144 | assert(type->tp_dict != NULL); |
| 145 | return 1; |
| 146 | } |
| 147 | #endif |
| 148 | |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 149 | static const char * |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 150 | _PyType_DocWithoutSignature(const char *name, const char *internal_doc) |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 151 | { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 152 | const char *doc = find_signature(name, internal_doc); |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 153 | |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 154 | if (doc) { |
| 155 | doc = skip_signature(doc); |
| 156 | if (doc) |
| 157 | return doc; |
| 158 | } |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 159 | return internal_doc; |
| 160 | } |
| 161 | |
| 162 | PyObject * |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 163 | _PyType_GetDocFromInternalDoc(const char *name, const char *internal_doc) |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 164 | { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 165 | const char *doc = _PyType_DocWithoutSignature(name, internal_doc); |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 166 | |
Zachary Ware | 8ef887c | 2015-04-13 18:22:35 -0500 | [diff] [blame] | 167 | if (!doc || *doc == '\0') { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 168 | Py_RETURN_NONE; |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | return PyUnicode_FromString(doc); |
| 172 | } |
| 173 | |
| 174 | PyObject * |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 175 | _PyType_GetTextSignatureFromInternalDoc(const char *name, const char *internal_doc) |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 176 | { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 177 | const char *start = find_signature(name, internal_doc); |
| 178 | const char *end; |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 179 | |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 180 | if (start) |
| 181 | end = skip_signature(start); |
| 182 | else |
| 183 | end = NULL; |
| 184 | if (!end) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 185 | Py_RETURN_NONE; |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 188 | /* back "end" up until it points just past the final ')' */ |
| 189 | end -= SIGNATURE_END_MARKER_LENGTH - 1; |
| 190 | assert((end - start) >= 2); /* should be "()" at least */ |
| 191 | assert(end[-1] == ')'); |
| 192 | assert(end[0] == '\n'); |
| 193 | return PyUnicode_FromStringAndSize(start, end - start); |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 196 | unsigned int |
| 197 | PyType_ClearCache(void) |
| 198 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | Py_ssize_t i; |
| 200 | unsigned int cur_version_tag = next_version_tag - 1; |
| 201 | |
Antoine Pitrou | 2a40e36 | 2014-11-15 00:56:27 +0100 | [diff] [blame] | 202 | #if MCACHE_STATS |
| 203 | size_t total = method_cache_hits + method_cache_collisions + method_cache_misses; |
| 204 | fprintf(stderr, "-- Method cache hits = %zd (%d%%)\n", |
| 205 | method_cache_hits, (int) (100.0 * method_cache_hits / total)); |
| 206 | fprintf(stderr, "-- Method cache true misses = %zd (%d%%)\n", |
| 207 | method_cache_misses, (int) (100.0 * method_cache_misses / total)); |
| 208 | fprintf(stderr, "-- Method cache collisions = %zd (%d%%)\n", |
| 209 | method_cache_collisions, (int) (100.0 * method_cache_collisions / total)); |
Victor Stinner | 8c663fd | 2017-11-08 14:44:44 -0800 | [diff] [blame] | 210 | fprintf(stderr, "-- Method cache size = %zd KiB\n", |
Antoine Pitrou | 2a40e36 | 2014-11-15 00:56:27 +0100 | [diff] [blame] | 211 | sizeof(method_cache) / 1024); |
| 212 | #endif |
| 213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { |
| 215 | method_cache[i].version = 0; |
| 216 | Py_CLEAR(method_cache[i].name); |
| 217 | method_cache[i].value = NULL; |
| 218 | } |
| 219 | next_version_tag = 0; |
| 220 | /* mark all version tags as invalid */ |
| 221 | PyType_Modified(&PyBaseObject_Type); |
| 222 | return cur_version_tag; |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 223 | } |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 224 | |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 225 | void |
Antoine Pitrou | 957a23b | 2013-05-04 20:45:02 +0200 | [diff] [blame] | 226 | _PyType_Fini(void) |
| 227 | { |
| 228 | PyType_ClearCache(); |
Martin v. Löwis | 996b671 | 2014-07-26 16:44:07 +0200 | [diff] [blame] | 229 | clear_slotdefs(); |
Antoine Pitrou | 957a23b | 2013-05-04 20:45:02 +0200 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 233 | PyType_Modified(PyTypeObject *type) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 234 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | /* Invalidate any cached data for the specified type and all |
| 236 | subclasses. This function is called after the base |
| 237 | classes, mro, or attributes of the type are altered. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | Invariants: |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 241 | - Py_TPFLAGS_VALID_VERSION_TAG is never set if |
| 242 | Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type |
| 243 | objects coming from non-recompiled extension modules) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 244 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type, |
| 246 | it must first be set on all super types. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 247 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 248 | This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a |
| 249 | type (so it must first clear it on all subclasses). The |
| 250 | tp_version_tag value is meaningless unless this flag is set. |
| 251 | We don't assign new version tags eagerly, but only as |
| 252 | needed. |
| 253 | */ |
| 254 | PyObject *raw, *ref; |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 255 | Py_ssize_t i; |
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(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
| 258 | return; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 260 | raw = type->tp_subclasses; |
| 261 | if (raw != NULL) { |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 262 | assert(PyDict_CheckExact(raw)); |
| 263 | i = 0; |
| 264 | while (PyDict_Next(raw, &i, NULL, &ref)) { |
| 265 | assert(PyWeakref_CheckRef(ref)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 266 | ref = PyWeakref_GET_OBJECT(ref); |
| 267 | if (ref != Py_None) { |
| 268 | PyType_Modified((PyTypeObject *)ref); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static void |
| 276 | type_mro_modified(PyTypeObject *type, PyObject *bases) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 277 | /* |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 278 | 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] | 279 | able to be cached. This function is called after the base |
| 280 | classes or mro of the type are altered. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 281 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 282 | Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 283 | has a custom MRO that includes a type which is not officially |
| 284 | super type. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 285 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 286 | Called from mro_internal, which will subsequently be called on |
| 287 | each subclass when their mro is recursively updated. |
| 288 | */ |
| 289 | Py_ssize_t i, n; |
| 290 | int clear = 0; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 291 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) |
| 293 | return; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 | n = PyTuple_GET_SIZE(bases); |
| 296 | for (i = 0; i < n; i++) { |
| 297 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 298 | PyTypeObject *cls; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 299 | |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 300 | assert(PyType_Check(b)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | cls = (PyTypeObject *)b; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) || |
| 304 | !PyType_IsSubtype(type, cls)) { |
| 305 | clear = 1; |
| 306 | break; |
| 307 | } |
| 308 | } |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 | if (clear) |
| 311 | type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG| |
| 312 | Py_TPFLAGS_VALID_VERSION_TAG); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | static int |
| 316 | assign_version_tag(PyTypeObject *type) |
| 317 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | /* Ensure that the tp_version_tag is valid and set |
| 319 | Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this |
| 320 | must first be done on all super classes. Return 0 if this |
| 321 | cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG. |
| 322 | */ |
| 323 | Py_ssize_t i, n; |
| 324 | PyObject *bases; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
| 327 | return 1; |
| 328 | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) |
| 329 | return 0; |
| 330 | if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) |
| 331 | return 0; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | type->tp_version_tag = next_version_tag++; |
| 334 | /* for stress-testing: next_version_tag &= 0xFF; */ |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 | if (type->tp_version_tag == 0) { |
| 337 | /* wrap-around or just starting Python - clear the whole |
| 338 | cache by filling names with references to Py_None. |
| 339 | Values are also set to NULL for added protection, as they |
| 340 | are borrowed reference */ |
| 341 | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { |
| 342 | method_cache[i].value = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 343 | Py_INCREF(Py_None); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 344 | Py_XSETREF(method_cache[i].name, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 345 | } |
| 346 | /* mark all version tags as invalid */ |
| 347 | PyType_Modified(&PyBaseObject_Type); |
| 348 | return 1; |
| 349 | } |
| 350 | bases = type->tp_bases; |
| 351 | n = PyTuple_GET_SIZE(bases); |
| 352 | for (i = 0; i < n; i++) { |
| 353 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 354 | assert(PyType_Check(b)); |
| 355 | if (!assign_version_tag((PyTypeObject *)b)) |
| 356 | return 0; |
| 357 | } |
| 358 | type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG; |
| 359 | return 1; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 363 | static PyMemberDef type_members[] = { |
Benjamin Peterson | 0e10206 | 2010-08-25 23:13:17 +0000 | [diff] [blame] | 364 | {"__basicsize__", T_PYSSIZET, offsetof(PyTypeObject,tp_basicsize),READONLY}, |
| 365 | {"__itemsize__", T_PYSSIZET, offsetof(PyTypeObject, tp_itemsize), READONLY}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY}, |
| 367 | {"__weakrefoffset__", T_LONG, |
| 368 | offsetof(PyTypeObject, tp_weaklistoffset), READONLY}, |
| 369 | {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY}, |
| 370 | {"__dictoffset__", T_LONG, |
| 371 | offsetof(PyTypeObject, tp_dictoffset), READONLY}, |
| 372 | {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY}, |
| 373 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 374 | }; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 375 | |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 376 | static int |
| 377 | check_set_special_type_attr(PyTypeObject *type, PyObject *value, const char *name) |
| 378 | { |
| 379 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 380 | PyErr_Format(PyExc_TypeError, |
| 381 | "can't set %s.%s", type->tp_name, name); |
| 382 | return 0; |
| 383 | } |
| 384 | if (!value) { |
| 385 | PyErr_Format(PyExc_TypeError, |
| 386 | "can't delete %s.%s", type->tp_name, name); |
| 387 | return 0; |
| 388 | } |
| 389 | return 1; |
| 390 | } |
| 391 | |
Serhiy Storchaka | 4ab46d7 | 2017-09-17 21:11:04 +0300 | [diff] [blame] | 392 | const char * |
| 393 | _PyType_Name(PyTypeObject *type) |
| 394 | { |
| 395 | const char *s = strrchr(type->tp_name, '.'); |
| 396 | if (s == NULL) { |
| 397 | s = type->tp_name; |
| 398 | } |
| 399 | else { |
| 400 | s++; |
| 401 | } |
| 402 | return s; |
| 403 | } |
| 404 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 405 | static PyObject * |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 406 | type_name(PyTypeObject *type, void *context) |
| 407 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 408 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 409 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 410 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 411 | Py_INCREF(et->ht_name); |
| 412 | return et->ht_name; |
| 413 | } |
| 414 | else { |
Serhiy Storchaka | 4ab46d7 | 2017-09-17 21:11:04 +0300 | [diff] [blame] | 415 | return PyUnicode_FromString(_PyType_Name(type)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 416 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 419 | static PyObject * |
| 420 | type_qualname(PyTypeObject *type, void *context) |
| 421 | { |
| 422 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 423 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
| 424 | Py_INCREF(et->ht_qualname); |
| 425 | return et->ht_qualname; |
| 426 | } |
| 427 | else { |
Serhiy Storchaka | 4ab46d7 | 2017-09-17 21:11:04 +0300 | [diff] [blame] | 428 | return PyUnicode_FromString(_PyType_Name(type)); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 432 | static int |
| 433 | type_set_name(PyTypeObject *type, PyObject *value, void *context) |
| 434 | { |
Serhiy Storchaka | 42bf8fc | 2015-12-30 21:40:49 +0200 | [diff] [blame] | 435 | const char *tp_name; |
| 436 | Py_ssize_t name_size; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 437 | |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 438 | if (!check_set_special_type_attr(type, value, "__name__")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 439 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | if (!PyUnicode_Check(value)) { |
| 441 | PyErr_Format(PyExc_TypeError, |
| 442 | "can only assign string to %s.__name__, not '%s'", |
| 443 | type->tp_name, Py_TYPE(value)->tp_name); |
| 444 | return -1; |
| 445 | } |
Guido van Rossum | e845c0f | 2007-11-02 23:07:07 +0000 | [diff] [blame] | 446 | |
Serhiy Storchaka | 42bf8fc | 2015-12-30 21:40:49 +0200 | [diff] [blame] | 447 | tp_name = PyUnicode_AsUTF8AndSize(value, &name_size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 448 | if (tp_name == NULL) |
| 449 | return -1; |
Serhiy Storchaka | 42bf8fc | 2015-12-30 21:40:49 +0200 | [diff] [blame] | 450 | if (strlen(tp_name) != (size_t)name_size) { |
| 451 | PyErr_SetString(PyExc_ValueError, |
| 452 | "type name must not contain null characters"); |
| 453 | return -1; |
| 454 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 455 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 456 | type->tp_name = tp_name; |
Serhiy Storchaka | 1ed017a | 2015-12-27 15:51:32 +0200 | [diff] [blame] | 457 | Py_INCREF(value); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 458 | Py_SETREF(((PyHeapTypeObject*)type)->ht_name, value); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 459 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 460 | return 0; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 463 | static int |
| 464 | type_set_qualname(PyTypeObject *type, PyObject *value, void *context) |
| 465 | { |
| 466 | PyHeapTypeObject* et; |
| 467 | |
Benjamin Peterson | 2c05a2e | 2012-10-31 00:01:15 -0400 | [diff] [blame] | 468 | if (!check_set_special_type_attr(type, value, "__qualname__")) |
| 469 | return -1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 470 | if (!PyUnicode_Check(value)) { |
| 471 | PyErr_Format(PyExc_TypeError, |
| 472 | "can only assign string to %s.__qualname__, not '%s'", |
| 473 | type->tp_name, Py_TYPE(value)->tp_name); |
| 474 | return -1; |
| 475 | } |
| 476 | |
| 477 | et = (PyHeapTypeObject*)type; |
| 478 | Py_INCREF(value); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 479 | Py_SETREF(et->ht_qualname, value); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 480 | return 0; |
| 481 | } |
| 482 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 483 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 484 | type_module(PyTypeObject *type, void *context) |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 485 | { |
Serhiy Storchaka | 7c19aff | 2016-09-10 00:53:02 +0300 | [diff] [blame] | 486 | PyObject *mod; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 487 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 488 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
Serhiy Storchaka | 7c19aff | 2016-09-10 00:53:02 +0300 | [diff] [blame] | 489 | mod = _PyDict_GetItemId(type->tp_dict, &PyId___module__); |
| 490 | if (mod == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 491 | PyErr_Format(PyExc_AttributeError, "__module__"); |
Serhiy Storchaka | 7c19aff | 2016-09-10 00:53:02 +0300 | [diff] [blame] | 492 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | } |
Serhiy Storchaka | 27ba886 | 2016-05-25 16:14:55 +0300 | [diff] [blame] | 494 | Py_INCREF(mod); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 495 | } |
| 496 | else { |
Serhiy Storchaka | 7c19aff | 2016-09-10 00:53:02 +0300 | [diff] [blame] | 497 | const char *s = strrchr(type->tp_name, '.'); |
| 498 | if (s != NULL) { |
| 499 | mod = PyUnicode_FromStringAndSize( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 500 | type->tp_name, (Py_ssize_t)(s - type->tp_name)); |
Serhiy Storchaka | 7c19aff | 2016-09-10 00:53:02 +0300 | [diff] [blame] | 501 | if (mod != NULL) |
| 502 | PyUnicode_InternInPlace(&mod); |
| 503 | } |
| 504 | else { |
| 505 | mod = _PyUnicode_FromId(&PyId_builtins); |
| 506 | Py_XINCREF(mod); |
| 507 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | } |
Serhiy Storchaka | 7c19aff | 2016-09-10 00:53:02 +0300 | [diff] [blame] | 509 | return mod; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 512 | static int |
| 513 | type_set_module(PyTypeObject *type, PyObject *value, void *context) |
| 514 | { |
Benjamin Peterson | d9f23d2 | 2011-08-17 11:54:03 -0500 | [diff] [blame] | 515 | if (!check_set_special_type_attr(type, value, "__module__")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 519 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 520 | return _PyDict_SetItemId(type->tp_dict, &PyId___module__, value); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 523 | static PyObject * |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 524 | type_abstractmethods(PyTypeObject *type, void *context) |
| 525 | { |
Benjamin Peterson | aec5fd1 | 2010-10-02 00:03:31 +0000 | [diff] [blame] | 526 | PyObject *mod = NULL; |
Benjamin Peterson | 84060b8 | 2010-10-03 02:13:39 +0000 | [diff] [blame] | 527 | /* type itself has an __abstractmethods__ descriptor (this). Don't return |
| 528 | that. */ |
Benjamin Peterson | aec5fd1 | 2010-10-02 00:03:31 +0000 | [diff] [blame] | 529 | if (type != &PyType_Type) |
Victor Stinner | 3688aa9 | 2013-11-06 18:59:18 +0100 | [diff] [blame] | 530 | mod = _PyDict_GetItemId(type->tp_dict, &PyId___abstractmethods__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | if (!mod) { |
Victor Stinner | 3688aa9 | 2013-11-06 18:59:18 +0100 | [diff] [blame] | 532 | PyObject *message = _PyUnicode_FromId(&PyId___abstractmethods__); |
| 533 | if (message) |
| 534 | PyErr_SetObject(PyExc_AttributeError, message); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | return NULL; |
| 536 | } |
Serhiy Storchaka | 27ba886 | 2016-05-25 16:14:55 +0300 | [diff] [blame] | 537 | Py_INCREF(mod); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 538 | return mod; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | static int |
| 542 | type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context) |
| 543 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 544 | /* __abstractmethods__ should only be set once on a type, in |
| 545 | abc.ABCMeta.__new__, so this function doesn't do anything |
| 546 | special to update subclasses. |
| 547 | */ |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 548 | int abstract, res; |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 549 | if (value != NULL) { |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 550 | abstract = PyObject_IsTrue(value); |
| 551 | if (abstract < 0) |
| 552 | return -1; |
Victor Stinner | 3688aa9 | 2013-11-06 18:59:18 +0100 | [diff] [blame] | 553 | res = _PyDict_SetItemId(type->tp_dict, &PyId___abstractmethods__, value); |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 554 | } |
| 555 | else { |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 556 | abstract = 0; |
Victor Stinner | 3688aa9 | 2013-11-06 18:59:18 +0100 | [diff] [blame] | 557 | res = _PyDict_DelItemId(type->tp_dict, &PyId___abstractmethods__); |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 558 | if (res && PyErr_ExceptionMatches(PyExc_KeyError)) { |
Victor Stinner | 3688aa9 | 2013-11-06 18:59:18 +0100 | [diff] [blame] | 559 | PyObject *message = _PyUnicode_FromId(&PyId___abstractmethods__); |
| 560 | if (message) |
| 561 | PyErr_SetObject(PyExc_AttributeError, message); |
Benjamin Peterson | 477ba91 | 2011-01-12 15:34:01 +0000 | [diff] [blame] | 562 | return -1; |
| 563 | } |
| 564 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 565 | if (res == 0) { |
| 566 | PyType_Modified(type); |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 567 | if (abstract) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 568 | type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT; |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 569 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 570 | type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 571 | } |
| 572 | return res; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | static PyObject * |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 576 | type_get_bases(PyTypeObject *type, void *context) |
| 577 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 578 | Py_INCREF(type->tp_bases); |
| 579 | return type->tp_bases; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | static PyTypeObject *best_base(PyObject *); |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 583 | static int mro_internal(PyTypeObject *, PyObject **); |
Benjamin Peterson | 6cb526e | 2016-09-09 12:42:51 -0700 | [diff] [blame] | 584 | static int type_is_subtype_base_chain(PyTypeObject *, PyTypeObject *); |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 585 | static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, const char *); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 586 | static int add_subclass(PyTypeObject*, PyTypeObject*); |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 587 | static int add_all_subclasses(PyTypeObject *type, PyObject *bases); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 588 | static void remove_subclass(PyTypeObject *, PyTypeObject *); |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 589 | static void remove_all_subclasses(PyTypeObject *type, PyObject *bases); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 590 | static void update_all_slots(PyTypeObject *); |
| 591 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 592 | typedef int (*update_callback)(PyTypeObject *, void *); |
| 593 | static int update_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 594 | update_callback callback, void *data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 595 | static int recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 596 | update_callback callback, void *data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 597 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 598 | static int |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 599 | mro_hierarchy(PyTypeObject *type, PyObject *temp) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 600 | { |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 601 | int res; |
| 602 | PyObject *new_mro, *old_mro; |
| 603 | PyObject *tuple; |
| 604 | PyObject *subclasses; |
| 605 | Py_ssize_t i, n; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 606 | |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 607 | res = mro_internal(type, &old_mro); |
| 608 | if (res <= 0) |
| 609 | /* error / reentrance */ |
| 610 | return res; |
| 611 | new_mro = type->tp_mro; |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 612 | |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 613 | if (old_mro != NULL) |
| 614 | tuple = PyTuple_Pack(3, type, new_mro, old_mro); |
| 615 | else |
| 616 | tuple = PyTuple_Pack(2, type, new_mro); |
| 617 | |
| 618 | if (tuple != NULL) |
| 619 | res = PyList_Append(temp, tuple); |
| 620 | else |
| 621 | res = -1; |
| 622 | Py_XDECREF(tuple); |
| 623 | |
| 624 | if (res < 0) { |
| 625 | type->tp_mro = old_mro; |
| 626 | Py_DECREF(new_mro); |
| 627 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 628 | } |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 629 | Py_XDECREF(old_mro); |
| 630 | |
| 631 | /* Obtain a copy of subclasses list to iterate over. |
| 632 | |
| 633 | Otherwise type->tp_subclasses might be altered |
| 634 | in the middle of the loop, for example, through a custom mro(), |
| 635 | by invoking type_set_bases on some subclass of the type |
| 636 | which in turn calls remove_subclass/add_subclass on this type. |
| 637 | |
| 638 | Finally, this makes things simple avoiding the need to deal |
| 639 | with dictionary iterators and weak references. |
| 640 | */ |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 641 | subclasses = type___subclasses___impl(type); |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 642 | if (subclasses == NULL) |
| 643 | return -1; |
| 644 | n = PyList_GET_SIZE(subclasses); |
| 645 | for (i = 0; i < n; i++) { |
| 646 | PyTypeObject *subclass; |
| 647 | subclass = (PyTypeObject *)PyList_GET_ITEM(subclasses, i); |
| 648 | res = mro_hierarchy(subclass, temp); |
| 649 | if (res < 0) |
| 650 | break; |
| 651 | } |
| 652 | Py_DECREF(subclasses); |
| 653 | |
| 654 | return res; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | static int |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 658 | type_set_bases(PyTypeObject *type, PyObject *new_bases, void *context) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 659 | { |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 660 | int res = 0; |
| 661 | PyObject *temp; |
| 662 | PyObject *old_bases; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 663 | PyTypeObject *new_base, *old_base; |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 664 | Py_ssize_t i; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 665 | |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 666 | if (!check_set_special_type_attr(type, new_bases, "__bases__")) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 667 | return -1; |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 668 | if (!PyTuple_Check(new_bases)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 669 | PyErr_Format(PyExc_TypeError, |
| 670 | "can only assign tuple to %s.__bases__, not %s", |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 671 | type->tp_name, Py_TYPE(new_bases)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 672 | return -1; |
| 673 | } |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 674 | if (PyTuple_GET_SIZE(new_bases) == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 675 | PyErr_Format(PyExc_TypeError, |
| 676 | "can only assign non-empty tuple to %s.__bases__, not ()", |
| 677 | type->tp_name); |
| 678 | return -1; |
| 679 | } |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 680 | for (i = 0; i < PyTuple_GET_SIZE(new_bases); i++) { |
| 681 | PyObject *ob; |
| 682 | PyTypeObject *base; |
| 683 | |
| 684 | ob = PyTuple_GET_ITEM(new_bases, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 685 | if (!PyType_Check(ob)) { |
Benjamin Peterson | b6af60c | 2012-04-01 18:49:54 -0400 | [diff] [blame] | 686 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 9ee601e | 2012-04-01 18:51:37 -0400 | [diff] [blame] | 687 | "%s.__bases__ must be tuple of classes, not '%s'", |
Benjamin Peterson | b6af60c | 2012-04-01 18:49:54 -0400 | [diff] [blame] | 688 | type->tp_name, Py_TYPE(ob)->tp_name); |
| 689 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 690 | } |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 691 | |
| 692 | base = (PyTypeObject*)ob; |
| 693 | if (PyType_IsSubtype(base, type) || |
| 694 | /* In case of reentering here again through a custom mro() |
| 695 | the above check is not enough since it relies on |
| 696 | base->tp_mro which would gonna be updated inside |
| 697 | mro_internal only upon returning from the mro(). |
| 698 | |
| 699 | However, base->tp_base has already been assigned (see |
| 700 | below), which in turn may cause an inheritance cycle |
| 701 | through tp_base chain. And this is definitely |
| 702 | not what you want to ever happen. */ |
| 703 | (base->tp_mro != NULL && type_is_subtype_base_chain(base, type))) { |
| 704 | |
Benjamin Peterson | 3471bb6 | 2012-04-01 18:48:40 -0400 | [diff] [blame] | 705 | PyErr_SetString(PyExc_TypeError, |
| 706 | "a __bases__ item causes an inheritance cycle"); |
| 707 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 708 | } |
| 709 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 710 | |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 711 | new_base = best_base(new_bases); |
| 712 | if (new_base == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 714 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 715 | if (!compatible_for_assignment(type->tp_base, new_base, "__bases__")) |
| 716 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 717 | |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 718 | Py_INCREF(new_bases); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 719 | Py_INCREF(new_base); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 720 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 721 | old_bases = type->tp_bases; |
| 722 | old_base = type->tp_base; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 723 | |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 724 | type->tp_bases = new_bases; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 725 | type->tp_base = new_base; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 726 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 727 | temp = PyList_New(0); |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 728 | if (temp == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 729 | goto bail; |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 730 | if (mro_hierarchy(type, temp) < 0) |
| 731 | goto undo; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 732 | Py_DECREF(temp); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 733 | |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 734 | /* Take no action in case if type->tp_bases has been replaced |
| 735 | through reentrance. */ |
| 736 | if (type->tp_bases == new_bases) { |
| 737 | /* any base that was in __bases__ but now isn't, we |
| 738 | need to remove |type| from its tp_subclasses. |
| 739 | conversely, any class now in __bases__ that wasn't |
| 740 | needs to have |type| added to its subclasses. */ |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 741 | |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 742 | /* for now, sod that: just remove from all old_bases, |
| 743 | add to all new_bases */ |
| 744 | remove_all_subclasses(type, old_bases); |
| 745 | res = add_all_subclasses(type, new_bases); |
| 746 | update_all_slots(type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 747 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 748 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 749 | Py_DECREF(old_bases); |
| 750 | Py_DECREF(old_base); |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 751 | |
Victor Stinner | bda5a2b | 2017-01-25 23:33:27 +0100 | [diff] [blame] | 752 | assert(_PyType_CheckConsistency(type)); |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 753 | return res; |
| 754 | |
| 755 | undo: |
| 756 | for (i = PyList_GET_SIZE(temp) - 1; i >= 0; i--) { |
| 757 | PyTypeObject *cls; |
| 758 | PyObject *new_mro, *old_mro = NULL; |
| 759 | |
| 760 | PyArg_UnpackTuple(PyList_GET_ITEM(temp, i), |
| 761 | "", 2, 3, &cls, &new_mro, &old_mro); |
| 762 | /* Do not rollback if cls has a newer version of MRO. */ |
| 763 | if (cls->tp_mro == new_mro) { |
| 764 | Py_XINCREF(old_mro); |
| 765 | cls->tp_mro = old_mro; |
| 766 | Py_DECREF(new_mro); |
| 767 | } |
| 768 | } |
| 769 | Py_DECREF(temp); |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 770 | |
| 771 | bail: |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 772 | if (type->tp_bases == new_bases) { |
| 773 | assert(type->tp_base == new_base); |
Michael W. Hudson | e723e45 | 2003-08-07 14:58:10 +0000 | [diff] [blame] | 774 | |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 775 | type->tp_bases = old_bases; |
| 776 | type->tp_base = old_base; |
| 777 | |
| 778 | Py_DECREF(new_bases); |
| 779 | Py_DECREF(new_base); |
| 780 | } |
| 781 | else { |
| 782 | Py_DECREF(old_bases); |
| 783 | Py_DECREF(old_base); |
| 784 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 785 | |
Victor Stinner | bda5a2b | 2017-01-25 23:33:27 +0100 | [diff] [blame] | 786 | assert(_PyType_CheckConsistency(type)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 787 | return -1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 791 | type_dict(PyTypeObject *type, void *context) |
| 792 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 793 | if (type->tp_dict == NULL) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 794 | Py_RETURN_NONE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 795 | } |
| 796 | return PyDictProxy_New(type->tp_dict); |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 799 | static PyObject * |
| 800 | type_get_doc(PyTypeObject *type, void *context) |
| 801 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 802 | PyObject *result; |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 803 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 804 | return _PyType_GetDocFromInternalDoc(type->tp_name, type->tp_doc); |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 805 | } |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 806 | result = _PyDict_GetItemId(type->tp_dict, &PyId___doc__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 807 | if (result == NULL) { |
| 808 | result = Py_None; |
| 809 | Py_INCREF(result); |
| 810 | } |
| 811 | else if (Py_TYPE(result)->tp_descr_get) { |
| 812 | result = Py_TYPE(result)->tp_descr_get(result, NULL, |
| 813 | (PyObject *)type); |
| 814 | } |
| 815 | else { |
| 816 | Py_INCREF(result); |
| 817 | } |
| 818 | return result; |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 821 | static PyObject * |
| 822 | type_get_text_signature(PyTypeObject *type, void *context) |
| 823 | { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 824 | return _PyType_GetTextSignatureFromInternalDoc(type->tp_name, type->tp_doc); |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 825 | } |
| 826 | |
Benjamin Peterson | 01fc6cd | 2011-08-17 12:03:47 -0500 | [diff] [blame] | 827 | static int |
| 828 | type_set_doc(PyTypeObject *type, PyObject *value, void *context) |
| 829 | { |
| 830 | if (!check_set_special_type_attr(type, value, "__doc__")) |
| 831 | return -1; |
| 832 | PyType_Modified(type); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 833 | return _PyDict_SetItemId(type->tp_dict, &PyId___doc__, value); |
Benjamin Peterson | 01fc6cd | 2011-08-17 12:03:47 -0500 | [diff] [blame] | 834 | } |
| 835 | |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 836 | /*[clinic input] |
| 837 | type.__instancecheck__ -> bool |
| 838 | |
| 839 | instance: object |
| 840 | / |
| 841 | |
| 842 | Check if an object is an instance. |
| 843 | [clinic start generated code]*/ |
| 844 | |
| 845 | static int |
| 846 | type___instancecheck___impl(PyTypeObject *self, PyObject *instance) |
| 847 | /*[clinic end generated code: output=08b6bf5f591c3618 input=cdbfeaee82c01a0f]*/ |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 848 | { |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 849 | return _PyObject_RealIsInstance(instance, (PyObject *)self); |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 852 | /*[clinic input] |
| 853 | type.__subclasscheck__ -> bool |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 854 | |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 855 | subclass: object |
| 856 | / |
| 857 | |
| 858 | Check if a class is a subclass. |
| 859 | [clinic start generated code]*/ |
| 860 | |
| 861 | static int |
| 862 | type___subclasscheck___impl(PyTypeObject *self, PyObject *subclass) |
| 863 | /*[clinic end generated code: output=97a4e51694500941 input=071b2ca9e03355f4]*/ |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 864 | { |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 865 | return _PyObject_RealIsSubclass(subclass, (PyObject *)self); |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 868 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 869 | static PyGetSetDef type_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 870 | {"__name__", (getter)type_name, (setter)type_set_name, NULL}, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 871 | {"__qualname__", (getter)type_qualname, (setter)type_set_qualname, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 872 | {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL}, |
| 873 | {"__module__", (getter)type_module, (setter)type_set_module, NULL}, |
| 874 | {"__abstractmethods__", (getter)type_abstractmethods, |
| 875 | (setter)type_set_abstractmethods, NULL}, |
| 876 | {"__dict__", (getter)type_dict, NULL, NULL}, |
Benjamin Peterson | 01fc6cd | 2011-08-17 12:03:47 -0500 | [diff] [blame] | 877 | {"__doc__", (getter)type_get_doc, (setter)type_set_doc, NULL}, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 878 | {"__text_signature__", (getter)type_get_text_signature, NULL, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 880 | }; |
| 881 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 882 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 883 | type_repr(PyTypeObject *type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 884 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 885 | PyObject *mod, *name, *rtn; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 887 | mod = type_module(type, NULL); |
| 888 | if (mod == NULL) |
| 889 | PyErr_Clear(); |
| 890 | else if (!PyUnicode_Check(mod)) { |
| 891 | Py_DECREF(mod); |
| 892 | mod = NULL; |
| 893 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 894 | name = type_qualname(type, NULL); |
Christian Heimes | a0e7e41 | 2012-09-10 03:00:14 +0200 | [diff] [blame] | 895 | if (name == NULL) { |
| 896 | Py_XDECREF(mod); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | return NULL; |
Christian Heimes | a0e7e41 | 2012-09-10 03:00:14 +0200 | [diff] [blame] | 898 | } |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 899 | |
Serhiy Storchaka | f5894dd | 2016-11-16 15:40:39 +0200 | [diff] [blame] | 900 | if (mod != NULL && !_PyUnicode_EqualToASCIIId(mod, &PyId_builtins)) |
Benjamin Peterson | ab078e9 | 2016-07-13 21:13:29 -0700 | [diff] [blame] | 901 | rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name); |
| 902 | else |
| 903 | rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 904 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 905 | Py_XDECREF(mod); |
| 906 | Py_DECREF(name); |
| 907 | return rtn; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 910 | static PyObject * |
| 911 | type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 912 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 913 | PyObject *obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 915 | if (type->tp_new == NULL) { |
| 916 | PyErr_Format(PyExc_TypeError, |
| 917 | "cannot create '%.100s' instances", |
| 918 | type->tp_name); |
| 919 | return NULL; |
| 920 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 921 | |
Victor Stinner | 33824f6 | 2013-08-26 14:05:19 +0200 | [diff] [blame] | 922 | #ifdef Py_DEBUG |
| 923 | /* type_call() must not be called with an exception set, |
Victor Stinner | a8cb515 | 2017-01-18 14:12:51 +0100 | [diff] [blame] | 924 | because it can clear it (directly or indirectly) and so the |
Martin Panter | 9955a37 | 2015-10-07 10:26:23 +0000 | [diff] [blame] | 925 | caller loses its exception */ |
Victor Stinner | 33824f6 | 2013-08-26 14:05:19 +0200 | [diff] [blame] | 926 | assert(!PyErr_Occurred()); |
| 927 | #endif |
| 928 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | obj = type->tp_new(type, args, kwds); |
Victor Stinner | 99bb14b | 2015-09-03 12:16:49 +0200 | [diff] [blame] | 930 | obj = _Py_CheckFunctionResult((PyObject*)type, obj, NULL); |
| 931 | if (obj == NULL) |
| 932 | return NULL; |
| 933 | |
| 934 | /* Ugly exception: when the call was type(something), |
| 935 | don't call tp_init on the result. */ |
| 936 | if (type == &PyType_Type && |
| 937 | PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && |
| 938 | (kwds == NULL || |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 939 | (PyDict_Check(kwds) && PyDict_GET_SIZE(kwds) == 0))) |
Victor Stinner | 99bb14b | 2015-09-03 12:16:49 +0200 | [diff] [blame] | 940 | return obj; |
| 941 | |
| 942 | /* If the returned object is not an instance of type, |
| 943 | it won't be initialized. */ |
| 944 | if (!PyType_IsSubtype(Py_TYPE(obj), type)) |
| 945 | return obj; |
| 946 | |
| 947 | type = Py_TYPE(obj); |
| 948 | if (type->tp_init != NULL) { |
| 949 | int res = type->tp_init(obj, args, kwds); |
| 950 | if (res < 0) { |
| 951 | assert(PyErr_Occurred()); |
| 952 | Py_DECREF(obj); |
| 953 | obj = NULL; |
| 954 | } |
| 955 | else { |
| 956 | assert(!PyErr_Occurred()); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 963 | PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 964 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 965 | PyObject *obj; |
| 966 | const size_t size = _PyObject_VAR_SIZE(type, nitems+1); |
| 967 | /* note that we need to add one, for the sentinel */ |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 968 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 969 | if (PyType_IS_GC(type)) |
| 970 | obj = _PyObject_GC_Malloc(size); |
| 971 | else |
| 972 | obj = (PyObject *)PyObject_MALLOC(size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 973 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 974 | if (obj == NULL) |
| 975 | return PyErr_NoMemory(); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 976 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 977 | memset(obj, '\0', size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 980 | Py_INCREF(type); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 981 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 982 | if (type->tp_itemsize == 0) |
Christian Heimes | d3afe78 | 2013-12-04 09:27:47 +0100 | [diff] [blame] | 983 | (void)PyObject_INIT(obj, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 984 | else |
| 985 | (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 986 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 987 | if (PyType_IS_GC(type)) |
| 988 | _PyObject_GC_TRACK(obj); |
| 989 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | PyObject * |
| 993 | PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 994 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 995 | return type->tp_alloc(type, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 998 | /* Helpers for subtyping */ |
| 999 | |
| 1000 | static int |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1001 | traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg) |
| 1002 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1003 | Py_ssize_t i, n; |
| 1004 | PyMemberDef *mp; |
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 | n = Py_SIZE(type); |
| 1007 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
| 1008 | for (i = 0; i < n; i++, mp++) { |
| 1009 | if (mp->type == T_OBJECT_EX) { |
| 1010 | char *addr = (char *)self + mp->offset; |
| 1011 | PyObject *obj = *(PyObject **)addr; |
| 1012 | if (obj != NULL) { |
| 1013 | int err = visit(obj, arg); |
| 1014 | if (err) |
| 1015 | return err; |
| 1016 | } |
| 1017 | } |
| 1018 | } |
| 1019 | return 0; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | static int |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 1023 | subtype_traverse(PyObject *self, visitproc visit, void *arg) |
| 1024 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1025 | PyTypeObject *type, *base; |
| 1026 | traverseproc basetraverse; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 1027 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | /* Find the nearest base with a different tp_traverse, |
| 1029 | and traverse slots while we're at it */ |
| 1030 | type = Py_TYPE(self); |
| 1031 | base = type; |
| 1032 | while ((basetraverse = base->tp_traverse) == subtype_traverse) { |
| 1033 | if (Py_SIZE(base)) { |
| 1034 | int err = traverse_slots(base, self, visit, arg); |
| 1035 | if (err) |
| 1036 | return err; |
| 1037 | } |
| 1038 | base = base->tp_base; |
| 1039 | assert(base); |
| 1040 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 1041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 1043 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 1044 | if (dictptr && *dictptr) |
| 1045 | Py_VISIT(*dictptr); |
| 1046 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 1047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1048 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 1049 | /* For a heaptype, the instances count as references |
| 1050 | to the type. Traverse the type so the collector |
| 1051 | can find cycles involving this link. */ |
| 1052 | Py_VISIT(type); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 1053 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1054 | if (basetraverse) |
| 1055 | return basetraverse(self, visit, arg); |
| 1056 | return 0; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | static void |
| 1060 | clear_slots(PyTypeObject *type, PyObject *self) |
| 1061 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1062 | Py_ssize_t i, n; |
| 1063 | PyMemberDef *mp; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1065 | n = Py_SIZE(type); |
| 1066 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
| 1067 | for (i = 0; i < n; i++, mp++) { |
| 1068 | if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) { |
| 1069 | char *addr = (char *)self + mp->offset; |
| 1070 | PyObject *obj = *(PyObject **)addr; |
| 1071 | if (obj != NULL) { |
| 1072 | *(PyObject **)addr = NULL; |
| 1073 | Py_DECREF(obj); |
| 1074 | } |
| 1075 | } |
| 1076 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | static int |
| 1080 | subtype_clear(PyObject *self) |
| 1081 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1082 | PyTypeObject *type, *base; |
| 1083 | inquiry baseclear; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1084 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1085 | /* Find the nearest base with a different tp_clear |
| 1086 | and clear slots while we're at it */ |
| 1087 | type = Py_TYPE(self); |
| 1088 | base = type; |
| 1089 | while ((baseclear = base->tp_clear) == subtype_clear) { |
| 1090 | if (Py_SIZE(base)) |
| 1091 | clear_slots(base, self); |
| 1092 | base = base->tp_base; |
| 1093 | assert(base); |
| 1094 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1095 | |
Benjamin Peterson | 52c4243 | 2012-03-07 18:41:11 -0600 | [diff] [blame] | 1096 | /* Clear the instance dict (if any), to break cycles involving only |
| 1097 | __dict__ slots (as in the case 'self.__dict__ is self'). */ |
| 1098 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 1099 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 1100 | if (dictptr && *dictptr) |
| 1101 | Py_CLEAR(*dictptr); |
| 1102 | } |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1104 | if (baseclear) |
| 1105 | return baseclear(self); |
| 1106 | return 0; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 1107 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1108 | |
| 1109 | static void |
| 1110 | subtype_dealloc(PyObject *self) |
| 1111 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1112 | PyTypeObject *type, *base; |
| 1113 | destructor basedealloc; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 1114 | PyThreadState *tstate = PyThreadState_GET(); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1115 | int has_finalizer; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1117 | /* Extract the type; we expect it to be a heap type */ |
| 1118 | type = Py_TYPE(self); |
| 1119 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1121 | /* Test whether the type has GC exactly once */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1123 | if (!PyType_IS_GC(type)) { |
| 1124 | /* It's really rare to find a dynamic type that doesn't have |
| 1125 | GC; it can only happen when deriving from 'object' and not |
| 1126 | adding any slots or instance variables. This allows |
| 1127 | certain simplifications: there's no need to call |
| 1128 | clear_slots(), or DECREF the dict, or clear weakrefs. */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1129 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1130 | /* Maybe call finalizer; exit early if resurrected */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1131 | if (type->tp_finalize) { |
| 1132 | if (PyObject_CallFinalizerFromDealloc(self) < 0) |
| 1133 | return; |
| 1134 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1135 | if (type->tp_del) { |
| 1136 | type->tp_del(self); |
| 1137 | if (self->ob_refcnt > 0) |
| 1138 | return; |
| 1139 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1141 | /* Find the nearest base with a different tp_dealloc */ |
| 1142 | base = type; |
| 1143 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 1144 | assert(Py_SIZE(base) == 0); |
| 1145 | base = base->tp_base; |
| 1146 | assert(base); |
| 1147 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1149 | /* Extract the type again; tp_del may have changed it */ |
| 1150 | type = Py_TYPE(self); |
Benjamin Peterson | 193152c | 2009-04-25 01:08:45 +0000 | [diff] [blame] | 1151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1152 | /* Call the base tp_dealloc() */ |
| 1153 | assert(basedealloc); |
| 1154 | basedealloc(self); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1155 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1156 | /* Can't reference self beyond this point */ |
| 1157 | Py_DECREF(type); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1159 | /* Done */ |
| 1160 | return; |
| 1161 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1162 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1163 | /* We get here only if the type has GC */ |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1165 | /* UnTrack and re-Track around the trashcan macro, alas */ |
| 1166 | /* See explanation at end of function for full disclosure */ |
| 1167 | PyObject_GC_UnTrack(self); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1168 | ++_PyRuntime.gc.trash_delete_nesting; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 1169 | ++ tstate->trash_delete_nesting; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1170 | Py_TRASHCAN_SAFE_BEGIN(self); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1171 | --_PyRuntime.gc.trash_delete_nesting; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 1172 | -- tstate->trash_delete_nesting; |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1174 | /* Find the nearest base with a different tp_dealloc */ |
| 1175 | base = type; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 1176 | while ((/*basedealloc =*/ base->tp_dealloc) == subtype_dealloc) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1177 | base = base->tp_base; |
| 1178 | assert(base); |
| 1179 | } |
Guido van Rossum | 14227b4 | 2001-12-06 02:35:58 +0000 | [diff] [blame] | 1180 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1181 | has_finalizer = type->tp_finalize || type->tp_del; |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 1182 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1183 | if (type->tp_finalize) { |
Benjamin Peterson | 8f657c3 | 2016-10-04 00:00:02 -0700 | [diff] [blame] | 1184 | _PyObject_GC_TRACK(self); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1185 | if (PyObject_CallFinalizerFromDealloc(self) < 0) { |
| 1186 | /* Resurrected */ |
| 1187 | goto endlabel; |
| 1188 | } |
Benjamin Peterson | 8f657c3 | 2016-10-04 00:00:02 -0700 | [diff] [blame] | 1189 | _PyObject_GC_UNTRACK(self); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1190 | } |
Benjamin Peterson | 8f657c3 | 2016-10-04 00:00:02 -0700 | [diff] [blame] | 1191 | /* |
| 1192 | If we added a weaklist, we clear it. Do this *before* calling tp_del, |
| 1193 | clearing slots, or clearing the instance dict. |
| 1194 | |
| 1195 | GC tracking must be off at this point. weakref callbacks (if any, and |
| 1196 | whether directly here or indirectly in something we call) may trigger GC, |
| 1197 | and if self is tracked at that point, it will look like trash to GC and GC |
| 1198 | will try to delete self again. |
| 1199 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1200 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) |
| 1201 | PyObject_ClearWeakRefs(self); |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 1202 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1203 | if (type->tp_del) { |
Benjamin Peterson | 8f657c3 | 2016-10-04 00:00:02 -0700 | [diff] [blame] | 1204 | _PyObject_GC_TRACK(self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1205 | type->tp_del(self); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1206 | if (self->ob_refcnt > 0) { |
| 1207 | /* Resurrected */ |
| 1208 | goto endlabel; |
| 1209 | } |
Benjamin Peterson | 8f657c3 | 2016-10-04 00:00:02 -0700 | [diff] [blame] | 1210 | _PyObject_GC_UNTRACK(self); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1211 | } |
| 1212 | if (has_finalizer) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1213 | /* New weakrefs could be created during the finalizer call. |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1214 | If this occurs, clear them out without calling their |
| 1215 | finalizers since they might rely on part of the object |
| 1216 | being finalized that has already been destroyed. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1217 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) { |
| 1218 | /* Modeled after GET_WEAKREFS_LISTPTR() */ |
| 1219 | PyWeakReference **list = (PyWeakReference **) \ |
| 1220 | PyObject_GET_WEAKREFS_LISTPTR(self); |
| 1221 | while (*list) |
| 1222 | _PyWeakref_ClearRef(*list); |
| 1223 | } |
| 1224 | } |
Guido van Rossum | 1987c66 | 2003-05-29 14:29:23 +0000 | [diff] [blame] | 1225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1226 | /* Clear slots up to the nearest base with a different tp_dealloc */ |
| 1227 | base = type; |
| 1228 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
| 1229 | if (Py_SIZE(base)) |
| 1230 | clear_slots(base, self); |
| 1231 | base = base->tp_base; |
| 1232 | assert(base); |
| 1233 | } |
Guido van Rossum | 59195fd | 2003-06-13 20:54:40 +0000 | [diff] [blame] | 1234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1235 | /* If we added a dict, DECREF it */ |
| 1236 | if (type->tp_dictoffset && !base->tp_dictoffset) { |
| 1237 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 1238 | if (dictptr != NULL) { |
| 1239 | PyObject *dict = *dictptr; |
| 1240 | if (dict != NULL) { |
| 1241 | Py_DECREF(dict); |
| 1242 | *dictptr = NULL; |
| 1243 | } |
| 1244 | } |
| 1245 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1247 | /* Extract the type again; tp_del may have changed it */ |
| 1248 | type = Py_TYPE(self); |
Benjamin Peterson | 193152c | 2009-04-25 01:08:45 +0000 | [diff] [blame] | 1249 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1250 | /* Call the base tp_dealloc(); first retrack self if |
| 1251 | * basedealloc knows about gc. |
| 1252 | */ |
| 1253 | if (PyType_IS_GC(base)) |
| 1254 | _PyObject_GC_TRACK(self); |
| 1255 | assert(basedealloc); |
| 1256 | basedealloc(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1257 | |
Benjamin Peterson | 9d4cbcc | 2015-01-30 13:33:42 -0500 | [diff] [blame] | 1258 | /* Can't reference self beyond this point. It's possible tp_del switched |
| 1259 | our type from a HEAPTYPE to a non-HEAPTYPE, so be careful about |
| 1260 | reference counting. */ |
| 1261 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 1262 | Py_DECREF(type); |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 1263 | |
Guido van Rossum | 0906e07 | 2002-08-07 20:42:09 +0000 | [diff] [blame] | 1264 | endlabel: |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1265 | ++_PyRuntime.gc.trash_delete_nesting; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 1266 | ++ tstate->trash_delete_nesting; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1267 | Py_TRASHCAN_SAFE_END(self); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1268 | --_PyRuntime.gc.trash_delete_nesting; |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 1269 | -- tstate->trash_delete_nesting; |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1271 | /* Explanation of the weirdness around the trashcan macros: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1273 | Q. What do the trashcan macros do? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1275 | A. Read the comment titled "Trashcan mechanism" in object.h. |
| 1276 | For one, this explains why there must be a call to GC-untrack |
| 1277 | before the trashcan begin macro. Without understanding the |
| 1278 | trashcan code, the answers to the following questions don't make |
| 1279 | sense. |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1281 | Q. Why do we GC-untrack before the trashcan and then immediately |
| 1282 | GC-track again afterward? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1284 | A. In the case that the base class is GC-aware, the base class |
| 1285 | probably GC-untracks the object. If it does that using the |
| 1286 | UNTRACK macro, this will crash when the object is already |
| 1287 | untracked. Because we don't know what the base class does, the |
| 1288 | only safe thing is to make sure the object is tracked when we |
| 1289 | call the base class dealloc. But... The trashcan begin macro |
| 1290 | requires that the object is *untracked* before it is called. So |
| 1291 | the dance becomes: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1292 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1293 | GC untrack |
| 1294 | trashcan begin |
| 1295 | GC track |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1296 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1297 | Q. Why did the last question say "immediately GC-track again"? |
| 1298 | It's nowhere near immediately. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 1299 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1300 | A. Because the code *used* to re-track immediately. Bad Idea. |
| 1301 | self has a refcount of 0, and if gc ever gets its hands on it |
| 1302 | (which can happen if any weakref callback gets invoked), it |
| 1303 | 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] | 1304 | then. But we're already deleting self. Double deallocation is |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1305 | a subtle disaster. |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 1306 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1307 | Q. Why the bizarre (net-zero) manipulation of |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1308 | _PyRuntime.trash_delete_nesting around the trashcan macros? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1310 | A. Some base classes (e.g. list) also use the trashcan mechanism. |
| 1311 | The following scenario used to be possible: |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1313 | - suppose the trashcan level is one below the trashcan limit |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1315 | - subtype_dealloc() is called |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1316 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1317 | - the trashcan limit is not yet reached, so the trashcan level |
| 1318 | is incremented and the code between trashcan begin and end is |
| 1319 | executed |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1321 | - this destroys much of the object's contents, including its |
| 1322 | slots and __dict__ |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1323 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1324 | - basedealloc() is called; this is really list_dealloc(), or |
| 1325 | some other type which also uses the trashcan macros |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 | - the trashcan limit is now reached, so the object is put on the |
| 1328 | trashcan's to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1329 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1330 | - basedealloc() returns |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1332 | - subtype_dealloc() decrefs the object's type |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1334 | - subtype_dealloc() returns |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1336 | - later, the trashcan code starts deleting the objects from its |
| 1337 | to-be-deleted-later list |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1339 | - subtype_dealloc() is called *AGAIN* for the same object |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1340 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1341 | - at the very least (if the destroyed slots and __dict__ don't |
| 1342 | cause problems) the object's type gets decref'ed a second |
| 1343 | time, which is *BAD*!!! |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1345 | The remedy is to make sure that if the code between trashcan |
| 1346 | begin and end in subtype_dealloc() is called, the code between |
| 1347 | trashcan begin and end in basedealloc() will also be called. |
| 1348 | This is done by decrementing the level after passing into the |
| 1349 | trashcan block, and incrementing it just before leaving the |
| 1350 | block. |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1351 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1352 | But now it's possible that a chain of objects consisting solely |
| 1353 | of objects whose deallocator is subtype_dealloc() will defeat |
| 1354 | the trashcan mechanism completely: the decremented level means |
| 1355 | that the effective level never reaches the limit. Therefore, we |
| 1356 | *increment* the level *before* entering the trashcan block, and |
| 1357 | matchingly decrement it after leaving. This means the trashcan |
| 1358 | 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] | 1359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1360 | Q. Are there any live examples of code in need of all this |
| 1361 | complexity? |
Guido van Rossum | ce8bcd8 | 2003-02-05 22:39:45 +0000 | [diff] [blame] | 1362 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1363 | A. Yes. See SF bug 668433 for code that crashed (when Python was |
| 1364 | compiled in debug mode) before the trashcan level manipulations |
| 1365 | were added. For more discussion, see SF patches 581742, 575073 |
| 1366 | and bug 574207. |
| 1367 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1370 | static PyTypeObject *solid_base(PyTypeObject *type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1371 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1372 | /* type test with subclassing support */ |
| 1373 | |
Benjamin Peterson | 6cb526e | 2016-09-09 12:42:51 -0700 | [diff] [blame] | 1374 | static int |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1375 | type_is_subtype_base_chain(PyTypeObject *a, PyTypeObject *b) |
| 1376 | { |
| 1377 | do { |
| 1378 | if (a == b) |
| 1379 | return 1; |
| 1380 | a = a->tp_base; |
| 1381 | } while (a != NULL); |
| 1382 | |
| 1383 | return (b == &PyBaseObject_Type); |
| 1384 | } |
| 1385 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1386 | int |
| 1387 | PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
| 1388 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1389 | PyObject *mro; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1390 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1391 | mro = a->tp_mro; |
| 1392 | if (mro != NULL) { |
| 1393 | /* Deal with multiple inheritance without recursion |
| 1394 | by walking the MRO tuple */ |
| 1395 | Py_ssize_t i, n; |
| 1396 | assert(PyTuple_Check(mro)); |
| 1397 | n = PyTuple_GET_SIZE(mro); |
| 1398 | for (i = 0; i < n; i++) { |
| 1399 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) |
| 1400 | return 1; |
| 1401 | } |
| 1402 | return 0; |
| 1403 | } |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1404 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1405 | /* a is not completely initilized yet; follow tp_base */ |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1406 | return type_is_subtype_base_chain(a, b); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
Serhiy Storchaka | 4e624ca | 2017-06-01 08:18:25 +0300 | [diff] [blame] | 1409 | /* Routines to do a method lookup in the type without looking in the |
| 1410 | instance dictionary (so we can't use PyObject_GetAttr) but still |
| 1411 | binding it to the instance. |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1412 | |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 1413 | Variants: |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1414 | |
Serhiy Storchaka | 4e624ca | 2017-06-01 08:18:25 +0300 | [diff] [blame] | 1415 | - _PyObject_LookupSpecial() returns NULL without raising an exception |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1416 | when the _PyType_Lookup() call fails; |
| 1417 | |
Serhiy Storchaka | 4e624ca | 2017-06-01 08:18:25 +0300 | [diff] [blame] | 1418 | - lookup_maybe_method() and lookup_method() are internal routines similar |
| 1419 | to _PyObject_LookupSpecial(), but can return unbound PyFunction |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 1420 | to avoid temporary method object. Pass self as first argument when |
| 1421 | unbound == 1. |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1422 | */ |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1423 | |
Serhiy Storchaka | 4e624ca | 2017-06-01 08:18:25 +0300 | [diff] [blame] | 1424 | PyObject * |
| 1425 | _PyObject_LookupSpecial(PyObject *self, _Py_Identifier *attrid) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1426 | { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1427 | PyObject *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1428 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1429 | res = _PyType_LookupId(Py_TYPE(self), attrid); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1430 | if (res != NULL) { |
| 1431 | descrgetfunc f; |
| 1432 | if ((f = Py_TYPE(res)->tp_descr_get) == NULL) |
| 1433 | Py_INCREF(res); |
| 1434 | else |
| 1435 | res = f(res, self, (PyObject *)(Py_TYPE(self))); |
| 1436 | } |
| 1437 | return res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1440 | static PyObject * |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 1441 | lookup_maybe_method(PyObject *self, _Py_Identifier *attrid, int *unbound) |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1442 | { |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 1443 | PyObject *res = _PyType_LookupId(Py_TYPE(self), attrid); |
| 1444 | if (res == NULL) { |
| 1445 | return NULL; |
| 1446 | } |
| 1447 | |
| 1448 | if (PyFunction_Check(res)) { |
| 1449 | /* Avoid temporary PyMethodObject */ |
| 1450 | *unbound = 1; |
| 1451 | Py_INCREF(res); |
| 1452 | } |
| 1453 | else { |
| 1454 | *unbound = 0; |
| 1455 | descrgetfunc f = Py_TYPE(res)->tp_descr_get; |
| 1456 | if (f == NULL) { |
| 1457 | Py_INCREF(res); |
| 1458 | } |
| 1459 | else { |
| 1460 | res = f(res, self, (PyObject *)(Py_TYPE(self))); |
| 1461 | } |
| 1462 | } |
| 1463 | return res; |
| 1464 | } |
| 1465 | |
| 1466 | static PyObject * |
| 1467 | lookup_method(PyObject *self, _Py_Identifier *attrid, int *unbound) |
| 1468 | { |
| 1469 | PyObject *res = lookup_maybe_method(self, attrid, unbound); |
| 1470 | if (res == NULL && !PyErr_Occurred()) { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1471 | PyErr_SetObject(PyExc_AttributeError, attrid->object); |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 1472 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1473 | return res; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 1476 | static PyObject* |
| 1477 | call_unbound(int unbound, PyObject *func, PyObject *self, |
| 1478 | PyObject **args, Py_ssize_t nargs) |
| 1479 | { |
| 1480 | if (unbound) { |
| 1481 | return _PyObject_FastCall_Prepend(func, self, args, nargs); |
| 1482 | } |
| 1483 | else { |
| 1484 | return _PyObject_FastCall(func, args, nargs); |
| 1485 | } |
| 1486 | } |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1487 | |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 1488 | static PyObject* |
| 1489 | call_unbound_noarg(int unbound, PyObject *func, PyObject *self) |
| 1490 | { |
| 1491 | if (unbound) { |
| 1492 | PyObject *args[1] = {self}; |
| 1493 | return _PyObject_FastCall(func, args, 1); |
| 1494 | } |
| 1495 | else { |
| 1496 | return _PyObject_CallNoArg(func); |
| 1497 | } |
| 1498 | } |
| 1499 | |
Serhiy Storchaka | 4e624ca | 2017-06-01 08:18:25 +0300 | [diff] [blame] | 1500 | /* A variation of PyObject_CallMethod* that uses lookup_maybe_method() |
| 1501 | instead of PyObject_GetAttrString(). */ |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1502 | static PyObject * |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 1503 | call_method(PyObject *obj, _Py_Identifier *name, |
| 1504 | PyObject **args, Py_ssize_t nargs) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1505 | { |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 1506 | int unbound; |
Victor Stinner | 887b430 | 2016-12-09 00:41:46 +0100 | [diff] [blame] | 1507 | PyObject *func, *retval; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1508 | |
Serhiy Storchaka | 4e624ca | 2017-06-01 08:18:25 +0300 | [diff] [blame] | 1509 | func = lookup_method(obj, name, &unbound); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1510 | if (func == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1511 | return NULL; |
| 1512 | } |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 1513 | retval = call_unbound(unbound, func, obj, args, nargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1514 | Py_DECREF(func); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1515 | return retval; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | /* Clone of call_method() that returns NotImplemented when the lookup fails. */ |
| 1519 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 1520 | static PyObject * |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 1521 | call_maybe(PyObject *obj, _Py_Identifier *name, |
| 1522 | PyObject **args, Py_ssize_t nargs) |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1523 | { |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 1524 | int unbound; |
Victor Stinner | 887b430 | 2016-12-09 00:41:46 +0100 | [diff] [blame] | 1525 | PyObject *func, *retval; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 1526 | |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 1527 | func = lookup_maybe_method(obj, name, &unbound); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1528 | if (func == NULL) { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1529 | if (!PyErr_Occurred()) |
| 1530 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1531 | return NULL; |
| 1532 | } |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1533 | |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 1534 | retval = call_unbound(unbound, func, obj, args, nargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1535 | Py_DECREF(func); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1536 | return retval; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1539 | /* |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1540 | Method resolution order algorithm C3 described in |
| 1541 | "A Monotonic Superclass Linearization for Dylan", |
| 1542 | by Kim Barrett, Bob Cassel, Paul Haahr, |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1543 | David A. Moon, Keith Playford, and P. Tucker Withington. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1544 | (OOPSLA 1996) |
| 1545 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1546 | Some notes about the rules implied by C3: |
| 1547 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1548 | No duplicate bases. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1549 | It isn't legal to repeat a class in a list of base classes. |
| 1550 | |
| 1551 | The next three properties are the 3 constraints in "C3". |
| 1552 | |
Martin Panter | 69332c1 | 2016-08-04 13:07:31 +0000 | [diff] [blame] | 1553 | Local precedence order. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1554 | If A precedes B in C's MRO, then A will precede B in the MRO of all |
| 1555 | subclasses of C. |
| 1556 | |
| 1557 | Monotonicity. |
| 1558 | The MRO of a class must be an extension without reordering of the |
| 1559 | MRO of each of its superclasses. |
| 1560 | |
| 1561 | Extended Precedence Graph (EPG). |
| 1562 | Linearization is consistent if there is a path in the EPG from |
| 1563 | each class to all its successors in the linearization. See |
| 1564 | the paper for definition of EPG. |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1565 | */ |
| 1566 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1567 | static int |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1568 | tail_contains(PyObject *tuple, int whence, PyObject *o) |
| 1569 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | Py_ssize_t j, size; |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1571 | size = PyTuple_GET_SIZE(tuple); |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1572 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1573 | for (j = whence+1; j < size; j++) { |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1574 | if (PyTuple_GET_ITEM(tuple, j) == o) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1575 | return 1; |
| 1576 | } |
| 1577 | return 0; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1580 | static PyObject * |
| 1581 | class_name(PyObject *cls) |
| 1582 | { |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1583 | PyObject *name = _PyObject_GetAttrId(cls, &PyId___name__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1584 | if (name == NULL) { |
| 1585 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1586 | name = PyObject_Repr(cls); |
| 1587 | } |
| 1588 | if (name == NULL) |
| 1589 | return NULL; |
| 1590 | if (!PyUnicode_Check(name)) { |
| 1591 | Py_DECREF(name); |
| 1592 | return NULL; |
| 1593 | } |
| 1594 | return name; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | static int |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1598 | check_duplicates(PyObject *tuple) |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1599 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1600 | Py_ssize_t i, j, n; |
| 1601 | /* Let's use a quadratic time algorithm, |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1602 | assuming that the bases tuples is short. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1603 | */ |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1604 | n = PyTuple_GET_SIZE(tuple); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1605 | for (i = 0; i < n; i++) { |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1606 | PyObject *o = PyTuple_GET_ITEM(tuple, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1607 | for (j = i + 1; j < n; j++) { |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1608 | if (PyTuple_GET_ITEM(tuple, j) == o) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1609 | o = class_name(o); |
| 1610 | if (o != NULL) { |
| 1611 | PyErr_Format(PyExc_TypeError, |
| 1612 | "duplicate base class %U", |
| 1613 | o); |
| 1614 | Py_DECREF(o); |
| 1615 | } else { |
| 1616 | PyErr_SetString(PyExc_TypeError, |
| 1617 | "duplicate base class"); |
| 1618 | } |
| 1619 | return -1; |
| 1620 | } |
| 1621 | } |
| 1622 | } |
| 1623 | return 0; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | /* Raise a TypeError for an MRO order disagreement. |
| 1627 | |
| 1628 | It's hard to produce a good error message. In the absence of better |
| 1629 | insight into error reporting, report the classes that were candidates |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1630 | 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] | 1631 | order in which they should be put in the MRO, but it's hard to |
| 1632 | diagnose what constraint can't be satisfied. |
| 1633 | */ |
| 1634 | |
| 1635 | static void |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1636 | set_mro_error(PyObject **to_merge, Py_ssize_t to_merge_size, int *remain) |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1637 | { |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1638 | Py_ssize_t i, n, off; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1639 | char buf[1000]; |
| 1640 | PyObject *k, *v; |
| 1641 | PyObject *set = PyDict_New(); |
| 1642 | if (!set) return; |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1643 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1644 | for (i = 0; i < to_merge_size; i++) { |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1645 | PyObject *L = to_merge[i]; |
| 1646 | if (remain[i] < PyTuple_GET_SIZE(L)) { |
| 1647 | PyObject *c = PyTuple_GET_ITEM(L, remain[i]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1648 | if (PyDict_SetItem(set, c, Py_None) < 0) { |
| 1649 | Py_DECREF(set); |
| 1650 | return; |
| 1651 | } |
| 1652 | } |
| 1653 | } |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1654 | n = PyDict_GET_SIZE(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1656 | off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \ |
Raymond Hettinger | f394df4 | 2003-04-06 19:13:41 +0000 | [diff] [blame] | 1657 | consistent method resolution\norder (MRO) for bases"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1658 | i = 0; |
| 1659 | while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) { |
| 1660 | PyObject *name = class_name(k); |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 1661 | const char *name_str; |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1662 | if (name != NULL) { |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 1663 | name_str = PyUnicode_AsUTF8(name); |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1664 | if (name_str == NULL) |
Victor Stinner | ba644a6 | 2010-05-19 01:50:45 +0000 | [diff] [blame] | 1665 | name_str = "?"; |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1666 | } else |
Victor Stinner | ba644a6 | 2010-05-19 01:50:45 +0000 | [diff] [blame] | 1667 | name_str = "?"; |
Victor Stinner | e5f99f3 | 2010-05-19 01:42:46 +0000 | [diff] [blame] | 1668 | off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", name_str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1669 | Py_XDECREF(name); |
| 1670 | if (--n && (size_t)(off+1) < sizeof(buf)) { |
| 1671 | buf[off++] = ','; |
| 1672 | buf[off] = '\0'; |
| 1673 | } |
| 1674 | } |
| 1675 | PyErr_SetString(PyExc_TypeError, buf); |
| 1676 | Py_DECREF(set); |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1679 | static int |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1680 | pmerge(PyObject *acc, PyObject **to_merge, Py_ssize_t to_merge_size) |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1681 | { |
| 1682 | int res = 0; |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1683 | Py_ssize_t i, j, empty_cnt; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1684 | int *remain; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1685 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1686 | /* remain stores an index into each sublist of to_merge. |
| 1687 | remain[i] is the index of the next base in to_merge[i] |
| 1688 | that is not included in acc. |
| 1689 | */ |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1690 | remain = PyMem_New(int, to_merge_size); |
Victor Stinner | a41f085 | 2013-07-12 00:42:14 +0200 | [diff] [blame] | 1691 | if (remain == NULL) { |
| 1692 | PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1693 | return -1; |
Victor Stinner | a41f085 | 2013-07-12 00:42:14 +0200 | [diff] [blame] | 1694 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1695 | for (i = 0; i < to_merge_size; i++) |
| 1696 | remain[i] = 0; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1697 | |
| 1698 | again: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1699 | empty_cnt = 0; |
| 1700 | for (i = 0; i < to_merge_size; i++) { |
| 1701 | PyObject *candidate; |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 1702 | |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1703 | PyObject *cur_tuple = to_merge[i]; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1704 | |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1705 | if (remain[i] >= PyTuple_GET_SIZE(cur_tuple)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1706 | empty_cnt++; |
| 1707 | continue; |
| 1708 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1709 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1710 | /* Choose next candidate for MRO. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1711 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1712 | The input sequences alone can determine the choice. |
| 1713 | If not, choose the class which appears in the MRO |
| 1714 | of the earliest direct superclass of the new class. |
| 1715 | */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1716 | |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1717 | candidate = PyTuple_GET_ITEM(cur_tuple, remain[i]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1718 | for (j = 0; j < to_merge_size; j++) { |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1719 | PyObject *j_lst = to_merge[j]; |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1720 | if (tail_contains(j_lst, remain[j], candidate)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1721 | goto skip; /* continue outer loop */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1722 | } |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1723 | res = PyList_Append(acc, candidate); |
| 1724 | if (res < 0) |
| 1725 | goto out; |
| 1726 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1727 | for (j = 0; j < to_merge_size; j++) { |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1728 | PyObject *j_lst = to_merge[j]; |
| 1729 | if (remain[j] < PyTuple_GET_SIZE(j_lst) && |
| 1730 | PyTuple_GET_ITEM(j_lst, remain[j]) == candidate) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1731 | remain[j]++; |
| 1732 | } |
| 1733 | } |
| 1734 | goto again; |
| 1735 | skip: ; |
| 1736 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1737 | |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1738 | if (empty_cnt != to_merge_size) { |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1739 | set_mro_error(to_merge, to_merge_size, remain); |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1740 | res = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1741 | } |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1742 | |
| 1743 | out: |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1744 | PyMem_Del(remain); |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1745 | |
| 1746 | return res; |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1749 | static PyObject * |
| 1750 | mro_implementation(PyTypeObject *type) |
| 1751 | { |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1752 | PyObject *result; |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1753 | PyObject *bases; |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1754 | PyObject **to_merge; |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1755 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1756 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1757 | if (type->tp_dict == NULL) { |
| 1758 | if (PyType_Ready(type) < 0) |
| 1759 | return NULL; |
| 1760 | } |
Guido van Rossum | 6351757 | 2002-06-18 16:44:57 +0000 | [diff] [blame] | 1761 | |
Antoine Pitrou | 1f1a34c | 2017-12-20 15:58:21 +0100 | [diff] [blame] | 1762 | bases = type->tp_bases; |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1763 | assert(PyTuple_Check(bases)); |
Antoine Pitrou | 1f1a34c | 2017-12-20 15:58:21 +0100 | [diff] [blame] | 1764 | n = PyTuple_GET_SIZE(bases); |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1765 | for (i = 0; i < n; i++) { |
| 1766 | PyTypeObject *base = (PyTypeObject *)PyTuple_GET_ITEM(bases, i); |
Antoine Pitrou | 1f1a34c | 2017-12-20 15:58:21 +0100 | [diff] [blame] | 1767 | if (base->tp_mro == NULL) { |
| 1768 | PyErr_Format(PyExc_TypeError, |
| 1769 | "Cannot extend an incomplete type '%.100s'", |
| 1770 | base->tp_name); |
| 1771 | return NULL; |
| 1772 | } |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1773 | assert(PyTuple_Check(base->tp_mro)); |
| 1774 | } |
| 1775 | |
| 1776 | if (n == 1) { |
| 1777 | /* Fast path: if there is a single base, constructing the MRO |
| 1778 | * is trivial. |
| 1779 | */ |
| 1780 | PyTypeObject *base = (PyTypeObject *)PyTuple_GET_ITEM(bases, 0); |
| 1781 | Py_ssize_t k = PyTuple_GET_SIZE(base->tp_mro); |
Antoine Pitrou | 1f1a34c | 2017-12-20 15:58:21 +0100 | [diff] [blame] | 1782 | result = PyTuple_New(k + 1); |
| 1783 | if (result == NULL) { |
| 1784 | return NULL; |
| 1785 | } |
| 1786 | Py_INCREF(type); |
| 1787 | PyTuple_SET_ITEM(result, 0, (PyObject *) type); |
| 1788 | for (i = 0; i < k; i++) { |
| 1789 | PyObject *cls = PyTuple_GET_ITEM(base->tp_mro, i); |
| 1790 | Py_INCREF(cls); |
| 1791 | PyTuple_SET_ITEM(result, i + 1, cls); |
| 1792 | } |
| 1793 | return result; |
| 1794 | } |
| 1795 | |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1796 | /* This is just a basic sanity check. */ |
| 1797 | if (check_duplicates(bases) < 0) { |
| 1798 | return NULL; |
| 1799 | } |
| 1800 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1801 | /* Find a superclass linearization that honors the constraints |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1802 | of the explicit tuples of bases and the constraints implied by |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1803 | each base class. |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1804 | |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1805 | to_merge is an array of tuples, where each tuple is a superclass |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1806 | linearization implied by a base class. The last element of |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1807 | to_merge is the declared tuple of bases. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1808 | */ |
Guido van Rossum | 98f3373 | 2002-11-25 21:36:54 +0000 | [diff] [blame] | 1809 | |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1810 | to_merge = PyMem_New(PyObject *, n + 1); |
| 1811 | if (to_merge == NULL) { |
| 1812 | PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1813 | return NULL; |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1814 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1815 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1816 | for (i = 0; i < n; i++) { |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1817 | PyTypeObject *base = (PyTypeObject *)PyTuple_GET_ITEM(bases, i); |
| 1818 | to_merge[i] = base->tp_mro; |
| 1819 | } |
| 1820 | to_merge[n] = bases; |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1821 | |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1822 | result = PyList_New(1); |
| 1823 | if (result == NULL) { |
| 1824 | PyMem_Del(to_merge); |
| 1825 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1826 | } |
Guido van Rossum | 1f12131 | 2002-11-14 19:49:16 +0000 | [diff] [blame] | 1827 | |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1828 | Py_INCREF(type); |
| 1829 | PyList_SET_ITEM(result, 0, (PyObject *)type); |
| 1830 | if (pmerge(result, to_merge, n + 1) < 0) { |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1831 | Py_CLEAR(result); |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1832 | } |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1833 | |
Serhiy Storchaka | 6b91a59 | 2017-12-20 19:21:02 +0200 | [diff] [blame] | 1834 | PyMem_Del(to_merge); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1835 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 1838 | /*[clinic input] |
| 1839 | type.mro |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1840 | |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 1841 | Return a type's method resolution order. |
| 1842 | [clinic start generated code]*/ |
| 1843 | |
| 1844 | static PyObject * |
| 1845 | type_mro_impl(PyTypeObject *self) |
| 1846 | /*[clinic end generated code: output=bffc4a39b5b57027 input=28414f4e156db28d]*/ |
| 1847 | { |
Antoine Pitrou | 1f1a34c | 2017-12-20 15:58:21 +0100 | [diff] [blame] | 1848 | PyObject *seq; |
| 1849 | seq = mro_implementation(self); |
| 1850 | if (seq != NULL && !PyList_Check(seq)) { |
| 1851 | Py_SETREF(seq, PySequence_List(seq)); |
| 1852 | } |
| 1853 | return seq; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | static int |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1857 | mro_check(PyTypeObject *type, PyObject *mro) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1858 | { |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1859 | PyTypeObject *solid; |
| 1860 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1861 | |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1862 | solid = solid_base(type); |
| 1863 | |
| 1864 | n = PyTuple_GET_SIZE(mro); |
| 1865 | for (i = 0; i < n; i++) { |
| 1866 | PyTypeObject *base; |
| 1867 | PyObject *tmp; |
| 1868 | |
| 1869 | tmp = PyTuple_GET_ITEM(mro, i); |
| 1870 | if (!PyType_Check(tmp)) { |
| 1871 | PyErr_Format( |
| 1872 | PyExc_TypeError, |
| 1873 | "mro() returned a non-class ('%.500s')", |
| 1874 | Py_TYPE(tmp)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1875 | return -1; |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1876 | } |
Armin Rigo | 037d1e0 | 2005-12-29 17:07:39 +0000 | [diff] [blame] | 1877 | |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1878 | base = (PyTypeObject*)tmp; |
| 1879 | if (!PyType_IsSubtype(solid, solid_base(base))) { |
| 1880 | PyErr_Format( |
| 1881 | PyExc_TypeError, |
| 1882 | "mro() returned base with unsuitable layout ('%.500s')", |
| 1883 | base->tp_name); |
| 1884 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1885 | } |
| 1886 | } |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1887 | |
| 1888 | return 0; |
| 1889 | } |
| 1890 | |
| 1891 | /* Lookups an mcls.mro method, invokes it and checks the result (if needed, |
| 1892 | in case of a custom mro() implementation). |
| 1893 | |
| 1894 | Keep in mind that during execution of this function type->tp_mro |
| 1895 | can be replaced due to possible reentrance (for example, |
| 1896 | through type_set_bases): |
| 1897 | |
| 1898 | - when looking up the mcls.mro attribute (it could be |
| 1899 | a user-provided descriptor); |
| 1900 | |
| 1901 | - from inside a custom mro() itself; |
| 1902 | |
| 1903 | - through a finalizer of the return value of mro(). |
| 1904 | */ |
| 1905 | static PyObject * |
| 1906 | mro_invoke(PyTypeObject *type) |
| 1907 | { |
| 1908 | PyObject *mro_result; |
| 1909 | PyObject *new_mro; |
| 1910 | int custom = (Py_TYPE(type) != &PyType_Type); |
| 1911 | |
| 1912 | if (custom) { |
| 1913 | _Py_IDENTIFIER(mro); |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 1914 | int unbound; |
| 1915 | PyObject *mro_meth = lookup_method((PyObject *)type, &PyId_mro, |
| 1916 | &unbound); |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1917 | if (mro_meth == NULL) |
| 1918 | return NULL; |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 1919 | mro_result = call_unbound_noarg(unbound, mro_meth, (PyObject *)type); |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1920 | Py_DECREF(mro_meth); |
| 1921 | } |
| 1922 | else { |
| 1923 | mro_result = mro_implementation(type); |
| 1924 | } |
| 1925 | if (mro_result == NULL) |
| 1926 | return NULL; |
| 1927 | |
| 1928 | new_mro = PySequence_Tuple(mro_result); |
| 1929 | Py_DECREF(mro_result); |
| 1930 | if (new_mro == NULL) |
| 1931 | return NULL; |
| 1932 | |
| 1933 | if (custom && mro_check(type, new_mro) < 0) { |
| 1934 | Py_DECREF(new_mro); |
| 1935 | return NULL; |
| 1936 | } |
| 1937 | |
| 1938 | return new_mro; |
| 1939 | } |
| 1940 | |
| 1941 | /* Calculates and assigns a new MRO to type->tp_mro. |
| 1942 | Return values and invariants: |
| 1943 | |
| 1944 | - Returns 1 if a new MRO value has been set to type->tp_mro due to |
| 1945 | this call of mro_internal (no tricky reentrancy and no errors). |
| 1946 | |
| 1947 | In case if p_old_mro argument is not NULL, a previous value |
| 1948 | of type->tp_mro is put there, and the ownership of this |
| 1949 | reference is transferred to a caller. |
| 1950 | Otherwise, the previous value (if any) is decref'ed. |
| 1951 | |
| 1952 | - Returns 0 in case when type->tp_mro gets changed because of |
| 1953 | reentering here through a custom mro() (see a comment to mro_invoke). |
| 1954 | |
| 1955 | In this case, a refcount of an old type->tp_mro is adjusted |
| 1956 | somewhere deeper in the call stack (by the innermost mro_internal |
| 1957 | or its caller) and may become zero upon returning from here. |
| 1958 | This also implies that the whole hierarchy of subclasses of the type |
| 1959 | has seen the new value and updated their MRO accordingly. |
| 1960 | |
| 1961 | - Returns -1 in case of an error. |
| 1962 | */ |
| 1963 | static int |
| 1964 | mro_internal(PyTypeObject *type, PyObject **p_old_mro) |
| 1965 | { |
| 1966 | PyObject *new_mro, *old_mro; |
| 1967 | int reent; |
| 1968 | |
| 1969 | /* Keep a reference to be able to do a reentrancy check below. |
| 1970 | Don't let old_mro be GC'ed and its address be reused for |
| 1971 | another object, like (suddenly!) a new tp_mro. */ |
| 1972 | old_mro = type->tp_mro; |
| 1973 | Py_XINCREF(old_mro); |
| 1974 | new_mro = mro_invoke(type); /* might cause reentrance */ |
| 1975 | reent = (type->tp_mro != old_mro); |
| 1976 | Py_XDECREF(old_mro); |
| 1977 | if (new_mro == NULL) |
| 1978 | return -1; |
| 1979 | |
| 1980 | if (reent) { |
| 1981 | Py_DECREF(new_mro); |
| 1982 | return 0; |
| 1983 | } |
| 1984 | |
| 1985 | type->tp_mro = new_mro; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1986 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1987 | type_mro_modified(type, type->tp_mro); |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 1988 | /* corner case: the super class might have been hidden |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1989 | from the custom MRO */ |
| 1990 | type_mro_modified(type, type->tp_bases); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1991 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1992 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 1993 | |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 1994 | if (p_old_mro != NULL) |
| 1995 | *p_old_mro = old_mro; /* transfer the ownership */ |
| 1996 | else |
| 1997 | Py_XDECREF(old_mro); |
| 1998 | |
| 1999 | return 1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | |
| 2003 | /* Calculate the best base amongst multiple base classes. |
| 2004 | This is the first one that's on the path to the "solid base". */ |
| 2005 | |
| 2006 | static PyTypeObject * |
| 2007 | best_base(PyObject *bases) |
| 2008 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2009 | Py_ssize_t i, n; |
| 2010 | PyTypeObject *base, *winner, *candidate, *base_i; |
| 2011 | PyObject *base_proto; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2013 | assert(PyTuple_Check(bases)); |
| 2014 | n = PyTuple_GET_SIZE(bases); |
| 2015 | assert(n > 0); |
| 2016 | base = NULL; |
| 2017 | winner = NULL; |
| 2018 | for (i = 0; i < n; i++) { |
| 2019 | base_proto = PyTuple_GET_ITEM(bases, i); |
| 2020 | if (!PyType_Check(base_proto)) { |
| 2021 | PyErr_SetString( |
| 2022 | PyExc_TypeError, |
| 2023 | "bases must be types"); |
| 2024 | return NULL; |
| 2025 | } |
| 2026 | base_i = (PyTypeObject *)base_proto; |
| 2027 | if (base_i->tp_dict == NULL) { |
| 2028 | if (PyType_Ready(base_i) < 0) |
| 2029 | return NULL; |
| 2030 | } |
Benjamin Peterson | bd6c41a | 2015-10-06 19:36:54 -0700 | [diff] [blame] | 2031 | if (!PyType_HasFeature(base_i, Py_TPFLAGS_BASETYPE)) { |
| 2032 | PyErr_Format(PyExc_TypeError, |
| 2033 | "type '%.100s' is not an acceptable base type", |
| 2034 | base_i->tp_name); |
| 2035 | return NULL; |
| 2036 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2037 | candidate = solid_base(base_i); |
| 2038 | if (winner == NULL) { |
| 2039 | winner = candidate; |
| 2040 | base = base_i; |
| 2041 | } |
| 2042 | else if (PyType_IsSubtype(winner, candidate)) |
| 2043 | ; |
| 2044 | else if (PyType_IsSubtype(candidate, winner)) { |
| 2045 | winner = candidate; |
| 2046 | base = base_i; |
| 2047 | } |
| 2048 | else { |
| 2049 | PyErr_SetString( |
| 2050 | PyExc_TypeError, |
| 2051 | "multiple bases have " |
| 2052 | "instance lay-out conflict"); |
| 2053 | return NULL; |
| 2054 | } |
| 2055 | } |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 2056 | assert (base != NULL); |
| 2057 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2058 | return base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
| 2061 | static int |
| 2062 | extra_ivars(PyTypeObject *type, PyTypeObject *base) |
| 2063 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2064 | size_t t_size = type->tp_basicsize; |
| 2065 | size_t b_size = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2066 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2067 | assert(t_size >= b_size); /* Else type smaller than base! */ |
| 2068 | if (type->tp_itemsize || base->tp_itemsize) { |
| 2069 | /* If itemsize is involved, stricter rules */ |
| 2070 | return t_size != b_size || |
| 2071 | type->tp_itemsize != base->tp_itemsize; |
| 2072 | } |
| 2073 | if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 && |
| 2074 | type->tp_weaklistoffset + sizeof(PyObject *) == t_size && |
| 2075 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 2076 | t_size -= sizeof(PyObject *); |
| 2077 | if (type->tp_dictoffset && base->tp_dictoffset == 0 && |
| 2078 | type->tp_dictoffset + sizeof(PyObject *) == t_size && |
| 2079 | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 2080 | t_size -= sizeof(PyObject *); |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 2081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2082 | return t_size != b_size; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | static PyTypeObject * |
| 2086 | solid_base(PyTypeObject *type) |
| 2087 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2088 | PyTypeObject *base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2089 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2090 | if (type->tp_base) |
| 2091 | base = solid_base(type->tp_base); |
| 2092 | else |
| 2093 | base = &PyBaseObject_Type; |
| 2094 | if (extra_ivars(type, base)) |
| 2095 | return type; |
| 2096 | else |
| 2097 | return base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 2100 | static void object_dealloc(PyObject *); |
| 2101 | static int object_init(PyObject *, PyObject *, PyObject *); |
| 2102 | static int update_slot(PyTypeObject *, PyObject *); |
| 2103 | static void fixup_slot_dispatchers(PyTypeObject *); |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 2104 | static int set_names(PyTypeObject *); |
| 2105 | static int init_subclass(PyTypeObject *, PyObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2106 | |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 2107 | /* |
| 2108 | * Helpers for __dict__ descriptor. We don't want to expose the dicts |
| 2109 | * inherited from various builtin types. The builtin base usually provides |
| 2110 | * its own __dict__ descriptor, so we use that when we can. |
| 2111 | */ |
| 2112 | static PyTypeObject * |
| 2113 | get_builtin_base_with_dict(PyTypeObject *type) |
| 2114 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2115 | while (type->tp_base != NULL) { |
| 2116 | if (type->tp_dictoffset != 0 && |
| 2117 | !(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 2118 | return type; |
| 2119 | type = type->tp_base; |
| 2120 | } |
| 2121 | return NULL; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 2122 | } |
| 2123 | |
| 2124 | static PyObject * |
| 2125 | get_dict_descriptor(PyTypeObject *type) |
| 2126 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2127 | PyObject *descr; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 2128 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2129 | descr = _PyType_LookupId(type, &PyId___dict__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2130 | if (descr == NULL || !PyDescr_IsData(descr)) |
| 2131 | return NULL; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 2132 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2133 | return descr; |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 2134 | } |
| 2135 | |
| 2136 | static void |
| 2137 | raise_dict_descr_error(PyObject *obj) |
| 2138 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2139 | PyErr_Format(PyExc_TypeError, |
| 2140 | "this __dict__ descriptor does not support " |
| 2141 | "'%.200s' objects", Py_TYPE(obj)->tp_name); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2144 | static PyObject * |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 2145 | subtype_dict(PyObject *obj, void *context) |
| 2146 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2147 | PyTypeObject *base; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 2148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2149 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
| 2150 | if (base != NULL) { |
| 2151 | descrgetfunc func; |
| 2152 | PyObject *descr = get_dict_descriptor(base); |
| 2153 | if (descr == NULL) { |
| 2154 | raise_dict_descr_error(obj); |
| 2155 | return NULL; |
| 2156 | } |
| 2157 | func = Py_TYPE(descr)->tp_descr_get; |
| 2158 | if (func == NULL) { |
| 2159 | raise_dict_descr_error(obj); |
| 2160 | return NULL; |
| 2161 | } |
| 2162 | return func(descr, obj, (PyObject *)(Py_TYPE(obj))); |
| 2163 | } |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 2164 | return PyObject_GenericGetDict(obj, context); |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 2167 | static int |
| 2168 | subtype_setdict(PyObject *obj, PyObject *value, void *context) |
| 2169 | { |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 2170 | PyObject **dictptr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2171 | PyTypeObject *base; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 2172 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2173 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
| 2174 | if (base != NULL) { |
| 2175 | descrsetfunc func; |
| 2176 | PyObject *descr = get_dict_descriptor(base); |
| 2177 | if (descr == NULL) { |
| 2178 | raise_dict_descr_error(obj); |
| 2179 | return -1; |
| 2180 | } |
| 2181 | func = Py_TYPE(descr)->tp_descr_set; |
| 2182 | if (func == NULL) { |
| 2183 | raise_dict_descr_error(obj); |
| 2184 | return -1; |
| 2185 | } |
| 2186 | return func(descr, obj, value); |
| 2187 | } |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 2188 | /* Almost like PyObject_GenericSetDict, but allow __dict__ to be deleted. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2189 | dictptr = _PyObject_GetDictPtr(obj); |
| 2190 | if (dictptr == NULL) { |
| 2191 | PyErr_SetString(PyExc_AttributeError, |
| 2192 | "This object has no __dict__"); |
| 2193 | return -1; |
| 2194 | } |
Benjamin Peterson | 006c5a2 | 2012-02-19 20:36:12 -0500 | [diff] [blame] | 2195 | if (value != NULL && !PyDict_Check(value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2196 | PyErr_Format(PyExc_TypeError, |
| 2197 | "__dict__ must be set to a dictionary, " |
| 2198 | "not a '%.200s'", Py_TYPE(value)->tp_name); |
| 2199 | return -1; |
| 2200 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2201 | Py_XINCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 2202 | Py_XSETREF(*dictptr, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2203 | return 0; |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2206 | static PyObject * |
| 2207 | subtype_getweakref(PyObject *obj, void *context) |
| 2208 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2209 | PyObject **weaklistptr; |
| 2210 | PyObject *result; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2211 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2212 | if (Py_TYPE(obj)->tp_weaklistoffset == 0) { |
| 2213 | PyErr_SetString(PyExc_AttributeError, |
| 2214 | "This object has no __weakref__"); |
| 2215 | return NULL; |
| 2216 | } |
| 2217 | assert(Py_TYPE(obj)->tp_weaklistoffset > 0); |
| 2218 | assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <= |
| 2219 | (size_t)(Py_TYPE(obj)->tp_basicsize)); |
| 2220 | weaklistptr = (PyObject **) |
| 2221 | ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset); |
| 2222 | if (*weaklistptr == NULL) |
| 2223 | result = Py_None; |
| 2224 | else |
| 2225 | result = *weaklistptr; |
| 2226 | Py_INCREF(result); |
| 2227 | return result; |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2230 | /* Three variants on the subtype_getsets list. */ |
| 2231 | |
| 2232 | static PyGetSetDef subtype_getsets_full[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2233 | {"__dict__", subtype_dict, subtype_setdict, |
| 2234 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 2235 | {"__weakref__", subtype_getweakref, NULL, |
| 2236 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 2237 | {0} |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 2238 | }; |
| 2239 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2240 | static PyGetSetDef subtype_getsets_dict_only[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2241 | {"__dict__", subtype_dict, subtype_setdict, |
| 2242 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
| 2243 | {0} |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2244 | }; |
| 2245 | |
| 2246 | static PyGetSetDef subtype_getsets_weakref_only[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2247 | {"__weakref__", subtype_getweakref, NULL, |
| 2248 | PyDoc_STR("list of weak references to the object (if defined)")}, |
| 2249 | {0} |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2250 | }; |
| 2251 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 2252 | static int |
| 2253 | valid_identifier(PyObject *s) |
| 2254 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2255 | if (!PyUnicode_Check(s)) { |
| 2256 | PyErr_Format(PyExc_TypeError, |
| 2257 | "__slots__ items must be strings, not '%.200s'", |
| 2258 | Py_TYPE(s)->tp_name); |
| 2259 | return 0; |
| 2260 | } |
| 2261 | if (!PyUnicode_IsIdentifier(s)) { |
| 2262 | PyErr_SetString(PyExc_TypeError, |
| 2263 | "__slots__ must be identifiers"); |
| 2264 | return 0; |
| 2265 | } |
| 2266 | return 1; |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 2267 | } |
| 2268 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2269 | /* Forward */ |
| 2270 | static int |
| 2271 | object_init(PyObject *self, PyObject *args, PyObject *kwds); |
| 2272 | |
| 2273 | static int |
| 2274 | type_init(PyObject *cls, PyObject *args, PyObject *kwds) |
| 2275 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2276 | int res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2278 | assert(args != NULL && PyTuple_Check(args)); |
| 2279 | assert(kwds == NULL || PyDict_Check(kwds)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2280 | |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 2281 | if (kwds != NULL && PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2282 | PyDict_Check(kwds) && PyDict_GET_SIZE(kwds) != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2283 | PyErr_SetString(PyExc_TypeError, |
| 2284 | "type.__init__() takes no keyword arguments"); |
| 2285 | return -1; |
| 2286 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2288 | if (args != NULL && PyTuple_Check(args) && |
| 2289 | (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) { |
| 2290 | PyErr_SetString(PyExc_TypeError, |
| 2291 | "type.__init__() takes 1 or 3 arguments"); |
| 2292 | return -1; |
| 2293 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2295 | /* Call object.__init__(self) now. */ |
| 2296 | /* XXX Could call super(type, cls).__init__() but what's the point? */ |
| 2297 | args = PyTuple_GetSlice(args, 0, 0); |
| 2298 | res = object_init(cls, args, NULL); |
| 2299 | Py_DECREF(args); |
| 2300 | return res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 2303 | unsigned long |
Martin v. Löwis | 738236d | 2011-02-05 20:35:29 +0000 | [diff] [blame] | 2304 | PyType_GetFlags(PyTypeObject *type) |
| 2305 | { |
| 2306 | return type->tp_flags; |
| 2307 | } |
| 2308 | |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 2309 | /* Determine the most derived metatype. */ |
| 2310 | PyTypeObject * |
| 2311 | _PyType_CalculateMetaclass(PyTypeObject *metatype, PyObject *bases) |
| 2312 | { |
| 2313 | Py_ssize_t i, nbases; |
| 2314 | PyTypeObject *winner; |
| 2315 | PyObject *tmp; |
| 2316 | PyTypeObject *tmptype; |
| 2317 | |
| 2318 | /* Determine the proper metatype to deal with this, |
| 2319 | and check for metatype conflicts while we're at it. |
| 2320 | Note that if some other metatype wins to contract, |
| 2321 | it's possible that its instances are not types. */ |
| 2322 | |
| 2323 | nbases = PyTuple_GET_SIZE(bases); |
| 2324 | winner = metatype; |
| 2325 | for (i = 0; i < nbases; i++) { |
| 2326 | tmp = PyTuple_GET_ITEM(bases, i); |
| 2327 | tmptype = Py_TYPE(tmp); |
| 2328 | if (PyType_IsSubtype(winner, tmptype)) |
| 2329 | continue; |
| 2330 | if (PyType_IsSubtype(tmptype, winner)) { |
| 2331 | winner = tmptype; |
| 2332 | continue; |
| 2333 | } |
| 2334 | /* else: */ |
| 2335 | PyErr_SetString(PyExc_TypeError, |
| 2336 | "metaclass conflict: " |
| 2337 | "the metaclass of a derived class " |
| 2338 | "must be a (non-strict) subclass " |
| 2339 | "of the metaclasses of all its bases"); |
| 2340 | return NULL; |
| 2341 | } |
| 2342 | return winner; |
| 2343 | } |
| 2344 | |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 2345 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2346 | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) |
| 2347 | { |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2348 | PyObject *name, *bases = NULL, *orig_dict, *dict = NULL; |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2349 | PyObject *qualname, *slots = NULL, *tmp, *newslots, *cell; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2350 | PyTypeObject *type = NULL, *base, *tmptype, *winner; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2351 | PyHeapTypeObject *et; |
| 2352 | PyMemberDef *mp; |
Serhiy Storchaka | 42bf8fc | 2015-12-30 21:40:49 +0200 | [diff] [blame] | 2353 | Py_ssize_t i, nbases, nslots, slotoffset, name_size; |
| 2354 | int j, may_add_dict, may_add_weak, add_dict, add_weak; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2355 | _Py_IDENTIFIER(__qualname__); |
| 2356 | _Py_IDENTIFIER(__slots__); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2357 | _Py_IDENTIFIER(__classcell__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2358 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2359 | assert(args != NULL && PyTuple_Check(args)); |
| 2360 | assert(kwds == NULL || PyDict_Check(kwds)); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2362 | /* Special case: type(x) should return x->ob_type */ |
Berker Peksag | 3f015a6 | 2016-08-19 11:04:07 +0300 | [diff] [blame] | 2363 | /* We only want type itself to accept the one-argument form (#27157) |
| 2364 | Note: We don't call PyType_CheckExact as that also allows subclasses */ |
| 2365 | if (metatype == &PyType_Type) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2366 | const Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2367 | const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_GET_SIZE(kwds); |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2368 | |
Berker Peksag | 3f015a6 | 2016-08-19 11:04:07 +0300 | [diff] [blame] | 2369 | if (nargs == 1 && nkwds == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2370 | PyObject *x = PyTuple_GET_ITEM(args, 0); |
| 2371 | Py_INCREF(Py_TYPE(x)); |
| 2372 | return (PyObject *) Py_TYPE(x); |
| 2373 | } |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 2374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2375 | /* SF bug 475327 -- if that didn't trigger, we need 3 |
Berker Peksag | 78e24d4 | 2018-01-04 13:24:45 +0300 | [diff] [blame] | 2376 | arguments. but PyArg_ParseTuple below may give |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2377 | a msg saying type() needs exactly 3. */ |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 2378 | if (nargs != 3) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2379 | PyErr_SetString(PyExc_TypeError, |
| 2380 | "type() takes 1 or 3 arguments"); |
| 2381 | return NULL; |
| 2382 | } |
| 2383 | } |
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 | /* Check arguments: (name, bases, dict) */ |
Berker Peksag | 3f015a6 | 2016-08-19 11:04:07 +0300 | [diff] [blame] | 2386 | if (!PyArg_ParseTuple(args, "UO!O!:type.__new__", &name, &PyTuple_Type, |
| 2387 | &bases, &PyDict_Type, &orig_dict)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2388 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2390 | /* Adjust for empty tuple bases */ |
Nick Coghlan | de31b19 | 2011-10-23 22:04:16 +1000 | [diff] [blame] | 2391 | nbases = PyTuple_GET_SIZE(bases); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2392 | if (nbases == 0) { |
scoder | 2102c78 | 2017-10-01 10:37:47 +0200 | [diff] [blame] | 2393 | base = &PyBaseObject_Type; |
| 2394 | bases = PyTuple_Pack(1, base); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2395 | if (bases == NULL) |
scoder | 2102c78 | 2017-10-01 10:37:47 +0200 | [diff] [blame] | 2396 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2397 | nbases = 1; |
| 2398 | } |
scoder | 2102c78 | 2017-10-01 10:37:47 +0200 | [diff] [blame] | 2399 | else { |
Ivan Levkivskyi | 2b5fd1e | 2017-12-14 23:32:56 +0100 | [diff] [blame] | 2400 | _Py_IDENTIFIER(__mro_entries__); |
| 2401 | for (i = 0; i < nbases; i++) { |
| 2402 | tmp = PyTuple_GET_ITEM(bases, i); |
| 2403 | if (PyType_Check(tmp)) { |
| 2404 | continue; |
| 2405 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 2406 | if (_PyObject_LookupAttrId(tmp, &PyId___mro_entries__, &tmp) < 0) { |
| 2407 | return NULL; |
| 2408 | } |
Ivan Levkivskyi | 2b5fd1e | 2017-12-14 23:32:56 +0100 | [diff] [blame] | 2409 | if (tmp != NULL) { |
| 2410 | PyErr_SetString(PyExc_TypeError, |
| 2411 | "type() doesn't support MRO entry resolution; " |
| 2412 | "use types.new_class()"); |
| 2413 | Py_DECREF(tmp); |
| 2414 | return NULL; |
| 2415 | } |
Ivan Levkivskyi | 2b5fd1e | 2017-12-14 23:32:56 +0100 | [diff] [blame] | 2416 | } |
scoder | 2102c78 | 2017-10-01 10:37:47 +0200 | [diff] [blame] | 2417 | /* Search the bases for the proper metatype to deal with this: */ |
| 2418 | winner = _PyType_CalculateMetaclass(metatype, bases); |
| 2419 | if (winner == NULL) { |
| 2420 | return NULL; |
| 2421 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2422 | |
scoder | 2102c78 | 2017-10-01 10:37:47 +0200 | [diff] [blame] | 2423 | if (winner != metatype) { |
| 2424 | if (winner->tp_new != type_new) /* Pass it to the winner */ |
| 2425 | return winner->tp_new(winner, args, kwds); |
| 2426 | metatype = winner; |
| 2427 | } |
| 2428 | |
| 2429 | /* Calculate best base, and check that all bases are type objects */ |
| 2430 | base = best_base(bases); |
| 2431 | if (base == NULL) { |
| 2432 | return NULL; |
| 2433 | } |
| 2434 | |
| 2435 | Py_INCREF(bases); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2436 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2437 | |
scoder | 2102c78 | 2017-10-01 10:37:47 +0200 | [diff] [blame] | 2438 | /* Use "goto error" from this point on as we now own the reference to "bases". */ |
| 2439 | |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2440 | dict = PyDict_Copy(orig_dict); |
| 2441 | if (dict == NULL) |
| 2442 | goto error; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2443 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2444 | /* Check for a __slots__ sequence variable in dict, and count it */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2445 | slots = _PyDict_GetItemId(dict, &PyId___slots__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2446 | nslots = 0; |
| 2447 | add_dict = 0; |
| 2448 | add_weak = 0; |
| 2449 | may_add_dict = base->tp_dictoffset == 0; |
| 2450 | may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0; |
| 2451 | if (slots == NULL) { |
| 2452 | if (may_add_dict) { |
| 2453 | add_dict++; |
| 2454 | } |
| 2455 | if (may_add_weak) { |
| 2456 | add_weak++; |
| 2457 | } |
| 2458 | } |
| 2459 | else { |
| 2460 | /* Have slots */ |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2461 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2462 | /* Make it into a tuple */ |
| 2463 | if (PyUnicode_Check(slots)) |
| 2464 | slots = PyTuple_Pack(1, slots); |
| 2465 | else |
| 2466 | slots = PySequence_Tuple(slots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2467 | if (slots == NULL) |
| 2468 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2469 | assert(PyTuple_Check(slots)); |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2470 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2471 | /* Are slots allowed? */ |
| 2472 | nslots = PyTuple_GET_SIZE(slots); |
| 2473 | if (nslots > 0 && base->tp_itemsize != 0) { |
| 2474 | PyErr_Format(PyExc_TypeError, |
| 2475 | "nonempty __slots__ " |
| 2476 | "not supported for subtype of '%s'", |
| 2477 | base->tp_name); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2478 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2479 | } |
Guido van Rossum | ad47da0 | 2002-08-12 19:05:44 +0000 | [diff] [blame] | 2480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2481 | /* Check for valid slot names and two special cases */ |
| 2482 | for (i = 0; i < nslots; i++) { |
| 2483 | PyObject *tmp = PyTuple_GET_ITEM(slots, i); |
| 2484 | if (!valid_identifier(tmp)) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2485 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2486 | assert(PyUnicode_Check(tmp)); |
Serhiy Storchaka | f5894dd | 2016-11-16 15:40:39 +0200 | [diff] [blame] | 2487 | if (_PyUnicode_EqualToASCIIId(tmp, &PyId___dict__)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2488 | if (!may_add_dict || add_dict) { |
| 2489 | PyErr_SetString(PyExc_TypeError, |
| 2490 | "__dict__ slot disallowed: " |
| 2491 | "we already got one"); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2492 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2493 | } |
| 2494 | add_dict++; |
| 2495 | } |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 2496 | if (_PyUnicode_EqualToASCIIString(tmp, "__weakref__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2497 | if (!may_add_weak || add_weak) { |
| 2498 | PyErr_SetString(PyExc_TypeError, |
| 2499 | "__weakref__ slot disallowed: " |
| 2500 | "either we already got one, " |
| 2501 | "or __itemsize__ != 0"); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2502 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2503 | } |
| 2504 | add_weak++; |
| 2505 | } |
| 2506 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2508 | /* Copy slots into a list, mangle names and sort them. |
| 2509 | Sorted names are needed for __class__ assignment. |
| 2510 | Convert them back to tuple at the end. |
| 2511 | */ |
| 2512 | newslots = PyList_New(nslots - add_dict - add_weak); |
| 2513 | if (newslots == NULL) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2514 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2515 | for (i = j = 0; i < nslots; i++) { |
| 2516 | tmp = PyTuple_GET_ITEM(slots, i); |
| 2517 | if ((add_dict && |
Serhiy Storchaka | f5894dd | 2016-11-16 15:40:39 +0200 | [diff] [blame] | 2518 | _PyUnicode_EqualToASCIIId(tmp, &PyId___dict__)) || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2519 | (add_weak && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 2520 | _PyUnicode_EqualToASCIIString(tmp, "__weakref__"))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2521 | continue; |
| 2522 | tmp =_Py_Mangle(name, tmp); |
Benjamin Peterson | ae13c88 | 2011-08-16 22:26:48 -0500 | [diff] [blame] | 2523 | if (!tmp) { |
| 2524 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2525 | goto error; |
Benjamin Peterson | ae13c88 | 2011-08-16 22:26:48 -0500 | [diff] [blame] | 2526 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2527 | PyList_SET_ITEM(newslots, j, tmp); |
Benjamin Peterson | c4085c8 | 2011-08-16 18:53:26 -0500 | [diff] [blame] | 2528 | if (PyDict_GetItem(dict, tmp)) { |
Xiang Zhang | c393ee8 | 2017-03-08 11:18:49 +0800 | [diff] [blame] | 2529 | /* CPython inserts __qualname__ and __classcell__ (when needed) |
| 2530 | into the namespace when creating a class. They will be deleted |
| 2531 | below so won't act as class variables. */ |
| 2532 | if (!_PyUnicode_EqualToASCIIId(tmp, &PyId___qualname__) && |
| 2533 | !_PyUnicode_EqualToASCIIId(tmp, &PyId___classcell__)) { |
| 2534 | PyErr_Format(PyExc_ValueError, |
| 2535 | "%R in __slots__ conflicts with class variable", |
| 2536 | tmp); |
| 2537 | Py_DECREF(newslots); |
| 2538 | goto error; |
| 2539 | } |
Benjamin Peterson | c4085c8 | 2011-08-16 18:53:26 -0500 | [diff] [blame] | 2540 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2541 | j++; |
| 2542 | } |
| 2543 | assert(j == nslots - add_dict - add_weak); |
| 2544 | nslots = j; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2545 | Py_CLEAR(slots); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2546 | if (PyList_Sort(newslots) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2547 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2548 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2549 | } |
| 2550 | slots = PyList_AsTuple(newslots); |
| 2551 | Py_DECREF(newslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2552 | if (slots == NULL) |
| 2553 | goto error; |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 2554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2555 | /* Secondary bases may provide weakrefs or dict */ |
| 2556 | if (nbases > 1 && |
| 2557 | ((may_add_dict && !add_dict) || |
| 2558 | (may_add_weak && !add_weak))) { |
| 2559 | for (i = 0; i < nbases; i++) { |
| 2560 | tmp = PyTuple_GET_ITEM(bases, i); |
| 2561 | if (tmp == (PyObject *)base) |
| 2562 | continue; /* Skip primary base */ |
| 2563 | assert(PyType_Check(tmp)); |
| 2564 | tmptype = (PyTypeObject *)tmp; |
| 2565 | if (may_add_dict && !add_dict && |
| 2566 | tmptype->tp_dictoffset != 0) |
| 2567 | add_dict++; |
| 2568 | if (may_add_weak && !add_weak && |
| 2569 | tmptype->tp_weaklistoffset != 0) |
| 2570 | add_weak++; |
| 2571 | if (may_add_dict && !add_dict) |
| 2572 | continue; |
| 2573 | if (may_add_weak && !add_weak) |
| 2574 | continue; |
| 2575 | /* Nothing more to check */ |
| 2576 | break; |
| 2577 | } |
| 2578 | } |
| 2579 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2580 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2581 | /* Allocate the type object */ |
| 2582 | type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2583 | if (type == NULL) |
| 2584 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2586 | /* Keep name and slots alive in the extended type object */ |
| 2587 | et = (PyHeapTypeObject *)type; |
| 2588 | Py_INCREF(name); |
| 2589 | et->ht_name = name; |
| 2590 | et->ht_slots = slots; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2591 | slots = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2592 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2593 | /* Initialize tp_flags */ |
| 2594 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 2595 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_FINALIZE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2596 | if (base->tp_flags & Py_TPFLAGS_HAVE_GC) |
| 2597 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2599 | /* Initialize essential fields */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2600 | type->tp_as_async = &et->as_async; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2601 | type->tp_as_number = &et->as_number; |
| 2602 | type->tp_as_sequence = &et->as_sequence; |
| 2603 | type->tp_as_mapping = &et->as_mapping; |
| 2604 | type->tp_as_buffer = &et->as_buffer; |
Serhiy Storchaka | 42bf8fc | 2015-12-30 21:40:49 +0200 | [diff] [blame] | 2605 | type->tp_name = PyUnicode_AsUTF8AndSize(name, &name_size); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2606 | if (!type->tp_name) |
| 2607 | goto error; |
Serhiy Storchaka | 42bf8fc | 2015-12-30 21:40:49 +0200 | [diff] [blame] | 2608 | if (strlen(type->tp_name) != (size_t)name_size) { |
| 2609 | PyErr_SetString(PyExc_ValueError, |
| 2610 | "type name must not contain null characters"); |
| 2611 | goto error; |
| 2612 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2614 | /* Set tp_base and tp_bases */ |
| 2615 | type->tp_bases = bases; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2616 | bases = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2617 | Py_INCREF(base); |
| 2618 | type->tp_base = base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2620 | /* Initialize tp_dict from passed-in dict */ |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2621 | Py_INCREF(dict); |
| 2622 | type->tp_dict = dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2623 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2624 | /* Set __module__ in the dict */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2625 | if (_PyDict_GetItemId(dict, &PyId___module__) == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2626 | tmp = PyEval_GetGlobals(); |
| 2627 | if (tmp != NULL) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2628 | tmp = _PyDict_GetItemId(tmp, &PyId___name__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2629 | if (tmp != NULL) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2630 | if (_PyDict_SetItemId(dict, &PyId___module__, |
| 2631 | tmp) < 0) |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2632 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2633 | } |
| 2634 | } |
| 2635 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 2636 | |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2637 | /* Set ht_qualname to dict['__qualname__'] if available, else to |
| 2638 | __name__. The __qualname__ accessor will look for ht_qualname. |
| 2639 | */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2640 | qualname = _PyDict_GetItemId(dict, &PyId___qualname__); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2641 | if (qualname != NULL) { |
| 2642 | if (!PyUnicode_Check(qualname)) { |
| 2643 | PyErr_Format(PyExc_TypeError, |
| 2644 | "type __qualname__ must be a str, not %s", |
| 2645 | Py_TYPE(qualname)->tp_name); |
| 2646 | goto error; |
| 2647 | } |
| 2648 | } |
Benjamin Peterson | 8afa7fa | 2012-10-30 23:51:03 -0400 | [diff] [blame] | 2649 | et->ht_qualname = qualname ? qualname : et->ht_name; |
| 2650 | Py_INCREF(et->ht_qualname); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2651 | if (qualname != NULL && _PyDict_DelItemId(dict, &PyId___qualname__) < 0) |
Benjamin Peterson | 8afa7fa | 2012-10-30 23:51:03 -0400 | [diff] [blame] | 2652 | goto error; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2653 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2654 | /* Set tp_doc to a copy of dict['__doc__'], if the latter is there |
| 2655 | and is a string. The __doc__ accessor will first look for tp_doc; |
| 2656 | if that fails, it will still look into __dict__. |
| 2657 | */ |
| 2658 | { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2659 | PyObject *doc = _PyDict_GetItemId(dict, &PyId___doc__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2660 | if (doc != NULL && PyUnicode_Check(doc)) { |
| 2661 | Py_ssize_t len; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 2662 | const char *doc_str; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2663 | char *tp_doc; |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 2664 | |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 2665 | doc_str = PyUnicode_AsUTF8(doc); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2666 | if (doc_str == NULL) |
| 2667 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2668 | /* Silently truncate the docstring if it contains null bytes. */ |
| 2669 | len = strlen(doc_str); |
| 2670 | tp_doc = (char *)PyObject_MALLOC(len + 1); |
Victor Stinner | 53510cd | 2013-07-15 19:34:20 +0200 | [diff] [blame] | 2671 | if (tp_doc == NULL) { |
| 2672 | PyErr_NoMemory(); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2673 | goto error; |
Victor Stinner | 53510cd | 2013-07-15 19:34:20 +0200 | [diff] [blame] | 2674 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2675 | memcpy(tp_doc, doc_str, len + 1); |
| 2676 | type->tp_doc = tp_doc; |
| 2677 | } |
| 2678 | } |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2679 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2680 | /* Special-case __new__: if it's a plain function, |
| 2681 | make it a static function */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2682 | tmp = _PyDict_GetItemId(dict, &PyId___new__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2683 | if (tmp != NULL && PyFunction_Check(tmp)) { |
| 2684 | tmp = PyStaticMethod_New(tmp); |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2685 | if (tmp == NULL) |
| 2686 | goto error; |
Serhiy Storchaka | 484c913 | 2016-06-05 10:48:36 +0300 | [diff] [blame] | 2687 | if (_PyDict_SetItemId(dict, &PyId___new__, tmp) < 0) { |
| 2688 | Py_DECREF(tmp); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2689 | goto error; |
Serhiy Storchaka | 484c913 | 2016-06-05 10:48:36 +0300 | [diff] [blame] | 2690 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2691 | Py_DECREF(tmp); |
| 2692 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2693 | |
Serhiy Storchaka | ce5b0e9 | 2018-01-05 00:21:41 +0200 | [diff] [blame] | 2694 | /* Special-case __init_subclass__ and __class_getitem__: |
| 2695 | if they are plain functions, make them classmethods */ |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 2696 | tmp = _PyDict_GetItemId(dict, &PyId___init_subclass__); |
| 2697 | if (tmp != NULL && PyFunction_Check(tmp)) { |
| 2698 | tmp = PyClassMethod_New(tmp); |
| 2699 | if (tmp == NULL) |
| 2700 | goto error; |
| 2701 | if (_PyDict_SetItemId(dict, &PyId___init_subclass__, tmp) < 0) { |
| 2702 | Py_DECREF(tmp); |
| 2703 | goto error; |
| 2704 | } |
| 2705 | Py_DECREF(tmp); |
| 2706 | } |
| 2707 | |
Serhiy Storchaka | ce5b0e9 | 2018-01-05 00:21:41 +0200 | [diff] [blame] | 2708 | tmp = _PyDict_GetItemId(dict, &PyId___class_getitem__); |
| 2709 | if (tmp != NULL && PyFunction_Check(tmp)) { |
| 2710 | tmp = PyClassMethod_New(tmp); |
| 2711 | if (tmp == NULL) |
| 2712 | goto error; |
| 2713 | if (_PyDict_SetItemId(dict, &PyId___class_getitem__, tmp) < 0) { |
| 2714 | Py_DECREF(tmp); |
| 2715 | goto error; |
| 2716 | } |
| 2717 | Py_DECREF(tmp); |
| 2718 | } |
| 2719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2720 | /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
| 2721 | mp = PyHeapType_GET_MEMBERS(et); |
| 2722 | slotoffset = base->tp_basicsize; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2723 | if (et->ht_slots != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2724 | for (i = 0; i < nslots; i++, mp++) { |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 2725 | mp->name = PyUnicode_AsUTF8( |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2726 | PyTuple_GET_ITEM(et->ht_slots, i)); |
| 2727 | if (mp->name == NULL) |
| 2728 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2729 | mp->type = T_OBJECT_EX; |
| 2730 | mp->offset = slotoffset; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2731 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2732 | /* __dict__ and __weakref__ are already filtered out */ |
| 2733 | assert(strcmp(mp->name, "__dict__") != 0); |
| 2734 | assert(strcmp(mp->name, "__weakref__") != 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2735 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2736 | slotoffset += sizeof(PyObject *); |
| 2737 | } |
| 2738 | } |
| 2739 | if (add_dict) { |
| 2740 | if (base->tp_itemsize) |
| 2741 | type->tp_dictoffset = -(long)sizeof(PyObject *); |
| 2742 | else |
| 2743 | type->tp_dictoffset = slotoffset; |
| 2744 | slotoffset += sizeof(PyObject *); |
| 2745 | } |
| 2746 | if (add_weak) { |
| 2747 | assert(!base->tp_itemsize); |
| 2748 | type->tp_weaklistoffset = slotoffset; |
| 2749 | slotoffset += sizeof(PyObject *); |
| 2750 | } |
| 2751 | type->tp_basicsize = slotoffset; |
| 2752 | type->tp_itemsize = base->tp_itemsize; |
| 2753 | type->tp_members = PyHeapType_GET_MEMBERS(et); |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 2754 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2755 | if (type->tp_weaklistoffset && type->tp_dictoffset) |
| 2756 | type->tp_getset = subtype_getsets_full; |
| 2757 | else if (type->tp_weaklistoffset && !type->tp_dictoffset) |
| 2758 | type->tp_getset = subtype_getsets_weakref_only; |
| 2759 | else if (!type->tp_weaklistoffset && type->tp_dictoffset) |
| 2760 | type->tp_getset = subtype_getsets_dict_only; |
| 2761 | else |
| 2762 | type->tp_getset = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2764 | /* Special case some slots */ |
| 2765 | if (type->tp_dictoffset != 0 || nslots > 0) { |
| 2766 | if (base->tp_getattr == NULL && base->tp_getattro == NULL) |
| 2767 | type->tp_getattro = PyObject_GenericGetAttr; |
| 2768 | if (base->tp_setattr == NULL && base->tp_setattro == NULL) |
| 2769 | type->tp_setattro = PyObject_GenericSetAttr; |
| 2770 | } |
| 2771 | type->tp_dealloc = subtype_dealloc; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2772 | |
Antoine Pitrou | a63cc21 | 2015-04-13 20:10:06 +0200 | [diff] [blame] | 2773 | /* Enable GC unless this class is not adding new instance variables and |
| 2774 | the base class did not use GC. */ |
| 2775 | if ((base->tp_flags & Py_TPFLAGS_HAVE_GC) || |
| 2776 | type->tp_basicsize > base->tp_basicsize) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2777 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 2778 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2779 | /* Always override allocation strategy to use regular heap */ |
| 2780 | type->tp_alloc = PyType_GenericAlloc; |
| 2781 | if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { |
| 2782 | type->tp_free = PyObject_GC_Del; |
| 2783 | type->tp_traverse = subtype_traverse; |
| 2784 | type->tp_clear = subtype_clear; |
| 2785 | } |
| 2786 | else |
| 2787 | type->tp_free = PyObject_Del; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2788 | |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2789 | /* store type in class' cell if one is supplied */ |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2790 | cell = _PyDict_GetItemId(dict, &PyId___classcell__); |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2791 | if (cell != NULL) { |
| 2792 | /* At least one method requires a reference to its defining class */ |
| 2793 | if (!PyCell_Check(cell)) { |
| 2794 | PyErr_Format(PyExc_TypeError, |
| 2795 | "__classcell__ must be a nonlocal cell, not %.200R", |
| 2796 | Py_TYPE(cell)); |
| 2797 | goto error; |
| 2798 | } |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2799 | PyCell_Set(cell, (PyObject *) type); |
| 2800 | _PyDict_DelItemId(dict, &PyId___classcell__); |
| 2801 | PyErr_Clear(); |
| 2802 | } |
| 2803 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2804 | /* Initialize the rest */ |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2805 | if (PyType_Ready(type) < 0) |
| 2806 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2808 | /* Put the proper slots in place */ |
| 2809 | fixup_slot_dispatchers(type); |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 2810 | |
Benjamin Peterson | df81379 | 2014-03-17 15:57:17 -0500 | [diff] [blame] | 2811 | if (type->tp_dictoffset) { |
| 2812 | et->ht_cached_keys = _PyDict_NewKeysForClass(); |
| 2813 | } |
| 2814 | |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 2815 | if (set_names(type) < 0) |
| 2816 | goto error; |
| 2817 | |
| 2818 | if (init_subclass(type, kwds) < 0) |
| 2819 | goto error; |
| 2820 | |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2821 | Py_DECREF(dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2822 | return (PyObject *)type; |
Victor Stinner | 6f73874 | 2012-02-25 01:22:36 +0100 | [diff] [blame] | 2823 | |
| 2824 | error: |
| 2825 | Py_XDECREF(dict); |
| 2826 | Py_XDECREF(bases); |
| 2827 | Py_XDECREF(slots); |
| 2828 | Py_XDECREF(type); |
| 2829 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 2832 | static const short slotoffsets[] = { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2833 | -1, /* invalid slot */ |
| 2834 | #include "typeslots.inc" |
| 2835 | }; |
| 2836 | |
Benjamin Peterson | e28108c | 2012-01-29 20:13:18 -0500 | [diff] [blame] | 2837 | PyObject * |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2838 | PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases) |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2839 | { |
| 2840 | PyHeapTypeObject *res = (PyHeapTypeObject*)PyType_GenericAlloc(&PyType_Type, 0); |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2841 | PyTypeObject *type, *base; |
Nick Coghlan | a48db2b | 2015-05-24 01:03:46 +1000 | [diff] [blame] | 2842 | PyObject *modname; |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2843 | char *s; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2844 | char *res_start = (char*)res; |
| 2845 | PyType_Slot *slot; |
Victor Stinner | 54e4ca7 | 2013-07-11 22:42:25 +0200 | [diff] [blame] | 2846 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2847 | /* Set the type name and qualname */ |
| 2848 | s = strrchr(spec->name, '.'); |
| 2849 | if (s == NULL) |
| 2850 | s = (char*)spec->name; |
| 2851 | else |
| 2852 | s++; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2853 | |
Martin v. Löwis | 5e06a5d | 2011-02-21 16:24:00 +0000 | [diff] [blame] | 2854 | if (res == NULL) |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2855 | return NULL; |
| 2856 | type = &res->ht_type; |
Antoine Pitrou | 66a3a7e | 2012-06-24 00:42:59 +0200 | [diff] [blame] | 2857 | /* The flags must be initialized early, before the GC traverses us */ |
| 2858 | type->tp_flags = spec->flags | Py_TPFLAGS_HEAPTYPE; |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2859 | res->ht_name = PyUnicode_FromString(s); |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2860 | if (!res->ht_name) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2861 | goto fail; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2862 | res->ht_qualname = res->ht_name; |
| 2863 | Py_INCREF(res->ht_qualname); |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2864 | type->tp_name = spec->name; |
| 2865 | if (!type->tp_name) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2866 | goto fail; |
Victor Stinner | 54e4ca7 | 2013-07-11 22:42:25 +0200 | [diff] [blame] | 2867 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2868 | /* Adjust for empty tuple bases */ |
| 2869 | if (!bases) { |
| 2870 | base = &PyBaseObject_Type; |
| 2871 | /* See whether Py_tp_base(s) was specified */ |
| 2872 | for (slot = spec->slots; slot->slot; slot++) { |
| 2873 | if (slot->slot == Py_tp_base) |
| 2874 | base = slot->pfunc; |
| 2875 | else if (slot->slot == Py_tp_bases) { |
| 2876 | bases = slot->pfunc; |
| 2877 | Py_INCREF(bases); |
| 2878 | } |
| 2879 | } |
| 2880 | if (!bases) |
| 2881 | bases = PyTuple_Pack(1, base); |
| 2882 | if (!bases) |
| 2883 | goto fail; |
| 2884 | } |
| 2885 | else |
| 2886 | Py_INCREF(bases); |
| 2887 | |
| 2888 | /* Calculate best base, and check that all bases are type objects */ |
| 2889 | base = best_base(bases); |
| 2890 | if (base == NULL) { |
| 2891 | goto fail; |
| 2892 | } |
| 2893 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 2894 | PyErr_Format(PyExc_TypeError, |
| 2895 | "type '%.100s' is not an acceptable base type", |
| 2896 | base->tp_name); |
| 2897 | goto fail; |
| 2898 | } |
| 2899 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2900 | /* Initialize essential fields */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2901 | type->tp_as_async = &res->as_async; |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2902 | type->tp_as_number = &res->as_number; |
| 2903 | type->tp_as_sequence = &res->as_sequence; |
| 2904 | type->tp_as_mapping = &res->as_mapping; |
| 2905 | type->tp_as_buffer = &res->as_buffer; |
| 2906 | /* Set tp_base and tp_bases */ |
| 2907 | type->tp_bases = bases; |
| 2908 | bases = NULL; |
| 2909 | Py_INCREF(base); |
| 2910 | type->tp_base = base; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2911 | |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2912 | type->tp_basicsize = spec->basicsize; |
| 2913 | type->tp_itemsize = spec->itemsize; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 2914 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2915 | for (slot = spec->slots; slot->slot; slot++) { |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 2916 | if (slot->slot < 0 |
| 2917 | || (size_t)slot->slot >= Py_ARRAY_LENGTH(slotoffsets)) { |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2918 | PyErr_SetString(PyExc_RuntimeError, "invalid slot offset"); |
| 2919 | goto fail; |
| 2920 | } |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2921 | if (slot->slot == Py_tp_base || slot->slot == Py_tp_bases) |
| 2922 | /* Processed above */ |
| 2923 | continue; |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2924 | *(void**)(res_start + slotoffsets[slot->slot]) = slot->pfunc; |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2925 | |
| 2926 | /* need to make a copy of the docstring slot, which usually |
| 2927 | points to a static string literal */ |
| 2928 | if (slot->slot == Py_tp_doc) { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2929 | const char *old_doc = _PyType_DocWithoutSignature(type->tp_name, slot->pfunc); |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 2930 | size_t len = strlen(old_doc)+1; |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2931 | char *tp_doc = PyObject_MALLOC(len); |
Victor Stinner | 53510cd | 2013-07-15 19:34:20 +0200 | [diff] [blame] | 2932 | if (tp_doc == NULL) { |
| 2933 | PyErr_NoMemory(); |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2934 | goto fail; |
Victor Stinner | 53510cd | 2013-07-15 19:34:20 +0200 | [diff] [blame] | 2935 | } |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 2936 | memcpy(tp_doc, old_doc, len); |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2937 | type->tp_doc = tp_doc; |
Georg Brandl | 032400b | 2011-02-19 21:47:02 +0000 | [diff] [blame] | 2938 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2939 | } |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2940 | if (type->tp_dealloc == NULL) { |
Antoine Pitrou | 99cc629 | 2012-06-23 14:42:38 +0200 | [diff] [blame] | 2941 | /* It's a heap type, so needs the heap types' dealloc. |
| 2942 | subtype_dealloc will call the base type's tp_dealloc, if |
| 2943 | necessary. */ |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2944 | type->tp_dealloc = subtype_dealloc; |
Antoine Pitrou | 99cc629 | 2012-06-23 14:42:38 +0200 | [diff] [blame] | 2945 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2946 | |
Antoine Pitrou | bb78f57 | 2012-06-24 00:18:27 +0200 | [diff] [blame] | 2947 | if (PyType_Ready(type) < 0) |
Benjamin Peterson | 2652d25 | 2012-01-29 20:16:37 -0500 | [diff] [blame] | 2948 | goto fail; |
| 2949 | |
Benjamin Peterson | df81379 | 2014-03-17 15:57:17 -0500 | [diff] [blame] | 2950 | if (type->tp_dictoffset) { |
| 2951 | res->ht_cached_keys = _PyDict_NewKeysForClass(); |
| 2952 | } |
| 2953 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2954 | /* Set type.__module__ */ |
| 2955 | s = strrchr(spec->name, '.'); |
Nick Coghlan | a48db2b | 2015-05-24 01:03:46 +1000 | [diff] [blame] | 2956 | if (s != NULL) { |
Christian Heimes | 5cade88 | 2016-10-13 21:10:31 +0200 | [diff] [blame] | 2957 | int err; |
Nick Coghlan | a48db2b | 2015-05-24 01:03:46 +1000 | [diff] [blame] | 2958 | modname = PyUnicode_FromStringAndSize( |
| 2959 | spec->name, (Py_ssize_t)(s - spec->name)); |
| 2960 | if (modname == NULL) { |
| 2961 | goto fail; |
| 2962 | } |
Christian Heimes | 5cade88 | 2016-10-13 21:10:31 +0200 | [diff] [blame] | 2963 | err = _PyDict_SetItemId(type->tp_dict, &PyId___module__, modname); |
Nick Coghlan | a48db2b | 2015-05-24 01:03:46 +1000 | [diff] [blame] | 2964 | Py_DECREF(modname); |
Christian Heimes | 5cade88 | 2016-10-13 21:10:31 +0200 | [diff] [blame] | 2965 | if (err != 0) |
| 2966 | goto fail; |
Nick Coghlan | a48db2b | 2015-05-24 01:03:46 +1000 | [diff] [blame] | 2967 | } else { |
Serhiy Storchaka | 490055a | 2015-03-01 10:03:02 +0200 | [diff] [blame] | 2968 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
Serhiy Storchaka | 71c6f44 | 2015-03-01 14:39:20 +0200 | [diff] [blame] | 2969 | "builtin type %.200s has no __module__ attribute", |
Serhiy Storchaka | 490055a | 2015-03-01 10:03:02 +0200 | [diff] [blame] | 2970 | spec->name)) |
| 2971 | goto fail; |
| 2972 | } |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2973 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2974 | return (PyObject*)res; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 2975 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2976 | fail: |
| 2977 | Py_DECREF(res); |
| 2978 | return NULL; |
| 2979 | } |
| 2980 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 2981 | PyObject * |
| 2982 | PyType_FromSpec(PyType_Spec *spec) |
| 2983 | { |
| 2984 | return PyType_FromSpecWithBases(spec, NULL); |
| 2985 | } |
| 2986 | |
Martin v. Löwis | ca7b046 | 2014-02-04 09:33:05 +0100 | [diff] [blame] | 2987 | void * |
| 2988 | PyType_GetSlot(PyTypeObject *type, int slot) |
| 2989 | { |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 2990 | if (!PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE) || slot < 0) { |
Martin v. Löwis | ca7b046 | 2014-02-04 09:33:05 +0100 | [diff] [blame] | 2991 | PyErr_BadInternalCall(); |
| 2992 | return NULL; |
| 2993 | } |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 2994 | if ((size_t)slot >= Py_ARRAY_LENGTH(slotoffsets)) { |
Martin v. Löwis | ca7b046 | 2014-02-04 09:33:05 +0100 | [diff] [blame] | 2995 | /* Extension module requesting slot from a future version */ |
| 2996 | return NULL; |
| 2997 | } |
| 2998 | return *(void**)(((char*)type) + slotoffsets[slot]); |
| 2999 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 3000 | |
scoder | 2102c78 | 2017-10-01 10:37:47 +0200 | [diff] [blame] | 3001 | /* Internal API to look for a name through the MRO, bypassing the method cache. |
| 3002 | This returns a borrowed reference, and might set an exception. |
| 3003 | 'error' is set to: -1: error with exception; 1: error without exception; 0: ok */ |
| 3004 | static PyObject * |
| 3005 | find_name_in_mro(PyTypeObject *type, PyObject *name, int *error) |
| 3006 | { |
| 3007 | Py_ssize_t i, n; |
| 3008 | PyObject *mro, *res, *base, *dict; |
| 3009 | Py_hash_t hash; |
| 3010 | |
| 3011 | if (!PyUnicode_CheckExact(name) || |
| 3012 | (hash = ((PyASCIIObject *) name)->hash) == -1) |
| 3013 | { |
| 3014 | hash = PyObject_Hash(name); |
| 3015 | if (hash == -1) { |
| 3016 | *error = -1; |
| 3017 | return NULL; |
| 3018 | } |
| 3019 | } |
| 3020 | |
| 3021 | /* Look in tp_dict of types in MRO */ |
| 3022 | mro = type->tp_mro; |
| 3023 | |
| 3024 | if (mro == NULL) { |
| 3025 | if ((type->tp_flags & Py_TPFLAGS_READYING) == 0) { |
| 3026 | if (PyType_Ready(type) < 0) { |
| 3027 | *error = -1; |
| 3028 | return NULL; |
| 3029 | } |
| 3030 | mro = type->tp_mro; |
| 3031 | } |
| 3032 | if (mro == NULL) { |
| 3033 | *error = 1; |
| 3034 | return NULL; |
| 3035 | } |
| 3036 | } |
| 3037 | |
| 3038 | res = NULL; |
| 3039 | /* Keep a strong reference to mro because type->tp_mro can be replaced |
| 3040 | during dict lookup, e.g. when comparing to non-string keys. */ |
| 3041 | Py_INCREF(mro); |
| 3042 | assert(PyTuple_Check(mro)); |
| 3043 | n = PyTuple_GET_SIZE(mro); |
| 3044 | for (i = 0; i < n; i++) { |
| 3045 | base = PyTuple_GET_ITEM(mro, i); |
| 3046 | assert(PyType_Check(base)); |
| 3047 | dict = ((PyTypeObject *)base)->tp_dict; |
| 3048 | assert(dict && PyDict_Check(dict)); |
| 3049 | res = _PyDict_GetItem_KnownHash(dict, name, hash); |
| 3050 | if (res != NULL) |
| 3051 | break; |
| 3052 | if (PyErr_Occurred()) { |
| 3053 | *error = -1; |
| 3054 | goto done; |
| 3055 | } |
| 3056 | } |
| 3057 | *error = 0; |
| 3058 | done: |
| 3059 | Py_DECREF(mro); |
| 3060 | return res; |
| 3061 | } |
| 3062 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3063 | /* Internal API to look for a name through the MRO. |
| 3064 | This returns a borrowed reference, and doesn't set an exception! */ |
| 3065 | PyObject * |
| 3066 | _PyType_Lookup(PyTypeObject *type, PyObject *name) |
| 3067 | { |
scoder | 2102c78 | 2017-10-01 10:37:47 +0200 | [diff] [blame] | 3068 | PyObject *res; |
| 3069 | int error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3070 | unsigned int h; |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 3071 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3072 | if (MCACHE_CACHEABLE_NAME(name) && |
| 3073 | PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) { |
| 3074 | /* fast path */ |
| 3075 | h = MCACHE_HASH_METHOD(type, name); |
| 3076 | if (method_cache[h].version == type->tp_version_tag && |
Antoine Pitrou | 2a40e36 | 2014-11-15 00:56:27 +0100 | [diff] [blame] | 3077 | method_cache[h].name == name) { |
| 3078 | #if MCACHE_STATS |
| 3079 | method_cache_hits++; |
| 3080 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3081 | return method_cache[h].value; |
Antoine Pitrou | 2a40e36 | 2014-11-15 00:56:27 +0100 | [diff] [blame] | 3082 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3083 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3084 | |
scoder | 2102c78 | 2017-10-01 10:37:47 +0200 | [diff] [blame] | 3085 | /* We may end up clearing live exceptions below, so make sure it's ours. */ |
| 3086 | assert(!PyErr_Occurred()); |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 3087 | |
scoder | 2102c78 | 2017-10-01 10:37:47 +0200 | [diff] [blame] | 3088 | res = find_name_in_mro(type, name, &error); |
| 3089 | /* Only put NULL results into cache if there was no error. */ |
| 3090 | if (error) { |
| 3091 | /* It's not ideal to clear the error condition, |
| 3092 | but this function is documented as not setting |
| 3093 | an exception, and I don't want to change that. |
| 3094 | E.g., when PyType_Ready() can't proceed, it won't |
| 3095 | set the "ready" flag, so future attempts to ready |
| 3096 | the same type will call it again -- hopefully |
| 3097 | in a context that propagates the exception out. |
| 3098 | */ |
| 3099 | if (error == -1) { |
Serhiy Storchaka | 8ef3460 | 2016-10-08 12:24:09 +0300 | [diff] [blame] | 3100 | PyErr_Clear(); |
Serhiy Storchaka | 8ef3460 | 2016-10-08 12:24:09 +0300 | [diff] [blame] | 3101 | } |
scoder | 2102c78 | 2017-10-01 10:37:47 +0200 | [diff] [blame] | 3102 | return NULL; |
Serhiy Storchaka | 8ef3460 | 2016-10-08 12:24:09 +0300 | [diff] [blame] | 3103 | } |
Guido van Rossum | 2309498 | 2002-06-10 14:30:43 +0000 | [diff] [blame] | 3104 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3105 | if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) { |
| 3106 | h = MCACHE_HASH_METHOD(type, name); |
| 3107 | method_cache[h].version = type->tp_version_tag; |
| 3108 | method_cache[h].value = res; /* borrowed */ |
| 3109 | Py_INCREF(name); |
Antoine Pitrou | 2a40e36 | 2014-11-15 00:56:27 +0100 | [diff] [blame] | 3110 | assert(((PyASCIIObject *)(name))->hash != -1); |
| 3111 | #if MCACHE_STATS |
| 3112 | if (method_cache[h].name != Py_None && method_cache[h].name != name) |
| 3113 | method_cache_collisions++; |
| 3114 | else |
| 3115 | method_cache_misses++; |
| 3116 | #endif |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 3117 | Py_SETREF(method_cache[h].name, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3118 | } |
| 3119 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3120 | } |
| 3121 | |
Raymond Hettinger | 2ff2190 | 2013-10-01 00:55:43 -0700 | [diff] [blame] | 3122 | PyObject * |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3123 | _PyType_LookupId(PyTypeObject *type, struct _Py_Identifier *name) |
| 3124 | { |
| 3125 | PyObject *oname; |
| 3126 | oname = _PyUnicode_FromId(name); /* borrowed */ |
| 3127 | if (oname == NULL) |
| 3128 | return NULL; |
| 3129 | return _PyType_Lookup(type, oname); |
| 3130 | } |
| 3131 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3132 | /* This is similar to PyObject_GenericGetAttr(), |
| 3133 | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ |
| 3134 | static PyObject * |
| 3135 | type_getattro(PyTypeObject *type, PyObject *name) |
| 3136 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3137 | PyTypeObject *metatype = Py_TYPE(type); |
| 3138 | PyObject *meta_attribute, *attribute; |
| 3139 | descrgetfunc meta_get; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3140 | |
Benjamin Peterson | 16d84ac | 2012-03-16 09:32:59 -0500 | [diff] [blame] | 3141 | if (!PyUnicode_Check(name)) { |
| 3142 | PyErr_Format(PyExc_TypeError, |
| 3143 | "attribute name must be string, not '%.200s'", |
| 3144 | name->ob_type->tp_name); |
| 3145 | return NULL; |
| 3146 | } |
| 3147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3148 | /* Initialize this type (we'll assume the metatype is initialized) */ |
| 3149 | if (type->tp_dict == NULL) { |
| 3150 | if (PyType_Ready(type) < 0) |
| 3151 | return NULL; |
| 3152 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3153 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3154 | /* No readable descriptor found yet */ |
| 3155 | meta_get = NULL; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 3156 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3157 | /* Look for the attribute in the metatype */ |
| 3158 | meta_attribute = _PyType_Lookup(metatype, name); |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 3159 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3160 | if (meta_attribute != NULL) { |
| 3161 | meta_get = Py_TYPE(meta_attribute)->tp_descr_get; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 3162 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3163 | if (meta_get != NULL && PyDescr_IsData(meta_attribute)) { |
| 3164 | /* Data descriptors implement tp_descr_set to intercept |
| 3165 | * writes. Assume the attribute is not overridden in |
| 3166 | * type's tp_dict (and bases): call the descriptor now. |
| 3167 | */ |
| 3168 | return meta_get(meta_attribute, (PyObject *)type, |
| 3169 | (PyObject *)metatype); |
| 3170 | } |
| 3171 | Py_INCREF(meta_attribute); |
| 3172 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3174 | /* No data descriptor found on metatype. Look in tp_dict of this |
| 3175 | * type and its bases */ |
| 3176 | attribute = _PyType_Lookup(type, name); |
| 3177 | if (attribute != NULL) { |
| 3178 | /* Implement descriptor functionality, if any */ |
| 3179 | descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get; |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 3180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3181 | Py_XDECREF(meta_attribute); |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 3182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3183 | if (local_get != NULL) { |
| 3184 | /* NULL 2nd argument indicates the descriptor was |
| 3185 | * found on the target object itself (or a base) */ |
| 3186 | return local_get(attribute, (PyObject *)NULL, |
| 3187 | (PyObject *)type); |
| 3188 | } |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 3189 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3190 | Py_INCREF(attribute); |
| 3191 | return attribute; |
| 3192 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3193 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3194 | /* No attribute found in local __dict__ (or bases): use the |
| 3195 | * descriptor from the metatype, if any */ |
| 3196 | if (meta_get != NULL) { |
| 3197 | PyObject *res; |
| 3198 | res = meta_get(meta_attribute, (PyObject *)type, |
| 3199 | (PyObject *)metatype); |
| 3200 | Py_DECREF(meta_attribute); |
| 3201 | return res; |
| 3202 | } |
Guido van Rossum | bfc2e5e | 2002-04-04 17:50:54 +0000 | [diff] [blame] | 3203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3204 | /* If an ordinary attribute was found on the metatype, return it now */ |
| 3205 | if (meta_attribute != NULL) { |
| 3206 | return meta_attribute; |
| 3207 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3209 | /* Give up */ |
| 3210 | PyErr_Format(PyExc_AttributeError, |
| 3211 | "type object '%.50s' has no attribute '%U'", |
| 3212 | type->tp_name, name); |
| 3213 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3214 | } |
| 3215 | |
| 3216 | static int |
| 3217 | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) |
| 3218 | { |
Victor Stinner | bda5a2b | 2017-01-25 23:33:27 +0100 | [diff] [blame] | 3219 | int res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3220 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 3221 | PyErr_Format( |
| 3222 | PyExc_TypeError, |
| 3223 | "can't set attributes of built-in/extension type '%s'", |
| 3224 | type->tp_name); |
| 3225 | return -1; |
| 3226 | } |
Serhiy Storchaka | d896985 | 2017-05-20 08:48:06 +0300 | [diff] [blame] | 3227 | if (PyUnicode_Check(name)) { |
| 3228 | if (PyUnicode_CheckExact(name)) { |
| 3229 | if (PyUnicode_READY(name) == -1) |
| 3230 | return -1; |
| 3231 | Py_INCREF(name); |
| 3232 | } |
| 3233 | else { |
| 3234 | name = _PyUnicode_Copy(name); |
| 3235 | if (name == NULL) |
| 3236 | return -1; |
| 3237 | } |
| 3238 | PyUnicode_InternInPlace(&name); |
| 3239 | if (!PyUnicode_CHECK_INTERNED(name)) { |
| 3240 | PyErr_SetString(PyExc_MemoryError, |
| 3241 | "Out of memory interning an attribute name"); |
| 3242 | Py_DECREF(name); |
| 3243 | return -1; |
| 3244 | } |
| 3245 | } |
| 3246 | else { |
| 3247 | /* Will fail in _PyObject_GenericSetAttrWithDict. */ |
| 3248 | Py_INCREF(name); |
| 3249 | } |
| 3250 | res = _PyObject_GenericSetAttrWithDict((PyObject *)type, name, value, NULL); |
| 3251 | if (res == 0) { |
| 3252 | res = update_slot(type, name); |
| 3253 | assert(_PyType_CheckConsistency(type)); |
| 3254 | } |
| 3255 | Py_DECREF(name); |
Victor Stinner | bda5a2b | 2017-01-25 23:33:27 +0100 | [diff] [blame] | 3256 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
Benjamin Peterson | 64acccf | 2012-04-27 15:07:36 -0400 | [diff] [blame] | 3259 | extern void |
| 3260 | _PyDictKeys_DecRef(PyDictKeysObject *keys); |
| 3261 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3262 | static void |
| 3263 | type_dealloc(PyTypeObject *type) |
| 3264 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3265 | PyHeapTypeObject *et; |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 3266 | PyObject *tp, *val, *tb; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3267 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3268 | /* Assert this is a heap-allocated type object */ |
| 3269 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
| 3270 | _PyObject_GC_UNTRACK(type); |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 3271 | PyErr_Fetch(&tp, &val, &tb); |
| 3272 | remove_all_subclasses(type, type->tp_bases); |
| 3273 | PyErr_Restore(tp, val, tb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3274 | PyObject_ClearWeakRefs((PyObject *)type); |
| 3275 | et = (PyHeapTypeObject *)type; |
| 3276 | Py_XDECREF(type->tp_base); |
| 3277 | Py_XDECREF(type->tp_dict); |
| 3278 | Py_XDECREF(type->tp_bases); |
| 3279 | Py_XDECREF(type->tp_mro); |
| 3280 | Py_XDECREF(type->tp_cache); |
| 3281 | Py_XDECREF(type->tp_subclasses); |
| 3282 | /* A type's tp_doc is heap allocated, unlike the tp_doc slots |
| 3283 | * of most other objects. It's okay to cast it to char *. |
| 3284 | */ |
| 3285 | PyObject_Free((char *)type->tp_doc); |
| 3286 | Py_XDECREF(et->ht_name); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 3287 | Py_XDECREF(et->ht_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3288 | Py_XDECREF(et->ht_slots); |
Benjamin Peterson | 64acccf | 2012-04-27 15:07:36 -0400 | [diff] [blame] | 3289 | if (et->ht_cached_keys) |
| 3290 | _PyDictKeys_DecRef(et->ht_cached_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3291 | Py_TYPE(type)->tp_free((PyObject *)type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 3294 | /*[clinic input] |
| 3295 | type.__subclasses__ |
| 3296 | |
| 3297 | Return a list of immediate subclasses. |
| 3298 | [clinic start generated code]*/ |
| 3299 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3300 | static PyObject * |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 3301 | type___subclasses___impl(PyTypeObject *self) |
| 3302 | /*[clinic end generated code: output=eb5eb54485942819 input=5af66132436f9a7b]*/ |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3303 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3304 | PyObject *list, *raw, *ref; |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 3305 | Py_ssize_t i; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3306 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3307 | list = PyList_New(0); |
| 3308 | if (list == NULL) |
| 3309 | return NULL; |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 3310 | raw = self->tp_subclasses; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3311 | if (raw == NULL) |
| 3312 | return list; |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 3313 | assert(PyDict_CheckExact(raw)); |
| 3314 | i = 0; |
| 3315 | while (PyDict_Next(raw, &i, NULL, &ref)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3316 | assert(PyWeakref_CheckRef(ref)); |
| 3317 | ref = PyWeakref_GET_OBJECT(ref); |
| 3318 | if (ref != Py_None) { |
| 3319 | if (PyList_Append(list, ref) < 0) { |
| 3320 | Py_DECREF(list); |
| 3321 | return NULL; |
| 3322 | } |
| 3323 | } |
| 3324 | } |
| 3325 | return list; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
Guido van Rossum | 4737482 | 2007-08-02 16:48:17 +0000 | [diff] [blame] | 3328 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 3329 | type_prepare(PyObject *self, PyObject *const *args, Py_ssize_t nargs, |
Victor Stinner | d526cfe | 2017-01-16 23:52:00 +0100 | [diff] [blame] | 3330 | PyObject *kwnames) |
Guido van Rossum | 4737482 | 2007-08-02 16:48:17 +0000 | [diff] [blame] | 3331 | { |
Eric Snow | 4f29e75 | 2016-09-08 15:11:11 -0700 | [diff] [blame] | 3332 | return PyDict_New(); |
Guido van Rossum | 4737482 | 2007-08-02 16:48:17 +0000 | [diff] [blame] | 3333 | } |
| 3334 | |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3335 | /* |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3336 | Merge the __dict__ of aclass into dict, and recursively also all |
| 3337 | the __dict__s of aclass's base classes. The order of merging isn't |
| 3338 | defined, as it's expected that only the final set of dict keys is |
| 3339 | interesting. |
| 3340 | Return 0 on success, -1 on error. |
| 3341 | */ |
| 3342 | |
| 3343 | static int |
| 3344 | merge_class_dict(PyObject *dict, PyObject *aclass) |
| 3345 | { |
| 3346 | PyObject *classdict; |
| 3347 | PyObject *bases; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3348 | _Py_IDENTIFIER(__bases__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3349 | |
| 3350 | assert(PyDict_Check(dict)); |
| 3351 | assert(aclass); |
| 3352 | |
| 3353 | /* Merge in the type's dict (if any). */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3354 | classdict = _PyObject_GetAttrId(aclass, &PyId___dict__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3355 | if (classdict == NULL) |
| 3356 | PyErr_Clear(); |
| 3357 | else { |
| 3358 | int status = PyDict_Update(dict, classdict); |
| 3359 | Py_DECREF(classdict); |
| 3360 | if (status < 0) |
| 3361 | return -1; |
| 3362 | } |
| 3363 | |
| 3364 | /* Recursively merge in the base types' (if any) dicts. */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3365 | bases = _PyObject_GetAttrId(aclass, &PyId___bases__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3366 | if (bases == NULL) |
| 3367 | PyErr_Clear(); |
| 3368 | else { |
| 3369 | /* We have no guarantee that bases is a real tuple */ |
| 3370 | Py_ssize_t i, n; |
| 3371 | n = PySequence_Size(bases); /* This better be right */ |
| 3372 | if (n < 0) |
| 3373 | PyErr_Clear(); |
| 3374 | else { |
| 3375 | for (i = 0; i < n; i++) { |
| 3376 | int status; |
| 3377 | PyObject *base = PySequence_GetItem(bases, i); |
| 3378 | if (base == NULL) { |
| 3379 | Py_DECREF(bases); |
| 3380 | return -1; |
| 3381 | } |
| 3382 | status = merge_class_dict(dict, base); |
| 3383 | Py_DECREF(base); |
| 3384 | if (status < 0) { |
| 3385 | Py_DECREF(bases); |
| 3386 | return -1; |
| 3387 | } |
| 3388 | } |
| 3389 | } |
| 3390 | Py_DECREF(bases); |
| 3391 | } |
| 3392 | return 0; |
| 3393 | } |
| 3394 | |
| 3395 | /* __dir__ for type objects: returns __dict__ and __bases__. |
| 3396 | We deliberately don't suck up its __class__, as methods belonging to the |
| 3397 | metaclass would probably be more confusing than helpful. |
| 3398 | */ |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 3399 | /*[clinic input] |
| 3400 | type.__dir__ |
| 3401 | |
| 3402 | Specialized __dir__ implementation for types. |
| 3403 | [clinic start generated code]*/ |
| 3404 | |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3405 | static PyObject * |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 3406 | type___dir___impl(PyTypeObject *self) |
| 3407 | /*[clinic end generated code: output=69d02fe92c0f15fa input=7733befbec645968]*/ |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3408 | { |
| 3409 | PyObject *result = NULL; |
| 3410 | PyObject *dict = PyDict_New(); |
| 3411 | |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 3412 | if (dict != NULL && merge_class_dict(dict, (PyObject *)self) == 0) |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 3413 | result = PyDict_Keys(dict); |
| 3414 | |
| 3415 | Py_XDECREF(dict); |
| 3416 | return result; |
| 3417 | } |
| 3418 | |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 3419 | /*[clinic input] |
| 3420 | type.__sizeof__ |
| 3421 | |
| 3422 | Return memory consumption of the type object. |
| 3423 | [clinic start generated code]*/ |
| 3424 | |
| 3425 | static PyObject * |
| 3426 | type___sizeof___impl(PyTypeObject *self) |
| 3427 | /*[clinic end generated code: output=766f4f16cd3b1854 input=99398f24b9cf45d6]*/ |
Martin v. Loewis | 4f2f3b6 | 2012-04-24 19:13:57 +0200 | [diff] [blame] | 3428 | { |
| 3429 | Py_ssize_t size; |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 3430 | if (self->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 3431 | PyHeapTypeObject* et = (PyHeapTypeObject*)self; |
Martin v. Loewis | 4f2f3b6 | 2012-04-24 19:13:57 +0200 | [diff] [blame] | 3432 | size = sizeof(PyHeapTypeObject); |
| 3433 | if (et->ht_cached_keys) |
| 3434 | size += _PyDict_KeysSize(et->ht_cached_keys); |
| 3435 | } |
| 3436 | else |
| 3437 | size = sizeof(PyTypeObject); |
| 3438 | return PyLong_FromSsize_t(size); |
| 3439 | } |
| 3440 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3441 | static PyMethodDef type_methods[] = { |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 3442 | TYPE_MRO_METHODDEF |
| 3443 | TYPE___SUBCLASSES___METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3444 | {"__prepare__", (PyCFunction)type_prepare, |
Serhiy Storchaka | 6969eaf | 2017-07-03 21:20:15 +0300 | [diff] [blame] | 3445 | METH_FASTCALL | METH_KEYWORDS | METH_CLASS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3446 | PyDoc_STR("__prepare__() -> dict\n" |
| 3447 | "used to create the namespace for the class statement")}, |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 3448 | TYPE___INSTANCECHECK___METHODDEF |
| 3449 | TYPE___SUBCLASSCHECK___METHODDEF |
| 3450 | TYPE___DIR___METHODDEF |
| 3451 | TYPE___SIZEOF___METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3452 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3453 | }; |
| 3454 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3455 | PyDoc_STRVAR(type_doc, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 3456 | /* this text signature cannot be accurate yet. will fix. --larry */ |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 3457 | "type(object_or_name, bases, dict)\n" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3458 | "type(object) -> the object's type\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3459 | "type(name, bases, dict) -> a new type"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3460 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3461 | static int |
| 3462 | type_traverse(PyTypeObject *type, visitproc visit, void *arg) |
| 3463 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3464 | /* Because of type_is_gc(), the collector only calls this |
| 3465 | for heaptypes. */ |
Antoine Pitrou | 1351ca6 | 2012-06-24 00:30:12 +0200 | [diff] [blame] | 3466 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 3467 | char msg[200]; |
| 3468 | sprintf(msg, "type_traverse() called for non-heap type '%.100s'", |
| 3469 | type->tp_name); |
| 3470 | Py_FatalError(msg); |
| 3471 | } |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3473 | Py_VISIT(type->tp_dict); |
| 3474 | Py_VISIT(type->tp_cache); |
| 3475 | Py_VISIT(type->tp_mro); |
| 3476 | Py_VISIT(type->tp_bases); |
| 3477 | Py_VISIT(type->tp_base); |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 3478 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3479 | /* There's no need to visit type->tp_subclasses or |
| 3480 | ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved |
| 3481 | in cycles; tp_subclasses is a list of weak references, |
| 3482 | and slots is a tuple of strings. */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3483 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3484 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3485 | } |
| 3486 | |
| 3487 | static int |
| 3488 | type_clear(PyTypeObject *type) |
| 3489 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3490 | PyDictKeysObject *cached_keys; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3491 | /* Because of type_is_gc(), the collector only calls this |
| 3492 | for heaptypes. */ |
| 3493 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3494 | |
Antoine Pitrou | 2e87208 | 2011-12-15 14:15:31 +0100 | [diff] [blame] | 3495 | /* We need to invalidate the method cache carefully before clearing |
| 3496 | the dict, so that other objects caught in a reference cycle |
| 3497 | don't start calling destroyed methods. |
| 3498 | |
| 3499 | Otherwise, the only field we need to clear is tp_mro, which is |
| 3500 | part of a hard cycle (its first element is the class itself) that |
| 3501 | 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] | 3502 | tp_clear handler). None of the other fields need to be |
| 3503 | cleared, and here's why: |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3505 | tp_cache: |
| 3506 | Not used; if it were, it would be a dict. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 3507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3508 | tp_bases, tp_base: |
| 3509 | If these are involved in a cycle, there must be at least |
| 3510 | one other, mutable object in the cycle, e.g. a base |
| 3511 | class's dict; the cycle will be broken that way. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 3512 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3513 | tp_subclasses: |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 3514 | A dict of weak references can't be part of a cycle; and |
| 3515 | dicts have their own tp_clear. |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 3516 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3517 | slots (in PyHeapTypeObject): |
| 3518 | A tuple of strings can't be part of a cycle. |
| 3519 | */ |
Guido van Rossum | a386209 | 2002-06-10 15:24:42 +0000 | [diff] [blame] | 3520 | |
Antoine Pitrou | 2e87208 | 2011-12-15 14:15:31 +0100 | [diff] [blame] | 3521 | PyType_Modified(type); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3522 | cached_keys = ((PyHeapTypeObject *)type)->ht_cached_keys; |
| 3523 | if (cached_keys != NULL) { |
| 3524 | ((PyHeapTypeObject *)type)->ht_cached_keys = NULL; |
| 3525 | _PyDictKeys_DecRef(cached_keys); |
| 3526 | } |
Antoine Pitrou | 2e87208 | 2011-12-15 14:15:31 +0100 | [diff] [blame] | 3527 | if (type->tp_dict) |
| 3528 | PyDict_Clear(type->tp_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3529 | Py_CLEAR(type->tp_mro); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3530 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3531 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3532 | } |
| 3533 | |
| 3534 | static int |
| 3535 | type_is_gc(PyTypeObject *type) |
| 3536 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3537 | return type->tp_flags & Py_TPFLAGS_HEAPTYPE; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3538 | } |
| 3539 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3540 | PyTypeObject PyType_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3541 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3542 | "type", /* tp_name */ |
| 3543 | sizeof(PyHeapTypeObject), /* tp_basicsize */ |
| 3544 | sizeof(PyMemberDef), /* tp_itemsize */ |
| 3545 | (destructor)type_dealloc, /* tp_dealloc */ |
| 3546 | 0, /* tp_print */ |
| 3547 | 0, /* tp_getattr */ |
| 3548 | 0, /* tp_setattr */ |
| 3549 | 0, /* tp_reserved */ |
| 3550 | (reprfunc)type_repr, /* tp_repr */ |
| 3551 | 0, /* tp_as_number */ |
| 3552 | 0, /* tp_as_sequence */ |
| 3553 | 0, /* tp_as_mapping */ |
| 3554 | 0, /* tp_hash */ |
| 3555 | (ternaryfunc)type_call, /* tp_call */ |
| 3556 | 0, /* tp_str */ |
| 3557 | (getattrofunc)type_getattro, /* tp_getattro */ |
| 3558 | (setattrofunc)type_setattro, /* tp_setattro */ |
| 3559 | 0, /* tp_as_buffer */ |
| 3560 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 3561 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */ |
| 3562 | type_doc, /* tp_doc */ |
| 3563 | (traverseproc)type_traverse, /* tp_traverse */ |
| 3564 | (inquiry)type_clear, /* tp_clear */ |
| 3565 | 0, /* tp_richcompare */ |
| 3566 | offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ |
| 3567 | 0, /* tp_iter */ |
| 3568 | 0, /* tp_iternext */ |
| 3569 | type_methods, /* tp_methods */ |
| 3570 | type_members, /* tp_members */ |
| 3571 | type_getsets, /* tp_getset */ |
| 3572 | 0, /* tp_base */ |
| 3573 | 0, /* tp_dict */ |
| 3574 | 0, /* tp_descr_get */ |
| 3575 | 0, /* tp_descr_set */ |
| 3576 | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ |
| 3577 | type_init, /* tp_init */ |
| 3578 | 0, /* tp_alloc */ |
| 3579 | type_new, /* tp_new */ |
| 3580 | PyObject_GC_Del, /* tp_free */ |
| 3581 | (inquiry)type_is_gc, /* tp_is_gc */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3582 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3583 | |
| 3584 | |
| 3585 | /* The base type of all types (eventually)... except itself. */ |
| 3586 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3587 | /* You may wonder why object.__new__() only complains about arguments |
| 3588 | when object.__init__() is not overridden, and vice versa. |
| 3589 | |
| 3590 | Consider the use cases: |
| 3591 | |
| 3592 | 1. When neither is overridden, we want to hear complaints about |
| 3593 | excess (i.e., any) arguments, since their presence could |
| 3594 | indicate there's a bug. |
| 3595 | |
| 3596 | 2. When defining an Immutable type, we are likely to override only |
| 3597 | __new__(), since __init__() is called too late to initialize an |
| 3598 | Immutable object. Since __new__() defines the signature for the |
| 3599 | type, it would be a pain to have to override __init__() just to |
| 3600 | stop it from complaining about excess arguments. |
| 3601 | |
| 3602 | 3. When defining a Mutable type, we are likely to override only |
| 3603 | __init__(). So here the converse reasoning applies: we don't |
| 3604 | want to have to override __new__() just to stop it from |
| 3605 | complaining. |
| 3606 | |
| 3607 | 4. When __init__() is overridden, and the subclass __init__() calls |
| 3608 | object.__init__(), the latter should complain about excess |
| 3609 | arguments; ditto for __new__(). |
| 3610 | |
| 3611 | Use cases 2 and 3 make it unattractive to unconditionally check for |
| 3612 | excess arguments. The best solution that addresses all four use |
| 3613 | cases is as follows: __init__() complains about excess arguments |
| 3614 | unless __new__() is overridden and __init__() is not overridden |
| 3615 | (IOW, if __init__() is overridden or __new__() is not overridden); |
| 3616 | symmetrically, __new__() complains about excess arguments unless |
| 3617 | __init__() is overridden and __new__() is not overridden |
| 3618 | (IOW, if __new__() is overridden or __init__() is not overridden). |
| 3619 | |
| 3620 | However, for backwards compatibility, this breaks too much code. |
| 3621 | Therefore, in 2.6, we'll *warn* about excess arguments when both |
| 3622 | methods are overridden; for all other cases we'll use the above |
| 3623 | rules. |
| 3624 | |
| 3625 | */ |
| 3626 | |
| 3627 | /* Forward */ |
| 3628 | static PyObject * |
| 3629 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 3630 | |
| 3631 | static int |
| 3632 | excess_args(PyObject *args, PyObject *kwds) |
| 3633 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3634 | return PyTuple_GET_SIZE(args) || |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3635 | (kwds && PyDict_Check(kwds) && PyDict_GET_SIZE(kwds)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3636 | } |
| 3637 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3638 | static int |
| 3639 | object_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 3640 | { |
Benjamin Peterson | 96384b9 | 2012-03-17 00:05:44 -0500 | [diff] [blame] | 3641 | PyTypeObject *type = Py_TYPE(self); |
Serhiy Storchaka | a6c0c06 | 2017-09-20 06:44:32 +0300 | [diff] [blame] | 3642 | if (excess_args(args, kwds)) { |
| 3643 | if (type->tp_init != object_init) { |
Sanyam Khurana | 780acc8 | 2017-12-10 05:44:22 +0530 | [diff] [blame] | 3644 | PyErr_SetString(PyExc_TypeError, "object.__init__() takes no arguments"); |
Serhiy Storchaka | a6c0c06 | 2017-09-20 06:44:32 +0300 | [diff] [blame] | 3645 | return -1; |
| 3646 | } |
| 3647 | if (type->tp_new == object_new) { |
Sanyam Khurana | 780acc8 | 2017-12-10 05:44:22 +0530 | [diff] [blame] | 3648 | PyErr_Format(PyExc_TypeError, "%.200s().__init__() takes no arguments", |
Serhiy Storchaka | a6c0c06 | 2017-09-20 06:44:32 +0300 | [diff] [blame] | 3649 | type->tp_name); |
| 3650 | return -1; |
| 3651 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3652 | } |
Serhiy Storchaka | a6c0c06 | 2017-09-20 06:44:32 +0300 | [diff] [blame] | 3653 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3654 | } |
| 3655 | |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 3656 | static PyObject * |
| 3657 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3658 | { |
Serhiy Storchaka | a6c0c06 | 2017-09-20 06:44:32 +0300 | [diff] [blame] | 3659 | if (excess_args(args, kwds)) { |
| 3660 | if (type->tp_new != object_new) { |
Sanyam Khurana | 780acc8 | 2017-12-10 05:44:22 +0530 | [diff] [blame] | 3661 | PyErr_SetString(PyExc_TypeError, "object.__new__() takes no arguments"); |
Serhiy Storchaka | a6c0c06 | 2017-09-20 06:44:32 +0300 | [diff] [blame] | 3662 | return NULL; |
| 3663 | } |
| 3664 | if (type->tp_init == object_init) { |
| 3665 | PyErr_Format(PyExc_TypeError, "%.200s() takes no arguments", |
| 3666 | type->tp_name); |
| 3667 | return NULL; |
| 3668 | } |
Benjamin Peterson | 96384b9 | 2012-03-17 00:05:44 -0500 | [diff] [blame] | 3669 | } |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3670 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3671 | if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3672 | PyObject *abstract_methods = NULL; |
| 3673 | PyObject *builtins; |
| 3674 | PyObject *sorted; |
| 3675 | PyObject *sorted_methods = NULL; |
| 3676 | PyObject *joined = NULL; |
Benjamin Peterson | 9a03ecf | 2012-03-16 20:15:54 -0500 | [diff] [blame] | 3677 | PyObject *comma; |
| 3678 | _Py_static_string(comma_id, ", "); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3679 | _Py_IDENTIFIER(sorted); |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3681 | /* Compute ", ".join(sorted(type.__abstractmethods__)) |
| 3682 | into joined. */ |
| 3683 | abstract_methods = type_abstractmethods(type, NULL); |
| 3684 | if (abstract_methods == NULL) |
| 3685 | goto error; |
| 3686 | builtins = PyEval_GetBuiltins(); |
| 3687 | if (builtins == NULL) |
| 3688 | goto error; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3689 | sorted = _PyDict_GetItemId(builtins, &PyId_sorted); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3690 | if (sorted == NULL) |
| 3691 | goto error; |
Victor Stinner | de4ae3d | 2016-12-04 22:59:09 +0100 | [diff] [blame] | 3692 | sorted_methods = PyObject_CallFunctionObjArgs(sorted, |
| 3693 | abstract_methods, |
| 3694 | NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3695 | if (sorted_methods == NULL) |
| 3696 | goto error; |
Benjamin Peterson | 9a03ecf | 2012-03-16 20:15:54 -0500 | [diff] [blame] | 3697 | comma = _PyUnicode_FromId(&comma_id); |
| 3698 | if (comma == NULL) |
| 3699 | goto error; |
| 3700 | joined = PyUnicode_Join(comma, sorted_methods); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3701 | if (joined == NULL) |
| 3702 | goto error; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 3703 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3704 | PyErr_Format(PyExc_TypeError, |
| 3705 | "Can't instantiate abstract class %s " |
| 3706 | "with abstract methods %U", |
| 3707 | type->tp_name, |
| 3708 | joined); |
| 3709 | error: |
| 3710 | Py_XDECREF(joined); |
| 3711 | Py_XDECREF(sorted_methods); |
| 3712 | Py_XDECREF(abstract_methods); |
| 3713 | return NULL; |
| 3714 | } |
| 3715 | return type->tp_alloc(type, 0); |
Guido van Rossum | 298e421 | 2003-02-13 16:30:16 +0000 | [diff] [blame] | 3716 | } |
| 3717 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3718 | static void |
| 3719 | object_dealloc(PyObject *self) |
| 3720 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3721 | Py_TYPE(self)->tp_free(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3722 | } |
| 3723 | |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 3724 | static PyObject * |
| 3725 | object_repr(PyObject *self) |
| 3726 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3727 | PyTypeObject *type; |
| 3728 | PyObject *mod, *name, *rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 3729 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3730 | type = Py_TYPE(self); |
| 3731 | mod = type_module(type, NULL); |
| 3732 | if (mod == NULL) |
| 3733 | PyErr_Clear(); |
| 3734 | else if (!PyUnicode_Check(mod)) { |
| 3735 | Py_DECREF(mod); |
| 3736 | mod = NULL; |
| 3737 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 3738 | name = type_qualname(type, NULL); |
Christian Heimes | e81dc29 | 2012-09-10 16:57:36 +0200 | [diff] [blame] | 3739 | if (name == NULL) { |
| 3740 | Py_XDECREF(mod); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3741 | return NULL; |
Christian Heimes | e81dc29 | 2012-09-10 16:57:36 +0200 | [diff] [blame] | 3742 | } |
Serhiy Storchaka | f5894dd | 2016-11-16 15:40:39 +0200 | [diff] [blame] | 3743 | if (mod != NULL && !_PyUnicode_EqualToASCIIId(mod, &PyId_builtins)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3744 | rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self); |
| 3745 | else |
| 3746 | rtn = PyUnicode_FromFormat("<%s object at %p>", |
| 3747 | type->tp_name, self); |
| 3748 | Py_XDECREF(mod); |
| 3749 | Py_DECREF(name); |
| 3750 | return rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 3751 | } |
| 3752 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3753 | static PyObject * |
| 3754 | object_str(PyObject *self) |
| 3755 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3756 | unaryfunc f; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3757 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3758 | f = Py_TYPE(self)->tp_repr; |
Benjamin Peterson | 7b16687 | 2012-04-24 11:06:25 -0400 | [diff] [blame] | 3759 | if (f == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3760 | f = object_repr; |
| 3761 | return f(self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3762 | } |
| 3763 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3764 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3765 | object_richcompare(PyObject *self, PyObject *other, int op) |
| 3766 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3767 | PyObject *res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3768 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3769 | switch (op) { |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3771 | case Py_EQ: |
| 3772 | /* Return NotImplemented instead of False, so if two |
| 3773 | objects are compared, both get a chance at the |
| 3774 | comparison. See issue #1393. */ |
| 3775 | res = (self == other) ? Py_True : Py_NotImplemented; |
| 3776 | Py_INCREF(res); |
| 3777 | break; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3778 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3779 | case Py_NE: |
Serhiy Storchaka | f4b7a02 | 2015-01-26 09:57:07 +0200 | [diff] [blame] | 3780 | /* By default, __ne__() delegates to __eq__() and inverts the result, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3781 | unless the latter returns NotImplemented. */ |
Serhiy Storchaka | f4b7a02 | 2015-01-26 09:57:07 +0200 | [diff] [blame] | 3782 | if (self->ob_type->tp_richcompare == NULL) { |
| 3783 | res = Py_NotImplemented; |
| 3784 | Py_INCREF(res); |
| 3785 | break; |
| 3786 | } |
| 3787 | res = (*self->ob_type->tp_richcompare)(self, other, Py_EQ); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3788 | if (res != NULL && res != Py_NotImplemented) { |
| 3789 | int ok = PyObject_IsTrue(res); |
| 3790 | Py_DECREF(res); |
| 3791 | if (ok < 0) |
| 3792 | res = NULL; |
| 3793 | else { |
| 3794 | if (ok) |
| 3795 | res = Py_False; |
| 3796 | else |
| 3797 | res = Py_True; |
| 3798 | Py_INCREF(res); |
| 3799 | } |
| 3800 | } |
| 3801 | break; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3803 | default: |
| 3804 | res = Py_NotImplemented; |
| 3805 | Py_INCREF(res); |
| 3806 | break; |
| 3807 | } |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3808 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3809 | return res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3810 | } |
| 3811 | |
| 3812 | static PyObject * |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3813 | object_get_class(PyObject *self, void *closure) |
| 3814 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3815 | Py_INCREF(Py_TYPE(self)); |
| 3816 | return (PyObject *)(Py_TYPE(self)); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3817 | } |
| 3818 | |
| 3819 | static int |
Benjamin Peterson | 9d4cbcc | 2015-01-30 13:33:42 -0500 | [diff] [blame] | 3820 | compatible_with_tp_base(PyTypeObject *child) |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3821 | { |
Benjamin Peterson | 9d4cbcc | 2015-01-30 13:33:42 -0500 | [diff] [blame] | 3822 | PyTypeObject *parent = child->tp_base; |
| 3823 | return (parent != NULL && |
| 3824 | child->tp_basicsize == parent->tp_basicsize && |
| 3825 | child->tp_itemsize == parent->tp_itemsize && |
| 3826 | child->tp_dictoffset == parent->tp_dictoffset && |
| 3827 | child->tp_weaklistoffset == parent->tp_weaklistoffset && |
| 3828 | ((child->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 3829 | (parent->tp_flags & Py_TPFLAGS_HAVE_GC)) && |
| 3830 | (child->tp_dealloc == subtype_dealloc || |
| 3831 | child->tp_dealloc == parent->tp_dealloc)); |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3832 | } |
| 3833 | |
| 3834 | static int |
| 3835 | same_slots_added(PyTypeObject *a, PyTypeObject *b) |
| 3836 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3837 | PyTypeObject *base = a->tp_base; |
| 3838 | Py_ssize_t size; |
| 3839 | PyObject *slots_a, *slots_b; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3840 | |
Benjamin Peterson | 67641d2 | 2011-01-17 19:24:34 +0000 | [diff] [blame] | 3841 | assert(base == b->tp_base); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3842 | size = base->tp_basicsize; |
| 3843 | if (a->tp_dictoffset == size && b->tp_dictoffset == size) |
| 3844 | size += sizeof(PyObject *); |
| 3845 | if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size) |
| 3846 | size += sizeof(PyObject *); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3847 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3848 | /* Check slots compliance */ |
Benjamin Peterson | 9d4cbcc | 2015-01-30 13:33:42 -0500 | [diff] [blame] | 3849 | if (!(a->tp_flags & Py_TPFLAGS_HEAPTYPE) || |
| 3850 | !(b->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 3851 | return 0; |
| 3852 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3853 | slots_a = ((PyHeapTypeObject *)a)->ht_slots; |
| 3854 | slots_b = ((PyHeapTypeObject *)b)->ht_slots; |
| 3855 | if (slots_a && slots_b) { |
| 3856 | if (PyObject_RichCompareBool(slots_a, slots_b, Py_EQ) != 1) |
| 3857 | return 0; |
| 3858 | size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a); |
| 3859 | } |
| 3860 | return size == a->tp_basicsize && size == b->tp_basicsize; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
| 3863 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 3864 | compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, const char* attr) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3865 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3866 | PyTypeObject *newbase, *oldbase; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3867 | |
Benjamin Peterson | 9d4cbcc | 2015-01-30 13:33:42 -0500 | [diff] [blame] | 3868 | if (newto->tp_free != oldto->tp_free) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3869 | PyErr_Format(PyExc_TypeError, |
| 3870 | "%s assignment: " |
| 3871 | "'%s' deallocator differs from '%s'", |
| 3872 | attr, |
| 3873 | newto->tp_name, |
| 3874 | oldto->tp_name); |
| 3875 | return 0; |
| 3876 | } |
Benjamin Peterson | 9d4cbcc | 2015-01-30 13:33:42 -0500 | [diff] [blame] | 3877 | /* |
| 3878 | It's tricky to tell if two arbitrary types are sufficiently compatible as |
| 3879 | to be interchangeable; e.g., even if they have the same tp_basicsize, they |
| 3880 | might have totally different struct fields. It's much easier to tell if a |
| 3881 | type and its supertype are compatible; e.g., if they have the same |
| 3882 | tp_basicsize, then that means they have identical fields. So to check |
| 3883 | whether two arbitrary types are compatible, we first find the highest |
| 3884 | supertype that each is compatible with, and then if those supertypes are |
| 3885 | compatible then the original types must also be compatible. |
| 3886 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3887 | newbase = newto; |
| 3888 | oldbase = oldto; |
Benjamin Peterson | 9d4cbcc | 2015-01-30 13:33:42 -0500 | [diff] [blame] | 3889 | while (compatible_with_tp_base(newbase)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3890 | newbase = newbase->tp_base; |
Benjamin Peterson | 9d4cbcc | 2015-01-30 13:33:42 -0500 | [diff] [blame] | 3891 | while (compatible_with_tp_base(oldbase)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3892 | oldbase = oldbase->tp_base; |
| 3893 | if (newbase != oldbase && |
| 3894 | (newbase->tp_base != oldbase->tp_base || |
| 3895 | !same_slots_added(newbase, oldbase))) { |
| 3896 | PyErr_Format(PyExc_TypeError, |
| 3897 | "%s assignment: " |
| 3898 | "'%s' object layout differs from '%s'", |
| 3899 | attr, |
| 3900 | newto->tp_name, |
| 3901 | oldto->tp_name); |
| 3902 | return 0; |
| 3903 | } |
Tim Peters | ea7f75d | 2002-12-07 21:39:16 +0000 | [diff] [blame] | 3904 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3905 | return 1; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3906 | } |
| 3907 | |
| 3908 | static int |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3909 | object_set_class(PyObject *self, PyObject *value, void *closure) |
| 3910 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3911 | PyTypeObject *oldto = Py_TYPE(self); |
| 3912 | PyTypeObject *newto; |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3914 | if (value == NULL) { |
| 3915 | PyErr_SetString(PyExc_TypeError, |
| 3916 | "can't delete __class__ attribute"); |
| 3917 | return -1; |
| 3918 | } |
| 3919 | if (!PyType_Check(value)) { |
| 3920 | PyErr_Format(PyExc_TypeError, |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 3921 | "__class__ must be set to a class, not '%s' object", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3922 | Py_TYPE(value)->tp_name); |
| 3923 | return -1; |
| 3924 | } |
| 3925 | newto = (PyTypeObject *)value; |
Guido van Rossum | 7d293ee | 2015-09-04 20:54:07 -0700 | [diff] [blame] | 3926 | /* In versions of CPython prior to 3.5, the code in |
| 3927 | compatible_for_assignment was not set up to correctly check for memory |
| 3928 | layout / slot / etc. compatibility for non-HEAPTYPE classes, so we just |
| 3929 | disallowed __class__ assignment in any case that wasn't HEAPTYPE -> |
| 3930 | HEAPTYPE. |
| 3931 | |
| 3932 | During the 3.5 development cycle, we fixed the code in |
| 3933 | compatible_for_assignment to correctly check compatibility between |
| 3934 | arbitrary types, and started allowing __class__ assignment in all cases |
| 3935 | where the old and new types did in fact have compatible slots and |
| 3936 | memory layout (regardless of whether they were implemented as HEAPTYPEs |
| 3937 | or not). |
| 3938 | |
| 3939 | Just before 3.5 was released, though, we discovered that this led to |
| 3940 | problems with immutable types like int, where the interpreter assumes |
| 3941 | they are immutable and interns some values. Formerly this wasn't a |
| 3942 | problem, because they really were immutable -- in particular, all the |
| 3943 | types where the interpreter applied this interning trick happened to |
| 3944 | also be statically allocated, so the old HEAPTYPE rules were |
| 3945 | "accidentally" stopping them from allowing __class__ assignment. But |
| 3946 | with the changes to __class__ assignment, we started allowing code like |
| 3947 | |
| 3948 | class MyInt(int): |
| 3949 | ... |
| 3950 | # Modifies the type of *all* instances of 1 in the whole program, |
| 3951 | # including future instances (!), because the 1 object is interned. |
| 3952 | (1).__class__ = MyInt |
| 3953 | |
| 3954 | (see https://bugs.python.org/issue24912). |
| 3955 | |
| 3956 | In theory the proper fix would be to identify which classes rely on |
| 3957 | this invariant and somehow disallow __class__ assignment only for them, |
| 3958 | perhaps via some mechanism like a new Py_TPFLAGS_IMMUTABLE flag (a |
| 3959 | "blacklisting" approach). But in practice, since this problem wasn't |
| 3960 | noticed late in the 3.5 RC cycle, we're taking the conservative |
| 3961 | approach and reinstating the same HEAPTYPE->HEAPTYPE check that we used |
| 3962 | to have, plus a "whitelist". For now, the whitelist consists only of |
| 3963 | ModuleType subtypes, since those are the cases that motivated the patch |
| 3964 | in the first place -- see https://bugs.python.org/issue22986 -- and |
| 3965 | since module objects are mutable we can be sure that they are |
| 3966 | definitely not being interned. So now we allow HEAPTYPE->HEAPTYPE *or* |
| 3967 | ModuleType subtype -> ModuleType subtype. |
| 3968 | |
| 3969 | So far as we know, all the code beyond the following 'if' statement |
| 3970 | will correctly handle non-HEAPTYPE classes, and the HEAPTYPE check is |
| 3971 | needed only to protect that subset of non-HEAPTYPE classes for which |
| 3972 | the interpreter has baked in the assumption that all instances are |
| 3973 | truly immutable. |
| 3974 | */ |
| 3975 | if (!(PyType_IsSubtype(newto, &PyModule_Type) && |
| 3976 | PyType_IsSubtype(oldto, &PyModule_Type)) && |
| 3977 | (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) || |
| 3978 | !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))) { |
| 3979 | PyErr_Format(PyExc_TypeError, |
| 3980 | "__class__ assignment only supported for heap types " |
| 3981 | "or ModuleType subclasses"); |
| 3982 | return -1; |
| 3983 | } |
| 3984 | |
Christian Heimes | de4d183 | 2013-07-20 14:19:46 +0200 | [diff] [blame] | 3985 | if (compatible_for_assignment(oldto, newto, "__class__")) { |
Benjamin Peterson | 9d4cbcc | 2015-01-30 13:33:42 -0500 | [diff] [blame] | 3986 | if (newto->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 3987 | Py_INCREF(newto); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3988 | Py_TYPE(self) = newto; |
Benjamin Peterson | 9d4cbcc | 2015-01-30 13:33:42 -0500 | [diff] [blame] | 3989 | if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 3990 | Py_DECREF(oldto); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3991 | return 0; |
| 3992 | } |
| 3993 | else { |
| 3994 | return -1; |
| 3995 | } |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 3996 | } |
| 3997 | |
| 3998 | static PyGetSetDef object_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3999 | {"__class__", object_get_class, object_set_class, |
| 4000 | PyDoc_STR("the object's class")}, |
| 4001 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4002 | }; |
| 4003 | |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 4004 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4005 | /* Stuff to implement __reduce_ex__ for pickle protocols >= 2. |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 4006 | We fall back to helpers in copyreg for: |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4007 | - pickle protocols < 2 |
| 4008 | - calculating the list of slot names (done only once per class) |
| 4009 | - the __newobj__ function (which is used as a token but never called) |
| 4010 | */ |
| 4011 | |
| 4012 | static PyObject * |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 4013 | import_copyreg(void) |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4014 | { |
Alexandre Vassalotti | 1a83070 | 2013-11-30 00:53:09 -0800 | [diff] [blame] | 4015 | PyObject *copyreg_str; |
| 4016 | PyObject *copyreg_module; |
Alexandre Vassalotti | 1a83070 | 2013-11-30 00:53:09 -0800 | [diff] [blame] | 4017 | _Py_IDENTIFIER(copyreg); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 4018 | |
Alexandre Vassalotti | 1a83070 | 2013-11-30 00:53:09 -0800 | [diff] [blame] | 4019 | copyreg_str = _PyUnicode_FromId(&PyId_copyreg); |
| 4020 | if (copyreg_str == NULL) { |
| 4021 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4022 | } |
Alexandre Vassalotti | 1a83070 | 2013-11-30 00:53:09 -0800 | [diff] [blame] | 4023 | /* Try to fetch cached copy of copyreg from sys.modules first in an |
| 4024 | attempt to avoid the import overhead. Previously this was implemented |
| 4025 | by storing a reference to the cached module in a static variable, but |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 4026 | this broke when multiple embedded interpreters were in use (see issue |
Alexandre Vassalotti | 1a83070 | 2013-11-30 00:53:09 -0800 | [diff] [blame] | 4027 | #17408 and #19088). */ |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 4028 | copyreg_module = PyImport_GetModule(copyreg_str); |
Alexandre Vassalotti | 1a83070 | 2013-11-30 00:53:09 -0800 | [diff] [blame] | 4029 | if (copyreg_module != NULL) { |
Alexandre Vassalotti | 1a83070 | 2013-11-30 00:53:09 -0800 | [diff] [blame] | 4030 | return copyreg_module; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 4031 | } |
Alexandre Vassalotti | 1a83070 | 2013-11-30 00:53:09 -0800 | [diff] [blame] | 4032 | if (PyErr_Occurred()) { |
| 4033 | return NULL; |
| 4034 | } |
| 4035 | return PyImport_Import(copyreg_str); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4036 | } |
| 4037 | |
Benjamin Peterson | 6cb526e | 2016-09-09 12:42:51 -0700 | [diff] [blame] | 4038 | static PyObject * |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4039 | _PyType_GetSlotNames(PyTypeObject *cls) |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4040 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4041 | PyObject *copyreg; |
| 4042 | PyObject *slotnames; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4043 | _Py_IDENTIFIER(__slotnames__); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4044 | _Py_IDENTIFIER(_slotnames); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4045 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4046 | assert(PyType_Check(cls)); |
| 4047 | |
| 4048 | /* Get the slot names from the cache in the class if possible. */ |
| 4049 | slotnames = _PyDict_GetItemIdWithError(cls->tp_dict, &PyId___slotnames__); |
| 4050 | if (slotnames != NULL) { |
| 4051 | if (slotnames != Py_None && !PyList_Check(slotnames)) { |
| 4052 | PyErr_Format(PyExc_TypeError, |
| 4053 | "%.200s.__slotnames__ should be a list or None, " |
| 4054 | "not %.200s", |
| 4055 | cls->tp_name, Py_TYPE(slotnames)->tp_name); |
| 4056 | return NULL; |
| 4057 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4058 | Py_INCREF(slotnames); |
| 4059 | return slotnames; |
| 4060 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4061 | else { |
| 4062 | if (PyErr_Occurred()) { |
| 4063 | return NULL; |
| 4064 | } |
| 4065 | /* The class does not have the slot names cached yet. */ |
| 4066 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4067 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4068 | copyreg = import_copyreg(); |
| 4069 | if (copyreg == NULL) |
| 4070 | return NULL; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4071 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4072 | /* Use _slotnames function from the copyreg module to find the slots |
| 4073 | by this class and its bases. This function will cache the result |
| 4074 | in __slotnames__. */ |
| 4075 | slotnames = _PyObject_CallMethodIdObjArgs(copyreg, &PyId__slotnames, |
| 4076 | cls, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4077 | Py_DECREF(copyreg); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4078 | if (slotnames == NULL) |
| 4079 | return NULL; |
| 4080 | |
| 4081 | if (slotnames != Py_None && !PyList_Check(slotnames)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4082 | PyErr_SetString(PyExc_TypeError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4083 | "copyreg._slotnames didn't return a list or None"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4084 | Py_DECREF(slotnames); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4085 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4086 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4087 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4088 | return slotnames; |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4089 | } |
| 4090 | |
Benjamin Peterson | 6cb526e | 2016-09-09 12:42:51 -0700 | [diff] [blame] | 4091 | static PyObject * |
Serhiy Storchaka | f81be8a | 2015-12-25 21:04:29 +0200 | [diff] [blame] | 4092 | _PyObject_GetState(PyObject *obj, int required) |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4093 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4094 | PyObject *state; |
| 4095 | PyObject *getstate; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4096 | _Py_IDENTIFIER(__getstate__); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4097 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4098 | if (_PyObject_LookupAttrId(obj, &PyId___getstate__, &getstate) < 0) { |
| 4099 | return NULL; |
| 4100 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4101 | if (getstate == NULL) { |
| 4102 | PyObject *slotnames; |
| 4103 | |
Serhiy Storchaka | f81be8a | 2015-12-25 21:04:29 +0200 | [diff] [blame] | 4104 | if (required && obj->ob_type->tp_itemsize) { |
| 4105 | PyErr_Format(PyExc_TypeError, |
| 4106 | "can't pickle %.200s objects", |
| 4107 | Py_TYPE(obj)->tp_name); |
| 4108 | return NULL; |
| 4109 | } |
| 4110 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4111 | { |
| 4112 | PyObject **dict; |
| 4113 | dict = _PyObject_GetDictPtr(obj); |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 4114 | /* It is possible that the object's dict is not initialized |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4115 | yet. In this case, we will return None for the state. |
| 4116 | We also return None if the dict is empty to make the behavior |
| 4117 | consistent regardless whether the dict was initialized or not. |
| 4118 | This make unit testing easier. */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 4119 | if (dict != NULL && *dict != NULL && PyDict_GET_SIZE(*dict)) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4120 | state = *dict; |
| 4121 | } |
| 4122 | else { |
| 4123 | state = Py_None; |
| 4124 | } |
| 4125 | Py_INCREF(state); |
| 4126 | } |
| 4127 | |
| 4128 | slotnames = _PyType_GetSlotNames(Py_TYPE(obj)); |
| 4129 | if (slotnames == NULL) { |
| 4130 | Py_DECREF(state); |
| 4131 | return NULL; |
| 4132 | } |
| 4133 | |
| 4134 | assert(slotnames == Py_None || PyList_Check(slotnames)); |
Serhiy Storchaka | f81be8a | 2015-12-25 21:04:29 +0200 | [diff] [blame] | 4135 | if (required) { |
| 4136 | Py_ssize_t basicsize = PyBaseObject_Type.tp_basicsize; |
| 4137 | if (obj->ob_type->tp_dictoffset) |
| 4138 | basicsize += sizeof(PyObject *); |
| 4139 | if (obj->ob_type->tp_weaklistoffset) |
| 4140 | basicsize += sizeof(PyObject *); |
| 4141 | if (slotnames != Py_None) |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4142 | basicsize += sizeof(PyObject *) * PyList_GET_SIZE(slotnames); |
Serhiy Storchaka | f81be8a | 2015-12-25 21:04:29 +0200 | [diff] [blame] | 4143 | if (obj->ob_type->tp_basicsize > basicsize) { |
| 4144 | Py_DECREF(slotnames); |
| 4145 | Py_DECREF(state); |
| 4146 | PyErr_Format(PyExc_TypeError, |
| 4147 | "can't pickle %.200s objects", |
| 4148 | Py_TYPE(obj)->tp_name); |
| 4149 | return NULL; |
| 4150 | } |
| 4151 | } |
| 4152 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4153 | if (slotnames != Py_None && PyList_GET_SIZE(slotnames) > 0) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4154 | PyObject *slots; |
| 4155 | Py_ssize_t slotnames_size, i; |
| 4156 | |
| 4157 | slots = PyDict_New(); |
| 4158 | if (slots == NULL) { |
| 4159 | Py_DECREF(slotnames); |
| 4160 | Py_DECREF(state); |
| 4161 | return NULL; |
| 4162 | } |
| 4163 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4164 | slotnames_size = PyList_GET_SIZE(slotnames); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4165 | for (i = 0; i < slotnames_size; i++) { |
| 4166 | PyObject *name, *value; |
| 4167 | |
| 4168 | name = PyList_GET_ITEM(slotnames, i); |
Serhiy Storchaka | d28bb62 | 2015-11-25 18:33:29 +0200 | [diff] [blame] | 4169 | Py_INCREF(name); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4170 | if (_PyObject_LookupAttr(obj, name, &value) < 0) { |
| 4171 | goto error; |
| 4172 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4173 | if (value == NULL) { |
Serhiy Storchaka | d28bb62 | 2015-11-25 18:33:29 +0200 | [diff] [blame] | 4174 | Py_DECREF(name); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4175 | /* It is not an error if the attribute is not present. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4176 | } |
| 4177 | else { |
| 4178 | int err = PyDict_SetItem(slots, name, value); |
Serhiy Storchaka | d28bb62 | 2015-11-25 18:33:29 +0200 | [diff] [blame] | 4179 | Py_DECREF(name); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4180 | Py_DECREF(value); |
| 4181 | if (err) { |
| 4182 | goto error; |
| 4183 | } |
| 4184 | } |
| 4185 | |
Martin Panter | 8f26565 | 2016-04-19 04:03:41 +0000 | [diff] [blame] | 4186 | /* The list is stored on the class so it may mutate while we |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4187 | iterate over it */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4188 | if (slotnames_size != PyList_GET_SIZE(slotnames)) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4189 | PyErr_Format(PyExc_RuntimeError, |
| 4190 | "__slotsname__ changed size during iteration"); |
| 4191 | goto error; |
| 4192 | } |
| 4193 | |
| 4194 | /* We handle errors within the loop here. */ |
| 4195 | if (0) { |
| 4196 | error: |
| 4197 | Py_DECREF(slotnames); |
| 4198 | Py_DECREF(slots); |
| 4199 | Py_DECREF(state); |
| 4200 | return NULL; |
| 4201 | } |
| 4202 | } |
| 4203 | |
| 4204 | /* If we found some slot attributes, pack them in a tuple along |
Berker Peksag | 0ac70c0 | 2016-04-29 16:54:10 +0300 | [diff] [blame] | 4205 | the original attribute dictionary. */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 4206 | if (PyDict_GET_SIZE(slots) > 0) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4207 | PyObject *state2; |
| 4208 | |
| 4209 | state2 = PyTuple_Pack(2, state, slots); |
| 4210 | Py_DECREF(state); |
| 4211 | if (state2 == NULL) { |
| 4212 | Py_DECREF(slotnames); |
| 4213 | Py_DECREF(slots); |
| 4214 | return NULL; |
| 4215 | } |
| 4216 | state = state2; |
| 4217 | } |
| 4218 | Py_DECREF(slots); |
| 4219 | } |
| 4220 | Py_DECREF(slotnames); |
| 4221 | } |
| 4222 | else { /* getstate != NULL */ |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 4223 | state = _PyObject_CallNoArg(getstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4224 | Py_DECREF(getstate); |
| 4225 | if (state == NULL) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4226 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4227 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4228 | |
| 4229 | return state; |
| 4230 | } |
| 4231 | |
Benjamin Peterson | 6cb526e | 2016-09-09 12:42:51 -0700 | [diff] [blame] | 4232 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4233 | _PyObject_GetNewArguments(PyObject *obj, PyObject **args, PyObject **kwargs) |
| 4234 | { |
| 4235 | PyObject *getnewargs, *getnewargs_ex; |
| 4236 | _Py_IDENTIFIER(__getnewargs_ex__); |
| 4237 | _Py_IDENTIFIER(__getnewargs__); |
| 4238 | |
| 4239 | if (args == NULL || kwargs == NULL) { |
| 4240 | PyErr_BadInternalCall(); |
| 4241 | return -1; |
| 4242 | } |
| 4243 | |
| 4244 | /* We first attempt to fetch the arguments for __new__ by calling |
| 4245 | __getnewargs_ex__ on the object. */ |
Benjamin Peterson | e686c5c | 2014-02-16 13:49:16 -0500 | [diff] [blame] | 4246 | getnewargs_ex = _PyObject_LookupSpecial(obj, &PyId___getnewargs_ex__); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4247 | if (getnewargs_ex != NULL) { |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 4248 | PyObject *newargs = _PyObject_CallNoArg(getnewargs_ex); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4249 | Py_DECREF(getnewargs_ex); |
| 4250 | if (newargs == NULL) { |
| 4251 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4252 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4253 | if (!PyTuple_Check(newargs)) { |
| 4254 | PyErr_Format(PyExc_TypeError, |
| 4255 | "__getnewargs_ex__ should return a tuple, " |
| 4256 | "not '%.200s'", Py_TYPE(newargs)->tp_name); |
| 4257 | Py_DECREF(newargs); |
| 4258 | return -1; |
| 4259 | } |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4260 | if (PyTuple_GET_SIZE(newargs) != 2) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4261 | PyErr_Format(PyExc_ValueError, |
| 4262 | "__getnewargs_ex__ should return a tuple of " |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4263 | "length 2, not %zd", PyTuple_GET_SIZE(newargs)); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4264 | Py_DECREF(newargs); |
| 4265 | return -1; |
| 4266 | } |
| 4267 | *args = PyTuple_GET_ITEM(newargs, 0); |
| 4268 | Py_INCREF(*args); |
| 4269 | *kwargs = PyTuple_GET_ITEM(newargs, 1); |
| 4270 | Py_INCREF(*kwargs); |
| 4271 | Py_DECREF(newargs); |
| 4272 | |
| 4273 | /* XXX We should perhaps allow None to be passed here. */ |
| 4274 | if (!PyTuple_Check(*args)) { |
| 4275 | PyErr_Format(PyExc_TypeError, |
| 4276 | "first item of the tuple returned by " |
| 4277 | "__getnewargs_ex__ must be a tuple, not '%.200s'", |
| 4278 | Py_TYPE(*args)->tp_name); |
| 4279 | Py_CLEAR(*args); |
| 4280 | Py_CLEAR(*kwargs); |
| 4281 | return -1; |
| 4282 | } |
| 4283 | if (!PyDict_Check(*kwargs)) { |
| 4284 | PyErr_Format(PyExc_TypeError, |
| 4285 | "second item of the tuple returned by " |
| 4286 | "__getnewargs_ex__ must be a dict, not '%.200s'", |
| 4287 | Py_TYPE(*kwargs)->tp_name); |
| 4288 | Py_CLEAR(*args); |
| 4289 | Py_CLEAR(*kwargs); |
| 4290 | return -1; |
| 4291 | } |
| 4292 | return 0; |
Benjamin Peterson | e686c5c | 2014-02-16 13:49:16 -0500 | [diff] [blame] | 4293 | } else if (PyErr_Occurred()) { |
| 4294 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4295 | } |
| 4296 | |
| 4297 | /* The object does not have __getnewargs_ex__ so we fallback on using |
| 4298 | __getnewargs__ instead. */ |
Benjamin Peterson | e686c5c | 2014-02-16 13:49:16 -0500 | [diff] [blame] | 4299 | getnewargs = _PyObject_LookupSpecial(obj, &PyId___getnewargs__); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4300 | if (getnewargs != NULL) { |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 4301 | *args = _PyObject_CallNoArg(getnewargs); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4302 | Py_DECREF(getnewargs); |
| 4303 | if (*args == NULL) { |
| 4304 | return -1; |
| 4305 | } |
| 4306 | if (!PyTuple_Check(*args)) { |
| 4307 | PyErr_Format(PyExc_TypeError, |
| 4308 | "__getnewargs__ should return a tuple, " |
| 4309 | "not '%.200s'", Py_TYPE(*args)->tp_name); |
| 4310 | Py_CLEAR(*args); |
| 4311 | return -1; |
| 4312 | } |
| 4313 | *kwargs = NULL; |
| 4314 | return 0; |
Benjamin Peterson | e686c5c | 2014-02-16 13:49:16 -0500 | [diff] [blame] | 4315 | } else if (PyErr_Occurred()) { |
| 4316 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4317 | } |
| 4318 | |
| 4319 | /* The object does not have __getnewargs_ex__ and __getnewargs__. This may |
Martin Panter | 8f26565 | 2016-04-19 04:03:41 +0000 | [diff] [blame] | 4320 | mean __new__ does not takes any arguments on this object, or that the |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4321 | object does not implement the reduce protocol for pickling or |
| 4322 | copying. */ |
| 4323 | *args = NULL; |
| 4324 | *kwargs = NULL; |
| 4325 | return 0; |
| 4326 | } |
| 4327 | |
Benjamin Peterson | 6cb526e | 2016-09-09 12:42:51 -0700 | [diff] [blame] | 4328 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4329 | _PyObject_GetItemsIter(PyObject *obj, PyObject **listitems, |
| 4330 | PyObject **dictitems) |
| 4331 | { |
| 4332 | if (listitems == NULL || dictitems == NULL) { |
| 4333 | PyErr_BadInternalCall(); |
| 4334 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4335 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4336 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4337 | if (!PyList_Check(obj)) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4338 | *listitems = Py_None; |
| 4339 | Py_INCREF(*listitems); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4340 | } |
| 4341 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4342 | *listitems = PyObject_GetIter(obj); |
Christian Heimes | 2489bd8 | 2013-11-23 21:19:43 +0100 | [diff] [blame] | 4343 | if (*listitems == NULL) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4344 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4345 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4346 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4347 | if (!PyDict_Check(obj)) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4348 | *dictitems = Py_None; |
| 4349 | Py_INCREF(*dictitems); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4350 | } |
| 4351 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4352 | PyObject *items; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4353 | _Py_IDENTIFIER(items); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4354 | |
| 4355 | items = _PyObject_CallMethodIdObjArgs(obj, &PyId_items, NULL); |
| 4356 | if (items == NULL) { |
| 4357 | Py_CLEAR(*listitems); |
| 4358 | return -1; |
| 4359 | } |
| 4360 | *dictitems = PyObject_GetIter(items); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4361 | Py_DECREF(items); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4362 | if (*dictitems == NULL) { |
| 4363 | Py_CLEAR(*listitems); |
| 4364 | return -1; |
| 4365 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4366 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4367 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4368 | assert(*listitems != NULL && *dictitems != NULL); |
| 4369 | |
| 4370 | return 0; |
| 4371 | } |
| 4372 | |
| 4373 | static PyObject * |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 4374 | reduce_newobj(PyObject *obj) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4375 | { |
| 4376 | PyObject *args = NULL, *kwargs = NULL; |
| 4377 | PyObject *copyreg; |
| 4378 | PyObject *newobj, *newargs, *state, *listitems, *dictitems; |
| 4379 | PyObject *result; |
Serhiy Storchaka | f81be8a | 2015-12-25 21:04:29 +0200 | [diff] [blame] | 4380 | int hasargs; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4381 | |
Serhiy Storchaka | d7a4415 | 2015-11-12 11:23:04 +0200 | [diff] [blame] | 4382 | if (Py_TYPE(obj)->tp_new == NULL) { |
| 4383 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | f81be8a | 2015-12-25 21:04:29 +0200 | [diff] [blame] | 4384 | "can't pickle %.200s objects", |
Serhiy Storchaka | d7a4415 | 2015-11-12 11:23:04 +0200 | [diff] [blame] | 4385 | Py_TYPE(obj)->tp_name); |
| 4386 | return NULL; |
| 4387 | } |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 4388 | if (_PyObject_GetNewArguments(obj, &args, &kwargs) < 0) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4389 | return NULL; |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 4390 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4391 | copyreg = import_copyreg(); |
| 4392 | if (copyreg == NULL) { |
Serhiy Storchaka | f81be8a | 2015-12-25 21:04:29 +0200 | [diff] [blame] | 4393 | Py_XDECREF(args); |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 4394 | Py_XDECREF(kwargs); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4395 | return NULL; |
| 4396 | } |
Serhiy Storchaka | f81be8a | 2015-12-25 21:04:29 +0200 | [diff] [blame] | 4397 | hasargs = (args != NULL); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 4398 | if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) { |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 4399 | _Py_IDENTIFIER(__newobj__); |
| 4400 | PyObject *cls; |
| 4401 | Py_ssize_t i, n; |
| 4402 | |
| 4403 | Py_XDECREF(kwargs); |
| 4404 | newobj = _PyObject_GetAttrId(copyreg, &PyId___newobj__); |
| 4405 | Py_DECREF(copyreg); |
| 4406 | if (newobj == NULL) { |
Serhiy Storchaka | f81be8a | 2015-12-25 21:04:29 +0200 | [diff] [blame] | 4407 | Py_XDECREF(args); |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 4408 | return NULL; |
| 4409 | } |
Serhiy Storchaka | f81be8a | 2015-12-25 21:04:29 +0200 | [diff] [blame] | 4410 | n = args ? PyTuple_GET_SIZE(args) : 0; |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 4411 | newargs = PyTuple_New(n+1); |
| 4412 | if (newargs == NULL) { |
Serhiy Storchaka | f81be8a | 2015-12-25 21:04:29 +0200 | [diff] [blame] | 4413 | Py_XDECREF(args); |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 4414 | Py_DECREF(newobj); |
| 4415 | return NULL; |
| 4416 | } |
| 4417 | cls = (PyObject *) Py_TYPE(obj); |
| 4418 | Py_INCREF(cls); |
| 4419 | PyTuple_SET_ITEM(newargs, 0, cls); |
| 4420 | for (i = 0; i < n; i++) { |
| 4421 | PyObject *v = PyTuple_GET_ITEM(args, i); |
| 4422 | Py_INCREF(v); |
| 4423 | PyTuple_SET_ITEM(newargs, i+1, v); |
| 4424 | } |
Serhiy Storchaka | f81be8a | 2015-12-25 21:04:29 +0200 | [diff] [blame] | 4425 | Py_XDECREF(args); |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 4426 | } |
Christian Heimes | 07a2a1b | 2016-09-09 00:21:22 +0200 | [diff] [blame] | 4427 | else if (args != NULL) { |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 4428 | _Py_IDENTIFIER(__newobj_ex__); |
| 4429 | |
| 4430 | newobj = _PyObject_GetAttrId(copyreg, &PyId___newobj_ex__); |
| 4431 | Py_DECREF(copyreg); |
| 4432 | if (newobj == NULL) { |
| 4433 | Py_DECREF(args); |
| 4434 | Py_DECREF(kwargs); |
| 4435 | return NULL; |
| 4436 | } |
| 4437 | newargs = PyTuple_Pack(3, Py_TYPE(obj), args, kwargs); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4438 | Py_DECREF(args); |
| 4439 | Py_DECREF(kwargs); |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 4440 | if (newargs == NULL) { |
| 4441 | Py_DECREF(newobj); |
| 4442 | return NULL; |
| 4443 | } |
| 4444 | } |
Christian Heimes | 07a2a1b | 2016-09-09 00:21:22 +0200 | [diff] [blame] | 4445 | else { |
| 4446 | /* args == NULL */ |
| 4447 | Py_DECREF(kwargs); |
| 4448 | PyErr_BadInternalCall(); |
| 4449 | return NULL; |
| 4450 | } |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 4451 | |
Serhiy Storchaka | f81be8a | 2015-12-25 21:04:29 +0200 | [diff] [blame] | 4452 | state = _PyObject_GetState(obj, |
| 4453 | !hasargs && !PyList_Check(obj) && !PyDict_Check(obj)); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4454 | if (state == NULL) { |
| 4455 | Py_DECREF(newobj); |
| 4456 | Py_DECREF(newargs); |
| 4457 | return NULL; |
| 4458 | } |
| 4459 | if (_PyObject_GetItemsIter(obj, &listitems, &dictitems) < 0) { |
| 4460 | Py_DECREF(newobj); |
| 4461 | Py_DECREF(newargs); |
| 4462 | Py_DECREF(state); |
| 4463 | return NULL; |
| 4464 | } |
| 4465 | |
| 4466 | result = PyTuple_Pack(5, newobj, newargs, state, listitems, dictitems); |
| 4467 | Py_DECREF(newobj); |
| 4468 | Py_DECREF(newargs); |
| 4469 | Py_DECREF(state); |
| 4470 | Py_DECREF(listitems); |
| 4471 | Py_DECREF(dictitems); |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 4472 | return result; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4473 | } |
| 4474 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4475 | /* |
| 4476 | * There were two problems when object.__reduce__ and object.__reduce_ex__ |
| 4477 | * were implemented in the same function: |
| 4478 | * - trying to pickle an object with a custom __reduce__ method that |
| 4479 | * fell back to object.__reduce__ in certain circumstances led to |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 4480 | * infinite recursion at Python level and eventual RecursionError. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4481 | * - Pickling objects that lied about their type by overwriting the |
| 4482 | * __class__ descriptor could lead to infinite recursion at C level |
| 4483 | * and eventual segfault. |
| 4484 | * |
| 4485 | * Because of backwards compatibility, the two methods still have to |
| 4486 | * behave in the same way, even if this is not required by the pickle |
| 4487 | * protocol. This common functionality was moved to the _common_reduce |
| 4488 | * function. |
| 4489 | */ |
| 4490 | static PyObject * |
| 4491 | _common_reduce(PyObject *self, int proto) |
| 4492 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4493 | PyObject *copyreg, *res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4494 | |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 4495 | if (proto >= 2) |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 4496 | return reduce_newobj(self); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4497 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4498 | copyreg = import_copyreg(); |
| 4499 | if (!copyreg) |
| 4500 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4501 | |
INADA Naoki | 72dccde | 2017-02-16 09:26:01 +0900 | [diff] [blame] | 4502 | res = PyObject_CallMethod(copyreg, "_reduce_ex", "Oi", self, proto); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4503 | Py_DECREF(copyreg); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4505 | return res; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4506 | } |
| 4507 | |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 4508 | /*[clinic input] |
| 4509 | object.__reduce__ |
| 4510 | |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 4511 | Helper for pickle. |
| 4512 | [clinic start generated code]*/ |
| 4513 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4514 | static PyObject * |
Serhiy Storchaka | 205e00c | 2017-04-08 09:52:59 +0300 | [diff] [blame] | 4515 | object___reduce___impl(PyObject *self) |
| 4516 | /*[clinic end generated code: output=d4ca691f891c6e2f input=11562e663947e18b]*/ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4517 | { |
Serhiy Storchaka | 205e00c | 2017-04-08 09:52:59 +0300 | [diff] [blame] | 4518 | return _common_reduce(self, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4519 | } |
| 4520 | |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 4521 | /*[clinic input] |
| 4522 | object.__reduce_ex__ |
| 4523 | |
Serhiy Storchaka | 205e00c | 2017-04-08 09:52:59 +0300 | [diff] [blame] | 4524 | protocol: int |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 4525 | / |
| 4526 | |
| 4527 | Helper for pickle. |
| 4528 | [clinic start generated code]*/ |
| 4529 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4530 | static PyObject * |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 4531 | object___reduce_ex___impl(PyObject *self, int protocol) |
Serhiy Storchaka | 205e00c | 2017-04-08 09:52:59 +0300 | [diff] [blame] | 4532 | /*[clinic end generated code: output=2e157766f6b50094 input=f326b43fb8a4c5ff]*/ |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4533 | { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4534 | static PyObject *objreduce; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4535 | PyObject *reduce, *res; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4536 | _Py_IDENTIFIER(__reduce__); |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4537 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4538 | if (objreduce == NULL) { |
| 4539 | objreduce = _PyDict_GetItemId(PyBaseObject_Type.tp_dict, |
| 4540 | &PyId___reduce__); |
| 4541 | if (objreduce == NULL) |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 4542 | return NULL; |
| 4543 | } |
| 4544 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4545 | reduce = _PyObject_GetAttrId(self, &PyId___reduce__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4546 | if (reduce == NULL) |
| 4547 | PyErr_Clear(); |
| 4548 | else { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 4549 | PyObject *cls, *clsreduce; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4550 | int override; |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 4551 | |
| 4552 | cls = (PyObject *) Py_TYPE(self); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4553 | clsreduce = _PyObject_GetAttrId(cls, &PyId___reduce__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4554 | if (clsreduce == NULL) { |
| 4555 | Py_DECREF(reduce); |
| 4556 | return NULL; |
| 4557 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4558 | override = (clsreduce != objreduce); |
| 4559 | Py_DECREF(clsreduce); |
| 4560 | if (override) { |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 4561 | res = _PyObject_CallNoArg(reduce); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4562 | Py_DECREF(reduce); |
| 4563 | return res; |
| 4564 | } |
| 4565 | else |
| 4566 | Py_DECREF(reduce); |
| 4567 | } |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4568 | |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 4569 | return _common_reduce(self, protocol); |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 4570 | } |
| 4571 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 4572 | static PyObject * |
| 4573 | object_subclasshook(PyObject *cls, PyObject *args) |
| 4574 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4575 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 4576 | } |
| 4577 | |
| 4578 | PyDoc_STRVAR(object_subclasshook_doc, |
| 4579 | "Abstract classes can override this to customize issubclass().\n" |
| 4580 | "\n" |
| 4581 | "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n" |
| 4582 | "It should return True, False or NotImplemented. If it returns\n" |
| 4583 | "NotImplemented, the normal algorithm is used. Otherwise, it\n" |
| 4584 | "overrides the normal algorithm (and the outcome is cached).\n"); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 4585 | |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 4586 | static PyObject * |
| 4587 | object_init_subclass(PyObject *cls, PyObject *arg) |
| 4588 | { |
| 4589 | Py_RETURN_NONE; |
| 4590 | } |
| 4591 | |
| 4592 | PyDoc_STRVAR(object_init_subclass_doc, |
Berker Peksag | 01d1719 | 2016-07-30 14:06:15 +0300 | [diff] [blame] | 4593 | "This method is called when a class is subclassed.\n" |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 4594 | "\n" |
Berker Peksag | 01d1719 | 2016-07-30 14:06:15 +0300 | [diff] [blame] | 4595 | "The default implementation does nothing. It may be\n" |
| 4596 | "overridden to extend subclasses.\n"); |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 4597 | |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 4598 | /*[clinic input] |
| 4599 | object.__format__ |
| 4600 | |
| 4601 | format_spec: unicode |
| 4602 | / |
| 4603 | |
| 4604 | Default object formatter. |
| 4605 | [clinic start generated code]*/ |
| 4606 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 4607 | static PyObject * |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 4608 | object___format___impl(PyObject *self, PyObject *format_spec) |
| 4609 | /*[clinic end generated code: output=34897efb543a974b input=7c3b3bc53a6fb7fa]*/ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 4610 | { |
Serhiy Storchaka | d1af5ef | 2016-10-30 19:33:54 +0200 | [diff] [blame] | 4611 | /* Issue 7994: If we're converting to a string, we |
| 4612 | should reject format specifications */ |
| 4613 | if (PyUnicode_GET_LENGTH(format_spec) > 0) { |
| 4614 | PyErr_Format(PyExc_TypeError, |
| 4615 | "unsupported format string passed to %.200s.__format__", |
| 4616 | self->ob_type->tp_name); |
| 4617 | return NULL; |
| 4618 | } |
Serhiy Storchaka | 7e19dbc | 2017-05-13 12:40:52 +0300 | [diff] [blame] | 4619 | return PyObject_Str(self); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 4620 | } |
| 4621 | |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 4622 | /*[clinic input] |
| 4623 | object.__sizeof__ |
| 4624 | |
| 4625 | Size of object in memory, in bytes. |
| 4626 | [clinic start generated code]*/ |
| 4627 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 4628 | static PyObject * |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 4629 | object___sizeof___impl(PyObject *self) |
| 4630 | /*[clinic end generated code: output=73edab332f97d550 input=1200ff3dfe485306]*/ |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 4631 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4632 | Py_ssize_t res, isize; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 4633 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4634 | res = 0; |
| 4635 | isize = self->ob_type->tp_itemsize; |
| 4636 | if (isize > 0) |
Antoine Pitrou | a654510 | 2015-03-10 22:32:00 +0100 | [diff] [blame] | 4637 | res = Py_SIZE(self) * isize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4638 | res += self->ob_type->tp_basicsize; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 4639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4640 | return PyLong_FromSsize_t(res); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 4641 | } |
| 4642 | |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 4643 | /* __dir__ for generic objects: returns __dict__, __class__, |
| 4644 | and recursively up the __class__.__bases__ chain. |
| 4645 | */ |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 4646 | /*[clinic input] |
| 4647 | object.__dir__ |
| 4648 | |
| 4649 | Default dir() implementation. |
| 4650 | [clinic start generated code]*/ |
| 4651 | |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 4652 | static PyObject * |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 4653 | object___dir___impl(PyObject *self) |
| 4654 | /*[clinic end generated code: output=66dd48ea62f26c90 input=0a89305bec669b10]*/ |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 4655 | { |
| 4656 | PyObject *result = NULL; |
| 4657 | PyObject *dict = NULL; |
| 4658 | PyObject *itsclass = NULL; |
| 4659 | |
| 4660 | /* 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] | 4661 | dict = _PyObject_GetAttrId(self, &PyId___dict__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 4662 | if (dict == NULL) { |
| 4663 | PyErr_Clear(); |
| 4664 | dict = PyDict_New(); |
| 4665 | } |
| 4666 | else if (!PyDict_Check(dict)) { |
| 4667 | Py_DECREF(dict); |
| 4668 | dict = PyDict_New(); |
| 4669 | } |
| 4670 | else { |
| 4671 | /* Copy __dict__ to avoid mutating it. */ |
| 4672 | PyObject *temp = PyDict_Copy(dict); |
| 4673 | Py_DECREF(dict); |
| 4674 | dict = temp; |
| 4675 | } |
| 4676 | |
| 4677 | if (dict == NULL) |
| 4678 | goto error; |
| 4679 | |
| 4680 | /* Merge in attrs reachable from its class. */ |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 4681 | itsclass = _PyObject_GetAttrId(self, &PyId___class__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 4682 | if (itsclass == NULL) |
| 4683 | /* XXX(tomer): Perhaps fall back to obj->ob_type if no |
| 4684 | __class__ exists? */ |
| 4685 | PyErr_Clear(); |
| 4686 | else if (merge_class_dict(dict, itsclass) != 0) |
| 4687 | goto error; |
| 4688 | |
| 4689 | result = PyDict_Keys(dict); |
| 4690 | /* fall through */ |
| 4691 | error: |
| 4692 | Py_XDECREF(itsclass); |
| 4693 | Py_XDECREF(dict); |
| 4694 | return result; |
| 4695 | } |
| 4696 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 4697 | static PyMethodDef object_methods[] = { |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 4698 | OBJECT___REDUCE_EX___METHODDEF |
| 4699 | OBJECT___REDUCE___METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4700 | {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS, |
| 4701 | object_subclasshook_doc}, |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 4702 | {"__init_subclass__", object_init_subclass, METH_CLASS | METH_NOARGS, |
| 4703 | object_init_subclass_doc}, |
Serhiy Storchaka | 5c643a0 | 2017-03-19 08:46:44 +0200 | [diff] [blame] | 4704 | OBJECT___FORMAT___METHODDEF |
| 4705 | OBJECT___SIZEOF___METHODDEF |
| 4706 | OBJECT___DIR___METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4707 | {0} |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 4708 | }; |
| 4709 | |
Guido van Rossum | 036f999 | 2003-02-21 22:02:54 +0000 | [diff] [blame] | 4710 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4711 | PyTypeObject PyBaseObject_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4712 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 4713 | "object", /* tp_name */ |
| 4714 | sizeof(PyObject), /* tp_basicsize */ |
| 4715 | 0, /* tp_itemsize */ |
| 4716 | object_dealloc, /* tp_dealloc */ |
| 4717 | 0, /* tp_print */ |
| 4718 | 0, /* tp_getattr */ |
| 4719 | 0, /* tp_setattr */ |
| 4720 | 0, /* tp_reserved */ |
| 4721 | object_repr, /* tp_repr */ |
| 4722 | 0, /* tp_as_number */ |
| 4723 | 0, /* tp_as_sequence */ |
| 4724 | 0, /* tp_as_mapping */ |
| 4725 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
| 4726 | 0, /* tp_call */ |
| 4727 | object_str, /* tp_str */ |
| 4728 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4729 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4730 | 0, /* tp_as_buffer */ |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 4731 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4732 | PyDoc_STR("object()\n--\n\nThe most base type"), /* tp_doc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4733 | 0, /* tp_traverse */ |
| 4734 | 0, /* tp_clear */ |
| 4735 | object_richcompare, /* tp_richcompare */ |
| 4736 | 0, /* tp_weaklistoffset */ |
| 4737 | 0, /* tp_iter */ |
| 4738 | 0, /* tp_iternext */ |
| 4739 | object_methods, /* tp_methods */ |
| 4740 | 0, /* tp_members */ |
| 4741 | object_getsets, /* tp_getset */ |
| 4742 | 0, /* tp_base */ |
| 4743 | 0, /* tp_dict */ |
| 4744 | 0, /* tp_descr_get */ |
| 4745 | 0, /* tp_descr_set */ |
| 4746 | 0, /* tp_dictoffset */ |
| 4747 | object_init, /* tp_init */ |
| 4748 | PyType_GenericAlloc, /* tp_alloc */ |
| 4749 | object_new, /* tp_new */ |
| 4750 | PyObject_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4751 | }; |
| 4752 | |
| 4753 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 4754 | /* 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] | 4755 | |
| 4756 | static int |
| 4757 | add_methods(PyTypeObject *type, PyMethodDef *meth) |
| 4758 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4759 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4760 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4761 | for (; meth->ml_name != NULL; meth++) { |
| 4762 | PyObject *descr; |
Benjamin Peterson | 89a6e9a | 2012-05-08 09:22:24 -0400 | [diff] [blame] | 4763 | int err; |
INADA Naoki | 35a96a2 | 2017-01-28 16:35:44 +0900 | [diff] [blame] | 4764 | int isdescr = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4765 | if (PyDict_GetItemString(dict, meth->ml_name) && |
| 4766 | !(meth->ml_flags & METH_COEXIST)) |
| 4767 | continue; |
| 4768 | if (meth->ml_flags & METH_CLASS) { |
| 4769 | if (meth->ml_flags & METH_STATIC) { |
| 4770 | PyErr_SetString(PyExc_ValueError, |
| 4771 | "method cannot be both class and static"); |
| 4772 | return -1; |
| 4773 | } |
| 4774 | descr = PyDescr_NewClassMethod(type, meth); |
| 4775 | } |
| 4776 | else if (meth->ml_flags & METH_STATIC) { |
Andrew Svetlov | 3ba3a3e | 2012-12-25 13:32:35 +0200 | [diff] [blame] | 4777 | PyObject *cfunc = PyCFunction_NewEx(meth, (PyObject*)type, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4778 | if (cfunc == NULL) |
| 4779 | return -1; |
| 4780 | descr = PyStaticMethod_New(cfunc); |
INADA Naoki | 35a96a2 | 2017-01-28 16:35:44 +0900 | [diff] [blame] | 4781 | isdescr = 0; // PyStaticMethod is not PyDescrObject |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4782 | Py_DECREF(cfunc); |
| 4783 | } |
| 4784 | else { |
| 4785 | descr = PyDescr_NewMethod(type, meth); |
| 4786 | } |
| 4787 | if (descr == NULL) |
| 4788 | return -1; |
INADA Naoki | 35a96a2 | 2017-01-28 16:35:44 +0900 | [diff] [blame] | 4789 | if (isdescr) { |
| 4790 | err = PyDict_SetItem(dict, PyDescr_NAME(descr), descr); |
| 4791 | } |
| 4792 | else { |
| 4793 | err = PyDict_SetItemString(dict, meth->ml_name, descr); |
| 4794 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4795 | Py_DECREF(descr); |
Benjamin Peterson | 89a6e9a | 2012-05-08 09:22:24 -0400 | [diff] [blame] | 4796 | if (err < 0) |
| 4797 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4798 | } |
| 4799 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4800 | } |
| 4801 | |
| 4802 | static int |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 4803 | add_members(PyTypeObject *type, PyMemberDef *memb) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4804 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4805 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4806 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4807 | for (; memb->name != NULL; memb++) { |
| 4808 | PyObject *descr; |
| 4809 | if (PyDict_GetItemString(dict, memb->name)) |
| 4810 | continue; |
| 4811 | descr = PyDescr_NewMember(type, memb); |
| 4812 | if (descr == NULL) |
| 4813 | return -1; |
INADA Naoki | 35a96a2 | 2017-01-28 16:35:44 +0900 | [diff] [blame] | 4814 | if (PyDict_SetItem(dict, PyDescr_NAME(descr), descr) < 0) { |
Benjamin Peterson | 27007dc | 2016-07-06 23:26:32 -0700 | [diff] [blame] | 4815 | Py_DECREF(descr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4816 | return -1; |
Benjamin Peterson | 27007dc | 2016-07-06 23:26:32 -0700 | [diff] [blame] | 4817 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4818 | Py_DECREF(descr); |
| 4819 | } |
| 4820 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4821 | } |
| 4822 | |
| 4823 | static int |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 4824 | add_getset(PyTypeObject *type, PyGetSetDef *gsp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4825 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4826 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4827 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4828 | for (; gsp->name != NULL; gsp++) { |
| 4829 | PyObject *descr; |
| 4830 | if (PyDict_GetItemString(dict, gsp->name)) |
| 4831 | continue; |
| 4832 | descr = PyDescr_NewGetSet(type, gsp); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4833 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4834 | if (descr == NULL) |
| 4835 | return -1; |
INADA Naoki | 35a96a2 | 2017-01-28 16:35:44 +0900 | [diff] [blame] | 4836 | if (PyDict_SetItem(dict, PyDescr_NAME(descr), descr) < 0) { |
Benjamin Peterson | 27007dc | 2016-07-06 23:26:32 -0700 | [diff] [blame] | 4837 | Py_DECREF(descr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4838 | return -1; |
Benjamin Peterson | 27007dc | 2016-07-06 23:26:32 -0700 | [diff] [blame] | 4839 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4840 | Py_DECREF(descr); |
| 4841 | } |
| 4842 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4843 | } |
| 4844 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4845 | static void |
| 4846 | inherit_special(PyTypeObject *type, PyTypeObject *base) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4847 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4848 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4849 | /* Copying basicsize is connected to the GC flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4850 | if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 4851 | (base->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 4852 | (!type->tp_traverse && !type->tp_clear)) { |
| 4853 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
| 4854 | if (type->tp_traverse == NULL) |
| 4855 | type->tp_traverse = base->tp_traverse; |
| 4856 | if (type->tp_clear == NULL) |
| 4857 | type->tp_clear = base->tp_clear; |
| 4858 | } |
| 4859 | { |
| 4860 | /* The condition below could use some explanation. |
| 4861 | It appears that tp_new is not inherited for static types |
| 4862 | whose base class is 'object'; this seems to be a precaution |
| 4863 | so that old extension types don't suddenly become |
| 4864 | callable (object.__new__ wouldn't insure the invariants |
| 4865 | that the extension type's own factory function ensures). |
| 4866 | Heap types, of course, are under our control, so they do |
| 4867 | inherit tp_new; static extension types that specify some |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 4868 | other built-in type as the default also |
| 4869 | inherit object.__new__. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4870 | if (base != &PyBaseObject_Type || |
| 4871 | (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 4872 | if (type->tp_new == NULL) |
| 4873 | type->tp_new = base->tp_new; |
| 4874 | } |
| 4875 | } |
Benjamin Peterson | a7465e2 | 2010-07-05 15:01:22 +0000 | [diff] [blame] | 4876 | if (type->tp_basicsize == 0) |
| 4877 | type->tp_basicsize = base->tp_basicsize; |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 4878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4879 | /* Copy other non-function slots */ |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 4880 | |
| 4881 | #undef COPYVAL |
| 4882 | #define COPYVAL(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4883 | if (type->SLOT == 0) type->SLOT = base->SLOT |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 4884 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4885 | COPYVAL(tp_itemsize); |
| 4886 | COPYVAL(tp_weaklistoffset); |
| 4887 | COPYVAL(tp_dictoffset); |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 4888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4889 | /* Setup fast subclass flags */ |
| 4890 | if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException)) |
| 4891 | type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS; |
| 4892 | else if (PyType_IsSubtype(base, &PyType_Type)) |
| 4893 | type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS; |
| 4894 | else if (PyType_IsSubtype(base, &PyLong_Type)) |
| 4895 | type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS; |
| 4896 | else if (PyType_IsSubtype(base, &PyBytes_Type)) |
| 4897 | type->tp_flags |= Py_TPFLAGS_BYTES_SUBCLASS; |
| 4898 | else if (PyType_IsSubtype(base, &PyUnicode_Type)) |
| 4899 | type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS; |
| 4900 | else if (PyType_IsSubtype(base, &PyTuple_Type)) |
| 4901 | type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS; |
| 4902 | else if (PyType_IsSubtype(base, &PyList_Type)) |
| 4903 | type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS; |
| 4904 | else if (PyType_IsSubtype(base, &PyDict_Type)) |
| 4905 | type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4906 | } |
| 4907 | |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 4908 | static int |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 4909 | overrides_hash(PyTypeObject *type) |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 4910 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4911 | PyObject *dict = type->tp_dict; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4912 | _Py_IDENTIFIER(__eq__); |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 4913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4914 | assert(dict != NULL); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 4915 | if (_PyDict_GetItemId(dict, &PyId___eq__) != NULL) |
| 4916 | return 1; |
| 4917 | if (_PyDict_GetItemId(dict, &PyId___hash__) != NULL) |
| 4918 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4919 | return 0; |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 4920 | } |
| 4921 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4922 | static void |
| 4923 | inherit_slots(PyTypeObject *type, PyTypeObject *base) |
| 4924 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4925 | PyTypeObject *basebase; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4926 | |
| 4927 | #undef SLOTDEFINED |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4928 | #undef COPYSLOT |
| 4929 | #undef COPYNUM |
| 4930 | #undef COPYSEQ |
| 4931 | #undef COPYMAP |
Guido van Rossum | 5af588b | 2001-10-12 14:13:21 +0000 | [diff] [blame] | 4932 | #undef COPYBUF |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4933 | |
| 4934 | #define SLOTDEFINED(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4935 | (base->SLOT != 0 && \ |
| 4936 | (basebase == NULL || base->SLOT != basebase->SLOT)) |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4937 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4938 | #define COPYSLOT(SLOT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4939 | if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4940 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4941 | #define COPYASYNC(SLOT) COPYSLOT(tp_as_async->SLOT) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4942 | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT) |
| 4943 | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT) |
| 4944 | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT) |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 4945 | #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4947 | /* This won't inherit indirect slots (from tp_as_number etc.) |
| 4948 | if type doesn't provide the space. */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 4949 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4950 | if (type->tp_as_number != NULL && base->tp_as_number != NULL) { |
| 4951 | basebase = base->tp_base; |
| 4952 | if (basebase->tp_as_number == NULL) |
| 4953 | basebase = NULL; |
| 4954 | COPYNUM(nb_add); |
| 4955 | COPYNUM(nb_subtract); |
| 4956 | COPYNUM(nb_multiply); |
| 4957 | COPYNUM(nb_remainder); |
| 4958 | COPYNUM(nb_divmod); |
| 4959 | COPYNUM(nb_power); |
| 4960 | COPYNUM(nb_negative); |
| 4961 | COPYNUM(nb_positive); |
| 4962 | COPYNUM(nb_absolute); |
| 4963 | COPYNUM(nb_bool); |
| 4964 | COPYNUM(nb_invert); |
| 4965 | COPYNUM(nb_lshift); |
| 4966 | COPYNUM(nb_rshift); |
| 4967 | COPYNUM(nb_and); |
| 4968 | COPYNUM(nb_xor); |
| 4969 | COPYNUM(nb_or); |
| 4970 | COPYNUM(nb_int); |
| 4971 | COPYNUM(nb_float); |
| 4972 | COPYNUM(nb_inplace_add); |
| 4973 | COPYNUM(nb_inplace_subtract); |
| 4974 | COPYNUM(nb_inplace_multiply); |
| 4975 | COPYNUM(nb_inplace_remainder); |
| 4976 | COPYNUM(nb_inplace_power); |
| 4977 | COPYNUM(nb_inplace_lshift); |
| 4978 | COPYNUM(nb_inplace_rshift); |
| 4979 | COPYNUM(nb_inplace_and); |
| 4980 | COPYNUM(nb_inplace_xor); |
| 4981 | COPYNUM(nb_inplace_or); |
| 4982 | COPYNUM(nb_true_divide); |
| 4983 | COPYNUM(nb_floor_divide); |
| 4984 | COPYNUM(nb_inplace_true_divide); |
| 4985 | COPYNUM(nb_inplace_floor_divide); |
| 4986 | COPYNUM(nb_index); |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 4987 | COPYNUM(nb_matrix_multiply); |
| 4988 | COPYNUM(nb_inplace_matrix_multiply); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4989 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4990 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4991 | if (type->tp_as_async != NULL && base->tp_as_async != NULL) { |
| 4992 | basebase = base->tp_base; |
| 4993 | if (basebase->tp_as_async == NULL) |
| 4994 | basebase = NULL; |
| 4995 | COPYASYNC(am_await); |
| 4996 | COPYASYNC(am_aiter); |
| 4997 | COPYASYNC(am_anext); |
| 4998 | } |
| 4999 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5000 | if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) { |
| 5001 | basebase = base->tp_base; |
| 5002 | if (basebase->tp_as_sequence == NULL) |
| 5003 | basebase = NULL; |
| 5004 | COPYSEQ(sq_length); |
| 5005 | COPYSEQ(sq_concat); |
| 5006 | COPYSEQ(sq_repeat); |
| 5007 | COPYSEQ(sq_item); |
| 5008 | COPYSEQ(sq_ass_item); |
| 5009 | COPYSEQ(sq_contains); |
| 5010 | COPYSEQ(sq_inplace_concat); |
| 5011 | COPYSEQ(sq_inplace_repeat); |
| 5012 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5013 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5014 | if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) { |
| 5015 | basebase = base->tp_base; |
| 5016 | if (basebase->tp_as_mapping == NULL) |
| 5017 | basebase = NULL; |
| 5018 | COPYMAP(mp_length); |
| 5019 | COPYMAP(mp_subscript); |
| 5020 | COPYMAP(mp_ass_subscript); |
| 5021 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5022 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5023 | if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) { |
| 5024 | basebase = base->tp_base; |
| 5025 | if (basebase->tp_as_buffer == NULL) |
| 5026 | basebase = NULL; |
| 5027 | COPYBUF(bf_getbuffer); |
| 5028 | COPYBUF(bf_releasebuffer); |
| 5029 | } |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 5030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5031 | basebase = base->tp_base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5032 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5033 | COPYSLOT(tp_dealloc); |
| 5034 | if (type->tp_getattr == NULL && type->tp_getattro == NULL) { |
| 5035 | type->tp_getattr = base->tp_getattr; |
| 5036 | type->tp_getattro = base->tp_getattro; |
| 5037 | } |
| 5038 | if (type->tp_setattr == NULL && type->tp_setattro == NULL) { |
| 5039 | type->tp_setattr = base->tp_setattr; |
| 5040 | type->tp_setattro = base->tp_setattro; |
| 5041 | } |
| 5042 | /* tp_reserved is ignored */ |
| 5043 | COPYSLOT(tp_repr); |
| 5044 | /* tp_hash see tp_richcompare */ |
| 5045 | COPYSLOT(tp_call); |
| 5046 | COPYSLOT(tp_str); |
| 5047 | { |
| 5048 | /* Copy comparison-related slots only when |
| 5049 | not overriding them anywhere */ |
| 5050 | if (type->tp_richcompare == NULL && |
| 5051 | type->tp_hash == NULL && |
| 5052 | !overrides_hash(type)) |
| 5053 | { |
| 5054 | type->tp_richcompare = base->tp_richcompare; |
| 5055 | type->tp_hash = base->tp_hash; |
| 5056 | } |
| 5057 | } |
| 5058 | { |
| 5059 | COPYSLOT(tp_iter); |
| 5060 | COPYSLOT(tp_iternext); |
| 5061 | } |
| 5062 | { |
| 5063 | COPYSLOT(tp_descr_get); |
| 5064 | COPYSLOT(tp_descr_set); |
| 5065 | COPYSLOT(tp_dictoffset); |
| 5066 | COPYSLOT(tp_init); |
| 5067 | COPYSLOT(tp_alloc); |
| 5068 | COPYSLOT(tp_is_gc); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 5069 | if ((type->tp_flags & Py_TPFLAGS_HAVE_FINALIZE) && |
| 5070 | (base->tp_flags & Py_TPFLAGS_HAVE_FINALIZE)) { |
| 5071 | COPYSLOT(tp_finalize); |
| 5072 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5073 | if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 5074 | (base->tp_flags & Py_TPFLAGS_HAVE_GC)) { |
| 5075 | /* They agree about gc. */ |
| 5076 | COPYSLOT(tp_free); |
| 5077 | } |
| 5078 | else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 5079 | type->tp_free == NULL && |
| 5080 | base->tp_free == PyObject_Free) { |
| 5081 | /* A bit of magic to plug in the correct default |
| 5082 | * tp_free function when a derived class adds gc, |
| 5083 | * didn't define tp_free, and the base uses the |
| 5084 | * default non-gc tp_free. |
| 5085 | */ |
| 5086 | type->tp_free = PyObject_GC_Del; |
| 5087 | } |
| 5088 | /* else they didn't agree about gc, and there isn't something |
| 5089 | * obvious to be done -- the type is on its own. |
| 5090 | */ |
| 5091 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5092 | } |
| 5093 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 5094 | static int add_operators(PyTypeObject *); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 5095 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5096 | int |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 5097 | PyType_Ready(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5098 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5099 | PyObject *dict, *bases; |
| 5100 | PyTypeObject *base; |
| 5101 | Py_ssize_t i, n; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5103 | if (type->tp_flags & Py_TPFLAGS_READY) { |
Victor Stinner | bda5a2b | 2017-01-25 23:33:27 +0100 | [diff] [blame] | 5104 | assert(_PyType_CheckConsistency(type)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5105 | return 0; |
| 5106 | } |
| 5107 | assert((type->tp_flags & Py_TPFLAGS_READYING) == 0); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 5108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5109 | type->tp_flags |= Py_TPFLAGS_READYING; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5110 | |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 5111 | #ifdef Py_TRACE_REFS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5112 | /* PyType_Ready is the closest thing we have to a choke point |
| 5113 | * for type objects, so is the best place I can think of to try |
| 5114 | * to get type objects into the doubly-linked list of all objects. |
Leo Arias | c3d9508 | 2018-02-03 18:36:10 -0600 | [diff] [blame] | 5115 | * Still, not all type objects go through PyType_Ready. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5116 | */ |
| 5117 | _Py_AddToAllObjects((PyObject *)type, 0); |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 5118 | #endif |
| 5119 | |
Serhiy Storchaka | de0574b | 2016-10-07 23:24:35 +0300 | [diff] [blame] | 5120 | if (type->tp_name == NULL) { |
| 5121 | PyErr_Format(PyExc_SystemError, |
| 5122 | "Type does not define the tp_name field."); |
| 5123 | goto error; |
| 5124 | } |
| 5125 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5126 | /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
| 5127 | base = type->tp_base; |
| 5128 | if (base == NULL && type != &PyBaseObject_Type) { |
| 5129 | base = type->tp_base = &PyBaseObject_Type; |
| 5130 | Py_INCREF(base); |
| 5131 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5132 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5133 | /* Now the only way base can still be NULL is if type is |
| 5134 | * &PyBaseObject_Type. |
| 5135 | */ |
Guido van Rossum | 692cdbc | 2006-03-10 02:04:28 +0000 | [diff] [blame] | 5136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5137 | /* Initialize the base class */ |
| 5138 | if (base != NULL && base->tp_dict == NULL) { |
| 5139 | if (PyType_Ready(base) < 0) |
| 5140 | goto error; |
| 5141 | } |
Guido van Rossum | 323a9cf | 2002-08-14 17:26:30 +0000 | [diff] [blame] | 5142 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5143 | /* Initialize ob_type if NULL. This means extensions that want to be |
| 5144 | compilable separately on Windows can call PyType_Ready() instead of |
| 5145 | initializing the ob_type field of their type objects. */ |
| 5146 | /* The test for base != NULL is really unnecessary, since base is only |
| 5147 | NULL when type is &PyBaseObject_Type, and we know its ob_type is |
| 5148 | not NULL (it's initialized to &PyType_Type). But coverity doesn't |
| 5149 | know that. */ |
| 5150 | if (Py_TYPE(type) == NULL && base != NULL) |
| 5151 | Py_TYPE(type) = Py_TYPE(base); |
Guido van Rossum | 0986d82 | 2002-04-08 01:38:42 +0000 | [diff] [blame] | 5152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5153 | /* Initialize tp_bases */ |
| 5154 | bases = type->tp_bases; |
| 5155 | if (bases == NULL) { |
| 5156 | if (base == NULL) |
| 5157 | bases = PyTuple_New(0); |
| 5158 | else |
| 5159 | bases = PyTuple_Pack(1, base); |
| 5160 | if (bases == NULL) |
| 5161 | goto error; |
| 5162 | type->tp_bases = bases; |
| 5163 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5165 | /* Initialize tp_dict */ |
| 5166 | dict = type->tp_dict; |
| 5167 | if (dict == NULL) { |
| 5168 | dict = PyDict_New(); |
| 5169 | if (dict == NULL) |
| 5170 | goto error; |
| 5171 | type->tp_dict = dict; |
| 5172 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5174 | /* Add type-specific descriptors to tp_dict */ |
| 5175 | if (add_operators(type) < 0) |
| 5176 | goto error; |
| 5177 | if (type->tp_methods != NULL) { |
| 5178 | if (add_methods(type, type->tp_methods) < 0) |
| 5179 | goto error; |
| 5180 | } |
| 5181 | if (type->tp_members != NULL) { |
| 5182 | if (add_members(type, type->tp_members) < 0) |
| 5183 | goto error; |
| 5184 | } |
| 5185 | if (type->tp_getset != NULL) { |
| 5186 | if (add_getset(type, type->tp_getset) < 0) |
| 5187 | goto error; |
| 5188 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5189 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5190 | /* Calculate method resolution order */ |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 5191 | if (mro_internal(type, NULL) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5192 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5193 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5194 | /* Inherit special flags from dominant base */ |
| 5195 | if (type->tp_base != NULL) |
| 5196 | inherit_special(type, type->tp_base); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 5197 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5198 | /* Initialize tp_dict properly */ |
| 5199 | bases = type->tp_mro; |
| 5200 | assert(bases != NULL); |
| 5201 | assert(PyTuple_Check(bases)); |
| 5202 | n = PyTuple_GET_SIZE(bases); |
| 5203 | for (i = 1; i < n; i++) { |
| 5204 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 5205 | if (PyType_Check(b)) |
| 5206 | inherit_slots(type, (PyTypeObject *)b); |
| 5207 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5208 | |
Serhiy Storchaka | e09bcc8 | 2015-01-28 11:03:33 +0200 | [diff] [blame] | 5209 | /* All bases of statically allocated type should be statically allocated */ |
| 5210 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 5211 | for (i = 0; i < n; i++) { |
| 5212 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 5213 | if (PyType_Check(b) && |
| 5214 | (((PyTypeObject *)b)->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 5215 | PyErr_Format(PyExc_TypeError, |
| 5216 | "type '%.100s' is not dynamically allocated but " |
| 5217 | "its base type '%.100s' is dynamically allocated", |
| 5218 | type->tp_name, ((PyTypeObject *)b)->tp_name); |
| 5219 | goto error; |
| 5220 | } |
| 5221 | } |
| 5222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5223 | /* Sanity check for tp_free. */ |
| 5224 | if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) && |
| 5225 | (type->tp_free == NULL || type->tp_free == PyObject_Del)) { |
| 5226 | /* This base class needs to call tp_free, but doesn't have |
| 5227 | * one, or its tp_free is for non-gc'ed objects. |
| 5228 | */ |
| 5229 | PyErr_Format(PyExc_TypeError, "type '%.100s' participates in " |
| 5230 | "gc and is a base type but has inappropriate " |
| 5231 | "tp_free slot", |
| 5232 | type->tp_name); |
| 5233 | goto error; |
| 5234 | } |
Tim Peters | 3cfe754 | 2003-05-21 21:29:48 +0000 | [diff] [blame] | 5235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5236 | /* if the type dictionary doesn't contain a __doc__, set it from |
| 5237 | the tp_doc slot. |
| 5238 | */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5239 | if (_PyDict_GetItemId(type->tp_dict, &PyId___doc__) == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5240 | if (type->tp_doc != NULL) { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 5241 | const char *old_doc = _PyType_DocWithoutSignature(type->tp_name, |
| 5242 | type->tp_doc); |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 5243 | PyObject *doc = PyUnicode_FromString(old_doc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5244 | if (doc == NULL) |
| 5245 | goto error; |
Victor Stinner | 5967bf4 | 2013-07-17 22:01:37 +0200 | [diff] [blame] | 5246 | if (_PyDict_SetItemId(type->tp_dict, &PyId___doc__, doc) < 0) { |
| 5247 | Py_DECREF(doc); |
| 5248 | goto error; |
| 5249 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5250 | Py_DECREF(doc); |
| 5251 | } else { |
Victor Stinner | 5967bf4 | 2013-07-17 22:01:37 +0200 | [diff] [blame] | 5252 | if (_PyDict_SetItemId(type->tp_dict, |
| 5253 | &PyId___doc__, Py_None) < 0) |
| 5254 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5255 | } |
| 5256 | } |
Martin v. Löwis | f9bd6b0 | 2002-02-18 17:46:48 +0000 | [diff] [blame] | 5257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5258 | /* Hack for tp_hash and __hash__. |
| 5259 | If after all that, tp_hash is still NULL, and __hash__ is not in |
| 5260 | tp_dict, set tp_hash to PyObject_HashNotImplemented and |
| 5261 | tp_dict['__hash__'] equal to None. |
| 5262 | This signals that __hash__ is not inherited. |
| 5263 | */ |
| 5264 | if (type->tp_hash == NULL) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5265 | if (_PyDict_GetItemId(type->tp_dict, &PyId___hash__) == NULL) { |
| 5266 | if (_PyDict_SetItemId(type->tp_dict, &PyId___hash__, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5267 | goto error; |
| 5268 | type->tp_hash = PyObject_HashNotImplemented; |
| 5269 | } |
| 5270 | } |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 5271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5272 | /* Some more special stuff */ |
| 5273 | base = type->tp_base; |
| 5274 | if (base != NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5275 | if (type->tp_as_async == NULL) |
| 5276 | type->tp_as_async = base->tp_as_async; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5277 | if (type->tp_as_number == NULL) |
| 5278 | type->tp_as_number = base->tp_as_number; |
| 5279 | if (type->tp_as_sequence == NULL) |
| 5280 | type->tp_as_sequence = base->tp_as_sequence; |
| 5281 | if (type->tp_as_mapping == NULL) |
| 5282 | type->tp_as_mapping = base->tp_as_mapping; |
| 5283 | if (type->tp_as_buffer == NULL) |
| 5284 | type->tp_as_buffer = base->tp_as_buffer; |
| 5285 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5287 | /* Link into each base class's list of subclasses */ |
| 5288 | bases = type->tp_bases; |
| 5289 | n = PyTuple_GET_SIZE(bases); |
| 5290 | for (i = 0; i < n; i++) { |
| 5291 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
| 5292 | if (PyType_Check(b) && |
| 5293 | add_subclass((PyTypeObject *)b, type) < 0) |
| 5294 | goto error; |
| 5295 | } |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 5296 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5297 | /* All done -- set the ready flag */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5298 | type->tp_flags = |
| 5299 | (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; |
Victor Stinner | bda5a2b | 2017-01-25 23:33:27 +0100 | [diff] [blame] | 5300 | assert(_PyType_CheckConsistency(type)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5301 | return 0; |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 5302 | |
| 5303 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5304 | type->tp_flags &= ~Py_TPFLAGS_READYING; |
| 5305 | return -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5306 | } |
| 5307 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 5308 | static int |
| 5309 | add_subclass(PyTypeObject *base, PyTypeObject *type) |
| 5310 | { |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 5311 | int result = -1; |
| 5312 | PyObject *dict, *key, *newobj; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 5313 | |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 5314 | dict = base->tp_subclasses; |
| 5315 | if (dict == NULL) { |
| 5316 | base->tp_subclasses = dict = PyDict_New(); |
| 5317 | if (dict == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5318 | return -1; |
| 5319 | } |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 5320 | assert(PyDict_CheckExact(dict)); |
| 5321 | key = PyLong_FromVoidPtr((void *) type); |
| 5322 | if (key == NULL) |
Victor Stinner | 9812af8 | 2013-07-08 22:25:48 +0200 | [diff] [blame] | 5323 | return -1; |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 5324 | newobj = PyWeakref_NewRef((PyObject *)type, NULL); |
| 5325 | if (newobj != NULL) { |
| 5326 | result = PyDict_SetItem(dict, key, newobj); |
| 5327 | Py_DECREF(newobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5328 | } |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 5329 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5330 | return result; |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 5331 | } |
| 5332 | |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 5333 | static int |
| 5334 | add_all_subclasses(PyTypeObject *type, PyObject *bases) |
| 5335 | { |
| 5336 | int res = 0; |
| 5337 | |
| 5338 | if (bases) { |
| 5339 | Py_ssize_t i; |
| 5340 | for (i = 0; i < PyTuple_GET_SIZE(bases); i++) { |
| 5341 | PyObject *base = PyTuple_GET_ITEM(bases, i); |
| 5342 | if (PyType_Check(base) && |
| 5343 | add_subclass((PyTypeObject*)base, type) < 0) |
| 5344 | res = -1; |
| 5345 | } |
| 5346 | } |
| 5347 | |
| 5348 | return res; |
| 5349 | } |
| 5350 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 5351 | static void |
| 5352 | remove_subclass(PyTypeObject *base, PyTypeObject *type) |
| 5353 | { |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 5354 | PyObject *dict, *key; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 5355 | |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 5356 | dict = base->tp_subclasses; |
| 5357 | if (dict == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5358 | return; |
| 5359 | } |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 5360 | assert(PyDict_CheckExact(dict)); |
| 5361 | key = PyLong_FromVoidPtr((void *) type); |
| 5362 | if (key == NULL || PyDict_DelItem(dict, key)) { |
| 5363 | /* This can happen if the type initialization errored out before |
| 5364 | the base subclasses were updated (e.g. a non-str __qualname__ |
| 5365 | was passed in the type dict). */ |
| 5366 | PyErr_Clear(); |
| 5367 | } |
| 5368 | Py_XDECREF(key); |
| 5369 | } |
| 5370 | |
| 5371 | static void |
| 5372 | remove_all_subclasses(PyTypeObject *type, PyObject *bases) |
| 5373 | { |
| 5374 | if (bases) { |
| 5375 | Py_ssize_t i; |
| 5376 | for (i = 0; i < PyTuple_GET_SIZE(bases); i++) { |
| 5377 | PyObject *base = PyTuple_GET_ITEM(bases, i); |
| 5378 | if (PyType_Check(base)) |
| 5379 | remove_subclass((PyTypeObject*) base, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5380 | } |
| 5381 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 5382 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5383 | |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 5384 | static int |
| 5385 | check_num_args(PyObject *ob, int n) |
| 5386 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5387 | if (!PyTuple_CheckExact(ob)) { |
| 5388 | PyErr_SetString(PyExc_SystemError, |
| 5389 | "PyArg_UnpackTuple() argument list is not a tuple"); |
| 5390 | return 0; |
| 5391 | } |
| 5392 | if (n == PyTuple_GET_SIZE(ob)) |
| 5393 | return 1; |
| 5394 | PyErr_Format( |
| 5395 | PyExc_TypeError, |
| 5396 | "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob)); |
| 5397 | return 0; |
Raymond Hettinger | 6a8bbdb | 2003-12-13 15:21:55 +0000 | [diff] [blame] | 5398 | } |
| 5399 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5400 | /* Generic wrappers for overloadable 'operators' such as __getitem__ */ |
| 5401 | |
| 5402 | /* There's a wrapper *function* for each distinct function typedef used |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5403 | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5404 | wrapper *table* for each distinct operation (e.g. __len__, __add__). |
| 5405 | Most tables have only one entry; the tables for binary operators have two |
| 5406 | entries, one regular and one with reversed arguments. */ |
| 5407 | |
| 5408 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5409 | wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5410 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5411 | lenfunc func = (lenfunc)wrapped; |
| 5412 | Py_ssize_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5413 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5414 | if (!check_num_args(args, 0)) |
| 5415 | return NULL; |
| 5416 | res = (*func)(self); |
| 5417 | if (res == -1 && PyErr_Occurred()) |
| 5418 | return NULL; |
| 5419 | return PyLong_FromLong((long)res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5420 | } |
| 5421 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5422 | static PyObject * |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 5423 | wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped) |
| 5424 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5425 | inquiry func = (inquiry)wrapped; |
| 5426 | int res; |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 5427 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5428 | if (!check_num_args(args, 0)) |
| 5429 | return NULL; |
| 5430 | res = (*func)(self); |
| 5431 | if (res == -1 && PyErr_Occurred()) |
| 5432 | return NULL; |
| 5433 | return PyBool_FromLong((long)res); |
Raymond Hettinger | f34f264 | 2003-10-11 17:29:04 +0000 | [diff] [blame] | 5434 | } |
| 5435 | |
| 5436 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5437 | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 5438 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5439 | binaryfunc func = (binaryfunc)wrapped; |
| 5440 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5441 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5442 | if (!check_num_args(args, 1)) |
| 5443 | return NULL; |
| 5444 | other = PyTuple_GET_ITEM(args, 0); |
| 5445 | return (*func)(self, other); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5446 | } |
| 5447 | |
| 5448 | static PyObject * |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 5449 | wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped) |
| 5450 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5451 | binaryfunc func = (binaryfunc)wrapped; |
| 5452 | PyObject *other; |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 5453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5454 | if (!check_num_args(args, 1)) |
| 5455 | return NULL; |
| 5456 | other = PyTuple_GET_ITEM(args, 0); |
| 5457 | return (*func)(self, other); |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 5458 | } |
| 5459 | |
| 5460 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5461 | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 5462 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5463 | binaryfunc func = (binaryfunc)wrapped; |
| 5464 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5466 | if (!check_num_args(args, 1)) |
| 5467 | return NULL; |
| 5468 | other = PyTuple_GET_ITEM(args, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5469 | return (*func)(other, self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5470 | } |
| 5471 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 5472 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5473 | wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 5474 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5475 | ternaryfunc func = (ternaryfunc)wrapped; |
| 5476 | PyObject *other; |
| 5477 | PyObject *third = Py_None; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5478 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5479 | /* Note: This wrapper only works for __pow__() */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5481 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
| 5482 | return NULL; |
| 5483 | return (*func)(self, other, third); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5484 | } |
| 5485 | |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 5486 | static PyObject * |
| 5487 | wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 5488 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5489 | ternaryfunc func = (ternaryfunc)wrapped; |
| 5490 | PyObject *other; |
| 5491 | PyObject *third = Py_None; |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 5492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5493 | /* Note: This wrapper only works for __pow__() */ |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 5494 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5495 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
| 5496 | return NULL; |
| 5497 | return (*func)(other, self, third); |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 5498 | } |
| 5499 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5500 | static PyObject * |
| 5501 | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 5502 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5503 | unaryfunc func = (unaryfunc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5505 | if (!check_num_args(args, 0)) |
| 5506 | return NULL; |
| 5507 | return (*func)(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5508 | } |
| 5509 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5510 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5511 | wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5512 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5513 | ssizeargfunc func = (ssizeargfunc)wrapped; |
| 5514 | PyObject* o; |
| 5515 | Py_ssize_t i; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5516 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5517 | if (!PyArg_UnpackTuple(args, "", 1, 1, &o)) |
| 5518 | return NULL; |
| 5519 | i = PyNumber_AsSsize_t(o, PyExc_OverflowError); |
| 5520 | if (i == -1 && PyErr_Occurred()) |
| 5521 | return NULL; |
| 5522 | return (*func)(self, i); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5523 | } |
| 5524 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5525 | static Py_ssize_t |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 5526 | getindex(PyObject *self, PyObject *arg) |
| 5527 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5528 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 5529 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5530 | i = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
| 5531 | if (i == -1 && PyErr_Occurred()) |
| 5532 | return -1; |
| 5533 | if (i < 0) { |
| 5534 | PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence; |
| 5535 | if (sq && sq->sq_length) { |
| 5536 | Py_ssize_t n = (*sq->sq_length)(self); |
Serhiy Storchaka | 813f943 | 2017-04-16 09:21:44 +0300 | [diff] [blame] | 5537 | if (n < 0) { |
| 5538 | assert(PyErr_Occurred()); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5539 | return -1; |
Serhiy Storchaka | 813f943 | 2017-04-16 09:21:44 +0300 | [diff] [blame] | 5540 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5541 | i += n; |
| 5542 | } |
| 5543 | } |
| 5544 | return i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 5545 | } |
| 5546 | |
| 5547 | static PyObject * |
| 5548 | wrap_sq_item(PyObject *self, PyObject *args, void *wrapped) |
| 5549 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5550 | ssizeargfunc func = (ssizeargfunc)wrapped; |
| 5551 | PyObject *arg; |
| 5552 | Py_ssize_t i; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 5553 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5554 | if (PyTuple_GET_SIZE(args) == 1) { |
| 5555 | arg = PyTuple_GET_ITEM(args, 0); |
| 5556 | i = getindex(self, arg); |
| 5557 | if (i == -1 && PyErr_Occurred()) |
| 5558 | return NULL; |
| 5559 | return (*func)(self, i); |
| 5560 | } |
| 5561 | check_num_args(args, 1); |
| 5562 | assert(PyErr_Occurred()); |
| 5563 | return NULL; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 5564 | } |
| 5565 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5566 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 5567 | wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5568 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5569 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 5570 | Py_ssize_t i; |
| 5571 | int res; |
| 5572 | PyObject *arg, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5573 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5574 | if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value)) |
| 5575 | return NULL; |
| 5576 | i = getindex(self, arg); |
| 5577 | if (i == -1 && PyErr_Occurred()) |
| 5578 | return NULL; |
| 5579 | res = (*func)(self, i, value); |
| 5580 | if (res == -1 && PyErr_Occurred()) |
| 5581 | return NULL; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 5582 | Py_RETURN_NONE; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5583 | } |
| 5584 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 5585 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 5586 | wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 5587 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5588 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
| 5589 | Py_ssize_t i; |
| 5590 | int res; |
| 5591 | PyObject *arg; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 5592 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5593 | if (!check_num_args(args, 1)) |
| 5594 | return NULL; |
| 5595 | arg = PyTuple_GET_ITEM(args, 0); |
| 5596 | i = getindex(self, arg); |
| 5597 | if (i == -1 && PyErr_Occurred()) |
| 5598 | return NULL; |
| 5599 | res = (*func)(self, i, NULL); |
| 5600 | if (res == -1 && PyErr_Occurred()) |
| 5601 | return NULL; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 5602 | Py_RETURN_NONE; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 5603 | } |
| 5604 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5605 | /* XXX objobjproc is a misnomer; should be objargpred */ |
| 5606 | static PyObject * |
| 5607 | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) |
| 5608 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5609 | objobjproc func = (objobjproc)wrapped; |
| 5610 | int res; |
| 5611 | PyObject *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5612 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5613 | if (!check_num_args(args, 1)) |
| 5614 | return NULL; |
| 5615 | value = PyTuple_GET_ITEM(args, 0); |
| 5616 | res = (*func)(self, value); |
| 5617 | if (res == -1 && PyErr_Occurred()) |
| 5618 | return NULL; |
| 5619 | else |
| 5620 | return PyBool_FromLong(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5621 | } |
| 5622 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5623 | static PyObject * |
| 5624 | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 5625 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5626 | objobjargproc func = (objobjargproc)wrapped; |
| 5627 | int res; |
| 5628 | PyObject *key, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5629 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5630 | if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value)) |
| 5631 | return NULL; |
| 5632 | res = (*func)(self, key, value); |
| 5633 | if (res == -1 && PyErr_Occurred()) |
| 5634 | return NULL; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 5635 | Py_RETURN_NONE; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5636 | } |
| 5637 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 5638 | static PyObject * |
| 5639 | wrap_delitem(PyObject *self, PyObject *args, void *wrapped) |
| 5640 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5641 | objobjargproc func = (objobjargproc)wrapped; |
| 5642 | int res; |
| 5643 | PyObject *key; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 5644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5645 | if (!check_num_args(args, 1)) |
| 5646 | return NULL; |
| 5647 | key = PyTuple_GET_ITEM(args, 0); |
| 5648 | res = (*func)(self, key, NULL); |
| 5649 | if (res == -1 && PyErr_Occurred()) |
| 5650 | return NULL; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 5651 | Py_RETURN_NONE; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 5652 | } |
| 5653 | |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 5654 | /* 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] | 5655 | This is called the Carlo Verre hack after its discoverer. */ |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 5656 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 5657 | hackcheck(PyObject *self, setattrofunc func, const char *what) |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 5658 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5659 | PyTypeObject *type = Py_TYPE(self); |
| 5660 | while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 5661 | type = type->tp_base; |
| 5662 | /* If type is NULL now, this is a really weird type. |
| 5663 | In the spirit of backwards compatibility (?), just shut up. */ |
| 5664 | if (type && type->tp_setattro != func) { |
| 5665 | PyErr_Format(PyExc_TypeError, |
| 5666 | "can't apply this %s to %s object", |
| 5667 | what, |
| 5668 | type->tp_name); |
| 5669 | return 0; |
| 5670 | } |
| 5671 | return 1; |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 5672 | } |
| 5673 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5674 | static PyObject * |
| 5675 | wrap_setattr(PyObject *self, PyObject *args, void *wrapped) |
| 5676 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5677 | setattrofunc func = (setattrofunc)wrapped; |
| 5678 | int res; |
| 5679 | PyObject *name, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5681 | if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value)) |
| 5682 | return NULL; |
| 5683 | if (!hackcheck(self, func, "__setattr__")) |
| 5684 | return NULL; |
| 5685 | res = (*func)(self, name, value); |
| 5686 | if (res < 0) |
| 5687 | return NULL; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 5688 | Py_RETURN_NONE; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5689 | } |
| 5690 | |
| 5691 | static PyObject * |
| 5692 | wrap_delattr(PyObject *self, PyObject *args, void *wrapped) |
| 5693 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5694 | setattrofunc func = (setattrofunc)wrapped; |
| 5695 | int res; |
| 5696 | PyObject *name; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5697 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5698 | if (!check_num_args(args, 1)) |
| 5699 | return NULL; |
| 5700 | name = PyTuple_GET_ITEM(args, 0); |
| 5701 | if (!hackcheck(self, func, "__delattr__")) |
| 5702 | return NULL; |
| 5703 | res = (*func)(self, name, NULL); |
| 5704 | if (res < 0) |
| 5705 | return NULL; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 5706 | Py_RETURN_NONE; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5707 | } |
| 5708 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5709 | static PyObject * |
| 5710 | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) |
| 5711 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5712 | hashfunc func = (hashfunc)wrapped; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 5713 | Py_hash_t res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5714 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5715 | if (!check_num_args(args, 0)) |
| 5716 | return NULL; |
| 5717 | res = (*func)(self); |
| 5718 | if (res == -1 && PyErr_Occurred()) |
| 5719 | return NULL; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 5720 | return PyLong_FromSsize_t(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5721 | } |
| 5722 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5723 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5724 | wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5725 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5726 | ternaryfunc func = (ternaryfunc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5727 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5728 | return (*func)(self, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5729 | } |
| 5730 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5731 | static PyObject * |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 5732 | wrap_del(PyObject *self, PyObject *args, void *wrapped) |
| 5733 | { |
| 5734 | destructor func = (destructor)wrapped; |
| 5735 | |
| 5736 | if (!check_num_args(args, 0)) |
| 5737 | return NULL; |
| 5738 | |
| 5739 | (*func)(self); |
| 5740 | Py_RETURN_NONE; |
| 5741 | } |
| 5742 | |
| 5743 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5744 | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) |
| 5745 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5746 | richcmpfunc func = (richcmpfunc)wrapped; |
| 5747 | PyObject *other; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5748 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5749 | if (!check_num_args(args, 1)) |
| 5750 | return NULL; |
| 5751 | other = PyTuple_GET_ITEM(args, 0); |
| 5752 | return (*func)(self, other, op); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5753 | } |
| 5754 | |
| 5755 | #undef RICHCMP_WRAPPER |
| 5756 | #define RICHCMP_WRAPPER(NAME, OP) \ |
| 5757 | static PyObject * \ |
| 5758 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ |
| 5759 | { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5760 | return wrap_richcmpfunc(self, args, wrapped, OP); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5761 | } |
| 5762 | |
Jack Jansen | 8e938b4 | 2001-08-08 15:29:49 +0000 | [diff] [blame] | 5763 | RICHCMP_WRAPPER(lt, Py_LT) |
| 5764 | RICHCMP_WRAPPER(le, Py_LE) |
| 5765 | RICHCMP_WRAPPER(eq, Py_EQ) |
| 5766 | RICHCMP_WRAPPER(ne, Py_NE) |
| 5767 | RICHCMP_WRAPPER(gt, Py_GT) |
| 5768 | RICHCMP_WRAPPER(ge, Py_GE) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5769 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5770 | static PyObject * |
| 5771 | wrap_next(PyObject *self, PyObject *args, void *wrapped) |
| 5772 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5773 | unaryfunc func = (unaryfunc)wrapped; |
| 5774 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5775 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5776 | if (!check_num_args(args, 0)) |
| 5777 | return NULL; |
| 5778 | res = (*func)(self); |
| 5779 | if (res == NULL && !PyErr_Occurred()) |
| 5780 | PyErr_SetNone(PyExc_StopIteration); |
| 5781 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5782 | } |
| 5783 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5784 | static PyObject * |
| 5785 | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped) |
| 5786 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5787 | descrgetfunc func = (descrgetfunc)wrapped; |
| 5788 | PyObject *obj; |
| 5789 | PyObject *type = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5790 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5791 | if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type)) |
| 5792 | return NULL; |
| 5793 | if (obj == Py_None) |
| 5794 | obj = NULL; |
| 5795 | if (type == Py_None) |
| 5796 | type = NULL; |
| 5797 | if (type == NULL &&obj == NULL) { |
| 5798 | PyErr_SetString(PyExc_TypeError, |
| 5799 | "__get__(None, None) is invalid"); |
| 5800 | return NULL; |
| 5801 | } |
| 5802 | return (*func)(self, obj, type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5803 | } |
| 5804 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5805 | static PyObject * |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 5806 | wrap_descr_set(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5807 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5808 | descrsetfunc func = (descrsetfunc)wrapped; |
| 5809 | PyObject *obj, *value; |
| 5810 | int ret; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5811 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5812 | if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value)) |
| 5813 | return NULL; |
| 5814 | ret = (*func)(self, obj, value); |
| 5815 | if (ret < 0) |
| 5816 | return NULL; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 5817 | Py_RETURN_NONE; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5818 | } |
Guido van Rossum | 22b1387 | 2002-08-06 21:41:44 +0000 | [diff] [blame] | 5819 | |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 5820 | static PyObject * |
| 5821 | wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped) |
| 5822 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5823 | descrsetfunc func = (descrsetfunc)wrapped; |
| 5824 | PyObject *obj; |
| 5825 | int ret; |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 5826 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5827 | if (!check_num_args(args, 1)) |
| 5828 | return NULL; |
| 5829 | obj = PyTuple_GET_ITEM(args, 0); |
| 5830 | ret = (*func)(self, obj, NULL); |
| 5831 | if (ret < 0) |
| 5832 | return NULL; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 5833 | Py_RETURN_NONE; |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 5834 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5835 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5836 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 5837 | wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5838 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5839 | initproc func = (initproc)wrapped; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5840 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5841 | if (func(self, args, kwds) < 0) |
| 5842 | return NULL; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 5843 | Py_RETURN_NONE; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5844 | } |
| 5845 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5846 | static PyObject * |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 5847 | tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5848 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5849 | PyTypeObject *type, *subtype, *staticbase; |
| 5850 | PyObject *arg0, *res; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 5851 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5852 | if (self == NULL || !PyType_Check(self)) |
| 5853 | Py_FatalError("__new__() called with non-type 'self'"); |
| 5854 | type = (PyTypeObject *)self; |
| 5855 | if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) { |
| 5856 | PyErr_Format(PyExc_TypeError, |
| 5857 | "%s.__new__(): not enough arguments", |
| 5858 | type->tp_name); |
| 5859 | return NULL; |
| 5860 | } |
| 5861 | arg0 = PyTuple_GET_ITEM(args, 0); |
| 5862 | if (!PyType_Check(arg0)) { |
| 5863 | PyErr_Format(PyExc_TypeError, |
| 5864 | "%s.__new__(X): X is not a type object (%s)", |
| 5865 | type->tp_name, |
| 5866 | Py_TYPE(arg0)->tp_name); |
| 5867 | return NULL; |
| 5868 | } |
| 5869 | subtype = (PyTypeObject *)arg0; |
| 5870 | if (!PyType_IsSubtype(subtype, type)) { |
| 5871 | PyErr_Format(PyExc_TypeError, |
| 5872 | "%s.__new__(%s): %s is not a subtype of %s", |
| 5873 | type->tp_name, |
| 5874 | subtype->tp_name, |
| 5875 | subtype->tp_name, |
| 5876 | type->tp_name); |
| 5877 | return NULL; |
| 5878 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 5879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5880 | /* Check that the use doesn't do something silly and unsafe like |
| 5881 | object.__new__(dict). To do this, we check that the |
| 5882 | most derived base that's not a heap type is this type. */ |
| 5883 | staticbase = subtype; |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 5884 | while (staticbase && (staticbase->tp_new == slot_tp_new)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5885 | staticbase = staticbase->tp_base; |
| 5886 | /* If staticbase is NULL now, it is a really weird type. |
| 5887 | In the spirit of backwards compatibility (?), just shut up. */ |
| 5888 | if (staticbase && staticbase->tp_new != type->tp_new) { |
| 5889 | PyErr_Format(PyExc_TypeError, |
| 5890 | "%s.__new__(%s) is not safe, use %s.__new__()", |
| 5891 | type->tp_name, |
| 5892 | subtype->tp_name, |
Benjamin Peterson | e823933 | 2014-11-26 23:03:11 -0600 | [diff] [blame] | 5893 | staticbase->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5894 | return NULL; |
| 5895 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 5896 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5897 | args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 5898 | if (args == NULL) |
| 5899 | return NULL; |
| 5900 | res = type->tp_new(subtype, args, kwds); |
| 5901 | Py_DECREF(args); |
| 5902 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5903 | } |
| 5904 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 5905 | static struct PyMethodDef tp_new_methoddef[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5906 | {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 5907 | PyDoc_STR("__new__($type, *args, **kwargs)\n--\n\n" |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 5908 | "Create and return a new object. " |
| 5909 | "See help(type) for accurate signature.")}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5910 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5911 | }; |
| 5912 | |
| 5913 | static int |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 5914 | add_tp_new_wrapper(PyTypeObject *type) |
| 5915 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5916 | PyObject *func; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 5917 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5918 | if (_PyDict_GetItemId(type->tp_dict, &PyId___new__) != NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5919 | return 0; |
Andrew Svetlov | 3ba3a3e | 2012-12-25 13:32:35 +0200 | [diff] [blame] | 5920 | func = PyCFunction_NewEx(tp_new_methoddef, (PyObject *)type, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5921 | if (func == NULL) |
| 5922 | return -1; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5923 | if (_PyDict_SetItemId(type->tp_dict, &PyId___new__, func)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5924 | Py_DECREF(func); |
| 5925 | return -1; |
| 5926 | } |
| 5927 | Py_DECREF(func); |
| 5928 | return 0; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 5929 | } |
| 5930 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 5931 | /* Slot wrappers that call the corresponding __foo__ slot. See comments |
| 5932 | below at override_slots() for more explanation. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5933 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5934 | #define SLOT0(FUNCNAME, OPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5935 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5936 | FUNCNAME(PyObject *self) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5937 | { \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5938 | _Py_static_string(id, OPSTR); \ |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 5939 | return call_method(self, &id, NULL, 0); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5940 | } |
| 5941 | |
Victor Stinner | 887b430 | 2016-12-09 00:41:46 +0100 | [diff] [blame] | 5942 | #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5943 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5944 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5945 | { \ |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 5946 | PyObject* stack[1] = {arg1}; \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5947 | _Py_static_string(id, OPSTR); \ |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 5948 | return call_method(self, &id, stack, 1); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5949 | } |
| 5950 | |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 5951 | /* Boolean helper for SLOT1BINFULL(). |
| 5952 | right.__class__ is a nontrivial subclass of left.__class__. */ |
| 5953 | static int |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5954 | method_is_overloaded(PyObject *left, PyObject *right, struct _Py_Identifier *name) |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 5955 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5956 | PyObject *a, *b; |
| 5957 | int ok; |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 5958 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5959 | b = _PyObject_GetAttrId((PyObject *)(Py_TYPE(right)), name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5960 | if (b == NULL) { |
| 5961 | PyErr_Clear(); |
| 5962 | /* If right doesn't have it, it's not overloaded */ |
| 5963 | return 0; |
| 5964 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 5965 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 5966 | a = _PyObject_GetAttrId((PyObject *)(Py_TYPE(left)), name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5967 | if (a == NULL) { |
| 5968 | PyErr_Clear(); |
| 5969 | Py_DECREF(b); |
| 5970 | /* If right has it but left doesn't, it's overloaded */ |
| 5971 | return 1; |
| 5972 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 5973 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5974 | ok = PyObject_RichCompareBool(a, b, Py_NE); |
| 5975 | Py_DECREF(a); |
| 5976 | Py_DECREF(b); |
| 5977 | if (ok < 0) { |
| 5978 | PyErr_Clear(); |
| 5979 | return 0; |
| 5980 | } |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 5981 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5982 | return ok; |
Guido van Rossum | cd11880 | 2003-01-06 22:57:47 +0000 | [diff] [blame] | 5983 | } |
| 5984 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5985 | |
| 5986 | #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5987 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 5988 | FUNCNAME(PyObject *self, PyObject *other) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5989 | { \ |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 5990 | PyObject* stack[1]; \ |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5991 | _Py_static_string(op_id, OPSTR); \ |
| 5992 | _Py_static_string(rop_id, ROPSTR); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5993 | int do_other = Py_TYPE(self) != Py_TYPE(other) && \ |
| 5994 | Py_TYPE(other)->tp_as_number != NULL && \ |
| 5995 | Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \ |
| 5996 | if (Py_TYPE(self)->tp_as_number != NULL && \ |
| 5997 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ |
| 5998 | PyObject *r; \ |
| 5999 | if (do_other && \ |
| 6000 | PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 6001 | method_is_overloaded(self, other, &rop_id)) { \ |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 6002 | stack[0] = self; \ |
| 6003 | r = call_maybe(other, &rop_id, stack, 1); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6004 | if (r != Py_NotImplemented) \ |
| 6005 | return r; \ |
| 6006 | Py_DECREF(r); \ |
| 6007 | do_other = 0; \ |
| 6008 | } \ |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 6009 | stack[0] = other; \ |
| 6010 | r = call_maybe(self, &op_id, stack, 1); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6011 | if (r != Py_NotImplemented || \ |
| 6012 | Py_TYPE(other) == Py_TYPE(self)) \ |
| 6013 | return r; \ |
| 6014 | Py_DECREF(r); \ |
| 6015 | } \ |
| 6016 | if (do_other) { \ |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 6017 | stack[0] = self; \ |
| 6018 | return call_maybe(other, &rop_id, stack, 1); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6019 | } \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 6020 | Py_RETURN_NOTIMPLEMENTED; \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 6021 | } |
| 6022 | |
| 6023 | #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6024 | SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR) |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 6025 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6026 | static Py_ssize_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6027 | slot_sq_length(PyObject *self) |
| 6028 | { |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 6029 | PyObject *res = call_method(self, &PyId___len__, NULL, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6030 | Py_ssize_t len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6032 | if (res == NULL) |
| 6033 | return -1; |
Serhiy Storchaka | baf9f29 | 2017-04-16 09:37:18 +0300 | [diff] [blame] | 6034 | |
| 6035 | Py_SETREF(res, PyNumber_Index(res)); |
| 6036 | if (res == NULL) |
| 6037 | return -1; |
| 6038 | |
| 6039 | assert(PyLong_Check(res)); |
| 6040 | if (Py_SIZE(res) < 0) { |
Xiang Zhang | 8e1ddbd | 2017-04-17 00:54:21 +0800 | [diff] [blame] | 6041 | Py_DECREF(res); |
Serhiy Storchaka | baf9f29 | 2017-04-16 09:37:18 +0300 | [diff] [blame] | 6042 | PyErr_SetString(PyExc_ValueError, |
| 6043 | "__len__() should return >= 0"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6044 | return -1; |
| 6045 | } |
Serhiy Storchaka | baf9f29 | 2017-04-16 09:37:18 +0300 | [diff] [blame] | 6046 | |
| 6047 | len = PyNumber_AsSsize_t(res, PyExc_OverflowError); |
| 6048 | assert(len >= 0 || PyErr_ExceptionMatches(PyExc_OverflowError)); |
| 6049 | Py_DECREF(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6050 | return len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6051 | } |
| 6052 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 6053 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6054 | slot_sq_item(PyObject *self, Py_ssize_t i) |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 6055 | { |
Serhiy Storchaka | 4e624ca | 2017-06-01 08:18:25 +0300 | [diff] [blame] | 6056 | PyObject *retval; |
| 6057 | PyObject *args[1]; |
| 6058 | PyObject *ival = PyLong_FromSsize_t(i); |
| 6059 | if (ival == NULL) { |
Victor Stinner | 018016d | 2016-08-19 18:17:37 +0200 | [diff] [blame] | 6060 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6061 | } |
Serhiy Storchaka | 4e624ca | 2017-06-01 08:18:25 +0300 | [diff] [blame] | 6062 | args[0] = ival; |
| 6063 | retval = call_method(self, &PyId___getitem__, args, 1); |
Victor Stinner | 5e87749 | 2016-08-19 18:19:42 +0200 | [diff] [blame] | 6064 | Py_DECREF(ival); |
Victor Stinner | 018016d | 2016-08-19 18:17:37 +0200 | [diff] [blame] | 6065 | return retval; |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 6066 | } |
| 6067 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6068 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6069 | slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6070 | { |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 6071 | PyObject *stack[2]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6072 | PyObject *res; |
Victor Stinner | 887b430 | 2016-12-09 00:41:46 +0100 | [diff] [blame] | 6073 | PyObject *index_obj; |
| 6074 | |
| 6075 | index_obj = PyLong_FromSsize_t(index); |
| 6076 | if (index_obj == NULL) { |
| 6077 | return -1; |
| 6078 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6079 | |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 6080 | stack[0] = index_obj; |
| 6081 | if (value == NULL) { |
| 6082 | res = call_method(self, &PyId___delitem__, stack, 1); |
| 6083 | } |
| 6084 | else { |
| 6085 | stack[1] = value; |
| 6086 | res = call_method(self, &PyId___setitem__, stack, 2); |
| 6087 | } |
Victor Stinner | 887b430 | 2016-12-09 00:41:46 +0100 | [diff] [blame] | 6088 | Py_DECREF(index_obj); |
| 6089 | |
| 6090 | if (res == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6091 | return -1; |
Victor Stinner | 887b430 | 2016-12-09 00:41:46 +0100 | [diff] [blame] | 6092 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6093 | Py_DECREF(res); |
| 6094 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6095 | } |
| 6096 | |
| 6097 | static int |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6098 | slot_sq_contains(PyObject *self, PyObject *value) |
| 6099 | { |
Victor Stinner | a7720f6 | 2016-08-19 17:48:51 +0200 | [diff] [blame] | 6100 | PyObject *func, *res; |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6101 | int result = -1, unbound; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 6102 | _Py_IDENTIFIER(__contains__); |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 6103 | |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6104 | func = lookup_maybe_method(self, &PyId___contains__, &unbound); |
Guido van Rossum | 97c1adf | 2016-08-18 09:22:23 -0700 | [diff] [blame] | 6105 | if (func == Py_None) { |
| 6106 | Py_DECREF(func); |
| 6107 | PyErr_Format(PyExc_TypeError, |
| 6108 | "'%.200s' object is not a container", |
| 6109 | Py_TYPE(self)->tp_name); |
| 6110 | return -1; |
| 6111 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6112 | if (func != NULL) { |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6113 | PyObject *args[1] = {value}; |
| 6114 | res = call_unbound(unbound, func, self, args, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6115 | Py_DECREF(func); |
| 6116 | if (res != NULL) { |
| 6117 | result = PyObject_IsTrue(res); |
| 6118 | Py_DECREF(res); |
| 6119 | } |
| 6120 | } |
| 6121 | else if (! PyErr_Occurred()) { |
| 6122 | /* Possible results: -1 and 1 */ |
| 6123 | result = (int)_PySequence_IterSearch(self, value, |
| 6124 | PY_ITERSEARCH_CONTAINS); |
| 6125 | } |
| 6126 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6127 | } |
| 6128 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6129 | #define slot_mp_length slot_sq_length |
| 6130 | |
Victor Stinner | 887b430 | 2016-12-09 00:41:46 +0100 | [diff] [blame] | 6131 | SLOT1(slot_mp_subscript, "__getitem__", PyObject *) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6132 | |
| 6133 | static int |
| 6134 | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) |
| 6135 | { |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 6136 | PyObject *stack[2]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6137 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6138 | |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 6139 | stack[0] = key; |
| 6140 | if (value == NULL) { |
| 6141 | res = call_method(self, &PyId___delitem__, stack, 1); |
| 6142 | } |
| 6143 | else { |
| 6144 | stack[1] = value; |
| 6145 | res = call_method(self, &PyId___setitem__, stack, 2); |
| 6146 | } |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 6147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6148 | if (res == NULL) |
| 6149 | return -1; |
| 6150 | Py_DECREF(res); |
| 6151 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6152 | } |
| 6153 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 6154 | SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__") |
| 6155 | SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__") |
| 6156 | SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__") |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 6157 | SLOT1BIN(slot_nb_matrix_multiply, nb_matrix_multiply, "__matmul__", "__rmatmul__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 6158 | SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__") |
| 6159 | SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__") |
| 6160 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 6161 | static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 6162 | |
| 6163 | SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6164 | nb_power, "__pow__", "__rpow__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 6165 | |
| 6166 | static PyObject * |
| 6167 | slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) |
| 6168 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 6169 | _Py_IDENTIFIER(__pow__); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 6170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6171 | if (modulus == Py_None) |
| 6172 | return slot_nb_power_binary(self, other); |
| 6173 | /* Three-arg power doesn't use __rpow__. But ternary_op |
| 6174 | can call this when the second argument's type uses |
| 6175 | slot_nb_power, so check before calling self.__pow__. */ |
| 6176 | if (Py_TYPE(self)->tp_as_number != NULL && |
| 6177 | Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) { |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 6178 | PyObject* stack[2] = {other, modulus}; |
| 6179 | return call_method(self, &PyId___pow__, stack, 2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6180 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 6181 | Py_RETURN_NOTIMPLEMENTED; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 6182 | } |
| 6183 | |
| 6184 | SLOT0(slot_nb_negative, "__neg__") |
| 6185 | SLOT0(slot_nb_positive, "__pos__") |
| 6186 | SLOT0(slot_nb_absolute, "__abs__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6187 | |
| 6188 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 6189 | slot_nb_bool(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6190 | { |
Victor Stinner | 20a3007 | 2016-08-19 18:28:25 +0200 | [diff] [blame] | 6191 | PyObject *func, *value; |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6192 | int result, unbound; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6193 | int using_len = 0; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 6194 | _Py_IDENTIFIER(__bool__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6195 | |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6196 | func = lookup_maybe_method(self, &PyId___bool__, &unbound); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6197 | if (func == NULL) { |
Victor Stinner | a12eec4 | 2016-08-19 18:26:05 +0200 | [diff] [blame] | 6198 | if (PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6199 | return -1; |
Victor Stinner | a12eec4 | 2016-08-19 18:26:05 +0200 | [diff] [blame] | 6200 | } |
| 6201 | |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6202 | func = lookup_maybe_method(self, &PyId___len__, &unbound); |
Victor Stinner | a12eec4 | 2016-08-19 18:26:05 +0200 | [diff] [blame] | 6203 | if (func == NULL) { |
| 6204 | if (PyErr_Occurred()) { |
| 6205 | return -1; |
| 6206 | } |
| 6207 | return 1; |
| 6208 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6209 | using_len = 1; |
| 6210 | } |
Victor Stinner | a12eec4 | 2016-08-19 18:26:05 +0200 | [diff] [blame] | 6211 | |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6212 | value = call_unbound_noarg(unbound, func, self); |
Victor Stinner | a12eec4 | 2016-08-19 18:26:05 +0200 | [diff] [blame] | 6213 | if (value == NULL) { |
| 6214 | goto error; |
| 6215 | } |
| 6216 | |
| 6217 | if (using_len) { |
| 6218 | /* bool type enforced by slot_nb_len */ |
| 6219 | result = PyObject_IsTrue(value); |
| 6220 | } |
| 6221 | else if (PyBool_Check(value)) { |
| 6222 | result = PyObject_IsTrue(value); |
| 6223 | } |
| 6224 | else { |
| 6225 | PyErr_Format(PyExc_TypeError, |
| 6226 | "__bool__ should return " |
| 6227 | "bool, returned %s", |
| 6228 | Py_TYPE(value)->tp_name); |
| 6229 | result = -1; |
| 6230 | } |
| 6231 | |
| 6232 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6233 | Py_DECREF(func); |
| 6234 | return result; |
Victor Stinner | a12eec4 | 2016-08-19 18:26:05 +0200 | [diff] [blame] | 6235 | |
| 6236 | error: |
| 6237 | Py_DECREF(func); |
| 6238 | return -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6239 | } |
| 6240 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 6241 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6242 | static PyObject * |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 6243 | slot_nb_index(PyObject *self) |
| 6244 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 6245 | _Py_IDENTIFIER(__index__); |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 6246 | return call_method(self, &PyId___index__, NULL, 0); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 6247 | } |
| 6248 | |
| 6249 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 6250 | SLOT0(slot_nb_invert, "__invert__") |
| 6251 | SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") |
| 6252 | SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") |
| 6253 | SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") |
| 6254 | SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") |
| 6255 | SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 6256 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 6257 | SLOT0(slot_nb_int, "__int__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 6258 | SLOT0(slot_nb_float, "__float__") |
Victor Stinner | 887b430 | 2016-12-09 00:41:46 +0100 | [diff] [blame] | 6259 | SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *) |
| 6260 | SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *) |
| 6261 | SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *) |
| 6262 | SLOT1(slot_nb_inplace_matrix_multiply, "__imatmul__", PyObject *) |
| 6263 | SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6264 | /* Can't use SLOT1 here, because nb_inplace_power is ternary */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6265 | static PyObject * |
| 6266 | slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2) |
| 6267 | { |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 6268 | PyObject *stack[1] = {arg1}; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 6269 | _Py_IDENTIFIER(__ipow__); |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 6270 | return call_method(self, &PyId___ipow__, stack, 1); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6271 | } |
Victor Stinner | 887b430 | 2016-12-09 00:41:46 +0100 | [diff] [blame] | 6272 | SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *) |
| 6273 | SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *) |
| 6274 | SLOT1(slot_nb_inplace_and, "__iand__", PyObject *) |
| 6275 | SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *) |
| 6276 | SLOT1(slot_nb_inplace_or, "__ior__", PyObject *) |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 6277 | SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6278 | "__floordiv__", "__rfloordiv__") |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 6279 | SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__") |
Victor Stinner | 887b430 | 2016-12-09 00:41:46 +0100 | [diff] [blame] | 6280 | SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *) |
| 6281 | SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6282 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 6283 | static PyObject * |
| 6284 | slot_tp_repr(PyObject *self) |
| 6285 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6286 | PyObject *func, *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 6287 | _Py_IDENTIFIER(__repr__); |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6288 | int unbound; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 6289 | |
Serhiy Storchaka | 4e624ca | 2017-06-01 08:18:25 +0300 | [diff] [blame] | 6290 | func = lookup_maybe_method(self, &PyId___repr__, &unbound); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6291 | if (func != NULL) { |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6292 | res = call_unbound_noarg(unbound, func, self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6293 | Py_DECREF(func); |
| 6294 | return res; |
| 6295 | } |
| 6296 | PyErr_Clear(); |
| 6297 | return PyUnicode_FromFormat("<%s object at %p>", |
| 6298 | Py_TYPE(self)->tp_name, self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 6299 | } |
| 6300 | |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6301 | SLOT0(slot_tp_str, "__str__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6302 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 6303 | static Py_hash_t |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6304 | slot_tp_hash(PyObject *self) |
| 6305 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6306 | PyObject *func, *res; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 6307 | Py_ssize_t h; |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6308 | int unbound; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6309 | |
Serhiy Storchaka | 4e624ca | 2017-06-01 08:18:25 +0300 | [diff] [blame] | 6310 | func = lookup_maybe_method(self, &PyId___hash__, &unbound); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 6311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6312 | if (func == Py_None) { |
| 6313 | Py_DECREF(func); |
| 6314 | func = NULL; |
| 6315 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 6316 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6317 | if (func == NULL) { |
| 6318 | return PyObject_HashNotImplemented(self); |
| 6319 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 6320 | |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6321 | res = call_unbound_noarg(unbound, func, self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6322 | Py_DECREF(func); |
| 6323 | if (res == NULL) |
| 6324 | return -1; |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 6325 | |
| 6326 | if (!PyLong_Check(res)) { |
| 6327 | PyErr_SetString(PyExc_TypeError, |
| 6328 | "__hash__ method should return an integer"); |
| 6329 | return -1; |
| 6330 | } |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 6331 | /* Transform the PyLong `res` to a Py_hash_t `h`. For an existing |
| 6332 | hashable Python object x, hash(x) will always lie within the range of |
| 6333 | Py_hash_t. Therefore our transformation must preserve values that |
| 6334 | already lie within this range, to ensure that if x.__hash__() returns |
| 6335 | hash(y) then hash(x) == hash(y). */ |
| 6336 | h = PyLong_AsSsize_t(res); |
| 6337 | if (h == -1 && PyErr_Occurred()) { |
| 6338 | /* 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] | 6339 | use any sufficiently bit-mixing transformation; |
| 6340 | long.__hash__ will do nicely. */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 6341 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6342 | h = PyLong_Type.tp_hash(res); |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 6343 | } |
Benjamin Peterson | e7dfeeb | 2010-10-17 21:27:01 +0000 | [diff] [blame] | 6344 | /* -1 is reserved for errors. */ |
| 6345 | if (h == -1) |
| 6346 | h = -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6347 | Py_DECREF(res); |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 6348 | return h; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6349 | } |
| 6350 | |
| 6351 | static PyObject * |
| 6352 | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 6353 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 6354 | _Py_IDENTIFIER(__call__); |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6355 | int unbound; |
| 6356 | PyObject *meth = lookup_method(self, &PyId___call__, &unbound); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6357 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6358 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6359 | if (meth == NULL) |
| 6360 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6361 | |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6362 | if (unbound) { |
| 6363 | res = _PyObject_Call_Prepend(meth, self, args, kwds); |
| 6364 | } |
| 6365 | else { |
| 6366 | res = PyObject_Call(meth, args, kwds); |
| 6367 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6369 | Py_DECREF(meth); |
| 6370 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6371 | } |
| 6372 | |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 6373 | /* There are two slot dispatch functions for tp_getattro. |
| 6374 | |
| 6375 | - slot_tp_getattro() is used when __getattribute__ is overridden |
| 6376 | but no __getattr__ hook is present; |
| 6377 | |
| 6378 | - slot_tp_getattr_hook() is used when a __getattr__ hook is present. |
| 6379 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6380 | The code in update_one_slot() always installs slot_tp_getattr_hook(); this |
| 6381 | detects the absence of __getattr__ and then installs the simpler slot if |
| 6382 | necessary. */ |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 6383 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6384 | static PyObject * |
| 6385 | slot_tp_getattro(PyObject *self, PyObject *name) |
| 6386 | { |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 6387 | PyObject *stack[1] = {name}; |
| 6388 | return call_method(self, &PyId___getattribute__, stack, 1); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6389 | } |
| 6390 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 6391 | static PyObject * |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 6392 | call_attribute(PyObject *self, PyObject *attr, PyObject *name) |
| 6393 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6394 | PyObject *res, *descr = NULL; |
| 6395 | descrgetfunc f = Py_TYPE(attr)->tp_descr_get; |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 6396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6397 | if (f != NULL) { |
| 6398 | descr = f(attr, self, (PyObject *)(Py_TYPE(self))); |
| 6399 | if (descr == NULL) |
| 6400 | return NULL; |
| 6401 | else |
| 6402 | attr = descr; |
| 6403 | } |
Victor Stinner | de4ae3d | 2016-12-04 22:59:09 +0100 | [diff] [blame] | 6404 | res = PyObject_CallFunctionObjArgs(attr, name, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6405 | Py_XDECREF(descr); |
| 6406 | return res; |
Benjamin Peterson | 9262b84 | 2008-11-17 22:45:50 +0000 | [diff] [blame] | 6407 | } |
| 6408 | |
| 6409 | static PyObject * |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 6410 | slot_tp_getattr_hook(PyObject *self, PyObject *name) |
| 6411 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6412 | PyTypeObject *tp = Py_TYPE(self); |
| 6413 | PyObject *getattr, *getattribute, *res; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 6414 | _Py_IDENTIFIER(__getattr__); |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 6415 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6416 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 6417 | method fully for each attribute lookup for classes with |
| 6418 | __getattr__, even when the attribute is present. So we use |
| 6419 | _PyType_Lookup and create the method only when needed, with |
| 6420 | call_attribute. */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 6421 | getattr = _PyType_LookupId(tp, &PyId___getattr__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6422 | if (getattr == NULL) { |
| 6423 | /* No __getattr__ hook: use a simpler dispatcher */ |
| 6424 | tp->tp_getattro = slot_tp_getattro; |
| 6425 | return slot_tp_getattro(self, name); |
| 6426 | } |
| 6427 | Py_INCREF(getattr); |
| 6428 | /* speed hack: we could use lookup_maybe, but that would resolve the |
| 6429 | method fully for each attribute lookup for classes with |
| 6430 | __getattr__, even when self has the default __getattribute__ |
| 6431 | method. So we use _PyType_Lookup and create the method only when |
| 6432 | needed, with call_attribute. */ |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 6433 | getattribute = _PyType_LookupId(tp, &PyId___getattribute__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6434 | if (getattribute == NULL || |
| 6435 | (Py_TYPE(getattribute) == &PyWrapperDescr_Type && |
| 6436 | ((PyWrapperDescrObject *)getattribute)->d_wrapped == |
| 6437 | (void *)PyObject_GenericGetAttr)) |
| 6438 | res = PyObject_GenericGetAttr(self, name); |
| 6439 | else { |
| 6440 | Py_INCREF(getattribute); |
| 6441 | res = call_attribute(self, getattribute, name); |
| 6442 | Py_DECREF(getattribute); |
| 6443 | } |
| 6444 | if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 6445 | PyErr_Clear(); |
| 6446 | res = call_attribute(self, getattr, name); |
| 6447 | } |
| 6448 | Py_DECREF(getattr); |
| 6449 | return res; |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 6450 | } |
| 6451 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6452 | static int |
| 6453 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) |
| 6454 | { |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 6455 | PyObject *stack[2]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6456 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 6457 | _Py_IDENTIFIER(__delattr__); |
| 6458 | _Py_IDENTIFIER(__setattr__); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6459 | |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 6460 | stack[0] = name; |
| 6461 | if (value == NULL) { |
| 6462 | res = call_method(self, &PyId___delattr__, stack, 1); |
| 6463 | } |
| 6464 | else { |
| 6465 | stack[1] = value; |
| 6466 | res = call_method(self, &PyId___setattr__, stack, 2); |
| 6467 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6468 | if (res == NULL) |
| 6469 | return -1; |
| 6470 | Py_DECREF(res); |
| 6471 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6472 | } |
| 6473 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 6474 | static _Py_Identifier name_op[] = { |
| 6475 | {0, "__lt__", 0}, |
| 6476 | {0, "__le__", 0}, |
| 6477 | {0, "__eq__", 0}, |
| 6478 | {0, "__ne__", 0}, |
| 6479 | {0, "__gt__", 0}, |
| 6480 | {0, "__ge__", 0} |
Guido van Rossum | f5243f0 | 2008-01-01 04:06:48 +0000 | [diff] [blame] | 6481 | }; |
| 6482 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6483 | static PyObject * |
Mark Dickinson | 6f1d049 | 2009-11-15 13:58:49 +0000 | [diff] [blame] | 6484 | slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6485 | { |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6486 | int unbound; |
Victor Stinner | a7720f6 | 2016-08-19 17:48:51 +0200 | [diff] [blame] | 6487 | PyObject *func, *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6488 | |
Serhiy Storchaka | 4e624ca | 2017-06-01 08:18:25 +0300 | [diff] [blame] | 6489 | func = lookup_maybe_method(self, &name_op[op], &unbound); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6490 | if (func == NULL) { |
| 6491 | PyErr_Clear(); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 6492 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6493 | } |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6494 | |
| 6495 | PyObject *args[1] = {other}; |
| 6496 | res = call_unbound(unbound, func, self, args, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6497 | Py_DECREF(func); |
| 6498 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6499 | } |
| 6500 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 6501 | static PyObject * |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 6502 | slot_tp_iter(PyObject *self) |
| 6503 | { |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6504 | int unbound; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6505 | PyObject *func, *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 6506 | _Py_IDENTIFIER(__iter__); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 6507 | |
Serhiy Storchaka | 4e624ca | 2017-06-01 08:18:25 +0300 | [diff] [blame] | 6508 | func = lookup_maybe_method(self, &PyId___iter__, &unbound); |
Guido van Rossum | 97c1adf | 2016-08-18 09:22:23 -0700 | [diff] [blame] | 6509 | if (func == Py_None) { |
| 6510 | Py_DECREF(func); |
| 6511 | PyErr_Format(PyExc_TypeError, |
| 6512 | "'%.200s' object is not iterable", |
| 6513 | Py_TYPE(self)->tp_name); |
| 6514 | return NULL; |
| 6515 | } |
Victor Stinner | 6911267 | 2016-08-19 18:41:02 +0200 | [diff] [blame] | 6516 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6517 | if (func != NULL) { |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6518 | res = call_unbound_noarg(unbound, func, self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6519 | Py_DECREF(func); |
| 6520 | return res; |
| 6521 | } |
Victor Stinner | 6911267 | 2016-08-19 18:41:02 +0200 | [diff] [blame] | 6522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6523 | PyErr_Clear(); |
Serhiy Storchaka | 4e624ca | 2017-06-01 08:18:25 +0300 | [diff] [blame] | 6524 | func = lookup_maybe_method(self, &PyId___getitem__, &unbound); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6525 | if (func == NULL) { |
| 6526 | PyErr_Format(PyExc_TypeError, |
| 6527 | "'%.200s' object is not iterable", |
| 6528 | Py_TYPE(self)->tp_name); |
| 6529 | return NULL; |
| 6530 | } |
| 6531 | Py_DECREF(func); |
| 6532 | return PySeqIter_New(self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 6533 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6534 | |
| 6535 | static PyObject * |
| 6536 | slot_tp_iternext(PyObject *self) |
| 6537 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 6538 | _Py_IDENTIFIER(__next__); |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 6539 | return call_method(self, &PyId___next__, NULL, 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6540 | } |
| 6541 | |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 6542 | static PyObject * |
| 6543 | slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 6544 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6545 | PyTypeObject *tp = Py_TYPE(self); |
| 6546 | PyObject *get; |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 6547 | _Py_IDENTIFIER(__get__); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 6548 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 6549 | get = _PyType_LookupId(tp, &PyId___get__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6550 | if (get == NULL) { |
| 6551 | /* Avoid further slowdowns */ |
| 6552 | if (tp->tp_descr_get == slot_tp_descr_get) |
| 6553 | tp->tp_descr_get = NULL; |
| 6554 | Py_INCREF(self); |
| 6555 | return self; |
| 6556 | } |
| 6557 | if (obj == NULL) |
| 6558 | obj = Py_None; |
| 6559 | if (type == NULL) |
| 6560 | type = Py_None; |
| 6561 | return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL); |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 6562 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6563 | |
| 6564 | static int |
| 6565 | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) |
| 6566 | { |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 6567 | PyObject* stack[2]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6568 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 6569 | _Py_IDENTIFIER(__delete__); |
| 6570 | _Py_IDENTIFIER(__set__); |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 6571 | |
Victor Stinner | 434723f | 2017-01-11 00:07:40 +0100 | [diff] [blame] | 6572 | stack[0] = target; |
| 6573 | if (value == NULL) { |
| 6574 | res = call_method(self, &PyId___delete__, stack, 1); |
| 6575 | } |
| 6576 | else { |
| 6577 | stack[1] = value; |
| 6578 | res = call_method(self, &PyId___set__, stack, 2); |
| 6579 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6580 | if (res == NULL) |
| 6581 | return -1; |
| 6582 | Py_DECREF(res); |
| 6583 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6584 | } |
| 6585 | |
| 6586 | static int |
| 6587 | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 6588 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 6589 | _Py_IDENTIFIER(__init__); |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6590 | int unbound; |
| 6591 | PyObject *meth = lookup_method(self, &PyId___init__, &unbound); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6592 | PyObject *res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6593 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6594 | if (meth == NULL) |
| 6595 | return -1; |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6596 | if (unbound) { |
| 6597 | res = _PyObject_Call_Prepend(meth, self, args, kwds); |
| 6598 | } |
| 6599 | else { |
| 6600 | res = PyObject_Call(meth, args, kwds); |
| 6601 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6602 | Py_DECREF(meth); |
| 6603 | if (res == NULL) |
| 6604 | return -1; |
| 6605 | if (res != Py_None) { |
| 6606 | PyErr_Format(PyExc_TypeError, |
| 6607 | "__init__() should return None, not '%.200s'", |
| 6608 | Py_TYPE(res)->tp_name); |
| 6609 | Py_DECREF(res); |
| 6610 | return -1; |
| 6611 | } |
| 6612 | Py_DECREF(res); |
| 6613 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6614 | } |
| 6615 | |
| 6616 | static PyObject * |
| 6617 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 6618 | { |
Victor Stinner | 3f1057a | 2016-08-25 01:04:14 +0200 | [diff] [blame] | 6619 | PyObject *func, *result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6620 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 6621 | func = _PyObject_GetAttrId((PyObject *)type, &PyId___new__); |
Victor Stinner | 3f1057a | 2016-08-25 01:04:14 +0200 | [diff] [blame] | 6622 | if (func == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6623 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6624 | } |
Victor Stinner | 3f1057a | 2016-08-25 01:04:14 +0200 | [diff] [blame] | 6625 | |
| 6626 | result = _PyObject_Call_Prepend(func, (PyObject *)type, args, kwds); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6627 | Py_DECREF(func); |
Victor Stinner | 3f1057a | 2016-08-25 01:04:14 +0200 | [diff] [blame] | 6628 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6629 | } |
| 6630 | |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 6631 | static void |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 6632 | slot_tp_finalize(PyObject *self) |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 6633 | { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 6634 | _Py_IDENTIFIER(__del__); |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6635 | int unbound; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6636 | PyObject *del, *res; |
| 6637 | PyObject *error_type, *error_value, *error_traceback; |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 6638 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6639 | /* Save the current exception, if any. */ |
| 6640 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 6641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6642 | /* Execute __del__ method, if any. */ |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6643 | del = lookup_maybe_method(self, &PyId___del__, &unbound); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6644 | if (del != NULL) { |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6645 | res = call_unbound_noarg(unbound, del, self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6646 | if (res == NULL) |
| 6647 | PyErr_WriteUnraisable(del); |
| 6648 | else |
| 6649 | Py_DECREF(res); |
| 6650 | Py_DECREF(del); |
| 6651 | } |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 6652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6653 | /* Restore the saved exception. */ |
| 6654 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | febd61d | 2002-08-08 20:55:20 +0000 | [diff] [blame] | 6655 | } |
| 6656 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 6657 | static PyObject * |
| 6658 | slot_am_await(PyObject *self) |
| 6659 | { |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6660 | int unbound; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 6661 | PyObject *func, *res; |
| 6662 | _Py_IDENTIFIER(__await__); |
| 6663 | |
Serhiy Storchaka | 4e624ca | 2017-06-01 08:18:25 +0300 | [diff] [blame] | 6664 | func = lookup_maybe_method(self, &PyId___await__, &unbound); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 6665 | if (func != NULL) { |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6666 | res = call_unbound_noarg(unbound, func, self); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 6667 | Py_DECREF(func); |
| 6668 | return res; |
| 6669 | } |
| 6670 | PyErr_Format(PyExc_AttributeError, |
| 6671 | "object %.50s does not have __await__ method", |
| 6672 | Py_TYPE(self)->tp_name); |
| 6673 | return NULL; |
| 6674 | } |
| 6675 | |
| 6676 | static PyObject * |
| 6677 | slot_am_aiter(PyObject *self) |
| 6678 | { |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6679 | int unbound; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 6680 | PyObject *func, *res; |
| 6681 | _Py_IDENTIFIER(__aiter__); |
| 6682 | |
Serhiy Storchaka | 4e624ca | 2017-06-01 08:18:25 +0300 | [diff] [blame] | 6683 | func = lookup_maybe_method(self, &PyId___aiter__, &unbound); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 6684 | if (func != NULL) { |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6685 | res = call_unbound_noarg(unbound, func, self); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 6686 | Py_DECREF(func); |
| 6687 | return res; |
| 6688 | } |
| 6689 | PyErr_Format(PyExc_AttributeError, |
| 6690 | "object %.50s does not have __aiter__ method", |
| 6691 | Py_TYPE(self)->tp_name); |
| 6692 | return NULL; |
| 6693 | } |
| 6694 | |
| 6695 | static PyObject * |
| 6696 | slot_am_anext(PyObject *self) |
| 6697 | { |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6698 | int unbound; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 6699 | PyObject *func, *res; |
| 6700 | _Py_IDENTIFIER(__anext__); |
| 6701 | |
Serhiy Storchaka | 4e624ca | 2017-06-01 08:18:25 +0300 | [diff] [blame] | 6702 | func = lookup_maybe_method(self, &PyId___anext__, &unbound); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 6703 | if (func != NULL) { |
Victor Stinner | 516b981 | 2017-02-09 22:53:47 +0100 | [diff] [blame] | 6704 | res = call_unbound_noarg(unbound, func, self); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 6705 | Py_DECREF(func); |
| 6706 | return res; |
| 6707 | } |
| 6708 | PyErr_Format(PyExc_AttributeError, |
| 6709 | "object %.50s does not have __anext__ method", |
| 6710 | Py_TYPE(self)->tp_name); |
| 6711 | return NULL; |
| 6712 | } |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6713 | |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6714 | /* |
| 6715 | Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper functions. |
| 6716 | |
| 6717 | The table is ordered by offsets relative to the 'PyHeapTypeObject' structure, |
| 6718 | which incorporates the additional structures used for numbers, sequences and |
| 6719 | mappings. Note that multiple names may map to the same slot (e.g. __eq__, |
| 6720 | __ne__ etc. all map to tp_richcompare) and one name may map to multiple slots |
| 6721 | (e.g. __str__ affects tp_str as well as tp_repr). The table is terminated with |
| 6722 | an all-zero entry. (This table is further initialized in init_slotdefs().) |
| 6723 | */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6724 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6725 | typedef struct wrapperbase slotdef; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6726 | |
| 6727 | #undef TPSLOT |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 6728 | #undef FLSLOT |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 6729 | #undef AMSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6730 | #undef ETSLOT |
| 6731 | #undef SQSLOT |
| 6732 | #undef MPSLOT |
| 6733 | #undef NBSLOT |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6734 | #undef UNSLOT |
| 6735 | #undef IBSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6736 | #undef BINSLOT |
| 6737 | #undef RBINSLOT |
| 6738 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6739 | #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6740 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 6741 | PyDoc_STR(DOC)} |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 6742 | #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6743 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 6744 | PyDoc_STR(DOC), FLAGS} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6745 | #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6746 | {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 6747 | PyDoc_STR(DOC)} |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 6748 | #define AMSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 6749 | ETSLOT(NAME, as_async.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6750 | #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6751 | ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6752 | #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6753 | ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6754 | #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6755 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6756 | #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6757 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6758 | NAME "($self, /)\n--\n\n" DOC) |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6759 | #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6760 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6761 | NAME "($self, value, /)\n--\n\nReturn self" DOC "value.") |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6762 | #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6763 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6764 | NAME "($self, value, /)\n--\n\nReturn self" DOC "value.") |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 6765 | #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6766 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6767 | NAME "($self, value, /)\n--\n\nReturn value" DOC "self.") |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 6768 | #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6769 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6770 | NAME "($self, value, /)\n--\n\n" DOC) |
Anthony Baxter | 5661699 | 2005-06-03 14:12:21 +0000 | [diff] [blame] | 6771 | #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6772 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6773 | NAME "($self, value, /)\n--\n\n" DOC) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6774 | |
| 6775 | static slotdef slotdefs[] = { |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6776 | TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), |
| 6777 | TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), |
| 6778 | TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), |
| 6779 | TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), |
| 6780 | TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6781 | "__repr__($self, /)\n--\n\nReturn repr(self)."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6782 | TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6783 | "__hash__($self, /)\n--\n\nReturn hash(self)."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6784 | FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6785 | "__call__($self, /, *args, **kwargs)\n--\n\nCall self as a function.", |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6786 | PyWrapperFlag_KEYWORDS), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6787 | TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6788 | "__str__($self, /)\n--\n\nReturn str(self)."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6789 | TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6790 | wrap_binaryfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6791 | "__getattribute__($self, name, /)\n--\n\nReturn getattr(self, name)."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6792 | TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), |
| 6793 | TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6794 | "__setattr__($self, name, value, /)\n--\n\nImplement setattr(self, name, value)."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6795 | TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6796 | "__delattr__($self, name, /)\n--\n\nImplement delattr(self, name)."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6797 | TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6798 | "__lt__($self, value, /)\n--\n\nReturn self<value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6799 | TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6800 | "__le__($self, value, /)\n--\n\nReturn self<=value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6801 | TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6802 | "__eq__($self, value, /)\n--\n\nReturn self==value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6803 | TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6804 | "__ne__($self, value, /)\n--\n\nReturn self!=value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6805 | TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6806 | "__gt__($self, value, /)\n--\n\nReturn self>value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6807 | TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, |
Zachary Ware | ea42b4c | 2014-04-18 09:14:31 -0500 | [diff] [blame] | 6808 | "__ge__($self, value, /)\n--\n\nReturn self>=value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6809 | TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6810 | "__iter__($self, /)\n--\n\nImplement iter(self)."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6811 | TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6812 | "__next__($self, /)\n--\n\nImplement next(self)."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6813 | TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6814 | "__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] | 6815 | TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6816 | "__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] | 6817 | TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6818 | wrap_descr_delete, |
Yury Selivanov | 056e265 | 2014-03-02 12:25:27 -0500 | [diff] [blame] | 6819 | "__delete__($self, instance, /)\n--\n\nDelete an attribute of instance."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6820 | FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6821 | "__init__($self, /, *args, **kwargs)\n--\n\n" |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6822 | "Initialize self. See help(type(self)) for accurate signature.", |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6823 | PyWrapperFlag_KEYWORDS), |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6824 | TPSLOT("__new__", tp_new, slot_tp_new, NULL, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6825 | "__new__(type, /, *args, **kwargs)\n--\n\n" |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6826 | "Create and return new object. See help(type) for accurate signature."), |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 6827 | TPSLOT("__del__", tp_finalize, slot_tp_finalize, (wrapperfunc)wrap_del, ""), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6828 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 6829 | AMSLOT("__await__", am_await, slot_am_await, wrap_unaryfunc, |
| 6830 | "__await__($self, /)\n--\n\nReturn an iterator to be used in await expression."), |
| 6831 | AMSLOT("__aiter__", am_aiter, slot_am_aiter, wrap_unaryfunc, |
| 6832 | "__aiter__($self, /)\n--\n\nReturn an awaitable, that resolves in asynchronous iterator."), |
| 6833 | AMSLOT("__anext__", am_anext, slot_am_anext, wrap_unaryfunc, |
| 6834 | "__anext__($self, /)\n--\n\nReturn a value or raise StopAsyncIteration."), |
| 6835 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6836 | BINSLOT("__add__", nb_add, slot_nb_add, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6837 | "+"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6838 | RBINSLOT("__radd__", nb_add, slot_nb_add, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6839 | "+"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6840 | BINSLOT("__sub__", nb_subtract, slot_nb_subtract, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6841 | "-"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6842 | RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6843 | "-"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6844 | BINSLOT("__mul__", nb_multiply, slot_nb_multiply, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6845 | "*"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6846 | RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6847 | "*"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6848 | BINSLOT("__mod__", nb_remainder, slot_nb_remainder, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6849 | "%"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6850 | RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6851 | "%"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6852 | BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod, |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6853 | "Return divmod(self, value)."), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6854 | RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod, |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6855 | "Return divmod(value, self)."), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6856 | NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6857 | "__pow__($self, value, mod=None, /)\n--\n\nReturn pow(self, value, mod)."), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6858 | NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6859 | "__rpow__($self, value, mod=None, /)\n--\n\nReturn pow(value, self, mod)."), |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6860 | UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-self"), |
| 6861 | UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+self"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6862 | UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6863 | "abs(self)"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6864 | UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6865 | "self != 0"), |
| 6866 | UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~self"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6867 | BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 6868 | RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 6869 | BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 6870 | RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 6871 | BINSLOT("__and__", nb_and, slot_nb_and, "&"), |
| 6872 | RBINSLOT("__rand__", nb_and, slot_nb_and, "&"), |
| 6873 | BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"), |
| 6874 | RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), |
| 6875 | BINSLOT("__or__", nb_or, slot_nb_or, "|"), |
| 6876 | RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), |
| 6877 | UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6878 | "int(self)"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6879 | UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6880 | "float(self)"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6881 | IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 6882 | wrap_binaryfunc, "+="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6883 | IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 6884 | wrap_binaryfunc, "-="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6885 | IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 6886 | wrap_binaryfunc, "*="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6887 | IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 6888 | wrap_binaryfunc, "%="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6889 | IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 6890 | wrap_binaryfunc, "**="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6891 | IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 6892 | wrap_binaryfunc, "<<="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6893 | IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 6894 | wrap_binaryfunc, ">>="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6895 | IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 6896 | wrap_binaryfunc, "&="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6897 | IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 6898 | wrap_binaryfunc, "^="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6899 | IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or, |
Eli Bendersky | d3baae7 | 2011-11-11 16:57:05 +0200 | [diff] [blame] | 6900 | wrap_binaryfunc, "|="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6901 | BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 6902 | RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 6903 | BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 6904 | RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 6905 | IBSLOT("__ifloordiv__", nb_inplace_floor_divide, |
Georg Brandl | e1d26f3 | 2014-10-31 13:12:57 +0100 | [diff] [blame] | 6906 | slot_nb_inplace_floor_divide, wrap_binaryfunc, "//="), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6907 | IBSLOT("__itruediv__", nb_inplace_true_divide, |
Georg Brandl | e1d26f3 | 2014-10-31 13:12:57 +0100 | [diff] [blame] | 6908 | slot_nb_inplace_true_divide, wrap_binaryfunc, "/="), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6909 | NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6910 | "__index__($self, /)\n--\n\n" |
Zachary Ware | 715ef02 | 2014-04-18 09:23:14 -0500 | [diff] [blame] | 6911 | "Return self converted to an integer, if self is suitable " |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6912 | "for use as an index into a list."), |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 6913 | BINSLOT("__matmul__", nb_matrix_multiply, slot_nb_matrix_multiply, |
| 6914 | "@"), |
| 6915 | RBINSLOT("__rmatmul__", nb_matrix_multiply, slot_nb_matrix_multiply, |
| 6916 | "@"), |
| 6917 | IBSLOT("__imatmul__", nb_inplace_matrix_multiply, slot_nb_inplace_matrix_multiply, |
| 6918 | wrap_binaryfunc, "@="), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6919 | MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6920 | "__len__($self, /)\n--\n\nReturn len(self)."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6921 | MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, |
| 6922 | wrap_binaryfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6923 | "__getitem__($self, key, /)\n--\n\nReturn self[key]."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6924 | MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, |
| 6925 | wrap_objobjargproc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6926 | "__setitem__($self, key, value, /)\n--\n\nSet self[key] to value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6927 | MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, |
| 6928 | wrap_delitem, |
Yury Selivanov | 056e265 | 2014-03-02 12:25:27 -0500 | [diff] [blame] | 6929 | "__delitem__($self, key, /)\n--\n\nDelete self[key]."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6930 | |
| 6931 | SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6932 | "__len__($self, /)\n--\n\nReturn len(self)."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6933 | /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL. |
| 6934 | The logic in abstract.c always falls back to nb_add/nb_multiply in |
| 6935 | this case. Defining both the nb_* and the sq_* slots to call the |
| 6936 | user-defined methods has unexpected side-effects, as shown by |
| 6937 | test_descr.notimplemented() */ |
| 6938 | SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6939 | "__add__($self, value, /)\n--\n\nReturn self+value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6940 | SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6941 | "__mul__($self, value, /)\n--\n\nReturn self*value.n"), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6942 | SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6943 | "__rmul__($self, value, /)\n--\n\nReturn self*value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6944 | SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6945 | "__getitem__($self, key, /)\n--\n\nReturn self[key]."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6946 | SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6947 | "__setitem__($self, key, value, /)\n--\n\nSet self[key] to value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6948 | SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6949 | "__delitem__($self, key, /)\n--\n\nDelete self[key]."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6950 | SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6951 | "__contains__($self, key, /)\n--\n\nReturn key in self."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6952 | SQSLOT("__iadd__", sq_inplace_concat, NULL, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6953 | wrap_binaryfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6954 | "__iadd__($self, value, /)\n--\n\nImplement self+=value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6955 | SQSLOT("__imul__", sq_inplace_repeat, NULL, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 6956 | wrap_indexargfunc, |
Larry Hastings | 69a2547 | 2014-02-09 22:22:38 -0800 | [diff] [blame] | 6957 | "__imul__($self, value, /)\n--\n\nImplement self*=value."), |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 6958 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6959 | {NULL} |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6960 | }; |
| 6961 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 6962 | /* 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] | 6963 | 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] | 6964 | the offset to the type pointer, since it takes care to indirect through the |
| 6965 | proper indirection pointer (as_buffer, etc.); it returns NULL if the |
| 6966 | indirection pointer is NULL. */ |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6967 | static void ** |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6968 | slotptr(PyTypeObject *type, int ioffset) |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6969 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6970 | char *ptr; |
| 6971 | long offset = ioffset; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6972 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6973 | /* Note: this depends on the order of the members of PyHeapTypeObject! */ |
| 6974 | assert(offset >= 0); |
| 6975 | assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer)); |
| 6976 | if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) { |
| 6977 | ptr = (char *)type->tp_as_sequence; |
| 6978 | offset -= offsetof(PyHeapTypeObject, as_sequence); |
| 6979 | } |
| 6980 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) { |
| 6981 | ptr = (char *)type->tp_as_mapping; |
| 6982 | offset -= offsetof(PyHeapTypeObject, as_mapping); |
| 6983 | } |
| 6984 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) { |
| 6985 | ptr = (char *)type->tp_as_number; |
| 6986 | offset -= offsetof(PyHeapTypeObject, as_number); |
| 6987 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 6988 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_async)) { |
| 6989 | ptr = (char *)type->tp_as_async; |
| 6990 | offset -= offsetof(PyHeapTypeObject, as_async); |
| 6991 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6992 | else { |
| 6993 | ptr = (char *)type; |
| 6994 | } |
| 6995 | if (ptr != NULL) |
| 6996 | ptr += offset; |
| 6997 | return (void **)ptr; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 6998 | } |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 6999 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 7000 | /* Length of array of slotdef pointers used to store slots with the |
| 7001 | same __name__. There should be at most MAX_EQUIV-1 slotdef entries with |
| 7002 | the same __name__, for any __name__. Since that's a static property, it is |
| 7003 | appropriate to declare fixed-size arrays for this. */ |
| 7004 | #define MAX_EQUIV 10 |
| 7005 | |
| 7006 | /* Return a slot pointer for a given name, but ONLY if the attribute has |
| 7007 | exactly one slot function. The name must be an interned string. */ |
| 7008 | static void ** |
| 7009 | resolve_slotdups(PyTypeObject *type, PyObject *name) |
| 7010 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7011 | /* 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] | 7012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7013 | /* pname and ptrs act as a little cache */ |
| 7014 | static PyObject *pname; |
| 7015 | static slotdef *ptrs[MAX_EQUIV]; |
| 7016 | slotdef *p, **pp; |
| 7017 | void **res, **ptr; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 7018 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7019 | if (pname != name) { |
| 7020 | /* Collect all slotdefs that match name into ptrs. */ |
| 7021 | pname = name; |
| 7022 | pp = ptrs; |
| 7023 | for (p = slotdefs; p->name_strobj; p++) { |
| 7024 | if (p->name_strobj == name) |
| 7025 | *pp++ = p; |
| 7026 | } |
| 7027 | *pp = NULL; |
| 7028 | } |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 7029 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7030 | /* Look in all matching slots of the type; if exactly one of these has |
| 7031 | a filled-in slot, return its value. Otherwise return NULL. */ |
| 7032 | res = NULL; |
| 7033 | for (pp = ptrs; *pp; pp++) { |
| 7034 | ptr = slotptr(type, (*pp)->offset); |
| 7035 | if (ptr == NULL || *ptr == NULL) |
| 7036 | continue; |
| 7037 | if (res != NULL) |
| 7038 | return NULL; |
| 7039 | res = ptr; |
| 7040 | } |
| 7041 | return res; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 7042 | } |
| 7043 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 7044 | /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 7045 | does some incredibly complex thinking and then sticks something into the |
| 7046 | slot. (It sees if the adjacent slotdefs for the same slot have conflicting |
| 7047 | interests, and then stores a generic wrapper or a specific function into |
| 7048 | the slot.) Return a pointer to the next slotdef with a different offset, |
| 7049 | because that's convenient for fixup_slot_dispatchers(). */ |
| 7050 | static slotdef * |
| 7051 | update_one_slot(PyTypeObject *type, slotdef *p) |
| 7052 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7053 | PyObject *descr; |
| 7054 | PyWrapperDescrObject *d; |
| 7055 | void *generic = NULL, *specific = NULL; |
| 7056 | int use_generic = 0; |
| 7057 | int offset = p->offset; |
scoder | 2102c78 | 2017-10-01 10:37:47 +0200 | [diff] [blame] | 7058 | int error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7059 | void **ptr = slotptr(type, offset); |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 7060 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7061 | if (ptr == NULL) { |
| 7062 | do { |
| 7063 | ++p; |
| 7064 | } while (p->offset == offset); |
| 7065 | return p; |
| 7066 | } |
scoder | 2102c78 | 2017-10-01 10:37:47 +0200 | [diff] [blame] | 7067 | /* We may end up clearing live exceptions below, so make sure it's ours. */ |
| 7068 | assert(!PyErr_Occurred()); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7069 | do { |
scoder | 2102c78 | 2017-10-01 10:37:47 +0200 | [diff] [blame] | 7070 | /* Use faster uncached lookup as we won't get any cache hits during type setup. */ |
| 7071 | descr = find_name_in_mro(type, p->name_strobj, &error); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7072 | if (descr == NULL) { |
scoder | 2102c78 | 2017-10-01 10:37:47 +0200 | [diff] [blame] | 7073 | if (error == -1) { |
| 7074 | /* It is unlikely by not impossible that there has been an exception |
| 7075 | during lookup. Since this function originally expected no errors, |
| 7076 | we ignore them here in order to keep up the interface. */ |
| 7077 | PyErr_Clear(); |
| 7078 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7079 | if (ptr == (void**)&type->tp_iternext) { |
Trent Nelson | ab02db2 | 2012-09-18 21:58:03 -0400 | [diff] [blame] | 7080 | specific = (void *)_PyObject_NextNotImplemented; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7081 | } |
| 7082 | continue; |
| 7083 | } |
Benjamin Peterson | 7b16687 | 2012-04-24 11:06:25 -0400 | [diff] [blame] | 7084 | if (Py_TYPE(descr) == &PyWrapperDescr_Type && |
| 7085 | ((PyWrapperDescrObject *)descr)->d_base->name_strobj == p->name_strobj) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7086 | void **tptr = resolve_slotdups(type, p->name_strobj); |
| 7087 | if (tptr == NULL || tptr == ptr) |
| 7088 | generic = p->function; |
| 7089 | d = (PyWrapperDescrObject *)descr; |
| 7090 | if (d->d_base->wrapper == p->wrapper && |
| 7091 | PyType_IsSubtype(type, PyDescr_TYPE(d))) |
| 7092 | { |
| 7093 | if (specific == NULL || |
| 7094 | specific == d->d_wrapped) |
| 7095 | specific = d->d_wrapped; |
| 7096 | else |
| 7097 | use_generic = 1; |
| 7098 | } |
| 7099 | } |
| 7100 | else if (Py_TYPE(descr) == &PyCFunction_Type && |
| 7101 | PyCFunction_GET_FUNCTION(descr) == |
| 7102 | (PyCFunction)tp_new_wrapper && |
| 7103 | ptr == (void**)&type->tp_new) |
| 7104 | { |
| 7105 | /* The __new__ wrapper is not a wrapper descriptor, |
| 7106 | so must be special-cased differently. |
| 7107 | If we don't do this, creating an instance will |
| 7108 | always use slot_tp_new which will look up |
| 7109 | __new__ in the MRO which will call tp_new_wrapper |
| 7110 | which will look through the base classes looking |
| 7111 | for a static base and call its tp_new (usually |
| 7112 | PyType_GenericNew), after performing various |
| 7113 | sanity checks and constructing a new argument |
| 7114 | list. Cut all that nonsense short -- this speeds |
| 7115 | up instance creation tremendously. */ |
Serhiy Storchaka | 49010ee | 2016-12-14 19:52:17 +0200 | [diff] [blame] | 7116 | specific = (void *)type->tp_new; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7117 | /* XXX I'm not 100% sure that there isn't a hole |
| 7118 | in this reasoning that requires additional |
| 7119 | sanity checks. I'll buy the first person to |
| 7120 | point out a bug in this reasoning a beer. */ |
| 7121 | } |
| 7122 | else if (descr == Py_None && |
| 7123 | ptr == (void**)&type->tp_hash) { |
| 7124 | /* We specifically allow __hash__ to be set to None |
| 7125 | to prevent inheritance of the default |
| 7126 | implementation from object.__hash__ */ |
Trent Nelson | ab02db2 | 2012-09-18 21:58:03 -0400 | [diff] [blame] | 7127 | specific = (void *)PyObject_HashNotImplemented; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7128 | } |
| 7129 | else { |
| 7130 | use_generic = 1; |
| 7131 | generic = p->function; |
| 7132 | } |
| 7133 | } while ((++p)->offset == offset); |
| 7134 | if (specific && !use_generic) |
| 7135 | *ptr = specific; |
| 7136 | else |
| 7137 | *ptr = generic; |
| 7138 | return p; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 7139 | } |
| 7140 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 7141 | /* In the type, update the slots whose slotdefs are gathered in the pp array. |
| 7142 | This is a callback for update_subclasses(). */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 7143 | static int |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 7144 | update_slots_callback(PyTypeObject *type, void *data) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 7145 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7146 | slotdef **pp = (slotdef **)data; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 7147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7148 | for (; *pp; pp++) |
| 7149 | update_one_slot(type, *pp); |
| 7150 | return 0; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 7151 | } |
| 7152 | |
Martin v. Löwis | 996b671 | 2014-07-26 16:44:07 +0200 | [diff] [blame] | 7153 | static int slotdefs_initialized = 0; |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 7154 | /* 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] | 7155 | names. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 7156 | static void |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 7157 | init_slotdefs(void) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 7158 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7159 | slotdef *p; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 7160 | |
Martin v. Löwis | 996b671 | 2014-07-26 16:44:07 +0200 | [diff] [blame] | 7161 | if (slotdefs_initialized) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7162 | return; |
| 7163 | for (p = slotdefs; p->name; p++) { |
Benjamin Peterson | 6395241 | 2013-04-01 17:41:41 -0400 | [diff] [blame] | 7164 | /* Slots must be ordered by their offset in the PyHeapTypeObject. */ |
| 7165 | assert(!p[1].name || p->offset <= p[1].offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7166 | p->name_strobj = PyUnicode_InternFromString(p->name); |
Serhiy Storchaka | d896985 | 2017-05-20 08:48:06 +0300 | [diff] [blame] | 7167 | if (!p->name_strobj || !PyUnicode_CHECK_INTERNED(p->name_strobj)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7168 | Py_FatalError("Out of memory interning slotdef names"); |
| 7169 | } |
Martin v. Löwis | 996b671 | 2014-07-26 16:44:07 +0200 | [diff] [blame] | 7170 | slotdefs_initialized = 1; |
| 7171 | } |
| 7172 | |
| 7173 | /* Undo init_slotdefs, releasing the interned strings. */ |
Victor Stinner | 331a726 | 2014-07-27 16:11:30 +0200 | [diff] [blame] | 7174 | static void clear_slotdefs(void) |
Martin v. Löwis | 996b671 | 2014-07-26 16:44:07 +0200 | [diff] [blame] | 7175 | { |
| 7176 | slotdef *p; |
| 7177 | for (p = slotdefs; p->name; p++) { |
| 7178 | Py_CLEAR(p->name_strobj); |
| 7179 | } |
| 7180 | slotdefs_initialized = 0; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 7181 | } |
| 7182 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 7183 | /* Update the slots after assignment to a class (type) attribute. */ |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 7184 | static int |
| 7185 | update_slot(PyTypeObject *type, PyObject *name) |
| 7186 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7187 | slotdef *ptrs[MAX_EQUIV]; |
| 7188 | slotdef *p; |
| 7189 | slotdef **pp; |
| 7190 | int offset; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 7191 | |
Serhiy Storchaka | d896985 | 2017-05-20 08:48:06 +0300 | [diff] [blame] | 7192 | assert(PyUnicode_CheckExact(name)); |
| 7193 | assert(PyUnicode_CHECK_INTERNED(name)); |
| 7194 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7195 | /* Clear the VALID_VERSION flag of 'type' and all its |
| 7196 | subclasses. This could possibly be unified with the |
| 7197 | update_subclasses() recursion below, but carefully: |
| 7198 | they each have their own conditions on which to stop |
| 7199 | recursing into subclasses. */ |
| 7200 | PyType_Modified(type); |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 7201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7202 | init_slotdefs(); |
| 7203 | pp = ptrs; |
| 7204 | for (p = slotdefs; p->name; p++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7205 | if (p->name_strobj == name) |
| 7206 | *pp++ = p; |
| 7207 | } |
| 7208 | *pp = NULL; |
| 7209 | for (pp = ptrs; *pp; pp++) { |
| 7210 | p = *pp; |
| 7211 | offset = p->offset; |
| 7212 | while (p > slotdefs && (p-1)->offset == offset) |
| 7213 | --p; |
| 7214 | *pp = p; |
| 7215 | } |
| 7216 | if (ptrs[0] == NULL) |
| 7217 | return 0; /* Not an attribute that affects any slots */ |
| 7218 | return update_subclasses(type, name, |
| 7219 | update_slots_callback, (void *)ptrs); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 7220 | } |
| 7221 | |
Guido van Rossum | c334df5 | 2002-04-04 23:44:47 +0000 | [diff] [blame] | 7222 | /* Store the proper functions in the slot dispatches at class (type) |
| 7223 | definition time, based upon which operations the class overrides in its |
| 7224 | dict. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7225 | static void |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 7226 | fixup_slot_dispatchers(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7227 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7228 | slotdef *p; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7230 | init_slotdefs(); |
| 7231 | for (p = slotdefs; p->name; ) |
| 7232 | p = update_one_slot(type, p); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7233 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7234 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 7235 | static void |
| 7236 | update_all_slots(PyTypeObject* type) |
| 7237 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7238 | slotdef *p; |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 7239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7240 | init_slotdefs(); |
| 7241 | for (p = slotdefs; p->name; p++) { |
| 7242 | /* update_slot returns int but can't actually fail */ |
| 7243 | update_slot(type, p->name_strobj); |
| 7244 | } |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 7245 | } |
| 7246 | |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 7247 | /* Call __set_name__ on all descriptors in a newly generated type */ |
| 7248 | static int |
| 7249 | set_names(PyTypeObject *type) |
| 7250 | { |
Serhiy Storchaka | 9ec0772 | 2016-11-29 09:54:17 +0200 | [diff] [blame] | 7251 | PyObject *names_to_set, *key, *value, *set_name, *tmp; |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 7252 | Py_ssize_t i = 0; |
| 7253 | |
Serhiy Storchaka | 9ec0772 | 2016-11-29 09:54:17 +0200 | [diff] [blame] | 7254 | names_to_set = PyDict_Copy(type->tp_dict); |
| 7255 | if (names_to_set == NULL) |
| 7256 | return -1; |
| 7257 | |
| 7258 | while (PyDict_Next(names_to_set, &i, &key, &value)) { |
Serhiy Storchaka | 4e624ca | 2017-06-01 08:18:25 +0300 | [diff] [blame] | 7259 | set_name = _PyObject_LookupSpecial(value, &PyId___set_name__); |
Serhiy Storchaka | afd02a4 | 2016-09-21 15:54:59 +0300 | [diff] [blame] | 7260 | if (set_name != NULL) { |
| 7261 | tmp = PyObject_CallFunctionObjArgs(set_name, type, key, NULL); |
| 7262 | Py_DECREF(set_name); |
Serhiy Storchaka | d5d32d2 | 2016-10-21 17:13:31 +0300 | [diff] [blame] | 7263 | if (tmp == NULL) { |
| 7264 | _PyErr_FormatFromCause(PyExc_RuntimeError, |
| 7265 | "Error calling __set_name__ on '%.100s' instance %R " |
| 7266 | "in '%.100s'", |
| 7267 | value->ob_type->tp_name, key, type->tp_name); |
Serhiy Storchaka | 9ec0772 | 2016-11-29 09:54:17 +0200 | [diff] [blame] | 7268 | Py_DECREF(names_to_set); |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 7269 | return -1; |
Serhiy Storchaka | d5d32d2 | 2016-10-21 17:13:31 +0300 | [diff] [blame] | 7270 | } |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 7271 | else |
| 7272 | Py_DECREF(tmp); |
| 7273 | } |
Serhiy Storchaka | 9ec0772 | 2016-11-29 09:54:17 +0200 | [diff] [blame] | 7274 | else if (PyErr_Occurred()) { |
| 7275 | Py_DECREF(names_to_set); |
Serhiy Storchaka | afd02a4 | 2016-09-21 15:54:59 +0300 | [diff] [blame] | 7276 | return -1; |
Serhiy Storchaka | 9ec0772 | 2016-11-29 09:54:17 +0200 | [diff] [blame] | 7277 | } |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 7278 | } |
| 7279 | |
Serhiy Storchaka | 9ec0772 | 2016-11-29 09:54:17 +0200 | [diff] [blame] | 7280 | Py_DECREF(names_to_set); |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 7281 | return 0; |
| 7282 | } |
| 7283 | |
| 7284 | /* Call __init_subclass__ on the parent of a newly generated type */ |
| 7285 | static int |
| 7286 | init_subclass(PyTypeObject *type, PyObject *kwds) |
| 7287 | { |
Victor Stinner | 463b86a | 2016-08-22 23:33:13 +0200 | [diff] [blame] | 7288 | PyObject *super, *func, *result; |
| 7289 | PyObject *args[2] = {(PyObject *)type, (PyObject *)type}; |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 7290 | |
Victor Stinner | 463b86a | 2016-08-22 23:33:13 +0200 | [diff] [blame] | 7291 | super = _PyObject_FastCall((PyObject *)&PySuper_Type, args, 2); |
| 7292 | if (super == NULL) { |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 7293 | return -1; |
Victor Stinner | 253021d | 2016-08-20 02:37:41 +0200 | [diff] [blame] | 7294 | } |
| 7295 | |
Victor Stinner | 463b86a | 2016-08-22 23:33:13 +0200 | [diff] [blame] | 7296 | func = _PyObject_GetAttrId(super, &PyId___init_subclass__); |
| 7297 | Py_DECREF(super); |
| 7298 | if (func == NULL) { |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 7299 | return -1; |
Victor Stinner | 463b86a | 2016-08-22 23:33:13 +0200 | [diff] [blame] | 7300 | } |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 7301 | |
Victor Stinner | 463b86a | 2016-08-22 23:33:13 +0200 | [diff] [blame] | 7302 | |
| 7303 | result = _PyObject_FastCallDict(func, NULL, 0, kwds); |
| 7304 | Py_DECREF(func); |
| 7305 | if (result == NULL) { |
| 7306 | return -1; |
| 7307 | } |
| 7308 | |
| 7309 | Py_DECREF(result); |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 7310 | return 0; |
| 7311 | } |
| 7312 | |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 7313 | /* recurse_down_subclasses() and update_subclasses() are mutually |
| 7314 | recursive functions to call a callback for all subclasses, |
| 7315 | but refraining from recursing into subclasses that define 'name'. */ |
| 7316 | |
| 7317 | static int |
| 7318 | update_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7319 | update_callback callback, void *data) |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 7320 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7321 | if (callback(type, data) < 0) |
| 7322 | return -1; |
| 7323 | return recurse_down_subclasses(type, name, callback, data); |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 7324 | } |
| 7325 | |
| 7326 | static int |
| 7327 | recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7328 | update_callback callback, void *data) |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 7329 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7330 | PyTypeObject *subclass; |
| 7331 | PyObject *ref, *subclasses, *dict; |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 7332 | Py_ssize_t i; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 7333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7334 | subclasses = type->tp_subclasses; |
| 7335 | if (subclasses == NULL) |
| 7336 | return 0; |
Antoine Pitrou | 84745ab | 2013-10-29 21:31:25 +0100 | [diff] [blame] | 7337 | assert(PyDict_CheckExact(subclasses)); |
| 7338 | i = 0; |
| 7339 | while (PyDict_Next(subclasses, &i, NULL, &ref)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7340 | assert(PyWeakref_CheckRef(ref)); |
| 7341 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 7342 | assert(subclass != NULL); |
| 7343 | if ((PyObject *)subclass == Py_None) |
| 7344 | continue; |
| 7345 | assert(PyType_Check(subclass)); |
| 7346 | /* Avoid recursing down into unaffected classes */ |
| 7347 | dict = subclass->tp_dict; |
| 7348 | if (dict != NULL && PyDict_Check(dict) && |
| 7349 | PyDict_GetItem(dict, name) != NULL) |
| 7350 | continue; |
| 7351 | if (update_subclasses(subclass, name, callback, data) < 0) |
| 7352 | return -1; |
| 7353 | } |
| 7354 | return 0; |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 7355 | } |
| 7356 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 7357 | /* This function is called by PyType_Ready() to populate the type's |
| 7358 | dictionary with method descriptors for function slots. For each |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 7359 | function slot (like tp_repr) that's defined in the type, one or more |
| 7360 | corresponding descriptors are added in the type's tp_dict dictionary |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7361 | under the appropriate name (like __repr__). Some function slots |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 7362 | cause more than one descriptor to be added (for example, the nb_add |
| 7363 | slot adds both __add__ and __radd__ descriptors) and some function |
| 7364 | slots compete for the same descriptor (for example both sq_item and |
| 7365 | mp_subscript generate a __getitem__ descriptor). |
| 7366 | |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 7367 | In the latter case, the first slotdef entry encountered wins. Since |
Tim Peters | bf9b244 | 2003-03-23 05:35:36 +0000 | [diff] [blame] | 7368 | 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] | 7369 | PyHeapTypeObject, this gives us some control over disambiguating |
Guido van Rossum | 8d24ee9 | 2003-03-24 23:49:49 +0000 | [diff] [blame] | 7370 | between competing slots: the members of PyHeapTypeObject are listed |
| 7371 | from most general to least general, so the most general slot is |
| 7372 | preferred. In particular, because as_mapping comes before as_sequence, |
| 7373 | for a type that defines both mp_subscript and sq_item, mp_subscript |
| 7374 | wins. |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 7375 | |
| 7376 | This only adds new descriptors and doesn't overwrite entries in |
| 7377 | tp_dict that were previously defined. The descriptors contain a |
| 7378 | reference to the C function they must call, so that it's safe if they |
| 7379 | are copied into a subtype's __dict__ and the subtype has a different |
| 7380 | C function in its slot -- calling the method defined by the |
| 7381 | descriptor will call the C function that was used to create it, |
| 7382 | rather than the C function present in the slot when it is called. |
| 7383 | (This is important because a subtype may have a C function in the |
| 7384 | slot that calls the method from the dictionary, and we want to avoid |
| 7385 | infinite recursion here.) */ |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 7386 | |
| 7387 | static int |
| 7388 | add_operators(PyTypeObject *type) |
| 7389 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7390 | PyObject *dict = type->tp_dict; |
| 7391 | slotdef *p; |
| 7392 | PyObject *descr; |
| 7393 | void **ptr; |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 7394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7395 | init_slotdefs(); |
| 7396 | for (p = slotdefs; p->name; p++) { |
| 7397 | if (p->wrapper == NULL) |
| 7398 | continue; |
| 7399 | ptr = slotptr(type, p->offset); |
| 7400 | if (!ptr || !*ptr) |
| 7401 | continue; |
| 7402 | if (PyDict_GetItem(dict, p->name_strobj)) |
| 7403 | continue; |
Trent Nelson | ab02db2 | 2012-09-18 21:58:03 -0400 | [diff] [blame] | 7404 | if (*ptr == (void *)PyObject_HashNotImplemented) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7405 | /* Classes may prevent the inheritance of the tp_hash |
| 7406 | slot by storing PyObject_HashNotImplemented in it. Make it |
| 7407 | visible as a None value for the __hash__ attribute. */ |
| 7408 | if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0) |
| 7409 | return -1; |
| 7410 | } |
| 7411 | else { |
| 7412 | descr = PyDescr_NewWrapper(type, p, *ptr); |
| 7413 | if (descr == NULL) |
| 7414 | return -1; |
Benjamin Peterson | 27007dc | 2016-07-06 23:26:32 -0700 | [diff] [blame] | 7415 | if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) { |
| 7416 | Py_DECREF(descr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7417 | return -1; |
Benjamin Peterson | 27007dc | 2016-07-06 23:26:32 -0700 | [diff] [blame] | 7418 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7419 | Py_DECREF(descr); |
| 7420 | } |
| 7421 | } |
| 7422 | if (type->tp_new != NULL) { |
| 7423 | if (add_tp_new_wrapper(type) < 0) |
| 7424 | return -1; |
| 7425 | } |
| 7426 | return 0; |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 7427 | } |
| 7428 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7429 | |
| 7430 | /* Cooperative 'super' */ |
| 7431 | |
| 7432 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7433 | PyObject_HEAD |
| 7434 | PyTypeObject *type; |
| 7435 | PyObject *obj; |
| 7436 | PyTypeObject *obj_type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7437 | } superobject; |
| 7438 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 7439 | static PyMemberDef super_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7440 | {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY, |
| 7441 | "the class invoking super()"}, |
| 7442 | {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY, |
| 7443 | "the instance invoking super(); may be None"}, |
| 7444 | {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY, |
| 7445 | "the type of the instance invoking super(); may be None"}, |
| 7446 | {0} |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 7447 | }; |
| 7448 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7449 | static void |
| 7450 | super_dealloc(PyObject *self) |
| 7451 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7452 | superobject *su = (superobject *)self; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7454 | _PyObject_GC_UNTRACK(self); |
| 7455 | Py_XDECREF(su->obj); |
| 7456 | Py_XDECREF(su->type); |
| 7457 | Py_XDECREF(su->obj_type); |
| 7458 | Py_TYPE(self)->tp_free(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7459 | } |
| 7460 | |
| 7461 | static PyObject * |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 7462 | super_repr(PyObject *self) |
| 7463 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7464 | superobject *su = (superobject *)self; |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 7465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7466 | if (su->obj_type) |
| 7467 | return PyUnicode_FromFormat( |
| 7468 | "<super: <class '%s'>, <%s object>>", |
| 7469 | su->type ? su->type->tp_name : "NULL", |
| 7470 | su->obj_type->tp_name); |
| 7471 | else |
| 7472 | return PyUnicode_FromFormat( |
| 7473 | "<super: <class '%s'>, NULL>", |
| 7474 | su->type ? su->type->tp_name : "NULL"); |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 7475 | } |
| 7476 | |
| 7477 | static PyObject * |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7478 | super_getattro(PyObject *self, PyObject *name) |
| 7479 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7480 | superobject *su = (superobject *)self; |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 7481 | PyTypeObject *starttype; |
| 7482 | PyObject *mro; |
| 7483 | Py_ssize_t i, n; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7484 | |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 7485 | starttype = su->obj_type; |
| 7486 | if (starttype == NULL) |
| 7487 | goto skip; |
| 7488 | |
| 7489 | /* We want __class__ to return the class of the super object |
| 7490 | (i.e. super, or a subclass), not the class of su->obj. */ |
| 7491 | if (PyUnicode_Check(name) && |
| 7492 | PyUnicode_GET_LENGTH(name) == 9 && |
Serhiy Storchaka | f5894dd | 2016-11-16 15:40:39 +0200 | [diff] [blame] | 7493 | _PyUnicode_EqualToASCIIId(name, &PyId___class__)) |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 7494 | goto skip; |
| 7495 | |
| 7496 | mro = starttype->tp_mro; |
| 7497 | if (mro == NULL) |
| 7498 | goto skip; |
| 7499 | |
| 7500 | assert(PyTuple_Check(mro)); |
| 7501 | n = PyTuple_GET_SIZE(mro); |
| 7502 | |
| 7503 | /* No need to check the last one: it's gonna be skipped anyway. */ |
| 7504 | for (i = 0; i+1 < n; i++) { |
| 7505 | if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) |
| 7506 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7507 | } |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 7508 | i++; /* skip su->type (if any) */ |
| 7509 | if (i >= n) |
| 7510 | goto skip; |
Guido van Rossum | 76ba09f | 2003-04-16 19:40:58 +0000 | [diff] [blame] | 7511 | |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 7512 | /* keep a strong reference to mro because starttype->tp_mro can be |
| 7513 | replaced during PyDict_GetItem(dict, name) */ |
| 7514 | Py_INCREF(mro); |
| 7515 | do { |
| 7516 | PyObject *res, *tmp, *dict; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7517 | descrgetfunc f; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7518 | |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 7519 | tmp = PyTuple_GET_ITEM(mro, i); |
| 7520 | assert(PyType_Check(tmp)); |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 7521 | |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 7522 | dict = ((PyTypeObject *)tmp)->tp_dict; |
| 7523 | assert(dict != NULL && PyDict_Check(dict)); |
| 7524 | |
| 7525 | res = PyDict_GetItem(dict, name); |
| 7526 | if (res != NULL) { |
| 7527 | Py_INCREF(res); |
| 7528 | |
| 7529 | f = Py_TYPE(res)->tp_descr_get; |
| 7530 | if (f != NULL) { |
| 7531 | tmp = f(res, |
| 7532 | /* Only pass 'obj' param if this is instance-mode super |
| 7533 | (See SF ID #743627) */ |
| 7534 | (su->obj == (PyObject *)starttype) ? NULL : su->obj, |
| 7535 | (PyObject *)starttype); |
| 7536 | Py_DECREF(res); |
| 7537 | res = tmp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7538 | } |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 7539 | |
| 7540 | Py_DECREF(mro); |
| 7541 | return res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7542 | } |
Benjamin Peterson | 104b9e0 | 2015-02-05 22:29:14 -0500 | [diff] [blame] | 7543 | |
| 7544 | i++; |
| 7545 | } while (i < n); |
| 7546 | Py_DECREF(mro); |
| 7547 | |
| 7548 | skip: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7549 | return PyObject_GenericGetAttr(self, name); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7550 | } |
| 7551 | |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 7552 | static PyTypeObject * |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 7553 | supercheck(PyTypeObject *type, PyObject *obj) |
| 7554 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7555 | /* Check that a super() call makes sense. Return a type object. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 7556 | |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 7557 | obj can be a class, or an instance of one: |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 7558 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7559 | - If it is a class, it must be a subclass of 'type'. This case is |
| 7560 | used for class methods; the return value is obj. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 7561 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7562 | - If it is an instance, it must be an instance of 'type'. This is |
| 7563 | the normal case; the return value is obj.__class__. |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 7564 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7565 | But... when obj is an instance, we want to allow for the case where |
| 7566 | Py_TYPE(obj) is not a subclass of type, but obj.__class__ is! |
| 7567 | This will allow using super() with a proxy for obj. |
| 7568 | */ |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 7569 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7570 | /* Check for first bullet above (special case) */ |
| 7571 | if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) { |
| 7572 | Py_INCREF(obj); |
| 7573 | return (PyTypeObject *)obj; |
| 7574 | } |
Guido van Rossum | 8e80a72 | 2003-02-18 19:22:22 +0000 | [diff] [blame] | 7575 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7576 | /* Normal case */ |
| 7577 | if (PyType_IsSubtype(Py_TYPE(obj), type)) { |
| 7578 | Py_INCREF(Py_TYPE(obj)); |
| 7579 | return Py_TYPE(obj); |
| 7580 | } |
| 7581 | else { |
| 7582 | /* Try the slow way */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7583 | PyObject *class_attr; |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 7584 | |
Martin v. Löwis | bfc6d74 | 2011-10-13 20:03:57 +0200 | [diff] [blame] | 7585 | class_attr = _PyObject_GetAttrId(obj, &PyId___class__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7586 | if (class_attr != NULL && |
| 7587 | PyType_Check(class_attr) && |
| 7588 | (PyTypeObject *)class_attr != Py_TYPE(obj)) |
| 7589 | { |
| 7590 | int ok = PyType_IsSubtype( |
| 7591 | (PyTypeObject *)class_attr, type); |
| 7592 | if (ok) |
| 7593 | return (PyTypeObject *)class_attr; |
| 7594 | } |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 7595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7596 | if (class_attr == NULL) |
| 7597 | PyErr_Clear(); |
| 7598 | else |
| 7599 | Py_DECREF(class_attr); |
| 7600 | } |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 7601 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7602 | PyErr_SetString(PyExc_TypeError, |
| 7603 | "super(type, obj): " |
| 7604 | "obj must be an instance or subtype of type"); |
| 7605 | return NULL; |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 7606 | } |
| 7607 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7608 | static PyObject * |
| 7609 | super_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 7610 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7611 | superobject *su = (superobject *)self; |
| 7612 | superobject *newobj; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7614 | if (obj == NULL || obj == Py_None || su->obj != NULL) { |
| 7615 | /* Not binding to an object, or already bound */ |
| 7616 | Py_INCREF(self); |
| 7617 | return self; |
| 7618 | } |
| 7619 | if (Py_TYPE(su) != &PySuper_Type) |
| 7620 | /* If su is an instance of a (strict) subclass of super, |
| 7621 | call its type */ |
| 7622 | return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su), |
| 7623 | su->type, obj, NULL); |
| 7624 | else { |
| 7625 | /* Inline the common case */ |
| 7626 | PyTypeObject *obj_type = supercheck(su->type, obj); |
| 7627 | if (obj_type == NULL) |
| 7628 | return NULL; |
| 7629 | newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type, |
| 7630 | NULL, NULL); |
| 7631 | if (newobj == NULL) |
| 7632 | return NULL; |
| 7633 | Py_INCREF(su->type); |
| 7634 | Py_INCREF(obj); |
| 7635 | newobj->type = su->type; |
| 7636 | newobj->obj = obj; |
| 7637 | newobj->obj_type = obj_type; |
| 7638 | return (PyObject *)newobj; |
| 7639 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7640 | } |
| 7641 | |
| 7642 | static int |
| 7643 | super_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 7644 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7645 | superobject *su = (superobject *)self; |
| 7646 | PyTypeObject *type = NULL; |
| 7647 | PyObject *obj = NULL; |
| 7648 | PyTypeObject *obj_type = NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7649 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7650 | if (!_PyArg_NoKeywords("super", kwds)) |
| 7651 | return -1; |
| 7652 | if (!PyArg_ParseTuple(args, "|O!O:super", &PyType_Type, &type, &obj)) |
| 7653 | return -1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7655 | if (type == NULL) { |
| 7656 | /* Call super(), without args -- fill in from __class__ |
| 7657 | and first local variable on the stack. */ |
Victor Stinner | 1c6970f | 2014-05-13 01:32:36 +0200 | [diff] [blame] | 7658 | PyFrameObject *f; |
| 7659 | PyCodeObject *co; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 7660 | Py_ssize_t i, n; |
Victor Stinner | 1c6970f | 2014-05-13 01:32:36 +0200 | [diff] [blame] | 7661 | f = PyThreadState_GET()->frame; |
| 7662 | if (f == NULL) { |
| 7663 | PyErr_SetString(PyExc_RuntimeError, |
| 7664 | "super(): no current frame"); |
| 7665 | return -1; |
| 7666 | } |
| 7667 | co = f->f_code; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7668 | if (co == NULL) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 7669 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7670 | "super(): no code object"); |
| 7671 | return -1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7672 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7673 | if (co->co_argcount == 0) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 7674 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7675 | "super(): no arguments"); |
| 7676 | return -1; |
| 7677 | } |
| 7678 | obj = f->f_localsplus[0]; |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 7679 | if (obj == NULL && co->co_cell2arg) { |
| 7680 | /* The first argument might be a cell. */ |
| 7681 | n = PyTuple_GET_SIZE(co->co_cellvars); |
| 7682 | for (i = 0; i < n; i++) { |
| 7683 | if (co->co_cell2arg[i] == 0) { |
| 7684 | PyObject *cell = f->f_localsplus[co->co_nlocals + i]; |
| 7685 | assert(PyCell_Check(cell)); |
| 7686 | obj = PyCell_GET(cell); |
| 7687 | break; |
| 7688 | } |
| 7689 | } |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 7690 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7691 | if (obj == NULL) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 7692 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7693 | "super(): arg[0] deleted"); |
| 7694 | return -1; |
| 7695 | } |
| 7696 | if (co->co_freevars == NULL) |
| 7697 | n = 0; |
| 7698 | else { |
| 7699 | assert(PyTuple_Check(co->co_freevars)); |
| 7700 | n = PyTuple_GET_SIZE(co->co_freevars); |
| 7701 | } |
| 7702 | for (i = 0; i < n; i++) { |
| 7703 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 7704 | assert(PyUnicode_Check(name)); |
Serhiy Storchaka | f5894dd | 2016-11-16 15:40:39 +0200 | [diff] [blame] | 7705 | if (_PyUnicode_EqualToASCIIId(name, &PyId___class__)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7706 | Py_ssize_t index = co->co_nlocals + |
| 7707 | PyTuple_GET_SIZE(co->co_cellvars) + i; |
| 7708 | PyObject *cell = f->f_localsplus[index]; |
| 7709 | if (cell == NULL || !PyCell_Check(cell)) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 7710 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7711 | "super(): bad __class__ cell"); |
| 7712 | return -1; |
| 7713 | } |
| 7714 | type = (PyTypeObject *) PyCell_GET(cell); |
| 7715 | if (type == NULL) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 7716 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7717 | "super(): empty __class__ cell"); |
| 7718 | return -1; |
| 7719 | } |
| 7720 | if (!PyType_Check(type)) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 7721 | PyErr_Format(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7722 | "super(): __class__ is not a type (%s)", |
| 7723 | Py_TYPE(type)->tp_name); |
| 7724 | return -1; |
| 7725 | } |
| 7726 | break; |
| 7727 | } |
| 7728 | } |
| 7729 | if (type == NULL) { |
Benjamin Peterson | 6a42bd6 | 2012-09-01 23:04:38 -0400 | [diff] [blame] | 7730 | PyErr_SetString(PyExc_RuntimeError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7731 | "super(): __class__ cell not found"); |
| 7732 | return -1; |
| 7733 | } |
| 7734 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7735 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7736 | if (obj == Py_None) |
| 7737 | obj = NULL; |
| 7738 | if (obj != NULL) { |
| 7739 | obj_type = supercheck(type, obj); |
| 7740 | if (obj_type == NULL) |
| 7741 | return -1; |
| 7742 | Py_INCREF(obj); |
| 7743 | } |
| 7744 | Py_INCREF(type); |
Serhiy Storchaka | 3d74976 | 2016-04-13 15:27:33 +0300 | [diff] [blame] | 7745 | Py_XSETREF(su->type, type); |
| 7746 | Py_XSETREF(su->obj, obj); |
| 7747 | Py_XSETREF(su->obj_type, obj_type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7748 | return 0; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7749 | } |
| 7750 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7751 | PyDoc_STRVAR(super_doc, |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7752 | "super() -> same as super(__class__, <first argument>)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7753 | "super(type) -> unbound super object\n" |
| 7754 | "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 7755 | "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7756 | "Typical use to call a cooperative superclass method:\n" |
| 7757 | "class C(B):\n" |
| 7758 | " def meth(self, arg):\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7759 | " super().meth(arg)\n" |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7760 | "This works for class methods too:\n" |
| 7761 | "class C(B):\n" |
| 7762 | " @classmethod\n" |
| 7763 | " def cmeth(cls, arg):\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7764 | " super().cmeth(arg)\n"); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7765 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 7766 | static int |
| 7767 | super_traverse(PyObject *self, visitproc visit, void *arg) |
| 7768 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7769 | superobject *su = (superobject *)self; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 7770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7771 | Py_VISIT(su->obj); |
| 7772 | Py_VISIT(su->type); |
| 7773 | Py_VISIT(su->obj_type); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 7774 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7775 | return 0; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 7776 | } |
| 7777 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7778 | PyTypeObject PySuper_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7779 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 7780 | "super", /* tp_name */ |
| 7781 | sizeof(superobject), /* tp_basicsize */ |
| 7782 | 0, /* tp_itemsize */ |
| 7783 | /* methods */ |
| 7784 | super_dealloc, /* tp_dealloc */ |
| 7785 | 0, /* tp_print */ |
| 7786 | 0, /* tp_getattr */ |
| 7787 | 0, /* tp_setattr */ |
| 7788 | 0, /* tp_reserved */ |
| 7789 | super_repr, /* tp_repr */ |
| 7790 | 0, /* tp_as_number */ |
| 7791 | 0, /* tp_as_sequence */ |
| 7792 | 0, /* tp_as_mapping */ |
| 7793 | 0, /* tp_hash */ |
| 7794 | 0, /* tp_call */ |
| 7795 | 0, /* tp_str */ |
| 7796 | super_getattro, /* tp_getattro */ |
| 7797 | 0, /* tp_setattro */ |
| 7798 | 0, /* tp_as_buffer */ |
| 7799 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 7800 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 7801 | super_doc, /* tp_doc */ |
| 7802 | super_traverse, /* tp_traverse */ |
| 7803 | 0, /* tp_clear */ |
| 7804 | 0, /* tp_richcompare */ |
| 7805 | 0, /* tp_weaklistoffset */ |
| 7806 | 0, /* tp_iter */ |
| 7807 | 0, /* tp_iternext */ |
| 7808 | 0, /* tp_methods */ |
| 7809 | super_members, /* tp_members */ |
| 7810 | 0, /* tp_getset */ |
| 7811 | 0, /* tp_base */ |
| 7812 | 0, /* tp_dict */ |
| 7813 | super_descr_get, /* tp_descr_get */ |
| 7814 | 0, /* tp_descr_set */ |
| 7815 | 0, /* tp_dictoffset */ |
| 7816 | super_init, /* tp_init */ |
| 7817 | PyType_GenericAlloc, /* tp_alloc */ |
| 7818 | PyType_GenericNew, /* tp_new */ |
| 7819 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 7820 | }; |