| 1 | n/a | /* Type object implementation */ |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | #include "Python.h" |
|---|
| 4 | n/a | #include "frameobject.h" |
|---|
| 5 | n/a | #include "structmember.h" |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | #include <ctype.h> |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | /* Support type attribute cache */ |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | /* The cache can keep references to the names alive for longer than |
|---|
| 13 | n/a | they normally would. This is why the maximum size is limited to |
|---|
| 14 | n/a | MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large |
|---|
| 15 | n/a | strings are used as attribute names. */ |
|---|
| 16 | n/a | #define MCACHE_MAX_ATTR_SIZE 100 |
|---|
| 17 | n/a | #define MCACHE_SIZE_EXP 12 |
|---|
| 18 | n/a | #define MCACHE_HASH(version, name_hash) \ |
|---|
| 19 | n/a | (((unsigned int)(version) ^ (unsigned int)(name_hash)) \ |
|---|
| 20 | n/a | & ((1 << MCACHE_SIZE_EXP) - 1)) |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | #define MCACHE_HASH_METHOD(type, name) \ |
|---|
| 23 | n/a | MCACHE_HASH((type)->tp_version_tag, \ |
|---|
| 24 | n/a | ((PyASCIIObject *)(name))->hash) |
|---|
| 25 | n/a | #define MCACHE_CACHEABLE_NAME(name) \ |
|---|
| 26 | n/a | PyUnicode_CheckExact(name) && \ |
|---|
| 27 | n/a | PyUnicode_READY(name) != -1 && \ |
|---|
| 28 | n/a | PyUnicode_GET_LENGTH(name) <= MCACHE_MAX_ATTR_SIZE |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | struct method_cache_entry { |
|---|
| 31 | n/a | unsigned int version; |
|---|
| 32 | n/a | PyObject *name; /* reference to exactly a str or None */ |
|---|
| 33 | n/a | PyObject *value; /* borrowed */ |
|---|
| 34 | n/a | }; |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP]; |
|---|
| 37 | n/a | static unsigned int next_version_tag = 0; |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | #define MCACHE_STATS 0 |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | #if MCACHE_STATS |
|---|
| 42 | n/a | static size_t method_cache_hits = 0; |
|---|
| 43 | n/a | static size_t method_cache_misses = 0; |
|---|
| 44 | n/a | static size_t method_cache_collisions = 0; |
|---|
| 45 | n/a | #endif |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | /* alphabetical order */ |
|---|
| 48 | n/a | _Py_IDENTIFIER(__abstractmethods__); |
|---|
| 49 | n/a | _Py_IDENTIFIER(__class__); |
|---|
| 50 | n/a | _Py_IDENTIFIER(__delitem__); |
|---|
| 51 | n/a | _Py_IDENTIFIER(__dict__); |
|---|
| 52 | n/a | _Py_IDENTIFIER(__doc__); |
|---|
| 53 | n/a | _Py_IDENTIFIER(__getattribute__); |
|---|
| 54 | n/a | _Py_IDENTIFIER(__getitem__); |
|---|
| 55 | n/a | _Py_IDENTIFIER(__hash__); |
|---|
| 56 | n/a | _Py_IDENTIFIER(__init_subclass__); |
|---|
| 57 | n/a | _Py_IDENTIFIER(__len__); |
|---|
| 58 | n/a | _Py_IDENTIFIER(__module__); |
|---|
| 59 | n/a | _Py_IDENTIFIER(__name__); |
|---|
| 60 | n/a | _Py_IDENTIFIER(__new__); |
|---|
| 61 | n/a | _Py_IDENTIFIER(__set_name__); |
|---|
| 62 | n/a | _Py_IDENTIFIER(__setitem__); |
|---|
| 63 | n/a | _Py_IDENTIFIER(builtins); |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | static PyObject * |
|---|
| 66 | n/a | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | static void |
|---|
| 69 | n/a | clear_slotdefs(void); |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | /* |
|---|
| 72 | n/a | * finds the beginning of the docstring's introspection signature. |
|---|
| 73 | n/a | * if present, returns a pointer pointing to the first '('. |
|---|
| 74 | n/a | * otherwise returns NULL. |
|---|
| 75 | n/a | * |
|---|
| 76 | n/a | * doesn't guarantee that the signature is valid, only that it |
|---|
| 77 | n/a | * has a valid prefix. (the signature must also pass skip_signature.) |
|---|
| 78 | n/a | */ |
|---|
| 79 | n/a | static const char * |
|---|
| 80 | n/a | find_signature(const char *name, const char *doc) |
|---|
| 81 | n/a | { |
|---|
| 82 | n/a | const char *dot; |
|---|
| 83 | n/a | size_t length; |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | if (!doc) |
|---|
| 86 | n/a | return NULL; |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | assert(name != NULL); |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | /* for dotted names like classes, only use the last component */ |
|---|
| 91 | n/a | dot = strrchr(name, '.'); |
|---|
| 92 | n/a | if (dot) |
|---|
| 93 | n/a | name = dot + 1; |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | length = strlen(name); |
|---|
| 96 | n/a | if (strncmp(doc, name, length)) |
|---|
| 97 | n/a | return NULL; |
|---|
| 98 | n/a | doc += length; |
|---|
| 99 | n/a | if (*doc != '(') |
|---|
| 100 | n/a | return NULL; |
|---|
| 101 | n/a | return doc; |
|---|
| 102 | n/a | } |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | #define SIGNATURE_END_MARKER ")\n--\n\n" |
|---|
| 105 | n/a | #define SIGNATURE_END_MARKER_LENGTH 6 |
|---|
| 106 | n/a | /* |
|---|
| 107 | n/a | * skips past the end of the docstring's instrospection signature. |
|---|
| 108 | n/a | * (assumes doc starts with a valid signature prefix.) |
|---|
| 109 | n/a | */ |
|---|
| 110 | n/a | static const char * |
|---|
| 111 | n/a | skip_signature(const char *doc) |
|---|
| 112 | n/a | { |
|---|
| 113 | n/a | while (*doc) { |
|---|
| 114 | n/a | if ((*doc == *SIGNATURE_END_MARKER) && |
|---|
| 115 | n/a | !strncmp(doc, SIGNATURE_END_MARKER, SIGNATURE_END_MARKER_LENGTH)) |
|---|
| 116 | n/a | return doc + SIGNATURE_END_MARKER_LENGTH; |
|---|
| 117 | n/a | if ((*doc == '\n') && (doc[1] == '\n')) |
|---|
| 118 | n/a | return NULL; |
|---|
| 119 | n/a | doc++; |
|---|
| 120 | n/a | } |
|---|
| 121 | n/a | return NULL; |
|---|
| 122 | n/a | } |
|---|
| 123 | n/a | |
|---|
| 124 | n/a | #ifdef Py_DEBUG |
|---|
| 125 | n/a | static int |
|---|
| 126 | n/a | _PyType_CheckConsistency(PyTypeObject *type) |
|---|
| 127 | n/a | { |
|---|
| 128 | n/a | if (!(type->tp_flags & Py_TPFLAGS_READY)) { |
|---|
| 129 | n/a | /* don't check types before PyType_Ready() */ |
|---|
| 130 | n/a | return 1; |
|---|
| 131 | n/a | } |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | assert(!(type->tp_flags & Py_TPFLAGS_READYING)); |
|---|
| 134 | n/a | assert(type->tp_mro != NULL && PyTuple_Check(type->tp_mro)); |
|---|
| 135 | n/a | assert(type->tp_dict != NULL); |
|---|
| 136 | n/a | return 1; |
|---|
| 137 | n/a | } |
|---|
| 138 | n/a | #endif |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | static const char * |
|---|
| 141 | n/a | _PyType_DocWithoutSignature(const char *name, const char *internal_doc) |
|---|
| 142 | n/a | { |
|---|
| 143 | n/a | const char *doc = find_signature(name, internal_doc); |
|---|
| 144 | n/a | |
|---|
| 145 | n/a | if (doc) { |
|---|
| 146 | n/a | doc = skip_signature(doc); |
|---|
| 147 | n/a | if (doc) |
|---|
| 148 | n/a | return doc; |
|---|
| 149 | n/a | } |
|---|
| 150 | n/a | return internal_doc; |
|---|
| 151 | n/a | } |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | PyObject * |
|---|
| 154 | n/a | _PyType_GetDocFromInternalDoc(const char *name, const char *internal_doc) |
|---|
| 155 | n/a | { |
|---|
| 156 | n/a | const char *doc = _PyType_DocWithoutSignature(name, internal_doc); |
|---|
| 157 | n/a | |
|---|
| 158 | n/a | if (!doc || *doc == '\0') { |
|---|
| 159 | n/a | Py_RETURN_NONE; |
|---|
| 160 | n/a | } |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | return PyUnicode_FromString(doc); |
|---|
| 163 | n/a | } |
|---|
| 164 | n/a | |
|---|
| 165 | n/a | PyObject * |
|---|
| 166 | n/a | _PyType_GetTextSignatureFromInternalDoc(const char *name, const char *internal_doc) |
|---|
| 167 | n/a | { |
|---|
| 168 | n/a | const char *start = find_signature(name, internal_doc); |
|---|
| 169 | n/a | const char *end; |
|---|
| 170 | n/a | |
|---|
| 171 | n/a | if (start) |
|---|
| 172 | n/a | end = skip_signature(start); |
|---|
| 173 | n/a | else |
|---|
| 174 | n/a | end = NULL; |
|---|
| 175 | n/a | if (!end) { |
|---|
| 176 | n/a | Py_RETURN_NONE; |
|---|
| 177 | n/a | } |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | /* back "end" up until it points just past the final ')' */ |
|---|
| 180 | n/a | end -= SIGNATURE_END_MARKER_LENGTH - 1; |
|---|
| 181 | n/a | assert((end - start) >= 2); /* should be "()" at least */ |
|---|
| 182 | n/a | assert(end[-1] == ')'); |
|---|
| 183 | n/a | assert(end[0] == '\n'); |
|---|
| 184 | n/a | return PyUnicode_FromStringAndSize(start, end - start); |
|---|
| 185 | n/a | } |
|---|
| 186 | n/a | |
|---|
| 187 | n/a | unsigned int |
|---|
| 188 | n/a | PyType_ClearCache(void) |
|---|
| 189 | n/a | { |
|---|
| 190 | n/a | Py_ssize_t i; |
|---|
| 191 | n/a | unsigned int cur_version_tag = next_version_tag - 1; |
|---|
| 192 | n/a | |
|---|
| 193 | n/a | #if MCACHE_STATS |
|---|
| 194 | n/a | size_t total = method_cache_hits + method_cache_collisions + method_cache_misses; |
|---|
| 195 | n/a | fprintf(stderr, "-- Method cache hits = %zd (%d%%)\n", |
|---|
| 196 | n/a | method_cache_hits, (int) (100.0 * method_cache_hits / total)); |
|---|
| 197 | n/a | fprintf(stderr, "-- Method cache true misses = %zd (%d%%)\n", |
|---|
| 198 | n/a | method_cache_misses, (int) (100.0 * method_cache_misses / total)); |
|---|
| 199 | n/a | fprintf(stderr, "-- Method cache collisions = %zd (%d%%)\n", |
|---|
| 200 | n/a | method_cache_collisions, (int) (100.0 * method_cache_collisions / total)); |
|---|
| 201 | n/a | fprintf(stderr, "-- Method cache size = %zd KB\n", |
|---|
| 202 | n/a | sizeof(method_cache) / 1024); |
|---|
| 203 | n/a | #endif |
|---|
| 204 | n/a | |
|---|
| 205 | n/a | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { |
|---|
| 206 | n/a | method_cache[i].version = 0; |
|---|
| 207 | n/a | Py_CLEAR(method_cache[i].name); |
|---|
| 208 | n/a | method_cache[i].value = NULL; |
|---|
| 209 | n/a | } |
|---|
| 210 | n/a | next_version_tag = 0; |
|---|
| 211 | n/a | /* mark all version tags as invalid */ |
|---|
| 212 | n/a | PyType_Modified(&PyBaseObject_Type); |
|---|
| 213 | n/a | return cur_version_tag; |
|---|
| 214 | n/a | } |
|---|
| 215 | n/a | |
|---|
| 216 | n/a | void |
|---|
| 217 | n/a | _PyType_Fini(void) |
|---|
| 218 | n/a | { |
|---|
| 219 | n/a | PyType_ClearCache(); |
|---|
| 220 | n/a | clear_slotdefs(); |
|---|
| 221 | n/a | } |
|---|
| 222 | n/a | |
|---|
| 223 | n/a | void |
|---|
| 224 | n/a | PyType_Modified(PyTypeObject *type) |
|---|
| 225 | n/a | { |
|---|
| 226 | n/a | /* Invalidate any cached data for the specified type and all |
|---|
| 227 | n/a | subclasses. This function is called after the base |
|---|
| 228 | n/a | classes, mro, or attributes of the type are altered. |
|---|
| 229 | n/a | |
|---|
| 230 | n/a | Invariants: |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | - Py_TPFLAGS_VALID_VERSION_TAG is never set if |
|---|
| 233 | n/a | Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type |
|---|
| 234 | n/a | objects coming from non-recompiled extension modules) |
|---|
| 235 | n/a | |
|---|
| 236 | n/a | - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type, |
|---|
| 237 | n/a | it must first be set on all super types. |
|---|
| 238 | n/a | |
|---|
| 239 | n/a | This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a |
|---|
| 240 | n/a | type (so it must first clear it on all subclasses). The |
|---|
| 241 | n/a | tp_version_tag value is meaningless unless this flag is set. |
|---|
| 242 | n/a | We don't assign new version tags eagerly, but only as |
|---|
| 243 | n/a | needed. |
|---|
| 244 | n/a | */ |
|---|
| 245 | n/a | PyObject *raw, *ref; |
|---|
| 246 | n/a | Py_ssize_t i; |
|---|
| 247 | n/a | |
|---|
| 248 | n/a | if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
|---|
| 249 | n/a | return; |
|---|
| 250 | n/a | |
|---|
| 251 | n/a | raw = type->tp_subclasses; |
|---|
| 252 | n/a | if (raw != NULL) { |
|---|
| 253 | n/a | assert(PyDict_CheckExact(raw)); |
|---|
| 254 | n/a | i = 0; |
|---|
| 255 | n/a | while (PyDict_Next(raw, &i, NULL, &ref)) { |
|---|
| 256 | n/a | assert(PyWeakref_CheckRef(ref)); |
|---|
| 257 | n/a | ref = PyWeakref_GET_OBJECT(ref); |
|---|
| 258 | n/a | if (ref != Py_None) { |
|---|
| 259 | n/a | PyType_Modified((PyTypeObject *)ref); |
|---|
| 260 | n/a | } |
|---|
| 261 | n/a | } |
|---|
| 262 | n/a | } |
|---|
| 263 | n/a | type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG; |
|---|
| 264 | n/a | } |
|---|
| 265 | n/a | |
|---|
| 266 | n/a | static void |
|---|
| 267 | n/a | type_mro_modified(PyTypeObject *type, PyObject *bases) { |
|---|
| 268 | n/a | /* |
|---|
| 269 | n/a | Check that all base classes or elements of the MRO of type are |
|---|
| 270 | n/a | able to be cached. This function is called after the base |
|---|
| 271 | n/a | classes or mro of the type are altered. |
|---|
| 272 | n/a | |
|---|
| 273 | n/a | Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type |
|---|
| 274 | n/a | has a custom MRO that includes a type which is not officially |
|---|
| 275 | n/a | super type. |
|---|
| 276 | n/a | |
|---|
| 277 | n/a | Called from mro_internal, which will subsequently be called on |
|---|
| 278 | n/a | each subclass when their mro is recursively updated. |
|---|
| 279 | n/a | */ |
|---|
| 280 | n/a | Py_ssize_t i, n; |
|---|
| 281 | n/a | int clear = 0; |
|---|
| 282 | n/a | |
|---|
| 283 | n/a | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) |
|---|
| 284 | n/a | return; |
|---|
| 285 | n/a | |
|---|
| 286 | n/a | n = PyTuple_GET_SIZE(bases); |
|---|
| 287 | n/a | for (i = 0; i < n; i++) { |
|---|
| 288 | n/a | PyObject *b = PyTuple_GET_ITEM(bases, i); |
|---|
| 289 | n/a | PyTypeObject *cls; |
|---|
| 290 | n/a | |
|---|
| 291 | n/a | assert(PyType_Check(b)); |
|---|
| 292 | n/a | cls = (PyTypeObject *)b; |
|---|
| 293 | n/a | |
|---|
| 294 | n/a | if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) || |
|---|
| 295 | n/a | !PyType_IsSubtype(type, cls)) { |
|---|
| 296 | n/a | clear = 1; |
|---|
| 297 | n/a | break; |
|---|
| 298 | n/a | } |
|---|
| 299 | n/a | } |
|---|
| 300 | n/a | |
|---|
| 301 | n/a | if (clear) |
|---|
| 302 | n/a | type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG| |
|---|
| 303 | n/a | Py_TPFLAGS_VALID_VERSION_TAG); |
|---|
| 304 | n/a | } |
|---|
| 305 | n/a | |
|---|
| 306 | n/a | static int |
|---|
| 307 | n/a | assign_version_tag(PyTypeObject *type) |
|---|
| 308 | n/a | { |
|---|
| 309 | n/a | /* Ensure that the tp_version_tag is valid and set |
|---|
| 310 | n/a | Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this |
|---|
| 311 | n/a | must first be done on all super classes. Return 0 if this |
|---|
| 312 | n/a | cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG. |
|---|
| 313 | n/a | */ |
|---|
| 314 | n/a | Py_ssize_t i, n; |
|---|
| 315 | n/a | PyObject *bases; |
|---|
| 316 | n/a | |
|---|
| 317 | n/a | if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
|---|
| 318 | n/a | return 1; |
|---|
| 319 | n/a | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) |
|---|
| 320 | n/a | return 0; |
|---|
| 321 | n/a | if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) |
|---|
| 322 | n/a | return 0; |
|---|
| 323 | n/a | |
|---|
| 324 | n/a | type->tp_version_tag = next_version_tag++; |
|---|
| 325 | n/a | /* for stress-testing: next_version_tag &= 0xFF; */ |
|---|
| 326 | n/a | |
|---|
| 327 | n/a | if (type->tp_version_tag == 0) { |
|---|
| 328 | n/a | /* wrap-around or just starting Python - clear the whole |
|---|
| 329 | n/a | cache by filling names with references to Py_None. |
|---|
| 330 | n/a | Values are also set to NULL for added protection, as they |
|---|
| 331 | n/a | are borrowed reference */ |
|---|
| 332 | n/a | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { |
|---|
| 333 | n/a | method_cache[i].value = NULL; |
|---|
| 334 | n/a | Py_INCREF(Py_None); |
|---|
| 335 | n/a | Py_XSETREF(method_cache[i].name, Py_None); |
|---|
| 336 | n/a | } |
|---|
| 337 | n/a | /* mark all version tags as invalid */ |
|---|
| 338 | n/a | PyType_Modified(&PyBaseObject_Type); |
|---|
| 339 | n/a | return 1; |
|---|
| 340 | n/a | } |
|---|
| 341 | n/a | bases = type->tp_bases; |
|---|
| 342 | n/a | n = PyTuple_GET_SIZE(bases); |
|---|
| 343 | n/a | for (i = 0; i < n; i++) { |
|---|
| 344 | n/a | PyObject *b = PyTuple_GET_ITEM(bases, i); |
|---|
| 345 | n/a | assert(PyType_Check(b)); |
|---|
| 346 | n/a | if (!assign_version_tag((PyTypeObject *)b)) |
|---|
| 347 | n/a | return 0; |
|---|
| 348 | n/a | } |
|---|
| 349 | n/a | type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG; |
|---|
| 350 | n/a | return 1; |
|---|
| 351 | n/a | } |
|---|
| 352 | n/a | |
|---|
| 353 | n/a | |
|---|
| 354 | n/a | static PyMemberDef type_members[] = { |
|---|
| 355 | n/a | {"__basicsize__", T_PYSSIZET, offsetof(PyTypeObject,tp_basicsize),READONLY}, |
|---|
| 356 | n/a | {"__itemsize__", T_PYSSIZET, offsetof(PyTypeObject, tp_itemsize), READONLY}, |
|---|
| 357 | n/a | {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY}, |
|---|
| 358 | n/a | {"__weakrefoffset__", T_LONG, |
|---|
| 359 | n/a | offsetof(PyTypeObject, tp_weaklistoffset), READONLY}, |
|---|
| 360 | n/a | {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY}, |
|---|
| 361 | n/a | {"__dictoffset__", T_LONG, |
|---|
| 362 | n/a | offsetof(PyTypeObject, tp_dictoffset), READONLY}, |
|---|
| 363 | n/a | {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY}, |
|---|
| 364 | n/a | {0} |
|---|
| 365 | n/a | }; |
|---|
| 366 | n/a | |
|---|
| 367 | n/a | static int |
|---|
| 368 | n/a | check_set_special_type_attr(PyTypeObject *type, PyObject *value, const char *name) |
|---|
| 369 | n/a | { |
|---|
| 370 | n/a | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
|---|
| 371 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 372 | n/a | "can't set %s.%s", type->tp_name, name); |
|---|
| 373 | n/a | return 0; |
|---|
| 374 | n/a | } |
|---|
| 375 | n/a | if (!value) { |
|---|
| 376 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 377 | n/a | "can't delete %s.%s", type->tp_name, name); |
|---|
| 378 | n/a | return 0; |
|---|
| 379 | n/a | } |
|---|
| 380 | n/a | return 1; |
|---|
| 381 | n/a | } |
|---|
| 382 | n/a | |
|---|
| 383 | n/a | static PyObject * |
|---|
| 384 | n/a | type_name(PyTypeObject *type, void *context) |
|---|
| 385 | n/a | { |
|---|
| 386 | n/a | const char *s; |
|---|
| 387 | n/a | |
|---|
| 388 | n/a | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
|---|
| 389 | n/a | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
|---|
| 390 | n/a | |
|---|
| 391 | n/a | Py_INCREF(et->ht_name); |
|---|
| 392 | n/a | return et->ht_name; |
|---|
| 393 | n/a | } |
|---|
| 394 | n/a | else { |
|---|
| 395 | n/a | s = strrchr(type->tp_name, '.'); |
|---|
| 396 | n/a | if (s == NULL) |
|---|
| 397 | n/a | s = type->tp_name; |
|---|
| 398 | n/a | else |
|---|
| 399 | n/a | s++; |
|---|
| 400 | n/a | return PyUnicode_FromString(s); |
|---|
| 401 | n/a | } |
|---|
| 402 | n/a | } |
|---|
| 403 | n/a | |
|---|
| 404 | n/a | static PyObject * |
|---|
| 405 | n/a | type_qualname(PyTypeObject *type, void *context) |
|---|
| 406 | n/a | { |
|---|
| 407 | n/a | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
|---|
| 408 | n/a | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
|---|
| 409 | n/a | Py_INCREF(et->ht_qualname); |
|---|
| 410 | n/a | return et->ht_qualname; |
|---|
| 411 | n/a | } |
|---|
| 412 | n/a | else { |
|---|
| 413 | n/a | return type_name(type, context); |
|---|
| 414 | n/a | } |
|---|
| 415 | n/a | } |
|---|
| 416 | n/a | |
|---|
| 417 | n/a | static int |
|---|
| 418 | n/a | type_set_name(PyTypeObject *type, PyObject *value, void *context) |
|---|
| 419 | n/a | { |
|---|
| 420 | n/a | const char *tp_name; |
|---|
| 421 | n/a | Py_ssize_t name_size; |
|---|
| 422 | n/a | |
|---|
| 423 | n/a | if (!check_set_special_type_attr(type, value, "__name__")) |
|---|
| 424 | n/a | return -1; |
|---|
| 425 | n/a | if (!PyUnicode_Check(value)) { |
|---|
| 426 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 427 | n/a | "can only assign string to %s.__name__, not '%s'", |
|---|
| 428 | n/a | type->tp_name, Py_TYPE(value)->tp_name); |
|---|
| 429 | n/a | return -1; |
|---|
| 430 | n/a | } |
|---|
| 431 | n/a | |
|---|
| 432 | n/a | tp_name = PyUnicode_AsUTF8AndSize(value, &name_size); |
|---|
| 433 | n/a | if (tp_name == NULL) |
|---|
| 434 | n/a | return -1; |
|---|
| 435 | n/a | if (strlen(tp_name) != (size_t)name_size) { |
|---|
| 436 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 437 | n/a | "type name must not contain null characters"); |
|---|
| 438 | n/a | return -1; |
|---|
| 439 | n/a | } |
|---|
| 440 | n/a | |
|---|
| 441 | n/a | type->tp_name = tp_name; |
|---|
| 442 | n/a | Py_INCREF(value); |
|---|
| 443 | n/a | Py_SETREF(((PyHeapTypeObject*)type)->ht_name, value); |
|---|
| 444 | n/a | |
|---|
| 445 | n/a | return 0; |
|---|
| 446 | n/a | } |
|---|
| 447 | n/a | |
|---|
| 448 | n/a | static int |
|---|
| 449 | n/a | type_set_qualname(PyTypeObject *type, PyObject *value, void *context) |
|---|
| 450 | n/a | { |
|---|
| 451 | n/a | PyHeapTypeObject* et; |
|---|
| 452 | n/a | |
|---|
| 453 | n/a | if (!check_set_special_type_attr(type, value, "__qualname__")) |
|---|
| 454 | n/a | return -1; |
|---|
| 455 | n/a | if (!PyUnicode_Check(value)) { |
|---|
| 456 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 457 | n/a | "can only assign string to %s.__qualname__, not '%s'", |
|---|
| 458 | n/a | type->tp_name, Py_TYPE(value)->tp_name); |
|---|
| 459 | n/a | return -1; |
|---|
| 460 | n/a | } |
|---|
| 461 | n/a | |
|---|
| 462 | n/a | et = (PyHeapTypeObject*)type; |
|---|
| 463 | n/a | Py_INCREF(value); |
|---|
| 464 | n/a | Py_SETREF(et->ht_qualname, value); |
|---|
| 465 | n/a | return 0; |
|---|
| 466 | n/a | } |
|---|
| 467 | n/a | |
|---|
| 468 | n/a | static PyObject * |
|---|
| 469 | n/a | type_module(PyTypeObject *type, void *context) |
|---|
| 470 | n/a | { |
|---|
| 471 | n/a | PyObject *mod; |
|---|
| 472 | n/a | |
|---|
| 473 | n/a | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
|---|
| 474 | n/a | mod = _PyDict_GetItemId(type->tp_dict, &PyId___module__); |
|---|
| 475 | n/a | if (mod == NULL) { |
|---|
| 476 | n/a | PyErr_Format(PyExc_AttributeError, "__module__"); |
|---|
| 477 | n/a | return NULL; |
|---|
| 478 | n/a | } |
|---|
| 479 | n/a | Py_INCREF(mod); |
|---|
| 480 | n/a | } |
|---|
| 481 | n/a | else { |
|---|
| 482 | n/a | const char *s = strrchr(type->tp_name, '.'); |
|---|
| 483 | n/a | if (s != NULL) { |
|---|
| 484 | n/a | mod = PyUnicode_FromStringAndSize( |
|---|
| 485 | n/a | type->tp_name, (Py_ssize_t)(s - type->tp_name)); |
|---|
| 486 | n/a | if (mod != NULL) |
|---|
| 487 | n/a | PyUnicode_InternInPlace(&mod); |
|---|
| 488 | n/a | } |
|---|
| 489 | n/a | else { |
|---|
| 490 | n/a | mod = _PyUnicode_FromId(&PyId_builtins); |
|---|
| 491 | n/a | Py_XINCREF(mod); |
|---|
| 492 | n/a | } |
|---|
| 493 | n/a | } |
|---|
| 494 | n/a | return mod; |
|---|
| 495 | n/a | } |
|---|
| 496 | n/a | |
|---|
| 497 | n/a | static int |
|---|
| 498 | n/a | type_set_module(PyTypeObject *type, PyObject *value, void *context) |
|---|
| 499 | n/a | { |
|---|
| 500 | n/a | if (!check_set_special_type_attr(type, value, "__module__")) |
|---|
| 501 | n/a | return -1; |
|---|
| 502 | n/a | |
|---|
| 503 | n/a | PyType_Modified(type); |
|---|
| 504 | n/a | |
|---|
| 505 | n/a | return _PyDict_SetItemId(type->tp_dict, &PyId___module__, value); |
|---|
| 506 | n/a | } |
|---|
| 507 | n/a | |
|---|
| 508 | n/a | static PyObject * |
|---|
| 509 | n/a | type_abstractmethods(PyTypeObject *type, void *context) |
|---|
| 510 | n/a | { |
|---|
| 511 | n/a | PyObject *mod = NULL; |
|---|
| 512 | n/a | /* type itself has an __abstractmethods__ descriptor (this). Don't return |
|---|
| 513 | n/a | that. */ |
|---|
| 514 | n/a | if (type != &PyType_Type) |
|---|
| 515 | n/a | mod = _PyDict_GetItemId(type->tp_dict, &PyId___abstractmethods__); |
|---|
| 516 | n/a | if (!mod) { |
|---|
| 517 | n/a | PyObject *message = _PyUnicode_FromId(&PyId___abstractmethods__); |
|---|
| 518 | n/a | if (message) |
|---|
| 519 | n/a | PyErr_SetObject(PyExc_AttributeError, message); |
|---|
| 520 | n/a | return NULL; |
|---|
| 521 | n/a | } |
|---|
| 522 | n/a | Py_INCREF(mod); |
|---|
| 523 | n/a | return mod; |
|---|
| 524 | n/a | } |
|---|
| 525 | n/a | |
|---|
| 526 | n/a | static int |
|---|
| 527 | n/a | type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context) |
|---|
| 528 | n/a | { |
|---|
| 529 | n/a | /* __abstractmethods__ should only be set once on a type, in |
|---|
| 530 | n/a | abc.ABCMeta.__new__, so this function doesn't do anything |
|---|
| 531 | n/a | special to update subclasses. |
|---|
| 532 | n/a | */ |
|---|
| 533 | n/a | int abstract, res; |
|---|
| 534 | n/a | if (value != NULL) { |
|---|
| 535 | n/a | abstract = PyObject_IsTrue(value); |
|---|
| 536 | n/a | if (abstract < 0) |
|---|
| 537 | n/a | return -1; |
|---|
| 538 | n/a | res = _PyDict_SetItemId(type->tp_dict, &PyId___abstractmethods__, value); |
|---|
| 539 | n/a | } |
|---|
| 540 | n/a | else { |
|---|
| 541 | n/a | abstract = 0; |
|---|
| 542 | n/a | res = _PyDict_DelItemId(type->tp_dict, &PyId___abstractmethods__); |
|---|
| 543 | n/a | if (res && PyErr_ExceptionMatches(PyExc_KeyError)) { |
|---|
| 544 | n/a | PyObject *message = _PyUnicode_FromId(&PyId___abstractmethods__); |
|---|
| 545 | n/a | if (message) |
|---|
| 546 | n/a | PyErr_SetObject(PyExc_AttributeError, message); |
|---|
| 547 | n/a | return -1; |
|---|
| 548 | n/a | } |
|---|
| 549 | n/a | } |
|---|
| 550 | n/a | if (res == 0) { |
|---|
| 551 | n/a | PyType_Modified(type); |
|---|
| 552 | n/a | if (abstract) |
|---|
| 553 | n/a | type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT; |
|---|
| 554 | n/a | else |
|---|
| 555 | n/a | type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT; |
|---|
| 556 | n/a | } |
|---|
| 557 | n/a | return res; |
|---|
| 558 | n/a | } |
|---|
| 559 | n/a | |
|---|
| 560 | n/a | static PyObject * |
|---|
| 561 | n/a | type_get_bases(PyTypeObject *type, void *context) |
|---|
| 562 | n/a | { |
|---|
| 563 | n/a | Py_INCREF(type->tp_bases); |
|---|
| 564 | n/a | return type->tp_bases; |
|---|
| 565 | n/a | } |
|---|
| 566 | n/a | |
|---|
| 567 | n/a | static PyTypeObject *best_base(PyObject *); |
|---|
| 568 | n/a | static int mro_internal(PyTypeObject *, PyObject **); |
|---|
| 569 | n/a | static int type_is_subtype_base_chain(PyTypeObject *, PyTypeObject *); |
|---|
| 570 | n/a | static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, const char *); |
|---|
| 571 | n/a | static int add_subclass(PyTypeObject*, PyTypeObject*); |
|---|
| 572 | n/a | static int add_all_subclasses(PyTypeObject *type, PyObject *bases); |
|---|
| 573 | n/a | static void remove_subclass(PyTypeObject *, PyTypeObject *); |
|---|
| 574 | n/a | static void remove_all_subclasses(PyTypeObject *type, PyObject *bases); |
|---|
| 575 | n/a | static void update_all_slots(PyTypeObject *); |
|---|
| 576 | n/a | |
|---|
| 577 | n/a | typedef int (*update_callback)(PyTypeObject *, void *); |
|---|
| 578 | n/a | static int update_subclasses(PyTypeObject *type, PyObject *name, |
|---|
| 579 | n/a | update_callback callback, void *data); |
|---|
| 580 | n/a | static int recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
|---|
| 581 | n/a | update_callback callback, void *data); |
|---|
| 582 | n/a | static PyObject *type_subclasses(PyTypeObject *type, PyObject *ignored); |
|---|
| 583 | n/a | |
|---|
| 584 | n/a | static int |
|---|
| 585 | n/a | mro_hierarchy(PyTypeObject *type, PyObject *temp) |
|---|
| 586 | n/a | { |
|---|
| 587 | n/a | int res; |
|---|
| 588 | n/a | PyObject *new_mro, *old_mro; |
|---|
| 589 | n/a | PyObject *tuple; |
|---|
| 590 | n/a | PyObject *subclasses; |
|---|
| 591 | n/a | Py_ssize_t i, n; |
|---|
| 592 | n/a | |
|---|
| 593 | n/a | res = mro_internal(type, &old_mro); |
|---|
| 594 | n/a | if (res <= 0) |
|---|
| 595 | n/a | /* error / reentrance */ |
|---|
| 596 | n/a | return res; |
|---|
| 597 | n/a | new_mro = type->tp_mro; |
|---|
| 598 | n/a | |
|---|
| 599 | n/a | if (old_mro != NULL) |
|---|
| 600 | n/a | tuple = PyTuple_Pack(3, type, new_mro, old_mro); |
|---|
| 601 | n/a | else |
|---|
| 602 | n/a | tuple = PyTuple_Pack(2, type, new_mro); |
|---|
| 603 | n/a | |
|---|
| 604 | n/a | if (tuple != NULL) |
|---|
| 605 | n/a | res = PyList_Append(temp, tuple); |
|---|
| 606 | n/a | else |
|---|
| 607 | n/a | res = -1; |
|---|
| 608 | n/a | Py_XDECREF(tuple); |
|---|
| 609 | n/a | |
|---|
| 610 | n/a | if (res < 0) { |
|---|
| 611 | n/a | type->tp_mro = old_mro; |
|---|
| 612 | n/a | Py_DECREF(new_mro); |
|---|
| 613 | n/a | return -1; |
|---|
| 614 | n/a | } |
|---|
| 615 | n/a | Py_XDECREF(old_mro); |
|---|
| 616 | n/a | |
|---|
| 617 | n/a | /* Obtain a copy of subclasses list to iterate over. |
|---|
| 618 | n/a | |
|---|
| 619 | n/a | Otherwise type->tp_subclasses might be altered |
|---|
| 620 | n/a | in the middle of the loop, for example, through a custom mro(), |
|---|
| 621 | n/a | by invoking type_set_bases on some subclass of the type |
|---|
| 622 | n/a | which in turn calls remove_subclass/add_subclass on this type. |
|---|
| 623 | n/a | |
|---|
| 624 | n/a | Finally, this makes things simple avoiding the need to deal |
|---|
| 625 | n/a | with dictionary iterators and weak references. |
|---|
| 626 | n/a | */ |
|---|
| 627 | n/a | subclasses = type_subclasses(type, NULL); |
|---|
| 628 | n/a | if (subclasses == NULL) |
|---|
| 629 | n/a | return -1; |
|---|
| 630 | n/a | n = PyList_GET_SIZE(subclasses); |
|---|
| 631 | n/a | for (i = 0; i < n; i++) { |
|---|
| 632 | n/a | PyTypeObject *subclass; |
|---|
| 633 | n/a | subclass = (PyTypeObject *)PyList_GET_ITEM(subclasses, i); |
|---|
| 634 | n/a | res = mro_hierarchy(subclass, temp); |
|---|
| 635 | n/a | if (res < 0) |
|---|
| 636 | n/a | break; |
|---|
| 637 | n/a | } |
|---|
| 638 | n/a | Py_DECREF(subclasses); |
|---|
| 639 | n/a | |
|---|
| 640 | n/a | return res; |
|---|
| 641 | n/a | } |
|---|
| 642 | n/a | |
|---|
| 643 | n/a | static int |
|---|
| 644 | n/a | type_set_bases(PyTypeObject *type, PyObject *new_bases, void *context) |
|---|
| 645 | n/a | { |
|---|
| 646 | n/a | int res = 0; |
|---|
| 647 | n/a | PyObject *temp; |
|---|
| 648 | n/a | PyObject *old_bases; |
|---|
| 649 | n/a | PyTypeObject *new_base, *old_base; |
|---|
| 650 | n/a | Py_ssize_t i; |
|---|
| 651 | n/a | |
|---|
| 652 | n/a | if (!check_set_special_type_attr(type, new_bases, "__bases__")) |
|---|
| 653 | n/a | return -1; |
|---|
| 654 | n/a | if (!PyTuple_Check(new_bases)) { |
|---|
| 655 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 656 | n/a | "can only assign tuple to %s.__bases__, not %s", |
|---|
| 657 | n/a | type->tp_name, Py_TYPE(new_bases)->tp_name); |
|---|
| 658 | n/a | return -1; |
|---|
| 659 | n/a | } |
|---|
| 660 | n/a | if (PyTuple_GET_SIZE(new_bases) == 0) { |
|---|
| 661 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 662 | n/a | "can only assign non-empty tuple to %s.__bases__, not ()", |
|---|
| 663 | n/a | type->tp_name); |
|---|
| 664 | n/a | return -1; |
|---|
| 665 | n/a | } |
|---|
| 666 | n/a | for (i = 0; i < PyTuple_GET_SIZE(new_bases); i++) { |
|---|
| 667 | n/a | PyObject *ob; |
|---|
| 668 | n/a | PyTypeObject *base; |
|---|
| 669 | n/a | |
|---|
| 670 | n/a | ob = PyTuple_GET_ITEM(new_bases, i); |
|---|
| 671 | n/a | if (!PyType_Check(ob)) { |
|---|
| 672 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 673 | n/a | "%s.__bases__ must be tuple of classes, not '%s'", |
|---|
| 674 | n/a | type->tp_name, Py_TYPE(ob)->tp_name); |
|---|
| 675 | n/a | return -1; |
|---|
| 676 | n/a | } |
|---|
| 677 | n/a | |
|---|
| 678 | n/a | base = (PyTypeObject*)ob; |
|---|
| 679 | n/a | if (PyType_IsSubtype(base, type) || |
|---|
| 680 | n/a | /* In case of reentering here again through a custom mro() |
|---|
| 681 | n/a | the above check is not enough since it relies on |
|---|
| 682 | n/a | base->tp_mro which would gonna be updated inside |
|---|
| 683 | n/a | mro_internal only upon returning from the mro(). |
|---|
| 684 | n/a | |
|---|
| 685 | n/a | However, base->tp_base has already been assigned (see |
|---|
| 686 | n/a | below), which in turn may cause an inheritance cycle |
|---|
| 687 | n/a | through tp_base chain. And this is definitely |
|---|
| 688 | n/a | not what you want to ever happen. */ |
|---|
| 689 | n/a | (base->tp_mro != NULL && type_is_subtype_base_chain(base, type))) { |
|---|
| 690 | n/a | |
|---|
| 691 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 692 | n/a | "a __bases__ item causes an inheritance cycle"); |
|---|
| 693 | n/a | return -1; |
|---|
| 694 | n/a | } |
|---|
| 695 | n/a | } |
|---|
| 696 | n/a | |
|---|
| 697 | n/a | new_base = best_base(new_bases); |
|---|
| 698 | n/a | if (new_base == NULL) |
|---|
| 699 | n/a | return -1; |
|---|
| 700 | n/a | |
|---|
| 701 | n/a | if (!compatible_for_assignment(type->tp_base, new_base, "__bases__")) |
|---|
| 702 | n/a | return -1; |
|---|
| 703 | n/a | |
|---|
| 704 | n/a | Py_INCREF(new_bases); |
|---|
| 705 | n/a | Py_INCREF(new_base); |
|---|
| 706 | n/a | |
|---|
| 707 | n/a | old_bases = type->tp_bases; |
|---|
| 708 | n/a | old_base = type->tp_base; |
|---|
| 709 | n/a | |
|---|
| 710 | n/a | type->tp_bases = new_bases; |
|---|
| 711 | n/a | type->tp_base = new_base; |
|---|
| 712 | n/a | |
|---|
| 713 | n/a | temp = PyList_New(0); |
|---|
| 714 | n/a | if (temp == NULL) |
|---|
| 715 | n/a | goto bail; |
|---|
| 716 | n/a | if (mro_hierarchy(type, temp) < 0) |
|---|
| 717 | n/a | goto undo; |
|---|
| 718 | n/a | Py_DECREF(temp); |
|---|
| 719 | n/a | |
|---|
| 720 | n/a | /* Take no action in case if type->tp_bases has been replaced |
|---|
| 721 | n/a | through reentrance. */ |
|---|
| 722 | n/a | if (type->tp_bases == new_bases) { |
|---|
| 723 | n/a | /* any base that was in __bases__ but now isn't, we |
|---|
| 724 | n/a | need to remove |type| from its tp_subclasses. |
|---|
| 725 | n/a | conversely, any class now in __bases__ that wasn't |
|---|
| 726 | n/a | needs to have |type| added to its subclasses. */ |
|---|
| 727 | n/a | |
|---|
| 728 | n/a | /* for now, sod that: just remove from all old_bases, |
|---|
| 729 | n/a | add to all new_bases */ |
|---|
| 730 | n/a | remove_all_subclasses(type, old_bases); |
|---|
| 731 | n/a | res = add_all_subclasses(type, new_bases); |
|---|
| 732 | n/a | update_all_slots(type); |
|---|
| 733 | n/a | } |
|---|
| 734 | n/a | |
|---|
| 735 | n/a | Py_DECREF(old_bases); |
|---|
| 736 | n/a | Py_DECREF(old_base); |
|---|
| 737 | n/a | |
|---|
| 738 | n/a | assert(_PyType_CheckConsistency(type)); |
|---|
| 739 | n/a | return res; |
|---|
| 740 | n/a | |
|---|
| 741 | n/a | undo: |
|---|
| 742 | n/a | for (i = PyList_GET_SIZE(temp) - 1; i >= 0; i--) { |
|---|
| 743 | n/a | PyTypeObject *cls; |
|---|
| 744 | n/a | PyObject *new_mro, *old_mro = NULL; |
|---|
| 745 | n/a | |
|---|
| 746 | n/a | PyArg_UnpackTuple(PyList_GET_ITEM(temp, i), |
|---|
| 747 | n/a | "", 2, 3, &cls, &new_mro, &old_mro); |
|---|
| 748 | n/a | /* Do not rollback if cls has a newer version of MRO. */ |
|---|
| 749 | n/a | if (cls->tp_mro == new_mro) { |
|---|
| 750 | n/a | Py_XINCREF(old_mro); |
|---|
| 751 | n/a | cls->tp_mro = old_mro; |
|---|
| 752 | n/a | Py_DECREF(new_mro); |
|---|
| 753 | n/a | } |
|---|
| 754 | n/a | } |
|---|
| 755 | n/a | Py_DECREF(temp); |
|---|
| 756 | n/a | |
|---|
| 757 | n/a | bail: |
|---|
| 758 | n/a | if (type->tp_bases == new_bases) { |
|---|
| 759 | n/a | assert(type->tp_base == new_base); |
|---|
| 760 | n/a | |
|---|
| 761 | n/a | type->tp_bases = old_bases; |
|---|
| 762 | n/a | type->tp_base = old_base; |
|---|
| 763 | n/a | |
|---|
| 764 | n/a | Py_DECREF(new_bases); |
|---|
| 765 | n/a | Py_DECREF(new_base); |
|---|
| 766 | n/a | } |
|---|
| 767 | n/a | else { |
|---|
| 768 | n/a | Py_DECREF(old_bases); |
|---|
| 769 | n/a | Py_DECREF(old_base); |
|---|
| 770 | n/a | } |
|---|
| 771 | n/a | |
|---|
| 772 | n/a | assert(_PyType_CheckConsistency(type)); |
|---|
| 773 | n/a | return -1; |
|---|
| 774 | n/a | } |
|---|
| 775 | n/a | |
|---|
| 776 | n/a | static PyObject * |
|---|
| 777 | n/a | type_dict(PyTypeObject *type, void *context) |
|---|
| 778 | n/a | { |
|---|
| 779 | n/a | if (type->tp_dict == NULL) { |
|---|
| 780 | n/a | Py_RETURN_NONE; |
|---|
| 781 | n/a | } |
|---|
| 782 | n/a | return PyDictProxy_New(type->tp_dict); |
|---|
| 783 | n/a | } |
|---|
| 784 | n/a | |
|---|
| 785 | n/a | static PyObject * |
|---|
| 786 | n/a | type_get_doc(PyTypeObject *type, void *context) |
|---|
| 787 | n/a | { |
|---|
| 788 | n/a | PyObject *result; |
|---|
| 789 | n/a | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) { |
|---|
| 790 | n/a | return _PyType_GetDocFromInternalDoc(type->tp_name, type->tp_doc); |
|---|
| 791 | n/a | } |
|---|
| 792 | n/a | result = _PyDict_GetItemId(type->tp_dict, &PyId___doc__); |
|---|
| 793 | n/a | if (result == NULL) { |
|---|
| 794 | n/a | result = Py_None; |
|---|
| 795 | n/a | Py_INCREF(result); |
|---|
| 796 | n/a | } |
|---|
| 797 | n/a | else if (Py_TYPE(result)->tp_descr_get) { |
|---|
| 798 | n/a | result = Py_TYPE(result)->tp_descr_get(result, NULL, |
|---|
| 799 | n/a | (PyObject *)type); |
|---|
| 800 | n/a | } |
|---|
| 801 | n/a | else { |
|---|
| 802 | n/a | Py_INCREF(result); |
|---|
| 803 | n/a | } |
|---|
| 804 | n/a | return result; |
|---|
| 805 | n/a | } |
|---|
| 806 | n/a | |
|---|
| 807 | n/a | static PyObject * |
|---|
| 808 | n/a | type_get_text_signature(PyTypeObject *type, void *context) |
|---|
| 809 | n/a | { |
|---|
| 810 | n/a | return _PyType_GetTextSignatureFromInternalDoc(type->tp_name, type->tp_doc); |
|---|
| 811 | n/a | } |
|---|
| 812 | n/a | |
|---|
| 813 | n/a | static int |
|---|
| 814 | n/a | type_set_doc(PyTypeObject *type, PyObject *value, void *context) |
|---|
| 815 | n/a | { |
|---|
| 816 | n/a | if (!check_set_special_type_attr(type, value, "__doc__")) |
|---|
| 817 | n/a | return -1; |
|---|
| 818 | n/a | PyType_Modified(type); |
|---|
| 819 | n/a | return _PyDict_SetItemId(type->tp_dict, &PyId___doc__, value); |
|---|
| 820 | n/a | } |
|---|
| 821 | n/a | |
|---|
| 822 | n/a | static PyObject * |
|---|
| 823 | n/a | type___instancecheck__(PyObject *type, PyObject *inst) |
|---|
| 824 | n/a | { |
|---|
| 825 | n/a | switch (_PyObject_RealIsInstance(inst, type)) { |
|---|
| 826 | n/a | case -1: |
|---|
| 827 | n/a | return NULL; |
|---|
| 828 | n/a | case 0: |
|---|
| 829 | n/a | Py_RETURN_FALSE; |
|---|
| 830 | n/a | default: |
|---|
| 831 | n/a | Py_RETURN_TRUE; |
|---|
| 832 | n/a | } |
|---|
| 833 | n/a | } |
|---|
| 834 | n/a | |
|---|
| 835 | n/a | |
|---|
| 836 | n/a | static PyObject * |
|---|
| 837 | n/a | type___subclasscheck__(PyObject *type, PyObject *inst) |
|---|
| 838 | n/a | { |
|---|
| 839 | n/a | switch (_PyObject_RealIsSubclass(inst, type)) { |
|---|
| 840 | n/a | case -1: |
|---|
| 841 | n/a | return NULL; |
|---|
| 842 | n/a | case 0: |
|---|
| 843 | n/a | Py_RETURN_FALSE; |
|---|
| 844 | n/a | default: |
|---|
| 845 | n/a | Py_RETURN_TRUE; |
|---|
| 846 | n/a | } |
|---|
| 847 | n/a | } |
|---|
| 848 | n/a | |
|---|
| 849 | n/a | |
|---|
| 850 | n/a | static PyGetSetDef type_getsets[] = { |
|---|
| 851 | n/a | {"__name__", (getter)type_name, (setter)type_set_name, NULL}, |
|---|
| 852 | n/a | {"__qualname__", (getter)type_qualname, (setter)type_set_qualname, NULL}, |
|---|
| 853 | n/a | {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL}, |
|---|
| 854 | n/a | {"__module__", (getter)type_module, (setter)type_set_module, NULL}, |
|---|
| 855 | n/a | {"__abstractmethods__", (getter)type_abstractmethods, |
|---|
| 856 | n/a | (setter)type_set_abstractmethods, NULL}, |
|---|
| 857 | n/a | {"__dict__", (getter)type_dict, NULL, NULL}, |
|---|
| 858 | n/a | {"__doc__", (getter)type_get_doc, (setter)type_set_doc, NULL}, |
|---|
| 859 | n/a | {"__text_signature__", (getter)type_get_text_signature, NULL, NULL}, |
|---|
| 860 | n/a | {0} |
|---|
| 861 | n/a | }; |
|---|
| 862 | n/a | |
|---|
| 863 | n/a | static PyObject * |
|---|
| 864 | n/a | type_repr(PyTypeObject *type) |
|---|
| 865 | n/a | { |
|---|
| 866 | n/a | PyObject *mod, *name, *rtn; |
|---|
| 867 | n/a | |
|---|
| 868 | n/a | mod = type_module(type, NULL); |
|---|
| 869 | n/a | if (mod == NULL) |
|---|
| 870 | n/a | PyErr_Clear(); |
|---|
| 871 | n/a | else if (!PyUnicode_Check(mod)) { |
|---|
| 872 | n/a | Py_DECREF(mod); |
|---|
| 873 | n/a | mod = NULL; |
|---|
| 874 | n/a | } |
|---|
| 875 | n/a | name = type_qualname(type, NULL); |
|---|
| 876 | n/a | if (name == NULL) { |
|---|
| 877 | n/a | Py_XDECREF(mod); |
|---|
| 878 | n/a | return NULL; |
|---|
| 879 | n/a | } |
|---|
| 880 | n/a | |
|---|
| 881 | n/a | if (mod != NULL && !_PyUnicode_EqualToASCIIId(mod, &PyId_builtins)) |
|---|
| 882 | n/a | rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name); |
|---|
| 883 | n/a | else |
|---|
| 884 | n/a | rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name); |
|---|
| 885 | n/a | |
|---|
| 886 | n/a | Py_XDECREF(mod); |
|---|
| 887 | n/a | Py_DECREF(name); |
|---|
| 888 | n/a | return rtn; |
|---|
| 889 | n/a | } |
|---|
| 890 | n/a | |
|---|
| 891 | n/a | static PyObject * |
|---|
| 892 | n/a | type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 893 | n/a | { |
|---|
| 894 | n/a | PyObject *obj; |
|---|
| 895 | n/a | |
|---|
| 896 | n/a | if (type->tp_new == NULL) { |
|---|
| 897 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 898 | n/a | "cannot create '%.100s' instances", |
|---|
| 899 | n/a | type->tp_name); |
|---|
| 900 | n/a | return NULL; |
|---|
| 901 | n/a | } |
|---|
| 902 | n/a | |
|---|
| 903 | n/a | #ifdef Py_DEBUG |
|---|
| 904 | n/a | /* type_call() must not be called with an exception set, |
|---|
| 905 | n/a | because it can clear it (directly or indirectly) and so the |
|---|
| 906 | n/a | caller loses its exception */ |
|---|
| 907 | n/a | assert(!PyErr_Occurred()); |
|---|
| 908 | n/a | #endif |
|---|
| 909 | n/a | |
|---|
| 910 | n/a | obj = type->tp_new(type, args, kwds); |
|---|
| 911 | n/a | obj = _Py_CheckFunctionResult((PyObject*)type, obj, NULL); |
|---|
| 912 | n/a | if (obj == NULL) |
|---|
| 913 | n/a | return NULL; |
|---|
| 914 | n/a | |
|---|
| 915 | n/a | /* Ugly exception: when the call was type(something), |
|---|
| 916 | n/a | don't call tp_init on the result. */ |
|---|
| 917 | n/a | if (type == &PyType_Type && |
|---|
| 918 | n/a | PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && |
|---|
| 919 | n/a | (kwds == NULL || |
|---|
| 920 | n/a | (PyDict_Check(kwds) && PyDict_GET_SIZE(kwds) == 0))) |
|---|
| 921 | n/a | return obj; |
|---|
| 922 | n/a | |
|---|
| 923 | n/a | /* If the returned object is not an instance of type, |
|---|
| 924 | n/a | it won't be initialized. */ |
|---|
| 925 | n/a | if (!PyType_IsSubtype(Py_TYPE(obj), type)) |
|---|
| 926 | n/a | return obj; |
|---|
| 927 | n/a | |
|---|
| 928 | n/a | type = Py_TYPE(obj); |
|---|
| 929 | n/a | if (type->tp_init != NULL) { |
|---|
| 930 | n/a | int res = type->tp_init(obj, args, kwds); |
|---|
| 931 | n/a | if (res < 0) { |
|---|
| 932 | n/a | assert(PyErr_Occurred()); |
|---|
| 933 | n/a | Py_DECREF(obj); |
|---|
| 934 | n/a | obj = NULL; |
|---|
| 935 | n/a | } |
|---|
| 936 | n/a | else { |
|---|
| 937 | n/a | assert(!PyErr_Occurred()); |
|---|
| 938 | n/a | } |
|---|
| 939 | n/a | } |
|---|
| 940 | n/a | return obj; |
|---|
| 941 | n/a | } |
|---|
| 942 | n/a | |
|---|
| 943 | n/a | PyObject * |
|---|
| 944 | n/a | PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
|---|
| 945 | n/a | { |
|---|
| 946 | n/a | PyObject *obj; |
|---|
| 947 | n/a | const size_t size = _PyObject_VAR_SIZE(type, nitems+1); |
|---|
| 948 | n/a | /* note that we need to add one, for the sentinel */ |
|---|
| 949 | n/a | |
|---|
| 950 | n/a | if (PyType_IS_GC(type)) |
|---|
| 951 | n/a | obj = _PyObject_GC_Malloc(size); |
|---|
| 952 | n/a | else |
|---|
| 953 | n/a | obj = (PyObject *)PyObject_MALLOC(size); |
|---|
| 954 | n/a | |
|---|
| 955 | n/a | if (obj == NULL) |
|---|
| 956 | n/a | return PyErr_NoMemory(); |
|---|
| 957 | n/a | |
|---|
| 958 | n/a | memset(obj, '\0', size); |
|---|
| 959 | n/a | |
|---|
| 960 | n/a | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
|---|
| 961 | n/a | Py_INCREF(type); |
|---|
| 962 | n/a | |
|---|
| 963 | n/a | if (type->tp_itemsize == 0) |
|---|
| 964 | n/a | (void)PyObject_INIT(obj, type); |
|---|
| 965 | n/a | else |
|---|
| 966 | n/a | (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems); |
|---|
| 967 | n/a | |
|---|
| 968 | n/a | if (PyType_IS_GC(type)) |
|---|
| 969 | n/a | _PyObject_GC_TRACK(obj); |
|---|
| 970 | n/a | return obj; |
|---|
| 971 | n/a | } |
|---|
| 972 | n/a | |
|---|
| 973 | n/a | PyObject * |
|---|
| 974 | n/a | PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 975 | n/a | { |
|---|
| 976 | n/a | return type->tp_alloc(type, 0); |
|---|
| 977 | n/a | } |
|---|
| 978 | n/a | |
|---|
| 979 | n/a | /* Helpers for subtyping */ |
|---|
| 980 | n/a | |
|---|
| 981 | n/a | static int |
|---|
| 982 | n/a | traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg) |
|---|
| 983 | n/a | { |
|---|
| 984 | n/a | Py_ssize_t i, n; |
|---|
| 985 | n/a | PyMemberDef *mp; |
|---|
| 986 | n/a | |
|---|
| 987 | n/a | n = Py_SIZE(type); |
|---|
| 988 | n/a | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
|---|
| 989 | n/a | for (i = 0; i < n; i++, mp++) { |
|---|
| 990 | n/a | if (mp->type == T_OBJECT_EX) { |
|---|
| 991 | n/a | char *addr = (char *)self + mp->offset; |
|---|
| 992 | n/a | PyObject *obj = *(PyObject **)addr; |
|---|
| 993 | n/a | if (obj != NULL) { |
|---|
| 994 | n/a | int err = visit(obj, arg); |
|---|
| 995 | n/a | if (err) |
|---|
| 996 | n/a | return err; |
|---|
| 997 | n/a | } |
|---|
| 998 | n/a | } |
|---|
| 999 | n/a | } |
|---|
| 1000 | n/a | return 0; |
|---|
| 1001 | n/a | } |
|---|
| 1002 | n/a | |
|---|
| 1003 | n/a | static int |
|---|
| 1004 | n/a | subtype_traverse(PyObject *self, visitproc visit, void *arg) |
|---|
| 1005 | n/a | { |
|---|
| 1006 | n/a | PyTypeObject *type, *base; |
|---|
| 1007 | n/a | traverseproc basetraverse; |
|---|
| 1008 | n/a | |
|---|
| 1009 | n/a | /* Find the nearest base with a different tp_traverse, |
|---|
| 1010 | n/a | and traverse slots while we're at it */ |
|---|
| 1011 | n/a | type = Py_TYPE(self); |
|---|
| 1012 | n/a | base = type; |
|---|
| 1013 | n/a | while ((basetraverse = base->tp_traverse) == subtype_traverse) { |
|---|
| 1014 | n/a | if (Py_SIZE(base)) { |
|---|
| 1015 | n/a | int err = traverse_slots(base, self, visit, arg); |
|---|
| 1016 | n/a | if (err) |
|---|
| 1017 | n/a | return err; |
|---|
| 1018 | n/a | } |
|---|
| 1019 | n/a | base = base->tp_base; |
|---|
| 1020 | n/a | assert(base); |
|---|
| 1021 | n/a | } |
|---|
| 1022 | n/a | |
|---|
| 1023 | n/a | if (type->tp_dictoffset != base->tp_dictoffset) { |
|---|
| 1024 | n/a | PyObject **dictptr = _PyObject_GetDictPtr(self); |
|---|
| 1025 | n/a | if (dictptr && *dictptr) |
|---|
| 1026 | n/a | Py_VISIT(*dictptr); |
|---|
| 1027 | n/a | } |
|---|
| 1028 | n/a | |
|---|
| 1029 | n/a | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
|---|
| 1030 | n/a | /* For a heaptype, the instances count as references |
|---|
| 1031 | n/a | to the type. Traverse the type so the collector |
|---|
| 1032 | n/a | can find cycles involving this link. */ |
|---|
| 1033 | n/a | Py_VISIT(type); |
|---|
| 1034 | n/a | |
|---|
| 1035 | n/a | if (basetraverse) |
|---|
| 1036 | n/a | return basetraverse(self, visit, arg); |
|---|
| 1037 | n/a | return 0; |
|---|
| 1038 | n/a | } |
|---|
| 1039 | n/a | |
|---|
| 1040 | n/a | static void |
|---|
| 1041 | n/a | clear_slots(PyTypeObject *type, PyObject *self) |
|---|
| 1042 | n/a | { |
|---|
| 1043 | n/a | Py_ssize_t i, n; |
|---|
| 1044 | n/a | PyMemberDef *mp; |
|---|
| 1045 | n/a | |
|---|
| 1046 | n/a | n = Py_SIZE(type); |
|---|
| 1047 | n/a | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
|---|
| 1048 | n/a | for (i = 0; i < n; i++, mp++) { |
|---|
| 1049 | n/a | if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) { |
|---|
| 1050 | n/a | char *addr = (char *)self + mp->offset; |
|---|
| 1051 | n/a | PyObject *obj = *(PyObject **)addr; |
|---|
| 1052 | n/a | if (obj != NULL) { |
|---|
| 1053 | n/a | *(PyObject **)addr = NULL; |
|---|
| 1054 | n/a | Py_DECREF(obj); |
|---|
| 1055 | n/a | } |
|---|
| 1056 | n/a | } |
|---|
| 1057 | n/a | } |
|---|
| 1058 | n/a | } |
|---|
| 1059 | n/a | |
|---|
| 1060 | n/a | static int |
|---|
| 1061 | n/a | subtype_clear(PyObject *self) |
|---|
| 1062 | n/a | { |
|---|
| 1063 | n/a | PyTypeObject *type, *base; |
|---|
| 1064 | n/a | inquiry baseclear; |
|---|
| 1065 | n/a | |
|---|
| 1066 | n/a | /* Find the nearest base with a different tp_clear |
|---|
| 1067 | n/a | and clear slots while we're at it */ |
|---|
| 1068 | n/a | type = Py_TYPE(self); |
|---|
| 1069 | n/a | base = type; |
|---|
| 1070 | n/a | while ((baseclear = base->tp_clear) == subtype_clear) { |
|---|
| 1071 | n/a | if (Py_SIZE(base)) |
|---|
| 1072 | n/a | clear_slots(base, self); |
|---|
| 1073 | n/a | base = base->tp_base; |
|---|
| 1074 | n/a | assert(base); |
|---|
| 1075 | n/a | } |
|---|
| 1076 | n/a | |
|---|
| 1077 | n/a | /* Clear the instance dict (if any), to break cycles involving only |
|---|
| 1078 | n/a | __dict__ slots (as in the case 'self.__dict__ is self'). */ |
|---|
| 1079 | n/a | if (type->tp_dictoffset != base->tp_dictoffset) { |
|---|
| 1080 | n/a | PyObject **dictptr = _PyObject_GetDictPtr(self); |
|---|
| 1081 | n/a | if (dictptr && *dictptr) |
|---|
| 1082 | n/a | Py_CLEAR(*dictptr); |
|---|
| 1083 | n/a | } |
|---|
| 1084 | n/a | |
|---|
| 1085 | n/a | if (baseclear) |
|---|
| 1086 | n/a | return baseclear(self); |
|---|
| 1087 | n/a | return 0; |
|---|
| 1088 | n/a | } |
|---|
| 1089 | n/a | |
|---|
| 1090 | n/a | static void |
|---|
| 1091 | n/a | subtype_dealloc(PyObject *self) |
|---|
| 1092 | n/a | { |
|---|
| 1093 | n/a | PyTypeObject *type, *base; |
|---|
| 1094 | n/a | destructor basedealloc; |
|---|
| 1095 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
|---|
| 1096 | n/a | int has_finalizer; |
|---|
| 1097 | n/a | |
|---|
| 1098 | n/a | /* Extract the type; we expect it to be a heap type */ |
|---|
| 1099 | n/a | type = Py_TYPE(self); |
|---|
| 1100 | n/a | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
|---|
| 1101 | n/a | |
|---|
| 1102 | n/a | /* Test whether the type has GC exactly once */ |
|---|
| 1103 | n/a | |
|---|
| 1104 | n/a | if (!PyType_IS_GC(type)) { |
|---|
| 1105 | n/a | /* It's really rare to find a dynamic type that doesn't have |
|---|
| 1106 | n/a | GC; it can only happen when deriving from 'object' and not |
|---|
| 1107 | n/a | adding any slots or instance variables. This allows |
|---|
| 1108 | n/a | certain simplifications: there's no need to call |
|---|
| 1109 | n/a | clear_slots(), or DECREF the dict, or clear weakrefs. */ |
|---|
| 1110 | n/a | |
|---|
| 1111 | n/a | /* Maybe call finalizer; exit early if resurrected */ |
|---|
| 1112 | n/a | if (type->tp_finalize) { |
|---|
| 1113 | n/a | if (PyObject_CallFinalizerFromDealloc(self) < 0) |
|---|
| 1114 | n/a | return; |
|---|
| 1115 | n/a | } |
|---|
| 1116 | n/a | if (type->tp_del) { |
|---|
| 1117 | n/a | type->tp_del(self); |
|---|
| 1118 | n/a | if (self->ob_refcnt > 0) |
|---|
| 1119 | n/a | return; |
|---|
| 1120 | n/a | } |
|---|
| 1121 | n/a | |
|---|
| 1122 | n/a | /* Find the nearest base with a different tp_dealloc */ |
|---|
| 1123 | n/a | base = type; |
|---|
| 1124 | n/a | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
|---|
| 1125 | n/a | assert(Py_SIZE(base) == 0); |
|---|
| 1126 | n/a | base = base->tp_base; |
|---|
| 1127 | n/a | assert(base); |
|---|
| 1128 | n/a | } |
|---|
| 1129 | n/a | |
|---|
| 1130 | n/a | /* Extract the type again; tp_del may have changed it */ |
|---|
| 1131 | n/a | type = Py_TYPE(self); |
|---|
| 1132 | n/a | |
|---|
| 1133 | n/a | /* Call the base tp_dealloc() */ |
|---|
| 1134 | n/a | assert(basedealloc); |
|---|
| 1135 | n/a | basedealloc(self); |
|---|
| 1136 | n/a | |
|---|
| 1137 | n/a | /* Can't reference self beyond this point */ |
|---|
| 1138 | n/a | Py_DECREF(type); |
|---|
| 1139 | n/a | |
|---|
| 1140 | n/a | /* Done */ |
|---|
| 1141 | n/a | return; |
|---|
| 1142 | n/a | } |
|---|
| 1143 | n/a | |
|---|
| 1144 | n/a | /* We get here only if the type has GC */ |
|---|
| 1145 | n/a | |
|---|
| 1146 | n/a | /* UnTrack and re-Track around the trashcan macro, alas */ |
|---|
| 1147 | n/a | /* See explanation at end of function for full disclosure */ |
|---|
| 1148 | n/a | PyObject_GC_UnTrack(self); |
|---|
| 1149 | n/a | ++_PyTrash_delete_nesting; |
|---|
| 1150 | n/a | ++ tstate->trash_delete_nesting; |
|---|
| 1151 | n/a | Py_TRASHCAN_SAFE_BEGIN(self); |
|---|
| 1152 | n/a | --_PyTrash_delete_nesting; |
|---|
| 1153 | n/a | -- tstate->trash_delete_nesting; |
|---|
| 1154 | n/a | |
|---|
| 1155 | n/a | /* Find the nearest base with a different tp_dealloc */ |
|---|
| 1156 | n/a | base = type; |
|---|
| 1157 | n/a | while ((/*basedealloc =*/ base->tp_dealloc) == subtype_dealloc) { |
|---|
| 1158 | n/a | base = base->tp_base; |
|---|
| 1159 | n/a | assert(base); |
|---|
| 1160 | n/a | } |
|---|
| 1161 | n/a | |
|---|
| 1162 | n/a | has_finalizer = type->tp_finalize || type->tp_del; |
|---|
| 1163 | n/a | |
|---|
| 1164 | n/a | if (type->tp_finalize) { |
|---|
| 1165 | n/a | _PyObject_GC_TRACK(self); |
|---|
| 1166 | n/a | if (PyObject_CallFinalizerFromDealloc(self) < 0) { |
|---|
| 1167 | n/a | /* Resurrected */ |
|---|
| 1168 | n/a | goto endlabel; |
|---|
| 1169 | n/a | } |
|---|
| 1170 | n/a | _PyObject_GC_UNTRACK(self); |
|---|
| 1171 | n/a | } |
|---|
| 1172 | n/a | /* |
|---|
| 1173 | n/a | If we added a weaklist, we clear it. Do this *before* calling tp_del, |
|---|
| 1174 | n/a | clearing slots, or clearing the instance dict. |
|---|
| 1175 | n/a | |
|---|
| 1176 | n/a | GC tracking must be off at this point. weakref callbacks (if any, and |
|---|
| 1177 | n/a | whether directly here or indirectly in something we call) may trigger GC, |
|---|
| 1178 | n/a | and if self is tracked at that point, it will look like trash to GC and GC |
|---|
| 1179 | n/a | will try to delete self again. |
|---|
| 1180 | n/a | */ |
|---|
| 1181 | n/a | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) |
|---|
| 1182 | n/a | PyObject_ClearWeakRefs(self); |
|---|
| 1183 | n/a | |
|---|
| 1184 | n/a | if (type->tp_del) { |
|---|
| 1185 | n/a | _PyObject_GC_TRACK(self); |
|---|
| 1186 | n/a | type->tp_del(self); |
|---|
| 1187 | n/a | if (self->ob_refcnt > 0) { |
|---|
| 1188 | n/a | /* Resurrected */ |
|---|
| 1189 | n/a | goto endlabel; |
|---|
| 1190 | n/a | } |
|---|
| 1191 | n/a | _PyObject_GC_UNTRACK(self); |
|---|
| 1192 | n/a | } |
|---|
| 1193 | n/a | if (has_finalizer) { |
|---|
| 1194 | n/a | /* New weakrefs could be created during the finalizer call. |
|---|
| 1195 | n/a | If this occurs, clear them out without calling their |
|---|
| 1196 | n/a | finalizers since they might rely on part of the object |
|---|
| 1197 | n/a | being finalized that has already been destroyed. */ |
|---|
| 1198 | n/a | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) { |
|---|
| 1199 | n/a | /* Modeled after GET_WEAKREFS_LISTPTR() */ |
|---|
| 1200 | n/a | PyWeakReference **list = (PyWeakReference **) \ |
|---|
| 1201 | n/a | PyObject_GET_WEAKREFS_LISTPTR(self); |
|---|
| 1202 | n/a | while (*list) |
|---|
| 1203 | n/a | _PyWeakref_ClearRef(*list); |
|---|
| 1204 | n/a | } |
|---|
| 1205 | n/a | } |
|---|
| 1206 | n/a | |
|---|
| 1207 | n/a | /* Clear slots up to the nearest base with a different tp_dealloc */ |
|---|
| 1208 | n/a | base = type; |
|---|
| 1209 | n/a | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { |
|---|
| 1210 | n/a | if (Py_SIZE(base)) |
|---|
| 1211 | n/a | clear_slots(base, self); |
|---|
| 1212 | n/a | base = base->tp_base; |
|---|
| 1213 | n/a | assert(base); |
|---|
| 1214 | n/a | } |
|---|
| 1215 | n/a | |
|---|
| 1216 | n/a | /* If we added a dict, DECREF it */ |
|---|
| 1217 | n/a | if (type->tp_dictoffset && !base->tp_dictoffset) { |
|---|
| 1218 | n/a | PyObject **dictptr = _PyObject_GetDictPtr(self); |
|---|
| 1219 | n/a | if (dictptr != NULL) { |
|---|
| 1220 | n/a | PyObject *dict = *dictptr; |
|---|
| 1221 | n/a | if (dict != NULL) { |
|---|
| 1222 | n/a | Py_DECREF(dict); |
|---|
| 1223 | n/a | *dictptr = NULL; |
|---|
| 1224 | n/a | } |
|---|
| 1225 | n/a | } |
|---|
| 1226 | n/a | } |
|---|
| 1227 | n/a | |
|---|
| 1228 | n/a | /* Extract the type again; tp_del may have changed it */ |
|---|
| 1229 | n/a | type = Py_TYPE(self); |
|---|
| 1230 | n/a | |
|---|
| 1231 | n/a | /* Call the base tp_dealloc(); first retrack self if |
|---|
| 1232 | n/a | * basedealloc knows about gc. |
|---|
| 1233 | n/a | */ |
|---|
| 1234 | n/a | if (PyType_IS_GC(base)) |
|---|
| 1235 | n/a | _PyObject_GC_TRACK(self); |
|---|
| 1236 | n/a | assert(basedealloc); |
|---|
| 1237 | n/a | basedealloc(self); |
|---|
| 1238 | n/a | |
|---|
| 1239 | n/a | /* Can't reference self beyond this point. It's possible tp_del switched |
|---|
| 1240 | n/a | our type from a HEAPTYPE to a non-HEAPTYPE, so be careful about |
|---|
| 1241 | n/a | reference counting. */ |
|---|
| 1242 | n/a | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
|---|
| 1243 | n/a | Py_DECREF(type); |
|---|
| 1244 | n/a | |
|---|
| 1245 | n/a | endlabel: |
|---|
| 1246 | n/a | ++_PyTrash_delete_nesting; |
|---|
| 1247 | n/a | ++ tstate->trash_delete_nesting; |
|---|
| 1248 | n/a | Py_TRASHCAN_SAFE_END(self); |
|---|
| 1249 | n/a | --_PyTrash_delete_nesting; |
|---|
| 1250 | n/a | -- tstate->trash_delete_nesting; |
|---|
| 1251 | n/a | |
|---|
| 1252 | n/a | /* Explanation of the weirdness around the trashcan macros: |
|---|
| 1253 | n/a | |
|---|
| 1254 | n/a | Q. What do the trashcan macros do? |
|---|
| 1255 | n/a | |
|---|
| 1256 | n/a | A. Read the comment titled "Trashcan mechanism" in object.h. |
|---|
| 1257 | n/a | For one, this explains why there must be a call to GC-untrack |
|---|
| 1258 | n/a | before the trashcan begin macro. Without understanding the |
|---|
| 1259 | n/a | trashcan code, the answers to the following questions don't make |
|---|
| 1260 | n/a | sense. |
|---|
| 1261 | n/a | |
|---|
| 1262 | n/a | Q. Why do we GC-untrack before the trashcan and then immediately |
|---|
| 1263 | n/a | GC-track again afterward? |
|---|
| 1264 | n/a | |
|---|
| 1265 | n/a | A. In the case that the base class is GC-aware, the base class |
|---|
| 1266 | n/a | probably GC-untracks the object. If it does that using the |
|---|
| 1267 | n/a | UNTRACK macro, this will crash when the object is already |
|---|
| 1268 | n/a | untracked. Because we don't know what the base class does, the |
|---|
| 1269 | n/a | only safe thing is to make sure the object is tracked when we |
|---|
| 1270 | n/a | call the base class dealloc. But... The trashcan begin macro |
|---|
| 1271 | n/a | requires that the object is *untracked* before it is called. So |
|---|
| 1272 | n/a | the dance becomes: |
|---|
| 1273 | n/a | |
|---|
| 1274 | n/a | GC untrack |
|---|
| 1275 | n/a | trashcan begin |
|---|
| 1276 | n/a | GC track |
|---|
| 1277 | n/a | |
|---|
| 1278 | n/a | Q. Why did the last question say "immediately GC-track again"? |
|---|
| 1279 | n/a | It's nowhere near immediately. |
|---|
| 1280 | n/a | |
|---|
| 1281 | n/a | A. Because the code *used* to re-track immediately. Bad Idea. |
|---|
| 1282 | n/a | self has a refcount of 0, and if gc ever gets its hands on it |
|---|
| 1283 | n/a | (which can happen if any weakref callback gets invoked), it |
|---|
| 1284 | n/a | looks like trash to gc too, and gc also tries to delete self |
|---|
| 1285 | n/a | then. But we're already deleting self. Double deallocation is |
|---|
| 1286 | n/a | a subtle disaster. |
|---|
| 1287 | n/a | |
|---|
| 1288 | n/a | Q. Why the bizarre (net-zero) manipulation of |
|---|
| 1289 | n/a | _PyTrash_delete_nesting around the trashcan macros? |
|---|
| 1290 | n/a | |
|---|
| 1291 | n/a | A. Some base classes (e.g. list) also use the trashcan mechanism. |
|---|
| 1292 | n/a | The following scenario used to be possible: |
|---|
| 1293 | n/a | |
|---|
| 1294 | n/a | - suppose the trashcan level is one below the trashcan limit |
|---|
| 1295 | n/a | |
|---|
| 1296 | n/a | - subtype_dealloc() is called |
|---|
| 1297 | n/a | |
|---|
| 1298 | n/a | - the trashcan limit is not yet reached, so the trashcan level |
|---|
| 1299 | n/a | is incremented and the code between trashcan begin and end is |
|---|
| 1300 | n/a | executed |
|---|
| 1301 | n/a | |
|---|
| 1302 | n/a | - this destroys much of the object's contents, including its |
|---|
| 1303 | n/a | slots and __dict__ |
|---|
| 1304 | n/a | |
|---|
| 1305 | n/a | - basedealloc() is called; this is really list_dealloc(), or |
|---|
| 1306 | n/a | some other type which also uses the trashcan macros |
|---|
| 1307 | n/a | |
|---|
| 1308 | n/a | - the trashcan limit is now reached, so the object is put on the |
|---|
| 1309 | n/a | trashcan's to-be-deleted-later list |
|---|
| 1310 | n/a | |
|---|
| 1311 | n/a | - basedealloc() returns |
|---|
| 1312 | n/a | |
|---|
| 1313 | n/a | - subtype_dealloc() decrefs the object's type |
|---|
| 1314 | n/a | |
|---|
| 1315 | n/a | - subtype_dealloc() returns |
|---|
| 1316 | n/a | |
|---|
| 1317 | n/a | - later, the trashcan code starts deleting the objects from its |
|---|
| 1318 | n/a | to-be-deleted-later list |
|---|
| 1319 | n/a | |
|---|
| 1320 | n/a | - subtype_dealloc() is called *AGAIN* for the same object |
|---|
| 1321 | n/a | |
|---|
| 1322 | n/a | - at the very least (if the destroyed slots and __dict__ don't |
|---|
| 1323 | n/a | cause problems) the object's type gets decref'ed a second |
|---|
| 1324 | n/a | time, which is *BAD*!!! |
|---|
| 1325 | n/a | |
|---|
| 1326 | n/a | The remedy is to make sure that if the code between trashcan |
|---|
| 1327 | n/a | begin and end in subtype_dealloc() is called, the code between |
|---|
| 1328 | n/a | trashcan begin and end in basedealloc() will also be called. |
|---|
| 1329 | n/a | This is done by decrementing the level after passing into the |
|---|
| 1330 | n/a | trashcan block, and incrementing it just before leaving the |
|---|
| 1331 | n/a | block. |
|---|
| 1332 | n/a | |
|---|
| 1333 | n/a | But now it's possible that a chain of objects consisting solely |
|---|
| 1334 | n/a | of objects whose deallocator is subtype_dealloc() will defeat |
|---|
| 1335 | n/a | the trashcan mechanism completely: the decremented level means |
|---|
| 1336 | n/a | that the effective level never reaches the limit. Therefore, we |
|---|
| 1337 | n/a | *increment* the level *before* entering the trashcan block, and |
|---|
| 1338 | n/a | matchingly decrement it after leaving. This means the trashcan |
|---|
| 1339 | n/a | code will trigger a little early, but that's no big deal. |
|---|
| 1340 | n/a | |
|---|
| 1341 | n/a | Q. Are there any live examples of code in need of all this |
|---|
| 1342 | n/a | complexity? |
|---|
| 1343 | n/a | |
|---|
| 1344 | n/a | A. Yes. See SF bug 668433 for code that crashed (when Python was |
|---|
| 1345 | n/a | compiled in debug mode) before the trashcan level manipulations |
|---|
| 1346 | n/a | were added. For more discussion, see SF patches 581742, 575073 |
|---|
| 1347 | n/a | and bug 574207. |
|---|
| 1348 | n/a | */ |
|---|
| 1349 | n/a | } |
|---|
| 1350 | n/a | |
|---|
| 1351 | n/a | static PyTypeObject *solid_base(PyTypeObject *type); |
|---|
| 1352 | n/a | |
|---|
| 1353 | n/a | /* type test with subclassing support */ |
|---|
| 1354 | n/a | |
|---|
| 1355 | n/a | static int |
|---|
| 1356 | n/a | type_is_subtype_base_chain(PyTypeObject *a, PyTypeObject *b) |
|---|
| 1357 | n/a | { |
|---|
| 1358 | n/a | do { |
|---|
| 1359 | n/a | if (a == b) |
|---|
| 1360 | n/a | return 1; |
|---|
| 1361 | n/a | a = a->tp_base; |
|---|
| 1362 | n/a | } while (a != NULL); |
|---|
| 1363 | n/a | |
|---|
| 1364 | n/a | return (b == &PyBaseObject_Type); |
|---|
| 1365 | n/a | } |
|---|
| 1366 | n/a | |
|---|
| 1367 | n/a | int |
|---|
| 1368 | n/a | PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
|---|
| 1369 | n/a | { |
|---|
| 1370 | n/a | PyObject *mro; |
|---|
| 1371 | n/a | |
|---|
| 1372 | n/a | mro = a->tp_mro; |
|---|
| 1373 | n/a | if (mro != NULL) { |
|---|
| 1374 | n/a | /* Deal with multiple inheritance without recursion |
|---|
| 1375 | n/a | by walking the MRO tuple */ |
|---|
| 1376 | n/a | Py_ssize_t i, n; |
|---|
| 1377 | n/a | assert(PyTuple_Check(mro)); |
|---|
| 1378 | n/a | n = PyTuple_GET_SIZE(mro); |
|---|
| 1379 | n/a | for (i = 0; i < n; i++) { |
|---|
| 1380 | n/a | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) |
|---|
| 1381 | n/a | return 1; |
|---|
| 1382 | n/a | } |
|---|
| 1383 | n/a | return 0; |
|---|
| 1384 | n/a | } |
|---|
| 1385 | n/a | else |
|---|
| 1386 | n/a | /* a is not completely initilized yet; follow tp_base */ |
|---|
| 1387 | n/a | return type_is_subtype_base_chain(a, b); |
|---|
| 1388 | n/a | } |
|---|
| 1389 | n/a | |
|---|
| 1390 | n/a | /* Internal routines to do a method lookup in the type |
|---|
| 1391 | n/a | without looking in the instance dictionary |
|---|
| 1392 | n/a | (so we can't use PyObject_GetAttr) but still binding |
|---|
| 1393 | n/a | it to the instance. The arguments are the object, |
|---|
| 1394 | n/a | the method name as a C string, and the address of a |
|---|
| 1395 | n/a | static variable used to cache the interned Python string. |
|---|
| 1396 | n/a | |
|---|
| 1397 | n/a | Variants: |
|---|
| 1398 | n/a | |
|---|
| 1399 | n/a | - lookup_maybe() returns NULL without raising an exception |
|---|
| 1400 | n/a | when the _PyType_Lookup() call fails; |
|---|
| 1401 | n/a | |
|---|
| 1402 | n/a | - lookup_maybe_method() and lookup_method() are similar to |
|---|
| 1403 | n/a | lookup_maybe(), but can return unbound PyFunction |
|---|
| 1404 | n/a | to avoid temporary method object. Pass self as first argument when |
|---|
| 1405 | n/a | unbound == 1. |
|---|
| 1406 | n/a | |
|---|
| 1407 | n/a | - _PyObject_LookupSpecial() expose lookup_maybe for the benefit of |
|---|
| 1408 | n/a | other places. |
|---|
| 1409 | n/a | */ |
|---|
| 1410 | n/a | |
|---|
| 1411 | n/a | static PyObject * |
|---|
| 1412 | n/a | lookup_maybe(PyObject *self, _Py_Identifier *attrid) |
|---|
| 1413 | n/a | { |
|---|
| 1414 | n/a | PyObject *res; |
|---|
| 1415 | n/a | |
|---|
| 1416 | n/a | res = _PyType_LookupId(Py_TYPE(self), attrid); |
|---|
| 1417 | n/a | if (res != NULL) { |
|---|
| 1418 | n/a | descrgetfunc f; |
|---|
| 1419 | n/a | if ((f = Py_TYPE(res)->tp_descr_get) == NULL) |
|---|
| 1420 | n/a | Py_INCREF(res); |
|---|
| 1421 | n/a | else |
|---|
| 1422 | n/a | res = f(res, self, (PyObject *)(Py_TYPE(self))); |
|---|
| 1423 | n/a | } |
|---|
| 1424 | n/a | return res; |
|---|
| 1425 | n/a | } |
|---|
| 1426 | n/a | |
|---|
| 1427 | n/a | static PyObject * |
|---|
| 1428 | n/a | lookup_maybe_method(PyObject *self, _Py_Identifier *attrid, int *unbound) |
|---|
| 1429 | n/a | { |
|---|
| 1430 | n/a | PyObject *res = _PyType_LookupId(Py_TYPE(self), attrid); |
|---|
| 1431 | n/a | if (res == NULL) { |
|---|
| 1432 | n/a | return NULL; |
|---|
| 1433 | n/a | } |
|---|
| 1434 | n/a | |
|---|
| 1435 | n/a | if (PyFunction_Check(res)) { |
|---|
| 1436 | n/a | /* Avoid temporary PyMethodObject */ |
|---|
| 1437 | n/a | *unbound = 1; |
|---|
| 1438 | n/a | Py_INCREF(res); |
|---|
| 1439 | n/a | } |
|---|
| 1440 | n/a | else { |
|---|
| 1441 | n/a | *unbound = 0; |
|---|
| 1442 | n/a | descrgetfunc f = Py_TYPE(res)->tp_descr_get; |
|---|
| 1443 | n/a | if (f == NULL) { |
|---|
| 1444 | n/a | Py_INCREF(res); |
|---|
| 1445 | n/a | } |
|---|
| 1446 | n/a | else { |
|---|
| 1447 | n/a | res = f(res, self, (PyObject *)(Py_TYPE(self))); |
|---|
| 1448 | n/a | } |
|---|
| 1449 | n/a | } |
|---|
| 1450 | n/a | return res; |
|---|
| 1451 | n/a | } |
|---|
| 1452 | n/a | |
|---|
| 1453 | n/a | static PyObject * |
|---|
| 1454 | n/a | lookup_method(PyObject *self, _Py_Identifier *attrid, int *unbound) |
|---|
| 1455 | n/a | { |
|---|
| 1456 | n/a | PyObject *res = lookup_maybe_method(self, attrid, unbound); |
|---|
| 1457 | n/a | if (res == NULL && !PyErr_Occurred()) { |
|---|
| 1458 | n/a | PyErr_SetObject(PyExc_AttributeError, attrid->object); |
|---|
| 1459 | n/a | } |
|---|
| 1460 | n/a | return res; |
|---|
| 1461 | n/a | } |
|---|
| 1462 | n/a | |
|---|
| 1463 | n/a | PyObject * |
|---|
| 1464 | n/a | _PyObject_LookupSpecial(PyObject *self, _Py_Identifier *attrid) |
|---|
| 1465 | n/a | { |
|---|
| 1466 | n/a | return lookup_maybe(self, attrid); |
|---|
| 1467 | n/a | } |
|---|
| 1468 | n/a | |
|---|
| 1469 | n/a | static PyObject* |
|---|
| 1470 | n/a | call_unbound(int unbound, PyObject *func, PyObject *self, |
|---|
| 1471 | n/a | PyObject **args, Py_ssize_t nargs) |
|---|
| 1472 | n/a | { |
|---|
| 1473 | n/a | if (unbound) { |
|---|
| 1474 | n/a | return _PyObject_FastCall_Prepend(func, self, args, nargs); |
|---|
| 1475 | n/a | } |
|---|
| 1476 | n/a | else { |
|---|
| 1477 | n/a | return _PyObject_FastCall(func, args, nargs); |
|---|
| 1478 | n/a | } |
|---|
| 1479 | n/a | } |
|---|
| 1480 | n/a | |
|---|
| 1481 | n/a | static PyObject* |
|---|
| 1482 | n/a | call_unbound_noarg(int unbound, PyObject *func, PyObject *self) |
|---|
| 1483 | n/a | { |
|---|
| 1484 | n/a | if (unbound) { |
|---|
| 1485 | n/a | PyObject *args[1] = {self}; |
|---|
| 1486 | n/a | return _PyObject_FastCall(func, args, 1); |
|---|
| 1487 | n/a | } |
|---|
| 1488 | n/a | else { |
|---|
| 1489 | n/a | return _PyObject_CallNoArg(func); |
|---|
| 1490 | n/a | } |
|---|
| 1491 | n/a | } |
|---|
| 1492 | n/a | |
|---|
| 1493 | n/a | /* A variation of PyObject_CallMethodObjArgs that uses lookup_maybe_method() |
|---|
| 1494 | n/a | instead of PyObject_GetAttrString(). This uses the same convention |
|---|
| 1495 | n/a | as lookup_maybe_method to cache the interned name string object. */ |
|---|
| 1496 | n/a | static PyObject * |
|---|
| 1497 | n/a | call_method(PyObject *obj, _Py_Identifier *name, |
|---|
| 1498 | n/a | PyObject **args, Py_ssize_t nargs) |
|---|
| 1499 | n/a | { |
|---|
| 1500 | n/a | int unbound; |
|---|
| 1501 | n/a | PyObject *func, *retval; |
|---|
| 1502 | n/a | |
|---|
| 1503 | n/a | func = lookup_maybe_method(obj, name, &unbound); |
|---|
| 1504 | n/a | if (func == NULL) { |
|---|
| 1505 | n/a | if (!PyErr_Occurred()) |
|---|
| 1506 | n/a | PyErr_SetObject(PyExc_AttributeError, name->object); |
|---|
| 1507 | n/a | return NULL; |
|---|
| 1508 | n/a | } |
|---|
| 1509 | n/a | |
|---|
| 1510 | n/a | retval = call_unbound(unbound, func, obj, args, nargs); |
|---|
| 1511 | n/a | Py_DECREF(func); |
|---|
| 1512 | n/a | return retval; |
|---|
| 1513 | n/a | } |
|---|
| 1514 | n/a | |
|---|
| 1515 | n/a | /* Clone of call_method() that returns NotImplemented when the lookup fails. */ |
|---|
| 1516 | n/a | |
|---|
| 1517 | n/a | static PyObject * |
|---|
| 1518 | n/a | call_maybe(PyObject *obj, _Py_Identifier *name, |
|---|
| 1519 | n/a | PyObject **args, Py_ssize_t nargs) |
|---|
| 1520 | n/a | { |
|---|
| 1521 | n/a | int unbound; |
|---|
| 1522 | n/a | PyObject *func, *retval; |
|---|
| 1523 | n/a | |
|---|
| 1524 | n/a | func = lookup_maybe_method(obj, name, &unbound); |
|---|
| 1525 | n/a | if (func == NULL) { |
|---|
| 1526 | n/a | if (!PyErr_Occurred()) |
|---|
| 1527 | n/a | Py_RETURN_NOTIMPLEMENTED; |
|---|
| 1528 | n/a | return NULL; |
|---|
| 1529 | n/a | } |
|---|
| 1530 | n/a | |
|---|
| 1531 | n/a | retval = call_unbound(unbound, func, obj, args, nargs); |
|---|
| 1532 | n/a | Py_DECREF(func); |
|---|
| 1533 | n/a | return retval; |
|---|
| 1534 | n/a | } |
|---|
| 1535 | n/a | |
|---|
| 1536 | n/a | /* |
|---|
| 1537 | n/a | Method resolution order algorithm C3 described in |
|---|
| 1538 | n/a | "A Monotonic Superclass Linearization for Dylan", |
|---|
| 1539 | n/a | by Kim Barrett, Bob Cassel, Paul Haahr, |
|---|
| 1540 | n/a | David A. Moon, Keith Playford, and P. Tucker Withington. |
|---|
| 1541 | n/a | (OOPSLA 1996) |
|---|
| 1542 | n/a | |
|---|
| 1543 | n/a | Some notes about the rules implied by C3: |
|---|
| 1544 | n/a | |
|---|
| 1545 | n/a | No duplicate bases. |
|---|
| 1546 | n/a | It isn't legal to repeat a class in a list of base classes. |
|---|
| 1547 | n/a | |
|---|
| 1548 | n/a | The next three properties are the 3 constraints in "C3". |
|---|
| 1549 | n/a | |
|---|
| 1550 | n/a | Local precedence order. |
|---|
| 1551 | n/a | If A precedes B in C's MRO, then A will precede B in the MRO of all |
|---|
| 1552 | n/a | subclasses of C. |
|---|
| 1553 | n/a | |
|---|
| 1554 | n/a | Monotonicity. |
|---|
| 1555 | n/a | The MRO of a class must be an extension without reordering of the |
|---|
| 1556 | n/a | MRO of each of its superclasses. |
|---|
| 1557 | n/a | |
|---|
| 1558 | n/a | Extended Precedence Graph (EPG). |
|---|
| 1559 | n/a | Linearization is consistent if there is a path in the EPG from |
|---|
| 1560 | n/a | each class to all its successors in the linearization. See |
|---|
| 1561 | n/a | the paper for definition of EPG. |
|---|
| 1562 | n/a | */ |
|---|
| 1563 | n/a | |
|---|
| 1564 | n/a | static int |
|---|
| 1565 | n/a | tail_contains(PyObject *list, int whence, PyObject *o) { |
|---|
| 1566 | n/a | Py_ssize_t j, size; |
|---|
| 1567 | n/a | size = PyList_GET_SIZE(list); |
|---|
| 1568 | n/a | |
|---|
| 1569 | n/a | for (j = whence+1; j < size; j++) { |
|---|
| 1570 | n/a | if (PyList_GET_ITEM(list, j) == o) |
|---|
| 1571 | n/a | return 1; |
|---|
| 1572 | n/a | } |
|---|
| 1573 | n/a | return 0; |
|---|
| 1574 | n/a | } |
|---|
| 1575 | n/a | |
|---|
| 1576 | n/a | static PyObject * |
|---|
| 1577 | n/a | class_name(PyObject *cls) |
|---|
| 1578 | n/a | { |
|---|
| 1579 | n/a | PyObject *name = _PyObject_GetAttrId(cls, &PyId___name__); |
|---|
| 1580 | n/a | if (name == NULL) { |
|---|
| 1581 | n/a | PyErr_Clear(); |
|---|
| 1582 | n/a | name = PyObject_Repr(cls); |
|---|
| 1583 | n/a | } |
|---|
| 1584 | n/a | if (name == NULL) |
|---|
| 1585 | n/a | return NULL; |
|---|
| 1586 | n/a | if (!PyUnicode_Check(name)) { |
|---|
| 1587 | n/a | Py_DECREF(name); |
|---|
| 1588 | n/a | return NULL; |
|---|
| 1589 | n/a | } |
|---|
| 1590 | n/a | return name; |
|---|
| 1591 | n/a | } |
|---|
| 1592 | n/a | |
|---|
| 1593 | n/a | static int |
|---|
| 1594 | n/a | check_duplicates(PyObject *list) |
|---|
| 1595 | n/a | { |
|---|
| 1596 | n/a | Py_ssize_t i, j, n; |
|---|
| 1597 | n/a | /* Let's use a quadratic time algorithm, |
|---|
| 1598 | n/a | assuming that the bases lists is short. |
|---|
| 1599 | n/a | */ |
|---|
| 1600 | n/a | n = PyList_GET_SIZE(list); |
|---|
| 1601 | n/a | for (i = 0; i < n; i++) { |
|---|
| 1602 | n/a | PyObject *o = PyList_GET_ITEM(list, i); |
|---|
| 1603 | n/a | for (j = i + 1; j < n; j++) { |
|---|
| 1604 | n/a | if (PyList_GET_ITEM(list, j) == o) { |
|---|
| 1605 | n/a | o = class_name(o); |
|---|
| 1606 | n/a | if (o != NULL) { |
|---|
| 1607 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 1608 | n/a | "duplicate base class %U", |
|---|
| 1609 | n/a | o); |
|---|
| 1610 | n/a | Py_DECREF(o); |
|---|
| 1611 | n/a | } else { |
|---|
| 1612 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 1613 | n/a | "duplicate base class"); |
|---|
| 1614 | n/a | } |
|---|
| 1615 | n/a | return -1; |
|---|
| 1616 | n/a | } |
|---|
| 1617 | n/a | } |
|---|
| 1618 | n/a | } |
|---|
| 1619 | n/a | return 0; |
|---|
| 1620 | n/a | } |
|---|
| 1621 | n/a | |
|---|
| 1622 | n/a | /* Raise a TypeError for an MRO order disagreement. |
|---|
| 1623 | n/a | |
|---|
| 1624 | n/a | It's hard to produce a good error message. In the absence of better |
|---|
| 1625 | n/a | insight into error reporting, report the classes that were candidates |
|---|
| 1626 | n/a | to be put next into the MRO. There is some conflict between the |
|---|
| 1627 | n/a | order in which they should be put in the MRO, but it's hard to |
|---|
| 1628 | n/a | diagnose what constraint can't be satisfied. |
|---|
| 1629 | n/a | */ |
|---|
| 1630 | n/a | |
|---|
| 1631 | n/a | static void |
|---|
| 1632 | n/a | set_mro_error(PyObject *to_merge, int *remain) |
|---|
| 1633 | n/a | { |
|---|
| 1634 | n/a | Py_ssize_t i, n, off, to_merge_size; |
|---|
| 1635 | n/a | char buf[1000]; |
|---|
| 1636 | n/a | PyObject *k, *v; |
|---|
| 1637 | n/a | PyObject *set = PyDict_New(); |
|---|
| 1638 | n/a | if (!set) return; |
|---|
| 1639 | n/a | |
|---|
| 1640 | n/a | to_merge_size = PyList_GET_SIZE(to_merge); |
|---|
| 1641 | n/a | for (i = 0; i < to_merge_size; i++) { |
|---|
| 1642 | n/a | PyObject *L = PyList_GET_ITEM(to_merge, i); |
|---|
| 1643 | n/a | if (remain[i] < PyList_GET_SIZE(L)) { |
|---|
| 1644 | n/a | PyObject *c = PyList_GET_ITEM(L, remain[i]); |
|---|
| 1645 | n/a | if (PyDict_SetItem(set, c, Py_None) < 0) { |
|---|
| 1646 | n/a | Py_DECREF(set); |
|---|
| 1647 | n/a | return; |
|---|
| 1648 | n/a | } |
|---|
| 1649 | n/a | } |
|---|
| 1650 | n/a | } |
|---|
| 1651 | n/a | n = PyDict_GET_SIZE(set); |
|---|
| 1652 | n/a | |
|---|
| 1653 | n/a | off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \ |
|---|
| 1654 | n/a | consistent method resolution\norder (MRO) for bases"); |
|---|
| 1655 | n/a | i = 0; |
|---|
| 1656 | n/a | while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) { |
|---|
| 1657 | n/a | PyObject *name = class_name(k); |
|---|
| 1658 | n/a | const char *name_str; |
|---|
| 1659 | n/a | if (name != NULL) { |
|---|
| 1660 | n/a | name_str = PyUnicode_AsUTF8(name); |
|---|
| 1661 | n/a | if (name_str == NULL) |
|---|
| 1662 | n/a | name_str = "?"; |
|---|
| 1663 | n/a | } else |
|---|
| 1664 | n/a | name_str = "?"; |
|---|
| 1665 | n/a | off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", name_str); |
|---|
| 1666 | n/a | Py_XDECREF(name); |
|---|
| 1667 | n/a | if (--n && (size_t)(off+1) < sizeof(buf)) { |
|---|
| 1668 | n/a | buf[off++] = ','; |
|---|
| 1669 | n/a | buf[off] = '\0'; |
|---|
| 1670 | n/a | } |
|---|
| 1671 | n/a | } |
|---|
| 1672 | n/a | PyErr_SetString(PyExc_TypeError, buf); |
|---|
| 1673 | n/a | Py_DECREF(set); |
|---|
| 1674 | n/a | } |
|---|
| 1675 | n/a | |
|---|
| 1676 | n/a | static int |
|---|
| 1677 | n/a | pmerge(PyObject *acc, PyObject* to_merge) |
|---|
| 1678 | n/a | { |
|---|
| 1679 | n/a | int res = 0; |
|---|
| 1680 | n/a | Py_ssize_t i, j, to_merge_size, empty_cnt; |
|---|
| 1681 | n/a | int *remain; |
|---|
| 1682 | n/a | |
|---|
| 1683 | n/a | to_merge_size = PyList_GET_SIZE(to_merge); |
|---|
| 1684 | n/a | |
|---|
| 1685 | n/a | /* remain stores an index into each sublist of to_merge. |
|---|
| 1686 | n/a | remain[i] is the index of the next base in to_merge[i] |
|---|
| 1687 | n/a | that is not included in acc. |
|---|
| 1688 | n/a | */ |
|---|
| 1689 | n/a | remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size); |
|---|
| 1690 | n/a | if (remain == NULL) { |
|---|
| 1691 | n/a | PyErr_NoMemory(); |
|---|
| 1692 | n/a | return -1; |
|---|
| 1693 | n/a | } |
|---|
| 1694 | n/a | for (i = 0; i < to_merge_size; i++) |
|---|
| 1695 | n/a | remain[i] = 0; |
|---|
| 1696 | n/a | |
|---|
| 1697 | n/a | again: |
|---|
| 1698 | n/a | empty_cnt = 0; |
|---|
| 1699 | n/a | for (i = 0; i < to_merge_size; i++) { |
|---|
| 1700 | n/a | PyObject *candidate; |
|---|
| 1701 | n/a | |
|---|
| 1702 | n/a | PyObject *cur_list = PyList_GET_ITEM(to_merge, i); |
|---|
| 1703 | n/a | |
|---|
| 1704 | n/a | if (remain[i] >= PyList_GET_SIZE(cur_list)) { |
|---|
| 1705 | n/a | empty_cnt++; |
|---|
| 1706 | n/a | continue; |
|---|
| 1707 | n/a | } |
|---|
| 1708 | n/a | |
|---|
| 1709 | n/a | /* Choose next candidate for MRO. |
|---|
| 1710 | n/a | |
|---|
| 1711 | n/a | The input sequences alone can determine the choice. |
|---|
| 1712 | n/a | If not, choose the class which appears in the MRO |
|---|
| 1713 | n/a | of the earliest direct superclass of the new class. |
|---|
| 1714 | n/a | */ |
|---|
| 1715 | n/a | |
|---|
| 1716 | n/a | candidate = PyList_GET_ITEM(cur_list, remain[i]); |
|---|
| 1717 | n/a | for (j = 0; j < to_merge_size; j++) { |
|---|
| 1718 | n/a | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
|---|
| 1719 | n/a | if (tail_contains(j_lst, remain[j], candidate)) |
|---|
| 1720 | n/a | goto skip; /* continue outer loop */ |
|---|
| 1721 | n/a | } |
|---|
| 1722 | n/a | res = PyList_Append(acc, candidate); |
|---|
| 1723 | n/a | if (res < 0) |
|---|
| 1724 | n/a | goto out; |
|---|
| 1725 | n/a | |
|---|
| 1726 | n/a | for (j = 0; j < to_merge_size; j++) { |
|---|
| 1727 | n/a | PyObject *j_lst = PyList_GET_ITEM(to_merge, j); |
|---|
| 1728 | n/a | if (remain[j] < PyList_GET_SIZE(j_lst) && |
|---|
| 1729 | n/a | PyList_GET_ITEM(j_lst, remain[j]) == candidate) { |
|---|
| 1730 | n/a | remain[j]++; |
|---|
| 1731 | n/a | } |
|---|
| 1732 | n/a | } |
|---|
| 1733 | n/a | goto again; |
|---|
| 1734 | n/a | skip: ; |
|---|
| 1735 | n/a | } |
|---|
| 1736 | n/a | |
|---|
| 1737 | n/a | if (empty_cnt != to_merge_size) { |
|---|
| 1738 | n/a | set_mro_error(to_merge, remain); |
|---|
| 1739 | n/a | res = -1; |
|---|
| 1740 | n/a | } |
|---|
| 1741 | n/a | |
|---|
| 1742 | n/a | out: |
|---|
| 1743 | n/a | PyMem_FREE(remain); |
|---|
| 1744 | n/a | |
|---|
| 1745 | n/a | return res; |
|---|
| 1746 | n/a | } |
|---|
| 1747 | n/a | |
|---|
| 1748 | n/a | static PyObject * |
|---|
| 1749 | n/a | mro_implementation(PyTypeObject *type) |
|---|
| 1750 | n/a | { |
|---|
| 1751 | n/a | PyObject *result = NULL; |
|---|
| 1752 | n/a | PyObject *bases; |
|---|
| 1753 | n/a | PyObject *to_merge, *bases_aslist; |
|---|
| 1754 | n/a | int res; |
|---|
| 1755 | n/a | Py_ssize_t i, n; |
|---|
| 1756 | n/a | |
|---|
| 1757 | n/a | if (type->tp_dict == NULL) { |
|---|
| 1758 | n/a | if (PyType_Ready(type) < 0) |
|---|
| 1759 | n/a | return NULL; |
|---|
| 1760 | n/a | } |
|---|
| 1761 | n/a | |
|---|
| 1762 | n/a | /* Find a superclass linearization that honors the constraints |
|---|
| 1763 | n/a | of the explicit lists of bases and the constraints implied by |
|---|
| 1764 | n/a | each base class. |
|---|
| 1765 | n/a | |
|---|
| 1766 | n/a | to_merge is a list of lists, where each list is a superclass |
|---|
| 1767 | n/a | linearization implied by a base class. The last element of |
|---|
| 1768 | n/a | to_merge is the declared list of bases. |
|---|
| 1769 | n/a | */ |
|---|
| 1770 | n/a | |
|---|
| 1771 | n/a | bases = type->tp_bases; |
|---|
| 1772 | n/a | n = PyTuple_GET_SIZE(bases); |
|---|
| 1773 | n/a | |
|---|
| 1774 | n/a | to_merge = PyList_New(n+1); |
|---|
| 1775 | n/a | if (to_merge == NULL) |
|---|
| 1776 | n/a | return NULL; |
|---|
| 1777 | n/a | |
|---|
| 1778 | n/a | for (i = 0; i < n; i++) { |
|---|
| 1779 | n/a | PyTypeObject *base; |
|---|
| 1780 | n/a | PyObject *base_mro_aslist; |
|---|
| 1781 | n/a | |
|---|
| 1782 | n/a | base = (PyTypeObject *)PyTuple_GET_ITEM(bases, i); |
|---|
| 1783 | n/a | if (base->tp_mro == NULL) { |
|---|
| 1784 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 1785 | n/a | "Cannot extend an incomplete type '%.100s'", |
|---|
| 1786 | n/a | base->tp_name); |
|---|
| 1787 | n/a | goto out; |
|---|
| 1788 | n/a | } |
|---|
| 1789 | n/a | |
|---|
| 1790 | n/a | base_mro_aslist = PySequence_List(base->tp_mro); |
|---|
| 1791 | n/a | if (base_mro_aslist == NULL) |
|---|
| 1792 | n/a | goto out; |
|---|
| 1793 | n/a | |
|---|
| 1794 | n/a | PyList_SET_ITEM(to_merge, i, base_mro_aslist); |
|---|
| 1795 | n/a | } |
|---|
| 1796 | n/a | |
|---|
| 1797 | n/a | bases_aslist = PySequence_List(bases); |
|---|
| 1798 | n/a | if (bases_aslist == NULL) |
|---|
| 1799 | n/a | goto out; |
|---|
| 1800 | n/a | /* This is just a basic sanity check. */ |
|---|
| 1801 | n/a | if (check_duplicates(bases_aslist) < 0) { |
|---|
| 1802 | n/a | Py_DECREF(bases_aslist); |
|---|
| 1803 | n/a | goto out; |
|---|
| 1804 | n/a | } |
|---|
| 1805 | n/a | PyList_SET_ITEM(to_merge, n, bases_aslist); |
|---|
| 1806 | n/a | |
|---|
| 1807 | n/a | result = Py_BuildValue("[O]", (PyObject *)type); |
|---|
| 1808 | n/a | if (result == NULL) |
|---|
| 1809 | n/a | goto out; |
|---|
| 1810 | n/a | |
|---|
| 1811 | n/a | res = pmerge(result, to_merge); |
|---|
| 1812 | n/a | if (res < 0) |
|---|
| 1813 | n/a | Py_CLEAR(result); |
|---|
| 1814 | n/a | |
|---|
| 1815 | n/a | out: |
|---|
| 1816 | n/a | Py_DECREF(to_merge); |
|---|
| 1817 | n/a | |
|---|
| 1818 | n/a | return result; |
|---|
| 1819 | n/a | } |
|---|
| 1820 | n/a | |
|---|
| 1821 | n/a | static PyObject * |
|---|
| 1822 | n/a | mro_external(PyObject *self) |
|---|
| 1823 | n/a | { |
|---|
| 1824 | n/a | PyTypeObject *type = (PyTypeObject *)self; |
|---|
| 1825 | n/a | |
|---|
| 1826 | n/a | return mro_implementation(type); |
|---|
| 1827 | n/a | } |
|---|
| 1828 | n/a | |
|---|
| 1829 | n/a | static int |
|---|
| 1830 | n/a | mro_check(PyTypeObject *type, PyObject *mro) |
|---|
| 1831 | n/a | { |
|---|
| 1832 | n/a | PyTypeObject *solid; |
|---|
| 1833 | n/a | Py_ssize_t i, n; |
|---|
| 1834 | n/a | |
|---|
| 1835 | n/a | solid = solid_base(type); |
|---|
| 1836 | n/a | |
|---|
| 1837 | n/a | n = PyTuple_GET_SIZE(mro); |
|---|
| 1838 | n/a | for (i = 0; i < n; i++) { |
|---|
| 1839 | n/a | PyTypeObject *base; |
|---|
| 1840 | n/a | PyObject *tmp; |
|---|
| 1841 | n/a | |
|---|
| 1842 | n/a | tmp = PyTuple_GET_ITEM(mro, i); |
|---|
| 1843 | n/a | if (!PyType_Check(tmp)) { |
|---|
| 1844 | n/a | PyErr_Format( |
|---|
| 1845 | n/a | PyExc_TypeError, |
|---|
| 1846 | n/a | "mro() returned a non-class ('%.500s')", |
|---|
| 1847 | n/a | Py_TYPE(tmp)->tp_name); |
|---|
| 1848 | n/a | return -1; |
|---|
| 1849 | n/a | } |
|---|
| 1850 | n/a | |
|---|
| 1851 | n/a | base = (PyTypeObject*)tmp; |
|---|
| 1852 | n/a | if (!PyType_IsSubtype(solid, solid_base(base))) { |
|---|
| 1853 | n/a | PyErr_Format( |
|---|
| 1854 | n/a | PyExc_TypeError, |
|---|
| 1855 | n/a | "mro() returned base with unsuitable layout ('%.500s')", |
|---|
| 1856 | n/a | base->tp_name); |
|---|
| 1857 | n/a | return -1; |
|---|
| 1858 | n/a | } |
|---|
| 1859 | n/a | } |
|---|
| 1860 | n/a | |
|---|
| 1861 | n/a | return 0; |
|---|
| 1862 | n/a | } |
|---|
| 1863 | n/a | |
|---|
| 1864 | n/a | /* Lookups an mcls.mro method, invokes it and checks the result (if needed, |
|---|
| 1865 | n/a | in case of a custom mro() implementation). |
|---|
| 1866 | n/a | |
|---|
| 1867 | n/a | Keep in mind that during execution of this function type->tp_mro |
|---|
| 1868 | n/a | can be replaced due to possible reentrance (for example, |
|---|
| 1869 | n/a | through type_set_bases): |
|---|
| 1870 | n/a | |
|---|
| 1871 | n/a | - when looking up the mcls.mro attribute (it could be |
|---|
| 1872 | n/a | a user-provided descriptor); |
|---|
| 1873 | n/a | |
|---|
| 1874 | n/a | - from inside a custom mro() itself; |
|---|
| 1875 | n/a | |
|---|
| 1876 | n/a | - through a finalizer of the return value of mro(). |
|---|
| 1877 | n/a | */ |
|---|
| 1878 | n/a | static PyObject * |
|---|
| 1879 | n/a | mro_invoke(PyTypeObject *type) |
|---|
| 1880 | n/a | { |
|---|
| 1881 | n/a | PyObject *mro_result; |
|---|
| 1882 | n/a | PyObject *new_mro; |
|---|
| 1883 | n/a | int custom = (Py_TYPE(type) != &PyType_Type); |
|---|
| 1884 | n/a | |
|---|
| 1885 | n/a | if (custom) { |
|---|
| 1886 | n/a | _Py_IDENTIFIER(mro); |
|---|
| 1887 | n/a | int unbound; |
|---|
| 1888 | n/a | PyObject *mro_meth = lookup_method((PyObject *)type, &PyId_mro, |
|---|
| 1889 | n/a | &unbound); |
|---|
| 1890 | n/a | if (mro_meth == NULL) |
|---|
| 1891 | n/a | return NULL; |
|---|
| 1892 | n/a | mro_result = call_unbound_noarg(unbound, mro_meth, (PyObject *)type); |
|---|
| 1893 | n/a | Py_DECREF(mro_meth); |
|---|
| 1894 | n/a | } |
|---|
| 1895 | n/a | else { |
|---|
| 1896 | n/a | mro_result = mro_implementation(type); |
|---|
| 1897 | n/a | } |
|---|
| 1898 | n/a | if (mro_result == NULL) |
|---|
| 1899 | n/a | return NULL; |
|---|
| 1900 | n/a | |
|---|
| 1901 | n/a | new_mro = PySequence_Tuple(mro_result); |
|---|
| 1902 | n/a | Py_DECREF(mro_result); |
|---|
| 1903 | n/a | if (new_mro == NULL) |
|---|
| 1904 | n/a | return NULL; |
|---|
| 1905 | n/a | |
|---|
| 1906 | n/a | if (custom && mro_check(type, new_mro) < 0) { |
|---|
| 1907 | n/a | Py_DECREF(new_mro); |
|---|
| 1908 | n/a | return NULL; |
|---|
| 1909 | n/a | } |
|---|
| 1910 | n/a | |
|---|
| 1911 | n/a | return new_mro; |
|---|
| 1912 | n/a | } |
|---|
| 1913 | n/a | |
|---|
| 1914 | n/a | /* Calculates and assigns a new MRO to type->tp_mro. |
|---|
| 1915 | n/a | Return values and invariants: |
|---|
| 1916 | n/a | |
|---|
| 1917 | n/a | - Returns 1 if a new MRO value has been set to type->tp_mro due to |
|---|
| 1918 | n/a | this call of mro_internal (no tricky reentrancy and no errors). |
|---|
| 1919 | n/a | |
|---|
| 1920 | n/a | In case if p_old_mro argument is not NULL, a previous value |
|---|
| 1921 | n/a | of type->tp_mro is put there, and the ownership of this |
|---|
| 1922 | n/a | reference is transferred to a caller. |
|---|
| 1923 | n/a | Otherwise, the previous value (if any) is decref'ed. |
|---|
| 1924 | n/a | |
|---|
| 1925 | n/a | - Returns 0 in case when type->tp_mro gets changed because of |
|---|
| 1926 | n/a | reentering here through a custom mro() (see a comment to mro_invoke). |
|---|
| 1927 | n/a | |
|---|
| 1928 | n/a | In this case, a refcount of an old type->tp_mro is adjusted |
|---|
| 1929 | n/a | somewhere deeper in the call stack (by the innermost mro_internal |
|---|
| 1930 | n/a | or its caller) and may become zero upon returning from here. |
|---|
| 1931 | n/a | This also implies that the whole hierarchy of subclasses of the type |
|---|
| 1932 | n/a | has seen the new value and updated their MRO accordingly. |
|---|
| 1933 | n/a | |
|---|
| 1934 | n/a | - Returns -1 in case of an error. |
|---|
| 1935 | n/a | */ |
|---|
| 1936 | n/a | static int |
|---|
| 1937 | n/a | mro_internal(PyTypeObject *type, PyObject **p_old_mro) |
|---|
| 1938 | n/a | { |
|---|
| 1939 | n/a | PyObject *new_mro, *old_mro; |
|---|
| 1940 | n/a | int reent; |
|---|
| 1941 | n/a | |
|---|
| 1942 | n/a | /* Keep a reference to be able to do a reentrancy check below. |
|---|
| 1943 | n/a | Don't let old_mro be GC'ed and its address be reused for |
|---|
| 1944 | n/a | another object, like (suddenly!) a new tp_mro. */ |
|---|
| 1945 | n/a | old_mro = type->tp_mro; |
|---|
| 1946 | n/a | Py_XINCREF(old_mro); |
|---|
| 1947 | n/a | new_mro = mro_invoke(type); /* might cause reentrance */ |
|---|
| 1948 | n/a | reent = (type->tp_mro != old_mro); |
|---|
| 1949 | n/a | Py_XDECREF(old_mro); |
|---|
| 1950 | n/a | if (new_mro == NULL) |
|---|
| 1951 | n/a | return -1; |
|---|
| 1952 | n/a | |
|---|
| 1953 | n/a | if (reent) { |
|---|
| 1954 | n/a | Py_DECREF(new_mro); |
|---|
| 1955 | n/a | return 0; |
|---|
| 1956 | n/a | } |
|---|
| 1957 | n/a | |
|---|
| 1958 | n/a | type->tp_mro = new_mro; |
|---|
| 1959 | n/a | |
|---|
| 1960 | n/a | type_mro_modified(type, type->tp_mro); |
|---|
| 1961 | n/a | /* corner case: the super class might have been hidden |
|---|
| 1962 | n/a | from the custom MRO */ |
|---|
| 1963 | n/a | type_mro_modified(type, type->tp_bases); |
|---|
| 1964 | n/a | |
|---|
| 1965 | n/a | PyType_Modified(type); |
|---|
| 1966 | n/a | |
|---|
| 1967 | n/a | if (p_old_mro != NULL) |
|---|
| 1968 | n/a | *p_old_mro = old_mro; /* transfer the ownership */ |
|---|
| 1969 | n/a | else |
|---|
| 1970 | n/a | Py_XDECREF(old_mro); |
|---|
| 1971 | n/a | |
|---|
| 1972 | n/a | return 1; |
|---|
| 1973 | n/a | } |
|---|
| 1974 | n/a | |
|---|
| 1975 | n/a | |
|---|
| 1976 | n/a | /* Calculate the best base amongst multiple base classes. |
|---|
| 1977 | n/a | This is the first one that's on the path to the "solid base". */ |
|---|
| 1978 | n/a | |
|---|
| 1979 | n/a | static PyTypeObject * |
|---|
| 1980 | n/a | best_base(PyObject *bases) |
|---|
| 1981 | n/a | { |
|---|
| 1982 | n/a | Py_ssize_t i, n; |
|---|
| 1983 | n/a | PyTypeObject *base, *winner, *candidate, *base_i; |
|---|
| 1984 | n/a | PyObject *base_proto; |
|---|
| 1985 | n/a | |
|---|
| 1986 | n/a | assert(PyTuple_Check(bases)); |
|---|
| 1987 | n/a | n = PyTuple_GET_SIZE(bases); |
|---|
| 1988 | n/a | assert(n > 0); |
|---|
| 1989 | n/a | base = NULL; |
|---|
| 1990 | n/a | winner = NULL; |
|---|
| 1991 | n/a | for (i = 0; i < n; i++) { |
|---|
| 1992 | n/a | base_proto = PyTuple_GET_ITEM(bases, i); |
|---|
| 1993 | n/a | if (!PyType_Check(base_proto)) { |
|---|
| 1994 | n/a | PyErr_SetString( |
|---|
| 1995 | n/a | PyExc_TypeError, |
|---|
| 1996 | n/a | "bases must be types"); |
|---|
| 1997 | n/a | return NULL; |
|---|
| 1998 | n/a | } |
|---|
| 1999 | n/a | base_i = (PyTypeObject *)base_proto; |
|---|
| 2000 | n/a | if (base_i->tp_dict == NULL) { |
|---|
| 2001 | n/a | if (PyType_Ready(base_i) < 0) |
|---|
| 2002 | n/a | return NULL; |
|---|
| 2003 | n/a | } |
|---|
| 2004 | n/a | if (!PyType_HasFeature(base_i, Py_TPFLAGS_BASETYPE)) { |
|---|
| 2005 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 2006 | n/a | "type '%.100s' is not an acceptable base type", |
|---|
| 2007 | n/a | base_i->tp_name); |
|---|
| 2008 | n/a | return NULL; |
|---|
| 2009 | n/a | } |
|---|
| 2010 | n/a | candidate = solid_base(base_i); |
|---|
| 2011 | n/a | if (winner == NULL) { |
|---|
| 2012 | n/a | winner = candidate; |
|---|
| 2013 | n/a | base = base_i; |
|---|
| 2014 | n/a | } |
|---|
| 2015 | n/a | else if (PyType_IsSubtype(winner, candidate)) |
|---|
| 2016 | n/a | ; |
|---|
| 2017 | n/a | else if (PyType_IsSubtype(candidate, winner)) { |
|---|
| 2018 | n/a | winner = candidate; |
|---|
| 2019 | n/a | base = base_i; |
|---|
| 2020 | n/a | } |
|---|
| 2021 | n/a | else { |
|---|
| 2022 | n/a | PyErr_SetString( |
|---|
| 2023 | n/a | PyExc_TypeError, |
|---|
| 2024 | n/a | "multiple bases have " |
|---|
| 2025 | n/a | "instance lay-out conflict"); |
|---|
| 2026 | n/a | return NULL; |
|---|
| 2027 | n/a | } |
|---|
| 2028 | n/a | } |
|---|
| 2029 | n/a | assert (base != NULL); |
|---|
| 2030 | n/a | |
|---|
| 2031 | n/a | return base; |
|---|
| 2032 | n/a | } |
|---|
| 2033 | n/a | |
|---|
| 2034 | n/a | static int |
|---|
| 2035 | n/a | extra_ivars(PyTypeObject *type, PyTypeObject *base) |
|---|
| 2036 | n/a | { |
|---|
| 2037 | n/a | size_t t_size = type->tp_basicsize; |
|---|
| 2038 | n/a | size_t b_size = base->tp_basicsize; |
|---|
| 2039 | n/a | |
|---|
| 2040 | n/a | assert(t_size >= b_size); /* Else type smaller than base! */ |
|---|
| 2041 | n/a | if (type->tp_itemsize || base->tp_itemsize) { |
|---|
| 2042 | n/a | /* If itemsize is involved, stricter rules */ |
|---|
| 2043 | n/a | return t_size != b_size || |
|---|
| 2044 | n/a | type->tp_itemsize != base->tp_itemsize; |
|---|
| 2045 | n/a | } |
|---|
| 2046 | n/a | if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 && |
|---|
| 2047 | n/a | type->tp_weaklistoffset + sizeof(PyObject *) == t_size && |
|---|
| 2048 | n/a | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
|---|
| 2049 | n/a | t_size -= sizeof(PyObject *); |
|---|
| 2050 | n/a | if (type->tp_dictoffset && base->tp_dictoffset == 0 && |
|---|
| 2051 | n/a | type->tp_dictoffset + sizeof(PyObject *) == t_size && |
|---|
| 2052 | n/a | type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
|---|
| 2053 | n/a | t_size -= sizeof(PyObject *); |
|---|
| 2054 | n/a | |
|---|
| 2055 | n/a | return t_size != b_size; |
|---|
| 2056 | n/a | } |
|---|
| 2057 | n/a | |
|---|
| 2058 | n/a | static PyTypeObject * |
|---|
| 2059 | n/a | solid_base(PyTypeObject *type) |
|---|
| 2060 | n/a | { |
|---|
| 2061 | n/a | PyTypeObject *base; |
|---|
| 2062 | n/a | |
|---|
| 2063 | n/a | if (type->tp_base) |
|---|
| 2064 | n/a | base = solid_base(type->tp_base); |
|---|
| 2065 | n/a | else |
|---|
| 2066 | n/a | base = &PyBaseObject_Type; |
|---|
| 2067 | n/a | if (extra_ivars(type, base)) |
|---|
| 2068 | n/a | return type; |
|---|
| 2069 | n/a | else |
|---|
| 2070 | n/a | return base; |
|---|
| 2071 | n/a | } |
|---|
| 2072 | n/a | |
|---|
| 2073 | n/a | static void object_dealloc(PyObject *); |
|---|
| 2074 | n/a | static int object_init(PyObject *, PyObject *, PyObject *); |
|---|
| 2075 | n/a | static int update_slot(PyTypeObject *, PyObject *); |
|---|
| 2076 | n/a | static void fixup_slot_dispatchers(PyTypeObject *); |
|---|
| 2077 | n/a | static int set_names(PyTypeObject *); |
|---|
| 2078 | n/a | static int init_subclass(PyTypeObject *, PyObject *); |
|---|
| 2079 | n/a | |
|---|
| 2080 | n/a | /* |
|---|
| 2081 | n/a | * Helpers for __dict__ descriptor. We don't want to expose the dicts |
|---|
| 2082 | n/a | * inherited from various builtin types. The builtin base usually provides |
|---|
| 2083 | n/a | * its own __dict__ descriptor, so we use that when we can. |
|---|
| 2084 | n/a | */ |
|---|
| 2085 | n/a | static PyTypeObject * |
|---|
| 2086 | n/a | get_builtin_base_with_dict(PyTypeObject *type) |
|---|
| 2087 | n/a | { |
|---|
| 2088 | n/a | while (type->tp_base != NULL) { |
|---|
| 2089 | n/a | if (type->tp_dictoffset != 0 && |
|---|
| 2090 | n/a | !(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
|---|
| 2091 | n/a | return type; |
|---|
| 2092 | n/a | type = type->tp_base; |
|---|
| 2093 | n/a | } |
|---|
| 2094 | n/a | return NULL; |
|---|
| 2095 | n/a | } |
|---|
| 2096 | n/a | |
|---|
| 2097 | n/a | static PyObject * |
|---|
| 2098 | n/a | get_dict_descriptor(PyTypeObject *type) |
|---|
| 2099 | n/a | { |
|---|
| 2100 | n/a | PyObject *descr; |
|---|
| 2101 | n/a | |
|---|
| 2102 | n/a | descr = _PyType_LookupId(type, &PyId___dict__); |
|---|
| 2103 | n/a | if (descr == NULL || !PyDescr_IsData(descr)) |
|---|
| 2104 | n/a | return NULL; |
|---|
| 2105 | n/a | |
|---|
| 2106 | n/a | return descr; |
|---|
| 2107 | n/a | } |
|---|
| 2108 | n/a | |
|---|
| 2109 | n/a | static void |
|---|
| 2110 | n/a | raise_dict_descr_error(PyObject *obj) |
|---|
| 2111 | n/a | { |
|---|
| 2112 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 2113 | n/a | "this __dict__ descriptor does not support " |
|---|
| 2114 | n/a | "'%.200s' objects", Py_TYPE(obj)->tp_name); |
|---|
| 2115 | n/a | } |
|---|
| 2116 | n/a | |
|---|
| 2117 | n/a | static PyObject * |
|---|
| 2118 | n/a | subtype_dict(PyObject *obj, void *context) |
|---|
| 2119 | n/a | { |
|---|
| 2120 | n/a | PyTypeObject *base; |
|---|
| 2121 | n/a | |
|---|
| 2122 | n/a | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
|---|
| 2123 | n/a | if (base != NULL) { |
|---|
| 2124 | n/a | descrgetfunc func; |
|---|
| 2125 | n/a | PyObject *descr = get_dict_descriptor(base); |
|---|
| 2126 | n/a | if (descr == NULL) { |
|---|
| 2127 | n/a | raise_dict_descr_error(obj); |
|---|
| 2128 | n/a | return NULL; |
|---|
| 2129 | n/a | } |
|---|
| 2130 | n/a | func = Py_TYPE(descr)->tp_descr_get; |
|---|
| 2131 | n/a | if (func == NULL) { |
|---|
| 2132 | n/a | raise_dict_descr_error(obj); |
|---|
| 2133 | n/a | return NULL; |
|---|
| 2134 | n/a | } |
|---|
| 2135 | n/a | return func(descr, obj, (PyObject *)(Py_TYPE(obj))); |
|---|
| 2136 | n/a | } |
|---|
| 2137 | n/a | return PyObject_GenericGetDict(obj, context); |
|---|
| 2138 | n/a | } |
|---|
| 2139 | n/a | |
|---|
| 2140 | n/a | static int |
|---|
| 2141 | n/a | subtype_setdict(PyObject *obj, PyObject *value, void *context) |
|---|
| 2142 | n/a | { |
|---|
| 2143 | n/a | PyObject **dictptr; |
|---|
| 2144 | n/a | PyTypeObject *base; |
|---|
| 2145 | n/a | |
|---|
| 2146 | n/a | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
|---|
| 2147 | n/a | if (base != NULL) { |
|---|
| 2148 | n/a | descrsetfunc func; |
|---|
| 2149 | n/a | PyObject *descr = get_dict_descriptor(base); |
|---|
| 2150 | n/a | if (descr == NULL) { |
|---|
| 2151 | n/a | raise_dict_descr_error(obj); |
|---|
| 2152 | n/a | return -1; |
|---|
| 2153 | n/a | } |
|---|
| 2154 | n/a | func = Py_TYPE(descr)->tp_descr_set; |
|---|
| 2155 | n/a | if (func == NULL) { |
|---|
| 2156 | n/a | raise_dict_descr_error(obj); |
|---|
| 2157 | n/a | return -1; |
|---|
| 2158 | n/a | } |
|---|
| 2159 | n/a | return func(descr, obj, value); |
|---|
| 2160 | n/a | } |
|---|
| 2161 | n/a | /* Almost like PyObject_GenericSetDict, but allow __dict__ to be deleted. */ |
|---|
| 2162 | n/a | dictptr = _PyObject_GetDictPtr(obj); |
|---|
| 2163 | n/a | if (dictptr == NULL) { |
|---|
| 2164 | n/a | PyErr_SetString(PyExc_AttributeError, |
|---|
| 2165 | n/a | "This object has no __dict__"); |
|---|
| 2166 | n/a | return -1; |
|---|
| 2167 | n/a | } |
|---|
| 2168 | n/a | if (value != NULL && !PyDict_Check(value)) { |
|---|
| 2169 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 2170 | n/a | "__dict__ must be set to a dictionary, " |
|---|
| 2171 | n/a | "not a '%.200s'", Py_TYPE(value)->tp_name); |
|---|
| 2172 | n/a | return -1; |
|---|
| 2173 | n/a | } |
|---|
| 2174 | n/a | Py_XINCREF(value); |
|---|
| 2175 | n/a | Py_XSETREF(*dictptr, value); |
|---|
| 2176 | n/a | return 0; |
|---|
| 2177 | n/a | } |
|---|
| 2178 | n/a | |
|---|
| 2179 | n/a | static PyObject * |
|---|
| 2180 | n/a | subtype_getweakref(PyObject *obj, void *context) |
|---|
| 2181 | n/a | { |
|---|
| 2182 | n/a | PyObject **weaklistptr; |
|---|
| 2183 | n/a | PyObject *result; |
|---|
| 2184 | n/a | |
|---|
| 2185 | n/a | if (Py_TYPE(obj)->tp_weaklistoffset == 0) { |
|---|
| 2186 | n/a | PyErr_SetString(PyExc_AttributeError, |
|---|
| 2187 | n/a | "This object has no __weakref__"); |
|---|
| 2188 | n/a | return NULL; |
|---|
| 2189 | n/a | } |
|---|
| 2190 | n/a | assert(Py_TYPE(obj)->tp_weaklistoffset > 0); |
|---|
| 2191 | n/a | assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <= |
|---|
| 2192 | n/a | (size_t)(Py_TYPE(obj)->tp_basicsize)); |
|---|
| 2193 | n/a | weaklistptr = (PyObject **) |
|---|
| 2194 | n/a | ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset); |
|---|
| 2195 | n/a | if (*weaklistptr == NULL) |
|---|
| 2196 | n/a | result = Py_None; |
|---|
| 2197 | n/a | else |
|---|
| 2198 | n/a | result = *weaklistptr; |
|---|
| 2199 | n/a | Py_INCREF(result); |
|---|
| 2200 | n/a | return result; |
|---|
| 2201 | n/a | } |
|---|
| 2202 | n/a | |
|---|
| 2203 | n/a | /* Three variants on the subtype_getsets list. */ |
|---|
| 2204 | n/a | |
|---|
| 2205 | n/a | static PyGetSetDef subtype_getsets_full[] = { |
|---|
| 2206 | n/a | {"__dict__", subtype_dict, subtype_setdict, |
|---|
| 2207 | n/a | PyDoc_STR("dictionary for instance variables (if defined)")}, |
|---|
| 2208 | n/a | {"__weakref__", subtype_getweakref, NULL, |
|---|
| 2209 | n/a | PyDoc_STR("list of weak references to the object (if defined)")}, |
|---|
| 2210 | n/a | {0} |
|---|
| 2211 | n/a | }; |
|---|
| 2212 | n/a | |
|---|
| 2213 | n/a | static PyGetSetDef subtype_getsets_dict_only[] = { |
|---|
| 2214 | n/a | {"__dict__", subtype_dict, subtype_setdict, |
|---|
| 2215 | n/a | PyDoc_STR("dictionary for instance variables (if defined)")}, |
|---|
| 2216 | n/a | {0} |
|---|
| 2217 | n/a | }; |
|---|
| 2218 | n/a | |
|---|
| 2219 | n/a | static PyGetSetDef subtype_getsets_weakref_only[] = { |
|---|
| 2220 | n/a | {"__weakref__", subtype_getweakref, NULL, |
|---|
| 2221 | n/a | PyDoc_STR("list of weak references to the object (if defined)")}, |
|---|
| 2222 | n/a | {0} |
|---|
| 2223 | n/a | }; |
|---|
| 2224 | n/a | |
|---|
| 2225 | n/a | static int |
|---|
| 2226 | n/a | valid_identifier(PyObject *s) |
|---|
| 2227 | n/a | { |
|---|
| 2228 | n/a | if (!PyUnicode_Check(s)) { |
|---|
| 2229 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 2230 | n/a | "__slots__ items must be strings, not '%.200s'", |
|---|
| 2231 | n/a | Py_TYPE(s)->tp_name); |
|---|
| 2232 | n/a | return 0; |
|---|
| 2233 | n/a | } |
|---|
| 2234 | n/a | if (!PyUnicode_IsIdentifier(s)) { |
|---|
| 2235 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 2236 | n/a | "__slots__ must be identifiers"); |
|---|
| 2237 | n/a | return 0; |
|---|
| 2238 | n/a | } |
|---|
| 2239 | n/a | return 1; |
|---|
| 2240 | n/a | } |
|---|
| 2241 | n/a | |
|---|
| 2242 | n/a | /* Forward */ |
|---|
| 2243 | n/a | static int |
|---|
| 2244 | n/a | object_init(PyObject *self, PyObject *args, PyObject *kwds); |
|---|
| 2245 | n/a | |
|---|
| 2246 | n/a | static int |
|---|
| 2247 | n/a | type_init(PyObject *cls, PyObject *args, PyObject *kwds) |
|---|
| 2248 | n/a | { |
|---|
| 2249 | n/a | int res; |
|---|
| 2250 | n/a | |
|---|
| 2251 | n/a | assert(args != NULL && PyTuple_Check(args)); |
|---|
| 2252 | n/a | assert(kwds == NULL || PyDict_Check(kwds)); |
|---|
| 2253 | n/a | |
|---|
| 2254 | n/a | if (kwds != NULL && PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && |
|---|
| 2255 | n/a | PyDict_Check(kwds) && PyDict_GET_SIZE(kwds) != 0) { |
|---|
| 2256 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 2257 | n/a | "type.__init__() takes no keyword arguments"); |
|---|
| 2258 | n/a | return -1; |
|---|
| 2259 | n/a | } |
|---|
| 2260 | n/a | |
|---|
| 2261 | n/a | if (args != NULL && PyTuple_Check(args) && |
|---|
| 2262 | n/a | (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) { |
|---|
| 2263 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 2264 | n/a | "type.__init__() takes 1 or 3 arguments"); |
|---|
| 2265 | n/a | return -1; |
|---|
| 2266 | n/a | } |
|---|
| 2267 | n/a | |
|---|
| 2268 | n/a | /* Call object.__init__(self) now. */ |
|---|
| 2269 | n/a | /* XXX Could call super(type, cls).__init__() but what's the point? */ |
|---|
| 2270 | n/a | args = PyTuple_GetSlice(args, 0, 0); |
|---|
| 2271 | n/a | res = object_init(cls, args, NULL); |
|---|
| 2272 | n/a | Py_DECREF(args); |
|---|
| 2273 | n/a | return res; |
|---|
| 2274 | n/a | } |
|---|
| 2275 | n/a | |
|---|
| 2276 | n/a | unsigned long |
|---|
| 2277 | n/a | PyType_GetFlags(PyTypeObject *type) |
|---|
| 2278 | n/a | { |
|---|
| 2279 | n/a | return type->tp_flags; |
|---|
| 2280 | n/a | } |
|---|
| 2281 | n/a | |
|---|
| 2282 | n/a | /* Determine the most derived metatype. */ |
|---|
| 2283 | n/a | PyTypeObject * |
|---|
| 2284 | n/a | _PyType_CalculateMetaclass(PyTypeObject *metatype, PyObject *bases) |
|---|
| 2285 | n/a | { |
|---|
| 2286 | n/a | Py_ssize_t i, nbases; |
|---|
| 2287 | n/a | PyTypeObject *winner; |
|---|
| 2288 | n/a | PyObject *tmp; |
|---|
| 2289 | n/a | PyTypeObject *tmptype; |
|---|
| 2290 | n/a | |
|---|
| 2291 | n/a | /* Determine the proper metatype to deal with this, |
|---|
| 2292 | n/a | and check for metatype conflicts while we're at it. |
|---|
| 2293 | n/a | Note that if some other metatype wins to contract, |
|---|
| 2294 | n/a | it's possible that its instances are not types. */ |
|---|
| 2295 | n/a | |
|---|
| 2296 | n/a | nbases = PyTuple_GET_SIZE(bases); |
|---|
| 2297 | n/a | winner = metatype; |
|---|
| 2298 | n/a | for (i = 0; i < nbases; i++) { |
|---|
| 2299 | n/a | tmp = PyTuple_GET_ITEM(bases, i); |
|---|
| 2300 | n/a | tmptype = Py_TYPE(tmp); |
|---|
| 2301 | n/a | if (PyType_IsSubtype(winner, tmptype)) |
|---|
| 2302 | n/a | continue; |
|---|
| 2303 | n/a | if (PyType_IsSubtype(tmptype, winner)) { |
|---|
| 2304 | n/a | winner = tmptype; |
|---|
| 2305 | n/a | continue; |
|---|
| 2306 | n/a | } |
|---|
| 2307 | n/a | /* else: */ |
|---|
| 2308 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 2309 | n/a | "metaclass conflict: " |
|---|
| 2310 | n/a | "the metaclass of a derived class " |
|---|
| 2311 | n/a | "must be a (non-strict) subclass " |
|---|
| 2312 | n/a | "of the metaclasses of all its bases"); |
|---|
| 2313 | n/a | return NULL; |
|---|
| 2314 | n/a | } |
|---|
| 2315 | n/a | return winner; |
|---|
| 2316 | n/a | } |
|---|
| 2317 | n/a | |
|---|
| 2318 | n/a | static PyObject * |
|---|
| 2319 | n/a | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) |
|---|
| 2320 | n/a | { |
|---|
| 2321 | n/a | PyObject *name, *bases = NULL, *orig_dict, *dict = NULL; |
|---|
| 2322 | n/a | PyObject *qualname, *slots = NULL, *tmp, *newslots, *cell; |
|---|
| 2323 | n/a | PyTypeObject *type = NULL, *base, *tmptype, *winner; |
|---|
| 2324 | n/a | PyHeapTypeObject *et; |
|---|
| 2325 | n/a | PyMemberDef *mp; |
|---|
| 2326 | n/a | Py_ssize_t i, nbases, nslots, slotoffset, name_size; |
|---|
| 2327 | n/a | int j, may_add_dict, may_add_weak, add_dict, add_weak; |
|---|
| 2328 | n/a | _Py_IDENTIFIER(__qualname__); |
|---|
| 2329 | n/a | _Py_IDENTIFIER(__slots__); |
|---|
| 2330 | n/a | _Py_IDENTIFIER(__classcell__); |
|---|
| 2331 | n/a | |
|---|
| 2332 | n/a | assert(args != NULL && PyTuple_Check(args)); |
|---|
| 2333 | n/a | assert(kwds == NULL || PyDict_Check(kwds)); |
|---|
| 2334 | n/a | |
|---|
| 2335 | n/a | /* Special case: type(x) should return x->ob_type */ |
|---|
| 2336 | n/a | /* We only want type itself to accept the one-argument form (#27157) |
|---|
| 2337 | n/a | Note: We don't call PyType_CheckExact as that also allows subclasses */ |
|---|
| 2338 | n/a | if (metatype == &PyType_Type) { |
|---|
| 2339 | n/a | const Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
|---|
| 2340 | n/a | const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_GET_SIZE(kwds); |
|---|
| 2341 | n/a | |
|---|
| 2342 | n/a | if (nargs == 1 && nkwds == 0) { |
|---|
| 2343 | n/a | PyObject *x = PyTuple_GET_ITEM(args, 0); |
|---|
| 2344 | n/a | Py_INCREF(Py_TYPE(x)); |
|---|
| 2345 | n/a | return (PyObject *) Py_TYPE(x); |
|---|
| 2346 | n/a | } |
|---|
| 2347 | n/a | |
|---|
| 2348 | n/a | /* SF bug 475327 -- if that didn't trigger, we need 3 |
|---|
| 2349 | n/a | arguments. but PyArg_ParseTupleAndKeywords below may give |
|---|
| 2350 | n/a | a msg saying type() needs exactly 3. */ |
|---|
| 2351 | n/a | if (nargs != 3) { |
|---|
| 2352 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 2353 | n/a | "type() takes 1 or 3 arguments"); |
|---|
| 2354 | n/a | return NULL; |
|---|
| 2355 | n/a | } |
|---|
| 2356 | n/a | } |
|---|
| 2357 | n/a | |
|---|
| 2358 | n/a | /* Check arguments: (name, bases, dict) */ |
|---|
| 2359 | n/a | if (!PyArg_ParseTuple(args, "UO!O!:type.__new__", &name, &PyTuple_Type, |
|---|
| 2360 | n/a | &bases, &PyDict_Type, &orig_dict)) |
|---|
| 2361 | n/a | return NULL; |
|---|
| 2362 | n/a | |
|---|
| 2363 | n/a | /* Determine the proper metatype to deal with this: */ |
|---|
| 2364 | n/a | winner = _PyType_CalculateMetaclass(metatype, bases); |
|---|
| 2365 | n/a | if (winner == NULL) { |
|---|
| 2366 | n/a | return NULL; |
|---|
| 2367 | n/a | } |
|---|
| 2368 | n/a | |
|---|
| 2369 | n/a | if (winner != metatype) { |
|---|
| 2370 | n/a | if (winner->tp_new != type_new) /* Pass it to the winner */ |
|---|
| 2371 | n/a | return winner->tp_new(winner, args, kwds); |
|---|
| 2372 | n/a | metatype = winner; |
|---|
| 2373 | n/a | } |
|---|
| 2374 | n/a | |
|---|
| 2375 | n/a | /* Adjust for empty tuple bases */ |
|---|
| 2376 | n/a | nbases = PyTuple_GET_SIZE(bases); |
|---|
| 2377 | n/a | if (nbases == 0) { |
|---|
| 2378 | n/a | bases = PyTuple_Pack(1, &PyBaseObject_Type); |
|---|
| 2379 | n/a | if (bases == NULL) |
|---|
| 2380 | n/a | goto error; |
|---|
| 2381 | n/a | nbases = 1; |
|---|
| 2382 | n/a | } |
|---|
| 2383 | n/a | else |
|---|
| 2384 | n/a | Py_INCREF(bases); |
|---|
| 2385 | n/a | |
|---|
| 2386 | n/a | /* Calculate best base, and check that all bases are type objects */ |
|---|
| 2387 | n/a | base = best_base(bases); |
|---|
| 2388 | n/a | if (base == NULL) { |
|---|
| 2389 | n/a | goto error; |
|---|
| 2390 | n/a | } |
|---|
| 2391 | n/a | |
|---|
| 2392 | n/a | dict = PyDict_Copy(orig_dict); |
|---|
| 2393 | n/a | if (dict == NULL) |
|---|
| 2394 | n/a | goto error; |
|---|
| 2395 | n/a | |
|---|
| 2396 | n/a | /* Check for a __slots__ sequence variable in dict, and count it */ |
|---|
| 2397 | n/a | slots = _PyDict_GetItemId(dict, &PyId___slots__); |
|---|
| 2398 | n/a | nslots = 0; |
|---|
| 2399 | n/a | add_dict = 0; |
|---|
| 2400 | n/a | add_weak = 0; |
|---|
| 2401 | n/a | may_add_dict = base->tp_dictoffset == 0; |
|---|
| 2402 | n/a | may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0; |
|---|
| 2403 | n/a | if (slots == NULL) { |
|---|
| 2404 | n/a | if (may_add_dict) { |
|---|
| 2405 | n/a | add_dict++; |
|---|
| 2406 | n/a | } |
|---|
| 2407 | n/a | if (may_add_weak) { |
|---|
| 2408 | n/a | add_weak++; |
|---|
| 2409 | n/a | } |
|---|
| 2410 | n/a | } |
|---|
| 2411 | n/a | else { |
|---|
| 2412 | n/a | /* Have slots */ |
|---|
| 2413 | n/a | |
|---|
| 2414 | n/a | /* Make it into a tuple */ |
|---|
| 2415 | n/a | if (PyUnicode_Check(slots)) |
|---|
| 2416 | n/a | slots = PyTuple_Pack(1, slots); |
|---|
| 2417 | n/a | else |
|---|
| 2418 | n/a | slots = PySequence_Tuple(slots); |
|---|
| 2419 | n/a | if (slots == NULL) |
|---|
| 2420 | n/a | goto error; |
|---|
| 2421 | n/a | assert(PyTuple_Check(slots)); |
|---|
| 2422 | n/a | |
|---|
| 2423 | n/a | /* Are slots allowed? */ |
|---|
| 2424 | n/a | nslots = PyTuple_GET_SIZE(slots); |
|---|
| 2425 | n/a | if (nslots > 0 && base->tp_itemsize != 0) { |
|---|
| 2426 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 2427 | n/a | "nonempty __slots__ " |
|---|
| 2428 | n/a | "not supported for subtype of '%s'", |
|---|
| 2429 | n/a | base->tp_name); |
|---|
| 2430 | n/a | goto error; |
|---|
| 2431 | n/a | } |
|---|
| 2432 | n/a | |
|---|
| 2433 | n/a | /* Check for valid slot names and two special cases */ |
|---|
| 2434 | n/a | for (i = 0; i < nslots; i++) { |
|---|
| 2435 | n/a | PyObject *tmp = PyTuple_GET_ITEM(slots, i); |
|---|
| 2436 | n/a | if (!valid_identifier(tmp)) |
|---|
| 2437 | n/a | goto error; |
|---|
| 2438 | n/a | assert(PyUnicode_Check(tmp)); |
|---|
| 2439 | n/a | if (_PyUnicode_EqualToASCIIId(tmp, &PyId___dict__)) { |
|---|
| 2440 | n/a | if (!may_add_dict || add_dict) { |
|---|
| 2441 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 2442 | n/a | "__dict__ slot disallowed: " |
|---|
| 2443 | n/a | "we already got one"); |
|---|
| 2444 | n/a | goto error; |
|---|
| 2445 | n/a | } |
|---|
| 2446 | n/a | add_dict++; |
|---|
| 2447 | n/a | } |
|---|
| 2448 | n/a | if (_PyUnicode_EqualToASCIIString(tmp, "__weakref__")) { |
|---|
| 2449 | n/a | if (!may_add_weak || add_weak) { |
|---|
| 2450 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 2451 | n/a | "__weakref__ slot disallowed: " |
|---|
| 2452 | n/a | "either we already got one, " |
|---|
| 2453 | n/a | "or __itemsize__ != 0"); |
|---|
| 2454 | n/a | goto error; |
|---|
| 2455 | n/a | } |
|---|
| 2456 | n/a | add_weak++; |
|---|
| 2457 | n/a | } |
|---|
| 2458 | n/a | } |
|---|
| 2459 | n/a | |
|---|
| 2460 | n/a | /* Copy slots into a list, mangle names and sort them. |
|---|
| 2461 | n/a | Sorted names are needed for __class__ assignment. |
|---|
| 2462 | n/a | Convert them back to tuple at the end. |
|---|
| 2463 | n/a | */ |
|---|
| 2464 | n/a | newslots = PyList_New(nslots - add_dict - add_weak); |
|---|
| 2465 | n/a | if (newslots == NULL) |
|---|
| 2466 | n/a | goto error; |
|---|
| 2467 | n/a | for (i = j = 0; i < nslots; i++) { |
|---|
| 2468 | n/a | tmp = PyTuple_GET_ITEM(slots, i); |
|---|
| 2469 | n/a | if ((add_dict && |
|---|
| 2470 | n/a | _PyUnicode_EqualToASCIIId(tmp, &PyId___dict__)) || |
|---|
| 2471 | n/a | (add_weak && |
|---|
| 2472 | n/a | _PyUnicode_EqualToASCIIString(tmp, "__weakref__"))) |
|---|
| 2473 | n/a | continue; |
|---|
| 2474 | n/a | tmp =_Py_Mangle(name, tmp); |
|---|
| 2475 | n/a | if (!tmp) { |
|---|
| 2476 | n/a | Py_DECREF(newslots); |
|---|
| 2477 | n/a | goto error; |
|---|
| 2478 | n/a | } |
|---|
| 2479 | n/a | PyList_SET_ITEM(newslots, j, tmp); |
|---|
| 2480 | n/a | if (PyDict_GetItem(dict, tmp)) { |
|---|
| 2481 | n/a | PyErr_Format(PyExc_ValueError, |
|---|
| 2482 | n/a | "%R in __slots__ conflicts with class variable", |
|---|
| 2483 | n/a | tmp); |
|---|
| 2484 | n/a | Py_DECREF(newslots); |
|---|
| 2485 | n/a | goto error; |
|---|
| 2486 | n/a | } |
|---|
| 2487 | n/a | j++; |
|---|
| 2488 | n/a | } |
|---|
| 2489 | n/a | assert(j == nslots - add_dict - add_weak); |
|---|
| 2490 | n/a | nslots = j; |
|---|
| 2491 | n/a | Py_CLEAR(slots); |
|---|
| 2492 | n/a | if (PyList_Sort(newslots) == -1) { |
|---|
| 2493 | n/a | Py_DECREF(newslots); |
|---|
| 2494 | n/a | goto error; |
|---|
| 2495 | n/a | } |
|---|
| 2496 | n/a | slots = PyList_AsTuple(newslots); |
|---|
| 2497 | n/a | Py_DECREF(newslots); |
|---|
| 2498 | n/a | if (slots == NULL) |
|---|
| 2499 | n/a | goto error; |
|---|
| 2500 | n/a | |
|---|
| 2501 | n/a | /* Secondary bases may provide weakrefs or dict */ |
|---|
| 2502 | n/a | if (nbases > 1 && |
|---|
| 2503 | n/a | ((may_add_dict && !add_dict) || |
|---|
| 2504 | n/a | (may_add_weak && !add_weak))) { |
|---|
| 2505 | n/a | for (i = 0; i < nbases; i++) { |
|---|
| 2506 | n/a | tmp = PyTuple_GET_ITEM(bases, i); |
|---|
| 2507 | n/a | if (tmp == (PyObject *)base) |
|---|
| 2508 | n/a | continue; /* Skip primary base */ |
|---|
| 2509 | n/a | assert(PyType_Check(tmp)); |
|---|
| 2510 | n/a | tmptype = (PyTypeObject *)tmp; |
|---|
| 2511 | n/a | if (may_add_dict && !add_dict && |
|---|
| 2512 | n/a | tmptype->tp_dictoffset != 0) |
|---|
| 2513 | n/a | add_dict++; |
|---|
| 2514 | n/a | if (may_add_weak && !add_weak && |
|---|
| 2515 | n/a | tmptype->tp_weaklistoffset != 0) |
|---|
| 2516 | n/a | add_weak++; |
|---|
| 2517 | n/a | if (may_add_dict && !add_dict) |
|---|
| 2518 | n/a | continue; |
|---|
| 2519 | n/a | if (may_add_weak && !add_weak) |
|---|
| 2520 | n/a | continue; |
|---|
| 2521 | n/a | /* Nothing more to check */ |
|---|
| 2522 | n/a | break; |
|---|
| 2523 | n/a | } |
|---|
| 2524 | n/a | } |
|---|
| 2525 | n/a | } |
|---|
| 2526 | n/a | |
|---|
| 2527 | n/a | /* Allocate the type object */ |
|---|
| 2528 | n/a | type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots); |
|---|
| 2529 | n/a | if (type == NULL) |
|---|
| 2530 | n/a | goto error; |
|---|
| 2531 | n/a | |
|---|
| 2532 | n/a | /* Keep name and slots alive in the extended type object */ |
|---|
| 2533 | n/a | et = (PyHeapTypeObject *)type; |
|---|
| 2534 | n/a | Py_INCREF(name); |
|---|
| 2535 | n/a | et->ht_name = name; |
|---|
| 2536 | n/a | et->ht_slots = slots; |
|---|
| 2537 | n/a | slots = NULL; |
|---|
| 2538 | n/a | |
|---|
| 2539 | n/a | /* Initialize tp_flags */ |
|---|
| 2540 | n/a | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | |
|---|
| 2541 | n/a | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_FINALIZE; |
|---|
| 2542 | n/a | if (base->tp_flags & Py_TPFLAGS_HAVE_GC) |
|---|
| 2543 | n/a | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
|---|
| 2544 | n/a | |
|---|
| 2545 | n/a | /* Initialize essential fields */ |
|---|
| 2546 | n/a | type->tp_as_async = &et->as_async; |
|---|
| 2547 | n/a | type->tp_as_number = &et->as_number; |
|---|
| 2548 | n/a | type->tp_as_sequence = &et->as_sequence; |
|---|
| 2549 | n/a | type->tp_as_mapping = &et->as_mapping; |
|---|
| 2550 | n/a | type->tp_as_buffer = &et->as_buffer; |
|---|
| 2551 | n/a | type->tp_name = PyUnicode_AsUTF8AndSize(name, &name_size); |
|---|
| 2552 | n/a | if (!type->tp_name) |
|---|
| 2553 | n/a | goto error; |
|---|
| 2554 | n/a | if (strlen(type->tp_name) != (size_t)name_size) { |
|---|
| 2555 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 2556 | n/a | "type name must not contain null characters"); |
|---|
| 2557 | n/a | goto error; |
|---|
| 2558 | n/a | } |
|---|
| 2559 | n/a | |
|---|
| 2560 | n/a | /* Set tp_base and tp_bases */ |
|---|
| 2561 | n/a | type->tp_bases = bases; |
|---|
| 2562 | n/a | bases = NULL; |
|---|
| 2563 | n/a | Py_INCREF(base); |
|---|
| 2564 | n/a | type->tp_base = base; |
|---|
| 2565 | n/a | |
|---|
| 2566 | n/a | /* Initialize tp_dict from passed-in dict */ |
|---|
| 2567 | n/a | Py_INCREF(dict); |
|---|
| 2568 | n/a | type->tp_dict = dict; |
|---|
| 2569 | n/a | |
|---|
| 2570 | n/a | /* Set __module__ in the dict */ |
|---|
| 2571 | n/a | if (_PyDict_GetItemId(dict, &PyId___module__) == NULL) { |
|---|
| 2572 | n/a | tmp = PyEval_GetGlobals(); |
|---|
| 2573 | n/a | if (tmp != NULL) { |
|---|
| 2574 | n/a | tmp = _PyDict_GetItemId(tmp, &PyId___name__); |
|---|
| 2575 | n/a | if (tmp != NULL) { |
|---|
| 2576 | n/a | if (_PyDict_SetItemId(dict, &PyId___module__, |
|---|
| 2577 | n/a | tmp) < 0) |
|---|
| 2578 | n/a | goto error; |
|---|
| 2579 | n/a | } |
|---|
| 2580 | n/a | } |
|---|
| 2581 | n/a | } |
|---|
| 2582 | n/a | |
|---|
| 2583 | n/a | /* Set ht_qualname to dict['__qualname__'] if available, else to |
|---|
| 2584 | n/a | __name__. The __qualname__ accessor will look for ht_qualname. |
|---|
| 2585 | n/a | */ |
|---|
| 2586 | n/a | qualname = _PyDict_GetItemId(dict, &PyId___qualname__); |
|---|
| 2587 | n/a | if (qualname != NULL) { |
|---|
| 2588 | n/a | if (!PyUnicode_Check(qualname)) { |
|---|
| 2589 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 2590 | n/a | "type __qualname__ must be a str, not %s", |
|---|
| 2591 | n/a | Py_TYPE(qualname)->tp_name); |
|---|
| 2592 | n/a | goto error; |
|---|
| 2593 | n/a | } |
|---|
| 2594 | n/a | } |
|---|
| 2595 | n/a | et->ht_qualname = qualname ? qualname : et->ht_name; |
|---|
| 2596 | n/a | Py_INCREF(et->ht_qualname); |
|---|
| 2597 | n/a | if (qualname != NULL && _PyDict_DelItemId(dict, &PyId___qualname__) < 0) |
|---|
| 2598 | n/a | goto error; |
|---|
| 2599 | n/a | |
|---|
| 2600 | n/a | /* Set tp_doc to a copy of dict['__doc__'], if the latter is there |
|---|
| 2601 | n/a | and is a string. The __doc__ accessor will first look for tp_doc; |
|---|
| 2602 | n/a | if that fails, it will still look into __dict__. |
|---|
| 2603 | n/a | */ |
|---|
| 2604 | n/a | { |
|---|
| 2605 | n/a | PyObject *doc = _PyDict_GetItemId(dict, &PyId___doc__); |
|---|
| 2606 | n/a | if (doc != NULL && PyUnicode_Check(doc)) { |
|---|
| 2607 | n/a | Py_ssize_t len; |
|---|
| 2608 | n/a | const char *doc_str; |
|---|
| 2609 | n/a | char *tp_doc; |
|---|
| 2610 | n/a | |
|---|
| 2611 | n/a | doc_str = PyUnicode_AsUTF8(doc); |
|---|
| 2612 | n/a | if (doc_str == NULL) |
|---|
| 2613 | n/a | goto error; |
|---|
| 2614 | n/a | /* Silently truncate the docstring if it contains null bytes. */ |
|---|
| 2615 | n/a | len = strlen(doc_str); |
|---|
| 2616 | n/a | tp_doc = (char *)PyObject_MALLOC(len + 1); |
|---|
| 2617 | n/a | if (tp_doc == NULL) { |
|---|
| 2618 | n/a | PyErr_NoMemory(); |
|---|
| 2619 | n/a | goto error; |
|---|
| 2620 | n/a | } |
|---|
| 2621 | n/a | memcpy(tp_doc, doc_str, len + 1); |
|---|
| 2622 | n/a | type->tp_doc = tp_doc; |
|---|
| 2623 | n/a | } |
|---|
| 2624 | n/a | } |
|---|
| 2625 | n/a | |
|---|
| 2626 | n/a | /* Special-case __new__: if it's a plain function, |
|---|
| 2627 | n/a | make it a static function */ |
|---|
| 2628 | n/a | tmp = _PyDict_GetItemId(dict, &PyId___new__); |
|---|
| 2629 | n/a | if (tmp != NULL && PyFunction_Check(tmp)) { |
|---|
| 2630 | n/a | tmp = PyStaticMethod_New(tmp); |
|---|
| 2631 | n/a | if (tmp == NULL) |
|---|
| 2632 | n/a | goto error; |
|---|
| 2633 | n/a | if (_PyDict_SetItemId(dict, &PyId___new__, tmp) < 0) { |
|---|
| 2634 | n/a | Py_DECREF(tmp); |
|---|
| 2635 | n/a | goto error; |
|---|
| 2636 | n/a | } |
|---|
| 2637 | n/a | Py_DECREF(tmp); |
|---|
| 2638 | n/a | } |
|---|
| 2639 | n/a | |
|---|
| 2640 | n/a | /* Special-case __init_subclass__: if it's a plain function, |
|---|
| 2641 | n/a | make it a classmethod */ |
|---|
| 2642 | n/a | tmp = _PyDict_GetItemId(dict, &PyId___init_subclass__); |
|---|
| 2643 | n/a | if (tmp != NULL && PyFunction_Check(tmp)) { |
|---|
| 2644 | n/a | tmp = PyClassMethod_New(tmp); |
|---|
| 2645 | n/a | if (tmp == NULL) |
|---|
| 2646 | n/a | goto error; |
|---|
| 2647 | n/a | if (_PyDict_SetItemId(dict, &PyId___init_subclass__, tmp) < 0) { |
|---|
| 2648 | n/a | Py_DECREF(tmp); |
|---|
| 2649 | n/a | goto error; |
|---|
| 2650 | n/a | } |
|---|
| 2651 | n/a | Py_DECREF(tmp); |
|---|
| 2652 | n/a | } |
|---|
| 2653 | n/a | |
|---|
| 2654 | n/a | /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
|---|
| 2655 | n/a | mp = PyHeapType_GET_MEMBERS(et); |
|---|
| 2656 | n/a | slotoffset = base->tp_basicsize; |
|---|
| 2657 | n/a | if (et->ht_slots != NULL) { |
|---|
| 2658 | n/a | for (i = 0; i < nslots; i++, mp++) { |
|---|
| 2659 | n/a | mp->name = PyUnicode_AsUTF8( |
|---|
| 2660 | n/a | PyTuple_GET_ITEM(et->ht_slots, i)); |
|---|
| 2661 | n/a | if (mp->name == NULL) |
|---|
| 2662 | n/a | goto error; |
|---|
| 2663 | n/a | mp->type = T_OBJECT_EX; |
|---|
| 2664 | n/a | mp->offset = slotoffset; |
|---|
| 2665 | n/a | |
|---|
| 2666 | n/a | /* __dict__ and __weakref__ are already filtered out */ |
|---|
| 2667 | n/a | assert(strcmp(mp->name, "__dict__") != 0); |
|---|
| 2668 | n/a | assert(strcmp(mp->name, "__weakref__") != 0); |
|---|
| 2669 | n/a | |
|---|
| 2670 | n/a | slotoffset += sizeof(PyObject *); |
|---|
| 2671 | n/a | } |
|---|
| 2672 | n/a | } |
|---|
| 2673 | n/a | if (add_dict) { |
|---|
| 2674 | n/a | if (base->tp_itemsize) |
|---|
| 2675 | n/a | type->tp_dictoffset = -(long)sizeof(PyObject *); |
|---|
| 2676 | n/a | else |
|---|
| 2677 | n/a | type->tp_dictoffset = slotoffset; |
|---|
| 2678 | n/a | slotoffset += sizeof(PyObject *); |
|---|
| 2679 | n/a | } |
|---|
| 2680 | n/a | if (add_weak) { |
|---|
| 2681 | n/a | assert(!base->tp_itemsize); |
|---|
| 2682 | n/a | type->tp_weaklistoffset = slotoffset; |
|---|
| 2683 | n/a | slotoffset += sizeof(PyObject *); |
|---|
| 2684 | n/a | } |
|---|
| 2685 | n/a | type->tp_basicsize = slotoffset; |
|---|
| 2686 | n/a | type->tp_itemsize = base->tp_itemsize; |
|---|
| 2687 | n/a | type->tp_members = PyHeapType_GET_MEMBERS(et); |
|---|
| 2688 | n/a | |
|---|
| 2689 | n/a | if (type->tp_weaklistoffset && type->tp_dictoffset) |
|---|
| 2690 | n/a | type->tp_getset = subtype_getsets_full; |
|---|
| 2691 | n/a | else if (type->tp_weaklistoffset && !type->tp_dictoffset) |
|---|
| 2692 | n/a | type->tp_getset = subtype_getsets_weakref_only; |
|---|
| 2693 | n/a | else if (!type->tp_weaklistoffset && type->tp_dictoffset) |
|---|
| 2694 | n/a | type->tp_getset = subtype_getsets_dict_only; |
|---|
| 2695 | n/a | else |
|---|
| 2696 | n/a | type->tp_getset = NULL; |
|---|
| 2697 | n/a | |
|---|
| 2698 | n/a | /* Special case some slots */ |
|---|
| 2699 | n/a | if (type->tp_dictoffset != 0 || nslots > 0) { |
|---|
| 2700 | n/a | if (base->tp_getattr == NULL && base->tp_getattro == NULL) |
|---|
| 2701 | n/a | type->tp_getattro = PyObject_GenericGetAttr; |
|---|
| 2702 | n/a | if (base->tp_setattr == NULL && base->tp_setattro == NULL) |
|---|
| 2703 | n/a | type->tp_setattro = PyObject_GenericSetAttr; |
|---|
| 2704 | n/a | } |
|---|
| 2705 | n/a | type->tp_dealloc = subtype_dealloc; |
|---|
| 2706 | n/a | |
|---|
| 2707 | n/a | /* Enable GC unless this class is not adding new instance variables and |
|---|
| 2708 | n/a | the base class did not use GC. */ |
|---|
| 2709 | n/a | if ((base->tp_flags & Py_TPFLAGS_HAVE_GC) || |
|---|
| 2710 | n/a | type->tp_basicsize > base->tp_basicsize) |
|---|
| 2711 | n/a | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
|---|
| 2712 | n/a | |
|---|
| 2713 | n/a | /* Always override allocation strategy to use regular heap */ |
|---|
| 2714 | n/a | type->tp_alloc = PyType_GenericAlloc; |
|---|
| 2715 | n/a | if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { |
|---|
| 2716 | n/a | type->tp_free = PyObject_GC_Del; |
|---|
| 2717 | n/a | type->tp_traverse = subtype_traverse; |
|---|
| 2718 | n/a | type->tp_clear = subtype_clear; |
|---|
| 2719 | n/a | } |
|---|
| 2720 | n/a | else |
|---|
| 2721 | n/a | type->tp_free = PyObject_Del; |
|---|
| 2722 | n/a | |
|---|
| 2723 | n/a | /* store type in class' cell if one is supplied */ |
|---|
| 2724 | n/a | cell = _PyDict_GetItemId(dict, &PyId___classcell__); |
|---|
| 2725 | n/a | if (cell != NULL) { |
|---|
| 2726 | n/a | /* At least one method requires a reference to its defining class */ |
|---|
| 2727 | n/a | if (!PyCell_Check(cell)) { |
|---|
| 2728 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 2729 | n/a | "__classcell__ must be a nonlocal cell, not %.200R", |
|---|
| 2730 | n/a | Py_TYPE(cell)); |
|---|
| 2731 | n/a | goto error; |
|---|
| 2732 | n/a | } |
|---|
| 2733 | n/a | PyCell_Set(cell, (PyObject *) type); |
|---|
| 2734 | n/a | _PyDict_DelItemId(dict, &PyId___classcell__); |
|---|
| 2735 | n/a | PyErr_Clear(); |
|---|
| 2736 | n/a | } |
|---|
| 2737 | n/a | |
|---|
| 2738 | n/a | /* Initialize the rest */ |
|---|
| 2739 | n/a | if (PyType_Ready(type) < 0) |
|---|
| 2740 | n/a | goto error; |
|---|
| 2741 | n/a | |
|---|
| 2742 | n/a | /* Put the proper slots in place */ |
|---|
| 2743 | n/a | fixup_slot_dispatchers(type); |
|---|
| 2744 | n/a | |
|---|
| 2745 | n/a | if (type->tp_dictoffset) { |
|---|
| 2746 | n/a | et->ht_cached_keys = _PyDict_NewKeysForClass(); |
|---|
| 2747 | n/a | } |
|---|
| 2748 | n/a | |
|---|
| 2749 | n/a | if (set_names(type) < 0) |
|---|
| 2750 | n/a | goto error; |
|---|
| 2751 | n/a | |
|---|
| 2752 | n/a | if (init_subclass(type, kwds) < 0) |
|---|
| 2753 | n/a | goto error; |
|---|
| 2754 | n/a | |
|---|
| 2755 | n/a | Py_DECREF(dict); |
|---|
| 2756 | n/a | return (PyObject *)type; |
|---|
| 2757 | n/a | |
|---|
| 2758 | n/a | error: |
|---|
| 2759 | n/a | Py_XDECREF(dict); |
|---|
| 2760 | n/a | Py_XDECREF(bases); |
|---|
| 2761 | n/a | Py_XDECREF(slots); |
|---|
| 2762 | n/a | Py_XDECREF(type); |
|---|
| 2763 | n/a | return NULL; |
|---|
| 2764 | n/a | } |
|---|
| 2765 | n/a | |
|---|
| 2766 | n/a | static const short slotoffsets[] = { |
|---|
| 2767 | n/a | -1, /* invalid slot */ |
|---|
| 2768 | n/a | #include "typeslots.inc" |
|---|
| 2769 | n/a | }; |
|---|
| 2770 | n/a | |
|---|
| 2771 | n/a | PyObject * |
|---|
| 2772 | n/a | PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases) |
|---|
| 2773 | n/a | { |
|---|
| 2774 | n/a | PyHeapTypeObject *res = (PyHeapTypeObject*)PyType_GenericAlloc(&PyType_Type, 0); |
|---|
| 2775 | n/a | PyTypeObject *type, *base; |
|---|
| 2776 | n/a | PyObject *modname; |
|---|
| 2777 | n/a | char *s; |
|---|
| 2778 | n/a | char *res_start = (char*)res; |
|---|
| 2779 | n/a | PyType_Slot *slot; |
|---|
| 2780 | n/a | |
|---|
| 2781 | n/a | /* Set the type name and qualname */ |
|---|
| 2782 | n/a | s = strrchr(spec->name, '.'); |
|---|
| 2783 | n/a | if (s == NULL) |
|---|
| 2784 | n/a | s = (char*)spec->name; |
|---|
| 2785 | n/a | else |
|---|
| 2786 | n/a | s++; |
|---|
| 2787 | n/a | |
|---|
| 2788 | n/a | if (res == NULL) |
|---|
| 2789 | n/a | return NULL; |
|---|
| 2790 | n/a | type = &res->ht_type; |
|---|
| 2791 | n/a | /* The flags must be initialized early, before the GC traverses us */ |
|---|
| 2792 | n/a | type->tp_flags = spec->flags | Py_TPFLAGS_HEAPTYPE; |
|---|
| 2793 | n/a | res->ht_name = PyUnicode_FromString(s); |
|---|
| 2794 | n/a | if (!res->ht_name) |
|---|
| 2795 | n/a | goto fail; |
|---|
| 2796 | n/a | res->ht_qualname = res->ht_name; |
|---|
| 2797 | n/a | Py_INCREF(res->ht_qualname); |
|---|
| 2798 | n/a | type->tp_name = spec->name; |
|---|
| 2799 | n/a | if (!type->tp_name) |
|---|
| 2800 | n/a | goto fail; |
|---|
| 2801 | n/a | |
|---|
| 2802 | n/a | /* Adjust for empty tuple bases */ |
|---|
| 2803 | n/a | if (!bases) { |
|---|
| 2804 | n/a | base = &PyBaseObject_Type; |
|---|
| 2805 | n/a | /* See whether Py_tp_base(s) was specified */ |
|---|
| 2806 | n/a | for (slot = spec->slots; slot->slot; slot++) { |
|---|
| 2807 | n/a | if (slot->slot == Py_tp_base) |
|---|
| 2808 | n/a | base = slot->pfunc; |
|---|
| 2809 | n/a | else if (slot->slot == Py_tp_bases) { |
|---|
| 2810 | n/a | bases = slot->pfunc; |
|---|
| 2811 | n/a | Py_INCREF(bases); |
|---|
| 2812 | n/a | } |
|---|
| 2813 | n/a | } |
|---|
| 2814 | n/a | if (!bases) |
|---|
| 2815 | n/a | bases = PyTuple_Pack(1, base); |
|---|
| 2816 | n/a | if (!bases) |
|---|
| 2817 | n/a | goto fail; |
|---|
| 2818 | n/a | } |
|---|
| 2819 | n/a | else |
|---|
| 2820 | n/a | Py_INCREF(bases); |
|---|
| 2821 | n/a | |
|---|
| 2822 | n/a | /* Calculate best base, and check that all bases are type objects */ |
|---|
| 2823 | n/a | base = best_base(bases); |
|---|
| 2824 | n/a | if (base == NULL) { |
|---|
| 2825 | n/a | goto fail; |
|---|
| 2826 | n/a | } |
|---|
| 2827 | n/a | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
|---|
| 2828 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 2829 | n/a | "type '%.100s' is not an acceptable base type", |
|---|
| 2830 | n/a | base->tp_name); |
|---|
| 2831 | n/a | goto fail; |
|---|
| 2832 | n/a | } |
|---|
| 2833 | n/a | |
|---|
| 2834 | n/a | /* Initialize essential fields */ |
|---|
| 2835 | n/a | type->tp_as_async = &res->as_async; |
|---|
| 2836 | n/a | type->tp_as_number = &res->as_number; |
|---|
| 2837 | n/a | type->tp_as_sequence = &res->as_sequence; |
|---|
| 2838 | n/a | type->tp_as_mapping = &res->as_mapping; |
|---|
| 2839 | n/a | type->tp_as_buffer = &res->as_buffer; |
|---|
| 2840 | n/a | /* Set tp_base and tp_bases */ |
|---|
| 2841 | n/a | type->tp_bases = bases; |
|---|
| 2842 | n/a | bases = NULL; |
|---|
| 2843 | n/a | Py_INCREF(base); |
|---|
| 2844 | n/a | type->tp_base = base; |
|---|
| 2845 | n/a | |
|---|
| 2846 | n/a | type->tp_basicsize = spec->basicsize; |
|---|
| 2847 | n/a | type->tp_itemsize = spec->itemsize; |
|---|
| 2848 | n/a | |
|---|
| 2849 | n/a | for (slot = spec->slots; slot->slot; slot++) { |
|---|
| 2850 | n/a | if (slot->slot < 0 |
|---|
| 2851 | n/a | || (size_t)slot->slot >= Py_ARRAY_LENGTH(slotoffsets)) { |
|---|
| 2852 | n/a | PyErr_SetString(PyExc_RuntimeError, "invalid slot offset"); |
|---|
| 2853 | n/a | goto fail; |
|---|
| 2854 | n/a | } |
|---|
| 2855 | n/a | if (slot->slot == Py_tp_base || slot->slot == Py_tp_bases) |
|---|
| 2856 | n/a | /* Processed above */ |
|---|
| 2857 | n/a | continue; |
|---|
| 2858 | n/a | *(void**)(res_start + slotoffsets[slot->slot]) = slot->pfunc; |
|---|
| 2859 | n/a | |
|---|
| 2860 | n/a | /* need to make a copy of the docstring slot, which usually |
|---|
| 2861 | n/a | points to a static string literal */ |
|---|
| 2862 | n/a | if (slot->slot == Py_tp_doc) { |
|---|
| 2863 | n/a | const char *old_doc = _PyType_DocWithoutSignature(type->tp_name, slot->pfunc); |
|---|
| 2864 | n/a | size_t len = strlen(old_doc)+1; |
|---|
| 2865 | n/a | char *tp_doc = PyObject_MALLOC(len); |
|---|
| 2866 | n/a | if (tp_doc == NULL) { |
|---|
| 2867 | n/a | PyErr_NoMemory(); |
|---|
| 2868 | n/a | goto fail; |
|---|
| 2869 | n/a | } |
|---|
| 2870 | n/a | memcpy(tp_doc, old_doc, len); |
|---|
| 2871 | n/a | type->tp_doc = tp_doc; |
|---|
| 2872 | n/a | } |
|---|
| 2873 | n/a | } |
|---|
| 2874 | n/a | if (type->tp_dealloc == NULL) { |
|---|
| 2875 | n/a | /* It's a heap type, so needs the heap types' dealloc. |
|---|
| 2876 | n/a | subtype_dealloc will call the base type's tp_dealloc, if |
|---|
| 2877 | n/a | necessary. */ |
|---|
| 2878 | n/a | type->tp_dealloc = subtype_dealloc; |
|---|
| 2879 | n/a | } |
|---|
| 2880 | n/a | |
|---|
| 2881 | n/a | if (PyType_Ready(type) < 0) |
|---|
| 2882 | n/a | goto fail; |
|---|
| 2883 | n/a | |
|---|
| 2884 | n/a | if (type->tp_dictoffset) { |
|---|
| 2885 | n/a | res->ht_cached_keys = _PyDict_NewKeysForClass(); |
|---|
| 2886 | n/a | } |
|---|
| 2887 | n/a | |
|---|
| 2888 | n/a | /* Set type.__module__ */ |
|---|
| 2889 | n/a | s = strrchr(spec->name, '.'); |
|---|
| 2890 | n/a | if (s != NULL) { |
|---|
| 2891 | n/a | int err; |
|---|
| 2892 | n/a | modname = PyUnicode_FromStringAndSize( |
|---|
| 2893 | n/a | spec->name, (Py_ssize_t)(s - spec->name)); |
|---|
| 2894 | n/a | if (modname == NULL) { |
|---|
| 2895 | n/a | goto fail; |
|---|
| 2896 | n/a | } |
|---|
| 2897 | n/a | err = _PyDict_SetItemId(type->tp_dict, &PyId___module__, modname); |
|---|
| 2898 | n/a | Py_DECREF(modname); |
|---|
| 2899 | n/a | if (err != 0) |
|---|
| 2900 | n/a | goto fail; |
|---|
| 2901 | n/a | } else { |
|---|
| 2902 | n/a | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
|---|
| 2903 | n/a | "builtin type %.200s has no __module__ attribute", |
|---|
| 2904 | n/a | spec->name)) |
|---|
| 2905 | n/a | goto fail; |
|---|
| 2906 | n/a | } |
|---|
| 2907 | n/a | |
|---|
| 2908 | n/a | return (PyObject*)res; |
|---|
| 2909 | n/a | |
|---|
| 2910 | n/a | fail: |
|---|
| 2911 | n/a | Py_DECREF(res); |
|---|
| 2912 | n/a | return NULL; |
|---|
| 2913 | n/a | } |
|---|
| 2914 | n/a | |
|---|
| 2915 | n/a | PyObject * |
|---|
| 2916 | n/a | PyType_FromSpec(PyType_Spec *spec) |
|---|
| 2917 | n/a | { |
|---|
| 2918 | n/a | return PyType_FromSpecWithBases(spec, NULL); |
|---|
| 2919 | n/a | } |
|---|
| 2920 | n/a | |
|---|
| 2921 | n/a | void * |
|---|
| 2922 | n/a | PyType_GetSlot(PyTypeObject *type, int slot) |
|---|
| 2923 | n/a | { |
|---|
| 2924 | n/a | if (!PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE) || slot < 0) { |
|---|
| 2925 | n/a | PyErr_BadInternalCall(); |
|---|
| 2926 | n/a | return NULL; |
|---|
| 2927 | n/a | } |
|---|
| 2928 | n/a | if ((size_t)slot >= Py_ARRAY_LENGTH(slotoffsets)) { |
|---|
| 2929 | n/a | /* Extension module requesting slot from a future version */ |
|---|
| 2930 | n/a | return NULL; |
|---|
| 2931 | n/a | } |
|---|
| 2932 | n/a | return *(void**)(((char*)type) + slotoffsets[slot]); |
|---|
| 2933 | n/a | } |
|---|
| 2934 | n/a | |
|---|
| 2935 | n/a | /* Internal API to look for a name through the MRO. |
|---|
| 2936 | n/a | This returns a borrowed reference, and doesn't set an exception! */ |
|---|
| 2937 | n/a | PyObject * |
|---|
| 2938 | n/a | _PyType_Lookup(PyTypeObject *type, PyObject *name) |
|---|
| 2939 | n/a | { |
|---|
| 2940 | n/a | Py_ssize_t i, n; |
|---|
| 2941 | n/a | PyObject *mro, *res, *base, *dict; |
|---|
| 2942 | n/a | unsigned int h; |
|---|
| 2943 | n/a | |
|---|
| 2944 | n/a | if (MCACHE_CACHEABLE_NAME(name) && |
|---|
| 2945 | n/a | PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) { |
|---|
| 2946 | n/a | /* fast path */ |
|---|
| 2947 | n/a | h = MCACHE_HASH_METHOD(type, name); |
|---|
| 2948 | n/a | if (method_cache[h].version == type->tp_version_tag && |
|---|
| 2949 | n/a | method_cache[h].name == name) { |
|---|
| 2950 | n/a | #if MCACHE_STATS |
|---|
| 2951 | n/a | method_cache_hits++; |
|---|
| 2952 | n/a | #endif |
|---|
| 2953 | n/a | return method_cache[h].value; |
|---|
| 2954 | n/a | } |
|---|
| 2955 | n/a | } |
|---|
| 2956 | n/a | |
|---|
| 2957 | n/a | /* Look in tp_dict of types in MRO */ |
|---|
| 2958 | n/a | mro = type->tp_mro; |
|---|
| 2959 | n/a | |
|---|
| 2960 | n/a | if (mro == NULL) { |
|---|
| 2961 | n/a | if ((type->tp_flags & Py_TPFLAGS_READYING) == 0 && |
|---|
| 2962 | n/a | PyType_Ready(type) < 0) { |
|---|
| 2963 | n/a | /* It's not ideal to clear the error condition, |
|---|
| 2964 | n/a | but this function is documented as not setting |
|---|
| 2965 | n/a | an exception, and I don't want to change that. |
|---|
| 2966 | n/a | When PyType_Ready() can't proceed, it won't |
|---|
| 2967 | n/a | set the "ready" flag, so future attempts to ready |
|---|
| 2968 | n/a | the same type will call it again -- hopefully |
|---|
| 2969 | n/a | in a context that propagates the exception out. |
|---|
| 2970 | n/a | */ |
|---|
| 2971 | n/a | PyErr_Clear(); |
|---|
| 2972 | n/a | return NULL; |
|---|
| 2973 | n/a | } |
|---|
| 2974 | n/a | mro = type->tp_mro; |
|---|
| 2975 | n/a | if (mro == NULL) { |
|---|
| 2976 | n/a | return NULL; |
|---|
| 2977 | n/a | } |
|---|
| 2978 | n/a | } |
|---|
| 2979 | n/a | |
|---|
| 2980 | n/a | res = NULL; |
|---|
| 2981 | n/a | /* keep a strong reference to mro because type->tp_mro can be replaced |
|---|
| 2982 | n/a | during PyDict_GetItem(dict, name) */ |
|---|
| 2983 | n/a | Py_INCREF(mro); |
|---|
| 2984 | n/a | assert(PyTuple_Check(mro)); |
|---|
| 2985 | n/a | n = PyTuple_GET_SIZE(mro); |
|---|
| 2986 | n/a | for (i = 0; i < n; i++) { |
|---|
| 2987 | n/a | base = PyTuple_GET_ITEM(mro, i); |
|---|
| 2988 | n/a | assert(PyType_Check(base)); |
|---|
| 2989 | n/a | dict = ((PyTypeObject *)base)->tp_dict; |
|---|
| 2990 | n/a | assert(dict && PyDict_Check(dict)); |
|---|
| 2991 | n/a | res = PyDict_GetItem(dict, name); |
|---|
| 2992 | n/a | if (res != NULL) |
|---|
| 2993 | n/a | break; |
|---|
| 2994 | n/a | } |
|---|
| 2995 | n/a | Py_DECREF(mro); |
|---|
| 2996 | n/a | |
|---|
| 2997 | n/a | if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) { |
|---|
| 2998 | n/a | h = MCACHE_HASH_METHOD(type, name); |
|---|
| 2999 | n/a | method_cache[h].version = type->tp_version_tag; |
|---|
| 3000 | n/a | method_cache[h].value = res; /* borrowed */ |
|---|
| 3001 | n/a | Py_INCREF(name); |
|---|
| 3002 | n/a | assert(((PyASCIIObject *)(name))->hash != -1); |
|---|
| 3003 | n/a | #if MCACHE_STATS |
|---|
| 3004 | n/a | if (method_cache[h].name != Py_None && method_cache[h].name != name) |
|---|
| 3005 | n/a | method_cache_collisions++; |
|---|
| 3006 | n/a | else |
|---|
| 3007 | n/a | method_cache_misses++; |
|---|
| 3008 | n/a | #endif |
|---|
| 3009 | n/a | Py_SETREF(method_cache[h].name, name); |
|---|
| 3010 | n/a | } |
|---|
| 3011 | n/a | return res; |
|---|
| 3012 | n/a | } |
|---|
| 3013 | n/a | |
|---|
| 3014 | n/a | PyObject * |
|---|
| 3015 | n/a | _PyType_LookupId(PyTypeObject *type, struct _Py_Identifier *name) |
|---|
| 3016 | n/a | { |
|---|
| 3017 | n/a | PyObject *oname; |
|---|
| 3018 | n/a | oname = _PyUnicode_FromId(name); /* borrowed */ |
|---|
| 3019 | n/a | if (oname == NULL) |
|---|
| 3020 | n/a | return NULL; |
|---|
| 3021 | n/a | return _PyType_Lookup(type, oname); |
|---|
| 3022 | n/a | } |
|---|
| 3023 | n/a | |
|---|
| 3024 | n/a | /* This is similar to PyObject_GenericGetAttr(), |
|---|
| 3025 | n/a | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ |
|---|
| 3026 | n/a | static PyObject * |
|---|
| 3027 | n/a | type_getattro(PyTypeObject *type, PyObject *name) |
|---|
| 3028 | n/a | { |
|---|
| 3029 | n/a | PyTypeObject *metatype = Py_TYPE(type); |
|---|
| 3030 | n/a | PyObject *meta_attribute, *attribute; |
|---|
| 3031 | n/a | descrgetfunc meta_get; |
|---|
| 3032 | n/a | |
|---|
| 3033 | n/a | if (!PyUnicode_Check(name)) { |
|---|
| 3034 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 3035 | n/a | "attribute name must be string, not '%.200s'", |
|---|
| 3036 | n/a | name->ob_type->tp_name); |
|---|
| 3037 | n/a | return NULL; |
|---|
| 3038 | n/a | } |
|---|
| 3039 | n/a | |
|---|
| 3040 | n/a | /* Initialize this type (we'll assume the metatype is initialized) */ |
|---|
| 3041 | n/a | if (type->tp_dict == NULL) { |
|---|
| 3042 | n/a | if (PyType_Ready(type) < 0) |
|---|
| 3043 | n/a | return NULL; |
|---|
| 3044 | n/a | } |
|---|
| 3045 | n/a | |
|---|
| 3046 | n/a | /* No readable descriptor found yet */ |
|---|
| 3047 | n/a | meta_get = NULL; |
|---|
| 3048 | n/a | |
|---|
| 3049 | n/a | /* Look for the attribute in the metatype */ |
|---|
| 3050 | n/a | meta_attribute = _PyType_Lookup(metatype, name); |
|---|
| 3051 | n/a | |
|---|
| 3052 | n/a | if (meta_attribute != NULL) { |
|---|
| 3053 | n/a | meta_get = Py_TYPE(meta_attribute)->tp_descr_get; |
|---|
| 3054 | n/a | |
|---|
| 3055 | n/a | if (meta_get != NULL && PyDescr_IsData(meta_attribute)) { |
|---|
| 3056 | n/a | /* Data descriptors implement tp_descr_set to intercept |
|---|
| 3057 | n/a | * writes. Assume the attribute is not overridden in |
|---|
| 3058 | n/a | * type's tp_dict (and bases): call the descriptor now. |
|---|
| 3059 | n/a | */ |
|---|
| 3060 | n/a | return meta_get(meta_attribute, (PyObject *)type, |
|---|
| 3061 | n/a | (PyObject *)metatype); |
|---|
| 3062 | n/a | } |
|---|
| 3063 | n/a | Py_INCREF(meta_attribute); |
|---|
| 3064 | n/a | } |
|---|
| 3065 | n/a | |
|---|
| 3066 | n/a | /* No data descriptor found on metatype. Look in tp_dict of this |
|---|
| 3067 | n/a | * type and its bases */ |
|---|
| 3068 | n/a | attribute = _PyType_Lookup(type, name); |
|---|
| 3069 | n/a | if (attribute != NULL) { |
|---|
| 3070 | n/a | /* Implement descriptor functionality, if any */ |
|---|
| 3071 | n/a | descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get; |
|---|
| 3072 | n/a | |
|---|
| 3073 | n/a | Py_XDECREF(meta_attribute); |
|---|
| 3074 | n/a | |
|---|
| 3075 | n/a | if (local_get != NULL) { |
|---|
| 3076 | n/a | /* NULL 2nd argument indicates the descriptor was |
|---|
| 3077 | n/a | * found on the target object itself (or a base) */ |
|---|
| 3078 | n/a | return local_get(attribute, (PyObject *)NULL, |
|---|
| 3079 | n/a | (PyObject *)type); |
|---|
| 3080 | n/a | } |
|---|
| 3081 | n/a | |
|---|
| 3082 | n/a | Py_INCREF(attribute); |
|---|
| 3083 | n/a | return attribute; |
|---|
| 3084 | n/a | } |
|---|
| 3085 | n/a | |
|---|
| 3086 | n/a | /* No attribute found in local __dict__ (or bases): use the |
|---|
| 3087 | n/a | * descriptor from the metatype, if any */ |
|---|
| 3088 | n/a | if (meta_get != NULL) { |
|---|
| 3089 | n/a | PyObject *res; |
|---|
| 3090 | n/a | res = meta_get(meta_attribute, (PyObject *)type, |
|---|
| 3091 | n/a | (PyObject *)metatype); |
|---|
| 3092 | n/a | Py_DECREF(meta_attribute); |
|---|
| 3093 | n/a | return res; |
|---|
| 3094 | n/a | } |
|---|
| 3095 | n/a | |
|---|
| 3096 | n/a | /* If an ordinary attribute was found on the metatype, return it now */ |
|---|
| 3097 | n/a | if (meta_attribute != NULL) { |
|---|
| 3098 | n/a | return meta_attribute; |
|---|
| 3099 | n/a | } |
|---|
| 3100 | n/a | |
|---|
| 3101 | n/a | /* Give up */ |
|---|
| 3102 | n/a | PyErr_Format(PyExc_AttributeError, |
|---|
| 3103 | n/a | "type object '%.50s' has no attribute '%U'", |
|---|
| 3104 | n/a | type->tp_name, name); |
|---|
| 3105 | n/a | return NULL; |
|---|
| 3106 | n/a | } |
|---|
| 3107 | n/a | |
|---|
| 3108 | n/a | static int |
|---|
| 3109 | n/a | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) |
|---|
| 3110 | n/a | { |
|---|
| 3111 | n/a | int res; |
|---|
| 3112 | n/a | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
|---|
| 3113 | n/a | PyErr_Format( |
|---|
| 3114 | n/a | PyExc_TypeError, |
|---|
| 3115 | n/a | "can't set attributes of built-in/extension type '%s'", |
|---|
| 3116 | n/a | type->tp_name); |
|---|
| 3117 | n/a | return -1; |
|---|
| 3118 | n/a | } |
|---|
| 3119 | n/a | if (_PyObject_GenericSetAttrWithDict((PyObject *)type, name, value, NULL) < 0) |
|---|
| 3120 | n/a | return -1; |
|---|
| 3121 | n/a | res = update_slot(type, name); |
|---|
| 3122 | n/a | assert(_PyType_CheckConsistency(type)); |
|---|
| 3123 | n/a | return res; |
|---|
| 3124 | n/a | } |
|---|
| 3125 | n/a | |
|---|
| 3126 | n/a | extern void |
|---|
| 3127 | n/a | _PyDictKeys_DecRef(PyDictKeysObject *keys); |
|---|
| 3128 | n/a | |
|---|
| 3129 | n/a | static void |
|---|
| 3130 | n/a | type_dealloc(PyTypeObject *type) |
|---|
| 3131 | n/a | { |
|---|
| 3132 | n/a | PyHeapTypeObject *et; |
|---|
| 3133 | n/a | PyObject *tp, *val, *tb; |
|---|
| 3134 | n/a | |
|---|
| 3135 | n/a | /* Assert this is a heap-allocated type object */ |
|---|
| 3136 | n/a | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
|---|
| 3137 | n/a | _PyObject_GC_UNTRACK(type); |
|---|
| 3138 | n/a | PyErr_Fetch(&tp, &val, &tb); |
|---|
| 3139 | n/a | remove_all_subclasses(type, type->tp_bases); |
|---|
| 3140 | n/a | PyErr_Restore(tp, val, tb); |
|---|
| 3141 | n/a | PyObject_ClearWeakRefs((PyObject *)type); |
|---|
| 3142 | n/a | et = (PyHeapTypeObject *)type; |
|---|
| 3143 | n/a | Py_XDECREF(type->tp_base); |
|---|
| 3144 | n/a | Py_XDECREF(type->tp_dict); |
|---|
| 3145 | n/a | Py_XDECREF(type->tp_bases); |
|---|
| 3146 | n/a | Py_XDECREF(type->tp_mro); |
|---|
| 3147 | n/a | Py_XDECREF(type->tp_cache); |
|---|
| 3148 | n/a | Py_XDECREF(type->tp_subclasses); |
|---|
| 3149 | n/a | /* A type's tp_doc is heap allocated, unlike the tp_doc slots |
|---|
| 3150 | n/a | * of most other objects. It's okay to cast it to char *. |
|---|
| 3151 | n/a | */ |
|---|
| 3152 | n/a | PyObject_Free((char *)type->tp_doc); |
|---|
| 3153 | n/a | Py_XDECREF(et->ht_name); |
|---|
| 3154 | n/a | Py_XDECREF(et->ht_qualname); |
|---|
| 3155 | n/a | Py_XDECREF(et->ht_slots); |
|---|
| 3156 | n/a | if (et->ht_cached_keys) |
|---|
| 3157 | n/a | _PyDictKeys_DecRef(et->ht_cached_keys); |
|---|
| 3158 | n/a | Py_TYPE(type)->tp_free((PyObject *)type); |
|---|
| 3159 | n/a | } |
|---|
| 3160 | n/a | |
|---|
| 3161 | n/a | static PyObject * |
|---|
| 3162 | n/a | type_subclasses(PyTypeObject *type, PyObject *args_ignored) |
|---|
| 3163 | n/a | { |
|---|
| 3164 | n/a | PyObject *list, *raw, *ref; |
|---|
| 3165 | n/a | Py_ssize_t i; |
|---|
| 3166 | n/a | |
|---|
| 3167 | n/a | list = PyList_New(0); |
|---|
| 3168 | n/a | if (list == NULL) |
|---|
| 3169 | n/a | return NULL; |
|---|
| 3170 | n/a | raw = type->tp_subclasses; |
|---|
| 3171 | n/a | if (raw == NULL) |
|---|
| 3172 | n/a | return list; |
|---|
| 3173 | n/a | assert(PyDict_CheckExact(raw)); |
|---|
| 3174 | n/a | i = 0; |
|---|
| 3175 | n/a | while (PyDict_Next(raw, &i, NULL, &ref)) { |
|---|
| 3176 | n/a | assert(PyWeakref_CheckRef(ref)); |
|---|
| 3177 | n/a | ref = PyWeakref_GET_OBJECT(ref); |
|---|
| 3178 | n/a | if (ref != Py_None) { |
|---|
| 3179 | n/a | if (PyList_Append(list, ref) < 0) { |
|---|
| 3180 | n/a | Py_DECREF(list); |
|---|
| 3181 | n/a | return NULL; |
|---|
| 3182 | n/a | } |
|---|
| 3183 | n/a | } |
|---|
| 3184 | n/a | } |
|---|
| 3185 | n/a | return list; |
|---|
| 3186 | n/a | } |
|---|
| 3187 | n/a | |
|---|
| 3188 | n/a | static PyObject * |
|---|
| 3189 | n/a | type_prepare(PyObject *self, PyObject **args, Py_ssize_t nargs, |
|---|
| 3190 | n/a | PyObject *kwnames) |
|---|
| 3191 | n/a | { |
|---|
| 3192 | n/a | return PyDict_New(); |
|---|
| 3193 | n/a | } |
|---|
| 3194 | n/a | |
|---|
| 3195 | n/a | /* |
|---|
| 3196 | n/a | Merge the __dict__ of aclass into dict, and recursively also all |
|---|
| 3197 | n/a | the __dict__s of aclass's base classes. The order of merging isn't |
|---|
| 3198 | n/a | defined, as it's expected that only the final set of dict keys is |
|---|
| 3199 | n/a | interesting. |
|---|
| 3200 | n/a | Return 0 on success, -1 on error. |
|---|
| 3201 | n/a | */ |
|---|
| 3202 | n/a | |
|---|
| 3203 | n/a | static int |
|---|
| 3204 | n/a | merge_class_dict(PyObject *dict, PyObject *aclass) |
|---|
| 3205 | n/a | { |
|---|
| 3206 | n/a | PyObject *classdict; |
|---|
| 3207 | n/a | PyObject *bases; |
|---|
| 3208 | n/a | _Py_IDENTIFIER(__bases__); |
|---|
| 3209 | n/a | |
|---|
| 3210 | n/a | assert(PyDict_Check(dict)); |
|---|
| 3211 | n/a | assert(aclass); |
|---|
| 3212 | n/a | |
|---|
| 3213 | n/a | /* Merge in the type's dict (if any). */ |
|---|
| 3214 | n/a | classdict = _PyObject_GetAttrId(aclass, &PyId___dict__); |
|---|
| 3215 | n/a | if (classdict == NULL) |
|---|
| 3216 | n/a | PyErr_Clear(); |
|---|
| 3217 | n/a | else { |
|---|
| 3218 | n/a | int status = PyDict_Update(dict, classdict); |
|---|
| 3219 | n/a | Py_DECREF(classdict); |
|---|
| 3220 | n/a | if (status < 0) |
|---|
| 3221 | n/a | return -1; |
|---|
| 3222 | n/a | } |
|---|
| 3223 | n/a | |
|---|
| 3224 | n/a | /* Recursively merge in the base types' (if any) dicts. */ |
|---|
| 3225 | n/a | bases = _PyObject_GetAttrId(aclass, &PyId___bases__); |
|---|
| 3226 | n/a | if (bases == NULL) |
|---|
| 3227 | n/a | PyErr_Clear(); |
|---|
| 3228 | n/a | else { |
|---|
| 3229 | n/a | /* We have no guarantee that bases is a real tuple */ |
|---|
| 3230 | n/a | Py_ssize_t i, n; |
|---|
| 3231 | n/a | n = PySequence_Size(bases); /* This better be right */ |
|---|
| 3232 | n/a | if (n < 0) |
|---|
| 3233 | n/a | PyErr_Clear(); |
|---|
| 3234 | n/a | else { |
|---|
| 3235 | n/a | for (i = 0; i < n; i++) { |
|---|
| 3236 | n/a | int status; |
|---|
| 3237 | n/a | PyObject *base = PySequence_GetItem(bases, i); |
|---|
| 3238 | n/a | if (base == NULL) { |
|---|
| 3239 | n/a | Py_DECREF(bases); |
|---|
| 3240 | n/a | return -1; |
|---|
| 3241 | n/a | } |
|---|
| 3242 | n/a | status = merge_class_dict(dict, base); |
|---|
| 3243 | n/a | Py_DECREF(base); |
|---|
| 3244 | n/a | if (status < 0) { |
|---|
| 3245 | n/a | Py_DECREF(bases); |
|---|
| 3246 | n/a | return -1; |
|---|
| 3247 | n/a | } |
|---|
| 3248 | n/a | } |
|---|
| 3249 | n/a | } |
|---|
| 3250 | n/a | Py_DECREF(bases); |
|---|
| 3251 | n/a | } |
|---|
| 3252 | n/a | return 0; |
|---|
| 3253 | n/a | } |
|---|
| 3254 | n/a | |
|---|
| 3255 | n/a | /* __dir__ for type objects: returns __dict__ and __bases__. |
|---|
| 3256 | n/a | We deliberately don't suck up its __class__, as methods belonging to the |
|---|
| 3257 | n/a | metaclass would probably be more confusing than helpful. |
|---|
| 3258 | n/a | */ |
|---|
| 3259 | n/a | static PyObject * |
|---|
| 3260 | n/a | type_dir(PyObject *self, PyObject *args) |
|---|
| 3261 | n/a | { |
|---|
| 3262 | n/a | PyObject *result = NULL; |
|---|
| 3263 | n/a | PyObject *dict = PyDict_New(); |
|---|
| 3264 | n/a | |
|---|
| 3265 | n/a | if (dict != NULL && merge_class_dict(dict, self) == 0) |
|---|
| 3266 | n/a | result = PyDict_Keys(dict); |
|---|
| 3267 | n/a | |
|---|
| 3268 | n/a | Py_XDECREF(dict); |
|---|
| 3269 | n/a | return result; |
|---|
| 3270 | n/a | } |
|---|
| 3271 | n/a | |
|---|
| 3272 | n/a | static PyObject* |
|---|
| 3273 | n/a | type_sizeof(PyObject *self, PyObject *args_unused) |
|---|
| 3274 | n/a | { |
|---|
| 3275 | n/a | Py_ssize_t size; |
|---|
| 3276 | n/a | PyTypeObject *type = (PyTypeObject*)self; |
|---|
| 3277 | n/a | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
|---|
| 3278 | n/a | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
|---|
| 3279 | n/a | size = sizeof(PyHeapTypeObject); |
|---|
| 3280 | n/a | if (et->ht_cached_keys) |
|---|
| 3281 | n/a | size += _PyDict_KeysSize(et->ht_cached_keys); |
|---|
| 3282 | n/a | } |
|---|
| 3283 | n/a | else |
|---|
| 3284 | n/a | size = sizeof(PyTypeObject); |
|---|
| 3285 | n/a | return PyLong_FromSsize_t(size); |
|---|
| 3286 | n/a | } |
|---|
| 3287 | n/a | |
|---|
| 3288 | n/a | static PyMethodDef type_methods[] = { |
|---|
| 3289 | n/a | {"mro", (PyCFunction)mro_external, METH_NOARGS, |
|---|
| 3290 | n/a | PyDoc_STR("mro() -> list\nreturn a type's method resolution order")}, |
|---|
| 3291 | n/a | {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS, |
|---|
| 3292 | n/a | PyDoc_STR("__subclasses__() -> list of immediate subclasses")}, |
|---|
| 3293 | n/a | {"__prepare__", (PyCFunction)type_prepare, |
|---|
| 3294 | n/a | METH_FASTCALL | METH_CLASS, |
|---|
| 3295 | n/a | PyDoc_STR("__prepare__() -> dict\n" |
|---|
| 3296 | n/a | "used to create the namespace for the class statement")}, |
|---|
| 3297 | n/a | {"__instancecheck__", type___instancecheck__, METH_O, |
|---|
| 3298 | n/a | PyDoc_STR("__instancecheck__() -> bool\ncheck if an object is an instance")}, |
|---|
| 3299 | n/a | {"__subclasscheck__", type___subclasscheck__, METH_O, |
|---|
| 3300 | n/a | PyDoc_STR("__subclasscheck__() -> bool\ncheck if a class is a subclass")}, |
|---|
| 3301 | n/a | {"__dir__", type_dir, METH_NOARGS, |
|---|
| 3302 | n/a | PyDoc_STR("__dir__() -> list\nspecialized __dir__ implementation for types")}, |
|---|
| 3303 | n/a | {"__sizeof__", type_sizeof, METH_NOARGS, |
|---|
| 3304 | n/a | "__sizeof__() -> int\nreturn memory consumption of the type object"}, |
|---|
| 3305 | n/a | {0} |
|---|
| 3306 | n/a | }; |
|---|
| 3307 | n/a | |
|---|
| 3308 | n/a | PyDoc_STRVAR(type_doc, |
|---|
| 3309 | n/a | /* this text signature cannot be accurate yet. will fix. --larry */ |
|---|
| 3310 | n/a | "type(object_or_name, bases, dict)\n" |
|---|
| 3311 | n/a | "type(object) -> the object's type\n" |
|---|
| 3312 | n/a | "type(name, bases, dict) -> a new type"); |
|---|
| 3313 | n/a | |
|---|
| 3314 | n/a | static int |
|---|
| 3315 | n/a | type_traverse(PyTypeObject *type, visitproc visit, void *arg) |
|---|
| 3316 | n/a | { |
|---|
| 3317 | n/a | /* Because of type_is_gc(), the collector only calls this |
|---|
| 3318 | n/a | for heaptypes. */ |
|---|
| 3319 | n/a | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
|---|
| 3320 | n/a | char msg[200]; |
|---|
| 3321 | n/a | sprintf(msg, "type_traverse() called for non-heap type '%.100s'", |
|---|
| 3322 | n/a | type->tp_name); |
|---|
| 3323 | n/a | Py_FatalError(msg); |
|---|
| 3324 | n/a | } |
|---|
| 3325 | n/a | |
|---|
| 3326 | n/a | Py_VISIT(type->tp_dict); |
|---|
| 3327 | n/a | Py_VISIT(type->tp_cache); |
|---|
| 3328 | n/a | Py_VISIT(type->tp_mro); |
|---|
| 3329 | n/a | Py_VISIT(type->tp_bases); |
|---|
| 3330 | n/a | Py_VISIT(type->tp_base); |
|---|
| 3331 | n/a | |
|---|
| 3332 | n/a | /* There's no need to visit type->tp_subclasses or |
|---|
| 3333 | n/a | ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved |
|---|
| 3334 | n/a | in cycles; tp_subclasses is a list of weak references, |
|---|
| 3335 | n/a | and slots is a tuple of strings. */ |
|---|
| 3336 | n/a | |
|---|
| 3337 | n/a | return 0; |
|---|
| 3338 | n/a | } |
|---|
| 3339 | n/a | |
|---|
| 3340 | n/a | static int |
|---|
| 3341 | n/a | type_clear(PyTypeObject *type) |
|---|
| 3342 | n/a | { |
|---|
| 3343 | n/a | PyDictKeysObject *cached_keys; |
|---|
| 3344 | n/a | /* Because of type_is_gc(), the collector only calls this |
|---|
| 3345 | n/a | for heaptypes. */ |
|---|
| 3346 | n/a | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
|---|
| 3347 | n/a | |
|---|
| 3348 | n/a | /* We need to invalidate the method cache carefully before clearing |
|---|
| 3349 | n/a | the dict, so that other objects caught in a reference cycle |
|---|
| 3350 | n/a | don't start calling destroyed methods. |
|---|
| 3351 | n/a | |
|---|
| 3352 | n/a | Otherwise, the only field we need to clear is tp_mro, which is |
|---|
| 3353 | n/a | part of a hard cycle (its first element is the class itself) that |
|---|
| 3354 | n/a | won't be broken otherwise (it's a tuple and tuples don't have a |
|---|
| 3355 | n/a | tp_clear handler). None of the other fields need to be |
|---|
| 3356 | n/a | cleared, and here's why: |
|---|
| 3357 | n/a | |
|---|
| 3358 | n/a | tp_cache: |
|---|
| 3359 | n/a | Not used; if it were, it would be a dict. |
|---|
| 3360 | n/a | |
|---|
| 3361 | n/a | tp_bases, tp_base: |
|---|
| 3362 | n/a | If these are involved in a cycle, there must be at least |
|---|
| 3363 | n/a | one other, mutable object in the cycle, e.g. a base |
|---|
| 3364 | n/a | class's dict; the cycle will be broken that way. |
|---|
| 3365 | n/a | |
|---|
| 3366 | n/a | tp_subclasses: |
|---|
| 3367 | n/a | A dict of weak references can't be part of a cycle; and |
|---|
| 3368 | n/a | dicts have their own tp_clear. |
|---|
| 3369 | n/a | |
|---|
| 3370 | n/a | slots (in PyHeapTypeObject): |
|---|
| 3371 | n/a | A tuple of strings can't be part of a cycle. |
|---|
| 3372 | n/a | */ |
|---|
| 3373 | n/a | |
|---|
| 3374 | n/a | PyType_Modified(type); |
|---|
| 3375 | n/a | cached_keys = ((PyHeapTypeObject *)type)->ht_cached_keys; |
|---|
| 3376 | n/a | if (cached_keys != NULL) { |
|---|
| 3377 | n/a | ((PyHeapTypeObject *)type)->ht_cached_keys = NULL; |
|---|
| 3378 | n/a | _PyDictKeys_DecRef(cached_keys); |
|---|
| 3379 | n/a | } |
|---|
| 3380 | n/a | if (type->tp_dict) |
|---|
| 3381 | n/a | PyDict_Clear(type->tp_dict); |
|---|
| 3382 | n/a | Py_CLEAR(type->tp_mro); |
|---|
| 3383 | n/a | |
|---|
| 3384 | n/a | return 0; |
|---|
| 3385 | n/a | } |
|---|
| 3386 | n/a | |
|---|
| 3387 | n/a | static int |
|---|
| 3388 | n/a | type_is_gc(PyTypeObject *type) |
|---|
| 3389 | n/a | { |
|---|
| 3390 | n/a | return type->tp_flags & Py_TPFLAGS_HEAPTYPE; |
|---|
| 3391 | n/a | } |
|---|
| 3392 | n/a | |
|---|
| 3393 | n/a | PyTypeObject PyType_Type = { |
|---|
| 3394 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|---|
| 3395 | n/a | "type", /* tp_name */ |
|---|
| 3396 | n/a | sizeof(PyHeapTypeObject), /* tp_basicsize */ |
|---|
| 3397 | n/a | sizeof(PyMemberDef), /* tp_itemsize */ |
|---|
| 3398 | n/a | (destructor)type_dealloc, /* tp_dealloc */ |
|---|
| 3399 | n/a | 0, /* tp_print */ |
|---|
| 3400 | n/a | 0, /* tp_getattr */ |
|---|
| 3401 | n/a | 0, /* tp_setattr */ |
|---|
| 3402 | n/a | 0, /* tp_reserved */ |
|---|
| 3403 | n/a | (reprfunc)type_repr, /* tp_repr */ |
|---|
| 3404 | n/a | 0, /* tp_as_number */ |
|---|
| 3405 | n/a | 0, /* tp_as_sequence */ |
|---|
| 3406 | n/a | 0, /* tp_as_mapping */ |
|---|
| 3407 | n/a | 0, /* tp_hash */ |
|---|
| 3408 | n/a | (ternaryfunc)type_call, /* tp_call */ |
|---|
| 3409 | n/a | 0, /* tp_str */ |
|---|
| 3410 | n/a | (getattrofunc)type_getattro, /* tp_getattro */ |
|---|
| 3411 | n/a | (setattrofunc)type_setattro, /* tp_setattro */ |
|---|
| 3412 | n/a | 0, /* tp_as_buffer */ |
|---|
| 3413 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
|---|
| 3414 | n/a | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */ |
|---|
| 3415 | n/a | type_doc, /* tp_doc */ |
|---|
| 3416 | n/a | (traverseproc)type_traverse, /* tp_traverse */ |
|---|
| 3417 | n/a | (inquiry)type_clear, /* tp_clear */ |
|---|
| 3418 | n/a | 0, /* tp_richcompare */ |
|---|
| 3419 | n/a | offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ |
|---|
| 3420 | n/a | 0, /* tp_iter */ |
|---|
| 3421 | n/a | 0, /* tp_iternext */ |
|---|
| 3422 | n/a | type_methods, /* tp_methods */ |
|---|
| 3423 | n/a | type_members, /* tp_members */ |
|---|
| 3424 | n/a | type_getsets, /* tp_getset */ |
|---|
| 3425 | n/a | 0, /* tp_base */ |
|---|
| 3426 | n/a | 0, /* tp_dict */ |
|---|
| 3427 | n/a | 0, /* tp_descr_get */ |
|---|
| 3428 | n/a | 0, /* tp_descr_set */ |
|---|
| 3429 | n/a | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ |
|---|
| 3430 | n/a | type_init, /* tp_init */ |
|---|
| 3431 | n/a | 0, /* tp_alloc */ |
|---|
| 3432 | n/a | type_new, /* tp_new */ |
|---|
| 3433 | n/a | PyObject_GC_Del, /* tp_free */ |
|---|
| 3434 | n/a | (inquiry)type_is_gc, /* tp_is_gc */ |
|---|
| 3435 | n/a | }; |
|---|
| 3436 | n/a | |
|---|
| 3437 | n/a | |
|---|
| 3438 | n/a | /* The base type of all types (eventually)... except itself. */ |
|---|
| 3439 | n/a | |
|---|
| 3440 | n/a | /* You may wonder why object.__new__() only complains about arguments |
|---|
| 3441 | n/a | when object.__init__() is not overridden, and vice versa. |
|---|
| 3442 | n/a | |
|---|
| 3443 | n/a | Consider the use cases: |
|---|
| 3444 | n/a | |
|---|
| 3445 | n/a | 1. When neither is overridden, we want to hear complaints about |
|---|
| 3446 | n/a | excess (i.e., any) arguments, since their presence could |
|---|
| 3447 | n/a | indicate there's a bug. |
|---|
| 3448 | n/a | |
|---|
| 3449 | n/a | 2. When defining an Immutable type, we are likely to override only |
|---|
| 3450 | n/a | __new__(), since __init__() is called too late to initialize an |
|---|
| 3451 | n/a | Immutable object. Since __new__() defines the signature for the |
|---|
| 3452 | n/a | type, it would be a pain to have to override __init__() just to |
|---|
| 3453 | n/a | stop it from complaining about excess arguments. |
|---|
| 3454 | n/a | |
|---|
| 3455 | n/a | 3. When defining a Mutable type, we are likely to override only |
|---|
| 3456 | n/a | __init__(). So here the converse reasoning applies: we don't |
|---|
| 3457 | n/a | want to have to override __new__() just to stop it from |
|---|
| 3458 | n/a | complaining. |
|---|
| 3459 | n/a | |
|---|
| 3460 | n/a | 4. When __init__() is overridden, and the subclass __init__() calls |
|---|
| 3461 | n/a | object.__init__(), the latter should complain about excess |
|---|
| 3462 | n/a | arguments; ditto for __new__(). |
|---|
| 3463 | n/a | |
|---|
| 3464 | n/a | Use cases 2 and 3 make it unattractive to unconditionally check for |
|---|
| 3465 | n/a | excess arguments. The best solution that addresses all four use |
|---|
| 3466 | n/a | cases is as follows: __init__() complains about excess arguments |
|---|
| 3467 | n/a | unless __new__() is overridden and __init__() is not overridden |
|---|
| 3468 | n/a | (IOW, if __init__() is overridden or __new__() is not overridden); |
|---|
| 3469 | n/a | symmetrically, __new__() complains about excess arguments unless |
|---|
| 3470 | n/a | __init__() is overridden and __new__() is not overridden |
|---|
| 3471 | n/a | (IOW, if __new__() is overridden or __init__() is not overridden). |
|---|
| 3472 | n/a | |
|---|
| 3473 | n/a | However, for backwards compatibility, this breaks too much code. |
|---|
| 3474 | n/a | Therefore, in 2.6, we'll *warn* about excess arguments when both |
|---|
| 3475 | n/a | methods are overridden; for all other cases we'll use the above |
|---|
| 3476 | n/a | rules. |
|---|
| 3477 | n/a | |
|---|
| 3478 | n/a | */ |
|---|
| 3479 | n/a | |
|---|
| 3480 | n/a | /* Forward */ |
|---|
| 3481 | n/a | static PyObject * |
|---|
| 3482 | n/a | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
|---|
| 3483 | n/a | |
|---|
| 3484 | n/a | static int |
|---|
| 3485 | n/a | excess_args(PyObject *args, PyObject *kwds) |
|---|
| 3486 | n/a | { |
|---|
| 3487 | n/a | return PyTuple_GET_SIZE(args) || |
|---|
| 3488 | n/a | (kwds && PyDict_Check(kwds) && PyDict_GET_SIZE(kwds)); |
|---|
| 3489 | n/a | } |
|---|
| 3490 | n/a | |
|---|
| 3491 | n/a | static int |
|---|
| 3492 | n/a | object_init(PyObject *self, PyObject *args, PyObject *kwds) |
|---|
| 3493 | n/a | { |
|---|
| 3494 | n/a | int err = 0; |
|---|
| 3495 | n/a | PyTypeObject *type = Py_TYPE(self); |
|---|
| 3496 | n/a | if (excess_args(args, kwds) && |
|---|
| 3497 | n/a | (type->tp_new == object_new || type->tp_init != object_init)) { |
|---|
| 3498 | n/a | PyErr_SetString(PyExc_TypeError, "object.__init__() takes no parameters"); |
|---|
| 3499 | n/a | err = -1; |
|---|
| 3500 | n/a | } |
|---|
| 3501 | n/a | return err; |
|---|
| 3502 | n/a | } |
|---|
| 3503 | n/a | |
|---|
| 3504 | n/a | static PyObject * |
|---|
| 3505 | n/a | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 3506 | n/a | { |
|---|
| 3507 | n/a | if (excess_args(args, kwds) && |
|---|
| 3508 | n/a | (type->tp_init == object_init || type->tp_new != object_new)) { |
|---|
| 3509 | n/a | PyErr_SetString(PyExc_TypeError, "object() takes no parameters"); |
|---|
| 3510 | n/a | return NULL; |
|---|
| 3511 | n/a | } |
|---|
| 3512 | n/a | |
|---|
| 3513 | n/a | if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) { |
|---|
| 3514 | n/a | PyObject *abstract_methods = NULL; |
|---|
| 3515 | n/a | PyObject *builtins; |
|---|
| 3516 | n/a | PyObject *sorted; |
|---|
| 3517 | n/a | PyObject *sorted_methods = NULL; |
|---|
| 3518 | n/a | PyObject *joined = NULL; |
|---|
| 3519 | n/a | PyObject *comma; |
|---|
| 3520 | n/a | _Py_static_string(comma_id, ", "); |
|---|
| 3521 | n/a | _Py_IDENTIFIER(sorted); |
|---|
| 3522 | n/a | |
|---|
| 3523 | n/a | /* Compute ", ".join(sorted(type.__abstractmethods__)) |
|---|
| 3524 | n/a | into joined. */ |
|---|
| 3525 | n/a | abstract_methods = type_abstractmethods(type, NULL); |
|---|
| 3526 | n/a | if (abstract_methods == NULL) |
|---|
| 3527 | n/a | goto error; |
|---|
| 3528 | n/a | builtins = PyEval_GetBuiltins(); |
|---|
| 3529 | n/a | if (builtins == NULL) |
|---|
| 3530 | n/a | goto error; |
|---|
| 3531 | n/a | sorted = _PyDict_GetItemId(builtins, &PyId_sorted); |
|---|
| 3532 | n/a | if (sorted == NULL) |
|---|
| 3533 | n/a | goto error; |
|---|
| 3534 | n/a | sorted_methods = PyObject_CallFunctionObjArgs(sorted, |
|---|
| 3535 | n/a | abstract_methods, |
|---|
| 3536 | n/a | NULL); |
|---|
| 3537 | n/a | if (sorted_methods == NULL) |
|---|
| 3538 | n/a | goto error; |
|---|
| 3539 | n/a | comma = _PyUnicode_FromId(&comma_id); |
|---|
| 3540 | n/a | if (comma == NULL) |
|---|
| 3541 | n/a | goto error; |
|---|
| 3542 | n/a | joined = PyUnicode_Join(comma, sorted_methods); |
|---|
| 3543 | n/a | if (joined == NULL) |
|---|
| 3544 | n/a | goto error; |
|---|
| 3545 | n/a | |
|---|
| 3546 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 3547 | n/a | "Can't instantiate abstract class %s " |
|---|
| 3548 | n/a | "with abstract methods %U", |
|---|
| 3549 | n/a | type->tp_name, |
|---|
| 3550 | n/a | joined); |
|---|
| 3551 | n/a | error: |
|---|
| 3552 | n/a | Py_XDECREF(joined); |
|---|
| 3553 | n/a | Py_XDECREF(sorted_methods); |
|---|
| 3554 | n/a | Py_XDECREF(abstract_methods); |
|---|
| 3555 | n/a | return NULL; |
|---|
| 3556 | n/a | } |
|---|
| 3557 | n/a | return type->tp_alloc(type, 0); |
|---|
| 3558 | n/a | } |
|---|
| 3559 | n/a | |
|---|
| 3560 | n/a | static void |
|---|
| 3561 | n/a | object_dealloc(PyObject *self) |
|---|
| 3562 | n/a | { |
|---|
| 3563 | n/a | Py_TYPE(self)->tp_free(self); |
|---|
| 3564 | n/a | } |
|---|
| 3565 | n/a | |
|---|
| 3566 | n/a | static PyObject * |
|---|
| 3567 | n/a | object_repr(PyObject *self) |
|---|
| 3568 | n/a | { |
|---|
| 3569 | n/a | PyTypeObject *type; |
|---|
| 3570 | n/a | PyObject *mod, *name, *rtn; |
|---|
| 3571 | n/a | |
|---|
| 3572 | n/a | type = Py_TYPE(self); |
|---|
| 3573 | n/a | mod = type_module(type, NULL); |
|---|
| 3574 | n/a | if (mod == NULL) |
|---|
| 3575 | n/a | PyErr_Clear(); |
|---|
| 3576 | n/a | else if (!PyUnicode_Check(mod)) { |
|---|
| 3577 | n/a | Py_DECREF(mod); |
|---|
| 3578 | n/a | mod = NULL; |
|---|
| 3579 | n/a | } |
|---|
| 3580 | n/a | name = type_qualname(type, NULL); |
|---|
| 3581 | n/a | if (name == NULL) { |
|---|
| 3582 | n/a | Py_XDECREF(mod); |
|---|
| 3583 | n/a | return NULL; |
|---|
| 3584 | n/a | } |
|---|
| 3585 | n/a | if (mod != NULL && !_PyUnicode_EqualToASCIIId(mod, &PyId_builtins)) |
|---|
| 3586 | n/a | rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self); |
|---|
| 3587 | n/a | else |
|---|
| 3588 | n/a | rtn = PyUnicode_FromFormat("<%s object at %p>", |
|---|
| 3589 | n/a | type->tp_name, self); |
|---|
| 3590 | n/a | Py_XDECREF(mod); |
|---|
| 3591 | n/a | Py_DECREF(name); |
|---|
| 3592 | n/a | return rtn; |
|---|
| 3593 | n/a | } |
|---|
| 3594 | n/a | |
|---|
| 3595 | n/a | static PyObject * |
|---|
| 3596 | n/a | object_str(PyObject *self) |
|---|
| 3597 | n/a | { |
|---|
| 3598 | n/a | unaryfunc f; |
|---|
| 3599 | n/a | |
|---|
| 3600 | n/a | f = Py_TYPE(self)->tp_repr; |
|---|
| 3601 | n/a | if (f == NULL) |
|---|
| 3602 | n/a | f = object_repr; |
|---|
| 3603 | n/a | return f(self); |
|---|
| 3604 | n/a | } |
|---|
| 3605 | n/a | |
|---|
| 3606 | n/a | static PyObject * |
|---|
| 3607 | n/a | object_richcompare(PyObject *self, PyObject *other, int op) |
|---|
| 3608 | n/a | { |
|---|
| 3609 | n/a | PyObject *res; |
|---|
| 3610 | n/a | |
|---|
| 3611 | n/a | switch (op) { |
|---|
| 3612 | n/a | |
|---|
| 3613 | n/a | case Py_EQ: |
|---|
| 3614 | n/a | /* Return NotImplemented instead of False, so if two |
|---|
| 3615 | n/a | objects are compared, both get a chance at the |
|---|
| 3616 | n/a | comparison. See issue #1393. */ |
|---|
| 3617 | n/a | res = (self == other) ? Py_True : Py_NotImplemented; |
|---|
| 3618 | n/a | Py_INCREF(res); |
|---|
| 3619 | n/a | break; |
|---|
| 3620 | n/a | |
|---|
| 3621 | n/a | case Py_NE: |
|---|
| 3622 | n/a | /* By default, __ne__() delegates to __eq__() and inverts the result, |
|---|
| 3623 | n/a | unless the latter returns NotImplemented. */ |
|---|
| 3624 | n/a | if (self->ob_type->tp_richcompare == NULL) { |
|---|
| 3625 | n/a | res = Py_NotImplemented; |
|---|
| 3626 | n/a | Py_INCREF(res); |
|---|
| 3627 | n/a | break; |
|---|
| 3628 | n/a | } |
|---|
| 3629 | n/a | res = (*self->ob_type->tp_richcompare)(self, other, Py_EQ); |
|---|
| 3630 | n/a | if (res != NULL && res != Py_NotImplemented) { |
|---|
| 3631 | n/a | int ok = PyObject_IsTrue(res); |
|---|
| 3632 | n/a | Py_DECREF(res); |
|---|
| 3633 | n/a | if (ok < 0) |
|---|
| 3634 | n/a | res = NULL; |
|---|
| 3635 | n/a | else { |
|---|
| 3636 | n/a | if (ok) |
|---|
| 3637 | n/a | res = Py_False; |
|---|
| 3638 | n/a | else |
|---|
| 3639 | n/a | res = Py_True; |
|---|
| 3640 | n/a | Py_INCREF(res); |
|---|
| 3641 | n/a | } |
|---|
| 3642 | n/a | } |
|---|
| 3643 | n/a | break; |
|---|
| 3644 | n/a | |
|---|
| 3645 | n/a | default: |
|---|
| 3646 | n/a | res = Py_NotImplemented; |
|---|
| 3647 | n/a | Py_INCREF(res); |
|---|
| 3648 | n/a | break; |
|---|
| 3649 | n/a | } |
|---|
| 3650 | n/a | |
|---|
| 3651 | n/a | return res; |
|---|
| 3652 | n/a | } |
|---|
| 3653 | n/a | |
|---|
| 3654 | n/a | static PyObject * |
|---|
| 3655 | n/a | object_get_class(PyObject *self, void *closure) |
|---|
| 3656 | n/a | { |
|---|
| 3657 | n/a | Py_INCREF(Py_TYPE(self)); |
|---|
| 3658 | n/a | return (PyObject *)(Py_TYPE(self)); |
|---|
| 3659 | n/a | } |
|---|
| 3660 | n/a | |
|---|
| 3661 | n/a | static int |
|---|
| 3662 | n/a | compatible_with_tp_base(PyTypeObject *child) |
|---|
| 3663 | n/a | { |
|---|
| 3664 | n/a | PyTypeObject *parent = child->tp_base; |
|---|
| 3665 | n/a | return (parent != NULL && |
|---|
| 3666 | n/a | child->tp_basicsize == parent->tp_basicsize && |
|---|
| 3667 | n/a | child->tp_itemsize == parent->tp_itemsize && |
|---|
| 3668 | n/a | child->tp_dictoffset == parent->tp_dictoffset && |
|---|
| 3669 | n/a | child->tp_weaklistoffset == parent->tp_weaklistoffset && |
|---|
| 3670 | n/a | ((child->tp_flags & Py_TPFLAGS_HAVE_GC) == |
|---|
| 3671 | n/a | (parent->tp_flags & Py_TPFLAGS_HAVE_GC)) && |
|---|
| 3672 | n/a | (child->tp_dealloc == subtype_dealloc || |
|---|
| 3673 | n/a | child->tp_dealloc == parent->tp_dealloc)); |
|---|
| 3674 | n/a | } |
|---|
| 3675 | n/a | |
|---|
| 3676 | n/a | static int |
|---|
| 3677 | n/a | same_slots_added(PyTypeObject *a, PyTypeObject *b) |
|---|
| 3678 | n/a | { |
|---|
| 3679 | n/a | PyTypeObject *base = a->tp_base; |
|---|
| 3680 | n/a | Py_ssize_t size; |
|---|
| 3681 | n/a | PyObject *slots_a, *slots_b; |
|---|
| 3682 | n/a | |
|---|
| 3683 | n/a | assert(base == b->tp_base); |
|---|
| 3684 | n/a | size = base->tp_basicsize; |
|---|
| 3685 | n/a | if (a->tp_dictoffset == size && b->tp_dictoffset == size) |
|---|
| 3686 | n/a | size += sizeof(PyObject *); |
|---|
| 3687 | n/a | if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size) |
|---|
| 3688 | n/a | size += sizeof(PyObject *); |
|---|
| 3689 | n/a | |
|---|
| 3690 | n/a | /* Check slots compliance */ |
|---|
| 3691 | n/a | if (!(a->tp_flags & Py_TPFLAGS_HEAPTYPE) || |
|---|
| 3692 | n/a | !(b->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
|---|
| 3693 | n/a | return 0; |
|---|
| 3694 | n/a | } |
|---|
| 3695 | n/a | slots_a = ((PyHeapTypeObject *)a)->ht_slots; |
|---|
| 3696 | n/a | slots_b = ((PyHeapTypeObject *)b)->ht_slots; |
|---|
| 3697 | n/a | if (slots_a && slots_b) { |
|---|
| 3698 | n/a | if (PyObject_RichCompareBool(slots_a, slots_b, Py_EQ) != 1) |
|---|
| 3699 | n/a | return 0; |
|---|
| 3700 | n/a | size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a); |
|---|
| 3701 | n/a | } |
|---|
| 3702 | n/a | return size == a->tp_basicsize && size == b->tp_basicsize; |
|---|
| 3703 | n/a | } |
|---|
| 3704 | n/a | |
|---|
| 3705 | n/a | static int |
|---|
| 3706 | n/a | compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, const char* attr) |
|---|
| 3707 | n/a | { |
|---|
| 3708 | n/a | PyTypeObject *newbase, *oldbase; |
|---|
| 3709 | n/a | |
|---|
| 3710 | n/a | if (newto->tp_free != oldto->tp_free) { |
|---|
| 3711 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 3712 | n/a | "%s assignment: " |
|---|
| 3713 | n/a | "'%s' deallocator differs from '%s'", |
|---|
| 3714 | n/a | attr, |
|---|
| 3715 | n/a | newto->tp_name, |
|---|
| 3716 | n/a | oldto->tp_name); |
|---|
| 3717 | n/a | return 0; |
|---|
| 3718 | n/a | } |
|---|
| 3719 | n/a | /* |
|---|
| 3720 | n/a | It's tricky to tell if two arbitrary types are sufficiently compatible as |
|---|
| 3721 | n/a | to be interchangeable; e.g., even if they have the same tp_basicsize, they |
|---|
| 3722 | n/a | might have totally different struct fields. It's much easier to tell if a |
|---|
| 3723 | n/a | type and its supertype are compatible; e.g., if they have the same |
|---|
| 3724 | n/a | tp_basicsize, then that means they have identical fields. So to check |
|---|
| 3725 | n/a | whether two arbitrary types are compatible, we first find the highest |
|---|
| 3726 | n/a | supertype that each is compatible with, and then if those supertypes are |
|---|
| 3727 | n/a | compatible then the original types must also be compatible. |
|---|
| 3728 | n/a | */ |
|---|
| 3729 | n/a | newbase = newto; |
|---|
| 3730 | n/a | oldbase = oldto; |
|---|
| 3731 | n/a | while (compatible_with_tp_base(newbase)) |
|---|
| 3732 | n/a | newbase = newbase->tp_base; |
|---|
| 3733 | n/a | while (compatible_with_tp_base(oldbase)) |
|---|
| 3734 | n/a | oldbase = oldbase->tp_base; |
|---|
| 3735 | n/a | if (newbase != oldbase && |
|---|
| 3736 | n/a | (newbase->tp_base != oldbase->tp_base || |
|---|
| 3737 | n/a | !same_slots_added(newbase, oldbase))) { |
|---|
| 3738 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 3739 | n/a | "%s assignment: " |
|---|
| 3740 | n/a | "'%s' object layout differs from '%s'", |
|---|
| 3741 | n/a | attr, |
|---|
| 3742 | n/a | newto->tp_name, |
|---|
| 3743 | n/a | oldto->tp_name); |
|---|
| 3744 | n/a | return 0; |
|---|
| 3745 | n/a | } |
|---|
| 3746 | n/a | |
|---|
| 3747 | n/a | return 1; |
|---|
| 3748 | n/a | } |
|---|
| 3749 | n/a | |
|---|
| 3750 | n/a | static int |
|---|
| 3751 | n/a | object_set_class(PyObject *self, PyObject *value, void *closure) |
|---|
| 3752 | n/a | { |
|---|
| 3753 | n/a | PyTypeObject *oldto = Py_TYPE(self); |
|---|
| 3754 | n/a | PyTypeObject *newto; |
|---|
| 3755 | n/a | |
|---|
| 3756 | n/a | if (value == NULL) { |
|---|
| 3757 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 3758 | n/a | "can't delete __class__ attribute"); |
|---|
| 3759 | n/a | return -1; |
|---|
| 3760 | n/a | } |
|---|
| 3761 | n/a | if (!PyType_Check(value)) { |
|---|
| 3762 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 3763 | n/a | "__class__ must be set to a class, not '%s' object", |
|---|
| 3764 | n/a | Py_TYPE(value)->tp_name); |
|---|
| 3765 | n/a | return -1; |
|---|
| 3766 | n/a | } |
|---|
| 3767 | n/a | newto = (PyTypeObject *)value; |
|---|
| 3768 | n/a | /* In versions of CPython prior to 3.5, the code in |
|---|
| 3769 | n/a | compatible_for_assignment was not set up to correctly check for memory |
|---|
| 3770 | n/a | layout / slot / etc. compatibility for non-HEAPTYPE classes, so we just |
|---|
| 3771 | n/a | disallowed __class__ assignment in any case that wasn't HEAPTYPE -> |
|---|
| 3772 | n/a | HEAPTYPE. |
|---|
| 3773 | n/a | |
|---|
| 3774 | n/a | During the 3.5 development cycle, we fixed the code in |
|---|
| 3775 | n/a | compatible_for_assignment to correctly check compatibility between |
|---|
| 3776 | n/a | arbitrary types, and started allowing __class__ assignment in all cases |
|---|
| 3777 | n/a | where the old and new types did in fact have compatible slots and |
|---|
| 3778 | n/a | memory layout (regardless of whether they were implemented as HEAPTYPEs |
|---|
| 3779 | n/a | or not). |
|---|
| 3780 | n/a | |
|---|
| 3781 | n/a | Just before 3.5 was released, though, we discovered that this led to |
|---|
| 3782 | n/a | problems with immutable types like int, where the interpreter assumes |
|---|
| 3783 | n/a | they are immutable and interns some values. Formerly this wasn't a |
|---|
| 3784 | n/a | problem, because they really were immutable -- in particular, all the |
|---|
| 3785 | n/a | types where the interpreter applied this interning trick happened to |
|---|
| 3786 | n/a | also be statically allocated, so the old HEAPTYPE rules were |
|---|
| 3787 | n/a | "accidentally" stopping them from allowing __class__ assignment. But |
|---|
| 3788 | n/a | with the changes to __class__ assignment, we started allowing code like |
|---|
| 3789 | n/a | |
|---|
| 3790 | n/a | class MyInt(int): |
|---|
| 3791 | n/a | ... |
|---|
| 3792 | n/a | # Modifies the type of *all* instances of 1 in the whole program, |
|---|
| 3793 | n/a | # including future instances (!), because the 1 object is interned. |
|---|
| 3794 | n/a | (1).__class__ = MyInt |
|---|
| 3795 | n/a | |
|---|
| 3796 | n/a | (see https://bugs.python.org/issue24912). |
|---|
| 3797 | n/a | |
|---|
| 3798 | n/a | In theory the proper fix would be to identify which classes rely on |
|---|
| 3799 | n/a | this invariant and somehow disallow __class__ assignment only for them, |
|---|
| 3800 | n/a | perhaps via some mechanism like a new Py_TPFLAGS_IMMUTABLE flag (a |
|---|
| 3801 | n/a | "blacklisting" approach). But in practice, since this problem wasn't |
|---|
| 3802 | n/a | noticed late in the 3.5 RC cycle, we're taking the conservative |
|---|
| 3803 | n/a | approach and reinstating the same HEAPTYPE->HEAPTYPE check that we used |
|---|
| 3804 | n/a | to have, plus a "whitelist". For now, the whitelist consists only of |
|---|
| 3805 | n/a | ModuleType subtypes, since those are the cases that motivated the patch |
|---|
| 3806 | n/a | in the first place -- see https://bugs.python.org/issue22986 -- and |
|---|
| 3807 | n/a | since module objects are mutable we can be sure that they are |
|---|
| 3808 | n/a | definitely not being interned. So now we allow HEAPTYPE->HEAPTYPE *or* |
|---|
| 3809 | n/a | ModuleType subtype -> ModuleType subtype. |
|---|
| 3810 | n/a | |
|---|
| 3811 | n/a | So far as we know, all the code beyond the following 'if' statement |
|---|
| 3812 | n/a | will correctly handle non-HEAPTYPE classes, and the HEAPTYPE check is |
|---|
| 3813 | n/a | needed only to protect that subset of non-HEAPTYPE classes for which |
|---|
| 3814 | n/a | the interpreter has baked in the assumption that all instances are |
|---|
| 3815 | n/a | truly immutable. |
|---|
| 3816 | n/a | */ |
|---|
| 3817 | n/a | if (!(PyType_IsSubtype(newto, &PyModule_Type) && |
|---|
| 3818 | n/a | PyType_IsSubtype(oldto, &PyModule_Type)) && |
|---|
| 3819 | n/a | (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) || |
|---|
| 3820 | n/a | !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))) { |
|---|
| 3821 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 3822 | n/a | "__class__ assignment only supported for heap types " |
|---|
| 3823 | n/a | "or ModuleType subclasses"); |
|---|
| 3824 | n/a | return -1; |
|---|
| 3825 | n/a | } |
|---|
| 3826 | n/a | |
|---|
| 3827 | n/a | if (compatible_for_assignment(oldto, newto, "__class__")) { |
|---|
| 3828 | n/a | if (newto->tp_flags & Py_TPFLAGS_HEAPTYPE) |
|---|
| 3829 | n/a | Py_INCREF(newto); |
|---|
| 3830 | n/a | Py_TYPE(self) = newto; |
|---|
| 3831 | n/a | if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE) |
|---|
| 3832 | n/a | Py_DECREF(oldto); |
|---|
| 3833 | n/a | return 0; |
|---|
| 3834 | n/a | } |
|---|
| 3835 | n/a | else { |
|---|
| 3836 | n/a | return -1; |
|---|
| 3837 | n/a | } |
|---|
| 3838 | n/a | } |
|---|
| 3839 | n/a | |
|---|
| 3840 | n/a | static PyGetSetDef object_getsets[] = { |
|---|
| 3841 | n/a | {"__class__", object_get_class, object_set_class, |
|---|
| 3842 | n/a | PyDoc_STR("the object's class")}, |
|---|
| 3843 | n/a | {0} |
|---|
| 3844 | n/a | }; |
|---|
| 3845 | n/a | |
|---|
| 3846 | n/a | |
|---|
| 3847 | n/a | /* Stuff to implement __reduce_ex__ for pickle protocols >= 2. |
|---|
| 3848 | n/a | We fall back to helpers in copyreg for: |
|---|
| 3849 | n/a | - pickle protocols < 2 |
|---|
| 3850 | n/a | - calculating the list of slot names (done only once per class) |
|---|
| 3851 | n/a | - the __newobj__ function (which is used as a token but never called) |
|---|
| 3852 | n/a | */ |
|---|
| 3853 | n/a | |
|---|
| 3854 | n/a | static PyObject * |
|---|
| 3855 | n/a | import_copyreg(void) |
|---|
| 3856 | n/a | { |
|---|
| 3857 | n/a | PyObject *copyreg_str; |
|---|
| 3858 | n/a | PyObject *copyreg_module; |
|---|
| 3859 | n/a | PyInterpreterState *interp = PyThreadState_GET()->interp; |
|---|
| 3860 | n/a | _Py_IDENTIFIER(copyreg); |
|---|
| 3861 | n/a | |
|---|
| 3862 | n/a | copyreg_str = _PyUnicode_FromId(&PyId_copyreg); |
|---|
| 3863 | n/a | if (copyreg_str == NULL) { |
|---|
| 3864 | n/a | return NULL; |
|---|
| 3865 | n/a | } |
|---|
| 3866 | n/a | /* Try to fetch cached copy of copyreg from sys.modules first in an |
|---|
| 3867 | n/a | attempt to avoid the import overhead. Previously this was implemented |
|---|
| 3868 | n/a | by storing a reference to the cached module in a static variable, but |
|---|
| 3869 | n/a | this broke when multiple embedded interpreters were in use (see issue |
|---|
| 3870 | n/a | #17408 and #19088). */ |
|---|
| 3871 | n/a | copyreg_module = PyDict_GetItemWithError(interp->modules, copyreg_str); |
|---|
| 3872 | n/a | if (copyreg_module != NULL) { |
|---|
| 3873 | n/a | Py_INCREF(copyreg_module); |
|---|
| 3874 | n/a | return copyreg_module; |
|---|
| 3875 | n/a | } |
|---|
| 3876 | n/a | if (PyErr_Occurred()) { |
|---|
| 3877 | n/a | return NULL; |
|---|
| 3878 | n/a | } |
|---|
| 3879 | n/a | return PyImport_Import(copyreg_str); |
|---|
| 3880 | n/a | } |
|---|
| 3881 | n/a | |
|---|
| 3882 | n/a | static PyObject * |
|---|
| 3883 | n/a | _PyType_GetSlotNames(PyTypeObject *cls) |
|---|
| 3884 | n/a | { |
|---|
| 3885 | n/a | PyObject *copyreg; |
|---|
| 3886 | n/a | PyObject *slotnames; |
|---|
| 3887 | n/a | _Py_IDENTIFIER(__slotnames__); |
|---|
| 3888 | n/a | _Py_IDENTIFIER(_slotnames); |
|---|
| 3889 | n/a | |
|---|
| 3890 | n/a | assert(PyType_Check(cls)); |
|---|
| 3891 | n/a | |
|---|
| 3892 | n/a | /* Get the slot names from the cache in the class if possible. */ |
|---|
| 3893 | n/a | slotnames = _PyDict_GetItemIdWithError(cls->tp_dict, &PyId___slotnames__); |
|---|
| 3894 | n/a | if (slotnames != NULL) { |
|---|
| 3895 | n/a | if (slotnames != Py_None && !PyList_Check(slotnames)) { |
|---|
| 3896 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 3897 | n/a | "%.200s.__slotnames__ should be a list or None, " |
|---|
| 3898 | n/a | "not %.200s", |
|---|
| 3899 | n/a | cls->tp_name, Py_TYPE(slotnames)->tp_name); |
|---|
| 3900 | n/a | return NULL; |
|---|
| 3901 | n/a | } |
|---|
| 3902 | n/a | Py_INCREF(slotnames); |
|---|
| 3903 | n/a | return slotnames; |
|---|
| 3904 | n/a | } |
|---|
| 3905 | n/a | else { |
|---|
| 3906 | n/a | if (PyErr_Occurred()) { |
|---|
| 3907 | n/a | return NULL; |
|---|
| 3908 | n/a | } |
|---|
| 3909 | n/a | /* The class does not have the slot names cached yet. */ |
|---|
| 3910 | n/a | } |
|---|
| 3911 | n/a | |
|---|
| 3912 | n/a | copyreg = import_copyreg(); |
|---|
| 3913 | n/a | if (copyreg == NULL) |
|---|
| 3914 | n/a | return NULL; |
|---|
| 3915 | n/a | |
|---|
| 3916 | n/a | /* Use _slotnames function from the copyreg module to find the slots |
|---|
| 3917 | n/a | by this class and its bases. This function will cache the result |
|---|
| 3918 | n/a | in __slotnames__. */ |
|---|
| 3919 | n/a | slotnames = _PyObject_CallMethodIdObjArgs(copyreg, &PyId__slotnames, |
|---|
| 3920 | n/a | cls, NULL); |
|---|
| 3921 | n/a | Py_DECREF(copyreg); |
|---|
| 3922 | n/a | if (slotnames == NULL) |
|---|
| 3923 | n/a | return NULL; |
|---|
| 3924 | n/a | |
|---|
| 3925 | n/a | if (slotnames != Py_None && !PyList_Check(slotnames)) { |
|---|
| 3926 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 3927 | n/a | "copyreg._slotnames didn't return a list or None"); |
|---|
| 3928 | n/a | Py_DECREF(slotnames); |
|---|
| 3929 | n/a | return NULL; |
|---|
| 3930 | n/a | } |
|---|
| 3931 | n/a | |
|---|
| 3932 | n/a | return slotnames; |
|---|
| 3933 | n/a | } |
|---|
| 3934 | n/a | |
|---|
| 3935 | n/a | static PyObject * |
|---|
| 3936 | n/a | _PyObject_GetState(PyObject *obj, int required) |
|---|
| 3937 | n/a | { |
|---|
| 3938 | n/a | PyObject *state; |
|---|
| 3939 | n/a | PyObject *getstate; |
|---|
| 3940 | n/a | _Py_IDENTIFIER(__getstate__); |
|---|
| 3941 | n/a | |
|---|
| 3942 | n/a | getstate = _PyObject_GetAttrId(obj, &PyId___getstate__); |
|---|
| 3943 | n/a | if (getstate == NULL) { |
|---|
| 3944 | n/a | PyObject *slotnames; |
|---|
| 3945 | n/a | |
|---|
| 3946 | n/a | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
|---|
| 3947 | n/a | return NULL; |
|---|
| 3948 | n/a | } |
|---|
| 3949 | n/a | PyErr_Clear(); |
|---|
| 3950 | n/a | |
|---|
| 3951 | n/a | if (required && obj->ob_type->tp_itemsize) { |
|---|
| 3952 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 3953 | n/a | "can't pickle %.200s objects", |
|---|
| 3954 | n/a | Py_TYPE(obj)->tp_name); |
|---|
| 3955 | n/a | return NULL; |
|---|
| 3956 | n/a | } |
|---|
| 3957 | n/a | |
|---|
| 3958 | n/a | { |
|---|
| 3959 | n/a | PyObject **dict; |
|---|
| 3960 | n/a | dict = _PyObject_GetDictPtr(obj); |
|---|
| 3961 | n/a | /* It is possible that the object's dict is not initialized |
|---|
| 3962 | n/a | yet. In this case, we will return None for the state. |
|---|
| 3963 | n/a | We also return None if the dict is empty to make the behavior |
|---|
| 3964 | n/a | consistent regardless whether the dict was initialized or not. |
|---|
| 3965 | n/a | This make unit testing easier. */ |
|---|
| 3966 | n/a | if (dict != NULL && *dict != NULL && PyDict_GET_SIZE(*dict)) { |
|---|
| 3967 | n/a | state = *dict; |
|---|
| 3968 | n/a | } |
|---|
| 3969 | n/a | else { |
|---|
| 3970 | n/a | state = Py_None; |
|---|
| 3971 | n/a | } |
|---|
| 3972 | n/a | Py_INCREF(state); |
|---|
| 3973 | n/a | } |
|---|
| 3974 | n/a | |
|---|
| 3975 | n/a | slotnames = _PyType_GetSlotNames(Py_TYPE(obj)); |
|---|
| 3976 | n/a | if (slotnames == NULL) { |
|---|
| 3977 | n/a | Py_DECREF(state); |
|---|
| 3978 | n/a | return NULL; |
|---|
| 3979 | n/a | } |
|---|
| 3980 | n/a | |
|---|
| 3981 | n/a | assert(slotnames == Py_None || PyList_Check(slotnames)); |
|---|
| 3982 | n/a | if (required) { |
|---|
| 3983 | n/a | Py_ssize_t basicsize = PyBaseObject_Type.tp_basicsize; |
|---|
| 3984 | n/a | if (obj->ob_type->tp_dictoffset) |
|---|
| 3985 | n/a | basicsize += sizeof(PyObject *); |
|---|
| 3986 | n/a | if (obj->ob_type->tp_weaklistoffset) |
|---|
| 3987 | n/a | basicsize += sizeof(PyObject *); |
|---|
| 3988 | n/a | if (slotnames != Py_None) |
|---|
| 3989 | n/a | basicsize += sizeof(PyObject *) * Py_SIZE(slotnames); |
|---|
| 3990 | n/a | if (obj->ob_type->tp_basicsize > basicsize) { |
|---|
| 3991 | n/a | Py_DECREF(slotnames); |
|---|
| 3992 | n/a | Py_DECREF(state); |
|---|
| 3993 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 3994 | n/a | "can't pickle %.200s objects", |
|---|
| 3995 | n/a | Py_TYPE(obj)->tp_name); |
|---|
| 3996 | n/a | return NULL; |
|---|
| 3997 | n/a | } |
|---|
| 3998 | n/a | } |
|---|
| 3999 | n/a | |
|---|
| 4000 | n/a | if (slotnames != Py_None && Py_SIZE(slotnames) > 0) { |
|---|
| 4001 | n/a | PyObject *slots; |
|---|
| 4002 | n/a | Py_ssize_t slotnames_size, i; |
|---|
| 4003 | n/a | |
|---|
| 4004 | n/a | slots = PyDict_New(); |
|---|
| 4005 | n/a | if (slots == NULL) { |
|---|
| 4006 | n/a | Py_DECREF(slotnames); |
|---|
| 4007 | n/a | Py_DECREF(state); |
|---|
| 4008 | n/a | return NULL; |
|---|
| 4009 | n/a | } |
|---|
| 4010 | n/a | |
|---|
| 4011 | n/a | slotnames_size = Py_SIZE(slotnames); |
|---|
| 4012 | n/a | for (i = 0; i < slotnames_size; i++) { |
|---|
| 4013 | n/a | PyObject *name, *value; |
|---|
| 4014 | n/a | |
|---|
| 4015 | n/a | name = PyList_GET_ITEM(slotnames, i); |
|---|
| 4016 | n/a | Py_INCREF(name); |
|---|
| 4017 | n/a | value = PyObject_GetAttr(obj, name); |
|---|
| 4018 | n/a | if (value == NULL) { |
|---|
| 4019 | n/a | Py_DECREF(name); |
|---|
| 4020 | n/a | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
|---|
| 4021 | n/a | goto error; |
|---|
| 4022 | n/a | } |
|---|
| 4023 | n/a | /* It is not an error if the attribute is not present. */ |
|---|
| 4024 | n/a | PyErr_Clear(); |
|---|
| 4025 | n/a | } |
|---|
| 4026 | n/a | else { |
|---|
| 4027 | n/a | int err = PyDict_SetItem(slots, name, value); |
|---|
| 4028 | n/a | Py_DECREF(name); |
|---|
| 4029 | n/a | Py_DECREF(value); |
|---|
| 4030 | n/a | if (err) { |
|---|
| 4031 | n/a | goto error; |
|---|
| 4032 | n/a | } |
|---|
| 4033 | n/a | } |
|---|
| 4034 | n/a | |
|---|
| 4035 | n/a | /* The list is stored on the class so it may mutate while we |
|---|
| 4036 | n/a | iterate over it */ |
|---|
| 4037 | n/a | if (slotnames_size != Py_SIZE(slotnames)) { |
|---|
| 4038 | n/a | PyErr_Format(PyExc_RuntimeError, |
|---|
| 4039 | n/a | "__slotsname__ changed size during iteration"); |
|---|
| 4040 | n/a | goto error; |
|---|
| 4041 | n/a | } |
|---|
| 4042 | n/a | |
|---|
| 4043 | n/a | /* We handle errors within the loop here. */ |
|---|
| 4044 | n/a | if (0) { |
|---|
| 4045 | n/a | error: |
|---|
| 4046 | n/a | Py_DECREF(slotnames); |
|---|
| 4047 | n/a | Py_DECREF(slots); |
|---|
| 4048 | n/a | Py_DECREF(state); |
|---|
| 4049 | n/a | return NULL; |
|---|
| 4050 | n/a | } |
|---|
| 4051 | n/a | } |
|---|
| 4052 | n/a | |
|---|
| 4053 | n/a | /* If we found some slot attributes, pack them in a tuple along |
|---|
| 4054 | n/a | the original attribute dictionary. */ |
|---|
| 4055 | n/a | if (PyDict_GET_SIZE(slots) > 0) { |
|---|
| 4056 | n/a | PyObject *state2; |
|---|
| 4057 | n/a | |
|---|
| 4058 | n/a | state2 = PyTuple_Pack(2, state, slots); |
|---|
| 4059 | n/a | Py_DECREF(state); |
|---|
| 4060 | n/a | if (state2 == NULL) { |
|---|
| 4061 | n/a | Py_DECREF(slotnames); |
|---|
| 4062 | n/a | Py_DECREF(slots); |
|---|
| 4063 | n/a | return NULL; |
|---|
| 4064 | n/a | } |
|---|
| 4065 | n/a | state = state2; |
|---|
| 4066 | n/a | } |
|---|
| 4067 | n/a | Py_DECREF(slots); |
|---|
| 4068 | n/a | } |
|---|
| 4069 | n/a | Py_DECREF(slotnames); |
|---|
| 4070 | n/a | } |
|---|
| 4071 | n/a | else { /* getstate != NULL */ |
|---|
| 4072 | n/a | state = _PyObject_CallNoArg(getstate); |
|---|
| 4073 | n/a | Py_DECREF(getstate); |
|---|
| 4074 | n/a | if (state == NULL) |
|---|
| 4075 | n/a | return NULL; |
|---|
| 4076 | n/a | } |
|---|
| 4077 | n/a | |
|---|
| 4078 | n/a | return state; |
|---|
| 4079 | n/a | } |
|---|
| 4080 | n/a | |
|---|
| 4081 | n/a | static int |
|---|
| 4082 | n/a | _PyObject_GetNewArguments(PyObject *obj, PyObject **args, PyObject **kwargs) |
|---|
| 4083 | n/a | { |
|---|
| 4084 | n/a | PyObject *getnewargs, *getnewargs_ex; |
|---|
| 4085 | n/a | _Py_IDENTIFIER(__getnewargs_ex__); |
|---|
| 4086 | n/a | _Py_IDENTIFIER(__getnewargs__); |
|---|
| 4087 | n/a | |
|---|
| 4088 | n/a | if (args == NULL || kwargs == NULL) { |
|---|
| 4089 | n/a | PyErr_BadInternalCall(); |
|---|
| 4090 | n/a | return -1; |
|---|
| 4091 | n/a | } |
|---|
| 4092 | n/a | |
|---|
| 4093 | n/a | /* We first attempt to fetch the arguments for __new__ by calling |
|---|
| 4094 | n/a | __getnewargs_ex__ on the object. */ |
|---|
| 4095 | n/a | getnewargs_ex = _PyObject_LookupSpecial(obj, &PyId___getnewargs_ex__); |
|---|
| 4096 | n/a | if (getnewargs_ex != NULL) { |
|---|
| 4097 | n/a | PyObject *newargs = _PyObject_CallNoArg(getnewargs_ex); |
|---|
| 4098 | n/a | Py_DECREF(getnewargs_ex); |
|---|
| 4099 | n/a | if (newargs == NULL) { |
|---|
| 4100 | n/a | return -1; |
|---|
| 4101 | n/a | } |
|---|
| 4102 | n/a | if (!PyTuple_Check(newargs)) { |
|---|
| 4103 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 4104 | n/a | "__getnewargs_ex__ should return a tuple, " |
|---|
| 4105 | n/a | "not '%.200s'", Py_TYPE(newargs)->tp_name); |
|---|
| 4106 | n/a | Py_DECREF(newargs); |
|---|
| 4107 | n/a | return -1; |
|---|
| 4108 | n/a | } |
|---|
| 4109 | n/a | if (Py_SIZE(newargs) != 2) { |
|---|
| 4110 | n/a | PyErr_Format(PyExc_ValueError, |
|---|
| 4111 | n/a | "__getnewargs_ex__ should return a tuple of " |
|---|
| 4112 | n/a | "length 2, not %zd", Py_SIZE(newargs)); |
|---|
| 4113 | n/a | Py_DECREF(newargs); |
|---|
| 4114 | n/a | return -1; |
|---|
| 4115 | n/a | } |
|---|
| 4116 | n/a | *args = PyTuple_GET_ITEM(newargs, 0); |
|---|
| 4117 | n/a | Py_INCREF(*args); |
|---|
| 4118 | n/a | *kwargs = PyTuple_GET_ITEM(newargs, 1); |
|---|
| 4119 | n/a | Py_INCREF(*kwargs); |
|---|
| 4120 | n/a | Py_DECREF(newargs); |
|---|
| 4121 | n/a | |
|---|
| 4122 | n/a | /* XXX We should perhaps allow None to be passed here. */ |
|---|
| 4123 | n/a | if (!PyTuple_Check(*args)) { |
|---|
| 4124 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 4125 | n/a | "first item of the tuple returned by " |
|---|
| 4126 | n/a | "__getnewargs_ex__ must be a tuple, not '%.200s'", |
|---|
| 4127 | n/a | Py_TYPE(*args)->tp_name); |
|---|
| 4128 | n/a | Py_CLEAR(*args); |
|---|
| 4129 | n/a | Py_CLEAR(*kwargs); |
|---|
| 4130 | n/a | return -1; |
|---|
| 4131 | n/a | } |
|---|
| 4132 | n/a | if (!PyDict_Check(*kwargs)) { |
|---|
| 4133 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 4134 | n/a | "second item of the tuple returned by " |
|---|
| 4135 | n/a | "__getnewargs_ex__ must be a dict, not '%.200s'", |
|---|
| 4136 | n/a | Py_TYPE(*kwargs)->tp_name); |
|---|
| 4137 | n/a | Py_CLEAR(*args); |
|---|
| 4138 | n/a | Py_CLEAR(*kwargs); |
|---|
| 4139 | n/a | return -1; |
|---|
| 4140 | n/a | } |
|---|
| 4141 | n/a | return 0; |
|---|
| 4142 | n/a | } else if (PyErr_Occurred()) { |
|---|
| 4143 | n/a | return -1; |
|---|
| 4144 | n/a | } |
|---|
| 4145 | n/a | |
|---|
| 4146 | n/a | /* The object does not have __getnewargs_ex__ so we fallback on using |
|---|
| 4147 | n/a | __getnewargs__ instead. */ |
|---|
| 4148 | n/a | getnewargs = _PyObject_LookupSpecial(obj, &PyId___getnewargs__); |
|---|
| 4149 | n/a | if (getnewargs != NULL) { |
|---|
| 4150 | n/a | *args = _PyObject_CallNoArg(getnewargs); |
|---|
| 4151 | n/a | Py_DECREF(getnewargs); |
|---|
| 4152 | n/a | if (*args == NULL) { |
|---|
| 4153 | n/a | return -1; |
|---|
| 4154 | n/a | } |
|---|
| 4155 | n/a | if (!PyTuple_Check(*args)) { |
|---|
| 4156 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 4157 | n/a | "__getnewargs__ should return a tuple, " |
|---|
| 4158 | n/a | "not '%.200s'", Py_TYPE(*args)->tp_name); |
|---|
| 4159 | n/a | Py_CLEAR(*args); |
|---|
| 4160 | n/a | return -1; |
|---|
| 4161 | n/a | } |
|---|
| 4162 | n/a | *kwargs = NULL; |
|---|
| 4163 | n/a | return 0; |
|---|
| 4164 | n/a | } else if (PyErr_Occurred()) { |
|---|
| 4165 | n/a | return -1; |
|---|
| 4166 | n/a | } |
|---|
| 4167 | n/a | |
|---|
| 4168 | n/a | /* The object does not have __getnewargs_ex__ and __getnewargs__. This may |
|---|
| 4169 | n/a | mean __new__ does not takes any arguments on this object, or that the |
|---|
| 4170 | n/a | object does not implement the reduce protocol for pickling or |
|---|
| 4171 | n/a | copying. */ |
|---|
| 4172 | n/a | *args = NULL; |
|---|
| 4173 | n/a | *kwargs = NULL; |
|---|
| 4174 | n/a | return 0; |
|---|
| 4175 | n/a | } |
|---|
| 4176 | n/a | |
|---|
| 4177 | n/a | static int |
|---|
| 4178 | n/a | _PyObject_GetItemsIter(PyObject *obj, PyObject **listitems, |
|---|
| 4179 | n/a | PyObject **dictitems) |
|---|
| 4180 | n/a | { |
|---|
| 4181 | n/a | if (listitems == NULL || dictitems == NULL) { |
|---|
| 4182 | n/a | PyErr_BadInternalCall(); |
|---|
| 4183 | n/a | return -1; |
|---|
| 4184 | n/a | } |
|---|
| 4185 | n/a | |
|---|
| 4186 | n/a | if (!PyList_Check(obj)) { |
|---|
| 4187 | n/a | *listitems = Py_None; |
|---|
| 4188 | n/a | Py_INCREF(*listitems); |
|---|
| 4189 | n/a | } |
|---|
| 4190 | n/a | else { |
|---|
| 4191 | n/a | *listitems = PyObject_GetIter(obj); |
|---|
| 4192 | n/a | if (*listitems == NULL) |
|---|
| 4193 | n/a | return -1; |
|---|
| 4194 | n/a | } |
|---|
| 4195 | n/a | |
|---|
| 4196 | n/a | if (!PyDict_Check(obj)) { |
|---|
| 4197 | n/a | *dictitems = Py_None; |
|---|
| 4198 | n/a | Py_INCREF(*dictitems); |
|---|
| 4199 | n/a | } |
|---|
| 4200 | n/a | else { |
|---|
| 4201 | n/a | PyObject *items; |
|---|
| 4202 | n/a | _Py_IDENTIFIER(items); |
|---|
| 4203 | n/a | |
|---|
| 4204 | n/a | items = _PyObject_CallMethodIdObjArgs(obj, &PyId_items, NULL); |
|---|
| 4205 | n/a | if (items == NULL) { |
|---|
| 4206 | n/a | Py_CLEAR(*listitems); |
|---|
| 4207 | n/a | return -1; |
|---|
| 4208 | n/a | } |
|---|
| 4209 | n/a | *dictitems = PyObject_GetIter(items); |
|---|
| 4210 | n/a | Py_DECREF(items); |
|---|
| 4211 | n/a | if (*dictitems == NULL) { |
|---|
| 4212 | n/a | Py_CLEAR(*listitems); |
|---|
| 4213 | n/a | return -1; |
|---|
| 4214 | n/a | } |
|---|
| 4215 | n/a | } |
|---|
| 4216 | n/a | |
|---|
| 4217 | n/a | assert(*listitems != NULL && *dictitems != NULL); |
|---|
| 4218 | n/a | |
|---|
| 4219 | n/a | return 0; |
|---|
| 4220 | n/a | } |
|---|
| 4221 | n/a | |
|---|
| 4222 | n/a | static PyObject * |
|---|
| 4223 | n/a | reduce_newobj(PyObject *obj) |
|---|
| 4224 | n/a | { |
|---|
| 4225 | n/a | PyObject *args = NULL, *kwargs = NULL; |
|---|
| 4226 | n/a | PyObject *copyreg; |
|---|
| 4227 | n/a | PyObject *newobj, *newargs, *state, *listitems, *dictitems; |
|---|
| 4228 | n/a | PyObject *result; |
|---|
| 4229 | n/a | int hasargs; |
|---|
| 4230 | n/a | |
|---|
| 4231 | n/a | if (Py_TYPE(obj)->tp_new == NULL) { |
|---|
| 4232 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 4233 | n/a | "can't pickle %.200s objects", |
|---|
| 4234 | n/a | Py_TYPE(obj)->tp_name); |
|---|
| 4235 | n/a | return NULL; |
|---|
| 4236 | n/a | } |
|---|
| 4237 | n/a | if (_PyObject_GetNewArguments(obj, &args, &kwargs) < 0) |
|---|
| 4238 | n/a | return NULL; |
|---|
| 4239 | n/a | |
|---|
| 4240 | n/a | copyreg = import_copyreg(); |
|---|
| 4241 | n/a | if (copyreg == NULL) { |
|---|
| 4242 | n/a | Py_XDECREF(args); |
|---|
| 4243 | n/a | Py_XDECREF(kwargs); |
|---|
| 4244 | n/a | return NULL; |
|---|
| 4245 | n/a | } |
|---|
| 4246 | n/a | hasargs = (args != NULL); |
|---|
| 4247 | n/a | if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) { |
|---|
| 4248 | n/a | _Py_IDENTIFIER(__newobj__); |
|---|
| 4249 | n/a | PyObject *cls; |
|---|
| 4250 | n/a | Py_ssize_t i, n; |
|---|
| 4251 | n/a | |
|---|
| 4252 | n/a | Py_XDECREF(kwargs); |
|---|
| 4253 | n/a | newobj = _PyObject_GetAttrId(copyreg, &PyId___newobj__); |
|---|
| 4254 | n/a | Py_DECREF(copyreg); |
|---|
| 4255 | n/a | if (newobj == NULL) { |
|---|
| 4256 | n/a | Py_XDECREF(args); |
|---|
| 4257 | n/a | return NULL; |
|---|
| 4258 | n/a | } |
|---|
| 4259 | n/a | n = args ? PyTuple_GET_SIZE(args) : 0; |
|---|
| 4260 | n/a | newargs = PyTuple_New(n+1); |
|---|
| 4261 | n/a | if (newargs == NULL) { |
|---|
| 4262 | n/a | Py_XDECREF(args); |
|---|
| 4263 | n/a | Py_DECREF(newobj); |
|---|
| 4264 | n/a | return NULL; |
|---|
| 4265 | n/a | } |
|---|
| 4266 | n/a | cls = (PyObject *) Py_TYPE(obj); |
|---|
| 4267 | n/a | Py_INCREF(cls); |
|---|
| 4268 | n/a | PyTuple_SET_ITEM(newargs, 0, cls); |
|---|
| 4269 | n/a | for (i = 0; i < n; i++) { |
|---|
| 4270 | n/a | PyObject *v = PyTuple_GET_ITEM(args, i); |
|---|
| 4271 | n/a | Py_INCREF(v); |
|---|
| 4272 | n/a | PyTuple_SET_ITEM(newargs, i+1, v); |
|---|
| 4273 | n/a | } |
|---|
| 4274 | n/a | Py_XDECREF(args); |
|---|
| 4275 | n/a | } |
|---|
| 4276 | n/a | else if (args != NULL) { |
|---|
| 4277 | n/a | _Py_IDENTIFIER(__newobj_ex__); |
|---|
| 4278 | n/a | |
|---|
| 4279 | n/a | newobj = _PyObject_GetAttrId(copyreg, &PyId___newobj_ex__); |
|---|
| 4280 | n/a | Py_DECREF(copyreg); |
|---|
| 4281 | n/a | if (newobj == NULL) { |
|---|
| 4282 | n/a | Py_DECREF(args); |
|---|
| 4283 | n/a | Py_DECREF(kwargs); |
|---|
| 4284 | n/a | return NULL; |
|---|
| 4285 | n/a | } |
|---|
| 4286 | n/a | newargs = PyTuple_Pack(3, Py_TYPE(obj), args, kwargs); |
|---|
| 4287 | n/a | Py_DECREF(args); |
|---|
| 4288 | n/a | Py_DECREF(kwargs); |
|---|
| 4289 | n/a | if (newargs == NULL) { |
|---|
| 4290 | n/a | Py_DECREF(newobj); |
|---|
| 4291 | n/a | return NULL; |
|---|
| 4292 | n/a | } |
|---|
| 4293 | n/a | } |
|---|
| 4294 | n/a | else { |
|---|
| 4295 | n/a | /* args == NULL */ |
|---|
| 4296 | n/a | Py_DECREF(kwargs); |
|---|
| 4297 | n/a | PyErr_BadInternalCall(); |
|---|
| 4298 | n/a | return NULL; |
|---|
| 4299 | n/a | } |
|---|
| 4300 | n/a | |
|---|
| 4301 | n/a | state = _PyObject_GetState(obj, |
|---|
| 4302 | n/a | !hasargs && !PyList_Check(obj) && !PyDict_Check(obj)); |
|---|
| 4303 | n/a | if (state == NULL) { |
|---|
| 4304 | n/a | Py_DECREF(newobj); |
|---|
| 4305 | n/a | Py_DECREF(newargs); |
|---|
| 4306 | n/a | return NULL; |
|---|
| 4307 | n/a | } |
|---|
| 4308 | n/a | if (_PyObject_GetItemsIter(obj, &listitems, &dictitems) < 0) { |
|---|
| 4309 | n/a | Py_DECREF(newobj); |
|---|
| 4310 | n/a | Py_DECREF(newargs); |
|---|
| 4311 | n/a | Py_DECREF(state); |
|---|
| 4312 | n/a | return NULL; |
|---|
| 4313 | n/a | } |
|---|
| 4314 | n/a | |
|---|
| 4315 | n/a | result = PyTuple_Pack(5, newobj, newargs, state, listitems, dictitems); |
|---|
| 4316 | n/a | Py_DECREF(newobj); |
|---|
| 4317 | n/a | Py_DECREF(newargs); |
|---|
| 4318 | n/a | Py_DECREF(state); |
|---|
| 4319 | n/a | Py_DECREF(listitems); |
|---|
| 4320 | n/a | Py_DECREF(dictitems); |
|---|
| 4321 | n/a | return result; |
|---|
| 4322 | n/a | } |
|---|
| 4323 | n/a | |
|---|
| 4324 | n/a | /* |
|---|
| 4325 | n/a | * There were two problems when object.__reduce__ and object.__reduce_ex__ |
|---|
| 4326 | n/a | * were implemented in the same function: |
|---|
| 4327 | n/a | * - trying to pickle an object with a custom __reduce__ method that |
|---|
| 4328 | n/a | * fell back to object.__reduce__ in certain circumstances led to |
|---|
| 4329 | n/a | * infinite recursion at Python level and eventual RecursionError. |
|---|
| 4330 | n/a | * - Pickling objects that lied about their type by overwriting the |
|---|
| 4331 | n/a | * __class__ descriptor could lead to infinite recursion at C level |
|---|
| 4332 | n/a | * and eventual segfault. |
|---|
| 4333 | n/a | * |
|---|
| 4334 | n/a | * Because of backwards compatibility, the two methods still have to |
|---|
| 4335 | n/a | * behave in the same way, even if this is not required by the pickle |
|---|
| 4336 | n/a | * protocol. This common functionality was moved to the _common_reduce |
|---|
| 4337 | n/a | * function. |
|---|
| 4338 | n/a | */ |
|---|
| 4339 | n/a | static PyObject * |
|---|
| 4340 | n/a | _common_reduce(PyObject *self, int proto) |
|---|
| 4341 | n/a | { |
|---|
| 4342 | n/a | PyObject *copyreg, *res; |
|---|
| 4343 | n/a | |
|---|
| 4344 | n/a | if (proto >= 2) |
|---|
| 4345 | n/a | return reduce_newobj(self); |
|---|
| 4346 | n/a | |
|---|
| 4347 | n/a | copyreg = import_copyreg(); |
|---|
| 4348 | n/a | if (!copyreg) |
|---|
| 4349 | n/a | return NULL; |
|---|
| 4350 | n/a | |
|---|
| 4351 | n/a | res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto); |
|---|
| 4352 | n/a | Py_DECREF(copyreg); |
|---|
| 4353 | n/a | |
|---|
| 4354 | n/a | return res; |
|---|
| 4355 | n/a | } |
|---|
| 4356 | n/a | |
|---|
| 4357 | n/a | static PyObject * |
|---|
| 4358 | n/a | object_reduce(PyObject *self, PyObject *args) |
|---|
| 4359 | n/a | { |
|---|
| 4360 | n/a | int proto = 0; |
|---|
| 4361 | n/a | |
|---|
| 4362 | n/a | if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto)) |
|---|
| 4363 | n/a | return NULL; |
|---|
| 4364 | n/a | |
|---|
| 4365 | n/a | return _common_reduce(self, proto); |
|---|
| 4366 | n/a | } |
|---|
| 4367 | n/a | |
|---|
| 4368 | n/a | static PyObject * |
|---|
| 4369 | n/a | object_reduce_ex(PyObject *self, PyObject *args) |
|---|
| 4370 | n/a | { |
|---|
| 4371 | n/a | static PyObject *objreduce; |
|---|
| 4372 | n/a | PyObject *reduce, *res; |
|---|
| 4373 | n/a | int proto = 0; |
|---|
| 4374 | n/a | _Py_IDENTIFIER(__reduce__); |
|---|
| 4375 | n/a | |
|---|
| 4376 | n/a | if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto)) |
|---|
| 4377 | n/a | return NULL; |
|---|
| 4378 | n/a | |
|---|
| 4379 | n/a | if (objreduce == NULL) { |
|---|
| 4380 | n/a | objreduce = _PyDict_GetItemId(PyBaseObject_Type.tp_dict, |
|---|
| 4381 | n/a | &PyId___reduce__); |
|---|
| 4382 | n/a | if (objreduce == NULL) |
|---|
| 4383 | n/a | return NULL; |
|---|
| 4384 | n/a | } |
|---|
| 4385 | n/a | |
|---|
| 4386 | n/a | reduce = _PyObject_GetAttrId(self, &PyId___reduce__); |
|---|
| 4387 | n/a | if (reduce == NULL) |
|---|
| 4388 | n/a | PyErr_Clear(); |
|---|
| 4389 | n/a | else { |
|---|
| 4390 | n/a | PyObject *cls, *clsreduce; |
|---|
| 4391 | n/a | int override; |
|---|
| 4392 | n/a | |
|---|
| 4393 | n/a | cls = (PyObject *) Py_TYPE(self); |
|---|
| 4394 | n/a | clsreduce = _PyObject_GetAttrId(cls, &PyId___reduce__); |
|---|
| 4395 | n/a | if (clsreduce == NULL) { |
|---|
| 4396 | n/a | Py_DECREF(reduce); |
|---|
| 4397 | n/a | return NULL; |
|---|
| 4398 | n/a | } |
|---|
| 4399 | n/a | override = (clsreduce != objreduce); |
|---|
| 4400 | n/a | Py_DECREF(clsreduce); |
|---|
| 4401 | n/a | if (override) { |
|---|
| 4402 | n/a | res = _PyObject_CallNoArg(reduce); |
|---|
| 4403 | n/a | Py_DECREF(reduce); |
|---|
| 4404 | n/a | return res; |
|---|
| 4405 | n/a | } |
|---|
| 4406 | n/a | else |
|---|
| 4407 | n/a | Py_DECREF(reduce); |
|---|
| 4408 | n/a | } |
|---|
| 4409 | n/a | |
|---|
| 4410 | n/a | return _common_reduce(self, proto); |
|---|
| 4411 | n/a | } |
|---|
| 4412 | n/a | |
|---|
| 4413 | n/a | static PyObject * |
|---|
| 4414 | n/a | object_subclasshook(PyObject *cls, PyObject *args) |
|---|
| 4415 | n/a | { |
|---|
| 4416 | n/a | Py_RETURN_NOTIMPLEMENTED; |
|---|
| 4417 | n/a | } |
|---|
| 4418 | n/a | |
|---|
| 4419 | n/a | PyDoc_STRVAR(object_subclasshook_doc, |
|---|
| 4420 | n/a | "Abstract classes can override this to customize issubclass().\n" |
|---|
| 4421 | n/a | "\n" |
|---|
| 4422 | n/a | "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n" |
|---|
| 4423 | n/a | "It should return True, False or NotImplemented. If it returns\n" |
|---|
| 4424 | n/a | "NotImplemented, the normal algorithm is used. Otherwise, it\n" |
|---|
| 4425 | n/a | "overrides the normal algorithm (and the outcome is cached).\n"); |
|---|
| 4426 | n/a | |
|---|
| 4427 | n/a | static PyObject * |
|---|
| 4428 | n/a | object_init_subclass(PyObject *cls, PyObject *arg) |
|---|
| 4429 | n/a | { |
|---|
| 4430 | n/a | Py_RETURN_NONE; |
|---|
| 4431 | n/a | } |
|---|
| 4432 | n/a | |
|---|
| 4433 | n/a | PyDoc_STRVAR(object_init_subclass_doc, |
|---|
| 4434 | n/a | "This method is called when a class is subclassed.\n" |
|---|
| 4435 | n/a | "\n" |
|---|
| 4436 | n/a | "The default implementation does nothing. It may be\n" |
|---|
| 4437 | n/a | "overridden to extend subclasses.\n"); |
|---|
| 4438 | n/a | |
|---|
| 4439 | n/a | static PyObject * |
|---|
| 4440 | n/a | object_format(PyObject *self, PyObject *args) |
|---|
| 4441 | n/a | { |
|---|
| 4442 | n/a | PyObject *format_spec; |
|---|
| 4443 | n/a | PyObject *self_as_str = NULL; |
|---|
| 4444 | n/a | PyObject *result = NULL; |
|---|
| 4445 | n/a | |
|---|
| 4446 | n/a | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
|---|
| 4447 | n/a | return NULL; |
|---|
| 4448 | n/a | |
|---|
| 4449 | n/a | /* Issue 7994: If we're converting to a string, we |
|---|
| 4450 | n/a | should reject format specifications */ |
|---|
| 4451 | n/a | if (PyUnicode_GET_LENGTH(format_spec) > 0) { |
|---|
| 4452 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 4453 | n/a | "unsupported format string passed to %.200s.__format__", |
|---|
| 4454 | n/a | self->ob_type->tp_name); |
|---|
| 4455 | n/a | return NULL; |
|---|
| 4456 | n/a | } |
|---|
| 4457 | n/a | self_as_str = PyObject_Str(self); |
|---|
| 4458 | n/a | if (self_as_str != NULL) { |
|---|
| 4459 | n/a | result = PyObject_Format(self_as_str, format_spec); |
|---|
| 4460 | n/a | Py_DECREF(self_as_str); |
|---|
| 4461 | n/a | } |
|---|
| 4462 | n/a | return result; |
|---|
| 4463 | n/a | } |
|---|
| 4464 | n/a | |
|---|
| 4465 | n/a | static PyObject * |
|---|
| 4466 | n/a | object_sizeof(PyObject *self, PyObject *args) |
|---|
| 4467 | n/a | { |
|---|
| 4468 | n/a | Py_ssize_t res, isize; |
|---|
| 4469 | n/a | |
|---|
| 4470 | n/a | res = 0; |
|---|
| 4471 | n/a | isize = self->ob_type->tp_itemsize; |
|---|
| 4472 | n/a | if (isize > 0) |
|---|
| 4473 | n/a | res = Py_SIZE(self) * isize; |
|---|
| 4474 | n/a | res += self->ob_type->tp_basicsize; |
|---|
| 4475 | n/a | |
|---|
| 4476 | n/a | return PyLong_FromSsize_t(res); |
|---|
| 4477 | n/a | } |
|---|
| 4478 | n/a | |
|---|
| 4479 | n/a | /* __dir__ for generic objects: returns __dict__, __class__, |
|---|
| 4480 | n/a | and recursively up the __class__.__bases__ chain. |
|---|
| 4481 | n/a | */ |
|---|
| 4482 | n/a | static PyObject * |
|---|
| 4483 | n/a | object_dir(PyObject *self, PyObject *args) |
|---|
| 4484 | n/a | { |
|---|
| 4485 | n/a | PyObject *result = NULL; |
|---|
| 4486 | n/a | PyObject *dict = NULL; |
|---|
| 4487 | n/a | PyObject *itsclass = NULL; |
|---|
| 4488 | n/a | |
|---|
| 4489 | n/a | /* Get __dict__ (which may or may not be a real dict...) */ |
|---|
| 4490 | n/a | dict = _PyObject_GetAttrId(self, &PyId___dict__); |
|---|
| 4491 | n/a | if (dict == NULL) { |
|---|
| 4492 | n/a | PyErr_Clear(); |
|---|
| 4493 | n/a | dict = PyDict_New(); |
|---|
| 4494 | n/a | } |
|---|
| 4495 | n/a | else if (!PyDict_Check(dict)) { |
|---|
| 4496 | n/a | Py_DECREF(dict); |
|---|
| 4497 | n/a | dict = PyDict_New(); |
|---|
| 4498 | n/a | } |
|---|
| 4499 | n/a | else { |
|---|
| 4500 | n/a | /* Copy __dict__ to avoid mutating it. */ |
|---|
| 4501 | n/a | PyObject *temp = PyDict_Copy(dict); |
|---|
| 4502 | n/a | Py_DECREF(dict); |
|---|
| 4503 | n/a | dict = temp; |
|---|
| 4504 | n/a | } |
|---|
| 4505 | n/a | |
|---|
| 4506 | n/a | if (dict == NULL) |
|---|
| 4507 | n/a | goto error; |
|---|
| 4508 | n/a | |
|---|
| 4509 | n/a | /* Merge in attrs reachable from its class. */ |
|---|
| 4510 | n/a | itsclass = _PyObject_GetAttrId(self, &PyId___class__); |
|---|
| 4511 | n/a | if (itsclass == NULL) |
|---|
| 4512 | n/a | /* XXX(tomer): Perhaps fall back to obj->ob_type if no |
|---|
| 4513 | n/a | __class__ exists? */ |
|---|
| 4514 | n/a | PyErr_Clear(); |
|---|
| 4515 | n/a | else if (merge_class_dict(dict, itsclass) != 0) |
|---|
| 4516 | n/a | goto error; |
|---|
| 4517 | n/a | |
|---|
| 4518 | n/a | result = PyDict_Keys(dict); |
|---|
| 4519 | n/a | /* fall through */ |
|---|
| 4520 | n/a | error: |
|---|
| 4521 | n/a | Py_XDECREF(itsclass); |
|---|
| 4522 | n/a | Py_XDECREF(dict); |
|---|
| 4523 | n/a | return result; |
|---|
| 4524 | n/a | } |
|---|
| 4525 | n/a | |
|---|
| 4526 | n/a | static PyMethodDef object_methods[] = { |
|---|
| 4527 | n/a | {"__reduce_ex__", object_reduce_ex, METH_VARARGS, |
|---|
| 4528 | n/a | PyDoc_STR("helper for pickle")}, |
|---|
| 4529 | n/a | {"__reduce__", object_reduce, METH_VARARGS, |
|---|
| 4530 | n/a | PyDoc_STR("helper for pickle")}, |
|---|
| 4531 | n/a | {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS, |
|---|
| 4532 | n/a | object_subclasshook_doc}, |
|---|
| 4533 | n/a | {"__init_subclass__", object_init_subclass, METH_CLASS | METH_NOARGS, |
|---|
| 4534 | n/a | object_init_subclass_doc}, |
|---|
| 4535 | n/a | {"__format__", object_format, METH_VARARGS, |
|---|
| 4536 | n/a | PyDoc_STR("default object formatter")}, |
|---|
| 4537 | n/a | {"__sizeof__", object_sizeof, METH_NOARGS, |
|---|
| 4538 | n/a | PyDoc_STR("__sizeof__() -> int\nsize of object in memory, in bytes")}, |
|---|
| 4539 | n/a | {"__dir__", object_dir, METH_NOARGS, |
|---|
| 4540 | n/a | PyDoc_STR("__dir__() -> list\ndefault dir() implementation")}, |
|---|
| 4541 | n/a | {0} |
|---|
| 4542 | n/a | }; |
|---|
| 4543 | n/a | |
|---|
| 4544 | n/a | |
|---|
| 4545 | n/a | PyTypeObject PyBaseObject_Type = { |
|---|
| 4546 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|---|
| 4547 | n/a | "object", /* tp_name */ |
|---|
| 4548 | n/a | sizeof(PyObject), /* tp_basicsize */ |
|---|
| 4549 | n/a | 0, /* tp_itemsize */ |
|---|
| 4550 | n/a | object_dealloc, /* tp_dealloc */ |
|---|
| 4551 | n/a | 0, /* tp_print */ |
|---|
| 4552 | n/a | 0, /* tp_getattr */ |
|---|
| 4553 | n/a | 0, /* tp_setattr */ |
|---|
| 4554 | n/a | 0, /* tp_reserved */ |
|---|
| 4555 | n/a | object_repr, /* tp_repr */ |
|---|
| 4556 | n/a | 0, /* tp_as_number */ |
|---|
| 4557 | n/a | 0, /* tp_as_sequence */ |
|---|
| 4558 | n/a | 0, /* tp_as_mapping */ |
|---|
| 4559 | n/a | (hashfunc)_Py_HashPointer, /* tp_hash */ |
|---|
| 4560 | n/a | 0, /* tp_call */ |
|---|
| 4561 | n/a | object_str, /* tp_str */ |
|---|
| 4562 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 4563 | n/a | PyObject_GenericSetAttr, /* tp_setattro */ |
|---|
| 4564 | n/a | 0, /* tp_as_buffer */ |
|---|
| 4565 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 4566 | n/a | PyDoc_STR("object()\n--\n\nThe most base type"), /* tp_doc */ |
|---|
| 4567 | n/a | 0, /* tp_traverse */ |
|---|
| 4568 | n/a | 0, /* tp_clear */ |
|---|
| 4569 | n/a | object_richcompare, /* tp_richcompare */ |
|---|
| 4570 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 4571 | n/a | 0, /* tp_iter */ |
|---|
| 4572 | n/a | 0, /* tp_iternext */ |
|---|
| 4573 | n/a | object_methods, /* tp_methods */ |
|---|
| 4574 | n/a | 0, /* tp_members */ |
|---|
| 4575 | n/a | object_getsets, /* tp_getset */ |
|---|
| 4576 | n/a | 0, /* tp_base */ |
|---|
| 4577 | n/a | 0, /* tp_dict */ |
|---|
| 4578 | n/a | 0, /* tp_descr_get */ |
|---|
| 4579 | n/a | 0, /* tp_descr_set */ |
|---|
| 4580 | n/a | 0, /* tp_dictoffset */ |
|---|
| 4581 | n/a | object_init, /* tp_init */ |
|---|
| 4582 | n/a | PyType_GenericAlloc, /* tp_alloc */ |
|---|
| 4583 | n/a | object_new, /* tp_new */ |
|---|
| 4584 | n/a | PyObject_Del, /* tp_free */ |
|---|
| 4585 | n/a | }; |
|---|
| 4586 | n/a | |
|---|
| 4587 | n/a | |
|---|
| 4588 | n/a | /* Add the methods from tp_methods to the __dict__ in a type object */ |
|---|
| 4589 | n/a | |
|---|
| 4590 | n/a | static int |
|---|
| 4591 | n/a | add_methods(PyTypeObject *type, PyMethodDef *meth) |
|---|
| 4592 | n/a | { |
|---|
| 4593 | n/a | PyObject *dict = type->tp_dict; |
|---|
| 4594 | n/a | |
|---|
| 4595 | n/a | for (; meth->ml_name != NULL; meth++) { |
|---|
| 4596 | n/a | PyObject *descr; |
|---|
| 4597 | n/a | int err; |
|---|
| 4598 | n/a | int isdescr = 1; |
|---|
| 4599 | n/a | if (PyDict_GetItemString(dict, meth->ml_name) && |
|---|
| 4600 | n/a | !(meth->ml_flags & METH_COEXIST)) |
|---|
| 4601 | n/a | continue; |
|---|
| 4602 | n/a | if (meth->ml_flags & METH_CLASS) { |
|---|
| 4603 | n/a | if (meth->ml_flags & METH_STATIC) { |
|---|
| 4604 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 4605 | n/a | "method cannot be both class and static"); |
|---|
| 4606 | n/a | return -1; |
|---|
| 4607 | n/a | } |
|---|
| 4608 | n/a | descr = PyDescr_NewClassMethod(type, meth); |
|---|
| 4609 | n/a | } |
|---|
| 4610 | n/a | else if (meth->ml_flags & METH_STATIC) { |
|---|
| 4611 | n/a | PyObject *cfunc = PyCFunction_NewEx(meth, (PyObject*)type, NULL); |
|---|
| 4612 | n/a | if (cfunc == NULL) |
|---|
| 4613 | n/a | return -1; |
|---|
| 4614 | n/a | descr = PyStaticMethod_New(cfunc); |
|---|
| 4615 | n/a | isdescr = 0; // PyStaticMethod is not PyDescrObject |
|---|
| 4616 | n/a | Py_DECREF(cfunc); |
|---|
| 4617 | n/a | } |
|---|
| 4618 | n/a | else { |
|---|
| 4619 | n/a | descr = PyDescr_NewMethod(type, meth); |
|---|
| 4620 | n/a | } |
|---|
| 4621 | n/a | if (descr == NULL) |
|---|
| 4622 | n/a | return -1; |
|---|
| 4623 | n/a | if (isdescr) { |
|---|
| 4624 | n/a | err = PyDict_SetItem(dict, PyDescr_NAME(descr), descr); |
|---|
| 4625 | n/a | } |
|---|
| 4626 | n/a | else { |
|---|
| 4627 | n/a | err = PyDict_SetItemString(dict, meth->ml_name, descr); |
|---|
| 4628 | n/a | } |
|---|
| 4629 | n/a | Py_DECREF(descr); |
|---|
| 4630 | n/a | if (err < 0) |
|---|
| 4631 | n/a | return -1; |
|---|
| 4632 | n/a | } |
|---|
| 4633 | n/a | return 0; |
|---|
| 4634 | n/a | } |
|---|
| 4635 | n/a | |
|---|
| 4636 | n/a | static int |
|---|
| 4637 | n/a | add_members(PyTypeObject *type, PyMemberDef *memb) |
|---|
| 4638 | n/a | { |
|---|
| 4639 | n/a | PyObject *dict = type->tp_dict; |
|---|
| 4640 | n/a | |
|---|
| 4641 | n/a | for (; memb->name != NULL; memb++) { |
|---|
| 4642 | n/a | PyObject *descr; |
|---|
| 4643 | n/a | if (PyDict_GetItemString(dict, memb->name)) |
|---|
| 4644 | n/a | continue; |
|---|
| 4645 | n/a | descr = PyDescr_NewMember(type, memb); |
|---|
| 4646 | n/a | if (descr == NULL) |
|---|
| 4647 | n/a | return -1; |
|---|
| 4648 | n/a | if (PyDict_SetItem(dict, PyDescr_NAME(descr), descr) < 0) { |
|---|
| 4649 | n/a | Py_DECREF(descr); |
|---|
| 4650 | n/a | return -1; |
|---|
| 4651 | n/a | } |
|---|
| 4652 | n/a | Py_DECREF(descr); |
|---|
| 4653 | n/a | } |
|---|
| 4654 | n/a | return 0; |
|---|
| 4655 | n/a | } |
|---|
| 4656 | n/a | |
|---|
| 4657 | n/a | static int |
|---|
| 4658 | n/a | add_getset(PyTypeObject *type, PyGetSetDef *gsp) |
|---|
| 4659 | n/a | { |
|---|
| 4660 | n/a | PyObject *dict = type->tp_dict; |
|---|
| 4661 | n/a | |
|---|
| 4662 | n/a | for (; gsp->name != NULL; gsp++) { |
|---|
| 4663 | n/a | PyObject *descr; |
|---|
| 4664 | n/a | if (PyDict_GetItemString(dict, gsp->name)) |
|---|
| 4665 | n/a | continue; |
|---|
| 4666 | n/a | descr = PyDescr_NewGetSet(type, gsp); |
|---|
| 4667 | n/a | |
|---|
| 4668 | n/a | if (descr == NULL) |
|---|
| 4669 | n/a | return -1; |
|---|
| 4670 | n/a | if (PyDict_SetItem(dict, PyDescr_NAME(descr), descr) < 0) { |
|---|
| 4671 | n/a | Py_DECREF(descr); |
|---|
| 4672 | n/a | return -1; |
|---|
| 4673 | n/a | } |
|---|
| 4674 | n/a | Py_DECREF(descr); |
|---|
| 4675 | n/a | } |
|---|
| 4676 | n/a | return 0; |
|---|
| 4677 | n/a | } |
|---|
| 4678 | n/a | |
|---|
| 4679 | n/a | static void |
|---|
| 4680 | n/a | inherit_special(PyTypeObject *type, PyTypeObject *base) |
|---|
| 4681 | n/a | { |
|---|
| 4682 | n/a | |
|---|
| 4683 | n/a | /* Copying basicsize is connected to the GC flags */ |
|---|
| 4684 | n/a | if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
|---|
| 4685 | n/a | (base->tp_flags & Py_TPFLAGS_HAVE_GC) && |
|---|
| 4686 | n/a | (!type->tp_traverse && !type->tp_clear)) { |
|---|
| 4687 | n/a | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
|---|
| 4688 | n/a | if (type->tp_traverse == NULL) |
|---|
| 4689 | n/a | type->tp_traverse = base->tp_traverse; |
|---|
| 4690 | n/a | if (type->tp_clear == NULL) |
|---|
| 4691 | n/a | type->tp_clear = base->tp_clear; |
|---|
| 4692 | n/a | } |
|---|
| 4693 | n/a | { |
|---|
| 4694 | n/a | /* The condition below could use some explanation. |
|---|
| 4695 | n/a | It appears that tp_new is not inherited for static types |
|---|
| 4696 | n/a | whose base class is 'object'; this seems to be a precaution |
|---|
| 4697 | n/a | so that old extension types don't suddenly become |
|---|
| 4698 | n/a | callable (object.__new__ wouldn't insure the invariants |
|---|
| 4699 | n/a | that the extension type's own factory function ensures). |
|---|
| 4700 | n/a | Heap types, of course, are under our control, so they do |
|---|
| 4701 | n/a | inherit tp_new; static extension types that specify some |
|---|
| 4702 | n/a | other built-in type as the default also |
|---|
| 4703 | n/a | inherit object.__new__. */ |
|---|
| 4704 | n/a | if (base != &PyBaseObject_Type || |
|---|
| 4705 | n/a | (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
|---|
| 4706 | n/a | if (type->tp_new == NULL) |
|---|
| 4707 | n/a | type->tp_new = base->tp_new; |
|---|
| 4708 | n/a | } |
|---|
| 4709 | n/a | } |
|---|
| 4710 | n/a | if (type->tp_basicsize == 0) |
|---|
| 4711 | n/a | type->tp_basicsize = base->tp_basicsize; |
|---|
| 4712 | n/a | |
|---|
| 4713 | n/a | /* Copy other non-function slots */ |
|---|
| 4714 | n/a | |
|---|
| 4715 | n/a | #undef COPYVAL |
|---|
| 4716 | n/a | #define COPYVAL(SLOT) \ |
|---|
| 4717 | n/a | if (type->SLOT == 0) type->SLOT = base->SLOT |
|---|
| 4718 | n/a | |
|---|
| 4719 | n/a | COPYVAL(tp_itemsize); |
|---|
| 4720 | n/a | COPYVAL(tp_weaklistoffset); |
|---|
| 4721 | n/a | COPYVAL(tp_dictoffset); |
|---|
| 4722 | n/a | |
|---|
| 4723 | n/a | /* Setup fast subclass flags */ |
|---|
| 4724 | n/a | if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException)) |
|---|
| 4725 | n/a | type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS; |
|---|
| 4726 | n/a | else if (PyType_IsSubtype(base, &PyType_Type)) |
|---|
| 4727 | n/a | type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS; |
|---|
| 4728 | n/a | else if (PyType_IsSubtype(base, &PyLong_Type)) |
|---|
| 4729 | n/a | type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS; |
|---|
| 4730 | n/a | else if (PyType_IsSubtype(base, &PyBytes_Type)) |
|---|
| 4731 | n/a | type->tp_flags |= Py_TPFLAGS_BYTES_SUBCLASS; |
|---|
| 4732 | n/a | else if (PyType_IsSubtype(base, &PyUnicode_Type)) |
|---|
| 4733 | n/a | type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS; |
|---|
| 4734 | n/a | else if (PyType_IsSubtype(base, &PyTuple_Type)) |
|---|
| 4735 | n/a | type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS; |
|---|
| 4736 | n/a | else if (PyType_IsSubtype(base, &PyList_Type)) |
|---|
| 4737 | n/a | type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS; |
|---|
| 4738 | n/a | else if (PyType_IsSubtype(base, &PyDict_Type)) |
|---|
| 4739 | n/a | type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS; |
|---|
| 4740 | n/a | } |
|---|
| 4741 | n/a | |
|---|
| 4742 | n/a | static int |
|---|
| 4743 | n/a | overrides_hash(PyTypeObject *type) |
|---|
| 4744 | n/a | { |
|---|
| 4745 | n/a | PyObject *dict = type->tp_dict; |
|---|
| 4746 | n/a | _Py_IDENTIFIER(__eq__); |
|---|
| 4747 | n/a | |
|---|
| 4748 | n/a | assert(dict != NULL); |
|---|
| 4749 | n/a | if (_PyDict_GetItemId(dict, &PyId___eq__) != NULL) |
|---|
| 4750 | n/a | return 1; |
|---|
| 4751 | n/a | if (_PyDict_GetItemId(dict, &PyId___hash__) != NULL) |
|---|
| 4752 | n/a | return 1; |
|---|
| 4753 | n/a | return 0; |
|---|
| 4754 | n/a | } |
|---|
| 4755 | n/a | |
|---|
| 4756 | n/a | static void |
|---|
| 4757 | n/a | inherit_slots(PyTypeObject *type, PyTypeObject *base) |
|---|
| 4758 | n/a | { |
|---|
| 4759 | n/a | PyTypeObject *basebase; |
|---|
| 4760 | n/a | |
|---|
| 4761 | n/a | #undef SLOTDEFINED |
|---|
| 4762 | n/a | #undef COPYSLOT |
|---|
| 4763 | n/a | #undef COPYNUM |
|---|
| 4764 | n/a | #undef COPYSEQ |
|---|
| 4765 | n/a | #undef COPYMAP |
|---|
| 4766 | n/a | #undef COPYBUF |
|---|
| 4767 | n/a | |
|---|
| 4768 | n/a | #define SLOTDEFINED(SLOT) \ |
|---|
| 4769 | n/a | (base->SLOT != 0 && \ |
|---|
| 4770 | n/a | (basebase == NULL || base->SLOT != basebase->SLOT)) |
|---|
| 4771 | n/a | |
|---|
| 4772 | n/a | #define COPYSLOT(SLOT) \ |
|---|
| 4773 | n/a | if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT |
|---|
| 4774 | n/a | |
|---|
| 4775 | n/a | #define COPYASYNC(SLOT) COPYSLOT(tp_as_async->SLOT) |
|---|
| 4776 | n/a | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT) |
|---|
| 4777 | n/a | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT) |
|---|
| 4778 | n/a | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT) |
|---|
| 4779 | n/a | #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT) |
|---|
| 4780 | n/a | |
|---|
| 4781 | n/a | /* This won't inherit indirect slots (from tp_as_number etc.) |
|---|
| 4782 | n/a | if type doesn't provide the space. */ |
|---|
| 4783 | n/a | |
|---|
| 4784 | n/a | if (type->tp_as_number != NULL && base->tp_as_number != NULL) { |
|---|
| 4785 | n/a | basebase = base->tp_base; |
|---|
| 4786 | n/a | if (basebase->tp_as_number == NULL) |
|---|
| 4787 | n/a | basebase = NULL; |
|---|
| 4788 | n/a | COPYNUM(nb_add); |
|---|
| 4789 | n/a | COPYNUM(nb_subtract); |
|---|
| 4790 | n/a | COPYNUM(nb_multiply); |
|---|
| 4791 | n/a | COPYNUM(nb_remainder); |
|---|
| 4792 | n/a | COPYNUM(nb_divmod); |
|---|
| 4793 | n/a | COPYNUM(nb_power); |
|---|
| 4794 | n/a | COPYNUM(nb_negative); |
|---|
| 4795 | n/a | COPYNUM(nb_positive); |
|---|
| 4796 | n/a | COPYNUM(nb_absolute); |
|---|
| 4797 | n/a | COPYNUM(nb_bool); |
|---|
| 4798 | n/a | COPYNUM(nb_invert); |
|---|
| 4799 | n/a | COPYNUM(nb_lshift); |
|---|
| 4800 | n/a | COPYNUM(nb_rshift); |
|---|
| 4801 | n/a | COPYNUM(nb_and); |
|---|
| 4802 | n/a | COPYNUM(nb_xor); |
|---|
| 4803 | n/a | COPYNUM(nb_or); |
|---|
| 4804 | n/a | COPYNUM(nb_int); |
|---|
| 4805 | n/a | COPYNUM(nb_float); |
|---|
| 4806 | n/a | COPYNUM(nb_inplace_add); |
|---|
| 4807 | n/a | COPYNUM(nb_inplace_subtract); |
|---|
| 4808 | n/a | COPYNUM(nb_inplace_multiply); |
|---|
| 4809 | n/a | COPYNUM(nb_inplace_remainder); |
|---|
| 4810 | n/a | COPYNUM(nb_inplace_power); |
|---|
| 4811 | n/a | COPYNUM(nb_inplace_lshift); |
|---|
| 4812 | n/a | COPYNUM(nb_inplace_rshift); |
|---|
| 4813 | n/a | COPYNUM(nb_inplace_and); |
|---|
| 4814 | n/a | COPYNUM(nb_inplace_xor); |
|---|
| 4815 | n/a | COPYNUM(nb_inplace_or); |
|---|
| 4816 | n/a | COPYNUM(nb_true_divide); |
|---|
| 4817 | n/a | COPYNUM(nb_floor_divide); |
|---|
| 4818 | n/a | COPYNUM(nb_inplace_true_divide); |
|---|
| 4819 | n/a | COPYNUM(nb_inplace_floor_divide); |
|---|
| 4820 | n/a | COPYNUM(nb_index); |
|---|
| 4821 | n/a | COPYNUM(nb_matrix_multiply); |
|---|
| 4822 | n/a | COPYNUM(nb_inplace_matrix_multiply); |
|---|
| 4823 | n/a | } |
|---|
| 4824 | n/a | |
|---|
| 4825 | n/a | if (type->tp_as_async != NULL && base->tp_as_async != NULL) { |
|---|
| 4826 | n/a | basebase = base->tp_base; |
|---|
| 4827 | n/a | if (basebase->tp_as_async == NULL) |
|---|
| 4828 | n/a | basebase = NULL; |
|---|
| 4829 | n/a | COPYASYNC(am_await); |
|---|
| 4830 | n/a | COPYASYNC(am_aiter); |
|---|
| 4831 | n/a | COPYASYNC(am_anext); |
|---|
| 4832 | n/a | } |
|---|
| 4833 | n/a | |
|---|
| 4834 | n/a | if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) { |
|---|
| 4835 | n/a | basebase = base->tp_base; |
|---|
| 4836 | n/a | if (basebase->tp_as_sequence == NULL) |
|---|
| 4837 | n/a | basebase = NULL; |
|---|
| 4838 | n/a | COPYSEQ(sq_length); |
|---|
| 4839 | n/a | COPYSEQ(sq_concat); |
|---|
| 4840 | n/a | COPYSEQ(sq_repeat); |
|---|
| 4841 | n/a | COPYSEQ(sq_item); |
|---|
| 4842 | n/a | COPYSEQ(sq_ass_item); |
|---|
| 4843 | n/a | COPYSEQ(sq_contains); |
|---|
| 4844 | n/a | COPYSEQ(sq_inplace_concat); |
|---|
| 4845 | n/a | COPYSEQ(sq_inplace_repeat); |
|---|
| 4846 | n/a | } |
|---|
| 4847 | n/a | |
|---|
| 4848 | n/a | if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) { |
|---|
| 4849 | n/a | basebase = base->tp_base; |
|---|
| 4850 | n/a | if (basebase->tp_as_mapping == NULL) |
|---|
| 4851 | n/a | basebase = NULL; |
|---|
| 4852 | n/a | COPYMAP(mp_length); |
|---|
| 4853 | n/a | COPYMAP(mp_subscript); |
|---|
| 4854 | n/a | COPYMAP(mp_ass_subscript); |
|---|
| 4855 | n/a | } |
|---|
| 4856 | n/a | |
|---|
| 4857 | n/a | if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) { |
|---|
| 4858 | n/a | basebase = base->tp_base; |
|---|
| 4859 | n/a | if (basebase->tp_as_buffer == NULL) |
|---|
| 4860 | n/a | basebase = NULL; |
|---|
| 4861 | n/a | COPYBUF(bf_getbuffer); |
|---|
| 4862 | n/a | COPYBUF(bf_releasebuffer); |
|---|
| 4863 | n/a | } |
|---|
| 4864 | n/a | |
|---|
| 4865 | n/a | basebase = base->tp_base; |
|---|
| 4866 | n/a | |
|---|
| 4867 | n/a | COPYSLOT(tp_dealloc); |
|---|
| 4868 | n/a | if (type->tp_getattr == NULL && type->tp_getattro == NULL) { |
|---|
| 4869 | n/a | type->tp_getattr = base->tp_getattr; |
|---|
| 4870 | n/a | type->tp_getattro = base->tp_getattro; |
|---|
| 4871 | n/a | } |
|---|
| 4872 | n/a | if (type->tp_setattr == NULL && type->tp_setattro == NULL) { |
|---|
| 4873 | n/a | type->tp_setattr = base->tp_setattr; |
|---|
| 4874 | n/a | type->tp_setattro = base->tp_setattro; |
|---|
| 4875 | n/a | } |
|---|
| 4876 | n/a | /* tp_reserved is ignored */ |
|---|
| 4877 | n/a | COPYSLOT(tp_repr); |
|---|
| 4878 | n/a | /* tp_hash see tp_richcompare */ |
|---|
| 4879 | n/a | COPYSLOT(tp_call); |
|---|
| 4880 | n/a | COPYSLOT(tp_str); |
|---|
| 4881 | n/a | { |
|---|
| 4882 | n/a | /* Copy comparison-related slots only when |
|---|
| 4883 | n/a | not overriding them anywhere */ |
|---|
| 4884 | n/a | if (type->tp_richcompare == NULL && |
|---|
| 4885 | n/a | type->tp_hash == NULL && |
|---|
| 4886 | n/a | !overrides_hash(type)) |
|---|
| 4887 | n/a | { |
|---|
| 4888 | n/a | type->tp_richcompare = base->tp_richcompare; |
|---|
| 4889 | n/a | type->tp_hash = base->tp_hash; |
|---|
| 4890 | n/a | } |
|---|
| 4891 | n/a | } |
|---|
| 4892 | n/a | { |
|---|
| 4893 | n/a | COPYSLOT(tp_iter); |
|---|
| 4894 | n/a | COPYSLOT(tp_iternext); |
|---|
| 4895 | n/a | } |
|---|
| 4896 | n/a | { |
|---|
| 4897 | n/a | COPYSLOT(tp_descr_get); |
|---|
| 4898 | n/a | COPYSLOT(tp_descr_set); |
|---|
| 4899 | n/a | COPYSLOT(tp_dictoffset); |
|---|
| 4900 | n/a | COPYSLOT(tp_init); |
|---|
| 4901 | n/a | COPYSLOT(tp_alloc); |
|---|
| 4902 | n/a | COPYSLOT(tp_is_gc); |
|---|
| 4903 | n/a | if ((type->tp_flags & Py_TPFLAGS_HAVE_FINALIZE) && |
|---|
| 4904 | n/a | (base->tp_flags & Py_TPFLAGS_HAVE_FINALIZE)) { |
|---|
| 4905 | n/a | COPYSLOT(tp_finalize); |
|---|
| 4906 | n/a | } |
|---|
| 4907 | n/a | if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) == |
|---|
| 4908 | n/a | (base->tp_flags & Py_TPFLAGS_HAVE_GC)) { |
|---|
| 4909 | n/a | /* They agree about gc. */ |
|---|
| 4910 | n/a | COPYSLOT(tp_free); |
|---|
| 4911 | n/a | } |
|---|
| 4912 | n/a | else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
|---|
| 4913 | n/a | type->tp_free == NULL && |
|---|
| 4914 | n/a | base->tp_free == PyObject_Free) { |
|---|
| 4915 | n/a | /* A bit of magic to plug in the correct default |
|---|
| 4916 | n/a | * tp_free function when a derived class adds gc, |
|---|
| 4917 | n/a | * didn't define tp_free, and the base uses the |
|---|
| 4918 | n/a | * default non-gc tp_free. |
|---|
| 4919 | n/a | */ |
|---|
| 4920 | n/a | type->tp_free = PyObject_GC_Del; |
|---|
| 4921 | n/a | } |
|---|
| 4922 | n/a | /* else they didn't agree about gc, and there isn't something |
|---|
| 4923 | n/a | * obvious to be done -- the type is on its own. |
|---|
| 4924 | n/a | */ |
|---|
| 4925 | n/a | } |
|---|
| 4926 | n/a | } |
|---|
| 4927 | n/a | |
|---|
| 4928 | n/a | static int add_operators(PyTypeObject *); |
|---|
| 4929 | n/a | |
|---|
| 4930 | n/a | int |
|---|
| 4931 | n/a | PyType_Ready(PyTypeObject *type) |
|---|
| 4932 | n/a | { |
|---|
| 4933 | n/a | PyObject *dict, *bases; |
|---|
| 4934 | n/a | PyTypeObject *base; |
|---|
| 4935 | n/a | Py_ssize_t i, n; |
|---|
| 4936 | n/a | |
|---|
| 4937 | n/a | if (type->tp_flags & Py_TPFLAGS_READY) { |
|---|
| 4938 | n/a | assert(_PyType_CheckConsistency(type)); |
|---|
| 4939 | n/a | return 0; |
|---|
| 4940 | n/a | } |
|---|
| 4941 | n/a | assert((type->tp_flags & Py_TPFLAGS_READYING) == 0); |
|---|
| 4942 | n/a | |
|---|
| 4943 | n/a | type->tp_flags |= Py_TPFLAGS_READYING; |
|---|
| 4944 | n/a | |
|---|
| 4945 | n/a | #ifdef Py_TRACE_REFS |
|---|
| 4946 | n/a | /* PyType_Ready is the closest thing we have to a choke point |
|---|
| 4947 | n/a | * for type objects, so is the best place I can think of to try |
|---|
| 4948 | n/a | * to get type objects into the doubly-linked list of all objects. |
|---|
| 4949 | n/a | * Still, not all type objects go thru PyType_Ready. |
|---|
| 4950 | n/a | */ |
|---|
| 4951 | n/a | _Py_AddToAllObjects((PyObject *)type, 0); |
|---|
| 4952 | n/a | #endif |
|---|
| 4953 | n/a | |
|---|
| 4954 | n/a | if (type->tp_name == NULL) { |
|---|
| 4955 | n/a | PyErr_Format(PyExc_SystemError, |
|---|
| 4956 | n/a | "Type does not define the tp_name field."); |
|---|
| 4957 | n/a | goto error; |
|---|
| 4958 | n/a | } |
|---|
| 4959 | n/a | |
|---|
| 4960 | n/a | /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
|---|
| 4961 | n/a | base = type->tp_base; |
|---|
| 4962 | n/a | if (base == NULL && type != &PyBaseObject_Type) { |
|---|
| 4963 | n/a | base = type->tp_base = &PyBaseObject_Type; |
|---|
| 4964 | n/a | Py_INCREF(base); |
|---|
| 4965 | n/a | } |
|---|
| 4966 | n/a | |
|---|
| 4967 | n/a | /* Now the only way base can still be NULL is if type is |
|---|
| 4968 | n/a | * &PyBaseObject_Type. |
|---|
| 4969 | n/a | */ |
|---|
| 4970 | n/a | |
|---|
| 4971 | n/a | /* Initialize the base class */ |
|---|
| 4972 | n/a | if (base != NULL && base->tp_dict == NULL) { |
|---|
| 4973 | n/a | if (PyType_Ready(base) < 0) |
|---|
| 4974 | n/a | goto error; |
|---|
| 4975 | n/a | } |
|---|
| 4976 | n/a | |
|---|
| 4977 | n/a | /* Initialize ob_type if NULL. This means extensions that want to be |
|---|
| 4978 | n/a | compilable separately on Windows can call PyType_Ready() instead of |
|---|
| 4979 | n/a | initializing the ob_type field of their type objects. */ |
|---|
| 4980 | n/a | /* The test for base != NULL is really unnecessary, since base is only |
|---|
| 4981 | n/a | NULL when type is &PyBaseObject_Type, and we know its ob_type is |
|---|
| 4982 | n/a | not NULL (it's initialized to &PyType_Type). But coverity doesn't |
|---|
| 4983 | n/a | know that. */ |
|---|
| 4984 | n/a | if (Py_TYPE(type) == NULL && base != NULL) |
|---|
| 4985 | n/a | Py_TYPE(type) = Py_TYPE(base); |
|---|
| 4986 | n/a | |
|---|
| 4987 | n/a | /* Initialize tp_bases */ |
|---|
| 4988 | n/a | bases = type->tp_bases; |
|---|
| 4989 | n/a | if (bases == NULL) { |
|---|
| 4990 | n/a | if (base == NULL) |
|---|
| 4991 | n/a | bases = PyTuple_New(0); |
|---|
| 4992 | n/a | else |
|---|
| 4993 | n/a | bases = PyTuple_Pack(1, base); |
|---|
| 4994 | n/a | if (bases == NULL) |
|---|
| 4995 | n/a | goto error; |
|---|
| 4996 | n/a | type->tp_bases = bases; |
|---|
| 4997 | n/a | } |
|---|
| 4998 | n/a | |
|---|
| 4999 | n/a | /* Initialize tp_dict */ |
|---|
| 5000 | n/a | dict = type->tp_dict; |
|---|
| 5001 | n/a | if (dict == NULL) { |
|---|
| 5002 | n/a | dict = PyDict_New(); |
|---|
| 5003 | n/a | if (dict == NULL) |
|---|
| 5004 | n/a | goto error; |
|---|
| 5005 | n/a | type->tp_dict = dict; |
|---|
| 5006 | n/a | } |
|---|
| 5007 | n/a | |
|---|
| 5008 | n/a | /* Add type-specific descriptors to tp_dict */ |
|---|
| 5009 | n/a | if (add_operators(type) < 0) |
|---|
| 5010 | n/a | goto error; |
|---|
| 5011 | n/a | if (type->tp_methods != NULL) { |
|---|
| 5012 | n/a | if (add_methods(type, type->tp_methods) < 0) |
|---|
| 5013 | n/a | goto error; |
|---|
| 5014 | n/a | } |
|---|
| 5015 | n/a | if (type->tp_members != NULL) { |
|---|
| 5016 | n/a | if (add_members(type, type->tp_members) < 0) |
|---|
| 5017 | n/a | goto error; |
|---|
| 5018 | n/a | } |
|---|
| 5019 | n/a | if (type->tp_getset != NULL) { |
|---|
| 5020 | n/a | if (add_getset(type, type->tp_getset) < 0) |
|---|
| 5021 | n/a | goto error; |
|---|
| 5022 | n/a | } |
|---|
| 5023 | n/a | |
|---|
| 5024 | n/a | /* Calculate method resolution order */ |
|---|
| 5025 | n/a | if (mro_internal(type, NULL) < 0) |
|---|
| 5026 | n/a | goto error; |
|---|
| 5027 | n/a | |
|---|
| 5028 | n/a | /* Inherit special flags from dominant base */ |
|---|
| 5029 | n/a | if (type->tp_base != NULL) |
|---|
| 5030 | n/a | inherit_special(type, type->tp_base); |
|---|
| 5031 | n/a | |
|---|
| 5032 | n/a | /* Initialize tp_dict properly */ |
|---|
| 5033 | n/a | bases = type->tp_mro; |
|---|
| 5034 | n/a | assert(bases != NULL); |
|---|
| 5035 | n/a | assert(PyTuple_Check(bases)); |
|---|
| 5036 | n/a | n = PyTuple_GET_SIZE(bases); |
|---|
| 5037 | n/a | for (i = 1; i < n; i++) { |
|---|
| 5038 | n/a | PyObject *b = PyTuple_GET_ITEM(bases, i); |
|---|
| 5039 | n/a | if (PyType_Check(b)) |
|---|
| 5040 | n/a | inherit_slots(type, (PyTypeObject *)b); |
|---|
| 5041 | n/a | } |
|---|
| 5042 | n/a | |
|---|
| 5043 | n/a | /* All bases of statically allocated type should be statically allocated */ |
|---|
| 5044 | n/a | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
|---|
| 5045 | n/a | for (i = 0; i < n; i++) { |
|---|
| 5046 | n/a | PyObject *b = PyTuple_GET_ITEM(bases, i); |
|---|
| 5047 | n/a | if (PyType_Check(b) && |
|---|
| 5048 | n/a | (((PyTypeObject *)b)->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
|---|
| 5049 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 5050 | n/a | "type '%.100s' is not dynamically allocated but " |
|---|
| 5051 | n/a | "its base type '%.100s' is dynamically allocated", |
|---|
| 5052 | n/a | type->tp_name, ((PyTypeObject *)b)->tp_name); |
|---|
| 5053 | n/a | goto error; |
|---|
| 5054 | n/a | } |
|---|
| 5055 | n/a | } |
|---|
| 5056 | n/a | |
|---|
| 5057 | n/a | /* Sanity check for tp_free. */ |
|---|
| 5058 | n/a | if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) && |
|---|
| 5059 | n/a | (type->tp_free == NULL || type->tp_free == PyObject_Del)) { |
|---|
| 5060 | n/a | /* This base class needs to call tp_free, but doesn't have |
|---|
| 5061 | n/a | * one, or its tp_free is for non-gc'ed objects. |
|---|
| 5062 | n/a | */ |
|---|
| 5063 | n/a | PyErr_Format(PyExc_TypeError, "type '%.100s' participates in " |
|---|
| 5064 | n/a | "gc and is a base type but has inappropriate " |
|---|
| 5065 | n/a | "tp_free slot", |
|---|
| 5066 | n/a | type->tp_name); |
|---|
| 5067 | n/a | goto error; |
|---|
| 5068 | n/a | } |
|---|
| 5069 | n/a | |
|---|
| 5070 | n/a | /* if the type dictionary doesn't contain a __doc__, set it from |
|---|
| 5071 | n/a | the tp_doc slot. |
|---|
| 5072 | n/a | */ |
|---|
| 5073 | n/a | if (_PyDict_GetItemId(type->tp_dict, &PyId___doc__) == NULL) { |
|---|
| 5074 | n/a | if (type->tp_doc != NULL) { |
|---|
| 5075 | n/a | const char *old_doc = _PyType_DocWithoutSignature(type->tp_name, |
|---|
| 5076 | n/a | type->tp_doc); |
|---|
| 5077 | n/a | PyObject *doc = PyUnicode_FromString(old_doc); |
|---|
| 5078 | n/a | if (doc == NULL) |
|---|
| 5079 | n/a | goto error; |
|---|
| 5080 | n/a | if (_PyDict_SetItemId(type->tp_dict, &PyId___doc__, doc) < 0) { |
|---|
| 5081 | n/a | Py_DECREF(doc); |
|---|
| 5082 | n/a | goto error; |
|---|
| 5083 | n/a | } |
|---|
| 5084 | n/a | Py_DECREF(doc); |
|---|
| 5085 | n/a | } else { |
|---|
| 5086 | n/a | if (_PyDict_SetItemId(type->tp_dict, |
|---|
| 5087 | n/a | &PyId___doc__, Py_None) < 0) |
|---|
| 5088 | n/a | goto error; |
|---|
| 5089 | n/a | } |
|---|
| 5090 | n/a | } |
|---|
| 5091 | n/a | |
|---|
| 5092 | n/a | /* Hack for tp_hash and __hash__. |
|---|
| 5093 | n/a | If after all that, tp_hash is still NULL, and __hash__ is not in |
|---|
| 5094 | n/a | tp_dict, set tp_hash to PyObject_HashNotImplemented and |
|---|
| 5095 | n/a | tp_dict['__hash__'] equal to None. |
|---|
| 5096 | n/a | This signals that __hash__ is not inherited. |
|---|
| 5097 | n/a | */ |
|---|
| 5098 | n/a | if (type->tp_hash == NULL) { |
|---|
| 5099 | n/a | if (_PyDict_GetItemId(type->tp_dict, &PyId___hash__) == NULL) { |
|---|
| 5100 | n/a | if (_PyDict_SetItemId(type->tp_dict, &PyId___hash__, Py_None) < 0) |
|---|
| 5101 | n/a | goto error; |
|---|
| 5102 | n/a | type->tp_hash = PyObject_HashNotImplemented; |
|---|
| 5103 | n/a | } |
|---|
| 5104 | n/a | } |
|---|
| 5105 | n/a | |
|---|
| 5106 | n/a | /* Some more special stuff */ |
|---|
| 5107 | n/a | base = type->tp_base; |
|---|
| 5108 | n/a | if (base != NULL) { |
|---|
| 5109 | n/a | if (type->tp_as_async == NULL) |
|---|
| 5110 | n/a | type->tp_as_async = base->tp_as_async; |
|---|
| 5111 | n/a | if (type->tp_as_number == NULL) |
|---|
| 5112 | n/a | type->tp_as_number = base->tp_as_number; |
|---|
| 5113 | n/a | if (type->tp_as_sequence == NULL) |
|---|
| 5114 | n/a | type->tp_as_sequence = base->tp_as_sequence; |
|---|
| 5115 | n/a | if (type->tp_as_mapping == NULL) |
|---|
| 5116 | n/a | type->tp_as_mapping = base->tp_as_mapping; |
|---|
| 5117 | n/a | if (type->tp_as_buffer == NULL) |
|---|
| 5118 | n/a | type->tp_as_buffer = base->tp_as_buffer; |
|---|
| 5119 | n/a | } |
|---|
| 5120 | n/a | |
|---|
| 5121 | n/a | /* Link into each base class's list of subclasses */ |
|---|
| 5122 | n/a | bases = type->tp_bases; |
|---|
| 5123 | n/a | n = PyTuple_GET_SIZE(bases); |
|---|
| 5124 | n/a | for (i = 0; i < n; i++) { |
|---|
| 5125 | n/a | PyObject *b = PyTuple_GET_ITEM(bases, i); |
|---|
| 5126 | n/a | if (PyType_Check(b) && |
|---|
| 5127 | n/a | add_subclass((PyTypeObject *)b, type) < 0) |
|---|
| 5128 | n/a | goto error; |
|---|
| 5129 | n/a | } |
|---|
| 5130 | n/a | |
|---|
| 5131 | n/a | /* All done -- set the ready flag */ |
|---|
| 5132 | n/a | type->tp_flags = |
|---|
| 5133 | n/a | (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; |
|---|
| 5134 | n/a | assert(_PyType_CheckConsistency(type)); |
|---|
| 5135 | n/a | return 0; |
|---|
| 5136 | n/a | |
|---|
| 5137 | n/a | error: |
|---|
| 5138 | n/a | type->tp_flags &= ~Py_TPFLAGS_READYING; |
|---|
| 5139 | n/a | return -1; |
|---|
| 5140 | n/a | } |
|---|
| 5141 | n/a | |
|---|
| 5142 | n/a | static int |
|---|
| 5143 | n/a | add_subclass(PyTypeObject *base, PyTypeObject *type) |
|---|
| 5144 | n/a | { |
|---|
| 5145 | n/a | int result = -1; |
|---|
| 5146 | n/a | PyObject *dict, *key, *newobj; |
|---|
| 5147 | n/a | |
|---|
| 5148 | n/a | dict = base->tp_subclasses; |
|---|
| 5149 | n/a | if (dict == NULL) { |
|---|
| 5150 | n/a | base->tp_subclasses = dict = PyDict_New(); |
|---|
| 5151 | n/a | if (dict == NULL) |
|---|
| 5152 | n/a | return -1; |
|---|
| 5153 | n/a | } |
|---|
| 5154 | n/a | assert(PyDict_CheckExact(dict)); |
|---|
| 5155 | n/a | key = PyLong_FromVoidPtr((void *) type); |
|---|
| 5156 | n/a | if (key == NULL) |
|---|
| 5157 | n/a | return -1; |
|---|
| 5158 | n/a | newobj = PyWeakref_NewRef((PyObject *)type, NULL); |
|---|
| 5159 | n/a | if (newobj != NULL) { |
|---|
| 5160 | n/a | result = PyDict_SetItem(dict, key, newobj); |
|---|
| 5161 | n/a | Py_DECREF(newobj); |
|---|
| 5162 | n/a | } |
|---|
| 5163 | n/a | Py_DECREF(key); |
|---|
| 5164 | n/a | return result; |
|---|
| 5165 | n/a | } |
|---|
| 5166 | n/a | |
|---|
| 5167 | n/a | static int |
|---|
| 5168 | n/a | add_all_subclasses(PyTypeObject *type, PyObject *bases) |
|---|
| 5169 | n/a | { |
|---|
| 5170 | n/a | int res = 0; |
|---|
| 5171 | n/a | |
|---|
| 5172 | n/a | if (bases) { |
|---|
| 5173 | n/a | Py_ssize_t i; |
|---|
| 5174 | n/a | for (i = 0; i < PyTuple_GET_SIZE(bases); i++) { |
|---|
| 5175 | n/a | PyObject *base = PyTuple_GET_ITEM(bases, i); |
|---|
| 5176 | n/a | if (PyType_Check(base) && |
|---|
| 5177 | n/a | add_subclass((PyTypeObject*)base, type) < 0) |
|---|
| 5178 | n/a | res = -1; |
|---|
| 5179 | n/a | } |
|---|
| 5180 | n/a | } |
|---|
| 5181 | n/a | |
|---|
| 5182 | n/a | return res; |
|---|
| 5183 | n/a | } |
|---|
| 5184 | n/a | |
|---|
| 5185 | n/a | static void |
|---|
| 5186 | n/a | remove_subclass(PyTypeObject *base, PyTypeObject *type) |
|---|
| 5187 | n/a | { |
|---|
| 5188 | n/a | PyObject *dict, *key; |
|---|
| 5189 | n/a | |
|---|
| 5190 | n/a | dict = base->tp_subclasses; |
|---|
| 5191 | n/a | if (dict == NULL) { |
|---|
| 5192 | n/a | return; |
|---|
| 5193 | n/a | } |
|---|
| 5194 | n/a | assert(PyDict_CheckExact(dict)); |
|---|
| 5195 | n/a | key = PyLong_FromVoidPtr((void *) type); |
|---|
| 5196 | n/a | if (key == NULL || PyDict_DelItem(dict, key)) { |
|---|
| 5197 | n/a | /* This can happen if the type initialization errored out before |
|---|
| 5198 | n/a | the base subclasses were updated (e.g. a non-str __qualname__ |
|---|
| 5199 | n/a | was passed in the type dict). */ |
|---|
| 5200 | n/a | PyErr_Clear(); |
|---|
| 5201 | n/a | } |
|---|
| 5202 | n/a | Py_XDECREF(key); |
|---|
| 5203 | n/a | } |
|---|
| 5204 | n/a | |
|---|
| 5205 | n/a | static void |
|---|
| 5206 | n/a | remove_all_subclasses(PyTypeObject *type, PyObject *bases) |
|---|
| 5207 | n/a | { |
|---|
| 5208 | n/a | if (bases) { |
|---|
| 5209 | n/a | Py_ssize_t i; |
|---|
| 5210 | n/a | for (i = 0; i < PyTuple_GET_SIZE(bases); i++) { |
|---|
| 5211 | n/a | PyObject *base = PyTuple_GET_ITEM(bases, i); |
|---|
| 5212 | n/a | if (PyType_Check(base)) |
|---|
| 5213 | n/a | remove_subclass((PyTypeObject*) base, type); |
|---|
| 5214 | n/a | } |
|---|
| 5215 | n/a | } |
|---|
| 5216 | n/a | } |
|---|
| 5217 | n/a | |
|---|
| 5218 | n/a | static int |
|---|
| 5219 | n/a | check_num_args(PyObject *ob, int n) |
|---|
| 5220 | n/a | { |
|---|
| 5221 | n/a | if (!PyTuple_CheckExact(ob)) { |
|---|
| 5222 | n/a | PyErr_SetString(PyExc_SystemError, |
|---|
| 5223 | n/a | "PyArg_UnpackTuple() argument list is not a tuple"); |
|---|
| 5224 | n/a | return 0; |
|---|
| 5225 | n/a | } |
|---|
| 5226 | n/a | if (n == PyTuple_GET_SIZE(ob)) |
|---|
| 5227 | n/a | return 1; |
|---|
| 5228 | n/a | PyErr_Format( |
|---|
| 5229 | n/a | PyExc_TypeError, |
|---|
| 5230 | n/a | "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob)); |
|---|
| 5231 | n/a | return 0; |
|---|
| 5232 | n/a | } |
|---|
| 5233 | n/a | |
|---|
| 5234 | n/a | /* Generic wrappers for overloadable 'operators' such as __getitem__ */ |
|---|
| 5235 | n/a | |
|---|
| 5236 | n/a | /* There's a wrapper *function* for each distinct function typedef used |
|---|
| 5237 | n/a | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a |
|---|
| 5238 | n/a | wrapper *table* for each distinct operation (e.g. __len__, __add__). |
|---|
| 5239 | n/a | Most tables have only one entry; the tables for binary operators have two |
|---|
| 5240 | n/a | entries, one regular and one with reversed arguments. */ |
|---|
| 5241 | n/a | |
|---|
| 5242 | n/a | static PyObject * |
|---|
| 5243 | n/a | wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5244 | n/a | { |
|---|
| 5245 | n/a | lenfunc func = (lenfunc)wrapped; |
|---|
| 5246 | n/a | Py_ssize_t res; |
|---|
| 5247 | n/a | |
|---|
| 5248 | n/a | if (!check_num_args(args, 0)) |
|---|
| 5249 | n/a | return NULL; |
|---|
| 5250 | n/a | res = (*func)(self); |
|---|
| 5251 | n/a | if (res == -1 && PyErr_Occurred()) |
|---|
| 5252 | n/a | return NULL; |
|---|
| 5253 | n/a | return PyLong_FromLong((long)res); |
|---|
| 5254 | n/a | } |
|---|
| 5255 | n/a | |
|---|
| 5256 | n/a | static PyObject * |
|---|
| 5257 | n/a | wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5258 | n/a | { |
|---|
| 5259 | n/a | inquiry func = (inquiry)wrapped; |
|---|
| 5260 | n/a | int res; |
|---|
| 5261 | n/a | |
|---|
| 5262 | n/a | if (!check_num_args(args, 0)) |
|---|
| 5263 | n/a | return NULL; |
|---|
| 5264 | n/a | res = (*func)(self); |
|---|
| 5265 | n/a | if (res == -1 && PyErr_Occurred()) |
|---|
| 5266 | n/a | return NULL; |
|---|
| 5267 | n/a | return PyBool_FromLong((long)res); |
|---|
| 5268 | n/a | } |
|---|
| 5269 | n/a | |
|---|
| 5270 | n/a | static PyObject * |
|---|
| 5271 | n/a | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5272 | n/a | { |
|---|
| 5273 | n/a | binaryfunc func = (binaryfunc)wrapped; |
|---|
| 5274 | n/a | PyObject *other; |
|---|
| 5275 | n/a | |
|---|
| 5276 | n/a | if (!check_num_args(args, 1)) |
|---|
| 5277 | n/a | return NULL; |
|---|
| 5278 | n/a | other = PyTuple_GET_ITEM(args, 0); |
|---|
| 5279 | n/a | return (*func)(self, other); |
|---|
| 5280 | n/a | } |
|---|
| 5281 | n/a | |
|---|
| 5282 | n/a | static PyObject * |
|---|
| 5283 | n/a | wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5284 | n/a | { |
|---|
| 5285 | n/a | binaryfunc func = (binaryfunc)wrapped; |
|---|
| 5286 | n/a | PyObject *other; |
|---|
| 5287 | n/a | |
|---|
| 5288 | n/a | if (!check_num_args(args, 1)) |
|---|
| 5289 | n/a | return NULL; |
|---|
| 5290 | n/a | other = PyTuple_GET_ITEM(args, 0); |
|---|
| 5291 | n/a | return (*func)(self, other); |
|---|
| 5292 | n/a | } |
|---|
| 5293 | n/a | |
|---|
| 5294 | n/a | static PyObject * |
|---|
| 5295 | n/a | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5296 | n/a | { |
|---|
| 5297 | n/a | binaryfunc func = (binaryfunc)wrapped; |
|---|
| 5298 | n/a | PyObject *other; |
|---|
| 5299 | n/a | |
|---|
| 5300 | n/a | if (!check_num_args(args, 1)) |
|---|
| 5301 | n/a | return NULL; |
|---|
| 5302 | n/a | other = PyTuple_GET_ITEM(args, 0); |
|---|
| 5303 | n/a | return (*func)(other, self); |
|---|
| 5304 | n/a | } |
|---|
| 5305 | n/a | |
|---|
| 5306 | n/a | static PyObject * |
|---|
| 5307 | n/a | wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5308 | n/a | { |
|---|
| 5309 | n/a | ternaryfunc func = (ternaryfunc)wrapped; |
|---|
| 5310 | n/a | PyObject *other; |
|---|
| 5311 | n/a | PyObject *third = Py_None; |
|---|
| 5312 | n/a | |
|---|
| 5313 | n/a | /* Note: This wrapper only works for __pow__() */ |
|---|
| 5314 | n/a | |
|---|
| 5315 | n/a | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
|---|
| 5316 | n/a | return NULL; |
|---|
| 5317 | n/a | return (*func)(self, other, third); |
|---|
| 5318 | n/a | } |
|---|
| 5319 | n/a | |
|---|
| 5320 | n/a | static PyObject * |
|---|
| 5321 | n/a | wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5322 | n/a | { |
|---|
| 5323 | n/a | ternaryfunc func = (ternaryfunc)wrapped; |
|---|
| 5324 | n/a | PyObject *other; |
|---|
| 5325 | n/a | PyObject *third = Py_None; |
|---|
| 5326 | n/a | |
|---|
| 5327 | n/a | /* Note: This wrapper only works for __pow__() */ |
|---|
| 5328 | n/a | |
|---|
| 5329 | n/a | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) |
|---|
| 5330 | n/a | return NULL; |
|---|
| 5331 | n/a | return (*func)(other, self, third); |
|---|
| 5332 | n/a | } |
|---|
| 5333 | n/a | |
|---|
| 5334 | n/a | static PyObject * |
|---|
| 5335 | n/a | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5336 | n/a | { |
|---|
| 5337 | n/a | unaryfunc func = (unaryfunc)wrapped; |
|---|
| 5338 | n/a | |
|---|
| 5339 | n/a | if (!check_num_args(args, 0)) |
|---|
| 5340 | n/a | return NULL; |
|---|
| 5341 | n/a | return (*func)(self); |
|---|
| 5342 | n/a | } |
|---|
| 5343 | n/a | |
|---|
| 5344 | n/a | static PyObject * |
|---|
| 5345 | n/a | wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5346 | n/a | { |
|---|
| 5347 | n/a | ssizeargfunc func = (ssizeargfunc)wrapped; |
|---|
| 5348 | n/a | PyObject* o; |
|---|
| 5349 | n/a | Py_ssize_t i; |
|---|
| 5350 | n/a | |
|---|
| 5351 | n/a | if (!PyArg_UnpackTuple(args, "", 1, 1, &o)) |
|---|
| 5352 | n/a | return NULL; |
|---|
| 5353 | n/a | i = PyNumber_AsSsize_t(o, PyExc_OverflowError); |
|---|
| 5354 | n/a | if (i == -1 && PyErr_Occurred()) |
|---|
| 5355 | n/a | return NULL; |
|---|
| 5356 | n/a | return (*func)(self, i); |
|---|
| 5357 | n/a | } |
|---|
| 5358 | n/a | |
|---|
| 5359 | n/a | static Py_ssize_t |
|---|
| 5360 | n/a | getindex(PyObject *self, PyObject *arg) |
|---|
| 5361 | n/a | { |
|---|
| 5362 | n/a | Py_ssize_t i; |
|---|
| 5363 | n/a | |
|---|
| 5364 | n/a | i = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
|---|
| 5365 | n/a | if (i == -1 && PyErr_Occurred()) |
|---|
| 5366 | n/a | return -1; |
|---|
| 5367 | n/a | if (i < 0) { |
|---|
| 5368 | n/a | PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence; |
|---|
| 5369 | n/a | if (sq && sq->sq_length) { |
|---|
| 5370 | n/a | Py_ssize_t n = (*sq->sq_length)(self); |
|---|
| 5371 | n/a | if (n < 0) |
|---|
| 5372 | n/a | return -1; |
|---|
| 5373 | n/a | i += n; |
|---|
| 5374 | n/a | } |
|---|
| 5375 | n/a | } |
|---|
| 5376 | n/a | return i; |
|---|
| 5377 | n/a | } |
|---|
| 5378 | n/a | |
|---|
| 5379 | n/a | static PyObject * |
|---|
| 5380 | n/a | wrap_sq_item(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5381 | n/a | { |
|---|
| 5382 | n/a | ssizeargfunc func = (ssizeargfunc)wrapped; |
|---|
| 5383 | n/a | PyObject *arg; |
|---|
| 5384 | n/a | Py_ssize_t i; |
|---|
| 5385 | n/a | |
|---|
| 5386 | n/a | if (PyTuple_GET_SIZE(args) == 1) { |
|---|
| 5387 | n/a | arg = PyTuple_GET_ITEM(args, 0); |
|---|
| 5388 | n/a | i = getindex(self, arg); |
|---|
| 5389 | n/a | if (i == -1 && PyErr_Occurred()) |
|---|
| 5390 | n/a | return NULL; |
|---|
| 5391 | n/a | return (*func)(self, i); |
|---|
| 5392 | n/a | } |
|---|
| 5393 | n/a | check_num_args(args, 1); |
|---|
| 5394 | n/a | assert(PyErr_Occurred()); |
|---|
| 5395 | n/a | return NULL; |
|---|
| 5396 | n/a | } |
|---|
| 5397 | n/a | |
|---|
| 5398 | n/a | static PyObject * |
|---|
| 5399 | n/a | wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5400 | n/a | { |
|---|
| 5401 | n/a | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
|---|
| 5402 | n/a | Py_ssize_t i; |
|---|
| 5403 | n/a | int res; |
|---|
| 5404 | n/a | PyObject *arg, *value; |
|---|
| 5405 | n/a | |
|---|
| 5406 | n/a | if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value)) |
|---|
| 5407 | n/a | return NULL; |
|---|
| 5408 | n/a | i = getindex(self, arg); |
|---|
| 5409 | n/a | if (i == -1 && PyErr_Occurred()) |
|---|
| 5410 | n/a | return NULL; |
|---|
| 5411 | n/a | res = (*func)(self, i, value); |
|---|
| 5412 | n/a | if (res == -1 && PyErr_Occurred()) |
|---|
| 5413 | n/a | return NULL; |
|---|
| 5414 | n/a | Py_RETURN_NONE; |
|---|
| 5415 | n/a | } |
|---|
| 5416 | n/a | |
|---|
| 5417 | n/a | static PyObject * |
|---|
| 5418 | n/a | wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5419 | n/a | { |
|---|
| 5420 | n/a | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
|---|
| 5421 | n/a | Py_ssize_t i; |
|---|
| 5422 | n/a | int res; |
|---|
| 5423 | n/a | PyObject *arg; |
|---|
| 5424 | n/a | |
|---|
| 5425 | n/a | if (!check_num_args(args, 1)) |
|---|
| 5426 | n/a | return NULL; |
|---|
| 5427 | n/a | arg = PyTuple_GET_ITEM(args, 0); |
|---|
| 5428 | n/a | i = getindex(self, arg); |
|---|
| 5429 | n/a | if (i == -1 && PyErr_Occurred()) |
|---|
| 5430 | n/a | return NULL; |
|---|
| 5431 | n/a | res = (*func)(self, i, NULL); |
|---|
| 5432 | n/a | if (res == -1 && PyErr_Occurred()) |
|---|
| 5433 | n/a | return NULL; |
|---|
| 5434 | n/a | Py_RETURN_NONE; |
|---|
| 5435 | n/a | } |
|---|
| 5436 | n/a | |
|---|
| 5437 | n/a | /* XXX objobjproc is a misnomer; should be objargpred */ |
|---|
| 5438 | n/a | static PyObject * |
|---|
| 5439 | n/a | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5440 | n/a | { |
|---|
| 5441 | n/a | objobjproc func = (objobjproc)wrapped; |
|---|
| 5442 | n/a | int res; |
|---|
| 5443 | n/a | PyObject *value; |
|---|
| 5444 | n/a | |
|---|
| 5445 | n/a | if (!check_num_args(args, 1)) |
|---|
| 5446 | n/a | return NULL; |
|---|
| 5447 | n/a | value = PyTuple_GET_ITEM(args, 0); |
|---|
| 5448 | n/a | res = (*func)(self, value); |
|---|
| 5449 | n/a | if (res == -1 && PyErr_Occurred()) |
|---|
| 5450 | n/a | return NULL; |
|---|
| 5451 | n/a | else |
|---|
| 5452 | n/a | return PyBool_FromLong(res); |
|---|
| 5453 | n/a | } |
|---|
| 5454 | n/a | |
|---|
| 5455 | n/a | static PyObject * |
|---|
| 5456 | n/a | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5457 | n/a | { |
|---|
| 5458 | n/a | objobjargproc func = (objobjargproc)wrapped; |
|---|
| 5459 | n/a | int res; |
|---|
| 5460 | n/a | PyObject *key, *value; |
|---|
| 5461 | n/a | |
|---|
| 5462 | n/a | if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value)) |
|---|
| 5463 | n/a | return NULL; |
|---|
| 5464 | n/a | res = (*func)(self, key, value); |
|---|
| 5465 | n/a | if (res == -1 && PyErr_Occurred()) |
|---|
| 5466 | n/a | return NULL; |
|---|
| 5467 | n/a | Py_RETURN_NONE; |
|---|
| 5468 | n/a | } |
|---|
| 5469 | n/a | |
|---|
| 5470 | n/a | static PyObject * |
|---|
| 5471 | n/a | wrap_delitem(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5472 | n/a | { |
|---|
| 5473 | n/a | objobjargproc func = (objobjargproc)wrapped; |
|---|
| 5474 | n/a | int res; |
|---|
| 5475 | n/a | PyObject *key; |
|---|
| 5476 | n/a | |
|---|
| 5477 | n/a | if (!check_num_args(args, 1)) |
|---|
| 5478 | n/a | return NULL; |
|---|
| 5479 | n/a | key = PyTuple_GET_ITEM(args, 0); |
|---|
| 5480 | n/a | res = (*func)(self, key, NULL); |
|---|
| 5481 | n/a | if (res == -1 && PyErr_Occurred()) |
|---|
| 5482 | n/a | return NULL; |
|---|
| 5483 | n/a | Py_RETURN_NONE; |
|---|
| 5484 | n/a | } |
|---|
| 5485 | n/a | |
|---|
| 5486 | n/a | /* Helper to check for object.__setattr__ or __delattr__ applied to a type. |
|---|
| 5487 | n/a | This is called the Carlo Verre hack after its discoverer. */ |
|---|
| 5488 | n/a | static int |
|---|
| 5489 | n/a | hackcheck(PyObject *self, setattrofunc func, const char *what) |
|---|
| 5490 | n/a | { |
|---|
| 5491 | n/a | PyTypeObject *type = Py_TYPE(self); |
|---|
| 5492 | n/a | while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
|---|
| 5493 | n/a | type = type->tp_base; |
|---|
| 5494 | n/a | /* If type is NULL now, this is a really weird type. |
|---|
| 5495 | n/a | In the spirit of backwards compatibility (?), just shut up. */ |
|---|
| 5496 | n/a | if (type && type->tp_setattro != func) { |
|---|
| 5497 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 5498 | n/a | "can't apply this %s to %s object", |
|---|
| 5499 | n/a | what, |
|---|
| 5500 | n/a | type->tp_name); |
|---|
| 5501 | n/a | return 0; |
|---|
| 5502 | n/a | } |
|---|
| 5503 | n/a | return 1; |
|---|
| 5504 | n/a | } |
|---|
| 5505 | n/a | |
|---|
| 5506 | n/a | static PyObject * |
|---|
| 5507 | n/a | wrap_setattr(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5508 | n/a | { |
|---|
| 5509 | n/a | setattrofunc func = (setattrofunc)wrapped; |
|---|
| 5510 | n/a | int res; |
|---|
| 5511 | n/a | PyObject *name, *value; |
|---|
| 5512 | n/a | |
|---|
| 5513 | n/a | if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value)) |
|---|
| 5514 | n/a | return NULL; |
|---|
| 5515 | n/a | if (!hackcheck(self, func, "__setattr__")) |
|---|
| 5516 | n/a | return NULL; |
|---|
| 5517 | n/a | res = (*func)(self, name, value); |
|---|
| 5518 | n/a | if (res < 0) |
|---|
| 5519 | n/a | return NULL; |
|---|
| 5520 | n/a | Py_RETURN_NONE; |
|---|
| 5521 | n/a | } |
|---|
| 5522 | n/a | |
|---|
| 5523 | n/a | static PyObject * |
|---|
| 5524 | n/a | wrap_delattr(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5525 | n/a | { |
|---|
| 5526 | n/a | setattrofunc func = (setattrofunc)wrapped; |
|---|
| 5527 | n/a | int res; |
|---|
| 5528 | n/a | PyObject *name; |
|---|
| 5529 | n/a | |
|---|
| 5530 | n/a | if (!check_num_args(args, 1)) |
|---|
| 5531 | n/a | return NULL; |
|---|
| 5532 | n/a | name = PyTuple_GET_ITEM(args, 0); |
|---|
| 5533 | n/a | if (!hackcheck(self, func, "__delattr__")) |
|---|
| 5534 | n/a | return NULL; |
|---|
| 5535 | n/a | res = (*func)(self, name, NULL); |
|---|
| 5536 | n/a | if (res < 0) |
|---|
| 5537 | n/a | return NULL; |
|---|
| 5538 | n/a | Py_RETURN_NONE; |
|---|
| 5539 | n/a | } |
|---|
| 5540 | n/a | |
|---|
| 5541 | n/a | static PyObject * |
|---|
| 5542 | n/a | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5543 | n/a | { |
|---|
| 5544 | n/a | hashfunc func = (hashfunc)wrapped; |
|---|
| 5545 | n/a | Py_hash_t res; |
|---|
| 5546 | n/a | |
|---|
| 5547 | n/a | if (!check_num_args(args, 0)) |
|---|
| 5548 | n/a | return NULL; |
|---|
| 5549 | n/a | res = (*func)(self); |
|---|
| 5550 | n/a | if (res == -1 && PyErr_Occurred()) |
|---|
| 5551 | n/a | return NULL; |
|---|
| 5552 | n/a | return PyLong_FromSsize_t(res); |
|---|
| 5553 | n/a | } |
|---|
| 5554 | n/a | |
|---|
| 5555 | n/a | static PyObject * |
|---|
| 5556 | n/a | wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
|---|
| 5557 | n/a | { |
|---|
| 5558 | n/a | ternaryfunc func = (ternaryfunc)wrapped; |
|---|
| 5559 | n/a | |
|---|
| 5560 | n/a | return (*func)(self, args, kwds); |
|---|
| 5561 | n/a | } |
|---|
| 5562 | n/a | |
|---|
| 5563 | n/a | static PyObject * |
|---|
| 5564 | n/a | wrap_del(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5565 | n/a | { |
|---|
| 5566 | n/a | destructor func = (destructor)wrapped; |
|---|
| 5567 | n/a | |
|---|
| 5568 | n/a | if (!check_num_args(args, 0)) |
|---|
| 5569 | n/a | return NULL; |
|---|
| 5570 | n/a | |
|---|
| 5571 | n/a | (*func)(self); |
|---|
| 5572 | n/a | Py_RETURN_NONE; |
|---|
| 5573 | n/a | } |
|---|
| 5574 | n/a | |
|---|
| 5575 | n/a | static PyObject * |
|---|
| 5576 | n/a | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) |
|---|
| 5577 | n/a | { |
|---|
| 5578 | n/a | richcmpfunc func = (richcmpfunc)wrapped; |
|---|
| 5579 | n/a | PyObject *other; |
|---|
| 5580 | n/a | |
|---|
| 5581 | n/a | if (!check_num_args(args, 1)) |
|---|
| 5582 | n/a | return NULL; |
|---|
| 5583 | n/a | other = PyTuple_GET_ITEM(args, 0); |
|---|
| 5584 | n/a | return (*func)(self, other, op); |
|---|
| 5585 | n/a | } |
|---|
| 5586 | n/a | |
|---|
| 5587 | n/a | #undef RICHCMP_WRAPPER |
|---|
| 5588 | n/a | #define RICHCMP_WRAPPER(NAME, OP) \ |
|---|
| 5589 | n/a | static PyObject * \ |
|---|
| 5590 | n/a | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ |
|---|
| 5591 | n/a | { \ |
|---|
| 5592 | n/a | return wrap_richcmpfunc(self, args, wrapped, OP); \ |
|---|
| 5593 | n/a | } |
|---|
| 5594 | n/a | |
|---|
| 5595 | n/a | RICHCMP_WRAPPER(lt, Py_LT) |
|---|
| 5596 | n/a | RICHCMP_WRAPPER(le, Py_LE) |
|---|
| 5597 | n/a | RICHCMP_WRAPPER(eq, Py_EQ) |
|---|
| 5598 | n/a | RICHCMP_WRAPPER(ne, Py_NE) |
|---|
| 5599 | n/a | RICHCMP_WRAPPER(gt, Py_GT) |
|---|
| 5600 | n/a | RICHCMP_WRAPPER(ge, Py_GE) |
|---|
| 5601 | n/a | |
|---|
| 5602 | n/a | static PyObject * |
|---|
| 5603 | n/a | wrap_next(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5604 | n/a | { |
|---|
| 5605 | n/a | unaryfunc func = (unaryfunc)wrapped; |
|---|
| 5606 | n/a | PyObject *res; |
|---|
| 5607 | n/a | |
|---|
| 5608 | n/a | if (!check_num_args(args, 0)) |
|---|
| 5609 | n/a | return NULL; |
|---|
| 5610 | n/a | res = (*func)(self); |
|---|
| 5611 | n/a | if (res == NULL && !PyErr_Occurred()) |
|---|
| 5612 | n/a | PyErr_SetNone(PyExc_StopIteration); |
|---|
| 5613 | n/a | return res; |
|---|
| 5614 | n/a | } |
|---|
| 5615 | n/a | |
|---|
| 5616 | n/a | static PyObject * |
|---|
| 5617 | n/a | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5618 | n/a | { |
|---|
| 5619 | n/a | descrgetfunc func = (descrgetfunc)wrapped; |
|---|
| 5620 | n/a | PyObject *obj; |
|---|
| 5621 | n/a | PyObject *type = NULL; |
|---|
| 5622 | n/a | |
|---|
| 5623 | n/a | if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type)) |
|---|
| 5624 | n/a | return NULL; |
|---|
| 5625 | n/a | if (obj == Py_None) |
|---|
| 5626 | n/a | obj = NULL; |
|---|
| 5627 | n/a | if (type == Py_None) |
|---|
| 5628 | n/a | type = NULL; |
|---|
| 5629 | n/a | if (type == NULL &&obj == NULL) { |
|---|
| 5630 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 5631 | n/a | "__get__(None, None) is invalid"); |
|---|
| 5632 | n/a | return NULL; |
|---|
| 5633 | n/a | } |
|---|
| 5634 | n/a | return (*func)(self, obj, type); |
|---|
| 5635 | n/a | } |
|---|
| 5636 | n/a | |
|---|
| 5637 | n/a | static PyObject * |
|---|
| 5638 | n/a | wrap_descr_set(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5639 | n/a | { |
|---|
| 5640 | n/a | descrsetfunc func = (descrsetfunc)wrapped; |
|---|
| 5641 | n/a | PyObject *obj, *value; |
|---|
| 5642 | n/a | int ret; |
|---|
| 5643 | n/a | |
|---|
| 5644 | n/a | if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value)) |
|---|
| 5645 | n/a | return NULL; |
|---|
| 5646 | n/a | ret = (*func)(self, obj, value); |
|---|
| 5647 | n/a | if (ret < 0) |
|---|
| 5648 | n/a | return NULL; |
|---|
| 5649 | n/a | Py_RETURN_NONE; |
|---|
| 5650 | n/a | } |
|---|
| 5651 | n/a | |
|---|
| 5652 | n/a | static PyObject * |
|---|
| 5653 | n/a | wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped) |
|---|
| 5654 | n/a | { |
|---|
| 5655 | n/a | descrsetfunc func = (descrsetfunc)wrapped; |
|---|
| 5656 | n/a | PyObject *obj; |
|---|
| 5657 | n/a | int ret; |
|---|
| 5658 | n/a | |
|---|
| 5659 | n/a | if (!check_num_args(args, 1)) |
|---|
| 5660 | n/a | return NULL; |
|---|
| 5661 | n/a | obj = PyTuple_GET_ITEM(args, 0); |
|---|
| 5662 | n/a | ret = (*func)(self, obj, NULL); |
|---|
| 5663 | n/a | if (ret < 0) |
|---|
| 5664 | n/a | return NULL; |
|---|
| 5665 | n/a | Py_RETURN_NONE; |
|---|
| 5666 | n/a | } |
|---|
| 5667 | n/a | |
|---|
| 5668 | n/a | static PyObject * |
|---|
| 5669 | n/a | wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
|---|
| 5670 | n/a | { |
|---|
| 5671 | n/a | initproc func = (initproc)wrapped; |
|---|
| 5672 | n/a | |
|---|
| 5673 | n/a | if (func(self, args, kwds) < 0) |
|---|
| 5674 | n/a | return NULL; |
|---|
| 5675 | n/a | Py_RETURN_NONE; |
|---|
| 5676 | n/a | } |
|---|
| 5677 | n/a | |
|---|
| 5678 | n/a | static PyObject * |
|---|
| 5679 | n/a | tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) |
|---|
| 5680 | n/a | { |
|---|
| 5681 | n/a | PyTypeObject *type, *subtype, *staticbase; |
|---|
| 5682 | n/a | PyObject *arg0, *res; |
|---|
| 5683 | n/a | |
|---|
| 5684 | n/a | if (self == NULL || !PyType_Check(self)) |
|---|
| 5685 | n/a | Py_FatalError("__new__() called with non-type 'self'"); |
|---|
| 5686 | n/a | type = (PyTypeObject *)self; |
|---|
| 5687 | n/a | if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) { |
|---|
| 5688 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 5689 | n/a | "%s.__new__(): not enough arguments", |
|---|
| 5690 | n/a | type->tp_name); |
|---|
| 5691 | n/a | return NULL; |
|---|
| 5692 | n/a | } |
|---|
| 5693 | n/a | arg0 = PyTuple_GET_ITEM(args, 0); |
|---|
| 5694 | n/a | if (!PyType_Check(arg0)) { |
|---|
| 5695 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 5696 | n/a | "%s.__new__(X): X is not a type object (%s)", |
|---|
| 5697 | n/a | type->tp_name, |
|---|
| 5698 | n/a | Py_TYPE(arg0)->tp_name); |
|---|
| 5699 | n/a | return NULL; |
|---|
| 5700 | n/a | } |
|---|
| 5701 | n/a | subtype = (PyTypeObject *)arg0; |
|---|
| 5702 | n/a | if (!PyType_IsSubtype(subtype, type)) { |
|---|
| 5703 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 5704 | n/a | "%s.__new__(%s): %s is not a subtype of %s", |
|---|
| 5705 | n/a | type->tp_name, |
|---|
| 5706 | n/a | subtype->tp_name, |
|---|
| 5707 | n/a | subtype->tp_name, |
|---|
| 5708 | n/a | type->tp_name); |
|---|
| 5709 | n/a | return NULL; |
|---|
| 5710 | n/a | } |
|---|
| 5711 | n/a | |
|---|
| 5712 | n/a | /* Check that the use doesn't do something silly and unsafe like |
|---|
| 5713 | n/a | object.__new__(dict). To do this, we check that the |
|---|
| 5714 | n/a | most derived base that's not a heap type is this type. */ |
|---|
| 5715 | n/a | staticbase = subtype; |
|---|
| 5716 | n/a | while (staticbase && (staticbase->tp_new == slot_tp_new)) |
|---|
| 5717 | n/a | staticbase = staticbase->tp_base; |
|---|
| 5718 | n/a | /* If staticbase is NULL now, it is a really weird type. |
|---|
| 5719 | n/a | In the spirit of backwards compatibility (?), just shut up. */ |
|---|
| 5720 | n/a | if (staticbase && staticbase->tp_new != type->tp_new) { |
|---|
| 5721 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 5722 | n/a | "%s.__new__(%s) is not safe, use %s.__new__()", |
|---|
| 5723 | n/a | type->tp_name, |
|---|
| 5724 | n/a | subtype->tp_name, |
|---|
| 5725 | n/a | staticbase->tp_name); |
|---|
| 5726 | n/a | return NULL; |
|---|
| 5727 | n/a | } |
|---|
| 5728 | n/a | |
|---|
| 5729 | n/a | args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
|---|
| 5730 | n/a | if (args == NULL) |
|---|
| 5731 | n/a | return NULL; |
|---|
| 5732 | n/a | res = type->tp_new(subtype, args, kwds); |
|---|
| 5733 | n/a | Py_DECREF(args); |
|---|
| 5734 | n/a | return res; |
|---|
| 5735 | n/a | } |
|---|
| 5736 | n/a | |
|---|
| 5737 | n/a | static struct PyMethodDef tp_new_methoddef[] = { |
|---|
| 5738 | n/a | {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS, |
|---|
| 5739 | n/a | PyDoc_STR("__new__($type, *args, **kwargs)\n--\n\n" |
|---|
| 5740 | n/a | "Create and return a new object. " |
|---|
| 5741 | n/a | "See help(type) for accurate signature.")}, |
|---|
| 5742 | n/a | {0} |
|---|
| 5743 | n/a | }; |
|---|
| 5744 | n/a | |
|---|
| 5745 | n/a | static int |
|---|
| 5746 | n/a | add_tp_new_wrapper(PyTypeObject *type) |
|---|
| 5747 | n/a | { |
|---|
| 5748 | n/a | PyObject *func; |
|---|
| 5749 | n/a | |
|---|
| 5750 | n/a | if (_PyDict_GetItemId(type->tp_dict, &PyId___new__) != NULL) |
|---|
| 5751 | n/a | return 0; |
|---|
| 5752 | n/a | func = PyCFunction_NewEx(tp_new_methoddef, (PyObject *)type, NULL); |
|---|
| 5753 | n/a | if (func == NULL) |
|---|
| 5754 | n/a | return -1; |
|---|
| 5755 | n/a | if (_PyDict_SetItemId(type->tp_dict, &PyId___new__, func)) { |
|---|
| 5756 | n/a | Py_DECREF(func); |
|---|
| 5757 | n/a | return -1; |
|---|
| 5758 | n/a | } |
|---|
| 5759 | n/a | Py_DECREF(func); |
|---|
| 5760 | n/a | return 0; |
|---|
| 5761 | n/a | } |
|---|
| 5762 | n/a | |
|---|
| 5763 | n/a | /* Slot wrappers that call the corresponding __foo__ slot. See comments |
|---|
| 5764 | n/a | below at override_slots() for more explanation. */ |
|---|
| 5765 | n/a | |
|---|
| 5766 | n/a | #define SLOT0(FUNCNAME, OPSTR) \ |
|---|
| 5767 | n/a | static PyObject * \ |
|---|
| 5768 | n/a | FUNCNAME(PyObject *self) \ |
|---|
| 5769 | n/a | { \ |
|---|
| 5770 | n/a | _Py_static_string(id, OPSTR); \ |
|---|
| 5771 | n/a | return call_method(self, &id, NULL, 0); \ |
|---|
| 5772 | n/a | } |
|---|
| 5773 | n/a | |
|---|
| 5774 | n/a | #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE) \ |
|---|
| 5775 | n/a | static PyObject * \ |
|---|
| 5776 | n/a | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ |
|---|
| 5777 | n/a | { \ |
|---|
| 5778 | n/a | PyObject* stack[1] = {arg1}; \ |
|---|
| 5779 | n/a | _Py_static_string(id, OPSTR); \ |
|---|
| 5780 | n/a | return call_method(self, &id, stack, 1); \ |
|---|
| 5781 | n/a | } |
|---|
| 5782 | n/a | |
|---|
| 5783 | n/a | /* Boolean helper for SLOT1BINFULL(). |
|---|
| 5784 | n/a | right.__class__ is a nontrivial subclass of left.__class__. */ |
|---|
| 5785 | n/a | static int |
|---|
| 5786 | n/a | method_is_overloaded(PyObject *left, PyObject *right, struct _Py_Identifier *name) |
|---|
| 5787 | n/a | { |
|---|
| 5788 | n/a | PyObject *a, *b; |
|---|
| 5789 | n/a | int ok; |
|---|
| 5790 | n/a | |
|---|
| 5791 | n/a | b = _PyObject_GetAttrId((PyObject *)(Py_TYPE(right)), name); |
|---|
| 5792 | n/a | if (b == NULL) { |
|---|
| 5793 | n/a | PyErr_Clear(); |
|---|
| 5794 | n/a | /* If right doesn't have it, it's not overloaded */ |
|---|
| 5795 | n/a | return 0; |
|---|
| 5796 | n/a | } |
|---|
| 5797 | n/a | |
|---|
| 5798 | n/a | a = _PyObject_GetAttrId((PyObject *)(Py_TYPE(left)), name); |
|---|
| 5799 | n/a | if (a == NULL) { |
|---|
| 5800 | n/a | PyErr_Clear(); |
|---|
| 5801 | n/a | Py_DECREF(b); |
|---|
| 5802 | n/a | /* If right has it but left doesn't, it's overloaded */ |
|---|
| 5803 | n/a | return 1; |
|---|
| 5804 | n/a | } |
|---|
| 5805 | n/a | |
|---|
| 5806 | n/a | ok = PyObject_RichCompareBool(a, b, Py_NE); |
|---|
| 5807 | n/a | Py_DECREF(a); |
|---|
| 5808 | n/a | Py_DECREF(b); |
|---|
| 5809 | n/a | if (ok < 0) { |
|---|
| 5810 | n/a | PyErr_Clear(); |
|---|
| 5811 | n/a | return 0; |
|---|
| 5812 | n/a | } |
|---|
| 5813 | n/a | |
|---|
| 5814 | n/a | return ok; |
|---|
| 5815 | n/a | } |
|---|
| 5816 | n/a | |
|---|
| 5817 | n/a | |
|---|
| 5818 | n/a | #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \ |
|---|
| 5819 | n/a | static PyObject * \ |
|---|
| 5820 | n/a | FUNCNAME(PyObject *self, PyObject *other) \ |
|---|
| 5821 | n/a | { \ |
|---|
| 5822 | n/a | PyObject* stack[1]; \ |
|---|
| 5823 | n/a | _Py_static_string(op_id, OPSTR); \ |
|---|
| 5824 | n/a | _Py_static_string(rop_id, ROPSTR); \ |
|---|
| 5825 | n/a | int do_other = Py_TYPE(self) != Py_TYPE(other) && \ |
|---|
| 5826 | n/a | Py_TYPE(other)->tp_as_number != NULL && \ |
|---|
| 5827 | n/a | Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \ |
|---|
| 5828 | n/a | if (Py_TYPE(self)->tp_as_number != NULL && \ |
|---|
| 5829 | n/a | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ |
|---|
| 5830 | n/a | PyObject *r; \ |
|---|
| 5831 | n/a | if (do_other && \ |
|---|
| 5832 | n/a | PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \ |
|---|
| 5833 | n/a | method_is_overloaded(self, other, &rop_id)) { \ |
|---|
| 5834 | n/a | stack[0] = self; \ |
|---|
| 5835 | n/a | r = call_maybe(other, &rop_id, stack, 1); \ |
|---|
| 5836 | n/a | if (r != Py_NotImplemented) \ |
|---|
| 5837 | n/a | return r; \ |
|---|
| 5838 | n/a | Py_DECREF(r); \ |
|---|
| 5839 | n/a | do_other = 0; \ |
|---|
| 5840 | n/a | } \ |
|---|
| 5841 | n/a | stack[0] = other; \ |
|---|
| 5842 | n/a | r = call_maybe(self, &op_id, stack, 1); \ |
|---|
| 5843 | n/a | if (r != Py_NotImplemented || \ |
|---|
| 5844 | n/a | Py_TYPE(other) == Py_TYPE(self)) \ |
|---|
| 5845 | n/a | return r; \ |
|---|
| 5846 | n/a | Py_DECREF(r); \ |
|---|
| 5847 | n/a | } \ |
|---|
| 5848 | n/a | if (do_other) { \ |
|---|
| 5849 | n/a | stack[0] = self; \ |
|---|
| 5850 | n/a | return call_maybe(other, &rop_id, stack, 1); \ |
|---|
| 5851 | n/a | } \ |
|---|
| 5852 | n/a | Py_RETURN_NOTIMPLEMENTED; \ |
|---|
| 5853 | n/a | } |
|---|
| 5854 | n/a | |
|---|
| 5855 | n/a | #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ |
|---|
| 5856 | n/a | SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR) |
|---|
| 5857 | n/a | |
|---|
| 5858 | n/a | static Py_ssize_t |
|---|
| 5859 | n/a | slot_sq_length(PyObject *self) |
|---|
| 5860 | n/a | { |
|---|
| 5861 | n/a | PyObject *res = call_method(self, &PyId___len__, NULL, 0); |
|---|
| 5862 | n/a | Py_ssize_t len; |
|---|
| 5863 | n/a | |
|---|
| 5864 | n/a | if (res == NULL) |
|---|
| 5865 | n/a | return -1; |
|---|
| 5866 | n/a | len = PyNumber_AsSsize_t(res, PyExc_OverflowError); |
|---|
| 5867 | n/a | Py_DECREF(res); |
|---|
| 5868 | n/a | if (len < 0) { |
|---|
| 5869 | n/a | if (!PyErr_Occurred()) |
|---|
| 5870 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 5871 | n/a | "__len__() should return >= 0"); |
|---|
| 5872 | n/a | return -1; |
|---|
| 5873 | n/a | } |
|---|
| 5874 | n/a | return len; |
|---|
| 5875 | n/a | } |
|---|
| 5876 | n/a | |
|---|
| 5877 | n/a | /* Super-optimized version of slot_sq_item. |
|---|
| 5878 | n/a | Other slots could do the same... */ |
|---|
| 5879 | n/a | static PyObject * |
|---|
| 5880 | n/a | slot_sq_item(PyObject *self, Py_ssize_t i) |
|---|
| 5881 | n/a | { |
|---|
| 5882 | n/a | PyObject *func, *ival = NULL, *retval = NULL; |
|---|
| 5883 | n/a | descrgetfunc f; |
|---|
| 5884 | n/a | |
|---|
| 5885 | n/a | func = _PyType_LookupId(Py_TYPE(self), &PyId___getitem__); |
|---|
| 5886 | n/a | if (func == NULL) { |
|---|
| 5887 | n/a | PyObject *getitem_str = _PyUnicode_FromId(&PyId___getitem__); |
|---|
| 5888 | n/a | PyErr_SetObject(PyExc_AttributeError, getitem_str); |
|---|
| 5889 | n/a | return NULL; |
|---|
| 5890 | n/a | } |
|---|
| 5891 | n/a | |
|---|
| 5892 | n/a | f = Py_TYPE(func)->tp_descr_get; |
|---|
| 5893 | n/a | if (f == NULL) { |
|---|
| 5894 | n/a | Py_INCREF(func); |
|---|
| 5895 | n/a | } |
|---|
| 5896 | n/a | else { |
|---|
| 5897 | n/a | func = f(func, self, (PyObject *)(Py_TYPE(self))); |
|---|
| 5898 | n/a | if (func == NULL) { |
|---|
| 5899 | n/a | return NULL; |
|---|
| 5900 | n/a | } |
|---|
| 5901 | n/a | } |
|---|
| 5902 | n/a | |
|---|
| 5903 | n/a | ival = PyLong_FromSsize_t(i); |
|---|
| 5904 | n/a | if (ival == NULL) { |
|---|
| 5905 | n/a | goto error; |
|---|
| 5906 | n/a | } |
|---|
| 5907 | n/a | |
|---|
| 5908 | n/a | retval = PyObject_CallFunctionObjArgs(func, ival, NULL); |
|---|
| 5909 | n/a | Py_DECREF(func); |
|---|
| 5910 | n/a | Py_DECREF(ival); |
|---|
| 5911 | n/a | return retval; |
|---|
| 5912 | n/a | |
|---|
| 5913 | n/a | error: |
|---|
| 5914 | n/a | Py_DECREF(func); |
|---|
| 5915 | n/a | return NULL; |
|---|
| 5916 | n/a | } |
|---|
| 5917 | n/a | |
|---|
| 5918 | n/a | static int |
|---|
| 5919 | n/a | slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) |
|---|
| 5920 | n/a | { |
|---|
| 5921 | n/a | PyObject *stack[2]; |
|---|
| 5922 | n/a | PyObject *res; |
|---|
| 5923 | n/a | PyObject *index_obj; |
|---|
| 5924 | n/a | |
|---|
| 5925 | n/a | index_obj = PyLong_FromSsize_t(index); |
|---|
| 5926 | n/a | if (index_obj == NULL) { |
|---|
| 5927 | n/a | return -1; |
|---|
| 5928 | n/a | } |
|---|
| 5929 | n/a | |
|---|
| 5930 | n/a | stack[0] = index_obj; |
|---|
| 5931 | n/a | if (value == NULL) { |
|---|
| 5932 | n/a | res = call_method(self, &PyId___delitem__, stack, 1); |
|---|
| 5933 | n/a | } |
|---|
| 5934 | n/a | else { |
|---|
| 5935 | n/a | stack[1] = value; |
|---|
| 5936 | n/a | res = call_method(self, &PyId___setitem__, stack, 2); |
|---|
| 5937 | n/a | } |
|---|
| 5938 | n/a | Py_DECREF(index_obj); |
|---|
| 5939 | n/a | |
|---|
| 5940 | n/a | if (res == NULL) { |
|---|
| 5941 | n/a | return -1; |
|---|
| 5942 | n/a | } |
|---|
| 5943 | n/a | Py_DECREF(res); |
|---|
| 5944 | n/a | return 0; |
|---|
| 5945 | n/a | } |
|---|
| 5946 | n/a | |
|---|
| 5947 | n/a | static int |
|---|
| 5948 | n/a | slot_sq_contains(PyObject *self, PyObject *value) |
|---|
| 5949 | n/a | { |
|---|
| 5950 | n/a | PyObject *func, *res; |
|---|
| 5951 | n/a | int result = -1, unbound; |
|---|
| 5952 | n/a | _Py_IDENTIFIER(__contains__); |
|---|
| 5953 | n/a | |
|---|
| 5954 | n/a | func = lookup_maybe_method(self, &PyId___contains__, &unbound); |
|---|
| 5955 | n/a | if (func == Py_None) { |
|---|
| 5956 | n/a | Py_DECREF(func); |
|---|
| 5957 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 5958 | n/a | "'%.200s' object is not a container", |
|---|
| 5959 | n/a | Py_TYPE(self)->tp_name); |
|---|
| 5960 | n/a | return -1; |
|---|
| 5961 | n/a | } |
|---|
| 5962 | n/a | if (func != NULL) { |
|---|
| 5963 | n/a | PyObject *args[1] = {value}; |
|---|
| 5964 | n/a | res = call_unbound(unbound, func, self, args, 1); |
|---|
| 5965 | n/a | Py_DECREF(func); |
|---|
| 5966 | n/a | if (res != NULL) { |
|---|
| 5967 | n/a | result = PyObject_IsTrue(res); |
|---|
| 5968 | n/a | Py_DECREF(res); |
|---|
| 5969 | n/a | } |
|---|
| 5970 | n/a | } |
|---|
| 5971 | n/a | else if (! PyErr_Occurred()) { |
|---|
| 5972 | n/a | /* Possible results: -1 and 1 */ |
|---|
| 5973 | n/a | result = (int)_PySequence_IterSearch(self, value, |
|---|
| 5974 | n/a | PY_ITERSEARCH_CONTAINS); |
|---|
| 5975 | n/a | } |
|---|
| 5976 | n/a | return result; |
|---|
| 5977 | n/a | } |
|---|
| 5978 | n/a | |
|---|
| 5979 | n/a | #define slot_mp_length slot_sq_length |
|---|
| 5980 | n/a | |
|---|
| 5981 | n/a | SLOT1(slot_mp_subscript, "__getitem__", PyObject *) |
|---|
| 5982 | n/a | |
|---|
| 5983 | n/a | static int |
|---|
| 5984 | n/a | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) |
|---|
| 5985 | n/a | { |
|---|
| 5986 | n/a | PyObject *stack[2]; |
|---|
| 5987 | n/a | PyObject *res; |
|---|
| 5988 | n/a | |
|---|
| 5989 | n/a | stack[0] = key; |
|---|
| 5990 | n/a | if (value == NULL) { |
|---|
| 5991 | n/a | res = call_method(self, &PyId___delitem__, stack, 1); |
|---|
| 5992 | n/a | } |
|---|
| 5993 | n/a | else { |
|---|
| 5994 | n/a | stack[1] = value; |
|---|
| 5995 | n/a | res = call_method(self, &PyId___setitem__, stack, 2); |
|---|
| 5996 | n/a | } |
|---|
| 5997 | n/a | |
|---|
| 5998 | n/a | if (res == NULL) |
|---|
| 5999 | n/a | return -1; |
|---|
| 6000 | n/a | Py_DECREF(res); |
|---|
| 6001 | n/a | return 0; |
|---|
| 6002 | n/a | } |
|---|
| 6003 | n/a | |
|---|
| 6004 | n/a | SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__") |
|---|
| 6005 | n/a | SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__") |
|---|
| 6006 | n/a | SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__") |
|---|
| 6007 | n/a | SLOT1BIN(slot_nb_matrix_multiply, nb_matrix_multiply, "__matmul__", "__rmatmul__") |
|---|
| 6008 | n/a | SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__") |
|---|
| 6009 | n/a | SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__") |
|---|
| 6010 | n/a | |
|---|
| 6011 | n/a | static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); |
|---|
| 6012 | n/a | |
|---|
| 6013 | n/a | SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, |
|---|
| 6014 | n/a | nb_power, "__pow__", "__rpow__") |
|---|
| 6015 | n/a | |
|---|
| 6016 | n/a | static PyObject * |
|---|
| 6017 | n/a | slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) |
|---|
| 6018 | n/a | { |
|---|
| 6019 | n/a | _Py_IDENTIFIER(__pow__); |
|---|
| 6020 | n/a | |
|---|
| 6021 | n/a | if (modulus == Py_None) |
|---|
| 6022 | n/a | return slot_nb_power_binary(self, other); |
|---|
| 6023 | n/a | /* Three-arg power doesn't use __rpow__. But ternary_op |
|---|
| 6024 | n/a | can call this when the second argument's type uses |
|---|
| 6025 | n/a | slot_nb_power, so check before calling self.__pow__. */ |
|---|
| 6026 | n/a | if (Py_TYPE(self)->tp_as_number != NULL && |
|---|
| 6027 | n/a | Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) { |
|---|
| 6028 | n/a | PyObject* stack[2] = {other, modulus}; |
|---|
| 6029 | n/a | return call_method(self, &PyId___pow__, stack, 2); |
|---|
| 6030 | n/a | } |
|---|
| 6031 | n/a | Py_RETURN_NOTIMPLEMENTED; |
|---|
| 6032 | n/a | } |
|---|
| 6033 | n/a | |
|---|
| 6034 | n/a | SLOT0(slot_nb_negative, "__neg__") |
|---|
| 6035 | n/a | SLOT0(slot_nb_positive, "__pos__") |
|---|
| 6036 | n/a | SLOT0(slot_nb_absolute, "__abs__") |
|---|
| 6037 | n/a | |
|---|
| 6038 | n/a | static int |
|---|
| 6039 | n/a | slot_nb_bool(PyObject *self) |
|---|
| 6040 | n/a | { |
|---|
| 6041 | n/a | PyObject *func, *value; |
|---|
| 6042 | n/a | int result, unbound; |
|---|
| 6043 | n/a | int using_len = 0; |
|---|
| 6044 | n/a | _Py_IDENTIFIER(__bool__); |
|---|
| 6045 | n/a | |
|---|
| 6046 | n/a | func = lookup_maybe_method(self, &PyId___bool__, &unbound); |
|---|
| 6047 | n/a | if (func == NULL) { |
|---|
| 6048 | n/a | if (PyErr_Occurred()) { |
|---|
| 6049 | n/a | return -1; |
|---|
| 6050 | n/a | } |
|---|
| 6051 | n/a | |
|---|
| 6052 | n/a | func = lookup_maybe_method(self, &PyId___len__, &unbound); |
|---|
| 6053 | n/a | if (func == NULL) { |
|---|
| 6054 | n/a | if (PyErr_Occurred()) { |
|---|
| 6055 | n/a | return -1; |
|---|
| 6056 | n/a | } |
|---|
| 6057 | n/a | return 1; |
|---|
| 6058 | n/a | } |
|---|
| 6059 | n/a | using_len = 1; |
|---|
| 6060 | n/a | } |
|---|
| 6061 | n/a | |
|---|
| 6062 | n/a | value = call_unbound_noarg(unbound, func, self); |
|---|
| 6063 | n/a | if (value == NULL) { |
|---|
| 6064 | n/a | goto error; |
|---|
| 6065 | n/a | } |
|---|
| 6066 | n/a | |
|---|
| 6067 | n/a | if (using_len) { |
|---|
| 6068 | n/a | /* bool type enforced by slot_nb_len */ |
|---|
| 6069 | n/a | result = PyObject_IsTrue(value); |
|---|
| 6070 | n/a | } |
|---|
| 6071 | n/a | else if (PyBool_Check(value)) { |
|---|
| 6072 | n/a | result = PyObject_IsTrue(value); |
|---|
| 6073 | n/a | } |
|---|
| 6074 | n/a | else { |
|---|
| 6075 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 6076 | n/a | "__bool__ should return " |
|---|
| 6077 | n/a | "bool, returned %s", |
|---|
| 6078 | n/a | Py_TYPE(value)->tp_name); |
|---|
| 6079 | n/a | result = -1; |
|---|
| 6080 | n/a | } |
|---|
| 6081 | n/a | |
|---|
| 6082 | n/a | Py_DECREF(value); |
|---|
| 6083 | n/a | Py_DECREF(func); |
|---|
| 6084 | n/a | return result; |
|---|
| 6085 | n/a | |
|---|
| 6086 | n/a | error: |
|---|
| 6087 | n/a | Py_DECREF(func); |
|---|
| 6088 | n/a | return -1; |
|---|
| 6089 | n/a | } |
|---|
| 6090 | n/a | |
|---|
| 6091 | n/a | |
|---|
| 6092 | n/a | static PyObject * |
|---|
| 6093 | n/a | slot_nb_index(PyObject *self) |
|---|
| 6094 | n/a | { |
|---|
| 6095 | n/a | _Py_IDENTIFIER(__index__); |
|---|
| 6096 | n/a | return call_method(self, &PyId___index__, NULL, 0); |
|---|
| 6097 | n/a | } |
|---|
| 6098 | n/a | |
|---|
| 6099 | n/a | |
|---|
| 6100 | n/a | SLOT0(slot_nb_invert, "__invert__") |
|---|
| 6101 | n/a | SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") |
|---|
| 6102 | n/a | SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") |
|---|
| 6103 | n/a | SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") |
|---|
| 6104 | n/a | SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") |
|---|
| 6105 | n/a | SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") |
|---|
| 6106 | n/a | |
|---|
| 6107 | n/a | SLOT0(slot_nb_int, "__int__") |
|---|
| 6108 | n/a | SLOT0(slot_nb_float, "__float__") |
|---|
| 6109 | n/a | SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *) |
|---|
| 6110 | n/a | SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *) |
|---|
| 6111 | n/a | SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *) |
|---|
| 6112 | n/a | SLOT1(slot_nb_inplace_matrix_multiply, "__imatmul__", PyObject *) |
|---|
| 6113 | n/a | SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *) |
|---|
| 6114 | n/a | /* Can't use SLOT1 here, because nb_inplace_power is ternary */ |
|---|
| 6115 | n/a | static PyObject * |
|---|
| 6116 | n/a | slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2) |
|---|
| 6117 | n/a | { |
|---|
| 6118 | n/a | PyObject *stack[1] = {arg1}; |
|---|
| 6119 | n/a | _Py_IDENTIFIER(__ipow__); |
|---|
| 6120 | n/a | return call_method(self, &PyId___ipow__, stack, 1); |
|---|
| 6121 | n/a | } |
|---|
| 6122 | n/a | SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *) |
|---|
| 6123 | n/a | SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *) |
|---|
| 6124 | n/a | SLOT1(slot_nb_inplace_and, "__iand__", PyObject *) |
|---|
| 6125 | n/a | SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *) |
|---|
| 6126 | n/a | SLOT1(slot_nb_inplace_or, "__ior__", PyObject *) |
|---|
| 6127 | n/a | SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, |
|---|
| 6128 | n/a | "__floordiv__", "__rfloordiv__") |
|---|
| 6129 | n/a | SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__") |
|---|
| 6130 | n/a | SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *) |
|---|
| 6131 | n/a | SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *) |
|---|
| 6132 | n/a | |
|---|
| 6133 | n/a | static PyObject * |
|---|
| 6134 | n/a | slot_tp_repr(PyObject *self) |
|---|
| 6135 | n/a | { |
|---|
| 6136 | n/a | PyObject *func, *res; |
|---|
| 6137 | n/a | _Py_IDENTIFIER(__repr__); |
|---|
| 6138 | n/a | int unbound; |
|---|
| 6139 | n/a | |
|---|
| 6140 | n/a | func = lookup_method(self, &PyId___repr__, &unbound); |
|---|
| 6141 | n/a | if (func != NULL) { |
|---|
| 6142 | n/a | res = call_unbound_noarg(unbound, func, self); |
|---|
| 6143 | n/a | Py_DECREF(func); |
|---|
| 6144 | n/a | return res; |
|---|
| 6145 | n/a | } |
|---|
| 6146 | n/a | PyErr_Clear(); |
|---|
| 6147 | n/a | return PyUnicode_FromFormat("<%s object at %p>", |
|---|
| 6148 | n/a | Py_TYPE(self)->tp_name, self); |
|---|
| 6149 | n/a | } |
|---|
| 6150 | n/a | |
|---|
| 6151 | n/a | SLOT0(slot_tp_str, "__str__") |
|---|
| 6152 | n/a | |
|---|
| 6153 | n/a | static Py_hash_t |
|---|
| 6154 | n/a | slot_tp_hash(PyObject *self) |
|---|
| 6155 | n/a | { |
|---|
| 6156 | n/a | PyObject *func, *res; |
|---|
| 6157 | n/a | Py_ssize_t h; |
|---|
| 6158 | n/a | int unbound; |
|---|
| 6159 | n/a | |
|---|
| 6160 | n/a | func = lookup_method(self, &PyId___hash__, &unbound); |
|---|
| 6161 | n/a | |
|---|
| 6162 | n/a | if (func == Py_None) { |
|---|
| 6163 | n/a | Py_DECREF(func); |
|---|
| 6164 | n/a | func = NULL; |
|---|
| 6165 | n/a | } |
|---|
| 6166 | n/a | |
|---|
| 6167 | n/a | if (func == NULL) { |
|---|
| 6168 | n/a | return PyObject_HashNotImplemented(self); |
|---|
| 6169 | n/a | } |
|---|
| 6170 | n/a | |
|---|
| 6171 | n/a | res = call_unbound_noarg(unbound, func, self); |
|---|
| 6172 | n/a | Py_DECREF(func); |
|---|
| 6173 | n/a | if (res == NULL) |
|---|
| 6174 | n/a | return -1; |
|---|
| 6175 | n/a | |
|---|
| 6176 | n/a | if (!PyLong_Check(res)) { |
|---|
| 6177 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 6178 | n/a | "__hash__ method should return an integer"); |
|---|
| 6179 | n/a | return -1; |
|---|
| 6180 | n/a | } |
|---|
| 6181 | n/a | /* Transform the PyLong `res` to a Py_hash_t `h`. For an existing |
|---|
| 6182 | n/a | hashable Python object x, hash(x) will always lie within the range of |
|---|
| 6183 | n/a | Py_hash_t. Therefore our transformation must preserve values that |
|---|
| 6184 | n/a | already lie within this range, to ensure that if x.__hash__() returns |
|---|
| 6185 | n/a | hash(y) then hash(x) == hash(y). */ |
|---|
| 6186 | n/a | h = PyLong_AsSsize_t(res); |
|---|
| 6187 | n/a | if (h == -1 && PyErr_Occurred()) { |
|---|
| 6188 | n/a | /* res was not within the range of a Py_hash_t, so we're free to |
|---|
| 6189 | n/a | use any sufficiently bit-mixing transformation; |
|---|
| 6190 | n/a | long.__hash__ will do nicely. */ |
|---|
| 6191 | n/a | PyErr_Clear(); |
|---|
| 6192 | n/a | h = PyLong_Type.tp_hash(res); |
|---|
| 6193 | n/a | } |
|---|
| 6194 | n/a | /* -1 is reserved for errors. */ |
|---|
| 6195 | n/a | if (h == -1) |
|---|
| 6196 | n/a | h = -2; |
|---|
| 6197 | n/a | Py_DECREF(res); |
|---|
| 6198 | n/a | return h; |
|---|
| 6199 | n/a | } |
|---|
| 6200 | n/a | |
|---|
| 6201 | n/a | static PyObject * |
|---|
| 6202 | n/a | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) |
|---|
| 6203 | n/a | { |
|---|
| 6204 | n/a | _Py_IDENTIFIER(__call__); |
|---|
| 6205 | n/a | int unbound; |
|---|
| 6206 | n/a | PyObject *meth = lookup_method(self, &PyId___call__, &unbound); |
|---|
| 6207 | n/a | PyObject *res; |
|---|
| 6208 | n/a | |
|---|
| 6209 | n/a | if (meth == NULL) |
|---|
| 6210 | n/a | return NULL; |
|---|
| 6211 | n/a | |
|---|
| 6212 | n/a | if (unbound) { |
|---|
| 6213 | n/a | res = _PyObject_Call_Prepend(meth, self, args, kwds); |
|---|
| 6214 | n/a | } |
|---|
| 6215 | n/a | else { |
|---|
| 6216 | n/a | res = PyObject_Call(meth, args, kwds); |
|---|
| 6217 | n/a | } |
|---|
| 6218 | n/a | |
|---|
| 6219 | n/a | Py_DECREF(meth); |
|---|
| 6220 | n/a | return res; |
|---|
| 6221 | n/a | } |
|---|
| 6222 | n/a | |
|---|
| 6223 | n/a | /* There are two slot dispatch functions for tp_getattro. |
|---|
| 6224 | n/a | |
|---|
| 6225 | n/a | - slot_tp_getattro() is used when __getattribute__ is overridden |
|---|
| 6226 | n/a | but no __getattr__ hook is present; |
|---|
| 6227 | n/a | |
|---|
| 6228 | n/a | - slot_tp_getattr_hook() is used when a __getattr__ hook is present. |
|---|
| 6229 | n/a | |
|---|
| 6230 | n/a | The code in update_one_slot() always installs slot_tp_getattr_hook(); this |
|---|
| 6231 | n/a | detects the absence of __getattr__ and then installs the simpler slot if |
|---|
| 6232 | n/a | necessary. */ |
|---|
| 6233 | n/a | |
|---|
| 6234 | n/a | static PyObject * |
|---|
| 6235 | n/a | slot_tp_getattro(PyObject *self, PyObject *name) |
|---|
| 6236 | n/a | { |
|---|
| 6237 | n/a | PyObject *stack[1] = {name}; |
|---|
| 6238 | n/a | return call_method(self, &PyId___getattribute__, stack, 1); |
|---|
| 6239 | n/a | } |
|---|
| 6240 | n/a | |
|---|
| 6241 | n/a | static PyObject * |
|---|
| 6242 | n/a | call_attribute(PyObject *self, PyObject *attr, PyObject *name) |
|---|
| 6243 | n/a | { |
|---|
| 6244 | n/a | PyObject *res, *descr = NULL; |
|---|
| 6245 | n/a | descrgetfunc f = Py_TYPE(attr)->tp_descr_get; |
|---|
| 6246 | n/a | |
|---|
| 6247 | n/a | if (f != NULL) { |
|---|
| 6248 | n/a | descr = f(attr, self, (PyObject *)(Py_TYPE(self))); |
|---|
| 6249 | n/a | if (descr == NULL) |
|---|
| 6250 | n/a | return NULL; |
|---|
| 6251 | n/a | else |
|---|
| 6252 | n/a | attr = descr; |
|---|
| 6253 | n/a | } |
|---|
| 6254 | n/a | res = PyObject_CallFunctionObjArgs(attr, name, NULL); |
|---|
| 6255 | n/a | Py_XDECREF(descr); |
|---|
| 6256 | n/a | return res; |
|---|
| 6257 | n/a | } |
|---|
| 6258 | n/a | |
|---|
| 6259 | n/a | static PyObject * |
|---|
| 6260 | n/a | slot_tp_getattr_hook(PyObject *self, PyObject *name) |
|---|
| 6261 | n/a | { |
|---|
| 6262 | n/a | PyTypeObject *tp = Py_TYPE(self); |
|---|
| 6263 | n/a | PyObject *getattr, *getattribute, *res; |
|---|
| 6264 | n/a | _Py_IDENTIFIER(__getattr__); |
|---|
| 6265 | n/a | |
|---|
| 6266 | n/a | /* speed hack: we could use lookup_maybe, but that would resolve the |
|---|
| 6267 | n/a | method fully for each attribute lookup for classes with |
|---|
| 6268 | n/a | __getattr__, even when the attribute is present. So we use |
|---|
| 6269 | n/a | _PyType_Lookup and create the method only when needed, with |
|---|
| 6270 | n/a | call_attribute. */ |
|---|
| 6271 | n/a | getattr = _PyType_LookupId(tp, &PyId___getattr__); |
|---|
| 6272 | n/a | if (getattr == NULL) { |
|---|
| 6273 | n/a | /* No __getattr__ hook: use a simpler dispatcher */ |
|---|
| 6274 | n/a | tp->tp_getattro = slot_tp_getattro; |
|---|
| 6275 | n/a | return slot_tp_getattro(self, name); |
|---|
| 6276 | n/a | } |
|---|
| 6277 | n/a | Py_INCREF(getattr); |
|---|
| 6278 | n/a | /* speed hack: we could use lookup_maybe, but that would resolve the |
|---|
| 6279 | n/a | method fully for each attribute lookup for classes with |
|---|
| 6280 | n/a | __getattr__, even when self has the default __getattribute__ |
|---|
| 6281 | n/a | method. So we use _PyType_Lookup and create the method only when |
|---|
| 6282 | n/a | needed, with call_attribute. */ |
|---|
| 6283 | n/a | getattribute = _PyType_LookupId(tp, &PyId___getattribute__); |
|---|
| 6284 | n/a | if (getattribute == NULL || |
|---|
| 6285 | n/a | (Py_TYPE(getattribute) == &PyWrapperDescr_Type && |
|---|
| 6286 | n/a | ((PyWrapperDescrObject *)getattribute)->d_wrapped == |
|---|
| 6287 | n/a | (void *)PyObject_GenericGetAttr)) |
|---|
| 6288 | n/a | res = PyObject_GenericGetAttr(self, name); |
|---|
| 6289 | n/a | else { |
|---|
| 6290 | n/a | Py_INCREF(getattribute); |
|---|
| 6291 | n/a | res = call_attribute(self, getattribute, name); |
|---|
| 6292 | n/a | Py_DECREF(getattribute); |
|---|
| 6293 | n/a | } |
|---|
| 6294 | n/a | if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
|---|
| 6295 | n/a | PyErr_Clear(); |
|---|
| 6296 | n/a | res = call_attribute(self, getattr, name); |
|---|
| 6297 | n/a | } |
|---|
| 6298 | n/a | Py_DECREF(getattr); |
|---|
| 6299 | n/a | return res; |
|---|
| 6300 | n/a | } |
|---|
| 6301 | n/a | |
|---|
| 6302 | n/a | static int |
|---|
| 6303 | n/a | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) |
|---|
| 6304 | n/a | { |
|---|
| 6305 | n/a | PyObject *stack[2]; |
|---|
| 6306 | n/a | PyObject *res; |
|---|
| 6307 | n/a | _Py_IDENTIFIER(__delattr__); |
|---|
| 6308 | n/a | _Py_IDENTIFIER(__setattr__); |
|---|
| 6309 | n/a | |
|---|
| 6310 | n/a | stack[0] = name; |
|---|
| 6311 | n/a | if (value == NULL) { |
|---|
| 6312 | n/a | res = call_method(self, &PyId___delattr__, stack, 1); |
|---|
| 6313 | n/a | } |
|---|
| 6314 | n/a | else { |
|---|
| 6315 | n/a | stack[1] = value; |
|---|
| 6316 | n/a | res = call_method(self, &PyId___setattr__, stack, 2); |
|---|
| 6317 | n/a | } |
|---|
| 6318 | n/a | if (res == NULL) |
|---|
| 6319 | n/a | return -1; |
|---|
| 6320 | n/a | Py_DECREF(res); |
|---|
| 6321 | n/a | return 0; |
|---|
| 6322 | n/a | } |
|---|
| 6323 | n/a | |
|---|
| 6324 | n/a | static _Py_Identifier name_op[] = { |
|---|
| 6325 | n/a | {0, "__lt__", 0}, |
|---|
| 6326 | n/a | {0, "__le__", 0}, |
|---|
| 6327 | n/a | {0, "__eq__", 0}, |
|---|
| 6328 | n/a | {0, "__ne__", 0}, |
|---|
| 6329 | n/a | {0, "__gt__", 0}, |
|---|
| 6330 | n/a | {0, "__ge__", 0} |
|---|
| 6331 | n/a | }; |
|---|
| 6332 | n/a | |
|---|
| 6333 | n/a | static PyObject * |
|---|
| 6334 | n/a | slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
|---|
| 6335 | n/a | { |
|---|
| 6336 | n/a | int unbound; |
|---|
| 6337 | n/a | PyObject *func, *res; |
|---|
| 6338 | n/a | |
|---|
| 6339 | n/a | func = lookup_method(self, &name_op[op], &unbound); |
|---|
| 6340 | n/a | if (func == NULL) { |
|---|
| 6341 | n/a | PyErr_Clear(); |
|---|
| 6342 | n/a | Py_RETURN_NOTIMPLEMENTED; |
|---|
| 6343 | n/a | } |
|---|
| 6344 | n/a | |
|---|
| 6345 | n/a | PyObject *args[1] = {other}; |
|---|
| 6346 | n/a | res = call_unbound(unbound, func, self, args, 1); |
|---|
| 6347 | n/a | Py_DECREF(func); |
|---|
| 6348 | n/a | return res; |
|---|
| 6349 | n/a | } |
|---|
| 6350 | n/a | |
|---|
| 6351 | n/a | static PyObject * |
|---|
| 6352 | n/a | slot_tp_iter(PyObject *self) |
|---|
| 6353 | n/a | { |
|---|
| 6354 | n/a | int unbound; |
|---|
| 6355 | n/a | PyObject *func, *res; |
|---|
| 6356 | n/a | _Py_IDENTIFIER(__iter__); |
|---|
| 6357 | n/a | |
|---|
| 6358 | n/a | func = lookup_method(self, &PyId___iter__, &unbound); |
|---|
| 6359 | n/a | if (func == Py_None) { |
|---|
| 6360 | n/a | Py_DECREF(func); |
|---|
| 6361 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 6362 | n/a | "'%.200s' object is not iterable", |
|---|
| 6363 | n/a | Py_TYPE(self)->tp_name); |
|---|
| 6364 | n/a | return NULL; |
|---|
| 6365 | n/a | } |
|---|
| 6366 | n/a | |
|---|
| 6367 | n/a | if (func != NULL) { |
|---|
| 6368 | n/a | res = call_unbound_noarg(unbound, func, self); |
|---|
| 6369 | n/a | Py_DECREF(func); |
|---|
| 6370 | n/a | return res; |
|---|
| 6371 | n/a | } |
|---|
| 6372 | n/a | |
|---|
| 6373 | n/a | PyErr_Clear(); |
|---|
| 6374 | n/a | func = lookup_method(self, &PyId___getitem__, &unbound); |
|---|
| 6375 | n/a | if (func == NULL) { |
|---|
| 6376 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 6377 | n/a | "'%.200s' object is not iterable", |
|---|
| 6378 | n/a | Py_TYPE(self)->tp_name); |
|---|
| 6379 | n/a | return NULL; |
|---|
| 6380 | n/a | } |
|---|
| 6381 | n/a | Py_DECREF(func); |
|---|
| 6382 | n/a | return PySeqIter_New(self); |
|---|
| 6383 | n/a | } |
|---|
| 6384 | n/a | |
|---|
| 6385 | n/a | static PyObject * |
|---|
| 6386 | n/a | slot_tp_iternext(PyObject *self) |
|---|
| 6387 | n/a | { |
|---|
| 6388 | n/a | _Py_IDENTIFIER(__next__); |
|---|
| 6389 | n/a | return call_method(self, &PyId___next__, NULL, 0); |
|---|
| 6390 | n/a | } |
|---|
| 6391 | n/a | |
|---|
| 6392 | n/a | static PyObject * |
|---|
| 6393 | n/a | slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
|---|
| 6394 | n/a | { |
|---|
| 6395 | n/a | PyTypeObject *tp = Py_TYPE(self); |
|---|
| 6396 | n/a | PyObject *get; |
|---|
| 6397 | n/a | _Py_IDENTIFIER(__get__); |
|---|
| 6398 | n/a | |
|---|
| 6399 | n/a | get = _PyType_LookupId(tp, &PyId___get__); |
|---|
| 6400 | n/a | if (get == NULL) { |
|---|
| 6401 | n/a | /* Avoid further slowdowns */ |
|---|
| 6402 | n/a | if (tp->tp_descr_get == slot_tp_descr_get) |
|---|
| 6403 | n/a | tp->tp_descr_get = NULL; |
|---|
| 6404 | n/a | Py_INCREF(self); |
|---|
| 6405 | n/a | return self; |
|---|
| 6406 | n/a | } |
|---|
| 6407 | n/a | if (obj == NULL) |
|---|
| 6408 | n/a | obj = Py_None; |
|---|
| 6409 | n/a | if (type == NULL) |
|---|
| 6410 | n/a | type = Py_None; |
|---|
| 6411 | n/a | return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL); |
|---|
| 6412 | n/a | } |
|---|
| 6413 | n/a | |
|---|
| 6414 | n/a | static int |
|---|
| 6415 | n/a | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) |
|---|
| 6416 | n/a | { |
|---|
| 6417 | n/a | PyObject* stack[2]; |
|---|
| 6418 | n/a | PyObject *res; |
|---|
| 6419 | n/a | _Py_IDENTIFIER(__delete__); |
|---|
| 6420 | n/a | _Py_IDENTIFIER(__set__); |
|---|
| 6421 | n/a | |
|---|
| 6422 | n/a | stack[0] = target; |
|---|
| 6423 | n/a | if (value == NULL) { |
|---|
| 6424 | n/a | res = call_method(self, &PyId___delete__, stack, 1); |
|---|
| 6425 | n/a | } |
|---|
| 6426 | n/a | else { |
|---|
| 6427 | n/a | stack[1] = value; |
|---|
| 6428 | n/a | res = call_method(self, &PyId___set__, stack, 2); |
|---|
| 6429 | n/a | } |
|---|
| 6430 | n/a | if (res == NULL) |
|---|
| 6431 | n/a | return -1; |
|---|
| 6432 | n/a | Py_DECREF(res); |
|---|
| 6433 | n/a | return 0; |
|---|
| 6434 | n/a | } |
|---|
| 6435 | n/a | |
|---|
| 6436 | n/a | static int |
|---|
| 6437 | n/a | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
|---|
| 6438 | n/a | { |
|---|
| 6439 | n/a | _Py_IDENTIFIER(__init__); |
|---|
| 6440 | n/a | int unbound; |
|---|
| 6441 | n/a | PyObject *meth = lookup_method(self, &PyId___init__, &unbound); |
|---|
| 6442 | n/a | PyObject *res; |
|---|
| 6443 | n/a | |
|---|
| 6444 | n/a | if (meth == NULL) |
|---|
| 6445 | n/a | return -1; |
|---|
| 6446 | n/a | if (unbound) { |
|---|
| 6447 | n/a | res = _PyObject_Call_Prepend(meth, self, args, kwds); |
|---|
| 6448 | n/a | } |
|---|
| 6449 | n/a | else { |
|---|
| 6450 | n/a | res = PyObject_Call(meth, args, kwds); |
|---|
| 6451 | n/a | } |
|---|
| 6452 | n/a | Py_DECREF(meth); |
|---|
| 6453 | n/a | if (res == NULL) |
|---|
| 6454 | n/a | return -1; |
|---|
| 6455 | n/a | if (res != Py_None) { |
|---|
| 6456 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 6457 | n/a | "__init__() should return None, not '%.200s'", |
|---|
| 6458 | n/a | Py_TYPE(res)->tp_name); |
|---|
| 6459 | n/a | Py_DECREF(res); |
|---|
| 6460 | n/a | return -1; |
|---|
| 6461 | n/a | } |
|---|
| 6462 | n/a | Py_DECREF(res); |
|---|
| 6463 | n/a | return 0; |
|---|
| 6464 | n/a | } |
|---|
| 6465 | n/a | |
|---|
| 6466 | n/a | static PyObject * |
|---|
| 6467 | n/a | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 6468 | n/a | { |
|---|
| 6469 | n/a | PyObject *func, *result; |
|---|
| 6470 | n/a | |
|---|
| 6471 | n/a | func = _PyObject_GetAttrId((PyObject *)type, &PyId___new__); |
|---|
| 6472 | n/a | if (func == NULL) { |
|---|
| 6473 | n/a | return NULL; |
|---|
| 6474 | n/a | } |
|---|
| 6475 | n/a | |
|---|
| 6476 | n/a | result = _PyObject_Call_Prepend(func, (PyObject *)type, args, kwds); |
|---|
| 6477 | n/a | Py_DECREF(func); |
|---|
| 6478 | n/a | return result; |
|---|
| 6479 | n/a | } |
|---|
| 6480 | n/a | |
|---|
| 6481 | n/a | static void |
|---|
| 6482 | n/a | slot_tp_finalize(PyObject *self) |
|---|
| 6483 | n/a | { |
|---|
| 6484 | n/a | _Py_IDENTIFIER(__del__); |
|---|
| 6485 | n/a | int unbound; |
|---|
| 6486 | n/a | PyObject *del, *res; |
|---|
| 6487 | n/a | PyObject *error_type, *error_value, *error_traceback; |
|---|
| 6488 | n/a | |
|---|
| 6489 | n/a | /* Save the current exception, if any. */ |
|---|
| 6490 | n/a | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
|---|
| 6491 | n/a | |
|---|
| 6492 | n/a | /* Execute __del__ method, if any. */ |
|---|
| 6493 | n/a | del = lookup_maybe_method(self, &PyId___del__, &unbound); |
|---|
| 6494 | n/a | if (del != NULL) { |
|---|
| 6495 | n/a | res = call_unbound_noarg(unbound, del, self); |
|---|
| 6496 | n/a | if (res == NULL) |
|---|
| 6497 | n/a | PyErr_WriteUnraisable(del); |
|---|
| 6498 | n/a | else |
|---|
| 6499 | n/a | Py_DECREF(res); |
|---|
| 6500 | n/a | Py_DECREF(del); |
|---|
| 6501 | n/a | } |
|---|
| 6502 | n/a | |
|---|
| 6503 | n/a | /* Restore the saved exception. */ |
|---|
| 6504 | n/a | PyErr_Restore(error_type, error_value, error_traceback); |
|---|
| 6505 | n/a | } |
|---|
| 6506 | n/a | |
|---|
| 6507 | n/a | static PyObject * |
|---|
| 6508 | n/a | slot_am_await(PyObject *self) |
|---|
| 6509 | n/a | { |
|---|
| 6510 | n/a | int unbound; |
|---|
| 6511 | n/a | PyObject *func, *res; |
|---|
| 6512 | n/a | _Py_IDENTIFIER(__await__); |
|---|
| 6513 | n/a | |
|---|
| 6514 | n/a | func = lookup_method(self, &PyId___await__, &unbound); |
|---|
| 6515 | n/a | if (func != NULL) { |
|---|
| 6516 | n/a | res = call_unbound_noarg(unbound, func, self); |
|---|
| 6517 | n/a | Py_DECREF(func); |
|---|
| 6518 | n/a | return res; |
|---|
| 6519 | n/a | } |
|---|
| 6520 | n/a | PyErr_Format(PyExc_AttributeError, |
|---|
| 6521 | n/a | "object %.50s does not have __await__ method", |
|---|
| 6522 | n/a | Py_TYPE(self)->tp_name); |
|---|
| 6523 | n/a | return NULL; |
|---|
| 6524 | n/a | } |
|---|
| 6525 | n/a | |
|---|
| 6526 | n/a | static PyObject * |
|---|
| 6527 | n/a | slot_am_aiter(PyObject *self) |
|---|
| 6528 | n/a | { |
|---|
| 6529 | n/a | int unbound; |
|---|
| 6530 | n/a | PyObject *func, *res; |
|---|
| 6531 | n/a | _Py_IDENTIFIER(__aiter__); |
|---|
| 6532 | n/a | |
|---|
| 6533 | n/a | func = lookup_method(self, &PyId___aiter__, &unbound); |
|---|
| 6534 | n/a | if (func != NULL) { |
|---|
| 6535 | n/a | res = call_unbound_noarg(unbound, func, self); |
|---|
| 6536 | n/a | Py_DECREF(func); |
|---|
| 6537 | n/a | return res; |
|---|
| 6538 | n/a | } |
|---|
| 6539 | n/a | PyErr_Format(PyExc_AttributeError, |
|---|
| 6540 | n/a | "object %.50s does not have __aiter__ method", |
|---|
| 6541 | n/a | Py_TYPE(self)->tp_name); |
|---|
| 6542 | n/a | return NULL; |
|---|
| 6543 | n/a | } |
|---|
| 6544 | n/a | |
|---|
| 6545 | n/a | static PyObject * |
|---|
| 6546 | n/a | slot_am_anext(PyObject *self) |
|---|
| 6547 | n/a | { |
|---|
| 6548 | n/a | int unbound; |
|---|
| 6549 | n/a | PyObject *func, *res; |
|---|
| 6550 | n/a | _Py_IDENTIFIER(__anext__); |
|---|
| 6551 | n/a | |
|---|
| 6552 | n/a | func = lookup_method(self, &PyId___anext__, &unbound); |
|---|
| 6553 | n/a | if (func != NULL) { |
|---|
| 6554 | n/a | res = call_unbound_noarg(unbound, func, self); |
|---|
| 6555 | n/a | Py_DECREF(func); |
|---|
| 6556 | n/a | return res; |
|---|
| 6557 | n/a | } |
|---|
| 6558 | n/a | PyErr_Format(PyExc_AttributeError, |
|---|
| 6559 | n/a | "object %.50s does not have __anext__ method", |
|---|
| 6560 | n/a | Py_TYPE(self)->tp_name); |
|---|
| 6561 | n/a | return NULL; |
|---|
| 6562 | n/a | } |
|---|
| 6563 | n/a | |
|---|
| 6564 | n/a | /* |
|---|
| 6565 | n/a | Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper functions. |
|---|
| 6566 | n/a | |
|---|
| 6567 | n/a | The table is ordered by offsets relative to the 'PyHeapTypeObject' structure, |
|---|
| 6568 | n/a | which incorporates the additional structures used for numbers, sequences and |
|---|
| 6569 | n/a | mappings. Note that multiple names may map to the same slot (e.g. __eq__, |
|---|
| 6570 | n/a | __ne__ etc. all map to tp_richcompare) and one name may map to multiple slots |
|---|
| 6571 | n/a | (e.g. __str__ affects tp_str as well as tp_repr). The table is terminated with |
|---|
| 6572 | n/a | an all-zero entry. (This table is further initialized in init_slotdefs().) |
|---|
| 6573 | n/a | */ |
|---|
| 6574 | n/a | |
|---|
| 6575 | n/a | typedef struct wrapperbase slotdef; |
|---|
| 6576 | n/a | |
|---|
| 6577 | n/a | #undef TPSLOT |
|---|
| 6578 | n/a | #undef FLSLOT |
|---|
| 6579 | n/a | #undef AMSLOT |
|---|
| 6580 | n/a | #undef ETSLOT |
|---|
| 6581 | n/a | #undef SQSLOT |
|---|
| 6582 | n/a | #undef MPSLOT |
|---|
| 6583 | n/a | #undef NBSLOT |
|---|
| 6584 | n/a | #undef UNSLOT |
|---|
| 6585 | n/a | #undef IBSLOT |
|---|
| 6586 | n/a | #undef BINSLOT |
|---|
| 6587 | n/a | #undef RBINSLOT |
|---|
| 6588 | n/a | |
|---|
| 6589 | n/a | #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
|---|
| 6590 | n/a | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
|---|
| 6591 | n/a | PyDoc_STR(DOC)} |
|---|
| 6592 | n/a | #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ |
|---|
| 6593 | n/a | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
|---|
| 6594 | n/a | PyDoc_STR(DOC), FLAGS} |
|---|
| 6595 | n/a | #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
|---|
| 6596 | n/a | {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
|---|
| 6597 | n/a | PyDoc_STR(DOC)} |
|---|
| 6598 | n/a | #define AMSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
|---|
| 6599 | n/a | ETSLOT(NAME, as_async.SLOT, FUNCTION, WRAPPER, DOC) |
|---|
| 6600 | n/a | #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
|---|
| 6601 | n/a | ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) |
|---|
| 6602 | n/a | #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
|---|
| 6603 | n/a | ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) |
|---|
| 6604 | n/a | #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
|---|
| 6605 | n/a | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) |
|---|
| 6606 | n/a | #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
|---|
| 6607 | n/a | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
|---|
| 6608 | n/a | NAME "($self, /)\n--\n\n" DOC) |
|---|
| 6609 | n/a | #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
|---|
| 6610 | n/a | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
|---|
| 6611 | n/a | NAME "($self, value, /)\n--\n\nReturn self" DOC "value.") |
|---|
| 6612 | n/a | #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
|---|
| 6613 | n/a | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
|---|
| 6614 | n/a | NAME "($self, value, /)\n--\n\nReturn self" DOC "value.") |
|---|
| 6615 | n/a | #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
|---|
| 6616 | n/a | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
|---|
| 6617 | n/a | NAME "($self, value, /)\n--\n\nReturn value" DOC "self.") |
|---|
| 6618 | n/a | #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
|---|
| 6619 | n/a | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
|---|
| 6620 | n/a | NAME "($self, value, /)\n--\n\n" DOC) |
|---|
| 6621 | n/a | #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
|---|
| 6622 | n/a | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
|---|
| 6623 | n/a | NAME "($self, value, /)\n--\n\n" DOC) |
|---|
| 6624 | n/a | |
|---|
| 6625 | n/a | static slotdef slotdefs[] = { |
|---|
| 6626 | n/a | TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), |
|---|
| 6627 | n/a | TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), |
|---|
| 6628 | n/a | TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), |
|---|
| 6629 | n/a | TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), |
|---|
| 6630 | n/a | TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, |
|---|
| 6631 | n/a | "__repr__($self, /)\n--\n\nReturn repr(self)."), |
|---|
| 6632 | n/a | TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, |
|---|
| 6633 | n/a | "__hash__($self, /)\n--\n\nReturn hash(self)."), |
|---|
| 6634 | n/a | FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, |
|---|
| 6635 | n/a | "__call__($self, /, *args, **kwargs)\n--\n\nCall self as a function.", |
|---|
| 6636 | n/a | PyWrapperFlag_KEYWORDS), |
|---|
| 6637 | n/a | TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, |
|---|
| 6638 | n/a | "__str__($self, /)\n--\n\nReturn str(self)."), |
|---|
| 6639 | n/a | TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, |
|---|
| 6640 | n/a | wrap_binaryfunc, |
|---|
| 6641 | n/a | "__getattribute__($self, name, /)\n--\n\nReturn getattr(self, name)."), |
|---|
| 6642 | n/a | TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), |
|---|
| 6643 | n/a | TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, |
|---|
| 6644 | n/a | "__setattr__($self, name, value, /)\n--\n\nImplement setattr(self, name, value)."), |
|---|
| 6645 | n/a | TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, |
|---|
| 6646 | n/a | "__delattr__($self, name, /)\n--\n\nImplement delattr(self, name)."), |
|---|
| 6647 | n/a | TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, |
|---|
| 6648 | n/a | "__lt__($self, value, /)\n--\n\nReturn self<value."), |
|---|
| 6649 | n/a | TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, |
|---|
| 6650 | n/a | "__le__($self, value, /)\n--\n\nReturn self<=value."), |
|---|
| 6651 | n/a | TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, |
|---|
| 6652 | n/a | "__eq__($self, value, /)\n--\n\nReturn self==value."), |
|---|
| 6653 | n/a | TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, |
|---|
| 6654 | n/a | "__ne__($self, value, /)\n--\n\nReturn self!=value."), |
|---|
| 6655 | n/a | TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, |
|---|
| 6656 | n/a | "__gt__($self, value, /)\n--\n\nReturn self>value."), |
|---|
| 6657 | n/a | TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, |
|---|
| 6658 | n/a | "__ge__($self, value, /)\n--\n\nReturn self>=value."), |
|---|
| 6659 | n/a | TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, |
|---|
| 6660 | n/a | "__iter__($self, /)\n--\n\nImplement iter(self)."), |
|---|
| 6661 | n/a | TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next, |
|---|
| 6662 | n/a | "__next__($self, /)\n--\n\nImplement next(self)."), |
|---|
| 6663 | n/a | TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, |
|---|
| 6664 | n/a | "__get__($self, instance, owner, /)\n--\n\nReturn an attribute of instance, which is of type owner."), |
|---|
| 6665 | n/a | TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, |
|---|
| 6666 | n/a | "__set__($self, instance, value, /)\n--\n\nSet an attribute of instance to value."), |
|---|
| 6667 | n/a | TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set, |
|---|
| 6668 | n/a | wrap_descr_delete, |
|---|
| 6669 | n/a | "__delete__($self, instance, /)\n--\n\nDelete an attribute of instance."), |
|---|
| 6670 | n/a | FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init, |
|---|
| 6671 | n/a | "__init__($self, /, *args, **kwargs)\n--\n\n" |
|---|
| 6672 | n/a | "Initialize self. See help(type(self)) for accurate signature.", |
|---|
| 6673 | n/a | PyWrapperFlag_KEYWORDS), |
|---|
| 6674 | n/a | TPSLOT("__new__", tp_new, slot_tp_new, NULL, |
|---|
| 6675 | n/a | "__new__(type, /, *args, **kwargs)\n--\n\n" |
|---|
| 6676 | n/a | "Create and return new object. See help(type) for accurate signature."), |
|---|
| 6677 | n/a | TPSLOT("__del__", tp_finalize, slot_tp_finalize, (wrapperfunc)wrap_del, ""), |
|---|
| 6678 | n/a | |
|---|
| 6679 | n/a | AMSLOT("__await__", am_await, slot_am_await, wrap_unaryfunc, |
|---|
| 6680 | n/a | "__await__($self, /)\n--\n\nReturn an iterator to be used in await expression."), |
|---|
| 6681 | n/a | AMSLOT("__aiter__", am_aiter, slot_am_aiter, wrap_unaryfunc, |
|---|
| 6682 | n/a | "__aiter__($self, /)\n--\n\nReturn an awaitable, that resolves in asynchronous iterator."), |
|---|
| 6683 | n/a | AMSLOT("__anext__", am_anext, slot_am_anext, wrap_unaryfunc, |
|---|
| 6684 | n/a | "__anext__($self, /)\n--\n\nReturn a value or raise StopAsyncIteration."), |
|---|
| 6685 | n/a | |
|---|
| 6686 | n/a | BINSLOT("__add__", nb_add, slot_nb_add, |
|---|
| 6687 | n/a | "+"), |
|---|
| 6688 | n/a | RBINSLOT("__radd__", nb_add, slot_nb_add, |
|---|
| 6689 | n/a | "+"), |
|---|
| 6690 | n/a | BINSLOT("__sub__", nb_subtract, slot_nb_subtract, |
|---|
| 6691 | n/a | "-"), |
|---|
| 6692 | n/a | RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract, |
|---|
| 6693 | n/a | "-"), |
|---|
| 6694 | n/a | BINSLOT("__mul__", nb_multiply, slot_nb_multiply, |
|---|
| 6695 | n/a | "*"), |
|---|
| 6696 | n/a | RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply, |
|---|
| 6697 | n/a | "*"), |
|---|
| 6698 | n/a | BINSLOT("__mod__", nb_remainder, slot_nb_remainder, |
|---|
| 6699 | n/a | "%"), |
|---|
| 6700 | n/a | RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder, |
|---|
| 6701 | n/a | "%"), |
|---|
| 6702 | n/a | BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod, |
|---|
| 6703 | n/a | "Return divmod(self, value)."), |
|---|
| 6704 | n/a | RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod, |
|---|
| 6705 | n/a | "Return divmod(value, self)."), |
|---|
| 6706 | n/a | NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, |
|---|
| 6707 | n/a | "__pow__($self, value, mod=None, /)\n--\n\nReturn pow(self, value, mod)."), |
|---|
| 6708 | n/a | NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, |
|---|
| 6709 | n/a | "__rpow__($self, value, mod=None, /)\n--\n\nReturn pow(value, self, mod)."), |
|---|
| 6710 | n/a | UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-self"), |
|---|
| 6711 | n/a | UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+self"), |
|---|
| 6712 | n/a | UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, |
|---|
| 6713 | n/a | "abs(self)"), |
|---|
| 6714 | n/a | UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred, |
|---|
| 6715 | n/a | "self != 0"), |
|---|
| 6716 | n/a | UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~self"), |
|---|
| 6717 | n/a | BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), |
|---|
| 6718 | n/a | RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), |
|---|
| 6719 | n/a | BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), |
|---|
| 6720 | n/a | RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"), |
|---|
| 6721 | n/a | BINSLOT("__and__", nb_and, slot_nb_and, "&"), |
|---|
| 6722 | n/a | RBINSLOT("__rand__", nb_and, slot_nb_and, "&"), |
|---|
| 6723 | n/a | BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"), |
|---|
| 6724 | n/a | RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), |
|---|
| 6725 | n/a | BINSLOT("__or__", nb_or, slot_nb_or, "|"), |
|---|
| 6726 | n/a | RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), |
|---|
| 6727 | n/a | UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, |
|---|
| 6728 | n/a | "int(self)"), |
|---|
| 6729 | n/a | UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, |
|---|
| 6730 | n/a | "float(self)"), |
|---|
| 6731 | n/a | IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, |
|---|
| 6732 | n/a | wrap_binaryfunc, "+="), |
|---|
| 6733 | n/a | IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, |
|---|
| 6734 | n/a | wrap_binaryfunc, "-="), |
|---|
| 6735 | n/a | IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply, |
|---|
| 6736 | n/a | wrap_binaryfunc, "*="), |
|---|
| 6737 | n/a | IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder, |
|---|
| 6738 | n/a | wrap_binaryfunc, "%="), |
|---|
| 6739 | n/a | IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power, |
|---|
| 6740 | n/a | wrap_binaryfunc, "**="), |
|---|
| 6741 | n/a | IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift, |
|---|
| 6742 | n/a | wrap_binaryfunc, "<<="), |
|---|
| 6743 | n/a | IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift, |
|---|
| 6744 | n/a | wrap_binaryfunc, ">>="), |
|---|
| 6745 | n/a | IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and, |
|---|
| 6746 | n/a | wrap_binaryfunc, "&="), |
|---|
| 6747 | n/a | IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor, |
|---|
| 6748 | n/a | wrap_binaryfunc, "^="), |
|---|
| 6749 | n/a | IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or, |
|---|
| 6750 | n/a | wrap_binaryfunc, "|="), |
|---|
| 6751 | n/a | BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
|---|
| 6752 | n/a | RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
|---|
| 6753 | n/a | BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"), |
|---|
| 6754 | n/a | RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"), |
|---|
| 6755 | n/a | IBSLOT("__ifloordiv__", nb_inplace_floor_divide, |
|---|
| 6756 | n/a | slot_nb_inplace_floor_divide, wrap_binaryfunc, "//="), |
|---|
| 6757 | n/a | IBSLOT("__itruediv__", nb_inplace_true_divide, |
|---|
| 6758 | n/a | slot_nb_inplace_true_divide, wrap_binaryfunc, "/="), |
|---|
| 6759 | n/a | NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc, |
|---|
| 6760 | n/a | "__index__($self, /)\n--\n\n" |
|---|
| 6761 | n/a | "Return self converted to an integer, if self is suitable " |
|---|
| 6762 | n/a | "for use as an index into a list."), |
|---|
| 6763 | n/a | BINSLOT("__matmul__", nb_matrix_multiply, slot_nb_matrix_multiply, |
|---|
| 6764 | n/a | "@"), |
|---|
| 6765 | n/a | RBINSLOT("__rmatmul__", nb_matrix_multiply, slot_nb_matrix_multiply, |
|---|
| 6766 | n/a | "@"), |
|---|
| 6767 | n/a | IBSLOT("__imatmul__", nb_inplace_matrix_multiply, slot_nb_inplace_matrix_multiply, |
|---|
| 6768 | n/a | wrap_binaryfunc, "@="), |
|---|
| 6769 | n/a | MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc, |
|---|
| 6770 | n/a | "__len__($self, /)\n--\n\nReturn len(self)."), |
|---|
| 6771 | n/a | MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, |
|---|
| 6772 | n/a | wrap_binaryfunc, |
|---|
| 6773 | n/a | "__getitem__($self, key, /)\n--\n\nReturn self[key]."), |
|---|
| 6774 | n/a | MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, |
|---|
| 6775 | n/a | wrap_objobjargproc, |
|---|
| 6776 | n/a | "__setitem__($self, key, value, /)\n--\n\nSet self[key] to value."), |
|---|
| 6777 | n/a | MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, |
|---|
| 6778 | n/a | wrap_delitem, |
|---|
| 6779 | n/a | "__delitem__($self, key, /)\n--\n\nDelete self[key]."), |
|---|
| 6780 | n/a | |
|---|
| 6781 | n/a | SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc, |
|---|
| 6782 | n/a | "__len__($self, /)\n--\n\nReturn len(self)."), |
|---|
| 6783 | n/a | /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL. |
|---|
| 6784 | n/a | The logic in abstract.c always falls back to nb_add/nb_multiply in |
|---|
| 6785 | n/a | this case. Defining both the nb_* and the sq_* slots to call the |
|---|
| 6786 | n/a | user-defined methods has unexpected side-effects, as shown by |
|---|
| 6787 | n/a | test_descr.notimplemented() */ |
|---|
| 6788 | n/a | SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc, |
|---|
| 6789 | n/a | "__add__($self, value, /)\n--\n\nReturn self+value."), |
|---|
| 6790 | n/a | SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc, |
|---|
| 6791 | n/a | "__mul__($self, value, /)\n--\n\nReturn self*value.n"), |
|---|
| 6792 | n/a | SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc, |
|---|
| 6793 | n/a | "__rmul__($self, value, /)\n--\n\nReturn self*value."), |
|---|
| 6794 | n/a | SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, |
|---|
| 6795 | n/a | "__getitem__($self, key, /)\n--\n\nReturn self[key]."), |
|---|
| 6796 | n/a | SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, |
|---|
| 6797 | n/a | "__setitem__($self, key, value, /)\n--\n\nSet self[key] to value."), |
|---|
| 6798 | n/a | SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, |
|---|
| 6799 | n/a | "__delitem__($self, key, /)\n--\n\nDelete self[key]."), |
|---|
| 6800 | n/a | SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, |
|---|
| 6801 | n/a | "__contains__($self, key, /)\n--\n\nReturn key in self."), |
|---|
| 6802 | n/a | SQSLOT("__iadd__", sq_inplace_concat, NULL, |
|---|
| 6803 | n/a | wrap_binaryfunc, |
|---|
| 6804 | n/a | "__iadd__($self, value, /)\n--\n\nImplement self+=value."), |
|---|
| 6805 | n/a | SQSLOT("__imul__", sq_inplace_repeat, NULL, |
|---|
| 6806 | n/a | wrap_indexargfunc, |
|---|
| 6807 | n/a | "__imul__($self, value, /)\n--\n\nImplement self*=value."), |
|---|
| 6808 | n/a | |
|---|
| 6809 | n/a | {NULL} |
|---|
| 6810 | n/a | }; |
|---|
| 6811 | n/a | |
|---|
| 6812 | n/a | /* Given a type pointer and an offset gotten from a slotdef entry, return a |
|---|
| 6813 | n/a | pointer to the actual slot. This is not quite the same as simply adding |
|---|
| 6814 | n/a | the offset to the type pointer, since it takes care to indirect through the |
|---|
| 6815 | n/a | proper indirection pointer (as_buffer, etc.); it returns NULL if the |
|---|
| 6816 | n/a | indirection pointer is NULL. */ |
|---|
| 6817 | n/a | static void ** |
|---|
| 6818 | n/a | slotptr(PyTypeObject *type, int ioffset) |
|---|
| 6819 | n/a | { |
|---|
| 6820 | n/a | char *ptr; |
|---|
| 6821 | n/a | long offset = ioffset; |
|---|
| 6822 | n/a | |
|---|
| 6823 | n/a | /* Note: this depends on the order of the members of PyHeapTypeObject! */ |
|---|
| 6824 | n/a | assert(offset >= 0); |
|---|
| 6825 | n/a | assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer)); |
|---|
| 6826 | n/a | if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) { |
|---|
| 6827 | n/a | ptr = (char *)type->tp_as_sequence; |
|---|
| 6828 | n/a | offset -= offsetof(PyHeapTypeObject, as_sequence); |
|---|
| 6829 | n/a | } |
|---|
| 6830 | n/a | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) { |
|---|
| 6831 | n/a | ptr = (char *)type->tp_as_mapping; |
|---|
| 6832 | n/a | offset -= offsetof(PyHeapTypeObject, as_mapping); |
|---|
| 6833 | n/a | } |
|---|
| 6834 | n/a | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) { |
|---|
| 6835 | n/a | ptr = (char *)type->tp_as_number; |
|---|
| 6836 | n/a | offset -= offsetof(PyHeapTypeObject, as_number); |
|---|
| 6837 | n/a | } |
|---|
| 6838 | n/a | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_async)) { |
|---|
| 6839 | n/a | ptr = (char *)type->tp_as_async; |
|---|
| 6840 | n/a | offset -= offsetof(PyHeapTypeObject, as_async); |
|---|
| 6841 | n/a | } |
|---|
| 6842 | n/a | else { |
|---|
| 6843 | n/a | ptr = (char *)type; |
|---|
| 6844 | n/a | } |
|---|
| 6845 | n/a | if (ptr != NULL) |
|---|
| 6846 | n/a | ptr += offset; |
|---|
| 6847 | n/a | return (void **)ptr; |
|---|
| 6848 | n/a | } |
|---|
| 6849 | n/a | |
|---|
| 6850 | n/a | /* Length of array of slotdef pointers used to store slots with the |
|---|
| 6851 | n/a | same __name__. There should be at most MAX_EQUIV-1 slotdef entries with |
|---|
| 6852 | n/a | the same __name__, for any __name__. Since that's a static property, it is |
|---|
| 6853 | n/a | appropriate to declare fixed-size arrays for this. */ |
|---|
| 6854 | n/a | #define MAX_EQUIV 10 |
|---|
| 6855 | n/a | |
|---|
| 6856 | n/a | /* Return a slot pointer for a given name, but ONLY if the attribute has |
|---|
| 6857 | n/a | exactly one slot function. The name must be an interned string. */ |
|---|
| 6858 | n/a | static void ** |
|---|
| 6859 | n/a | resolve_slotdups(PyTypeObject *type, PyObject *name) |
|---|
| 6860 | n/a | { |
|---|
| 6861 | n/a | /* XXX Maybe this could be optimized more -- but is it worth it? */ |
|---|
| 6862 | n/a | |
|---|
| 6863 | n/a | /* pname and ptrs act as a little cache */ |
|---|
| 6864 | n/a | static PyObject *pname; |
|---|
| 6865 | n/a | static slotdef *ptrs[MAX_EQUIV]; |
|---|
| 6866 | n/a | slotdef *p, **pp; |
|---|
| 6867 | n/a | void **res, **ptr; |
|---|
| 6868 | n/a | |
|---|
| 6869 | n/a | if (pname != name) { |
|---|
| 6870 | n/a | /* Collect all slotdefs that match name into ptrs. */ |
|---|
| 6871 | n/a | pname = name; |
|---|
| 6872 | n/a | pp = ptrs; |
|---|
| 6873 | n/a | for (p = slotdefs; p->name_strobj; p++) { |
|---|
| 6874 | n/a | if (p->name_strobj == name) |
|---|
| 6875 | n/a | *pp++ = p; |
|---|
| 6876 | n/a | } |
|---|
| 6877 | n/a | *pp = NULL; |
|---|
| 6878 | n/a | } |
|---|
| 6879 | n/a | |
|---|
| 6880 | n/a | /* Look in all matching slots of the type; if exactly one of these has |
|---|
| 6881 | n/a | a filled-in slot, return its value. Otherwise return NULL. */ |
|---|
| 6882 | n/a | res = NULL; |
|---|
| 6883 | n/a | for (pp = ptrs; *pp; pp++) { |
|---|
| 6884 | n/a | ptr = slotptr(type, (*pp)->offset); |
|---|
| 6885 | n/a | if (ptr == NULL || *ptr == NULL) |
|---|
| 6886 | n/a | continue; |
|---|
| 6887 | n/a | if (res != NULL) |
|---|
| 6888 | n/a | return NULL; |
|---|
| 6889 | n/a | res = ptr; |
|---|
| 6890 | n/a | } |
|---|
| 6891 | n/a | return res; |
|---|
| 6892 | n/a | } |
|---|
| 6893 | n/a | |
|---|
| 6894 | n/a | /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This |
|---|
| 6895 | n/a | does some incredibly complex thinking and then sticks something into the |
|---|
| 6896 | n/a | slot. (It sees if the adjacent slotdefs for the same slot have conflicting |
|---|
| 6897 | n/a | interests, and then stores a generic wrapper or a specific function into |
|---|
| 6898 | n/a | the slot.) Return a pointer to the next slotdef with a different offset, |
|---|
| 6899 | n/a | because that's convenient for fixup_slot_dispatchers(). */ |
|---|
| 6900 | n/a | static slotdef * |
|---|
| 6901 | n/a | update_one_slot(PyTypeObject *type, slotdef *p) |
|---|
| 6902 | n/a | { |
|---|
| 6903 | n/a | PyObject *descr; |
|---|
| 6904 | n/a | PyWrapperDescrObject *d; |
|---|
| 6905 | n/a | void *generic = NULL, *specific = NULL; |
|---|
| 6906 | n/a | int use_generic = 0; |
|---|
| 6907 | n/a | int offset = p->offset; |
|---|
| 6908 | n/a | void **ptr = slotptr(type, offset); |
|---|
| 6909 | n/a | |
|---|
| 6910 | n/a | if (ptr == NULL) { |
|---|
| 6911 | n/a | do { |
|---|
| 6912 | n/a | ++p; |
|---|
| 6913 | n/a | } while (p->offset == offset); |
|---|
| 6914 | n/a | return p; |
|---|
| 6915 | n/a | } |
|---|
| 6916 | n/a | do { |
|---|
| 6917 | n/a | descr = _PyType_Lookup(type, p->name_strobj); |
|---|
| 6918 | n/a | if (descr == NULL) { |
|---|
| 6919 | n/a | if (ptr == (void**)&type->tp_iternext) { |
|---|
| 6920 | n/a | specific = (void *)_PyObject_NextNotImplemented; |
|---|
| 6921 | n/a | } |
|---|
| 6922 | n/a | continue; |
|---|
| 6923 | n/a | } |
|---|
| 6924 | n/a | if (Py_TYPE(descr) == &PyWrapperDescr_Type && |
|---|
| 6925 | n/a | ((PyWrapperDescrObject *)descr)->d_base->name_strobj == p->name_strobj) { |
|---|
| 6926 | n/a | void **tptr = resolve_slotdups(type, p->name_strobj); |
|---|
| 6927 | n/a | if (tptr == NULL || tptr == ptr) |
|---|
| 6928 | n/a | generic = p->function; |
|---|
| 6929 | n/a | d = (PyWrapperDescrObject *)descr; |
|---|
| 6930 | n/a | if (d->d_base->wrapper == p->wrapper && |
|---|
| 6931 | n/a | PyType_IsSubtype(type, PyDescr_TYPE(d))) |
|---|
| 6932 | n/a | { |
|---|
| 6933 | n/a | if (specific == NULL || |
|---|
| 6934 | n/a | specific == d->d_wrapped) |
|---|
| 6935 | n/a | specific = d->d_wrapped; |
|---|
| 6936 | n/a | else |
|---|
| 6937 | n/a | use_generic = 1; |
|---|
| 6938 | n/a | } |
|---|
| 6939 | n/a | } |
|---|
| 6940 | n/a | else if (Py_TYPE(descr) == &PyCFunction_Type && |
|---|
| 6941 | n/a | PyCFunction_GET_FUNCTION(descr) == |
|---|
| 6942 | n/a | (PyCFunction)tp_new_wrapper && |
|---|
| 6943 | n/a | ptr == (void**)&type->tp_new) |
|---|
| 6944 | n/a | { |
|---|
| 6945 | n/a | /* The __new__ wrapper is not a wrapper descriptor, |
|---|
| 6946 | n/a | so must be special-cased differently. |
|---|
| 6947 | n/a | If we don't do this, creating an instance will |
|---|
| 6948 | n/a | always use slot_tp_new which will look up |
|---|
| 6949 | n/a | __new__ in the MRO which will call tp_new_wrapper |
|---|
| 6950 | n/a | which will look through the base classes looking |
|---|
| 6951 | n/a | for a static base and call its tp_new (usually |
|---|
| 6952 | n/a | PyType_GenericNew), after performing various |
|---|
| 6953 | n/a | sanity checks and constructing a new argument |
|---|
| 6954 | n/a | list. Cut all that nonsense short -- this speeds |
|---|
| 6955 | n/a | up instance creation tremendously. */ |
|---|
| 6956 | n/a | specific = (void *)type->tp_new; |
|---|
| 6957 | n/a | /* XXX I'm not 100% sure that there isn't a hole |
|---|
| 6958 | n/a | in this reasoning that requires additional |
|---|
| 6959 | n/a | sanity checks. I'll buy the first person to |
|---|
| 6960 | n/a | point out a bug in this reasoning a beer. */ |
|---|
| 6961 | n/a | } |
|---|
| 6962 | n/a | else if (descr == Py_None && |
|---|
| 6963 | n/a | ptr == (void**)&type->tp_hash) { |
|---|
| 6964 | n/a | /* We specifically allow __hash__ to be set to None |
|---|
| 6965 | n/a | to prevent inheritance of the default |
|---|
| 6966 | n/a | implementation from object.__hash__ */ |
|---|
| 6967 | n/a | specific = (void *)PyObject_HashNotImplemented; |
|---|
| 6968 | n/a | } |
|---|
| 6969 | n/a | else { |
|---|
| 6970 | n/a | use_generic = 1; |
|---|
| 6971 | n/a | generic = p->function; |
|---|
| 6972 | n/a | } |
|---|
| 6973 | n/a | } while ((++p)->offset == offset); |
|---|
| 6974 | n/a | if (specific && !use_generic) |
|---|
| 6975 | n/a | *ptr = specific; |
|---|
| 6976 | n/a | else |
|---|
| 6977 | n/a | *ptr = generic; |
|---|
| 6978 | n/a | return p; |
|---|
| 6979 | n/a | } |
|---|
| 6980 | n/a | |
|---|
| 6981 | n/a | /* In the type, update the slots whose slotdefs are gathered in the pp array. |
|---|
| 6982 | n/a | This is a callback for update_subclasses(). */ |
|---|
| 6983 | n/a | static int |
|---|
| 6984 | n/a | update_slots_callback(PyTypeObject *type, void *data) |
|---|
| 6985 | n/a | { |
|---|
| 6986 | n/a | slotdef **pp = (slotdef **)data; |
|---|
| 6987 | n/a | |
|---|
| 6988 | n/a | for (; *pp; pp++) |
|---|
| 6989 | n/a | update_one_slot(type, *pp); |
|---|
| 6990 | n/a | return 0; |
|---|
| 6991 | n/a | } |
|---|
| 6992 | n/a | |
|---|
| 6993 | n/a | static int slotdefs_initialized = 0; |
|---|
| 6994 | n/a | /* Initialize the slotdefs table by adding interned string objects for the |
|---|
| 6995 | n/a | names. */ |
|---|
| 6996 | n/a | static void |
|---|
| 6997 | n/a | init_slotdefs(void) |
|---|
| 6998 | n/a | { |
|---|
| 6999 | n/a | slotdef *p; |
|---|
| 7000 | n/a | |
|---|
| 7001 | n/a | if (slotdefs_initialized) |
|---|
| 7002 | n/a | return; |
|---|
| 7003 | n/a | for (p = slotdefs; p->name; p++) { |
|---|
| 7004 | n/a | /* Slots must be ordered by their offset in the PyHeapTypeObject. */ |
|---|
| 7005 | n/a | assert(!p[1].name || p->offset <= p[1].offset); |
|---|
| 7006 | n/a | p->name_strobj = PyUnicode_InternFromString(p->name); |
|---|
| 7007 | n/a | if (!p->name_strobj) |
|---|
| 7008 | n/a | Py_FatalError("Out of memory interning slotdef names"); |
|---|
| 7009 | n/a | } |
|---|
| 7010 | n/a | slotdefs_initialized = 1; |
|---|
| 7011 | n/a | } |
|---|
| 7012 | n/a | |
|---|
| 7013 | n/a | /* Undo init_slotdefs, releasing the interned strings. */ |
|---|
| 7014 | n/a | static void clear_slotdefs(void) |
|---|
| 7015 | n/a | { |
|---|
| 7016 | n/a | slotdef *p; |
|---|
| 7017 | n/a | for (p = slotdefs; p->name; p++) { |
|---|
| 7018 | n/a | Py_CLEAR(p->name_strobj); |
|---|
| 7019 | n/a | } |
|---|
| 7020 | n/a | slotdefs_initialized = 0; |
|---|
| 7021 | n/a | } |
|---|
| 7022 | n/a | |
|---|
| 7023 | n/a | /* Update the slots after assignment to a class (type) attribute. */ |
|---|
| 7024 | n/a | static int |
|---|
| 7025 | n/a | update_slot(PyTypeObject *type, PyObject *name) |
|---|
| 7026 | n/a | { |
|---|
| 7027 | n/a | slotdef *ptrs[MAX_EQUIV]; |
|---|
| 7028 | n/a | slotdef *p; |
|---|
| 7029 | n/a | slotdef **pp; |
|---|
| 7030 | n/a | int offset; |
|---|
| 7031 | n/a | |
|---|
| 7032 | n/a | /* Clear the VALID_VERSION flag of 'type' and all its |
|---|
| 7033 | n/a | subclasses. This could possibly be unified with the |
|---|
| 7034 | n/a | update_subclasses() recursion below, but carefully: |
|---|
| 7035 | n/a | they each have their own conditions on which to stop |
|---|
| 7036 | n/a | recursing into subclasses. */ |
|---|
| 7037 | n/a | PyType_Modified(type); |
|---|
| 7038 | n/a | |
|---|
| 7039 | n/a | init_slotdefs(); |
|---|
| 7040 | n/a | pp = ptrs; |
|---|
| 7041 | n/a | for (p = slotdefs; p->name; p++) { |
|---|
| 7042 | n/a | /* XXX assume name is interned! */ |
|---|
| 7043 | n/a | if (p->name_strobj == name) |
|---|
| 7044 | n/a | *pp++ = p; |
|---|
| 7045 | n/a | } |
|---|
| 7046 | n/a | *pp = NULL; |
|---|
| 7047 | n/a | for (pp = ptrs; *pp; pp++) { |
|---|
| 7048 | n/a | p = *pp; |
|---|
| 7049 | n/a | offset = p->offset; |
|---|
| 7050 | n/a | while (p > slotdefs && (p-1)->offset == offset) |
|---|
| 7051 | n/a | --p; |
|---|
| 7052 | n/a | *pp = p; |
|---|
| 7053 | n/a | } |
|---|
| 7054 | n/a | if (ptrs[0] == NULL) |
|---|
| 7055 | n/a | return 0; /* Not an attribute that affects any slots */ |
|---|
| 7056 | n/a | return update_subclasses(type, name, |
|---|
| 7057 | n/a | update_slots_callback, (void *)ptrs); |
|---|
| 7058 | n/a | } |
|---|
| 7059 | n/a | |
|---|
| 7060 | n/a | /* Store the proper functions in the slot dispatches at class (type) |
|---|
| 7061 | n/a | definition time, based upon which operations the class overrides in its |
|---|
| 7062 | n/a | dict. */ |
|---|
| 7063 | n/a | static void |
|---|
| 7064 | n/a | fixup_slot_dispatchers(PyTypeObject *type) |
|---|
| 7065 | n/a | { |
|---|
| 7066 | n/a | slotdef *p; |
|---|
| 7067 | n/a | |
|---|
| 7068 | n/a | init_slotdefs(); |
|---|
| 7069 | n/a | for (p = slotdefs; p->name; ) |
|---|
| 7070 | n/a | p = update_one_slot(type, p); |
|---|
| 7071 | n/a | } |
|---|
| 7072 | n/a | |
|---|
| 7073 | n/a | static void |
|---|
| 7074 | n/a | update_all_slots(PyTypeObject* type) |
|---|
| 7075 | n/a | { |
|---|
| 7076 | n/a | slotdef *p; |
|---|
| 7077 | n/a | |
|---|
| 7078 | n/a | init_slotdefs(); |
|---|
| 7079 | n/a | for (p = slotdefs; p->name; p++) { |
|---|
| 7080 | n/a | /* update_slot returns int but can't actually fail */ |
|---|
| 7081 | n/a | update_slot(type, p->name_strobj); |
|---|
| 7082 | n/a | } |
|---|
| 7083 | n/a | } |
|---|
| 7084 | n/a | |
|---|
| 7085 | n/a | /* Call __set_name__ on all descriptors in a newly generated type */ |
|---|
| 7086 | n/a | static int |
|---|
| 7087 | n/a | set_names(PyTypeObject *type) |
|---|
| 7088 | n/a | { |
|---|
| 7089 | n/a | PyObject *names_to_set, *key, *value, *set_name, *tmp; |
|---|
| 7090 | n/a | Py_ssize_t i = 0; |
|---|
| 7091 | n/a | |
|---|
| 7092 | n/a | names_to_set = PyDict_Copy(type->tp_dict); |
|---|
| 7093 | n/a | if (names_to_set == NULL) |
|---|
| 7094 | n/a | return -1; |
|---|
| 7095 | n/a | |
|---|
| 7096 | n/a | while (PyDict_Next(names_to_set, &i, &key, &value)) { |
|---|
| 7097 | n/a | set_name = lookup_maybe(value, &PyId___set_name__); |
|---|
| 7098 | n/a | if (set_name != NULL) { |
|---|
| 7099 | n/a | tmp = PyObject_CallFunctionObjArgs(set_name, type, key, NULL); |
|---|
| 7100 | n/a | Py_DECREF(set_name); |
|---|
| 7101 | n/a | if (tmp == NULL) { |
|---|
| 7102 | n/a | _PyErr_FormatFromCause(PyExc_RuntimeError, |
|---|
| 7103 | n/a | "Error calling __set_name__ on '%.100s' instance %R " |
|---|
| 7104 | n/a | "in '%.100s'", |
|---|
| 7105 | n/a | value->ob_type->tp_name, key, type->tp_name); |
|---|
| 7106 | n/a | Py_DECREF(names_to_set); |
|---|
| 7107 | n/a | return -1; |
|---|
| 7108 | n/a | } |
|---|
| 7109 | n/a | else |
|---|
| 7110 | n/a | Py_DECREF(tmp); |
|---|
| 7111 | n/a | } |
|---|
| 7112 | n/a | else if (PyErr_Occurred()) { |
|---|
| 7113 | n/a | Py_DECREF(names_to_set); |
|---|
| 7114 | n/a | return -1; |
|---|
| 7115 | n/a | } |
|---|
| 7116 | n/a | } |
|---|
| 7117 | n/a | |
|---|
| 7118 | n/a | Py_DECREF(names_to_set); |
|---|
| 7119 | n/a | return 0; |
|---|
| 7120 | n/a | } |
|---|
| 7121 | n/a | |
|---|
| 7122 | n/a | /* Call __init_subclass__ on the parent of a newly generated type */ |
|---|
| 7123 | n/a | static int |
|---|
| 7124 | n/a | init_subclass(PyTypeObject *type, PyObject *kwds) |
|---|
| 7125 | n/a | { |
|---|
| 7126 | n/a | PyObject *super, *func, *result; |
|---|
| 7127 | n/a | PyObject *args[2] = {(PyObject *)type, (PyObject *)type}; |
|---|
| 7128 | n/a | |
|---|
| 7129 | n/a | super = _PyObject_FastCall((PyObject *)&PySuper_Type, args, 2); |
|---|
| 7130 | n/a | if (super == NULL) { |
|---|
| 7131 | n/a | return -1; |
|---|
| 7132 | n/a | } |
|---|
| 7133 | n/a | |
|---|
| 7134 | n/a | func = _PyObject_GetAttrId(super, &PyId___init_subclass__); |
|---|
| 7135 | n/a | Py_DECREF(super); |
|---|
| 7136 | n/a | if (func == NULL) { |
|---|
| 7137 | n/a | return -1; |
|---|
| 7138 | n/a | } |
|---|
| 7139 | n/a | |
|---|
| 7140 | n/a | |
|---|
| 7141 | n/a | result = _PyObject_FastCallDict(func, NULL, 0, kwds); |
|---|
| 7142 | n/a | Py_DECREF(func); |
|---|
| 7143 | n/a | if (result == NULL) { |
|---|
| 7144 | n/a | return -1; |
|---|
| 7145 | n/a | } |
|---|
| 7146 | n/a | |
|---|
| 7147 | n/a | Py_DECREF(result); |
|---|
| 7148 | n/a | return 0; |
|---|
| 7149 | n/a | } |
|---|
| 7150 | n/a | |
|---|
| 7151 | n/a | /* recurse_down_subclasses() and update_subclasses() are mutually |
|---|
| 7152 | n/a | recursive functions to call a callback for all subclasses, |
|---|
| 7153 | n/a | but refraining from recursing into subclasses that define 'name'. */ |
|---|
| 7154 | n/a | |
|---|
| 7155 | n/a | static int |
|---|
| 7156 | n/a | update_subclasses(PyTypeObject *type, PyObject *name, |
|---|
| 7157 | n/a | update_callback callback, void *data) |
|---|
| 7158 | n/a | { |
|---|
| 7159 | n/a | if (callback(type, data) < 0) |
|---|
| 7160 | n/a | return -1; |
|---|
| 7161 | n/a | return recurse_down_subclasses(type, name, callback, data); |
|---|
| 7162 | n/a | } |
|---|
| 7163 | n/a | |
|---|
| 7164 | n/a | static int |
|---|
| 7165 | n/a | recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
|---|
| 7166 | n/a | update_callback callback, void *data) |
|---|
| 7167 | n/a | { |
|---|
| 7168 | n/a | PyTypeObject *subclass; |
|---|
| 7169 | n/a | PyObject *ref, *subclasses, *dict; |
|---|
| 7170 | n/a | Py_ssize_t i; |
|---|
| 7171 | n/a | |
|---|
| 7172 | n/a | subclasses = type->tp_subclasses; |
|---|
| 7173 | n/a | if (subclasses == NULL) |
|---|
| 7174 | n/a | return 0; |
|---|
| 7175 | n/a | assert(PyDict_CheckExact(subclasses)); |
|---|
| 7176 | n/a | i = 0; |
|---|
| 7177 | n/a | while (PyDict_Next(subclasses, &i, NULL, &ref)) { |
|---|
| 7178 | n/a | assert(PyWeakref_CheckRef(ref)); |
|---|
| 7179 | n/a | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
|---|
| 7180 | n/a | assert(subclass != NULL); |
|---|
| 7181 | n/a | if ((PyObject *)subclass == Py_None) |
|---|
| 7182 | n/a | continue; |
|---|
| 7183 | n/a | assert(PyType_Check(subclass)); |
|---|
| 7184 | n/a | /* Avoid recursing down into unaffected classes */ |
|---|
| 7185 | n/a | dict = subclass->tp_dict; |
|---|
| 7186 | n/a | if (dict != NULL && PyDict_Check(dict) && |
|---|
| 7187 | n/a | PyDict_GetItem(dict, name) != NULL) |
|---|
| 7188 | n/a | continue; |
|---|
| 7189 | n/a | if (update_subclasses(subclass, name, callback, data) < 0) |
|---|
| 7190 | n/a | return -1; |
|---|
| 7191 | n/a | } |
|---|
| 7192 | n/a | return 0; |
|---|
| 7193 | n/a | } |
|---|
| 7194 | n/a | |
|---|
| 7195 | n/a | /* This function is called by PyType_Ready() to populate the type's |
|---|
| 7196 | n/a | dictionary with method descriptors for function slots. For each |
|---|
| 7197 | n/a | function slot (like tp_repr) that's defined in the type, one or more |
|---|
| 7198 | n/a | corresponding descriptors are added in the type's tp_dict dictionary |
|---|
| 7199 | n/a | under the appropriate name (like __repr__). Some function slots |
|---|
| 7200 | n/a | cause more than one descriptor to be added (for example, the nb_add |
|---|
| 7201 | n/a | slot adds both __add__ and __radd__ descriptors) and some function |
|---|
| 7202 | n/a | slots compete for the same descriptor (for example both sq_item and |
|---|
| 7203 | n/a | mp_subscript generate a __getitem__ descriptor). |
|---|
| 7204 | n/a | |
|---|
| 7205 | n/a | In the latter case, the first slotdef entry encountered wins. Since |
|---|
| 7206 | n/a | slotdef entries are sorted by the offset of the slot in the |
|---|
| 7207 | n/a | PyHeapTypeObject, this gives us some control over disambiguating |
|---|
| 7208 | n/a | between competing slots: the members of PyHeapTypeObject are listed |
|---|
| 7209 | n/a | from most general to least general, so the most general slot is |
|---|
| 7210 | n/a | preferred. In particular, because as_mapping comes before as_sequence, |
|---|
| 7211 | n/a | for a type that defines both mp_subscript and sq_item, mp_subscript |
|---|
| 7212 | n/a | wins. |
|---|
| 7213 | n/a | |
|---|
| 7214 | n/a | This only adds new descriptors and doesn't overwrite entries in |
|---|
| 7215 | n/a | tp_dict that were previously defined. The descriptors contain a |
|---|
| 7216 | n/a | reference to the C function they must call, so that it's safe if they |
|---|
| 7217 | n/a | are copied into a subtype's __dict__ and the subtype has a different |
|---|
| 7218 | n/a | C function in its slot -- calling the method defined by the |
|---|
| 7219 | n/a | descriptor will call the C function that was used to create it, |
|---|
| 7220 | n/a | rather than the C function present in the slot when it is called. |
|---|
| 7221 | n/a | (This is important because a subtype may have a C function in the |
|---|
| 7222 | n/a | slot that calls the method from the dictionary, and we want to avoid |
|---|
| 7223 | n/a | infinite recursion here.) */ |
|---|
| 7224 | n/a | |
|---|
| 7225 | n/a | static int |
|---|
| 7226 | n/a | add_operators(PyTypeObject *type) |
|---|
| 7227 | n/a | { |
|---|
| 7228 | n/a | PyObject *dict = type->tp_dict; |
|---|
| 7229 | n/a | slotdef *p; |
|---|
| 7230 | n/a | PyObject *descr; |
|---|
| 7231 | n/a | void **ptr; |
|---|
| 7232 | n/a | |
|---|
| 7233 | n/a | init_slotdefs(); |
|---|
| 7234 | n/a | for (p = slotdefs; p->name; p++) { |
|---|
| 7235 | n/a | if (p->wrapper == NULL) |
|---|
| 7236 | n/a | continue; |
|---|
| 7237 | n/a | ptr = slotptr(type, p->offset); |
|---|
| 7238 | n/a | if (!ptr || !*ptr) |
|---|
| 7239 | n/a | continue; |
|---|
| 7240 | n/a | if (PyDict_GetItem(dict, p->name_strobj)) |
|---|
| 7241 | n/a | continue; |
|---|
| 7242 | n/a | if (*ptr == (void *)PyObject_HashNotImplemented) { |
|---|
| 7243 | n/a | /* Classes may prevent the inheritance of the tp_hash |
|---|
| 7244 | n/a | slot by storing PyObject_HashNotImplemented in it. Make it |
|---|
| 7245 | n/a | visible as a None value for the __hash__ attribute. */ |
|---|
| 7246 | n/a | if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0) |
|---|
| 7247 | n/a | return -1; |
|---|
| 7248 | n/a | } |
|---|
| 7249 | n/a | else { |
|---|
| 7250 | n/a | descr = PyDescr_NewWrapper(type, p, *ptr); |
|---|
| 7251 | n/a | if (descr == NULL) |
|---|
| 7252 | n/a | return -1; |
|---|
| 7253 | n/a | if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) { |
|---|
| 7254 | n/a | Py_DECREF(descr); |
|---|
| 7255 | n/a | return -1; |
|---|
| 7256 | n/a | } |
|---|
| 7257 | n/a | Py_DECREF(descr); |
|---|
| 7258 | n/a | } |
|---|
| 7259 | n/a | } |
|---|
| 7260 | n/a | if (type->tp_new != NULL) { |
|---|
| 7261 | n/a | if (add_tp_new_wrapper(type) < 0) |
|---|
| 7262 | n/a | return -1; |
|---|
| 7263 | n/a | } |
|---|
| 7264 | n/a | return 0; |
|---|
| 7265 | n/a | } |
|---|
| 7266 | n/a | |
|---|
| 7267 | n/a | |
|---|
| 7268 | n/a | /* Cooperative 'super' */ |
|---|
| 7269 | n/a | |
|---|
| 7270 | n/a | typedef struct { |
|---|
| 7271 | n/a | PyObject_HEAD |
|---|
| 7272 | n/a | PyTypeObject *type; |
|---|
| 7273 | n/a | PyObject *obj; |
|---|
| 7274 | n/a | PyTypeObject *obj_type; |
|---|
| 7275 | n/a | } superobject; |
|---|
| 7276 | n/a | |
|---|
| 7277 | n/a | static PyMemberDef super_members[] = { |
|---|
| 7278 | n/a | {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY, |
|---|
| 7279 | n/a | "the class invoking super()"}, |
|---|
| 7280 | n/a | {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY, |
|---|
| 7281 | n/a | "the instance invoking super(); may be None"}, |
|---|
| 7282 | n/a | {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY, |
|---|
| 7283 | n/a | "the type of the instance invoking super(); may be None"}, |
|---|
| 7284 | n/a | {0} |
|---|
| 7285 | n/a | }; |
|---|
| 7286 | n/a | |
|---|
| 7287 | n/a | static void |
|---|
| 7288 | n/a | super_dealloc(PyObject *self) |
|---|
| 7289 | n/a | { |
|---|
| 7290 | n/a | superobject *su = (superobject *)self; |
|---|
| 7291 | n/a | |
|---|
| 7292 | n/a | _PyObject_GC_UNTRACK(self); |
|---|
| 7293 | n/a | Py_XDECREF(su->obj); |
|---|
| 7294 | n/a | Py_XDECREF(su->type); |
|---|
| 7295 | n/a | Py_XDECREF(su->obj_type); |
|---|
| 7296 | n/a | Py_TYPE(self)->tp_free(self); |
|---|
| 7297 | n/a | } |
|---|
| 7298 | n/a | |
|---|
| 7299 | n/a | static PyObject * |
|---|
| 7300 | n/a | super_repr(PyObject *self) |
|---|
| 7301 | n/a | { |
|---|
| 7302 | n/a | superobject *su = (superobject *)self; |
|---|
| 7303 | n/a | |
|---|
| 7304 | n/a | if (su->obj_type) |
|---|
| 7305 | n/a | return PyUnicode_FromFormat( |
|---|
| 7306 | n/a | "<super: <class '%s'>, <%s object>>", |
|---|
| 7307 | n/a | su->type ? su->type->tp_name : "NULL", |
|---|
| 7308 | n/a | su->obj_type->tp_name); |
|---|
| 7309 | n/a | else |
|---|
| 7310 | n/a | return PyUnicode_FromFormat( |
|---|
| 7311 | n/a | "<super: <class '%s'>, NULL>", |
|---|
| 7312 | n/a | su->type ? su->type->tp_name : "NULL"); |
|---|
| 7313 | n/a | } |
|---|
| 7314 | n/a | |
|---|
| 7315 | n/a | static PyObject * |
|---|
| 7316 | n/a | super_getattro(PyObject *self, PyObject *name) |
|---|
| 7317 | n/a | { |
|---|
| 7318 | n/a | superobject *su = (superobject *)self; |
|---|
| 7319 | n/a | PyTypeObject *starttype; |
|---|
| 7320 | n/a | PyObject *mro; |
|---|
| 7321 | n/a | Py_ssize_t i, n; |
|---|
| 7322 | n/a | |
|---|
| 7323 | n/a | starttype = su->obj_type; |
|---|
| 7324 | n/a | if (starttype == NULL) |
|---|
| 7325 | n/a | goto skip; |
|---|
| 7326 | n/a | |
|---|
| 7327 | n/a | /* We want __class__ to return the class of the super object |
|---|
| 7328 | n/a | (i.e. super, or a subclass), not the class of su->obj. */ |
|---|
| 7329 | n/a | if (PyUnicode_Check(name) && |
|---|
| 7330 | n/a | PyUnicode_GET_LENGTH(name) == 9 && |
|---|
| 7331 | n/a | _PyUnicode_EqualToASCIIId(name, &PyId___class__)) |
|---|
| 7332 | n/a | goto skip; |
|---|
| 7333 | n/a | |
|---|
| 7334 | n/a | mro = starttype->tp_mro; |
|---|
| 7335 | n/a | if (mro == NULL) |
|---|
| 7336 | n/a | goto skip; |
|---|
| 7337 | n/a | |
|---|
| 7338 | n/a | assert(PyTuple_Check(mro)); |
|---|
| 7339 | n/a | n = PyTuple_GET_SIZE(mro); |
|---|
| 7340 | n/a | |
|---|
| 7341 | n/a | /* No need to check the last one: it's gonna be skipped anyway. */ |
|---|
| 7342 | n/a | for (i = 0; i+1 < n; i++) { |
|---|
| 7343 | n/a | if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) |
|---|
| 7344 | n/a | break; |
|---|
| 7345 | n/a | } |
|---|
| 7346 | n/a | i++; /* skip su->type (if any) */ |
|---|
| 7347 | n/a | if (i >= n) |
|---|
| 7348 | n/a | goto skip; |
|---|
| 7349 | n/a | |
|---|
| 7350 | n/a | /* keep a strong reference to mro because starttype->tp_mro can be |
|---|
| 7351 | n/a | replaced during PyDict_GetItem(dict, name) */ |
|---|
| 7352 | n/a | Py_INCREF(mro); |
|---|
| 7353 | n/a | do { |
|---|
| 7354 | n/a | PyObject *res, *tmp, *dict; |
|---|
| 7355 | n/a | descrgetfunc f; |
|---|
| 7356 | n/a | |
|---|
| 7357 | n/a | tmp = PyTuple_GET_ITEM(mro, i); |
|---|
| 7358 | n/a | assert(PyType_Check(tmp)); |
|---|
| 7359 | n/a | |
|---|
| 7360 | n/a | dict = ((PyTypeObject *)tmp)->tp_dict; |
|---|
| 7361 | n/a | assert(dict != NULL && PyDict_Check(dict)); |
|---|
| 7362 | n/a | |
|---|
| 7363 | n/a | res = PyDict_GetItem(dict, name); |
|---|
| 7364 | n/a | if (res != NULL) { |
|---|
| 7365 | n/a | Py_INCREF(res); |
|---|
| 7366 | n/a | |
|---|
| 7367 | n/a | f = Py_TYPE(res)->tp_descr_get; |
|---|
| 7368 | n/a | if (f != NULL) { |
|---|
| 7369 | n/a | tmp = f(res, |
|---|
| 7370 | n/a | /* Only pass 'obj' param if this is instance-mode super |
|---|
| 7371 | n/a | (See SF ID #743627) */ |
|---|
| 7372 | n/a | (su->obj == (PyObject *)starttype) ? NULL : su->obj, |
|---|
| 7373 | n/a | (PyObject *)starttype); |
|---|
| 7374 | n/a | Py_DECREF(res); |
|---|
| 7375 | n/a | res = tmp; |
|---|
| 7376 | n/a | } |
|---|
| 7377 | n/a | |
|---|
| 7378 | n/a | Py_DECREF(mro); |
|---|
| 7379 | n/a | return res; |
|---|
| 7380 | n/a | } |
|---|
| 7381 | n/a | |
|---|
| 7382 | n/a | i++; |
|---|
| 7383 | n/a | } while (i < n); |
|---|
| 7384 | n/a | Py_DECREF(mro); |
|---|
| 7385 | n/a | |
|---|
| 7386 | n/a | skip: |
|---|
| 7387 | n/a | return PyObject_GenericGetAttr(self, name); |
|---|
| 7388 | n/a | } |
|---|
| 7389 | n/a | |
|---|
| 7390 | n/a | static PyTypeObject * |
|---|
| 7391 | n/a | supercheck(PyTypeObject *type, PyObject *obj) |
|---|
| 7392 | n/a | { |
|---|
| 7393 | n/a | /* Check that a super() call makes sense. Return a type object. |
|---|
| 7394 | n/a | |
|---|
| 7395 | n/a | obj can be a class, or an instance of one: |
|---|
| 7396 | n/a | |
|---|
| 7397 | n/a | - If it is a class, it must be a subclass of 'type'. This case is |
|---|
| 7398 | n/a | used for class methods; the return value is obj. |
|---|
| 7399 | n/a | |
|---|
| 7400 | n/a | - If it is an instance, it must be an instance of 'type'. This is |
|---|
| 7401 | n/a | the normal case; the return value is obj.__class__. |
|---|
| 7402 | n/a | |
|---|
| 7403 | n/a | But... when obj is an instance, we want to allow for the case where |
|---|
| 7404 | n/a | Py_TYPE(obj) is not a subclass of type, but obj.__class__ is! |
|---|
| 7405 | n/a | This will allow using super() with a proxy for obj. |
|---|
| 7406 | n/a | */ |
|---|
| 7407 | n/a | |
|---|
| 7408 | n/a | /* Check for first bullet above (special case) */ |
|---|
| 7409 | n/a | if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) { |
|---|
| 7410 | n/a | Py_INCREF(obj); |
|---|
| 7411 | n/a | return (PyTypeObject *)obj; |
|---|
| 7412 | n/a | } |
|---|
| 7413 | n/a | |
|---|
| 7414 | n/a | /* Normal case */ |
|---|
| 7415 | n/a | if (PyType_IsSubtype(Py_TYPE(obj), type)) { |
|---|
| 7416 | n/a | Py_INCREF(Py_TYPE(obj)); |
|---|
| 7417 | n/a | return Py_TYPE(obj); |
|---|
| 7418 | n/a | } |
|---|
| 7419 | n/a | else { |
|---|
| 7420 | n/a | /* Try the slow way */ |
|---|
| 7421 | n/a | PyObject *class_attr; |
|---|
| 7422 | n/a | |
|---|
| 7423 | n/a | class_attr = _PyObject_GetAttrId(obj, &PyId___class__); |
|---|
| 7424 | n/a | if (class_attr != NULL && |
|---|
| 7425 | n/a | PyType_Check(class_attr) && |
|---|
| 7426 | n/a | (PyTypeObject *)class_attr != Py_TYPE(obj)) |
|---|
| 7427 | n/a | { |
|---|
| 7428 | n/a | int ok = PyType_IsSubtype( |
|---|
| 7429 | n/a | (PyTypeObject *)class_attr, type); |
|---|
| 7430 | n/a | if (ok) |
|---|
| 7431 | n/a | return (PyTypeObject *)class_attr; |
|---|
| 7432 | n/a | } |
|---|
| 7433 | n/a | |
|---|
| 7434 | n/a | if (class_attr == NULL) |
|---|
| 7435 | n/a | PyErr_Clear(); |
|---|
| 7436 | n/a | else |
|---|
| 7437 | n/a | Py_DECREF(class_attr); |
|---|
| 7438 | n/a | } |
|---|
| 7439 | n/a | |
|---|
| 7440 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 7441 | n/a | "super(type, obj): " |
|---|
| 7442 | n/a | "obj must be an instance or subtype of type"); |
|---|
| 7443 | n/a | return NULL; |
|---|
| 7444 | n/a | } |
|---|
| 7445 | n/a | |
|---|
| 7446 | n/a | static PyObject * |
|---|
| 7447 | n/a | super_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
|---|
| 7448 | n/a | { |
|---|
| 7449 | n/a | superobject *su = (superobject *)self; |
|---|
| 7450 | n/a | superobject *newobj; |
|---|
| 7451 | n/a | |
|---|
| 7452 | n/a | if (obj == NULL || obj == Py_None || su->obj != NULL) { |
|---|
| 7453 | n/a | /* Not binding to an object, or already bound */ |
|---|
| 7454 | n/a | Py_INCREF(self); |
|---|
| 7455 | n/a | return self; |
|---|
| 7456 | n/a | } |
|---|
| 7457 | n/a | if (Py_TYPE(su) != &PySuper_Type) |
|---|
| 7458 | n/a | /* If su is an instance of a (strict) subclass of super, |
|---|
| 7459 | n/a | call its type */ |
|---|
| 7460 | n/a | return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su), |
|---|
| 7461 | n/a | su->type, obj, NULL); |
|---|
| 7462 | n/a | else { |
|---|
| 7463 | n/a | /* Inline the common case */ |
|---|
| 7464 | n/a | PyTypeObject *obj_type = supercheck(su->type, obj); |
|---|
| 7465 | n/a | if (obj_type == NULL) |
|---|
| 7466 | n/a | return NULL; |
|---|
| 7467 | n/a | newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type, |
|---|
| 7468 | n/a | NULL, NULL); |
|---|
| 7469 | n/a | if (newobj == NULL) |
|---|
| 7470 | n/a | return NULL; |
|---|
| 7471 | n/a | Py_INCREF(su->type); |
|---|
| 7472 | n/a | Py_INCREF(obj); |
|---|
| 7473 | n/a | newobj->type = su->type; |
|---|
| 7474 | n/a | newobj->obj = obj; |
|---|
| 7475 | n/a | newobj->obj_type = obj_type; |
|---|
| 7476 | n/a | return (PyObject *)newobj; |
|---|
| 7477 | n/a | } |
|---|
| 7478 | n/a | } |
|---|
| 7479 | n/a | |
|---|
| 7480 | n/a | static int |
|---|
| 7481 | n/a | super_init(PyObject *self, PyObject *args, PyObject *kwds) |
|---|
| 7482 | n/a | { |
|---|
| 7483 | n/a | superobject *su = (superobject *)self; |
|---|
| 7484 | n/a | PyTypeObject *type = NULL; |
|---|
| 7485 | n/a | PyObject *obj = NULL; |
|---|
| 7486 | n/a | PyTypeObject *obj_type = NULL; |
|---|
| 7487 | n/a | |
|---|
| 7488 | n/a | if (!_PyArg_NoKeywords("super", kwds)) |
|---|
| 7489 | n/a | return -1; |
|---|
| 7490 | n/a | if (!PyArg_ParseTuple(args, "|O!O:super", &PyType_Type, &type, &obj)) |
|---|
| 7491 | n/a | return -1; |
|---|
| 7492 | n/a | |
|---|
| 7493 | n/a | if (type == NULL) { |
|---|
| 7494 | n/a | /* Call super(), without args -- fill in from __class__ |
|---|
| 7495 | n/a | and first local variable on the stack. */ |
|---|
| 7496 | n/a | PyFrameObject *f; |
|---|
| 7497 | n/a | PyCodeObject *co; |
|---|
| 7498 | n/a | Py_ssize_t i, n; |
|---|
| 7499 | n/a | f = PyThreadState_GET()->frame; |
|---|
| 7500 | n/a | if (f == NULL) { |
|---|
| 7501 | n/a | PyErr_SetString(PyExc_RuntimeError, |
|---|
| 7502 | n/a | "super(): no current frame"); |
|---|
| 7503 | n/a | return -1; |
|---|
| 7504 | n/a | } |
|---|
| 7505 | n/a | co = f->f_code; |
|---|
| 7506 | n/a | if (co == NULL) { |
|---|
| 7507 | n/a | PyErr_SetString(PyExc_RuntimeError, |
|---|
| 7508 | n/a | "super(): no code object"); |
|---|
| 7509 | n/a | return -1; |
|---|
| 7510 | n/a | } |
|---|
| 7511 | n/a | if (co->co_argcount == 0) { |
|---|
| 7512 | n/a | PyErr_SetString(PyExc_RuntimeError, |
|---|
| 7513 | n/a | "super(): no arguments"); |
|---|
| 7514 | n/a | return -1; |
|---|
| 7515 | n/a | } |
|---|
| 7516 | n/a | obj = f->f_localsplus[0]; |
|---|
| 7517 | n/a | if (obj == NULL && co->co_cell2arg) { |
|---|
| 7518 | n/a | /* The first argument might be a cell. */ |
|---|
| 7519 | n/a | n = PyTuple_GET_SIZE(co->co_cellvars); |
|---|
| 7520 | n/a | for (i = 0; i < n; i++) { |
|---|
| 7521 | n/a | if (co->co_cell2arg[i] == 0) { |
|---|
| 7522 | n/a | PyObject *cell = f->f_localsplus[co->co_nlocals + i]; |
|---|
| 7523 | n/a | assert(PyCell_Check(cell)); |
|---|
| 7524 | n/a | obj = PyCell_GET(cell); |
|---|
| 7525 | n/a | break; |
|---|
| 7526 | n/a | } |
|---|
| 7527 | n/a | } |
|---|
| 7528 | n/a | } |
|---|
| 7529 | n/a | if (obj == NULL) { |
|---|
| 7530 | n/a | PyErr_SetString(PyExc_RuntimeError, |
|---|
| 7531 | n/a | "super(): arg[0] deleted"); |
|---|
| 7532 | n/a | return -1; |
|---|
| 7533 | n/a | } |
|---|
| 7534 | n/a | if (co->co_freevars == NULL) |
|---|
| 7535 | n/a | n = 0; |
|---|
| 7536 | n/a | else { |
|---|
| 7537 | n/a | assert(PyTuple_Check(co->co_freevars)); |
|---|
| 7538 | n/a | n = PyTuple_GET_SIZE(co->co_freevars); |
|---|
| 7539 | n/a | } |
|---|
| 7540 | n/a | for (i = 0; i < n; i++) { |
|---|
| 7541 | n/a | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
|---|
| 7542 | n/a | assert(PyUnicode_Check(name)); |
|---|
| 7543 | n/a | if (_PyUnicode_EqualToASCIIId(name, &PyId___class__)) { |
|---|
| 7544 | n/a | Py_ssize_t index = co->co_nlocals + |
|---|
| 7545 | n/a | PyTuple_GET_SIZE(co->co_cellvars) + i; |
|---|
| 7546 | n/a | PyObject *cell = f->f_localsplus[index]; |
|---|
| 7547 | n/a | if (cell == NULL || !PyCell_Check(cell)) { |
|---|
| 7548 | n/a | PyErr_SetString(PyExc_RuntimeError, |
|---|
| 7549 | n/a | "super(): bad __class__ cell"); |
|---|
| 7550 | n/a | return -1; |
|---|
| 7551 | n/a | } |
|---|
| 7552 | n/a | type = (PyTypeObject *) PyCell_GET(cell); |
|---|
| 7553 | n/a | if (type == NULL) { |
|---|
| 7554 | n/a | PyErr_SetString(PyExc_RuntimeError, |
|---|
| 7555 | n/a | "super(): empty __class__ cell"); |
|---|
| 7556 | n/a | return -1; |
|---|
| 7557 | n/a | } |
|---|
| 7558 | n/a | if (!PyType_Check(type)) { |
|---|
| 7559 | n/a | PyErr_Format(PyExc_RuntimeError, |
|---|
| 7560 | n/a | "super(): __class__ is not a type (%s)", |
|---|
| 7561 | n/a | Py_TYPE(type)->tp_name); |
|---|
| 7562 | n/a | return -1; |
|---|
| 7563 | n/a | } |
|---|
| 7564 | n/a | break; |
|---|
| 7565 | n/a | } |
|---|
| 7566 | n/a | } |
|---|
| 7567 | n/a | if (type == NULL) { |
|---|
| 7568 | n/a | PyErr_SetString(PyExc_RuntimeError, |
|---|
| 7569 | n/a | "super(): __class__ cell not found"); |
|---|
| 7570 | n/a | return -1; |
|---|
| 7571 | n/a | } |
|---|
| 7572 | n/a | } |
|---|
| 7573 | n/a | |
|---|
| 7574 | n/a | if (obj == Py_None) |
|---|
| 7575 | n/a | obj = NULL; |
|---|
| 7576 | n/a | if (obj != NULL) { |
|---|
| 7577 | n/a | obj_type = supercheck(type, obj); |
|---|
| 7578 | n/a | if (obj_type == NULL) |
|---|
| 7579 | n/a | return -1; |
|---|
| 7580 | n/a | Py_INCREF(obj); |
|---|
| 7581 | n/a | } |
|---|
| 7582 | n/a | Py_INCREF(type); |
|---|
| 7583 | n/a | Py_XSETREF(su->type, type); |
|---|
| 7584 | n/a | Py_XSETREF(su->obj, obj); |
|---|
| 7585 | n/a | Py_XSETREF(su->obj_type, obj_type); |
|---|
| 7586 | n/a | return 0; |
|---|
| 7587 | n/a | } |
|---|
| 7588 | n/a | |
|---|
| 7589 | n/a | PyDoc_STRVAR(super_doc, |
|---|
| 7590 | n/a | "super() -> same as super(__class__, <first argument>)\n" |
|---|
| 7591 | n/a | "super(type) -> unbound super object\n" |
|---|
| 7592 | n/a | "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" |
|---|
| 7593 | n/a | "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" |
|---|
| 7594 | n/a | "Typical use to call a cooperative superclass method:\n" |
|---|
| 7595 | n/a | "class C(B):\n" |
|---|
| 7596 | n/a | " def meth(self, arg):\n" |
|---|
| 7597 | n/a | " super().meth(arg)\n" |
|---|
| 7598 | n/a | "This works for class methods too:\n" |
|---|
| 7599 | n/a | "class C(B):\n" |
|---|
| 7600 | n/a | " @classmethod\n" |
|---|
| 7601 | n/a | " def cmeth(cls, arg):\n" |
|---|
| 7602 | n/a | " super().cmeth(arg)\n"); |
|---|
| 7603 | n/a | |
|---|
| 7604 | n/a | static int |
|---|
| 7605 | n/a | super_traverse(PyObject *self, visitproc visit, void *arg) |
|---|
| 7606 | n/a | { |
|---|
| 7607 | n/a | superobject *su = (superobject *)self; |
|---|
| 7608 | n/a | |
|---|
| 7609 | n/a | Py_VISIT(su->obj); |
|---|
| 7610 | n/a | Py_VISIT(su->type); |
|---|
| 7611 | n/a | Py_VISIT(su->obj_type); |
|---|
| 7612 | n/a | |
|---|
| 7613 | n/a | return 0; |
|---|
| 7614 | n/a | } |
|---|
| 7615 | n/a | |
|---|
| 7616 | n/a | PyTypeObject PySuper_Type = { |
|---|
| 7617 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|---|
| 7618 | n/a | "super", /* tp_name */ |
|---|
| 7619 | n/a | sizeof(superobject), /* tp_basicsize */ |
|---|
| 7620 | n/a | 0, /* tp_itemsize */ |
|---|
| 7621 | n/a | /* methods */ |
|---|
| 7622 | n/a | super_dealloc, /* tp_dealloc */ |
|---|
| 7623 | n/a | 0, /* tp_print */ |
|---|
| 7624 | n/a | 0, /* tp_getattr */ |
|---|
| 7625 | n/a | 0, /* tp_setattr */ |
|---|
| 7626 | n/a | 0, /* tp_reserved */ |
|---|
| 7627 | n/a | super_repr, /* tp_repr */ |
|---|
| 7628 | n/a | 0, /* tp_as_number */ |
|---|
| 7629 | n/a | 0, /* tp_as_sequence */ |
|---|
| 7630 | n/a | 0, /* tp_as_mapping */ |
|---|
| 7631 | n/a | 0, /* tp_hash */ |
|---|
| 7632 | n/a | 0, /* tp_call */ |
|---|
| 7633 | n/a | 0, /* tp_str */ |
|---|
| 7634 | n/a | super_getattro, /* tp_getattro */ |
|---|
| 7635 | n/a | 0, /* tp_setattro */ |
|---|
| 7636 | n/a | 0, /* tp_as_buffer */ |
|---|
| 7637 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
|---|
| 7638 | n/a | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 7639 | n/a | super_doc, /* tp_doc */ |
|---|
| 7640 | n/a | super_traverse, /* tp_traverse */ |
|---|
| 7641 | n/a | 0, /* tp_clear */ |
|---|
| 7642 | n/a | 0, /* tp_richcompare */ |
|---|
| 7643 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 7644 | n/a | 0, /* tp_iter */ |
|---|
| 7645 | n/a | 0, /* tp_iternext */ |
|---|
| 7646 | n/a | 0, /* tp_methods */ |
|---|
| 7647 | n/a | super_members, /* tp_members */ |
|---|
| 7648 | n/a | 0, /* tp_getset */ |
|---|
| 7649 | n/a | 0, /* tp_base */ |
|---|
| 7650 | n/a | 0, /* tp_dict */ |
|---|
| 7651 | n/a | super_descr_get, /* tp_descr_get */ |
|---|
| 7652 | n/a | 0, /* tp_descr_set */ |
|---|
| 7653 | n/a | 0, /* tp_dictoffset */ |
|---|
| 7654 | n/a | super_init, /* tp_init */ |
|---|
| 7655 | n/a | PyType_GenericAlloc, /* tp_alloc */ |
|---|
| 7656 | n/a | PyType_GenericNew, /* tp_new */ |
|---|
| 7657 | n/a | PyObject_GC_Del, /* tp_free */ |
|---|
| 7658 | n/a | }; |
|---|